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> 58690555a1Sachartre #include <vm/seg_map.h> 591ae08745Sheppo 60*bae9e67eSachartre #define ONE_TERABYTE (1ULL << 40) 61*bae9e67eSachartre 621ae08745Sheppo /* Virtual disk server initialization flags */ 63d10e4ef2Snarayan #define VDS_LDI 0x01 64d10e4ef2Snarayan #define VDS_MDEG 0x02 651ae08745Sheppo 661ae08745Sheppo /* Virtual disk server tunable parameters */ 673c96341aSnarayan #define VDS_RETRIES 5 683c96341aSnarayan #define VDS_LDC_DELAY 1000 /* 1 msecs */ 693c96341aSnarayan #define VDS_DEV_DELAY 10000000 /* 10 secs */ 701ae08745Sheppo #define VDS_NCHAINS 32 711ae08745Sheppo 721ae08745Sheppo /* Identification parameters for MD, synthetic dkio(7i) structures, etc. */ 731ae08745Sheppo #define VDS_NAME "virtual-disk-server" 741ae08745Sheppo 751ae08745Sheppo #define VD_NAME "vd" 761ae08745Sheppo #define VD_VOLUME_NAME "vdisk" 771ae08745Sheppo #define VD_ASCIILABEL "Virtual Disk" 781ae08745Sheppo 791ae08745Sheppo #define VD_CHANNEL_ENDPOINT "channel-endpoint" 801ae08745Sheppo #define VD_ID_PROP "id" 811ae08745Sheppo #define VD_BLOCK_DEVICE_PROP "vds-block-device" 82047ba61eSachartre #define VD_BLOCK_DEVICE_OPTS "vds-block-device-opts" 83445b4c2eSsb155480 #define VD_REG_PROP "reg" 841ae08745Sheppo 851ae08745Sheppo /* Virtual disk initialization flags */ 863c96341aSnarayan #define VD_DISK_READY 0x01 873c96341aSnarayan #define VD_LOCKING 0x02 883c96341aSnarayan #define VD_LDC 0x04 893c96341aSnarayan #define VD_DRING 0x08 903c96341aSnarayan #define VD_SID 0x10 913c96341aSnarayan #define VD_SEQ_NUM 0x20 92047ba61eSachartre #define VD_SETUP_ERROR 0x40 931ae08745Sheppo 94eba0cb4eSachartre /* Flags for writing to a vdisk which is a file */ 95eba0cb4eSachartre #define VD_FILE_WRITE_FLAGS SM_ASYNC 96eba0cb4eSachartre 9787a7269eSachartre /* Number of backup labels */ 9887a7269eSachartre #define VD_FILE_NUM_BACKUP 5 9987a7269eSachartre 10087a7269eSachartre /* Timeout for SCSI I/O */ 10187a7269eSachartre #define VD_SCSI_RDWR_TIMEOUT 30 /* 30 secs */ 10287a7269eSachartre 103edcc0754Sachartre /* Maximum number of logical partitions */ 104edcc0754Sachartre #define VD_MAXPART (NDKMAP + 1) 105edcc0754Sachartre 1061ae08745Sheppo /* 1071ae08745Sheppo * By Solaris convention, slice/partition 2 represents the entire disk; 1081ae08745Sheppo * unfortunately, this convention does not appear to be codified. 1091ae08745Sheppo */ 1101ae08745Sheppo #define VD_ENTIRE_DISK_SLICE 2 1111ae08745Sheppo 112*bae9e67eSachartre /* Logical block address for EFI */ 113*bae9e67eSachartre #define VD_EFI_LBA_GPT 1 /* LBA of the GPT */ 114*bae9e67eSachartre #define VD_EFI_LBA_GPE 2 /* LBA of the GPE */ 115*bae9e67eSachartre 1168fce2fd6Sachartre /* Driver types */ 1178fce2fd6Sachartre typedef enum vd_driver { 1188fce2fd6Sachartre VD_DRIVER_UNKNOWN = 0, /* driver type unknown */ 1198fce2fd6Sachartre VD_DRIVER_DISK, /* disk driver */ 1208fce2fd6Sachartre VD_DRIVER_VOLUME /* volume driver */ 1218fce2fd6Sachartre } vd_driver_t; 1228fce2fd6Sachartre 1238fce2fd6Sachartre #define VD_DRIVER_NAME_LEN 64 1248fce2fd6Sachartre 1258fce2fd6Sachartre #define VDS_NUM_DRIVERS (sizeof (vds_driver_types) / sizeof (vd_driver_type_t)) 1268fce2fd6Sachartre 1278fce2fd6Sachartre typedef struct vd_driver_type { 1288fce2fd6Sachartre char name[VD_DRIVER_NAME_LEN]; /* driver name */ 1298fce2fd6Sachartre vd_driver_t type; /* driver type (disk or volume) */ 1308fce2fd6Sachartre } vd_driver_type_t; 1318fce2fd6Sachartre 1328fce2fd6Sachartre /* 1338fce2fd6Sachartre * There is no reliable way to determine if a device is representing a disk 1348fce2fd6Sachartre * or a volume, especially with pseudo devices. So we maintain a list of well 1358fce2fd6Sachartre * known drivers and the type of device they represent (either a disk or a 1368fce2fd6Sachartre * volume). 1378fce2fd6Sachartre * 1388fce2fd6Sachartre * The list can be extended by adding a "driver-type-list" entry in vds.conf 1398fce2fd6Sachartre * with the following syntax: 1408fce2fd6Sachartre * 1418fce2fd6Sachartre * driver-type-list="<driver>:<type>", ... ,"<driver>:<type>"; 1428fce2fd6Sachartre * 1438fce2fd6Sachartre * Where: 1448fce2fd6Sachartre * <driver> is the name of a driver (limited to 64 characters) 1458fce2fd6Sachartre * <type> is either the string "disk" or "volume" 1468fce2fd6Sachartre * 1478fce2fd6Sachartre * Invalid entries in "driver-type-list" will be ignored. 1488fce2fd6Sachartre * 1498fce2fd6Sachartre * For example, the following line in vds.conf: 1508fce2fd6Sachartre * 1518fce2fd6Sachartre * driver-type-list="foo:disk","bar:volume"; 1528fce2fd6Sachartre * 1538fce2fd6Sachartre * defines that "foo" is a disk driver, and driver "bar" is a volume driver. 1548fce2fd6Sachartre * 1558fce2fd6Sachartre * When a list is defined in vds.conf, it is checked before the built-in list 1568fce2fd6Sachartre * (vds_driver_types[]) so that any definition from this list can be overriden 1578fce2fd6Sachartre * using vds.conf. 1588fce2fd6Sachartre */ 1598fce2fd6Sachartre vd_driver_type_t vds_driver_types[] = { 1608fce2fd6Sachartre { "dad", VD_DRIVER_DISK }, /* Solaris */ 1618fce2fd6Sachartre { "did", VD_DRIVER_DISK }, /* Sun Cluster */ 1625b98b509Sachartre { "emcp", VD_DRIVER_DISK }, /* EMC Powerpath */ 1638fce2fd6Sachartre { "lofi", VD_DRIVER_VOLUME }, /* Solaris */ 1648fce2fd6Sachartre { "md", VD_DRIVER_VOLUME }, /* Solaris - SVM */ 1658fce2fd6Sachartre { "sd", VD_DRIVER_DISK }, /* Solaris */ 1668fce2fd6Sachartre { "ssd", VD_DRIVER_DISK }, /* Solaris */ 1678fce2fd6Sachartre { "vdc", VD_DRIVER_DISK }, /* Solaris */ 1688fce2fd6Sachartre { "vxdmp", VD_DRIVER_DISK }, /* Veritas */ 1698fce2fd6Sachartre { "vxio", VD_DRIVER_VOLUME }, /* Veritas - VxVM */ 1708fce2fd6Sachartre { "zfs", VD_DRIVER_VOLUME } /* Solaris */ 1718fce2fd6Sachartre }; 1728fce2fd6Sachartre 1731ae08745Sheppo /* Return a cpp token as a string */ 1741ae08745Sheppo #define STRINGIZE(token) #token 1751ae08745Sheppo 1761ae08745Sheppo /* 1771ae08745Sheppo * Print a message prefixed with the current function name to the message log 1781ae08745Sheppo * (and optionally to the console for verbose boots); these macros use cpp's 1791ae08745Sheppo * concatenation of string literals and C99 variable-length-argument-list 1801ae08745Sheppo * macros 1811ae08745Sheppo */ 1821ae08745Sheppo #define PRN(...) _PRN("?%s(): "__VA_ARGS__, "") 1831ae08745Sheppo #define _PRN(format, ...) \ 1841ae08745Sheppo cmn_err(CE_CONT, format"%s", __func__, __VA_ARGS__) 1851ae08745Sheppo 1861ae08745Sheppo /* Return a pointer to the "i"th vdisk dring element */ 1871ae08745Sheppo #define VD_DRING_ELEM(i) ((vd_dring_entry_t *)(void *) \ 1881ae08745Sheppo (vd->dring + (i)*vd->descriptor_size)) 1891ae08745Sheppo 1901ae08745Sheppo /* Return the virtual disk client's type as a string (for use in messages) */ 1911ae08745Sheppo #define VD_CLIENT(vd) \ 1921ae08745Sheppo (((vd)->xfer_mode == VIO_DESC_MODE) ? "in-band client" : \ 193f0ca1d9aSsb155480 (((vd)->xfer_mode == VIO_DRING_MODE_V1_0) ? "dring client" : \ 1941ae08745Sheppo (((vd)->xfer_mode == 0) ? "null client" : \ 1951ae08745Sheppo "unsupported client"))) 1961ae08745Sheppo 197690555a1Sachartre /* Read disk label from a disk on file */ 198690555a1Sachartre #define VD_FILE_LABEL_READ(vd, labelp) \ 19987a7269eSachartre vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)labelp, \ 200690555a1Sachartre 0, sizeof (struct dk_label)) 201690555a1Sachartre 202690555a1Sachartre /* Write disk label to a disk on file */ 203690555a1Sachartre #define VD_FILE_LABEL_WRITE(vd, labelp) \ 20487a7269eSachartre vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)labelp, \ 205690555a1Sachartre 0, sizeof (struct dk_label)) 206690555a1Sachartre 2072f5224aeSachartre /* Message for disk access rights reset failure */ 2082f5224aeSachartre #define VD_RESET_ACCESS_FAILURE_MSG \ 2092f5224aeSachartre "Fail to reset disk access rights for disk %s" 2102f5224aeSachartre 211445b4c2eSsb155480 /* 212445b4c2eSsb155480 * Specification of an MD node passed to the MDEG to filter any 213445b4c2eSsb155480 * 'vport' nodes that do not belong to the specified node. This 214445b4c2eSsb155480 * template is copied for each vds instance and filled in with 215445b4c2eSsb155480 * the appropriate 'cfg-handle' value before being passed to the MDEG. 216445b4c2eSsb155480 */ 217445b4c2eSsb155480 static mdeg_prop_spec_t vds_prop_template[] = { 218445b4c2eSsb155480 { MDET_PROP_STR, "name", VDS_NAME }, 219445b4c2eSsb155480 { MDET_PROP_VAL, "cfg-handle", NULL }, 220445b4c2eSsb155480 { MDET_LIST_END, NULL, NULL } 221445b4c2eSsb155480 }; 222445b4c2eSsb155480 223445b4c2eSsb155480 #define VDS_SET_MDEG_PROP_INST(specp, val) (specp)[1].ps_val = (val); 224445b4c2eSsb155480 225445b4c2eSsb155480 /* 226445b4c2eSsb155480 * Matching criteria passed to the MDEG to register interest 227445b4c2eSsb155480 * in changes to 'virtual-device-port' nodes identified by their 228445b4c2eSsb155480 * 'id' property. 229445b4c2eSsb155480 */ 230445b4c2eSsb155480 static md_prop_match_t vd_prop_match[] = { 231445b4c2eSsb155480 { MDET_PROP_VAL, VD_ID_PROP }, 232445b4c2eSsb155480 { MDET_LIST_END, NULL } 233445b4c2eSsb155480 }; 234445b4c2eSsb155480 235445b4c2eSsb155480 static mdeg_node_match_t vd_match = {"virtual-device-port", 236445b4c2eSsb155480 vd_prop_match}; 237445b4c2eSsb155480 238047ba61eSachartre /* 239047ba61eSachartre * Options for the VD_BLOCK_DEVICE_OPTS property. 240047ba61eSachartre */ 241047ba61eSachartre #define VD_OPT_RDONLY 0x1 /* read-only */ 242047ba61eSachartre #define VD_OPT_SLICE 0x2 /* single slice */ 243047ba61eSachartre #define VD_OPT_EXCLUSIVE 0x4 /* exclusive access */ 244047ba61eSachartre 245047ba61eSachartre #define VD_OPTION_NLEN 128 246047ba61eSachartre 247047ba61eSachartre typedef struct vd_option { 248047ba61eSachartre char vdo_name[VD_OPTION_NLEN]; 249047ba61eSachartre uint64_t vdo_value; 250047ba61eSachartre } vd_option_t; 251047ba61eSachartre 252047ba61eSachartre vd_option_t vd_bdev_options[] = { 253047ba61eSachartre { "ro", VD_OPT_RDONLY }, 254047ba61eSachartre { "slice", VD_OPT_SLICE }, 255047ba61eSachartre { "excl", VD_OPT_EXCLUSIVE } 256047ba61eSachartre }; 257047ba61eSachartre 2581ae08745Sheppo /* Debugging macros */ 2591ae08745Sheppo #ifdef DEBUG 2603af08d82Slm66018 2613af08d82Slm66018 static int vd_msglevel = 0; 2623af08d82Slm66018 2631ae08745Sheppo #define PR0 if (vd_msglevel > 0) PRN 2641ae08745Sheppo #define PR1 if (vd_msglevel > 1) PRN 2651ae08745Sheppo #define PR2 if (vd_msglevel > 2) PRN 2661ae08745Sheppo 2671ae08745Sheppo #define VD_DUMP_DRING_ELEM(elem) \ 2683c96341aSnarayan PR0("dst:%x op:%x st:%u nb:%lx addr:%lx ncook:%u\n", \ 2691ae08745Sheppo elem->hdr.dstate, \ 2701ae08745Sheppo elem->payload.operation, \ 2711ae08745Sheppo elem->payload.status, \ 2721ae08745Sheppo elem->payload.nbytes, \ 2731ae08745Sheppo elem->payload.addr, \ 2741ae08745Sheppo elem->payload.ncookies); 2751ae08745Sheppo 2763af08d82Slm66018 char * 2773af08d82Slm66018 vd_decode_state(int state) 2783af08d82Slm66018 { 2793af08d82Slm66018 char *str; 2803af08d82Slm66018 2813af08d82Slm66018 #define CASE_STATE(_s) case _s: str = #_s; break; 2823af08d82Slm66018 2833af08d82Slm66018 switch (state) { 2843af08d82Slm66018 CASE_STATE(VD_STATE_INIT) 2853af08d82Slm66018 CASE_STATE(VD_STATE_VER) 2863af08d82Slm66018 CASE_STATE(VD_STATE_ATTR) 2873af08d82Slm66018 CASE_STATE(VD_STATE_DRING) 2883af08d82Slm66018 CASE_STATE(VD_STATE_RDX) 2893af08d82Slm66018 CASE_STATE(VD_STATE_DATA) 2903af08d82Slm66018 default: str = "unknown"; break; 2913af08d82Slm66018 } 2923af08d82Slm66018 2933af08d82Slm66018 #undef CASE_STATE 2943af08d82Slm66018 2953af08d82Slm66018 return (str); 2963af08d82Slm66018 } 2973af08d82Slm66018 2983af08d82Slm66018 void 2993af08d82Slm66018 vd_decode_tag(vio_msg_t *msg) 3003af08d82Slm66018 { 3013af08d82Slm66018 char *tstr, *sstr, *estr; 3023af08d82Slm66018 3033af08d82Slm66018 #define CASE_TYPE(_s) case _s: tstr = #_s; break; 3043af08d82Slm66018 3053af08d82Slm66018 switch (msg->tag.vio_msgtype) { 3063af08d82Slm66018 CASE_TYPE(VIO_TYPE_CTRL) 3073af08d82Slm66018 CASE_TYPE(VIO_TYPE_DATA) 3083af08d82Slm66018 CASE_TYPE(VIO_TYPE_ERR) 3093af08d82Slm66018 default: tstr = "unknown"; break; 3103af08d82Slm66018 } 3113af08d82Slm66018 3123af08d82Slm66018 #undef CASE_TYPE 3133af08d82Slm66018 3143af08d82Slm66018 #define CASE_SUBTYPE(_s) case _s: sstr = #_s; break; 3153af08d82Slm66018 3163af08d82Slm66018 switch (msg->tag.vio_subtype) { 3173af08d82Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_INFO) 3183af08d82Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_ACK) 3193af08d82Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_NACK) 3203af08d82Slm66018 default: sstr = "unknown"; break; 3213af08d82Slm66018 } 3223af08d82Slm66018 3233af08d82Slm66018 #undef CASE_SUBTYPE 3243af08d82Slm66018 3253af08d82Slm66018 #define CASE_ENV(_s) case _s: estr = #_s; break; 3263af08d82Slm66018 3273af08d82Slm66018 switch (msg->tag.vio_subtype_env) { 3283af08d82Slm66018 CASE_ENV(VIO_VER_INFO) 3293af08d82Slm66018 CASE_ENV(VIO_ATTR_INFO) 3303af08d82Slm66018 CASE_ENV(VIO_DRING_REG) 3313af08d82Slm66018 CASE_ENV(VIO_DRING_UNREG) 3323af08d82Slm66018 CASE_ENV(VIO_RDX) 3333af08d82Slm66018 CASE_ENV(VIO_PKT_DATA) 3343af08d82Slm66018 CASE_ENV(VIO_DESC_DATA) 3353af08d82Slm66018 CASE_ENV(VIO_DRING_DATA) 3363af08d82Slm66018 default: estr = "unknown"; break; 3373af08d82Slm66018 } 3383af08d82Slm66018 3393af08d82Slm66018 #undef CASE_ENV 3403af08d82Slm66018 3413af08d82Slm66018 PR1("(%x/%x/%x) message : (%s/%s/%s)", 3423af08d82Slm66018 msg->tag.vio_msgtype, msg->tag.vio_subtype, 3433af08d82Slm66018 msg->tag.vio_subtype_env, tstr, sstr, estr); 3443af08d82Slm66018 } 3453af08d82Slm66018 3461ae08745Sheppo #else /* !DEBUG */ 3473af08d82Slm66018 3481ae08745Sheppo #define PR0(...) 3491ae08745Sheppo #define PR1(...) 3501ae08745Sheppo #define PR2(...) 3511ae08745Sheppo 3521ae08745Sheppo #define VD_DUMP_DRING_ELEM(elem) 3531ae08745Sheppo 3543af08d82Slm66018 #define vd_decode_state(_s) (NULL) 3553af08d82Slm66018 #define vd_decode_tag(_s) (NULL) 3563af08d82Slm66018 3571ae08745Sheppo #endif /* DEBUG */ 3581ae08745Sheppo 3591ae08745Sheppo 360d10e4ef2Snarayan /* 361d10e4ef2Snarayan * Soft state structure for a vds instance 362d10e4ef2Snarayan */ 3631ae08745Sheppo typedef struct vds { 3641ae08745Sheppo uint_t initialized; /* driver inst initialization flags */ 3651ae08745Sheppo dev_info_t *dip; /* driver inst devinfo pointer */ 3661ae08745Sheppo ldi_ident_t ldi_ident; /* driver's identifier for LDI */ 3671ae08745Sheppo mod_hash_t *vd_table; /* table of virtual disks served */ 368445b4c2eSsb155480 mdeg_node_spec_t *ispecp; /* mdeg node specification */ 3691ae08745Sheppo mdeg_handle_t mdeg; /* handle for MDEG operations */ 3708fce2fd6Sachartre vd_driver_type_t *driver_types; /* extra driver types (from vds.conf) */ 3718fce2fd6Sachartre int num_drivers; /* num of extra driver types */ 3721ae08745Sheppo } vds_t; 3731ae08745Sheppo 374d10e4ef2Snarayan /* 375d10e4ef2Snarayan * Types of descriptor-processing tasks 376d10e4ef2Snarayan */ 377d10e4ef2Snarayan typedef enum vd_task_type { 378d10e4ef2Snarayan VD_NONFINAL_RANGE_TASK, /* task for intermediate descriptor in range */ 379d10e4ef2Snarayan VD_FINAL_RANGE_TASK, /* task for last in a range of descriptors */ 380d10e4ef2Snarayan } vd_task_type_t; 381d10e4ef2Snarayan 382d10e4ef2Snarayan /* 383d10e4ef2Snarayan * Structure describing the task for processing a descriptor 384d10e4ef2Snarayan */ 385d10e4ef2Snarayan typedef struct vd_task { 386d10e4ef2Snarayan struct vd *vd; /* vd instance task is for */ 387d10e4ef2Snarayan vd_task_type_t type; /* type of descriptor task */ 388d10e4ef2Snarayan int index; /* dring elem index for task */ 389d10e4ef2Snarayan vio_msg_t *msg; /* VIO message task is for */ 390d10e4ef2Snarayan size_t msglen; /* length of message content */ 391d10e4ef2Snarayan vd_dring_payload_t *request; /* request task will perform */ 392d10e4ef2Snarayan struct buf buf; /* buf(9s) for I/O request */ 3934bac2208Snarayan ldc_mem_handle_t mhdl; /* task memory handle */ 394205eeb1aSlm66018 int status; /* status of processing task */ 395205eeb1aSlm66018 int (*completef)(struct vd_task *task); /* completion func ptr */ 396d10e4ef2Snarayan } vd_task_t; 397d10e4ef2Snarayan 398d10e4ef2Snarayan /* 399d10e4ef2Snarayan * Soft state structure for a virtual disk instance 400d10e4ef2Snarayan */ 4011ae08745Sheppo typedef struct vd { 4021ae08745Sheppo uint_t initialized; /* vdisk initialization flags */ 40317cadca8Slm66018 uint64_t operations; /* bitmask of VD_OPs exported */ 40417cadca8Slm66018 vio_ver_t version; /* ver negotiated with client */ 4051ae08745Sheppo vds_t *vds; /* server for this vdisk */ 406d10e4ef2Snarayan ddi_taskq_t *startq; /* queue for I/O start tasks */ 407d10e4ef2Snarayan ddi_taskq_t *completionq; /* queue for completion tasks */ 4081ae08745Sheppo ldi_handle_t ldi_handle[V_NUMPAR]; /* LDI slice handles */ 4093c96341aSnarayan char device_path[MAXPATHLEN + 1]; /* vdisk device */ 4101ae08745Sheppo dev_t dev[V_NUMPAR]; /* dev numbers for slices */ 411047ba61eSachartre int open_flags; /* open flags */ 412*bae9e67eSachartre uint_t nslices; /* number of slices we export */ 4131ae08745Sheppo size_t vdisk_size; /* number of blocks in vdisk */ 41417cadca8Slm66018 size_t vdisk_block_size; /* size of each vdisk block */ 4151ae08745Sheppo vd_disk_type_t vdisk_type; /* slice or entire disk */ 4164bac2208Snarayan vd_disk_label_t vdisk_label; /* EFI or VTOC label */ 41717cadca8Slm66018 vd_media_t vdisk_media; /* media type of backing dev. */ 41817cadca8Slm66018 boolean_t is_atapi_dev; /* Is this an IDE CD-ROM dev? */ 419e1ebb9ecSlm66018 ushort_t max_xfer_sz; /* max xfer size in DEV_BSIZE */ 42017cadca8Slm66018 size_t block_size; /* blk size of actual device */ 4218fce2fd6Sachartre boolean_t volume; /* is vDisk backed by volume */ 42217cadca8Slm66018 boolean_t file; /* is vDisk backed by a file? */ 4232f5224aeSachartre boolean_t scsi; /* is vDisk backed by scsi? */ 4243c96341aSnarayan vnode_t *file_vnode; /* file vnode */ 4253c96341aSnarayan size_t file_size; /* file size */ 42687a7269eSachartre ddi_devid_t file_devid; /* devid for disk image */ 427edcc0754Sachartre int efi_reserved; /* EFI reserved slice */ 428*bae9e67eSachartre caddr_t flabel; /* fake label for slice type */ 429*bae9e67eSachartre uint_t flabel_size; /* fake label size */ 430*bae9e67eSachartre uint_t flabel_limit; /* limit of the fake label */ 4311ae08745Sheppo struct dk_geom dk_geom; /* synthetic for slice type */ 4321ae08745Sheppo struct vtoc vtoc; /* synthetic for slice type */ 433edcc0754Sachartre vd_slice_t slices[VD_MAXPART]; /* logical partitions */ 4342f5224aeSachartre boolean_t ownership; /* disk ownership status */ 4351ae08745Sheppo ldc_status_t ldc_state; /* LDC connection state */ 4361ae08745Sheppo ldc_handle_t ldc_handle; /* handle for LDC comm */ 4371ae08745Sheppo size_t max_msglen; /* largest LDC message len */ 4381ae08745Sheppo vd_state_t state; /* client handshake state */ 4391ae08745Sheppo uint8_t xfer_mode; /* transfer mode with client */ 4401ae08745Sheppo uint32_t sid; /* client's session ID */ 4411ae08745Sheppo uint64_t seq_num; /* message sequence number */ 4421ae08745Sheppo uint64_t dring_ident; /* identifier of dring */ 4431ae08745Sheppo ldc_dring_handle_t dring_handle; /* handle for dring ops */ 4441ae08745Sheppo uint32_t descriptor_size; /* num bytes in desc */ 4451ae08745Sheppo uint32_t dring_len; /* number of dring elements */ 4461ae08745Sheppo caddr_t dring; /* address of dring */ 4473af08d82Slm66018 caddr_t vio_msgp; /* vio msg staging buffer */ 448d10e4ef2Snarayan vd_task_t inband_task; /* task for inband descriptor */ 449d10e4ef2Snarayan vd_task_t *dring_task; /* tasks dring elements */ 450d10e4ef2Snarayan 451d10e4ef2Snarayan kmutex_t lock; /* protects variables below */ 452d10e4ef2Snarayan boolean_t enabled; /* is vdisk enabled? */ 453d10e4ef2Snarayan boolean_t reset_state; /* reset connection state? */ 454d10e4ef2Snarayan boolean_t reset_ldc; /* reset LDC channel? */ 4551ae08745Sheppo } vd_t; 4561ae08745Sheppo 457*bae9e67eSachartre /* 458*bae9e67eSachartre * Macros to manipulate the fake label (flabel) for single slice disks. 459*bae9e67eSachartre * 460*bae9e67eSachartre * If we fake a VTOC label then the fake label consists of only one block 461*bae9e67eSachartre * containing the VTOC label (struct dk_label). 462*bae9e67eSachartre * 463*bae9e67eSachartre * If we fake an EFI label then the fake label consists of a blank block 464*bae9e67eSachartre * followed by a GPT (efi_gpt_t) and a GPE (efi_gpe_t). 465*bae9e67eSachartre * 466*bae9e67eSachartre */ 467*bae9e67eSachartre #define VD_LABEL_VTOC_SIZE \ 468*bae9e67eSachartre P2ROUNDUP(sizeof (struct dk_label), DEV_BSIZE) 469*bae9e67eSachartre 470*bae9e67eSachartre #define VD_LABEL_EFI_SIZE \ 471*bae9e67eSachartre P2ROUNDUP(DEV_BSIZE + sizeof (efi_gpt_t) + \ 472*bae9e67eSachartre sizeof (efi_gpe_t) * VD_MAXPART, DEV_BSIZE) 473*bae9e67eSachartre 474*bae9e67eSachartre #define VD_LABEL_VTOC(vd) \ 475*bae9e67eSachartre ((struct dk_label *)((vd)->flabel)) 476*bae9e67eSachartre 477*bae9e67eSachartre #define VD_LABEL_EFI_GPT(vd) \ 478*bae9e67eSachartre ((efi_gpt_t *)((vd)->flabel + DEV_BSIZE)) 479*bae9e67eSachartre #define VD_LABEL_EFI_GPE(vd) \ 480*bae9e67eSachartre ((efi_gpe_t *)((vd)->flabel + DEV_BSIZE + sizeof (efi_gpt_t))) 481*bae9e67eSachartre 482*bae9e67eSachartre 4831ae08745Sheppo typedef struct vds_operation { 4843af08d82Slm66018 char *namep; 4851ae08745Sheppo uint8_t operation; 486d10e4ef2Snarayan int (*start)(vd_task_t *task); 487205eeb1aSlm66018 int (*complete)(vd_task_t *task); 4881ae08745Sheppo } vds_operation_t; 4891ae08745Sheppo 4900a55fbb7Slm66018 typedef struct vd_ioctl { 4910a55fbb7Slm66018 uint8_t operation; /* vdisk operation */ 4920a55fbb7Slm66018 const char *operation_name; /* vdisk operation name */ 4930a55fbb7Slm66018 size_t nbytes; /* size of operation buffer */ 4940a55fbb7Slm66018 int cmd; /* corresponding ioctl cmd */ 4950a55fbb7Slm66018 const char *cmd_name; /* ioctl cmd name */ 4960a55fbb7Slm66018 void *arg; /* ioctl cmd argument */ 4970a55fbb7Slm66018 /* convert input vd_buf to output ioctl_arg */ 4982f5224aeSachartre int (*copyin)(void *vd_buf, size_t, void *ioctl_arg); 4990a55fbb7Slm66018 /* convert input ioctl_arg to output vd_buf */ 5000a55fbb7Slm66018 void (*copyout)(void *ioctl_arg, void *vd_buf); 501047ba61eSachartre /* write is true if the operation writes any data to the backend */ 502047ba61eSachartre boolean_t write; 5030a55fbb7Slm66018 } vd_ioctl_t; 5040a55fbb7Slm66018 5050a55fbb7Slm66018 /* Define trivial copyin/copyout conversion function flag */ 5062f5224aeSachartre #define VD_IDENTITY_IN ((int (*)(void *, size_t, void *))-1) 5072f5224aeSachartre #define VD_IDENTITY_OUT ((void (*)(void *, void *))-1) 5081ae08745Sheppo 5091ae08745Sheppo 5103c96341aSnarayan static int vds_ldc_retries = VDS_RETRIES; 5113af08d82Slm66018 static int vds_ldc_delay = VDS_LDC_DELAY; 5123c96341aSnarayan static int vds_dev_retries = VDS_RETRIES; 5133c96341aSnarayan static int vds_dev_delay = VDS_DEV_DELAY; 5141ae08745Sheppo static void *vds_state; 5151ae08745Sheppo 516eba0cb4eSachartre static uint_t vd_file_write_flags = VD_FILE_WRITE_FLAGS; 517eba0cb4eSachartre 51887a7269eSachartre static short vd_scsi_rdwr_timeout = VD_SCSI_RDWR_TIMEOUT; 5192f5224aeSachartre static int vd_scsi_debug = USCSI_SILENT; 5202f5224aeSachartre 5212f5224aeSachartre /* 5222f5224aeSachartre * Tunable to define the behavior of the service domain if the vdisk server 5232f5224aeSachartre * fails to reset disk exclusive access when a LDC channel is reset. When a 5242f5224aeSachartre * LDC channel is reset the vdisk server will try to reset disk exclusive 5252f5224aeSachartre * access by releasing any SCSI-2 reservation or resetting the disk. If these 5262f5224aeSachartre * actions fail then the default behavior (vd_reset_access_failure = 0) is to 5272f5224aeSachartre * print a warning message. This default behavior can be changed by setting 5282f5224aeSachartre * the vd_reset_access_failure variable to A_REBOOT (= 0x1) and that will 5292f5224aeSachartre * cause the service domain to reboot, or A_DUMP (= 0x5) and that will cause 5302f5224aeSachartre * the service domain to panic. In both cases, the reset of the service domain 5312f5224aeSachartre * should trigger a reset SCSI buses and hopefully clear any SCSI-2 reservation. 5322f5224aeSachartre */ 5332f5224aeSachartre static int vd_reset_access_failure = 0; 5342f5224aeSachartre 5352f5224aeSachartre /* 5362f5224aeSachartre * Tunable for backward compatibility. When this variable is set to B_TRUE, 5372f5224aeSachartre * all disk volumes (ZFS, SVM, VxvM volumes) will be exported as single 5382f5224aeSachartre * slice disks whether or not they have the "slice" option set. This is 5392f5224aeSachartre * to provide a simple backward compatibility mechanism when upgrading 5402f5224aeSachartre * the vds driver and using a domain configuration created before the 5412f5224aeSachartre * "slice" option was available. 5422f5224aeSachartre */ 5432f5224aeSachartre static boolean_t vd_volume_force_slice = B_FALSE; 54487a7269eSachartre 5450a55fbb7Slm66018 /* 54666cfcfbeSachartre * The label of disk images created with some earlier versions of the virtual 54766cfcfbeSachartre * disk software is not entirely correct and have an incorrect v_sanity field 54866cfcfbeSachartre * (usually 0) instead of VTOC_SANE. This creates a compatibility problem with 54966cfcfbeSachartre * these images because we are now validating that the disk label (and the 55066cfcfbeSachartre * sanity) is correct when a disk image is opened. 55166cfcfbeSachartre * 55266cfcfbeSachartre * This tunable is set to false to not validate the sanity field and ensure 55366cfcfbeSachartre * compatibility. If the tunable is set to true, we will do a strict checking 55466cfcfbeSachartre * of the sanity but this can create compatibility problems with old disk 55566cfcfbeSachartre * images. 55666cfcfbeSachartre */ 55766cfcfbeSachartre static boolean_t vd_file_validate_sanity = B_FALSE; 55866cfcfbeSachartre 55966cfcfbeSachartre /* 560*bae9e67eSachartre * When a backend is exported as a single-slice disk then we entirely fake 561*bae9e67eSachartre * its disk label. So it can be exported either with a VTOC label or with 562*bae9e67eSachartre * an EFI label. If vd_slice_label is set to VD_DISK_LABEL_VTOC then all 563*bae9e67eSachartre * single-slice disks will be exported with a VTOC label; and if it is set 564*bae9e67eSachartre * to VD_DISK_LABEL_EFI then all single-slice disks will be exported with 565*bae9e67eSachartre * an EFI label. 566*bae9e67eSachartre * 567*bae9e67eSachartre * If vd_slice_label is set to VD_DISK_LABEL_UNK and the backend is a disk 568*bae9e67eSachartre * or volume device then it will be exported with the same type of label as 569*bae9e67eSachartre * defined on the device. Otherwise if the backend is a file then it will 570*bae9e67eSachartre * exported with the disk label type set in the vd_file_slice_label variable. 571*bae9e67eSachartre * 572*bae9e67eSachartre * Note that if the backend size is greater than 1TB then it will always be 573*bae9e67eSachartre * exported with an EFI label no matter what the setting is. 574*bae9e67eSachartre */ 575*bae9e67eSachartre static vd_disk_label_t vd_slice_label = VD_DISK_LABEL_UNK; 576*bae9e67eSachartre 577*bae9e67eSachartre static vd_disk_label_t vd_file_slice_label = VD_DISK_LABEL_VTOC; 578*bae9e67eSachartre 579*bae9e67eSachartre /* 580*bae9e67eSachartre * Tunable for backward compatibility. If this variable is set to B_TRUE then 581*bae9e67eSachartre * single-slice disks are exported as disks with only one slice instead of 582*bae9e67eSachartre * faking a complete disk partitioning. 583*bae9e67eSachartre */ 584*bae9e67eSachartre static boolean_t vd_slice_single_slice = B_FALSE; 585*bae9e67eSachartre 586*bae9e67eSachartre /* 5870a55fbb7Slm66018 * Supported protocol version pairs, from highest (newest) to lowest (oldest) 5880a55fbb7Slm66018 * 5890a55fbb7Slm66018 * Each supported major version should appear only once, paired with (and only 5900a55fbb7Slm66018 * with) its highest supported minor version number (as the protocol requires 5910a55fbb7Slm66018 * supporting all lower minor version numbers as well) 5920a55fbb7Slm66018 */ 59317cadca8Slm66018 static const vio_ver_t vds_version[] = {{1, 1}}; 5940a55fbb7Slm66018 static const size_t vds_num_versions = 5950a55fbb7Slm66018 sizeof (vds_version)/sizeof (vds_version[0]); 5960a55fbb7Slm66018 5973af08d82Slm66018 static void vd_free_dring_task(vd_t *vdp); 5983c96341aSnarayan static int vd_setup_vd(vd_t *vd); 599047ba61eSachartre static int vd_setup_single_slice_disk(vd_t *vd); 6002f5224aeSachartre static int vd_setup_mediainfo(vd_t *vd); 6013c96341aSnarayan static boolean_t vd_enabled(vd_t *vd); 60278fcd0a1Sachartre static ushort_t vd_lbl2cksum(struct dk_label *label); 60378fcd0a1Sachartre static int vd_file_validate_geometry(vd_t *vd); 60417cadca8Slm66018 static boolean_t vd_file_is_iso_image(vd_t *vd); 60517cadca8Slm66018 static void vd_set_exported_operations(vd_t *vd); 6062f5224aeSachartre static void vd_reset_access(vd_t *vd); 607edcc0754Sachartre static int vd_backend_ioctl(vd_t *vd, int cmd, caddr_t arg); 608edcc0754Sachartre static int vds_efi_alloc_and_read(vd_t *, efi_gpt_t **, efi_gpe_t **); 609edcc0754Sachartre static void vds_efi_free(vd_t *, efi_gpt_t *, efi_gpe_t *); 6108fce2fd6Sachartre static void vds_driver_types_free(vds_t *vds); 611*bae9e67eSachartre static void vd_vtocgeom_to_label(struct vtoc *vtoc, struct dk_geom *geom, 612*bae9e67eSachartre struct dk_label *label); 613*bae9e67eSachartre static void vd_label_to_vtocgeom(struct dk_label *label, struct vtoc *vtoc, 614*bae9e67eSachartre struct dk_geom *geom); 615*bae9e67eSachartre static boolean_t vd_slice_geom_isvalid(vd_t *vd, struct dk_geom *geom); 616*bae9e67eSachartre static boolean_t vd_slice_vtoc_isvalid(vd_t *vd, struct vtoc *vtoc); 617*bae9e67eSachartre 618*bae9e67eSachartre extern int is_pseudo_device(dev_info_t *); 619*bae9e67eSachartre 620*bae9e67eSachartre /* 621*bae9e67eSachartre * Function: 622*bae9e67eSachartre * vd_get_readable_size 623*bae9e67eSachartre * 624*bae9e67eSachartre * Description: 625*bae9e67eSachartre * Convert a given size in bytes to a human readable format in 626*bae9e67eSachartre * kilobytes, megabytes, gigabytes or terabytes. 627*bae9e67eSachartre * 628*bae9e67eSachartre * Parameters: 629*bae9e67eSachartre * full_size - the size to convert in bytes. 630*bae9e67eSachartre * size - the converted size. 631*bae9e67eSachartre * unit - the unit of the converted size: 'K' (kilobyte), 632*bae9e67eSachartre * 'M' (Megabyte), 'G' (Gigabyte), 'T' (Terabyte). 633*bae9e67eSachartre * 634*bae9e67eSachartre * Return Code: 635*bae9e67eSachartre * none 636*bae9e67eSachartre */ 637*bae9e67eSachartre void 638*bae9e67eSachartre vd_get_readable_size(size_t full_size, size_t *size, char *unit) 639*bae9e67eSachartre { 640*bae9e67eSachartre if (full_size < (1ULL << 20)) { 641*bae9e67eSachartre *size = full_size >> 10; 642*bae9e67eSachartre *unit = 'K'; /* Kilobyte */ 643*bae9e67eSachartre } else if (full_size < (1ULL << 30)) { 644*bae9e67eSachartre *size = full_size >> 20; 645*bae9e67eSachartre *unit = 'M'; /* Megabyte */ 646*bae9e67eSachartre } else if (full_size < (1ULL << 40)) { 647*bae9e67eSachartre *size = full_size >> 30; 648*bae9e67eSachartre *unit = 'G'; /* Gigabyte */ 649*bae9e67eSachartre } else { 650*bae9e67eSachartre *size = full_size >> 40; 651*bae9e67eSachartre *unit = 'T'; /* Terabyte */ 652*bae9e67eSachartre } 653*bae9e67eSachartre } 654047ba61eSachartre 655690555a1Sachartre /* 656690555a1Sachartre * Function: 657690555a1Sachartre * vd_file_rw 658690555a1Sachartre * 659690555a1Sachartre * Description: 660690555a1Sachartre * Read or write to a disk on file. 661690555a1Sachartre * 662690555a1Sachartre * Parameters: 663690555a1Sachartre * vd - disk on which the operation is performed. 664690555a1Sachartre * slice - slice on which the operation is performed, 66587a7269eSachartre * VD_SLICE_NONE indicates that the operation 66687a7269eSachartre * is done using an absolute disk offset. 667690555a1Sachartre * operation - operation to execute: read (VD_OP_BREAD) or 668690555a1Sachartre * write (VD_OP_BWRITE). 669690555a1Sachartre * data - buffer where data are read to or written from. 670690555a1Sachartre * blk - starting block for the operation. 671690555a1Sachartre * len - number of bytes to read or write. 672690555a1Sachartre * 673690555a1Sachartre * Return Code: 674690555a1Sachartre * n >= 0 - success, n indicates the number of bytes read 675690555a1Sachartre * or written. 676690555a1Sachartre * -1 - error. 677690555a1Sachartre */ 678690555a1Sachartre static ssize_t 679690555a1Sachartre vd_file_rw(vd_t *vd, int slice, int operation, caddr_t data, size_t blk, 680690555a1Sachartre size_t len) 681690555a1Sachartre { 682690555a1Sachartre caddr_t maddr; 683690555a1Sachartre size_t offset, maxlen, moffset, mlen, n; 684690555a1Sachartre uint_t smflags; 685690555a1Sachartre enum seg_rw srw; 686690555a1Sachartre 687690555a1Sachartre ASSERT(vd->file); 688690555a1Sachartre ASSERT(len > 0); 689690555a1Sachartre 690047ba61eSachartre /* 691047ba61eSachartre * If a file is exported as a slice then we don't care about the vtoc. 692047ba61eSachartre * In that case, the vtoc is a fake mainly to make newfs happy and we 693047ba61eSachartre * handle any I/O as a raw disk access so that we can have access to the 694047ba61eSachartre * entire backend. 695047ba61eSachartre */ 696047ba61eSachartre if (vd->vdisk_type == VD_DISK_TYPE_SLICE || slice == VD_SLICE_NONE) { 697690555a1Sachartre /* raw disk access */ 698690555a1Sachartre offset = blk * DEV_BSIZE; 699*bae9e67eSachartre if (offset >= vd->file_size) { 700*bae9e67eSachartre /* offset past the end of the disk */ 701*bae9e67eSachartre PR0("offset (0x%lx) >= fsize (0x%lx)", 702*bae9e67eSachartre offset, vd->file_size); 703*bae9e67eSachartre return (0); 704*bae9e67eSachartre } 705*bae9e67eSachartre maxlen = vd->file_size - offset; 706690555a1Sachartre } else { 707690555a1Sachartre ASSERT(slice >= 0 && slice < V_NUMPAR); 70878fcd0a1Sachartre 70917cadca8Slm66018 /* 71017cadca8Slm66018 * v1.0 vDisk clients depended on the server not verifying 71117cadca8Slm66018 * the label of a unformatted disk. This "feature" is 71217cadca8Slm66018 * maintained for backward compatibility but all versions 71317cadca8Slm66018 * from v1.1 onwards must do the right thing. 71417cadca8Slm66018 */ 71578fcd0a1Sachartre if (vd->vdisk_label == VD_DISK_LABEL_UNK && 716edcc0754Sachartre vio_ver_is_supported(vd->version, 1, 1)) { 717edcc0754Sachartre (void) vd_file_validate_geometry(vd); 718edcc0754Sachartre if (vd->vdisk_label == VD_DISK_LABEL_UNK) { 719edcc0754Sachartre PR0("Unknown disk label, can't do I/O " 720edcc0754Sachartre "from slice %d", slice); 72178fcd0a1Sachartre return (-1); 72278fcd0a1Sachartre } 723edcc0754Sachartre } 72478fcd0a1Sachartre 725edcc0754Sachartre if (vd->vdisk_label == VD_DISK_LABEL_VTOC) { 726edcc0754Sachartre ASSERT(vd->vtoc.v_sectorsz == DEV_BSIZE); 727edcc0754Sachartre } else { 728edcc0754Sachartre ASSERT(vd->vdisk_label == VD_DISK_LABEL_EFI); 729edcc0754Sachartre ASSERT(vd->vdisk_block_size == DEV_BSIZE); 730edcc0754Sachartre } 731edcc0754Sachartre 732edcc0754Sachartre if (blk >= vd->slices[slice].nblocks) { 733690555a1Sachartre /* address past the end of the slice */ 734*bae9e67eSachartre PR0("req_addr (0x%lx) >= psize (0x%lx)", 735edcc0754Sachartre blk, vd->slices[slice].nblocks); 736690555a1Sachartre return (0); 737690555a1Sachartre } 738690555a1Sachartre 739edcc0754Sachartre offset = (vd->slices[slice].start + blk) * DEV_BSIZE; 740*bae9e67eSachartre maxlen = (vd->slices[slice].nblocks - blk) * DEV_BSIZE; 741*bae9e67eSachartre } 742690555a1Sachartre 743690555a1Sachartre /* 744690555a1Sachartre * If the requested size is greater than the size 745690555a1Sachartre * of the partition, truncate the read/write. 746690555a1Sachartre */ 747690555a1Sachartre if (len > maxlen) { 748690555a1Sachartre PR0("I/O size truncated to %lu bytes from %lu bytes", 749690555a1Sachartre maxlen, len); 750690555a1Sachartre len = maxlen; 751690555a1Sachartre } 752690555a1Sachartre 753690555a1Sachartre /* 754690555a1Sachartre * We have to ensure that we are reading/writing into the mmap 755690555a1Sachartre * range. If we have a partial disk image (e.g. an image of 756690555a1Sachartre * s0 instead s2) the system can try to access slices that 757690555a1Sachartre * are not included into the disk image. 758690555a1Sachartre */ 759edcc0754Sachartre if ((offset + len) > vd->file_size) { 760edcc0754Sachartre PR0("offset + nbytes (0x%lx + 0x%lx) > " 761690555a1Sachartre "file_size (0x%lx)", offset, len, vd->file_size); 762690555a1Sachartre return (-1); 763690555a1Sachartre } 764690555a1Sachartre 765690555a1Sachartre srw = (operation == VD_OP_BREAD)? S_READ : S_WRITE; 766eba0cb4eSachartre smflags = (operation == VD_OP_BREAD)? 0 : 767eba0cb4eSachartre (SM_WRITE | vd_file_write_flags); 768690555a1Sachartre n = len; 769690555a1Sachartre 770690555a1Sachartre do { 771690555a1Sachartre /* 772690555a1Sachartre * segmap_getmapflt() returns a MAXBSIZE chunk which is 773690555a1Sachartre * MAXBSIZE aligned. 774690555a1Sachartre */ 775690555a1Sachartre moffset = offset & MAXBOFFSET; 776690555a1Sachartre mlen = MIN(MAXBSIZE - moffset, n); 777690555a1Sachartre maddr = segmap_getmapflt(segkmap, vd->file_vnode, offset, 778690555a1Sachartre mlen, 1, srw); 779690555a1Sachartre /* 780690555a1Sachartre * Fault in the pages so we can check for error and ensure 781690555a1Sachartre * that we can safely used the mapped address. 782690555a1Sachartre */ 783690555a1Sachartre if (segmap_fault(kas.a_hat, segkmap, maddr, mlen, 784690555a1Sachartre F_SOFTLOCK, srw) != 0) { 785690555a1Sachartre (void) segmap_release(segkmap, maddr, 0); 786690555a1Sachartre return (-1); 787690555a1Sachartre } 788690555a1Sachartre 789690555a1Sachartre if (operation == VD_OP_BREAD) 790690555a1Sachartre bcopy(maddr + moffset, data, mlen); 791690555a1Sachartre else 792690555a1Sachartre bcopy(data, maddr + moffset, mlen); 793690555a1Sachartre 794690555a1Sachartre if (segmap_fault(kas.a_hat, segkmap, maddr, mlen, 795690555a1Sachartre F_SOFTUNLOCK, srw) != 0) { 796690555a1Sachartre (void) segmap_release(segkmap, maddr, 0); 797690555a1Sachartre return (-1); 798690555a1Sachartre } 799690555a1Sachartre if (segmap_release(segkmap, maddr, smflags) != 0) 800690555a1Sachartre return (-1); 801690555a1Sachartre n -= mlen; 802690555a1Sachartre offset += mlen; 803690555a1Sachartre data += mlen; 804690555a1Sachartre 805690555a1Sachartre } while (n > 0); 806690555a1Sachartre 807690555a1Sachartre return (len); 808690555a1Sachartre } 809690555a1Sachartre 81087a7269eSachartre /* 81187a7269eSachartre * Function: 812*bae9e67eSachartre * vd_build_default_label 81378fcd0a1Sachartre * 81478fcd0a1Sachartre * Description: 815*bae9e67eSachartre * Return a default label for a given disk size. This is used when the disk 81678fcd0a1Sachartre * does not have a valid VTOC so that the user can get a valid default 81717cadca8Slm66018 * configuration. The default label has all slice sizes set to 0 (except 81878fcd0a1Sachartre * slice 2 which is the entire disk) to force the user to write a valid 81978fcd0a1Sachartre * label onto the disk image. 82078fcd0a1Sachartre * 82178fcd0a1Sachartre * Parameters: 822*bae9e67eSachartre * disk_size - the disk size in bytes 82378fcd0a1Sachartre * label - the returned default label. 82478fcd0a1Sachartre * 82578fcd0a1Sachartre * Return Code: 82678fcd0a1Sachartre * none. 82778fcd0a1Sachartre */ 82878fcd0a1Sachartre static void 829*bae9e67eSachartre vd_build_default_label(size_t disk_size, struct dk_label *label) 83078fcd0a1Sachartre { 83178fcd0a1Sachartre size_t size; 832*bae9e67eSachartre char unit; 833edcc0754Sachartre 834edcc0754Sachartre bzero(label, sizeof (struct dk_label)); 83578fcd0a1Sachartre 83678fcd0a1Sachartre /* 83778fcd0a1Sachartre * We must have a resonable number of cylinders and sectors so 83878fcd0a1Sachartre * that newfs can run using default values. 83978fcd0a1Sachartre * 84078fcd0a1Sachartre * if (disk_size < 2MB) 84178fcd0a1Sachartre * phys_cylinders = disk_size / 100K 84278fcd0a1Sachartre * else 84378fcd0a1Sachartre * phys_cylinders = disk_size / 300K 84478fcd0a1Sachartre * 84578fcd0a1Sachartre * phys_cylinders = (phys_cylinders == 0) ? 1 : phys_cylinders 84678fcd0a1Sachartre * alt_cylinders = (phys_cylinders > 2) ? 2 : 0; 84778fcd0a1Sachartre * data_cylinders = phys_cylinders - alt_cylinders 84878fcd0a1Sachartre * 84978fcd0a1Sachartre * sectors = disk_size / (phys_cylinders * blk_size) 85078fcd0a1Sachartre * 85178fcd0a1Sachartre * The file size test is an attempt to not have too few cylinders 85278fcd0a1Sachartre * for a small file, or so many on a big file that you waste space 85378fcd0a1Sachartre * for backup superblocks or cylinder group structures. 85478fcd0a1Sachartre */ 855*bae9e67eSachartre if (disk_size < (2 * 1024 * 1024)) 856*bae9e67eSachartre label->dkl_pcyl = disk_size / (100 * 1024); 85778fcd0a1Sachartre else 858*bae9e67eSachartre label->dkl_pcyl = disk_size / (300 * 1024); 85978fcd0a1Sachartre 86078fcd0a1Sachartre if (label->dkl_pcyl == 0) 86178fcd0a1Sachartre label->dkl_pcyl = 1; 86278fcd0a1Sachartre 863047ba61eSachartre label->dkl_acyl = 0; 864047ba61eSachartre 86578fcd0a1Sachartre if (label->dkl_pcyl > 2) 86678fcd0a1Sachartre label->dkl_acyl = 2; 86778fcd0a1Sachartre 868*bae9e67eSachartre label->dkl_nsect = disk_size / (DEV_BSIZE * label->dkl_pcyl); 86978fcd0a1Sachartre label->dkl_ncyl = label->dkl_pcyl - label->dkl_acyl; 87078fcd0a1Sachartre label->dkl_nhead = 1; 87178fcd0a1Sachartre label->dkl_write_reinstruct = 0; 87278fcd0a1Sachartre label->dkl_read_reinstruct = 0; 87378fcd0a1Sachartre label->dkl_rpm = 7200; 87478fcd0a1Sachartre label->dkl_apc = 0; 87578fcd0a1Sachartre label->dkl_intrlv = 0; 87678fcd0a1Sachartre 877*bae9e67eSachartre PR0("requested disk size: %ld bytes\n", disk_size); 87878fcd0a1Sachartre PR0("setup: ncyl=%d nhead=%d nsec=%d\n", label->dkl_pcyl, 87978fcd0a1Sachartre label->dkl_nhead, label->dkl_nsect); 88078fcd0a1Sachartre PR0("provided disk size: %ld bytes\n", (uint64_t) 88178fcd0a1Sachartre (label->dkl_pcyl * label->dkl_nhead * 88278fcd0a1Sachartre label->dkl_nsect * DEV_BSIZE)); 88378fcd0a1Sachartre 884*bae9e67eSachartre vd_get_readable_size(disk_size, &size, &unit); 88578fcd0a1Sachartre 88678fcd0a1Sachartre /* 88778fcd0a1Sachartre * We must have a correct label name otherwise format(1m) will 88878fcd0a1Sachartre * not recognized the disk as labeled. 88978fcd0a1Sachartre */ 89078fcd0a1Sachartre (void) snprintf(label->dkl_asciilabel, LEN_DKL_ASCII, 89178fcd0a1Sachartre "SUN-DiskImage-%ld%cB cyl %d alt %d hd %d sec %d", 892*bae9e67eSachartre size, unit, 89378fcd0a1Sachartre label->dkl_ncyl, label->dkl_acyl, label->dkl_nhead, 89478fcd0a1Sachartre label->dkl_nsect); 89578fcd0a1Sachartre 89678fcd0a1Sachartre /* default VTOC */ 89778fcd0a1Sachartre label->dkl_vtoc.v_version = V_VERSION; 898edcc0754Sachartre label->dkl_vtoc.v_nparts = V_NUMPAR; 89978fcd0a1Sachartre label->dkl_vtoc.v_sanity = VTOC_SANE; 900edcc0754Sachartre label->dkl_vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_tag = V_BACKUP; 901edcc0754Sachartre label->dkl_map[VD_ENTIRE_DISK_SLICE].dkl_cylno = 0; 902edcc0754Sachartre label->dkl_map[VD_ENTIRE_DISK_SLICE].dkl_nblk = label->dkl_ncyl * 90378fcd0a1Sachartre label->dkl_nhead * label->dkl_nsect; 904edcc0754Sachartre label->dkl_magic = DKL_MAGIC; 90578fcd0a1Sachartre label->dkl_cksum = vd_lbl2cksum(label); 90678fcd0a1Sachartre } 90778fcd0a1Sachartre 90878fcd0a1Sachartre /* 90978fcd0a1Sachartre * Function: 91087a7269eSachartre * vd_file_set_vtoc 91187a7269eSachartre * 91287a7269eSachartre * Description: 91387a7269eSachartre * Set the vtoc of a disk image by writing the label and backup 91487a7269eSachartre * labels into the disk image backend. 91587a7269eSachartre * 91687a7269eSachartre * Parameters: 91787a7269eSachartre * vd - disk on which the operation is performed. 91887a7269eSachartre * label - the data to be written. 91987a7269eSachartre * 92087a7269eSachartre * Return Code: 92187a7269eSachartre * 0 - success. 92287a7269eSachartre * n > 0 - error, n indicates the errno code. 92387a7269eSachartre */ 92487a7269eSachartre static int 92587a7269eSachartre vd_file_set_vtoc(vd_t *vd, struct dk_label *label) 92687a7269eSachartre { 92787a7269eSachartre int blk, sec, cyl, head, cnt; 92887a7269eSachartre 92987a7269eSachartre ASSERT(vd->file); 93087a7269eSachartre 93187a7269eSachartre if (VD_FILE_LABEL_WRITE(vd, label) < 0) { 93287a7269eSachartre PR0("fail to write disk label"); 93387a7269eSachartre return (EIO); 93487a7269eSachartre } 93587a7269eSachartre 93687a7269eSachartre /* 93787a7269eSachartre * Backup labels are on the last alternate cylinder's 93887a7269eSachartre * first five odd sectors. 93987a7269eSachartre */ 94087a7269eSachartre if (label->dkl_acyl == 0) { 94187a7269eSachartre PR0("no alternate cylinder, can not store backup labels"); 94287a7269eSachartre return (0); 94387a7269eSachartre } 94487a7269eSachartre 94587a7269eSachartre cyl = label->dkl_ncyl + label->dkl_acyl - 1; 94687a7269eSachartre head = label->dkl_nhead - 1; 94787a7269eSachartre 94887a7269eSachartre blk = (cyl * ((label->dkl_nhead * label->dkl_nsect) - label->dkl_apc)) + 94987a7269eSachartre (head * label->dkl_nsect); 95087a7269eSachartre 95187a7269eSachartre /* 95287a7269eSachartre * Write the backup labels. Make sure we don't try to write past 95387a7269eSachartre * the last cylinder. 95487a7269eSachartre */ 95587a7269eSachartre sec = 1; 95687a7269eSachartre 95787a7269eSachartre for (cnt = 0; cnt < VD_FILE_NUM_BACKUP; cnt++) { 95887a7269eSachartre 95987a7269eSachartre if (sec >= label->dkl_nsect) { 96087a7269eSachartre PR0("not enough sector to store all backup labels"); 96187a7269eSachartre return (0); 96287a7269eSachartre } 96387a7269eSachartre 96487a7269eSachartre if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)label, 96587a7269eSachartre blk + sec, sizeof (struct dk_label)) < 0) { 96687a7269eSachartre PR0("error writing backup label at block %d\n", 96787a7269eSachartre blk + sec); 96887a7269eSachartre return (EIO); 96987a7269eSachartre } 97087a7269eSachartre 97187a7269eSachartre PR1("wrote backup label at block %d\n", blk + sec); 97287a7269eSachartre 97387a7269eSachartre sec += 2; 97487a7269eSachartre } 97587a7269eSachartre 97687a7269eSachartre return (0); 97787a7269eSachartre } 97887a7269eSachartre 97987a7269eSachartre /* 98087a7269eSachartre * Function: 98187a7269eSachartre * vd_file_get_devid_block 98287a7269eSachartre * 98387a7269eSachartre * Description: 98487a7269eSachartre * Return the block number where the device id is stored. 98587a7269eSachartre * 98687a7269eSachartre * Parameters: 98787a7269eSachartre * vd - disk on which the operation is performed. 98887a7269eSachartre * blkp - pointer to the block number 98987a7269eSachartre * 99087a7269eSachartre * Return Code: 99187a7269eSachartre * 0 - success 99287a7269eSachartre * ENOSPC - disk has no space to store a device id 99387a7269eSachartre */ 99487a7269eSachartre static int 99587a7269eSachartre vd_file_get_devid_block(vd_t *vd, size_t *blkp) 99687a7269eSachartre { 99787a7269eSachartre diskaddr_t spc, head, cyl; 99887a7269eSachartre 99987a7269eSachartre ASSERT(vd->file); 1000edcc0754Sachartre 1001edcc0754Sachartre if (vd->vdisk_label == VD_DISK_LABEL_UNK) { 1002edcc0754Sachartre /* 1003edcc0754Sachartre * If no label is defined we don't know where to find 1004edcc0754Sachartre * a device id. 1005edcc0754Sachartre */ 1006edcc0754Sachartre return (ENOSPC); 1007edcc0754Sachartre } 1008edcc0754Sachartre 1009edcc0754Sachartre if (vd->vdisk_label == VD_DISK_LABEL_EFI) { 1010edcc0754Sachartre /* 1011edcc0754Sachartre * For an EFI disk, the devid is at the beginning of 1012edcc0754Sachartre * the reserved slice 1013edcc0754Sachartre */ 1014edcc0754Sachartre if (vd->efi_reserved == -1) { 1015edcc0754Sachartre PR0("EFI disk has no reserved slice"); 1016edcc0754Sachartre return (ENOSPC); 1017edcc0754Sachartre } 1018edcc0754Sachartre 1019edcc0754Sachartre *blkp = vd->slices[vd->efi_reserved].start; 1020edcc0754Sachartre return (0); 1021edcc0754Sachartre } 1022edcc0754Sachartre 102387a7269eSachartre ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC); 102487a7269eSachartre 102587a7269eSachartre /* this geometry doesn't allow us to have a devid */ 102687a7269eSachartre if (vd->dk_geom.dkg_acyl < 2) { 102787a7269eSachartre PR0("not enough alternate cylinder available for devid " 102887a7269eSachartre "(acyl=%u)", vd->dk_geom.dkg_acyl); 102987a7269eSachartre return (ENOSPC); 103087a7269eSachartre } 103187a7269eSachartre 103287a7269eSachartre /* the devid is in on the track next to the last cylinder */ 103387a7269eSachartre cyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl - 2; 103487a7269eSachartre spc = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect; 103587a7269eSachartre head = vd->dk_geom.dkg_nhead - 1; 103687a7269eSachartre 103787a7269eSachartre *blkp = (cyl * (spc - vd->dk_geom.dkg_apc)) + 103887a7269eSachartre (head * vd->dk_geom.dkg_nsect) + 1; 103987a7269eSachartre 104087a7269eSachartre return (0); 104187a7269eSachartre } 104287a7269eSachartre 104387a7269eSachartre /* 104487a7269eSachartre * Return the checksum of a disk block containing an on-disk devid. 104587a7269eSachartre */ 104687a7269eSachartre static uint_t 104787a7269eSachartre vd_dkdevid2cksum(struct dk_devid *dkdevid) 104887a7269eSachartre { 104987a7269eSachartre uint_t chksum, *ip; 105087a7269eSachartre int i; 105187a7269eSachartre 105287a7269eSachartre chksum = 0; 105387a7269eSachartre ip = (uint_t *)dkdevid; 105487a7269eSachartre for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int)); i++) 105587a7269eSachartre chksum ^= ip[i]; 105687a7269eSachartre 105787a7269eSachartre return (chksum); 105887a7269eSachartre } 105987a7269eSachartre 106087a7269eSachartre /* 106187a7269eSachartre * Function: 106287a7269eSachartre * vd_file_read_devid 106387a7269eSachartre * 106487a7269eSachartre * Description: 106587a7269eSachartre * Read the device id stored on a disk image. 106687a7269eSachartre * 106787a7269eSachartre * Parameters: 106887a7269eSachartre * vd - disk on which the operation is performed. 106987a7269eSachartre * devid - the return address of the device ID. 107087a7269eSachartre * 107187a7269eSachartre * Return Code: 107287a7269eSachartre * 0 - success 107387a7269eSachartre * EIO - I/O error while trying to access the disk image 107487a7269eSachartre * EINVAL - no valid device id was found 107587a7269eSachartre * ENOSPC - disk has no space to store a device id 107687a7269eSachartre */ 107787a7269eSachartre static int 107887a7269eSachartre vd_file_read_devid(vd_t *vd, ddi_devid_t *devid) 107987a7269eSachartre { 108087a7269eSachartre struct dk_devid *dkdevid; 108187a7269eSachartre size_t blk; 108287a7269eSachartre uint_t chksum; 108387a7269eSachartre int status, sz; 108487a7269eSachartre 108587a7269eSachartre if ((status = vd_file_get_devid_block(vd, &blk)) != 0) 108687a7269eSachartre return (status); 108787a7269eSachartre 108887a7269eSachartre dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP); 108987a7269eSachartre 109087a7269eSachartre /* get the devid */ 109187a7269eSachartre if ((vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)dkdevid, blk, 109287a7269eSachartre DEV_BSIZE)) < 0) { 109387a7269eSachartre PR0("error reading devid block at %lu", blk); 109487a7269eSachartre status = EIO; 109587a7269eSachartre goto done; 109687a7269eSachartre } 109787a7269eSachartre 109887a7269eSachartre /* validate the revision */ 109987a7269eSachartre if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) || 110087a7269eSachartre (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) { 110187a7269eSachartre PR0("invalid devid found at block %lu (bad revision)", blk); 110287a7269eSachartre status = EINVAL; 110387a7269eSachartre goto done; 110487a7269eSachartre } 110587a7269eSachartre 110687a7269eSachartre /* compute checksum */ 110787a7269eSachartre chksum = vd_dkdevid2cksum(dkdevid); 110887a7269eSachartre 110987a7269eSachartre /* compare the checksums */ 111087a7269eSachartre if (DKD_GETCHKSUM(dkdevid) != chksum) { 111187a7269eSachartre PR0("invalid devid found at block %lu (bad checksum)", blk); 111287a7269eSachartre status = EINVAL; 111387a7269eSachartre goto done; 111487a7269eSachartre } 111587a7269eSachartre 111687a7269eSachartre /* validate the device id */ 111787a7269eSachartre if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) { 111887a7269eSachartre PR0("invalid devid found at block %lu", blk); 111987a7269eSachartre status = EINVAL; 112087a7269eSachartre goto done; 112187a7269eSachartre } 112287a7269eSachartre 112387a7269eSachartre PR1("devid read at block %lu", blk); 112487a7269eSachartre 112587a7269eSachartre sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid); 112687a7269eSachartre *devid = kmem_alloc(sz, KM_SLEEP); 112787a7269eSachartre bcopy(&dkdevid->dkd_devid, *devid, sz); 112887a7269eSachartre 112987a7269eSachartre done: 113087a7269eSachartre kmem_free(dkdevid, DEV_BSIZE); 113187a7269eSachartre return (status); 113287a7269eSachartre 113387a7269eSachartre } 113487a7269eSachartre 113587a7269eSachartre /* 113687a7269eSachartre * Function: 113787a7269eSachartre * vd_file_write_devid 113887a7269eSachartre * 113987a7269eSachartre * Description: 114087a7269eSachartre * Write a device id into disk image. 114187a7269eSachartre * 114287a7269eSachartre * Parameters: 114387a7269eSachartre * vd - disk on which the operation is performed. 114487a7269eSachartre * devid - the device ID to store. 114587a7269eSachartre * 114687a7269eSachartre * Return Code: 114787a7269eSachartre * 0 - success 114887a7269eSachartre * EIO - I/O error while trying to access the disk image 114987a7269eSachartre * ENOSPC - disk has no space to store a device id 115087a7269eSachartre */ 115187a7269eSachartre static int 115287a7269eSachartre vd_file_write_devid(vd_t *vd, ddi_devid_t devid) 115387a7269eSachartre { 115487a7269eSachartre struct dk_devid *dkdevid; 115587a7269eSachartre uint_t chksum; 115687a7269eSachartre size_t blk; 115787a7269eSachartre int status; 115887a7269eSachartre 1159edcc0754Sachartre if (devid == NULL) { 1160edcc0754Sachartre /* nothing to write */ 1161edcc0754Sachartre return (0); 1162edcc0754Sachartre } 1163edcc0754Sachartre 116487a7269eSachartre if ((status = vd_file_get_devid_block(vd, &blk)) != 0) 116587a7269eSachartre return (status); 116687a7269eSachartre 116787a7269eSachartre dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP); 116887a7269eSachartre 116987a7269eSachartre /* set revision */ 117087a7269eSachartre dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB; 117187a7269eSachartre dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB; 117287a7269eSachartre 117387a7269eSachartre /* copy devid */ 117487a7269eSachartre bcopy(devid, &dkdevid->dkd_devid, ddi_devid_sizeof(devid)); 117587a7269eSachartre 117687a7269eSachartre /* compute checksum */ 117787a7269eSachartre chksum = vd_dkdevid2cksum(dkdevid); 117887a7269eSachartre 117987a7269eSachartre /* set checksum */ 118087a7269eSachartre DKD_FORMCHKSUM(chksum, dkdevid); 118187a7269eSachartre 118287a7269eSachartre /* store the devid */ 118387a7269eSachartre if ((status = vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, 118487a7269eSachartre (caddr_t)dkdevid, blk, DEV_BSIZE)) < 0) { 118587a7269eSachartre PR0("Error writing devid block at %lu", blk); 118687a7269eSachartre status = EIO; 118787a7269eSachartre } else { 118887a7269eSachartre PR1("devid written at block %lu", blk); 118987a7269eSachartre status = 0; 119087a7269eSachartre } 119187a7269eSachartre 119287a7269eSachartre kmem_free(dkdevid, DEV_BSIZE); 119387a7269eSachartre return (status); 119487a7269eSachartre } 119587a7269eSachartre 119687a7269eSachartre /* 119787a7269eSachartre * Function: 119817cadca8Slm66018 * vd_do_scsi_rdwr 119987a7269eSachartre * 120087a7269eSachartre * Description: 120187a7269eSachartre * Read or write to a SCSI disk using an absolute disk offset. 120287a7269eSachartre * 120387a7269eSachartre * Parameters: 120487a7269eSachartre * vd - disk on which the operation is performed. 120587a7269eSachartre * operation - operation to execute: read (VD_OP_BREAD) or 120687a7269eSachartre * write (VD_OP_BWRITE). 120787a7269eSachartre * data - buffer where data are read to or written from. 120887a7269eSachartre * blk - starting block for the operation. 120987a7269eSachartre * len - number of bytes to read or write. 121087a7269eSachartre * 121187a7269eSachartre * Return Code: 121287a7269eSachartre * 0 - success 121387a7269eSachartre * n != 0 - error. 121487a7269eSachartre */ 121587a7269eSachartre static int 121617cadca8Slm66018 vd_do_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t blk, size_t len) 121787a7269eSachartre { 121887a7269eSachartre struct uscsi_cmd ucmd; 121987a7269eSachartre union scsi_cdb cdb; 122087a7269eSachartre int nsectors, nblk; 122187a7269eSachartre int max_sectors; 122287a7269eSachartre int status, rval; 122387a7269eSachartre 122487a7269eSachartre ASSERT(!vd->file); 122517cadca8Slm66018 ASSERT(vd->vdisk_block_size > 0); 122687a7269eSachartre 122787a7269eSachartre max_sectors = vd->max_xfer_sz; 122817cadca8Slm66018 nblk = (len / vd->vdisk_block_size); 122987a7269eSachartre 123017cadca8Slm66018 if (len % vd->vdisk_block_size != 0) 123187a7269eSachartre return (EINVAL); 123287a7269eSachartre 123387a7269eSachartre /* 123487a7269eSachartre * Build and execute the uscsi ioctl. We build a group0, group1 123587a7269eSachartre * or group4 command as necessary, since some targets 123687a7269eSachartre * do not support group1 commands. 123787a7269eSachartre */ 123887a7269eSachartre while (nblk) { 123987a7269eSachartre 124087a7269eSachartre bzero(&ucmd, sizeof (ucmd)); 124187a7269eSachartre bzero(&cdb, sizeof (cdb)); 124287a7269eSachartre 124387a7269eSachartre nsectors = (max_sectors < nblk) ? max_sectors : nblk; 124487a7269eSachartre 124517cadca8Slm66018 /* 124617cadca8Slm66018 * Some of the optical drives on sun4v machines are ATAPI 124717cadca8Slm66018 * devices which use Group 1 Read/Write commands so we need 124817cadca8Slm66018 * to explicitly check a flag which is set when a domain 124917cadca8Slm66018 * is bound. 125017cadca8Slm66018 */ 125117cadca8Slm66018 if (blk < (2 << 20) && nsectors <= 0xff && !vd->is_atapi_dev) { 125287a7269eSachartre FORMG0ADDR(&cdb, blk); 125387a7269eSachartre FORMG0COUNT(&cdb, nsectors); 125487a7269eSachartre ucmd.uscsi_cdblen = CDB_GROUP0; 125587a7269eSachartre } else if (blk > 0xffffffff) { 125687a7269eSachartre FORMG4LONGADDR(&cdb, blk); 125787a7269eSachartre FORMG4COUNT(&cdb, nsectors); 125887a7269eSachartre ucmd.uscsi_cdblen = CDB_GROUP4; 125987a7269eSachartre cdb.scc_cmd |= SCMD_GROUP4; 126087a7269eSachartre } else { 126187a7269eSachartre FORMG1ADDR(&cdb, blk); 126287a7269eSachartre FORMG1COUNT(&cdb, nsectors); 126387a7269eSachartre ucmd.uscsi_cdblen = CDB_GROUP1; 126487a7269eSachartre cdb.scc_cmd |= SCMD_GROUP1; 126587a7269eSachartre } 126687a7269eSachartre ucmd.uscsi_cdb = (caddr_t)&cdb; 126787a7269eSachartre ucmd.uscsi_bufaddr = data; 126817cadca8Slm66018 ucmd.uscsi_buflen = nsectors * vd->block_size; 126987a7269eSachartre ucmd.uscsi_timeout = vd_scsi_rdwr_timeout; 127087a7269eSachartre /* 127187a7269eSachartre * Set flags so that the command is isolated from normal 127287a7269eSachartre * commands and no error message is printed. 127387a7269eSachartre */ 127487a7269eSachartre ucmd.uscsi_flags = USCSI_ISOLATE | USCSI_SILENT; 127587a7269eSachartre 127687a7269eSachartre if (operation == VD_OP_BREAD) { 127787a7269eSachartre cdb.scc_cmd |= SCMD_READ; 127887a7269eSachartre ucmd.uscsi_flags |= USCSI_READ; 127987a7269eSachartre } else { 128087a7269eSachartre cdb.scc_cmd |= SCMD_WRITE; 128187a7269eSachartre } 128287a7269eSachartre 128387a7269eSachartre status = ldi_ioctl(vd->ldi_handle[VD_ENTIRE_DISK_SLICE], 1284047ba61eSachartre USCSICMD, (intptr_t)&ucmd, (vd->open_flags | FKIOCTL), 128587a7269eSachartre kcred, &rval); 128687a7269eSachartre 128787a7269eSachartre if (status == 0) 128887a7269eSachartre status = ucmd.uscsi_status; 128987a7269eSachartre 129087a7269eSachartre if (status != 0) 129187a7269eSachartre break; 129287a7269eSachartre 129387a7269eSachartre /* 129487a7269eSachartre * Check if partial DMA breakup is required. If so, reduce 129587a7269eSachartre * the request size by half and retry the last request. 129687a7269eSachartre */ 129787a7269eSachartre if (ucmd.uscsi_resid == ucmd.uscsi_buflen) { 129887a7269eSachartre max_sectors >>= 1; 129987a7269eSachartre if (max_sectors <= 0) { 130087a7269eSachartre status = EIO; 130187a7269eSachartre break; 130287a7269eSachartre } 130387a7269eSachartre continue; 130487a7269eSachartre } 130587a7269eSachartre 130687a7269eSachartre if (ucmd.uscsi_resid != 0) { 130787a7269eSachartre status = EIO; 130887a7269eSachartre break; 130987a7269eSachartre } 131087a7269eSachartre 131187a7269eSachartre blk += nsectors; 131287a7269eSachartre nblk -= nsectors; 131317cadca8Slm66018 data += nsectors * vd->vdisk_block_size; /* SECSIZE */ 131487a7269eSachartre } 131587a7269eSachartre 131687a7269eSachartre return (status); 131787a7269eSachartre } 131887a7269eSachartre 1319205eeb1aSlm66018 /* 132017cadca8Slm66018 * Function: 132117cadca8Slm66018 * vd_scsi_rdwr 132217cadca8Slm66018 * 132317cadca8Slm66018 * Description: 132417cadca8Slm66018 * Wrapper function to read or write to a SCSI disk using an absolute 132517cadca8Slm66018 * disk offset. It checks the blocksize of the underlying device and, 132617cadca8Slm66018 * if necessary, adjusts the buffers accordingly before calling 132717cadca8Slm66018 * vd_do_scsi_rdwr() to do the actual read or write. 132817cadca8Slm66018 * 132917cadca8Slm66018 * Parameters: 133017cadca8Slm66018 * vd - disk on which the operation is performed. 133117cadca8Slm66018 * operation - operation to execute: read (VD_OP_BREAD) or 133217cadca8Slm66018 * write (VD_OP_BWRITE). 133317cadca8Slm66018 * data - buffer where data are read to or written from. 133417cadca8Slm66018 * blk - starting block for the operation. 133517cadca8Slm66018 * len - number of bytes to read or write. 133617cadca8Slm66018 * 133717cadca8Slm66018 * Return Code: 133817cadca8Slm66018 * 0 - success 133917cadca8Slm66018 * n != 0 - error. 134017cadca8Slm66018 */ 134117cadca8Slm66018 static int 134217cadca8Slm66018 vd_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t vblk, size_t vlen) 134317cadca8Slm66018 { 134417cadca8Slm66018 int rv; 134517cadca8Slm66018 134617cadca8Slm66018 size_t pblk; /* physical device block number of data on device */ 134717cadca8Slm66018 size_t delta; /* relative offset between pblk and vblk */ 134817cadca8Slm66018 size_t pnblk; /* number of physical blocks to be read from device */ 134917cadca8Slm66018 size_t plen; /* length of data to be read from physical device */ 135017cadca8Slm66018 char *buf; /* buffer area to fit physical device's block size */ 135117cadca8Slm66018 13522f5224aeSachartre if (vd->block_size == 0) { 13532f5224aeSachartre /* 13542f5224aeSachartre * The block size was not available during the attach, 13552f5224aeSachartre * try to update it now. 13562f5224aeSachartre */ 13572f5224aeSachartre if (vd_setup_mediainfo(vd) != 0) 13582f5224aeSachartre return (EIO); 13592f5224aeSachartre } 13602f5224aeSachartre 136117cadca8Slm66018 /* 136217cadca8Slm66018 * If the vdisk block size and the block size of the underlying device 136317cadca8Slm66018 * match we can skip straight to vd_do_scsi_rdwr(), otherwise we need 136417cadca8Slm66018 * to create a buffer large enough to handle the device's block size 136517cadca8Slm66018 * and adjust the block to be read from and the amount of data to 136617cadca8Slm66018 * read to correspond with the device's block size. 136717cadca8Slm66018 */ 136817cadca8Slm66018 if (vd->vdisk_block_size == vd->block_size) 136917cadca8Slm66018 return (vd_do_scsi_rdwr(vd, operation, data, vblk, vlen)); 137017cadca8Slm66018 137117cadca8Slm66018 if (vd->vdisk_block_size > vd->block_size) 137217cadca8Slm66018 return (EINVAL); 137317cadca8Slm66018 137417cadca8Slm66018 /* 137517cadca8Slm66018 * Writing of physical block sizes larger than the virtual block size 137617cadca8Slm66018 * is not supported. This would be added if/when support for guests 137717cadca8Slm66018 * writing to DVDs is implemented. 137817cadca8Slm66018 */ 137917cadca8Slm66018 if (operation == VD_OP_BWRITE) 138017cadca8Slm66018 return (ENOTSUP); 138117cadca8Slm66018 138217cadca8Slm66018 /* BEGIN CSTYLED */ 138317cadca8Slm66018 /* 138417cadca8Slm66018 * Below is a diagram showing the relationship between the physical 138517cadca8Slm66018 * and virtual blocks. If the virtual blocks marked by 'X' below are 138617cadca8Slm66018 * requested, then the physical blocks denoted by 'Y' are read. 138717cadca8Slm66018 * 138817cadca8Slm66018 * vblk 138917cadca8Slm66018 * | vlen 139017cadca8Slm66018 * |<--------------->| 139117cadca8Slm66018 * v v 139217cadca8Slm66018 * --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+- virtual disk: 139317cadca8Slm66018 * | | | |XX|XX|XX|XX|XX|XX| | | | | | } block size is 139417cadca8Slm66018 * --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+- vd->vdisk_block_size 139517cadca8Slm66018 * : : : : 139617cadca8Slm66018 * >:==:< delta : : 139717cadca8Slm66018 * : : : : 139817cadca8Slm66018 * --+-----+-----+-----+-----+-----+-----+-----+-- physical disk: 139917cadca8Slm66018 * | |YY:YY|YYYYY|YYYYY|YY:YY| | | } block size is 140017cadca8Slm66018 * --+-----+-----+-----+-----+-----+-----+-----+-- vd->block_size 140117cadca8Slm66018 * ^ ^ 140217cadca8Slm66018 * |<--------------------->| 140317cadca8Slm66018 * | plen 140417cadca8Slm66018 * pblk 140517cadca8Slm66018 */ 140617cadca8Slm66018 /* END CSTYLED */ 140717cadca8Slm66018 pblk = (vblk * vd->vdisk_block_size) / vd->block_size; 140817cadca8Slm66018 delta = (vblk * vd->vdisk_block_size) - (pblk * vd->block_size); 140917cadca8Slm66018 pnblk = ((delta + vlen - 1) / vd->block_size) + 1; 141017cadca8Slm66018 plen = pnblk * vd->block_size; 141117cadca8Slm66018 141217cadca8Slm66018 PR2("vblk %lx:pblk %lx: vlen %ld:plen %ld", vblk, pblk, vlen, plen); 141317cadca8Slm66018 141417cadca8Slm66018 buf = kmem_zalloc(sizeof (caddr_t) * plen, KM_SLEEP); 141517cadca8Slm66018 rv = vd_do_scsi_rdwr(vd, operation, (caddr_t)buf, pblk, plen); 141617cadca8Slm66018 bcopy(buf + delta, data, vlen); 141717cadca8Slm66018 141817cadca8Slm66018 kmem_free(buf, sizeof (caddr_t) * plen); 141917cadca8Slm66018 142017cadca8Slm66018 return (rv); 142117cadca8Slm66018 } 142217cadca8Slm66018 142317cadca8Slm66018 /* 1424*bae9e67eSachartre * Function: 1425*bae9e67eSachartre * vd_slice_flabel_read 1426*bae9e67eSachartre * 1427*bae9e67eSachartre * Description: 1428*bae9e67eSachartre * This function simulates a read operation from the fake label of 1429*bae9e67eSachartre * a single-slice disk. 1430*bae9e67eSachartre * 1431*bae9e67eSachartre * Parameters: 1432*bae9e67eSachartre * vd - single-slice disk to read from 1433*bae9e67eSachartre * data - buffer where data should be read to 1434*bae9e67eSachartre * offset - offset in byte where the read should start 1435*bae9e67eSachartre * length - number of bytes to read 1436*bae9e67eSachartre * 1437*bae9e67eSachartre * Return Code: 1438*bae9e67eSachartre * n >= 0 - success, n indicates the number of bytes read 1439*bae9e67eSachartre * -1 - error 1440*bae9e67eSachartre */ 1441*bae9e67eSachartre static ssize_t 1442*bae9e67eSachartre vd_slice_flabel_read(vd_t *vd, caddr_t data, size_t offset, size_t length) 1443*bae9e67eSachartre { 1444*bae9e67eSachartre size_t n = 0; 1445*bae9e67eSachartre uint_t limit = vd->flabel_limit * DEV_BSIZE; 1446*bae9e67eSachartre 1447*bae9e67eSachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 1448*bae9e67eSachartre ASSERT(vd->flabel != NULL); 1449*bae9e67eSachartre 1450*bae9e67eSachartre /* if offset is past the fake label limit there's nothing to read */ 1451*bae9e67eSachartre if (offset >= limit) 1452*bae9e67eSachartre return (0); 1453*bae9e67eSachartre 1454*bae9e67eSachartre /* data with offset 0 to flabel_size are read from flabel */ 1455*bae9e67eSachartre if (offset < vd->flabel_size) { 1456*bae9e67eSachartre 1457*bae9e67eSachartre if (offset + length <= vd->flabel_size) { 1458*bae9e67eSachartre bcopy(vd->flabel + offset, data, length); 1459*bae9e67eSachartre return (length); 1460*bae9e67eSachartre } 1461*bae9e67eSachartre 1462*bae9e67eSachartre n = vd->flabel_size - offset; 1463*bae9e67eSachartre bcopy(vd->flabel + offset, data, n); 1464*bae9e67eSachartre data += n; 1465*bae9e67eSachartre } 1466*bae9e67eSachartre 1467*bae9e67eSachartre /* data with offset from flabel_size to flabel_limit are all zeros */ 1468*bae9e67eSachartre if (offset + length <= limit) { 1469*bae9e67eSachartre bzero(data, length - n); 1470*bae9e67eSachartre return (length); 1471*bae9e67eSachartre } 1472*bae9e67eSachartre 1473*bae9e67eSachartre bzero(data, limit - offset - n); 1474*bae9e67eSachartre return (limit - offset); 1475*bae9e67eSachartre } 1476*bae9e67eSachartre 1477*bae9e67eSachartre /* 1478*bae9e67eSachartre * Function: 1479*bae9e67eSachartre * vd_slice_flabel_write 1480*bae9e67eSachartre * 1481*bae9e67eSachartre * Description: 1482*bae9e67eSachartre * This function simulates a write operation to the fake label of 1483*bae9e67eSachartre * a single-slice disk. Write operations are actually faked and return 1484*bae9e67eSachartre * success although the label is never changed. This is mostly to 1485*bae9e67eSachartre * simulate a successful label update. 1486*bae9e67eSachartre * 1487*bae9e67eSachartre * Parameters: 1488*bae9e67eSachartre * vd - single-slice disk to write to 1489*bae9e67eSachartre * data - buffer where data should be written from 1490*bae9e67eSachartre * offset - offset in byte where the write should start 1491*bae9e67eSachartre * length - number of bytes to written 1492*bae9e67eSachartre * 1493*bae9e67eSachartre * Return Code: 1494*bae9e67eSachartre * n >= 0 - success, n indicates the number of bytes written 1495*bae9e67eSachartre * -1 - error 1496*bae9e67eSachartre */ 1497*bae9e67eSachartre static ssize_t 1498*bae9e67eSachartre vd_slice_flabel_write(vd_t *vd, caddr_t data, size_t offset, size_t length) 1499*bae9e67eSachartre { 1500*bae9e67eSachartre uint_t limit = vd->flabel_limit * DEV_BSIZE; 1501*bae9e67eSachartre struct dk_label *label; 1502*bae9e67eSachartre struct dk_geom geom; 1503*bae9e67eSachartre struct vtoc vtoc; 1504*bae9e67eSachartre 1505*bae9e67eSachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 1506*bae9e67eSachartre ASSERT(vd->flabel != NULL); 1507*bae9e67eSachartre 1508*bae9e67eSachartre if (offset >= limit) 1509*bae9e67eSachartre return (0); 1510*bae9e67eSachartre 1511*bae9e67eSachartre /* 1512*bae9e67eSachartre * If this is a request to overwrite the VTOC disk label, check that 1513*bae9e67eSachartre * the new label is similar to the previous one and return that the 1514*bae9e67eSachartre * write was successful, but note that nothing is actually overwritten. 1515*bae9e67eSachartre */ 1516*bae9e67eSachartre if (vd->vdisk_label == VD_DISK_LABEL_VTOC && 1517*bae9e67eSachartre offset == 0 && length == DEV_BSIZE) { 1518*bae9e67eSachartre label = (struct dk_label *)data; 1519*bae9e67eSachartre 1520*bae9e67eSachartre /* check that this is a valid label */ 1521*bae9e67eSachartre if (label->dkl_magic != DKL_MAGIC || 1522*bae9e67eSachartre label->dkl_cksum != vd_lbl2cksum(label)) 1523*bae9e67eSachartre return (-1); 1524*bae9e67eSachartre 1525*bae9e67eSachartre /* check the vtoc and geometry */ 1526*bae9e67eSachartre vd_label_to_vtocgeom(label, &vtoc, &geom); 1527*bae9e67eSachartre if (vd_slice_geom_isvalid(vd, &geom) && 1528*bae9e67eSachartre vd_slice_vtoc_isvalid(vd, &vtoc)) 1529*bae9e67eSachartre return (length); 1530*bae9e67eSachartre } 1531*bae9e67eSachartre 1532*bae9e67eSachartre /* fail any other write */ 1533*bae9e67eSachartre return (-1); 1534*bae9e67eSachartre } 1535*bae9e67eSachartre 1536*bae9e67eSachartre /* 1537*bae9e67eSachartre * Function: 1538*bae9e67eSachartre * vd_slice_fake_rdwr 1539*bae9e67eSachartre * 1540*bae9e67eSachartre * Description: 1541*bae9e67eSachartre * This function simulates a raw read or write operation to a single-slice 1542*bae9e67eSachartre * disk. It only handles the faked part of the operation i.e. I/Os to 1543*bae9e67eSachartre * blocks which have no mapping with the vdisk backend (I/Os to the 1544*bae9e67eSachartre * beginning and to the end of the vdisk). 1545*bae9e67eSachartre * 1546*bae9e67eSachartre * The function returns 0 is the operation is completed and it has been 1547*bae9e67eSachartre * entirely handled as a fake read or write. In that case, lengthp points 1548*bae9e67eSachartre * to the number of bytes not read or written. Values returned by datap 1549*bae9e67eSachartre * and blkp are undefined. 1550*bae9e67eSachartre * 1551*bae9e67eSachartre * If the fake operation has succeeded but the read or write is not 1552*bae9e67eSachartre * complete (i.e. the read/write operation extends beyond the blocks 1553*bae9e67eSachartre * we fake) then the function returns EAGAIN and datap, blkp and lengthp 1554*bae9e67eSachartre * pointers points to the parameters for completing the operation. 1555*bae9e67eSachartre * 1556*bae9e67eSachartre * In case of an error, for example if the slice is empty or parameters 1557*bae9e67eSachartre * are invalid, then the function returns a non-zero value different 1558*bae9e67eSachartre * from EAGAIN. In that case, the returned values of datap, blkp and 1559*bae9e67eSachartre * lengthp are undefined. 1560*bae9e67eSachartre * 1561*bae9e67eSachartre * Parameters: 1562*bae9e67eSachartre * vd - single-slice disk on which the operation is performed 1563*bae9e67eSachartre * slice - slice on which the operation is performed, 1564*bae9e67eSachartre * VD_SLICE_NONE indicates that the operation 1565*bae9e67eSachartre * is done using an absolute disk offset. 1566*bae9e67eSachartre * operation - operation to execute: read (VD_OP_BREAD) or 1567*bae9e67eSachartre * write (VD_OP_BWRITE). 1568*bae9e67eSachartre * datap - pointer to the buffer where data are read to 1569*bae9e67eSachartre * or written from. Return the pointer where remaining 1570*bae9e67eSachartre * data have to be read to or written from. 1571*bae9e67eSachartre * blkp - pointer to the starting block for the operation. 1572*bae9e67eSachartre * Return the starting block relative to the vdisk 1573*bae9e67eSachartre * backend for the remaining operation. 1574*bae9e67eSachartre * lengthp - pointer to the number of bytes to read or write. 1575*bae9e67eSachartre * This should be a multiple of DEV_BSIZE. Return the 1576*bae9e67eSachartre * remaining number of bytes to read or write. 1577*bae9e67eSachartre * 1578*bae9e67eSachartre * Return Code: 1579*bae9e67eSachartre * 0 - read/write operation is completed 1580*bae9e67eSachartre * EAGAIN - read/write operation is not completed 1581*bae9e67eSachartre * other values - error 1582*bae9e67eSachartre */ 1583*bae9e67eSachartre static int 1584*bae9e67eSachartre vd_slice_fake_rdwr(vd_t *vd, int slice, int operation, caddr_t *datap, 1585*bae9e67eSachartre size_t *blkp, size_t *lengthp) 1586*bae9e67eSachartre { 1587*bae9e67eSachartre struct dk_label *label; 1588*bae9e67eSachartre caddr_t data; 1589*bae9e67eSachartre size_t blk, length, csize; 1590*bae9e67eSachartre size_t ablk, asize, aoff, alen; 1591*bae9e67eSachartre ssize_t n; 1592*bae9e67eSachartre int sec, status; 1593*bae9e67eSachartre 1594*bae9e67eSachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 1595*bae9e67eSachartre ASSERT(slice != 0); 1596*bae9e67eSachartre 1597*bae9e67eSachartre data = *datap; 1598*bae9e67eSachartre blk = *blkp; 1599*bae9e67eSachartre length = *lengthp; 1600*bae9e67eSachartre 1601*bae9e67eSachartre /* 1602*bae9e67eSachartre * If this is not a raw I/O or an I/O from a full disk slice then 1603*bae9e67eSachartre * this is an I/O to/from an empty slice. 1604*bae9e67eSachartre */ 1605*bae9e67eSachartre if (slice != VD_SLICE_NONE && 1606*bae9e67eSachartre (slice != VD_ENTIRE_DISK_SLICE || 1607*bae9e67eSachartre vd->vdisk_label != VD_DISK_LABEL_VTOC) && 1608*bae9e67eSachartre (slice != VD_EFI_WD_SLICE || 1609*bae9e67eSachartre vd->vdisk_label != VD_DISK_LABEL_EFI)) { 1610*bae9e67eSachartre return (EIO); 1611*bae9e67eSachartre } 1612*bae9e67eSachartre 1613*bae9e67eSachartre if (length % DEV_BSIZE != 0) 1614*bae9e67eSachartre return (EINVAL); 1615*bae9e67eSachartre 1616*bae9e67eSachartre /* handle any I/O with the fake label */ 1617*bae9e67eSachartre if (operation == VD_OP_BWRITE) 1618*bae9e67eSachartre n = vd_slice_flabel_write(vd, data, blk * DEV_BSIZE, length); 1619*bae9e67eSachartre else 1620*bae9e67eSachartre n = vd_slice_flabel_read(vd, data, blk * DEV_BSIZE, length); 1621*bae9e67eSachartre 1622*bae9e67eSachartre if (n == -1) 1623*bae9e67eSachartre return (EINVAL); 1624*bae9e67eSachartre 1625*bae9e67eSachartre ASSERT(n % DEV_BSIZE == 0); 1626*bae9e67eSachartre 1627*bae9e67eSachartre /* adjust I/O arguments */ 1628*bae9e67eSachartre data += n; 1629*bae9e67eSachartre blk += n / DEV_BSIZE; 1630*bae9e67eSachartre length -= n; 1631*bae9e67eSachartre 1632*bae9e67eSachartre /* check if there's something else to process */ 1633*bae9e67eSachartre if (length == 0) { 1634*bae9e67eSachartre status = 0; 1635*bae9e67eSachartre goto done; 1636*bae9e67eSachartre } 1637*bae9e67eSachartre 1638*bae9e67eSachartre if (vd->vdisk_label == VD_DISK_LABEL_VTOC && 1639*bae9e67eSachartre slice == VD_ENTIRE_DISK_SLICE) { 1640*bae9e67eSachartre status = EAGAIN; 1641*bae9e67eSachartre goto done; 1642*bae9e67eSachartre } 1643*bae9e67eSachartre 1644*bae9e67eSachartre if (vd->vdisk_label == VD_DISK_LABEL_EFI) { 1645*bae9e67eSachartre asize = EFI_MIN_RESV_SIZE + 33; 1646*bae9e67eSachartre ablk = vd->vdisk_size - asize; 1647*bae9e67eSachartre } else { 1648*bae9e67eSachartre ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC); 1649*bae9e67eSachartre ASSERT(vd->dk_geom.dkg_apc == 0); 1650*bae9e67eSachartre 1651*bae9e67eSachartre csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect; 1652*bae9e67eSachartre ablk = vd->dk_geom.dkg_ncyl * csize; 1653*bae9e67eSachartre asize = vd->dk_geom.dkg_acyl * csize; 1654*bae9e67eSachartre } 1655*bae9e67eSachartre 1656*bae9e67eSachartre alen = length / DEV_BSIZE; 1657*bae9e67eSachartre aoff = blk; 1658*bae9e67eSachartre 1659*bae9e67eSachartre /* if we have reached the last block then the I/O is completed */ 1660*bae9e67eSachartre if (aoff == ablk + asize) { 1661*bae9e67eSachartre status = 0; 1662*bae9e67eSachartre goto done; 1663*bae9e67eSachartre } 1664*bae9e67eSachartre 1665*bae9e67eSachartre /* if we are past the last block then return an error */ 1666*bae9e67eSachartre if (aoff > ablk + asize) 1667*bae9e67eSachartre return (EIO); 1668*bae9e67eSachartre 1669*bae9e67eSachartre /* check if there is any I/O to end of the disk */ 1670*bae9e67eSachartre if (aoff + alen < ablk) { 1671*bae9e67eSachartre status = EAGAIN; 1672*bae9e67eSachartre goto done; 1673*bae9e67eSachartre } 1674*bae9e67eSachartre 1675*bae9e67eSachartre /* we don't allow any write to the end of the disk */ 1676*bae9e67eSachartre if (operation == VD_OP_BWRITE) 1677*bae9e67eSachartre return (EIO); 1678*bae9e67eSachartre 1679*bae9e67eSachartre if (aoff < ablk) { 1680*bae9e67eSachartre alen -= (ablk - aoff); 1681*bae9e67eSachartre aoff = ablk; 1682*bae9e67eSachartre } 1683*bae9e67eSachartre 1684*bae9e67eSachartre if (aoff + alen > ablk + asize) { 1685*bae9e67eSachartre alen = ablk + asize - aoff; 1686*bae9e67eSachartre } 1687*bae9e67eSachartre 1688*bae9e67eSachartre alen *= DEV_BSIZE; 1689*bae9e67eSachartre 1690*bae9e67eSachartre if (operation == VD_OP_BREAD) { 1691*bae9e67eSachartre bzero(data + (aoff - blk) * DEV_BSIZE, alen); 1692*bae9e67eSachartre 1693*bae9e67eSachartre if (vd->vdisk_label == VD_DISK_LABEL_VTOC) { 1694*bae9e67eSachartre /* check if we read backup labels */ 1695*bae9e67eSachartre label = VD_LABEL_VTOC(vd); 1696*bae9e67eSachartre ablk += (label->dkl_acyl - 1) * csize + 1697*bae9e67eSachartre (label->dkl_nhead - 1) * label->dkl_nsect; 1698*bae9e67eSachartre 1699*bae9e67eSachartre for (sec = 1; (sec < 5 * 2 + 1); sec += 2) { 1700*bae9e67eSachartre 1701*bae9e67eSachartre if (ablk + sec >= blk && 1702*bae9e67eSachartre ablk + sec < blk + (length / DEV_BSIZE)) { 1703*bae9e67eSachartre bcopy(label, data + 1704*bae9e67eSachartre (ablk + sec - blk) * DEV_BSIZE, 1705*bae9e67eSachartre sizeof (struct dk_label)); 1706*bae9e67eSachartre } 1707*bae9e67eSachartre } 1708*bae9e67eSachartre } 1709*bae9e67eSachartre } 1710*bae9e67eSachartre 1711*bae9e67eSachartre length -= alen; 1712*bae9e67eSachartre 1713*bae9e67eSachartre status = (length == 0)? 0: EAGAIN; 1714*bae9e67eSachartre 1715*bae9e67eSachartre done: 1716*bae9e67eSachartre ASSERT(length == 0 || blk >= vd->flabel_limit); 1717*bae9e67eSachartre 1718*bae9e67eSachartre /* 1719*bae9e67eSachartre * Return the parameters for the remaining I/O. The starting block is 1720*bae9e67eSachartre * adjusted so that it is relative to the vdisk backend. 1721*bae9e67eSachartre */ 1722*bae9e67eSachartre *datap = data; 1723*bae9e67eSachartre *blkp = blk - vd->flabel_limit; 1724*bae9e67eSachartre *lengthp = length; 1725*bae9e67eSachartre 1726*bae9e67eSachartre return (status); 1727*bae9e67eSachartre } 1728*bae9e67eSachartre 1729*bae9e67eSachartre /* 1730205eeb1aSlm66018 * Return Values 1731205eeb1aSlm66018 * EINPROGRESS - operation was successfully started 1732205eeb1aSlm66018 * EIO - encountered LDC (aka. task error) 1733205eeb1aSlm66018 * 0 - operation completed successfully 1734205eeb1aSlm66018 * 1735205eeb1aSlm66018 * Side Effect 1736205eeb1aSlm66018 * sets request->status = <disk operation status> 1737205eeb1aSlm66018 */ 17381ae08745Sheppo static int 1739d10e4ef2Snarayan vd_start_bio(vd_task_t *task) 17401ae08745Sheppo { 17414bac2208Snarayan int rv, status = 0; 1742d10e4ef2Snarayan vd_t *vd = task->vd; 1743d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 1744d10e4ef2Snarayan struct buf *buf = &task->buf; 17454bac2208Snarayan uint8_t mtype; 17463c96341aSnarayan int slice; 1747047ba61eSachartre char *bufaddr = 0; 1748047ba61eSachartre size_t buflen; 1749*bae9e67eSachartre size_t offset, length, nbytes; 1750d10e4ef2Snarayan 1751d10e4ef2Snarayan ASSERT(vd != NULL); 1752d10e4ef2Snarayan ASSERT(request != NULL); 17533c96341aSnarayan 17543c96341aSnarayan slice = request->slice; 17553c96341aSnarayan 175687a7269eSachartre ASSERT(slice == VD_SLICE_NONE || slice < vd->nslices); 1757d10e4ef2Snarayan ASSERT((request->operation == VD_OP_BREAD) || 1758d10e4ef2Snarayan (request->operation == VD_OP_BWRITE)); 1759d10e4ef2Snarayan 1760205eeb1aSlm66018 if (request->nbytes == 0) { 1761205eeb1aSlm66018 /* no service for trivial requests */ 1762205eeb1aSlm66018 request->status = EINVAL; 1763205eeb1aSlm66018 return (0); 1764205eeb1aSlm66018 } 17651ae08745Sheppo 1766d10e4ef2Snarayan PR1("%s %lu bytes at block %lu", 1767d10e4ef2Snarayan (request->operation == VD_OP_BREAD) ? "Read" : "Write", 1768d10e4ef2Snarayan request->nbytes, request->addr); 17691ae08745Sheppo 1770047ba61eSachartre /* 1771047ba61eSachartre * We have to check the open flags because the functions processing 1772047ba61eSachartre * the read/write request will not do it. 1773047ba61eSachartre */ 1774047ba61eSachartre if (request->operation == VD_OP_BWRITE && !(vd->open_flags & FWRITE)) { 1775047ba61eSachartre PR0("write fails because backend is opened read-only"); 1776047ba61eSachartre request->nbytes = 0; 1777047ba61eSachartre request->status = EROFS; 1778047ba61eSachartre return (0); 1779047ba61eSachartre } 1780d10e4ef2Snarayan 17814bac2208Snarayan mtype = (&vd->inband_task == task) ? LDC_SHADOW_MAP : LDC_DIRECT_MAP; 17824bac2208Snarayan 17834bac2208Snarayan /* Map memory exported by client */ 17844bac2208Snarayan status = ldc_mem_map(task->mhdl, request->cookie, request->ncookies, 17854bac2208Snarayan mtype, (request->operation == VD_OP_BREAD) ? LDC_MEM_W : LDC_MEM_R, 1786047ba61eSachartre &bufaddr, NULL); 17874bac2208Snarayan if (status != 0) { 17883af08d82Slm66018 PR0("ldc_mem_map() returned err %d ", status); 1789205eeb1aSlm66018 return (EIO); 1790d10e4ef2Snarayan } 1791d10e4ef2Snarayan 1792*bae9e67eSachartre /* 1793*bae9e67eSachartre * The buffer size has to be 8-byte aligned, so the client should have 1794*bae9e67eSachartre * sent a buffer which size is roundup to the next 8-byte aligned value. 1795*bae9e67eSachartre */ 1796*bae9e67eSachartre buflen = P2ROUNDUP(request->nbytes, 8); 1797047ba61eSachartre 1798047ba61eSachartre status = ldc_mem_acquire(task->mhdl, 0, buflen); 17994bac2208Snarayan if (status != 0) { 18004bac2208Snarayan (void) ldc_mem_unmap(task->mhdl); 18013af08d82Slm66018 PR0("ldc_mem_acquire() returned err %d ", status); 1802205eeb1aSlm66018 return (EIO); 18034bac2208Snarayan } 18044bac2208Snarayan 1805*bae9e67eSachartre offset = request->addr; 1806*bae9e67eSachartre nbytes = request->nbytes; 1807*bae9e67eSachartre length = nbytes; 1808*bae9e67eSachartre 1809*bae9e67eSachartre /* default number of byte returned by the I/O */ 1810*bae9e67eSachartre request->nbytes = 0; 1811*bae9e67eSachartre 1812*bae9e67eSachartre if (vd->vdisk_type == VD_DISK_TYPE_SLICE) { 1813*bae9e67eSachartre 1814*bae9e67eSachartre if (slice != 0) { 1815*bae9e67eSachartre /* handle any fake I/O */ 1816*bae9e67eSachartre rv = vd_slice_fake_rdwr(vd, slice, request->operation, 1817*bae9e67eSachartre &bufaddr, &offset, &length); 1818*bae9e67eSachartre 1819*bae9e67eSachartre /* record the number of bytes from the fake I/O */ 1820*bae9e67eSachartre request->nbytes = nbytes - length; 1821*bae9e67eSachartre 1822*bae9e67eSachartre if (rv == 0) { 1823*bae9e67eSachartre request->status = 0; 1824*bae9e67eSachartre goto io_done; 1825*bae9e67eSachartre } 1826*bae9e67eSachartre 1827*bae9e67eSachartre if (rv != EAGAIN) { 18283c96341aSnarayan request->nbytes = 0; 1829205eeb1aSlm66018 request->status = EIO; 1830*bae9e67eSachartre goto io_done; 18313c96341aSnarayan } 1832*bae9e67eSachartre 1833*bae9e67eSachartre /* 1834*bae9e67eSachartre * If we return with EAGAIN then this means that there 1835*bae9e67eSachartre * are still data to read or write. 1836*bae9e67eSachartre */ 1837*bae9e67eSachartre ASSERT(length != 0); 1838*bae9e67eSachartre 1839*bae9e67eSachartre /* 1840*bae9e67eSachartre * We need to continue the I/O from the slice backend to 1841*bae9e67eSachartre * complete the request. The variables bufaddr, offset 1842*bae9e67eSachartre * and length have been adjusted to have the right 1843*bae9e67eSachartre * information to do the remaining I/O from the backend. 1844*bae9e67eSachartre * The backend is entirely mapped to slice 0 so we just 1845*bae9e67eSachartre * have to complete the I/O from that slice. 1846*bae9e67eSachartre */ 1847*bae9e67eSachartre slice = 0; 1848*bae9e67eSachartre } 1849*bae9e67eSachartre 1850*bae9e67eSachartre } else if ((slice == VD_SLICE_NONE) && (!vd->file)) { 1851*bae9e67eSachartre 185287a7269eSachartre /* 185387a7269eSachartre * This is not a disk image so it is a real disk. We 185487a7269eSachartre * assume that the underlying device driver supports 185587a7269eSachartre * USCSICMD ioctls. This is the case of all SCSI devices 185687a7269eSachartre * (sd, ssd...). 185787a7269eSachartre * 185887a7269eSachartre * In the future if we have non-SCSI disks we would need 185987a7269eSachartre * to invoke the appropriate function to do I/O using an 186017cadca8Slm66018 * absolute disk offset (for example using DIOCTL_RWCMD 186187a7269eSachartre * for IDE disks). 186287a7269eSachartre */ 1863*bae9e67eSachartre rv = vd_scsi_rdwr(vd, request->operation, bufaddr, offset, 1864*bae9e67eSachartre length); 186587a7269eSachartre if (rv != 0) { 1866*bae9e67eSachartre request->status = EIO; 1867*bae9e67eSachartre } else { 1868*bae9e67eSachartre request->nbytes = length; 1869*bae9e67eSachartre request->status = 0; 1870*bae9e67eSachartre } 1871*bae9e67eSachartre goto io_done; 1872*bae9e67eSachartre } 1873*bae9e67eSachartre 1874*bae9e67eSachartre /* Start the block I/O */ 1875*bae9e67eSachartre if (vd->file) { 1876*bae9e67eSachartre rv = vd_file_rw(vd, slice, request->operation, bufaddr, offset, 1877*bae9e67eSachartre length); 1878*bae9e67eSachartre if (rv < 0) { 187987a7269eSachartre request->nbytes = 0; 1880205eeb1aSlm66018 request->status = EIO; 188187a7269eSachartre } else { 1882*bae9e67eSachartre request->nbytes += rv; 1883205eeb1aSlm66018 request->status = 0; 188487a7269eSachartre } 188587a7269eSachartre } else { 1886047ba61eSachartre bioinit(buf); 1887047ba61eSachartre buf->b_flags = B_BUSY; 1888*bae9e67eSachartre buf->b_bcount = length; 1889*bae9e67eSachartre buf->b_lblkno = offset; 1890*bae9e67eSachartre buf->b_bufsize = buflen; 1891047ba61eSachartre buf->b_edev = vd->dev[slice]; 1892047ba61eSachartre buf->b_un.b_addr = bufaddr; 1893047ba61eSachartre buf->b_flags |= (request->operation == VD_OP_BREAD)? 1894047ba61eSachartre B_READ : B_WRITE; 1895047ba61eSachartre 1896*bae9e67eSachartre request->status = ldi_strategy(vd->ldi_handle[slice], buf); 1897205eeb1aSlm66018 1898205eeb1aSlm66018 /* 1899205eeb1aSlm66018 * This is to indicate to the caller that the request 1900205eeb1aSlm66018 * needs to be finished by vd_complete_bio() by calling 1901205eeb1aSlm66018 * biowait() there and waiting for that to return before 1902205eeb1aSlm66018 * triggering the notification of the vDisk client. 1903205eeb1aSlm66018 * 1904205eeb1aSlm66018 * This is necessary when writing to real disks as 1905205eeb1aSlm66018 * otherwise calls to ldi_strategy() would be serialized 1906205eeb1aSlm66018 * behind the calls to biowait() and performance would 1907205eeb1aSlm66018 * suffer. 1908205eeb1aSlm66018 */ 1909205eeb1aSlm66018 if (request->status == 0) 191087a7269eSachartre return (EINPROGRESS); 1911047ba61eSachartre 1912047ba61eSachartre biofini(buf); 191387a7269eSachartre } 19143c96341aSnarayan 1915*bae9e67eSachartre io_done: 1916*bae9e67eSachartre /* Clean up after error or completion */ 1917047ba61eSachartre rv = ldc_mem_release(task->mhdl, 0, buflen); 19184bac2208Snarayan if (rv) { 19193af08d82Slm66018 PR0("ldc_mem_release() returned err %d ", rv); 1920205eeb1aSlm66018 status = EIO; 19214bac2208Snarayan } 19224bac2208Snarayan rv = ldc_mem_unmap(task->mhdl); 19234bac2208Snarayan if (rv) { 1924205eeb1aSlm66018 PR0("ldc_mem_unmap() returned err %d ", rv); 1925205eeb1aSlm66018 status = EIO; 19264bac2208Snarayan } 19274bac2208Snarayan 1928d10e4ef2Snarayan return (status); 1929d10e4ef2Snarayan } 1930d10e4ef2Snarayan 1931205eeb1aSlm66018 /* 1932205eeb1aSlm66018 * This function should only be called from vd_notify to ensure that requests 1933205eeb1aSlm66018 * are responded to in the order that they are received. 1934205eeb1aSlm66018 */ 1935d10e4ef2Snarayan static int 1936d10e4ef2Snarayan send_msg(ldc_handle_t ldc_handle, void *msg, size_t msglen) 1937d10e4ef2Snarayan { 19383af08d82Slm66018 int status; 1939d10e4ef2Snarayan size_t nbytes; 1940d10e4ef2Snarayan 19413af08d82Slm66018 do { 1942d10e4ef2Snarayan nbytes = msglen; 1943d10e4ef2Snarayan status = ldc_write(ldc_handle, msg, &nbytes); 19443af08d82Slm66018 if (status != EWOULDBLOCK) 19453af08d82Slm66018 break; 19463af08d82Slm66018 drv_usecwait(vds_ldc_delay); 19473af08d82Slm66018 } while (status == EWOULDBLOCK); 1948d10e4ef2Snarayan 1949d10e4ef2Snarayan if (status != 0) { 19503af08d82Slm66018 if (status != ECONNRESET) 19513af08d82Slm66018 PR0("ldc_write() returned errno %d", status); 1952d10e4ef2Snarayan return (status); 1953d10e4ef2Snarayan } else if (nbytes != msglen) { 19543af08d82Slm66018 PR0("ldc_write() performed only partial write"); 1955d10e4ef2Snarayan return (EIO); 1956d10e4ef2Snarayan } 1957d10e4ef2Snarayan 1958d10e4ef2Snarayan PR1("SENT %lu bytes", msglen); 1959d10e4ef2Snarayan return (0); 1960d10e4ef2Snarayan } 1961d10e4ef2Snarayan 1962d10e4ef2Snarayan static void 1963d10e4ef2Snarayan vd_need_reset(vd_t *vd, boolean_t reset_ldc) 1964d10e4ef2Snarayan { 1965d10e4ef2Snarayan mutex_enter(&vd->lock); 1966d10e4ef2Snarayan vd->reset_state = B_TRUE; 1967d10e4ef2Snarayan vd->reset_ldc = reset_ldc; 1968d10e4ef2Snarayan mutex_exit(&vd->lock); 1969d10e4ef2Snarayan } 1970d10e4ef2Snarayan 1971d10e4ef2Snarayan /* 1972d10e4ef2Snarayan * Reset the state of the connection with a client, if needed; reset the LDC 1973d10e4ef2Snarayan * transport as well, if needed. This function should only be called from the 19743af08d82Slm66018 * "vd_recv_msg", as it waits for tasks - otherwise a deadlock can occur. 1975d10e4ef2Snarayan */ 1976d10e4ef2Snarayan static void 1977d10e4ef2Snarayan vd_reset_if_needed(vd_t *vd) 1978d10e4ef2Snarayan { 1979d10e4ef2Snarayan int status = 0; 1980d10e4ef2Snarayan 1981d10e4ef2Snarayan mutex_enter(&vd->lock); 1982d10e4ef2Snarayan if (!vd->reset_state) { 1983d10e4ef2Snarayan ASSERT(!vd->reset_ldc); 1984d10e4ef2Snarayan mutex_exit(&vd->lock); 1985d10e4ef2Snarayan return; 1986d10e4ef2Snarayan } 1987d10e4ef2Snarayan mutex_exit(&vd->lock); 1988d10e4ef2Snarayan 1989d10e4ef2Snarayan PR0("Resetting connection state with %s", VD_CLIENT(vd)); 1990d10e4ef2Snarayan 1991d10e4ef2Snarayan /* 1992d10e4ef2Snarayan * Let any asynchronous I/O complete before possibly pulling the rug 1993d10e4ef2Snarayan * out from under it; defer checking vd->reset_ldc, as one of the 1994d10e4ef2Snarayan * asynchronous tasks might set it 1995d10e4ef2Snarayan */ 1996d10e4ef2Snarayan ddi_taskq_wait(vd->completionq); 1997d10e4ef2Snarayan 19983c96341aSnarayan if (vd->file) { 1999da6c28aaSamw status = VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL); 20003c96341aSnarayan if (status) { 20013c96341aSnarayan PR0("VOP_FSYNC returned errno %d", status); 20023c96341aSnarayan } 20033c96341aSnarayan } 20043c96341aSnarayan 2005d10e4ef2Snarayan if ((vd->initialized & VD_DRING) && 2006d10e4ef2Snarayan ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0)) 20073af08d82Slm66018 PR0("ldc_mem_dring_unmap() returned errno %d", status); 2008d10e4ef2Snarayan 20093af08d82Slm66018 vd_free_dring_task(vd); 20103af08d82Slm66018 20113af08d82Slm66018 /* Free the staging buffer for msgs */ 20123af08d82Slm66018 if (vd->vio_msgp != NULL) { 20133af08d82Slm66018 kmem_free(vd->vio_msgp, vd->max_msglen); 20143af08d82Slm66018 vd->vio_msgp = NULL; 2015d10e4ef2Snarayan } 2016d10e4ef2Snarayan 20173af08d82Slm66018 /* Free the inband message buffer */ 20183af08d82Slm66018 if (vd->inband_task.msg != NULL) { 20193af08d82Slm66018 kmem_free(vd->inband_task.msg, vd->max_msglen); 20203af08d82Slm66018 vd->inband_task.msg = NULL; 20213af08d82Slm66018 } 2022d10e4ef2Snarayan 2023d10e4ef2Snarayan mutex_enter(&vd->lock); 20243af08d82Slm66018 20253af08d82Slm66018 if (vd->reset_ldc) 20263af08d82Slm66018 PR0("taking down LDC channel"); 2027e1ebb9ecSlm66018 if (vd->reset_ldc && ((status = ldc_down(vd->ldc_handle)) != 0)) 20283af08d82Slm66018 PR0("ldc_down() returned errno %d", status); 2029d10e4ef2Snarayan 20302f5224aeSachartre /* Reset exclusive access rights */ 20312f5224aeSachartre vd_reset_access(vd); 20322f5224aeSachartre 2033d10e4ef2Snarayan vd->initialized &= ~(VD_SID | VD_SEQ_NUM | VD_DRING); 2034d10e4ef2Snarayan vd->state = VD_STATE_INIT; 2035d10e4ef2Snarayan vd->max_msglen = sizeof (vio_msg_t); /* baseline vio message size */ 2036d10e4ef2Snarayan 20373af08d82Slm66018 /* Allocate the staging buffer */ 20383af08d82Slm66018 vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP); 20393af08d82Slm66018 20403af08d82Slm66018 PR0("calling ldc_up\n"); 20413af08d82Slm66018 (void) ldc_up(vd->ldc_handle); 20423af08d82Slm66018 2043d10e4ef2Snarayan vd->reset_state = B_FALSE; 2044d10e4ef2Snarayan vd->reset_ldc = B_FALSE; 20453af08d82Slm66018 2046d10e4ef2Snarayan mutex_exit(&vd->lock); 2047d10e4ef2Snarayan } 2048d10e4ef2Snarayan 20493af08d82Slm66018 static void vd_recv_msg(void *arg); 20503af08d82Slm66018 20513af08d82Slm66018 static void 20523af08d82Slm66018 vd_mark_in_reset(vd_t *vd) 20533af08d82Slm66018 { 20543af08d82Slm66018 int status; 20553af08d82Slm66018 20563af08d82Slm66018 PR0("vd_mark_in_reset: marking vd in reset\n"); 20573af08d82Slm66018 20583af08d82Slm66018 vd_need_reset(vd, B_FALSE); 20593af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, DDI_SLEEP); 20603af08d82Slm66018 if (status == DDI_FAILURE) { 20613af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 20623af08d82Slm66018 vd_need_reset(vd, B_TRUE); 20633af08d82Slm66018 return; 20643af08d82Slm66018 } 20653af08d82Slm66018 } 20663af08d82Slm66018 2067d10e4ef2Snarayan static int 20683c96341aSnarayan vd_mark_elem_done(vd_t *vd, int idx, int elem_status, int elem_nbytes) 2069d10e4ef2Snarayan { 2070d10e4ef2Snarayan boolean_t accepted; 2071d10e4ef2Snarayan int status; 2072d10e4ef2Snarayan vd_dring_entry_t *elem = VD_DRING_ELEM(idx); 2073d10e4ef2Snarayan 20743af08d82Slm66018 if (vd->reset_state) 20753af08d82Slm66018 return (0); 2076d10e4ef2Snarayan 2077d10e4ef2Snarayan /* Acquire the element */ 20783af08d82Slm66018 if (!vd->reset_state && 20793af08d82Slm66018 (status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) { 20803af08d82Slm66018 if (status == ECONNRESET) { 20813af08d82Slm66018 vd_mark_in_reset(vd); 20823af08d82Slm66018 return (0); 20833af08d82Slm66018 } else { 20843af08d82Slm66018 PR0("ldc_mem_dring_acquire() returned errno %d", 20853af08d82Slm66018 status); 2086d10e4ef2Snarayan return (status); 2087d10e4ef2Snarayan } 20883af08d82Slm66018 } 2089d10e4ef2Snarayan 2090d10e4ef2Snarayan /* Set the element's status and mark it done */ 2091d10e4ef2Snarayan accepted = (elem->hdr.dstate == VIO_DESC_ACCEPTED); 2092d10e4ef2Snarayan if (accepted) { 20933c96341aSnarayan elem->payload.nbytes = elem_nbytes; 2094d10e4ef2Snarayan elem->payload.status = elem_status; 2095d10e4ef2Snarayan elem->hdr.dstate = VIO_DESC_DONE; 2096d10e4ef2Snarayan } else { 2097d10e4ef2Snarayan /* Perhaps client timed out waiting for I/O... */ 20983af08d82Slm66018 PR0("element %u no longer \"accepted\"", idx); 2099d10e4ef2Snarayan VD_DUMP_DRING_ELEM(elem); 2100d10e4ef2Snarayan } 2101d10e4ef2Snarayan /* Release the element */ 21023af08d82Slm66018 if (!vd->reset_state && 21033af08d82Slm66018 (status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) { 21043af08d82Slm66018 if (status == ECONNRESET) { 21053af08d82Slm66018 vd_mark_in_reset(vd); 21063af08d82Slm66018 return (0); 21073af08d82Slm66018 } else { 21083af08d82Slm66018 PR0("ldc_mem_dring_release() returned errno %d", 21093af08d82Slm66018 status); 2110d10e4ef2Snarayan return (status); 2111d10e4ef2Snarayan } 21123af08d82Slm66018 } 2113d10e4ef2Snarayan 2114d10e4ef2Snarayan return (accepted ? 0 : EINVAL); 2115d10e4ef2Snarayan } 2116d10e4ef2Snarayan 2117205eeb1aSlm66018 /* 2118205eeb1aSlm66018 * Return Values 2119205eeb1aSlm66018 * 0 - operation completed successfully 2120205eeb1aSlm66018 * EIO - encountered LDC / task error 2121205eeb1aSlm66018 * 2122205eeb1aSlm66018 * Side Effect 2123205eeb1aSlm66018 * sets request->status = <disk operation status> 2124205eeb1aSlm66018 */ 2125205eeb1aSlm66018 static int 2126205eeb1aSlm66018 vd_complete_bio(vd_task_t *task) 2127d10e4ef2Snarayan { 2128d10e4ef2Snarayan int status = 0; 2129205eeb1aSlm66018 int rv = 0; 2130d10e4ef2Snarayan vd_t *vd = task->vd; 2131d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 2132d10e4ef2Snarayan struct buf *buf = &task->buf; 2133d10e4ef2Snarayan 2134d10e4ef2Snarayan 2135d10e4ef2Snarayan ASSERT(vd != NULL); 2136d10e4ef2Snarayan ASSERT(request != NULL); 2137d10e4ef2Snarayan ASSERT(task->msg != NULL); 2138d10e4ef2Snarayan ASSERT(task->msglen >= sizeof (*task->msg)); 21393c96341aSnarayan ASSERT(!vd->file); 2140*bae9e67eSachartre ASSERT(request->slice != VD_SLICE_NONE || (!vd_slice_single_slice && 2141*bae9e67eSachartre vd->vdisk_type == VD_DISK_TYPE_SLICE)); 2142d10e4ef2Snarayan 2143205eeb1aSlm66018 /* Wait for the I/O to complete [ call to ldi_strategy(9f) ] */ 2144d10e4ef2Snarayan request->status = biowait(buf); 2145d10e4ef2Snarayan 2146*bae9e67eSachartre /* Update the number of bytes read/written */ 2147*bae9e67eSachartre request->nbytes += buf->b_bcount - buf->b_resid; 21483c96341aSnarayan 21494bac2208Snarayan /* Release the buffer */ 21503af08d82Slm66018 if (!vd->reset_state) 2151*bae9e67eSachartre status = ldc_mem_release(task->mhdl, 0, buf->b_bufsize); 21524bac2208Snarayan if (status) { 21533af08d82Slm66018 PR0("ldc_mem_release() returned errno %d copying to " 21543af08d82Slm66018 "client", status); 21553af08d82Slm66018 if (status == ECONNRESET) { 21563af08d82Slm66018 vd_mark_in_reset(vd); 21573af08d82Slm66018 } 2158205eeb1aSlm66018 rv = EIO; 21591ae08745Sheppo } 21601ae08745Sheppo 21613af08d82Slm66018 /* Unmap the memory, even if in reset */ 21624bac2208Snarayan status = ldc_mem_unmap(task->mhdl); 21634bac2208Snarayan if (status) { 21643af08d82Slm66018 PR0("ldc_mem_unmap() returned errno %d copying to client", 21654bac2208Snarayan status); 21663af08d82Slm66018 if (status == ECONNRESET) { 21673af08d82Slm66018 vd_mark_in_reset(vd); 21683af08d82Slm66018 } 2169205eeb1aSlm66018 rv = EIO; 21704bac2208Snarayan } 21714bac2208Snarayan 2172d10e4ef2Snarayan biofini(buf); 21731ae08745Sheppo 2174205eeb1aSlm66018 return (rv); 2175205eeb1aSlm66018 } 2176205eeb1aSlm66018 2177205eeb1aSlm66018 /* 2178205eeb1aSlm66018 * Description: 2179205eeb1aSlm66018 * This function is called by the two functions called by a taskq 2180205eeb1aSlm66018 * [ vd_complete_notify() and vd_serial_notify()) ] to send the 2181205eeb1aSlm66018 * message to the client. 2182205eeb1aSlm66018 * 2183205eeb1aSlm66018 * Parameters: 2184205eeb1aSlm66018 * arg - opaque pointer to structure containing task to be completed 2185205eeb1aSlm66018 * 2186205eeb1aSlm66018 * Return Values 2187205eeb1aSlm66018 * None 2188205eeb1aSlm66018 */ 2189205eeb1aSlm66018 static void 2190205eeb1aSlm66018 vd_notify(vd_task_t *task) 2191205eeb1aSlm66018 { 2192205eeb1aSlm66018 int status; 2193205eeb1aSlm66018 2194205eeb1aSlm66018 ASSERT(task != NULL); 2195205eeb1aSlm66018 ASSERT(task->vd != NULL); 2196205eeb1aSlm66018 2197205eeb1aSlm66018 if (task->vd->reset_state) 2198205eeb1aSlm66018 return; 2199205eeb1aSlm66018 2200205eeb1aSlm66018 /* 2201205eeb1aSlm66018 * Send the "ack" or "nack" back to the client; if sending the message 2202205eeb1aSlm66018 * via LDC fails, arrange to reset both the connection state and LDC 2203205eeb1aSlm66018 * itself 2204205eeb1aSlm66018 */ 2205205eeb1aSlm66018 PR2("Sending %s", 2206205eeb1aSlm66018 (task->msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK"); 2207205eeb1aSlm66018 2208205eeb1aSlm66018 status = send_msg(task->vd->ldc_handle, task->msg, task->msglen); 2209205eeb1aSlm66018 switch (status) { 2210205eeb1aSlm66018 case 0: 2211205eeb1aSlm66018 break; 2212205eeb1aSlm66018 case ECONNRESET: 2213205eeb1aSlm66018 vd_mark_in_reset(task->vd); 2214205eeb1aSlm66018 break; 2215205eeb1aSlm66018 default: 2216205eeb1aSlm66018 PR0("initiating full reset"); 2217205eeb1aSlm66018 vd_need_reset(task->vd, B_TRUE); 2218205eeb1aSlm66018 break; 2219205eeb1aSlm66018 } 2220205eeb1aSlm66018 2221205eeb1aSlm66018 DTRACE_PROBE1(task__end, vd_task_t *, task); 2222205eeb1aSlm66018 } 2223205eeb1aSlm66018 2224205eeb1aSlm66018 /* 2225205eeb1aSlm66018 * Description: 2226205eeb1aSlm66018 * Mark the Dring entry as Done and (if necessary) send an ACK/NACK to 2227205eeb1aSlm66018 * the vDisk client 2228205eeb1aSlm66018 * 2229205eeb1aSlm66018 * Parameters: 2230205eeb1aSlm66018 * task - structure containing the request sent from client 2231205eeb1aSlm66018 * 2232205eeb1aSlm66018 * Return Values 2233205eeb1aSlm66018 * None 2234205eeb1aSlm66018 */ 2235205eeb1aSlm66018 static void 2236205eeb1aSlm66018 vd_complete_notify(vd_task_t *task) 2237205eeb1aSlm66018 { 2238205eeb1aSlm66018 int status = 0; 2239205eeb1aSlm66018 vd_t *vd = task->vd; 2240205eeb1aSlm66018 vd_dring_payload_t *request = task->request; 2241205eeb1aSlm66018 2242d10e4ef2Snarayan /* Update the dring element for a dring client */ 2243f0ca1d9aSsb155480 if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE_V1_0)) { 22443c96341aSnarayan status = vd_mark_elem_done(vd, task->index, 22453c96341aSnarayan request->status, request->nbytes); 22463af08d82Slm66018 if (status == ECONNRESET) 22473af08d82Slm66018 vd_mark_in_reset(vd); 22483af08d82Slm66018 } 22491ae08745Sheppo 2250d10e4ef2Snarayan /* 2251205eeb1aSlm66018 * If a transport error occurred while marking the element done or 2252205eeb1aSlm66018 * previously while executing the task, arrange to "nack" the message 2253205eeb1aSlm66018 * when the final task in the descriptor element range completes 2254d10e4ef2Snarayan */ 2255205eeb1aSlm66018 if ((status != 0) || (task->status != 0)) 2256d10e4ef2Snarayan task->msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 22571ae08745Sheppo 2258d10e4ef2Snarayan /* 2259d10e4ef2Snarayan * Only the final task for a range of elements will respond to and 2260d10e4ef2Snarayan * free the message 2261d10e4ef2Snarayan */ 22623af08d82Slm66018 if (task->type == VD_NONFINAL_RANGE_TASK) { 2263d10e4ef2Snarayan return; 22643af08d82Slm66018 } 22651ae08745Sheppo 2266205eeb1aSlm66018 vd_notify(task); 2267205eeb1aSlm66018 } 2268205eeb1aSlm66018 2269d10e4ef2Snarayan /* 2270205eeb1aSlm66018 * Description: 2271205eeb1aSlm66018 * This is the basic completion function called to handle inband data 2272205eeb1aSlm66018 * requests and handshake messages. All it needs to do is trigger a 2273205eeb1aSlm66018 * message to the client that the request is completed. 2274205eeb1aSlm66018 * 2275205eeb1aSlm66018 * Parameters: 2276205eeb1aSlm66018 * arg - opaque pointer to structure containing task to be completed 2277205eeb1aSlm66018 * 2278205eeb1aSlm66018 * Return Values 2279205eeb1aSlm66018 * None 2280d10e4ef2Snarayan */ 2281205eeb1aSlm66018 static void 2282205eeb1aSlm66018 vd_serial_notify(void *arg) 2283205eeb1aSlm66018 { 2284205eeb1aSlm66018 vd_task_t *task = (vd_task_t *)arg; 2285205eeb1aSlm66018 2286205eeb1aSlm66018 ASSERT(task != NULL); 2287205eeb1aSlm66018 vd_notify(task); 22881ae08745Sheppo } 22891ae08745Sheppo 22902f5224aeSachartre /* ARGSUSED */ 22912f5224aeSachartre static int 22922f5224aeSachartre vd_geom2dk_geom(void *vd_buf, size_t vd_buf_len, void *ioctl_arg) 22930a55fbb7Slm66018 { 22940a55fbb7Slm66018 VD_GEOM2DK_GEOM((vd_geom_t *)vd_buf, (struct dk_geom *)ioctl_arg); 22952f5224aeSachartre return (0); 22960a55fbb7Slm66018 } 22970a55fbb7Slm66018 22982f5224aeSachartre /* ARGSUSED */ 22992f5224aeSachartre static int 23002f5224aeSachartre vd_vtoc2vtoc(void *vd_buf, size_t vd_buf_len, void *ioctl_arg) 23010a55fbb7Slm66018 { 23020a55fbb7Slm66018 VD_VTOC2VTOC((vd_vtoc_t *)vd_buf, (struct vtoc *)ioctl_arg); 23032f5224aeSachartre return (0); 23040a55fbb7Slm66018 } 23050a55fbb7Slm66018 23060a55fbb7Slm66018 static void 23070a55fbb7Slm66018 dk_geom2vd_geom(void *ioctl_arg, void *vd_buf) 23080a55fbb7Slm66018 { 23090a55fbb7Slm66018 DK_GEOM2VD_GEOM((struct dk_geom *)ioctl_arg, (vd_geom_t *)vd_buf); 23100a55fbb7Slm66018 } 23110a55fbb7Slm66018 23120a55fbb7Slm66018 static void 23130a55fbb7Slm66018 vtoc2vd_vtoc(void *ioctl_arg, void *vd_buf) 23140a55fbb7Slm66018 { 23150a55fbb7Slm66018 VTOC2VD_VTOC((struct vtoc *)ioctl_arg, (vd_vtoc_t *)vd_buf); 23160a55fbb7Slm66018 } 23170a55fbb7Slm66018 23182f5224aeSachartre static int 23192f5224aeSachartre vd_get_efi_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg) 23204bac2208Snarayan { 23214bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 23224bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 23232f5224aeSachartre size_t data_len; 23242f5224aeSachartre 23252f5224aeSachartre data_len = vd_buf_len - (sizeof (vd_efi_t) - sizeof (uint64_t)); 23262f5224aeSachartre if (vd_efi->length > data_len) 23272f5224aeSachartre return (EINVAL); 23284bac2208Snarayan 23294bac2208Snarayan dk_efi->dki_lba = vd_efi->lba; 23304bac2208Snarayan dk_efi->dki_length = vd_efi->length; 23314bac2208Snarayan dk_efi->dki_data = kmem_zalloc(vd_efi->length, KM_SLEEP); 23322f5224aeSachartre return (0); 23334bac2208Snarayan } 23344bac2208Snarayan 23354bac2208Snarayan static void 23364bac2208Snarayan vd_get_efi_out(void *ioctl_arg, void *vd_buf) 23374bac2208Snarayan { 23384bac2208Snarayan int len; 23394bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 23404bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 23414bac2208Snarayan 23424bac2208Snarayan len = vd_efi->length; 23434bac2208Snarayan DK_EFI2VD_EFI(dk_efi, vd_efi); 23444bac2208Snarayan kmem_free(dk_efi->dki_data, len); 23454bac2208Snarayan } 23464bac2208Snarayan 23472f5224aeSachartre static int 23482f5224aeSachartre vd_set_efi_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg) 23494bac2208Snarayan { 23504bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 23514bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 23522f5224aeSachartre size_t data_len; 23532f5224aeSachartre 23542f5224aeSachartre data_len = vd_buf_len - (sizeof (vd_efi_t) - sizeof (uint64_t)); 23552f5224aeSachartre if (vd_efi->length > data_len) 23562f5224aeSachartre return (EINVAL); 23574bac2208Snarayan 23584bac2208Snarayan dk_efi->dki_data = kmem_alloc(vd_efi->length, KM_SLEEP); 23594bac2208Snarayan VD_EFI2DK_EFI(vd_efi, dk_efi); 23602f5224aeSachartre return (0); 23614bac2208Snarayan } 23624bac2208Snarayan 23634bac2208Snarayan static void 23644bac2208Snarayan vd_set_efi_out(void *ioctl_arg, void *vd_buf) 23654bac2208Snarayan { 23664bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 23674bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 23684bac2208Snarayan 23694bac2208Snarayan kmem_free(dk_efi->dki_data, vd_efi->length); 23704bac2208Snarayan } 23714bac2208Snarayan 23722f5224aeSachartre static int 23732f5224aeSachartre vd_scsicmd_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg) 23742f5224aeSachartre { 23752f5224aeSachartre size_t vd_scsi_len; 23762f5224aeSachartre vd_scsi_t *vd_scsi = (vd_scsi_t *)vd_buf; 23772f5224aeSachartre struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl_arg; 23782f5224aeSachartre 23792f5224aeSachartre /* check buffer size */ 23802f5224aeSachartre vd_scsi_len = VD_SCSI_SIZE; 23812f5224aeSachartre vd_scsi_len += P2ROUNDUP(vd_scsi->cdb_len, sizeof (uint64_t)); 23822f5224aeSachartre vd_scsi_len += P2ROUNDUP(vd_scsi->sense_len, sizeof (uint64_t)); 23832f5224aeSachartre vd_scsi_len += P2ROUNDUP(vd_scsi->datain_len, sizeof (uint64_t)); 23842f5224aeSachartre vd_scsi_len += P2ROUNDUP(vd_scsi->dataout_len, sizeof (uint64_t)); 23852f5224aeSachartre 23862f5224aeSachartre ASSERT(vd_scsi_len % sizeof (uint64_t) == 0); 23872f5224aeSachartre 23882f5224aeSachartre if (vd_buf_len < vd_scsi_len) 23892f5224aeSachartre return (EINVAL); 23902f5224aeSachartre 23912f5224aeSachartre /* set flags */ 23922f5224aeSachartre uscsi->uscsi_flags = vd_scsi_debug; 23932f5224aeSachartre 23942f5224aeSachartre if (vd_scsi->options & VD_SCSI_OPT_NORETRY) { 23952f5224aeSachartre uscsi->uscsi_flags |= USCSI_ISOLATE; 23962f5224aeSachartre uscsi->uscsi_flags |= USCSI_DIAGNOSE; 23972f5224aeSachartre } 23982f5224aeSachartre 23992f5224aeSachartre /* task attribute */ 24002f5224aeSachartre switch (vd_scsi->task_attribute) { 24012f5224aeSachartre case VD_SCSI_TASK_ACA: 24022f5224aeSachartre uscsi->uscsi_flags |= USCSI_HEAD; 24032f5224aeSachartre break; 24042f5224aeSachartre case VD_SCSI_TASK_HQUEUE: 24052f5224aeSachartre uscsi->uscsi_flags |= USCSI_HTAG; 24062f5224aeSachartre break; 24072f5224aeSachartre case VD_SCSI_TASK_ORDERED: 24082f5224aeSachartre uscsi->uscsi_flags |= USCSI_OTAG; 24092f5224aeSachartre break; 24102f5224aeSachartre default: 24112f5224aeSachartre uscsi->uscsi_flags |= USCSI_NOTAG; 24122f5224aeSachartre break; 24132f5224aeSachartre } 24142f5224aeSachartre 24152f5224aeSachartre /* timeout */ 24162f5224aeSachartre uscsi->uscsi_timeout = vd_scsi->timeout; 24172f5224aeSachartre 24182f5224aeSachartre /* cdb data */ 24192f5224aeSachartre uscsi->uscsi_cdb = (caddr_t)VD_SCSI_DATA_CDB(vd_scsi); 24202f5224aeSachartre uscsi->uscsi_cdblen = vd_scsi->cdb_len; 24212f5224aeSachartre 24222f5224aeSachartre /* sense buffer */ 24232f5224aeSachartre if (vd_scsi->sense_len != 0) { 24242f5224aeSachartre uscsi->uscsi_flags |= USCSI_RQENABLE; 24252f5224aeSachartre uscsi->uscsi_rqbuf = (caddr_t)VD_SCSI_DATA_SENSE(vd_scsi); 24262f5224aeSachartre uscsi->uscsi_rqlen = vd_scsi->sense_len; 24272f5224aeSachartre } 24282f5224aeSachartre 24292f5224aeSachartre if (vd_scsi->datain_len != 0 && vd_scsi->dataout_len != 0) { 24302f5224aeSachartre /* uscsi does not support read/write request */ 24312f5224aeSachartre return (EINVAL); 24322f5224aeSachartre } 24332f5224aeSachartre 24342f5224aeSachartre /* request data-in */ 24352f5224aeSachartre if (vd_scsi->datain_len != 0) { 24362f5224aeSachartre uscsi->uscsi_flags |= USCSI_READ; 24372f5224aeSachartre uscsi->uscsi_buflen = vd_scsi->datain_len; 24382f5224aeSachartre uscsi->uscsi_bufaddr = (char *)VD_SCSI_DATA_IN(vd_scsi); 24392f5224aeSachartre } 24402f5224aeSachartre 24412f5224aeSachartre /* request data-out */ 24422f5224aeSachartre if (vd_scsi->dataout_len != 0) { 24432f5224aeSachartre uscsi->uscsi_buflen = vd_scsi->dataout_len; 24442f5224aeSachartre uscsi->uscsi_bufaddr = (char *)VD_SCSI_DATA_OUT(vd_scsi); 24452f5224aeSachartre } 24462f5224aeSachartre 24472f5224aeSachartre return (0); 24482f5224aeSachartre } 24492f5224aeSachartre 24502f5224aeSachartre static void 24512f5224aeSachartre vd_scsicmd_out(void *ioctl_arg, void *vd_buf) 24522f5224aeSachartre { 24532f5224aeSachartre vd_scsi_t *vd_scsi = (vd_scsi_t *)vd_buf; 24542f5224aeSachartre struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl_arg; 24552f5224aeSachartre 24562f5224aeSachartre /* output fields */ 24572f5224aeSachartre vd_scsi->cmd_status = uscsi->uscsi_status; 24582f5224aeSachartre 24592f5224aeSachartre /* sense data */ 24602f5224aeSachartre if ((uscsi->uscsi_flags & USCSI_RQENABLE) && 24612f5224aeSachartre (uscsi->uscsi_status == STATUS_CHECK || 24622f5224aeSachartre uscsi->uscsi_status == STATUS_TERMINATED)) { 24632f5224aeSachartre vd_scsi->sense_status = uscsi->uscsi_rqstatus; 24642f5224aeSachartre if (uscsi->uscsi_rqstatus == STATUS_GOOD) 24652f5224aeSachartre vd_scsi->sense_len -= uscsi->uscsi_resid; 24662f5224aeSachartre else 24672f5224aeSachartre vd_scsi->sense_len = 0; 24682f5224aeSachartre } else { 24692f5224aeSachartre vd_scsi->sense_len = 0; 24702f5224aeSachartre } 24712f5224aeSachartre 24722f5224aeSachartre if (uscsi->uscsi_status != STATUS_GOOD) { 24732f5224aeSachartre vd_scsi->dataout_len = 0; 24742f5224aeSachartre vd_scsi->datain_len = 0; 24752f5224aeSachartre return; 24762f5224aeSachartre } 24772f5224aeSachartre 24782f5224aeSachartre if (uscsi->uscsi_flags & USCSI_READ) { 24792f5224aeSachartre /* request data (read) */ 24802f5224aeSachartre vd_scsi->datain_len -= uscsi->uscsi_resid; 24812f5224aeSachartre vd_scsi->dataout_len = 0; 24822f5224aeSachartre } else { 24832f5224aeSachartre /* request data (write) */ 24842f5224aeSachartre vd_scsi->datain_len = 0; 24852f5224aeSachartre vd_scsi->dataout_len -= uscsi->uscsi_resid; 24862f5224aeSachartre } 24872f5224aeSachartre } 24882f5224aeSachartre 2489690555a1Sachartre static ushort_t 24903c96341aSnarayan vd_lbl2cksum(struct dk_label *label) 24913c96341aSnarayan { 24923c96341aSnarayan int count; 2493690555a1Sachartre ushort_t sum, *sp; 24943c96341aSnarayan 24953c96341aSnarayan count = (sizeof (struct dk_label)) / (sizeof (short)) - 1; 2496690555a1Sachartre sp = (ushort_t *)label; 24973c96341aSnarayan sum = 0; 24983c96341aSnarayan while (count--) { 24993c96341aSnarayan sum ^= *sp++; 25003c96341aSnarayan } 25013c96341aSnarayan 25023c96341aSnarayan return (sum); 25033c96341aSnarayan } 25043c96341aSnarayan 250587a7269eSachartre /* 2506*bae9e67eSachartre * Copy information from a vtoc and dk_geom structures to a dk_label structure. 2507*bae9e67eSachartre */ 2508*bae9e67eSachartre static void 2509*bae9e67eSachartre vd_vtocgeom_to_label(struct vtoc *vtoc, struct dk_geom *geom, 2510*bae9e67eSachartre struct dk_label *label) 2511*bae9e67eSachartre { 2512*bae9e67eSachartre int i; 2513*bae9e67eSachartre 2514*bae9e67eSachartre ASSERT(vtoc->v_nparts == V_NUMPAR); 2515*bae9e67eSachartre ASSERT(vtoc->v_sanity == VTOC_SANE); 2516*bae9e67eSachartre 2517*bae9e67eSachartre bzero(label, sizeof (struct dk_label)); 2518*bae9e67eSachartre 2519*bae9e67eSachartre label->dkl_ncyl = geom->dkg_ncyl; 2520*bae9e67eSachartre label->dkl_acyl = geom->dkg_acyl; 2521*bae9e67eSachartre label->dkl_pcyl = geom->dkg_pcyl; 2522*bae9e67eSachartre label->dkl_nhead = geom->dkg_nhead; 2523*bae9e67eSachartre label->dkl_nsect = geom->dkg_nsect; 2524*bae9e67eSachartre label->dkl_intrlv = geom->dkg_intrlv; 2525*bae9e67eSachartre label->dkl_apc = geom->dkg_apc; 2526*bae9e67eSachartre label->dkl_rpm = geom->dkg_rpm; 2527*bae9e67eSachartre label->dkl_write_reinstruct = geom->dkg_write_reinstruct; 2528*bae9e67eSachartre label->dkl_read_reinstruct = geom->dkg_read_reinstruct; 2529*bae9e67eSachartre 2530*bae9e67eSachartre label->dkl_vtoc.v_nparts = V_NUMPAR; 2531*bae9e67eSachartre label->dkl_vtoc.v_sanity = VTOC_SANE; 2532*bae9e67eSachartre label->dkl_vtoc.v_version = vtoc->v_version; 2533*bae9e67eSachartre for (i = 0; i < V_NUMPAR; i++) { 2534*bae9e67eSachartre label->dkl_vtoc.v_timestamp[i] = vtoc->timestamp[i]; 2535*bae9e67eSachartre label->dkl_vtoc.v_part[i].p_tag = vtoc->v_part[i].p_tag; 2536*bae9e67eSachartre label->dkl_vtoc.v_part[i].p_flag = vtoc->v_part[i].p_flag; 2537*bae9e67eSachartre label->dkl_map[i].dkl_cylno = vtoc->v_part[i].p_start / 2538*bae9e67eSachartre (label->dkl_nhead * label->dkl_nsect); 2539*bae9e67eSachartre label->dkl_map[i].dkl_nblk = vtoc->v_part[i].p_size; 2540*bae9e67eSachartre } 2541*bae9e67eSachartre 2542*bae9e67eSachartre /* 2543*bae9e67eSachartre * The bootinfo array can not be copied with bcopy() because 2544*bae9e67eSachartre * elements are of type long in vtoc (so 64-bit) and of type 2545*bae9e67eSachartre * int in dk_vtoc (so 32-bit). 2546*bae9e67eSachartre */ 2547*bae9e67eSachartre label->dkl_vtoc.v_bootinfo[0] = vtoc->v_bootinfo[0]; 2548*bae9e67eSachartre label->dkl_vtoc.v_bootinfo[1] = vtoc->v_bootinfo[1]; 2549*bae9e67eSachartre label->dkl_vtoc.v_bootinfo[2] = vtoc->v_bootinfo[2]; 2550*bae9e67eSachartre bcopy(vtoc->v_asciilabel, label->dkl_asciilabel, LEN_DKL_ASCII); 2551*bae9e67eSachartre bcopy(vtoc->v_volume, label->dkl_vtoc.v_volume, LEN_DKL_VVOL); 2552*bae9e67eSachartre 2553*bae9e67eSachartre /* re-compute checksum */ 2554*bae9e67eSachartre label->dkl_magic = DKL_MAGIC; 2555*bae9e67eSachartre label->dkl_cksum = vd_lbl2cksum(label); 2556*bae9e67eSachartre } 2557*bae9e67eSachartre 2558*bae9e67eSachartre /* 2559*bae9e67eSachartre * Copy information from a dk_label structure to a vtoc and dk_geom structures. 2560*bae9e67eSachartre */ 2561*bae9e67eSachartre static void 2562*bae9e67eSachartre vd_label_to_vtocgeom(struct dk_label *label, struct vtoc *vtoc, 2563*bae9e67eSachartre struct dk_geom *geom) 2564*bae9e67eSachartre { 2565*bae9e67eSachartre int i; 2566*bae9e67eSachartre 2567*bae9e67eSachartre bzero(vtoc, sizeof (struct vtoc)); 2568*bae9e67eSachartre bzero(geom, sizeof (struct dk_geom)); 2569*bae9e67eSachartre 2570*bae9e67eSachartre geom->dkg_ncyl = label->dkl_ncyl; 2571*bae9e67eSachartre geom->dkg_acyl = label->dkl_acyl; 2572*bae9e67eSachartre geom->dkg_nhead = label->dkl_nhead; 2573*bae9e67eSachartre geom->dkg_nsect = label->dkl_nsect; 2574*bae9e67eSachartre geom->dkg_intrlv = label->dkl_intrlv; 2575*bae9e67eSachartre geom->dkg_apc = label->dkl_apc; 2576*bae9e67eSachartre geom->dkg_rpm = label->dkl_rpm; 2577*bae9e67eSachartre geom->dkg_pcyl = label->dkl_pcyl; 2578*bae9e67eSachartre geom->dkg_write_reinstruct = label->dkl_write_reinstruct; 2579*bae9e67eSachartre geom->dkg_read_reinstruct = label->dkl_read_reinstruct; 2580*bae9e67eSachartre 2581*bae9e67eSachartre vtoc->v_sanity = label->dkl_vtoc.v_sanity; 2582*bae9e67eSachartre vtoc->v_version = label->dkl_vtoc.v_version; 2583*bae9e67eSachartre vtoc->v_sectorsz = DEV_BSIZE; 2584*bae9e67eSachartre vtoc->v_nparts = label->dkl_vtoc.v_nparts; 2585*bae9e67eSachartre 2586*bae9e67eSachartre for (i = 0; i < vtoc->v_nparts; i++) { 2587*bae9e67eSachartre vtoc->v_part[i].p_tag = label->dkl_vtoc.v_part[i].p_tag; 2588*bae9e67eSachartre vtoc->v_part[i].p_flag = label->dkl_vtoc.v_part[i].p_flag; 2589*bae9e67eSachartre vtoc->v_part[i].p_start = label->dkl_map[i].dkl_cylno * 2590*bae9e67eSachartre (label->dkl_nhead * label->dkl_nsect); 2591*bae9e67eSachartre vtoc->v_part[i].p_size = label->dkl_map[i].dkl_nblk; 2592*bae9e67eSachartre vtoc->timestamp[i] = label->dkl_vtoc.v_timestamp[i]; 2593*bae9e67eSachartre } 2594*bae9e67eSachartre 2595*bae9e67eSachartre /* 2596*bae9e67eSachartre * The bootinfo array can not be copied with bcopy() because 2597*bae9e67eSachartre * elements are of type long in vtoc (so 64-bit) and of type 2598*bae9e67eSachartre * int in dk_vtoc (so 32-bit). 2599*bae9e67eSachartre */ 2600*bae9e67eSachartre vtoc->v_bootinfo[0] = label->dkl_vtoc.v_bootinfo[0]; 2601*bae9e67eSachartre vtoc->v_bootinfo[1] = label->dkl_vtoc.v_bootinfo[1]; 2602*bae9e67eSachartre vtoc->v_bootinfo[2] = label->dkl_vtoc.v_bootinfo[2]; 2603*bae9e67eSachartre bcopy(label->dkl_asciilabel, vtoc->v_asciilabel, LEN_DKL_ASCII); 2604*bae9e67eSachartre bcopy(label->dkl_vtoc.v_volume, vtoc->v_volume, LEN_DKL_VVOL); 2605*bae9e67eSachartre } 2606*bae9e67eSachartre 2607*bae9e67eSachartre /* 2608*bae9e67eSachartre * Check if a geometry is valid for a single-slice disk. A geometry is 2609*bae9e67eSachartre * considered valid if the main attributes of the geometry match with the 2610*bae9e67eSachartre * attributes of the fake geometry we have created. 2611*bae9e67eSachartre */ 2612*bae9e67eSachartre static boolean_t 2613*bae9e67eSachartre vd_slice_geom_isvalid(vd_t *vd, struct dk_geom *geom) 2614*bae9e67eSachartre { 2615*bae9e67eSachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 2616*bae9e67eSachartre ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC); 2617*bae9e67eSachartre 2618*bae9e67eSachartre if (geom->dkg_ncyl != vd->dk_geom.dkg_ncyl || 2619*bae9e67eSachartre geom->dkg_acyl != vd->dk_geom.dkg_acyl || 2620*bae9e67eSachartre geom->dkg_nsect != vd->dk_geom.dkg_nsect || 2621*bae9e67eSachartre geom->dkg_pcyl != vd->dk_geom.dkg_pcyl) 2622*bae9e67eSachartre return (B_FALSE); 2623*bae9e67eSachartre 2624*bae9e67eSachartre return (B_TRUE); 2625*bae9e67eSachartre } 2626*bae9e67eSachartre 2627*bae9e67eSachartre /* 2628*bae9e67eSachartre * Check if a vtoc is valid for a single-slice disk. A vtoc is considered 2629*bae9e67eSachartre * valid if the main attributes of the vtoc match with the attributes of the 2630*bae9e67eSachartre * fake vtoc we have created. 2631*bae9e67eSachartre */ 2632*bae9e67eSachartre static boolean_t 2633*bae9e67eSachartre vd_slice_vtoc_isvalid(vd_t *vd, struct vtoc *vtoc) 2634*bae9e67eSachartre { 2635*bae9e67eSachartre size_t csize; 2636*bae9e67eSachartre int i; 2637*bae9e67eSachartre 2638*bae9e67eSachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 2639*bae9e67eSachartre ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC); 2640*bae9e67eSachartre 2641*bae9e67eSachartre if (vtoc->v_sanity != vd->vtoc.v_sanity || 2642*bae9e67eSachartre vtoc->v_version != vd->vtoc.v_version || 2643*bae9e67eSachartre vtoc->v_nparts != vd->vtoc.v_nparts || 2644*bae9e67eSachartre strcmp(vtoc->v_volume, vd->vtoc.v_volume) != 0 || 2645*bae9e67eSachartre strcmp(vtoc->v_asciilabel, vd->vtoc.v_asciilabel) != 0) 2646*bae9e67eSachartre return (B_FALSE); 2647*bae9e67eSachartre 2648*bae9e67eSachartre /* slice 2 should be unchanged */ 2649*bae9e67eSachartre if (vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_start != 2650*bae9e67eSachartre vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_start || 2651*bae9e67eSachartre vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_size != 2652*bae9e67eSachartre vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_size) 2653*bae9e67eSachartre return (B_FALSE); 2654*bae9e67eSachartre 2655*bae9e67eSachartre /* 2656*bae9e67eSachartre * Slice 0 should be mostly unchanged and cover most of the disk. 2657*bae9e67eSachartre * However we allow some flexibility wrt to the start and the size 2658*bae9e67eSachartre * of this slice mainly because we can't exactly know how it will 2659*bae9e67eSachartre * be defined by the OS installer. 2660*bae9e67eSachartre * 2661*bae9e67eSachartre * We allow slice 0 to be defined as starting on any of the first 2662*bae9e67eSachartre * 4 cylinders. 2663*bae9e67eSachartre */ 2664*bae9e67eSachartre csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect; 2665*bae9e67eSachartre 2666*bae9e67eSachartre if (vtoc->v_part[0].p_start > 4 * csize || 2667*bae9e67eSachartre vtoc->v_part[0].p_size > vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_size) 2668*bae9e67eSachartre return (B_FALSE); 2669*bae9e67eSachartre 2670*bae9e67eSachartre if (vd->vtoc.v_part[0].p_size >= 4 * csize && 2671*bae9e67eSachartre vtoc->v_part[0].p_size < vd->vtoc.v_part[0].p_size - 4 *csize) 2672*bae9e67eSachartre return (B_FALSE); 2673*bae9e67eSachartre 2674*bae9e67eSachartre /* any other slice should have a size of 0 */ 2675*bae9e67eSachartre for (i = 1; i < vtoc->v_nparts; i++) { 2676*bae9e67eSachartre if (i != VD_ENTIRE_DISK_SLICE && 2677*bae9e67eSachartre vtoc->v_part[i].p_size != 0) 2678*bae9e67eSachartre return (B_FALSE); 2679*bae9e67eSachartre } 2680*bae9e67eSachartre 2681*bae9e67eSachartre return (B_TRUE); 2682*bae9e67eSachartre } 2683*bae9e67eSachartre 2684*bae9e67eSachartre /* 268587a7269eSachartre * Handle ioctls to a disk slice. 2686205eeb1aSlm66018 * 2687205eeb1aSlm66018 * Return Values 2688205eeb1aSlm66018 * 0 - Indicates that there are no errors in disk operations 2689205eeb1aSlm66018 * ENOTSUP - Unknown disk label type or unsupported DKIO ioctl 2690205eeb1aSlm66018 * EINVAL - Not enough room to copy the EFI label 2691205eeb1aSlm66018 * 269287a7269eSachartre */ 26931ae08745Sheppo static int 26940a55fbb7Slm66018 vd_do_slice_ioctl(vd_t *vd, int cmd, void *ioctl_arg) 26951ae08745Sheppo { 26964bac2208Snarayan dk_efi_t *dk_ioc; 2697*bae9e67eSachartre struct vtoc *vtoc; 2698*bae9e67eSachartre struct dk_geom *geom; 2699*bae9e67eSachartre size_t len, lba; 2700edcc0754Sachartre int rval; 2701edcc0754Sachartre 2702edcc0754Sachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 2703edcc0754Sachartre 2704edcc0754Sachartre if (cmd == DKIOCFLUSHWRITECACHE) { 2705edcc0754Sachartre if (vd->file) { 2706edcc0754Sachartre return (VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL)); 2707edcc0754Sachartre } else { 2708edcc0754Sachartre return (ldi_ioctl(vd->ldi_handle[0], cmd, 2709edcc0754Sachartre (intptr_t)ioctl_arg, vd->open_flags | FKIOCTL, 2710edcc0754Sachartre kcred, &rval)); 2711edcc0754Sachartre } 2712edcc0754Sachartre } 27134bac2208Snarayan 27144bac2208Snarayan switch (vd->vdisk_label) { 27154bac2208Snarayan 2716edcc0754Sachartre /* ioctls for a single slice disk with a VTOC label */ 27174bac2208Snarayan case VD_DISK_LABEL_VTOC: 27184bac2208Snarayan 27191ae08745Sheppo switch (cmd) { 2720*bae9e67eSachartre 27211ae08745Sheppo case DKIOCGGEOM: 27220a55fbb7Slm66018 ASSERT(ioctl_arg != NULL); 27230a55fbb7Slm66018 bcopy(&vd->dk_geom, ioctl_arg, sizeof (vd->dk_geom)); 27241ae08745Sheppo return (0); 2725*bae9e67eSachartre 27261ae08745Sheppo case DKIOCGVTOC: 27270a55fbb7Slm66018 ASSERT(ioctl_arg != NULL); 27280a55fbb7Slm66018 bcopy(&vd->vtoc, ioctl_arg, sizeof (vd->vtoc)); 27291ae08745Sheppo return (0); 2730*bae9e67eSachartre 2731*bae9e67eSachartre case DKIOCSGEOM: 2732*bae9e67eSachartre ASSERT(ioctl_arg != NULL); 2733*bae9e67eSachartre if (vd_slice_single_slice) 2734*bae9e67eSachartre return (ENOTSUP); 2735*bae9e67eSachartre 2736*bae9e67eSachartre /* fake success only if new geometry is valid */ 2737*bae9e67eSachartre geom = (struct dk_geom *)ioctl_arg; 2738*bae9e67eSachartre if (!vd_slice_geom_isvalid(vd, geom)) 2739*bae9e67eSachartre return (EINVAL); 2740*bae9e67eSachartre 2741*bae9e67eSachartre return (0); 2742*bae9e67eSachartre 2743*bae9e67eSachartre case DKIOCSVTOC: 2744*bae9e67eSachartre ASSERT(ioctl_arg != NULL); 2745*bae9e67eSachartre if (vd_slice_single_slice) 2746*bae9e67eSachartre return (ENOTSUP); 2747*bae9e67eSachartre 2748*bae9e67eSachartre /* fake sucess only if the new vtoc is valid */ 2749*bae9e67eSachartre vtoc = (struct vtoc *)ioctl_arg; 2750*bae9e67eSachartre if (!vd_slice_vtoc_isvalid(vd, vtoc)) 2751*bae9e67eSachartre return (EINVAL); 2752*bae9e67eSachartre 2753*bae9e67eSachartre return (0); 2754*bae9e67eSachartre 275587a7269eSachartre default: 27563c96341aSnarayan return (ENOTSUP); 275787a7269eSachartre } 275887a7269eSachartre 2759edcc0754Sachartre /* ioctls for a single slice disk with an EFI label */ 276087a7269eSachartre case VD_DISK_LABEL_EFI: 276187a7269eSachartre 2762*bae9e67eSachartre if (cmd != DKIOCGETEFI && cmd != DKIOCSETEFI) 2763*bae9e67eSachartre return (ENOTSUP); 2764*bae9e67eSachartre 27653c96341aSnarayan ASSERT(ioctl_arg != NULL); 276687a7269eSachartre dk_ioc = (dk_efi_t *)ioctl_arg; 2767edcc0754Sachartre 2768*bae9e67eSachartre len = dk_ioc->dki_length; 2769*bae9e67eSachartre lba = dk_ioc->dki_lba; 2770edcc0754Sachartre 2771*bae9e67eSachartre if ((lba != VD_EFI_LBA_GPT && lba != VD_EFI_LBA_GPE) || 2772*bae9e67eSachartre (lba == VD_EFI_LBA_GPT && len < sizeof (efi_gpt_t)) || 2773*bae9e67eSachartre (lba == VD_EFI_LBA_GPE && len < sizeof (efi_gpe_t))) 277487a7269eSachartre return (EINVAL); 2775edcc0754Sachartre 2776*bae9e67eSachartre switch (cmd) { 2777*bae9e67eSachartre case DKIOCGETEFI: 2778*bae9e67eSachartre len = vd_slice_flabel_read(vd, 2779*bae9e67eSachartre (caddr_t)dk_ioc->dki_data, lba * DEV_BSIZE, len); 2780edcc0754Sachartre 2781*bae9e67eSachartre ASSERT(len > 0); 2782edcc0754Sachartre 278387a7269eSachartre return (0); 2784*bae9e67eSachartre 2785*bae9e67eSachartre case DKIOCSETEFI: 2786*bae9e67eSachartre if (vd_slice_single_slice) 278787a7269eSachartre return (ENOTSUP); 2788*bae9e67eSachartre 2789*bae9e67eSachartre /* we currently don't support writing EFI */ 2790*bae9e67eSachartre return (EIO); 279187a7269eSachartre } 279287a7269eSachartre 279387a7269eSachartre default: 2794205eeb1aSlm66018 /* Unknown disk label type */ 279587a7269eSachartre return (ENOTSUP); 279687a7269eSachartre } 279787a7269eSachartre } 279887a7269eSachartre 2799edcc0754Sachartre static int 2800edcc0754Sachartre vds_efi_alloc_and_read(vd_t *vd, efi_gpt_t **gpt, efi_gpe_t **gpe) 2801edcc0754Sachartre { 2802edcc0754Sachartre vd_efi_dev_t edev; 2803edcc0754Sachartre int status; 2804edcc0754Sachartre 2805edcc0754Sachartre VD_EFI_DEV_SET(edev, vd, (vd_efi_ioctl_func)vd_backend_ioctl); 2806edcc0754Sachartre 2807edcc0754Sachartre status = vd_efi_alloc_and_read(&edev, gpt, gpe); 2808edcc0754Sachartre 2809edcc0754Sachartre return (status); 2810edcc0754Sachartre } 2811edcc0754Sachartre 2812edcc0754Sachartre static void 2813edcc0754Sachartre vds_efi_free(vd_t *vd, efi_gpt_t *gpt, efi_gpe_t *gpe) 2814edcc0754Sachartre { 2815edcc0754Sachartre vd_efi_dev_t edev; 2816edcc0754Sachartre 2817edcc0754Sachartre VD_EFI_DEV_SET(edev, vd, (vd_efi_ioctl_func)vd_backend_ioctl); 2818edcc0754Sachartre 2819edcc0754Sachartre vd_efi_free(&edev, gpt, gpe); 2820edcc0754Sachartre } 2821edcc0754Sachartre 2822edcc0754Sachartre static int 2823edcc0754Sachartre vd_file_validate_efi(vd_t *vd) 2824edcc0754Sachartre { 2825edcc0754Sachartre efi_gpt_t *gpt; 2826edcc0754Sachartre efi_gpe_t *gpe; 2827edcc0754Sachartre int i, nparts, status; 2828edcc0754Sachartre struct uuid efi_reserved = EFI_RESERVED; 2829edcc0754Sachartre 2830edcc0754Sachartre if ((status = vds_efi_alloc_and_read(vd, &gpt, &gpe)) != 0) 2831edcc0754Sachartre return (status); 2832edcc0754Sachartre 2833edcc0754Sachartre bzero(&vd->vtoc, sizeof (struct vtoc)); 2834edcc0754Sachartre bzero(&vd->dk_geom, sizeof (struct dk_geom)); 2835edcc0754Sachartre bzero(vd->slices, sizeof (vd_slice_t) * VD_MAXPART); 2836edcc0754Sachartre 2837edcc0754Sachartre vd->efi_reserved = -1; 2838edcc0754Sachartre 2839edcc0754Sachartre nparts = gpt->efi_gpt_NumberOfPartitionEntries; 2840edcc0754Sachartre 2841edcc0754Sachartre for (i = 0; i < nparts && i < VD_MAXPART; i++) { 2842edcc0754Sachartre 2843edcc0754Sachartre if (gpe[i].efi_gpe_StartingLBA == 0 || 2844edcc0754Sachartre gpe[i].efi_gpe_EndingLBA == 0) { 2845edcc0754Sachartre continue; 2846edcc0754Sachartre } 2847edcc0754Sachartre 2848edcc0754Sachartre vd->slices[i].start = gpe[i].efi_gpe_StartingLBA; 2849edcc0754Sachartre vd->slices[i].nblocks = gpe[i].efi_gpe_EndingLBA - 2850edcc0754Sachartre gpe[i].efi_gpe_StartingLBA + 1; 2851edcc0754Sachartre 2852edcc0754Sachartre if (bcmp(&gpe[i].efi_gpe_PartitionTypeGUID, &efi_reserved, 2853edcc0754Sachartre sizeof (struct uuid)) == 0) 2854edcc0754Sachartre vd->efi_reserved = i; 2855edcc0754Sachartre 2856edcc0754Sachartre } 2857edcc0754Sachartre 2858edcc0754Sachartre ASSERT(vd->vdisk_size != 0); 2859edcc0754Sachartre vd->slices[VD_EFI_WD_SLICE].start = 0; 2860edcc0754Sachartre vd->slices[VD_EFI_WD_SLICE].nblocks = vd->vdisk_size; 2861edcc0754Sachartre 2862edcc0754Sachartre vds_efi_free(vd, gpt, gpe); 2863edcc0754Sachartre 2864edcc0754Sachartre return (status); 2865edcc0754Sachartre } 2866edcc0754Sachartre 286787a7269eSachartre /* 286878fcd0a1Sachartre * Function: 286978fcd0a1Sachartre * vd_file_validate_geometry 2870205eeb1aSlm66018 * 287178fcd0a1Sachartre * Description: 287278fcd0a1Sachartre * Read the label and validate the geometry of a disk image. The driver 287378fcd0a1Sachartre * label, vtoc and geometry information are updated according to the 287478fcd0a1Sachartre * label read from the disk image. 287578fcd0a1Sachartre * 287678fcd0a1Sachartre * If no valid label is found, the label is set to unknown and the 287778fcd0a1Sachartre * function returns EINVAL, but a default vtoc and geometry are provided 2878edcc0754Sachartre * to the driver. If an EFI label is found, ENOTSUP is returned. 287978fcd0a1Sachartre * 288078fcd0a1Sachartre * Parameters: 288178fcd0a1Sachartre * vd - disk on which the operation is performed. 288278fcd0a1Sachartre * 288378fcd0a1Sachartre * Return Code: 288478fcd0a1Sachartre * 0 - success. 288578fcd0a1Sachartre * EIO - error reading the label from the disk image. 288678fcd0a1Sachartre * EINVAL - unknown disk label. 2887edcc0754Sachartre * ENOTSUP - geometry not applicable (EFI label). 288887a7269eSachartre */ 288987a7269eSachartre static int 289078fcd0a1Sachartre vd_file_validate_geometry(vd_t *vd) 289187a7269eSachartre { 289287a7269eSachartre struct dk_label label; 289378fcd0a1Sachartre struct dk_geom *geom = &vd->dk_geom; 289478fcd0a1Sachartre struct vtoc *vtoc = &vd->vtoc; 289578fcd0a1Sachartre int i; 289678fcd0a1Sachartre int status = 0; 289787a7269eSachartre 289887a7269eSachartre ASSERT(vd->file); 2899edcc0754Sachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK); 290087a7269eSachartre 290187a7269eSachartre if (VD_FILE_LABEL_READ(vd, &label) < 0) 290287a7269eSachartre return (EIO); 290387a7269eSachartre 290487a7269eSachartre if (label.dkl_magic != DKL_MAGIC || 290578fcd0a1Sachartre label.dkl_cksum != vd_lbl2cksum(&label) || 290666cfcfbeSachartre (vd_file_validate_sanity && label.dkl_vtoc.v_sanity != VTOC_SANE) || 290778fcd0a1Sachartre label.dkl_vtoc.v_nparts != V_NUMPAR) { 2908edcc0754Sachartre 2909edcc0754Sachartre if (vd_file_validate_efi(vd) == 0) { 2910edcc0754Sachartre vd->vdisk_label = VD_DISK_LABEL_EFI; 2911edcc0754Sachartre return (ENOTSUP); 2912edcc0754Sachartre } 2913edcc0754Sachartre 291478fcd0a1Sachartre vd->vdisk_label = VD_DISK_LABEL_UNK; 2915*bae9e67eSachartre vd_build_default_label(vd->file_size, &label); 291678fcd0a1Sachartre status = EINVAL; 291778fcd0a1Sachartre } else { 291878fcd0a1Sachartre vd->vdisk_label = VD_DISK_LABEL_VTOC; 291978fcd0a1Sachartre } 292087a7269eSachartre 2921*bae9e67eSachartre /* Update the driver geometry and vtoc */ 2922*bae9e67eSachartre vd_label_to_vtocgeom(&label, vtoc, geom); 292387a7269eSachartre 2924edcc0754Sachartre /* Update logical partitions */ 2925edcc0754Sachartre bzero(vd->slices, sizeof (vd_slice_t) * VD_MAXPART); 2926edcc0754Sachartre if (vd->vdisk_label != VD_DISK_LABEL_UNK) { 2927edcc0754Sachartre for (i = 0; i < vtoc->v_nparts; i++) { 2928edcc0754Sachartre vd->slices[i].start = vtoc->v_part[i].p_start; 2929edcc0754Sachartre vd->slices[i].nblocks = vtoc->v_part[i].p_size; 2930edcc0754Sachartre } 2931edcc0754Sachartre } 2932edcc0754Sachartre 293378fcd0a1Sachartre return (status); 293478fcd0a1Sachartre } 293578fcd0a1Sachartre 293678fcd0a1Sachartre /* 293778fcd0a1Sachartre * Handle ioctls to a disk image (file-based). 293878fcd0a1Sachartre * 293978fcd0a1Sachartre * Return Values 294078fcd0a1Sachartre * 0 - Indicates that there are no errors 294178fcd0a1Sachartre * != 0 - Disk operation returned an error 294278fcd0a1Sachartre */ 294378fcd0a1Sachartre static int 294478fcd0a1Sachartre vd_do_file_ioctl(vd_t *vd, int cmd, void *ioctl_arg) 294578fcd0a1Sachartre { 294678fcd0a1Sachartre struct dk_label label; 294778fcd0a1Sachartre struct dk_geom *geom; 294878fcd0a1Sachartre struct vtoc *vtoc; 2949edcc0754Sachartre dk_efi_t *efi; 2950*bae9e67eSachartre int rc; 295178fcd0a1Sachartre 295278fcd0a1Sachartre ASSERT(vd->file); 2953edcc0754Sachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK); 295478fcd0a1Sachartre 295578fcd0a1Sachartre switch (cmd) { 295678fcd0a1Sachartre 295778fcd0a1Sachartre case DKIOCGGEOM: 295878fcd0a1Sachartre ASSERT(ioctl_arg != NULL); 295978fcd0a1Sachartre geom = (struct dk_geom *)ioctl_arg; 296078fcd0a1Sachartre 296178fcd0a1Sachartre rc = vd_file_validate_geometry(vd); 2962edcc0754Sachartre if (rc != 0 && rc != EINVAL) 296378fcd0a1Sachartre return (rc); 296478fcd0a1Sachartre bcopy(&vd->dk_geom, geom, sizeof (struct dk_geom)); 296578fcd0a1Sachartre return (0); 296678fcd0a1Sachartre 296778fcd0a1Sachartre case DKIOCGVTOC: 296878fcd0a1Sachartre ASSERT(ioctl_arg != NULL); 296978fcd0a1Sachartre vtoc = (struct vtoc *)ioctl_arg; 297078fcd0a1Sachartre 297178fcd0a1Sachartre rc = vd_file_validate_geometry(vd); 2972edcc0754Sachartre if (rc != 0 && rc != EINVAL) 297378fcd0a1Sachartre return (rc); 297478fcd0a1Sachartre bcopy(&vd->vtoc, vtoc, sizeof (struct vtoc)); 297587a7269eSachartre return (0); 297687a7269eSachartre 297787a7269eSachartre case DKIOCSGEOM: 297887a7269eSachartre ASSERT(ioctl_arg != NULL); 297987a7269eSachartre geom = (struct dk_geom *)ioctl_arg; 298087a7269eSachartre 298187a7269eSachartre if (geom->dkg_nhead == 0 || geom->dkg_nsect == 0) 298287a7269eSachartre return (EINVAL); 298387a7269eSachartre 298487a7269eSachartre /* 298587a7269eSachartre * The current device geometry is not updated, just the driver 298687a7269eSachartre * "notion" of it. The device geometry will be effectively 298787a7269eSachartre * updated when a label is written to the device during a next 298887a7269eSachartre * DKIOCSVTOC. 298987a7269eSachartre */ 299087a7269eSachartre bcopy(ioctl_arg, &vd->dk_geom, sizeof (vd->dk_geom)); 299187a7269eSachartre return (0); 299287a7269eSachartre 299387a7269eSachartre case DKIOCSVTOC: 299487a7269eSachartre ASSERT(ioctl_arg != NULL); 299587a7269eSachartre ASSERT(vd->dk_geom.dkg_nhead != 0 && 299687a7269eSachartre vd->dk_geom.dkg_nsect != 0); 2997690555a1Sachartre vtoc = (struct vtoc *)ioctl_arg; 2998690555a1Sachartre 2999690555a1Sachartre if (vtoc->v_sanity != VTOC_SANE || 3000690555a1Sachartre vtoc->v_sectorsz != DEV_BSIZE || 3001690555a1Sachartre vtoc->v_nparts != V_NUMPAR) 3002690555a1Sachartre return (EINVAL); 3003690555a1Sachartre 3004*bae9e67eSachartre vd_vtocgeom_to_label(vtoc, &vd->dk_geom, &label); 3005690555a1Sachartre 300687a7269eSachartre /* write label to the disk image */ 300787a7269eSachartre if ((rc = vd_file_set_vtoc(vd, &label)) != 0) 300887a7269eSachartre return (rc); 3009690555a1Sachartre 3010edcc0754Sachartre break; 3011edcc0754Sachartre 3012edcc0754Sachartre case DKIOCFLUSHWRITECACHE: 3013edcc0754Sachartre return (VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL)); 3014edcc0754Sachartre 3015edcc0754Sachartre case DKIOCGETEFI: 3016edcc0754Sachartre ASSERT(ioctl_arg != NULL); 3017edcc0754Sachartre efi = (dk_efi_t *)ioctl_arg; 3018edcc0754Sachartre 3019edcc0754Sachartre if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, 3020edcc0754Sachartre (caddr_t)efi->dki_data, efi->dki_lba, efi->dki_length) < 0) 3021edcc0754Sachartre return (EIO); 3022edcc0754Sachartre 3023edcc0754Sachartre return (0); 3024edcc0754Sachartre 3025edcc0754Sachartre case DKIOCSETEFI: 3026edcc0754Sachartre ASSERT(ioctl_arg != NULL); 3027edcc0754Sachartre efi = (dk_efi_t *)ioctl_arg; 3028edcc0754Sachartre 3029edcc0754Sachartre if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, 3030edcc0754Sachartre (caddr_t)efi->dki_data, efi->dki_lba, efi->dki_length) < 0) 3031edcc0754Sachartre return (EIO); 3032edcc0754Sachartre 3033edcc0754Sachartre break; 3034edcc0754Sachartre 3035edcc0754Sachartre 3036edcc0754Sachartre default: 3037edcc0754Sachartre return (ENOTSUP); 3038edcc0754Sachartre } 3039edcc0754Sachartre 3040edcc0754Sachartre ASSERT(cmd == DKIOCSVTOC || cmd == DKIOCSETEFI); 3041edcc0754Sachartre 3042edcc0754Sachartre /* label has changed, revalidate the geometry */ 3043edcc0754Sachartre (void) vd_file_validate_geometry(vd); 30443c96341aSnarayan 304587a7269eSachartre /* 304687a7269eSachartre * The disk geometry may have changed, so we need to write 304787a7269eSachartre * the devid (if there is one) so that it is stored at the 304887a7269eSachartre * right location. 304987a7269eSachartre */ 3050edcc0754Sachartre if (vd_file_write_devid(vd, vd->file_devid) != 0) { 305187a7269eSachartre PR0("Fail to write devid"); 30521ae08745Sheppo } 30534bac2208Snarayan 30544bac2208Snarayan return (0); 30554bac2208Snarayan } 3056edcc0754Sachartre 3057edcc0754Sachartre static int 3058edcc0754Sachartre vd_backend_ioctl(vd_t *vd, int cmd, caddr_t arg) 3059edcc0754Sachartre { 3060edcc0754Sachartre int rval = 0, status; 3061edcc0754Sachartre 3062edcc0754Sachartre /* 3063edcc0754Sachartre * Call the appropriate function to execute the ioctl depending 3064edcc0754Sachartre * on the type of vdisk. 3065edcc0754Sachartre */ 3066edcc0754Sachartre if (vd->vdisk_type == VD_DISK_TYPE_SLICE) { 3067edcc0754Sachartre 3068edcc0754Sachartre /* slice, file or volume exported as a single slice disk */ 3069edcc0754Sachartre status = vd_do_slice_ioctl(vd, cmd, arg); 3070edcc0754Sachartre 3071edcc0754Sachartre } else if (vd->file) { 3072edcc0754Sachartre 3073edcc0754Sachartre /* file or volume exported as a full disk */ 3074edcc0754Sachartre status = vd_do_file_ioctl(vd, cmd, arg); 3075edcc0754Sachartre 3076edcc0754Sachartre } else { 3077edcc0754Sachartre 3078edcc0754Sachartre /* disk device exported as a full disk */ 3079edcc0754Sachartre status = ldi_ioctl(vd->ldi_handle[0], cmd, (intptr_t)arg, 3080edcc0754Sachartre vd->open_flags | FKIOCTL, kcred, &rval); 3081edcc0754Sachartre } 3082edcc0754Sachartre 3083edcc0754Sachartre #ifdef DEBUG 3084edcc0754Sachartre if (rval != 0) { 3085edcc0754Sachartre PR0("ioctl %x set rval = %d, which is not being returned" 3086edcc0754Sachartre " to caller", cmd, rval); 3087edcc0754Sachartre } 3088edcc0754Sachartre #endif /* DEBUG */ 3089edcc0754Sachartre 3090edcc0754Sachartre return (status); 30911ae08745Sheppo } 30921ae08745Sheppo 3093205eeb1aSlm66018 /* 3094205eeb1aSlm66018 * Description: 3095205eeb1aSlm66018 * This is the function that processes the ioctl requests (farming it 3096205eeb1aSlm66018 * out to functions that handle slices, files or whole disks) 3097205eeb1aSlm66018 * 3098205eeb1aSlm66018 * Return Values 3099205eeb1aSlm66018 * 0 - ioctl operation completed successfully 3100205eeb1aSlm66018 * != 0 - The LDC error value encountered 3101205eeb1aSlm66018 * (propagated back up the call stack as a task error) 3102205eeb1aSlm66018 * 3103205eeb1aSlm66018 * Side Effect 3104205eeb1aSlm66018 * sets request->status to the return value of the ioctl function. 3105205eeb1aSlm66018 */ 31061ae08745Sheppo static int 31070a55fbb7Slm66018 vd_do_ioctl(vd_t *vd, vd_dring_payload_t *request, void* buf, vd_ioctl_t *ioctl) 31081ae08745Sheppo { 3109edcc0754Sachartre int status = 0; 31101ae08745Sheppo size_t nbytes = request->nbytes; /* modifiable copy */ 31111ae08745Sheppo 31121ae08745Sheppo 31131ae08745Sheppo ASSERT(request->slice < vd->nslices); 31141ae08745Sheppo PR0("Performing %s", ioctl->operation_name); 31151ae08745Sheppo 31160a55fbb7Slm66018 /* Get data from client and convert, if necessary */ 31170a55fbb7Slm66018 if (ioctl->copyin != NULL) { 31181ae08745Sheppo ASSERT(nbytes != 0 && buf != NULL); 31191ae08745Sheppo PR1("Getting \"arg\" data from client"); 31201ae08745Sheppo if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes, 31211ae08745Sheppo request->cookie, request->ncookies, 31221ae08745Sheppo LDC_COPY_IN)) != 0) { 31233af08d82Slm66018 PR0("ldc_mem_copy() returned errno %d " 31241ae08745Sheppo "copying from client", status); 31251ae08745Sheppo return (status); 31261ae08745Sheppo } 31270a55fbb7Slm66018 31280a55fbb7Slm66018 /* Convert client's data, if necessary */ 31292f5224aeSachartre if (ioctl->copyin == VD_IDENTITY_IN) { 31302f5224aeSachartre /* use client buffer */ 31310a55fbb7Slm66018 ioctl->arg = buf; 31322f5224aeSachartre } else { 31332f5224aeSachartre /* convert client vdisk operation data to ioctl data */ 31342f5224aeSachartre status = (ioctl->copyin)(buf, nbytes, 31352f5224aeSachartre (void *)ioctl->arg); 31362f5224aeSachartre if (status != 0) { 31372f5224aeSachartre request->status = status; 31382f5224aeSachartre return (0); 31392f5224aeSachartre } 31402f5224aeSachartre } 31412f5224aeSachartre } 31422f5224aeSachartre 31432f5224aeSachartre if (ioctl->operation == VD_OP_SCSICMD) { 31442f5224aeSachartre struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl->arg; 31452f5224aeSachartre 31462f5224aeSachartre /* check write permission */ 31472f5224aeSachartre if (!(vd->open_flags & FWRITE) && 31482f5224aeSachartre !(uscsi->uscsi_flags & USCSI_READ)) { 31492f5224aeSachartre PR0("uscsi fails because backend is opened read-only"); 31502f5224aeSachartre request->status = EROFS; 31512f5224aeSachartre return (0); 31522f5224aeSachartre } 31531ae08745Sheppo } 31541ae08745Sheppo 31551ae08745Sheppo /* 3156edcc0754Sachartre * Send the ioctl to the disk backend. 31571ae08745Sheppo */ 3158edcc0754Sachartre request->status = vd_backend_ioctl(vd, ioctl->cmd, ioctl->arg); 3159205eeb1aSlm66018 3160205eeb1aSlm66018 if (request->status != 0) { 3161205eeb1aSlm66018 PR0("ioctl(%s) = errno %d", ioctl->cmd_name, request->status); 31622f5224aeSachartre if (ioctl->operation == VD_OP_SCSICMD && 31632f5224aeSachartre ((struct uscsi_cmd *)ioctl->arg)->uscsi_status != 0) 31642f5224aeSachartre /* 31652f5224aeSachartre * USCSICMD has reported an error and the uscsi_status 31662f5224aeSachartre * field is not zero. This means that the SCSI command 31672f5224aeSachartre * has completed but it has an error. So we should 31682f5224aeSachartre * mark the VD operation has succesfully completed 31692f5224aeSachartre * and clients can check the SCSI status field for 31702f5224aeSachartre * SCSI errors. 31712f5224aeSachartre */ 31722f5224aeSachartre request->status = 0; 31732f5224aeSachartre else 3174205eeb1aSlm66018 return (0); 3175205eeb1aSlm66018 } 31761ae08745Sheppo 31770a55fbb7Slm66018 /* Convert data and send to client, if necessary */ 31780a55fbb7Slm66018 if (ioctl->copyout != NULL) { 31791ae08745Sheppo ASSERT(nbytes != 0 && buf != NULL); 31801ae08745Sheppo PR1("Sending \"arg\" data to client"); 31810a55fbb7Slm66018 31820a55fbb7Slm66018 /* Convert ioctl data to vdisk operation data, if necessary */ 31832f5224aeSachartre if (ioctl->copyout != VD_IDENTITY_OUT) 31840a55fbb7Slm66018 (ioctl->copyout)((void *)ioctl->arg, buf); 31850a55fbb7Slm66018 31861ae08745Sheppo if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes, 31871ae08745Sheppo request->cookie, request->ncookies, 31881ae08745Sheppo LDC_COPY_OUT)) != 0) { 31893af08d82Slm66018 PR0("ldc_mem_copy() returned errno %d " 31901ae08745Sheppo "copying to client", status); 31911ae08745Sheppo return (status); 31921ae08745Sheppo } 31931ae08745Sheppo } 31941ae08745Sheppo 31951ae08745Sheppo return (status); 31961ae08745Sheppo } 31971ae08745Sheppo 31981ae08745Sheppo #define RNDSIZE(expr) P2ROUNDUP(sizeof (expr), sizeof (uint64_t)) 3199205eeb1aSlm66018 3200205eeb1aSlm66018 /* 3201205eeb1aSlm66018 * Description: 3202205eeb1aSlm66018 * This generic function is called by the task queue to complete 3203205eeb1aSlm66018 * the processing of the tasks. The specific completion function 3204205eeb1aSlm66018 * is passed in as a field in the task pointer. 3205205eeb1aSlm66018 * 3206205eeb1aSlm66018 * Parameters: 3207205eeb1aSlm66018 * arg - opaque pointer to structure containing task to be completed 3208205eeb1aSlm66018 * 3209205eeb1aSlm66018 * Return Values 3210205eeb1aSlm66018 * None 3211205eeb1aSlm66018 */ 3212205eeb1aSlm66018 static void 3213205eeb1aSlm66018 vd_complete(void *arg) 3214205eeb1aSlm66018 { 3215205eeb1aSlm66018 vd_task_t *task = (vd_task_t *)arg; 3216205eeb1aSlm66018 3217205eeb1aSlm66018 ASSERT(task != NULL); 3218205eeb1aSlm66018 ASSERT(task->status == EINPROGRESS); 3219205eeb1aSlm66018 ASSERT(task->completef != NULL); 3220205eeb1aSlm66018 3221205eeb1aSlm66018 task->status = task->completef(task); 3222205eeb1aSlm66018 if (task->status) 3223205eeb1aSlm66018 PR0("%s: Error %d completing task", __func__, task->status); 3224205eeb1aSlm66018 3225205eeb1aSlm66018 /* Now notify the vDisk client */ 3226205eeb1aSlm66018 vd_complete_notify(task); 3227205eeb1aSlm66018 } 3228205eeb1aSlm66018 32291ae08745Sheppo static int 3230d10e4ef2Snarayan vd_ioctl(vd_task_t *task) 32311ae08745Sheppo { 323287a7269eSachartre int i, status; 32331ae08745Sheppo void *buf = NULL; 32340a55fbb7Slm66018 struct dk_geom dk_geom = {0}; 32350a55fbb7Slm66018 struct vtoc vtoc = {0}; 32364bac2208Snarayan struct dk_efi dk_efi = {0}; 32372f5224aeSachartre struct uscsi_cmd uscsi = {0}; 3238d10e4ef2Snarayan vd_t *vd = task->vd; 3239d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 32400a55fbb7Slm66018 vd_ioctl_t ioctl[] = { 32410a55fbb7Slm66018 /* Command (no-copy) operations */ 32420a55fbb7Slm66018 {VD_OP_FLUSH, STRINGIZE(VD_OP_FLUSH), 0, 32430a55fbb7Slm66018 DKIOCFLUSHWRITECACHE, STRINGIZE(DKIOCFLUSHWRITECACHE), 3244047ba61eSachartre NULL, NULL, NULL, B_TRUE}, 32450a55fbb7Slm66018 32460a55fbb7Slm66018 /* "Get" (copy-out) operations */ 32470a55fbb7Slm66018 {VD_OP_GET_WCE, STRINGIZE(VD_OP_GET_WCE), RNDSIZE(int), 32480a55fbb7Slm66018 DKIOCGETWCE, STRINGIZE(DKIOCGETWCE), 32492f5224aeSachartre NULL, VD_IDENTITY_IN, VD_IDENTITY_OUT, B_FALSE}, 32500a55fbb7Slm66018 {VD_OP_GET_DISKGEOM, STRINGIZE(VD_OP_GET_DISKGEOM), 32510a55fbb7Slm66018 RNDSIZE(vd_geom_t), 32520a55fbb7Slm66018 DKIOCGGEOM, STRINGIZE(DKIOCGGEOM), 3253047ba61eSachartre &dk_geom, NULL, dk_geom2vd_geom, B_FALSE}, 32540a55fbb7Slm66018 {VD_OP_GET_VTOC, STRINGIZE(VD_OP_GET_VTOC), RNDSIZE(vd_vtoc_t), 32550a55fbb7Slm66018 DKIOCGVTOC, STRINGIZE(DKIOCGVTOC), 3256047ba61eSachartre &vtoc, NULL, vtoc2vd_vtoc, B_FALSE}, 32574bac2208Snarayan {VD_OP_GET_EFI, STRINGIZE(VD_OP_GET_EFI), RNDSIZE(vd_efi_t), 32584bac2208Snarayan DKIOCGETEFI, STRINGIZE(DKIOCGETEFI), 3259047ba61eSachartre &dk_efi, vd_get_efi_in, vd_get_efi_out, B_FALSE}, 32600a55fbb7Slm66018 32610a55fbb7Slm66018 /* "Set" (copy-in) operations */ 32620a55fbb7Slm66018 {VD_OP_SET_WCE, STRINGIZE(VD_OP_SET_WCE), RNDSIZE(int), 32630a55fbb7Slm66018 DKIOCSETWCE, STRINGIZE(DKIOCSETWCE), 32642f5224aeSachartre NULL, VD_IDENTITY_IN, VD_IDENTITY_OUT, B_TRUE}, 32650a55fbb7Slm66018 {VD_OP_SET_DISKGEOM, STRINGIZE(VD_OP_SET_DISKGEOM), 32660a55fbb7Slm66018 RNDSIZE(vd_geom_t), 32670a55fbb7Slm66018 DKIOCSGEOM, STRINGIZE(DKIOCSGEOM), 3268047ba61eSachartre &dk_geom, vd_geom2dk_geom, NULL, B_TRUE}, 32690a55fbb7Slm66018 {VD_OP_SET_VTOC, STRINGIZE(VD_OP_SET_VTOC), RNDSIZE(vd_vtoc_t), 32700a55fbb7Slm66018 DKIOCSVTOC, STRINGIZE(DKIOCSVTOC), 3271047ba61eSachartre &vtoc, vd_vtoc2vtoc, NULL, B_TRUE}, 32724bac2208Snarayan {VD_OP_SET_EFI, STRINGIZE(VD_OP_SET_EFI), RNDSIZE(vd_efi_t), 32734bac2208Snarayan DKIOCSETEFI, STRINGIZE(DKIOCSETEFI), 3274047ba61eSachartre &dk_efi, vd_set_efi_in, vd_set_efi_out, B_TRUE}, 32752f5224aeSachartre 32762f5224aeSachartre {VD_OP_SCSICMD, STRINGIZE(VD_OP_SCSICMD), RNDSIZE(vd_scsi_t), 32772f5224aeSachartre USCSICMD, STRINGIZE(USCSICMD), 32782f5224aeSachartre &uscsi, vd_scsicmd_in, vd_scsicmd_out, B_FALSE}, 32790a55fbb7Slm66018 }; 32801ae08745Sheppo size_t nioctls = (sizeof (ioctl))/(sizeof (ioctl[0])); 32811ae08745Sheppo 32821ae08745Sheppo 3283d10e4ef2Snarayan ASSERT(vd != NULL); 3284d10e4ef2Snarayan ASSERT(request != NULL); 32851ae08745Sheppo ASSERT(request->slice < vd->nslices); 32861ae08745Sheppo 32871ae08745Sheppo /* 32881ae08745Sheppo * Determine ioctl corresponding to caller's "operation" and 32891ae08745Sheppo * validate caller's "nbytes" 32901ae08745Sheppo */ 32911ae08745Sheppo for (i = 0; i < nioctls; i++) { 32921ae08745Sheppo if (request->operation == ioctl[i].operation) { 32930a55fbb7Slm66018 /* LDC memory operations require 8-byte multiples */ 32940a55fbb7Slm66018 ASSERT(ioctl[i].nbytes % sizeof (uint64_t) == 0); 32950a55fbb7Slm66018 32964bac2208Snarayan if (request->operation == VD_OP_GET_EFI || 32972f5224aeSachartre request->operation == VD_OP_SET_EFI || 32982f5224aeSachartre request->operation == VD_OP_SCSICMD) { 32994bac2208Snarayan if (request->nbytes >= ioctl[i].nbytes) 33004bac2208Snarayan break; 33013af08d82Slm66018 PR0("%s: Expected at least nbytes = %lu, " 33024bac2208Snarayan "got %lu", ioctl[i].operation_name, 33034bac2208Snarayan ioctl[i].nbytes, request->nbytes); 33044bac2208Snarayan return (EINVAL); 33054bac2208Snarayan } 33064bac2208Snarayan 33070a55fbb7Slm66018 if (request->nbytes != ioctl[i].nbytes) { 33083af08d82Slm66018 PR0("%s: Expected nbytes = %lu, got %lu", 33090a55fbb7Slm66018 ioctl[i].operation_name, ioctl[i].nbytes, 33100a55fbb7Slm66018 request->nbytes); 33111ae08745Sheppo return (EINVAL); 33121ae08745Sheppo } 33131ae08745Sheppo 33141ae08745Sheppo break; 33151ae08745Sheppo } 33161ae08745Sheppo } 33171ae08745Sheppo ASSERT(i < nioctls); /* because "operation" already validated */ 33181ae08745Sheppo 3319047ba61eSachartre if (!(vd->open_flags & FWRITE) && ioctl[i].write) { 3320047ba61eSachartre PR0("%s fails because backend is opened read-only", 3321047ba61eSachartre ioctl[i].operation_name); 3322047ba61eSachartre request->status = EROFS; 3323047ba61eSachartre return (0); 3324047ba61eSachartre } 3325047ba61eSachartre 33261ae08745Sheppo if (request->nbytes) 33271ae08745Sheppo buf = kmem_zalloc(request->nbytes, KM_SLEEP); 33281ae08745Sheppo status = vd_do_ioctl(vd, request, buf, &ioctl[i]); 33291ae08745Sheppo if (request->nbytes) 33301ae08745Sheppo kmem_free(buf, request->nbytes); 333187a7269eSachartre 33321ae08745Sheppo return (status); 33331ae08745Sheppo } 33341ae08745Sheppo 33354bac2208Snarayan static int 33364bac2208Snarayan vd_get_devid(vd_task_t *task) 33374bac2208Snarayan { 33384bac2208Snarayan vd_t *vd = task->vd; 33394bac2208Snarayan vd_dring_payload_t *request = task->request; 33404bac2208Snarayan vd_devid_t *vd_devid; 33414bac2208Snarayan impl_devid_t *devid; 334287a7269eSachartre int status, bufid_len, devid_len, len, sz; 33433af08d82Slm66018 int bufbytes; 33444bac2208Snarayan 33453af08d82Slm66018 PR1("Get Device ID, nbytes=%ld", request->nbytes); 33464bac2208Snarayan 3347*bae9e67eSachartre if (vd->vdisk_type == VD_DISK_TYPE_SLICE) { 3348*bae9e67eSachartre /* 3349*bae9e67eSachartre * We don't support devid for single-slice disks because we 3350*bae9e67eSachartre * have no space to store a fabricated devid and for physical 3351*bae9e67eSachartre * disk slices, we can't use the devid of the disk otherwise 3352*bae9e67eSachartre * exporting multiple slices from the same disk will produce 3353*bae9e67eSachartre * the same devids. 3354*bae9e67eSachartre */ 3355*bae9e67eSachartre PR2("No Device ID for slices"); 3356*bae9e67eSachartre request->status = ENOTSUP; 3357*bae9e67eSachartre return (0); 3358*bae9e67eSachartre } 3359*bae9e67eSachartre 33603c96341aSnarayan if (vd->file) { 336187a7269eSachartre if (vd->file_devid == NULL) { 33623af08d82Slm66018 PR2("No Device ID"); 3363205eeb1aSlm66018 request->status = ENOENT; 3364205eeb1aSlm66018 return (0); 336587a7269eSachartre } else { 336687a7269eSachartre sz = ddi_devid_sizeof(vd->file_devid); 336787a7269eSachartre devid = kmem_alloc(sz, KM_SLEEP); 336887a7269eSachartre bcopy(vd->file_devid, devid, sz); 336987a7269eSachartre } 337087a7269eSachartre } else { 337187a7269eSachartre if (ddi_lyr_get_devid(vd->dev[request->slice], 337287a7269eSachartre (ddi_devid_t *)&devid) != DDI_SUCCESS) { 337387a7269eSachartre PR2("No Device ID"); 3374205eeb1aSlm66018 request->status = ENOENT; 3375205eeb1aSlm66018 return (0); 337687a7269eSachartre } 33774bac2208Snarayan } 33784bac2208Snarayan 33794bac2208Snarayan bufid_len = request->nbytes - sizeof (vd_devid_t) + 1; 33804bac2208Snarayan devid_len = DEVID_GETLEN(devid); 33814bac2208Snarayan 33823af08d82Slm66018 /* 33833af08d82Slm66018 * Save the buffer size here for use in deallocation. 33843af08d82Slm66018 * The actual number of bytes copied is returned in 33853af08d82Slm66018 * the 'nbytes' field of the request structure. 33863af08d82Slm66018 */ 33873af08d82Slm66018 bufbytes = request->nbytes; 33883af08d82Slm66018 33893af08d82Slm66018 vd_devid = kmem_zalloc(bufbytes, KM_SLEEP); 33904bac2208Snarayan vd_devid->length = devid_len; 33914bac2208Snarayan vd_devid->type = DEVID_GETTYPE(devid); 33924bac2208Snarayan 33934bac2208Snarayan len = (devid_len > bufid_len)? bufid_len : devid_len; 33944bac2208Snarayan 33954bac2208Snarayan bcopy(devid->did_id, vd_devid->id, len); 33964bac2208Snarayan 339778fcd0a1Sachartre request->status = 0; 339878fcd0a1Sachartre 33994bac2208Snarayan /* LDC memory operations require 8-byte multiples */ 34004bac2208Snarayan ASSERT(request->nbytes % sizeof (uint64_t) == 0); 34014bac2208Snarayan 34024bac2208Snarayan if ((status = ldc_mem_copy(vd->ldc_handle, (caddr_t)vd_devid, 0, 34034bac2208Snarayan &request->nbytes, request->cookie, request->ncookies, 34044bac2208Snarayan LDC_COPY_OUT)) != 0) { 34053af08d82Slm66018 PR0("ldc_mem_copy() returned errno %d copying to client", 34064bac2208Snarayan status); 34074bac2208Snarayan } 34083af08d82Slm66018 PR1("post mem_copy: nbytes=%ld", request->nbytes); 34094bac2208Snarayan 34103af08d82Slm66018 kmem_free(vd_devid, bufbytes); 34114bac2208Snarayan ddi_devid_free((ddi_devid_t)devid); 34124bac2208Snarayan 34134bac2208Snarayan return (status); 34144bac2208Snarayan } 34154bac2208Snarayan 34162f5224aeSachartre static int 34172f5224aeSachartre vd_scsi_reset(vd_t *vd) 34182f5224aeSachartre { 34192f5224aeSachartre int rval, status; 34202f5224aeSachartre struct uscsi_cmd uscsi = { 0 }; 34212f5224aeSachartre 34222f5224aeSachartre uscsi.uscsi_flags = vd_scsi_debug | USCSI_RESET; 34232f5224aeSachartre uscsi.uscsi_timeout = vd_scsi_rdwr_timeout; 34242f5224aeSachartre 34252f5224aeSachartre status = ldi_ioctl(vd->ldi_handle[0], USCSICMD, (intptr_t)&uscsi, 34262f5224aeSachartre (vd->open_flags | FKIOCTL), kcred, &rval); 34272f5224aeSachartre 34282f5224aeSachartre return (status); 34292f5224aeSachartre } 34302f5224aeSachartre 34312f5224aeSachartre static int 34322f5224aeSachartre vd_reset(vd_task_t *task) 34332f5224aeSachartre { 34342f5224aeSachartre vd_t *vd = task->vd; 34352f5224aeSachartre vd_dring_payload_t *request = task->request; 34362f5224aeSachartre 34372f5224aeSachartre ASSERT(request->operation == VD_OP_RESET); 34382f5224aeSachartre ASSERT(vd->scsi); 34392f5224aeSachartre 34402f5224aeSachartre PR0("Performing VD_OP_RESET"); 34412f5224aeSachartre 34422f5224aeSachartre if (request->nbytes != 0) { 34432f5224aeSachartre PR0("VD_OP_RESET: Expected nbytes = 0, got %lu", 34442f5224aeSachartre request->nbytes); 34452f5224aeSachartre return (EINVAL); 34462f5224aeSachartre } 34472f5224aeSachartre 34482f5224aeSachartre request->status = vd_scsi_reset(vd); 34492f5224aeSachartre 34502f5224aeSachartre return (0); 34512f5224aeSachartre } 34522f5224aeSachartre 34532f5224aeSachartre static int 34542f5224aeSachartre vd_get_capacity(vd_task_t *task) 34552f5224aeSachartre { 34562f5224aeSachartre int rv; 34572f5224aeSachartre size_t nbytes; 34582f5224aeSachartre vd_t *vd = task->vd; 34592f5224aeSachartre vd_dring_payload_t *request = task->request; 34602f5224aeSachartre vd_capacity_t vd_cap = { 0 }; 34612f5224aeSachartre 34622f5224aeSachartre ASSERT(request->operation == VD_OP_GET_CAPACITY); 34632f5224aeSachartre ASSERT(vd->scsi); 34642f5224aeSachartre 34652f5224aeSachartre PR0("Performing VD_OP_GET_CAPACITY"); 34662f5224aeSachartre 34672f5224aeSachartre nbytes = request->nbytes; 34682f5224aeSachartre 34692f5224aeSachartre if (nbytes != RNDSIZE(vd_capacity_t)) { 34702f5224aeSachartre PR0("VD_OP_GET_CAPACITY: Expected nbytes = %lu, got %lu", 34712f5224aeSachartre RNDSIZE(vd_capacity_t), nbytes); 34722f5224aeSachartre return (EINVAL); 34732f5224aeSachartre } 34742f5224aeSachartre 34752f5224aeSachartre if (vd->vdisk_size == VD_SIZE_UNKNOWN) { 34762f5224aeSachartre if (vd_setup_mediainfo(vd) != 0) 34772f5224aeSachartre ASSERT(vd->vdisk_size == VD_SIZE_UNKNOWN); 34782f5224aeSachartre } 34792f5224aeSachartre 34802f5224aeSachartre ASSERT(vd->vdisk_size != 0); 34812f5224aeSachartre 34822f5224aeSachartre request->status = 0; 34832f5224aeSachartre 34842f5224aeSachartre vd_cap.vdisk_block_size = vd->vdisk_block_size; 34852f5224aeSachartre vd_cap.vdisk_size = vd->vdisk_size; 34862f5224aeSachartre 34872f5224aeSachartre if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&vd_cap, 0, &nbytes, 34882f5224aeSachartre request->cookie, request->ncookies, LDC_COPY_OUT)) != 0) { 34892f5224aeSachartre PR0("ldc_mem_copy() returned errno %d copying to client", rv); 34902f5224aeSachartre return (rv); 34912f5224aeSachartre } 34922f5224aeSachartre 34932f5224aeSachartre return (0); 34942f5224aeSachartre } 34952f5224aeSachartre 34962f5224aeSachartre static int 34972f5224aeSachartre vd_get_access(vd_task_t *task) 34982f5224aeSachartre { 34992f5224aeSachartre uint64_t access; 35002f5224aeSachartre int rv, rval = 0; 35012f5224aeSachartre size_t nbytes; 35022f5224aeSachartre vd_t *vd = task->vd; 35032f5224aeSachartre vd_dring_payload_t *request = task->request; 35042f5224aeSachartre 35052f5224aeSachartre ASSERT(request->operation == VD_OP_GET_ACCESS); 35062f5224aeSachartre ASSERT(vd->scsi); 35072f5224aeSachartre 35082f5224aeSachartre PR0("Performing VD_OP_GET_ACCESS"); 35092f5224aeSachartre 35102f5224aeSachartre nbytes = request->nbytes; 35112f5224aeSachartre 35122f5224aeSachartre if (nbytes != sizeof (uint64_t)) { 35132f5224aeSachartre PR0("VD_OP_GET_ACCESS: Expected nbytes = %lu, got %lu", 35142f5224aeSachartre sizeof (uint64_t), nbytes); 35152f5224aeSachartre return (EINVAL); 35162f5224aeSachartre } 35172f5224aeSachartre 35182f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], MHIOCSTATUS, 35192f5224aeSachartre NULL, (vd->open_flags | FKIOCTL), kcred, &rval); 35202f5224aeSachartre 35212f5224aeSachartre if (request->status != 0) 35222f5224aeSachartre return (0); 35232f5224aeSachartre 35242f5224aeSachartre access = (rval == 0)? VD_ACCESS_ALLOWED : VD_ACCESS_DENIED; 35252f5224aeSachartre 35262f5224aeSachartre if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&access, 0, &nbytes, 35272f5224aeSachartre request->cookie, request->ncookies, LDC_COPY_OUT)) != 0) { 35282f5224aeSachartre PR0("ldc_mem_copy() returned errno %d copying to client", rv); 35292f5224aeSachartre return (rv); 35302f5224aeSachartre } 35312f5224aeSachartre 35322f5224aeSachartre return (0); 35332f5224aeSachartre } 35342f5224aeSachartre 35352f5224aeSachartre static int 35362f5224aeSachartre vd_set_access(vd_task_t *task) 35372f5224aeSachartre { 35382f5224aeSachartre uint64_t flags; 35392f5224aeSachartre int rv, rval; 35402f5224aeSachartre size_t nbytes; 35412f5224aeSachartre vd_t *vd = task->vd; 35422f5224aeSachartre vd_dring_payload_t *request = task->request; 35432f5224aeSachartre 35442f5224aeSachartre ASSERT(request->operation == VD_OP_SET_ACCESS); 35452f5224aeSachartre ASSERT(vd->scsi); 35462f5224aeSachartre 35472f5224aeSachartre nbytes = request->nbytes; 35482f5224aeSachartre 35492f5224aeSachartre if (nbytes != sizeof (uint64_t)) { 35502f5224aeSachartre PR0("VD_OP_SET_ACCESS: Expected nbytes = %lu, got %lu", 35512f5224aeSachartre sizeof (uint64_t), nbytes); 35522f5224aeSachartre return (EINVAL); 35532f5224aeSachartre } 35542f5224aeSachartre 35552f5224aeSachartre if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&flags, 0, &nbytes, 35562f5224aeSachartre request->cookie, request->ncookies, LDC_COPY_IN)) != 0) { 35572f5224aeSachartre PR0("ldc_mem_copy() returned errno %d copying from client", rv); 35582f5224aeSachartre return (rv); 35592f5224aeSachartre } 35602f5224aeSachartre 35612f5224aeSachartre if (flags == VD_ACCESS_SET_CLEAR) { 35622f5224aeSachartre PR0("Performing VD_OP_SET_ACCESS (CLEAR)"); 35632f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 35642f5224aeSachartre MHIOCRELEASE, NULL, (vd->open_flags | FKIOCTL), kcred, 35652f5224aeSachartre &rval); 35662f5224aeSachartre if (request->status == 0) 35672f5224aeSachartre vd->ownership = B_FALSE; 35682f5224aeSachartre return (0); 35692f5224aeSachartre } 35702f5224aeSachartre 35712f5224aeSachartre /* 35722f5224aeSachartre * As per the VIO spec, the PREEMPT and PRESERVE flags are only valid 35732f5224aeSachartre * when the EXCLUSIVE flag is set. 35742f5224aeSachartre */ 35752f5224aeSachartre if (!(flags & VD_ACCESS_SET_EXCLUSIVE)) { 35762f5224aeSachartre PR0("Invalid VD_OP_SET_ACCESS flags: 0x%lx", flags); 35772f5224aeSachartre request->status = EINVAL; 35782f5224aeSachartre return (0); 35792f5224aeSachartre } 35802f5224aeSachartre 35812f5224aeSachartre switch (flags & (VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE)) { 35822f5224aeSachartre 35832f5224aeSachartre case VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE: 35842f5224aeSachartre /* 35852f5224aeSachartre * Flags EXCLUSIVE and PREEMPT and PRESERVE. We have to 35862f5224aeSachartre * acquire exclusive access rights, preserve them and we 35872f5224aeSachartre * can use preemption. So we can use the MHIOCTKNOWN ioctl. 35882f5224aeSachartre */ 35892f5224aeSachartre PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PREEMPT|PRESERVE)"); 35902f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 35912f5224aeSachartre MHIOCTKOWN, NULL, (vd->open_flags | FKIOCTL), kcred, &rval); 35922f5224aeSachartre break; 35932f5224aeSachartre 35942f5224aeSachartre case VD_ACCESS_SET_PRESERVE: 35952f5224aeSachartre /* 35962f5224aeSachartre * Flags EXCLUSIVE and PRESERVE. We have to acquire exclusive 35972f5224aeSachartre * access rights and preserve them, but not preempt any other 35982f5224aeSachartre * host. So we need to use the MHIOCTKOWN ioctl to enable the 35992f5224aeSachartre * "preserve" feature but we can not called it directly 36002f5224aeSachartre * because it uses preemption. So before that, we use the 36012f5224aeSachartre * MHIOCQRESERVE ioctl to ensure we can get exclusive rights 36022f5224aeSachartre * without preempting anyone. 36032f5224aeSachartre */ 36042f5224aeSachartre PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PRESERVE)"); 36052f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 36062f5224aeSachartre MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred, 36072f5224aeSachartre &rval); 36082f5224aeSachartre if (request->status != 0) 36092f5224aeSachartre break; 36102f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 36112f5224aeSachartre MHIOCTKOWN, NULL, (vd->open_flags | FKIOCTL), kcred, &rval); 36122f5224aeSachartre break; 36132f5224aeSachartre 36142f5224aeSachartre case VD_ACCESS_SET_PREEMPT: 36152f5224aeSachartre /* 36162f5224aeSachartre * Flags EXCLUSIVE and PREEMPT. We have to acquire exclusive 36172f5224aeSachartre * access rights and we can use preemption. So we try to do 36182f5224aeSachartre * a SCSI reservation, if it fails we reset the disk to clear 36192f5224aeSachartre * any reservation and we try to reserve again. 36202f5224aeSachartre */ 36212f5224aeSachartre PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PREEMPT)"); 36222f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 36232f5224aeSachartre MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred, 36242f5224aeSachartre &rval); 36252f5224aeSachartre if (request->status == 0) 36262f5224aeSachartre break; 36272f5224aeSachartre 36282f5224aeSachartre /* reset the disk */ 36292f5224aeSachartre (void) vd_scsi_reset(vd); 36302f5224aeSachartre 36312f5224aeSachartre /* try again even if the reset has failed */ 36322f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 36332f5224aeSachartre MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred, 36342f5224aeSachartre &rval); 36352f5224aeSachartre break; 36362f5224aeSachartre 36372f5224aeSachartre case 0: 36382f5224aeSachartre /* Flag EXCLUSIVE only. Just issue a SCSI reservation */ 36392f5224aeSachartre PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE)"); 36402f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 36412f5224aeSachartre MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred, 36422f5224aeSachartre &rval); 36432f5224aeSachartre break; 36442f5224aeSachartre } 36452f5224aeSachartre 36462f5224aeSachartre if (request->status == 0) 36472f5224aeSachartre vd->ownership = B_TRUE; 36482f5224aeSachartre else 36492f5224aeSachartre PR0("VD_OP_SET_ACCESS: error %d", request->status); 36502f5224aeSachartre 36512f5224aeSachartre return (0); 36522f5224aeSachartre } 36532f5224aeSachartre 36542f5224aeSachartre static void 36552f5224aeSachartre vd_reset_access(vd_t *vd) 36562f5224aeSachartre { 36572f5224aeSachartre int status, rval; 36582f5224aeSachartre 36592f5224aeSachartre if (vd->file || !vd->ownership) 36602f5224aeSachartre return; 36612f5224aeSachartre 36622f5224aeSachartre PR0("Releasing disk ownership"); 36632f5224aeSachartre status = ldi_ioctl(vd->ldi_handle[0], MHIOCRELEASE, NULL, 36642f5224aeSachartre (vd->open_flags | FKIOCTL), kcred, &rval); 36652f5224aeSachartre 36662f5224aeSachartre /* 36672f5224aeSachartre * An EACCES failure means that there is a reservation conflict, 36682f5224aeSachartre * so we are not the owner of the disk anymore. 36692f5224aeSachartre */ 36702f5224aeSachartre if (status == 0 || status == EACCES) { 36712f5224aeSachartre vd->ownership = B_FALSE; 36722f5224aeSachartre return; 36732f5224aeSachartre } 36742f5224aeSachartre 36752f5224aeSachartre PR0("Fail to release ownership, error %d", status); 36762f5224aeSachartre 36772f5224aeSachartre /* 36782f5224aeSachartre * We have failed to release the ownership, try to reset the disk 36792f5224aeSachartre * to release reservations. 36802f5224aeSachartre */ 36812f5224aeSachartre PR0("Resetting disk"); 36822f5224aeSachartre status = vd_scsi_reset(vd); 36832f5224aeSachartre 36842f5224aeSachartre if (status != 0) 36852f5224aeSachartre PR0("Fail to reset disk, error %d", status); 36862f5224aeSachartre 36872f5224aeSachartre /* whatever the result of the reset is, we try the release again */ 36882f5224aeSachartre status = ldi_ioctl(vd->ldi_handle[0], MHIOCRELEASE, NULL, 36892f5224aeSachartre (vd->open_flags | FKIOCTL), kcred, &rval); 36902f5224aeSachartre 36912f5224aeSachartre if (status == 0 || status == EACCES) { 36922f5224aeSachartre vd->ownership = B_FALSE; 36932f5224aeSachartre return; 36942f5224aeSachartre } 36952f5224aeSachartre 36962f5224aeSachartre PR0("Fail to release ownership, error %d", status); 36972f5224aeSachartre 36982f5224aeSachartre /* 36992f5224aeSachartre * At this point we have done our best to try to reset the 37002f5224aeSachartre * access rights to the disk and we don't know if we still 37012f5224aeSachartre * own a reservation and if any mechanism to preserve the 37022f5224aeSachartre * ownership is still in place. The ultimate solution would 37032f5224aeSachartre * be to reset the system but this is usually not what we 37042f5224aeSachartre * want to happen. 37052f5224aeSachartre */ 37062f5224aeSachartre 37072f5224aeSachartre if (vd_reset_access_failure == A_REBOOT) { 37082f5224aeSachartre cmn_err(CE_WARN, VD_RESET_ACCESS_FAILURE_MSG 37092f5224aeSachartre ", rebooting the system", vd->device_path); 37102f5224aeSachartre (void) uadmin(A_SHUTDOWN, AD_BOOT, NULL); 37112f5224aeSachartre } else if (vd_reset_access_failure == A_DUMP) { 37122f5224aeSachartre panic(VD_RESET_ACCESS_FAILURE_MSG, vd->device_path); 37132f5224aeSachartre } 37142f5224aeSachartre 37152f5224aeSachartre cmn_err(CE_WARN, VD_RESET_ACCESS_FAILURE_MSG, vd->device_path); 37162f5224aeSachartre } 37172f5224aeSachartre 37181ae08745Sheppo /* 37191ae08745Sheppo * Define the supported operations once the functions for performing them have 37201ae08745Sheppo * been defined 37211ae08745Sheppo */ 37221ae08745Sheppo static const vds_operation_t vds_operation[] = { 37233af08d82Slm66018 #define X(_s) #_s, _s 37243af08d82Slm66018 {X(VD_OP_BREAD), vd_start_bio, vd_complete_bio}, 37253af08d82Slm66018 {X(VD_OP_BWRITE), vd_start_bio, vd_complete_bio}, 37263af08d82Slm66018 {X(VD_OP_FLUSH), vd_ioctl, NULL}, 37273af08d82Slm66018 {X(VD_OP_GET_WCE), vd_ioctl, NULL}, 37283af08d82Slm66018 {X(VD_OP_SET_WCE), vd_ioctl, NULL}, 37293af08d82Slm66018 {X(VD_OP_GET_VTOC), vd_ioctl, NULL}, 37303af08d82Slm66018 {X(VD_OP_SET_VTOC), vd_ioctl, NULL}, 37313af08d82Slm66018 {X(VD_OP_GET_DISKGEOM), vd_ioctl, NULL}, 37323af08d82Slm66018 {X(VD_OP_SET_DISKGEOM), vd_ioctl, NULL}, 37333af08d82Slm66018 {X(VD_OP_GET_EFI), vd_ioctl, NULL}, 37343af08d82Slm66018 {X(VD_OP_SET_EFI), vd_ioctl, NULL}, 37353af08d82Slm66018 {X(VD_OP_GET_DEVID), vd_get_devid, NULL}, 37362f5224aeSachartre {X(VD_OP_SCSICMD), vd_ioctl, NULL}, 37372f5224aeSachartre {X(VD_OP_RESET), vd_reset, NULL}, 37382f5224aeSachartre {X(VD_OP_GET_CAPACITY), vd_get_capacity, NULL}, 37392f5224aeSachartre {X(VD_OP_SET_ACCESS), vd_set_access, NULL}, 37402f5224aeSachartre {X(VD_OP_GET_ACCESS), vd_get_access, NULL}, 37413af08d82Slm66018 #undef X 37421ae08745Sheppo }; 37431ae08745Sheppo 37441ae08745Sheppo static const size_t vds_noperations = 37451ae08745Sheppo (sizeof (vds_operation))/(sizeof (vds_operation[0])); 37461ae08745Sheppo 37471ae08745Sheppo /* 3748d10e4ef2Snarayan * Process a task specifying a client I/O request 3749205eeb1aSlm66018 * 3750205eeb1aSlm66018 * Parameters: 3751205eeb1aSlm66018 * task - structure containing the request sent from client 3752205eeb1aSlm66018 * 3753205eeb1aSlm66018 * Return Value 3754205eeb1aSlm66018 * 0 - success 3755205eeb1aSlm66018 * ENOTSUP - Unknown/Unsupported VD_OP_XXX operation 3756205eeb1aSlm66018 * EINVAL - Invalid disk slice 3757205eeb1aSlm66018 * != 0 - some other non-zero return value from start function 37581ae08745Sheppo */ 37591ae08745Sheppo static int 3760205eeb1aSlm66018 vd_do_process_task(vd_task_t *task) 37611ae08745Sheppo { 3762205eeb1aSlm66018 int i; 3763d10e4ef2Snarayan vd_t *vd = task->vd; 3764d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 37651ae08745Sheppo 3766d10e4ef2Snarayan ASSERT(vd != NULL); 3767d10e4ef2Snarayan ASSERT(request != NULL); 37681ae08745Sheppo 3769d10e4ef2Snarayan /* Find the requested operation */ 3770205eeb1aSlm66018 for (i = 0; i < vds_noperations; i++) { 3771205eeb1aSlm66018 if (request->operation == vds_operation[i].operation) { 3772205eeb1aSlm66018 /* all operations should have a start func */ 3773205eeb1aSlm66018 ASSERT(vds_operation[i].start != NULL); 3774205eeb1aSlm66018 3775205eeb1aSlm66018 task->completef = vds_operation[i].complete; 3776d10e4ef2Snarayan break; 3777205eeb1aSlm66018 } 3778205eeb1aSlm66018 } 377917cadca8Slm66018 378017cadca8Slm66018 /* 378117cadca8Slm66018 * We need to check that the requested operation is permitted 378217cadca8Slm66018 * for the particular client that sent it or that the loop above 378317cadca8Slm66018 * did not complete without finding the operation type (indicating 378417cadca8Slm66018 * that the requested operation is unknown/unimplemented) 378517cadca8Slm66018 */ 378617cadca8Slm66018 if ((VD_OP_SUPPORTED(vd->operations, request->operation) == B_FALSE) || 378717cadca8Slm66018 (i == vds_noperations)) { 37883af08d82Slm66018 PR0("Unsupported operation %u", request->operation); 378917cadca8Slm66018 request->status = ENOTSUP; 379017cadca8Slm66018 return (0); 37911ae08745Sheppo } 37921ae08745Sheppo 37937636cb21Slm66018 /* Range-check slice */ 379487a7269eSachartre if (request->slice >= vd->nslices && 3795*bae9e67eSachartre ((vd->vdisk_type != VD_DISK_TYPE_DISK && vd_slice_single_slice) || 379687a7269eSachartre request->slice != VD_SLICE_NONE)) { 37973af08d82Slm66018 PR0("Invalid \"slice\" %u (max %u) for virtual disk", 37987636cb21Slm66018 request->slice, (vd->nslices - 1)); 3799*bae9e67eSachartre request->status = EINVAL; 3800*bae9e67eSachartre return (0); 38017636cb21Slm66018 } 38027636cb21Slm66018 3803205eeb1aSlm66018 /* 3804205eeb1aSlm66018 * Call the function pointer that starts the operation. 3805205eeb1aSlm66018 */ 3806205eeb1aSlm66018 return (vds_operation[i].start(task)); 38071ae08745Sheppo } 38081ae08745Sheppo 3809205eeb1aSlm66018 /* 3810205eeb1aSlm66018 * Description: 3811205eeb1aSlm66018 * This function is called by both the in-band and descriptor ring 3812205eeb1aSlm66018 * message processing functions paths to actually execute the task 3813205eeb1aSlm66018 * requested by the vDisk client. It in turn calls its worker 3814205eeb1aSlm66018 * function, vd_do_process_task(), to carry our the request. 3815205eeb1aSlm66018 * 3816205eeb1aSlm66018 * Any transport errors (e.g. LDC errors, vDisk protocol errors) are 3817205eeb1aSlm66018 * saved in the 'status' field of the task and are propagated back 3818205eeb1aSlm66018 * up the call stack to trigger a NACK 3819205eeb1aSlm66018 * 3820205eeb1aSlm66018 * Any request errors (e.g. ENOTTY from an ioctl) are saved in 3821205eeb1aSlm66018 * the 'status' field of the request and result in an ACK being sent 3822205eeb1aSlm66018 * by the completion handler. 3823205eeb1aSlm66018 * 3824205eeb1aSlm66018 * Parameters: 3825205eeb1aSlm66018 * task - structure containing the request sent from client 3826205eeb1aSlm66018 * 3827205eeb1aSlm66018 * Return Value 3828205eeb1aSlm66018 * 0 - successful synchronous request. 3829205eeb1aSlm66018 * != 0 - transport error (e.g. LDC errors, vDisk protocol) 3830205eeb1aSlm66018 * EINPROGRESS - task will be finished in a completion handler 3831205eeb1aSlm66018 */ 3832205eeb1aSlm66018 static int 3833205eeb1aSlm66018 vd_process_task(vd_task_t *task) 3834205eeb1aSlm66018 { 3835205eeb1aSlm66018 vd_t *vd = task->vd; 3836205eeb1aSlm66018 int status; 38371ae08745Sheppo 3838205eeb1aSlm66018 DTRACE_PROBE1(task__start, vd_task_t *, task); 38393af08d82Slm66018 3840205eeb1aSlm66018 task->status = vd_do_process_task(task); 3841205eeb1aSlm66018 3842205eeb1aSlm66018 /* 3843205eeb1aSlm66018 * If the task processing function returned EINPROGRESS indicating 3844205eeb1aSlm66018 * that the task needs completing then schedule a taskq entry to 3845205eeb1aSlm66018 * finish it now. 3846205eeb1aSlm66018 * 3847205eeb1aSlm66018 * Otherwise the task processing function returned either zero 3848205eeb1aSlm66018 * indicating that the task was finished in the start function (and we 3849205eeb1aSlm66018 * don't need to wait in a completion function) or the start function 3850205eeb1aSlm66018 * returned an error - in both cases all that needs to happen is the 3851205eeb1aSlm66018 * notification to the vDisk client higher up the call stack. 3852205eeb1aSlm66018 * If the task was using a Descriptor Ring, we need to mark it as done 3853205eeb1aSlm66018 * at this stage. 3854205eeb1aSlm66018 */ 3855205eeb1aSlm66018 if (task->status == EINPROGRESS) { 3856d10e4ef2Snarayan /* Queue a task to complete the operation */ 3857205eeb1aSlm66018 (void) ddi_taskq_dispatch(vd->completionq, vd_complete, 3858d10e4ef2Snarayan task, DDI_SLEEP); 3859d10e4ef2Snarayan 3860f0ca1d9aSsb155480 } else if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE_V1_0)) { 3861205eeb1aSlm66018 /* Update the dring element if it's a dring client */ 3862205eeb1aSlm66018 status = vd_mark_elem_done(vd, task->index, 3863205eeb1aSlm66018 task->request->status, task->request->nbytes); 3864205eeb1aSlm66018 if (status == ECONNRESET) 3865205eeb1aSlm66018 vd_mark_in_reset(vd); 3866205eeb1aSlm66018 } 3867205eeb1aSlm66018 3868205eeb1aSlm66018 return (task->status); 38691ae08745Sheppo } 38701ae08745Sheppo 38711ae08745Sheppo /* 38720a55fbb7Slm66018 * Return true if the "type", "subtype", and "env" fields of the "tag" first 38730a55fbb7Slm66018 * argument match the corresponding remaining arguments; otherwise, return false 38741ae08745Sheppo */ 38750a55fbb7Slm66018 boolean_t 38761ae08745Sheppo vd_msgtype(vio_msg_tag_t *tag, int type, int subtype, int env) 38771ae08745Sheppo { 38781ae08745Sheppo return ((tag->vio_msgtype == type) && 38791ae08745Sheppo (tag->vio_subtype == subtype) && 38800a55fbb7Slm66018 (tag->vio_subtype_env == env)) ? B_TRUE : B_FALSE; 38811ae08745Sheppo } 38821ae08745Sheppo 38830a55fbb7Slm66018 /* 38840a55fbb7Slm66018 * Check whether the major/minor version specified in "ver_msg" is supported 38850a55fbb7Slm66018 * by this server. 38860a55fbb7Slm66018 */ 38870a55fbb7Slm66018 static boolean_t 38880a55fbb7Slm66018 vds_supported_version(vio_ver_msg_t *ver_msg) 38890a55fbb7Slm66018 { 38900a55fbb7Slm66018 for (int i = 0; i < vds_num_versions; i++) { 38910a55fbb7Slm66018 ASSERT(vds_version[i].major > 0); 38920a55fbb7Slm66018 ASSERT((i == 0) || 38930a55fbb7Slm66018 (vds_version[i].major < vds_version[i-1].major)); 38940a55fbb7Slm66018 38950a55fbb7Slm66018 /* 38960a55fbb7Slm66018 * If the major versions match, adjust the minor version, if 38970a55fbb7Slm66018 * necessary, down to the highest value supported by this 38980a55fbb7Slm66018 * server and return true so this message will get "ack"ed; 38990a55fbb7Slm66018 * the client should also support all minor versions lower 39000a55fbb7Slm66018 * than the value it sent 39010a55fbb7Slm66018 */ 39020a55fbb7Slm66018 if (ver_msg->ver_major == vds_version[i].major) { 39030a55fbb7Slm66018 if (ver_msg->ver_minor > vds_version[i].minor) { 39040a55fbb7Slm66018 PR0("Adjusting minor version from %u to %u", 39050a55fbb7Slm66018 ver_msg->ver_minor, vds_version[i].minor); 39060a55fbb7Slm66018 ver_msg->ver_minor = vds_version[i].minor; 39070a55fbb7Slm66018 } 39080a55fbb7Slm66018 return (B_TRUE); 39090a55fbb7Slm66018 } 39100a55fbb7Slm66018 39110a55fbb7Slm66018 /* 39120a55fbb7Slm66018 * If the message contains a higher major version number, set 39130a55fbb7Slm66018 * the message's major/minor versions to the current values 39140a55fbb7Slm66018 * and return false, so this message will get "nack"ed with 39150a55fbb7Slm66018 * these values, and the client will potentially try again 39160a55fbb7Slm66018 * with the same or a lower version 39170a55fbb7Slm66018 */ 39180a55fbb7Slm66018 if (ver_msg->ver_major > vds_version[i].major) { 39190a55fbb7Slm66018 ver_msg->ver_major = vds_version[i].major; 39200a55fbb7Slm66018 ver_msg->ver_minor = vds_version[i].minor; 39210a55fbb7Slm66018 return (B_FALSE); 39220a55fbb7Slm66018 } 39230a55fbb7Slm66018 39240a55fbb7Slm66018 /* 39250a55fbb7Slm66018 * Otherwise, the message's major version is less than the 39260a55fbb7Slm66018 * current major version, so continue the loop to the next 39270a55fbb7Slm66018 * (lower) supported version 39280a55fbb7Slm66018 */ 39290a55fbb7Slm66018 } 39300a55fbb7Slm66018 39310a55fbb7Slm66018 /* 39320a55fbb7Slm66018 * No common version was found; "ground" the version pair in the 39330a55fbb7Slm66018 * message to terminate negotiation 39340a55fbb7Slm66018 */ 39350a55fbb7Slm66018 ver_msg->ver_major = 0; 39360a55fbb7Slm66018 ver_msg->ver_minor = 0; 39370a55fbb7Slm66018 return (B_FALSE); 39380a55fbb7Slm66018 } 39390a55fbb7Slm66018 39400a55fbb7Slm66018 /* 39410a55fbb7Slm66018 * Process a version message from a client. vds expects to receive version 39420a55fbb7Slm66018 * messages from clients seeking service, but never issues version messages 39430a55fbb7Slm66018 * itself; therefore, vds can ACK or NACK client version messages, but does 39440a55fbb7Slm66018 * not expect to receive version-message ACKs or NACKs (and will treat such 39450a55fbb7Slm66018 * messages as invalid). 39460a55fbb7Slm66018 */ 39471ae08745Sheppo static int 39480a55fbb7Slm66018 vd_process_ver_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 39491ae08745Sheppo { 39501ae08745Sheppo vio_ver_msg_t *ver_msg = (vio_ver_msg_t *)msg; 39511ae08745Sheppo 39521ae08745Sheppo 39531ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 39541ae08745Sheppo 39551ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 39561ae08745Sheppo VIO_VER_INFO)) { 39571ae08745Sheppo return (ENOMSG); /* not a version message */ 39581ae08745Sheppo } 39591ae08745Sheppo 39601ae08745Sheppo if (msglen != sizeof (*ver_msg)) { 39613af08d82Slm66018 PR0("Expected %lu-byte version message; " 39621ae08745Sheppo "received %lu bytes", sizeof (*ver_msg), msglen); 39631ae08745Sheppo return (EBADMSG); 39641ae08745Sheppo } 39651ae08745Sheppo 39661ae08745Sheppo if (ver_msg->dev_class != VDEV_DISK) { 39673af08d82Slm66018 PR0("Expected device class %u (disk); received %u", 39681ae08745Sheppo VDEV_DISK, ver_msg->dev_class); 39691ae08745Sheppo return (EBADMSG); 39701ae08745Sheppo } 39711ae08745Sheppo 39720a55fbb7Slm66018 /* 39730a55fbb7Slm66018 * We're talking to the expected kind of client; set our device class 39740a55fbb7Slm66018 * for "ack/nack" back to the client 39750a55fbb7Slm66018 */ 39761ae08745Sheppo ver_msg->dev_class = VDEV_DISK_SERVER; 39770a55fbb7Slm66018 39780a55fbb7Slm66018 /* 39790a55fbb7Slm66018 * Check whether the (valid) version message specifies a version 39800a55fbb7Slm66018 * supported by this server. If the version is not supported, return 39810a55fbb7Slm66018 * EBADMSG so the message will get "nack"ed; vds_supported_version() 39820a55fbb7Slm66018 * will have updated the message with a supported version for the 39830a55fbb7Slm66018 * client to consider 39840a55fbb7Slm66018 */ 39850a55fbb7Slm66018 if (!vds_supported_version(ver_msg)) 39860a55fbb7Slm66018 return (EBADMSG); 39870a55fbb7Slm66018 39880a55fbb7Slm66018 39890a55fbb7Slm66018 /* 39900a55fbb7Slm66018 * A version has been agreed upon; use the client's SID for 39910a55fbb7Slm66018 * communication on this channel now 39920a55fbb7Slm66018 */ 39930a55fbb7Slm66018 ASSERT(!(vd->initialized & VD_SID)); 39940a55fbb7Slm66018 vd->sid = ver_msg->tag.vio_sid; 39950a55fbb7Slm66018 vd->initialized |= VD_SID; 39960a55fbb7Slm66018 39970a55fbb7Slm66018 /* 399817cadca8Slm66018 * Store the negotiated major and minor version values in the "vd" data 399917cadca8Slm66018 * structure so that we can check if certain operations are supported 400017cadca8Slm66018 * by the client. 40010a55fbb7Slm66018 */ 400217cadca8Slm66018 vd->version.major = ver_msg->ver_major; 400317cadca8Slm66018 vd->version.minor = ver_msg->ver_minor; 40040a55fbb7Slm66018 40050a55fbb7Slm66018 PR0("Using major version %u, minor version %u", 40060a55fbb7Slm66018 ver_msg->ver_major, ver_msg->ver_minor); 40071ae08745Sheppo return (0); 40081ae08745Sheppo } 40091ae08745Sheppo 401017cadca8Slm66018 static void 401117cadca8Slm66018 vd_set_exported_operations(vd_t *vd) 401217cadca8Slm66018 { 401317cadca8Slm66018 vd->operations = 0; /* clear field */ 401417cadca8Slm66018 401517cadca8Slm66018 /* 401617cadca8Slm66018 * We need to check from the highest version supported to the 401717cadca8Slm66018 * lowest because versions with a higher minor number implicitly 401817cadca8Slm66018 * support versions with a lower minor number. 401917cadca8Slm66018 */ 402017cadca8Slm66018 if (vio_ver_is_supported(vd->version, 1, 1)) { 402117cadca8Slm66018 ASSERT(vd->open_flags & FREAD); 402217cadca8Slm66018 vd->operations |= VD_OP_MASK_READ; 402317cadca8Slm66018 402417cadca8Slm66018 if (vd->open_flags & FWRITE) 402517cadca8Slm66018 vd->operations |= VD_OP_MASK_WRITE; 402617cadca8Slm66018 40272f5224aeSachartre if (vd->scsi) 40282f5224aeSachartre vd->operations |= VD_OP_MASK_SCSI; 40292f5224aeSachartre 403017cadca8Slm66018 if (vd->file && vd_file_is_iso_image(vd)) { 403117cadca8Slm66018 /* 403217cadca8Slm66018 * can't write to ISO images, make sure that write 403317cadca8Slm66018 * support is not set in case administrator did not 403417cadca8Slm66018 * use "options=ro" when doing an ldm add-vdsdev 403517cadca8Slm66018 */ 403617cadca8Slm66018 vd->operations &= ~VD_OP_MASK_WRITE; 403717cadca8Slm66018 } 403817cadca8Slm66018 } else if (vio_ver_is_supported(vd->version, 1, 0)) { 403917cadca8Slm66018 vd->operations = VD_OP_MASK_READ | VD_OP_MASK_WRITE; 404017cadca8Slm66018 } 404117cadca8Slm66018 404217cadca8Slm66018 /* we should have already agreed on a version */ 404317cadca8Slm66018 ASSERT(vd->operations != 0); 404417cadca8Slm66018 } 404517cadca8Slm66018 40461ae08745Sheppo static int 40471ae08745Sheppo vd_process_attr_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 40481ae08745Sheppo { 40491ae08745Sheppo vd_attr_msg_t *attr_msg = (vd_attr_msg_t *)msg; 40503c96341aSnarayan int status, retry = 0; 40511ae08745Sheppo 40521ae08745Sheppo 40531ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 40541ae08745Sheppo 40551ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 40561ae08745Sheppo VIO_ATTR_INFO)) { 4057d10e4ef2Snarayan PR0("Message is not an attribute message"); 4058d10e4ef2Snarayan return (ENOMSG); 40591ae08745Sheppo } 40601ae08745Sheppo 40611ae08745Sheppo if (msglen != sizeof (*attr_msg)) { 40623af08d82Slm66018 PR0("Expected %lu-byte attribute message; " 40631ae08745Sheppo "received %lu bytes", sizeof (*attr_msg), msglen); 40641ae08745Sheppo return (EBADMSG); 40651ae08745Sheppo } 40661ae08745Sheppo 40671ae08745Sheppo if (attr_msg->max_xfer_sz == 0) { 40683af08d82Slm66018 PR0("Received maximum transfer size of 0 from client"); 40691ae08745Sheppo return (EBADMSG); 40701ae08745Sheppo } 40711ae08745Sheppo 40721ae08745Sheppo if ((attr_msg->xfer_mode != VIO_DESC_MODE) && 4073f0ca1d9aSsb155480 (attr_msg->xfer_mode != VIO_DRING_MODE_V1_0)) { 40743af08d82Slm66018 PR0("Client requested unsupported transfer mode"); 40751ae08745Sheppo return (EBADMSG); 40761ae08745Sheppo } 40771ae08745Sheppo 40783c96341aSnarayan /* 40793c96341aSnarayan * check if the underlying disk is ready, if not try accessing 40803c96341aSnarayan * the device again. Open the vdisk device and extract info 40813c96341aSnarayan * about it, as this is needed to respond to the attr info msg 40823c96341aSnarayan */ 40833c96341aSnarayan if ((vd->initialized & VD_DISK_READY) == 0) { 40843c96341aSnarayan PR0("Retry setting up disk (%s)", vd->device_path); 40853c96341aSnarayan do { 40863c96341aSnarayan status = vd_setup_vd(vd); 40873c96341aSnarayan if (status != EAGAIN || ++retry > vds_dev_retries) 40883c96341aSnarayan break; 40893c96341aSnarayan 40903c96341aSnarayan /* incremental delay */ 40913c96341aSnarayan delay(drv_usectohz(vds_dev_delay)); 40923c96341aSnarayan 40933c96341aSnarayan /* if vdisk is no longer enabled - return error */ 40943c96341aSnarayan if (!vd_enabled(vd)) 40953c96341aSnarayan return (ENXIO); 40963c96341aSnarayan 40973c96341aSnarayan } while (status == EAGAIN); 40983c96341aSnarayan 40993c96341aSnarayan if (status) 41003c96341aSnarayan return (ENXIO); 41013c96341aSnarayan 41023c96341aSnarayan vd->initialized |= VD_DISK_READY; 41033c96341aSnarayan ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR); 41048fce2fd6Sachartre PR0("vdisk_type = %s, volume = %s, file = %s, nslices = %u", 41053c96341aSnarayan ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"), 41068fce2fd6Sachartre (vd->volume ? "yes" : "no"), 41073c96341aSnarayan (vd->file ? "yes" : "no"), 41083c96341aSnarayan vd->nslices); 41093c96341aSnarayan } 41103c96341aSnarayan 41111ae08745Sheppo /* Success: valid message and transfer mode */ 41121ae08745Sheppo vd->xfer_mode = attr_msg->xfer_mode; 41133af08d82Slm66018 41141ae08745Sheppo if (vd->xfer_mode == VIO_DESC_MODE) { 41153af08d82Slm66018 41161ae08745Sheppo /* 41171ae08745Sheppo * The vd_dring_inband_msg_t contains one cookie; need room 41181ae08745Sheppo * for up to n-1 more cookies, where "n" is the number of full 41191ae08745Sheppo * pages plus possibly one partial page required to cover 41201ae08745Sheppo * "max_xfer_sz". Add room for one more cookie if 41211ae08745Sheppo * "max_xfer_sz" isn't an integral multiple of the page size. 41221ae08745Sheppo * Must first get the maximum transfer size in bytes. 41231ae08745Sheppo */ 41241ae08745Sheppo size_t max_xfer_bytes = attr_msg->vdisk_block_size ? 41251ae08745Sheppo attr_msg->vdisk_block_size*attr_msg->max_xfer_sz : 41261ae08745Sheppo attr_msg->max_xfer_sz; 41271ae08745Sheppo size_t max_inband_msglen = 41281ae08745Sheppo sizeof (vd_dring_inband_msg_t) + 41291ae08745Sheppo ((max_xfer_bytes/PAGESIZE + 41301ae08745Sheppo ((max_xfer_bytes % PAGESIZE) ? 1 : 0))* 41311ae08745Sheppo (sizeof (ldc_mem_cookie_t))); 41321ae08745Sheppo 41331ae08745Sheppo /* 41341ae08745Sheppo * Set the maximum expected message length to 41351ae08745Sheppo * accommodate in-band-descriptor messages with all 41361ae08745Sheppo * their cookies 41371ae08745Sheppo */ 41381ae08745Sheppo vd->max_msglen = MAX(vd->max_msglen, max_inband_msglen); 4139d10e4ef2Snarayan 4140d10e4ef2Snarayan /* 4141d10e4ef2Snarayan * Initialize the data structure for processing in-band I/O 4142d10e4ef2Snarayan * request descriptors 4143d10e4ef2Snarayan */ 4144d10e4ef2Snarayan vd->inband_task.vd = vd; 41453af08d82Slm66018 vd->inband_task.msg = kmem_alloc(vd->max_msglen, KM_SLEEP); 4146d10e4ef2Snarayan vd->inband_task.index = 0; 4147d10e4ef2Snarayan vd->inband_task.type = VD_FINAL_RANGE_TASK; /* range == 1 */ 41481ae08745Sheppo } 41491ae08745Sheppo 4150e1ebb9ecSlm66018 /* Return the device's block size and max transfer size to the client */ 41512f5224aeSachartre attr_msg->vdisk_block_size = vd->vdisk_block_size; 4152e1ebb9ecSlm66018 attr_msg->max_xfer_sz = vd->max_xfer_sz; 4153e1ebb9ecSlm66018 41541ae08745Sheppo attr_msg->vdisk_size = vd->vdisk_size; 4155*bae9e67eSachartre attr_msg->vdisk_type = (vd_slice_single_slice)? vd->vdisk_type : 4156*bae9e67eSachartre VD_DISK_TYPE_DISK; 415717cadca8Slm66018 attr_msg->vdisk_media = vd->vdisk_media; 415817cadca8Slm66018 415917cadca8Slm66018 /* Discover and save the list of supported VD_OP_XXX operations */ 416017cadca8Slm66018 vd_set_exported_operations(vd); 416117cadca8Slm66018 attr_msg->operations = vd->operations; 416217cadca8Slm66018 41631ae08745Sheppo PR0("%s", VD_CLIENT(vd)); 41643af08d82Slm66018 41653af08d82Slm66018 ASSERT(vd->dring_task == NULL); 41663af08d82Slm66018 41671ae08745Sheppo return (0); 41681ae08745Sheppo } 41691ae08745Sheppo 41701ae08745Sheppo static int 41711ae08745Sheppo vd_process_dring_reg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 41721ae08745Sheppo { 41731ae08745Sheppo int status; 41741ae08745Sheppo size_t expected; 41751ae08745Sheppo ldc_mem_info_t dring_minfo; 41761ae08745Sheppo vio_dring_reg_msg_t *reg_msg = (vio_dring_reg_msg_t *)msg; 41771ae08745Sheppo 41781ae08745Sheppo 41791ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 41801ae08745Sheppo 41811ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 41821ae08745Sheppo VIO_DRING_REG)) { 4183d10e4ef2Snarayan PR0("Message is not a register-dring message"); 4184d10e4ef2Snarayan return (ENOMSG); 41851ae08745Sheppo } 41861ae08745Sheppo 41871ae08745Sheppo if (msglen < sizeof (*reg_msg)) { 41883af08d82Slm66018 PR0("Expected at least %lu-byte register-dring message; " 41891ae08745Sheppo "received %lu bytes", sizeof (*reg_msg), msglen); 41901ae08745Sheppo return (EBADMSG); 41911ae08745Sheppo } 41921ae08745Sheppo 41931ae08745Sheppo expected = sizeof (*reg_msg) + 41941ae08745Sheppo (reg_msg->ncookies - 1)*(sizeof (reg_msg->cookie[0])); 41951ae08745Sheppo if (msglen != expected) { 41963af08d82Slm66018 PR0("Expected %lu-byte register-dring message; " 41971ae08745Sheppo "received %lu bytes", expected, msglen); 41981ae08745Sheppo return (EBADMSG); 41991ae08745Sheppo } 42001ae08745Sheppo 42011ae08745Sheppo if (vd->initialized & VD_DRING) { 42023af08d82Slm66018 PR0("A dring was previously registered; only support one"); 42031ae08745Sheppo return (EBADMSG); 42041ae08745Sheppo } 42051ae08745Sheppo 4206d10e4ef2Snarayan if (reg_msg->num_descriptors > INT32_MAX) { 42073af08d82Slm66018 PR0("reg_msg->num_descriptors = %u; must be <= %u (%s)", 4208d10e4ef2Snarayan reg_msg->ncookies, INT32_MAX, STRINGIZE(INT32_MAX)); 4209d10e4ef2Snarayan return (EBADMSG); 4210d10e4ef2Snarayan } 4211d10e4ef2Snarayan 42121ae08745Sheppo if (reg_msg->ncookies != 1) { 42131ae08745Sheppo /* 42141ae08745Sheppo * In addition to fixing the assertion in the success case 42151ae08745Sheppo * below, supporting drings which require more than one 42161ae08745Sheppo * "cookie" requires increasing the value of vd->max_msglen 42171ae08745Sheppo * somewhere in the code path prior to receiving the message 42181ae08745Sheppo * which results in calling this function. Note that without 42191ae08745Sheppo * making this change, the larger message size required to 42201ae08745Sheppo * accommodate multiple cookies cannot be successfully 42211ae08745Sheppo * received, so this function will not even get called. 42221ae08745Sheppo * Gracefully accommodating more dring cookies might 42231ae08745Sheppo * reasonably demand exchanging an additional attribute or 42241ae08745Sheppo * making a minor protocol adjustment 42251ae08745Sheppo */ 42263af08d82Slm66018 PR0("reg_msg->ncookies = %u != 1", reg_msg->ncookies); 42271ae08745Sheppo return (EBADMSG); 42281ae08745Sheppo } 42291ae08745Sheppo 42301ae08745Sheppo status = ldc_mem_dring_map(vd->ldc_handle, reg_msg->cookie, 42311ae08745Sheppo reg_msg->ncookies, reg_msg->num_descriptors, 42324bac2208Snarayan reg_msg->descriptor_size, LDC_DIRECT_MAP, &vd->dring_handle); 42331ae08745Sheppo if (status != 0) { 42343af08d82Slm66018 PR0("ldc_mem_dring_map() returned errno %d", status); 42351ae08745Sheppo return (status); 42361ae08745Sheppo } 42371ae08745Sheppo 42381ae08745Sheppo /* 42391ae08745Sheppo * To remove the need for this assertion, must call 42401ae08745Sheppo * ldc_mem_dring_nextcookie() successfully ncookies-1 times after a 42411ae08745Sheppo * successful call to ldc_mem_dring_map() 42421ae08745Sheppo */ 42431ae08745Sheppo ASSERT(reg_msg->ncookies == 1); 42441ae08745Sheppo 42451ae08745Sheppo if ((status = 42461ae08745Sheppo ldc_mem_dring_info(vd->dring_handle, &dring_minfo)) != 0) { 42473af08d82Slm66018 PR0("ldc_mem_dring_info() returned errno %d", status); 42481ae08745Sheppo if ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0) 42493af08d82Slm66018 PR0("ldc_mem_dring_unmap() returned errno %d", status); 42501ae08745Sheppo return (status); 42511ae08745Sheppo } 42521ae08745Sheppo 42531ae08745Sheppo if (dring_minfo.vaddr == NULL) { 42543af08d82Slm66018 PR0("Descriptor ring virtual address is NULL"); 42550a55fbb7Slm66018 return (ENXIO); 42561ae08745Sheppo } 42571ae08745Sheppo 42581ae08745Sheppo 4259d10e4ef2Snarayan /* Initialize for valid message and mapped dring */ 42601ae08745Sheppo PR1("descriptor size = %u, dring length = %u", 42611ae08745Sheppo vd->descriptor_size, vd->dring_len); 42621ae08745Sheppo vd->initialized |= VD_DRING; 42631ae08745Sheppo vd->dring_ident = 1; /* "There Can Be Only One" */ 42641ae08745Sheppo vd->dring = dring_minfo.vaddr; 42651ae08745Sheppo vd->descriptor_size = reg_msg->descriptor_size; 42661ae08745Sheppo vd->dring_len = reg_msg->num_descriptors; 42671ae08745Sheppo reg_msg->dring_ident = vd->dring_ident; 4268d10e4ef2Snarayan 4269d10e4ef2Snarayan /* 4270d10e4ef2Snarayan * Allocate and initialize a "shadow" array of data structures for 4271d10e4ef2Snarayan * tasks to process I/O requests in dring elements 4272d10e4ef2Snarayan */ 4273d10e4ef2Snarayan vd->dring_task = 4274d10e4ef2Snarayan kmem_zalloc((sizeof (*vd->dring_task)) * vd->dring_len, KM_SLEEP); 4275d10e4ef2Snarayan for (int i = 0; i < vd->dring_len; i++) { 4276d10e4ef2Snarayan vd->dring_task[i].vd = vd; 4277d10e4ef2Snarayan vd->dring_task[i].index = i; 4278d10e4ef2Snarayan vd->dring_task[i].request = &VD_DRING_ELEM(i)->payload; 42794bac2208Snarayan 42804bac2208Snarayan status = ldc_mem_alloc_handle(vd->ldc_handle, 42814bac2208Snarayan &(vd->dring_task[i].mhdl)); 42824bac2208Snarayan if (status) { 42833af08d82Slm66018 PR0("ldc_mem_alloc_handle() returned err %d ", status); 42844bac2208Snarayan return (ENXIO); 42854bac2208Snarayan } 42863af08d82Slm66018 42873af08d82Slm66018 vd->dring_task[i].msg = kmem_alloc(vd->max_msglen, KM_SLEEP); 4288d10e4ef2Snarayan } 4289d10e4ef2Snarayan 42901ae08745Sheppo return (0); 42911ae08745Sheppo } 42921ae08745Sheppo 42931ae08745Sheppo static int 42941ae08745Sheppo vd_process_dring_unreg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 42951ae08745Sheppo { 42961ae08745Sheppo vio_dring_unreg_msg_t *unreg_msg = (vio_dring_unreg_msg_t *)msg; 42971ae08745Sheppo 42981ae08745Sheppo 42991ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 43001ae08745Sheppo 43011ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 43021ae08745Sheppo VIO_DRING_UNREG)) { 4303d10e4ef2Snarayan PR0("Message is not an unregister-dring message"); 4304d10e4ef2Snarayan return (ENOMSG); 43051ae08745Sheppo } 43061ae08745Sheppo 43071ae08745Sheppo if (msglen != sizeof (*unreg_msg)) { 43083af08d82Slm66018 PR0("Expected %lu-byte unregister-dring message; " 43091ae08745Sheppo "received %lu bytes", sizeof (*unreg_msg), msglen); 43101ae08745Sheppo return (EBADMSG); 43111ae08745Sheppo } 43121ae08745Sheppo 43131ae08745Sheppo if (unreg_msg->dring_ident != vd->dring_ident) { 43143af08d82Slm66018 PR0("Expected dring ident %lu; received %lu", 43151ae08745Sheppo vd->dring_ident, unreg_msg->dring_ident); 43161ae08745Sheppo return (EBADMSG); 43171ae08745Sheppo } 43181ae08745Sheppo 43191ae08745Sheppo return (0); 43201ae08745Sheppo } 43211ae08745Sheppo 43221ae08745Sheppo static int 43231ae08745Sheppo process_rdx_msg(vio_msg_t *msg, size_t msglen) 43241ae08745Sheppo { 43251ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 43261ae08745Sheppo 4327d10e4ef2Snarayan if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX)) { 4328d10e4ef2Snarayan PR0("Message is not an RDX message"); 4329d10e4ef2Snarayan return (ENOMSG); 4330d10e4ef2Snarayan } 43311ae08745Sheppo 43321ae08745Sheppo if (msglen != sizeof (vio_rdx_msg_t)) { 43333af08d82Slm66018 PR0("Expected %lu-byte RDX message; received %lu bytes", 43341ae08745Sheppo sizeof (vio_rdx_msg_t), msglen); 43351ae08745Sheppo return (EBADMSG); 43361ae08745Sheppo } 43371ae08745Sheppo 4338d10e4ef2Snarayan PR0("Valid RDX message"); 43391ae08745Sheppo return (0); 43401ae08745Sheppo } 43411ae08745Sheppo 43421ae08745Sheppo static int 43431ae08745Sheppo vd_check_seq_num(vd_t *vd, uint64_t seq_num) 43441ae08745Sheppo { 43451ae08745Sheppo if ((vd->initialized & VD_SEQ_NUM) && (seq_num != vd->seq_num + 1)) { 43463af08d82Slm66018 PR0("Received seq_num %lu; expected %lu", 43471ae08745Sheppo seq_num, (vd->seq_num + 1)); 43483af08d82Slm66018 PR0("initiating soft reset"); 4349d10e4ef2Snarayan vd_need_reset(vd, B_FALSE); 43501ae08745Sheppo return (1); 43511ae08745Sheppo } 43521ae08745Sheppo 43531ae08745Sheppo vd->seq_num = seq_num; 43541ae08745Sheppo vd->initialized |= VD_SEQ_NUM; /* superfluous after first time... */ 43551ae08745Sheppo return (0); 43561ae08745Sheppo } 43571ae08745Sheppo 43581ae08745Sheppo /* 43591ae08745Sheppo * Return the expected size of an inband-descriptor message with all the 43601ae08745Sheppo * cookies it claims to include 43611ae08745Sheppo */ 43621ae08745Sheppo static size_t 43631ae08745Sheppo expected_inband_size(vd_dring_inband_msg_t *msg) 43641ae08745Sheppo { 43651ae08745Sheppo return ((sizeof (*msg)) + 43661ae08745Sheppo (msg->payload.ncookies - 1)*(sizeof (msg->payload.cookie[0]))); 43671ae08745Sheppo } 43681ae08745Sheppo 43691ae08745Sheppo /* 43701ae08745Sheppo * Process an in-band descriptor message: used with clients like OBP, with 43711ae08745Sheppo * which vds exchanges descriptors within VIO message payloads, rather than 43721ae08745Sheppo * operating on them within a descriptor ring 43731ae08745Sheppo */ 43741ae08745Sheppo static int 43753af08d82Slm66018 vd_process_desc_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 43761ae08745Sheppo { 43771ae08745Sheppo size_t expected; 43781ae08745Sheppo vd_dring_inband_msg_t *desc_msg = (vd_dring_inband_msg_t *)msg; 43791ae08745Sheppo 43801ae08745Sheppo 43811ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 43821ae08745Sheppo 43831ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO, 4384d10e4ef2Snarayan VIO_DESC_DATA)) { 4385d10e4ef2Snarayan PR1("Message is not an in-band-descriptor message"); 4386d10e4ef2Snarayan return (ENOMSG); 4387d10e4ef2Snarayan } 43881ae08745Sheppo 43891ae08745Sheppo if (msglen < sizeof (*desc_msg)) { 43903af08d82Slm66018 PR0("Expected at least %lu-byte descriptor message; " 43911ae08745Sheppo "received %lu bytes", sizeof (*desc_msg), msglen); 43921ae08745Sheppo return (EBADMSG); 43931ae08745Sheppo } 43941ae08745Sheppo 43951ae08745Sheppo if (msglen != (expected = expected_inband_size(desc_msg))) { 43963af08d82Slm66018 PR0("Expected %lu-byte descriptor message; " 43971ae08745Sheppo "received %lu bytes", expected, msglen); 43981ae08745Sheppo return (EBADMSG); 43991ae08745Sheppo } 44001ae08745Sheppo 4401d10e4ef2Snarayan if (vd_check_seq_num(vd, desc_msg->hdr.seq_num) != 0) 44021ae08745Sheppo return (EBADMSG); 44031ae08745Sheppo 4404d10e4ef2Snarayan /* 4405d10e4ef2Snarayan * Valid message: Set up the in-band descriptor task and process the 4406d10e4ef2Snarayan * request. Arrange to acknowledge the client's message, unless an 4407d10e4ef2Snarayan * error processing the descriptor task results in setting 4408d10e4ef2Snarayan * VIO_SUBTYPE_NACK 4409d10e4ef2Snarayan */ 4410d10e4ef2Snarayan PR1("Valid in-band-descriptor message"); 4411d10e4ef2Snarayan msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 44123af08d82Slm66018 44133af08d82Slm66018 ASSERT(vd->inband_task.msg != NULL); 44143af08d82Slm66018 44153af08d82Slm66018 bcopy(msg, vd->inband_task.msg, msglen); 4416d10e4ef2Snarayan vd->inband_task.msglen = msglen; 44173af08d82Slm66018 44183af08d82Slm66018 /* 44193af08d82Slm66018 * The task request is now the payload of the message 44203af08d82Slm66018 * that was just copied into the body of the task. 44213af08d82Slm66018 */ 44223af08d82Slm66018 desc_msg = (vd_dring_inband_msg_t *)vd->inband_task.msg; 4423d10e4ef2Snarayan vd->inband_task.request = &desc_msg->payload; 44243af08d82Slm66018 4425d10e4ef2Snarayan return (vd_process_task(&vd->inband_task)); 44261ae08745Sheppo } 44271ae08745Sheppo 44281ae08745Sheppo static int 4429d10e4ef2Snarayan vd_process_element(vd_t *vd, vd_task_type_t type, uint32_t idx, 44303af08d82Slm66018 vio_msg_t *msg, size_t msglen) 44311ae08745Sheppo { 44321ae08745Sheppo int status; 4433d10e4ef2Snarayan boolean_t ready; 4434d10e4ef2Snarayan vd_dring_entry_t *elem = VD_DRING_ELEM(idx); 44351ae08745Sheppo 44361ae08745Sheppo 4437d10e4ef2Snarayan /* Accept the updated dring element */ 4438d10e4ef2Snarayan if ((status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) { 44393af08d82Slm66018 PR0("ldc_mem_dring_acquire() returned errno %d", status); 44401ae08745Sheppo return (status); 44411ae08745Sheppo } 4442d10e4ef2Snarayan ready = (elem->hdr.dstate == VIO_DESC_READY); 4443d10e4ef2Snarayan if (ready) { 4444d10e4ef2Snarayan elem->hdr.dstate = VIO_DESC_ACCEPTED; 4445d10e4ef2Snarayan } else { 44463af08d82Slm66018 PR0("descriptor %u not ready", idx); 4447d10e4ef2Snarayan VD_DUMP_DRING_ELEM(elem); 4448d10e4ef2Snarayan } 4449d10e4ef2Snarayan if ((status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) { 44503af08d82Slm66018 PR0("ldc_mem_dring_release() returned errno %d", status); 44511ae08745Sheppo return (status); 44521ae08745Sheppo } 4453d10e4ef2Snarayan if (!ready) 4454d10e4ef2Snarayan return (EBUSY); 44551ae08745Sheppo 44561ae08745Sheppo 4457d10e4ef2Snarayan /* Initialize a task and process the accepted element */ 4458d10e4ef2Snarayan PR1("Processing dring element %u", idx); 4459d10e4ef2Snarayan vd->dring_task[idx].type = type; 44603af08d82Slm66018 44613af08d82Slm66018 /* duplicate msg buf for cookies etc. */ 44623af08d82Slm66018 bcopy(msg, vd->dring_task[idx].msg, msglen); 44633af08d82Slm66018 4464d10e4ef2Snarayan vd->dring_task[idx].msglen = msglen; 4465205eeb1aSlm66018 return (vd_process_task(&vd->dring_task[idx])); 44661ae08745Sheppo } 44671ae08745Sheppo 44681ae08745Sheppo static int 4469d10e4ef2Snarayan vd_process_element_range(vd_t *vd, int start, int end, 44703af08d82Slm66018 vio_msg_t *msg, size_t msglen) 4471d10e4ef2Snarayan { 4472d10e4ef2Snarayan int i, n, nelem, status = 0; 4473d10e4ef2Snarayan boolean_t inprogress = B_FALSE; 4474d10e4ef2Snarayan vd_task_type_t type; 4475d10e4ef2Snarayan 4476d10e4ef2Snarayan 4477d10e4ef2Snarayan ASSERT(start >= 0); 4478d10e4ef2Snarayan ASSERT(end >= 0); 4479d10e4ef2Snarayan 4480d10e4ef2Snarayan /* 4481d10e4ef2Snarayan * Arrange to acknowledge the client's message, unless an error 4482d10e4ef2Snarayan * processing one of the dring elements results in setting 4483d10e4ef2Snarayan * VIO_SUBTYPE_NACK 4484d10e4ef2Snarayan */ 4485d10e4ef2Snarayan msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 4486d10e4ef2Snarayan 4487d10e4ef2Snarayan /* 4488d10e4ef2Snarayan * Process the dring elements in the range 4489d10e4ef2Snarayan */ 4490d10e4ef2Snarayan nelem = ((end < start) ? end + vd->dring_len : end) - start + 1; 4491d10e4ef2Snarayan for (i = start, n = nelem; n > 0; i = (i + 1) % vd->dring_len, n--) { 4492d10e4ef2Snarayan ((vio_dring_msg_t *)msg)->end_idx = i; 4493d10e4ef2Snarayan type = (n == 1) ? VD_FINAL_RANGE_TASK : VD_NONFINAL_RANGE_TASK; 44943af08d82Slm66018 status = vd_process_element(vd, type, i, msg, msglen); 4495d10e4ef2Snarayan if (status == EINPROGRESS) 4496d10e4ef2Snarayan inprogress = B_TRUE; 4497d10e4ef2Snarayan else if (status != 0) 4498d10e4ef2Snarayan break; 4499d10e4ef2Snarayan } 4500d10e4ef2Snarayan 4501d10e4ef2Snarayan /* 4502d10e4ef2Snarayan * If some, but not all, operations of a multi-element range are in 4503d10e4ef2Snarayan * progress, wait for other operations to complete before returning 4504d10e4ef2Snarayan * (which will result in "ack" or "nack" of the message). Note that 4505d10e4ef2Snarayan * all outstanding operations will need to complete, not just the ones 4506d10e4ef2Snarayan * corresponding to the current range of dring elements; howevever, as 4507d10e4ef2Snarayan * this situation is an error case, performance is less critical. 4508d10e4ef2Snarayan */ 4509d10e4ef2Snarayan if ((nelem > 1) && (status != EINPROGRESS) && inprogress) 4510d10e4ef2Snarayan ddi_taskq_wait(vd->completionq); 4511d10e4ef2Snarayan 4512d10e4ef2Snarayan return (status); 4513d10e4ef2Snarayan } 4514d10e4ef2Snarayan 4515d10e4ef2Snarayan static int 45163af08d82Slm66018 vd_process_dring_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 45171ae08745Sheppo { 45181ae08745Sheppo vio_dring_msg_t *dring_msg = (vio_dring_msg_t *)msg; 45191ae08745Sheppo 45201ae08745Sheppo 45211ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 45221ae08745Sheppo 45231ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO, 45241ae08745Sheppo VIO_DRING_DATA)) { 4525d10e4ef2Snarayan PR1("Message is not a dring-data message"); 4526d10e4ef2Snarayan return (ENOMSG); 45271ae08745Sheppo } 45281ae08745Sheppo 45291ae08745Sheppo if (msglen != sizeof (*dring_msg)) { 45303af08d82Slm66018 PR0("Expected %lu-byte dring message; received %lu bytes", 45311ae08745Sheppo sizeof (*dring_msg), msglen); 45321ae08745Sheppo return (EBADMSG); 45331ae08745Sheppo } 45341ae08745Sheppo 4535d10e4ef2Snarayan if (vd_check_seq_num(vd, dring_msg->seq_num) != 0) 45361ae08745Sheppo return (EBADMSG); 45371ae08745Sheppo 45381ae08745Sheppo if (dring_msg->dring_ident != vd->dring_ident) { 45393af08d82Slm66018 PR0("Expected dring ident %lu; received ident %lu", 45401ae08745Sheppo vd->dring_ident, dring_msg->dring_ident); 45411ae08745Sheppo return (EBADMSG); 45421ae08745Sheppo } 45431ae08745Sheppo 4544d10e4ef2Snarayan if (dring_msg->start_idx >= vd->dring_len) { 45453af08d82Slm66018 PR0("\"start_idx\" = %u; must be less than %u", 4546d10e4ef2Snarayan dring_msg->start_idx, vd->dring_len); 4547d10e4ef2Snarayan return (EBADMSG); 4548d10e4ef2Snarayan } 45491ae08745Sheppo 4550d10e4ef2Snarayan if ((dring_msg->end_idx < 0) || 4551d10e4ef2Snarayan (dring_msg->end_idx >= vd->dring_len)) { 45523af08d82Slm66018 PR0("\"end_idx\" = %u; must be >= 0 and less than %u", 4553d10e4ef2Snarayan dring_msg->end_idx, vd->dring_len); 4554d10e4ef2Snarayan return (EBADMSG); 4555d10e4ef2Snarayan } 4556d10e4ef2Snarayan 4557d10e4ef2Snarayan /* Valid message; process range of updated dring elements */ 4558d10e4ef2Snarayan PR1("Processing descriptor range, start = %u, end = %u", 4559d10e4ef2Snarayan dring_msg->start_idx, dring_msg->end_idx); 4560d10e4ef2Snarayan return (vd_process_element_range(vd, dring_msg->start_idx, 45613af08d82Slm66018 dring_msg->end_idx, msg, msglen)); 45621ae08745Sheppo } 45631ae08745Sheppo 45641ae08745Sheppo static int 45651ae08745Sheppo recv_msg(ldc_handle_t ldc_handle, void *msg, size_t *nbytes) 45661ae08745Sheppo { 45671ae08745Sheppo int retry, status; 45681ae08745Sheppo size_t size = *nbytes; 45691ae08745Sheppo 45701ae08745Sheppo 45711ae08745Sheppo for (retry = 0, status = ETIMEDOUT; 45721ae08745Sheppo retry < vds_ldc_retries && status == ETIMEDOUT; 45731ae08745Sheppo retry++) { 45741ae08745Sheppo PR1("ldc_read() attempt %d", (retry + 1)); 45751ae08745Sheppo *nbytes = size; 45761ae08745Sheppo status = ldc_read(ldc_handle, msg, nbytes); 45771ae08745Sheppo } 45781ae08745Sheppo 45793af08d82Slm66018 if (status) { 45803af08d82Slm66018 PR0("ldc_read() returned errno %d", status); 45813af08d82Slm66018 if (status != ECONNRESET) 45823af08d82Slm66018 return (ENOMSG); 45831ae08745Sheppo return (status); 45841ae08745Sheppo } else if (*nbytes == 0) { 45851ae08745Sheppo PR1("ldc_read() returned 0 and no message read"); 45861ae08745Sheppo return (ENOMSG); 45871ae08745Sheppo } 45881ae08745Sheppo 45891ae08745Sheppo PR1("RCVD %lu-byte message", *nbytes); 45901ae08745Sheppo return (0); 45911ae08745Sheppo } 45921ae08745Sheppo 45931ae08745Sheppo static int 45943af08d82Slm66018 vd_do_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 45951ae08745Sheppo { 45961ae08745Sheppo int status; 45971ae08745Sheppo 45981ae08745Sheppo 45991ae08745Sheppo PR1("Processing (%x/%x/%x) message", msg->tag.vio_msgtype, 46001ae08745Sheppo msg->tag.vio_subtype, msg->tag.vio_subtype_env); 46013af08d82Slm66018 #ifdef DEBUG 46023af08d82Slm66018 vd_decode_tag(msg); 46033af08d82Slm66018 #endif 46041ae08745Sheppo 46051ae08745Sheppo /* 46061ae08745Sheppo * Validate session ID up front, since it applies to all messages 46071ae08745Sheppo * once set 46081ae08745Sheppo */ 46091ae08745Sheppo if ((msg->tag.vio_sid != vd->sid) && (vd->initialized & VD_SID)) { 46103af08d82Slm66018 PR0("Expected SID %u, received %u", vd->sid, 46111ae08745Sheppo msg->tag.vio_sid); 46121ae08745Sheppo return (EBADMSG); 46131ae08745Sheppo } 46141ae08745Sheppo 46153af08d82Slm66018 PR1("\tWhile in state %d (%s)", vd->state, vd_decode_state(vd->state)); 46161ae08745Sheppo 46171ae08745Sheppo /* 46181ae08745Sheppo * Process the received message based on connection state 46191ae08745Sheppo */ 46201ae08745Sheppo switch (vd->state) { 46211ae08745Sheppo case VD_STATE_INIT: /* expect version message */ 46220a55fbb7Slm66018 if ((status = vd_process_ver_msg(vd, msg, msglen)) != 0) 46231ae08745Sheppo return (status); 46241ae08745Sheppo 46251ae08745Sheppo /* Version negotiated, move to that state */ 46261ae08745Sheppo vd->state = VD_STATE_VER; 46271ae08745Sheppo return (0); 46281ae08745Sheppo 46291ae08745Sheppo case VD_STATE_VER: /* expect attribute message */ 46301ae08745Sheppo if ((status = vd_process_attr_msg(vd, msg, msglen)) != 0) 46311ae08745Sheppo return (status); 46321ae08745Sheppo 46331ae08745Sheppo /* Attributes exchanged, move to that state */ 46341ae08745Sheppo vd->state = VD_STATE_ATTR; 46351ae08745Sheppo return (0); 46361ae08745Sheppo 46371ae08745Sheppo case VD_STATE_ATTR: 46381ae08745Sheppo switch (vd->xfer_mode) { 46391ae08745Sheppo case VIO_DESC_MODE: /* expect RDX message */ 46401ae08745Sheppo if ((status = process_rdx_msg(msg, msglen)) != 0) 46411ae08745Sheppo return (status); 46421ae08745Sheppo 46431ae08745Sheppo /* Ready to receive in-band descriptors */ 46441ae08745Sheppo vd->state = VD_STATE_DATA; 46451ae08745Sheppo return (0); 46461ae08745Sheppo 4647f0ca1d9aSsb155480 case VIO_DRING_MODE_V1_0: /* expect register-dring message */ 46481ae08745Sheppo if ((status = 46491ae08745Sheppo vd_process_dring_reg_msg(vd, msg, msglen)) != 0) 46501ae08745Sheppo return (status); 46511ae08745Sheppo 46521ae08745Sheppo /* One dring negotiated, move to that state */ 46531ae08745Sheppo vd->state = VD_STATE_DRING; 46541ae08745Sheppo return (0); 46551ae08745Sheppo 46561ae08745Sheppo default: 46571ae08745Sheppo ASSERT("Unsupported transfer mode"); 46583af08d82Slm66018 PR0("Unsupported transfer mode"); 46591ae08745Sheppo return (ENOTSUP); 46601ae08745Sheppo } 46611ae08745Sheppo 46621ae08745Sheppo case VD_STATE_DRING: /* expect RDX, register-dring, or unreg-dring */ 46631ae08745Sheppo if ((status = process_rdx_msg(msg, msglen)) == 0) { 46641ae08745Sheppo /* Ready to receive data */ 46651ae08745Sheppo vd->state = VD_STATE_DATA; 46661ae08745Sheppo return (0); 46671ae08745Sheppo } else if (status != ENOMSG) { 46681ae08745Sheppo return (status); 46691ae08745Sheppo } 46701ae08745Sheppo 46711ae08745Sheppo 46721ae08745Sheppo /* 46731ae08745Sheppo * If another register-dring message is received, stay in 46741ae08745Sheppo * dring state in case the client sends RDX; although the 46751ae08745Sheppo * protocol allows multiple drings, this server does not 46761ae08745Sheppo * support using more than one 46771ae08745Sheppo */ 46781ae08745Sheppo if ((status = 46791ae08745Sheppo vd_process_dring_reg_msg(vd, msg, msglen)) != ENOMSG) 46801ae08745Sheppo return (status); 46811ae08745Sheppo 46821ae08745Sheppo /* 46831ae08745Sheppo * Acknowledge an unregister-dring message, but reset the 46841ae08745Sheppo * connection anyway: Although the protocol allows 46851ae08745Sheppo * unregistering drings, this server cannot serve a vdisk 46861ae08745Sheppo * without its only dring 46871ae08745Sheppo */ 46881ae08745Sheppo status = vd_process_dring_unreg_msg(vd, msg, msglen); 46891ae08745Sheppo return ((status == 0) ? ENOTSUP : status); 46901ae08745Sheppo 46911ae08745Sheppo case VD_STATE_DATA: 46921ae08745Sheppo switch (vd->xfer_mode) { 46931ae08745Sheppo case VIO_DESC_MODE: /* expect in-band-descriptor message */ 46943af08d82Slm66018 return (vd_process_desc_msg(vd, msg, msglen)); 46951ae08745Sheppo 4696f0ca1d9aSsb155480 case VIO_DRING_MODE_V1_0: /* expect dring-data or unreg-dring */ 46971ae08745Sheppo /* 46981ae08745Sheppo * Typically expect dring-data messages, so handle 46991ae08745Sheppo * them first 47001ae08745Sheppo */ 47011ae08745Sheppo if ((status = vd_process_dring_msg(vd, msg, 47023af08d82Slm66018 msglen)) != ENOMSG) 47031ae08745Sheppo return (status); 47041ae08745Sheppo 47051ae08745Sheppo /* 47061ae08745Sheppo * Acknowledge an unregister-dring message, but reset 47071ae08745Sheppo * the connection anyway: Although the protocol 47081ae08745Sheppo * allows unregistering drings, this server cannot 47091ae08745Sheppo * serve a vdisk without its only dring 47101ae08745Sheppo */ 47111ae08745Sheppo status = vd_process_dring_unreg_msg(vd, msg, msglen); 47121ae08745Sheppo return ((status == 0) ? ENOTSUP : status); 47131ae08745Sheppo 47141ae08745Sheppo default: 47151ae08745Sheppo ASSERT("Unsupported transfer mode"); 47163af08d82Slm66018 PR0("Unsupported transfer mode"); 47171ae08745Sheppo return (ENOTSUP); 47181ae08745Sheppo } 47191ae08745Sheppo 47201ae08745Sheppo default: 47211ae08745Sheppo ASSERT("Invalid client connection state"); 47223af08d82Slm66018 PR0("Invalid client connection state"); 47231ae08745Sheppo return (ENOTSUP); 47241ae08745Sheppo } 47251ae08745Sheppo } 47261ae08745Sheppo 4727d10e4ef2Snarayan static int 47283af08d82Slm66018 vd_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 47291ae08745Sheppo { 47301ae08745Sheppo int status; 47311ae08745Sheppo boolean_t reset_ldc = B_FALSE; 4732205eeb1aSlm66018 vd_task_t task; 47331ae08745Sheppo 47341ae08745Sheppo /* 47351ae08745Sheppo * Check that the message is at least big enough for a "tag", so that 47361ae08745Sheppo * message processing can proceed based on tag-specified message type 47371ae08745Sheppo */ 47381ae08745Sheppo if (msglen < sizeof (vio_msg_tag_t)) { 47393af08d82Slm66018 PR0("Received short (%lu-byte) message", msglen); 47401ae08745Sheppo /* Can't "nack" short message, so drop the big hammer */ 47413af08d82Slm66018 PR0("initiating full reset"); 4742d10e4ef2Snarayan vd_need_reset(vd, B_TRUE); 4743d10e4ef2Snarayan return (EBADMSG); 47441ae08745Sheppo } 47451ae08745Sheppo 47461ae08745Sheppo /* 47471ae08745Sheppo * Process the message 47481ae08745Sheppo */ 47493af08d82Slm66018 switch (status = vd_do_process_msg(vd, msg, msglen)) { 47501ae08745Sheppo case 0: 47511ae08745Sheppo /* "ack" valid, successfully-processed messages */ 47521ae08745Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 47531ae08745Sheppo break; 47541ae08745Sheppo 4755d10e4ef2Snarayan case EINPROGRESS: 4756d10e4ef2Snarayan /* The completion handler will "ack" or "nack" the message */ 4757d10e4ef2Snarayan return (EINPROGRESS); 47581ae08745Sheppo case ENOMSG: 47593af08d82Slm66018 PR0("Received unexpected message"); 47601ae08745Sheppo _NOTE(FALLTHROUGH); 47611ae08745Sheppo case EBADMSG: 47621ae08745Sheppo case ENOTSUP: 4763205eeb1aSlm66018 /* "transport" error will cause NACK of invalid messages */ 47641ae08745Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 47651ae08745Sheppo break; 47661ae08745Sheppo 47671ae08745Sheppo default: 4768205eeb1aSlm66018 /* "transport" error will cause NACK of invalid messages */ 47691ae08745Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 47701ae08745Sheppo /* An LDC error probably occurred, so try resetting it */ 47711ae08745Sheppo reset_ldc = B_TRUE; 47721ae08745Sheppo break; 47731ae08745Sheppo } 47741ae08745Sheppo 47753af08d82Slm66018 PR1("\tResulting in state %d (%s)", vd->state, 47763af08d82Slm66018 vd_decode_state(vd->state)); 47773af08d82Slm66018 4778205eeb1aSlm66018 /* populate the task so we can dispatch it on the taskq */ 4779205eeb1aSlm66018 task.vd = vd; 4780205eeb1aSlm66018 task.msg = msg; 4781205eeb1aSlm66018 task.msglen = msglen; 4782205eeb1aSlm66018 4783205eeb1aSlm66018 /* 4784205eeb1aSlm66018 * Queue a task to send the notification that the operation completed. 4785205eeb1aSlm66018 * We need to ensure that requests are responded to in the correct 4786205eeb1aSlm66018 * order and since the taskq is processed serially this ordering 4787205eeb1aSlm66018 * is maintained. 4788205eeb1aSlm66018 */ 4789205eeb1aSlm66018 (void) ddi_taskq_dispatch(vd->completionq, vd_serial_notify, 4790205eeb1aSlm66018 &task, DDI_SLEEP); 4791205eeb1aSlm66018 4792205eeb1aSlm66018 /* 4793205eeb1aSlm66018 * To ensure handshake negotiations do not happen out of order, such 4794205eeb1aSlm66018 * requests that come through this path should not be done in parallel 4795205eeb1aSlm66018 * so we need to wait here until the response is sent to the client. 4796205eeb1aSlm66018 */ 4797205eeb1aSlm66018 ddi_taskq_wait(vd->completionq); 47981ae08745Sheppo 4799d10e4ef2Snarayan /* Arrange to reset the connection for nack'ed or failed messages */ 48003af08d82Slm66018 if ((status != 0) || reset_ldc) { 48013af08d82Slm66018 PR0("initiating %s reset", 48023af08d82Slm66018 (reset_ldc) ? "full" : "soft"); 4803d10e4ef2Snarayan vd_need_reset(vd, reset_ldc); 48043af08d82Slm66018 } 4805d10e4ef2Snarayan 4806d10e4ef2Snarayan return (status); 4807d10e4ef2Snarayan } 4808d10e4ef2Snarayan 4809d10e4ef2Snarayan static boolean_t 4810d10e4ef2Snarayan vd_enabled(vd_t *vd) 4811d10e4ef2Snarayan { 4812d10e4ef2Snarayan boolean_t enabled; 4813d10e4ef2Snarayan 4814d10e4ef2Snarayan mutex_enter(&vd->lock); 4815d10e4ef2Snarayan enabled = vd->enabled; 4816d10e4ef2Snarayan mutex_exit(&vd->lock); 4817d10e4ef2Snarayan return (enabled); 48181ae08745Sheppo } 48191ae08745Sheppo 48201ae08745Sheppo static void 48210a55fbb7Slm66018 vd_recv_msg(void *arg) 48221ae08745Sheppo { 48231ae08745Sheppo vd_t *vd = (vd_t *)arg; 48243af08d82Slm66018 int rv = 0, status = 0; 48251ae08745Sheppo 48261ae08745Sheppo ASSERT(vd != NULL); 48273af08d82Slm66018 4828d10e4ef2Snarayan PR2("New task to receive incoming message(s)"); 48293af08d82Slm66018 48303af08d82Slm66018 4831d10e4ef2Snarayan while (vd_enabled(vd) && status == 0) { 4832d10e4ef2Snarayan size_t msglen, msgsize; 48333af08d82Slm66018 ldc_status_t lstatus; 4834d10e4ef2Snarayan 48350a55fbb7Slm66018 /* 4836d10e4ef2Snarayan * Receive and process a message 48370a55fbb7Slm66018 */ 4838d10e4ef2Snarayan vd_reset_if_needed(vd); /* can change vd->max_msglen */ 48393af08d82Slm66018 48403af08d82Slm66018 /* 48413af08d82Slm66018 * check if channel is UP - else break out of loop 48423af08d82Slm66018 */ 48433af08d82Slm66018 status = ldc_status(vd->ldc_handle, &lstatus); 48443af08d82Slm66018 if (lstatus != LDC_UP) { 48453af08d82Slm66018 PR0("channel not up (status=%d), exiting recv loop\n", 48463af08d82Slm66018 lstatus); 48473af08d82Slm66018 break; 48483af08d82Slm66018 } 48493af08d82Slm66018 48503af08d82Slm66018 ASSERT(vd->max_msglen != 0); 48513af08d82Slm66018 4852d10e4ef2Snarayan msgsize = vd->max_msglen; /* stable copy for alloc/free */ 48533af08d82Slm66018 msglen = msgsize; /* actual len after recv_msg() */ 48543af08d82Slm66018 48553af08d82Slm66018 status = recv_msg(vd->ldc_handle, vd->vio_msgp, &msglen); 48563af08d82Slm66018 switch (status) { 48573af08d82Slm66018 case 0: 48583af08d82Slm66018 rv = vd_process_msg(vd, (vio_msg_t *)vd->vio_msgp, 48593af08d82Slm66018 msglen); 48603af08d82Slm66018 /* check if max_msglen changed */ 48613af08d82Slm66018 if (msgsize != vd->max_msglen) { 48623af08d82Slm66018 PR0("max_msglen changed 0x%lx to 0x%lx bytes\n", 48633af08d82Slm66018 msgsize, vd->max_msglen); 48643af08d82Slm66018 kmem_free(vd->vio_msgp, msgsize); 48653af08d82Slm66018 vd->vio_msgp = 48663af08d82Slm66018 kmem_alloc(vd->max_msglen, KM_SLEEP); 48673af08d82Slm66018 } 48683af08d82Slm66018 if (rv == EINPROGRESS) 48693af08d82Slm66018 continue; 48703af08d82Slm66018 break; 48713af08d82Slm66018 48723af08d82Slm66018 case ENOMSG: 48733af08d82Slm66018 break; 48743af08d82Slm66018 48753af08d82Slm66018 case ECONNRESET: 48763af08d82Slm66018 PR0("initiating soft reset (ECONNRESET)\n"); 48773af08d82Slm66018 vd_need_reset(vd, B_FALSE); 48783af08d82Slm66018 status = 0; 48793af08d82Slm66018 break; 48803af08d82Slm66018 48813af08d82Slm66018 default: 4882d10e4ef2Snarayan /* Probably an LDC failure; arrange to reset it */ 48833af08d82Slm66018 PR0("initiating full reset (status=0x%x)", status); 4884d10e4ef2Snarayan vd_need_reset(vd, B_TRUE); 48853af08d82Slm66018 break; 48860a55fbb7Slm66018 } 48871ae08745Sheppo } 48883af08d82Slm66018 4889d10e4ef2Snarayan PR2("Task finished"); 48900a55fbb7Slm66018 } 48910a55fbb7Slm66018 48920a55fbb7Slm66018 static uint_t 48931ae08745Sheppo vd_handle_ldc_events(uint64_t event, caddr_t arg) 48941ae08745Sheppo { 48951ae08745Sheppo vd_t *vd = (vd_t *)(void *)arg; 48963af08d82Slm66018 int status; 48971ae08745Sheppo 48981ae08745Sheppo ASSERT(vd != NULL); 4899d10e4ef2Snarayan 4900d10e4ef2Snarayan if (!vd_enabled(vd)) 4901d10e4ef2Snarayan return (LDC_SUCCESS); 4902d10e4ef2Snarayan 49033af08d82Slm66018 if (event & LDC_EVT_DOWN) { 490434683adeSsg70180 PR0("LDC_EVT_DOWN: LDC channel went down"); 49053af08d82Slm66018 49063af08d82Slm66018 vd_need_reset(vd, B_TRUE); 49073af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, 49083af08d82Slm66018 DDI_SLEEP); 49093af08d82Slm66018 if (status == DDI_FAILURE) { 49103af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 49113af08d82Slm66018 vd_need_reset(vd, B_TRUE); 49123af08d82Slm66018 } 49133af08d82Slm66018 } 49143af08d82Slm66018 4915d10e4ef2Snarayan if (event & LDC_EVT_RESET) { 49163af08d82Slm66018 PR0("LDC_EVT_RESET: LDC channel was reset"); 49173af08d82Slm66018 49183af08d82Slm66018 if (vd->state != VD_STATE_INIT) { 49193af08d82Slm66018 PR0("scheduling full reset"); 49203af08d82Slm66018 vd_need_reset(vd, B_FALSE); 49213af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, 49223af08d82Slm66018 vd, DDI_SLEEP); 49233af08d82Slm66018 if (status == DDI_FAILURE) { 49243af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 49253af08d82Slm66018 vd_need_reset(vd, B_TRUE); 49263af08d82Slm66018 } 49273af08d82Slm66018 49283af08d82Slm66018 } else { 49293af08d82Slm66018 PR0("channel already reset, ignoring...\n"); 49303af08d82Slm66018 PR0("doing ldc up...\n"); 49313af08d82Slm66018 (void) ldc_up(vd->ldc_handle); 49323af08d82Slm66018 } 49333af08d82Slm66018 4934d10e4ef2Snarayan return (LDC_SUCCESS); 4935d10e4ef2Snarayan } 4936d10e4ef2Snarayan 4937d10e4ef2Snarayan if (event & LDC_EVT_UP) { 49383af08d82Slm66018 PR0("EVT_UP: LDC is up\nResetting client connection state"); 49393af08d82Slm66018 PR0("initiating soft reset"); 4940d10e4ef2Snarayan vd_need_reset(vd, B_FALSE); 49413af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, 49423af08d82Slm66018 vd, DDI_SLEEP); 49433af08d82Slm66018 if (status == DDI_FAILURE) { 49443af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 49453af08d82Slm66018 vd_need_reset(vd, B_TRUE); 49463af08d82Slm66018 return (LDC_SUCCESS); 49473af08d82Slm66018 } 4948d10e4ef2Snarayan } 4949d10e4ef2Snarayan 4950d10e4ef2Snarayan if (event & LDC_EVT_READ) { 4951d10e4ef2Snarayan int status; 4952d10e4ef2Snarayan 4953d10e4ef2Snarayan PR1("New data available"); 4954d10e4ef2Snarayan /* Queue a task to receive the new data */ 4955d10e4ef2Snarayan status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, 4956d10e4ef2Snarayan DDI_SLEEP); 49573af08d82Slm66018 49583af08d82Slm66018 if (status == DDI_FAILURE) { 49593af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 49603af08d82Slm66018 vd_need_reset(vd, B_TRUE); 49613af08d82Slm66018 } 4962d10e4ef2Snarayan } 4963d10e4ef2Snarayan 4964d10e4ef2Snarayan return (LDC_SUCCESS); 49651ae08745Sheppo } 49661ae08745Sheppo 49671ae08745Sheppo static uint_t 49681ae08745Sheppo vds_check_for_vd(mod_hash_key_t key, mod_hash_val_t *val, void *arg) 49691ae08745Sheppo { 49701ae08745Sheppo _NOTE(ARGUNUSED(key, val)) 49711ae08745Sheppo (*((uint_t *)arg))++; 49721ae08745Sheppo return (MH_WALK_TERMINATE); 49731ae08745Sheppo } 49741ae08745Sheppo 49751ae08745Sheppo 49761ae08745Sheppo static int 49771ae08745Sheppo vds_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 49781ae08745Sheppo { 49791ae08745Sheppo uint_t vd_present = 0; 49801ae08745Sheppo minor_t instance; 49811ae08745Sheppo vds_t *vds; 49821ae08745Sheppo 49831ae08745Sheppo 49841ae08745Sheppo switch (cmd) { 49851ae08745Sheppo case DDI_DETACH: 49861ae08745Sheppo /* the real work happens below */ 49871ae08745Sheppo break; 49881ae08745Sheppo case DDI_SUSPEND: 4989d10e4ef2Snarayan PR0("No action required for DDI_SUSPEND"); 49901ae08745Sheppo return (DDI_SUCCESS); 49911ae08745Sheppo default: 49923af08d82Slm66018 PR0("Unrecognized \"cmd\""); 49931ae08745Sheppo return (DDI_FAILURE); 49941ae08745Sheppo } 49951ae08745Sheppo 49961ae08745Sheppo ASSERT(cmd == DDI_DETACH); 49971ae08745Sheppo instance = ddi_get_instance(dip); 49981ae08745Sheppo if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) { 49993af08d82Slm66018 PR0("Could not get state for instance %u", instance); 50001ae08745Sheppo ddi_soft_state_free(vds_state, instance); 50011ae08745Sheppo return (DDI_FAILURE); 50021ae08745Sheppo } 50031ae08745Sheppo 50041ae08745Sheppo /* Do no detach when serving any vdisks */ 50051ae08745Sheppo mod_hash_walk(vds->vd_table, vds_check_for_vd, &vd_present); 50061ae08745Sheppo if (vd_present) { 50071ae08745Sheppo PR0("Not detaching because serving vdisks"); 50081ae08745Sheppo return (DDI_FAILURE); 50091ae08745Sheppo } 50101ae08745Sheppo 50111ae08745Sheppo PR0("Detaching"); 5012445b4c2eSsb155480 if (vds->initialized & VDS_MDEG) { 50131ae08745Sheppo (void) mdeg_unregister(vds->mdeg); 5014445b4c2eSsb155480 kmem_free(vds->ispecp->specp, sizeof (vds_prop_template)); 5015445b4c2eSsb155480 kmem_free(vds->ispecp, sizeof (mdeg_node_spec_t)); 5016445b4c2eSsb155480 vds->ispecp = NULL; 5017445b4c2eSsb155480 vds->mdeg = NULL; 5018445b4c2eSsb155480 } 5019445b4c2eSsb155480 50208fce2fd6Sachartre vds_driver_types_free(vds); 50218fce2fd6Sachartre 50221ae08745Sheppo if (vds->initialized & VDS_LDI) 50231ae08745Sheppo (void) ldi_ident_release(vds->ldi_ident); 50241ae08745Sheppo mod_hash_destroy_hash(vds->vd_table); 50251ae08745Sheppo ddi_soft_state_free(vds_state, instance); 50261ae08745Sheppo return (DDI_SUCCESS); 50271ae08745Sheppo } 50281ae08745Sheppo 502917cadca8Slm66018 /* 503017cadca8Slm66018 * Description: 503117cadca8Slm66018 * This function checks to see if the file being used as a 503217cadca8Slm66018 * virtual disk is an ISO image. An ISO image is a special 503317cadca8Slm66018 * case which can be booted/installed from like a CD/DVD 503417cadca8Slm66018 * 503517cadca8Slm66018 * Parameters: 503617cadca8Slm66018 * vd - disk on which the operation is performed. 503717cadca8Slm66018 * 503817cadca8Slm66018 * Return Code: 503917cadca8Slm66018 * B_TRUE - The file is an ISO 9660 compliant image 504017cadca8Slm66018 * B_FALSE - just a regular disk image file 504117cadca8Slm66018 */ 504217cadca8Slm66018 static boolean_t 504317cadca8Slm66018 vd_file_is_iso_image(vd_t *vd) 504417cadca8Slm66018 { 504517cadca8Slm66018 char iso_buf[ISO_SECTOR_SIZE]; 504617cadca8Slm66018 int i, rv; 504717cadca8Slm66018 uint_t sec; 504817cadca8Slm66018 504917cadca8Slm66018 ASSERT(vd->file); 505017cadca8Slm66018 505117cadca8Slm66018 /* 505217cadca8Slm66018 * If we have already discovered and saved this info we can 505317cadca8Slm66018 * short-circuit the check and avoid reading the file. 505417cadca8Slm66018 */ 505517cadca8Slm66018 if (vd->vdisk_media == VD_MEDIA_DVD || vd->vdisk_media == VD_MEDIA_CD) 505617cadca8Slm66018 return (B_TRUE); 505717cadca8Slm66018 505817cadca8Slm66018 /* 505917cadca8Slm66018 * We wish to read the sector that should contain the 2nd ISO volume 506017cadca8Slm66018 * descriptor. The second field in this descriptor is called the 506117cadca8Slm66018 * Standard Identifier and is set to CD001 for a CD-ROM compliant 506217cadca8Slm66018 * to the ISO 9660 standard. 506317cadca8Slm66018 */ 506417cadca8Slm66018 sec = (ISO_VOLDESC_SEC * ISO_SECTOR_SIZE) / vd->vdisk_block_size; 506517cadca8Slm66018 rv = vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)iso_buf, 506617cadca8Slm66018 sec, ISO_SECTOR_SIZE); 506717cadca8Slm66018 506817cadca8Slm66018 if (rv < 0) 506917cadca8Slm66018 return (B_FALSE); 507017cadca8Slm66018 507117cadca8Slm66018 for (i = 0; i < ISO_ID_STRLEN; i++) { 507217cadca8Slm66018 if (ISO_STD_ID(iso_buf)[i] != ISO_ID_STRING[i]) 507317cadca8Slm66018 return (B_FALSE); 507417cadca8Slm66018 } 507517cadca8Slm66018 507617cadca8Slm66018 return (B_TRUE); 507717cadca8Slm66018 } 507817cadca8Slm66018 507917cadca8Slm66018 /* 508017cadca8Slm66018 * Description: 508117cadca8Slm66018 * This function checks to see if the virtual device is an ATAPI 508217cadca8Slm66018 * device. ATAPI devices use Group 1 Read/Write commands, so 508317cadca8Slm66018 * any USCSI calls vds makes need to take this into account. 508417cadca8Slm66018 * 508517cadca8Slm66018 * Parameters: 508617cadca8Slm66018 * vd - disk on which the operation is performed. 508717cadca8Slm66018 * 508817cadca8Slm66018 * Return Code: 508917cadca8Slm66018 * B_TRUE - The virtual disk is backed by an ATAPI device 509017cadca8Slm66018 * B_FALSE - not an ATAPI device (presumably SCSI) 509117cadca8Slm66018 */ 509217cadca8Slm66018 static boolean_t 509317cadca8Slm66018 vd_is_atapi_device(vd_t *vd) 509417cadca8Slm66018 { 509517cadca8Slm66018 boolean_t is_atapi = B_FALSE; 509617cadca8Slm66018 char *variantp; 509717cadca8Slm66018 int rv; 509817cadca8Slm66018 509917cadca8Slm66018 ASSERT(vd->ldi_handle[0] != NULL); 510017cadca8Slm66018 ASSERT(!vd->file); 510117cadca8Slm66018 510217cadca8Slm66018 rv = ldi_prop_lookup_string(vd->ldi_handle[0], 510317cadca8Slm66018 (LDI_DEV_T_ANY | DDI_PROP_DONTPASS), "variant", &variantp); 510417cadca8Slm66018 if (rv == DDI_PROP_SUCCESS) { 510517cadca8Slm66018 PR0("'variant' property exists for %s", vd->device_path); 510617cadca8Slm66018 if (strcmp(variantp, "atapi") == 0) 510717cadca8Slm66018 is_atapi = B_TRUE; 510817cadca8Slm66018 ddi_prop_free(variantp); 510917cadca8Slm66018 } 511017cadca8Slm66018 511117cadca8Slm66018 rv = ldi_prop_exists(vd->ldi_handle[0], LDI_DEV_T_ANY, "atapi"); 511217cadca8Slm66018 if (rv) { 511317cadca8Slm66018 PR0("'atapi' property exists for %s", vd->device_path); 511417cadca8Slm66018 is_atapi = B_TRUE; 511517cadca8Slm66018 } 511617cadca8Slm66018 511717cadca8Slm66018 return (is_atapi); 511817cadca8Slm66018 } 511917cadca8Slm66018 51201ae08745Sheppo static int 51212f5224aeSachartre vd_setup_mediainfo(vd_t *vd) 51220a55fbb7Slm66018 { 51232f5224aeSachartre int status, rval; 51244bac2208Snarayan struct dk_minfo dk_minfo; 51250a55fbb7Slm66018 51262f5224aeSachartre ASSERT(vd->ldi_handle[0] != NULL); 51272f5224aeSachartre ASSERT(vd->vdisk_block_size != 0); 51282f5224aeSachartre 51292f5224aeSachartre if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCGMEDIAINFO, 51302f5224aeSachartre (intptr_t)&dk_minfo, (vd->open_flags | FKIOCTL), 51312f5224aeSachartre kcred, &rval)) != 0) 51322f5224aeSachartre return (status); 51332f5224aeSachartre 51342f5224aeSachartre ASSERT(dk_minfo.dki_lbsize % vd->vdisk_block_size == 0); 51352f5224aeSachartre 51362f5224aeSachartre vd->block_size = dk_minfo.dki_lbsize; 51372f5224aeSachartre vd->vdisk_size = (dk_minfo.dki_capacity * dk_minfo.dki_lbsize) / 51382f5224aeSachartre vd->vdisk_block_size; 51392f5224aeSachartre vd->vdisk_media = DK_MEDIATYPE2VD_MEDIATYPE(dk_minfo.dki_media_type); 51402f5224aeSachartre return (0); 51412f5224aeSachartre } 51422f5224aeSachartre 51432f5224aeSachartre static int 51442f5224aeSachartre vd_setup_full_disk(vd_t *vd) 51452f5224aeSachartre { 51462f5224aeSachartre int status; 51472f5224aeSachartre major_t major = getmajor(vd->dev[0]); 51482f5224aeSachartre minor_t minor = getminor(vd->dev[0]) - VD_ENTIRE_DISK_SLICE; 51492f5224aeSachartre 5150047ba61eSachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK); 5151047ba61eSachartre 51522f5224aeSachartre vd->vdisk_block_size = DEV_BSIZE; 51532f5224aeSachartre 51544bac2208Snarayan /* 51554bac2208Snarayan * At this point, vdisk_size is set to the size of partition 2 but 51564bac2208Snarayan * this does not represent the size of the disk because partition 2 51574bac2208Snarayan * may not cover the entire disk and its size does not include reserved 51582f5224aeSachartre * blocks. So we call vd_get_mediainfo to udpate this information and 51592f5224aeSachartre * set the block size and the media type of the disk. 51604bac2208Snarayan */ 51612f5224aeSachartre status = vd_setup_mediainfo(vd); 51622f5224aeSachartre 51632f5224aeSachartre if (status != 0) { 51642f5224aeSachartre if (!vd->scsi) { 51652f5224aeSachartre /* unexpected failure */ 5166690555a1Sachartre PRN("ldi_ioctl(DKIOCGMEDIAINFO) returned errno %d", 51674bac2208Snarayan status); 51680a55fbb7Slm66018 return (status); 51690a55fbb7Slm66018 } 51702f5224aeSachartre 51712f5224aeSachartre /* 51722f5224aeSachartre * The function can fail for SCSI disks which are present but 51732f5224aeSachartre * reserved by another system. In that case, we don't know the 51742f5224aeSachartre * size of the disk and the block size. 51752f5224aeSachartre */ 51762f5224aeSachartre vd->vdisk_size = VD_SIZE_UNKNOWN; 51772f5224aeSachartre vd->block_size = 0; 51782f5224aeSachartre vd->vdisk_media = VD_MEDIA_FIXED; 51792f5224aeSachartre } 51800a55fbb7Slm66018 51810a55fbb7Slm66018 /* Move dev number and LDI handle to entire-disk-slice array elements */ 51820a55fbb7Slm66018 vd->dev[VD_ENTIRE_DISK_SLICE] = vd->dev[0]; 51830a55fbb7Slm66018 vd->dev[0] = 0; 51840a55fbb7Slm66018 vd->ldi_handle[VD_ENTIRE_DISK_SLICE] = vd->ldi_handle[0]; 51850a55fbb7Slm66018 vd->ldi_handle[0] = NULL; 51860a55fbb7Slm66018 51870a55fbb7Slm66018 /* Initialize device numbers for remaining slices and open them */ 51880a55fbb7Slm66018 for (int slice = 0; slice < vd->nslices; slice++) { 51890a55fbb7Slm66018 /* 51900a55fbb7Slm66018 * Skip the entire-disk slice, as it's already open and its 51910a55fbb7Slm66018 * device known 51920a55fbb7Slm66018 */ 51930a55fbb7Slm66018 if (slice == VD_ENTIRE_DISK_SLICE) 51940a55fbb7Slm66018 continue; 51950a55fbb7Slm66018 ASSERT(vd->dev[slice] == 0); 51960a55fbb7Slm66018 ASSERT(vd->ldi_handle[slice] == NULL); 51970a55fbb7Slm66018 51980a55fbb7Slm66018 /* 51990a55fbb7Slm66018 * Construct the device number for the current slice 52000a55fbb7Slm66018 */ 52010a55fbb7Slm66018 vd->dev[slice] = makedevice(major, (minor + slice)); 52020a55fbb7Slm66018 52030a55fbb7Slm66018 /* 520434683adeSsg70180 * Open all slices of the disk to serve them to the client. 520534683adeSsg70180 * Slices are opened exclusively to prevent other threads or 520634683adeSsg70180 * processes in the service domain from performing I/O to 520734683adeSsg70180 * slices being accessed by a client. Failure to open a slice 520834683adeSsg70180 * results in vds not serving this disk, as the client could 520934683adeSsg70180 * attempt (and should be able) to access any slice immediately. 521034683adeSsg70180 * Any slices successfully opened before a failure will get 521134683adeSsg70180 * closed by vds_destroy_vd() as a result of the error returned 521234683adeSsg70180 * by this function. 521334683adeSsg70180 * 521434683adeSsg70180 * We need to do the open with FNDELAY so that opening an empty 521534683adeSsg70180 * slice does not fail. 52160a55fbb7Slm66018 */ 52170a55fbb7Slm66018 PR0("Opening device major %u, minor %u = slice %u", 52180a55fbb7Slm66018 major, minor, slice); 5219047ba61eSachartre 5220047ba61eSachartre /* 5221047ba61eSachartre * Try to open the device. This can fail for example if we are 5222047ba61eSachartre * opening an empty slice. So in case of a failure, we try the 5223047ba61eSachartre * open again but this time with the FNDELAY flag. 5224047ba61eSachartre */ 5225047ba61eSachartre status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK, 5226047ba61eSachartre vd->open_flags, kcred, &vd->ldi_handle[slice], 5227047ba61eSachartre vd->vds->ldi_ident); 5228047ba61eSachartre 5229047ba61eSachartre if (status != 0) { 5230047ba61eSachartre status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK, 5231047ba61eSachartre vd->open_flags | FNDELAY, kcred, 5232047ba61eSachartre &vd->ldi_handle[slice], vd->vds->ldi_ident); 5233047ba61eSachartre } 5234047ba61eSachartre 5235047ba61eSachartre if (status != 0) { 5236690555a1Sachartre PRN("ldi_open_by_dev() returned errno %d " 52370a55fbb7Slm66018 "for slice %u", status, slice); 52380a55fbb7Slm66018 /* vds_destroy_vd() will close any open slices */ 5239690555a1Sachartre vd->ldi_handle[slice] = NULL; 52400a55fbb7Slm66018 return (status); 52410a55fbb7Slm66018 } 52420a55fbb7Slm66018 } 52430a55fbb7Slm66018 52440a55fbb7Slm66018 return (0); 52450a55fbb7Slm66018 } 52460a55fbb7Slm66018 5247edcc0754Sachartre /* 5248edcc0754Sachartre * When a slice or a volume is exported as a single-slice disk, we want 5249edcc0754Sachartre * the disk backend (i.e. the slice or volume) to be entirely mapped as 5250edcc0754Sachartre * a slice without the addition of any metadata. 5251edcc0754Sachartre * 5252edcc0754Sachartre * So when exporting the disk as a VTOC disk, we fake a disk with the following 5253edcc0754Sachartre * layout: 5254*bae9e67eSachartre * flabel +--- flabel_limit 5255*bae9e67eSachartre * <-> V 5256*bae9e67eSachartre * 0 1 C D E 5257*bae9e67eSachartre * +-+---+--------------------------+--+ 5258*bae9e67eSachartre * virtual disk: |L|XXX| slice 0 |AA| 5259*bae9e67eSachartre * +-+---+--------------------------+--+ 5260edcc0754Sachartre * ^ : : 5261edcc0754Sachartre * | : : 5262edcc0754Sachartre * VTOC LABEL--+ : : 5263edcc0754Sachartre * +--------------------------+ 5264*bae9e67eSachartre * disk backend: | slice/volume/file | 5265edcc0754Sachartre * +--------------------------+ 5266edcc0754Sachartre * 0 N 5267edcc0754Sachartre * 5268*bae9e67eSachartre * N is the number of blocks in the slice/volume/file. 5269edcc0754Sachartre * 5270*bae9e67eSachartre * We simulate a disk with N+M blocks, where M is the number of blocks 5271*bae9e67eSachartre * simluated at the beginning and at the end of the disk (blocks 0-C 5272*bae9e67eSachartre * and D-E). 5273edcc0754Sachartre * 5274*bae9e67eSachartre * The first blocks (0 to C-1) are emulated and can not be changed. Blocks C 5275*bae9e67eSachartre * to D defines slice 0 and are mapped to the backend. Finally we emulate 2 5276*bae9e67eSachartre * alternate cylinders at the end of the disk (blocks D-E). In summary we have: 5277edcc0754Sachartre * 5278*bae9e67eSachartre * - block 0 (L) returns a fake VTOC label 5279*bae9e67eSachartre * - blocks 1 to C-1 (X) are unused and return 0 5280*bae9e67eSachartre * - blocks C to D-1 are mapped to the exported slice or volume 5281*bae9e67eSachartre * - blocks D and E (A) are blocks defining alternate cylinders (2 cylinders) 5282*bae9e67eSachartre * 5283*bae9e67eSachartre * Note: because we define a fake disk geometry, it is possible that the length 5284*bae9e67eSachartre * of the backend is not a multiple of the size of cylinder, in that case the 5285*bae9e67eSachartre * very end of the backend will not map to any block of the virtual disk. 5286edcc0754Sachartre */ 52870a55fbb7Slm66018 static int 528878fcd0a1Sachartre vd_setup_partition_vtoc(vd_t *vd) 528978fcd0a1Sachartre { 529078fcd0a1Sachartre char *device_path = vd->device_path; 5291*bae9e67eSachartre char unit; 5292*bae9e67eSachartre size_t size, csize; 529378fcd0a1Sachartre 529478fcd0a1Sachartre /* Initialize dk_geom structure for single-slice device */ 529578fcd0a1Sachartre if (vd->dk_geom.dkg_nsect == 0) { 529678fcd0a1Sachartre PRN("%s geometry claims 0 sectors per track", device_path); 529778fcd0a1Sachartre return (EIO); 529878fcd0a1Sachartre } 529978fcd0a1Sachartre if (vd->dk_geom.dkg_nhead == 0) { 530078fcd0a1Sachartre PRN("%s geometry claims 0 heads", device_path); 530178fcd0a1Sachartre return (EIO); 530278fcd0a1Sachartre } 5303*bae9e67eSachartre 5304*bae9e67eSachartre /* size of a cylinder in block */ 5305*bae9e67eSachartre csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect; 5306*bae9e67eSachartre 5307*bae9e67eSachartre /* 5308*bae9e67eSachartre * Add extra cylinders: we emulate the first cylinder (which contains 5309*bae9e67eSachartre * the disk label). 5310*bae9e67eSachartre */ 5311*bae9e67eSachartre vd->dk_geom.dkg_ncyl = vd->vdisk_size / csize + 1; 5312*bae9e67eSachartre 5313*bae9e67eSachartre /* we emulate 2 alternate cylinders */ 5314*bae9e67eSachartre vd->dk_geom.dkg_acyl = 2; 531578fcd0a1Sachartre vd->dk_geom.dkg_pcyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl; 531678fcd0a1Sachartre 531778fcd0a1Sachartre 531878fcd0a1Sachartre /* Initialize vtoc structure for single-slice device */ 531978fcd0a1Sachartre bzero(vd->vtoc.v_part, sizeof (vd->vtoc.v_part)); 532078fcd0a1Sachartre vd->vtoc.v_part[0].p_tag = V_UNASSIGNED; 532178fcd0a1Sachartre vd->vtoc.v_part[0].p_flag = 0; 5322*bae9e67eSachartre /* 5323*bae9e67eSachartre * Partition 0 starts on cylinder 1 and its size has to be 5324*bae9e67eSachartre * a multiple of a number of cylinder. 5325*bae9e67eSachartre */ 5326*bae9e67eSachartre vd->vtoc.v_part[0].p_start = csize; /* start on cylinder 1 */ 5327*bae9e67eSachartre vd->vtoc.v_part[0].p_size = (vd->vdisk_size / csize) * csize; 532878fcd0a1Sachartre 5329*bae9e67eSachartre if (vd_slice_single_slice) { 5330*bae9e67eSachartre vd->vtoc.v_nparts = 1; 5331*bae9e67eSachartre bcopy(VD_ASCIILABEL, vd->vtoc.v_asciilabel, 5332*bae9e67eSachartre MIN(sizeof (VD_ASCIILABEL), 5333*bae9e67eSachartre sizeof (vd->vtoc.v_asciilabel))); 5334*bae9e67eSachartre bcopy(VD_VOLUME_NAME, vd->vtoc.v_volume, 5335*bae9e67eSachartre MIN(sizeof (VD_VOLUME_NAME), sizeof (vd->vtoc.v_volume))); 5336*bae9e67eSachartre } else { 5337*bae9e67eSachartre /* adjust the number of slices */ 5338*bae9e67eSachartre vd->nslices = V_NUMPAR; 5339*bae9e67eSachartre vd->vtoc.v_nparts = V_NUMPAR; 5340*bae9e67eSachartre 5341*bae9e67eSachartre /* define slice 2 representing the entire disk */ 5342*bae9e67eSachartre vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_tag = V_BACKUP; 5343*bae9e67eSachartre vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_flag = 0; 5344*bae9e67eSachartre vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_start = 0; 5345*bae9e67eSachartre vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_size = 5346*bae9e67eSachartre vd->dk_geom.dkg_ncyl * csize; 5347*bae9e67eSachartre 5348*bae9e67eSachartre vd_get_readable_size(vd->vdisk_size * vd->vdisk_block_size, 5349*bae9e67eSachartre &size, &unit); 5350*bae9e67eSachartre 5351*bae9e67eSachartre /* 5352*bae9e67eSachartre * Set some attributes of the geometry to what format(1m) uses 5353*bae9e67eSachartre * so that writing a default label using format(1m) does not 5354*bae9e67eSachartre * produce any error. 5355*bae9e67eSachartre */ 5356*bae9e67eSachartre vd->dk_geom.dkg_bcyl = 0; 5357*bae9e67eSachartre vd->dk_geom.dkg_intrlv = 1; 5358*bae9e67eSachartre vd->dk_geom.dkg_write_reinstruct = 0; 5359*bae9e67eSachartre vd->dk_geom.dkg_read_reinstruct = 0; 5360*bae9e67eSachartre 5361*bae9e67eSachartre /* 5362*bae9e67eSachartre * We must have a correct label name otherwise format(1m) will 5363*bae9e67eSachartre * not recognized the disk as labeled. 5364*bae9e67eSachartre */ 5365*bae9e67eSachartre (void) snprintf(vd->vtoc.v_asciilabel, LEN_DKL_ASCII, 5366*bae9e67eSachartre "SUN-DiskSlice-%ld%cB cyl %d alt %d hd %d sec %d", 5367*bae9e67eSachartre size, unit, 5368*bae9e67eSachartre vd->dk_geom.dkg_ncyl, vd->dk_geom.dkg_acyl, 5369*bae9e67eSachartre vd->dk_geom.dkg_nhead, vd->dk_geom.dkg_nsect); 5370*bae9e67eSachartre bzero(vd->vtoc.v_volume, sizeof (vd->vtoc.v_volume)); 5371*bae9e67eSachartre 5372*bae9e67eSachartre /* create a fake label from the vtoc and geometry */ 5373*bae9e67eSachartre vd->flabel_limit = csize; 5374*bae9e67eSachartre vd->flabel_size = VD_LABEL_VTOC_SIZE; 5375*bae9e67eSachartre vd->flabel = kmem_zalloc(vd->flabel_size, KM_SLEEP); 5376*bae9e67eSachartre vd_vtocgeom_to_label(&vd->vtoc, &vd->dk_geom, 5377*bae9e67eSachartre VD_LABEL_VTOC(vd)); 5378*bae9e67eSachartre } 5379*bae9e67eSachartre 5380*bae9e67eSachartre /* adjust the vdisk_size, we emulate 3 cylinders */ 5381*bae9e67eSachartre vd->vdisk_size += csize * 3; 5382edcc0754Sachartre 538378fcd0a1Sachartre return (0); 538478fcd0a1Sachartre } 538578fcd0a1Sachartre 5386edcc0754Sachartre /* 5387edcc0754Sachartre * When a slice, volume or file is exported as a single-slice disk, we want 5388edcc0754Sachartre * the disk backend (i.e. the slice, volume or file) to be entirely mapped 5389edcc0754Sachartre * as a slice without the addition of any metadata. 5390edcc0754Sachartre * 5391edcc0754Sachartre * So when exporting the disk as an EFI disk, we fake a disk with the following 5392edcc0754Sachartre * layout: 5393edcc0754Sachartre * 5394*bae9e67eSachartre * flabel +--- flabel_limit 5395*bae9e67eSachartre * <------> v 5396*bae9e67eSachartre * 0 1 2 L 34 34+N P 5397*bae9e67eSachartre * +-+-+--+-------+--------------------------+-------+ 5398*bae9e67eSachartre * virtual disk: |X|T|EE|XXXXXXX| slice 0 |RRRRRRR| 5399*bae9e67eSachartre * +-+-+--+-------+--------------------------+-------+ 5400edcc0754Sachartre * ^ ^ : : 5401edcc0754Sachartre * | | : : 5402edcc0754Sachartre * GPT-+ +-GPE : : 5403edcc0754Sachartre * +--------------------------+ 5404edcc0754Sachartre * disk backend: | slice/volume/file | 5405edcc0754Sachartre * +--------------------------+ 5406edcc0754Sachartre * 0 N 5407edcc0754Sachartre * 5408edcc0754Sachartre * N is the number of blocks in the slice/volume/file. 5409edcc0754Sachartre * 5410*bae9e67eSachartre * We simulate a disk with N+M blocks, where M is the number of blocks 5411*bae9e67eSachartre * simluated at the beginning and at the end of the disk (blocks 0-34 5412*bae9e67eSachartre * and 34+N-P). 5413edcc0754Sachartre * 5414*bae9e67eSachartre * The first 34 blocks (0 to 33) are emulated and can not be changed. Blocks 34 5415*bae9e67eSachartre * to 34+N defines slice 0 and are mapped to the exported backend, and we 5416*bae9e67eSachartre * emulate some blocks at the end of the disk (blocks 34+N to P) as a the EFI 5417*bae9e67eSachartre * reserved partition. 5418*bae9e67eSachartre * 5419*bae9e67eSachartre * - block 0 (X) is unused and return 0 5420edcc0754Sachartre * - block 1 (T) returns a fake EFI GPT (via DKIOCGETEFI) 5421*bae9e67eSachartre * - blocks 2 to L-1 (E) defines a fake EFI GPE (via DKIOCGETEFI) 5422*bae9e67eSachartre * - blocks L to 33 (X) are unused and return 0 5423*bae9e67eSachartre * - blocks 34 to 34+N are mapped to the exported slice, volume or file 5424*bae9e67eSachartre * - blocks 34+N+1 to P define a fake reserved partition and backup label, it 5425*bae9e67eSachartre * returns 0 5426edcc0754Sachartre * 5427*bae9e67eSachartre * Note: if the backend size is not a multiple of the vdisk block size 5428*bae9e67eSachartre * (DEV_BSIZE = 512 byte) then the very end of the backend will not map to 5429*bae9e67eSachartre * any block of the virtual disk. 5430edcc0754Sachartre */ 543178fcd0a1Sachartre static int 54324bac2208Snarayan vd_setup_partition_efi(vd_t *vd) 54334bac2208Snarayan { 54344bac2208Snarayan efi_gpt_t *gpt; 54354bac2208Snarayan efi_gpe_t *gpe; 5436edcc0754Sachartre struct uuid uuid = EFI_USR; 5437*bae9e67eSachartre struct uuid efi_reserved = EFI_RESERVED; 54384bac2208Snarayan uint32_t crc; 5439*bae9e67eSachartre uint64_t s0_start, s0_end; 54404bac2208Snarayan 5441*bae9e67eSachartre vd->flabel_limit = 34; 5442*bae9e67eSachartre vd->flabel_size = VD_LABEL_EFI_SIZE; 5443*bae9e67eSachartre vd->flabel = kmem_zalloc(vd->flabel_size, KM_SLEEP); 5444*bae9e67eSachartre gpt = VD_LABEL_EFI_GPT(vd); 5445*bae9e67eSachartre gpe = VD_LABEL_EFI_GPE(vd); 5446edcc0754Sachartre 5447edcc0754Sachartre /* adjust the vdisk_size, we emulate the first 34 blocks */ 5448edcc0754Sachartre vd->vdisk_size += 34; 5449*bae9e67eSachartre s0_start = 34; 5450*bae9e67eSachartre s0_end = vd->vdisk_size - 1; 54514bac2208Snarayan 54524bac2208Snarayan gpt->efi_gpt_Signature = LE_64(EFI_SIGNATURE); 54534bac2208Snarayan gpt->efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 54544bac2208Snarayan gpt->efi_gpt_HeaderSize = LE_32(sizeof (efi_gpt_t)); 5455edcc0754Sachartre gpt->efi_gpt_FirstUsableLBA = LE_64(34ULL); 5456edcc0754Sachartre gpt->efi_gpt_PartitionEntryLBA = LE_64(2ULL); 54574bac2208Snarayan gpt->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (efi_gpe_t)); 54584bac2208Snarayan 5459*bae9e67eSachartre UUID_LE_CONVERT(gpe[0].efi_gpe_PartitionTypeGUID, uuid); 5460*bae9e67eSachartre gpe[0].efi_gpe_StartingLBA = LE_64(s0_start); 5461*bae9e67eSachartre gpe[0].efi_gpe_EndingLBA = LE_64(s0_end); 54624bac2208Snarayan 5463*bae9e67eSachartre if (vd_slice_single_slice) { 5464*bae9e67eSachartre gpt->efi_gpt_NumberOfPartitionEntries = LE_32(1); 5465*bae9e67eSachartre } else { 5466*bae9e67eSachartre /* adjust the number of slices */ 5467*bae9e67eSachartre gpt->efi_gpt_NumberOfPartitionEntries = LE_32(VD_MAXPART); 5468*bae9e67eSachartre vd->nslices = V_NUMPAR; 5469*bae9e67eSachartre 5470*bae9e67eSachartre /* define a fake reserved partition */ 5471*bae9e67eSachartre UUID_LE_CONVERT(gpe[VD_MAXPART - 1].efi_gpe_PartitionTypeGUID, 5472*bae9e67eSachartre efi_reserved); 5473*bae9e67eSachartre gpe[VD_MAXPART - 1].efi_gpe_StartingLBA = 5474*bae9e67eSachartre LE_64(s0_end + 1); 5475*bae9e67eSachartre gpe[VD_MAXPART - 1].efi_gpe_EndingLBA = 5476*bae9e67eSachartre LE_64(s0_end + EFI_MIN_RESV_SIZE); 5477*bae9e67eSachartre 5478*bae9e67eSachartre /* adjust the vdisk_size to include the reserved slice */ 5479*bae9e67eSachartre vd->vdisk_size += EFI_MIN_RESV_SIZE; 5480*bae9e67eSachartre } 5481*bae9e67eSachartre 5482*bae9e67eSachartre gpt->efi_gpt_LastUsableLBA = LE_64(vd->vdisk_size - 1); 5483*bae9e67eSachartre 5484*bae9e67eSachartre /* adjust the vdisk size for the backup GPT and GPE */ 5485*bae9e67eSachartre vd->vdisk_size += 33; 5486*bae9e67eSachartre 5487*bae9e67eSachartre CRC32(crc, gpe, sizeof (efi_gpe_t) * VD_MAXPART, -1U, crc32_table); 54884bac2208Snarayan gpt->efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 54894bac2208Snarayan 54904bac2208Snarayan CRC32(crc, gpt, sizeof (efi_gpt_t), -1U, crc32_table); 54914bac2208Snarayan gpt->efi_gpt_HeaderCRC32 = LE_32(~crc); 54924bac2208Snarayan 54934bac2208Snarayan return (0); 54944bac2208Snarayan } 54954bac2208Snarayan 5496047ba61eSachartre /* 5497047ba61eSachartre * Setup for a virtual disk whose backend is a file (exported as a single slice 54988fce2fd6Sachartre * or as a full disk) or a volume device (for example a ZFS, SVM or VxVM volume) 5499047ba61eSachartre * exported as a full disk. In these cases, the backend is accessed using the 5500047ba61eSachartre * vnode interface. 5501047ba61eSachartre */ 55024bac2208Snarayan static int 5503047ba61eSachartre vd_setup_backend_vnode(vd_t *vd) 55043c96341aSnarayan { 550578fcd0a1Sachartre int rval, status; 55063c96341aSnarayan vattr_t vattr; 55073c96341aSnarayan dev_t dev; 55083c96341aSnarayan char *file_path = vd->device_path; 55093c96341aSnarayan char dev_path[MAXPATHLEN + 1]; 55103c96341aSnarayan ldi_handle_t lhandle; 55113c96341aSnarayan struct dk_cinfo dk_cinfo; 5512*bae9e67eSachartre struct dk_label label; 55133c96341aSnarayan 5514047ba61eSachartre if ((status = vn_open(file_path, UIO_SYSSPACE, vd->open_flags | FOFFMAX, 55153c96341aSnarayan 0, &vd->file_vnode, 0, 0)) != 0) { 5516690555a1Sachartre PRN("vn_open(%s) = errno %d", file_path, status); 55173c96341aSnarayan return (status); 55183c96341aSnarayan } 55193c96341aSnarayan 5520690555a1Sachartre /* 5521690555a1Sachartre * We set vd->file now so that vds_destroy_vd will take care of 5522690555a1Sachartre * closing the file and releasing the vnode in case of an error. 5523690555a1Sachartre */ 5524690555a1Sachartre vd->file = B_TRUE; 5525690555a1Sachartre 55263c96341aSnarayan vattr.va_mask = AT_SIZE; 5527da6c28aaSamw if ((status = VOP_GETATTR(vd->file_vnode, &vattr, 0, kcred, NULL)) 5528da6c28aaSamw != 0) { 5529690555a1Sachartre PRN("VOP_GETATTR(%s) = errno %d", file_path, status); 55303c96341aSnarayan return (EIO); 55313c96341aSnarayan } 55323c96341aSnarayan 55333c96341aSnarayan vd->file_size = vattr.va_size; 55343c96341aSnarayan /* size should be at least sizeof(dk_label) */ 55353c96341aSnarayan if (vd->file_size < sizeof (struct dk_label)) { 55363c96341aSnarayan PRN("Size of file has to be at least %ld bytes", 55373c96341aSnarayan sizeof (struct dk_label)); 55383c96341aSnarayan return (EIO); 55393c96341aSnarayan } 55403c96341aSnarayan 5541690555a1Sachartre if (vd->file_vnode->v_flag & VNOMAP) { 5542690555a1Sachartre PRN("File %s cannot be mapped", file_path); 55433c96341aSnarayan return (EIO); 55443c96341aSnarayan } 55453c96341aSnarayan 55463c96341aSnarayan /* sector size = block size = DEV_BSIZE */ 554717cadca8Slm66018 vd->block_size = DEV_BSIZE; 554817cadca8Slm66018 vd->vdisk_block_size = DEV_BSIZE; 554987a7269eSachartre vd->vdisk_size = vd->file_size / DEV_BSIZE; 55503c96341aSnarayan vd->max_xfer_sz = maxphys / DEV_BSIZE; /* default transfer size */ 55513c96341aSnarayan 5552047ba61eSachartre /* 5553047ba61eSachartre * Get max_xfer_sz from the device where the file is or from the device 55548fce2fd6Sachartre * itself if we have a volume device. 5555047ba61eSachartre */ 5556047ba61eSachartre dev_path[0] = '\0'; 5557047ba61eSachartre 55588fce2fd6Sachartre if (vd->volume) { 5559047ba61eSachartre status = ldi_open_by_name(file_path, FREAD, kcred, &lhandle, 5560047ba61eSachartre vd->vds->ldi_ident); 5561047ba61eSachartre } else { 55623c96341aSnarayan dev = vd->file_vnode->v_vfsp->vfs_dev; 55633c96341aSnarayan if (ddi_dev_pathname(dev, S_IFBLK, dev_path) == DDI_SUCCESS) { 55643c96341aSnarayan PR0("underlying device = %s\n", dev_path); 55653c96341aSnarayan } 55663c96341aSnarayan 5567047ba61eSachartre status = ldi_open_by_dev(&dev, OTYP_BLK, FREAD, kcred, &lhandle, 5568047ba61eSachartre vd->vds->ldi_ident); 5569047ba61eSachartre } 5570047ba61eSachartre 5571047ba61eSachartre if (status != 0) { 5572047ba61eSachartre PR0("ldi_open() returned errno %d for device %s", 5573047ba61eSachartre status, (dev_path[0] == '\0')? file_path : dev_path); 55743c96341aSnarayan } else { 55753c96341aSnarayan if ((status = ldi_ioctl(lhandle, DKIOCINFO, 5576047ba61eSachartre (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred, 55773c96341aSnarayan &rval)) != 0) { 55783c96341aSnarayan PR0("ldi_ioctl(DKIOCINFO) returned errno %d for %s", 55793c96341aSnarayan status, dev_path); 55803c96341aSnarayan } else { 55813c96341aSnarayan /* 55823c96341aSnarayan * Store the device's max transfer size for 55833c96341aSnarayan * return to the client 55843c96341aSnarayan */ 55853c96341aSnarayan vd->max_xfer_sz = dk_cinfo.dki_maxtransfer; 55863c96341aSnarayan } 55873c96341aSnarayan 55883c96341aSnarayan PR0("close the device %s", dev_path); 55893c96341aSnarayan (void) ldi_close(lhandle, FREAD, kcred); 55903c96341aSnarayan } 55913c96341aSnarayan 5592205eeb1aSlm66018 PR0("using file %s, dev %s, max_xfer = %u blks", 55933c96341aSnarayan file_path, dev_path, vd->max_xfer_sz); 55943c96341aSnarayan 5595edcc0754Sachartre if (vd->vdisk_type == VD_DISK_TYPE_SLICE) { 55968fce2fd6Sachartre ASSERT(!vd->volume); 5597*bae9e67eSachartre vd->vdisk_media = VD_MEDIA_FIXED; 5598*bae9e67eSachartre vd->vdisk_label = (vd_slice_label == VD_DISK_LABEL_UNK)? 5599*bae9e67eSachartre vd_file_slice_label : vd_slice_label; 5600*bae9e67eSachartre if (vd->vdisk_label == VD_DISK_LABEL_EFI || 5601*bae9e67eSachartre vd->file_size >= ONE_TERABYTE) { 5602edcc0754Sachartre status = vd_setup_partition_efi(vd); 5603*bae9e67eSachartre } else { 5604*bae9e67eSachartre /* 5605*bae9e67eSachartre * We build a default label to get a geometry for 5606*bae9e67eSachartre * the vdisk. Then the partition setup function will 5607*bae9e67eSachartre * adjust the vtoc so that it defines a single-slice 5608*bae9e67eSachartre * disk. 5609*bae9e67eSachartre */ 5610*bae9e67eSachartre vd_build_default_label(vd->file_size, &label); 5611*bae9e67eSachartre vd_label_to_vtocgeom(&label, &vd->vtoc, &vd->dk_geom); 5612*bae9e67eSachartre status = vd_setup_partition_vtoc(vd); 5613*bae9e67eSachartre } 5614*bae9e67eSachartre return (status); 5615edcc0754Sachartre } 5616edcc0754Sachartre 5617edcc0754Sachartre /* 5618edcc0754Sachartre * Find and validate the geometry of a disk image. 5619edcc0754Sachartre */ 5620edcc0754Sachartre status = vd_file_validate_geometry(vd); 5621edcc0754Sachartre if (status != 0 && status != EINVAL && status != ENOTSUP) { 5622edcc0754Sachartre PRN("Failed to read label from %s", file_path); 5623edcc0754Sachartre return (EIO); 5624edcc0754Sachartre } 5625edcc0754Sachartre 5626edcc0754Sachartre if (vd_file_is_iso_image(vd)) { 5627edcc0754Sachartre /* 5628edcc0754Sachartre * Indicate whether to call this a CD or DVD from the size 5629edcc0754Sachartre * of the ISO image (images for both drive types are stored 5630edcc0754Sachartre * in the ISO-9600 format). CDs can store up to just under 1Gb 5631edcc0754Sachartre */ 5632edcc0754Sachartre if ((vd->vdisk_size * vd->vdisk_block_size) > 5633edcc0754Sachartre (1024 * 1024 * 1024)) 5634edcc0754Sachartre vd->vdisk_media = VD_MEDIA_DVD; 5635edcc0754Sachartre else 5636edcc0754Sachartre vd->vdisk_media = VD_MEDIA_CD; 5637edcc0754Sachartre } else { 5638edcc0754Sachartre vd->vdisk_media = VD_MEDIA_FIXED; 5639edcc0754Sachartre } 5640edcc0754Sachartre 5641edcc0754Sachartre /* Setup devid for the disk image */ 5642047ba61eSachartre 564378fcd0a1Sachartre if (vd->vdisk_label != VD_DISK_LABEL_UNK) { 564478fcd0a1Sachartre 564587a7269eSachartre status = vd_file_read_devid(vd, &vd->file_devid); 564687a7269eSachartre 564787a7269eSachartre if (status == 0) { 564887a7269eSachartre /* a valid devid was found */ 564987a7269eSachartre return (0); 565087a7269eSachartre } 565187a7269eSachartre 565287a7269eSachartre if (status != EINVAL) { 565387a7269eSachartre /* 565478fcd0a1Sachartre * There was an error while trying to read the devid. 565578fcd0a1Sachartre * So this disk image may have a devid but we are 565678fcd0a1Sachartre * unable to read it. 565787a7269eSachartre */ 565887a7269eSachartre PR0("can not read devid for %s", file_path); 565987a7269eSachartre vd->file_devid = NULL; 566087a7269eSachartre return (0); 566187a7269eSachartre } 566278fcd0a1Sachartre } 566387a7269eSachartre 566487a7269eSachartre /* 566587a7269eSachartre * No valid device id was found so we create one. Note that a failure 566687a7269eSachartre * to create a device id is not fatal and does not prevent the disk 566787a7269eSachartre * image from being attached. 566887a7269eSachartre */ 566987a7269eSachartre PR1("creating devid for %s", file_path); 567087a7269eSachartre 567187a7269eSachartre if (ddi_devid_init(vd->vds->dip, DEVID_FAB, NULL, 0, 567287a7269eSachartre &vd->file_devid) != DDI_SUCCESS) { 567387a7269eSachartre PR0("fail to create devid for %s", file_path); 567487a7269eSachartre vd->file_devid = NULL; 567587a7269eSachartre return (0); 567687a7269eSachartre } 567787a7269eSachartre 567878fcd0a1Sachartre /* 567978fcd0a1Sachartre * Write devid to the disk image. The devid is stored into the disk 568078fcd0a1Sachartre * image if we have a valid label; otherwise the devid will be stored 568178fcd0a1Sachartre * when the user writes a valid label. 568278fcd0a1Sachartre */ 568378fcd0a1Sachartre if (vd->vdisk_label != VD_DISK_LABEL_UNK) { 568487a7269eSachartre if (vd_file_write_devid(vd, vd->file_devid) != 0) { 568587a7269eSachartre PR0("fail to write devid for %s", file_path); 568687a7269eSachartre ddi_devid_free(vd->file_devid); 568787a7269eSachartre vd->file_devid = NULL; 568887a7269eSachartre } 568978fcd0a1Sachartre } 569087a7269eSachartre 56913c96341aSnarayan return (0); 56923c96341aSnarayan } 56933c96341aSnarayan 569417cadca8Slm66018 569517cadca8Slm66018 /* 569617cadca8Slm66018 * Description: 569717cadca8Slm66018 * Open a device using its device path (supplied by ldm(1m)) 569817cadca8Slm66018 * 569917cadca8Slm66018 * Parameters: 570017cadca8Slm66018 * vd - pointer to structure containing the vDisk info 57018fce2fd6Sachartre * flags - open flags 570217cadca8Slm66018 * 570317cadca8Slm66018 * Return Value 570417cadca8Slm66018 * 0 - success 570517cadca8Slm66018 * != 0 - some other non-zero return value from ldi(9F) functions 570617cadca8Slm66018 */ 570717cadca8Slm66018 static int 57088fce2fd6Sachartre vd_open_using_ldi_by_name(vd_t *vd, int flags) 570917cadca8Slm66018 { 57108fce2fd6Sachartre int status; 571117cadca8Slm66018 char *device_path = vd->device_path; 571217cadca8Slm66018 57138fce2fd6Sachartre /* Attempt to open device */ 57148fce2fd6Sachartre status = ldi_open_by_name(device_path, flags, kcred, 571517cadca8Slm66018 &vd->ldi_handle[0], vd->vds->ldi_ident); 571617cadca8Slm66018 571717cadca8Slm66018 /* 571817cadca8Slm66018 * The open can fail for example if we are opening an empty slice. 571917cadca8Slm66018 * In case of a failure, we try the open again but this time with 572017cadca8Slm66018 * the FNDELAY flag. 572117cadca8Slm66018 */ 572217cadca8Slm66018 if (status != 0) 57238fce2fd6Sachartre status = ldi_open_by_name(device_path, flags | FNDELAY, 572417cadca8Slm66018 kcred, &vd->ldi_handle[0], vd->vds->ldi_ident); 572517cadca8Slm66018 572617cadca8Slm66018 if (status != 0) { 572717cadca8Slm66018 PR0("ldi_open_by_name(%s) = errno %d", device_path, status); 572817cadca8Slm66018 vd->ldi_handle[0] = NULL; 572917cadca8Slm66018 return (status); 573017cadca8Slm66018 } 573117cadca8Slm66018 573217cadca8Slm66018 return (0); 573317cadca8Slm66018 } 573417cadca8Slm66018 5735047ba61eSachartre /* 5736047ba61eSachartre * Setup for a virtual disk which backend is a device (a physical disk, 57378fce2fd6Sachartre * slice or volume device) that is directly exported either as a full disk 57388fce2fd6Sachartre * for a physical disk or as a slice for a volume device or a disk slice. 5739047ba61eSachartre * In these cases, the backend is accessed using the LDI interface. 5740047ba61eSachartre */ 57413c96341aSnarayan static int 5742047ba61eSachartre vd_setup_backend_ldi(vd_t *vd) 57431ae08745Sheppo { 5744e1ebb9ecSlm66018 int rval, status; 57451ae08745Sheppo struct dk_cinfo dk_cinfo; 57463c96341aSnarayan char *device_path = vd->device_path; 57471ae08745Sheppo 57488fce2fd6Sachartre /* device has been opened by vd_identify_dev() */ 57498fce2fd6Sachartre ASSERT(vd->ldi_handle[0] != NULL); 57508fce2fd6Sachartre ASSERT(vd->dev[0] != NULL); 57510a55fbb7Slm66018 57523c96341aSnarayan vd->file = B_FALSE; 57534bac2208Snarayan 575478fcd0a1Sachartre /* Verify backing device supports dk_cinfo */ 5755e1ebb9ecSlm66018 if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO, 5756047ba61eSachartre (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred, 5757e1ebb9ecSlm66018 &rval)) != 0) { 5758e1ebb9ecSlm66018 PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s", 5759e1ebb9ecSlm66018 status, device_path); 5760e1ebb9ecSlm66018 return (status); 5761e1ebb9ecSlm66018 } 5762e1ebb9ecSlm66018 if (dk_cinfo.dki_partition >= V_NUMPAR) { 5763e1ebb9ecSlm66018 PRN("slice %u >= maximum slice %u for %s", 5764e1ebb9ecSlm66018 dk_cinfo.dki_partition, V_NUMPAR, device_path); 5765e1ebb9ecSlm66018 return (EIO); 5766e1ebb9ecSlm66018 } 57674bac2208Snarayan 57688fce2fd6Sachartre /* 57698fce2fd6Sachartre * The device has been opened read-only by vd_identify_dev(), re-open 57708fce2fd6Sachartre * it read-write if the write flag is set and we don't have an optical 57718fce2fd6Sachartre * device such as a CD-ROM, which, for now, we do not permit writes to 57728fce2fd6Sachartre * and thus should not export write operations to the client. 57738fce2fd6Sachartre * 57748fce2fd6Sachartre * Future: if/when we implement support for guest domains writing to 57758fce2fd6Sachartre * optical devices we will need to do further checking of the media type 57768fce2fd6Sachartre * to distinguish between read-only and writable discs. 57778fce2fd6Sachartre */ 57788fce2fd6Sachartre if (dk_cinfo.dki_ctype == DKC_CDROM) { 57798fce2fd6Sachartre 57808fce2fd6Sachartre vd->open_flags &= ~FWRITE; 57818fce2fd6Sachartre 57828fce2fd6Sachartre } else if (vd->open_flags & FWRITE) { 57838fce2fd6Sachartre 57848fce2fd6Sachartre (void) ldi_close(vd->ldi_handle[0], vd->open_flags & ~FWRITE, 57858fce2fd6Sachartre kcred); 57868fce2fd6Sachartre status = vd_open_using_ldi_by_name(vd, vd->open_flags); 57878fce2fd6Sachartre if (status != 0) { 57888fce2fd6Sachartre PR0("Failed to open (%s) = errno %d", 57898fce2fd6Sachartre device_path, status); 57908fce2fd6Sachartre return (status); 57918fce2fd6Sachartre } 57928fce2fd6Sachartre } 57938fce2fd6Sachartre 5794e1ebb9ecSlm66018 /* Store the device's max transfer size for return to the client */ 5795e1ebb9ecSlm66018 vd->max_xfer_sz = dk_cinfo.dki_maxtransfer; 5796e1ebb9ecSlm66018 5797047ba61eSachartre /* 579817cadca8Slm66018 * We need to work out if it's an ATAPI (IDE CD-ROM) or SCSI device so 579917cadca8Slm66018 * that we can use the correct CDB group when sending USCSI commands. 580017cadca8Slm66018 */ 580117cadca8Slm66018 vd->is_atapi_dev = vd_is_atapi_device(vd); 580217cadca8Slm66018 580317cadca8Slm66018 /* 5804047ba61eSachartre * Export a full disk. 5805047ba61eSachartre * 5806047ba61eSachartre * When we use the LDI interface, we export a device as a full disk 5807047ba61eSachartre * if we have an entire disk slice (slice 2) and if this slice is 5808047ba61eSachartre * exported as a full disk and not as a single slice disk. 580917cadca8Slm66018 * Similarly, we want to use LDI if we are accessing a CD or DVD 581017cadca8Slm66018 * device (even if it isn't s2) 5811047ba61eSachartre * 58128fce2fd6Sachartre * Note that volume devices are exported as full disks using the vnode 5813047ba61eSachartre * interface, not the LDI interface. 5814047ba61eSachartre */ 581517cadca8Slm66018 if ((dk_cinfo.dki_partition == VD_ENTIRE_DISK_SLICE && 581617cadca8Slm66018 vd->vdisk_type == VD_DISK_TYPE_DISK) || 581717cadca8Slm66018 dk_cinfo.dki_ctype == DKC_CDROM) { 58188fce2fd6Sachartre ASSERT(!vd->volume); 58192f5224aeSachartre if (dk_cinfo.dki_ctype == DKC_SCSI_CCS) 58202f5224aeSachartre vd->scsi = B_TRUE; 5821047ba61eSachartre return (vd_setup_full_disk(vd)); 5822047ba61eSachartre } 5823047ba61eSachartre 5824047ba61eSachartre /* 5825047ba61eSachartre * Export a single slice disk. 5826047ba61eSachartre * 58278fce2fd6Sachartre * The exported device can be either a volume device or a disk slice. If 5828047ba61eSachartre * it is a disk slice different from slice 2 then it is always exported 5829047ba61eSachartre * as a single slice disk even if the "slice" option is not specified. 58308fce2fd6Sachartre * If it is disk slice 2 or a volume device then it is exported as a 5831047ba61eSachartre * single slice disk only if the "slice" option is specified. 5832047ba61eSachartre */ 5833047ba61eSachartre return (vd_setup_single_slice_disk(vd)); 5834047ba61eSachartre } 5835047ba61eSachartre 5836047ba61eSachartre static int 5837047ba61eSachartre vd_setup_single_slice_disk(vd_t *vd) 5838047ba61eSachartre { 5839edcc0754Sachartre int status, rval; 5840*bae9e67eSachartre struct dk_label label; 5841047ba61eSachartre char *device_path = vd->device_path; 5842047ba61eSachartre 5843047ba61eSachartre /* Get size of backing device */ 5844047ba61eSachartre if (ldi_get_size(vd->ldi_handle[0], &vd->vdisk_size) != DDI_SUCCESS) { 5845047ba61eSachartre PRN("ldi_get_size() failed for %s", device_path); 58461ae08745Sheppo return (EIO); 58471ae08745Sheppo } 5848047ba61eSachartre vd->vdisk_size = lbtodb(vd->vdisk_size); /* convert to blocks */ 584917cadca8Slm66018 vd->block_size = DEV_BSIZE; 585017cadca8Slm66018 vd->vdisk_block_size = DEV_BSIZE; 585117cadca8Slm66018 vd->vdisk_media = VD_MEDIA_FIXED; 5852047ba61eSachartre 58538fce2fd6Sachartre if (vd->volume) { 5854047ba61eSachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 585578fcd0a1Sachartre } 58560a55fbb7Slm66018 5857047ba61eSachartre /* 5858047ba61eSachartre * We export the slice as a single slice disk even if the "slice" 5859047ba61eSachartre * option was not specified. 5860047ba61eSachartre */ 58611ae08745Sheppo vd->vdisk_type = VD_DISK_TYPE_SLICE; 58621ae08745Sheppo vd->nslices = 1; 58631ae08745Sheppo 5864edcc0754Sachartre /* 5865edcc0754Sachartre * When exporting a slice or a device as a single slice disk, we don't 5866edcc0754Sachartre * care about any partitioning exposed by the backend. The goal is just 5867edcc0754Sachartre * to export the backend as a flat storage. We provide a fake partition 5868edcc0754Sachartre * table (either a VTOC or EFI), which presents only one slice, to 5869*bae9e67eSachartre * accommodate tools expecting a disk label. The selection of the label 5870*bae9e67eSachartre * type (VTOC or EFI) depends on the value of the vd_slice_label 5871*bae9e67eSachartre * variable. 5872edcc0754Sachartre */ 5873*bae9e67eSachartre if (vd_slice_label == VD_DISK_LABEL_EFI || 5874*bae9e67eSachartre vd->vdisk_size >= ONE_TERABYTE / DEV_BSIZE) { 5875*bae9e67eSachartre vd->vdisk_label = VD_DISK_LABEL_EFI; 5876*bae9e67eSachartre } else { 5877*bae9e67eSachartre status = ldi_ioctl(vd->ldi_handle[0], DKIOCGVTOC, 5878*bae9e67eSachartre (intptr_t)&vd->vtoc, (vd->open_flags | FKIOCTL), 5879*bae9e67eSachartre kcred, &rval); 5880edcc0754Sachartre 5881edcc0754Sachartre if (status == 0) { 5882*bae9e67eSachartre status = ldi_ioctl(vd->ldi_handle[0], DKIOCGGEOM, 5883*bae9e67eSachartre (intptr_t)&vd->dk_geom, (vd->open_flags | FKIOCTL), 5884*bae9e67eSachartre kcred, &rval); 5885*bae9e67eSachartre 5886*bae9e67eSachartre if (status != 0) { 5887*bae9e67eSachartre PRN("ldi_ioctl(DKIOCGEOM) returned errno %d " 5888*bae9e67eSachartre "for %s", status, device_path); 5889*bae9e67eSachartre return (status); 5890*bae9e67eSachartre } 5891edcc0754Sachartre vd->vdisk_label = VD_DISK_LABEL_VTOC; 5892*bae9e67eSachartre 5893*bae9e67eSachartre } else if (vd_slice_label == VD_DISK_LABEL_VTOC) { 5894*bae9e67eSachartre 5895*bae9e67eSachartre vd->vdisk_label = VD_DISK_LABEL_VTOC; 5896*bae9e67eSachartre vd_build_default_label(vd->vdisk_size * DEV_BSIZE, 5897*bae9e67eSachartre &label); 5898*bae9e67eSachartre vd_label_to_vtocgeom(&label, &vd->vtoc, &vd->dk_geom); 5899*bae9e67eSachartre 5900*bae9e67eSachartre } else { 5901*bae9e67eSachartre vd->vdisk_label = VD_DISK_LABEL_EFI; 5902*bae9e67eSachartre } 5903*bae9e67eSachartre } 5904*bae9e67eSachartre 5905*bae9e67eSachartre if (vd->vdisk_label == VD_DISK_LABEL_VTOC) { 5906*bae9e67eSachartre /* export with a fake VTOC label */ 590778fcd0a1Sachartre status = vd_setup_partition_vtoc(vd); 5908*bae9e67eSachartre 5909edcc0754Sachartre } else { 5910edcc0754Sachartre /* export with a fake EFI label */ 5911edcc0754Sachartre status = vd_setup_partition_efi(vd); 591278fcd0a1Sachartre } 591378fcd0a1Sachartre 59144bac2208Snarayan return (status); 59154bac2208Snarayan } 59161ae08745Sheppo 59178fce2fd6Sachartre /* 59188fce2fd6Sachartre * Description: 59198fce2fd6Sachartre * Open a device using its device path and identify if this is 59208fce2fd6Sachartre * a disk device or a volume device. 59218fce2fd6Sachartre * 59228fce2fd6Sachartre * Parameters: 59238fce2fd6Sachartre * vd - pointer to structure containing the vDisk info 59248fce2fd6Sachartre * dtype - return the driver type of the device 59258fce2fd6Sachartre * 59268fce2fd6Sachartre * Return Value 59278fce2fd6Sachartre * 0 - success 59288fce2fd6Sachartre * != 0 - some other non-zero return value from ldi(9F) functions 59298fce2fd6Sachartre */ 59308fce2fd6Sachartre static int 59318fce2fd6Sachartre vd_identify_dev(vd_t *vd, int *dtype) 59328fce2fd6Sachartre { 59338fce2fd6Sachartre int status, i; 59348fce2fd6Sachartre char *device_path = vd->device_path; 59358fce2fd6Sachartre char *drv_name; 59368fce2fd6Sachartre int drv_type; 59378fce2fd6Sachartre vds_t *vds = vd->vds; 59388fce2fd6Sachartre 59398fce2fd6Sachartre status = vd_open_using_ldi_by_name(vd, vd->open_flags & ~FWRITE); 59408fce2fd6Sachartre if (status != 0) { 59418fce2fd6Sachartre PR0("Failed to open (%s) = errno %d", device_path, status); 59428fce2fd6Sachartre return (status); 59438fce2fd6Sachartre } 59448fce2fd6Sachartre 59458fce2fd6Sachartre /* Get device number of backing device */ 59468fce2fd6Sachartre if ((status = ldi_get_dev(vd->ldi_handle[0], &vd->dev[0])) != 0) { 59478fce2fd6Sachartre PRN("ldi_get_dev() returned errno %d for %s", 59488fce2fd6Sachartre status, device_path); 59498fce2fd6Sachartre return (status); 59508fce2fd6Sachartre } 59518fce2fd6Sachartre 59528fce2fd6Sachartre /* 59538fce2fd6Sachartre * We start by looking if the driver is in the list from vds.conf 59548fce2fd6Sachartre * so that we can override the built-in list using vds.conf. 59558fce2fd6Sachartre */ 59568fce2fd6Sachartre drv_name = ddi_major_to_name(getmajor(vd->dev[0])); 59578fce2fd6Sachartre drv_type = VD_DRIVER_UNKNOWN; 59588fce2fd6Sachartre 59598fce2fd6Sachartre /* check vds.conf list */ 59608fce2fd6Sachartre for (i = 0; i < vds->num_drivers; i++) { 59618fce2fd6Sachartre if (vds->driver_types[i].type == VD_DRIVER_UNKNOWN) { 59628fce2fd6Sachartre /* ignore invalid entries */ 59638fce2fd6Sachartre continue; 59648fce2fd6Sachartre } 59658fce2fd6Sachartre if (strcmp(drv_name, vds->driver_types[i].name) == 0) { 59668fce2fd6Sachartre drv_type = vds->driver_types[i].type; 59678fce2fd6Sachartre goto done; 59688fce2fd6Sachartre } 59698fce2fd6Sachartre } 59708fce2fd6Sachartre 59718fce2fd6Sachartre /* check built-in list */ 59728fce2fd6Sachartre for (i = 0; i < VDS_NUM_DRIVERS; i++) { 59738fce2fd6Sachartre if (strcmp(drv_name, vds_driver_types[i].name) == 0) { 59748fce2fd6Sachartre drv_type = vds_driver_types[i].type; 59758fce2fd6Sachartre goto done; 59768fce2fd6Sachartre } 59778fce2fd6Sachartre } 59788fce2fd6Sachartre 59798fce2fd6Sachartre done: 59808fce2fd6Sachartre PR0("driver %s identified as %s", drv_name, 59818fce2fd6Sachartre (drv_type == VD_DRIVER_DISK)? "DISK" : 59828fce2fd6Sachartre (drv_type == VD_DRIVER_VOLUME)? "VOLUME" : "UNKNOWN"); 59838fce2fd6Sachartre 59848fce2fd6Sachartre *dtype = drv_type; 59858fce2fd6Sachartre 59868fce2fd6Sachartre return (0); 59878fce2fd6Sachartre } 59888fce2fd6Sachartre 59891ae08745Sheppo static int 5990047ba61eSachartre vd_setup_vd(vd_t *vd) 5991047ba61eSachartre { 59928fce2fd6Sachartre int status, drv_type, pseudo; 5993047ba61eSachartre dev_info_t *dip; 5994047ba61eSachartre vnode_t *vnp; 5995047ba61eSachartre char *path = vd->device_path; 5996047ba61eSachartre 5997047ba61eSachartre /* make sure the vdisk backend is valid */ 5998047ba61eSachartre if ((status = lookupname(path, UIO_SYSSPACE, 5999047ba61eSachartre FOLLOW, NULLVPP, &vnp)) != 0) { 6000047ba61eSachartre PR0("Cannot lookup %s errno %d", path, status); 6001047ba61eSachartre goto done; 6002047ba61eSachartre } 6003047ba61eSachartre 6004047ba61eSachartre switch (vnp->v_type) { 6005047ba61eSachartre case VREG: 6006047ba61eSachartre /* 6007047ba61eSachartre * Backend is a file so it is exported as a full disk or as a 6008047ba61eSachartre * single slice disk using the vnode interface. 6009047ba61eSachartre */ 6010047ba61eSachartre VN_RELE(vnp); 60118fce2fd6Sachartre vd->volume = B_FALSE; 6012047ba61eSachartre status = vd_setup_backend_vnode(vd); 6013047ba61eSachartre break; 6014047ba61eSachartre 6015047ba61eSachartre case VBLK: 6016047ba61eSachartre case VCHR: 6017047ba61eSachartre /* 6018047ba61eSachartre * Backend is a device. The way it is exported depends on the 6019047ba61eSachartre * type of the device. 6020047ba61eSachartre * 60218fce2fd6Sachartre * - A volume device is exported as a full disk using the vnode 6022047ba61eSachartre * interface or as a single slice disk using the LDI 6023047ba61eSachartre * interface. 6024047ba61eSachartre * 6025047ba61eSachartre * - A disk (represented by the slice 2 of that disk) is 6026047ba61eSachartre * exported as a full disk using the LDI interface. 6027047ba61eSachartre * 6028047ba61eSachartre * - A disk slice (different from slice 2) is always exported 6029047ba61eSachartre * as a single slice disk using the LDI interface. 6030047ba61eSachartre * 6031047ba61eSachartre * - The slice 2 of a disk is exported as a single slice disk 6032047ba61eSachartre * if the "slice" option is specified, otherwise the entire 6033047ba61eSachartre * disk will be exported. In any case, the LDI interface is 6034047ba61eSachartre * used. 6035047ba61eSachartre */ 6036047ba61eSachartre 6037047ba61eSachartre /* check if this is a pseudo device */ 6038047ba61eSachartre if ((dip = ddi_hold_devi_by_instance(getmajor(vnp->v_rdev), 6039047ba61eSachartre dev_to_instance(vnp->v_rdev), 0)) == NULL) { 6040047ba61eSachartre PRN("%s is no longer accessible", path); 6041047ba61eSachartre VN_RELE(vnp); 6042047ba61eSachartre status = EIO; 6043047ba61eSachartre break; 6044047ba61eSachartre } 60458fce2fd6Sachartre pseudo = is_pseudo_device(dip); 6046047ba61eSachartre ddi_release_devi(dip); 6047047ba61eSachartre VN_RELE(vnp); 6048047ba61eSachartre 60498fce2fd6Sachartre if (vd_identify_dev(vd, &drv_type) != 0) { 60508fce2fd6Sachartre PRN("%s identification failed", path); 60518fce2fd6Sachartre status = EIO; 60528fce2fd6Sachartre break; 60538fce2fd6Sachartre } 60548fce2fd6Sachartre 60558fce2fd6Sachartre /* 60568fce2fd6Sachartre * If the driver hasn't been identified then we consider that 60578fce2fd6Sachartre * pseudo devices are volumes and other devices are disks. 60588fce2fd6Sachartre */ 60598fce2fd6Sachartre if (drv_type == VD_DRIVER_VOLUME || 60608fce2fd6Sachartre (drv_type == VD_DRIVER_UNKNOWN && pseudo)) { 60618fce2fd6Sachartre vd->volume = B_TRUE; 60628fce2fd6Sachartre } else { 60632f5224aeSachartre status = vd_setup_backend_ldi(vd); 60642f5224aeSachartre break; 60652f5224aeSachartre } 60662f5224aeSachartre 6067047ba61eSachartre /* 60688fce2fd6Sachartre * If this is a volume device then its usage depends if the 6069047ba61eSachartre * "slice" option is set or not. If the "slice" option is set 60708fce2fd6Sachartre * then the volume device will be exported as a single slice, 6071047ba61eSachartre * otherwise it will be exported as a full disk. 60722f5224aeSachartre * 60732f5224aeSachartre * For backward compatibility, if vd_volume_force_slice is set 60748fce2fd6Sachartre * then we always export volume devices as slices. 6075047ba61eSachartre */ 60762f5224aeSachartre if (vd_volume_force_slice) { 60772f5224aeSachartre vd->vdisk_type = VD_DISK_TYPE_SLICE; 60782f5224aeSachartre vd->nslices = 1; 60792f5224aeSachartre } 60802f5224aeSachartre 60818fce2fd6Sachartre if (vd->vdisk_type == VD_DISK_TYPE_DISK) { 60828fce2fd6Sachartre /* close device opened during identification */ 60838fce2fd6Sachartre (void) ldi_close(vd->ldi_handle[0], 60848fce2fd6Sachartre vd->open_flags & ~FWRITE, kcred); 60858fce2fd6Sachartre vd->ldi_handle[0] = NULL; 60868fce2fd6Sachartre vd->dev[0] = 0; 6087047ba61eSachartre status = vd_setup_backend_vnode(vd); 60888fce2fd6Sachartre } else { 6089047ba61eSachartre status = vd_setup_backend_ldi(vd); 60908fce2fd6Sachartre } 6091047ba61eSachartre break; 6092047ba61eSachartre 6093047ba61eSachartre default: 6094047ba61eSachartre PRN("Unsupported vdisk backend %s", path); 6095047ba61eSachartre VN_RELE(vnp); 6096047ba61eSachartre status = EBADF; 6097047ba61eSachartre } 6098047ba61eSachartre 6099047ba61eSachartre done: 6100047ba61eSachartre if (status != 0) { 6101047ba61eSachartre /* 6102047ba61eSachartre * If the error is retryable print an error message only 6103047ba61eSachartre * during the first try. 6104047ba61eSachartre */ 6105047ba61eSachartre if (status == ENXIO || status == ENODEV || 6106047ba61eSachartre status == ENOENT || status == EROFS) { 6107047ba61eSachartre if (!(vd->initialized & VD_SETUP_ERROR)) { 6108047ba61eSachartre PRN("%s is currently inaccessible (error %d)", 6109047ba61eSachartre path, status); 6110047ba61eSachartre } 6111047ba61eSachartre status = EAGAIN; 6112047ba61eSachartre } else { 6113047ba61eSachartre PRN("%s can not be exported as a virtual disk " 6114047ba61eSachartre "(error %d)", path, status); 6115047ba61eSachartre } 6116047ba61eSachartre vd->initialized |= VD_SETUP_ERROR; 6117047ba61eSachartre 6118047ba61eSachartre } else if (vd->initialized & VD_SETUP_ERROR) { 6119047ba61eSachartre /* print a message only if we previously had an error */ 6120047ba61eSachartre PRN("%s is now online", path); 6121047ba61eSachartre vd->initialized &= ~VD_SETUP_ERROR; 6122047ba61eSachartre } 6123047ba61eSachartre 6124047ba61eSachartre return (status); 6125047ba61eSachartre } 6126047ba61eSachartre 6127047ba61eSachartre static int 6128047ba61eSachartre vds_do_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options, 6129047ba61eSachartre uint64_t ldc_id, vd_t **vdp) 61301ae08745Sheppo { 61311ae08745Sheppo char tq_name[TASKQ_NAMELEN]; 61320a55fbb7Slm66018 int status; 61331ae08745Sheppo ddi_iblock_cookie_t iblock = NULL; 61341ae08745Sheppo ldc_attr_t ldc_attr; 61351ae08745Sheppo vd_t *vd; 61361ae08745Sheppo 61371ae08745Sheppo 61381ae08745Sheppo ASSERT(vds != NULL); 6139e1ebb9ecSlm66018 ASSERT(device_path != NULL); 61401ae08745Sheppo ASSERT(vdp != NULL); 6141e1ebb9ecSlm66018 PR0("Adding vdisk for %s", device_path); 61421ae08745Sheppo 61431ae08745Sheppo if ((vd = kmem_zalloc(sizeof (*vd), KM_NOSLEEP)) == NULL) { 61441ae08745Sheppo PRN("No memory for virtual disk"); 61451ae08745Sheppo return (EAGAIN); 61461ae08745Sheppo } 61471ae08745Sheppo *vdp = vd; /* assign here so vds_destroy_vd() can cleanup later */ 61481ae08745Sheppo vd->vds = vds; 61493c96341aSnarayan (void) strncpy(vd->device_path, device_path, MAXPATHLEN); 61501ae08745Sheppo 6151047ba61eSachartre /* Setup open flags */ 6152047ba61eSachartre vd->open_flags = FREAD; 6153047ba61eSachartre 6154047ba61eSachartre if (!(options & VD_OPT_RDONLY)) 6155047ba61eSachartre vd->open_flags |= FWRITE; 6156047ba61eSachartre 6157047ba61eSachartre if (options & VD_OPT_EXCLUSIVE) 6158047ba61eSachartre vd->open_flags |= FEXCL; 6159047ba61eSachartre 6160047ba61eSachartre /* Setup disk type */ 6161047ba61eSachartre if (options & VD_OPT_SLICE) { 6162047ba61eSachartre vd->vdisk_type = VD_DISK_TYPE_SLICE; 6163047ba61eSachartre vd->nslices = 1; 6164047ba61eSachartre } else { 6165047ba61eSachartre vd->vdisk_type = VD_DISK_TYPE_DISK; 6166047ba61eSachartre vd->nslices = V_NUMPAR; 6167047ba61eSachartre } 6168047ba61eSachartre 6169047ba61eSachartre /* default disk label */ 6170047ba61eSachartre vd->vdisk_label = VD_DISK_LABEL_UNK; 6171047ba61eSachartre 61720a55fbb7Slm66018 /* Open vdisk and initialize parameters */ 61733c96341aSnarayan if ((status = vd_setup_vd(vd)) == 0) { 61743c96341aSnarayan vd->initialized |= VD_DISK_READY; 61751ae08745Sheppo 61763c96341aSnarayan ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR); 61778fce2fd6Sachartre PR0("vdisk_type = %s, volume = %s, file = %s, nslices = %u", 61783c96341aSnarayan ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"), 61798fce2fd6Sachartre (vd->volume ? "yes" : "no"), (vd->file ? "yes" : "no"), 61803c96341aSnarayan vd->nslices); 61813c96341aSnarayan } else { 61823c96341aSnarayan if (status != EAGAIN) 61833c96341aSnarayan return (status); 61843c96341aSnarayan } 61851ae08745Sheppo 61861ae08745Sheppo /* Initialize locking */ 61871ae08745Sheppo if (ddi_get_soft_iblock_cookie(vds->dip, DDI_SOFTINT_MED, 61881ae08745Sheppo &iblock) != DDI_SUCCESS) { 61891ae08745Sheppo PRN("Could not get iblock cookie."); 61901ae08745Sheppo return (EIO); 61911ae08745Sheppo } 61921ae08745Sheppo 61931ae08745Sheppo mutex_init(&vd->lock, NULL, MUTEX_DRIVER, iblock); 61941ae08745Sheppo vd->initialized |= VD_LOCKING; 61951ae08745Sheppo 61961ae08745Sheppo 6197d10e4ef2Snarayan /* Create start and completion task queues for the vdisk */ 6198d10e4ef2Snarayan (void) snprintf(tq_name, sizeof (tq_name), "vd_startq%lu", id); 61991ae08745Sheppo PR1("tq_name = %s", tq_name); 6200d10e4ef2Snarayan if ((vd->startq = ddi_taskq_create(vds->dip, tq_name, 1, 62011ae08745Sheppo TASKQ_DEFAULTPRI, 0)) == NULL) { 62021ae08745Sheppo PRN("Could not create task queue"); 62031ae08745Sheppo return (EIO); 62041ae08745Sheppo } 6205d10e4ef2Snarayan (void) snprintf(tq_name, sizeof (tq_name), "vd_completionq%lu", id); 6206d10e4ef2Snarayan PR1("tq_name = %s", tq_name); 6207d10e4ef2Snarayan if ((vd->completionq = ddi_taskq_create(vds->dip, tq_name, 1, 6208d10e4ef2Snarayan TASKQ_DEFAULTPRI, 0)) == NULL) { 6209d10e4ef2Snarayan PRN("Could not create task queue"); 6210d10e4ef2Snarayan return (EIO); 6211d10e4ef2Snarayan } 62125b98b509Sachartre 62135b98b509Sachartre /* Allocate the staging buffer */ 62145b98b509Sachartre vd->max_msglen = sizeof (vio_msg_t); /* baseline vio message size */ 62155b98b509Sachartre vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP); 62165b98b509Sachartre 6217d10e4ef2Snarayan vd->enabled = 1; /* before callback can dispatch to startq */ 62181ae08745Sheppo 62191ae08745Sheppo 62201ae08745Sheppo /* Bring up LDC */ 62211ae08745Sheppo ldc_attr.devclass = LDC_DEV_BLK_SVC; 62221ae08745Sheppo ldc_attr.instance = ddi_get_instance(vds->dip); 62231ae08745Sheppo ldc_attr.mode = LDC_MODE_UNRELIABLE; 6224e1ebb9ecSlm66018 ldc_attr.mtu = VD_LDC_MTU; 62251ae08745Sheppo if ((status = ldc_init(ldc_id, &ldc_attr, &vd->ldc_handle)) != 0) { 622617cadca8Slm66018 PRN("Could not initialize LDC channel %lx, " 6227690555a1Sachartre "init failed with error %d", ldc_id, status); 62281ae08745Sheppo return (status); 62291ae08745Sheppo } 62301ae08745Sheppo vd->initialized |= VD_LDC; 62311ae08745Sheppo 62321ae08745Sheppo if ((status = ldc_reg_callback(vd->ldc_handle, vd_handle_ldc_events, 62331ae08745Sheppo (caddr_t)vd)) != 0) { 6234690555a1Sachartre PRN("Could not initialize LDC channel %lu," 6235690555a1Sachartre "reg_callback failed with error %d", ldc_id, status); 62361ae08745Sheppo return (status); 62371ae08745Sheppo } 62381ae08745Sheppo 62391ae08745Sheppo if ((status = ldc_open(vd->ldc_handle)) != 0) { 6240690555a1Sachartre PRN("Could not initialize LDC channel %lu," 6241690555a1Sachartre "open failed with error %d", ldc_id, status); 62421ae08745Sheppo return (status); 62431ae08745Sheppo } 62441ae08745Sheppo 62453af08d82Slm66018 if ((status = ldc_up(vd->ldc_handle)) != 0) { 624634683adeSsg70180 PR0("ldc_up() returned errno %d", status); 62473af08d82Slm66018 } 62483af08d82Slm66018 62494bac2208Snarayan /* Allocate the inband task memory handle */ 62504bac2208Snarayan status = ldc_mem_alloc_handle(vd->ldc_handle, &(vd->inband_task.mhdl)); 62514bac2208Snarayan if (status) { 6252690555a1Sachartre PRN("Could not initialize LDC channel %lu," 6253690555a1Sachartre "alloc_handle failed with error %d", ldc_id, status); 62544bac2208Snarayan return (ENXIO); 62554bac2208Snarayan } 62561ae08745Sheppo 62571ae08745Sheppo /* Add the successfully-initialized vdisk to the server's table */ 62581ae08745Sheppo if (mod_hash_insert(vds->vd_table, (mod_hash_key_t)id, vd) != 0) { 62591ae08745Sheppo PRN("Error adding vdisk ID %lu to table", id); 62601ae08745Sheppo return (EIO); 62611ae08745Sheppo } 62621ae08745Sheppo 62633af08d82Slm66018 /* store initial state */ 62643af08d82Slm66018 vd->state = VD_STATE_INIT; 62653af08d82Slm66018 62661ae08745Sheppo return (0); 62671ae08745Sheppo } 62681ae08745Sheppo 62693af08d82Slm66018 static void 62703af08d82Slm66018 vd_free_dring_task(vd_t *vdp) 62713af08d82Slm66018 { 62723af08d82Slm66018 if (vdp->dring_task != NULL) { 62733af08d82Slm66018 ASSERT(vdp->dring_len != 0); 62743af08d82Slm66018 /* Free all dring_task memory handles */ 62753af08d82Slm66018 for (int i = 0; i < vdp->dring_len; i++) { 62763af08d82Slm66018 (void) ldc_mem_free_handle(vdp->dring_task[i].mhdl); 62773af08d82Slm66018 kmem_free(vdp->dring_task[i].msg, vdp->max_msglen); 62783af08d82Slm66018 vdp->dring_task[i].msg = NULL; 62793af08d82Slm66018 } 62803af08d82Slm66018 kmem_free(vdp->dring_task, 62813af08d82Slm66018 (sizeof (*vdp->dring_task)) * vdp->dring_len); 62823af08d82Slm66018 vdp->dring_task = NULL; 62833af08d82Slm66018 } 62843af08d82Slm66018 } 62853af08d82Slm66018 62861ae08745Sheppo /* 62871ae08745Sheppo * Destroy the state associated with a virtual disk 62881ae08745Sheppo */ 62891ae08745Sheppo static void 62901ae08745Sheppo vds_destroy_vd(void *arg) 62911ae08745Sheppo { 62921ae08745Sheppo vd_t *vd = (vd_t *)arg; 629334683adeSsg70180 int retry = 0, rv; 62941ae08745Sheppo 62951ae08745Sheppo if (vd == NULL) 62961ae08745Sheppo return; 62971ae08745Sheppo 6298d10e4ef2Snarayan PR0("Destroying vdisk state"); 6299d10e4ef2Snarayan 63001ae08745Sheppo /* Disable queuing requests for the vdisk */ 63011ae08745Sheppo if (vd->initialized & VD_LOCKING) { 63021ae08745Sheppo mutex_enter(&vd->lock); 63031ae08745Sheppo vd->enabled = 0; 63041ae08745Sheppo mutex_exit(&vd->lock); 63051ae08745Sheppo } 63061ae08745Sheppo 6307d10e4ef2Snarayan /* Drain and destroy start queue (*before* destroying completionq) */ 6308d10e4ef2Snarayan if (vd->startq != NULL) 6309d10e4ef2Snarayan ddi_taskq_destroy(vd->startq); /* waits for queued tasks */ 6310d10e4ef2Snarayan 6311d10e4ef2Snarayan /* Drain and destroy completion queue (*before* shutting down LDC) */ 6312d10e4ef2Snarayan if (vd->completionq != NULL) 6313d10e4ef2Snarayan ddi_taskq_destroy(vd->completionq); /* waits for tasks */ 6314d10e4ef2Snarayan 63153af08d82Slm66018 vd_free_dring_task(vd); 63163af08d82Slm66018 631734683adeSsg70180 /* Free the inband task memory handle */ 631834683adeSsg70180 (void) ldc_mem_free_handle(vd->inband_task.mhdl); 631934683adeSsg70180 632034683adeSsg70180 /* Shut down LDC */ 632134683adeSsg70180 if (vd->initialized & VD_LDC) { 632234683adeSsg70180 /* unmap the dring */ 632334683adeSsg70180 if (vd->initialized & VD_DRING) 632434683adeSsg70180 (void) ldc_mem_dring_unmap(vd->dring_handle); 632534683adeSsg70180 632634683adeSsg70180 /* close LDC channel - retry on EAGAIN */ 632734683adeSsg70180 while ((rv = ldc_close(vd->ldc_handle)) == EAGAIN) { 632834683adeSsg70180 if (++retry > vds_ldc_retries) { 632934683adeSsg70180 PR0("Timed out closing channel"); 633034683adeSsg70180 break; 633134683adeSsg70180 } 633234683adeSsg70180 drv_usecwait(vds_ldc_delay); 633334683adeSsg70180 } 633434683adeSsg70180 if (rv == 0) { 633534683adeSsg70180 (void) ldc_unreg_callback(vd->ldc_handle); 633634683adeSsg70180 (void) ldc_fini(vd->ldc_handle); 633734683adeSsg70180 } else { 633834683adeSsg70180 /* 633934683adeSsg70180 * Closing the LDC channel has failed. Ideally we should 634034683adeSsg70180 * fail here but there is no Zeus level infrastructure 634134683adeSsg70180 * to handle this. The MD has already been changed and 634234683adeSsg70180 * we have to do the close. So we try to do as much 634334683adeSsg70180 * clean up as we can. 634434683adeSsg70180 */ 634534683adeSsg70180 (void) ldc_set_cb_mode(vd->ldc_handle, LDC_CB_DISABLE); 634634683adeSsg70180 while (ldc_unreg_callback(vd->ldc_handle) == EAGAIN) 634734683adeSsg70180 drv_usecwait(vds_ldc_delay); 634834683adeSsg70180 } 634934683adeSsg70180 } 635034683adeSsg70180 63513af08d82Slm66018 /* Free the staging buffer for msgs */ 63523af08d82Slm66018 if (vd->vio_msgp != NULL) { 63533af08d82Slm66018 kmem_free(vd->vio_msgp, vd->max_msglen); 63543af08d82Slm66018 vd->vio_msgp = NULL; 63553af08d82Slm66018 } 63563af08d82Slm66018 63573af08d82Slm66018 /* Free the inband message buffer */ 63583af08d82Slm66018 if (vd->inband_task.msg != NULL) { 63593af08d82Slm66018 kmem_free(vd->inband_task.msg, vd->max_msglen); 63603af08d82Slm66018 vd->inband_task.msg = NULL; 6361d10e4ef2Snarayan } 6362da6c28aaSamw 63633c96341aSnarayan if (vd->file) { 6364690555a1Sachartre /* Close file */ 6365047ba61eSachartre (void) VOP_CLOSE(vd->file_vnode, vd->open_flags, 1, 6366da6c28aaSamw 0, kcred, NULL); 63673c96341aSnarayan VN_RELE(vd->file_vnode); 636887a7269eSachartre if (vd->file_devid != NULL) 636987a7269eSachartre ddi_devid_free(vd->file_devid); 63703c96341aSnarayan } else { 63711ae08745Sheppo /* Close any open backing-device slices */ 6372*bae9e67eSachartre for (uint_t slice = 0; slice < V_NUMPAR; slice++) { 63731ae08745Sheppo if (vd->ldi_handle[slice] != NULL) { 63741ae08745Sheppo PR0("Closing slice %u", slice); 63751ae08745Sheppo (void) ldi_close(vd->ldi_handle[slice], 6376047ba61eSachartre vd->open_flags, kcred); 63771ae08745Sheppo } 63781ae08745Sheppo } 63793c96341aSnarayan } 63801ae08745Sheppo 6381*bae9e67eSachartre /* Free any fake label */ 6382*bae9e67eSachartre if (vd->flabel) { 6383*bae9e67eSachartre kmem_free(vd->flabel, vd->flabel_size); 6384*bae9e67eSachartre vd->flabel = NULL; 6385*bae9e67eSachartre vd->flabel_size = 0; 6386*bae9e67eSachartre } 6387*bae9e67eSachartre 63881ae08745Sheppo /* Free lock */ 63891ae08745Sheppo if (vd->initialized & VD_LOCKING) 63901ae08745Sheppo mutex_destroy(&vd->lock); 63911ae08745Sheppo 63921ae08745Sheppo /* Finally, free the vdisk structure itself */ 63931ae08745Sheppo kmem_free(vd, sizeof (*vd)); 63941ae08745Sheppo } 63951ae08745Sheppo 63961ae08745Sheppo static int 6397047ba61eSachartre vds_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options, 6398047ba61eSachartre uint64_t ldc_id) 63991ae08745Sheppo { 64001ae08745Sheppo int status; 64011ae08745Sheppo vd_t *vd = NULL; 64021ae08745Sheppo 64031ae08745Sheppo 6404047ba61eSachartre if ((status = vds_do_init_vd(vds, id, device_path, options, 6405047ba61eSachartre ldc_id, &vd)) != 0) 64061ae08745Sheppo vds_destroy_vd(vd); 64071ae08745Sheppo 64081ae08745Sheppo return (status); 64091ae08745Sheppo } 64101ae08745Sheppo 64111ae08745Sheppo static int 64121ae08745Sheppo vds_do_get_ldc_id(md_t *md, mde_cookie_t vd_node, mde_cookie_t *channel, 64131ae08745Sheppo uint64_t *ldc_id) 64141ae08745Sheppo { 64151ae08745Sheppo int num_channels; 64161ae08745Sheppo 64171ae08745Sheppo 64181ae08745Sheppo /* Look for channel endpoint child(ren) of the vdisk MD node */ 64191ae08745Sheppo if ((num_channels = md_scan_dag(md, vd_node, 64201ae08745Sheppo md_find_name(md, VD_CHANNEL_ENDPOINT), 64211ae08745Sheppo md_find_name(md, "fwd"), channel)) <= 0) { 64221ae08745Sheppo PRN("No \"%s\" found for virtual disk", VD_CHANNEL_ENDPOINT); 64231ae08745Sheppo return (-1); 64241ae08745Sheppo } 64251ae08745Sheppo 64261ae08745Sheppo /* Get the "id" value for the first channel endpoint node */ 64271ae08745Sheppo if (md_get_prop_val(md, channel[0], VD_ID_PROP, ldc_id) != 0) { 64281ae08745Sheppo PRN("No \"%s\" property found for \"%s\" of vdisk", 64291ae08745Sheppo VD_ID_PROP, VD_CHANNEL_ENDPOINT); 64301ae08745Sheppo return (-1); 64311ae08745Sheppo } 64321ae08745Sheppo 64331ae08745Sheppo if (num_channels > 1) { 64341ae08745Sheppo PRN("Using ID of first of multiple channels for this vdisk"); 64351ae08745Sheppo } 64361ae08745Sheppo 64371ae08745Sheppo return (0); 64381ae08745Sheppo } 64391ae08745Sheppo 64401ae08745Sheppo static int 64411ae08745Sheppo vds_get_ldc_id(md_t *md, mde_cookie_t vd_node, uint64_t *ldc_id) 64421ae08745Sheppo { 64431ae08745Sheppo int num_nodes, status; 64441ae08745Sheppo size_t size; 64451ae08745Sheppo mde_cookie_t *channel; 64461ae08745Sheppo 64471ae08745Sheppo 64481ae08745Sheppo if ((num_nodes = md_node_count(md)) <= 0) { 64491ae08745Sheppo PRN("Invalid node count in Machine Description subtree"); 64501ae08745Sheppo return (-1); 64511ae08745Sheppo } 64521ae08745Sheppo size = num_nodes*(sizeof (*channel)); 64531ae08745Sheppo channel = kmem_zalloc(size, KM_SLEEP); 64541ae08745Sheppo status = vds_do_get_ldc_id(md, vd_node, channel, ldc_id); 64551ae08745Sheppo kmem_free(channel, size); 64561ae08745Sheppo 64571ae08745Sheppo return (status); 64581ae08745Sheppo } 64591ae08745Sheppo 6460047ba61eSachartre /* 6461047ba61eSachartre * Function: 6462047ba61eSachartre * vds_get_options 6463047ba61eSachartre * 6464047ba61eSachartre * Description: 6465047ba61eSachartre * Parse the options of a vds node. Options are defined as an array 6466047ba61eSachartre * of strings in the vds-block-device-opts property of the vds node 6467047ba61eSachartre * in the machine description. Options are returned as a bitmask. The 6468047ba61eSachartre * mapping between the bitmask options and the options strings from the 6469047ba61eSachartre * machine description is defined in the vd_bdev_options[] array. 6470047ba61eSachartre * 6471047ba61eSachartre * The vds-block-device-opts property is optional. If a vds has no such 6472047ba61eSachartre * property then no option is defined. 6473047ba61eSachartre * 6474047ba61eSachartre * Parameters: 6475047ba61eSachartre * md - machine description. 6476047ba61eSachartre * vd_node - vds node in the machine description for which 6477047ba61eSachartre * options have to be parsed. 6478047ba61eSachartre * options - the returned options. 6479047ba61eSachartre * 6480047ba61eSachartre * Return Code: 6481047ba61eSachartre * none. 6482047ba61eSachartre */ 6483047ba61eSachartre static void 6484047ba61eSachartre vds_get_options(md_t *md, mde_cookie_t vd_node, uint64_t *options) 6485047ba61eSachartre { 6486047ba61eSachartre char *optstr, *opt; 6487047ba61eSachartre int len, n, i; 6488047ba61eSachartre 6489047ba61eSachartre *options = 0; 6490047ba61eSachartre 6491047ba61eSachartre if (md_get_prop_data(md, vd_node, VD_BLOCK_DEVICE_OPTS, 6492047ba61eSachartre (uint8_t **)&optstr, &len) != 0) { 6493047ba61eSachartre PR0("No options found"); 6494047ba61eSachartre return; 6495047ba61eSachartre } 6496047ba61eSachartre 6497047ba61eSachartre /* parse options */ 6498047ba61eSachartre opt = optstr; 6499047ba61eSachartre n = sizeof (vd_bdev_options) / sizeof (vd_option_t); 6500047ba61eSachartre 6501047ba61eSachartre while (opt < optstr + len) { 6502047ba61eSachartre for (i = 0; i < n; i++) { 6503047ba61eSachartre if (strncmp(vd_bdev_options[i].vdo_name, 6504047ba61eSachartre opt, VD_OPTION_NLEN) == 0) { 6505047ba61eSachartre *options |= vd_bdev_options[i].vdo_value; 6506047ba61eSachartre break; 6507047ba61eSachartre } 6508047ba61eSachartre } 6509047ba61eSachartre 6510047ba61eSachartre if (i < n) { 6511047ba61eSachartre PR0("option: %s", opt); 6512047ba61eSachartre } else { 6513047ba61eSachartre PRN("option %s is unknown or unsupported", opt); 6514047ba61eSachartre } 6515047ba61eSachartre 6516047ba61eSachartre opt += strlen(opt) + 1; 6517047ba61eSachartre } 6518047ba61eSachartre } 6519047ba61eSachartre 65201ae08745Sheppo static void 65218fce2fd6Sachartre vds_driver_types_free(vds_t *vds) 65228fce2fd6Sachartre { 65238fce2fd6Sachartre if (vds->driver_types != NULL) { 65248fce2fd6Sachartre kmem_free(vds->driver_types, sizeof (vd_driver_type_t) * 65258fce2fd6Sachartre vds->num_drivers); 65268fce2fd6Sachartre vds->driver_types = NULL; 65278fce2fd6Sachartre vds->num_drivers = 0; 65288fce2fd6Sachartre } 65298fce2fd6Sachartre } 65308fce2fd6Sachartre 65318fce2fd6Sachartre /* 65328fce2fd6Sachartre * Update the driver type list with information from vds.conf. 65338fce2fd6Sachartre */ 65348fce2fd6Sachartre static void 65358fce2fd6Sachartre vds_driver_types_update(vds_t *vds) 65368fce2fd6Sachartre { 65378fce2fd6Sachartre char **list, *s; 65388fce2fd6Sachartre uint_t i, num, count = 0, len; 65398fce2fd6Sachartre 65408fce2fd6Sachartre if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, vds->dip, 65418fce2fd6Sachartre DDI_PROP_DONTPASS, "driver-type-list", &list, &num) != 65428fce2fd6Sachartre DDI_PROP_SUCCESS) 65438fce2fd6Sachartre return; 65448fce2fd6Sachartre 65458fce2fd6Sachartre /* 65468fce2fd6Sachartre * We create a driver_types list with as many as entries as there 65478fce2fd6Sachartre * is in the driver-type-list from vds.conf. However only valid 65488fce2fd6Sachartre * entries will be populated (i.e. entries from driver-type-list 65498fce2fd6Sachartre * with a valid syntax). Invalid entries will be left blank so 65508fce2fd6Sachartre * they will have no driver name and the driver type will be 65518fce2fd6Sachartre * VD_DRIVER_UNKNOWN (= 0). 65528fce2fd6Sachartre */ 65538fce2fd6Sachartre vds->num_drivers = num; 65548fce2fd6Sachartre vds->driver_types = kmem_zalloc(sizeof (vd_driver_type_t) * num, 65558fce2fd6Sachartre KM_SLEEP); 65568fce2fd6Sachartre 65578fce2fd6Sachartre for (i = 0; i < num; i++) { 65588fce2fd6Sachartre 65598fce2fd6Sachartre s = strchr(list[i], ':'); 65608fce2fd6Sachartre 65618fce2fd6Sachartre if (s == NULL) { 65628fce2fd6Sachartre PRN("vds.conf: driver-type-list, entry %d (%s): " 65638fce2fd6Sachartre "a colon is expected in the entry", 65648fce2fd6Sachartre i, list[i]); 65658fce2fd6Sachartre continue; 65668fce2fd6Sachartre } 65678fce2fd6Sachartre 65688fce2fd6Sachartre len = (uintptr_t)s - (uintptr_t)list[i]; 65698fce2fd6Sachartre 65708fce2fd6Sachartre if (len == 0) { 65718fce2fd6Sachartre PRN("vds.conf: driver-type-list, entry %d (%s): " 65728fce2fd6Sachartre "the driver name is empty", 65738fce2fd6Sachartre i, list[i]); 65748fce2fd6Sachartre continue; 65758fce2fd6Sachartre } 65768fce2fd6Sachartre 65778fce2fd6Sachartre if (len >= VD_DRIVER_NAME_LEN) { 65788fce2fd6Sachartre PRN("vds.conf: driver-type-list, entry %d (%s): " 65798fce2fd6Sachartre "the driver name is too long", 65808fce2fd6Sachartre i, list[i]); 65818fce2fd6Sachartre continue; 65828fce2fd6Sachartre } 65838fce2fd6Sachartre 65848fce2fd6Sachartre if (strcmp(s + 1, "disk") == 0) { 65858fce2fd6Sachartre 65868fce2fd6Sachartre vds->driver_types[i].type = VD_DRIVER_DISK; 65878fce2fd6Sachartre 65888fce2fd6Sachartre } else if (strcmp(s + 1, "volume") == 0) { 65898fce2fd6Sachartre 65908fce2fd6Sachartre vds->driver_types[i].type = VD_DRIVER_VOLUME; 65918fce2fd6Sachartre 65928fce2fd6Sachartre } else { 65938fce2fd6Sachartre PRN("vds.conf: driver-type-list, entry %d (%s): " 65948fce2fd6Sachartre "the driver type is invalid", 65958fce2fd6Sachartre i, list[i]); 65968fce2fd6Sachartre continue; 65978fce2fd6Sachartre } 65988fce2fd6Sachartre 65998fce2fd6Sachartre (void) strncpy(vds->driver_types[i].name, list[i], len); 66008fce2fd6Sachartre 66018fce2fd6Sachartre PR0("driver-type-list, entry %d (%s) added", 66028fce2fd6Sachartre i, list[i]); 66038fce2fd6Sachartre 66048fce2fd6Sachartre count++; 66058fce2fd6Sachartre } 66068fce2fd6Sachartre 66078fce2fd6Sachartre ddi_prop_free(list); 66088fce2fd6Sachartre 66098fce2fd6Sachartre if (count == 0) { 66108fce2fd6Sachartre /* nothing was added, clean up */ 66118fce2fd6Sachartre vds_driver_types_free(vds); 66128fce2fd6Sachartre } 66138fce2fd6Sachartre } 66148fce2fd6Sachartre 66158fce2fd6Sachartre static void 66161ae08745Sheppo vds_add_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node) 66171ae08745Sheppo { 6618e1ebb9ecSlm66018 char *device_path = NULL; 6619047ba61eSachartre uint64_t id = 0, ldc_id = 0, options = 0; 66201ae08745Sheppo 66211ae08745Sheppo if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) { 66221ae08745Sheppo PRN("Error getting vdisk \"%s\"", VD_ID_PROP); 66231ae08745Sheppo return; 66241ae08745Sheppo } 66251ae08745Sheppo PR0("Adding vdisk ID %lu", id); 66261ae08745Sheppo if (md_get_prop_str(md, vd_node, VD_BLOCK_DEVICE_PROP, 6627e1ebb9ecSlm66018 &device_path) != 0) { 66281ae08745Sheppo PRN("Error getting vdisk \"%s\"", VD_BLOCK_DEVICE_PROP); 66291ae08745Sheppo return; 66301ae08745Sheppo } 66311ae08745Sheppo 6632047ba61eSachartre vds_get_options(md, vd_node, &options); 6633047ba61eSachartre 66341ae08745Sheppo if (vds_get_ldc_id(md, vd_node, &ldc_id) != 0) { 66351ae08745Sheppo PRN("Error getting LDC ID for vdisk %lu", id); 66361ae08745Sheppo return; 66371ae08745Sheppo } 66381ae08745Sheppo 6639047ba61eSachartre if (vds_init_vd(vds, id, device_path, options, ldc_id) != 0) { 66401ae08745Sheppo PRN("Failed to add vdisk ID %lu", id); 664117cadca8Slm66018 if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0) 664217cadca8Slm66018 PRN("No vDisk entry found for vdisk ID %lu", id); 66431ae08745Sheppo return; 66441ae08745Sheppo } 66451ae08745Sheppo } 66461ae08745Sheppo 66471ae08745Sheppo static void 66481ae08745Sheppo vds_remove_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node) 66491ae08745Sheppo { 66501ae08745Sheppo uint64_t id = 0; 66511ae08745Sheppo 66521ae08745Sheppo 66531ae08745Sheppo if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) { 66541ae08745Sheppo PRN("Unable to get \"%s\" property from vdisk's MD node", 66551ae08745Sheppo VD_ID_PROP); 66561ae08745Sheppo return; 66571ae08745Sheppo } 66581ae08745Sheppo PR0("Removing vdisk ID %lu", id); 66591ae08745Sheppo if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0) 66601ae08745Sheppo PRN("No vdisk entry found for vdisk ID %lu", id); 66611ae08745Sheppo } 66621ae08745Sheppo 66631ae08745Sheppo static void 66641ae08745Sheppo vds_change_vd(vds_t *vds, md_t *prev_md, mde_cookie_t prev_vd_node, 66651ae08745Sheppo md_t *curr_md, mde_cookie_t curr_vd_node) 66661ae08745Sheppo { 66671ae08745Sheppo char *curr_dev, *prev_dev; 6668047ba61eSachartre uint64_t curr_id = 0, curr_ldc_id = 0, curr_options = 0; 6669047ba61eSachartre uint64_t prev_id = 0, prev_ldc_id = 0, prev_options = 0; 66701ae08745Sheppo size_t len; 66711ae08745Sheppo 66721ae08745Sheppo 66731ae08745Sheppo /* Validate that vdisk ID has not changed */ 66741ae08745Sheppo if (md_get_prop_val(prev_md, prev_vd_node, VD_ID_PROP, &prev_id) != 0) { 66751ae08745Sheppo PRN("Error getting previous vdisk \"%s\" property", 66761ae08745Sheppo VD_ID_PROP); 66771ae08745Sheppo return; 66781ae08745Sheppo } 66791ae08745Sheppo if (md_get_prop_val(curr_md, curr_vd_node, VD_ID_PROP, &curr_id) != 0) { 66801ae08745Sheppo PRN("Error getting current vdisk \"%s\" property", VD_ID_PROP); 66811ae08745Sheppo return; 66821ae08745Sheppo } 66831ae08745Sheppo if (curr_id != prev_id) { 66841ae08745Sheppo PRN("Not changing vdisk: ID changed from %lu to %lu", 66851ae08745Sheppo prev_id, curr_id); 66861ae08745Sheppo return; 66871ae08745Sheppo } 66881ae08745Sheppo 66891ae08745Sheppo /* Validate that LDC ID has not changed */ 66901ae08745Sheppo if (vds_get_ldc_id(prev_md, prev_vd_node, &prev_ldc_id) != 0) { 66911ae08745Sheppo PRN("Error getting LDC ID for vdisk %lu", prev_id); 66921ae08745Sheppo return; 66931ae08745Sheppo } 66941ae08745Sheppo 66951ae08745Sheppo if (vds_get_ldc_id(curr_md, curr_vd_node, &curr_ldc_id) != 0) { 66961ae08745Sheppo PRN("Error getting LDC ID for vdisk %lu", curr_id); 66971ae08745Sheppo return; 66981ae08745Sheppo } 66991ae08745Sheppo if (curr_ldc_id != prev_ldc_id) { 67000a55fbb7Slm66018 _NOTE(NOTREACHED); /* lint is confused */ 67011ae08745Sheppo PRN("Not changing vdisk: " 67021ae08745Sheppo "LDC ID changed from %lu to %lu", prev_ldc_id, curr_ldc_id); 67031ae08745Sheppo return; 67041ae08745Sheppo } 67051ae08745Sheppo 67061ae08745Sheppo /* Determine whether device path has changed */ 67071ae08745Sheppo if (md_get_prop_str(prev_md, prev_vd_node, VD_BLOCK_DEVICE_PROP, 67081ae08745Sheppo &prev_dev) != 0) { 67091ae08745Sheppo PRN("Error getting previous vdisk \"%s\"", 67101ae08745Sheppo VD_BLOCK_DEVICE_PROP); 67111ae08745Sheppo return; 67121ae08745Sheppo } 67131ae08745Sheppo if (md_get_prop_str(curr_md, curr_vd_node, VD_BLOCK_DEVICE_PROP, 67141ae08745Sheppo &curr_dev) != 0) { 67151ae08745Sheppo PRN("Error getting current vdisk \"%s\"", VD_BLOCK_DEVICE_PROP); 67161ae08745Sheppo return; 67171ae08745Sheppo } 67181ae08745Sheppo if (((len = strlen(curr_dev)) == strlen(prev_dev)) && 67191ae08745Sheppo (strncmp(curr_dev, prev_dev, len) == 0)) 67201ae08745Sheppo return; /* no relevant (supported) change */ 67211ae08745Sheppo 6722047ba61eSachartre /* Validate that options have not changed */ 6723047ba61eSachartre vds_get_options(prev_md, prev_vd_node, &prev_options); 6724047ba61eSachartre vds_get_options(curr_md, curr_vd_node, &curr_options); 6725047ba61eSachartre if (prev_options != curr_options) { 6726047ba61eSachartre PRN("Not changing vdisk: options changed from %lx to %lx", 6727047ba61eSachartre prev_options, curr_options); 6728047ba61eSachartre return; 6729047ba61eSachartre } 6730047ba61eSachartre 67311ae08745Sheppo PR0("Changing vdisk ID %lu", prev_id); 67323af08d82Slm66018 67331ae08745Sheppo /* Remove old state, which will close vdisk and reset */ 67341ae08745Sheppo if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)prev_id) != 0) 67351ae08745Sheppo PRN("No entry found for vdisk ID %lu", prev_id); 67363af08d82Slm66018 67371ae08745Sheppo /* Re-initialize vdisk with new state */ 6738047ba61eSachartre if (vds_init_vd(vds, curr_id, curr_dev, curr_options, 6739047ba61eSachartre curr_ldc_id) != 0) { 67401ae08745Sheppo PRN("Failed to change vdisk ID %lu", curr_id); 67411ae08745Sheppo return; 67421ae08745Sheppo } 67431ae08745Sheppo } 67441ae08745Sheppo 67451ae08745Sheppo static int 67461ae08745Sheppo vds_process_md(void *arg, mdeg_result_t *md) 67471ae08745Sheppo { 67481ae08745Sheppo int i; 67491ae08745Sheppo vds_t *vds = arg; 67501ae08745Sheppo 67511ae08745Sheppo 67521ae08745Sheppo if (md == NULL) 67531ae08745Sheppo return (MDEG_FAILURE); 67541ae08745Sheppo ASSERT(vds != NULL); 67551ae08745Sheppo 67561ae08745Sheppo for (i = 0; i < md->removed.nelem; i++) 67571ae08745Sheppo vds_remove_vd(vds, md->removed.mdp, md->removed.mdep[i]); 67581ae08745Sheppo for (i = 0; i < md->match_curr.nelem; i++) 67591ae08745Sheppo vds_change_vd(vds, md->match_prev.mdp, md->match_prev.mdep[i], 67601ae08745Sheppo md->match_curr.mdp, md->match_curr.mdep[i]); 67611ae08745Sheppo for (i = 0; i < md->added.nelem; i++) 67621ae08745Sheppo vds_add_vd(vds, md->added.mdp, md->added.mdep[i]); 67631ae08745Sheppo 67641ae08745Sheppo return (MDEG_SUCCESS); 67651ae08745Sheppo } 67661ae08745Sheppo 67673c96341aSnarayan 67681ae08745Sheppo static int 67691ae08745Sheppo vds_do_attach(dev_info_t *dip) 67701ae08745Sheppo { 6771445b4c2eSsb155480 int status, sz; 6772445b4c2eSsb155480 int cfg_handle; 67731ae08745Sheppo minor_t instance = ddi_get_instance(dip); 67741ae08745Sheppo vds_t *vds; 6775445b4c2eSsb155480 mdeg_prop_spec_t *pspecp; 6776445b4c2eSsb155480 mdeg_node_spec_t *ispecp; 67771ae08745Sheppo 67781ae08745Sheppo /* 67791ae08745Sheppo * The "cfg-handle" property of a vds node in an MD contains the MD's 67801ae08745Sheppo * notion of "instance", or unique identifier, for that node; OBP 67811ae08745Sheppo * stores the value of the "cfg-handle" MD property as the value of 67821ae08745Sheppo * the "reg" property on the node in the device tree it builds from 67831ae08745Sheppo * the MD and passes to Solaris. Thus, we look up the devinfo node's 67841ae08745Sheppo * "reg" property value to uniquely identify this device instance when 67851ae08745Sheppo * registering with the MD event-generation framework. If the "reg" 67861ae08745Sheppo * property cannot be found, the device tree state is presumably so 67871ae08745Sheppo * broken that there is no point in continuing. 67881ae08745Sheppo */ 6789445b4c2eSsb155480 if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 6790445b4c2eSsb155480 VD_REG_PROP)) { 6791445b4c2eSsb155480 PRN("vds \"%s\" property does not exist", VD_REG_PROP); 67921ae08745Sheppo return (DDI_FAILURE); 67931ae08745Sheppo } 67941ae08745Sheppo 67951ae08745Sheppo /* Get the MD instance for later MDEG registration */ 67961ae08745Sheppo cfg_handle = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 6797445b4c2eSsb155480 VD_REG_PROP, -1); 67981ae08745Sheppo 67991ae08745Sheppo if (ddi_soft_state_zalloc(vds_state, instance) != DDI_SUCCESS) { 68001ae08745Sheppo PRN("Could not allocate state for instance %u", instance); 68011ae08745Sheppo return (DDI_FAILURE); 68021ae08745Sheppo } 68031ae08745Sheppo 68041ae08745Sheppo if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) { 68051ae08745Sheppo PRN("Could not get state for instance %u", instance); 68061ae08745Sheppo ddi_soft_state_free(vds_state, instance); 68071ae08745Sheppo return (DDI_FAILURE); 68081ae08745Sheppo } 68091ae08745Sheppo 68101ae08745Sheppo vds->dip = dip; 68111ae08745Sheppo vds->vd_table = mod_hash_create_ptrhash("vds_vd_table", VDS_NCHAINS, 681287a7269eSachartre vds_destroy_vd, sizeof (void *)); 681387a7269eSachartre 68141ae08745Sheppo ASSERT(vds->vd_table != NULL); 68151ae08745Sheppo 68161ae08745Sheppo if ((status = ldi_ident_from_dip(dip, &vds->ldi_ident)) != 0) { 68171ae08745Sheppo PRN("ldi_ident_from_dip() returned errno %d", status); 68181ae08745Sheppo return (DDI_FAILURE); 68191ae08745Sheppo } 68201ae08745Sheppo vds->initialized |= VDS_LDI; 68211ae08745Sheppo 68221ae08745Sheppo /* Register for MD updates */ 6823445b4c2eSsb155480 sz = sizeof (vds_prop_template); 6824445b4c2eSsb155480 pspecp = kmem_alloc(sz, KM_SLEEP); 6825445b4c2eSsb155480 bcopy(vds_prop_template, pspecp, sz); 6826445b4c2eSsb155480 6827445b4c2eSsb155480 VDS_SET_MDEG_PROP_INST(pspecp, cfg_handle); 6828445b4c2eSsb155480 6829445b4c2eSsb155480 /* initialize the complete prop spec structure */ 6830445b4c2eSsb155480 ispecp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP); 6831445b4c2eSsb155480 ispecp->namep = "virtual-device"; 6832445b4c2eSsb155480 ispecp->specp = pspecp; 6833445b4c2eSsb155480 6834445b4c2eSsb155480 if (mdeg_register(ispecp, &vd_match, vds_process_md, vds, 68351ae08745Sheppo &vds->mdeg) != MDEG_SUCCESS) { 68361ae08745Sheppo PRN("Unable to register for MD updates"); 6837445b4c2eSsb155480 kmem_free(ispecp, sizeof (mdeg_node_spec_t)); 6838445b4c2eSsb155480 kmem_free(pspecp, sz); 68391ae08745Sheppo return (DDI_FAILURE); 68401ae08745Sheppo } 6841445b4c2eSsb155480 6842445b4c2eSsb155480 vds->ispecp = ispecp; 68431ae08745Sheppo vds->initialized |= VDS_MDEG; 68441ae08745Sheppo 68450a55fbb7Slm66018 /* Prevent auto-detaching so driver is available whenever MD changes */ 68460a55fbb7Slm66018 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1) != 68470a55fbb7Slm66018 DDI_PROP_SUCCESS) { 68480a55fbb7Slm66018 PRN("failed to set \"%s\" property for instance %u", 68490a55fbb7Slm66018 DDI_NO_AUTODETACH, instance); 68500a55fbb7Slm66018 } 68510a55fbb7Slm66018 68528fce2fd6Sachartre /* read any user defined driver types from conf file and update list */ 68538fce2fd6Sachartre vds_driver_types_update(vds); 68548fce2fd6Sachartre 68551ae08745Sheppo ddi_report_dev(dip); 68561ae08745Sheppo return (DDI_SUCCESS); 68571ae08745Sheppo } 68581ae08745Sheppo 68591ae08745Sheppo static int 68601ae08745Sheppo vds_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 68611ae08745Sheppo { 68621ae08745Sheppo int status; 68631ae08745Sheppo 68641ae08745Sheppo switch (cmd) { 68651ae08745Sheppo case DDI_ATTACH: 6866d10e4ef2Snarayan PR0("Attaching"); 68671ae08745Sheppo if ((status = vds_do_attach(dip)) != DDI_SUCCESS) 68681ae08745Sheppo (void) vds_detach(dip, DDI_DETACH); 68691ae08745Sheppo return (status); 68701ae08745Sheppo case DDI_RESUME: 6871d10e4ef2Snarayan PR0("No action required for DDI_RESUME"); 68721ae08745Sheppo return (DDI_SUCCESS); 68731ae08745Sheppo default: 68741ae08745Sheppo return (DDI_FAILURE); 68751ae08745Sheppo } 68761ae08745Sheppo } 68771ae08745Sheppo 68781ae08745Sheppo static struct dev_ops vds_ops = { 68791ae08745Sheppo DEVO_REV, /* devo_rev */ 68801ae08745Sheppo 0, /* devo_refcnt */ 68811ae08745Sheppo ddi_no_info, /* devo_getinfo */ 68821ae08745Sheppo nulldev, /* devo_identify */ 68831ae08745Sheppo nulldev, /* devo_probe */ 68841ae08745Sheppo vds_attach, /* devo_attach */ 68851ae08745Sheppo vds_detach, /* devo_detach */ 68861ae08745Sheppo nodev, /* devo_reset */ 68871ae08745Sheppo NULL, /* devo_cb_ops */ 68881ae08745Sheppo NULL, /* devo_bus_ops */ 68891ae08745Sheppo nulldev /* devo_power */ 68901ae08745Sheppo }; 68911ae08745Sheppo 68921ae08745Sheppo static struct modldrv modldrv = { 68931ae08745Sheppo &mod_driverops, 6894205eeb1aSlm66018 "virtual disk server", 68951ae08745Sheppo &vds_ops, 68961ae08745Sheppo }; 68971ae08745Sheppo 68981ae08745Sheppo static struct modlinkage modlinkage = { 68991ae08745Sheppo MODREV_1, 69001ae08745Sheppo &modldrv, 69011ae08745Sheppo NULL 69021ae08745Sheppo }; 69031ae08745Sheppo 69041ae08745Sheppo 69051ae08745Sheppo int 69061ae08745Sheppo _init(void) 69071ae08745Sheppo { 690817cadca8Slm66018 int status; 6909d10e4ef2Snarayan 69101ae08745Sheppo if ((status = ddi_soft_state_init(&vds_state, sizeof (vds_t), 1)) != 0) 69111ae08745Sheppo return (status); 691217cadca8Slm66018 69131ae08745Sheppo if ((status = mod_install(&modlinkage)) != 0) { 69141ae08745Sheppo ddi_soft_state_fini(&vds_state); 69151ae08745Sheppo return (status); 69161ae08745Sheppo } 69171ae08745Sheppo 69181ae08745Sheppo return (0); 69191ae08745Sheppo } 69201ae08745Sheppo 69211ae08745Sheppo int 69221ae08745Sheppo _info(struct modinfo *modinfop) 69231ae08745Sheppo { 69241ae08745Sheppo return (mod_info(&modlinkage, modinfop)); 69251ae08745Sheppo } 69261ae08745Sheppo 69271ae08745Sheppo int 69281ae08745Sheppo _fini(void) 69291ae08745Sheppo { 69301ae08745Sheppo int status; 69311ae08745Sheppo 69321ae08745Sheppo if ((status = mod_remove(&modlinkage)) != 0) 69331ae08745Sheppo return (status); 69341ae08745Sheppo ddi_soft_state_fini(&vds_state); 69351ae08745Sheppo return (0); 69361ae08745Sheppo } 6937