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 601ae08745Sheppo /* Virtual disk server initialization flags */ 61d10e4ef2Snarayan #define VDS_LDI 0x01 62d10e4ef2Snarayan #define VDS_MDEG 0x02 631ae08745Sheppo 641ae08745Sheppo /* Virtual disk server tunable parameters */ 653c96341aSnarayan #define VDS_RETRIES 5 663c96341aSnarayan #define VDS_LDC_DELAY 1000 /* 1 msecs */ 673c96341aSnarayan #define VDS_DEV_DELAY 10000000 /* 10 secs */ 681ae08745Sheppo #define VDS_NCHAINS 32 691ae08745Sheppo 701ae08745Sheppo /* Identification parameters for MD, synthetic dkio(7i) structures, etc. */ 711ae08745Sheppo #define VDS_NAME "virtual-disk-server" 721ae08745Sheppo 731ae08745Sheppo #define VD_NAME "vd" 741ae08745Sheppo #define VD_VOLUME_NAME "vdisk" 751ae08745Sheppo #define VD_ASCIILABEL "Virtual Disk" 761ae08745Sheppo 771ae08745Sheppo #define VD_CHANNEL_ENDPOINT "channel-endpoint" 781ae08745Sheppo #define VD_ID_PROP "id" 791ae08745Sheppo #define VD_BLOCK_DEVICE_PROP "vds-block-device" 80047ba61eSachartre #define VD_BLOCK_DEVICE_OPTS "vds-block-device-opts" 81445b4c2eSsb155480 #define VD_REG_PROP "reg" 821ae08745Sheppo 831ae08745Sheppo /* Virtual disk initialization flags */ 843c96341aSnarayan #define VD_DISK_READY 0x01 853c96341aSnarayan #define VD_LOCKING 0x02 863c96341aSnarayan #define VD_LDC 0x04 873c96341aSnarayan #define VD_DRING 0x08 883c96341aSnarayan #define VD_SID 0x10 893c96341aSnarayan #define VD_SEQ_NUM 0x20 90047ba61eSachartre #define VD_SETUP_ERROR 0x40 911ae08745Sheppo 92eba0cb4eSachartre /* Flags for writing to a vdisk which is a file */ 93eba0cb4eSachartre #define VD_FILE_WRITE_FLAGS SM_ASYNC 94eba0cb4eSachartre 9587a7269eSachartre /* Number of backup labels */ 9687a7269eSachartre #define VD_FILE_NUM_BACKUP 5 9787a7269eSachartre 9887a7269eSachartre /* Timeout for SCSI I/O */ 9987a7269eSachartre #define VD_SCSI_RDWR_TIMEOUT 30 /* 30 secs */ 10087a7269eSachartre 101edcc0754Sachartre /* Maximum number of logical partitions */ 102edcc0754Sachartre #define VD_MAXPART (NDKMAP + 1) 103edcc0754Sachartre 1041ae08745Sheppo /* 1051ae08745Sheppo * By Solaris convention, slice/partition 2 represents the entire disk; 1061ae08745Sheppo * unfortunately, this convention does not appear to be codified. 1071ae08745Sheppo */ 1081ae08745Sheppo #define VD_ENTIRE_DISK_SLICE 2 1091ae08745Sheppo 1108fce2fd6Sachartre /* Driver types */ 1118fce2fd6Sachartre typedef enum vd_driver { 1128fce2fd6Sachartre VD_DRIVER_UNKNOWN = 0, /* driver type unknown */ 1138fce2fd6Sachartre VD_DRIVER_DISK, /* disk driver */ 1148fce2fd6Sachartre VD_DRIVER_VOLUME /* volume driver */ 1158fce2fd6Sachartre } vd_driver_t; 1168fce2fd6Sachartre 1178fce2fd6Sachartre #define VD_DRIVER_NAME_LEN 64 1188fce2fd6Sachartre 1198fce2fd6Sachartre #define VDS_NUM_DRIVERS (sizeof (vds_driver_types) / sizeof (vd_driver_type_t)) 1208fce2fd6Sachartre 1218fce2fd6Sachartre typedef struct vd_driver_type { 1228fce2fd6Sachartre char name[VD_DRIVER_NAME_LEN]; /* driver name */ 1238fce2fd6Sachartre vd_driver_t type; /* driver type (disk or volume) */ 1248fce2fd6Sachartre } vd_driver_type_t; 1258fce2fd6Sachartre 1268fce2fd6Sachartre /* 1278fce2fd6Sachartre * There is no reliable way to determine if a device is representing a disk 1288fce2fd6Sachartre * or a volume, especially with pseudo devices. So we maintain a list of well 1298fce2fd6Sachartre * known drivers and the type of device they represent (either a disk or a 1308fce2fd6Sachartre * volume). 1318fce2fd6Sachartre * 1328fce2fd6Sachartre * The list can be extended by adding a "driver-type-list" entry in vds.conf 1338fce2fd6Sachartre * with the following syntax: 1348fce2fd6Sachartre * 1358fce2fd6Sachartre * driver-type-list="<driver>:<type>", ... ,"<driver>:<type>"; 1368fce2fd6Sachartre * 1378fce2fd6Sachartre * Where: 1388fce2fd6Sachartre * <driver> is the name of a driver (limited to 64 characters) 1398fce2fd6Sachartre * <type> is either the string "disk" or "volume" 1408fce2fd6Sachartre * 1418fce2fd6Sachartre * Invalid entries in "driver-type-list" will be ignored. 1428fce2fd6Sachartre * 1438fce2fd6Sachartre * For example, the following line in vds.conf: 1448fce2fd6Sachartre * 1458fce2fd6Sachartre * driver-type-list="foo:disk","bar:volume"; 1468fce2fd6Sachartre * 1478fce2fd6Sachartre * defines that "foo" is a disk driver, and driver "bar" is a volume driver. 1488fce2fd6Sachartre * 1498fce2fd6Sachartre * When a list is defined in vds.conf, it is checked before the built-in list 1508fce2fd6Sachartre * (vds_driver_types[]) so that any definition from this list can be overriden 1518fce2fd6Sachartre * using vds.conf. 1528fce2fd6Sachartre */ 1538fce2fd6Sachartre vd_driver_type_t vds_driver_types[] = { 1548fce2fd6Sachartre { "dad", VD_DRIVER_DISK }, /* Solaris */ 1558fce2fd6Sachartre { "did", VD_DRIVER_DISK }, /* Sun Cluster */ 156*5b98b509Sachartre { "emcp", VD_DRIVER_DISK }, /* EMC Powerpath */ 1578fce2fd6Sachartre { "lofi", VD_DRIVER_VOLUME }, /* Solaris */ 1588fce2fd6Sachartre { "md", VD_DRIVER_VOLUME }, /* Solaris - SVM */ 1598fce2fd6Sachartre { "sd", VD_DRIVER_DISK }, /* Solaris */ 1608fce2fd6Sachartre { "ssd", VD_DRIVER_DISK }, /* Solaris */ 1618fce2fd6Sachartre { "vdc", VD_DRIVER_DISK }, /* Solaris */ 1628fce2fd6Sachartre { "vxdmp", VD_DRIVER_DISK }, /* Veritas */ 1638fce2fd6Sachartre { "vxio", VD_DRIVER_VOLUME }, /* Veritas - VxVM */ 1648fce2fd6Sachartre { "zfs", VD_DRIVER_VOLUME } /* Solaris */ 1658fce2fd6Sachartre }; 1668fce2fd6Sachartre 1671ae08745Sheppo /* Return a cpp token as a string */ 1681ae08745Sheppo #define STRINGIZE(token) #token 1691ae08745Sheppo 1701ae08745Sheppo /* 1711ae08745Sheppo * Print a message prefixed with the current function name to the message log 1721ae08745Sheppo * (and optionally to the console for verbose boots); these macros use cpp's 1731ae08745Sheppo * concatenation of string literals and C99 variable-length-argument-list 1741ae08745Sheppo * macros 1751ae08745Sheppo */ 1761ae08745Sheppo #define PRN(...) _PRN("?%s(): "__VA_ARGS__, "") 1771ae08745Sheppo #define _PRN(format, ...) \ 1781ae08745Sheppo cmn_err(CE_CONT, format"%s", __func__, __VA_ARGS__) 1791ae08745Sheppo 1801ae08745Sheppo /* Return a pointer to the "i"th vdisk dring element */ 1811ae08745Sheppo #define VD_DRING_ELEM(i) ((vd_dring_entry_t *)(void *) \ 1821ae08745Sheppo (vd->dring + (i)*vd->descriptor_size)) 1831ae08745Sheppo 1841ae08745Sheppo /* Return the virtual disk client's type as a string (for use in messages) */ 1851ae08745Sheppo #define VD_CLIENT(vd) \ 1861ae08745Sheppo (((vd)->xfer_mode == VIO_DESC_MODE) ? "in-band client" : \ 187f0ca1d9aSsb155480 (((vd)->xfer_mode == VIO_DRING_MODE_V1_0) ? "dring client" : \ 1881ae08745Sheppo (((vd)->xfer_mode == 0) ? "null client" : \ 1891ae08745Sheppo "unsupported client"))) 1901ae08745Sheppo 191690555a1Sachartre /* Read disk label from a disk on file */ 192690555a1Sachartre #define VD_FILE_LABEL_READ(vd, labelp) \ 19387a7269eSachartre vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)labelp, \ 194690555a1Sachartre 0, sizeof (struct dk_label)) 195690555a1Sachartre 196690555a1Sachartre /* Write disk label to a disk on file */ 197690555a1Sachartre #define VD_FILE_LABEL_WRITE(vd, labelp) \ 19887a7269eSachartre vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)labelp, \ 199690555a1Sachartre 0, sizeof (struct dk_label)) 200690555a1Sachartre 2012f5224aeSachartre /* Message for disk access rights reset failure */ 2022f5224aeSachartre #define VD_RESET_ACCESS_FAILURE_MSG \ 2032f5224aeSachartre "Fail to reset disk access rights for disk %s" 2042f5224aeSachartre 205445b4c2eSsb155480 /* 206445b4c2eSsb155480 * Specification of an MD node passed to the MDEG to filter any 207445b4c2eSsb155480 * 'vport' nodes that do not belong to the specified node. This 208445b4c2eSsb155480 * template is copied for each vds instance and filled in with 209445b4c2eSsb155480 * the appropriate 'cfg-handle' value before being passed to the MDEG. 210445b4c2eSsb155480 */ 211445b4c2eSsb155480 static mdeg_prop_spec_t vds_prop_template[] = { 212445b4c2eSsb155480 { MDET_PROP_STR, "name", VDS_NAME }, 213445b4c2eSsb155480 { MDET_PROP_VAL, "cfg-handle", NULL }, 214445b4c2eSsb155480 { MDET_LIST_END, NULL, NULL } 215445b4c2eSsb155480 }; 216445b4c2eSsb155480 217445b4c2eSsb155480 #define VDS_SET_MDEG_PROP_INST(specp, val) (specp)[1].ps_val = (val); 218445b4c2eSsb155480 219445b4c2eSsb155480 /* 220445b4c2eSsb155480 * Matching criteria passed to the MDEG to register interest 221445b4c2eSsb155480 * in changes to 'virtual-device-port' nodes identified by their 222445b4c2eSsb155480 * 'id' property. 223445b4c2eSsb155480 */ 224445b4c2eSsb155480 static md_prop_match_t vd_prop_match[] = { 225445b4c2eSsb155480 { MDET_PROP_VAL, VD_ID_PROP }, 226445b4c2eSsb155480 { MDET_LIST_END, NULL } 227445b4c2eSsb155480 }; 228445b4c2eSsb155480 229445b4c2eSsb155480 static mdeg_node_match_t vd_match = {"virtual-device-port", 230445b4c2eSsb155480 vd_prop_match}; 231445b4c2eSsb155480 232047ba61eSachartre /* 233047ba61eSachartre * Options for the VD_BLOCK_DEVICE_OPTS property. 234047ba61eSachartre */ 235047ba61eSachartre #define VD_OPT_RDONLY 0x1 /* read-only */ 236047ba61eSachartre #define VD_OPT_SLICE 0x2 /* single slice */ 237047ba61eSachartre #define VD_OPT_EXCLUSIVE 0x4 /* exclusive access */ 238047ba61eSachartre 239047ba61eSachartre #define VD_OPTION_NLEN 128 240047ba61eSachartre 241047ba61eSachartre typedef struct vd_option { 242047ba61eSachartre char vdo_name[VD_OPTION_NLEN]; 243047ba61eSachartre uint64_t vdo_value; 244047ba61eSachartre } vd_option_t; 245047ba61eSachartre 246047ba61eSachartre vd_option_t vd_bdev_options[] = { 247047ba61eSachartre { "ro", VD_OPT_RDONLY }, 248047ba61eSachartre { "slice", VD_OPT_SLICE }, 249047ba61eSachartre { "excl", VD_OPT_EXCLUSIVE } 250047ba61eSachartre }; 251047ba61eSachartre 2521ae08745Sheppo /* Debugging macros */ 2531ae08745Sheppo #ifdef DEBUG 2543af08d82Slm66018 2553af08d82Slm66018 static int vd_msglevel = 0; 2563af08d82Slm66018 2571ae08745Sheppo #define PR0 if (vd_msglevel > 0) PRN 2581ae08745Sheppo #define PR1 if (vd_msglevel > 1) PRN 2591ae08745Sheppo #define PR2 if (vd_msglevel > 2) PRN 2601ae08745Sheppo 2611ae08745Sheppo #define VD_DUMP_DRING_ELEM(elem) \ 2623c96341aSnarayan PR0("dst:%x op:%x st:%u nb:%lx addr:%lx ncook:%u\n", \ 2631ae08745Sheppo elem->hdr.dstate, \ 2641ae08745Sheppo elem->payload.operation, \ 2651ae08745Sheppo elem->payload.status, \ 2661ae08745Sheppo elem->payload.nbytes, \ 2671ae08745Sheppo elem->payload.addr, \ 2681ae08745Sheppo elem->payload.ncookies); 2691ae08745Sheppo 2703af08d82Slm66018 char * 2713af08d82Slm66018 vd_decode_state(int state) 2723af08d82Slm66018 { 2733af08d82Slm66018 char *str; 2743af08d82Slm66018 2753af08d82Slm66018 #define CASE_STATE(_s) case _s: str = #_s; break; 2763af08d82Slm66018 2773af08d82Slm66018 switch (state) { 2783af08d82Slm66018 CASE_STATE(VD_STATE_INIT) 2793af08d82Slm66018 CASE_STATE(VD_STATE_VER) 2803af08d82Slm66018 CASE_STATE(VD_STATE_ATTR) 2813af08d82Slm66018 CASE_STATE(VD_STATE_DRING) 2823af08d82Slm66018 CASE_STATE(VD_STATE_RDX) 2833af08d82Slm66018 CASE_STATE(VD_STATE_DATA) 2843af08d82Slm66018 default: str = "unknown"; break; 2853af08d82Slm66018 } 2863af08d82Slm66018 2873af08d82Slm66018 #undef CASE_STATE 2883af08d82Slm66018 2893af08d82Slm66018 return (str); 2903af08d82Slm66018 } 2913af08d82Slm66018 2923af08d82Slm66018 void 2933af08d82Slm66018 vd_decode_tag(vio_msg_t *msg) 2943af08d82Slm66018 { 2953af08d82Slm66018 char *tstr, *sstr, *estr; 2963af08d82Slm66018 2973af08d82Slm66018 #define CASE_TYPE(_s) case _s: tstr = #_s; break; 2983af08d82Slm66018 2993af08d82Slm66018 switch (msg->tag.vio_msgtype) { 3003af08d82Slm66018 CASE_TYPE(VIO_TYPE_CTRL) 3013af08d82Slm66018 CASE_TYPE(VIO_TYPE_DATA) 3023af08d82Slm66018 CASE_TYPE(VIO_TYPE_ERR) 3033af08d82Slm66018 default: tstr = "unknown"; break; 3043af08d82Slm66018 } 3053af08d82Slm66018 3063af08d82Slm66018 #undef CASE_TYPE 3073af08d82Slm66018 3083af08d82Slm66018 #define CASE_SUBTYPE(_s) case _s: sstr = #_s; break; 3093af08d82Slm66018 3103af08d82Slm66018 switch (msg->tag.vio_subtype) { 3113af08d82Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_INFO) 3123af08d82Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_ACK) 3133af08d82Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_NACK) 3143af08d82Slm66018 default: sstr = "unknown"; break; 3153af08d82Slm66018 } 3163af08d82Slm66018 3173af08d82Slm66018 #undef CASE_SUBTYPE 3183af08d82Slm66018 3193af08d82Slm66018 #define CASE_ENV(_s) case _s: estr = #_s; break; 3203af08d82Slm66018 3213af08d82Slm66018 switch (msg->tag.vio_subtype_env) { 3223af08d82Slm66018 CASE_ENV(VIO_VER_INFO) 3233af08d82Slm66018 CASE_ENV(VIO_ATTR_INFO) 3243af08d82Slm66018 CASE_ENV(VIO_DRING_REG) 3253af08d82Slm66018 CASE_ENV(VIO_DRING_UNREG) 3263af08d82Slm66018 CASE_ENV(VIO_RDX) 3273af08d82Slm66018 CASE_ENV(VIO_PKT_DATA) 3283af08d82Slm66018 CASE_ENV(VIO_DESC_DATA) 3293af08d82Slm66018 CASE_ENV(VIO_DRING_DATA) 3303af08d82Slm66018 default: estr = "unknown"; break; 3313af08d82Slm66018 } 3323af08d82Slm66018 3333af08d82Slm66018 #undef CASE_ENV 3343af08d82Slm66018 3353af08d82Slm66018 PR1("(%x/%x/%x) message : (%s/%s/%s)", 3363af08d82Slm66018 msg->tag.vio_msgtype, msg->tag.vio_subtype, 3373af08d82Slm66018 msg->tag.vio_subtype_env, tstr, sstr, estr); 3383af08d82Slm66018 } 3393af08d82Slm66018 3401ae08745Sheppo #else /* !DEBUG */ 3413af08d82Slm66018 3421ae08745Sheppo #define PR0(...) 3431ae08745Sheppo #define PR1(...) 3441ae08745Sheppo #define PR2(...) 3451ae08745Sheppo 3461ae08745Sheppo #define VD_DUMP_DRING_ELEM(elem) 3471ae08745Sheppo 3483af08d82Slm66018 #define vd_decode_state(_s) (NULL) 3493af08d82Slm66018 #define vd_decode_tag(_s) (NULL) 3503af08d82Slm66018 3511ae08745Sheppo #endif /* DEBUG */ 3521ae08745Sheppo 3531ae08745Sheppo 354d10e4ef2Snarayan /* 355d10e4ef2Snarayan * Soft state structure for a vds instance 356d10e4ef2Snarayan */ 3571ae08745Sheppo typedef struct vds { 3581ae08745Sheppo uint_t initialized; /* driver inst initialization flags */ 3591ae08745Sheppo dev_info_t *dip; /* driver inst devinfo pointer */ 3601ae08745Sheppo ldi_ident_t ldi_ident; /* driver's identifier for LDI */ 3611ae08745Sheppo mod_hash_t *vd_table; /* table of virtual disks served */ 362445b4c2eSsb155480 mdeg_node_spec_t *ispecp; /* mdeg node specification */ 3631ae08745Sheppo mdeg_handle_t mdeg; /* handle for MDEG operations */ 3648fce2fd6Sachartre vd_driver_type_t *driver_types; /* extra driver types (from vds.conf) */ 3658fce2fd6Sachartre int num_drivers; /* num of extra driver types */ 3661ae08745Sheppo } vds_t; 3671ae08745Sheppo 368d10e4ef2Snarayan /* 369d10e4ef2Snarayan * Types of descriptor-processing tasks 370d10e4ef2Snarayan */ 371d10e4ef2Snarayan typedef enum vd_task_type { 372d10e4ef2Snarayan VD_NONFINAL_RANGE_TASK, /* task for intermediate descriptor in range */ 373d10e4ef2Snarayan VD_FINAL_RANGE_TASK, /* task for last in a range of descriptors */ 374d10e4ef2Snarayan } vd_task_type_t; 375d10e4ef2Snarayan 376d10e4ef2Snarayan /* 377d10e4ef2Snarayan * Structure describing the task for processing a descriptor 378d10e4ef2Snarayan */ 379d10e4ef2Snarayan typedef struct vd_task { 380d10e4ef2Snarayan struct vd *vd; /* vd instance task is for */ 381d10e4ef2Snarayan vd_task_type_t type; /* type of descriptor task */ 382d10e4ef2Snarayan int index; /* dring elem index for task */ 383d10e4ef2Snarayan vio_msg_t *msg; /* VIO message task is for */ 384d10e4ef2Snarayan size_t msglen; /* length of message content */ 385d10e4ef2Snarayan vd_dring_payload_t *request; /* request task will perform */ 386d10e4ef2Snarayan struct buf buf; /* buf(9s) for I/O request */ 3874bac2208Snarayan ldc_mem_handle_t mhdl; /* task memory handle */ 388205eeb1aSlm66018 int status; /* status of processing task */ 389205eeb1aSlm66018 int (*completef)(struct vd_task *task); /* completion func ptr */ 390d10e4ef2Snarayan } vd_task_t; 391d10e4ef2Snarayan 392d10e4ef2Snarayan /* 393d10e4ef2Snarayan * Soft state structure for a virtual disk instance 394d10e4ef2Snarayan */ 3951ae08745Sheppo typedef struct vd { 3961ae08745Sheppo uint_t initialized; /* vdisk initialization flags */ 39717cadca8Slm66018 uint64_t operations; /* bitmask of VD_OPs exported */ 39817cadca8Slm66018 vio_ver_t version; /* ver negotiated with client */ 3991ae08745Sheppo vds_t *vds; /* server for this vdisk */ 400d10e4ef2Snarayan ddi_taskq_t *startq; /* queue for I/O start tasks */ 401d10e4ef2Snarayan ddi_taskq_t *completionq; /* queue for completion tasks */ 4021ae08745Sheppo ldi_handle_t ldi_handle[V_NUMPAR]; /* LDI slice handles */ 4033c96341aSnarayan char device_path[MAXPATHLEN + 1]; /* vdisk device */ 4041ae08745Sheppo dev_t dev[V_NUMPAR]; /* dev numbers for slices */ 405047ba61eSachartre int open_flags; /* open flags */ 406e1ebb9ecSlm66018 uint_t nslices; /* number of slices */ 4071ae08745Sheppo size_t vdisk_size; /* number of blocks in vdisk */ 40817cadca8Slm66018 size_t vdisk_block_size; /* size of each vdisk block */ 4091ae08745Sheppo vd_disk_type_t vdisk_type; /* slice or entire disk */ 4104bac2208Snarayan vd_disk_label_t vdisk_label; /* EFI or VTOC label */ 41117cadca8Slm66018 vd_media_t vdisk_media; /* media type of backing dev. */ 41217cadca8Slm66018 boolean_t is_atapi_dev; /* Is this an IDE CD-ROM dev? */ 413e1ebb9ecSlm66018 ushort_t max_xfer_sz; /* max xfer size in DEV_BSIZE */ 41417cadca8Slm66018 size_t block_size; /* blk size of actual device */ 4158fce2fd6Sachartre boolean_t volume; /* is vDisk backed by volume */ 41617cadca8Slm66018 boolean_t file; /* is vDisk backed by a file? */ 4172f5224aeSachartre boolean_t scsi; /* is vDisk backed by scsi? */ 4183c96341aSnarayan vnode_t *file_vnode; /* file vnode */ 4193c96341aSnarayan size_t file_size; /* file size */ 42087a7269eSachartre ddi_devid_t file_devid; /* devid for disk image */ 421edcc0754Sachartre efi_gpt_t efi_gpt; /* EFI GPT for slice type */ 422edcc0754Sachartre efi_gpe_t efi_gpe; /* EFI GPE for slice type */ 423edcc0754Sachartre int efi_reserved; /* EFI reserved slice */ 4241ae08745Sheppo struct dk_geom dk_geom; /* synthetic for slice type */ 4251ae08745Sheppo struct vtoc vtoc; /* synthetic for slice type */ 426edcc0754Sachartre vd_slice_t slices[VD_MAXPART]; /* logical partitions */ 4272f5224aeSachartre boolean_t ownership; /* disk ownership status */ 4281ae08745Sheppo ldc_status_t ldc_state; /* LDC connection state */ 4291ae08745Sheppo ldc_handle_t ldc_handle; /* handle for LDC comm */ 4301ae08745Sheppo size_t max_msglen; /* largest LDC message len */ 4311ae08745Sheppo vd_state_t state; /* client handshake state */ 4321ae08745Sheppo uint8_t xfer_mode; /* transfer mode with client */ 4331ae08745Sheppo uint32_t sid; /* client's session ID */ 4341ae08745Sheppo uint64_t seq_num; /* message sequence number */ 4351ae08745Sheppo uint64_t dring_ident; /* identifier of dring */ 4361ae08745Sheppo ldc_dring_handle_t dring_handle; /* handle for dring ops */ 4371ae08745Sheppo uint32_t descriptor_size; /* num bytes in desc */ 4381ae08745Sheppo uint32_t dring_len; /* number of dring elements */ 4391ae08745Sheppo caddr_t dring; /* address of dring */ 4403af08d82Slm66018 caddr_t vio_msgp; /* vio msg staging buffer */ 441d10e4ef2Snarayan vd_task_t inband_task; /* task for inband descriptor */ 442d10e4ef2Snarayan vd_task_t *dring_task; /* tasks dring elements */ 443d10e4ef2Snarayan 444d10e4ef2Snarayan kmutex_t lock; /* protects variables below */ 445d10e4ef2Snarayan boolean_t enabled; /* is vdisk enabled? */ 446d10e4ef2Snarayan boolean_t reset_state; /* reset connection state? */ 447d10e4ef2Snarayan boolean_t reset_ldc; /* reset LDC channel? */ 4481ae08745Sheppo } vd_t; 4491ae08745Sheppo 4501ae08745Sheppo typedef struct vds_operation { 4513af08d82Slm66018 char *namep; 4521ae08745Sheppo uint8_t operation; 453d10e4ef2Snarayan int (*start)(vd_task_t *task); 454205eeb1aSlm66018 int (*complete)(vd_task_t *task); 4551ae08745Sheppo } vds_operation_t; 4561ae08745Sheppo 4570a55fbb7Slm66018 typedef struct vd_ioctl { 4580a55fbb7Slm66018 uint8_t operation; /* vdisk operation */ 4590a55fbb7Slm66018 const char *operation_name; /* vdisk operation name */ 4600a55fbb7Slm66018 size_t nbytes; /* size of operation buffer */ 4610a55fbb7Slm66018 int cmd; /* corresponding ioctl cmd */ 4620a55fbb7Slm66018 const char *cmd_name; /* ioctl cmd name */ 4630a55fbb7Slm66018 void *arg; /* ioctl cmd argument */ 4640a55fbb7Slm66018 /* convert input vd_buf to output ioctl_arg */ 4652f5224aeSachartre int (*copyin)(void *vd_buf, size_t, void *ioctl_arg); 4660a55fbb7Slm66018 /* convert input ioctl_arg to output vd_buf */ 4670a55fbb7Slm66018 void (*copyout)(void *ioctl_arg, void *vd_buf); 468047ba61eSachartre /* write is true if the operation writes any data to the backend */ 469047ba61eSachartre boolean_t write; 4700a55fbb7Slm66018 } vd_ioctl_t; 4710a55fbb7Slm66018 4720a55fbb7Slm66018 /* Define trivial copyin/copyout conversion function flag */ 4732f5224aeSachartre #define VD_IDENTITY_IN ((int (*)(void *, size_t, void *))-1) 4742f5224aeSachartre #define VD_IDENTITY_OUT ((void (*)(void *, void *))-1) 4751ae08745Sheppo 4761ae08745Sheppo 4773c96341aSnarayan static int vds_ldc_retries = VDS_RETRIES; 4783af08d82Slm66018 static int vds_ldc_delay = VDS_LDC_DELAY; 4793c96341aSnarayan static int vds_dev_retries = VDS_RETRIES; 4803c96341aSnarayan static int vds_dev_delay = VDS_DEV_DELAY; 4811ae08745Sheppo static void *vds_state; 4821ae08745Sheppo 483eba0cb4eSachartre static uint_t vd_file_write_flags = VD_FILE_WRITE_FLAGS; 484eba0cb4eSachartre 48587a7269eSachartre static short vd_scsi_rdwr_timeout = VD_SCSI_RDWR_TIMEOUT; 4862f5224aeSachartre static int vd_scsi_debug = USCSI_SILENT; 4872f5224aeSachartre 4882f5224aeSachartre /* 4892f5224aeSachartre * Tunable to define the behavior of the service domain if the vdisk server 4902f5224aeSachartre * fails to reset disk exclusive access when a LDC channel is reset. When a 4912f5224aeSachartre * LDC channel is reset the vdisk server will try to reset disk exclusive 4922f5224aeSachartre * access by releasing any SCSI-2 reservation or resetting the disk. If these 4932f5224aeSachartre * actions fail then the default behavior (vd_reset_access_failure = 0) is to 4942f5224aeSachartre * print a warning message. This default behavior can be changed by setting 4952f5224aeSachartre * the vd_reset_access_failure variable to A_REBOOT (= 0x1) and that will 4962f5224aeSachartre * cause the service domain to reboot, or A_DUMP (= 0x5) and that will cause 4972f5224aeSachartre * the service domain to panic. In both cases, the reset of the service domain 4982f5224aeSachartre * should trigger a reset SCSI buses and hopefully clear any SCSI-2 reservation. 4992f5224aeSachartre */ 5002f5224aeSachartre static int vd_reset_access_failure = 0; 5012f5224aeSachartre 5022f5224aeSachartre /* 5032f5224aeSachartre * Tunable for backward compatibility. When this variable is set to B_TRUE, 5042f5224aeSachartre * all disk volumes (ZFS, SVM, VxvM volumes) will be exported as single 5052f5224aeSachartre * slice disks whether or not they have the "slice" option set. This is 5062f5224aeSachartre * to provide a simple backward compatibility mechanism when upgrading 5072f5224aeSachartre * the vds driver and using a domain configuration created before the 5082f5224aeSachartre * "slice" option was available. 5092f5224aeSachartre */ 5102f5224aeSachartre static boolean_t vd_volume_force_slice = B_FALSE; 51187a7269eSachartre 5120a55fbb7Slm66018 /* 51366cfcfbeSachartre * The label of disk images created with some earlier versions of the virtual 51466cfcfbeSachartre * disk software is not entirely correct and have an incorrect v_sanity field 51566cfcfbeSachartre * (usually 0) instead of VTOC_SANE. This creates a compatibility problem with 51666cfcfbeSachartre * these images because we are now validating that the disk label (and the 51766cfcfbeSachartre * sanity) is correct when a disk image is opened. 51866cfcfbeSachartre * 51966cfcfbeSachartre * This tunable is set to false to not validate the sanity field and ensure 52066cfcfbeSachartre * compatibility. If the tunable is set to true, we will do a strict checking 52166cfcfbeSachartre * of the sanity but this can create compatibility problems with old disk 52266cfcfbeSachartre * images. 52366cfcfbeSachartre */ 52466cfcfbeSachartre static boolean_t vd_file_validate_sanity = B_FALSE; 52566cfcfbeSachartre 52666cfcfbeSachartre /* 5270a55fbb7Slm66018 * Supported protocol version pairs, from highest (newest) to lowest (oldest) 5280a55fbb7Slm66018 * 5290a55fbb7Slm66018 * Each supported major version should appear only once, paired with (and only 5300a55fbb7Slm66018 * with) its highest supported minor version number (as the protocol requires 5310a55fbb7Slm66018 * supporting all lower minor version numbers as well) 5320a55fbb7Slm66018 */ 53317cadca8Slm66018 static const vio_ver_t vds_version[] = {{1, 1}}; 5340a55fbb7Slm66018 static const size_t vds_num_versions = 5350a55fbb7Slm66018 sizeof (vds_version)/sizeof (vds_version[0]); 5360a55fbb7Slm66018 5373af08d82Slm66018 static void vd_free_dring_task(vd_t *vdp); 5383c96341aSnarayan static int vd_setup_vd(vd_t *vd); 539047ba61eSachartre static int vd_setup_single_slice_disk(vd_t *vd); 5402f5224aeSachartre static int vd_setup_mediainfo(vd_t *vd); 5413c96341aSnarayan static boolean_t vd_enabled(vd_t *vd); 54278fcd0a1Sachartre static ushort_t vd_lbl2cksum(struct dk_label *label); 54378fcd0a1Sachartre static int vd_file_validate_geometry(vd_t *vd); 54417cadca8Slm66018 static boolean_t vd_file_is_iso_image(vd_t *vd); 54517cadca8Slm66018 static void vd_set_exported_operations(vd_t *vd); 5462f5224aeSachartre static void vd_reset_access(vd_t *vd); 547edcc0754Sachartre static int vd_backend_ioctl(vd_t *vd, int cmd, caddr_t arg); 548edcc0754Sachartre static int vds_efi_alloc_and_read(vd_t *, efi_gpt_t **, efi_gpe_t **); 549edcc0754Sachartre static void vds_efi_free(vd_t *, efi_gpt_t *, efi_gpe_t *); 5508fce2fd6Sachartre static void vds_driver_types_free(vds_t *vds); 551047ba61eSachartre 552690555a1Sachartre /* 553690555a1Sachartre * Function: 554690555a1Sachartre * vd_file_rw 555690555a1Sachartre * 556690555a1Sachartre * Description: 557690555a1Sachartre * Read or write to a disk on file. 558690555a1Sachartre * 559690555a1Sachartre * Parameters: 560690555a1Sachartre * vd - disk on which the operation is performed. 561690555a1Sachartre * slice - slice on which the operation is performed, 56287a7269eSachartre * VD_SLICE_NONE indicates that the operation 56387a7269eSachartre * is done using an absolute disk offset. 564690555a1Sachartre * operation - operation to execute: read (VD_OP_BREAD) or 565690555a1Sachartre * write (VD_OP_BWRITE). 566690555a1Sachartre * data - buffer where data are read to or written from. 567690555a1Sachartre * blk - starting block for the operation. 568690555a1Sachartre * len - number of bytes to read or write. 569690555a1Sachartre * 570690555a1Sachartre * Return Code: 571690555a1Sachartre * n >= 0 - success, n indicates the number of bytes read 572690555a1Sachartre * or written. 573690555a1Sachartre * -1 - error. 574690555a1Sachartre */ 575690555a1Sachartre static ssize_t 576690555a1Sachartre vd_file_rw(vd_t *vd, int slice, int operation, caddr_t data, size_t blk, 577690555a1Sachartre size_t len) 578690555a1Sachartre { 579690555a1Sachartre caddr_t maddr; 580690555a1Sachartre size_t offset, maxlen, moffset, mlen, n; 581690555a1Sachartre uint_t smflags; 582690555a1Sachartre enum seg_rw srw; 583690555a1Sachartre 584690555a1Sachartre ASSERT(vd->file); 585690555a1Sachartre ASSERT(len > 0); 586690555a1Sachartre 587047ba61eSachartre /* 588047ba61eSachartre * If a file is exported as a slice then we don't care about the vtoc. 589047ba61eSachartre * In that case, the vtoc is a fake mainly to make newfs happy and we 590047ba61eSachartre * handle any I/O as a raw disk access so that we can have access to the 591047ba61eSachartre * entire backend. 592047ba61eSachartre */ 593047ba61eSachartre if (vd->vdisk_type == VD_DISK_TYPE_SLICE || slice == VD_SLICE_NONE) { 594690555a1Sachartre /* raw disk access */ 595690555a1Sachartre offset = blk * DEV_BSIZE; 596690555a1Sachartre } else { 597690555a1Sachartre ASSERT(slice >= 0 && slice < V_NUMPAR); 59878fcd0a1Sachartre 59917cadca8Slm66018 /* 60017cadca8Slm66018 * v1.0 vDisk clients depended on the server not verifying 60117cadca8Slm66018 * the label of a unformatted disk. This "feature" is 60217cadca8Slm66018 * maintained for backward compatibility but all versions 60317cadca8Slm66018 * from v1.1 onwards must do the right thing. 60417cadca8Slm66018 */ 60578fcd0a1Sachartre if (vd->vdisk_label == VD_DISK_LABEL_UNK && 606edcc0754Sachartre vio_ver_is_supported(vd->version, 1, 1)) { 607edcc0754Sachartre (void) vd_file_validate_geometry(vd); 608edcc0754Sachartre if (vd->vdisk_label == VD_DISK_LABEL_UNK) { 609edcc0754Sachartre PR0("Unknown disk label, can't do I/O " 610edcc0754Sachartre "from slice %d", slice); 61178fcd0a1Sachartre return (-1); 61278fcd0a1Sachartre } 613edcc0754Sachartre } 61478fcd0a1Sachartre 615edcc0754Sachartre if (vd->vdisk_label == VD_DISK_LABEL_VTOC) { 616edcc0754Sachartre ASSERT(vd->vtoc.v_sectorsz == DEV_BSIZE); 617edcc0754Sachartre } else { 618edcc0754Sachartre ASSERT(vd->vdisk_label == VD_DISK_LABEL_EFI); 619edcc0754Sachartre ASSERT(vd->vdisk_block_size == DEV_BSIZE); 620edcc0754Sachartre } 621edcc0754Sachartre 622edcc0754Sachartre if (blk >= vd->slices[slice].nblocks) { 623690555a1Sachartre /* address past the end of the slice */ 624690555a1Sachartre PR0("req_addr (0x%lx) > psize (0x%lx)", 625edcc0754Sachartre blk, vd->slices[slice].nblocks); 626690555a1Sachartre return (0); 627690555a1Sachartre } 628690555a1Sachartre 629edcc0754Sachartre offset = (vd->slices[slice].start + blk) * DEV_BSIZE; 630690555a1Sachartre 631690555a1Sachartre /* 632690555a1Sachartre * If the requested size is greater than the size 633690555a1Sachartre * of the partition, truncate the read/write. 634690555a1Sachartre */ 635edcc0754Sachartre maxlen = (vd->slices[slice].nblocks - blk) * DEV_BSIZE; 636690555a1Sachartre 637690555a1Sachartre if (len > maxlen) { 638690555a1Sachartre PR0("I/O size truncated to %lu bytes from %lu bytes", 639690555a1Sachartre maxlen, len); 640690555a1Sachartre len = maxlen; 641690555a1Sachartre } 642690555a1Sachartre } 643690555a1Sachartre 644690555a1Sachartre /* 645690555a1Sachartre * We have to ensure that we are reading/writing into the mmap 646690555a1Sachartre * range. If we have a partial disk image (e.g. an image of 647690555a1Sachartre * s0 instead s2) the system can try to access slices that 648690555a1Sachartre * are not included into the disk image. 649690555a1Sachartre */ 650edcc0754Sachartre if ((offset + len) > vd->file_size) { 651edcc0754Sachartre PR0("offset + nbytes (0x%lx + 0x%lx) > " 652690555a1Sachartre "file_size (0x%lx)", offset, len, vd->file_size); 653690555a1Sachartre return (-1); 654690555a1Sachartre } 655690555a1Sachartre 656690555a1Sachartre srw = (operation == VD_OP_BREAD)? S_READ : S_WRITE; 657eba0cb4eSachartre smflags = (operation == VD_OP_BREAD)? 0 : 658eba0cb4eSachartre (SM_WRITE | vd_file_write_flags); 659690555a1Sachartre n = len; 660690555a1Sachartre 661690555a1Sachartre do { 662690555a1Sachartre /* 663690555a1Sachartre * segmap_getmapflt() returns a MAXBSIZE chunk which is 664690555a1Sachartre * MAXBSIZE aligned. 665690555a1Sachartre */ 666690555a1Sachartre moffset = offset & MAXBOFFSET; 667690555a1Sachartre mlen = MIN(MAXBSIZE - moffset, n); 668690555a1Sachartre maddr = segmap_getmapflt(segkmap, vd->file_vnode, offset, 669690555a1Sachartre mlen, 1, srw); 670690555a1Sachartre /* 671690555a1Sachartre * Fault in the pages so we can check for error and ensure 672690555a1Sachartre * that we can safely used the mapped address. 673690555a1Sachartre */ 674690555a1Sachartre if (segmap_fault(kas.a_hat, segkmap, maddr, mlen, 675690555a1Sachartre F_SOFTLOCK, srw) != 0) { 676690555a1Sachartre (void) segmap_release(segkmap, maddr, 0); 677690555a1Sachartre return (-1); 678690555a1Sachartre } 679690555a1Sachartre 680690555a1Sachartre if (operation == VD_OP_BREAD) 681690555a1Sachartre bcopy(maddr + moffset, data, mlen); 682690555a1Sachartre else 683690555a1Sachartre bcopy(data, maddr + moffset, mlen); 684690555a1Sachartre 685690555a1Sachartre if (segmap_fault(kas.a_hat, segkmap, maddr, mlen, 686690555a1Sachartre F_SOFTUNLOCK, srw) != 0) { 687690555a1Sachartre (void) segmap_release(segkmap, maddr, 0); 688690555a1Sachartre return (-1); 689690555a1Sachartre } 690690555a1Sachartre if (segmap_release(segkmap, maddr, smflags) != 0) 691690555a1Sachartre return (-1); 692690555a1Sachartre n -= mlen; 693690555a1Sachartre offset += mlen; 694690555a1Sachartre data += mlen; 695690555a1Sachartre 696690555a1Sachartre } while (n > 0); 697690555a1Sachartre 698690555a1Sachartre return (len); 699690555a1Sachartre } 700690555a1Sachartre 70187a7269eSachartre /* 70287a7269eSachartre * Function: 70378fcd0a1Sachartre * vd_file_build_default_label 70478fcd0a1Sachartre * 70578fcd0a1Sachartre * Description: 70678fcd0a1Sachartre * Return a default label for the given disk. This is used when the disk 70778fcd0a1Sachartre * does not have a valid VTOC so that the user can get a valid default 70817cadca8Slm66018 * configuration. The default label has all slice sizes set to 0 (except 70978fcd0a1Sachartre * slice 2 which is the entire disk) to force the user to write a valid 71078fcd0a1Sachartre * label onto the disk image. 71178fcd0a1Sachartre * 71278fcd0a1Sachartre * Parameters: 71378fcd0a1Sachartre * vd - disk on which the operation is performed. 71478fcd0a1Sachartre * label - the returned default label. 71578fcd0a1Sachartre * 71678fcd0a1Sachartre * Return Code: 71778fcd0a1Sachartre * none. 71878fcd0a1Sachartre */ 71978fcd0a1Sachartre static void 72078fcd0a1Sachartre vd_file_build_default_label(vd_t *vd, struct dk_label *label) 72178fcd0a1Sachartre { 72278fcd0a1Sachartre size_t size; 72378fcd0a1Sachartre char prefix; 72478fcd0a1Sachartre 72578fcd0a1Sachartre ASSERT(vd->file); 726edcc0754Sachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK); 727edcc0754Sachartre 728edcc0754Sachartre bzero(label, sizeof (struct dk_label)); 72978fcd0a1Sachartre 73078fcd0a1Sachartre /* 73178fcd0a1Sachartre * We must have a resonable number of cylinders and sectors so 73278fcd0a1Sachartre * that newfs can run using default values. 73378fcd0a1Sachartre * 73478fcd0a1Sachartre * if (disk_size < 2MB) 73578fcd0a1Sachartre * phys_cylinders = disk_size / 100K 73678fcd0a1Sachartre * else 73778fcd0a1Sachartre * phys_cylinders = disk_size / 300K 73878fcd0a1Sachartre * 73978fcd0a1Sachartre * phys_cylinders = (phys_cylinders == 0) ? 1 : phys_cylinders 74078fcd0a1Sachartre * alt_cylinders = (phys_cylinders > 2) ? 2 : 0; 74178fcd0a1Sachartre * data_cylinders = phys_cylinders - alt_cylinders 74278fcd0a1Sachartre * 74378fcd0a1Sachartre * sectors = disk_size / (phys_cylinders * blk_size) 74478fcd0a1Sachartre * 74578fcd0a1Sachartre * The file size test is an attempt to not have too few cylinders 74678fcd0a1Sachartre * for a small file, or so many on a big file that you waste space 74778fcd0a1Sachartre * for backup superblocks or cylinder group structures. 74878fcd0a1Sachartre */ 74978fcd0a1Sachartre if (vd->file_size < (2 * 1024 * 1024)) 75078fcd0a1Sachartre label->dkl_pcyl = vd->file_size / (100 * 1024); 75178fcd0a1Sachartre else 75278fcd0a1Sachartre label->dkl_pcyl = vd->file_size / (300 * 1024); 75378fcd0a1Sachartre 75478fcd0a1Sachartre if (label->dkl_pcyl == 0) 75578fcd0a1Sachartre label->dkl_pcyl = 1; 75678fcd0a1Sachartre 757047ba61eSachartre label->dkl_acyl = 0; 758047ba61eSachartre 75978fcd0a1Sachartre if (label->dkl_pcyl > 2) 76078fcd0a1Sachartre label->dkl_acyl = 2; 76178fcd0a1Sachartre 76278fcd0a1Sachartre label->dkl_nsect = vd->file_size / 76378fcd0a1Sachartre (DEV_BSIZE * label->dkl_pcyl); 76478fcd0a1Sachartre label->dkl_ncyl = label->dkl_pcyl - label->dkl_acyl; 76578fcd0a1Sachartre label->dkl_nhead = 1; 76678fcd0a1Sachartre label->dkl_write_reinstruct = 0; 76778fcd0a1Sachartre label->dkl_read_reinstruct = 0; 76878fcd0a1Sachartre label->dkl_rpm = 7200; 76978fcd0a1Sachartre label->dkl_apc = 0; 77078fcd0a1Sachartre label->dkl_intrlv = 0; 77178fcd0a1Sachartre 77278fcd0a1Sachartre PR0("requested disk size: %ld bytes\n", vd->file_size); 77378fcd0a1Sachartre PR0("setup: ncyl=%d nhead=%d nsec=%d\n", label->dkl_pcyl, 77478fcd0a1Sachartre label->dkl_nhead, label->dkl_nsect); 77578fcd0a1Sachartre PR0("provided disk size: %ld bytes\n", (uint64_t) 77678fcd0a1Sachartre (label->dkl_pcyl * label->dkl_nhead * 77778fcd0a1Sachartre label->dkl_nsect * DEV_BSIZE)); 77878fcd0a1Sachartre 77978fcd0a1Sachartre if (vd->file_size < (1ULL << 20)) { 78078fcd0a1Sachartre size = vd->file_size >> 10; 78178fcd0a1Sachartre prefix = 'K'; /* Kilobyte */ 78278fcd0a1Sachartre } else if (vd->file_size < (1ULL << 30)) { 78378fcd0a1Sachartre size = vd->file_size >> 20; 78478fcd0a1Sachartre prefix = 'M'; /* Megabyte */ 78578fcd0a1Sachartre } else if (vd->file_size < (1ULL << 40)) { 78678fcd0a1Sachartre size = vd->file_size >> 30; 78778fcd0a1Sachartre prefix = 'G'; /* Gigabyte */ 78878fcd0a1Sachartre } else { 78978fcd0a1Sachartre size = vd->file_size >> 40; 79078fcd0a1Sachartre prefix = 'T'; /* Terabyte */ 79178fcd0a1Sachartre } 79278fcd0a1Sachartre 79378fcd0a1Sachartre /* 79478fcd0a1Sachartre * We must have a correct label name otherwise format(1m) will 79578fcd0a1Sachartre * not recognized the disk as labeled. 79678fcd0a1Sachartre */ 79778fcd0a1Sachartre (void) snprintf(label->dkl_asciilabel, LEN_DKL_ASCII, 79878fcd0a1Sachartre "SUN-DiskImage-%ld%cB cyl %d alt %d hd %d sec %d", 79978fcd0a1Sachartre size, prefix, 80078fcd0a1Sachartre label->dkl_ncyl, label->dkl_acyl, label->dkl_nhead, 80178fcd0a1Sachartre label->dkl_nsect); 80278fcd0a1Sachartre 80378fcd0a1Sachartre /* default VTOC */ 80478fcd0a1Sachartre label->dkl_vtoc.v_version = V_VERSION; 805edcc0754Sachartre label->dkl_vtoc.v_nparts = V_NUMPAR; 80678fcd0a1Sachartre label->dkl_vtoc.v_sanity = VTOC_SANE; 807edcc0754Sachartre label->dkl_vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_tag = V_BACKUP; 808edcc0754Sachartre label->dkl_map[VD_ENTIRE_DISK_SLICE].dkl_cylno = 0; 809edcc0754Sachartre label->dkl_map[VD_ENTIRE_DISK_SLICE].dkl_nblk = label->dkl_ncyl * 81078fcd0a1Sachartre label->dkl_nhead * label->dkl_nsect; 811edcc0754Sachartre label->dkl_magic = DKL_MAGIC; 81278fcd0a1Sachartre label->dkl_cksum = vd_lbl2cksum(label); 81378fcd0a1Sachartre } 81478fcd0a1Sachartre 81578fcd0a1Sachartre /* 81678fcd0a1Sachartre * Function: 81787a7269eSachartre * vd_file_set_vtoc 81887a7269eSachartre * 81987a7269eSachartre * Description: 82087a7269eSachartre * Set the vtoc of a disk image by writing the label and backup 82187a7269eSachartre * labels into the disk image backend. 82287a7269eSachartre * 82387a7269eSachartre * Parameters: 82487a7269eSachartre * vd - disk on which the operation is performed. 82587a7269eSachartre * label - the data to be written. 82687a7269eSachartre * 82787a7269eSachartre * Return Code: 82887a7269eSachartre * 0 - success. 82987a7269eSachartre * n > 0 - error, n indicates the errno code. 83087a7269eSachartre */ 83187a7269eSachartre static int 83287a7269eSachartre vd_file_set_vtoc(vd_t *vd, struct dk_label *label) 83387a7269eSachartre { 83487a7269eSachartre int blk, sec, cyl, head, cnt; 83587a7269eSachartre 83687a7269eSachartre ASSERT(vd->file); 83787a7269eSachartre 83887a7269eSachartre if (VD_FILE_LABEL_WRITE(vd, label) < 0) { 83987a7269eSachartre PR0("fail to write disk label"); 84087a7269eSachartre return (EIO); 84187a7269eSachartre } 84287a7269eSachartre 84387a7269eSachartre /* 84487a7269eSachartre * Backup labels are on the last alternate cylinder's 84587a7269eSachartre * first five odd sectors. 84687a7269eSachartre */ 84787a7269eSachartre if (label->dkl_acyl == 0) { 84887a7269eSachartre PR0("no alternate cylinder, can not store backup labels"); 84987a7269eSachartre return (0); 85087a7269eSachartre } 85187a7269eSachartre 85287a7269eSachartre cyl = label->dkl_ncyl + label->dkl_acyl - 1; 85387a7269eSachartre head = label->dkl_nhead - 1; 85487a7269eSachartre 85587a7269eSachartre blk = (cyl * ((label->dkl_nhead * label->dkl_nsect) - label->dkl_apc)) + 85687a7269eSachartre (head * label->dkl_nsect); 85787a7269eSachartre 85887a7269eSachartre /* 85987a7269eSachartre * Write the backup labels. Make sure we don't try to write past 86087a7269eSachartre * the last cylinder. 86187a7269eSachartre */ 86287a7269eSachartre sec = 1; 86387a7269eSachartre 86487a7269eSachartre for (cnt = 0; cnt < VD_FILE_NUM_BACKUP; cnt++) { 86587a7269eSachartre 86687a7269eSachartre if (sec >= label->dkl_nsect) { 86787a7269eSachartre PR0("not enough sector to store all backup labels"); 86887a7269eSachartre return (0); 86987a7269eSachartre } 87087a7269eSachartre 87187a7269eSachartre if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)label, 87287a7269eSachartre blk + sec, sizeof (struct dk_label)) < 0) { 87387a7269eSachartre PR0("error writing backup label at block %d\n", 87487a7269eSachartre blk + sec); 87587a7269eSachartre return (EIO); 87687a7269eSachartre } 87787a7269eSachartre 87887a7269eSachartre PR1("wrote backup label at block %d\n", blk + sec); 87987a7269eSachartre 88087a7269eSachartre sec += 2; 88187a7269eSachartre } 88287a7269eSachartre 88387a7269eSachartre return (0); 88487a7269eSachartre } 88587a7269eSachartre 88687a7269eSachartre /* 88787a7269eSachartre * Function: 88887a7269eSachartre * vd_file_get_devid_block 88987a7269eSachartre * 89087a7269eSachartre * Description: 89187a7269eSachartre * Return the block number where the device id is stored. 89287a7269eSachartre * 89387a7269eSachartre * Parameters: 89487a7269eSachartre * vd - disk on which the operation is performed. 89587a7269eSachartre * blkp - pointer to the block number 89687a7269eSachartre * 89787a7269eSachartre * Return Code: 89887a7269eSachartre * 0 - success 89987a7269eSachartre * ENOSPC - disk has no space to store a device id 90087a7269eSachartre */ 90187a7269eSachartre static int 90287a7269eSachartre vd_file_get_devid_block(vd_t *vd, size_t *blkp) 90387a7269eSachartre { 90487a7269eSachartre diskaddr_t spc, head, cyl; 90587a7269eSachartre 90687a7269eSachartre ASSERT(vd->file); 907edcc0754Sachartre 908edcc0754Sachartre if (vd->vdisk_label == VD_DISK_LABEL_UNK) { 909edcc0754Sachartre /* 910edcc0754Sachartre * If no label is defined we don't know where to find 911edcc0754Sachartre * a device id. 912edcc0754Sachartre */ 913edcc0754Sachartre return (ENOSPC); 914edcc0754Sachartre } 915edcc0754Sachartre 916edcc0754Sachartre if (vd->vdisk_label == VD_DISK_LABEL_EFI) { 917edcc0754Sachartre /* 918edcc0754Sachartre * For an EFI disk, the devid is at the beginning of 919edcc0754Sachartre * the reserved slice 920edcc0754Sachartre */ 921edcc0754Sachartre if (vd->efi_reserved == -1) { 922edcc0754Sachartre PR0("EFI disk has no reserved slice"); 923edcc0754Sachartre return (ENOSPC); 924edcc0754Sachartre } 925edcc0754Sachartre 926edcc0754Sachartre *blkp = vd->slices[vd->efi_reserved].start; 927edcc0754Sachartre return (0); 928edcc0754Sachartre } 929edcc0754Sachartre 93087a7269eSachartre ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC); 93187a7269eSachartre 93287a7269eSachartre /* this geometry doesn't allow us to have a devid */ 93387a7269eSachartre if (vd->dk_geom.dkg_acyl < 2) { 93487a7269eSachartre PR0("not enough alternate cylinder available for devid " 93587a7269eSachartre "(acyl=%u)", vd->dk_geom.dkg_acyl); 93687a7269eSachartre return (ENOSPC); 93787a7269eSachartre } 93887a7269eSachartre 93987a7269eSachartre /* the devid is in on the track next to the last cylinder */ 94087a7269eSachartre cyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl - 2; 94187a7269eSachartre spc = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect; 94287a7269eSachartre head = vd->dk_geom.dkg_nhead - 1; 94387a7269eSachartre 94487a7269eSachartre *blkp = (cyl * (spc - vd->dk_geom.dkg_apc)) + 94587a7269eSachartre (head * vd->dk_geom.dkg_nsect) + 1; 94687a7269eSachartre 94787a7269eSachartre return (0); 94887a7269eSachartre } 94987a7269eSachartre 95087a7269eSachartre /* 95187a7269eSachartre * Return the checksum of a disk block containing an on-disk devid. 95287a7269eSachartre */ 95387a7269eSachartre static uint_t 95487a7269eSachartre vd_dkdevid2cksum(struct dk_devid *dkdevid) 95587a7269eSachartre { 95687a7269eSachartre uint_t chksum, *ip; 95787a7269eSachartre int i; 95887a7269eSachartre 95987a7269eSachartre chksum = 0; 96087a7269eSachartre ip = (uint_t *)dkdevid; 96187a7269eSachartre for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int)); i++) 96287a7269eSachartre chksum ^= ip[i]; 96387a7269eSachartre 96487a7269eSachartre return (chksum); 96587a7269eSachartre } 96687a7269eSachartre 96787a7269eSachartre /* 96887a7269eSachartre * Function: 96987a7269eSachartre * vd_file_read_devid 97087a7269eSachartre * 97187a7269eSachartre * Description: 97287a7269eSachartre * Read the device id stored on a disk image. 97387a7269eSachartre * 97487a7269eSachartre * Parameters: 97587a7269eSachartre * vd - disk on which the operation is performed. 97687a7269eSachartre * devid - the return address of the device ID. 97787a7269eSachartre * 97887a7269eSachartre * Return Code: 97987a7269eSachartre * 0 - success 98087a7269eSachartre * EIO - I/O error while trying to access the disk image 98187a7269eSachartre * EINVAL - no valid device id was found 98287a7269eSachartre * ENOSPC - disk has no space to store a device id 98387a7269eSachartre */ 98487a7269eSachartre static int 98587a7269eSachartre vd_file_read_devid(vd_t *vd, ddi_devid_t *devid) 98687a7269eSachartre { 98787a7269eSachartre struct dk_devid *dkdevid; 98887a7269eSachartre size_t blk; 98987a7269eSachartre uint_t chksum; 99087a7269eSachartre int status, sz; 99187a7269eSachartre 99287a7269eSachartre if ((status = vd_file_get_devid_block(vd, &blk)) != 0) 99387a7269eSachartre return (status); 99487a7269eSachartre 99587a7269eSachartre dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP); 99687a7269eSachartre 99787a7269eSachartre /* get the devid */ 99887a7269eSachartre if ((vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)dkdevid, blk, 99987a7269eSachartre DEV_BSIZE)) < 0) { 100087a7269eSachartre PR0("error reading devid block at %lu", blk); 100187a7269eSachartre status = EIO; 100287a7269eSachartre goto done; 100387a7269eSachartre } 100487a7269eSachartre 100587a7269eSachartre /* validate the revision */ 100687a7269eSachartre if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) || 100787a7269eSachartre (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) { 100887a7269eSachartre PR0("invalid devid found at block %lu (bad revision)", blk); 100987a7269eSachartre status = EINVAL; 101087a7269eSachartre goto done; 101187a7269eSachartre } 101287a7269eSachartre 101387a7269eSachartre /* compute checksum */ 101487a7269eSachartre chksum = vd_dkdevid2cksum(dkdevid); 101587a7269eSachartre 101687a7269eSachartre /* compare the checksums */ 101787a7269eSachartre if (DKD_GETCHKSUM(dkdevid) != chksum) { 101887a7269eSachartre PR0("invalid devid found at block %lu (bad checksum)", blk); 101987a7269eSachartre status = EINVAL; 102087a7269eSachartre goto done; 102187a7269eSachartre } 102287a7269eSachartre 102387a7269eSachartre /* validate the device id */ 102487a7269eSachartre if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) { 102587a7269eSachartre PR0("invalid devid found at block %lu", blk); 102687a7269eSachartre status = EINVAL; 102787a7269eSachartre goto done; 102887a7269eSachartre } 102987a7269eSachartre 103087a7269eSachartre PR1("devid read at block %lu", blk); 103187a7269eSachartre 103287a7269eSachartre sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid); 103387a7269eSachartre *devid = kmem_alloc(sz, KM_SLEEP); 103487a7269eSachartre bcopy(&dkdevid->dkd_devid, *devid, sz); 103587a7269eSachartre 103687a7269eSachartre done: 103787a7269eSachartre kmem_free(dkdevid, DEV_BSIZE); 103887a7269eSachartre return (status); 103987a7269eSachartre 104087a7269eSachartre } 104187a7269eSachartre 104287a7269eSachartre /* 104387a7269eSachartre * Function: 104487a7269eSachartre * vd_file_write_devid 104587a7269eSachartre * 104687a7269eSachartre * Description: 104787a7269eSachartre * Write a device id into disk image. 104887a7269eSachartre * 104987a7269eSachartre * Parameters: 105087a7269eSachartre * vd - disk on which the operation is performed. 105187a7269eSachartre * devid - the device ID to store. 105287a7269eSachartre * 105387a7269eSachartre * Return Code: 105487a7269eSachartre * 0 - success 105587a7269eSachartre * EIO - I/O error while trying to access the disk image 105687a7269eSachartre * ENOSPC - disk has no space to store a device id 105787a7269eSachartre */ 105887a7269eSachartre static int 105987a7269eSachartre vd_file_write_devid(vd_t *vd, ddi_devid_t devid) 106087a7269eSachartre { 106187a7269eSachartre struct dk_devid *dkdevid; 106287a7269eSachartre uint_t chksum; 106387a7269eSachartre size_t blk; 106487a7269eSachartre int status; 106587a7269eSachartre 1066edcc0754Sachartre if (devid == NULL) { 1067edcc0754Sachartre /* nothing to write */ 1068edcc0754Sachartre return (0); 1069edcc0754Sachartre } 1070edcc0754Sachartre 107187a7269eSachartre if ((status = vd_file_get_devid_block(vd, &blk)) != 0) 107287a7269eSachartre return (status); 107387a7269eSachartre 107487a7269eSachartre dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP); 107587a7269eSachartre 107687a7269eSachartre /* set revision */ 107787a7269eSachartre dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB; 107887a7269eSachartre dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB; 107987a7269eSachartre 108087a7269eSachartre /* copy devid */ 108187a7269eSachartre bcopy(devid, &dkdevid->dkd_devid, ddi_devid_sizeof(devid)); 108287a7269eSachartre 108387a7269eSachartre /* compute checksum */ 108487a7269eSachartre chksum = vd_dkdevid2cksum(dkdevid); 108587a7269eSachartre 108687a7269eSachartre /* set checksum */ 108787a7269eSachartre DKD_FORMCHKSUM(chksum, dkdevid); 108887a7269eSachartre 108987a7269eSachartre /* store the devid */ 109087a7269eSachartre if ((status = vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, 109187a7269eSachartre (caddr_t)dkdevid, blk, DEV_BSIZE)) < 0) { 109287a7269eSachartre PR0("Error writing devid block at %lu", blk); 109387a7269eSachartre status = EIO; 109487a7269eSachartre } else { 109587a7269eSachartre PR1("devid written at block %lu", blk); 109687a7269eSachartre status = 0; 109787a7269eSachartre } 109887a7269eSachartre 109987a7269eSachartre kmem_free(dkdevid, DEV_BSIZE); 110087a7269eSachartre return (status); 110187a7269eSachartre } 110287a7269eSachartre 110387a7269eSachartre /* 110487a7269eSachartre * Function: 110517cadca8Slm66018 * vd_do_scsi_rdwr 110687a7269eSachartre * 110787a7269eSachartre * Description: 110887a7269eSachartre * Read or write to a SCSI disk using an absolute disk offset. 110987a7269eSachartre * 111087a7269eSachartre * Parameters: 111187a7269eSachartre * vd - disk on which the operation is performed. 111287a7269eSachartre * operation - operation to execute: read (VD_OP_BREAD) or 111387a7269eSachartre * write (VD_OP_BWRITE). 111487a7269eSachartre * data - buffer where data are read to or written from. 111587a7269eSachartre * blk - starting block for the operation. 111687a7269eSachartre * len - number of bytes to read or write. 111787a7269eSachartre * 111887a7269eSachartre * Return Code: 111987a7269eSachartre * 0 - success 112087a7269eSachartre * n != 0 - error. 112187a7269eSachartre */ 112287a7269eSachartre static int 112317cadca8Slm66018 vd_do_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t blk, size_t len) 112487a7269eSachartre { 112587a7269eSachartre struct uscsi_cmd ucmd; 112687a7269eSachartre union scsi_cdb cdb; 112787a7269eSachartre int nsectors, nblk; 112887a7269eSachartre int max_sectors; 112987a7269eSachartre int status, rval; 113087a7269eSachartre 113187a7269eSachartre ASSERT(!vd->file); 113217cadca8Slm66018 ASSERT(vd->vdisk_block_size > 0); 113387a7269eSachartre 113487a7269eSachartre max_sectors = vd->max_xfer_sz; 113517cadca8Slm66018 nblk = (len / vd->vdisk_block_size); 113687a7269eSachartre 113717cadca8Slm66018 if (len % vd->vdisk_block_size != 0) 113887a7269eSachartre return (EINVAL); 113987a7269eSachartre 114087a7269eSachartre /* 114187a7269eSachartre * Build and execute the uscsi ioctl. We build a group0, group1 114287a7269eSachartre * or group4 command as necessary, since some targets 114387a7269eSachartre * do not support group1 commands. 114487a7269eSachartre */ 114587a7269eSachartre while (nblk) { 114687a7269eSachartre 114787a7269eSachartre bzero(&ucmd, sizeof (ucmd)); 114887a7269eSachartre bzero(&cdb, sizeof (cdb)); 114987a7269eSachartre 115087a7269eSachartre nsectors = (max_sectors < nblk) ? max_sectors : nblk; 115187a7269eSachartre 115217cadca8Slm66018 /* 115317cadca8Slm66018 * Some of the optical drives on sun4v machines are ATAPI 115417cadca8Slm66018 * devices which use Group 1 Read/Write commands so we need 115517cadca8Slm66018 * to explicitly check a flag which is set when a domain 115617cadca8Slm66018 * is bound. 115717cadca8Slm66018 */ 115817cadca8Slm66018 if (blk < (2 << 20) && nsectors <= 0xff && !vd->is_atapi_dev) { 115987a7269eSachartre FORMG0ADDR(&cdb, blk); 116087a7269eSachartre FORMG0COUNT(&cdb, nsectors); 116187a7269eSachartre ucmd.uscsi_cdblen = CDB_GROUP0; 116287a7269eSachartre } else if (blk > 0xffffffff) { 116387a7269eSachartre FORMG4LONGADDR(&cdb, blk); 116487a7269eSachartre FORMG4COUNT(&cdb, nsectors); 116587a7269eSachartre ucmd.uscsi_cdblen = CDB_GROUP4; 116687a7269eSachartre cdb.scc_cmd |= SCMD_GROUP4; 116787a7269eSachartre } else { 116887a7269eSachartre FORMG1ADDR(&cdb, blk); 116987a7269eSachartre FORMG1COUNT(&cdb, nsectors); 117087a7269eSachartre ucmd.uscsi_cdblen = CDB_GROUP1; 117187a7269eSachartre cdb.scc_cmd |= SCMD_GROUP1; 117287a7269eSachartre } 117387a7269eSachartre ucmd.uscsi_cdb = (caddr_t)&cdb; 117487a7269eSachartre ucmd.uscsi_bufaddr = data; 117517cadca8Slm66018 ucmd.uscsi_buflen = nsectors * vd->block_size; 117687a7269eSachartre ucmd.uscsi_timeout = vd_scsi_rdwr_timeout; 117787a7269eSachartre /* 117887a7269eSachartre * Set flags so that the command is isolated from normal 117987a7269eSachartre * commands and no error message is printed. 118087a7269eSachartre */ 118187a7269eSachartre ucmd.uscsi_flags = USCSI_ISOLATE | USCSI_SILENT; 118287a7269eSachartre 118387a7269eSachartre if (operation == VD_OP_BREAD) { 118487a7269eSachartre cdb.scc_cmd |= SCMD_READ; 118587a7269eSachartre ucmd.uscsi_flags |= USCSI_READ; 118687a7269eSachartre } else { 118787a7269eSachartre cdb.scc_cmd |= SCMD_WRITE; 118887a7269eSachartre } 118987a7269eSachartre 119087a7269eSachartre status = ldi_ioctl(vd->ldi_handle[VD_ENTIRE_DISK_SLICE], 1191047ba61eSachartre USCSICMD, (intptr_t)&ucmd, (vd->open_flags | FKIOCTL), 119287a7269eSachartre kcred, &rval); 119387a7269eSachartre 119487a7269eSachartre if (status == 0) 119587a7269eSachartre status = ucmd.uscsi_status; 119687a7269eSachartre 119787a7269eSachartre if (status != 0) 119887a7269eSachartre break; 119987a7269eSachartre 120087a7269eSachartre /* 120187a7269eSachartre * Check if partial DMA breakup is required. If so, reduce 120287a7269eSachartre * the request size by half and retry the last request. 120387a7269eSachartre */ 120487a7269eSachartre if (ucmd.uscsi_resid == ucmd.uscsi_buflen) { 120587a7269eSachartre max_sectors >>= 1; 120687a7269eSachartre if (max_sectors <= 0) { 120787a7269eSachartre status = EIO; 120887a7269eSachartre break; 120987a7269eSachartre } 121087a7269eSachartre continue; 121187a7269eSachartre } 121287a7269eSachartre 121387a7269eSachartre if (ucmd.uscsi_resid != 0) { 121487a7269eSachartre status = EIO; 121587a7269eSachartre break; 121687a7269eSachartre } 121787a7269eSachartre 121887a7269eSachartre blk += nsectors; 121987a7269eSachartre nblk -= nsectors; 122017cadca8Slm66018 data += nsectors * vd->vdisk_block_size; /* SECSIZE */ 122187a7269eSachartre } 122287a7269eSachartre 122387a7269eSachartre return (status); 122487a7269eSachartre } 122587a7269eSachartre 1226205eeb1aSlm66018 /* 122717cadca8Slm66018 * Function: 122817cadca8Slm66018 * vd_scsi_rdwr 122917cadca8Slm66018 * 123017cadca8Slm66018 * Description: 123117cadca8Slm66018 * Wrapper function to read or write to a SCSI disk using an absolute 123217cadca8Slm66018 * disk offset. It checks the blocksize of the underlying device and, 123317cadca8Slm66018 * if necessary, adjusts the buffers accordingly before calling 123417cadca8Slm66018 * vd_do_scsi_rdwr() to do the actual read or write. 123517cadca8Slm66018 * 123617cadca8Slm66018 * Parameters: 123717cadca8Slm66018 * vd - disk on which the operation is performed. 123817cadca8Slm66018 * operation - operation to execute: read (VD_OP_BREAD) or 123917cadca8Slm66018 * write (VD_OP_BWRITE). 124017cadca8Slm66018 * data - buffer where data are read to or written from. 124117cadca8Slm66018 * blk - starting block for the operation. 124217cadca8Slm66018 * len - number of bytes to read or write. 124317cadca8Slm66018 * 124417cadca8Slm66018 * Return Code: 124517cadca8Slm66018 * 0 - success 124617cadca8Slm66018 * n != 0 - error. 124717cadca8Slm66018 */ 124817cadca8Slm66018 static int 124917cadca8Slm66018 vd_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t vblk, size_t vlen) 125017cadca8Slm66018 { 125117cadca8Slm66018 int rv; 125217cadca8Slm66018 125317cadca8Slm66018 size_t pblk; /* physical device block number of data on device */ 125417cadca8Slm66018 size_t delta; /* relative offset between pblk and vblk */ 125517cadca8Slm66018 size_t pnblk; /* number of physical blocks to be read from device */ 125617cadca8Slm66018 size_t plen; /* length of data to be read from physical device */ 125717cadca8Slm66018 char *buf; /* buffer area to fit physical device's block size */ 125817cadca8Slm66018 12592f5224aeSachartre if (vd->block_size == 0) { 12602f5224aeSachartre /* 12612f5224aeSachartre * The block size was not available during the attach, 12622f5224aeSachartre * try to update it now. 12632f5224aeSachartre */ 12642f5224aeSachartre if (vd_setup_mediainfo(vd) != 0) 12652f5224aeSachartre return (EIO); 12662f5224aeSachartre } 12672f5224aeSachartre 126817cadca8Slm66018 /* 126917cadca8Slm66018 * If the vdisk block size and the block size of the underlying device 127017cadca8Slm66018 * match we can skip straight to vd_do_scsi_rdwr(), otherwise we need 127117cadca8Slm66018 * to create a buffer large enough to handle the device's block size 127217cadca8Slm66018 * and adjust the block to be read from and the amount of data to 127317cadca8Slm66018 * read to correspond with the device's block size. 127417cadca8Slm66018 */ 127517cadca8Slm66018 if (vd->vdisk_block_size == vd->block_size) 127617cadca8Slm66018 return (vd_do_scsi_rdwr(vd, operation, data, vblk, vlen)); 127717cadca8Slm66018 127817cadca8Slm66018 if (vd->vdisk_block_size > vd->block_size) 127917cadca8Slm66018 return (EINVAL); 128017cadca8Slm66018 128117cadca8Slm66018 /* 128217cadca8Slm66018 * Writing of physical block sizes larger than the virtual block size 128317cadca8Slm66018 * is not supported. This would be added if/when support for guests 128417cadca8Slm66018 * writing to DVDs is implemented. 128517cadca8Slm66018 */ 128617cadca8Slm66018 if (operation == VD_OP_BWRITE) 128717cadca8Slm66018 return (ENOTSUP); 128817cadca8Slm66018 128917cadca8Slm66018 /* BEGIN CSTYLED */ 129017cadca8Slm66018 /* 129117cadca8Slm66018 * Below is a diagram showing the relationship between the physical 129217cadca8Slm66018 * and virtual blocks. If the virtual blocks marked by 'X' below are 129317cadca8Slm66018 * requested, then the physical blocks denoted by 'Y' are read. 129417cadca8Slm66018 * 129517cadca8Slm66018 * vblk 129617cadca8Slm66018 * | vlen 129717cadca8Slm66018 * |<--------------->| 129817cadca8Slm66018 * v v 129917cadca8Slm66018 * --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+- virtual disk: 130017cadca8Slm66018 * | | | |XX|XX|XX|XX|XX|XX| | | | | | } block size is 130117cadca8Slm66018 * --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+- vd->vdisk_block_size 130217cadca8Slm66018 * : : : : 130317cadca8Slm66018 * >:==:< delta : : 130417cadca8Slm66018 * : : : : 130517cadca8Slm66018 * --+-----+-----+-----+-----+-----+-----+-----+-- physical disk: 130617cadca8Slm66018 * | |YY:YY|YYYYY|YYYYY|YY:YY| | | } block size is 130717cadca8Slm66018 * --+-----+-----+-----+-----+-----+-----+-----+-- vd->block_size 130817cadca8Slm66018 * ^ ^ 130917cadca8Slm66018 * |<--------------------->| 131017cadca8Slm66018 * | plen 131117cadca8Slm66018 * pblk 131217cadca8Slm66018 */ 131317cadca8Slm66018 /* END CSTYLED */ 131417cadca8Slm66018 pblk = (vblk * vd->vdisk_block_size) / vd->block_size; 131517cadca8Slm66018 delta = (vblk * vd->vdisk_block_size) - (pblk * vd->block_size); 131617cadca8Slm66018 pnblk = ((delta + vlen - 1) / vd->block_size) + 1; 131717cadca8Slm66018 plen = pnblk * vd->block_size; 131817cadca8Slm66018 131917cadca8Slm66018 PR2("vblk %lx:pblk %lx: vlen %ld:plen %ld", vblk, pblk, vlen, plen); 132017cadca8Slm66018 132117cadca8Slm66018 buf = kmem_zalloc(sizeof (caddr_t) * plen, KM_SLEEP); 132217cadca8Slm66018 rv = vd_do_scsi_rdwr(vd, operation, (caddr_t)buf, pblk, plen); 132317cadca8Slm66018 bcopy(buf + delta, data, vlen); 132417cadca8Slm66018 132517cadca8Slm66018 kmem_free(buf, sizeof (caddr_t) * plen); 132617cadca8Slm66018 132717cadca8Slm66018 return (rv); 132817cadca8Slm66018 } 132917cadca8Slm66018 133017cadca8Slm66018 /* 1331205eeb1aSlm66018 * Return Values 1332205eeb1aSlm66018 * EINPROGRESS - operation was successfully started 1333205eeb1aSlm66018 * EIO - encountered LDC (aka. task error) 1334205eeb1aSlm66018 * 0 - operation completed successfully 1335205eeb1aSlm66018 * 1336205eeb1aSlm66018 * Side Effect 1337205eeb1aSlm66018 * sets request->status = <disk operation status> 1338205eeb1aSlm66018 */ 13391ae08745Sheppo static int 1340d10e4ef2Snarayan vd_start_bio(vd_task_t *task) 13411ae08745Sheppo { 13424bac2208Snarayan int rv, status = 0; 1343d10e4ef2Snarayan vd_t *vd = task->vd; 1344d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 1345d10e4ef2Snarayan struct buf *buf = &task->buf; 13464bac2208Snarayan uint8_t mtype; 13473c96341aSnarayan int slice; 1348047ba61eSachartre char *bufaddr = 0; 1349047ba61eSachartre size_t buflen; 1350d10e4ef2Snarayan 1351d10e4ef2Snarayan ASSERT(vd != NULL); 1352d10e4ef2Snarayan ASSERT(request != NULL); 13533c96341aSnarayan 13543c96341aSnarayan slice = request->slice; 13553c96341aSnarayan 135687a7269eSachartre ASSERT(slice == VD_SLICE_NONE || slice < vd->nslices); 1357d10e4ef2Snarayan ASSERT((request->operation == VD_OP_BREAD) || 1358d10e4ef2Snarayan (request->operation == VD_OP_BWRITE)); 1359d10e4ef2Snarayan 1360205eeb1aSlm66018 if (request->nbytes == 0) { 1361205eeb1aSlm66018 /* no service for trivial requests */ 1362205eeb1aSlm66018 request->status = EINVAL; 1363205eeb1aSlm66018 return (0); 1364205eeb1aSlm66018 } 13651ae08745Sheppo 1366d10e4ef2Snarayan PR1("%s %lu bytes at block %lu", 1367d10e4ef2Snarayan (request->operation == VD_OP_BREAD) ? "Read" : "Write", 1368d10e4ef2Snarayan request->nbytes, request->addr); 13691ae08745Sheppo 1370047ba61eSachartre /* 1371047ba61eSachartre * We have to check the open flags because the functions processing 1372047ba61eSachartre * the read/write request will not do it. 1373047ba61eSachartre */ 1374047ba61eSachartre if (request->operation == VD_OP_BWRITE && !(vd->open_flags & FWRITE)) { 1375047ba61eSachartre PR0("write fails because backend is opened read-only"); 1376047ba61eSachartre request->nbytes = 0; 1377047ba61eSachartre request->status = EROFS; 1378047ba61eSachartre return (0); 1379047ba61eSachartre } 1380d10e4ef2Snarayan 13814bac2208Snarayan mtype = (&vd->inband_task == task) ? LDC_SHADOW_MAP : LDC_DIRECT_MAP; 13824bac2208Snarayan 13834bac2208Snarayan /* Map memory exported by client */ 13844bac2208Snarayan status = ldc_mem_map(task->mhdl, request->cookie, request->ncookies, 13854bac2208Snarayan mtype, (request->operation == VD_OP_BREAD) ? LDC_MEM_W : LDC_MEM_R, 1386047ba61eSachartre &bufaddr, NULL); 13874bac2208Snarayan if (status != 0) { 13883af08d82Slm66018 PR0("ldc_mem_map() returned err %d ", status); 1389205eeb1aSlm66018 return (EIO); 1390d10e4ef2Snarayan } 1391d10e4ef2Snarayan 1392047ba61eSachartre buflen = request->nbytes; 1393047ba61eSachartre 1394047ba61eSachartre status = ldc_mem_acquire(task->mhdl, 0, buflen); 13954bac2208Snarayan if (status != 0) { 13964bac2208Snarayan (void) ldc_mem_unmap(task->mhdl); 13973af08d82Slm66018 PR0("ldc_mem_acquire() returned err %d ", status); 1398205eeb1aSlm66018 return (EIO); 13994bac2208Snarayan } 14004bac2208Snarayan 1401d10e4ef2Snarayan /* Start the block I/O */ 14023c96341aSnarayan if (vd->file) { 1403047ba61eSachartre rv = vd_file_rw(vd, slice, request->operation, bufaddr, 1404690555a1Sachartre request->addr, request->nbytes); 1405690555a1Sachartre if (rv < 0) { 14063c96341aSnarayan request->nbytes = 0; 1407205eeb1aSlm66018 request->status = EIO; 1408690555a1Sachartre } else { 1409690555a1Sachartre request->nbytes = rv; 1410205eeb1aSlm66018 request->status = 0; 14113c96341aSnarayan } 14123c96341aSnarayan } else { 141387a7269eSachartre if (slice == VD_SLICE_NONE) { 141487a7269eSachartre /* 141587a7269eSachartre * This is not a disk image so it is a real disk. We 141687a7269eSachartre * assume that the underlying device driver supports 141787a7269eSachartre * USCSICMD ioctls. This is the case of all SCSI devices 141887a7269eSachartre * (sd, ssd...). 141987a7269eSachartre * 142087a7269eSachartre * In the future if we have non-SCSI disks we would need 142187a7269eSachartre * to invoke the appropriate function to do I/O using an 142217cadca8Slm66018 * absolute disk offset (for example using DIOCTL_RWCMD 142387a7269eSachartre * for IDE disks). 142487a7269eSachartre */ 1425047ba61eSachartre rv = vd_scsi_rdwr(vd, request->operation, bufaddr, 1426047ba61eSachartre request->addr, request->nbytes); 142787a7269eSachartre if (rv != 0) { 142887a7269eSachartre request->nbytes = 0; 1429205eeb1aSlm66018 request->status = EIO; 143087a7269eSachartre } else { 1431205eeb1aSlm66018 request->status = 0; 143287a7269eSachartre } 143387a7269eSachartre } else { 1434047ba61eSachartre bioinit(buf); 1435047ba61eSachartre buf->b_flags = B_BUSY; 1436047ba61eSachartre buf->b_bcount = request->nbytes; 1437047ba61eSachartre buf->b_lblkno = request->addr; 1438047ba61eSachartre buf->b_edev = vd->dev[slice]; 1439047ba61eSachartre buf->b_un.b_addr = bufaddr; 1440047ba61eSachartre buf->b_flags |= (request->operation == VD_OP_BREAD)? 1441047ba61eSachartre B_READ : B_WRITE; 1442047ba61eSachartre 1443205eeb1aSlm66018 request->status = 1444205eeb1aSlm66018 ldi_strategy(vd->ldi_handle[slice], buf); 1445205eeb1aSlm66018 1446205eeb1aSlm66018 /* 1447205eeb1aSlm66018 * This is to indicate to the caller that the request 1448205eeb1aSlm66018 * needs to be finished by vd_complete_bio() by calling 1449205eeb1aSlm66018 * biowait() there and waiting for that to return before 1450205eeb1aSlm66018 * triggering the notification of the vDisk client. 1451205eeb1aSlm66018 * 1452205eeb1aSlm66018 * This is necessary when writing to real disks as 1453205eeb1aSlm66018 * otherwise calls to ldi_strategy() would be serialized 1454205eeb1aSlm66018 * behind the calls to biowait() and performance would 1455205eeb1aSlm66018 * suffer. 1456205eeb1aSlm66018 */ 1457205eeb1aSlm66018 if (request->status == 0) 145887a7269eSachartre return (EINPROGRESS); 1459047ba61eSachartre 1460047ba61eSachartre biofini(buf); 146187a7269eSachartre } 14623c96341aSnarayan } 14633c96341aSnarayan 1464d10e4ef2Snarayan /* Clean up after error */ 1465047ba61eSachartre rv = ldc_mem_release(task->mhdl, 0, buflen); 14664bac2208Snarayan if (rv) { 14673af08d82Slm66018 PR0("ldc_mem_release() returned err %d ", rv); 1468205eeb1aSlm66018 status = EIO; 14694bac2208Snarayan } 14704bac2208Snarayan rv = ldc_mem_unmap(task->mhdl); 14714bac2208Snarayan if (rv) { 1472205eeb1aSlm66018 PR0("ldc_mem_unmap() returned err %d ", rv); 1473205eeb1aSlm66018 status = EIO; 14744bac2208Snarayan } 14754bac2208Snarayan 1476d10e4ef2Snarayan return (status); 1477d10e4ef2Snarayan } 1478d10e4ef2Snarayan 1479205eeb1aSlm66018 /* 1480205eeb1aSlm66018 * This function should only be called from vd_notify to ensure that requests 1481205eeb1aSlm66018 * are responded to in the order that they are received. 1482205eeb1aSlm66018 */ 1483d10e4ef2Snarayan static int 1484d10e4ef2Snarayan send_msg(ldc_handle_t ldc_handle, void *msg, size_t msglen) 1485d10e4ef2Snarayan { 14863af08d82Slm66018 int status; 1487d10e4ef2Snarayan size_t nbytes; 1488d10e4ef2Snarayan 14893af08d82Slm66018 do { 1490d10e4ef2Snarayan nbytes = msglen; 1491d10e4ef2Snarayan status = ldc_write(ldc_handle, msg, &nbytes); 14923af08d82Slm66018 if (status != EWOULDBLOCK) 14933af08d82Slm66018 break; 14943af08d82Slm66018 drv_usecwait(vds_ldc_delay); 14953af08d82Slm66018 } while (status == EWOULDBLOCK); 1496d10e4ef2Snarayan 1497d10e4ef2Snarayan if (status != 0) { 14983af08d82Slm66018 if (status != ECONNRESET) 14993af08d82Slm66018 PR0("ldc_write() returned errno %d", status); 1500d10e4ef2Snarayan return (status); 1501d10e4ef2Snarayan } else if (nbytes != msglen) { 15023af08d82Slm66018 PR0("ldc_write() performed only partial write"); 1503d10e4ef2Snarayan return (EIO); 1504d10e4ef2Snarayan } 1505d10e4ef2Snarayan 1506d10e4ef2Snarayan PR1("SENT %lu bytes", msglen); 1507d10e4ef2Snarayan return (0); 1508d10e4ef2Snarayan } 1509d10e4ef2Snarayan 1510d10e4ef2Snarayan static void 1511d10e4ef2Snarayan vd_need_reset(vd_t *vd, boolean_t reset_ldc) 1512d10e4ef2Snarayan { 1513d10e4ef2Snarayan mutex_enter(&vd->lock); 1514d10e4ef2Snarayan vd->reset_state = B_TRUE; 1515d10e4ef2Snarayan vd->reset_ldc = reset_ldc; 1516d10e4ef2Snarayan mutex_exit(&vd->lock); 1517d10e4ef2Snarayan } 1518d10e4ef2Snarayan 1519d10e4ef2Snarayan /* 1520d10e4ef2Snarayan * Reset the state of the connection with a client, if needed; reset the LDC 1521d10e4ef2Snarayan * transport as well, if needed. This function should only be called from the 15223af08d82Slm66018 * "vd_recv_msg", as it waits for tasks - otherwise a deadlock can occur. 1523d10e4ef2Snarayan */ 1524d10e4ef2Snarayan static void 1525d10e4ef2Snarayan vd_reset_if_needed(vd_t *vd) 1526d10e4ef2Snarayan { 1527d10e4ef2Snarayan int status = 0; 1528d10e4ef2Snarayan 1529d10e4ef2Snarayan mutex_enter(&vd->lock); 1530d10e4ef2Snarayan if (!vd->reset_state) { 1531d10e4ef2Snarayan ASSERT(!vd->reset_ldc); 1532d10e4ef2Snarayan mutex_exit(&vd->lock); 1533d10e4ef2Snarayan return; 1534d10e4ef2Snarayan } 1535d10e4ef2Snarayan mutex_exit(&vd->lock); 1536d10e4ef2Snarayan 1537d10e4ef2Snarayan PR0("Resetting connection state with %s", VD_CLIENT(vd)); 1538d10e4ef2Snarayan 1539d10e4ef2Snarayan /* 1540d10e4ef2Snarayan * Let any asynchronous I/O complete before possibly pulling the rug 1541d10e4ef2Snarayan * out from under it; defer checking vd->reset_ldc, as one of the 1542d10e4ef2Snarayan * asynchronous tasks might set it 1543d10e4ef2Snarayan */ 1544d10e4ef2Snarayan ddi_taskq_wait(vd->completionq); 1545d10e4ef2Snarayan 15463c96341aSnarayan if (vd->file) { 1547da6c28aaSamw status = VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL); 15483c96341aSnarayan if (status) { 15493c96341aSnarayan PR0("VOP_FSYNC returned errno %d", status); 15503c96341aSnarayan } 15513c96341aSnarayan } 15523c96341aSnarayan 1553d10e4ef2Snarayan if ((vd->initialized & VD_DRING) && 1554d10e4ef2Snarayan ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0)) 15553af08d82Slm66018 PR0("ldc_mem_dring_unmap() returned errno %d", status); 1556d10e4ef2Snarayan 15573af08d82Slm66018 vd_free_dring_task(vd); 15583af08d82Slm66018 15593af08d82Slm66018 /* Free the staging buffer for msgs */ 15603af08d82Slm66018 if (vd->vio_msgp != NULL) { 15613af08d82Slm66018 kmem_free(vd->vio_msgp, vd->max_msglen); 15623af08d82Slm66018 vd->vio_msgp = NULL; 1563d10e4ef2Snarayan } 1564d10e4ef2Snarayan 15653af08d82Slm66018 /* Free the inband message buffer */ 15663af08d82Slm66018 if (vd->inband_task.msg != NULL) { 15673af08d82Slm66018 kmem_free(vd->inband_task.msg, vd->max_msglen); 15683af08d82Slm66018 vd->inband_task.msg = NULL; 15693af08d82Slm66018 } 1570d10e4ef2Snarayan 1571d10e4ef2Snarayan mutex_enter(&vd->lock); 15723af08d82Slm66018 15733af08d82Slm66018 if (vd->reset_ldc) 15743af08d82Slm66018 PR0("taking down LDC channel"); 1575e1ebb9ecSlm66018 if (vd->reset_ldc && ((status = ldc_down(vd->ldc_handle)) != 0)) 15763af08d82Slm66018 PR0("ldc_down() returned errno %d", status); 1577d10e4ef2Snarayan 15782f5224aeSachartre /* Reset exclusive access rights */ 15792f5224aeSachartre vd_reset_access(vd); 15802f5224aeSachartre 1581d10e4ef2Snarayan vd->initialized &= ~(VD_SID | VD_SEQ_NUM | VD_DRING); 1582d10e4ef2Snarayan vd->state = VD_STATE_INIT; 1583d10e4ef2Snarayan vd->max_msglen = sizeof (vio_msg_t); /* baseline vio message size */ 1584d10e4ef2Snarayan 15853af08d82Slm66018 /* Allocate the staging buffer */ 15863af08d82Slm66018 vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP); 15873af08d82Slm66018 15883af08d82Slm66018 PR0("calling ldc_up\n"); 15893af08d82Slm66018 (void) ldc_up(vd->ldc_handle); 15903af08d82Slm66018 1591d10e4ef2Snarayan vd->reset_state = B_FALSE; 1592d10e4ef2Snarayan vd->reset_ldc = B_FALSE; 15933af08d82Slm66018 1594d10e4ef2Snarayan mutex_exit(&vd->lock); 1595d10e4ef2Snarayan } 1596d10e4ef2Snarayan 15973af08d82Slm66018 static void vd_recv_msg(void *arg); 15983af08d82Slm66018 15993af08d82Slm66018 static void 16003af08d82Slm66018 vd_mark_in_reset(vd_t *vd) 16013af08d82Slm66018 { 16023af08d82Slm66018 int status; 16033af08d82Slm66018 16043af08d82Slm66018 PR0("vd_mark_in_reset: marking vd in reset\n"); 16053af08d82Slm66018 16063af08d82Slm66018 vd_need_reset(vd, B_FALSE); 16073af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, DDI_SLEEP); 16083af08d82Slm66018 if (status == DDI_FAILURE) { 16093af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 16103af08d82Slm66018 vd_need_reset(vd, B_TRUE); 16113af08d82Slm66018 return; 16123af08d82Slm66018 } 16133af08d82Slm66018 } 16143af08d82Slm66018 1615d10e4ef2Snarayan static int 16163c96341aSnarayan vd_mark_elem_done(vd_t *vd, int idx, int elem_status, int elem_nbytes) 1617d10e4ef2Snarayan { 1618d10e4ef2Snarayan boolean_t accepted; 1619d10e4ef2Snarayan int status; 1620d10e4ef2Snarayan vd_dring_entry_t *elem = VD_DRING_ELEM(idx); 1621d10e4ef2Snarayan 16223af08d82Slm66018 if (vd->reset_state) 16233af08d82Slm66018 return (0); 1624d10e4ef2Snarayan 1625d10e4ef2Snarayan /* Acquire the element */ 16263af08d82Slm66018 if (!vd->reset_state && 16273af08d82Slm66018 (status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) { 16283af08d82Slm66018 if (status == ECONNRESET) { 16293af08d82Slm66018 vd_mark_in_reset(vd); 16303af08d82Slm66018 return (0); 16313af08d82Slm66018 } else { 16323af08d82Slm66018 PR0("ldc_mem_dring_acquire() returned errno %d", 16333af08d82Slm66018 status); 1634d10e4ef2Snarayan return (status); 1635d10e4ef2Snarayan } 16363af08d82Slm66018 } 1637d10e4ef2Snarayan 1638d10e4ef2Snarayan /* Set the element's status and mark it done */ 1639d10e4ef2Snarayan accepted = (elem->hdr.dstate == VIO_DESC_ACCEPTED); 1640d10e4ef2Snarayan if (accepted) { 16413c96341aSnarayan elem->payload.nbytes = elem_nbytes; 1642d10e4ef2Snarayan elem->payload.status = elem_status; 1643d10e4ef2Snarayan elem->hdr.dstate = VIO_DESC_DONE; 1644d10e4ef2Snarayan } else { 1645d10e4ef2Snarayan /* Perhaps client timed out waiting for I/O... */ 16463af08d82Slm66018 PR0("element %u no longer \"accepted\"", idx); 1647d10e4ef2Snarayan VD_DUMP_DRING_ELEM(elem); 1648d10e4ef2Snarayan } 1649d10e4ef2Snarayan /* Release the element */ 16503af08d82Slm66018 if (!vd->reset_state && 16513af08d82Slm66018 (status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) { 16523af08d82Slm66018 if (status == ECONNRESET) { 16533af08d82Slm66018 vd_mark_in_reset(vd); 16543af08d82Slm66018 return (0); 16553af08d82Slm66018 } else { 16563af08d82Slm66018 PR0("ldc_mem_dring_release() returned errno %d", 16573af08d82Slm66018 status); 1658d10e4ef2Snarayan return (status); 1659d10e4ef2Snarayan } 16603af08d82Slm66018 } 1661d10e4ef2Snarayan 1662d10e4ef2Snarayan return (accepted ? 0 : EINVAL); 1663d10e4ef2Snarayan } 1664d10e4ef2Snarayan 1665205eeb1aSlm66018 /* 1666205eeb1aSlm66018 * Return Values 1667205eeb1aSlm66018 * 0 - operation completed successfully 1668205eeb1aSlm66018 * EIO - encountered LDC / task error 1669205eeb1aSlm66018 * 1670205eeb1aSlm66018 * Side Effect 1671205eeb1aSlm66018 * sets request->status = <disk operation status> 1672205eeb1aSlm66018 */ 1673205eeb1aSlm66018 static int 1674205eeb1aSlm66018 vd_complete_bio(vd_task_t *task) 1675d10e4ef2Snarayan { 1676d10e4ef2Snarayan int status = 0; 1677205eeb1aSlm66018 int rv = 0; 1678d10e4ef2Snarayan vd_t *vd = task->vd; 1679d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 1680d10e4ef2Snarayan struct buf *buf = &task->buf; 1681d10e4ef2Snarayan 1682d10e4ef2Snarayan 1683d10e4ef2Snarayan ASSERT(vd != NULL); 1684d10e4ef2Snarayan ASSERT(request != NULL); 1685d10e4ef2Snarayan ASSERT(task->msg != NULL); 1686d10e4ef2Snarayan ASSERT(task->msglen >= sizeof (*task->msg)); 16873c96341aSnarayan ASSERT(!vd->file); 1688205eeb1aSlm66018 ASSERT(request->slice != VD_SLICE_NONE); 1689d10e4ef2Snarayan 1690205eeb1aSlm66018 /* Wait for the I/O to complete [ call to ldi_strategy(9f) ] */ 1691d10e4ef2Snarayan request->status = biowait(buf); 1692d10e4ef2Snarayan 16933c96341aSnarayan /* return back the number of bytes read/written */ 16943c96341aSnarayan request->nbytes = buf->b_bcount - buf->b_resid; 16953c96341aSnarayan 16964bac2208Snarayan /* Release the buffer */ 16973af08d82Slm66018 if (!vd->reset_state) 16984bac2208Snarayan status = ldc_mem_release(task->mhdl, 0, buf->b_bcount); 16994bac2208Snarayan if (status) { 17003af08d82Slm66018 PR0("ldc_mem_release() returned errno %d copying to " 17013af08d82Slm66018 "client", status); 17023af08d82Slm66018 if (status == ECONNRESET) { 17033af08d82Slm66018 vd_mark_in_reset(vd); 17043af08d82Slm66018 } 1705205eeb1aSlm66018 rv = EIO; 17061ae08745Sheppo } 17071ae08745Sheppo 17083af08d82Slm66018 /* Unmap the memory, even if in reset */ 17094bac2208Snarayan status = ldc_mem_unmap(task->mhdl); 17104bac2208Snarayan if (status) { 17113af08d82Slm66018 PR0("ldc_mem_unmap() returned errno %d copying to client", 17124bac2208Snarayan status); 17133af08d82Slm66018 if (status == ECONNRESET) { 17143af08d82Slm66018 vd_mark_in_reset(vd); 17153af08d82Slm66018 } 1716205eeb1aSlm66018 rv = EIO; 17174bac2208Snarayan } 17184bac2208Snarayan 1719d10e4ef2Snarayan biofini(buf); 17201ae08745Sheppo 1721205eeb1aSlm66018 return (rv); 1722205eeb1aSlm66018 } 1723205eeb1aSlm66018 1724205eeb1aSlm66018 /* 1725205eeb1aSlm66018 * Description: 1726205eeb1aSlm66018 * This function is called by the two functions called by a taskq 1727205eeb1aSlm66018 * [ vd_complete_notify() and vd_serial_notify()) ] to send the 1728205eeb1aSlm66018 * message to the client. 1729205eeb1aSlm66018 * 1730205eeb1aSlm66018 * Parameters: 1731205eeb1aSlm66018 * arg - opaque pointer to structure containing task to be completed 1732205eeb1aSlm66018 * 1733205eeb1aSlm66018 * Return Values 1734205eeb1aSlm66018 * None 1735205eeb1aSlm66018 */ 1736205eeb1aSlm66018 static void 1737205eeb1aSlm66018 vd_notify(vd_task_t *task) 1738205eeb1aSlm66018 { 1739205eeb1aSlm66018 int status; 1740205eeb1aSlm66018 1741205eeb1aSlm66018 ASSERT(task != NULL); 1742205eeb1aSlm66018 ASSERT(task->vd != NULL); 1743205eeb1aSlm66018 1744205eeb1aSlm66018 if (task->vd->reset_state) 1745205eeb1aSlm66018 return; 1746205eeb1aSlm66018 1747205eeb1aSlm66018 /* 1748205eeb1aSlm66018 * Send the "ack" or "nack" back to the client; if sending the message 1749205eeb1aSlm66018 * via LDC fails, arrange to reset both the connection state and LDC 1750205eeb1aSlm66018 * itself 1751205eeb1aSlm66018 */ 1752205eeb1aSlm66018 PR2("Sending %s", 1753205eeb1aSlm66018 (task->msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK"); 1754205eeb1aSlm66018 1755205eeb1aSlm66018 status = send_msg(task->vd->ldc_handle, task->msg, task->msglen); 1756205eeb1aSlm66018 switch (status) { 1757205eeb1aSlm66018 case 0: 1758205eeb1aSlm66018 break; 1759205eeb1aSlm66018 case ECONNRESET: 1760205eeb1aSlm66018 vd_mark_in_reset(task->vd); 1761205eeb1aSlm66018 break; 1762205eeb1aSlm66018 default: 1763205eeb1aSlm66018 PR0("initiating full reset"); 1764205eeb1aSlm66018 vd_need_reset(task->vd, B_TRUE); 1765205eeb1aSlm66018 break; 1766205eeb1aSlm66018 } 1767205eeb1aSlm66018 1768205eeb1aSlm66018 DTRACE_PROBE1(task__end, vd_task_t *, task); 1769205eeb1aSlm66018 } 1770205eeb1aSlm66018 1771205eeb1aSlm66018 /* 1772205eeb1aSlm66018 * Description: 1773205eeb1aSlm66018 * Mark the Dring entry as Done and (if necessary) send an ACK/NACK to 1774205eeb1aSlm66018 * the vDisk client 1775205eeb1aSlm66018 * 1776205eeb1aSlm66018 * Parameters: 1777205eeb1aSlm66018 * task - structure containing the request sent from client 1778205eeb1aSlm66018 * 1779205eeb1aSlm66018 * Return Values 1780205eeb1aSlm66018 * None 1781205eeb1aSlm66018 */ 1782205eeb1aSlm66018 static void 1783205eeb1aSlm66018 vd_complete_notify(vd_task_t *task) 1784205eeb1aSlm66018 { 1785205eeb1aSlm66018 int status = 0; 1786205eeb1aSlm66018 vd_t *vd = task->vd; 1787205eeb1aSlm66018 vd_dring_payload_t *request = task->request; 1788205eeb1aSlm66018 1789d10e4ef2Snarayan /* Update the dring element for a dring client */ 1790f0ca1d9aSsb155480 if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE_V1_0)) { 17913c96341aSnarayan status = vd_mark_elem_done(vd, task->index, 17923c96341aSnarayan request->status, request->nbytes); 17933af08d82Slm66018 if (status == ECONNRESET) 17943af08d82Slm66018 vd_mark_in_reset(vd); 17953af08d82Slm66018 } 17961ae08745Sheppo 1797d10e4ef2Snarayan /* 1798205eeb1aSlm66018 * If a transport error occurred while marking the element done or 1799205eeb1aSlm66018 * previously while executing the task, arrange to "nack" the message 1800205eeb1aSlm66018 * when the final task in the descriptor element range completes 1801d10e4ef2Snarayan */ 1802205eeb1aSlm66018 if ((status != 0) || (task->status != 0)) 1803d10e4ef2Snarayan task->msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 18041ae08745Sheppo 1805d10e4ef2Snarayan /* 1806d10e4ef2Snarayan * Only the final task for a range of elements will respond to and 1807d10e4ef2Snarayan * free the message 1808d10e4ef2Snarayan */ 18093af08d82Slm66018 if (task->type == VD_NONFINAL_RANGE_TASK) { 1810d10e4ef2Snarayan return; 18113af08d82Slm66018 } 18121ae08745Sheppo 1813205eeb1aSlm66018 vd_notify(task); 1814205eeb1aSlm66018 } 1815205eeb1aSlm66018 1816d10e4ef2Snarayan /* 1817205eeb1aSlm66018 * Description: 1818205eeb1aSlm66018 * This is the basic completion function called to handle inband data 1819205eeb1aSlm66018 * requests and handshake messages. All it needs to do is trigger a 1820205eeb1aSlm66018 * message to the client that the request is completed. 1821205eeb1aSlm66018 * 1822205eeb1aSlm66018 * Parameters: 1823205eeb1aSlm66018 * arg - opaque pointer to structure containing task to be completed 1824205eeb1aSlm66018 * 1825205eeb1aSlm66018 * Return Values 1826205eeb1aSlm66018 * None 1827d10e4ef2Snarayan */ 1828205eeb1aSlm66018 static void 1829205eeb1aSlm66018 vd_serial_notify(void *arg) 1830205eeb1aSlm66018 { 1831205eeb1aSlm66018 vd_task_t *task = (vd_task_t *)arg; 1832205eeb1aSlm66018 1833205eeb1aSlm66018 ASSERT(task != NULL); 1834205eeb1aSlm66018 vd_notify(task); 18351ae08745Sheppo } 18361ae08745Sheppo 18372f5224aeSachartre /* ARGSUSED */ 18382f5224aeSachartre static int 18392f5224aeSachartre vd_geom2dk_geom(void *vd_buf, size_t vd_buf_len, void *ioctl_arg) 18400a55fbb7Slm66018 { 18410a55fbb7Slm66018 VD_GEOM2DK_GEOM((vd_geom_t *)vd_buf, (struct dk_geom *)ioctl_arg); 18422f5224aeSachartre return (0); 18430a55fbb7Slm66018 } 18440a55fbb7Slm66018 18452f5224aeSachartre /* ARGSUSED */ 18462f5224aeSachartre static int 18472f5224aeSachartre vd_vtoc2vtoc(void *vd_buf, size_t vd_buf_len, void *ioctl_arg) 18480a55fbb7Slm66018 { 18490a55fbb7Slm66018 VD_VTOC2VTOC((vd_vtoc_t *)vd_buf, (struct vtoc *)ioctl_arg); 18502f5224aeSachartre return (0); 18510a55fbb7Slm66018 } 18520a55fbb7Slm66018 18530a55fbb7Slm66018 static void 18540a55fbb7Slm66018 dk_geom2vd_geom(void *ioctl_arg, void *vd_buf) 18550a55fbb7Slm66018 { 18560a55fbb7Slm66018 DK_GEOM2VD_GEOM((struct dk_geom *)ioctl_arg, (vd_geom_t *)vd_buf); 18570a55fbb7Slm66018 } 18580a55fbb7Slm66018 18590a55fbb7Slm66018 static void 18600a55fbb7Slm66018 vtoc2vd_vtoc(void *ioctl_arg, void *vd_buf) 18610a55fbb7Slm66018 { 18620a55fbb7Slm66018 VTOC2VD_VTOC((struct vtoc *)ioctl_arg, (vd_vtoc_t *)vd_buf); 18630a55fbb7Slm66018 } 18640a55fbb7Slm66018 18652f5224aeSachartre static int 18662f5224aeSachartre vd_get_efi_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg) 18674bac2208Snarayan { 18684bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 18694bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 18702f5224aeSachartre size_t data_len; 18712f5224aeSachartre 18722f5224aeSachartre data_len = vd_buf_len - (sizeof (vd_efi_t) - sizeof (uint64_t)); 18732f5224aeSachartre if (vd_efi->length > data_len) 18742f5224aeSachartre return (EINVAL); 18754bac2208Snarayan 18764bac2208Snarayan dk_efi->dki_lba = vd_efi->lba; 18774bac2208Snarayan dk_efi->dki_length = vd_efi->length; 18784bac2208Snarayan dk_efi->dki_data = kmem_zalloc(vd_efi->length, KM_SLEEP); 18792f5224aeSachartre return (0); 18804bac2208Snarayan } 18814bac2208Snarayan 18824bac2208Snarayan static void 18834bac2208Snarayan vd_get_efi_out(void *ioctl_arg, void *vd_buf) 18844bac2208Snarayan { 18854bac2208Snarayan int len; 18864bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 18874bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 18884bac2208Snarayan 18894bac2208Snarayan len = vd_efi->length; 18904bac2208Snarayan DK_EFI2VD_EFI(dk_efi, vd_efi); 18914bac2208Snarayan kmem_free(dk_efi->dki_data, len); 18924bac2208Snarayan } 18934bac2208Snarayan 18942f5224aeSachartre static int 18952f5224aeSachartre vd_set_efi_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg) 18964bac2208Snarayan { 18974bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 18984bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 18992f5224aeSachartre size_t data_len; 19002f5224aeSachartre 19012f5224aeSachartre data_len = vd_buf_len - (sizeof (vd_efi_t) - sizeof (uint64_t)); 19022f5224aeSachartre if (vd_efi->length > data_len) 19032f5224aeSachartre return (EINVAL); 19044bac2208Snarayan 19054bac2208Snarayan dk_efi->dki_data = kmem_alloc(vd_efi->length, KM_SLEEP); 19064bac2208Snarayan VD_EFI2DK_EFI(vd_efi, dk_efi); 19072f5224aeSachartre return (0); 19084bac2208Snarayan } 19094bac2208Snarayan 19104bac2208Snarayan static void 19114bac2208Snarayan vd_set_efi_out(void *ioctl_arg, void *vd_buf) 19124bac2208Snarayan { 19134bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 19144bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 19154bac2208Snarayan 19164bac2208Snarayan kmem_free(dk_efi->dki_data, vd_efi->length); 19174bac2208Snarayan } 19184bac2208Snarayan 19192f5224aeSachartre static int 19202f5224aeSachartre vd_scsicmd_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg) 19212f5224aeSachartre { 19222f5224aeSachartre size_t vd_scsi_len; 19232f5224aeSachartre vd_scsi_t *vd_scsi = (vd_scsi_t *)vd_buf; 19242f5224aeSachartre struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl_arg; 19252f5224aeSachartre 19262f5224aeSachartre /* check buffer size */ 19272f5224aeSachartre vd_scsi_len = VD_SCSI_SIZE; 19282f5224aeSachartre vd_scsi_len += P2ROUNDUP(vd_scsi->cdb_len, sizeof (uint64_t)); 19292f5224aeSachartre vd_scsi_len += P2ROUNDUP(vd_scsi->sense_len, sizeof (uint64_t)); 19302f5224aeSachartre vd_scsi_len += P2ROUNDUP(vd_scsi->datain_len, sizeof (uint64_t)); 19312f5224aeSachartre vd_scsi_len += P2ROUNDUP(vd_scsi->dataout_len, sizeof (uint64_t)); 19322f5224aeSachartre 19332f5224aeSachartre ASSERT(vd_scsi_len % sizeof (uint64_t) == 0); 19342f5224aeSachartre 19352f5224aeSachartre if (vd_buf_len < vd_scsi_len) 19362f5224aeSachartre return (EINVAL); 19372f5224aeSachartre 19382f5224aeSachartre /* set flags */ 19392f5224aeSachartre uscsi->uscsi_flags = vd_scsi_debug; 19402f5224aeSachartre 19412f5224aeSachartre if (vd_scsi->options & VD_SCSI_OPT_NORETRY) { 19422f5224aeSachartre uscsi->uscsi_flags |= USCSI_ISOLATE; 19432f5224aeSachartre uscsi->uscsi_flags |= USCSI_DIAGNOSE; 19442f5224aeSachartre } 19452f5224aeSachartre 19462f5224aeSachartre /* task attribute */ 19472f5224aeSachartre switch (vd_scsi->task_attribute) { 19482f5224aeSachartre case VD_SCSI_TASK_ACA: 19492f5224aeSachartre uscsi->uscsi_flags |= USCSI_HEAD; 19502f5224aeSachartre break; 19512f5224aeSachartre case VD_SCSI_TASK_HQUEUE: 19522f5224aeSachartre uscsi->uscsi_flags |= USCSI_HTAG; 19532f5224aeSachartre break; 19542f5224aeSachartre case VD_SCSI_TASK_ORDERED: 19552f5224aeSachartre uscsi->uscsi_flags |= USCSI_OTAG; 19562f5224aeSachartre break; 19572f5224aeSachartre default: 19582f5224aeSachartre uscsi->uscsi_flags |= USCSI_NOTAG; 19592f5224aeSachartre break; 19602f5224aeSachartre } 19612f5224aeSachartre 19622f5224aeSachartre /* timeout */ 19632f5224aeSachartre uscsi->uscsi_timeout = vd_scsi->timeout; 19642f5224aeSachartre 19652f5224aeSachartre /* cdb data */ 19662f5224aeSachartre uscsi->uscsi_cdb = (caddr_t)VD_SCSI_DATA_CDB(vd_scsi); 19672f5224aeSachartre uscsi->uscsi_cdblen = vd_scsi->cdb_len; 19682f5224aeSachartre 19692f5224aeSachartre /* sense buffer */ 19702f5224aeSachartre if (vd_scsi->sense_len != 0) { 19712f5224aeSachartre uscsi->uscsi_flags |= USCSI_RQENABLE; 19722f5224aeSachartre uscsi->uscsi_rqbuf = (caddr_t)VD_SCSI_DATA_SENSE(vd_scsi); 19732f5224aeSachartre uscsi->uscsi_rqlen = vd_scsi->sense_len; 19742f5224aeSachartre } 19752f5224aeSachartre 19762f5224aeSachartre if (vd_scsi->datain_len != 0 && vd_scsi->dataout_len != 0) { 19772f5224aeSachartre /* uscsi does not support read/write request */ 19782f5224aeSachartre return (EINVAL); 19792f5224aeSachartre } 19802f5224aeSachartre 19812f5224aeSachartre /* request data-in */ 19822f5224aeSachartre if (vd_scsi->datain_len != 0) { 19832f5224aeSachartre uscsi->uscsi_flags |= USCSI_READ; 19842f5224aeSachartre uscsi->uscsi_buflen = vd_scsi->datain_len; 19852f5224aeSachartre uscsi->uscsi_bufaddr = (char *)VD_SCSI_DATA_IN(vd_scsi); 19862f5224aeSachartre } 19872f5224aeSachartre 19882f5224aeSachartre /* request data-out */ 19892f5224aeSachartre if (vd_scsi->dataout_len != 0) { 19902f5224aeSachartre uscsi->uscsi_buflen = vd_scsi->dataout_len; 19912f5224aeSachartre uscsi->uscsi_bufaddr = (char *)VD_SCSI_DATA_OUT(vd_scsi); 19922f5224aeSachartre } 19932f5224aeSachartre 19942f5224aeSachartre return (0); 19952f5224aeSachartre } 19962f5224aeSachartre 19972f5224aeSachartre static void 19982f5224aeSachartre vd_scsicmd_out(void *ioctl_arg, void *vd_buf) 19992f5224aeSachartre { 20002f5224aeSachartre vd_scsi_t *vd_scsi = (vd_scsi_t *)vd_buf; 20012f5224aeSachartre struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl_arg; 20022f5224aeSachartre 20032f5224aeSachartre /* output fields */ 20042f5224aeSachartre vd_scsi->cmd_status = uscsi->uscsi_status; 20052f5224aeSachartre 20062f5224aeSachartre /* sense data */ 20072f5224aeSachartre if ((uscsi->uscsi_flags & USCSI_RQENABLE) && 20082f5224aeSachartre (uscsi->uscsi_status == STATUS_CHECK || 20092f5224aeSachartre uscsi->uscsi_status == STATUS_TERMINATED)) { 20102f5224aeSachartre vd_scsi->sense_status = uscsi->uscsi_rqstatus; 20112f5224aeSachartre if (uscsi->uscsi_rqstatus == STATUS_GOOD) 20122f5224aeSachartre vd_scsi->sense_len -= uscsi->uscsi_resid; 20132f5224aeSachartre else 20142f5224aeSachartre vd_scsi->sense_len = 0; 20152f5224aeSachartre } else { 20162f5224aeSachartre vd_scsi->sense_len = 0; 20172f5224aeSachartre } 20182f5224aeSachartre 20192f5224aeSachartre if (uscsi->uscsi_status != STATUS_GOOD) { 20202f5224aeSachartre vd_scsi->dataout_len = 0; 20212f5224aeSachartre vd_scsi->datain_len = 0; 20222f5224aeSachartre return; 20232f5224aeSachartre } 20242f5224aeSachartre 20252f5224aeSachartre if (uscsi->uscsi_flags & USCSI_READ) { 20262f5224aeSachartre /* request data (read) */ 20272f5224aeSachartre vd_scsi->datain_len -= uscsi->uscsi_resid; 20282f5224aeSachartre vd_scsi->dataout_len = 0; 20292f5224aeSachartre } else { 20302f5224aeSachartre /* request data (write) */ 20312f5224aeSachartre vd_scsi->datain_len = 0; 20322f5224aeSachartre vd_scsi->dataout_len -= uscsi->uscsi_resid; 20332f5224aeSachartre } 20342f5224aeSachartre } 20352f5224aeSachartre 2036690555a1Sachartre static ushort_t 20373c96341aSnarayan vd_lbl2cksum(struct dk_label *label) 20383c96341aSnarayan { 20393c96341aSnarayan int count; 2040690555a1Sachartre ushort_t sum, *sp; 20413c96341aSnarayan 20423c96341aSnarayan count = (sizeof (struct dk_label)) / (sizeof (short)) - 1; 2043690555a1Sachartre sp = (ushort_t *)label; 20443c96341aSnarayan sum = 0; 20453c96341aSnarayan while (count--) { 20463c96341aSnarayan sum ^= *sp++; 20473c96341aSnarayan } 20483c96341aSnarayan 20493c96341aSnarayan return (sum); 20503c96341aSnarayan } 20513c96341aSnarayan 205287a7269eSachartre /* 205387a7269eSachartre * Handle ioctls to a disk slice. 2054205eeb1aSlm66018 * 2055205eeb1aSlm66018 * Return Values 2056205eeb1aSlm66018 * 0 - Indicates that there are no errors in disk operations 2057205eeb1aSlm66018 * ENOTSUP - Unknown disk label type or unsupported DKIO ioctl 2058205eeb1aSlm66018 * EINVAL - Not enough room to copy the EFI label 2059205eeb1aSlm66018 * 206087a7269eSachartre */ 20611ae08745Sheppo static int 20620a55fbb7Slm66018 vd_do_slice_ioctl(vd_t *vd, int cmd, void *ioctl_arg) 20631ae08745Sheppo { 20644bac2208Snarayan dk_efi_t *dk_ioc; 2065edcc0754Sachartre int rval; 2066edcc0754Sachartre 2067edcc0754Sachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 2068edcc0754Sachartre 2069edcc0754Sachartre if (cmd == DKIOCFLUSHWRITECACHE) { 2070edcc0754Sachartre if (vd->file) { 2071edcc0754Sachartre return (VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL)); 2072edcc0754Sachartre } else { 2073edcc0754Sachartre return (ldi_ioctl(vd->ldi_handle[0], cmd, 2074edcc0754Sachartre (intptr_t)ioctl_arg, vd->open_flags | FKIOCTL, 2075edcc0754Sachartre kcred, &rval)); 2076edcc0754Sachartre } 2077edcc0754Sachartre } 20784bac2208Snarayan 20794bac2208Snarayan switch (vd->vdisk_label) { 20804bac2208Snarayan 2081edcc0754Sachartre /* ioctls for a single slice disk with a VTOC label */ 20824bac2208Snarayan case VD_DISK_LABEL_VTOC: 20834bac2208Snarayan 20841ae08745Sheppo switch (cmd) { 20851ae08745Sheppo case DKIOCGGEOM: 20860a55fbb7Slm66018 ASSERT(ioctl_arg != NULL); 20870a55fbb7Slm66018 bcopy(&vd->dk_geom, ioctl_arg, sizeof (vd->dk_geom)); 20881ae08745Sheppo return (0); 20891ae08745Sheppo case DKIOCGVTOC: 20900a55fbb7Slm66018 ASSERT(ioctl_arg != NULL); 20910a55fbb7Slm66018 bcopy(&vd->vtoc, ioctl_arg, sizeof (vd->vtoc)); 20921ae08745Sheppo return (0); 209387a7269eSachartre default: 20943c96341aSnarayan return (ENOTSUP); 209587a7269eSachartre } 209687a7269eSachartre 2097edcc0754Sachartre /* ioctls for a single slice disk with an EFI label */ 209887a7269eSachartre case VD_DISK_LABEL_EFI: 209987a7269eSachartre 210087a7269eSachartre switch (cmd) { 210187a7269eSachartre case DKIOCGETEFI: 21023c96341aSnarayan ASSERT(ioctl_arg != NULL); 210387a7269eSachartre dk_ioc = (dk_efi_t *)ioctl_arg; 2104edcc0754Sachartre 2105edcc0754Sachartre /* 2106edcc0754Sachartre * For a single slice disk with an EFI label, we define 2107edcc0754Sachartre * a fake EFI label with the GPT at LBA 1 and one GPE 2108edcc0754Sachartre * at LBA 2. So we return the GPT or the GPE depending 2109edcc0754Sachartre * on which LBA is requested. 2110edcc0754Sachartre */ 2111edcc0754Sachartre if (dk_ioc->dki_lba == 1) { 2112edcc0754Sachartre 2113edcc0754Sachartre /* return the EFI GPT */ 2114edcc0754Sachartre if (dk_ioc->dki_length < sizeof (efi_gpt_t)) 211587a7269eSachartre return (EINVAL); 2116edcc0754Sachartre 2117edcc0754Sachartre bcopy(&vd->efi_gpt, dk_ioc->dki_data, 2118edcc0754Sachartre sizeof (efi_gpt_t)); 2119edcc0754Sachartre 2120edcc0754Sachartre /* also return the GPE if possible */ 2121edcc0754Sachartre if (dk_ioc->dki_length >= sizeof (efi_gpt_t) + 2122edcc0754Sachartre sizeof (efi_gpe_t)) { 2123edcc0754Sachartre bcopy(&vd->efi_gpe, dk_ioc->dki_data + 2124edcc0754Sachartre 1, sizeof (efi_gpe_t)); 2125edcc0754Sachartre } 2126edcc0754Sachartre 2127edcc0754Sachartre } else if (dk_ioc->dki_lba == 2) { 2128edcc0754Sachartre 2129edcc0754Sachartre /* return the EFI GPE */ 2130edcc0754Sachartre if (dk_ioc->dki_length < sizeof (efi_gpe_t)) 2131edcc0754Sachartre return (EINVAL); 2132edcc0754Sachartre 2133edcc0754Sachartre bcopy(&vd->efi_gpe, dk_ioc->dki_data, 2134edcc0754Sachartre sizeof (efi_gpe_t)); 2135edcc0754Sachartre 2136edcc0754Sachartre } else { 2137edcc0754Sachartre return (EINVAL); 2138edcc0754Sachartre } 2139edcc0754Sachartre 214087a7269eSachartre return (0); 214187a7269eSachartre default: 214287a7269eSachartre return (ENOTSUP); 214387a7269eSachartre } 214487a7269eSachartre 214587a7269eSachartre default: 2146205eeb1aSlm66018 /* Unknown disk label type */ 214787a7269eSachartre return (ENOTSUP); 214887a7269eSachartre } 214987a7269eSachartre } 215087a7269eSachartre 2151edcc0754Sachartre static int 2152edcc0754Sachartre vds_efi_alloc_and_read(vd_t *vd, efi_gpt_t **gpt, efi_gpe_t **gpe) 2153edcc0754Sachartre { 2154edcc0754Sachartre vd_efi_dev_t edev; 2155edcc0754Sachartre int status; 2156edcc0754Sachartre 2157edcc0754Sachartre VD_EFI_DEV_SET(edev, vd, (vd_efi_ioctl_func)vd_backend_ioctl); 2158edcc0754Sachartre 2159edcc0754Sachartre status = vd_efi_alloc_and_read(&edev, gpt, gpe); 2160edcc0754Sachartre 2161edcc0754Sachartre return (status); 2162edcc0754Sachartre } 2163edcc0754Sachartre 2164edcc0754Sachartre static void 2165edcc0754Sachartre vds_efi_free(vd_t *vd, efi_gpt_t *gpt, efi_gpe_t *gpe) 2166edcc0754Sachartre { 2167edcc0754Sachartre vd_efi_dev_t edev; 2168edcc0754Sachartre 2169edcc0754Sachartre VD_EFI_DEV_SET(edev, vd, (vd_efi_ioctl_func)vd_backend_ioctl); 2170edcc0754Sachartre 2171edcc0754Sachartre vd_efi_free(&edev, gpt, gpe); 2172edcc0754Sachartre } 2173edcc0754Sachartre 2174edcc0754Sachartre static int 2175edcc0754Sachartre vd_file_validate_efi(vd_t *vd) 2176edcc0754Sachartre { 2177edcc0754Sachartre efi_gpt_t *gpt; 2178edcc0754Sachartre efi_gpe_t *gpe; 2179edcc0754Sachartre int i, nparts, status; 2180edcc0754Sachartre struct uuid efi_reserved = EFI_RESERVED; 2181edcc0754Sachartre 2182edcc0754Sachartre if ((status = vds_efi_alloc_and_read(vd, &gpt, &gpe)) != 0) 2183edcc0754Sachartre return (status); 2184edcc0754Sachartre 2185edcc0754Sachartre bzero(&vd->vtoc, sizeof (struct vtoc)); 2186edcc0754Sachartre bzero(&vd->dk_geom, sizeof (struct dk_geom)); 2187edcc0754Sachartre bzero(vd->slices, sizeof (vd_slice_t) * VD_MAXPART); 2188edcc0754Sachartre 2189edcc0754Sachartre vd->efi_reserved = -1; 2190edcc0754Sachartre 2191edcc0754Sachartre nparts = gpt->efi_gpt_NumberOfPartitionEntries; 2192edcc0754Sachartre 2193edcc0754Sachartre for (i = 0; i < nparts && i < VD_MAXPART; i++) { 2194edcc0754Sachartre 2195edcc0754Sachartre if (gpe[i].efi_gpe_StartingLBA == 0 || 2196edcc0754Sachartre gpe[i].efi_gpe_EndingLBA == 0) { 2197edcc0754Sachartre continue; 2198edcc0754Sachartre } 2199edcc0754Sachartre 2200edcc0754Sachartre vd->slices[i].start = gpe[i].efi_gpe_StartingLBA; 2201edcc0754Sachartre vd->slices[i].nblocks = gpe[i].efi_gpe_EndingLBA - 2202edcc0754Sachartre gpe[i].efi_gpe_StartingLBA + 1; 2203edcc0754Sachartre 2204edcc0754Sachartre if (bcmp(&gpe[i].efi_gpe_PartitionTypeGUID, &efi_reserved, 2205edcc0754Sachartre sizeof (struct uuid)) == 0) 2206edcc0754Sachartre vd->efi_reserved = i; 2207edcc0754Sachartre 2208edcc0754Sachartre } 2209edcc0754Sachartre 2210edcc0754Sachartre ASSERT(vd->vdisk_size != 0); 2211edcc0754Sachartre vd->slices[VD_EFI_WD_SLICE].start = 0; 2212edcc0754Sachartre vd->slices[VD_EFI_WD_SLICE].nblocks = vd->vdisk_size; 2213edcc0754Sachartre 2214edcc0754Sachartre vds_efi_free(vd, gpt, gpe); 2215edcc0754Sachartre 2216edcc0754Sachartre return (status); 2217edcc0754Sachartre } 2218edcc0754Sachartre 221987a7269eSachartre /* 222078fcd0a1Sachartre * Function: 222178fcd0a1Sachartre * vd_file_validate_geometry 2222205eeb1aSlm66018 * 222378fcd0a1Sachartre * Description: 222478fcd0a1Sachartre * Read the label and validate the geometry of a disk image. The driver 222578fcd0a1Sachartre * label, vtoc and geometry information are updated according to the 222678fcd0a1Sachartre * label read from the disk image. 222778fcd0a1Sachartre * 222878fcd0a1Sachartre * If no valid label is found, the label is set to unknown and the 222978fcd0a1Sachartre * function returns EINVAL, but a default vtoc and geometry are provided 2230edcc0754Sachartre * to the driver. If an EFI label is found, ENOTSUP is returned. 223178fcd0a1Sachartre * 223278fcd0a1Sachartre * Parameters: 223378fcd0a1Sachartre * vd - disk on which the operation is performed. 223478fcd0a1Sachartre * 223578fcd0a1Sachartre * Return Code: 223678fcd0a1Sachartre * 0 - success. 223778fcd0a1Sachartre * EIO - error reading the label from the disk image. 223878fcd0a1Sachartre * EINVAL - unknown disk label. 2239edcc0754Sachartre * ENOTSUP - geometry not applicable (EFI label). 224087a7269eSachartre */ 224187a7269eSachartre static int 224278fcd0a1Sachartre vd_file_validate_geometry(vd_t *vd) 224387a7269eSachartre { 224487a7269eSachartre struct dk_label label; 224578fcd0a1Sachartre struct dk_geom *geom = &vd->dk_geom; 224678fcd0a1Sachartre struct vtoc *vtoc = &vd->vtoc; 224778fcd0a1Sachartre int i; 224878fcd0a1Sachartre int status = 0; 224987a7269eSachartre 225087a7269eSachartre ASSERT(vd->file); 2251edcc0754Sachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK); 225287a7269eSachartre 225387a7269eSachartre if (VD_FILE_LABEL_READ(vd, &label) < 0) 225487a7269eSachartre return (EIO); 225587a7269eSachartre 225687a7269eSachartre if (label.dkl_magic != DKL_MAGIC || 225778fcd0a1Sachartre label.dkl_cksum != vd_lbl2cksum(&label) || 225866cfcfbeSachartre (vd_file_validate_sanity && label.dkl_vtoc.v_sanity != VTOC_SANE) || 225978fcd0a1Sachartre label.dkl_vtoc.v_nparts != V_NUMPAR) { 2260edcc0754Sachartre 2261edcc0754Sachartre if (vd_file_validate_efi(vd) == 0) { 2262edcc0754Sachartre vd->vdisk_label = VD_DISK_LABEL_EFI; 2263edcc0754Sachartre return (ENOTSUP); 2264edcc0754Sachartre } 2265edcc0754Sachartre 226678fcd0a1Sachartre vd->vdisk_label = VD_DISK_LABEL_UNK; 226778fcd0a1Sachartre vd_file_build_default_label(vd, &label); 226878fcd0a1Sachartre status = EINVAL; 226978fcd0a1Sachartre } else { 227078fcd0a1Sachartre vd->vdisk_label = VD_DISK_LABEL_VTOC; 227178fcd0a1Sachartre } 227287a7269eSachartre 227378fcd0a1Sachartre /* Update the driver geometry */ 227487a7269eSachartre bzero(geom, sizeof (struct dk_geom)); 227578fcd0a1Sachartre 227687a7269eSachartre geom->dkg_ncyl = label.dkl_ncyl; 227787a7269eSachartre geom->dkg_acyl = label.dkl_acyl; 227887a7269eSachartre geom->dkg_nhead = label.dkl_nhead; 227987a7269eSachartre geom->dkg_nsect = label.dkl_nsect; 228087a7269eSachartre geom->dkg_intrlv = label.dkl_intrlv; 228187a7269eSachartre geom->dkg_apc = label.dkl_apc; 228287a7269eSachartre geom->dkg_rpm = label.dkl_rpm; 228387a7269eSachartre geom->dkg_pcyl = label.dkl_pcyl; 228487a7269eSachartre geom->dkg_write_reinstruct = label.dkl_write_reinstruct; 228587a7269eSachartre geom->dkg_read_reinstruct = label.dkl_read_reinstruct; 228687a7269eSachartre 228778fcd0a1Sachartre /* Update the driver vtoc */ 228887a7269eSachartre bzero(vtoc, sizeof (struct vtoc)); 228987a7269eSachartre 229087a7269eSachartre vtoc->v_sanity = label.dkl_vtoc.v_sanity; 229187a7269eSachartre vtoc->v_version = label.dkl_vtoc.v_version; 229287a7269eSachartre vtoc->v_sectorsz = DEV_BSIZE; 229387a7269eSachartre vtoc->v_nparts = label.dkl_vtoc.v_nparts; 229487a7269eSachartre 229587a7269eSachartre for (i = 0; i < vtoc->v_nparts; i++) { 229687a7269eSachartre vtoc->v_part[i].p_tag = 229787a7269eSachartre label.dkl_vtoc.v_part[i].p_tag; 229887a7269eSachartre vtoc->v_part[i].p_flag = 229987a7269eSachartre label.dkl_vtoc.v_part[i].p_flag; 230087a7269eSachartre vtoc->v_part[i].p_start = 230187a7269eSachartre label.dkl_map[i].dkl_cylno * 230287a7269eSachartre (label.dkl_nhead * label.dkl_nsect); 230387a7269eSachartre vtoc->v_part[i].p_size = label.dkl_map[i].dkl_nblk; 230487a7269eSachartre vtoc->timestamp[i] = 230587a7269eSachartre label.dkl_vtoc.v_timestamp[i]; 230687a7269eSachartre } 230787a7269eSachartre /* 230887a7269eSachartre * The bootinfo array can not be copied with bcopy() because 230987a7269eSachartre * elements are of type long in vtoc (so 64-bit) and of type 231087a7269eSachartre * int in dk_vtoc (so 32-bit). 231187a7269eSachartre */ 231287a7269eSachartre vtoc->v_bootinfo[0] = label.dkl_vtoc.v_bootinfo[0]; 231387a7269eSachartre vtoc->v_bootinfo[1] = label.dkl_vtoc.v_bootinfo[1]; 231487a7269eSachartre vtoc->v_bootinfo[2] = label.dkl_vtoc.v_bootinfo[2]; 231587a7269eSachartre bcopy(label.dkl_asciilabel, vtoc->v_asciilabel, 231687a7269eSachartre LEN_DKL_ASCII); 231787a7269eSachartre bcopy(label.dkl_vtoc.v_volume, vtoc->v_volume, 231887a7269eSachartre LEN_DKL_VVOL); 231987a7269eSachartre 2320edcc0754Sachartre /* Update logical partitions */ 2321edcc0754Sachartre bzero(vd->slices, sizeof (vd_slice_t) * VD_MAXPART); 2322edcc0754Sachartre if (vd->vdisk_label != VD_DISK_LABEL_UNK) { 2323edcc0754Sachartre for (i = 0; i < vtoc->v_nparts; i++) { 2324edcc0754Sachartre vd->slices[i].start = vtoc->v_part[i].p_start; 2325edcc0754Sachartre vd->slices[i].nblocks = vtoc->v_part[i].p_size; 2326edcc0754Sachartre } 2327edcc0754Sachartre } 2328edcc0754Sachartre 232978fcd0a1Sachartre return (status); 233078fcd0a1Sachartre } 233178fcd0a1Sachartre 233278fcd0a1Sachartre /* 233378fcd0a1Sachartre * Handle ioctls to a disk image (file-based). 233478fcd0a1Sachartre * 233578fcd0a1Sachartre * Return Values 233678fcd0a1Sachartre * 0 - Indicates that there are no errors 233778fcd0a1Sachartre * != 0 - Disk operation returned an error 233878fcd0a1Sachartre */ 233978fcd0a1Sachartre static int 234078fcd0a1Sachartre vd_do_file_ioctl(vd_t *vd, int cmd, void *ioctl_arg) 234178fcd0a1Sachartre { 234278fcd0a1Sachartre struct dk_label label; 234378fcd0a1Sachartre struct dk_geom *geom; 234478fcd0a1Sachartre struct vtoc *vtoc; 2345edcc0754Sachartre dk_efi_t *efi; 234678fcd0a1Sachartre int i, rc; 234778fcd0a1Sachartre 234878fcd0a1Sachartre ASSERT(vd->file); 2349edcc0754Sachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK); 235078fcd0a1Sachartre 235178fcd0a1Sachartre switch (cmd) { 235278fcd0a1Sachartre 235378fcd0a1Sachartre case DKIOCGGEOM: 235478fcd0a1Sachartre ASSERT(ioctl_arg != NULL); 235578fcd0a1Sachartre geom = (struct dk_geom *)ioctl_arg; 235678fcd0a1Sachartre 235778fcd0a1Sachartre rc = vd_file_validate_geometry(vd); 2358edcc0754Sachartre if (rc != 0 && rc != EINVAL) 235978fcd0a1Sachartre return (rc); 236078fcd0a1Sachartre bcopy(&vd->dk_geom, geom, sizeof (struct dk_geom)); 236178fcd0a1Sachartre return (0); 236278fcd0a1Sachartre 236378fcd0a1Sachartre case DKIOCGVTOC: 236478fcd0a1Sachartre ASSERT(ioctl_arg != NULL); 236578fcd0a1Sachartre vtoc = (struct vtoc *)ioctl_arg; 236678fcd0a1Sachartre 236778fcd0a1Sachartre rc = vd_file_validate_geometry(vd); 2368edcc0754Sachartre if (rc != 0 && rc != EINVAL) 236978fcd0a1Sachartre return (rc); 237078fcd0a1Sachartre bcopy(&vd->vtoc, vtoc, sizeof (struct vtoc)); 237187a7269eSachartre return (0); 237287a7269eSachartre 237387a7269eSachartre case DKIOCSGEOM: 237487a7269eSachartre ASSERT(ioctl_arg != NULL); 237587a7269eSachartre geom = (struct dk_geom *)ioctl_arg; 237687a7269eSachartre 237787a7269eSachartre if (geom->dkg_nhead == 0 || geom->dkg_nsect == 0) 237887a7269eSachartre return (EINVAL); 237987a7269eSachartre 238087a7269eSachartre /* 238187a7269eSachartre * The current device geometry is not updated, just the driver 238287a7269eSachartre * "notion" of it. The device geometry will be effectively 238387a7269eSachartre * updated when a label is written to the device during a next 238487a7269eSachartre * DKIOCSVTOC. 238587a7269eSachartre */ 238687a7269eSachartre bcopy(ioctl_arg, &vd->dk_geom, sizeof (vd->dk_geom)); 238787a7269eSachartre return (0); 238887a7269eSachartre 238987a7269eSachartre case DKIOCSVTOC: 239087a7269eSachartre ASSERT(ioctl_arg != NULL); 239187a7269eSachartre ASSERT(vd->dk_geom.dkg_nhead != 0 && 239287a7269eSachartre vd->dk_geom.dkg_nsect != 0); 2393690555a1Sachartre vtoc = (struct vtoc *)ioctl_arg; 2394690555a1Sachartre 2395690555a1Sachartre if (vtoc->v_sanity != VTOC_SANE || 2396690555a1Sachartre vtoc->v_sectorsz != DEV_BSIZE || 2397690555a1Sachartre vtoc->v_nparts != V_NUMPAR) 2398690555a1Sachartre return (EINVAL); 2399690555a1Sachartre 2400690555a1Sachartre bzero(&label, sizeof (label)); 2401690555a1Sachartre label.dkl_ncyl = vd->dk_geom.dkg_ncyl; 2402690555a1Sachartre label.dkl_acyl = vd->dk_geom.dkg_acyl; 2403690555a1Sachartre label.dkl_pcyl = vd->dk_geom.dkg_pcyl; 2404690555a1Sachartre label.dkl_nhead = vd->dk_geom.dkg_nhead; 2405690555a1Sachartre label.dkl_nsect = vd->dk_geom.dkg_nsect; 2406690555a1Sachartre label.dkl_intrlv = vd->dk_geom.dkg_intrlv; 2407690555a1Sachartre label.dkl_apc = vd->dk_geom.dkg_apc; 2408690555a1Sachartre label.dkl_rpm = vd->dk_geom.dkg_rpm; 240987a7269eSachartre label.dkl_write_reinstruct = vd->dk_geom.dkg_write_reinstruct; 241087a7269eSachartre label.dkl_read_reinstruct = vd->dk_geom.dkg_read_reinstruct; 2411690555a1Sachartre 241287a7269eSachartre label.dkl_vtoc.v_nparts = V_NUMPAR; 241387a7269eSachartre label.dkl_vtoc.v_sanity = VTOC_SANE; 2414690555a1Sachartre label.dkl_vtoc.v_version = vtoc->v_version; 241587a7269eSachartre for (i = 0; i < V_NUMPAR; i++) { 2416690555a1Sachartre label.dkl_vtoc.v_timestamp[i] = 2417690555a1Sachartre vtoc->timestamp[i]; 2418690555a1Sachartre label.dkl_vtoc.v_part[i].p_tag = 2419690555a1Sachartre vtoc->v_part[i].p_tag; 2420690555a1Sachartre label.dkl_vtoc.v_part[i].p_flag = 2421690555a1Sachartre vtoc->v_part[i].p_flag; 2422690555a1Sachartre label.dkl_map[i].dkl_cylno = 2423690555a1Sachartre vtoc->v_part[i].p_start / 2424690555a1Sachartre (label.dkl_nhead * label.dkl_nsect); 2425690555a1Sachartre label.dkl_map[i].dkl_nblk = 2426690555a1Sachartre vtoc->v_part[i].p_size; 24273c96341aSnarayan } 242887a7269eSachartre /* 242987a7269eSachartre * The bootinfo array can not be copied with bcopy() because 243087a7269eSachartre * elements are of type long in vtoc (so 64-bit) and of type 243187a7269eSachartre * int in dk_vtoc (so 32-bit). 243287a7269eSachartre */ 243387a7269eSachartre label.dkl_vtoc.v_bootinfo[0] = vtoc->v_bootinfo[0]; 243487a7269eSachartre label.dkl_vtoc.v_bootinfo[1] = vtoc->v_bootinfo[1]; 243587a7269eSachartre label.dkl_vtoc.v_bootinfo[2] = vtoc->v_bootinfo[2]; 2436690555a1Sachartre bcopy(vtoc->v_asciilabel, label.dkl_asciilabel, 2437690555a1Sachartre LEN_DKL_ASCII); 2438690555a1Sachartre bcopy(vtoc->v_volume, label.dkl_vtoc.v_volume, 2439690555a1Sachartre LEN_DKL_VVOL); 24403c96341aSnarayan 24413c96341aSnarayan /* re-compute checksum */ 2442690555a1Sachartre label.dkl_magic = DKL_MAGIC; 2443690555a1Sachartre label.dkl_cksum = vd_lbl2cksum(&label); 2444690555a1Sachartre 244587a7269eSachartre /* write label to the disk image */ 244687a7269eSachartre if ((rc = vd_file_set_vtoc(vd, &label)) != 0) 244787a7269eSachartre return (rc); 2448690555a1Sachartre 2449edcc0754Sachartre break; 2450edcc0754Sachartre 2451edcc0754Sachartre case DKIOCFLUSHWRITECACHE: 2452edcc0754Sachartre return (VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL)); 2453edcc0754Sachartre 2454edcc0754Sachartre case DKIOCGETEFI: 2455edcc0754Sachartre ASSERT(ioctl_arg != NULL); 2456edcc0754Sachartre efi = (dk_efi_t *)ioctl_arg; 2457edcc0754Sachartre 2458edcc0754Sachartre if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, 2459edcc0754Sachartre (caddr_t)efi->dki_data, efi->dki_lba, efi->dki_length) < 0) 2460edcc0754Sachartre return (EIO); 2461edcc0754Sachartre 2462edcc0754Sachartre return (0); 2463edcc0754Sachartre 2464edcc0754Sachartre case DKIOCSETEFI: 2465edcc0754Sachartre ASSERT(ioctl_arg != NULL); 2466edcc0754Sachartre efi = (dk_efi_t *)ioctl_arg; 2467edcc0754Sachartre 2468edcc0754Sachartre if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, 2469edcc0754Sachartre (caddr_t)efi->dki_data, efi->dki_lba, efi->dki_length) < 0) 2470edcc0754Sachartre return (EIO); 2471edcc0754Sachartre 2472edcc0754Sachartre break; 2473edcc0754Sachartre 2474edcc0754Sachartre 2475edcc0754Sachartre default: 2476edcc0754Sachartre return (ENOTSUP); 2477edcc0754Sachartre } 2478edcc0754Sachartre 2479edcc0754Sachartre ASSERT(cmd == DKIOCSVTOC || cmd == DKIOCSETEFI); 2480edcc0754Sachartre 2481edcc0754Sachartre /* label has changed, revalidate the geometry */ 2482edcc0754Sachartre (void) vd_file_validate_geometry(vd); 24833c96341aSnarayan 248487a7269eSachartre /* 248587a7269eSachartre * The disk geometry may have changed, so we need to write 248687a7269eSachartre * the devid (if there is one) so that it is stored at the 248787a7269eSachartre * right location. 248887a7269eSachartre */ 2489edcc0754Sachartre if (vd_file_write_devid(vd, vd->file_devid) != 0) { 249087a7269eSachartre PR0("Fail to write devid"); 24911ae08745Sheppo } 24924bac2208Snarayan 24934bac2208Snarayan return (0); 24944bac2208Snarayan } 2495edcc0754Sachartre 2496edcc0754Sachartre static int 2497edcc0754Sachartre vd_backend_ioctl(vd_t *vd, int cmd, caddr_t arg) 2498edcc0754Sachartre { 2499edcc0754Sachartre int rval = 0, status; 2500edcc0754Sachartre 2501edcc0754Sachartre /* 2502edcc0754Sachartre * Call the appropriate function to execute the ioctl depending 2503edcc0754Sachartre * on the type of vdisk. 2504edcc0754Sachartre */ 2505edcc0754Sachartre if (vd->vdisk_type == VD_DISK_TYPE_SLICE) { 2506edcc0754Sachartre 2507edcc0754Sachartre /* slice, file or volume exported as a single slice disk */ 2508edcc0754Sachartre status = vd_do_slice_ioctl(vd, cmd, arg); 2509edcc0754Sachartre 2510edcc0754Sachartre } else if (vd->file) { 2511edcc0754Sachartre 2512edcc0754Sachartre /* file or volume exported as a full disk */ 2513edcc0754Sachartre status = vd_do_file_ioctl(vd, cmd, arg); 2514edcc0754Sachartre 2515edcc0754Sachartre } else { 2516edcc0754Sachartre 2517edcc0754Sachartre /* disk device exported as a full disk */ 2518edcc0754Sachartre status = ldi_ioctl(vd->ldi_handle[0], cmd, (intptr_t)arg, 2519edcc0754Sachartre vd->open_flags | FKIOCTL, kcred, &rval); 2520edcc0754Sachartre } 2521edcc0754Sachartre 2522edcc0754Sachartre #ifdef DEBUG 2523edcc0754Sachartre if (rval != 0) { 2524edcc0754Sachartre PR0("ioctl %x set rval = %d, which is not being returned" 2525edcc0754Sachartre " to caller", cmd, rval); 2526edcc0754Sachartre } 2527edcc0754Sachartre #endif /* DEBUG */ 2528edcc0754Sachartre 2529edcc0754Sachartre return (status); 25301ae08745Sheppo } 25311ae08745Sheppo 2532205eeb1aSlm66018 /* 2533205eeb1aSlm66018 * Description: 2534205eeb1aSlm66018 * This is the function that processes the ioctl requests (farming it 2535205eeb1aSlm66018 * out to functions that handle slices, files or whole disks) 2536205eeb1aSlm66018 * 2537205eeb1aSlm66018 * Return Values 2538205eeb1aSlm66018 * 0 - ioctl operation completed successfully 2539205eeb1aSlm66018 * != 0 - The LDC error value encountered 2540205eeb1aSlm66018 * (propagated back up the call stack as a task error) 2541205eeb1aSlm66018 * 2542205eeb1aSlm66018 * Side Effect 2543205eeb1aSlm66018 * sets request->status to the return value of the ioctl function. 2544205eeb1aSlm66018 */ 25451ae08745Sheppo static int 25460a55fbb7Slm66018 vd_do_ioctl(vd_t *vd, vd_dring_payload_t *request, void* buf, vd_ioctl_t *ioctl) 25471ae08745Sheppo { 2548edcc0754Sachartre int status = 0; 25491ae08745Sheppo size_t nbytes = request->nbytes; /* modifiable copy */ 25501ae08745Sheppo 25511ae08745Sheppo 25521ae08745Sheppo ASSERT(request->slice < vd->nslices); 25531ae08745Sheppo PR0("Performing %s", ioctl->operation_name); 25541ae08745Sheppo 25550a55fbb7Slm66018 /* Get data from client and convert, if necessary */ 25560a55fbb7Slm66018 if (ioctl->copyin != NULL) { 25571ae08745Sheppo ASSERT(nbytes != 0 && buf != NULL); 25581ae08745Sheppo PR1("Getting \"arg\" data from client"); 25591ae08745Sheppo if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes, 25601ae08745Sheppo request->cookie, request->ncookies, 25611ae08745Sheppo LDC_COPY_IN)) != 0) { 25623af08d82Slm66018 PR0("ldc_mem_copy() returned errno %d " 25631ae08745Sheppo "copying from client", status); 25641ae08745Sheppo return (status); 25651ae08745Sheppo } 25660a55fbb7Slm66018 25670a55fbb7Slm66018 /* Convert client's data, if necessary */ 25682f5224aeSachartre if (ioctl->copyin == VD_IDENTITY_IN) { 25692f5224aeSachartre /* use client buffer */ 25700a55fbb7Slm66018 ioctl->arg = buf; 25712f5224aeSachartre } else { 25722f5224aeSachartre /* convert client vdisk operation data to ioctl data */ 25732f5224aeSachartre status = (ioctl->copyin)(buf, nbytes, 25742f5224aeSachartre (void *)ioctl->arg); 25752f5224aeSachartre if (status != 0) { 25762f5224aeSachartre request->status = status; 25772f5224aeSachartre return (0); 25782f5224aeSachartre } 25792f5224aeSachartre } 25802f5224aeSachartre } 25812f5224aeSachartre 25822f5224aeSachartre if (ioctl->operation == VD_OP_SCSICMD) { 25832f5224aeSachartre struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl->arg; 25842f5224aeSachartre 25852f5224aeSachartre /* check write permission */ 25862f5224aeSachartre if (!(vd->open_flags & FWRITE) && 25872f5224aeSachartre !(uscsi->uscsi_flags & USCSI_READ)) { 25882f5224aeSachartre PR0("uscsi fails because backend is opened read-only"); 25892f5224aeSachartre request->status = EROFS; 25902f5224aeSachartre return (0); 25912f5224aeSachartre } 25921ae08745Sheppo } 25931ae08745Sheppo 25941ae08745Sheppo /* 2595edcc0754Sachartre * Send the ioctl to the disk backend. 25961ae08745Sheppo */ 2597edcc0754Sachartre request->status = vd_backend_ioctl(vd, ioctl->cmd, ioctl->arg); 2598205eeb1aSlm66018 2599205eeb1aSlm66018 if (request->status != 0) { 2600205eeb1aSlm66018 PR0("ioctl(%s) = errno %d", ioctl->cmd_name, request->status); 26012f5224aeSachartre if (ioctl->operation == VD_OP_SCSICMD && 26022f5224aeSachartre ((struct uscsi_cmd *)ioctl->arg)->uscsi_status != 0) 26032f5224aeSachartre /* 26042f5224aeSachartre * USCSICMD has reported an error and the uscsi_status 26052f5224aeSachartre * field is not zero. This means that the SCSI command 26062f5224aeSachartre * has completed but it has an error. So we should 26072f5224aeSachartre * mark the VD operation has succesfully completed 26082f5224aeSachartre * and clients can check the SCSI status field for 26092f5224aeSachartre * SCSI errors. 26102f5224aeSachartre */ 26112f5224aeSachartre request->status = 0; 26122f5224aeSachartre else 2613205eeb1aSlm66018 return (0); 2614205eeb1aSlm66018 } 26151ae08745Sheppo 26160a55fbb7Slm66018 /* Convert data and send to client, if necessary */ 26170a55fbb7Slm66018 if (ioctl->copyout != NULL) { 26181ae08745Sheppo ASSERT(nbytes != 0 && buf != NULL); 26191ae08745Sheppo PR1("Sending \"arg\" data to client"); 26200a55fbb7Slm66018 26210a55fbb7Slm66018 /* Convert ioctl data to vdisk operation data, if necessary */ 26222f5224aeSachartre if (ioctl->copyout != VD_IDENTITY_OUT) 26230a55fbb7Slm66018 (ioctl->copyout)((void *)ioctl->arg, buf); 26240a55fbb7Slm66018 26251ae08745Sheppo if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes, 26261ae08745Sheppo request->cookie, request->ncookies, 26271ae08745Sheppo LDC_COPY_OUT)) != 0) { 26283af08d82Slm66018 PR0("ldc_mem_copy() returned errno %d " 26291ae08745Sheppo "copying to client", status); 26301ae08745Sheppo return (status); 26311ae08745Sheppo } 26321ae08745Sheppo } 26331ae08745Sheppo 26341ae08745Sheppo return (status); 26351ae08745Sheppo } 26361ae08745Sheppo 26371ae08745Sheppo #define RNDSIZE(expr) P2ROUNDUP(sizeof (expr), sizeof (uint64_t)) 2638205eeb1aSlm66018 2639205eeb1aSlm66018 /* 2640205eeb1aSlm66018 * Description: 2641205eeb1aSlm66018 * This generic function is called by the task queue to complete 2642205eeb1aSlm66018 * the processing of the tasks. The specific completion function 2643205eeb1aSlm66018 * is passed in as a field in the task pointer. 2644205eeb1aSlm66018 * 2645205eeb1aSlm66018 * Parameters: 2646205eeb1aSlm66018 * arg - opaque pointer to structure containing task to be completed 2647205eeb1aSlm66018 * 2648205eeb1aSlm66018 * Return Values 2649205eeb1aSlm66018 * None 2650205eeb1aSlm66018 */ 2651205eeb1aSlm66018 static void 2652205eeb1aSlm66018 vd_complete(void *arg) 2653205eeb1aSlm66018 { 2654205eeb1aSlm66018 vd_task_t *task = (vd_task_t *)arg; 2655205eeb1aSlm66018 2656205eeb1aSlm66018 ASSERT(task != NULL); 2657205eeb1aSlm66018 ASSERT(task->status == EINPROGRESS); 2658205eeb1aSlm66018 ASSERT(task->completef != NULL); 2659205eeb1aSlm66018 2660205eeb1aSlm66018 task->status = task->completef(task); 2661205eeb1aSlm66018 if (task->status) 2662205eeb1aSlm66018 PR0("%s: Error %d completing task", __func__, task->status); 2663205eeb1aSlm66018 2664205eeb1aSlm66018 /* Now notify the vDisk client */ 2665205eeb1aSlm66018 vd_complete_notify(task); 2666205eeb1aSlm66018 } 2667205eeb1aSlm66018 26681ae08745Sheppo static int 2669d10e4ef2Snarayan vd_ioctl(vd_task_t *task) 26701ae08745Sheppo { 267187a7269eSachartre int i, status; 26721ae08745Sheppo void *buf = NULL; 26730a55fbb7Slm66018 struct dk_geom dk_geom = {0}; 26740a55fbb7Slm66018 struct vtoc vtoc = {0}; 26754bac2208Snarayan struct dk_efi dk_efi = {0}; 26762f5224aeSachartre struct uscsi_cmd uscsi = {0}; 2677d10e4ef2Snarayan vd_t *vd = task->vd; 2678d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 26790a55fbb7Slm66018 vd_ioctl_t ioctl[] = { 26800a55fbb7Slm66018 /* Command (no-copy) operations */ 26810a55fbb7Slm66018 {VD_OP_FLUSH, STRINGIZE(VD_OP_FLUSH), 0, 26820a55fbb7Slm66018 DKIOCFLUSHWRITECACHE, STRINGIZE(DKIOCFLUSHWRITECACHE), 2683047ba61eSachartre NULL, NULL, NULL, B_TRUE}, 26840a55fbb7Slm66018 26850a55fbb7Slm66018 /* "Get" (copy-out) operations */ 26860a55fbb7Slm66018 {VD_OP_GET_WCE, STRINGIZE(VD_OP_GET_WCE), RNDSIZE(int), 26870a55fbb7Slm66018 DKIOCGETWCE, STRINGIZE(DKIOCGETWCE), 26882f5224aeSachartre NULL, VD_IDENTITY_IN, VD_IDENTITY_OUT, B_FALSE}, 26890a55fbb7Slm66018 {VD_OP_GET_DISKGEOM, STRINGIZE(VD_OP_GET_DISKGEOM), 26900a55fbb7Slm66018 RNDSIZE(vd_geom_t), 26910a55fbb7Slm66018 DKIOCGGEOM, STRINGIZE(DKIOCGGEOM), 2692047ba61eSachartre &dk_geom, NULL, dk_geom2vd_geom, B_FALSE}, 26930a55fbb7Slm66018 {VD_OP_GET_VTOC, STRINGIZE(VD_OP_GET_VTOC), RNDSIZE(vd_vtoc_t), 26940a55fbb7Slm66018 DKIOCGVTOC, STRINGIZE(DKIOCGVTOC), 2695047ba61eSachartre &vtoc, NULL, vtoc2vd_vtoc, B_FALSE}, 26964bac2208Snarayan {VD_OP_GET_EFI, STRINGIZE(VD_OP_GET_EFI), RNDSIZE(vd_efi_t), 26974bac2208Snarayan DKIOCGETEFI, STRINGIZE(DKIOCGETEFI), 2698047ba61eSachartre &dk_efi, vd_get_efi_in, vd_get_efi_out, B_FALSE}, 26990a55fbb7Slm66018 27000a55fbb7Slm66018 /* "Set" (copy-in) operations */ 27010a55fbb7Slm66018 {VD_OP_SET_WCE, STRINGIZE(VD_OP_SET_WCE), RNDSIZE(int), 27020a55fbb7Slm66018 DKIOCSETWCE, STRINGIZE(DKIOCSETWCE), 27032f5224aeSachartre NULL, VD_IDENTITY_IN, VD_IDENTITY_OUT, B_TRUE}, 27040a55fbb7Slm66018 {VD_OP_SET_DISKGEOM, STRINGIZE(VD_OP_SET_DISKGEOM), 27050a55fbb7Slm66018 RNDSIZE(vd_geom_t), 27060a55fbb7Slm66018 DKIOCSGEOM, STRINGIZE(DKIOCSGEOM), 2707047ba61eSachartre &dk_geom, vd_geom2dk_geom, NULL, B_TRUE}, 27080a55fbb7Slm66018 {VD_OP_SET_VTOC, STRINGIZE(VD_OP_SET_VTOC), RNDSIZE(vd_vtoc_t), 27090a55fbb7Slm66018 DKIOCSVTOC, STRINGIZE(DKIOCSVTOC), 2710047ba61eSachartre &vtoc, vd_vtoc2vtoc, NULL, B_TRUE}, 27114bac2208Snarayan {VD_OP_SET_EFI, STRINGIZE(VD_OP_SET_EFI), RNDSIZE(vd_efi_t), 27124bac2208Snarayan DKIOCSETEFI, STRINGIZE(DKIOCSETEFI), 2713047ba61eSachartre &dk_efi, vd_set_efi_in, vd_set_efi_out, B_TRUE}, 27142f5224aeSachartre 27152f5224aeSachartre {VD_OP_SCSICMD, STRINGIZE(VD_OP_SCSICMD), RNDSIZE(vd_scsi_t), 27162f5224aeSachartre USCSICMD, STRINGIZE(USCSICMD), 27172f5224aeSachartre &uscsi, vd_scsicmd_in, vd_scsicmd_out, B_FALSE}, 27180a55fbb7Slm66018 }; 27191ae08745Sheppo size_t nioctls = (sizeof (ioctl))/(sizeof (ioctl[0])); 27201ae08745Sheppo 27211ae08745Sheppo 2722d10e4ef2Snarayan ASSERT(vd != NULL); 2723d10e4ef2Snarayan ASSERT(request != NULL); 27241ae08745Sheppo ASSERT(request->slice < vd->nslices); 27251ae08745Sheppo 27261ae08745Sheppo /* 27271ae08745Sheppo * Determine ioctl corresponding to caller's "operation" and 27281ae08745Sheppo * validate caller's "nbytes" 27291ae08745Sheppo */ 27301ae08745Sheppo for (i = 0; i < nioctls; i++) { 27311ae08745Sheppo if (request->operation == ioctl[i].operation) { 27320a55fbb7Slm66018 /* LDC memory operations require 8-byte multiples */ 27330a55fbb7Slm66018 ASSERT(ioctl[i].nbytes % sizeof (uint64_t) == 0); 27340a55fbb7Slm66018 27354bac2208Snarayan if (request->operation == VD_OP_GET_EFI || 27362f5224aeSachartre request->operation == VD_OP_SET_EFI || 27372f5224aeSachartre request->operation == VD_OP_SCSICMD) { 27384bac2208Snarayan if (request->nbytes >= ioctl[i].nbytes) 27394bac2208Snarayan break; 27403af08d82Slm66018 PR0("%s: Expected at least nbytes = %lu, " 27414bac2208Snarayan "got %lu", ioctl[i].operation_name, 27424bac2208Snarayan ioctl[i].nbytes, request->nbytes); 27434bac2208Snarayan return (EINVAL); 27444bac2208Snarayan } 27454bac2208Snarayan 27460a55fbb7Slm66018 if (request->nbytes != ioctl[i].nbytes) { 27473af08d82Slm66018 PR0("%s: Expected nbytes = %lu, got %lu", 27480a55fbb7Slm66018 ioctl[i].operation_name, ioctl[i].nbytes, 27490a55fbb7Slm66018 request->nbytes); 27501ae08745Sheppo return (EINVAL); 27511ae08745Sheppo } 27521ae08745Sheppo 27531ae08745Sheppo break; 27541ae08745Sheppo } 27551ae08745Sheppo } 27561ae08745Sheppo ASSERT(i < nioctls); /* because "operation" already validated */ 27571ae08745Sheppo 2758047ba61eSachartre if (!(vd->open_flags & FWRITE) && ioctl[i].write) { 2759047ba61eSachartre PR0("%s fails because backend is opened read-only", 2760047ba61eSachartre ioctl[i].operation_name); 2761047ba61eSachartre request->status = EROFS; 2762047ba61eSachartre return (0); 2763047ba61eSachartre } 2764047ba61eSachartre 27651ae08745Sheppo if (request->nbytes) 27661ae08745Sheppo buf = kmem_zalloc(request->nbytes, KM_SLEEP); 27671ae08745Sheppo status = vd_do_ioctl(vd, request, buf, &ioctl[i]); 27681ae08745Sheppo if (request->nbytes) 27691ae08745Sheppo kmem_free(buf, request->nbytes); 277087a7269eSachartre 27711ae08745Sheppo return (status); 27721ae08745Sheppo } 27731ae08745Sheppo 27744bac2208Snarayan static int 27754bac2208Snarayan vd_get_devid(vd_task_t *task) 27764bac2208Snarayan { 27774bac2208Snarayan vd_t *vd = task->vd; 27784bac2208Snarayan vd_dring_payload_t *request = task->request; 27794bac2208Snarayan vd_devid_t *vd_devid; 27804bac2208Snarayan impl_devid_t *devid; 278187a7269eSachartre int status, bufid_len, devid_len, len, sz; 27823af08d82Slm66018 int bufbytes; 27834bac2208Snarayan 27843af08d82Slm66018 PR1("Get Device ID, nbytes=%ld", request->nbytes); 27854bac2208Snarayan 27863c96341aSnarayan if (vd->file) { 278787a7269eSachartre if (vd->file_devid == NULL) { 27883af08d82Slm66018 PR2("No Device ID"); 2789205eeb1aSlm66018 request->status = ENOENT; 2790205eeb1aSlm66018 return (0); 279187a7269eSachartre } else { 279287a7269eSachartre sz = ddi_devid_sizeof(vd->file_devid); 279387a7269eSachartre devid = kmem_alloc(sz, KM_SLEEP); 279487a7269eSachartre bcopy(vd->file_devid, devid, sz); 279587a7269eSachartre } 279687a7269eSachartre } else { 279787a7269eSachartre if (ddi_lyr_get_devid(vd->dev[request->slice], 279887a7269eSachartre (ddi_devid_t *)&devid) != DDI_SUCCESS) { 279987a7269eSachartre PR2("No Device ID"); 2800205eeb1aSlm66018 request->status = ENOENT; 2801205eeb1aSlm66018 return (0); 280287a7269eSachartre } 28034bac2208Snarayan } 28044bac2208Snarayan 28054bac2208Snarayan bufid_len = request->nbytes - sizeof (vd_devid_t) + 1; 28064bac2208Snarayan devid_len = DEVID_GETLEN(devid); 28074bac2208Snarayan 28083af08d82Slm66018 /* 28093af08d82Slm66018 * Save the buffer size here for use in deallocation. 28103af08d82Slm66018 * The actual number of bytes copied is returned in 28113af08d82Slm66018 * the 'nbytes' field of the request structure. 28123af08d82Slm66018 */ 28133af08d82Slm66018 bufbytes = request->nbytes; 28143af08d82Slm66018 28153af08d82Slm66018 vd_devid = kmem_zalloc(bufbytes, KM_SLEEP); 28164bac2208Snarayan vd_devid->length = devid_len; 28174bac2208Snarayan vd_devid->type = DEVID_GETTYPE(devid); 28184bac2208Snarayan 28194bac2208Snarayan len = (devid_len > bufid_len)? bufid_len : devid_len; 28204bac2208Snarayan 28214bac2208Snarayan bcopy(devid->did_id, vd_devid->id, len); 28224bac2208Snarayan 282378fcd0a1Sachartre request->status = 0; 282478fcd0a1Sachartre 28254bac2208Snarayan /* LDC memory operations require 8-byte multiples */ 28264bac2208Snarayan ASSERT(request->nbytes % sizeof (uint64_t) == 0); 28274bac2208Snarayan 28284bac2208Snarayan if ((status = ldc_mem_copy(vd->ldc_handle, (caddr_t)vd_devid, 0, 28294bac2208Snarayan &request->nbytes, request->cookie, request->ncookies, 28304bac2208Snarayan LDC_COPY_OUT)) != 0) { 28313af08d82Slm66018 PR0("ldc_mem_copy() returned errno %d copying to client", 28324bac2208Snarayan status); 28334bac2208Snarayan } 28343af08d82Slm66018 PR1("post mem_copy: nbytes=%ld", request->nbytes); 28354bac2208Snarayan 28363af08d82Slm66018 kmem_free(vd_devid, bufbytes); 28374bac2208Snarayan ddi_devid_free((ddi_devid_t)devid); 28384bac2208Snarayan 28394bac2208Snarayan return (status); 28404bac2208Snarayan } 28414bac2208Snarayan 28422f5224aeSachartre static int 28432f5224aeSachartre vd_scsi_reset(vd_t *vd) 28442f5224aeSachartre { 28452f5224aeSachartre int rval, status; 28462f5224aeSachartre struct uscsi_cmd uscsi = { 0 }; 28472f5224aeSachartre 28482f5224aeSachartre uscsi.uscsi_flags = vd_scsi_debug | USCSI_RESET; 28492f5224aeSachartre uscsi.uscsi_timeout = vd_scsi_rdwr_timeout; 28502f5224aeSachartre 28512f5224aeSachartre status = ldi_ioctl(vd->ldi_handle[0], USCSICMD, (intptr_t)&uscsi, 28522f5224aeSachartre (vd->open_flags | FKIOCTL), kcred, &rval); 28532f5224aeSachartre 28542f5224aeSachartre return (status); 28552f5224aeSachartre } 28562f5224aeSachartre 28572f5224aeSachartre static int 28582f5224aeSachartre vd_reset(vd_task_t *task) 28592f5224aeSachartre { 28602f5224aeSachartre vd_t *vd = task->vd; 28612f5224aeSachartre vd_dring_payload_t *request = task->request; 28622f5224aeSachartre 28632f5224aeSachartre ASSERT(request->operation == VD_OP_RESET); 28642f5224aeSachartre ASSERT(vd->scsi); 28652f5224aeSachartre 28662f5224aeSachartre PR0("Performing VD_OP_RESET"); 28672f5224aeSachartre 28682f5224aeSachartre if (request->nbytes != 0) { 28692f5224aeSachartre PR0("VD_OP_RESET: Expected nbytes = 0, got %lu", 28702f5224aeSachartre request->nbytes); 28712f5224aeSachartre return (EINVAL); 28722f5224aeSachartre } 28732f5224aeSachartre 28742f5224aeSachartre request->status = vd_scsi_reset(vd); 28752f5224aeSachartre 28762f5224aeSachartre return (0); 28772f5224aeSachartre } 28782f5224aeSachartre 28792f5224aeSachartre static int 28802f5224aeSachartre vd_get_capacity(vd_task_t *task) 28812f5224aeSachartre { 28822f5224aeSachartre int rv; 28832f5224aeSachartre size_t nbytes; 28842f5224aeSachartre vd_t *vd = task->vd; 28852f5224aeSachartre vd_dring_payload_t *request = task->request; 28862f5224aeSachartre vd_capacity_t vd_cap = { 0 }; 28872f5224aeSachartre 28882f5224aeSachartre ASSERT(request->operation == VD_OP_GET_CAPACITY); 28892f5224aeSachartre ASSERT(vd->scsi); 28902f5224aeSachartre 28912f5224aeSachartre PR0("Performing VD_OP_GET_CAPACITY"); 28922f5224aeSachartre 28932f5224aeSachartre nbytes = request->nbytes; 28942f5224aeSachartre 28952f5224aeSachartre if (nbytes != RNDSIZE(vd_capacity_t)) { 28962f5224aeSachartre PR0("VD_OP_GET_CAPACITY: Expected nbytes = %lu, got %lu", 28972f5224aeSachartre RNDSIZE(vd_capacity_t), nbytes); 28982f5224aeSachartre return (EINVAL); 28992f5224aeSachartre } 29002f5224aeSachartre 29012f5224aeSachartre if (vd->vdisk_size == VD_SIZE_UNKNOWN) { 29022f5224aeSachartre if (vd_setup_mediainfo(vd) != 0) 29032f5224aeSachartre ASSERT(vd->vdisk_size == VD_SIZE_UNKNOWN); 29042f5224aeSachartre } 29052f5224aeSachartre 29062f5224aeSachartre ASSERT(vd->vdisk_size != 0); 29072f5224aeSachartre 29082f5224aeSachartre request->status = 0; 29092f5224aeSachartre 29102f5224aeSachartre vd_cap.vdisk_block_size = vd->vdisk_block_size; 29112f5224aeSachartre vd_cap.vdisk_size = vd->vdisk_size; 29122f5224aeSachartre 29132f5224aeSachartre if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&vd_cap, 0, &nbytes, 29142f5224aeSachartre request->cookie, request->ncookies, LDC_COPY_OUT)) != 0) { 29152f5224aeSachartre PR0("ldc_mem_copy() returned errno %d copying to client", rv); 29162f5224aeSachartre return (rv); 29172f5224aeSachartre } 29182f5224aeSachartre 29192f5224aeSachartre return (0); 29202f5224aeSachartre } 29212f5224aeSachartre 29222f5224aeSachartre static int 29232f5224aeSachartre vd_get_access(vd_task_t *task) 29242f5224aeSachartre { 29252f5224aeSachartre uint64_t access; 29262f5224aeSachartre int rv, rval = 0; 29272f5224aeSachartre size_t nbytes; 29282f5224aeSachartre vd_t *vd = task->vd; 29292f5224aeSachartre vd_dring_payload_t *request = task->request; 29302f5224aeSachartre 29312f5224aeSachartre ASSERT(request->operation == VD_OP_GET_ACCESS); 29322f5224aeSachartre ASSERT(vd->scsi); 29332f5224aeSachartre 29342f5224aeSachartre PR0("Performing VD_OP_GET_ACCESS"); 29352f5224aeSachartre 29362f5224aeSachartre nbytes = request->nbytes; 29372f5224aeSachartre 29382f5224aeSachartre if (nbytes != sizeof (uint64_t)) { 29392f5224aeSachartre PR0("VD_OP_GET_ACCESS: Expected nbytes = %lu, got %lu", 29402f5224aeSachartre sizeof (uint64_t), nbytes); 29412f5224aeSachartre return (EINVAL); 29422f5224aeSachartre } 29432f5224aeSachartre 29442f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], MHIOCSTATUS, 29452f5224aeSachartre NULL, (vd->open_flags | FKIOCTL), kcred, &rval); 29462f5224aeSachartre 29472f5224aeSachartre if (request->status != 0) 29482f5224aeSachartre return (0); 29492f5224aeSachartre 29502f5224aeSachartre access = (rval == 0)? VD_ACCESS_ALLOWED : VD_ACCESS_DENIED; 29512f5224aeSachartre 29522f5224aeSachartre if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&access, 0, &nbytes, 29532f5224aeSachartre request->cookie, request->ncookies, LDC_COPY_OUT)) != 0) { 29542f5224aeSachartre PR0("ldc_mem_copy() returned errno %d copying to client", rv); 29552f5224aeSachartre return (rv); 29562f5224aeSachartre } 29572f5224aeSachartre 29582f5224aeSachartre return (0); 29592f5224aeSachartre } 29602f5224aeSachartre 29612f5224aeSachartre static int 29622f5224aeSachartre vd_set_access(vd_task_t *task) 29632f5224aeSachartre { 29642f5224aeSachartre uint64_t flags; 29652f5224aeSachartre int rv, rval; 29662f5224aeSachartre size_t nbytes; 29672f5224aeSachartre vd_t *vd = task->vd; 29682f5224aeSachartre vd_dring_payload_t *request = task->request; 29692f5224aeSachartre 29702f5224aeSachartre ASSERT(request->operation == VD_OP_SET_ACCESS); 29712f5224aeSachartre ASSERT(vd->scsi); 29722f5224aeSachartre 29732f5224aeSachartre nbytes = request->nbytes; 29742f5224aeSachartre 29752f5224aeSachartre if (nbytes != sizeof (uint64_t)) { 29762f5224aeSachartre PR0("VD_OP_SET_ACCESS: Expected nbytes = %lu, got %lu", 29772f5224aeSachartre sizeof (uint64_t), nbytes); 29782f5224aeSachartre return (EINVAL); 29792f5224aeSachartre } 29802f5224aeSachartre 29812f5224aeSachartre if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&flags, 0, &nbytes, 29822f5224aeSachartre request->cookie, request->ncookies, LDC_COPY_IN)) != 0) { 29832f5224aeSachartre PR0("ldc_mem_copy() returned errno %d copying from client", rv); 29842f5224aeSachartre return (rv); 29852f5224aeSachartre } 29862f5224aeSachartre 29872f5224aeSachartre if (flags == VD_ACCESS_SET_CLEAR) { 29882f5224aeSachartre PR0("Performing VD_OP_SET_ACCESS (CLEAR)"); 29892f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 29902f5224aeSachartre MHIOCRELEASE, NULL, (vd->open_flags | FKIOCTL), kcred, 29912f5224aeSachartre &rval); 29922f5224aeSachartre if (request->status == 0) 29932f5224aeSachartre vd->ownership = B_FALSE; 29942f5224aeSachartre return (0); 29952f5224aeSachartre } 29962f5224aeSachartre 29972f5224aeSachartre /* 29982f5224aeSachartre * As per the VIO spec, the PREEMPT and PRESERVE flags are only valid 29992f5224aeSachartre * when the EXCLUSIVE flag is set. 30002f5224aeSachartre */ 30012f5224aeSachartre if (!(flags & VD_ACCESS_SET_EXCLUSIVE)) { 30022f5224aeSachartre PR0("Invalid VD_OP_SET_ACCESS flags: 0x%lx", flags); 30032f5224aeSachartre request->status = EINVAL; 30042f5224aeSachartre return (0); 30052f5224aeSachartre } 30062f5224aeSachartre 30072f5224aeSachartre switch (flags & (VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE)) { 30082f5224aeSachartre 30092f5224aeSachartre case VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE: 30102f5224aeSachartre /* 30112f5224aeSachartre * Flags EXCLUSIVE and PREEMPT and PRESERVE. We have to 30122f5224aeSachartre * acquire exclusive access rights, preserve them and we 30132f5224aeSachartre * can use preemption. So we can use the MHIOCTKNOWN ioctl. 30142f5224aeSachartre */ 30152f5224aeSachartre PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PREEMPT|PRESERVE)"); 30162f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 30172f5224aeSachartre MHIOCTKOWN, NULL, (vd->open_flags | FKIOCTL), kcred, &rval); 30182f5224aeSachartre break; 30192f5224aeSachartre 30202f5224aeSachartre case VD_ACCESS_SET_PRESERVE: 30212f5224aeSachartre /* 30222f5224aeSachartre * Flags EXCLUSIVE and PRESERVE. We have to acquire exclusive 30232f5224aeSachartre * access rights and preserve them, but not preempt any other 30242f5224aeSachartre * host. So we need to use the MHIOCTKOWN ioctl to enable the 30252f5224aeSachartre * "preserve" feature but we can not called it directly 30262f5224aeSachartre * because it uses preemption. So before that, we use the 30272f5224aeSachartre * MHIOCQRESERVE ioctl to ensure we can get exclusive rights 30282f5224aeSachartre * without preempting anyone. 30292f5224aeSachartre */ 30302f5224aeSachartre PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PRESERVE)"); 30312f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 30322f5224aeSachartre MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred, 30332f5224aeSachartre &rval); 30342f5224aeSachartre if (request->status != 0) 30352f5224aeSachartre break; 30362f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 30372f5224aeSachartre MHIOCTKOWN, NULL, (vd->open_flags | FKIOCTL), kcred, &rval); 30382f5224aeSachartre break; 30392f5224aeSachartre 30402f5224aeSachartre case VD_ACCESS_SET_PREEMPT: 30412f5224aeSachartre /* 30422f5224aeSachartre * Flags EXCLUSIVE and PREEMPT. We have to acquire exclusive 30432f5224aeSachartre * access rights and we can use preemption. So we try to do 30442f5224aeSachartre * a SCSI reservation, if it fails we reset the disk to clear 30452f5224aeSachartre * any reservation and we try to reserve again. 30462f5224aeSachartre */ 30472f5224aeSachartre PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PREEMPT)"); 30482f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 30492f5224aeSachartre MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred, 30502f5224aeSachartre &rval); 30512f5224aeSachartre if (request->status == 0) 30522f5224aeSachartre break; 30532f5224aeSachartre 30542f5224aeSachartre /* reset the disk */ 30552f5224aeSachartre (void) vd_scsi_reset(vd); 30562f5224aeSachartre 30572f5224aeSachartre /* try again even if the reset has failed */ 30582f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 30592f5224aeSachartre MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred, 30602f5224aeSachartre &rval); 30612f5224aeSachartre break; 30622f5224aeSachartre 30632f5224aeSachartre case 0: 30642f5224aeSachartre /* Flag EXCLUSIVE only. Just issue a SCSI reservation */ 30652f5224aeSachartre PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE)"); 30662f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 30672f5224aeSachartre MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred, 30682f5224aeSachartre &rval); 30692f5224aeSachartre break; 30702f5224aeSachartre } 30712f5224aeSachartre 30722f5224aeSachartre if (request->status == 0) 30732f5224aeSachartre vd->ownership = B_TRUE; 30742f5224aeSachartre else 30752f5224aeSachartre PR0("VD_OP_SET_ACCESS: error %d", request->status); 30762f5224aeSachartre 30772f5224aeSachartre return (0); 30782f5224aeSachartre } 30792f5224aeSachartre 30802f5224aeSachartre static void 30812f5224aeSachartre vd_reset_access(vd_t *vd) 30822f5224aeSachartre { 30832f5224aeSachartre int status, rval; 30842f5224aeSachartre 30852f5224aeSachartre if (vd->file || !vd->ownership) 30862f5224aeSachartre return; 30872f5224aeSachartre 30882f5224aeSachartre PR0("Releasing disk ownership"); 30892f5224aeSachartre status = ldi_ioctl(vd->ldi_handle[0], MHIOCRELEASE, NULL, 30902f5224aeSachartre (vd->open_flags | FKIOCTL), kcred, &rval); 30912f5224aeSachartre 30922f5224aeSachartre /* 30932f5224aeSachartre * An EACCES failure means that there is a reservation conflict, 30942f5224aeSachartre * so we are not the owner of the disk anymore. 30952f5224aeSachartre */ 30962f5224aeSachartre if (status == 0 || status == EACCES) { 30972f5224aeSachartre vd->ownership = B_FALSE; 30982f5224aeSachartre return; 30992f5224aeSachartre } 31002f5224aeSachartre 31012f5224aeSachartre PR0("Fail to release ownership, error %d", status); 31022f5224aeSachartre 31032f5224aeSachartre /* 31042f5224aeSachartre * We have failed to release the ownership, try to reset the disk 31052f5224aeSachartre * to release reservations. 31062f5224aeSachartre */ 31072f5224aeSachartre PR0("Resetting disk"); 31082f5224aeSachartre status = vd_scsi_reset(vd); 31092f5224aeSachartre 31102f5224aeSachartre if (status != 0) 31112f5224aeSachartre PR0("Fail to reset disk, error %d", status); 31122f5224aeSachartre 31132f5224aeSachartre /* whatever the result of the reset is, we try the release again */ 31142f5224aeSachartre status = ldi_ioctl(vd->ldi_handle[0], MHIOCRELEASE, NULL, 31152f5224aeSachartre (vd->open_flags | FKIOCTL), kcred, &rval); 31162f5224aeSachartre 31172f5224aeSachartre if (status == 0 || status == EACCES) { 31182f5224aeSachartre vd->ownership = B_FALSE; 31192f5224aeSachartre return; 31202f5224aeSachartre } 31212f5224aeSachartre 31222f5224aeSachartre PR0("Fail to release ownership, error %d", status); 31232f5224aeSachartre 31242f5224aeSachartre /* 31252f5224aeSachartre * At this point we have done our best to try to reset the 31262f5224aeSachartre * access rights to the disk and we don't know if we still 31272f5224aeSachartre * own a reservation and if any mechanism to preserve the 31282f5224aeSachartre * ownership is still in place. The ultimate solution would 31292f5224aeSachartre * be to reset the system but this is usually not what we 31302f5224aeSachartre * want to happen. 31312f5224aeSachartre */ 31322f5224aeSachartre 31332f5224aeSachartre if (vd_reset_access_failure == A_REBOOT) { 31342f5224aeSachartre cmn_err(CE_WARN, VD_RESET_ACCESS_FAILURE_MSG 31352f5224aeSachartre ", rebooting the system", vd->device_path); 31362f5224aeSachartre (void) uadmin(A_SHUTDOWN, AD_BOOT, NULL); 31372f5224aeSachartre } else if (vd_reset_access_failure == A_DUMP) { 31382f5224aeSachartre panic(VD_RESET_ACCESS_FAILURE_MSG, vd->device_path); 31392f5224aeSachartre } 31402f5224aeSachartre 31412f5224aeSachartre cmn_err(CE_WARN, VD_RESET_ACCESS_FAILURE_MSG, vd->device_path); 31422f5224aeSachartre } 31432f5224aeSachartre 31441ae08745Sheppo /* 31451ae08745Sheppo * Define the supported operations once the functions for performing them have 31461ae08745Sheppo * been defined 31471ae08745Sheppo */ 31481ae08745Sheppo static const vds_operation_t vds_operation[] = { 31493af08d82Slm66018 #define X(_s) #_s, _s 31503af08d82Slm66018 {X(VD_OP_BREAD), vd_start_bio, vd_complete_bio}, 31513af08d82Slm66018 {X(VD_OP_BWRITE), vd_start_bio, vd_complete_bio}, 31523af08d82Slm66018 {X(VD_OP_FLUSH), vd_ioctl, NULL}, 31533af08d82Slm66018 {X(VD_OP_GET_WCE), vd_ioctl, NULL}, 31543af08d82Slm66018 {X(VD_OP_SET_WCE), vd_ioctl, NULL}, 31553af08d82Slm66018 {X(VD_OP_GET_VTOC), vd_ioctl, NULL}, 31563af08d82Slm66018 {X(VD_OP_SET_VTOC), vd_ioctl, NULL}, 31573af08d82Slm66018 {X(VD_OP_GET_DISKGEOM), vd_ioctl, NULL}, 31583af08d82Slm66018 {X(VD_OP_SET_DISKGEOM), vd_ioctl, NULL}, 31593af08d82Slm66018 {X(VD_OP_GET_EFI), vd_ioctl, NULL}, 31603af08d82Slm66018 {X(VD_OP_SET_EFI), vd_ioctl, NULL}, 31613af08d82Slm66018 {X(VD_OP_GET_DEVID), vd_get_devid, NULL}, 31622f5224aeSachartre {X(VD_OP_SCSICMD), vd_ioctl, NULL}, 31632f5224aeSachartre {X(VD_OP_RESET), vd_reset, NULL}, 31642f5224aeSachartre {X(VD_OP_GET_CAPACITY), vd_get_capacity, NULL}, 31652f5224aeSachartre {X(VD_OP_SET_ACCESS), vd_set_access, NULL}, 31662f5224aeSachartre {X(VD_OP_GET_ACCESS), vd_get_access, NULL}, 31673af08d82Slm66018 #undef X 31681ae08745Sheppo }; 31691ae08745Sheppo 31701ae08745Sheppo static const size_t vds_noperations = 31711ae08745Sheppo (sizeof (vds_operation))/(sizeof (vds_operation[0])); 31721ae08745Sheppo 31731ae08745Sheppo /* 3174d10e4ef2Snarayan * Process a task specifying a client I/O request 3175205eeb1aSlm66018 * 3176205eeb1aSlm66018 * Parameters: 3177205eeb1aSlm66018 * task - structure containing the request sent from client 3178205eeb1aSlm66018 * 3179205eeb1aSlm66018 * Return Value 3180205eeb1aSlm66018 * 0 - success 3181205eeb1aSlm66018 * ENOTSUP - Unknown/Unsupported VD_OP_XXX operation 3182205eeb1aSlm66018 * EINVAL - Invalid disk slice 3183205eeb1aSlm66018 * != 0 - some other non-zero return value from start function 31841ae08745Sheppo */ 31851ae08745Sheppo static int 3186205eeb1aSlm66018 vd_do_process_task(vd_task_t *task) 31871ae08745Sheppo { 3188205eeb1aSlm66018 int i; 3189d10e4ef2Snarayan vd_t *vd = task->vd; 3190d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 31911ae08745Sheppo 3192d10e4ef2Snarayan ASSERT(vd != NULL); 3193d10e4ef2Snarayan ASSERT(request != NULL); 31941ae08745Sheppo 3195d10e4ef2Snarayan /* Find the requested operation */ 3196205eeb1aSlm66018 for (i = 0; i < vds_noperations; i++) { 3197205eeb1aSlm66018 if (request->operation == vds_operation[i].operation) { 3198205eeb1aSlm66018 /* all operations should have a start func */ 3199205eeb1aSlm66018 ASSERT(vds_operation[i].start != NULL); 3200205eeb1aSlm66018 3201205eeb1aSlm66018 task->completef = vds_operation[i].complete; 3202d10e4ef2Snarayan break; 3203205eeb1aSlm66018 } 3204205eeb1aSlm66018 } 320517cadca8Slm66018 320617cadca8Slm66018 /* 320717cadca8Slm66018 * We need to check that the requested operation is permitted 320817cadca8Slm66018 * for the particular client that sent it or that the loop above 320917cadca8Slm66018 * did not complete without finding the operation type (indicating 321017cadca8Slm66018 * that the requested operation is unknown/unimplemented) 321117cadca8Slm66018 */ 321217cadca8Slm66018 if ((VD_OP_SUPPORTED(vd->operations, request->operation) == B_FALSE) || 321317cadca8Slm66018 (i == vds_noperations)) { 32143af08d82Slm66018 PR0("Unsupported operation %u", request->operation); 321517cadca8Slm66018 request->status = ENOTSUP; 321617cadca8Slm66018 return (0); 32171ae08745Sheppo } 32181ae08745Sheppo 32197636cb21Slm66018 /* Range-check slice */ 322087a7269eSachartre if (request->slice >= vd->nslices && 322187a7269eSachartre (vd->vdisk_type != VD_DISK_TYPE_DISK || 322287a7269eSachartre request->slice != VD_SLICE_NONE)) { 32233af08d82Slm66018 PR0("Invalid \"slice\" %u (max %u) for virtual disk", 32247636cb21Slm66018 request->slice, (vd->nslices - 1)); 32257636cb21Slm66018 return (EINVAL); 32267636cb21Slm66018 } 32277636cb21Slm66018 3228205eeb1aSlm66018 /* 3229205eeb1aSlm66018 * Call the function pointer that starts the operation. 3230205eeb1aSlm66018 */ 3231205eeb1aSlm66018 return (vds_operation[i].start(task)); 32321ae08745Sheppo } 32331ae08745Sheppo 3234205eeb1aSlm66018 /* 3235205eeb1aSlm66018 * Description: 3236205eeb1aSlm66018 * This function is called by both the in-band and descriptor ring 3237205eeb1aSlm66018 * message processing functions paths to actually execute the task 3238205eeb1aSlm66018 * requested by the vDisk client. It in turn calls its worker 3239205eeb1aSlm66018 * function, vd_do_process_task(), to carry our the request. 3240205eeb1aSlm66018 * 3241205eeb1aSlm66018 * Any transport errors (e.g. LDC errors, vDisk protocol errors) are 3242205eeb1aSlm66018 * saved in the 'status' field of the task and are propagated back 3243205eeb1aSlm66018 * up the call stack to trigger a NACK 3244205eeb1aSlm66018 * 3245205eeb1aSlm66018 * Any request errors (e.g. ENOTTY from an ioctl) are saved in 3246205eeb1aSlm66018 * the 'status' field of the request and result in an ACK being sent 3247205eeb1aSlm66018 * by the completion handler. 3248205eeb1aSlm66018 * 3249205eeb1aSlm66018 * Parameters: 3250205eeb1aSlm66018 * task - structure containing the request sent from client 3251205eeb1aSlm66018 * 3252205eeb1aSlm66018 * Return Value 3253205eeb1aSlm66018 * 0 - successful synchronous request. 3254205eeb1aSlm66018 * != 0 - transport error (e.g. LDC errors, vDisk protocol) 3255205eeb1aSlm66018 * EINPROGRESS - task will be finished in a completion handler 3256205eeb1aSlm66018 */ 3257205eeb1aSlm66018 static int 3258205eeb1aSlm66018 vd_process_task(vd_task_t *task) 3259205eeb1aSlm66018 { 3260205eeb1aSlm66018 vd_t *vd = task->vd; 3261205eeb1aSlm66018 int status; 32621ae08745Sheppo 3263205eeb1aSlm66018 DTRACE_PROBE1(task__start, vd_task_t *, task); 32643af08d82Slm66018 3265205eeb1aSlm66018 task->status = vd_do_process_task(task); 3266205eeb1aSlm66018 3267205eeb1aSlm66018 /* 3268205eeb1aSlm66018 * If the task processing function returned EINPROGRESS indicating 3269205eeb1aSlm66018 * that the task needs completing then schedule a taskq entry to 3270205eeb1aSlm66018 * finish it now. 3271205eeb1aSlm66018 * 3272205eeb1aSlm66018 * Otherwise the task processing function returned either zero 3273205eeb1aSlm66018 * indicating that the task was finished in the start function (and we 3274205eeb1aSlm66018 * don't need to wait in a completion function) or the start function 3275205eeb1aSlm66018 * returned an error - in both cases all that needs to happen is the 3276205eeb1aSlm66018 * notification to the vDisk client higher up the call stack. 3277205eeb1aSlm66018 * If the task was using a Descriptor Ring, we need to mark it as done 3278205eeb1aSlm66018 * at this stage. 3279205eeb1aSlm66018 */ 3280205eeb1aSlm66018 if (task->status == EINPROGRESS) { 3281d10e4ef2Snarayan /* Queue a task to complete the operation */ 3282205eeb1aSlm66018 (void) ddi_taskq_dispatch(vd->completionq, vd_complete, 3283d10e4ef2Snarayan task, DDI_SLEEP); 3284d10e4ef2Snarayan 3285f0ca1d9aSsb155480 } else if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE_V1_0)) { 3286205eeb1aSlm66018 /* Update the dring element if it's a dring client */ 3287205eeb1aSlm66018 status = vd_mark_elem_done(vd, task->index, 3288205eeb1aSlm66018 task->request->status, task->request->nbytes); 3289205eeb1aSlm66018 if (status == ECONNRESET) 3290205eeb1aSlm66018 vd_mark_in_reset(vd); 3291205eeb1aSlm66018 } 3292205eeb1aSlm66018 3293205eeb1aSlm66018 return (task->status); 32941ae08745Sheppo } 32951ae08745Sheppo 32961ae08745Sheppo /* 32970a55fbb7Slm66018 * Return true if the "type", "subtype", and "env" fields of the "tag" first 32980a55fbb7Slm66018 * argument match the corresponding remaining arguments; otherwise, return false 32991ae08745Sheppo */ 33000a55fbb7Slm66018 boolean_t 33011ae08745Sheppo vd_msgtype(vio_msg_tag_t *tag, int type, int subtype, int env) 33021ae08745Sheppo { 33031ae08745Sheppo return ((tag->vio_msgtype == type) && 33041ae08745Sheppo (tag->vio_subtype == subtype) && 33050a55fbb7Slm66018 (tag->vio_subtype_env == env)) ? B_TRUE : B_FALSE; 33061ae08745Sheppo } 33071ae08745Sheppo 33080a55fbb7Slm66018 /* 33090a55fbb7Slm66018 * Check whether the major/minor version specified in "ver_msg" is supported 33100a55fbb7Slm66018 * by this server. 33110a55fbb7Slm66018 */ 33120a55fbb7Slm66018 static boolean_t 33130a55fbb7Slm66018 vds_supported_version(vio_ver_msg_t *ver_msg) 33140a55fbb7Slm66018 { 33150a55fbb7Slm66018 for (int i = 0; i < vds_num_versions; i++) { 33160a55fbb7Slm66018 ASSERT(vds_version[i].major > 0); 33170a55fbb7Slm66018 ASSERT((i == 0) || 33180a55fbb7Slm66018 (vds_version[i].major < vds_version[i-1].major)); 33190a55fbb7Slm66018 33200a55fbb7Slm66018 /* 33210a55fbb7Slm66018 * If the major versions match, adjust the minor version, if 33220a55fbb7Slm66018 * necessary, down to the highest value supported by this 33230a55fbb7Slm66018 * server and return true so this message will get "ack"ed; 33240a55fbb7Slm66018 * the client should also support all minor versions lower 33250a55fbb7Slm66018 * than the value it sent 33260a55fbb7Slm66018 */ 33270a55fbb7Slm66018 if (ver_msg->ver_major == vds_version[i].major) { 33280a55fbb7Slm66018 if (ver_msg->ver_minor > vds_version[i].minor) { 33290a55fbb7Slm66018 PR0("Adjusting minor version from %u to %u", 33300a55fbb7Slm66018 ver_msg->ver_minor, vds_version[i].minor); 33310a55fbb7Slm66018 ver_msg->ver_minor = vds_version[i].minor; 33320a55fbb7Slm66018 } 33330a55fbb7Slm66018 return (B_TRUE); 33340a55fbb7Slm66018 } 33350a55fbb7Slm66018 33360a55fbb7Slm66018 /* 33370a55fbb7Slm66018 * If the message contains a higher major version number, set 33380a55fbb7Slm66018 * the message's major/minor versions to the current values 33390a55fbb7Slm66018 * and return false, so this message will get "nack"ed with 33400a55fbb7Slm66018 * these values, and the client will potentially try again 33410a55fbb7Slm66018 * with the same or a lower version 33420a55fbb7Slm66018 */ 33430a55fbb7Slm66018 if (ver_msg->ver_major > vds_version[i].major) { 33440a55fbb7Slm66018 ver_msg->ver_major = vds_version[i].major; 33450a55fbb7Slm66018 ver_msg->ver_minor = vds_version[i].minor; 33460a55fbb7Slm66018 return (B_FALSE); 33470a55fbb7Slm66018 } 33480a55fbb7Slm66018 33490a55fbb7Slm66018 /* 33500a55fbb7Slm66018 * Otherwise, the message's major version is less than the 33510a55fbb7Slm66018 * current major version, so continue the loop to the next 33520a55fbb7Slm66018 * (lower) supported version 33530a55fbb7Slm66018 */ 33540a55fbb7Slm66018 } 33550a55fbb7Slm66018 33560a55fbb7Slm66018 /* 33570a55fbb7Slm66018 * No common version was found; "ground" the version pair in the 33580a55fbb7Slm66018 * message to terminate negotiation 33590a55fbb7Slm66018 */ 33600a55fbb7Slm66018 ver_msg->ver_major = 0; 33610a55fbb7Slm66018 ver_msg->ver_minor = 0; 33620a55fbb7Slm66018 return (B_FALSE); 33630a55fbb7Slm66018 } 33640a55fbb7Slm66018 33650a55fbb7Slm66018 /* 33660a55fbb7Slm66018 * Process a version message from a client. vds expects to receive version 33670a55fbb7Slm66018 * messages from clients seeking service, but never issues version messages 33680a55fbb7Slm66018 * itself; therefore, vds can ACK or NACK client version messages, but does 33690a55fbb7Slm66018 * not expect to receive version-message ACKs or NACKs (and will treat such 33700a55fbb7Slm66018 * messages as invalid). 33710a55fbb7Slm66018 */ 33721ae08745Sheppo static int 33730a55fbb7Slm66018 vd_process_ver_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 33741ae08745Sheppo { 33751ae08745Sheppo vio_ver_msg_t *ver_msg = (vio_ver_msg_t *)msg; 33761ae08745Sheppo 33771ae08745Sheppo 33781ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 33791ae08745Sheppo 33801ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 33811ae08745Sheppo VIO_VER_INFO)) { 33821ae08745Sheppo return (ENOMSG); /* not a version message */ 33831ae08745Sheppo } 33841ae08745Sheppo 33851ae08745Sheppo if (msglen != sizeof (*ver_msg)) { 33863af08d82Slm66018 PR0("Expected %lu-byte version message; " 33871ae08745Sheppo "received %lu bytes", sizeof (*ver_msg), msglen); 33881ae08745Sheppo return (EBADMSG); 33891ae08745Sheppo } 33901ae08745Sheppo 33911ae08745Sheppo if (ver_msg->dev_class != VDEV_DISK) { 33923af08d82Slm66018 PR0("Expected device class %u (disk); received %u", 33931ae08745Sheppo VDEV_DISK, ver_msg->dev_class); 33941ae08745Sheppo return (EBADMSG); 33951ae08745Sheppo } 33961ae08745Sheppo 33970a55fbb7Slm66018 /* 33980a55fbb7Slm66018 * We're talking to the expected kind of client; set our device class 33990a55fbb7Slm66018 * for "ack/nack" back to the client 34000a55fbb7Slm66018 */ 34011ae08745Sheppo ver_msg->dev_class = VDEV_DISK_SERVER; 34020a55fbb7Slm66018 34030a55fbb7Slm66018 /* 34040a55fbb7Slm66018 * Check whether the (valid) version message specifies a version 34050a55fbb7Slm66018 * supported by this server. If the version is not supported, return 34060a55fbb7Slm66018 * EBADMSG so the message will get "nack"ed; vds_supported_version() 34070a55fbb7Slm66018 * will have updated the message with a supported version for the 34080a55fbb7Slm66018 * client to consider 34090a55fbb7Slm66018 */ 34100a55fbb7Slm66018 if (!vds_supported_version(ver_msg)) 34110a55fbb7Slm66018 return (EBADMSG); 34120a55fbb7Slm66018 34130a55fbb7Slm66018 34140a55fbb7Slm66018 /* 34150a55fbb7Slm66018 * A version has been agreed upon; use the client's SID for 34160a55fbb7Slm66018 * communication on this channel now 34170a55fbb7Slm66018 */ 34180a55fbb7Slm66018 ASSERT(!(vd->initialized & VD_SID)); 34190a55fbb7Slm66018 vd->sid = ver_msg->tag.vio_sid; 34200a55fbb7Slm66018 vd->initialized |= VD_SID; 34210a55fbb7Slm66018 34220a55fbb7Slm66018 /* 342317cadca8Slm66018 * Store the negotiated major and minor version values in the "vd" data 342417cadca8Slm66018 * structure so that we can check if certain operations are supported 342517cadca8Slm66018 * by the client. 34260a55fbb7Slm66018 */ 342717cadca8Slm66018 vd->version.major = ver_msg->ver_major; 342817cadca8Slm66018 vd->version.minor = ver_msg->ver_minor; 34290a55fbb7Slm66018 34300a55fbb7Slm66018 PR0("Using major version %u, minor version %u", 34310a55fbb7Slm66018 ver_msg->ver_major, ver_msg->ver_minor); 34321ae08745Sheppo return (0); 34331ae08745Sheppo } 34341ae08745Sheppo 343517cadca8Slm66018 static void 343617cadca8Slm66018 vd_set_exported_operations(vd_t *vd) 343717cadca8Slm66018 { 343817cadca8Slm66018 vd->operations = 0; /* clear field */ 343917cadca8Slm66018 344017cadca8Slm66018 /* 344117cadca8Slm66018 * We need to check from the highest version supported to the 344217cadca8Slm66018 * lowest because versions with a higher minor number implicitly 344317cadca8Slm66018 * support versions with a lower minor number. 344417cadca8Slm66018 */ 344517cadca8Slm66018 if (vio_ver_is_supported(vd->version, 1, 1)) { 344617cadca8Slm66018 ASSERT(vd->open_flags & FREAD); 344717cadca8Slm66018 vd->operations |= VD_OP_MASK_READ; 344817cadca8Slm66018 344917cadca8Slm66018 if (vd->open_flags & FWRITE) 345017cadca8Slm66018 vd->operations |= VD_OP_MASK_WRITE; 345117cadca8Slm66018 34522f5224aeSachartre if (vd->scsi) 34532f5224aeSachartre vd->operations |= VD_OP_MASK_SCSI; 34542f5224aeSachartre 345517cadca8Slm66018 if (vd->file && vd_file_is_iso_image(vd)) { 345617cadca8Slm66018 /* 345717cadca8Slm66018 * can't write to ISO images, make sure that write 345817cadca8Slm66018 * support is not set in case administrator did not 345917cadca8Slm66018 * use "options=ro" when doing an ldm add-vdsdev 346017cadca8Slm66018 */ 346117cadca8Slm66018 vd->operations &= ~VD_OP_MASK_WRITE; 346217cadca8Slm66018 } 346317cadca8Slm66018 } else if (vio_ver_is_supported(vd->version, 1, 0)) { 346417cadca8Slm66018 vd->operations = VD_OP_MASK_READ | VD_OP_MASK_WRITE; 346517cadca8Slm66018 } 346617cadca8Slm66018 346717cadca8Slm66018 /* we should have already agreed on a version */ 346817cadca8Slm66018 ASSERT(vd->operations != 0); 346917cadca8Slm66018 } 347017cadca8Slm66018 34711ae08745Sheppo static int 34721ae08745Sheppo vd_process_attr_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 34731ae08745Sheppo { 34741ae08745Sheppo vd_attr_msg_t *attr_msg = (vd_attr_msg_t *)msg; 34753c96341aSnarayan int status, retry = 0; 34761ae08745Sheppo 34771ae08745Sheppo 34781ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 34791ae08745Sheppo 34801ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 34811ae08745Sheppo VIO_ATTR_INFO)) { 3482d10e4ef2Snarayan PR0("Message is not an attribute message"); 3483d10e4ef2Snarayan return (ENOMSG); 34841ae08745Sheppo } 34851ae08745Sheppo 34861ae08745Sheppo if (msglen != sizeof (*attr_msg)) { 34873af08d82Slm66018 PR0("Expected %lu-byte attribute message; " 34881ae08745Sheppo "received %lu bytes", sizeof (*attr_msg), msglen); 34891ae08745Sheppo return (EBADMSG); 34901ae08745Sheppo } 34911ae08745Sheppo 34921ae08745Sheppo if (attr_msg->max_xfer_sz == 0) { 34933af08d82Slm66018 PR0("Received maximum transfer size of 0 from client"); 34941ae08745Sheppo return (EBADMSG); 34951ae08745Sheppo } 34961ae08745Sheppo 34971ae08745Sheppo if ((attr_msg->xfer_mode != VIO_DESC_MODE) && 3498f0ca1d9aSsb155480 (attr_msg->xfer_mode != VIO_DRING_MODE_V1_0)) { 34993af08d82Slm66018 PR0("Client requested unsupported transfer mode"); 35001ae08745Sheppo return (EBADMSG); 35011ae08745Sheppo } 35021ae08745Sheppo 35033c96341aSnarayan /* 35043c96341aSnarayan * check if the underlying disk is ready, if not try accessing 35053c96341aSnarayan * the device again. Open the vdisk device and extract info 35063c96341aSnarayan * about it, as this is needed to respond to the attr info msg 35073c96341aSnarayan */ 35083c96341aSnarayan if ((vd->initialized & VD_DISK_READY) == 0) { 35093c96341aSnarayan PR0("Retry setting up disk (%s)", vd->device_path); 35103c96341aSnarayan do { 35113c96341aSnarayan status = vd_setup_vd(vd); 35123c96341aSnarayan if (status != EAGAIN || ++retry > vds_dev_retries) 35133c96341aSnarayan break; 35143c96341aSnarayan 35153c96341aSnarayan /* incremental delay */ 35163c96341aSnarayan delay(drv_usectohz(vds_dev_delay)); 35173c96341aSnarayan 35183c96341aSnarayan /* if vdisk is no longer enabled - return error */ 35193c96341aSnarayan if (!vd_enabled(vd)) 35203c96341aSnarayan return (ENXIO); 35213c96341aSnarayan 35223c96341aSnarayan } while (status == EAGAIN); 35233c96341aSnarayan 35243c96341aSnarayan if (status) 35253c96341aSnarayan return (ENXIO); 35263c96341aSnarayan 35273c96341aSnarayan vd->initialized |= VD_DISK_READY; 35283c96341aSnarayan ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR); 35298fce2fd6Sachartre PR0("vdisk_type = %s, volume = %s, file = %s, nslices = %u", 35303c96341aSnarayan ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"), 35318fce2fd6Sachartre (vd->volume ? "yes" : "no"), 35323c96341aSnarayan (vd->file ? "yes" : "no"), 35333c96341aSnarayan vd->nslices); 35343c96341aSnarayan } 35353c96341aSnarayan 35361ae08745Sheppo /* Success: valid message and transfer mode */ 35371ae08745Sheppo vd->xfer_mode = attr_msg->xfer_mode; 35383af08d82Slm66018 35391ae08745Sheppo if (vd->xfer_mode == VIO_DESC_MODE) { 35403af08d82Slm66018 35411ae08745Sheppo /* 35421ae08745Sheppo * The vd_dring_inband_msg_t contains one cookie; need room 35431ae08745Sheppo * for up to n-1 more cookies, where "n" is the number of full 35441ae08745Sheppo * pages plus possibly one partial page required to cover 35451ae08745Sheppo * "max_xfer_sz". Add room for one more cookie if 35461ae08745Sheppo * "max_xfer_sz" isn't an integral multiple of the page size. 35471ae08745Sheppo * Must first get the maximum transfer size in bytes. 35481ae08745Sheppo */ 35491ae08745Sheppo size_t max_xfer_bytes = attr_msg->vdisk_block_size ? 35501ae08745Sheppo attr_msg->vdisk_block_size*attr_msg->max_xfer_sz : 35511ae08745Sheppo attr_msg->max_xfer_sz; 35521ae08745Sheppo size_t max_inband_msglen = 35531ae08745Sheppo sizeof (vd_dring_inband_msg_t) + 35541ae08745Sheppo ((max_xfer_bytes/PAGESIZE + 35551ae08745Sheppo ((max_xfer_bytes % PAGESIZE) ? 1 : 0))* 35561ae08745Sheppo (sizeof (ldc_mem_cookie_t))); 35571ae08745Sheppo 35581ae08745Sheppo /* 35591ae08745Sheppo * Set the maximum expected message length to 35601ae08745Sheppo * accommodate in-band-descriptor messages with all 35611ae08745Sheppo * their cookies 35621ae08745Sheppo */ 35631ae08745Sheppo vd->max_msglen = MAX(vd->max_msglen, max_inband_msglen); 3564d10e4ef2Snarayan 3565d10e4ef2Snarayan /* 3566d10e4ef2Snarayan * Initialize the data structure for processing in-band I/O 3567d10e4ef2Snarayan * request descriptors 3568d10e4ef2Snarayan */ 3569d10e4ef2Snarayan vd->inband_task.vd = vd; 35703af08d82Slm66018 vd->inband_task.msg = kmem_alloc(vd->max_msglen, KM_SLEEP); 3571d10e4ef2Snarayan vd->inband_task.index = 0; 3572d10e4ef2Snarayan vd->inband_task.type = VD_FINAL_RANGE_TASK; /* range == 1 */ 35731ae08745Sheppo } 35741ae08745Sheppo 3575e1ebb9ecSlm66018 /* Return the device's block size and max transfer size to the client */ 35762f5224aeSachartre attr_msg->vdisk_block_size = vd->vdisk_block_size; 3577e1ebb9ecSlm66018 attr_msg->max_xfer_sz = vd->max_xfer_sz; 3578e1ebb9ecSlm66018 35791ae08745Sheppo attr_msg->vdisk_size = vd->vdisk_size; 35801ae08745Sheppo attr_msg->vdisk_type = vd->vdisk_type; 358117cadca8Slm66018 attr_msg->vdisk_media = vd->vdisk_media; 358217cadca8Slm66018 358317cadca8Slm66018 /* Discover and save the list of supported VD_OP_XXX operations */ 358417cadca8Slm66018 vd_set_exported_operations(vd); 358517cadca8Slm66018 attr_msg->operations = vd->operations; 358617cadca8Slm66018 35871ae08745Sheppo PR0("%s", VD_CLIENT(vd)); 35883af08d82Slm66018 35893af08d82Slm66018 ASSERT(vd->dring_task == NULL); 35903af08d82Slm66018 35911ae08745Sheppo return (0); 35921ae08745Sheppo } 35931ae08745Sheppo 35941ae08745Sheppo static int 35951ae08745Sheppo vd_process_dring_reg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 35961ae08745Sheppo { 35971ae08745Sheppo int status; 35981ae08745Sheppo size_t expected; 35991ae08745Sheppo ldc_mem_info_t dring_minfo; 36001ae08745Sheppo vio_dring_reg_msg_t *reg_msg = (vio_dring_reg_msg_t *)msg; 36011ae08745Sheppo 36021ae08745Sheppo 36031ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 36041ae08745Sheppo 36051ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 36061ae08745Sheppo VIO_DRING_REG)) { 3607d10e4ef2Snarayan PR0("Message is not a register-dring message"); 3608d10e4ef2Snarayan return (ENOMSG); 36091ae08745Sheppo } 36101ae08745Sheppo 36111ae08745Sheppo if (msglen < sizeof (*reg_msg)) { 36123af08d82Slm66018 PR0("Expected at least %lu-byte register-dring message; " 36131ae08745Sheppo "received %lu bytes", sizeof (*reg_msg), msglen); 36141ae08745Sheppo return (EBADMSG); 36151ae08745Sheppo } 36161ae08745Sheppo 36171ae08745Sheppo expected = sizeof (*reg_msg) + 36181ae08745Sheppo (reg_msg->ncookies - 1)*(sizeof (reg_msg->cookie[0])); 36191ae08745Sheppo if (msglen != expected) { 36203af08d82Slm66018 PR0("Expected %lu-byte register-dring message; " 36211ae08745Sheppo "received %lu bytes", expected, msglen); 36221ae08745Sheppo return (EBADMSG); 36231ae08745Sheppo } 36241ae08745Sheppo 36251ae08745Sheppo if (vd->initialized & VD_DRING) { 36263af08d82Slm66018 PR0("A dring was previously registered; only support one"); 36271ae08745Sheppo return (EBADMSG); 36281ae08745Sheppo } 36291ae08745Sheppo 3630d10e4ef2Snarayan if (reg_msg->num_descriptors > INT32_MAX) { 36313af08d82Slm66018 PR0("reg_msg->num_descriptors = %u; must be <= %u (%s)", 3632d10e4ef2Snarayan reg_msg->ncookies, INT32_MAX, STRINGIZE(INT32_MAX)); 3633d10e4ef2Snarayan return (EBADMSG); 3634d10e4ef2Snarayan } 3635d10e4ef2Snarayan 36361ae08745Sheppo if (reg_msg->ncookies != 1) { 36371ae08745Sheppo /* 36381ae08745Sheppo * In addition to fixing the assertion in the success case 36391ae08745Sheppo * below, supporting drings which require more than one 36401ae08745Sheppo * "cookie" requires increasing the value of vd->max_msglen 36411ae08745Sheppo * somewhere in the code path prior to receiving the message 36421ae08745Sheppo * which results in calling this function. Note that without 36431ae08745Sheppo * making this change, the larger message size required to 36441ae08745Sheppo * accommodate multiple cookies cannot be successfully 36451ae08745Sheppo * received, so this function will not even get called. 36461ae08745Sheppo * Gracefully accommodating more dring cookies might 36471ae08745Sheppo * reasonably demand exchanging an additional attribute or 36481ae08745Sheppo * making a minor protocol adjustment 36491ae08745Sheppo */ 36503af08d82Slm66018 PR0("reg_msg->ncookies = %u != 1", reg_msg->ncookies); 36511ae08745Sheppo return (EBADMSG); 36521ae08745Sheppo } 36531ae08745Sheppo 36541ae08745Sheppo status = ldc_mem_dring_map(vd->ldc_handle, reg_msg->cookie, 36551ae08745Sheppo reg_msg->ncookies, reg_msg->num_descriptors, 36564bac2208Snarayan reg_msg->descriptor_size, LDC_DIRECT_MAP, &vd->dring_handle); 36571ae08745Sheppo if (status != 0) { 36583af08d82Slm66018 PR0("ldc_mem_dring_map() returned errno %d", status); 36591ae08745Sheppo return (status); 36601ae08745Sheppo } 36611ae08745Sheppo 36621ae08745Sheppo /* 36631ae08745Sheppo * To remove the need for this assertion, must call 36641ae08745Sheppo * ldc_mem_dring_nextcookie() successfully ncookies-1 times after a 36651ae08745Sheppo * successful call to ldc_mem_dring_map() 36661ae08745Sheppo */ 36671ae08745Sheppo ASSERT(reg_msg->ncookies == 1); 36681ae08745Sheppo 36691ae08745Sheppo if ((status = 36701ae08745Sheppo ldc_mem_dring_info(vd->dring_handle, &dring_minfo)) != 0) { 36713af08d82Slm66018 PR0("ldc_mem_dring_info() returned errno %d", status); 36721ae08745Sheppo if ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0) 36733af08d82Slm66018 PR0("ldc_mem_dring_unmap() returned errno %d", status); 36741ae08745Sheppo return (status); 36751ae08745Sheppo } 36761ae08745Sheppo 36771ae08745Sheppo if (dring_minfo.vaddr == NULL) { 36783af08d82Slm66018 PR0("Descriptor ring virtual address is NULL"); 36790a55fbb7Slm66018 return (ENXIO); 36801ae08745Sheppo } 36811ae08745Sheppo 36821ae08745Sheppo 3683d10e4ef2Snarayan /* Initialize for valid message and mapped dring */ 36841ae08745Sheppo PR1("descriptor size = %u, dring length = %u", 36851ae08745Sheppo vd->descriptor_size, vd->dring_len); 36861ae08745Sheppo vd->initialized |= VD_DRING; 36871ae08745Sheppo vd->dring_ident = 1; /* "There Can Be Only One" */ 36881ae08745Sheppo vd->dring = dring_minfo.vaddr; 36891ae08745Sheppo vd->descriptor_size = reg_msg->descriptor_size; 36901ae08745Sheppo vd->dring_len = reg_msg->num_descriptors; 36911ae08745Sheppo reg_msg->dring_ident = vd->dring_ident; 3692d10e4ef2Snarayan 3693d10e4ef2Snarayan /* 3694d10e4ef2Snarayan * Allocate and initialize a "shadow" array of data structures for 3695d10e4ef2Snarayan * tasks to process I/O requests in dring elements 3696d10e4ef2Snarayan */ 3697d10e4ef2Snarayan vd->dring_task = 3698d10e4ef2Snarayan kmem_zalloc((sizeof (*vd->dring_task)) * vd->dring_len, KM_SLEEP); 3699d10e4ef2Snarayan for (int i = 0; i < vd->dring_len; i++) { 3700d10e4ef2Snarayan vd->dring_task[i].vd = vd; 3701d10e4ef2Snarayan vd->dring_task[i].index = i; 3702d10e4ef2Snarayan vd->dring_task[i].request = &VD_DRING_ELEM(i)->payload; 37034bac2208Snarayan 37044bac2208Snarayan status = ldc_mem_alloc_handle(vd->ldc_handle, 37054bac2208Snarayan &(vd->dring_task[i].mhdl)); 37064bac2208Snarayan if (status) { 37073af08d82Slm66018 PR0("ldc_mem_alloc_handle() returned err %d ", status); 37084bac2208Snarayan return (ENXIO); 37094bac2208Snarayan } 37103af08d82Slm66018 37113af08d82Slm66018 vd->dring_task[i].msg = kmem_alloc(vd->max_msglen, KM_SLEEP); 3712d10e4ef2Snarayan } 3713d10e4ef2Snarayan 37141ae08745Sheppo return (0); 37151ae08745Sheppo } 37161ae08745Sheppo 37171ae08745Sheppo static int 37181ae08745Sheppo vd_process_dring_unreg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 37191ae08745Sheppo { 37201ae08745Sheppo vio_dring_unreg_msg_t *unreg_msg = (vio_dring_unreg_msg_t *)msg; 37211ae08745Sheppo 37221ae08745Sheppo 37231ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 37241ae08745Sheppo 37251ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 37261ae08745Sheppo VIO_DRING_UNREG)) { 3727d10e4ef2Snarayan PR0("Message is not an unregister-dring message"); 3728d10e4ef2Snarayan return (ENOMSG); 37291ae08745Sheppo } 37301ae08745Sheppo 37311ae08745Sheppo if (msglen != sizeof (*unreg_msg)) { 37323af08d82Slm66018 PR0("Expected %lu-byte unregister-dring message; " 37331ae08745Sheppo "received %lu bytes", sizeof (*unreg_msg), msglen); 37341ae08745Sheppo return (EBADMSG); 37351ae08745Sheppo } 37361ae08745Sheppo 37371ae08745Sheppo if (unreg_msg->dring_ident != vd->dring_ident) { 37383af08d82Slm66018 PR0("Expected dring ident %lu; received %lu", 37391ae08745Sheppo vd->dring_ident, unreg_msg->dring_ident); 37401ae08745Sheppo return (EBADMSG); 37411ae08745Sheppo } 37421ae08745Sheppo 37431ae08745Sheppo return (0); 37441ae08745Sheppo } 37451ae08745Sheppo 37461ae08745Sheppo static int 37471ae08745Sheppo process_rdx_msg(vio_msg_t *msg, size_t msglen) 37481ae08745Sheppo { 37491ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 37501ae08745Sheppo 3751d10e4ef2Snarayan if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX)) { 3752d10e4ef2Snarayan PR0("Message is not an RDX message"); 3753d10e4ef2Snarayan return (ENOMSG); 3754d10e4ef2Snarayan } 37551ae08745Sheppo 37561ae08745Sheppo if (msglen != sizeof (vio_rdx_msg_t)) { 37573af08d82Slm66018 PR0("Expected %lu-byte RDX message; received %lu bytes", 37581ae08745Sheppo sizeof (vio_rdx_msg_t), msglen); 37591ae08745Sheppo return (EBADMSG); 37601ae08745Sheppo } 37611ae08745Sheppo 3762d10e4ef2Snarayan PR0("Valid RDX message"); 37631ae08745Sheppo return (0); 37641ae08745Sheppo } 37651ae08745Sheppo 37661ae08745Sheppo static int 37671ae08745Sheppo vd_check_seq_num(vd_t *vd, uint64_t seq_num) 37681ae08745Sheppo { 37691ae08745Sheppo if ((vd->initialized & VD_SEQ_NUM) && (seq_num != vd->seq_num + 1)) { 37703af08d82Slm66018 PR0("Received seq_num %lu; expected %lu", 37711ae08745Sheppo seq_num, (vd->seq_num + 1)); 37723af08d82Slm66018 PR0("initiating soft reset"); 3773d10e4ef2Snarayan vd_need_reset(vd, B_FALSE); 37741ae08745Sheppo return (1); 37751ae08745Sheppo } 37761ae08745Sheppo 37771ae08745Sheppo vd->seq_num = seq_num; 37781ae08745Sheppo vd->initialized |= VD_SEQ_NUM; /* superfluous after first time... */ 37791ae08745Sheppo return (0); 37801ae08745Sheppo } 37811ae08745Sheppo 37821ae08745Sheppo /* 37831ae08745Sheppo * Return the expected size of an inband-descriptor message with all the 37841ae08745Sheppo * cookies it claims to include 37851ae08745Sheppo */ 37861ae08745Sheppo static size_t 37871ae08745Sheppo expected_inband_size(vd_dring_inband_msg_t *msg) 37881ae08745Sheppo { 37891ae08745Sheppo return ((sizeof (*msg)) + 37901ae08745Sheppo (msg->payload.ncookies - 1)*(sizeof (msg->payload.cookie[0]))); 37911ae08745Sheppo } 37921ae08745Sheppo 37931ae08745Sheppo /* 37941ae08745Sheppo * Process an in-band descriptor message: used with clients like OBP, with 37951ae08745Sheppo * which vds exchanges descriptors within VIO message payloads, rather than 37961ae08745Sheppo * operating on them within a descriptor ring 37971ae08745Sheppo */ 37981ae08745Sheppo static int 37993af08d82Slm66018 vd_process_desc_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 38001ae08745Sheppo { 38011ae08745Sheppo size_t expected; 38021ae08745Sheppo vd_dring_inband_msg_t *desc_msg = (vd_dring_inband_msg_t *)msg; 38031ae08745Sheppo 38041ae08745Sheppo 38051ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 38061ae08745Sheppo 38071ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO, 3808d10e4ef2Snarayan VIO_DESC_DATA)) { 3809d10e4ef2Snarayan PR1("Message is not an in-band-descriptor message"); 3810d10e4ef2Snarayan return (ENOMSG); 3811d10e4ef2Snarayan } 38121ae08745Sheppo 38131ae08745Sheppo if (msglen < sizeof (*desc_msg)) { 38143af08d82Slm66018 PR0("Expected at least %lu-byte descriptor message; " 38151ae08745Sheppo "received %lu bytes", sizeof (*desc_msg), msglen); 38161ae08745Sheppo return (EBADMSG); 38171ae08745Sheppo } 38181ae08745Sheppo 38191ae08745Sheppo if (msglen != (expected = expected_inband_size(desc_msg))) { 38203af08d82Slm66018 PR0("Expected %lu-byte descriptor message; " 38211ae08745Sheppo "received %lu bytes", expected, msglen); 38221ae08745Sheppo return (EBADMSG); 38231ae08745Sheppo } 38241ae08745Sheppo 3825d10e4ef2Snarayan if (vd_check_seq_num(vd, desc_msg->hdr.seq_num) != 0) 38261ae08745Sheppo return (EBADMSG); 38271ae08745Sheppo 3828d10e4ef2Snarayan /* 3829d10e4ef2Snarayan * Valid message: Set up the in-band descriptor task and process the 3830d10e4ef2Snarayan * request. Arrange to acknowledge the client's message, unless an 3831d10e4ef2Snarayan * error processing the descriptor task results in setting 3832d10e4ef2Snarayan * VIO_SUBTYPE_NACK 3833d10e4ef2Snarayan */ 3834d10e4ef2Snarayan PR1("Valid in-band-descriptor message"); 3835d10e4ef2Snarayan msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 38363af08d82Slm66018 38373af08d82Slm66018 ASSERT(vd->inband_task.msg != NULL); 38383af08d82Slm66018 38393af08d82Slm66018 bcopy(msg, vd->inband_task.msg, msglen); 3840d10e4ef2Snarayan vd->inband_task.msglen = msglen; 38413af08d82Slm66018 38423af08d82Slm66018 /* 38433af08d82Slm66018 * The task request is now the payload of the message 38443af08d82Slm66018 * that was just copied into the body of the task. 38453af08d82Slm66018 */ 38463af08d82Slm66018 desc_msg = (vd_dring_inband_msg_t *)vd->inband_task.msg; 3847d10e4ef2Snarayan vd->inband_task.request = &desc_msg->payload; 38483af08d82Slm66018 3849d10e4ef2Snarayan return (vd_process_task(&vd->inband_task)); 38501ae08745Sheppo } 38511ae08745Sheppo 38521ae08745Sheppo static int 3853d10e4ef2Snarayan vd_process_element(vd_t *vd, vd_task_type_t type, uint32_t idx, 38543af08d82Slm66018 vio_msg_t *msg, size_t msglen) 38551ae08745Sheppo { 38561ae08745Sheppo int status; 3857d10e4ef2Snarayan boolean_t ready; 3858d10e4ef2Snarayan vd_dring_entry_t *elem = VD_DRING_ELEM(idx); 38591ae08745Sheppo 38601ae08745Sheppo 3861d10e4ef2Snarayan /* Accept the updated dring element */ 3862d10e4ef2Snarayan if ((status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) { 38633af08d82Slm66018 PR0("ldc_mem_dring_acquire() returned errno %d", status); 38641ae08745Sheppo return (status); 38651ae08745Sheppo } 3866d10e4ef2Snarayan ready = (elem->hdr.dstate == VIO_DESC_READY); 3867d10e4ef2Snarayan if (ready) { 3868d10e4ef2Snarayan elem->hdr.dstate = VIO_DESC_ACCEPTED; 3869d10e4ef2Snarayan } else { 38703af08d82Slm66018 PR0("descriptor %u not ready", idx); 3871d10e4ef2Snarayan VD_DUMP_DRING_ELEM(elem); 3872d10e4ef2Snarayan } 3873d10e4ef2Snarayan if ((status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) { 38743af08d82Slm66018 PR0("ldc_mem_dring_release() returned errno %d", status); 38751ae08745Sheppo return (status); 38761ae08745Sheppo } 3877d10e4ef2Snarayan if (!ready) 3878d10e4ef2Snarayan return (EBUSY); 38791ae08745Sheppo 38801ae08745Sheppo 3881d10e4ef2Snarayan /* Initialize a task and process the accepted element */ 3882d10e4ef2Snarayan PR1("Processing dring element %u", idx); 3883d10e4ef2Snarayan vd->dring_task[idx].type = type; 38843af08d82Slm66018 38853af08d82Slm66018 /* duplicate msg buf for cookies etc. */ 38863af08d82Slm66018 bcopy(msg, vd->dring_task[idx].msg, msglen); 38873af08d82Slm66018 3888d10e4ef2Snarayan vd->dring_task[idx].msglen = msglen; 3889205eeb1aSlm66018 return (vd_process_task(&vd->dring_task[idx])); 38901ae08745Sheppo } 38911ae08745Sheppo 38921ae08745Sheppo static int 3893d10e4ef2Snarayan vd_process_element_range(vd_t *vd, int start, int end, 38943af08d82Slm66018 vio_msg_t *msg, size_t msglen) 3895d10e4ef2Snarayan { 3896d10e4ef2Snarayan int i, n, nelem, status = 0; 3897d10e4ef2Snarayan boolean_t inprogress = B_FALSE; 3898d10e4ef2Snarayan vd_task_type_t type; 3899d10e4ef2Snarayan 3900d10e4ef2Snarayan 3901d10e4ef2Snarayan ASSERT(start >= 0); 3902d10e4ef2Snarayan ASSERT(end >= 0); 3903d10e4ef2Snarayan 3904d10e4ef2Snarayan /* 3905d10e4ef2Snarayan * Arrange to acknowledge the client's message, unless an error 3906d10e4ef2Snarayan * processing one of the dring elements results in setting 3907d10e4ef2Snarayan * VIO_SUBTYPE_NACK 3908d10e4ef2Snarayan */ 3909d10e4ef2Snarayan msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 3910d10e4ef2Snarayan 3911d10e4ef2Snarayan /* 3912d10e4ef2Snarayan * Process the dring elements in the range 3913d10e4ef2Snarayan */ 3914d10e4ef2Snarayan nelem = ((end < start) ? end + vd->dring_len : end) - start + 1; 3915d10e4ef2Snarayan for (i = start, n = nelem; n > 0; i = (i + 1) % vd->dring_len, n--) { 3916d10e4ef2Snarayan ((vio_dring_msg_t *)msg)->end_idx = i; 3917d10e4ef2Snarayan type = (n == 1) ? VD_FINAL_RANGE_TASK : VD_NONFINAL_RANGE_TASK; 39183af08d82Slm66018 status = vd_process_element(vd, type, i, msg, msglen); 3919d10e4ef2Snarayan if (status == EINPROGRESS) 3920d10e4ef2Snarayan inprogress = B_TRUE; 3921d10e4ef2Snarayan else if (status != 0) 3922d10e4ef2Snarayan break; 3923d10e4ef2Snarayan } 3924d10e4ef2Snarayan 3925d10e4ef2Snarayan /* 3926d10e4ef2Snarayan * If some, but not all, operations of a multi-element range are in 3927d10e4ef2Snarayan * progress, wait for other operations to complete before returning 3928d10e4ef2Snarayan * (which will result in "ack" or "nack" of the message). Note that 3929d10e4ef2Snarayan * all outstanding operations will need to complete, not just the ones 3930d10e4ef2Snarayan * corresponding to the current range of dring elements; howevever, as 3931d10e4ef2Snarayan * this situation is an error case, performance is less critical. 3932d10e4ef2Snarayan */ 3933d10e4ef2Snarayan if ((nelem > 1) && (status != EINPROGRESS) && inprogress) 3934d10e4ef2Snarayan ddi_taskq_wait(vd->completionq); 3935d10e4ef2Snarayan 3936d10e4ef2Snarayan return (status); 3937d10e4ef2Snarayan } 3938d10e4ef2Snarayan 3939d10e4ef2Snarayan static int 39403af08d82Slm66018 vd_process_dring_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 39411ae08745Sheppo { 39421ae08745Sheppo vio_dring_msg_t *dring_msg = (vio_dring_msg_t *)msg; 39431ae08745Sheppo 39441ae08745Sheppo 39451ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 39461ae08745Sheppo 39471ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO, 39481ae08745Sheppo VIO_DRING_DATA)) { 3949d10e4ef2Snarayan PR1("Message is not a dring-data message"); 3950d10e4ef2Snarayan return (ENOMSG); 39511ae08745Sheppo } 39521ae08745Sheppo 39531ae08745Sheppo if (msglen != sizeof (*dring_msg)) { 39543af08d82Slm66018 PR0("Expected %lu-byte dring message; received %lu bytes", 39551ae08745Sheppo sizeof (*dring_msg), msglen); 39561ae08745Sheppo return (EBADMSG); 39571ae08745Sheppo } 39581ae08745Sheppo 3959d10e4ef2Snarayan if (vd_check_seq_num(vd, dring_msg->seq_num) != 0) 39601ae08745Sheppo return (EBADMSG); 39611ae08745Sheppo 39621ae08745Sheppo if (dring_msg->dring_ident != vd->dring_ident) { 39633af08d82Slm66018 PR0("Expected dring ident %lu; received ident %lu", 39641ae08745Sheppo vd->dring_ident, dring_msg->dring_ident); 39651ae08745Sheppo return (EBADMSG); 39661ae08745Sheppo } 39671ae08745Sheppo 3968d10e4ef2Snarayan if (dring_msg->start_idx >= vd->dring_len) { 39693af08d82Slm66018 PR0("\"start_idx\" = %u; must be less than %u", 3970d10e4ef2Snarayan dring_msg->start_idx, vd->dring_len); 3971d10e4ef2Snarayan return (EBADMSG); 3972d10e4ef2Snarayan } 39731ae08745Sheppo 3974d10e4ef2Snarayan if ((dring_msg->end_idx < 0) || 3975d10e4ef2Snarayan (dring_msg->end_idx >= vd->dring_len)) { 39763af08d82Slm66018 PR0("\"end_idx\" = %u; must be >= 0 and less than %u", 3977d10e4ef2Snarayan dring_msg->end_idx, vd->dring_len); 3978d10e4ef2Snarayan return (EBADMSG); 3979d10e4ef2Snarayan } 3980d10e4ef2Snarayan 3981d10e4ef2Snarayan /* Valid message; process range of updated dring elements */ 3982d10e4ef2Snarayan PR1("Processing descriptor range, start = %u, end = %u", 3983d10e4ef2Snarayan dring_msg->start_idx, dring_msg->end_idx); 3984d10e4ef2Snarayan return (vd_process_element_range(vd, dring_msg->start_idx, 39853af08d82Slm66018 dring_msg->end_idx, msg, msglen)); 39861ae08745Sheppo } 39871ae08745Sheppo 39881ae08745Sheppo static int 39891ae08745Sheppo recv_msg(ldc_handle_t ldc_handle, void *msg, size_t *nbytes) 39901ae08745Sheppo { 39911ae08745Sheppo int retry, status; 39921ae08745Sheppo size_t size = *nbytes; 39931ae08745Sheppo 39941ae08745Sheppo 39951ae08745Sheppo for (retry = 0, status = ETIMEDOUT; 39961ae08745Sheppo retry < vds_ldc_retries && status == ETIMEDOUT; 39971ae08745Sheppo retry++) { 39981ae08745Sheppo PR1("ldc_read() attempt %d", (retry + 1)); 39991ae08745Sheppo *nbytes = size; 40001ae08745Sheppo status = ldc_read(ldc_handle, msg, nbytes); 40011ae08745Sheppo } 40021ae08745Sheppo 40033af08d82Slm66018 if (status) { 40043af08d82Slm66018 PR0("ldc_read() returned errno %d", status); 40053af08d82Slm66018 if (status != ECONNRESET) 40063af08d82Slm66018 return (ENOMSG); 40071ae08745Sheppo return (status); 40081ae08745Sheppo } else if (*nbytes == 0) { 40091ae08745Sheppo PR1("ldc_read() returned 0 and no message read"); 40101ae08745Sheppo return (ENOMSG); 40111ae08745Sheppo } 40121ae08745Sheppo 40131ae08745Sheppo PR1("RCVD %lu-byte message", *nbytes); 40141ae08745Sheppo return (0); 40151ae08745Sheppo } 40161ae08745Sheppo 40171ae08745Sheppo static int 40183af08d82Slm66018 vd_do_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 40191ae08745Sheppo { 40201ae08745Sheppo int status; 40211ae08745Sheppo 40221ae08745Sheppo 40231ae08745Sheppo PR1("Processing (%x/%x/%x) message", msg->tag.vio_msgtype, 40241ae08745Sheppo msg->tag.vio_subtype, msg->tag.vio_subtype_env); 40253af08d82Slm66018 #ifdef DEBUG 40263af08d82Slm66018 vd_decode_tag(msg); 40273af08d82Slm66018 #endif 40281ae08745Sheppo 40291ae08745Sheppo /* 40301ae08745Sheppo * Validate session ID up front, since it applies to all messages 40311ae08745Sheppo * once set 40321ae08745Sheppo */ 40331ae08745Sheppo if ((msg->tag.vio_sid != vd->sid) && (vd->initialized & VD_SID)) { 40343af08d82Slm66018 PR0("Expected SID %u, received %u", vd->sid, 40351ae08745Sheppo msg->tag.vio_sid); 40361ae08745Sheppo return (EBADMSG); 40371ae08745Sheppo } 40381ae08745Sheppo 40393af08d82Slm66018 PR1("\tWhile in state %d (%s)", vd->state, vd_decode_state(vd->state)); 40401ae08745Sheppo 40411ae08745Sheppo /* 40421ae08745Sheppo * Process the received message based on connection state 40431ae08745Sheppo */ 40441ae08745Sheppo switch (vd->state) { 40451ae08745Sheppo case VD_STATE_INIT: /* expect version message */ 40460a55fbb7Slm66018 if ((status = vd_process_ver_msg(vd, msg, msglen)) != 0) 40471ae08745Sheppo return (status); 40481ae08745Sheppo 40491ae08745Sheppo /* Version negotiated, move to that state */ 40501ae08745Sheppo vd->state = VD_STATE_VER; 40511ae08745Sheppo return (0); 40521ae08745Sheppo 40531ae08745Sheppo case VD_STATE_VER: /* expect attribute message */ 40541ae08745Sheppo if ((status = vd_process_attr_msg(vd, msg, msglen)) != 0) 40551ae08745Sheppo return (status); 40561ae08745Sheppo 40571ae08745Sheppo /* Attributes exchanged, move to that state */ 40581ae08745Sheppo vd->state = VD_STATE_ATTR; 40591ae08745Sheppo return (0); 40601ae08745Sheppo 40611ae08745Sheppo case VD_STATE_ATTR: 40621ae08745Sheppo switch (vd->xfer_mode) { 40631ae08745Sheppo case VIO_DESC_MODE: /* expect RDX message */ 40641ae08745Sheppo if ((status = process_rdx_msg(msg, msglen)) != 0) 40651ae08745Sheppo return (status); 40661ae08745Sheppo 40671ae08745Sheppo /* Ready to receive in-band descriptors */ 40681ae08745Sheppo vd->state = VD_STATE_DATA; 40691ae08745Sheppo return (0); 40701ae08745Sheppo 4071f0ca1d9aSsb155480 case VIO_DRING_MODE_V1_0: /* expect register-dring message */ 40721ae08745Sheppo if ((status = 40731ae08745Sheppo vd_process_dring_reg_msg(vd, msg, msglen)) != 0) 40741ae08745Sheppo return (status); 40751ae08745Sheppo 40761ae08745Sheppo /* One dring negotiated, move to that state */ 40771ae08745Sheppo vd->state = VD_STATE_DRING; 40781ae08745Sheppo return (0); 40791ae08745Sheppo 40801ae08745Sheppo default: 40811ae08745Sheppo ASSERT("Unsupported transfer mode"); 40823af08d82Slm66018 PR0("Unsupported transfer mode"); 40831ae08745Sheppo return (ENOTSUP); 40841ae08745Sheppo } 40851ae08745Sheppo 40861ae08745Sheppo case VD_STATE_DRING: /* expect RDX, register-dring, or unreg-dring */ 40871ae08745Sheppo if ((status = process_rdx_msg(msg, msglen)) == 0) { 40881ae08745Sheppo /* Ready to receive data */ 40891ae08745Sheppo vd->state = VD_STATE_DATA; 40901ae08745Sheppo return (0); 40911ae08745Sheppo } else if (status != ENOMSG) { 40921ae08745Sheppo return (status); 40931ae08745Sheppo } 40941ae08745Sheppo 40951ae08745Sheppo 40961ae08745Sheppo /* 40971ae08745Sheppo * If another register-dring message is received, stay in 40981ae08745Sheppo * dring state in case the client sends RDX; although the 40991ae08745Sheppo * protocol allows multiple drings, this server does not 41001ae08745Sheppo * support using more than one 41011ae08745Sheppo */ 41021ae08745Sheppo if ((status = 41031ae08745Sheppo vd_process_dring_reg_msg(vd, msg, msglen)) != ENOMSG) 41041ae08745Sheppo return (status); 41051ae08745Sheppo 41061ae08745Sheppo /* 41071ae08745Sheppo * Acknowledge an unregister-dring message, but reset the 41081ae08745Sheppo * connection anyway: Although the protocol allows 41091ae08745Sheppo * unregistering drings, this server cannot serve a vdisk 41101ae08745Sheppo * without its only dring 41111ae08745Sheppo */ 41121ae08745Sheppo status = vd_process_dring_unreg_msg(vd, msg, msglen); 41131ae08745Sheppo return ((status == 0) ? ENOTSUP : status); 41141ae08745Sheppo 41151ae08745Sheppo case VD_STATE_DATA: 41161ae08745Sheppo switch (vd->xfer_mode) { 41171ae08745Sheppo case VIO_DESC_MODE: /* expect in-band-descriptor message */ 41183af08d82Slm66018 return (vd_process_desc_msg(vd, msg, msglen)); 41191ae08745Sheppo 4120f0ca1d9aSsb155480 case VIO_DRING_MODE_V1_0: /* expect dring-data or unreg-dring */ 41211ae08745Sheppo /* 41221ae08745Sheppo * Typically expect dring-data messages, so handle 41231ae08745Sheppo * them first 41241ae08745Sheppo */ 41251ae08745Sheppo if ((status = vd_process_dring_msg(vd, msg, 41263af08d82Slm66018 msglen)) != ENOMSG) 41271ae08745Sheppo return (status); 41281ae08745Sheppo 41291ae08745Sheppo /* 41301ae08745Sheppo * Acknowledge an unregister-dring message, but reset 41311ae08745Sheppo * the connection anyway: Although the protocol 41321ae08745Sheppo * allows unregistering drings, this server cannot 41331ae08745Sheppo * serve a vdisk without its only dring 41341ae08745Sheppo */ 41351ae08745Sheppo status = vd_process_dring_unreg_msg(vd, msg, msglen); 41361ae08745Sheppo return ((status == 0) ? ENOTSUP : status); 41371ae08745Sheppo 41381ae08745Sheppo default: 41391ae08745Sheppo ASSERT("Unsupported transfer mode"); 41403af08d82Slm66018 PR0("Unsupported transfer mode"); 41411ae08745Sheppo return (ENOTSUP); 41421ae08745Sheppo } 41431ae08745Sheppo 41441ae08745Sheppo default: 41451ae08745Sheppo ASSERT("Invalid client connection state"); 41463af08d82Slm66018 PR0("Invalid client connection state"); 41471ae08745Sheppo return (ENOTSUP); 41481ae08745Sheppo } 41491ae08745Sheppo } 41501ae08745Sheppo 4151d10e4ef2Snarayan static int 41523af08d82Slm66018 vd_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 41531ae08745Sheppo { 41541ae08745Sheppo int status; 41551ae08745Sheppo boolean_t reset_ldc = B_FALSE; 4156205eeb1aSlm66018 vd_task_t task; 41571ae08745Sheppo 41581ae08745Sheppo /* 41591ae08745Sheppo * Check that the message is at least big enough for a "tag", so that 41601ae08745Sheppo * message processing can proceed based on tag-specified message type 41611ae08745Sheppo */ 41621ae08745Sheppo if (msglen < sizeof (vio_msg_tag_t)) { 41633af08d82Slm66018 PR0("Received short (%lu-byte) message", msglen); 41641ae08745Sheppo /* Can't "nack" short message, so drop the big hammer */ 41653af08d82Slm66018 PR0("initiating full reset"); 4166d10e4ef2Snarayan vd_need_reset(vd, B_TRUE); 4167d10e4ef2Snarayan return (EBADMSG); 41681ae08745Sheppo } 41691ae08745Sheppo 41701ae08745Sheppo /* 41711ae08745Sheppo * Process the message 41721ae08745Sheppo */ 41733af08d82Slm66018 switch (status = vd_do_process_msg(vd, msg, msglen)) { 41741ae08745Sheppo case 0: 41751ae08745Sheppo /* "ack" valid, successfully-processed messages */ 41761ae08745Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 41771ae08745Sheppo break; 41781ae08745Sheppo 4179d10e4ef2Snarayan case EINPROGRESS: 4180d10e4ef2Snarayan /* The completion handler will "ack" or "nack" the message */ 4181d10e4ef2Snarayan return (EINPROGRESS); 41821ae08745Sheppo case ENOMSG: 41833af08d82Slm66018 PR0("Received unexpected message"); 41841ae08745Sheppo _NOTE(FALLTHROUGH); 41851ae08745Sheppo case EBADMSG: 41861ae08745Sheppo case ENOTSUP: 4187205eeb1aSlm66018 /* "transport" error will cause NACK of invalid messages */ 41881ae08745Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 41891ae08745Sheppo break; 41901ae08745Sheppo 41911ae08745Sheppo default: 4192205eeb1aSlm66018 /* "transport" error will cause NACK of invalid messages */ 41931ae08745Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 41941ae08745Sheppo /* An LDC error probably occurred, so try resetting it */ 41951ae08745Sheppo reset_ldc = B_TRUE; 41961ae08745Sheppo break; 41971ae08745Sheppo } 41981ae08745Sheppo 41993af08d82Slm66018 PR1("\tResulting in state %d (%s)", vd->state, 42003af08d82Slm66018 vd_decode_state(vd->state)); 42013af08d82Slm66018 4202205eeb1aSlm66018 /* populate the task so we can dispatch it on the taskq */ 4203205eeb1aSlm66018 task.vd = vd; 4204205eeb1aSlm66018 task.msg = msg; 4205205eeb1aSlm66018 task.msglen = msglen; 4206205eeb1aSlm66018 4207205eeb1aSlm66018 /* 4208205eeb1aSlm66018 * Queue a task to send the notification that the operation completed. 4209205eeb1aSlm66018 * We need to ensure that requests are responded to in the correct 4210205eeb1aSlm66018 * order and since the taskq is processed serially this ordering 4211205eeb1aSlm66018 * is maintained. 4212205eeb1aSlm66018 */ 4213205eeb1aSlm66018 (void) ddi_taskq_dispatch(vd->completionq, vd_serial_notify, 4214205eeb1aSlm66018 &task, DDI_SLEEP); 4215205eeb1aSlm66018 4216205eeb1aSlm66018 /* 4217205eeb1aSlm66018 * To ensure handshake negotiations do not happen out of order, such 4218205eeb1aSlm66018 * requests that come through this path should not be done in parallel 4219205eeb1aSlm66018 * so we need to wait here until the response is sent to the client. 4220205eeb1aSlm66018 */ 4221205eeb1aSlm66018 ddi_taskq_wait(vd->completionq); 42221ae08745Sheppo 4223d10e4ef2Snarayan /* Arrange to reset the connection for nack'ed or failed messages */ 42243af08d82Slm66018 if ((status != 0) || reset_ldc) { 42253af08d82Slm66018 PR0("initiating %s reset", 42263af08d82Slm66018 (reset_ldc) ? "full" : "soft"); 4227d10e4ef2Snarayan vd_need_reset(vd, reset_ldc); 42283af08d82Slm66018 } 4229d10e4ef2Snarayan 4230d10e4ef2Snarayan return (status); 4231d10e4ef2Snarayan } 4232d10e4ef2Snarayan 4233d10e4ef2Snarayan static boolean_t 4234d10e4ef2Snarayan vd_enabled(vd_t *vd) 4235d10e4ef2Snarayan { 4236d10e4ef2Snarayan boolean_t enabled; 4237d10e4ef2Snarayan 4238d10e4ef2Snarayan mutex_enter(&vd->lock); 4239d10e4ef2Snarayan enabled = vd->enabled; 4240d10e4ef2Snarayan mutex_exit(&vd->lock); 4241d10e4ef2Snarayan return (enabled); 42421ae08745Sheppo } 42431ae08745Sheppo 42441ae08745Sheppo static void 42450a55fbb7Slm66018 vd_recv_msg(void *arg) 42461ae08745Sheppo { 42471ae08745Sheppo vd_t *vd = (vd_t *)arg; 42483af08d82Slm66018 int rv = 0, status = 0; 42491ae08745Sheppo 42501ae08745Sheppo ASSERT(vd != NULL); 42513af08d82Slm66018 4252d10e4ef2Snarayan PR2("New task to receive incoming message(s)"); 42533af08d82Slm66018 42543af08d82Slm66018 4255d10e4ef2Snarayan while (vd_enabled(vd) && status == 0) { 4256d10e4ef2Snarayan size_t msglen, msgsize; 42573af08d82Slm66018 ldc_status_t lstatus; 4258d10e4ef2Snarayan 42590a55fbb7Slm66018 /* 4260d10e4ef2Snarayan * Receive and process a message 42610a55fbb7Slm66018 */ 4262d10e4ef2Snarayan vd_reset_if_needed(vd); /* can change vd->max_msglen */ 42633af08d82Slm66018 42643af08d82Slm66018 /* 42653af08d82Slm66018 * check if channel is UP - else break out of loop 42663af08d82Slm66018 */ 42673af08d82Slm66018 status = ldc_status(vd->ldc_handle, &lstatus); 42683af08d82Slm66018 if (lstatus != LDC_UP) { 42693af08d82Slm66018 PR0("channel not up (status=%d), exiting recv loop\n", 42703af08d82Slm66018 lstatus); 42713af08d82Slm66018 break; 42723af08d82Slm66018 } 42733af08d82Slm66018 42743af08d82Slm66018 ASSERT(vd->max_msglen != 0); 42753af08d82Slm66018 4276d10e4ef2Snarayan msgsize = vd->max_msglen; /* stable copy for alloc/free */ 42773af08d82Slm66018 msglen = msgsize; /* actual len after recv_msg() */ 42783af08d82Slm66018 42793af08d82Slm66018 status = recv_msg(vd->ldc_handle, vd->vio_msgp, &msglen); 42803af08d82Slm66018 switch (status) { 42813af08d82Slm66018 case 0: 42823af08d82Slm66018 rv = vd_process_msg(vd, (vio_msg_t *)vd->vio_msgp, 42833af08d82Slm66018 msglen); 42843af08d82Slm66018 /* check if max_msglen changed */ 42853af08d82Slm66018 if (msgsize != vd->max_msglen) { 42863af08d82Slm66018 PR0("max_msglen changed 0x%lx to 0x%lx bytes\n", 42873af08d82Slm66018 msgsize, vd->max_msglen); 42883af08d82Slm66018 kmem_free(vd->vio_msgp, msgsize); 42893af08d82Slm66018 vd->vio_msgp = 42903af08d82Slm66018 kmem_alloc(vd->max_msglen, KM_SLEEP); 42913af08d82Slm66018 } 42923af08d82Slm66018 if (rv == EINPROGRESS) 42933af08d82Slm66018 continue; 42943af08d82Slm66018 break; 42953af08d82Slm66018 42963af08d82Slm66018 case ENOMSG: 42973af08d82Slm66018 break; 42983af08d82Slm66018 42993af08d82Slm66018 case ECONNRESET: 43003af08d82Slm66018 PR0("initiating soft reset (ECONNRESET)\n"); 43013af08d82Slm66018 vd_need_reset(vd, B_FALSE); 43023af08d82Slm66018 status = 0; 43033af08d82Slm66018 break; 43043af08d82Slm66018 43053af08d82Slm66018 default: 4306d10e4ef2Snarayan /* Probably an LDC failure; arrange to reset it */ 43073af08d82Slm66018 PR0("initiating full reset (status=0x%x)", status); 4308d10e4ef2Snarayan vd_need_reset(vd, B_TRUE); 43093af08d82Slm66018 break; 43100a55fbb7Slm66018 } 43111ae08745Sheppo } 43123af08d82Slm66018 4313d10e4ef2Snarayan PR2("Task finished"); 43140a55fbb7Slm66018 } 43150a55fbb7Slm66018 43160a55fbb7Slm66018 static uint_t 43171ae08745Sheppo vd_handle_ldc_events(uint64_t event, caddr_t arg) 43181ae08745Sheppo { 43191ae08745Sheppo vd_t *vd = (vd_t *)(void *)arg; 43203af08d82Slm66018 int status; 43211ae08745Sheppo 43221ae08745Sheppo ASSERT(vd != NULL); 4323d10e4ef2Snarayan 4324d10e4ef2Snarayan if (!vd_enabled(vd)) 4325d10e4ef2Snarayan return (LDC_SUCCESS); 4326d10e4ef2Snarayan 43273af08d82Slm66018 if (event & LDC_EVT_DOWN) { 432834683adeSsg70180 PR0("LDC_EVT_DOWN: LDC channel went down"); 43293af08d82Slm66018 43303af08d82Slm66018 vd_need_reset(vd, B_TRUE); 43313af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, 43323af08d82Slm66018 DDI_SLEEP); 43333af08d82Slm66018 if (status == DDI_FAILURE) { 43343af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 43353af08d82Slm66018 vd_need_reset(vd, B_TRUE); 43363af08d82Slm66018 } 43373af08d82Slm66018 } 43383af08d82Slm66018 4339d10e4ef2Snarayan if (event & LDC_EVT_RESET) { 43403af08d82Slm66018 PR0("LDC_EVT_RESET: LDC channel was reset"); 43413af08d82Slm66018 43423af08d82Slm66018 if (vd->state != VD_STATE_INIT) { 43433af08d82Slm66018 PR0("scheduling full reset"); 43443af08d82Slm66018 vd_need_reset(vd, B_FALSE); 43453af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, 43463af08d82Slm66018 vd, DDI_SLEEP); 43473af08d82Slm66018 if (status == DDI_FAILURE) { 43483af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 43493af08d82Slm66018 vd_need_reset(vd, B_TRUE); 43503af08d82Slm66018 } 43513af08d82Slm66018 43523af08d82Slm66018 } else { 43533af08d82Slm66018 PR0("channel already reset, ignoring...\n"); 43543af08d82Slm66018 PR0("doing ldc up...\n"); 43553af08d82Slm66018 (void) ldc_up(vd->ldc_handle); 43563af08d82Slm66018 } 43573af08d82Slm66018 4358d10e4ef2Snarayan return (LDC_SUCCESS); 4359d10e4ef2Snarayan } 4360d10e4ef2Snarayan 4361d10e4ef2Snarayan if (event & LDC_EVT_UP) { 43623af08d82Slm66018 PR0("EVT_UP: LDC is up\nResetting client connection state"); 43633af08d82Slm66018 PR0("initiating soft reset"); 4364d10e4ef2Snarayan vd_need_reset(vd, B_FALSE); 43653af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, 43663af08d82Slm66018 vd, DDI_SLEEP); 43673af08d82Slm66018 if (status == DDI_FAILURE) { 43683af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 43693af08d82Slm66018 vd_need_reset(vd, B_TRUE); 43703af08d82Slm66018 return (LDC_SUCCESS); 43713af08d82Slm66018 } 4372d10e4ef2Snarayan } 4373d10e4ef2Snarayan 4374d10e4ef2Snarayan if (event & LDC_EVT_READ) { 4375d10e4ef2Snarayan int status; 4376d10e4ef2Snarayan 4377d10e4ef2Snarayan PR1("New data available"); 4378d10e4ef2Snarayan /* Queue a task to receive the new data */ 4379d10e4ef2Snarayan status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, 4380d10e4ef2Snarayan DDI_SLEEP); 43813af08d82Slm66018 43823af08d82Slm66018 if (status == DDI_FAILURE) { 43833af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 43843af08d82Slm66018 vd_need_reset(vd, B_TRUE); 43853af08d82Slm66018 } 4386d10e4ef2Snarayan } 4387d10e4ef2Snarayan 4388d10e4ef2Snarayan return (LDC_SUCCESS); 43891ae08745Sheppo } 43901ae08745Sheppo 43911ae08745Sheppo static uint_t 43921ae08745Sheppo vds_check_for_vd(mod_hash_key_t key, mod_hash_val_t *val, void *arg) 43931ae08745Sheppo { 43941ae08745Sheppo _NOTE(ARGUNUSED(key, val)) 43951ae08745Sheppo (*((uint_t *)arg))++; 43961ae08745Sheppo return (MH_WALK_TERMINATE); 43971ae08745Sheppo } 43981ae08745Sheppo 43991ae08745Sheppo 44001ae08745Sheppo static int 44011ae08745Sheppo vds_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 44021ae08745Sheppo { 44031ae08745Sheppo uint_t vd_present = 0; 44041ae08745Sheppo minor_t instance; 44051ae08745Sheppo vds_t *vds; 44061ae08745Sheppo 44071ae08745Sheppo 44081ae08745Sheppo switch (cmd) { 44091ae08745Sheppo case DDI_DETACH: 44101ae08745Sheppo /* the real work happens below */ 44111ae08745Sheppo break; 44121ae08745Sheppo case DDI_SUSPEND: 4413d10e4ef2Snarayan PR0("No action required for DDI_SUSPEND"); 44141ae08745Sheppo return (DDI_SUCCESS); 44151ae08745Sheppo default: 44163af08d82Slm66018 PR0("Unrecognized \"cmd\""); 44171ae08745Sheppo return (DDI_FAILURE); 44181ae08745Sheppo } 44191ae08745Sheppo 44201ae08745Sheppo ASSERT(cmd == DDI_DETACH); 44211ae08745Sheppo instance = ddi_get_instance(dip); 44221ae08745Sheppo if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) { 44233af08d82Slm66018 PR0("Could not get state for instance %u", instance); 44241ae08745Sheppo ddi_soft_state_free(vds_state, instance); 44251ae08745Sheppo return (DDI_FAILURE); 44261ae08745Sheppo } 44271ae08745Sheppo 44281ae08745Sheppo /* Do no detach when serving any vdisks */ 44291ae08745Sheppo mod_hash_walk(vds->vd_table, vds_check_for_vd, &vd_present); 44301ae08745Sheppo if (vd_present) { 44311ae08745Sheppo PR0("Not detaching because serving vdisks"); 44321ae08745Sheppo return (DDI_FAILURE); 44331ae08745Sheppo } 44341ae08745Sheppo 44351ae08745Sheppo PR0("Detaching"); 4436445b4c2eSsb155480 if (vds->initialized & VDS_MDEG) { 44371ae08745Sheppo (void) mdeg_unregister(vds->mdeg); 4438445b4c2eSsb155480 kmem_free(vds->ispecp->specp, sizeof (vds_prop_template)); 4439445b4c2eSsb155480 kmem_free(vds->ispecp, sizeof (mdeg_node_spec_t)); 4440445b4c2eSsb155480 vds->ispecp = NULL; 4441445b4c2eSsb155480 vds->mdeg = NULL; 4442445b4c2eSsb155480 } 4443445b4c2eSsb155480 44448fce2fd6Sachartre vds_driver_types_free(vds); 44458fce2fd6Sachartre 44461ae08745Sheppo if (vds->initialized & VDS_LDI) 44471ae08745Sheppo (void) ldi_ident_release(vds->ldi_ident); 44481ae08745Sheppo mod_hash_destroy_hash(vds->vd_table); 44491ae08745Sheppo ddi_soft_state_free(vds_state, instance); 44501ae08745Sheppo return (DDI_SUCCESS); 44511ae08745Sheppo } 44521ae08745Sheppo 44531ae08745Sheppo static boolean_t 44541ae08745Sheppo is_pseudo_device(dev_info_t *dip) 44551ae08745Sheppo { 44561ae08745Sheppo dev_info_t *parent, *root = ddi_root_node(); 44571ae08745Sheppo 44581ae08745Sheppo 44591ae08745Sheppo for (parent = ddi_get_parent(dip); (parent != NULL) && (parent != root); 44601ae08745Sheppo parent = ddi_get_parent(parent)) { 44611ae08745Sheppo if (strcmp(ddi_get_name(parent), DEVI_PSEUDO_NEXNAME) == 0) 44621ae08745Sheppo return (B_TRUE); 44631ae08745Sheppo } 44641ae08745Sheppo 44651ae08745Sheppo return (B_FALSE); 44661ae08745Sheppo } 44671ae08745Sheppo 446817cadca8Slm66018 /* 446917cadca8Slm66018 * Description: 447017cadca8Slm66018 * This function checks to see if the file being used as a 447117cadca8Slm66018 * virtual disk is an ISO image. An ISO image is a special 447217cadca8Slm66018 * case which can be booted/installed from like a CD/DVD 447317cadca8Slm66018 * 447417cadca8Slm66018 * Parameters: 447517cadca8Slm66018 * vd - disk on which the operation is performed. 447617cadca8Slm66018 * 447717cadca8Slm66018 * Return Code: 447817cadca8Slm66018 * B_TRUE - The file is an ISO 9660 compliant image 447917cadca8Slm66018 * B_FALSE - just a regular disk image file 448017cadca8Slm66018 */ 448117cadca8Slm66018 static boolean_t 448217cadca8Slm66018 vd_file_is_iso_image(vd_t *vd) 448317cadca8Slm66018 { 448417cadca8Slm66018 char iso_buf[ISO_SECTOR_SIZE]; 448517cadca8Slm66018 int i, rv; 448617cadca8Slm66018 uint_t sec; 448717cadca8Slm66018 448817cadca8Slm66018 ASSERT(vd->file); 448917cadca8Slm66018 449017cadca8Slm66018 /* 449117cadca8Slm66018 * If we have already discovered and saved this info we can 449217cadca8Slm66018 * short-circuit the check and avoid reading the file. 449317cadca8Slm66018 */ 449417cadca8Slm66018 if (vd->vdisk_media == VD_MEDIA_DVD || vd->vdisk_media == VD_MEDIA_CD) 449517cadca8Slm66018 return (B_TRUE); 449617cadca8Slm66018 449717cadca8Slm66018 /* 449817cadca8Slm66018 * We wish to read the sector that should contain the 2nd ISO volume 449917cadca8Slm66018 * descriptor. The second field in this descriptor is called the 450017cadca8Slm66018 * Standard Identifier and is set to CD001 for a CD-ROM compliant 450117cadca8Slm66018 * to the ISO 9660 standard. 450217cadca8Slm66018 */ 450317cadca8Slm66018 sec = (ISO_VOLDESC_SEC * ISO_SECTOR_SIZE) / vd->vdisk_block_size; 450417cadca8Slm66018 rv = vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)iso_buf, 450517cadca8Slm66018 sec, ISO_SECTOR_SIZE); 450617cadca8Slm66018 450717cadca8Slm66018 if (rv < 0) 450817cadca8Slm66018 return (B_FALSE); 450917cadca8Slm66018 451017cadca8Slm66018 for (i = 0; i < ISO_ID_STRLEN; i++) { 451117cadca8Slm66018 if (ISO_STD_ID(iso_buf)[i] != ISO_ID_STRING[i]) 451217cadca8Slm66018 return (B_FALSE); 451317cadca8Slm66018 } 451417cadca8Slm66018 451517cadca8Slm66018 return (B_TRUE); 451617cadca8Slm66018 } 451717cadca8Slm66018 451817cadca8Slm66018 /* 451917cadca8Slm66018 * Description: 452017cadca8Slm66018 * This function checks to see if the virtual device is an ATAPI 452117cadca8Slm66018 * device. ATAPI devices use Group 1 Read/Write commands, so 452217cadca8Slm66018 * any USCSI calls vds makes need to take this into account. 452317cadca8Slm66018 * 452417cadca8Slm66018 * Parameters: 452517cadca8Slm66018 * vd - disk on which the operation is performed. 452617cadca8Slm66018 * 452717cadca8Slm66018 * Return Code: 452817cadca8Slm66018 * B_TRUE - The virtual disk is backed by an ATAPI device 452917cadca8Slm66018 * B_FALSE - not an ATAPI device (presumably SCSI) 453017cadca8Slm66018 */ 453117cadca8Slm66018 static boolean_t 453217cadca8Slm66018 vd_is_atapi_device(vd_t *vd) 453317cadca8Slm66018 { 453417cadca8Slm66018 boolean_t is_atapi = B_FALSE; 453517cadca8Slm66018 char *variantp; 453617cadca8Slm66018 int rv; 453717cadca8Slm66018 453817cadca8Slm66018 ASSERT(vd->ldi_handle[0] != NULL); 453917cadca8Slm66018 ASSERT(!vd->file); 454017cadca8Slm66018 454117cadca8Slm66018 rv = ldi_prop_lookup_string(vd->ldi_handle[0], 454217cadca8Slm66018 (LDI_DEV_T_ANY | DDI_PROP_DONTPASS), "variant", &variantp); 454317cadca8Slm66018 if (rv == DDI_PROP_SUCCESS) { 454417cadca8Slm66018 PR0("'variant' property exists for %s", vd->device_path); 454517cadca8Slm66018 if (strcmp(variantp, "atapi") == 0) 454617cadca8Slm66018 is_atapi = B_TRUE; 454717cadca8Slm66018 ddi_prop_free(variantp); 454817cadca8Slm66018 } 454917cadca8Slm66018 455017cadca8Slm66018 rv = ldi_prop_exists(vd->ldi_handle[0], LDI_DEV_T_ANY, "atapi"); 455117cadca8Slm66018 if (rv) { 455217cadca8Slm66018 PR0("'atapi' property exists for %s", vd->device_path); 455317cadca8Slm66018 is_atapi = B_TRUE; 455417cadca8Slm66018 } 455517cadca8Slm66018 455617cadca8Slm66018 return (is_atapi); 455717cadca8Slm66018 } 455817cadca8Slm66018 45591ae08745Sheppo static int 45602f5224aeSachartre vd_setup_mediainfo(vd_t *vd) 45610a55fbb7Slm66018 { 45622f5224aeSachartre int status, rval; 45634bac2208Snarayan struct dk_minfo dk_minfo; 45640a55fbb7Slm66018 45652f5224aeSachartre ASSERT(vd->ldi_handle[0] != NULL); 45662f5224aeSachartre ASSERT(vd->vdisk_block_size != 0); 45672f5224aeSachartre 45682f5224aeSachartre if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCGMEDIAINFO, 45692f5224aeSachartre (intptr_t)&dk_minfo, (vd->open_flags | FKIOCTL), 45702f5224aeSachartre kcred, &rval)) != 0) 45712f5224aeSachartre return (status); 45722f5224aeSachartre 45732f5224aeSachartre ASSERT(dk_minfo.dki_lbsize % vd->vdisk_block_size == 0); 45742f5224aeSachartre 45752f5224aeSachartre vd->block_size = dk_minfo.dki_lbsize; 45762f5224aeSachartre vd->vdisk_size = (dk_minfo.dki_capacity * dk_minfo.dki_lbsize) / 45772f5224aeSachartre vd->vdisk_block_size; 45782f5224aeSachartre vd->vdisk_media = DK_MEDIATYPE2VD_MEDIATYPE(dk_minfo.dki_media_type); 45792f5224aeSachartre return (0); 45802f5224aeSachartre } 45812f5224aeSachartre 45822f5224aeSachartre static int 45832f5224aeSachartre vd_setup_full_disk(vd_t *vd) 45842f5224aeSachartre { 45852f5224aeSachartre int status; 45862f5224aeSachartre major_t major = getmajor(vd->dev[0]); 45872f5224aeSachartre minor_t minor = getminor(vd->dev[0]) - VD_ENTIRE_DISK_SLICE; 45882f5224aeSachartre 4589047ba61eSachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK); 4590047ba61eSachartre 45912f5224aeSachartre vd->vdisk_block_size = DEV_BSIZE; 45922f5224aeSachartre 45934bac2208Snarayan /* 45944bac2208Snarayan * At this point, vdisk_size is set to the size of partition 2 but 45954bac2208Snarayan * this does not represent the size of the disk because partition 2 45964bac2208Snarayan * may not cover the entire disk and its size does not include reserved 45972f5224aeSachartre * blocks. So we call vd_get_mediainfo to udpate this information and 45982f5224aeSachartre * set the block size and the media type of the disk. 45994bac2208Snarayan */ 46002f5224aeSachartre status = vd_setup_mediainfo(vd); 46012f5224aeSachartre 46022f5224aeSachartre if (status != 0) { 46032f5224aeSachartre if (!vd->scsi) { 46042f5224aeSachartre /* unexpected failure */ 4605690555a1Sachartre PRN("ldi_ioctl(DKIOCGMEDIAINFO) returned errno %d", 46064bac2208Snarayan status); 46070a55fbb7Slm66018 return (status); 46080a55fbb7Slm66018 } 46092f5224aeSachartre 46102f5224aeSachartre /* 46112f5224aeSachartre * The function can fail for SCSI disks which are present but 46122f5224aeSachartre * reserved by another system. In that case, we don't know the 46132f5224aeSachartre * size of the disk and the block size. 46142f5224aeSachartre */ 46152f5224aeSachartre vd->vdisk_size = VD_SIZE_UNKNOWN; 46162f5224aeSachartre vd->block_size = 0; 46172f5224aeSachartre vd->vdisk_media = VD_MEDIA_FIXED; 46182f5224aeSachartre } 46190a55fbb7Slm66018 46200a55fbb7Slm66018 /* Move dev number and LDI handle to entire-disk-slice array elements */ 46210a55fbb7Slm66018 vd->dev[VD_ENTIRE_DISK_SLICE] = vd->dev[0]; 46220a55fbb7Slm66018 vd->dev[0] = 0; 46230a55fbb7Slm66018 vd->ldi_handle[VD_ENTIRE_DISK_SLICE] = vd->ldi_handle[0]; 46240a55fbb7Slm66018 vd->ldi_handle[0] = NULL; 46250a55fbb7Slm66018 46260a55fbb7Slm66018 /* Initialize device numbers for remaining slices and open them */ 46270a55fbb7Slm66018 for (int slice = 0; slice < vd->nslices; slice++) { 46280a55fbb7Slm66018 /* 46290a55fbb7Slm66018 * Skip the entire-disk slice, as it's already open and its 46300a55fbb7Slm66018 * device known 46310a55fbb7Slm66018 */ 46320a55fbb7Slm66018 if (slice == VD_ENTIRE_DISK_SLICE) 46330a55fbb7Slm66018 continue; 46340a55fbb7Slm66018 ASSERT(vd->dev[slice] == 0); 46350a55fbb7Slm66018 ASSERT(vd->ldi_handle[slice] == NULL); 46360a55fbb7Slm66018 46370a55fbb7Slm66018 /* 46380a55fbb7Slm66018 * Construct the device number for the current slice 46390a55fbb7Slm66018 */ 46400a55fbb7Slm66018 vd->dev[slice] = makedevice(major, (minor + slice)); 46410a55fbb7Slm66018 46420a55fbb7Slm66018 /* 464334683adeSsg70180 * Open all slices of the disk to serve them to the client. 464434683adeSsg70180 * Slices are opened exclusively to prevent other threads or 464534683adeSsg70180 * processes in the service domain from performing I/O to 464634683adeSsg70180 * slices being accessed by a client. Failure to open a slice 464734683adeSsg70180 * results in vds not serving this disk, as the client could 464834683adeSsg70180 * attempt (and should be able) to access any slice immediately. 464934683adeSsg70180 * Any slices successfully opened before a failure will get 465034683adeSsg70180 * closed by vds_destroy_vd() as a result of the error returned 465134683adeSsg70180 * by this function. 465234683adeSsg70180 * 465334683adeSsg70180 * We need to do the open with FNDELAY so that opening an empty 465434683adeSsg70180 * slice does not fail. 46550a55fbb7Slm66018 */ 46560a55fbb7Slm66018 PR0("Opening device major %u, minor %u = slice %u", 46570a55fbb7Slm66018 major, minor, slice); 4658047ba61eSachartre 4659047ba61eSachartre /* 4660047ba61eSachartre * Try to open the device. This can fail for example if we are 4661047ba61eSachartre * opening an empty slice. So in case of a failure, we try the 4662047ba61eSachartre * open again but this time with the FNDELAY flag. 4663047ba61eSachartre */ 4664047ba61eSachartre status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK, 4665047ba61eSachartre vd->open_flags, kcred, &vd->ldi_handle[slice], 4666047ba61eSachartre vd->vds->ldi_ident); 4667047ba61eSachartre 4668047ba61eSachartre if (status != 0) { 4669047ba61eSachartre status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK, 4670047ba61eSachartre vd->open_flags | FNDELAY, kcred, 4671047ba61eSachartre &vd->ldi_handle[slice], vd->vds->ldi_ident); 4672047ba61eSachartre } 4673047ba61eSachartre 4674047ba61eSachartre if (status != 0) { 4675690555a1Sachartre PRN("ldi_open_by_dev() returned errno %d " 46760a55fbb7Slm66018 "for slice %u", status, slice); 46770a55fbb7Slm66018 /* vds_destroy_vd() will close any open slices */ 4678690555a1Sachartre vd->ldi_handle[slice] = NULL; 46790a55fbb7Slm66018 return (status); 46800a55fbb7Slm66018 } 46810a55fbb7Slm66018 } 46820a55fbb7Slm66018 46830a55fbb7Slm66018 return (0); 46840a55fbb7Slm66018 } 46850a55fbb7Slm66018 4686edcc0754Sachartre /* 4687edcc0754Sachartre * When a slice or a volume is exported as a single-slice disk, we want 4688edcc0754Sachartre * the disk backend (i.e. the slice or volume) to be entirely mapped as 4689edcc0754Sachartre * a slice without the addition of any metadata. 4690edcc0754Sachartre * 4691edcc0754Sachartre * So when exporting the disk as a VTOC disk, we fake a disk with the following 4692edcc0754Sachartre * layout: 4693edcc0754Sachartre * 4694edcc0754Sachartre * 0 1 N+1 4695edcc0754Sachartre * +-+--------------------------+ 4696edcc0754Sachartre * virtual disk: |L| slice 0 | 4697edcc0754Sachartre * +-+--------------------------+ 4698edcc0754Sachartre * ^: : 4699edcc0754Sachartre * |: : 4700edcc0754Sachartre * VTOC LABEL--+: : 4701edcc0754Sachartre * +--------------------------+ 4702edcc0754Sachartre * disk backend: | slice/volume | 4703edcc0754Sachartre * +--------------------------+ 4704edcc0754Sachartre * 0 N 4705edcc0754Sachartre * 4706edcc0754Sachartre * N is the number of blocks in the slice/volume. 4707edcc0754Sachartre * 4708edcc0754Sachartre * We simulate a disk with N+1 blocks. The first block (block 0) is faked and 4709edcc0754Sachartre * can not be changed. The remaining blocks (1 to N+1) defines slice 0 and are 4710edcc0754Sachartre * mapped to the exported slice or volume: 4711edcc0754Sachartre * 4712edcc0754Sachartre * - block 0 (L) can return a fake VTOC label if raw read was implemented. 4713edcc0754Sachartre * - block 1 to N+1 is mapped to the exported slice or volume. 4714edcc0754Sachartre * 4715edcc0754Sachartre */ 47160a55fbb7Slm66018 static int 471778fcd0a1Sachartre vd_setup_partition_vtoc(vd_t *vd) 471878fcd0a1Sachartre { 471978fcd0a1Sachartre int rval, status; 472078fcd0a1Sachartre char *device_path = vd->device_path; 472178fcd0a1Sachartre 472278fcd0a1Sachartre status = ldi_ioctl(vd->ldi_handle[0], DKIOCGGEOM, 4723047ba61eSachartre (intptr_t)&vd->dk_geom, (vd->open_flags | FKIOCTL), kcred, &rval); 472478fcd0a1Sachartre 472578fcd0a1Sachartre if (status != 0) { 472678fcd0a1Sachartre PRN("ldi_ioctl(DKIOCGEOM) returned errno %d for %s", 472778fcd0a1Sachartre status, device_path); 472878fcd0a1Sachartre return (status); 472978fcd0a1Sachartre } 473078fcd0a1Sachartre 473178fcd0a1Sachartre /* Initialize dk_geom structure for single-slice device */ 473278fcd0a1Sachartre if (vd->dk_geom.dkg_nsect == 0) { 473378fcd0a1Sachartre PRN("%s geometry claims 0 sectors per track", device_path); 473478fcd0a1Sachartre return (EIO); 473578fcd0a1Sachartre } 473678fcd0a1Sachartre if (vd->dk_geom.dkg_nhead == 0) { 473778fcd0a1Sachartre PRN("%s geometry claims 0 heads", device_path); 473878fcd0a1Sachartre return (EIO); 473978fcd0a1Sachartre } 4740edcc0754Sachartre vd->dk_geom.dkg_ncyl = (vd->vdisk_size + 1) / vd->dk_geom.dkg_nsect / 474178fcd0a1Sachartre vd->dk_geom.dkg_nhead; 474278fcd0a1Sachartre vd->dk_geom.dkg_acyl = 0; 474378fcd0a1Sachartre vd->dk_geom.dkg_pcyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl; 474478fcd0a1Sachartre 474578fcd0a1Sachartre 474678fcd0a1Sachartre /* Initialize vtoc structure for single-slice device */ 474778fcd0a1Sachartre bcopy(VD_VOLUME_NAME, vd->vtoc.v_volume, 474878fcd0a1Sachartre MIN(sizeof (VD_VOLUME_NAME), sizeof (vd->vtoc.v_volume))); 474978fcd0a1Sachartre bzero(vd->vtoc.v_part, sizeof (vd->vtoc.v_part)); 475078fcd0a1Sachartre vd->vtoc.v_nparts = 1; 475178fcd0a1Sachartre vd->vtoc.v_part[0].p_tag = V_UNASSIGNED; 475278fcd0a1Sachartre vd->vtoc.v_part[0].p_flag = 0; 4753edcc0754Sachartre vd->vtoc.v_part[0].p_start = 1; 475478fcd0a1Sachartre vd->vtoc.v_part[0].p_size = vd->vdisk_size; 475578fcd0a1Sachartre bcopy(VD_ASCIILABEL, vd->vtoc.v_asciilabel, 475678fcd0a1Sachartre MIN(sizeof (VD_ASCIILABEL), sizeof (vd->vtoc.v_asciilabel))); 475778fcd0a1Sachartre 4758edcc0754Sachartre /* adjust the vdisk_size, we emulate the first block */ 4759edcc0754Sachartre vd->vdisk_size += 1; 4760edcc0754Sachartre 476178fcd0a1Sachartre return (0); 476278fcd0a1Sachartre } 476378fcd0a1Sachartre 4764edcc0754Sachartre /* 4765edcc0754Sachartre * When a slice, volume or file is exported as a single-slice disk, we want 4766edcc0754Sachartre * the disk backend (i.e. the slice, volume or file) to be entirely mapped 4767edcc0754Sachartre * as a slice without the addition of any metadata. 4768edcc0754Sachartre * 4769edcc0754Sachartre * So when exporting the disk as an EFI disk, we fake a disk with the following 4770edcc0754Sachartre * layout: 4771edcc0754Sachartre * 4772edcc0754Sachartre * 0 1 2 3 34 34+N 4773edcc0754Sachartre * +-+-+-+-------+--------------------------+ 4774edcc0754Sachartre * virtual disk: |X|T|E|XXXXXXX| slice 0 | 4775edcc0754Sachartre * +-+-+-+-------+--------------------------+ 4776edcc0754Sachartre * ^ ^ : : 4777edcc0754Sachartre * | | : : 4778edcc0754Sachartre * GPT-+ +-GPE : : 4779edcc0754Sachartre * +--------------------------+ 4780edcc0754Sachartre * disk backend: | slice/volume/file | 4781edcc0754Sachartre * +--------------------------+ 4782edcc0754Sachartre * 0 N 4783edcc0754Sachartre * 4784edcc0754Sachartre * N is the number of blocks in the slice/volume/file. 4785edcc0754Sachartre * 4786edcc0754Sachartre * We simulate a disk with 34+N blocks. The first 34 blocks (0 to 33) are 4787edcc0754Sachartre * emulated and can not be changed. The remaining blocks (34 to 34+N) defines 4788edcc0754Sachartre * slice 0 and are mapped to the exported slice, volume or file: 4789edcc0754Sachartre * 4790edcc0754Sachartre * - block 0 (X) is unused and can return 0 if raw read was implemented. 4791edcc0754Sachartre * - block 1 (T) returns a fake EFI GPT (via DKIOCGETEFI) 4792edcc0754Sachartre * - block 2 (E) returns a fake EFI GPE (via DKIOCGETEFI) 4793edcc0754Sachartre * - block 3 to 33 (X) are unused and return 0 if raw read is implemented. 4794edcc0754Sachartre * - block 34 to 34+N is mapped to the exported slice, volume or file. 4795edcc0754Sachartre * 4796edcc0754Sachartre */ 479778fcd0a1Sachartre static int 47984bac2208Snarayan vd_setup_partition_efi(vd_t *vd) 47994bac2208Snarayan { 48004bac2208Snarayan efi_gpt_t *gpt; 48014bac2208Snarayan efi_gpe_t *gpe; 4802edcc0754Sachartre struct uuid uuid = EFI_USR; 48034bac2208Snarayan uint32_t crc; 48044bac2208Snarayan 4805edcc0754Sachartre gpt = &vd->efi_gpt; 4806edcc0754Sachartre gpe = &vd->efi_gpe; 48074bac2208Snarayan 4808edcc0754Sachartre bzero(gpt, sizeof (efi_gpt_t)); 4809edcc0754Sachartre bzero(gpe, sizeof (efi_gpe_t)); 4810edcc0754Sachartre 4811edcc0754Sachartre /* adjust the vdisk_size, we emulate the first 34 blocks */ 4812edcc0754Sachartre vd->vdisk_size += 34; 48134bac2208Snarayan 48144bac2208Snarayan gpt->efi_gpt_Signature = LE_64(EFI_SIGNATURE); 48154bac2208Snarayan gpt->efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 48164bac2208Snarayan gpt->efi_gpt_HeaderSize = LE_32(sizeof (efi_gpt_t)); 4817edcc0754Sachartre gpt->efi_gpt_FirstUsableLBA = LE_64(34ULL); 48184bac2208Snarayan gpt->efi_gpt_LastUsableLBA = LE_64(vd->vdisk_size - 1); 48194bac2208Snarayan gpt->efi_gpt_NumberOfPartitionEntries = LE_32(1); 4820edcc0754Sachartre gpt->efi_gpt_PartitionEntryLBA = LE_64(2ULL); 48214bac2208Snarayan gpt->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (efi_gpe_t)); 48224bac2208Snarayan 48234bac2208Snarayan UUID_LE_CONVERT(gpe->efi_gpe_PartitionTypeGUID, uuid); 48244bac2208Snarayan gpe->efi_gpe_StartingLBA = gpt->efi_gpt_FirstUsableLBA; 48254bac2208Snarayan gpe->efi_gpe_EndingLBA = gpt->efi_gpt_LastUsableLBA; 48264bac2208Snarayan 48274bac2208Snarayan CRC32(crc, gpe, sizeof (efi_gpe_t), -1U, crc32_table); 48284bac2208Snarayan gpt->efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 48294bac2208Snarayan 48304bac2208Snarayan CRC32(crc, gpt, sizeof (efi_gpt_t), -1U, crc32_table); 48314bac2208Snarayan gpt->efi_gpt_HeaderCRC32 = LE_32(~crc); 48324bac2208Snarayan 48334bac2208Snarayan return (0); 48344bac2208Snarayan } 48354bac2208Snarayan 4836047ba61eSachartre /* 4837047ba61eSachartre * Setup for a virtual disk whose backend is a file (exported as a single slice 48388fce2fd6Sachartre * or as a full disk) or a volume device (for example a ZFS, SVM or VxVM volume) 4839047ba61eSachartre * exported as a full disk. In these cases, the backend is accessed using the 4840047ba61eSachartre * vnode interface. 4841047ba61eSachartre */ 48424bac2208Snarayan static int 4843047ba61eSachartre vd_setup_backend_vnode(vd_t *vd) 48443c96341aSnarayan { 484578fcd0a1Sachartre int rval, status; 48463c96341aSnarayan vattr_t vattr; 48473c96341aSnarayan dev_t dev; 48483c96341aSnarayan char *file_path = vd->device_path; 48493c96341aSnarayan char dev_path[MAXPATHLEN + 1]; 48503c96341aSnarayan ldi_handle_t lhandle; 48513c96341aSnarayan struct dk_cinfo dk_cinfo; 48523c96341aSnarayan 4853047ba61eSachartre if ((status = vn_open(file_path, UIO_SYSSPACE, vd->open_flags | FOFFMAX, 48543c96341aSnarayan 0, &vd->file_vnode, 0, 0)) != 0) { 4855690555a1Sachartre PRN("vn_open(%s) = errno %d", file_path, status); 48563c96341aSnarayan return (status); 48573c96341aSnarayan } 48583c96341aSnarayan 4859690555a1Sachartre /* 4860690555a1Sachartre * We set vd->file now so that vds_destroy_vd will take care of 4861690555a1Sachartre * closing the file and releasing the vnode in case of an error. 4862690555a1Sachartre */ 4863690555a1Sachartre vd->file = B_TRUE; 4864690555a1Sachartre 48653c96341aSnarayan vattr.va_mask = AT_SIZE; 4866da6c28aaSamw if ((status = VOP_GETATTR(vd->file_vnode, &vattr, 0, kcred, NULL)) 4867da6c28aaSamw != 0) { 4868690555a1Sachartre PRN("VOP_GETATTR(%s) = errno %d", file_path, status); 48693c96341aSnarayan return (EIO); 48703c96341aSnarayan } 48713c96341aSnarayan 48723c96341aSnarayan vd->file_size = vattr.va_size; 48733c96341aSnarayan /* size should be at least sizeof(dk_label) */ 48743c96341aSnarayan if (vd->file_size < sizeof (struct dk_label)) { 48753c96341aSnarayan PRN("Size of file has to be at least %ld bytes", 48763c96341aSnarayan sizeof (struct dk_label)); 48773c96341aSnarayan return (EIO); 48783c96341aSnarayan } 48793c96341aSnarayan 4880690555a1Sachartre if (vd->file_vnode->v_flag & VNOMAP) { 4881690555a1Sachartre PRN("File %s cannot be mapped", file_path); 48823c96341aSnarayan return (EIO); 48833c96341aSnarayan } 48843c96341aSnarayan 48853c96341aSnarayan /* sector size = block size = DEV_BSIZE */ 488617cadca8Slm66018 vd->block_size = DEV_BSIZE; 488717cadca8Slm66018 vd->vdisk_block_size = DEV_BSIZE; 488887a7269eSachartre vd->vdisk_size = vd->file_size / DEV_BSIZE; 48893c96341aSnarayan vd->max_xfer_sz = maxphys / DEV_BSIZE; /* default transfer size */ 48903c96341aSnarayan 4891047ba61eSachartre /* 4892047ba61eSachartre * Get max_xfer_sz from the device where the file is or from the device 48938fce2fd6Sachartre * itself if we have a volume device. 4894047ba61eSachartre */ 4895047ba61eSachartre dev_path[0] = '\0'; 4896047ba61eSachartre 48978fce2fd6Sachartre if (vd->volume) { 4898047ba61eSachartre status = ldi_open_by_name(file_path, FREAD, kcred, &lhandle, 4899047ba61eSachartre vd->vds->ldi_ident); 4900047ba61eSachartre } else { 49013c96341aSnarayan dev = vd->file_vnode->v_vfsp->vfs_dev; 49023c96341aSnarayan if (ddi_dev_pathname(dev, S_IFBLK, dev_path) == DDI_SUCCESS) { 49033c96341aSnarayan PR0("underlying device = %s\n", dev_path); 49043c96341aSnarayan } 49053c96341aSnarayan 4906047ba61eSachartre status = ldi_open_by_dev(&dev, OTYP_BLK, FREAD, kcred, &lhandle, 4907047ba61eSachartre vd->vds->ldi_ident); 4908047ba61eSachartre } 4909047ba61eSachartre 4910047ba61eSachartre if (status != 0) { 4911047ba61eSachartre PR0("ldi_open() returned errno %d for device %s", 4912047ba61eSachartre status, (dev_path[0] == '\0')? file_path : dev_path); 49133c96341aSnarayan } else { 49143c96341aSnarayan if ((status = ldi_ioctl(lhandle, DKIOCINFO, 4915047ba61eSachartre (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred, 49163c96341aSnarayan &rval)) != 0) { 49173c96341aSnarayan PR0("ldi_ioctl(DKIOCINFO) returned errno %d for %s", 49183c96341aSnarayan status, dev_path); 49193c96341aSnarayan } else { 49203c96341aSnarayan /* 49213c96341aSnarayan * Store the device's max transfer size for 49223c96341aSnarayan * return to the client 49233c96341aSnarayan */ 49243c96341aSnarayan vd->max_xfer_sz = dk_cinfo.dki_maxtransfer; 49253c96341aSnarayan } 49263c96341aSnarayan 49273c96341aSnarayan PR0("close the device %s", dev_path); 49283c96341aSnarayan (void) ldi_close(lhandle, FREAD, kcred); 49293c96341aSnarayan } 49303c96341aSnarayan 4931205eeb1aSlm66018 PR0("using file %s, dev %s, max_xfer = %u blks", 49323c96341aSnarayan file_path, dev_path, vd->max_xfer_sz); 49333c96341aSnarayan 4934edcc0754Sachartre if (vd->vdisk_type == VD_DISK_TYPE_SLICE) { 49358fce2fd6Sachartre ASSERT(!vd->volume); 4936edcc0754Sachartre vd->vdisk_label = VD_DISK_LABEL_EFI; 4937edcc0754Sachartre status = vd_setup_partition_efi(vd); 4938047ba61eSachartre return (0); 4939edcc0754Sachartre } 4940edcc0754Sachartre 4941edcc0754Sachartre /* 4942edcc0754Sachartre * Find and validate the geometry of a disk image. 4943edcc0754Sachartre */ 4944edcc0754Sachartre status = vd_file_validate_geometry(vd); 4945edcc0754Sachartre if (status != 0 && status != EINVAL && status != ENOTSUP) { 4946edcc0754Sachartre PRN("Failed to read label from %s", file_path); 4947edcc0754Sachartre return (EIO); 4948edcc0754Sachartre } 4949edcc0754Sachartre 4950edcc0754Sachartre if (vd_file_is_iso_image(vd)) { 4951edcc0754Sachartre /* 4952edcc0754Sachartre * Indicate whether to call this a CD or DVD from the size 4953edcc0754Sachartre * of the ISO image (images for both drive types are stored 4954edcc0754Sachartre * in the ISO-9600 format). CDs can store up to just under 1Gb 4955edcc0754Sachartre */ 4956edcc0754Sachartre if ((vd->vdisk_size * vd->vdisk_block_size) > 4957edcc0754Sachartre (1024 * 1024 * 1024)) 4958edcc0754Sachartre vd->vdisk_media = VD_MEDIA_DVD; 4959edcc0754Sachartre else 4960edcc0754Sachartre vd->vdisk_media = VD_MEDIA_CD; 4961edcc0754Sachartre } else { 4962edcc0754Sachartre vd->vdisk_media = VD_MEDIA_FIXED; 4963edcc0754Sachartre } 4964edcc0754Sachartre 4965edcc0754Sachartre /* Setup devid for the disk image */ 4966047ba61eSachartre 496778fcd0a1Sachartre if (vd->vdisk_label != VD_DISK_LABEL_UNK) { 496878fcd0a1Sachartre 496987a7269eSachartre status = vd_file_read_devid(vd, &vd->file_devid); 497087a7269eSachartre 497187a7269eSachartre if (status == 0) { 497287a7269eSachartre /* a valid devid was found */ 497387a7269eSachartre return (0); 497487a7269eSachartre } 497587a7269eSachartre 497687a7269eSachartre if (status != EINVAL) { 497787a7269eSachartre /* 497878fcd0a1Sachartre * There was an error while trying to read the devid. 497978fcd0a1Sachartre * So this disk image may have a devid but we are 498078fcd0a1Sachartre * unable to read it. 498187a7269eSachartre */ 498287a7269eSachartre PR0("can not read devid for %s", file_path); 498387a7269eSachartre vd->file_devid = NULL; 498487a7269eSachartre return (0); 498587a7269eSachartre } 498678fcd0a1Sachartre } 498787a7269eSachartre 498887a7269eSachartre /* 498987a7269eSachartre * No valid device id was found so we create one. Note that a failure 499087a7269eSachartre * to create a device id is not fatal and does not prevent the disk 499187a7269eSachartre * image from being attached. 499287a7269eSachartre */ 499387a7269eSachartre PR1("creating devid for %s", file_path); 499487a7269eSachartre 499587a7269eSachartre if (ddi_devid_init(vd->vds->dip, DEVID_FAB, NULL, 0, 499687a7269eSachartre &vd->file_devid) != DDI_SUCCESS) { 499787a7269eSachartre PR0("fail to create devid for %s", file_path); 499887a7269eSachartre vd->file_devid = NULL; 499987a7269eSachartre return (0); 500087a7269eSachartre } 500187a7269eSachartre 500278fcd0a1Sachartre /* 500378fcd0a1Sachartre * Write devid to the disk image. The devid is stored into the disk 500478fcd0a1Sachartre * image if we have a valid label; otherwise the devid will be stored 500578fcd0a1Sachartre * when the user writes a valid label. 500678fcd0a1Sachartre */ 500778fcd0a1Sachartre if (vd->vdisk_label != VD_DISK_LABEL_UNK) { 500887a7269eSachartre if (vd_file_write_devid(vd, vd->file_devid) != 0) { 500987a7269eSachartre PR0("fail to write devid for %s", file_path); 501087a7269eSachartre ddi_devid_free(vd->file_devid); 501187a7269eSachartre vd->file_devid = NULL; 501287a7269eSachartre } 501378fcd0a1Sachartre } 501487a7269eSachartre 50153c96341aSnarayan return (0); 50163c96341aSnarayan } 50173c96341aSnarayan 501817cadca8Slm66018 501917cadca8Slm66018 /* 502017cadca8Slm66018 * Description: 502117cadca8Slm66018 * Open a device using its device path (supplied by ldm(1m)) 502217cadca8Slm66018 * 502317cadca8Slm66018 * Parameters: 502417cadca8Slm66018 * vd - pointer to structure containing the vDisk info 50258fce2fd6Sachartre * flags - open flags 502617cadca8Slm66018 * 502717cadca8Slm66018 * Return Value 502817cadca8Slm66018 * 0 - success 502917cadca8Slm66018 * != 0 - some other non-zero return value from ldi(9F) functions 503017cadca8Slm66018 */ 503117cadca8Slm66018 static int 50328fce2fd6Sachartre vd_open_using_ldi_by_name(vd_t *vd, int flags) 503317cadca8Slm66018 { 50348fce2fd6Sachartre int status; 503517cadca8Slm66018 char *device_path = vd->device_path; 503617cadca8Slm66018 50378fce2fd6Sachartre /* Attempt to open device */ 50388fce2fd6Sachartre status = ldi_open_by_name(device_path, flags, kcred, 503917cadca8Slm66018 &vd->ldi_handle[0], vd->vds->ldi_ident); 504017cadca8Slm66018 504117cadca8Slm66018 /* 504217cadca8Slm66018 * The open can fail for example if we are opening an empty slice. 504317cadca8Slm66018 * In case of a failure, we try the open again but this time with 504417cadca8Slm66018 * the FNDELAY flag. 504517cadca8Slm66018 */ 504617cadca8Slm66018 if (status != 0) 50478fce2fd6Sachartre status = ldi_open_by_name(device_path, flags | FNDELAY, 504817cadca8Slm66018 kcred, &vd->ldi_handle[0], vd->vds->ldi_ident); 504917cadca8Slm66018 505017cadca8Slm66018 if (status != 0) { 505117cadca8Slm66018 PR0("ldi_open_by_name(%s) = errno %d", device_path, status); 505217cadca8Slm66018 vd->ldi_handle[0] = NULL; 505317cadca8Slm66018 return (status); 505417cadca8Slm66018 } 505517cadca8Slm66018 505617cadca8Slm66018 return (0); 505717cadca8Slm66018 } 505817cadca8Slm66018 5059047ba61eSachartre /* 5060047ba61eSachartre * Setup for a virtual disk which backend is a device (a physical disk, 50618fce2fd6Sachartre * slice or volume device) that is directly exported either as a full disk 50628fce2fd6Sachartre * for a physical disk or as a slice for a volume device or a disk slice. 5063047ba61eSachartre * In these cases, the backend is accessed using the LDI interface. 5064047ba61eSachartre */ 50653c96341aSnarayan static int 5066047ba61eSachartre vd_setup_backend_ldi(vd_t *vd) 50671ae08745Sheppo { 5068e1ebb9ecSlm66018 int rval, status; 50691ae08745Sheppo struct dk_cinfo dk_cinfo; 50703c96341aSnarayan char *device_path = vd->device_path; 50711ae08745Sheppo 50728fce2fd6Sachartre /* device has been opened by vd_identify_dev() */ 50738fce2fd6Sachartre ASSERT(vd->ldi_handle[0] != NULL); 50748fce2fd6Sachartre ASSERT(vd->dev[0] != NULL); 50750a55fbb7Slm66018 50763c96341aSnarayan vd->file = B_FALSE; 50774bac2208Snarayan 507878fcd0a1Sachartre /* Verify backing device supports dk_cinfo */ 5079e1ebb9ecSlm66018 if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO, 5080047ba61eSachartre (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred, 5081e1ebb9ecSlm66018 &rval)) != 0) { 5082e1ebb9ecSlm66018 PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s", 5083e1ebb9ecSlm66018 status, device_path); 5084e1ebb9ecSlm66018 return (status); 5085e1ebb9ecSlm66018 } 5086e1ebb9ecSlm66018 if (dk_cinfo.dki_partition >= V_NUMPAR) { 5087e1ebb9ecSlm66018 PRN("slice %u >= maximum slice %u for %s", 5088e1ebb9ecSlm66018 dk_cinfo.dki_partition, V_NUMPAR, device_path); 5089e1ebb9ecSlm66018 return (EIO); 5090e1ebb9ecSlm66018 } 50914bac2208Snarayan 50928fce2fd6Sachartre /* 50938fce2fd6Sachartre * The device has been opened read-only by vd_identify_dev(), re-open 50948fce2fd6Sachartre * it read-write if the write flag is set and we don't have an optical 50958fce2fd6Sachartre * device such as a CD-ROM, which, for now, we do not permit writes to 50968fce2fd6Sachartre * and thus should not export write operations to the client. 50978fce2fd6Sachartre * 50988fce2fd6Sachartre * Future: if/when we implement support for guest domains writing to 50998fce2fd6Sachartre * optical devices we will need to do further checking of the media type 51008fce2fd6Sachartre * to distinguish between read-only and writable discs. 51018fce2fd6Sachartre */ 51028fce2fd6Sachartre if (dk_cinfo.dki_ctype == DKC_CDROM) { 51038fce2fd6Sachartre 51048fce2fd6Sachartre vd->open_flags &= ~FWRITE; 51058fce2fd6Sachartre 51068fce2fd6Sachartre } else if (vd->open_flags & FWRITE) { 51078fce2fd6Sachartre 51088fce2fd6Sachartre (void) ldi_close(vd->ldi_handle[0], vd->open_flags & ~FWRITE, 51098fce2fd6Sachartre kcred); 51108fce2fd6Sachartre status = vd_open_using_ldi_by_name(vd, vd->open_flags); 51118fce2fd6Sachartre if (status != 0) { 51128fce2fd6Sachartre PR0("Failed to open (%s) = errno %d", 51138fce2fd6Sachartre device_path, status); 51148fce2fd6Sachartre return (status); 51158fce2fd6Sachartre } 51168fce2fd6Sachartre } 51178fce2fd6Sachartre 5118e1ebb9ecSlm66018 /* Store the device's max transfer size for return to the client */ 5119e1ebb9ecSlm66018 vd->max_xfer_sz = dk_cinfo.dki_maxtransfer; 5120e1ebb9ecSlm66018 5121047ba61eSachartre /* 512217cadca8Slm66018 * We need to work out if it's an ATAPI (IDE CD-ROM) or SCSI device so 512317cadca8Slm66018 * that we can use the correct CDB group when sending USCSI commands. 512417cadca8Slm66018 */ 512517cadca8Slm66018 vd->is_atapi_dev = vd_is_atapi_device(vd); 512617cadca8Slm66018 512717cadca8Slm66018 /* 5128047ba61eSachartre * Export a full disk. 5129047ba61eSachartre * 5130047ba61eSachartre * When we use the LDI interface, we export a device as a full disk 5131047ba61eSachartre * if we have an entire disk slice (slice 2) and if this slice is 5132047ba61eSachartre * exported as a full disk and not as a single slice disk. 513317cadca8Slm66018 * Similarly, we want to use LDI if we are accessing a CD or DVD 513417cadca8Slm66018 * device (even if it isn't s2) 5135047ba61eSachartre * 51368fce2fd6Sachartre * Note that volume devices are exported as full disks using the vnode 5137047ba61eSachartre * interface, not the LDI interface. 5138047ba61eSachartre */ 513917cadca8Slm66018 if ((dk_cinfo.dki_partition == VD_ENTIRE_DISK_SLICE && 514017cadca8Slm66018 vd->vdisk_type == VD_DISK_TYPE_DISK) || 514117cadca8Slm66018 dk_cinfo.dki_ctype == DKC_CDROM) { 51428fce2fd6Sachartre ASSERT(!vd->volume); 51432f5224aeSachartre if (dk_cinfo.dki_ctype == DKC_SCSI_CCS) 51442f5224aeSachartre vd->scsi = B_TRUE; 5145047ba61eSachartre return (vd_setup_full_disk(vd)); 5146047ba61eSachartre } 5147047ba61eSachartre 5148047ba61eSachartre /* 5149047ba61eSachartre * Export a single slice disk. 5150047ba61eSachartre * 51518fce2fd6Sachartre * The exported device can be either a volume device or a disk slice. If 5152047ba61eSachartre * it is a disk slice different from slice 2 then it is always exported 5153047ba61eSachartre * as a single slice disk even if the "slice" option is not specified. 51548fce2fd6Sachartre * If it is disk slice 2 or a volume device then it is exported as a 5155047ba61eSachartre * single slice disk only if the "slice" option is specified. 5156047ba61eSachartre */ 5157047ba61eSachartre return (vd_setup_single_slice_disk(vd)); 5158047ba61eSachartre } 5159047ba61eSachartre 5160047ba61eSachartre static int 5161047ba61eSachartre vd_setup_single_slice_disk(vd_t *vd) 5162047ba61eSachartre { 5163edcc0754Sachartre int status, rval; 5164047ba61eSachartre char *device_path = vd->device_path; 5165047ba61eSachartre 5166047ba61eSachartre /* Get size of backing device */ 5167047ba61eSachartre if (ldi_get_size(vd->ldi_handle[0], &vd->vdisk_size) != DDI_SUCCESS) { 5168047ba61eSachartre PRN("ldi_get_size() failed for %s", device_path); 51691ae08745Sheppo return (EIO); 51701ae08745Sheppo } 5171047ba61eSachartre vd->vdisk_size = lbtodb(vd->vdisk_size); /* convert to blocks */ 517217cadca8Slm66018 vd->block_size = DEV_BSIZE; 517317cadca8Slm66018 vd->vdisk_block_size = DEV_BSIZE; 517417cadca8Slm66018 vd->vdisk_media = VD_MEDIA_FIXED; 5175047ba61eSachartre 51768fce2fd6Sachartre if (vd->volume) { 5177047ba61eSachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 517878fcd0a1Sachartre } 51790a55fbb7Slm66018 5180047ba61eSachartre /* 5181047ba61eSachartre * We export the slice as a single slice disk even if the "slice" 5182047ba61eSachartre * option was not specified. 5183047ba61eSachartre */ 51841ae08745Sheppo vd->vdisk_type = VD_DISK_TYPE_SLICE; 51851ae08745Sheppo vd->nslices = 1; 51861ae08745Sheppo 5187edcc0754Sachartre /* 5188edcc0754Sachartre * When exporting a slice or a device as a single slice disk, we don't 5189edcc0754Sachartre * care about any partitioning exposed by the backend. The goal is just 5190edcc0754Sachartre * to export the backend as a flat storage. We provide a fake partition 5191edcc0754Sachartre * table (either a VTOC or EFI), which presents only one slice, to 5192edcc0754Sachartre * accommodate tools expecting a disk label. 5193edcc0754Sachartre * 5194edcc0754Sachartre * We check the label of the backend to export the device as a slice 5195edcc0754Sachartre * using the same type of label (VTOC or EFI). If there is no label 5196edcc0754Sachartre * then we create a fake EFI label. 5197edcc0754Sachartre * 5198edcc0754Sachartre * Note that the partition table we are creating could also be faked 5199edcc0754Sachartre * by the client based on the size of the backend device. 5200edcc0754Sachartre */ 5201edcc0754Sachartre status = ldi_ioctl(vd->ldi_handle[0], DKIOCGVTOC, (intptr_t)&vd->vtoc, 5202edcc0754Sachartre (vd->open_flags | FKIOCTL), kcred, &rval); 5203edcc0754Sachartre 5204edcc0754Sachartre if (status == 0) { 5205edcc0754Sachartre /* export with a fake VTOC label */ 5206edcc0754Sachartre vd->vdisk_label = VD_DISK_LABEL_VTOC; 520778fcd0a1Sachartre status = vd_setup_partition_vtoc(vd); 5208edcc0754Sachartre } else { 5209edcc0754Sachartre /* export with a fake EFI label */ 5210edcc0754Sachartre vd->vdisk_label = VD_DISK_LABEL_EFI; 5211edcc0754Sachartre status = vd_setup_partition_efi(vd); 521278fcd0a1Sachartre } 521378fcd0a1Sachartre 52144bac2208Snarayan return (status); 52154bac2208Snarayan } 52161ae08745Sheppo 52178fce2fd6Sachartre /* 52188fce2fd6Sachartre * Description: 52198fce2fd6Sachartre * Open a device using its device path and identify if this is 52208fce2fd6Sachartre * a disk device or a volume device. 52218fce2fd6Sachartre * 52228fce2fd6Sachartre * Parameters: 52238fce2fd6Sachartre * vd - pointer to structure containing the vDisk info 52248fce2fd6Sachartre * dtype - return the driver type of the device 52258fce2fd6Sachartre * 52268fce2fd6Sachartre * Return Value 52278fce2fd6Sachartre * 0 - success 52288fce2fd6Sachartre * != 0 - some other non-zero return value from ldi(9F) functions 52298fce2fd6Sachartre */ 52308fce2fd6Sachartre static int 52318fce2fd6Sachartre vd_identify_dev(vd_t *vd, int *dtype) 52328fce2fd6Sachartre { 52338fce2fd6Sachartre int status, i; 52348fce2fd6Sachartre char *device_path = vd->device_path; 52358fce2fd6Sachartre char *drv_name; 52368fce2fd6Sachartre int drv_type; 52378fce2fd6Sachartre vds_t *vds = vd->vds; 52388fce2fd6Sachartre 52398fce2fd6Sachartre status = vd_open_using_ldi_by_name(vd, vd->open_flags & ~FWRITE); 52408fce2fd6Sachartre if (status != 0) { 52418fce2fd6Sachartre PR0("Failed to open (%s) = errno %d", device_path, status); 52428fce2fd6Sachartre return (status); 52438fce2fd6Sachartre } 52448fce2fd6Sachartre 52458fce2fd6Sachartre /* Get device number of backing device */ 52468fce2fd6Sachartre if ((status = ldi_get_dev(vd->ldi_handle[0], &vd->dev[0])) != 0) { 52478fce2fd6Sachartre PRN("ldi_get_dev() returned errno %d for %s", 52488fce2fd6Sachartre status, device_path); 52498fce2fd6Sachartre return (status); 52508fce2fd6Sachartre } 52518fce2fd6Sachartre 52528fce2fd6Sachartre /* 52538fce2fd6Sachartre * We start by looking if the driver is in the list from vds.conf 52548fce2fd6Sachartre * so that we can override the built-in list using vds.conf. 52558fce2fd6Sachartre */ 52568fce2fd6Sachartre drv_name = ddi_major_to_name(getmajor(vd->dev[0])); 52578fce2fd6Sachartre drv_type = VD_DRIVER_UNKNOWN; 52588fce2fd6Sachartre 52598fce2fd6Sachartre /* check vds.conf list */ 52608fce2fd6Sachartre for (i = 0; i < vds->num_drivers; i++) { 52618fce2fd6Sachartre if (vds->driver_types[i].type == VD_DRIVER_UNKNOWN) { 52628fce2fd6Sachartre /* ignore invalid entries */ 52638fce2fd6Sachartre continue; 52648fce2fd6Sachartre } 52658fce2fd6Sachartre if (strcmp(drv_name, vds->driver_types[i].name) == 0) { 52668fce2fd6Sachartre drv_type = vds->driver_types[i].type; 52678fce2fd6Sachartre goto done; 52688fce2fd6Sachartre } 52698fce2fd6Sachartre } 52708fce2fd6Sachartre 52718fce2fd6Sachartre /* check built-in list */ 52728fce2fd6Sachartre for (i = 0; i < VDS_NUM_DRIVERS; i++) { 52738fce2fd6Sachartre if (strcmp(drv_name, vds_driver_types[i].name) == 0) { 52748fce2fd6Sachartre drv_type = vds_driver_types[i].type; 52758fce2fd6Sachartre goto done; 52768fce2fd6Sachartre } 52778fce2fd6Sachartre } 52788fce2fd6Sachartre 52798fce2fd6Sachartre done: 52808fce2fd6Sachartre PR0("driver %s identified as %s", drv_name, 52818fce2fd6Sachartre (drv_type == VD_DRIVER_DISK)? "DISK" : 52828fce2fd6Sachartre (drv_type == VD_DRIVER_VOLUME)? "VOLUME" : "UNKNOWN"); 52838fce2fd6Sachartre 52848fce2fd6Sachartre *dtype = drv_type; 52858fce2fd6Sachartre 52868fce2fd6Sachartre return (0); 52878fce2fd6Sachartre } 52888fce2fd6Sachartre 52891ae08745Sheppo static int 5290047ba61eSachartre vd_setup_vd(vd_t *vd) 5291047ba61eSachartre { 52928fce2fd6Sachartre int status, drv_type, pseudo; 5293047ba61eSachartre dev_info_t *dip; 5294047ba61eSachartre vnode_t *vnp; 5295047ba61eSachartre char *path = vd->device_path; 5296047ba61eSachartre 5297047ba61eSachartre /* make sure the vdisk backend is valid */ 5298047ba61eSachartre if ((status = lookupname(path, UIO_SYSSPACE, 5299047ba61eSachartre FOLLOW, NULLVPP, &vnp)) != 0) { 5300047ba61eSachartre PR0("Cannot lookup %s errno %d", path, status); 5301047ba61eSachartre goto done; 5302047ba61eSachartre } 5303047ba61eSachartre 5304047ba61eSachartre switch (vnp->v_type) { 5305047ba61eSachartre case VREG: 5306047ba61eSachartre /* 5307047ba61eSachartre * Backend is a file so it is exported as a full disk or as a 5308047ba61eSachartre * single slice disk using the vnode interface. 5309047ba61eSachartre */ 5310047ba61eSachartre VN_RELE(vnp); 53118fce2fd6Sachartre vd->volume = B_FALSE; 5312047ba61eSachartre status = vd_setup_backend_vnode(vd); 5313047ba61eSachartre break; 5314047ba61eSachartre 5315047ba61eSachartre case VBLK: 5316047ba61eSachartre case VCHR: 5317047ba61eSachartre /* 5318047ba61eSachartre * Backend is a device. The way it is exported depends on the 5319047ba61eSachartre * type of the device. 5320047ba61eSachartre * 53218fce2fd6Sachartre * - A volume device is exported as a full disk using the vnode 5322047ba61eSachartre * interface or as a single slice disk using the LDI 5323047ba61eSachartre * interface. 5324047ba61eSachartre * 5325047ba61eSachartre * - A disk (represented by the slice 2 of that disk) is 5326047ba61eSachartre * exported as a full disk using the LDI interface. 5327047ba61eSachartre * 5328047ba61eSachartre * - A disk slice (different from slice 2) is always exported 5329047ba61eSachartre * as a single slice disk using the LDI interface. 5330047ba61eSachartre * 5331047ba61eSachartre * - The slice 2 of a disk is exported as a single slice disk 5332047ba61eSachartre * if the "slice" option is specified, otherwise the entire 5333047ba61eSachartre * disk will be exported. In any case, the LDI interface is 5334047ba61eSachartre * used. 5335047ba61eSachartre */ 5336047ba61eSachartre 5337047ba61eSachartre /* check if this is a pseudo device */ 5338047ba61eSachartre if ((dip = ddi_hold_devi_by_instance(getmajor(vnp->v_rdev), 5339047ba61eSachartre dev_to_instance(vnp->v_rdev), 0)) == NULL) { 5340047ba61eSachartre PRN("%s is no longer accessible", path); 5341047ba61eSachartre VN_RELE(vnp); 5342047ba61eSachartre status = EIO; 5343047ba61eSachartre break; 5344047ba61eSachartre } 53458fce2fd6Sachartre pseudo = is_pseudo_device(dip); 5346047ba61eSachartre ddi_release_devi(dip); 5347047ba61eSachartre VN_RELE(vnp); 5348047ba61eSachartre 53498fce2fd6Sachartre if (vd_identify_dev(vd, &drv_type) != 0) { 53508fce2fd6Sachartre PRN("%s identification failed", path); 53518fce2fd6Sachartre status = EIO; 53528fce2fd6Sachartre break; 53538fce2fd6Sachartre } 53548fce2fd6Sachartre 53558fce2fd6Sachartre /* 53568fce2fd6Sachartre * If the driver hasn't been identified then we consider that 53578fce2fd6Sachartre * pseudo devices are volumes and other devices are disks. 53588fce2fd6Sachartre */ 53598fce2fd6Sachartre if (drv_type == VD_DRIVER_VOLUME || 53608fce2fd6Sachartre (drv_type == VD_DRIVER_UNKNOWN && pseudo)) { 53618fce2fd6Sachartre vd->volume = B_TRUE; 53628fce2fd6Sachartre } else { 53632f5224aeSachartre status = vd_setup_backend_ldi(vd); 53642f5224aeSachartre break; 53652f5224aeSachartre } 53662f5224aeSachartre 5367047ba61eSachartre /* 53688fce2fd6Sachartre * If this is a volume device then its usage depends if the 5369047ba61eSachartre * "slice" option is set or not. If the "slice" option is set 53708fce2fd6Sachartre * then the volume device will be exported as a single slice, 5371047ba61eSachartre * otherwise it will be exported as a full disk. 53722f5224aeSachartre * 53732f5224aeSachartre * For backward compatibility, if vd_volume_force_slice is set 53748fce2fd6Sachartre * then we always export volume devices as slices. 5375047ba61eSachartre */ 53762f5224aeSachartre if (vd_volume_force_slice) { 53772f5224aeSachartre vd->vdisk_type = VD_DISK_TYPE_SLICE; 53782f5224aeSachartre vd->nslices = 1; 53792f5224aeSachartre } 53802f5224aeSachartre 53818fce2fd6Sachartre if (vd->vdisk_type == VD_DISK_TYPE_DISK) { 53828fce2fd6Sachartre /* close device opened during identification */ 53838fce2fd6Sachartre (void) ldi_close(vd->ldi_handle[0], 53848fce2fd6Sachartre vd->open_flags & ~FWRITE, kcred); 53858fce2fd6Sachartre vd->ldi_handle[0] = NULL; 53868fce2fd6Sachartre vd->dev[0] = 0; 5387047ba61eSachartre status = vd_setup_backend_vnode(vd); 53888fce2fd6Sachartre } else { 5389047ba61eSachartre status = vd_setup_backend_ldi(vd); 53908fce2fd6Sachartre } 5391047ba61eSachartre break; 5392047ba61eSachartre 5393047ba61eSachartre default: 5394047ba61eSachartre PRN("Unsupported vdisk backend %s", path); 5395047ba61eSachartre VN_RELE(vnp); 5396047ba61eSachartre status = EBADF; 5397047ba61eSachartre } 5398047ba61eSachartre 5399047ba61eSachartre done: 5400047ba61eSachartre if (status != 0) { 5401047ba61eSachartre /* 5402047ba61eSachartre * If the error is retryable print an error message only 5403047ba61eSachartre * during the first try. 5404047ba61eSachartre */ 5405047ba61eSachartre if (status == ENXIO || status == ENODEV || 5406047ba61eSachartre status == ENOENT || status == EROFS) { 5407047ba61eSachartre if (!(vd->initialized & VD_SETUP_ERROR)) { 5408047ba61eSachartre PRN("%s is currently inaccessible (error %d)", 5409047ba61eSachartre path, status); 5410047ba61eSachartre } 5411047ba61eSachartre status = EAGAIN; 5412047ba61eSachartre } else { 5413047ba61eSachartre PRN("%s can not be exported as a virtual disk " 5414047ba61eSachartre "(error %d)", path, status); 5415047ba61eSachartre } 5416047ba61eSachartre vd->initialized |= VD_SETUP_ERROR; 5417047ba61eSachartre 5418047ba61eSachartre } else if (vd->initialized & VD_SETUP_ERROR) { 5419047ba61eSachartre /* print a message only if we previously had an error */ 5420047ba61eSachartre PRN("%s is now online", path); 5421047ba61eSachartre vd->initialized &= ~VD_SETUP_ERROR; 5422047ba61eSachartre } 5423047ba61eSachartre 5424047ba61eSachartre return (status); 5425047ba61eSachartre } 5426047ba61eSachartre 5427047ba61eSachartre static int 5428047ba61eSachartre vds_do_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options, 5429047ba61eSachartre uint64_t ldc_id, vd_t **vdp) 54301ae08745Sheppo { 54311ae08745Sheppo char tq_name[TASKQ_NAMELEN]; 54320a55fbb7Slm66018 int status; 54331ae08745Sheppo ddi_iblock_cookie_t iblock = NULL; 54341ae08745Sheppo ldc_attr_t ldc_attr; 54351ae08745Sheppo vd_t *vd; 54361ae08745Sheppo 54371ae08745Sheppo 54381ae08745Sheppo ASSERT(vds != NULL); 5439e1ebb9ecSlm66018 ASSERT(device_path != NULL); 54401ae08745Sheppo ASSERT(vdp != NULL); 5441e1ebb9ecSlm66018 PR0("Adding vdisk for %s", device_path); 54421ae08745Sheppo 54431ae08745Sheppo if ((vd = kmem_zalloc(sizeof (*vd), KM_NOSLEEP)) == NULL) { 54441ae08745Sheppo PRN("No memory for virtual disk"); 54451ae08745Sheppo return (EAGAIN); 54461ae08745Sheppo } 54471ae08745Sheppo *vdp = vd; /* assign here so vds_destroy_vd() can cleanup later */ 54481ae08745Sheppo vd->vds = vds; 54493c96341aSnarayan (void) strncpy(vd->device_path, device_path, MAXPATHLEN); 54501ae08745Sheppo 5451047ba61eSachartre /* Setup open flags */ 5452047ba61eSachartre vd->open_flags = FREAD; 5453047ba61eSachartre 5454047ba61eSachartre if (!(options & VD_OPT_RDONLY)) 5455047ba61eSachartre vd->open_flags |= FWRITE; 5456047ba61eSachartre 5457047ba61eSachartre if (options & VD_OPT_EXCLUSIVE) 5458047ba61eSachartre vd->open_flags |= FEXCL; 5459047ba61eSachartre 5460047ba61eSachartre /* Setup disk type */ 5461047ba61eSachartre if (options & VD_OPT_SLICE) { 5462047ba61eSachartre vd->vdisk_type = VD_DISK_TYPE_SLICE; 5463047ba61eSachartre vd->nslices = 1; 5464047ba61eSachartre } else { 5465047ba61eSachartre vd->vdisk_type = VD_DISK_TYPE_DISK; 5466047ba61eSachartre vd->nslices = V_NUMPAR; 5467047ba61eSachartre } 5468047ba61eSachartre 5469047ba61eSachartre /* default disk label */ 5470047ba61eSachartre vd->vdisk_label = VD_DISK_LABEL_UNK; 5471047ba61eSachartre 54720a55fbb7Slm66018 /* Open vdisk and initialize parameters */ 54733c96341aSnarayan if ((status = vd_setup_vd(vd)) == 0) { 54743c96341aSnarayan vd->initialized |= VD_DISK_READY; 54751ae08745Sheppo 54763c96341aSnarayan ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR); 54778fce2fd6Sachartre PR0("vdisk_type = %s, volume = %s, file = %s, nslices = %u", 54783c96341aSnarayan ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"), 54798fce2fd6Sachartre (vd->volume ? "yes" : "no"), (vd->file ? "yes" : "no"), 54803c96341aSnarayan vd->nslices); 54813c96341aSnarayan } else { 54823c96341aSnarayan if (status != EAGAIN) 54833c96341aSnarayan return (status); 54843c96341aSnarayan } 54851ae08745Sheppo 54861ae08745Sheppo /* Initialize locking */ 54871ae08745Sheppo if (ddi_get_soft_iblock_cookie(vds->dip, DDI_SOFTINT_MED, 54881ae08745Sheppo &iblock) != DDI_SUCCESS) { 54891ae08745Sheppo PRN("Could not get iblock cookie."); 54901ae08745Sheppo return (EIO); 54911ae08745Sheppo } 54921ae08745Sheppo 54931ae08745Sheppo mutex_init(&vd->lock, NULL, MUTEX_DRIVER, iblock); 54941ae08745Sheppo vd->initialized |= VD_LOCKING; 54951ae08745Sheppo 54961ae08745Sheppo 5497d10e4ef2Snarayan /* Create start and completion task queues for the vdisk */ 5498d10e4ef2Snarayan (void) snprintf(tq_name, sizeof (tq_name), "vd_startq%lu", id); 54991ae08745Sheppo PR1("tq_name = %s", tq_name); 5500d10e4ef2Snarayan if ((vd->startq = ddi_taskq_create(vds->dip, tq_name, 1, 55011ae08745Sheppo TASKQ_DEFAULTPRI, 0)) == NULL) { 55021ae08745Sheppo PRN("Could not create task queue"); 55031ae08745Sheppo return (EIO); 55041ae08745Sheppo } 5505d10e4ef2Snarayan (void) snprintf(tq_name, sizeof (tq_name), "vd_completionq%lu", id); 5506d10e4ef2Snarayan PR1("tq_name = %s", tq_name); 5507d10e4ef2Snarayan if ((vd->completionq = ddi_taskq_create(vds->dip, tq_name, 1, 5508d10e4ef2Snarayan TASKQ_DEFAULTPRI, 0)) == NULL) { 5509d10e4ef2Snarayan PRN("Could not create task queue"); 5510d10e4ef2Snarayan return (EIO); 5511d10e4ef2Snarayan } 5512*5b98b509Sachartre 5513*5b98b509Sachartre /* Allocate the staging buffer */ 5514*5b98b509Sachartre vd->max_msglen = sizeof (vio_msg_t); /* baseline vio message size */ 5515*5b98b509Sachartre vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP); 5516*5b98b509Sachartre 5517d10e4ef2Snarayan vd->enabled = 1; /* before callback can dispatch to startq */ 55181ae08745Sheppo 55191ae08745Sheppo 55201ae08745Sheppo /* Bring up LDC */ 55211ae08745Sheppo ldc_attr.devclass = LDC_DEV_BLK_SVC; 55221ae08745Sheppo ldc_attr.instance = ddi_get_instance(vds->dip); 55231ae08745Sheppo ldc_attr.mode = LDC_MODE_UNRELIABLE; 5524e1ebb9ecSlm66018 ldc_attr.mtu = VD_LDC_MTU; 55251ae08745Sheppo if ((status = ldc_init(ldc_id, &ldc_attr, &vd->ldc_handle)) != 0) { 552617cadca8Slm66018 PRN("Could not initialize LDC channel %lx, " 5527690555a1Sachartre "init failed with error %d", ldc_id, status); 55281ae08745Sheppo return (status); 55291ae08745Sheppo } 55301ae08745Sheppo vd->initialized |= VD_LDC; 55311ae08745Sheppo 55321ae08745Sheppo if ((status = ldc_reg_callback(vd->ldc_handle, vd_handle_ldc_events, 55331ae08745Sheppo (caddr_t)vd)) != 0) { 5534690555a1Sachartre PRN("Could not initialize LDC channel %lu," 5535690555a1Sachartre "reg_callback failed with error %d", ldc_id, status); 55361ae08745Sheppo return (status); 55371ae08745Sheppo } 55381ae08745Sheppo 55391ae08745Sheppo if ((status = ldc_open(vd->ldc_handle)) != 0) { 5540690555a1Sachartre PRN("Could not initialize LDC channel %lu," 5541690555a1Sachartre "open failed with error %d", ldc_id, status); 55421ae08745Sheppo return (status); 55431ae08745Sheppo } 55441ae08745Sheppo 55453af08d82Slm66018 if ((status = ldc_up(vd->ldc_handle)) != 0) { 554634683adeSsg70180 PR0("ldc_up() returned errno %d", status); 55473af08d82Slm66018 } 55483af08d82Slm66018 55494bac2208Snarayan /* Allocate the inband task memory handle */ 55504bac2208Snarayan status = ldc_mem_alloc_handle(vd->ldc_handle, &(vd->inband_task.mhdl)); 55514bac2208Snarayan if (status) { 5552690555a1Sachartre PRN("Could not initialize LDC channel %lu," 5553690555a1Sachartre "alloc_handle failed with error %d", ldc_id, status); 55544bac2208Snarayan return (ENXIO); 55554bac2208Snarayan } 55561ae08745Sheppo 55571ae08745Sheppo /* Add the successfully-initialized vdisk to the server's table */ 55581ae08745Sheppo if (mod_hash_insert(vds->vd_table, (mod_hash_key_t)id, vd) != 0) { 55591ae08745Sheppo PRN("Error adding vdisk ID %lu to table", id); 55601ae08745Sheppo return (EIO); 55611ae08745Sheppo } 55621ae08745Sheppo 55633af08d82Slm66018 /* store initial state */ 55643af08d82Slm66018 vd->state = VD_STATE_INIT; 55653af08d82Slm66018 55661ae08745Sheppo return (0); 55671ae08745Sheppo } 55681ae08745Sheppo 55693af08d82Slm66018 static void 55703af08d82Slm66018 vd_free_dring_task(vd_t *vdp) 55713af08d82Slm66018 { 55723af08d82Slm66018 if (vdp->dring_task != NULL) { 55733af08d82Slm66018 ASSERT(vdp->dring_len != 0); 55743af08d82Slm66018 /* Free all dring_task memory handles */ 55753af08d82Slm66018 for (int i = 0; i < vdp->dring_len; i++) { 55763af08d82Slm66018 (void) ldc_mem_free_handle(vdp->dring_task[i].mhdl); 55773af08d82Slm66018 kmem_free(vdp->dring_task[i].msg, vdp->max_msglen); 55783af08d82Slm66018 vdp->dring_task[i].msg = NULL; 55793af08d82Slm66018 } 55803af08d82Slm66018 kmem_free(vdp->dring_task, 55813af08d82Slm66018 (sizeof (*vdp->dring_task)) * vdp->dring_len); 55823af08d82Slm66018 vdp->dring_task = NULL; 55833af08d82Slm66018 } 55843af08d82Slm66018 } 55853af08d82Slm66018 55861ae08745Sheppo /* 55871ae08745Sheppo * Destroy the state associated with a virtual disk 55881ae08745Sheppo */ 55891ae08745Sheppo static void 55901ae08745Sheppo vds_destroy_vd(void *arg) 55911ae08745Sheppo { 55921ae08745Sheppo vd_t *vd = (vd_t *)arg; 559334683adeSsg70180 int retry = 0, rv; 55941ae08745Sheppo 55951ae08745Sheppo if (vd == NULL) 55961ae08745Sheppo return; 55971ae08745Sheppo 5598d10e4ef2Snarayan PR0("Destroying vdisk state"); 5599d10e4ef2Snarayan 56001ae08745Sheppo /* Disable queuing requests for the vdisk */ 56011ae08745Sheppo if (vd->initialized & VD_LOCKING) { 56021ae08745Sheppo mutex_enter(&vd->lock); 56031ae08745Sheppo vd->enabled = 0; 56041ae08745Sheppo mutex_exit(&vd->lock); 56051ae08745Sheppo } 56061ae08745Sheppo 5607d10e4ef2Snarayan /* Drain and destroy start queue (*before* destroying completionq) */ 5608d10e4ef2Snarayan if (vd->startq != NULL) 5609d10e4ef2Snarayan ddi_taskq_destroy(vd->startq); /* waits for queued tasks */ 5610d10e4ef2Snarayan 5611d10e4ef2Snarayan /* Drain and destroy completion queue (*before* shutting down LDC) */ 5612d10e4ef2Snarayan if (vd->completionq != NULL) 5613d10e4ef2Snarayan ddi_taskq_destroy(vd->completionq); /* waits for tasks */ 5614d10e4ef2Snarayan 56153af08d82Slm66018 vd_free_dring_task(vd); 56163af08d82Slm66018 561734683adeSsg70180 /* Free the inband task memory handle */ 561834683adeSsg70180 (void) ldc_mem_free_handle(vd->inband_task.mhdl); 561934683adeSsg70180 562034683adeSsg70180 /* Shut down LDC */ 562134683adeSsg70180 if (vd->initialized & VD_LDC) { 562234683adeSsg70180 /* unmap the dring */ 562334683adeSsg70180 if (vd->initialized & VD_DRING) 562434683adeSsg70180 (void) ldc_mem_dring_unmap(vd->dring_handle); 562534683adeSsg70180 562634683adeSsg70180 /* close LDC channel - retry on EAGAIN */ 562734683adeSsg70180 while ((rv = ldc_close(vd->ldc_handle)) == EAGAIN) { 562834683adeSsg70180 if (++retry > vds_ldc_retries) { 562934683adeSsg70180 PR0("Timed out closing channel"); 563034683adeSsg70180 break; 563134683adeSsg70180 } 563234683adeSsg70180 drv_usecwait(vds_ldc_delay); 563334683adeSsg70180 } 563434683adeSsg70180 if (rv == 0) { 563534683adeSsg70180 (void) ldc_unreg_callback(vd->ldc_handle); 563634683adeSsg70180 (void) ldc_fini(vd->ldc_handle); 563734683adeSsg70180 } else { 563834683adeSsg70180 /* 563934683adeSsg70180 * Closing the LDC channel has failed. Ideally we should 564034683adeSsg70180 * fail here but there is no Zeus level infrastructure 564134683adeSsg70180 * to handle this. The MD has already been changed and 564234683adeSsg70180 * we have to do the close. So we try to do as much 564334683adeSsg70180 * clean up as we can. 564434683adeSsg70180 */ 564534683adeSsg70180 (void) ldc_set_cb_mode(vd->ldc_handle, LDC_CB_DISABLE); 564634683adeSsg70180 while (ldc_unreg_callback(vd->ldc_handle) == EAGAIN) 564734683adeSsg70180 drv_usecwait(vds_ldc_delay); 564834683adeSsg70180 } 564934683adeSsg70180 } 565034683adeSsg70180 56513af08d82Slm66018 /* Free the staging buffer for msgs */ 56523af08d82Slm66018 if (vd->vio_msgp != NULL) { 56533af08d82Slm66018 kmem_free(vd->vio_msgp, vd->max_msglen); 56543af08d82Slm66018 vd->vio_msgp = NULL; 56553af08d82Slm66018 } 56563af08d82Slm66018 56573af08d82Slm66018 /* Free the inband message buffer */ 56583af08d82Slm66018 if (vd->inband_task.msg != NULL) { 56593af08d82Slm66018 kmem_free(vd->inband_task.msg, vd->max_msglen); 56603af08d82Slm66018 vd->inband_task.msg = NULL; 5661d10e4ef2Snarayan } 5662da6c28aaSamw 56633c96341aSnarayan if (vd->file) { 5664690555a1Sachartre /* Close file */ 5665047ba61eSachartre (void) VOP_CLOSE(vd->file_vnode, vd->open_flags, 1, 5666da6c28aaSamw 0, kcred, NULL); 56673c96341aSnarayan VN_RELE(vd->file_vnode); 566887a7269eSachartre if (vd->file_devid != NULL) 566987a7269eSachartre ddi_devid_free(vd->file_devid); 56703c96341aSnarayan } else { 56711ae08745Sheppo /* Close any open backing-device slices */ 56721ae08745Sheppo for (uint_t slice = 0; slice < vd->nslices; slice++) { 56731ae08745Sheppo if (vd->ldi_handle[slice] != NULL) { 56741ae08745Sheppo PR0("Closing slice %u", slice); 56751ae08745Sheppo (void) ldi_close(vd->ldi_handle[slice], 5676047ba61eSachartre vd->open_flags, kcred); 56771ae08745Sheppo } 56781ae08745Sheppo } 56793c96341aSnarayan } 56801ae08745Sheppo 56811ae08745Sheppo /* Free lock */ 56821ae08745Sheppo if (vd->initialized & VD_LOCKING) 56831ae08745Sheppo mutex_destroy(&vd->lock); 56841ae08745Sheppo 56851ae08745Sheppo /* Finally, free the vdisk structure itself */ 56861ae08745Sheppo kmem_free(vd, sizeof (*vd)); 56871ae08745Sheppo } 56881ae08745Sheppo 56891ae08745Sheppo static int 5690047ba61eSachartre vds_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options, 5691047ba61eSachartre uint64_t ldc_id) 56921ae08745Sheppo { 56931ae08745Sheppo int status; 56941ae08745Sheppo vd_t *vd = NULL; 56951ae08745Sheppo 56961ae08745Sheppo 5697047ba61eSachartre if ((status = vds_do_init_vd(vds, id, device_path, options, 5698047ba61eSachartre ldc_id, &vd)) != 0) 56991ae08745Sheppo vds_destroy_vd(vd); 57001ae08745Sheppo 57011ae08745Sheppo return (status); 57021ae08745Sheppo } 57031ae08745Sheppo 57041ae08745Sheppo static int 57051ae08745Sheppo vds_do_get_ldc_id(md_t *md, mde_cookie_t vd_node, mde_cookie_t *channel, 57061ae08745Sheppo uint64_t *ldc_id) 57071ae08745Sheppo { 57081ae08745Sheppo int num_channels; 57091ae08745Sheppo 57101ae08745Sheppo 57111ae08745Sheppo /* Look for channel endpoint child(ren) of the vdisk MD node */ 57121ae08745Sheppo if ((num_channels = md_scan_dag(md, vd_node, 57131ae08745Sheppo md_find_name(md, VD_CHANNEL_ENDPOINT), 57141ae08745Sheppo md_find_name(md, "fwd"), channel)) <= 0) { 57151ae08745Sheppo PRN("No \"%s\" found for virtual disk", VD_CHANNEL_ENDPOINT); 57161ae08745Sheppo return (-1); 57171ae08745Sheppo } 57181ae08745Sheppo 57191ae08745Sheppo /* Get the "id" value for the first channel endpoint node */ 57201ae08745Sheppo if (md_get_prop_val(md, channel[0], VD_ID_PROP, ldc_id) != 0) { 57211ae08745Sheppo PRN("No \"%s\" property found for \"%s\" of vdisk", 57221ae08745Sheppo VD_ID_PROP, VD_CHANNEL_ENDPOINT); 57231ae08745Sheppo return (-1); 57241ae08745Sheppo } 57251ae08745Sheppo 57261ae08745Sheppo if (num_channels > 1) { 57271ae08745Sheppo PRN("Using ID of first of multiple channels for this vdisk"); 57281ae08745Sheppo } 57291ae08745Sheppo 57301ae08745Sheppo return (0); 57311ae08745Sheppo } 57321ae08745Sheppo 57331ae08745Sheppo static int 57341ae08745Sheppo vds_get_ldc_id(md_t *md, mde_cookie_t vd_node, uint64_t *ldc_id) 57351ae08745Sheppo { 57361ae08745Sheppo int num_nodes, status; 57371ae08745Sheppo size_t size; 57381ae08745Sheppo mde_cookie_t *channel; 57391ae08745Sheppo 57401ae08745Sheppo 57411ae08745Sheppo if ((num_nodes = md_node_count(md)) <= 0) { 57421ae08745Sheppo PRN("Invalid node count in Machine Description subtree"); 57431ae08745Sheppo return (-1); 57441ae08745Sheppo } 57451ae08745Sheppo size = num_nodes*(sizeof (*channel)); 57461ae08745Sheppo channel = kmem_zalloc(size, KM_SLEEP); 57471ae08745Sheppo status = vds_do_get_ldc_id(md, vd_node, channel, ldc_id); 57481ae08745Sheppo kmem_free(channel, size); 57491ae08745Sheppo 57501ae08745Sheppo return (status); 57511ae08745Sheppo } 57521ae08745Sheppo 5753047ba61eSachartre /* 5754047ba61eSachartre * Function: 5755047ba61eSachartre * vds_get_options 5756047ba61eSachartre * 5757047ba61eSachartre * Description: 5758047ba61eSachartre * Parse the options of a vds node. Options are defined as an array 5759047ba61eSachartre * of strings in the vds-block-device-opts property of the vds node 5760047ba61eSachartre * in the machine description. Options are returned as a bitmask. The 5761047ba61eSachartre * mapping between the bitmask options and the options strings from the 5762047ba61eSachartre * machine description is defined in the vd_bdev_options[] array. 5763047ba61eSachartre * 5764047ba61eSachartre * The vds-block-device-opts property is optional. If a vds has no such 5765047ba61eSachartre * property then no option is defined. 5766047ba61eSachartre * 5767047ba61eSachartre * Parameters: 5768047ba61eSachartre * md - machine description. 5769047ba61eSachartre * vd_node - vds node in the machine description for which 5770047ba61eSachartre * options have to be parsed. 5771047ba61eSachartre * options - the returned options. 5772047ba61eSachartre * 5773047ba61eSachartre * Return Code: 5774047ba61eSachartre * none. 5775047ba61eSachartre */ 5776047ba61eSachartre static void 5777047ba61eSachartre vds_get_options(md_t *md, mde_cookie_t vd_node, uint64_t *options) 5778047ba61eSachartre { 5779047ba61eSachartre char *optstr, *opt; 5780047ba61eSachartre int len, n, i; 5781047ba61eSachartre 5782047ba61eSachartre *options = 0; 5783047ba61eSachartre 5784047ba61eSachartre if (md_get_prop_data(md, vd_node, VD_BLOCK_DEVICE_OPTS, 5785047ba61eSachartre (uint8_t **)&optstr, &len) != 0) { 5786047ba61eSachartre PR0("No options found"); 5787047ba61eSachartre return; 5788047ba61eSachartre } 5789047ba61eSachartre 5790047ba61eSachartre /* parse options */ 5791047ba61eSachartre opt = optstr; 5792047ba61eSachartre n = sizeof (vd_bdev_options) / sizeof (vd_option_t); 5793047ba61eSachartre 5794047ba61eSachartre while (opt < optstr + len) { 5795047ba61eSachartre for (i = 0; i < n; i++) { 5796047ba61eSachartre if (strncmp(vd_bdev_options[i].vdo_name, 5797047ba61eSachartre opt, VD_OPTION_NLEN) == 0) { 5798047ba61eSachartre *options |= vd_bdev_options[i].vdo_value; 5799047ba61eSachartre break; 5800047ba61eSachartre } 5801047ba61eSachartre } 5802047ba61eSachartre 5803047ba61eSachartre if (i < n) { 5804047ba61eSachartre PR0("option: %s", opt); 5805047ba61eSachartre } else { 5806047ba61eSachartre PRN("option %s is unknown or unsupported", opt); 5807047ba61eSachartre } 5808047ba61eSachartre 5809047ba61eSachartre opt += strlen(opt) + 1; 5810047ba61eSachartre } 5811047ba61eSachartre } 5812047ba61eSachartre 58131ae08745Sheppo static void 58148fce2fd6Sachartre vds_driver_types_free(vds_t *vds) 58158fce2fd6Sachartre { 58168fce2fd6Sachartre if (vds->driver_types != NULL) { 58178fce2fd6Sachartre kmem_free(vds->driver_types, sizeof (vd_driver_type_t) * 58188fce2fd6Sachartre vds->num_drivers); 58198fce2fd6Sachartre vds->driver_types = NULL; 58208fce2fd6Sachartre vds->num_drivers = 0; 58218fce2fd6Sachartre } 58228fce2fd6Sachartre } 58238fce2fd6Sachartre 58248fce2fd6Sachartre /* 58258fce2fd6Sachartre * Update the driver type list with information from vds.conf. 58268fce2fd6Sachartre */ 58278fce2fd6Sachartre static void 58288fce2fd6Sachartre vds_driver_types_update(vds_t *vds) 58298fce2fd6Sachartre { 58308fce2fd6Sachartre char **list, *s; 58318fce2fd6Sachartre uint_t i, num, count = 0, len; 58328fce2fd6Sachartre 58338fce2fd6Sachartre if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, vds->dip, 58348fce2fd6Sachartre DDI_PROP_DONTPASS, "driver-type-list", &list, &num) != 58358fce2fd6Sachartre DDI_PROP_SUCCESS) 58368fce2fd6Sachartre return; 58378fce2fd6Sachartre 58388fce2fd6Sachartre /* 58398fce2fd6Sachartre * We create a driver_types list with as many as entries as there 58408fce2fd6Sachartre * is in the driver-type-list from vds.conf. However only valid 58418fce2fd6Sachartre * entries will be populated (i.e. entries from driver-type-list 58428fce2fd6Sachartre * with a valid syntax). Invalid entries will be left blank so 58438fce2fd6Sachartre * they will have no driver name and the driver type will be 58448fce2fd6Sachartre * VD_DRIVER_UNKNOWN (= 0). 58458fce2fd6Sachartre */ 58468fce2fd6Sachartre vds->num_drivers = num; 58478fce2fd6Sachartre vds->driver_types = kmem_zalloc(sizeof (vd_driver_type_t) * num, 58488fce2fd6Sachartre KM_SLEEP); 58498fce2fd6Sachartre 58508fce2fd6Sachartre for (i = 0; i < num; i++) { 58518fce2fd6Sachartre 58528fce2fd6Sachartre s = strchr(list[i], ':'); 58538fce2fd6Sachartre 58548fce2fd6Sachartre if (s == NULL) { 58558fce2fd6Sachartre PRN("vds.conf: driver-type-list, entry %d (%s): " 58568fce2fd6Sachartre "a colon is expected in the entry", 58578fce2fd6Sachartre i, list[i]); 58588fce2fd6Sachartre continue; 58598fce2fd6Sachartre } 58608fce2fd6Sachartre 58618fce2fd6Sachartre len = (uintptr_t)s - (uintptr_t)list[i]; 58628fce2fd6Sachartre 58638fce2fd6Sachartre if (len == 0) { 58648fce2fd6Sachartre PRN("vds.conf: driver-type-list, entry %d (%s): " 58658fce2fd6Sachartre "the driver name is empty", 58668fce2fd6Sachartre i, list[i]); 58678fce2fd6Sachartre continue; 58688fce2fd6Sachartre } 58698fce2fd6Sachartre 58708fce2fd6Sachartre if (len >= VD_DRIVER_NAME_LEN) { 58718fce2fd6Sachartre PRN("vds.conf: driver-type-list, entry %d (%s): " 58728fce2fd6Sachartre "the driver name is too long", 58738fce2fd6Sachartre i, list[i]); 58748fce2fd6Sachartre continue; 58758fce2fd6Sachartre } 58768fce2fd6Sachartre 58778fce2fd6Sachartre if (strcmp(s + 1, "disk") == 0) { 58788fce2fd6Sachartre 58798fce2fd6Sachartre vds->driver_types[i].type = VD_DRIVER_DISK; 58808fce2fd6Sachartre 58818fce2fd6Sachartre } else if (strcmp(s + 1, "volume") == 0) { 58828fce2fd6Sachartre 58838fce2fd6Sachartre vds->driver_types[i].type = VD_DRIVER_VOLUME; 58848fce2fd6Sachartre 58858fce2fd6Sachartre } else { 58868fce2fd6Sachartre PRN("vds.conf: driver-type-list, entry %d (%s): " 58878fce2fd6Sachartre "the driver type is invalid", 58888fce2fd6Sachartre i, list[i]); 58898fce2fd6Sachartre continue; 58908fce2fd6Sachartre } 58918fce2fd6Sachartre 58928fce2fd6Sachartre (void) strncpy(vds->driver_types[i].name, list[i], len); 58938fce2fd6Sachartre 58948fce2fd6Sachartre PR0("driver-type-list, entry %d (%s) added", 58958fce2fd6Sachartre i, list[i]); 58968fce2fd6Sachartre 58978fce2fd6Sachartre count++; 58988fce2fd6Sachartre } 58998fce2fd6Sachartre 59008fce2fd6Sachartre ddi_prop_free(list); 59018fce2fd6Sachartre 59028fce2fd6Sachartre if (count == 0) { 59038fce2fd6Sachartre /* nothing was added, clean up */ 59048fce2fd6Sachartre vds_driver_types_free(vds); 59058fce2fd6Sachartre } 59068fce2fd6Sachartre } 59078fce2fd6Sachartre 59088fce2fd6Sachartre static void 59091ae08745Sheppo vds_add_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node) 59101ae08745Sheppo { 5911e1ebb9ecSlm66018 char *device_path = NULL; 5912047ba61eSachartre uint64_t id = 0, ldc_id = 0, options = 0; 59131ae08745Sheppo 59141ae08745Sheppo if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) { 59151ae08745Sheppo PRN("Error getting vdisk \"%s\"", VD_ID_PROP); 59161ae08745Sheppo return; 59171ae08745Sheppo } 59181ae08745Sheppo PR0("Adding vdisk ID %lu", id); 59191ae08745Sheppo if (md_get_prop_str(md, vd_node, VD_BLOCK_DEVICE_PROP, 5920e1ebb9ecSlm66018 &device_path) != 0) { 59211ae08745Sheppo PRN("Error getting vdisk \"%s\"", VD_BLOCK_DEVICE_PROP); 59221ae08745Sheppo return; 59231ae08745Sheppo } 59241ae08745Sheppo 5925047ba61eSachartre vds_get_options(md, vd_node, &options); 5926047ba61eSachartre 59271ae08745Sheppo if (vds_get_ldc_id(md, vd_node, &ldc_id) != 0) { 59281ae08745Sheppo PRN("Error getting LDC ID for vdisk %lu", id); 59291ae08745Sheppo return; 59301ae08745Sheppo } 59311ae08745Sheppo 5932047ba61eSachartre if (vds_init_vd(vds, id, device_path, options, ldc_id) != 0) { 59331ae08745Sheppo PRN("Failed to add vdisk ID %lu", id); 593417cadca8Slm66018 if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0) 593517cadca8Slm66018 PRN("No vDisk entry found for vdisk ID %lu", id); 59361ae08745Sheppo return; 59371ae08745Sheppo } 59381ae08745Sheppo } 59391ae08745Sheppo 59401ae08745Sheppo static void 59411ae08745Sheppo vds_remove_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node) 59421ae08745Sheppo { 59431ae08745Sheppo uint64_t id = 0; 59441ae08745Sheppo 59451ae08745Sheppo 59461ae08745Sheppo if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) { 59471ae08745Sheppo PRN("Unable to get \"%s\" property from vdisk's MD node", 59481ae08745Sheppo VD_ID_PROP); 59491ae08745Sheppo return; 59501ae08745Sheppo } 59511ae08745Sheppo PR0("Removing vdisk ID %lu", id); 59521ae08745Sheppo if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0) 59531ae08745Sheppo PRN("No vdisk entry found for vdisk ID %lu", id); 59541ae08745Sheppo } 59551ae08745Sheppo 59561ae08745Sheppo static void 59571ae08745Sheppo vds_change_vd(vds_t *vds, md_t *prev_md, mde_cookie_t prev_vd_node, 59581ae08745Sheppo md_t *curr_md, mde_cookie_t curr_vd_node) 59591ae08745Sheppo { 59601ae08745Sheppo char *curr_dev, *prev_dev; 5961047ba61eSachartre uint64_t curr_id = 0, curr_ldc_id = 0, curr_options = 0; 5962047ba61eSachartre uint64_t prev_id = 0, prev_ldc_id = 0, prev_options = 0; 59631ae08745Sheppo size_t len; 59641ae08745Sheppo 59651ae08745Sheppo 59661ae08745Sheppo /* Validate that vdisk ID has not changed */ 59671ae08745Sheppo if (md_get_prop_val(prev_md, prev_vd_node, VD_ID_PROP, &prev_id) != 0) { 59681ae08745Sheppo PRN("Error getting previous vdisk \"%s\" property", 59691ae08745Sheppo VD_ID_PROP); 59701ae08745Sheppo return; 59711ae08745Sheppo } 59721ae08745Sheppo if (md_get_prop_val(curr_md, curr_vd_node, VD_ID_PROP, &curr_id) != 0) { 59731ae08745Sheppo PRN("Error getting current vdisk \"%s\" property", VD_ID_PROP); 59741ae08745Sheppo return; 59751ae08745Sheppo } 59761ae08745Sheppo if (curr_id != prev_id) { 59771ae08745Sheppo PRN("Not changing vdisk: ID changed from %lu to %lu", 59781ae08745Sheppo prev_id, curr_id); 59791ae08745Sheppo return; 59801ae08745Sheppo } 59811ae08745Sheppo 59821ae08745Sheppo /* Validate that LDC ID has not changed */ 59831ae08745Sheppo if (vds_get_ldc_id(prev_md, prev_vd_node, &prev_ldc_id) != 0) { 59841ae08745Sheppo PRN("Error getting LDC ID for vdisk %lu", prev_id); 59851ae08745Sheppo return; 59861ae08745Sheppo } 59871ae08745Sheppo 59881ae08745Sheppo if (vds_get_ldc_id(curr_md, curr_vd_node, &curr_ldc_id) != 0) { 59891ae08745Sheppo PRN("Error getting LDC ID for vdisk %lu", curr_id); 59901ae08745Sheppo return; 59911ae08745Sheppo } 59921ae08745Sheppo if (curr_ldc_id != prev_ldc_id) { 59930a55fbb7Slm66018 _NOTE(NOTREACHED); /* lint is confused */ 59941ae08745Sheppo PRN("Not changing vdisk: " 59951ae08745Sheppo "LDC ID changed from %lu to %lu", prev_ldc_id, curr_ldc_id); 59961ae08745Sheppo return; 59971ae08745Sheppo } 59981ae08745Sheppo 59991ae08745Sheppo /* Determine whether device path has changed */ 60001ae08745Sheppo if (md_get_prop_str(prev_md, prev_vd_node, VD_BLOCK_DEVICE_PROP, 60011ae08745Sheppo &prev_dev) != 0) { 60021ae08745Sheppo PRN("Error getting previous vdisk \"%s\"", 60031ae08745Sheppo VD_BLOCK_DEVICE_PROP); 60041ae08745Sheppo return; 60051ae08745Sheppo } 60061ae08745Sheppo if (md_get_prop_str(curr_md, curr_vd_node, VD_BLOCK_DEVICE_PROP, 60071ae08745Sheppo &curr_dev) != 0) { 60081ae08745Sheppo PRN("Error getting current vdisk \"%s\"", VD_BLOCK_DEVICE_PROP); 60091ae08745Sheppo return; 60101ae08745Sheppo } 60111ae08745Sheppo if (((len = strlen(curr_dev)) == strlen(prev_dev)) && 60121ae08745Sheppo (strncmp(curr_dev, prev_dev, len) == 0)) 60131ae08745Sheppo return; /* no relevant (supported) change */ 60141ae08745Sheppo 6015047ba61eSachartre /* Validate that options have not changed */ 6016047ba61eSachartre vds_get_options(prev_md, prev_vd_node, &prev_options); 6017047ba61eSachartre vds_get_options(curr_md, curr_vd_node, &curr_options); 6018047ba61eSachartre if (prev_options != curr_options) { 6019047ba61eSachartre PRN("Not changing vdisk: options changed from %lx to %lx", 6020047ba61eSachartre prev_options, curr_options); 6021047ba61eSachartre return; 6022047ba61eSachartre } 6023047ba61eSachartre 60241ae08745Sheppo PR0("Changing vdisk ID %lu", prev_id); 60253af08d82Slm66018 60261ae08745Sheppo /* Remove old state, which will close vdisk and reset */ 60271ae08745Sheppo if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)prev_id) != 0) 60281ae08745Sheppo PRN("No entry found for vdisk ID %lu", prev_id); 60293af08d82Slm66018 60301ae08745Sheppo /* Re-initialize vdisk with new state */ 6031047ba61eSachartre if (vds_init_vd(vds, curr_id, curr_dev, curr_options, 6032047ba61eSachartre curr_ldc_id) != 0) { 60331ae08745Sheppo PRN("Failed to change vdisk ID %lu", curr_id); 60341ae08745Sheppo return; 60351ae08745Sheppo } 60361ae08745Sheppo } 60371ae08745Sheppo 60381ae08745Sheppo static int 60391ae08745Sheppo vds_process_md(void *arg, mdeg_result_t *md) 60401ae08745Sheppo { 60411ae08745Sheppo int i; 60421ae08745Sheppo vds_t *vds = arg; 60431ae08745Sheppo 60441ae08745Sheppo 60451ae08745Sheppo if (md == NULL) 60461ae08745Sheppo return (MDEG_FAILURE); 60471ae08745Sheppo ASSERT(vds != NULL); 60481ae08745Sheppo 60491ae08745Sheppo for (i = 0; i < md->removed.nelem; i++) 60501ae08745Sheppo vds_remove_vd(vds, md->removed.mdp, md->removed.mdep[i]); 60511ae08745Sheppo for (i = 0; i < md->match_curr.nelem; i++) 60521ae08745Sheppo vds_change_vd(vds, md->match_prev.mdp, md->match_prev.mdep[i], 60531ae08745Sheppo md->match_curr.mdp, md->match_curr.mdep[i]); 60541ae08745Sheppo for (i = 0; i < md->added.nelem; i++) 60551ae08745Sheppo vds_add_vd(vds, md->added.mdp, md->added.mdep[i]); 60561ae08745Sheppo 60571ae08745Sheppo return (MDEG_SUCCESS); 60581ae08745Sheppo } 60591ae08745Sheppo 60603c96341aSnarayan 60611ae08745Sheppo static int 60621ae08745Sheppo vds_do_attach(dev_info_t *dip) 60631ae08745Sheppo { 6064445b4c2eSsb155480 int status, sz; 6065445b4c2eSsb155480 int cfg_handle; 60661ae08745Sheppo minor_t instance = ddi_get_instance(dip); 60671ae08745Sheppo vds_t *vds; 6068445b4c2eSsb155480 mdeg_prop_spec_t *pspecp; 6069445b4c2eSsb155480 mdeg_node_spec_t *ispecp; 60701ae08745Sheppo 60711ae08745Sheppo /* 60721ae08745Sheppo * The "cfg-handle" property of a vds node in an MD contains the MD's 60731ae08745Sheppo * notion of "instance", or unique identifier, for that node; OBP 60741ae08745Sheppo * stores the value of the "cfg-handle" MD property as the value of 60751ae08745Sheppo * the "reg" property on the node in the device tree it builds from 60761ae08745Sheppo * the MD and passes to Solaris. Thus, we look up the devinfo node's 60771ae08745Sheppo * "reg" property value to uniquely identify this device instance when 60781ae08745Sheppo * registering with the MD event-generation framework. If the "reg" 60791ae08745Sheppo * property cannot be found, the device tree state is presumably so 60801ae08745Sheppo * broken that there is no point in continuing. 60811ae08745Sheppo */ 6082445b4c2eSsb155480 if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 6083445b4c2eSsb155480 VD_REG_PROP)) { 6084445b4c2eSsb155480 PRN("vds \"%s\" property does not exist", VD_REG_PROP); 60851ae08745Sheppo return (DDI_FAILURE); 60861ae08745Sheppo } 60871ae08745Sheppo 60881ae08745Sheppo /* Get the MD instance for later MDEG registration */ 60891ae08745Sheppo cfg_handle = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 6090445b4c2eSsb155480 VD_REG_PROP, -1); 60911ae08745Sheppo 60921ae08745Sheppo if (ddi_soft_state_zalloc(vds_state, instance) != DDI_SUCCESS) { 60931ae08745Sheppo PRN("Could not allocate state for instance %u", instance); 60941ae08745Sheppo return (DDI_FAILURE); 60951ae08745Sheppo } 60961ae08745Sheppo 60971ae08745Sheppo if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) { 60981ae08745Sheppo PRN("Could not get state for instance %u", instance); 60991ae08745Sheppo ddi_soft_state_free(vds_state, instance); 61001ae08745Sheppo return (DDI_FAILURE); 61011ae08745Sheppo } 61021ae08745Sheppo 61031ae08745Sheppo vds->dip = dip; 61041ae08745Sheppo vds->vd_table = mod_hash_create_ptrhash("vds_vd_table", VDS_NCHAINS, 610587a7269eSachartre vds_destroy_vd, sizeof (void *)); 610687a7269eSachartre 61071ae08745Sheppo ASSERT(vds->vd_table != NULL); 61081ae08745Sheppo 61091ae08745Sheppo if ((status = ldi_ident_from_dip(dip, &vds->ldi_ident)) != 0) { 61101ae08745Sheppo PRN("ldi_ident_from_dip() returned errno %d", status); 61111ae08745Sheppo return (DDI_FAILURE); 61121ae08745Sheppo } 61131ae08745Sheppo vds->initialized |= VDS_LDI; 61141ae08745Sheppo 61151ae08745Sheppo /* Register for MD updates */ 6116445b4c2eSsb155480 sz = sizeof (vds_prop_template); 6117445b4c2eSsb155480 pspecp = kmem_alloc(sz, KM_SLEEP); 6118445b4c2eSsb155480 bcopy(vds_prop_template, pspecp, sz); 6119445b4c2eSsb155480 6120445b4c2eSsb155480 VDS_SET_MDEG_PROP_INST(pspecp, cfg_handle); 6121445b4c2eSsb155480 6122445b4c2eSsb155480 /* initialize the complete prop spec structure */ 6123445b4c2eSsb155480 ispecp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP); 6124445b4c2eSsb155480 ispecp->namep = "virtual-device"; 6125445b4c2eSsb155480 ispecp->specp = pspecp; 6126445b4c2eSsb155480 6127445b4c2eSsb155480 if (mdeg_register(ispecp, &vd_match, vds_process_md, vds, 61281ae08745Sheppo &vds->mdeg) != MDEG_SUCCESS) { 61291ae08745Sheppo PRN("Unable to register for MD updates"); 6130445b4c2eSsb155480 kmem_free(ispecp, sizeof (mdeg_node_spec_t)); 6131445b4c2eSsb155480 kmem_free(pspecp, sz); 61321ae08745Sheppo return (DDI_FAILURE); 61331ae08745Sheppo } 6134445b4c2eSsb155480 6135445b4c2eSsb155480 vds->ispecp = ispecp; 61361ae08745Sheppo vds->initialized |= VDS_MDEG; 61371ae08745Sheppo 61380a55fbb7Slm66018 /* Prevent auto-detaching so driver is available whenever MD changes */ 61390a55fbb7Slm66018 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1) != 61400a55fbb7Slm66018 DDI_PROP_SUCCESS) { 61410a55fbb7Slm66018 PRN("failed to set \"%s\" property for instance %u", 61420a55fbb7Slm66018 DDI_NO_AUTODETACH, instance); 61430a55fbb7Slm66018 } 61440a55fbb7Slm66018 61458fce2fd6Sachartre /* read any user defined driver types from conf file and update list */ 61468fce2fd6Sachartre vds_driver_types_update(vds); 61478fce2fd6Sachartre 61481ae08745Sheppo ddi_report_dev(dip); 61491ae08745Sheppo return (DDI_SUCCESS); 61501ae08745Sheppo } 61511ae08745Sheppo 61521ae08745Sheppo static int 61531ae08745Sheppo vds_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 61541ae08745Sheppo { 61551ae08745Sheppo int status; 61561ae08745Sheppo 61571ae08745Sheppo switch (cmd) { 61581ae08745Sheppo case DDI_ATTACH: 6159d10e4ef2Snarayan PR0("Attaching"); 61601ae08745Sheppo if ((status = vds_do_attach(dip)) != DDI_SUCCESS) 61611ae08745Sheppo (void) vds_detach(dip, DDI_DETACH); 61621ae08745Sheppo return (status); 61631ae08745Sheppo case DDI_RESUME: 6164d10e4ef2Snarayan PR0("No action required for DDI_RESUME"); 61651ae08745Sheppo return (DDI_SUCCESS); 61661ae08745Sheppo default: 61671ae08745Sheppo return (DDI_FAILURE); 61681ae08745Sheppo } 61691ae08745Sheppo } 61701ae08745Sheppo 61711ae08745Sheppo static struct dev_ops vds_ops = { 61721ae08745Sheppo DEVO_REV, /* devo_rev */ 61731ae08745Sheppo 0, /* devo_refcnt */ 61741ae08745Sheppo ddi_no_info, /* devo_getinfo */ 61751ae08745Sheppo nulldev, /* devo_identify */ 61761ae08745Sheppo nulldev, /* devo_probe */ 61771ae08745Sheppo vds_attach, /* devo_attach */ 61781ae08745Sheppo vds_detach, /* devo_detach */ 61791ae08745Sheppo nodev, /* devo_reset */ 61801ae08745Sheppo NULL, /* devo_cb_ops */ 61811ae08745Sheppo NULL, /* devo_bus_ops */ 61821ae08745Sheppo nulldev /* devo_power */ 61831ae08745Sheppo }; 61841ae08745Sheppo 61851ae08745Sheppo static struct modldrv modldrv = { 61861ae08745Sheppo &mod_driverops, 6187205eeb1aSlm66018 "virtual disk server", 61881ae08745Sheppo &vds_ops, 61891ae08745Sheppo }; 61901ae08745Sheppo 61911ae08745Sheppo static struct modlinkage modlinkage = { 61921ae08745Sheppo MODREV_1, 61931ae08745Sheppo &modldrv, 61941ae08745Sheppo NULL 61951ae08745Sheppo }; 61961ae08745Sheppo 61971ae08745Sheppo 61981ae08745Sheppo int 61991ae08745Sheppo _init(void) 62001ae08745Sheppo { 620117cadca8Slm66018 int status; 6202d10e4ef2Snarayan 62031ae08745Sheppo if ((status = ddi_soft_state_init(&vds_state, sizeof (vds_t), 1)) != 0) 62041ae08745Sheppo return (status); 620517cadca8Slm66018 62061ae08745Sheppo if ((status = mod_install(&modlinkage)) != 0) { 62071ae08745Sheppo ddi_soft_state_fini(&vds_state); 62081ae08745Sheppo return (status); 62091ae08745Sheppo } 62101ae08745Sheppo 62111ae08745Sheppo return (0); 62121ae08745Sheppo } 62131ae08745Sheppo 62141ae08745Sheppo int 62151ae08745Sheppo _info(struct modinfo *modinfop) 62161ae08745Sheppo { 62171ae08745Sheppo return (mod_info(&modlinkage, modinfop)); 62181ae08745Sheppo } 62191ae08745Sheppo 62201ae08745Sheppo int 62211ae08745Sheppo _fini(void) 62221ae08745Sheppo { 62231ae08745Sheppo int status; 62241ae08745Sheppo 62251ae08745Sheppo if ((status = mod_remove(&modlinkage)) != 0) 62261ae08745Sheppo return (status); 62271ae08745Sheppo ddi_soft_state_fini(&vds_state); 62281ae08745Sheppo return (0); 62291ae08745Sheppo } 6230