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 /* 233c96341aSnarayan * Copyright 2007 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> 401ae08745Sheppo #include <sys/mdeg.h> 411ae08745Sheppo #include <sys/modhash.h> 421ae08745Sheppo #include <sys/note.h> 431ae08745Sheppo #include <sys/pathname.h> 44205eeb1aSlm66018 #include <sys/sdt.h> 451ae08745Sheppo #include <sys/sunddi.h> 461ae08745Sheppo #include <sys/sunldi.h> 471ae08745Sheppo #include <sys/sysmacros.h> 481ae08745Sheppo #include <sys/vio_common.h> 491ae08745Sheppo #include <sys/vdsk_mailbox.h> 501ae08745Sheppo #include <sys/vdsk_common.h> 511ae08745Sheppo #include <sys/vtoc.h> 523c96341aSnarayan #include <sys/vfs.h> 533c96341aSnarayan #include <sys/stat.h> 5487a7269eSachartre #include <sys/scsi/impl/uscsi.h> 55690555a1Sachartre #include <vm/seg_map.h> 561ae08745Sheppo 571ae08745Sheppo /* Virtual disk server initialization flags */ 58d10e4ef2Snarayan #define VDS_LDI 0x01 59d10e4ef2Snarayan #define VDS_MDEG 0x02 601ae08745Sheppo 611ae08745Sheppo /* Virtual disk server tunable parameters */ 623c96341aSnarayan #define VDS_RETRIES 5 633c96341aSnarayan #define VDS_LDC_DELAY 1000 /* 1 msecs */ 643c96341aSnarayan #define VDS_DEV_DELAY 10000000 /* 10 secs */ 651ae08745Sheppo #define VDS_NCHAINS 32 661ae08745Sheppo 671ae08745Sheppo /* Identification parameters for MD, synthetic dkio(7i) structures, etc. */ 681ae08745Sheppo #define VDS_NAME "virtual-disk-server" 691ae08745Sheppo 701ae08745Sheppo #define VD_NAME "vd" 711ae08745Sheppo #define VD_VOLUME_NAME "vdisk" 721ae08745Sheppo #define VD_ASCIILABEL "Virtual Disk" 731ae08745Sheppo 741ae08745Sheppo #define VD_CHANNEL_ENDPOINT "channel-endpoint" 751ae08745Sheppo #define VD_ID_PROP "id" 761ae08745Sheppo #define VD_BLOCK_DEVICE_PROP "vds-block-device" 77047ba61eSachartre #define VD_BLOCK_DEVICE_OPTS "vds-block-device-opts" 78445b4c2eSsb155480 #define VD_REG_PROP "reg" 791ae08745Sheppo 801ae08745Sheppo /* Virtual disk initialization flags */ 813c96341aSnarayan #define VD_DISK_READY 0x01 823c96341aSnarayan #define VD_LOCKING 0x02 833c96341aSnarayan #define VD_LDC 0x04 843c96341aSnarayan #define VD_DRING 0x08 853c96341aSnarayan #define VD_SID 0x10 863c96341aSnarayan #define VD_SEQ_NUM 0x20 87047ba61eSachartre #define VD_SETUP_ERROR 0x40 881ae08745Sheppo 89eba0cb4eSachartre /* Flags for writing to a vdisk which is a file */ 90eba0cb4eSachartre #define VD_FILE_WRITE_FLAGS SM_ASYNC 91eba0cb4eSachartre 9287a7269eSachartre /* Number of backup labels */ 9387a7269eSachartre #define VD_FILE_NUM_BACKUP 5 9487a7269eSachartre 9587a7269eSachartre /* Timeout for SCSI I/O */ 9687a7269eSachartre #define VD_SCSI_RDWR_TIMEOUT 30 /* 30 secs */ 9787a7269eSachartre 981ae08745Sheppo /* 991ae08745Sheppo * By Solaris convention, slice/partition 2 represents the entire disk; 1001ae08745Sheppo * unfortunately, this convention does not appear to be codified. 1011ae08745Sheppo */ 1021ae08745Sheppo #define VD_ENTIRE_DISK_SLICE 2 1031ae08745Sheppo 1041ae08745Sheppo /* Return a cpp token as a string */ 1051ae08745Sheppo #define STRINGIZE(token) #token 1061ae08745Sheppo 1071ae08745Sheppo /* 1081ae08745Sheppo * Print a message prefixed with the current function name to the message log 1091ae08745Sheppo * (and optionally to the console for verbose boots); these macros use cpp's 1101ae08745Sheppo * concatenation of string literals and C99 variable-length-argument-list 1111ae08745Sheppo * macros 1121ae08745Sheppo */ 1131ae08745Sheppo #define PRN(...) _PRN("?%s(): "__VA_ARGS__, "") 1141ae08745Sheppo #define _PRN(format, ...) \ 1151ae08745Sheppo cmn_err(CE_CONT, format"%s", __func__, __VA_ARGS__) 1161ae08745Sheppo 1171ae08745Sheppo /* Return a pointer to the "i"th vdisk dring element */ 1181ae08745Sheppo #define VD_DRING_ELEM(i) ((vd_dring_entry_t *)(void *) \ 1191ae08745Sheppo (vd->dring + (i)*vd->descriptor_size)) 1201ae08745Sheppo 1211ae08745Sheppo /* Return the virtual disk client's type as a string (for use in messages) */ 1221ae08745Sheppo #define VD_CLIENT(vd) \ 1231ae08745Sheppo (((vd)->xfer_mode == VIO_DESC_MODE) ? "in-band client" : \ 1241ae08745Sheppo (((vd)->xfer_mode == VIO_DRING_MODE) ? "dring client" : \ 1251ae08745Sheppo (((vd)->xfer_mode == 0) ? "null client" : \ 1261ae08745Sheppo "unsupported client"))) 1271ae08745Sheppo 128690555a1Sachartre /* Read disk label from a disk on file */ 129690555a1Sachartre #define VD_FILE_LABEL_READ(vd, labelp) \ 13087a7269eSachartre vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)labelp, \ 131690555a1Sachartre 0, sizeof (struct dk_label)) 132690555a1Sachartre 133690555a1Sachartre /* Write disk label to a disk on file */ 134690555a1Sachartre #define VD_FILE_LABEL_WRITE(vd, labelp) \ 13587a7269eSachartre vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)labelp, \ 136690555a1Sachartre 0, sizeof (struct dk_label)) 137690555a1Sachartre 138445b4c2eSsb155480 /* 139445b4c2eSsb155480 * Specification of an MD node passed to the MDEG to filter any 140445b4c2eSsb155480 * 'vport' nodes that do not belong to the specified node. This 141445b4c2eSsb155480 * template is copied for each vds instance and filled in with 142445b4c2eSsb155480 * the appropriate 'cfg-handle' value before being passed to the MDEG. 143445b4c2eSsb155480 */ 144445b4c2eSsb155480 static mdeg_prop_spec_t vds_prop_template[] = { 145445b4c2eSsb155480 { MDET_PROP_STR, "name", VDS_NAME }, 146445b4c2eSsb155480 { MDET_PROP_VAL, "cfg-handle", NULL }, 147445b4c2eSsb155480 { MDET_LIST_END, NULL, NULL } 148445b4c2eSsb155480 }; 149445b4c2eSsb155480 150445b4c2eSsb155480 #define VDS_SET_MDEG_PROP_INST(specp, val) (specp)[1].ps_val = (val); 151445b4c2eSsb155480 152445b4c2eSsb155480 /* 153445b4c2eSsb155480 * Matching criteria passed to the MDEG to register interest 154445b4c2eSsb155480 * in changes to 'virtual-device-port' nodes identified by their 155445b4c2eSsb155480 * 'id' property. 156445b4c2eSsb155480 */ 157445b4c2eSsb155480 static md_prop_match_t vd_prop_match[] = { 158445b4c2eSsb155480 { MDET_PROP_VAL, VD_ID_PROP }, 159445b4c2eSsb155480 { MDET_LIST_END, NULL } 160445b4c2eSsb155480 }; 161445b4c2eSsb155480 162445b4c2eSsb155480 static mdeg_node_match_t vd_match = {"virtual-device-port", 163445b4c2eSsb155480 vd_prop_match}; 164445b4c2eSsb155480 165047ba61eSachartre /* 166047ba61eSachartre * Options for the VD_BLOCK_DEVICE_OPTS property. 167047ba61eSachartre */ 168047ba61eSachartre #define VD_OPT_RDONLY 0x1 /* read-only */ 169047ba61eSachartre #define VD_OPT_SLICE 0x2 /* single slice */ 170047ba61eSachartre #define VD_OPT_EXCLUSIVE 0x4 /* exclusive access */ 171047ba61eSachartre 172047ba61eSachartre #define VD_OPTION_NLEN 128 173047ba61eSachartre 174047ba61eSachartre typedef struct vd_option { 175047ba61eSachartre char vdo_name[VD_OPTION_NLEN]; 176047ba61eSachartre uint64_t vdo_value; 177047ba61eSachartre } vd_option_t; 178047ba61eSachartre 179047ba61eSachartre vd_option_t vd_bdev_options[] = { 180047ba61eSachartre { "ro", VD_OPT_RDONLY }, 181047ba61eSachartre { "slice", VD_OPT_SLICE }, 182047ba61eSachartre { "excl", VD_OPT_EXCLUSIVE } 183047ba61eSachartre }; 184047ba61eSachartre 1851ae08745Sheppo /* Debugging macros */ 1861ae08745Sheppo #ifdef DEBUG 1873af08d82Slm66018 1883af08d82Slm66018 static int vd_msglevel = 0; 1893af08d82Slm66018 1901ae08745Sheppo #define PR0 if (vd_msglevel > 0) PRN 1911ae08745Sheppo #define PR1 if (vd_msglevel > 1) PRN 1921ae08745Sheppo #define PR2 if (vd_msglevel > 2) PRN 1931ae08745Sheppo 1941ae08745Sheppo #define VD_DUMP_DRING_ELEM(elem) \ 1953c96341aSnarayan PR0("dst:%x op:%x st:%u nb:%lx addr:%lx ncook:%u\n", \ 1961ae08745Sheppo elem->hdr.dstate, \ 1971ae08745Sheppo elem->payload.operation, \ 1981ae08745Sheppo elem->payload.status, \ 1991ae08745Sheppo elem->payload.nbytes, \ 2001ae08745Sheppo elem->payload.addr, \ 2011ae08745Sheppo elem->payload.ncookies); 2021ae08745Sheppo 2033af08d82Slm66018 char * 2043af08d82Slm66018 vd_decode_state(int state) 2053af08d82Slm66018 { 2063af08d82Slm66018 char *str; 2073af08d82Slm66018 2083af08d82Slm66018 #define CASE_STATE(_s) case _s: str = #_s; break; 2093af08d82Slm66018 2103af08d82Slm66018 switch (state) { 2113af08d82Slm66018 CASE_STATE(VD_STATE_INIT) 2123af08d82Slm66018 CASE_STATE(VD_STATE_VER) 2133af08d82Slm66018 CASE_STATE(VD_STATE_ATTR) 2143af08d82Slm66018 CASE_STATE(VD_STATE_DRING) 2153af08d82Slm66018 CASE_STATE(VD_STATE_RDX) 2163af08d82Slm66018 CASE_STATE(VD_STATE_DATA) 2173af08d82Slm66018 default: str = "unknown"; break; 2183af08d82Slm66018 } 2193af08d82Slm66018 2203af08d82Slm66018 #undef CASE_STATE 2213af08d82Slm66018 2223af08d82Slm66018 return (str); 2233af08d82Slm66018 } 2243af08d82Slm66018 2253af08d82Slm66018 void 2263af08d82Slm66018 vd_decode_tag(vio_msg_t *msg) 2273af08d82Slm66018 { 2283af08d82Slm66018 char *tstr, *sstr, *estr; 2293af08d82Slm66018 2303af08d82Slm66018 #define CASE_TYPE(_s) case _s: tstr = #_s; break; 2313af08d82Slm66018 2323af08d82Slm66018 switch (msg->tag.vio_msgtype) { 2333af08d82Slm66018 CASE_TYPE(VIO_TYPE_CTRL) 2343af08d82Slm66018 CASE_TYPE(VIO_TYPE_DATA) 2353af08d82Slm66018 CASE_TYPE(VIO_TYPE_ERR) 2363af08d82Slm66018 default: tstr = "unknown"; break; 2373af08d82Slm66018 } 2383af08d82Slm66018 2393af08d82Slm66018 #undef CASE_TYPE 2403af08d82Slm66018 2413af08d82Slm66018 #define CASE_SUBTYPE(_s) case _s: sstr = #_s; break; 2423af08d82Slm66018 2433af08d82Slm66018 switch (msg->tag.vio_subtype) { 2443af08d82Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_INFO) 2453af08d82Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_ACK) 2463af08d82Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_NACK) 2473af08d82Slm66018 default: sstr = "unknown"; break; 2483af08d82Slm66018 } 2493af08d82Slm66018 2503af08d82Slm66018 #undef CASE_SUBTYPE 2513af08d82Slm66018 2523af08d82Slm66018 #define CASE_ENV(_s) case _s: estr = #_s; break; 2533af08d82Slm66018 2543af08d82Slm66018 switch (msg->tag.vio_subtype_env) { 2553af08d82Slm66018 CASE_ENV(VIO_VER_INFO) 2563af08d82Slm66018 CASE_ENV(VIO_ATTR_INFO) 2573af08d82Slm66018 CASE_ENV(VIO_DRING_REG) 2583af08d82Slm66018 CASE_ENV(VIO_DRING_UNREG) 2593af08d82Slm66018 CASE_ENV(VIO_RDX) 2603af08d82Slm66018 CASE_ENV(VIO_PKT_DATA) 2613af08d82Slm66018 CASE_ENV(VIO_DESC_DATA) 2623af08d82Slm66018 CASE_ENV(VIO_DRING_DATA) 2633af08d82Slm66018 default: estr = "unknown"; break; 2643af08d82Slm66018 } 2653af08d82Slm66018 2663af08d82Slm66018 #undef CASE_ENV 2673af08d82Slm66018 2683af08d82Slm66018 PR1("(%x/%x/%x) message : (%s/%s/%s)", 2693af08d82Slm66018 msg->tag.vio_msgtype, msg->tag.vio_subtype, 2703af08d82Slm66018 msg->tag.vio_subtype_env, tstr, sstr, estr); 2713af08d82Slm66018 } 2723af08d82Slm66018 2731ae08745Sheppo #else /* !DEBUG */ 2743af08d82Slm66018 2751ae08745Sheppo #define PR0(...) 2761ae08745Sheppo #define PR1(...) 2771ae08745Sheppo #define PR2(...) 2781ae08745Sheppo 2791ae08745Sheppo #define VD_DUMP_DRING_ELEM(elem) 2801ae08745Sheppo 2813af08d82Slm66018 #define vd_decode_state(_s) (NULL) 2823af08d82Slm66018 #define vd_decode_tag(_s) (NULL) 2833af08d82Slm66018 2841ae08745Sheppo #endif /* DEBUG */ 2851ae08745Sheppo 2861ae08745Sheppo 287d10e4ef2Snarayan /* 288d10e4ef2Snarayan * Soft state structure for a vds instance 289d10e4ef2Snarayan */ 2901ae08745Sheppo typedef struct vds { 2911ae08745Sheppo uint_t initialized; /* driver inst initialization flags */ 2921ae08745Sheppo dev_info_t *dip; /* driver inst devinfo pointer */ 2931ae08745Sheppo ldi_ident_t ldi_ident; /* driver's identifier for LDI */ 2941ae08745Sheppo mod_hash_t *vd_table; /* table of virtual disks served */ 295445b4c2eSsb155480 mdeg_node_spec_t *ispecp; /* mdeg node specification */ 2961ae08745Sheppo mdeg_handle_t mdeg; /* handle for MDEG operations */ 2971ae08745Sheppo } vds_t; 2981ae08745Sheppo 299d10e4ef2Snarayan /* 300d10e4ef2Snarayan * Types of descriptor-processing tasks 301d10e4ef2Snarayan */ 302d10e4ef2Snarayan typedef enum vd_task_type { 303d10e4ef2Snarayan VD_NONFINAL_RANGE_TASK, /* task for intermediate descriptor in range */ 304d10e4ef2Snarayan VD_FINAL_RANGE_TASK, /* task for last in a range of descriptors */ 305d10e4ef2Snarayan } vd_task_type_t; 306d10e4ef2Snarayan 307d10e4ef2Snarayan /* 308d10e4ef2Snarayan * Structure describing the task for processing a descriptor 309d10e4ef2Snarayan */ 310d10e4ef2Snarayan typedef struct vd_task { 311d10e4ef2Snarayan struct vd *vd; /* vd instance task is for */ 312d10e4ef2Snarayan vd_task_type_t type; /* type of descriptor task */ 313d10e4ef2Snarayan int index; /* dring elem index for task */ 314d10e4ef2Snarayan vio_msg_t *msg; /* VIO message task is for */ 315d10e4ef2Snarayan size_t msglen; /* length of message content */ 316d10e4ef2Snarayan vd_dring_payload_t *request; /* request task will perform */ 317d10e4ef2Snarayan struct buf buf; /* buf(9s) for I/O request */ 3184bac2208Snarayan ldc_mem_handle_t mhdl; /* task memory handle */ 319205eeb1aSlm66018 int status; /* status of processing task */ 320205eeb1aSlm66018 int (*completef)(struct vd_task *task); /* completion func ptr */ 321d10e4ef2Snarayan } vd_task_t; 322d10e4ef2Snarayan 323d10e4ef2Snarayan /* 324d10e4ef2Snarayan * Soft state structure for a virtual disk instance 325d10e4ef2Snarayan */ 3261ae08745Sheppo typedef struct vd { 3271ae08745Sheppo uint_t initialized; /* vdisk initialization flags */ 3281ae08745Sheppo vds_t *vds; /* server for this vdisk */ 329d10e4ef2Snarayan ddi_taskq_t *startq; /* queue for I/O start tasks */ 330d10e4ef2Snarayan ddi_taskq_t *completionq; /* queue for completion tasks */ 3311ae08745Sheppo ldi_handle_t ldi_handle[V_NUMPAR]; /* LDI slice handles */ 3323c96341aSnarayan char device_path[MAXPATHLEN + 1]; /* vdisk device */ 3331ae08745Sheppo dev_t dev[V_NUMPAR]; /* dev numbers for slices */ 334047ba61eSachartre int open_flags; /* open flags */ 335e1ebb9ecSlm66018 uint_t nslices; /* number of slices */ 3361ae08745Sheppo size_t vdisk_size; /* number of blocks in vdisk */ 3371ae08745Sheppo vd_disk_type_t vdisk_type; /* slice or entire disk */ 3384bac2208Snarayan vd_disk_label_t vdisk_label; /* EFI or VTOC label */ 339e1ebb9ecSlm66018 ushort_t max_xfer_sz; /* max xfer size in DEV_BSIZE */ 3401ae08745Sheppo boolean_t pseudo; /* underlying pseudo dev */ 3413c96341aSnarayan boolean_t file; /* underlying file */ 3423c96341aSnarayan vnode_t *file_vnode; /* file vnode */ 3433c96341aSnarayan size_t file_size; /* file size */ 34487a7269eSachartre ddi_devid_t file_devid; /* devid for disk image */ 3454bac2208Snarayan struct dk_efi dk_efi; /* synthetic for slice type */ 3461ae08745Sheppo struct dk_geom dk_geom; /* synthetic for slice type */ 3471ae08745Sheppo struct vtoc vtoc; /* synthetic for slice type */ 3481ae08745Sheppo ldc_status_t ldc_state; /* LDC connection state */ 3491ae08745Sheppo ldc_handle_t ldc_handle; /* handle for LDC comm */ 3501ae08745Sheppo size_t max_msglen; /* largest LDC message len */ 3511ae08745Sheppo vd_state_t state; /* client handshake state */ 3521ae08745Sheppo uint8_t xfer_mode; /* transfer mode with client */ 3531ae08745Sheppo uint32_t sid; /* client's session ID */ 3541ae08745Sheppo uint64_t seq_num; /* message sequence number */ 3551ae08745Sheppo uint64_t dring_ident; /* identifier of dring */ 3561ae08745Sheppo ldc_dring_handle_t dring_handle; /* handle for dring ops */ 3571ae08745Sheppo uint32_t descriptor_size; /* num bytes in desc */ 3581ae08745Sheppo uint32_t dring_len; /* number of dring elements */ 3591ae08745Sheppo caddr_t dring; /* address of dring */ 3603af08d82Slm66018 caddr_t vio_msgp; /* vio msg staging buffer */ 361d10e4ef2Snarayan vd_task_t inband_task; /* task for inband descriptor */ 362d10e4ef2Snarayan vd_task_t *dring_task; /* tasks dring elements */ 363d10e4ef2Snarayan 364d10e4ef2Snarayan kmutex_t lock; /* protects variables below */ 365d10e4ef2Snarayan boolean_t enabled; /* is vdisk enabled? */ 366d10e4ef2Snarayan boolean_t reset_state; /* reset connection state? */ 367d10e4ef2Snarayan boolean_t reset_ldc; /* reset LDC channel? */ 3681ae08745Sheppo } vd_t; 3691ae08745Sheppo 3701ae08745Sheppo typedef struct vds_operation { 3713af08d82Slm66018 char *namep; 3721ae08745Sheppo uint8_t operation; 373d10e4ef2Snarayan int (*start)(vd_task_t *task); 374205eeb1aSlm66018 int (*complete)(vd_task_t *task); 3751ae08745Sheppo } vds_operation_t; 3761ae08745Sheppo 3770a55fbb7Slm66018 typedef struct vd_ioctl { 3780a55fbb7Slm66018 uint8_t operation; /* vdisk operation */ 3790a55fbb7Slm66018 const char *operation_name; /* vdisk operation name */ 3800a55fbb7Slm66018 size_t nbytes; /* size of operation buffer */ 3810a55fbb7Slm66018 int cmd; /* corresponding ioctl cmd */ 3820a55fbb7Slm66018 const char *cmd_name; /* ioctl cmd name */ 3830a55fbb7Slm66018 void *arg; /* ioctl cmd argument */ 3840a55fbb7Slm66018 /* convert input vd_buf to output ioctl_arg */ 3850a55fbb7Slm66018 void (*copyin)(void *vd_buf, void *ioctl_arg); 3860a55fbb7Slm66018 /* convert input ioctl_arg to output vd_buf */ 3870a55fbb7Slm66018 void (*copyout)(void *ioctl_arg, void *vd_buf); 388047ba61eSachartre /* write is true if the operation writes any data to the backend */ 389047ba61eSachartre boolean_t write; 3900a55fbb7Slm66018 } vd_ioctl_t; 3910a55fbb7Slm66018 3920a55fbb7Slm66018 /* Define trivial copyin/copyout conversion function flag */ 3930a55fbb7Slm66018 #define VD_IDENTITY ((void (*)(void *, void *))-1) 3941ae08745Sheppo 3951ae08745Sheppo 3963c96341aSnarayan static int vds_ldc_retries = VDS_RETRIES; 3973af08d82Slm66018 static int vds_ldc_delay = VDS_LDC_DELAY; 3983c96341aSnarayan static int vds_dev_retries = VDS_RETRIES; 3993c96341aSnarayan static int vds_dev_delay = VDS_DEV_DELAY; 4001ae08745Sheppo static void *vds_state; 4011ae08745Sheppo static uint64_t vds_operations; /* see vds_operation[] definition below */ 4021ae08745Sheppo 403eba0cb4eSachartre static uint_t vd_file_write_flags = VD_FILE_WRITE_FLAGS; 404eba0cb4eSachartre 40587a7269eSachartre static short vd_scsi_rdwr_timeout = VD_SCSI_RDWR_TIMEOUT; 40687a7269eSachartre 4070a55fbb7Slm66018 /* 4080a55fbb7Slm66018 * Supported protocol version pairs, from highest (newest) to lowest (oldest) 4090a55fbb7Slm66018 * 4100a55fbb7Slm66018 * Each supported major version should appear only once, paired with (and only 4110a55fbb7Slm66018 * with) its highest supported minor version number (as the protocol requires 4120a55fbb7Slm66018 * supporting all lower minor version numbers as well) 4130a55fbb7Slm66018 */ 4140a55fbb7Slm66018 static const vio_ver_t vds_version[] = {{1, 0}}; 4150a55fbb7Slm66018 static const size_t vds_num_versions = 4160a55fbb7Slm66018 sizeof (vds_version)/sizeof (vds_version[0]); 4170a55fbb7Slm66018 4183af08d82Slm66018 static void vd_free_dring_task(vd_t *vdp); 4193c96341aSnarayan static int vd_setup_vd(vd_t *vd); 420047ba61eSachartre static int vd_setup_single_slice_disk(vd_t *vd); 4213c96341aSnarayan static boolean_t vd_enabled(vd_t *vd); 42278fcd0a1Sachartre static ushort_t vd_lbl2cksum(struct dk_label *label); 42378fcd0a1Sachartre static int vd_file_validate_geometry(vd_t *vd); 424047ba61eSachartre 425690555a1Sachartre /* 426690555a1Sachartre * Function: 427690555a1Sachartre * vd_file_rw 428690555a1Sachartre * 429690555a1Sachartre * Description: 430690555a1Sachartre * Read or write to a disk on file. 431690555a1Sachartre * 432690555a1Sachartre * Parameters: 433690555a1Sachartre * vd - disk on which the operation is performed. 434690555a1Sachartre * slice - slice on which the operation is performed, 43587a7269eSachartre * VD_SLICE_NONE indicates that the operation 43687a7269eSachartre * is done using an absolute disk offset. 437690555a1Sachartre * operation - operation to execute: read (VD_OP_BREAD) or 438690555a1Sachartre * write (VD_OP_BWRITE). 439690555a1Sachartre * data - buffer where data are read to or written from. 440690555a1Sachartre * blk - starting block for the operation. 441690555a1Sachartre * len - number of bytes to read or write. 442690555a1Sachartre * 443690555a1Sachartre * Return Code: 444690555a1Sachartre * n >= 0 - success, n indicates the number of bytes read 445690555a1Sachartre * or written. 446690555a1Sachartre * -1 - error. 447690555a1Sachartre */ 448690555a1Sachartre static ssize_t 449690555a1Sachartre vd_file_rw(vd_t *vd, int slice, int operation, caddr_t data, size_t blk, 450690555a1Sachartre size_t len) 451690555a1Sachartre { 452690555a1Sachartre caddr_t maddr; 453690555a1Sachartre size_t offset, maxlen, moffset, mlen, n; 454690555a1Sachartre uint_t smflags; 455690555a1Sachartre enum seg_rw srw; 456690555a1Sachartre 457690555a1Sachartre ASSERT(vd->file); 458690555a1Sachartre ASSERT(len > 0); 459690555a1Sachartre 460047ba61eSachartre /* 461047ba61eSachartre * If a file is exported as a slice then we don't care about the vtoc. 462047ba61eSachartre * In that case, the vtoc is a fake mainly to make newfs happy and we 463047ba61eSachartre * handle any I/O as a raw disk access so that we can have access to the 464047ba61eSachartre * entire backend. 465047ba61eSachartre */ 466047ba61eSachartre if (vd->vdisk_type == VD_DISK_TYPE_SLICE || slice == VD_SLICE_NONE) { 467690555a1Sachartre /* raw disk access */ 468690555a1Sachartre offset = blk * DEV_BSIZE; 469690555a1Sachartre } else { 470690555a1Sachartre ASSERT(slice >= 0 && slice < V_NUMPAR); 47178fcd0a1Sachartre 47278fcd0a1Sachartre if (vd->vdisk_label == VD_DISK_LABEL_UNK && 47378fcd0a1Sachartre vd_file_validate_geometry(vd) != 0) { 47478fcd0a1Sachartre PR0("Unknown disk label, can't do I/O from slice %d", 47578fcd0a1Sachartre slice); 47678fcd0a1Sachartre return (-1); 47778fcd0a1Sachartre } 47878fcd0a1Sachartre 479690555a1Sachartre if (blk >= vd->vtoc.v_part[slice].p_size) { 480690555a1Sachartre /* address past the end of the slice */ 481690555a1Sachartre PR0("req_addr (0x%lx) > psize (0x%lx)", 482690555a1Sachartre blk, vd->vtoc.v_part[slice].p_size); 483690555a1Sachartre return (0); 484690555a1Sachartre } 485690555a1Sachartre 486690555a1Sachartre offset = (vd->vtoc.v_part[slice].p_start + blk) * DEV_BSIZE; 487690555a1Sachartre 488690555a1Sachartre /* 489690555a1Sachartre * If the requested size is greater than the size 490690555a1Sachartre * of the partition, truncate the read/write. 491690555a1Sachartre */ 492690555a1Sachartre maxlen = (vd->vtoc.v_part[slice].p_size - blk) * DEV_BSIZE; 493690555a1Sachartre 494690555a1Sachartre if (len > maxlen) { 495690555a1Sachartre PR0("I/O size truncated to %lu bytes from %lu bytes", 496690555a1Sachartre maxlen, len); 497690555a1Sachartre len = maxlen; 498690555a1Sachartre } 499690555a1Sachartre } 500690555a1Sachartre 501690555a1Sachartre /* 502690555a1Sachartre * We have to ensure that we are reading/writing into the mmap 503690555a1Sachartre * range. If we have a partial disk image (e.g. an image of 504690555a1Sachartre * s0 instead s2) the system can try to access slices that 505690555a1Sachartre * are not included into the disk image. 506690555a1Sachartre */ 507690555a1Sachartre if ((offset + len) >= vd->file_size) { 508690555a1Sachartre PR0("offset + nbytes (0x%lx + 0x%lx) >= " 509690555a1Sachartre "file_size (0x%lx)", offset, len, vd->file_size); 510690555a1Sachartre return (-1); 511690555a1Sachartre } 512690555a1Sachartre 513690555a1Sachartre srw = (operation == VD_OP_BREAD)? S_READ : S_WRITE; 514eba0cb4eSachartre smflags = (operation == VD_OP_BREAD)? 0 : 515eba0cb4eSachartre (SM_WRITE | vd_file_write_flags); 516690555a1Sachartre n = len; 517690555a1Sachartre 518690555a1Sachartre do { 519690555a1Sachartre /* 520690555a1Sachartre * segmap_getmapflt() returns a MAXBSIZE chunk which is 521690555a1Sachartre * MAXBSIZE aligned. 522690555a1Sachartre */ 523690555a1Sachartre moffset = offset & MAXBOFFSET; 524690555a1Sachartre mlen = MIN(MAXBSIZE - moffset, n); 525690555a1Sachartre maddr = segmap_getmapflt(segkmap, vd->file_vnode, offset, 526690555a1Sachartre mlen, 1, srw); 527690555a1Sachartre /* 528690555a1Sachartre * Fault in the pages so we can check for error and ensure 529690555a1Sachartre * that we can safely used the mapped address. 530690555a1Sachartre */ 531690555a1Sachartre if (segmap_fault(kas.a_hat, segkmap, maddr, mlen, 532690555a1Sachartre F_SOFTLOCK, srw) != 0) { 533690555a1Sachartre (void) segmap_release(segkmap, maddr, 0); 534690555a1Sachartre return (-1); 535690555a1Sachartre } 536690555a1Sachartre 537690555a1Sachartre if (operation == VD_OP_BREAD) 538690555a1Sachartre bcopy(maddr + moffset, data, mlen); 539690555a1Sachartre else 540690555a1Sachartre bcopy(data, maddr + moffset, mlen); 541690555a1Sachartre 542690555a1Sachartre if (segmap_fault(kas.a_hat, segkmap, maddr, mlen, 543690555a1Sachartre F_SOFTUNLOCK, srw) != 0) { 544690555a1Sachartre (void) segmap_release(segkmap, maddr, 0); 545690555a1Sachartre return (-1); 546690555a1Sachartre } 547690555a1Sachartre if (segmap_release(segkmap, maddr, smflags) != 0) 548690555a1Sachartre return (-1); 549690555a1Sachartre n -= mlen; 550690555a1Sachartre offset += mlen; 551690555a1Sachartre data += mlen; 552690555a1Sachartre 553690555a1Sachartre } while (n > 0); 554690555a1Sachartre 555690555a1Sachartre return (len); 556690555a1Sachartre } 557690555a1Sachartre 55887a7269eSachartre /* 55987a7269eSachartre * Function: 56078fcd0a1Sachartre * vd_file_build_default_label 56178fcd0a1Sachartre * 56278fcd0a1Sachartre * Description: 56378fcd0a1Sachartre * Return a default label for the given disk. This is used when the disk 56478fcd0a1Sachartre * does not have a valid VTOC so that the user can get a valid default 56578fcd0a1Sachartre * configuration. The default label have all slices size set to 0 (except 56678fcd0a1Sachartre * slice 2 which is the entire disk) to force the user to write a valid 56778fcd0a1Sachartre * label onto the disk image. 56878fcd0a1Sachartre * 56978fcd0a1Sachartre * Parameters: 57078fcd0a1Sachartre * vd - disk on which the operation is performed. 57178fcd0a1Sachartre * label - the returned default label. 57278fcd0a1Sachartre * 57378fcd0a1Sachartre * Return Code: 57478fcd0a1Sachartre * none. 57578fcd0a1Sachartre */ 57678fcd0a1Sachartre static void 57778fcd0a1Sachartre vd_file_build_default_label(vd_t *vd, struct dk_label *label) 57878fcd0a1Sachartre { 57978fcd0a1Sachartre size_t size; 58078fcd0a1Sachartre char prefix; 581047ba61eSachartre int slice, nparts; 582047ba61eSachartre uint16_t tag; 58378fcd0a1Sachartre 58478fcd0a1Sachartre ASSERT(vd->file); 58578fcd0a1Sachartre 58678fcd0a1Sachartre /* 58778fcd0a1Sachartre * We must have a resonable number of cylinders and sectors so 58878fcd0a1Sachartre * that newfs can run using default values. 58978fcd0a1Sachartre * 59078fcd0a1Sachartre * if (disk_size < 2MB) 59178fcd0a1Sachartre * phys_cylinders = disk_size / 100K 59278fcd0a1Sachartre * else 59378fcd0a1Sachartre * phys_cylinders = disk_size / 300K 59478fcd0a1Sachartre * 59578fcd0a1Sachartre * phys_cylinders = (phys_cylinders == 0) ? 1 : phys_cylinders 59678fcd0a1Sachartre * alt_cylinders = (phys_cylinders > 2) ? 2 : 0; 59778fcd0a1Sachartre * data_cylinders = phys_cylinders - alt_cylinders 59878fcd0a1Sachartre * 59978fcd0a1Sachartre * sectors = disk_size / (phys_cylinders * blk_size) 60078fcd0a1Sachartre * 60178fcd0a1Sachartre * The file size test is an attempt to not have too few cylinders 60278fcd0a1Sachartre * for a small file, or so many on a big file that you waste space 60378fcd0a1Sachartre * for backup superblocks or cylinder group structures. 60478fcd0a1Sachartre */ 60578fcd0a1Sachartre if (vd->file_size < (2 * 1024 * 1024)) 60678fcd0a1Sachartre label->dkl_pcyl = vd->file_size / (100 * 1024); 60778fcd0a1Sachartre else 60878fcd0a1Sachartre label->dkl_pcyl = vd->file_size / (300 * 1024); 60978fcd0a1Sachartre 61078fcd0a1Sachartre if (label->dkl_pcyl == 0) 61178fcd0a1Sachartre label->dkl_pcyl = 1; 61278fcd0a1Sachartre 613047ba61eSachartre label->dkl_acyl = 0; 614047ba61eSachartre 615047ba61eSachartre if (vd->vdisk_type == VD_DISK_TYPE_SLICE) { 616047ba61eSachartre nparts = 1; 617047ba61eSachartre slice = 0; 618047ba61eSachartre tag = V_UNASSIGNED; 619047ba61eSachartre } else { 62078fcd0a1Sachartre if (label->dkl_pcyl > 2) 62178fcd0a1Sachartre label->dkl_acyl = 2; 622047ba61eSachartre nparts = V_NUMPAR; 623047ba61eSachartre slice = VD_ENTIRE_DISK_SLICE; 624047ba61eSachartre tag = V_BACKUP; 625047ba61eSachartre } 62678fcd0a1Sachartre 62778fcd0a1Sachartre label->dkl_nsect = vd->file_size / 62878fcd0a1Sachartre (DEV_BSIZE * label->dkl_pcyl); 62978fcd0a1Sachartre label->dkl_ncyl = label->dkl_pcyl - label->dkl_acyl; 63078fcd0a1Sachartre label->dkl_nhead = 1; 63178fcd0a1Sachartre label->dkl_write_reinstruct = 0; 63278fcd0a1Sachartre label->dkl_read_reinstruct = 0; 63378fcd0a1Sachartre label->dkl_rpm = 7200; 63478fcd0a1Sachartre label->dkl_apc = 0; 63578fcd0a1Sachartre label->dkl_intrlv = 0; 63678fcd0a1Sachartre 63778fcd0a1Sachartre PR0("requested disk size: %ld bytes\n", vd->file_size); 63878fcd0a1Sachartre PR0("setup: ncyl=%d nhead=%d nsec=%d\n", label->dkl_pcyl, 63978fcd0a1Sachartre label->dkl_nhead, label->dkl_nsect); 64078fcd0a1Sachartre PR0("provided disk size: %ld bytes\n", (uint64_t) 64178fcd0a1Sachartre (label->dkl_pcyl * label->dkl_nhead * 64278fcd0a1Sachartre label->dkl_nsect * DEV_BSIZE)); 64378fcd0a1Sachartre 64478fcd0a1Sachartre if (vd->file_size < (1ULL << 20)) { 64578fcd0a1Sachartre size = vd->file_size >> 10; 64678fcd0a1Sachartre prefix = 'K'; /* Kilobyte */ 64778fcd0a1Sachartre } else if (vd->file_size < (1ULL << 30)) { 64878fcd0a1Sachartre size = vd->file_size >> 20; 64978fcd0a1Sachartre prefix = 'M'; /* Megabyte */ 65078fcd0a1Sachartre } else if (vd->file_size < (1ULL << 40)) { 65178fcd0a1Sachartre size = vd->file_size >> 30; 65278fcd0a1Sachartre prefix = 'G'; /* Gigabyte */ 65378fcd0a1Sachartre } else { 65478fcd0a1Sachartre size = vd->file_size >> 40; 65578fcd0a1Sachartre prefix = 'T'; /* Terabyte */ 65678fcd0a1Sachartre } 65778fcd0a1Sachartre 65878fcd0a1Sachartre /* 65978fcd0a1Sachartre * We must have a correct label name otherwise format(1m) will 66078fcd0a1Sachartre * not recognized the disk as labeled. 66178fcd0a1Sachartre */ 66278fcd0a1Sachartre (void) snprintf(label->dkl_asciilabel, LEN_DKL_ASCII, 66378fcd0a1Sachartre "SUN-DiskImage-%ld%cB cyl %d alt %d hd %d sec %d", 66478fcd0a1Sachartre size, prefix, 66578fcd0a1Sachartre label->dkl_ncyl, label->dkl_acyl, label->dkl_nhead, 66678fcd0a1Sachartre label->dkl_nsect); 66778fcd0a1Sachartre 66878fcd0a1Sachartre /* default VTOC */ 66978fcd0a1Sachartre label->dkl_vtoc.v_version = V_VERSION; 670047ba61eSachartre label->dkl_vtoc.v_nparts = nparts; 67178fcd0a1Sachartre label->dkl_vtoc.v_sanity = VTOC_SANE; 672047ba61eSachartre label->dkl_vtoc.v_part[slice].p_tag = tag; 673047ba61eSachartre label->dkl_map[slice].dkl_cylno = 0; 674047ba61eSachartre label->dkl_map[slice].dkl_nblk = label->dkl_ncyl * 67578fcd0a1Sachartre label->dkl_nhead * label->dkl_nsect; 67678fcd0a1Sachartre label->dkl_cksum = vd_lbl2cksum(label); 67778fcd0a1Sachartre } 67878fcd0a1Sachartre 67978fcd0a1Sachartre /* 68078fcd0a1Sachartre * Function: 68187a7269eSachartre * vd_file_set_vtoc 68287a7269eSachartre * 68387a7269eSachartre * Description: 68487a7269eSachartre * Set the vtoc of a disk image by writing the label and backup 68587a7269eSachartre * labels into the disk image backend. 68687a7269eSachartre * 68787a7269eSachartre * Parameters: 68887a7269eSachartre * vd - disk on which the operation is performed. 68987a7269eSachartre * label - the data to be written. 69087a7269eSachartre * 69187a7269eSachartre * Return Code: 69287a7269eSachartre * 0 - success. 69387a7269eSachartre * n > 0 - error, n indicates the errno code. 69487a7269eSachartre */ 69587a7269eSachartre static int 69687a7269eSachartre vd_file_set_vtoc(vd_t *vd, struct dk_label *label) 69787a7269eSachartre { 69887a7269eSachartre int blk, sec, cyl, head, cnt; 69987a7269eSachartre 70087a7269eSachartre ASSERT(vd->file); 70187a7269eSachartre 70287a7269eSachartre if (VD_FILE_LABEL_WRITE(vd, label) < 0) { 70387a7269eSachartre PR0("fail to write disk label"); 70487a7269eSachartre return (EIO); 70587a7269eSachartre } 70687a7269eSachartre 70787a7269eSachartre /* 70887a7269eSachartre * Backup labels are on the last alternate cylinder's 70987a7269eSachartre * first five odd sectors. 71087a7269eSachartre */ 71187a7269eSachartre if (label->dkl_acyl == 0) { 71287a7269eSachartre PR0("no alternate cylinder, can not store backup labels"); 71387a7269eSachartre return (0); 71487a7269eSachartre } 71587a7269eSachartre 71687a7269eSachartre cyl = label->dkl_ncyl + label->dkl_acyl - 1; 71787a7269eSachartre head = label->dkl_nhead - 1; 71887a7269eSachartre 71987a7269eSachartre blk = (cyl * ((label->dkl_nhead * label->dkl_nsect) - label->dkl_apc)) + 72087a7269eSachartre (head * label->dkl_nsect); 72187a7269eSachartre 72287a7269eSachartre /* 72387a7269eSachartre * Write the backup labels. Make sure we don't try to write past 72487a7269eSachartre * the last cylinder. 72587a7269eSachartre */ 72687a7269eSachartre sec = 1; 72787a7269eSachartre 72887a7269eSachartre for (cnt = 0; cnt < VD_FILE_NUM_BACKUP; cnt++) { 72987a7269eSachartre 73087a7269eSachartre if (sec >= label->dkl_nsect) { 73187a7269eSachartre PR0("not enough sector to store all backup labels"); 73287a7269eSachartre return (0); 73387a7269eSachartre } 73487a7269eSachartre 73587a7269eSachartre if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)label, 73687a7269eSachartre blk + sec, sizeof (struct dk_label)) < 0) { 73787a7269eSachartre PR0("error writing backup label at block %d\n", 73887a7269eSachartre blk + sec); 73987a7269eSachartre return (EIO); 74087a7269eSachartre } 74187a7269eSachartre 74287a7269eSachartre PR1("wrote backup label at block %d\n", blk + sec); 74387a7269eSachartre 74487a7269eSachartre sec += 2; 74587a7269eSachartre } 74687a7269eSachartre 74787a7269eSachartre return (0); 74887a7269eSachartre } 74987a7269eSachartre 75087a7269eSachartre /* 75187a7269eSachartre * Function: 75287a7269eSachartre * vd_file_get_devid_block 75387a7269eSachartre * 75487a7269eSachartre * Description: 75587a7269eSachartre * Return the block number where the device id is stored. 75687a7269eSachartre * 75787a7269eSachartre * Parameters: 75887a7269eSachartre * vd - disk on which the operation is performed. 75987a7269eSachartre * blkp - pointer to the block number 76087a7269eSachartre * 76187a7269eSachartre * Return Code: 76287a7269eSachartre * 0 - success 76387a7269eSachartre * ENOSPC - disk has no space to store a device id 76487a7269eSachartre */ 76587a7269eSachartre static int 76687a7269eSachartre vd_file_get_devid_block(vd_t *vd, size_t *blkp) 76787a7269eSachartre { 76887a7269eSachartre diskaddr_t spc, head, cyl; 76987a7269eSachartre 77087a7269eSachartre ASSERT(vd->file); 77187a7269eSachartre ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC); 77287a7269eSachartre 77387a7269eSachartre /* this geometry doesn't allow us to have a devid */ 77487a7269eSachartre if (vd->dk_geom.dkg_acyl < 2) { 77587a7269eSachartre PR0("not enough alternate cylinder available for devid " 77687a7269eSachartre "(acyl=%u)", vd->dk_geom.dkg_acyl); 77787a7269eSachartre return (ENOSPC); 77887a7269eSachartre } 77987a7269eSachartre 78087a7269eSachartre /* the devid is in on the track next to the last cylinder */ 78187a7269eSachartre cyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl - 2; 78287a7269eSachartre spc = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect; 78387a7269eSachartre head = vd->dk_geom.dkg_nhead - 1; 78487a7269eSachartre 78587a7269eSachartre *blkp = (cyl * (spc - vd->dk_geom.dkg_apc)) + 78687a7269eSachartre (head * vd->dk_geom.dkg_nsect) + 1; 78787a7269eSachartre 78887a7269eSachartre return (0); 78987a7269eSachartre } 79087a7269eSachartre 79187a7269eSachartre /* 79287a7269eSachartre * Return the checksum of a disk block containing an on-disk devid. 79387a7269eSachartre */ 79487a7269eSachartre static uint_t 79587a7269eSachartre vd_dkdevid2cksum(struct dk_devid *dkdevid) 79687a7269eSachartre { 79787a7269eSachartre uint_t chksum, *ip; 79887a7269eSachartre int i; 79987a7269eSachartre 80087a7269eSachartre chksum = 0; 80187a7269eSachartre ip = (uint_t *)dkdevid; 80287a7269eSachartre for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int)); i++) 80387a7269eSachartre chksum ^= ip[i]; 80487a7269eSachartre 80587a7269eSachartre return (chksum); 80687a7269eSachartre } 80787a7269eSachartre 80887a7269eSachartre /* 80987a7269eSachartre * Function: 81087a7269eSachartre * vd_file_read_devid 81187a7269eSachartre * 81287a7269eSachartre * Description: 81387a7269eSachartre * Read the device id stored on a disk image. 81487a7269eSachartre * 81587a7269eSachartre * Parameters: 81687a7269eSachartre * vd - disk on which the operation is performed. 81787a7269eSachartre * devid - the return address of the device ID. 81887a7269eSachartre * 81987a7269eSachartre * Return Code: 82087a7269eSachartre * 0 - success 82187a7269eSachartre * EIO - I/O error while trying to access the disk image 82287a7269eSachartre * EINVAL - no valid device id was found 82387a7269eSachartre * ENOSPC - disk has no space to store a device id 82487a7269eSachartre */ 82587a7269eSachartre static int 82687a7269eSachartre vd_file_read_devid(vd_t *vd, ddi_devid_t *devid) 82787a7269eSachartre { 82887a7269eSachartre struct dk_devid *dkdevid; 82987a7269eSachartre size_t blk; 83087a7269eSachartre uint_t chksum; 83187a7269eSachartre int status, sz; 83287a7269eSachartre 83387a7269eSachartre if ((status = vd_file_get_devid_block(vd, &blk)) != 0) 83487a7269eSachartre return (status); 83587a7269eSachartre 83687a7269eSachartre dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP); 83787a7269eSachartre 83887a7269eSachartre /* get the devid */ 83987a7269eSachartre if ((vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)dkdevid, blk, 84087a7269eSachartre DEV_BSIZE)) < 0) { 84187a7269eSachartre PR0("error reading devid block at %lu", blk); 84287a7269eSachartre status = EIO; 84387a7269eSachartre goto done; 84487a7269eSachartre } 84587a7269eSachartre 84687a7269eSachartre /* validate the revision */ 84787a7269eSachartre if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) || 84887a7269eSachartre (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) { 84987a7269eSachartre PR0("invalid devid found at block %lu (bad revision)", blk); 85087a7269eSachartre status = EINVAL; 85187a7269eSachartre goto done; 85287a7269eSachartre } 85387a7269eSachartre 85487a7269eSachartre /* compute checksum */ 85587a7269eSachartre chksum = vd_dkdevid2cksum(dkdevid); 85687a7269eSachartre 85787a7269eSachartre /* compare the checksums */ 85887a7269eSachartre if (DKD_GETCHKSUM(dkdevid) != chksum) { 85987a7269eSachartre PR0("invalid devid found at block %lu (bad checksum)", blk); 86087a7269eSachartre status = EINVAL; 86187a7269eSachartre goto done; 86287a7269eSachartre } 86387a7269eSachartre 86487a7269eSachartre /* validate the device id */ 86587a7269eSachartre if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) { 86687a7269eSachartre PR0("invalid devid found at block %lu", blk); 86787a7269eSachartre status = EINVAL; 86887a7269eSachartre goto done; 86987a7269eSachartre } 87087a7269eSachartre 87187a7269eSachartre PR1("devid read at block %lu", blk); 87287a7269eSachartre 87387a7269eSachartre sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid); 87487a7269eSachartre *devid = kmem_alloc(sz, KM_SLEEP); 87587a7269eSachartre bcopy(&dkdevid->dkd_devid, *devid, sz); 87687a7269eSachartre 87787a7269eSachartre done: 87887a7269eSachartre kmem_free(dkdevid, DEV_BSIZE); 87987a7269eSachartre return (status); 88087a7269eSachartre 88187a7269eSachartre } 88287a7269eSachartre 88387a7269eSachartre /* 88487a7269eSachartre * Function: 88587a7269eSachartre * vd_file_write_devid 88687a7269eSachartre * 88787a7269eSachartre * Description: 88887a7269eSachartre * Write a device id into disk image. 88987a7269eSachartre * 89087a7269eSachartre * Parameters: 89187a7269eSachartre * vd - disk on which the operation is performed. 89287a7269eSachartre * devid - the device ID to store. 89387a7269eSachartre * 89487a7269eSachartre * Return Code: 89587a7269eSachartre * 0 - success 89687a7269eSachartre * EIO - I/O error while trying to access the disk image 89787a7269eSachartre * ENOSPC - disk has no space to store a device id 89887a7269eSachartre */ 89987a7269eSachartre static int 90087a7269eSachartre vd_file_write_devid(vd_t *vd, ddi_devid_t devid) 90187a7269eSachartre { 90287a7269eSachartre struct dk_devid *dkdevid; 90387a7269eSachartre uint_t chksum; 90487a7269eSachartre size_t blk; 90587a7269eSachartre int status; 90687a7269eSachartre 90787a7269eSachartre if ((status = vd_file_get_devid_block(vd, &blk)) != 0) 90887a7269eSachartre return (status); 90987a7269eSachartre 91087a7269eSachartre dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP); 91187a7269eSachartre 91287a7269eSachartre /* set revision */ 91387a7269eSachartre dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB; 91487a7269eSachartre dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB; 91587a7269eSachartre 91687a7269eSachartre /* copy devid */ 91787a7269eSachartre bcopy(devid, &dkdevid->dkd_devid, ddi_devid_sizeof(devid)); 91887a7269eSachartre 91987a7269eSachartre /* compute checksum */ 92087a7269eSachartre chksum = vd_dkdevid2cksum(dkdevid); 92187a7269eSachartre 92287a7269eSachartre /* set checksum */ 92387a7269eSachartre DKD_FORMCHKSUM(chksum, dkdevid); 92487a7269eSachartre 92587a7269eSachartre /* store the devid */ 92687a7269eSachartre if ((status = vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, 92787a7269eSachartre (caddr_t)dkdevid, blk, DEV_BSIZE)) < 0) { 92887a7269eSachartre PR0("Error writing devid block at %lu", blk); 92987a7269eSachartre status = EIO; 93087a7269eSachartre } else { 93187a7269eSachartre PR1("devid written at block %lu", blk); 93287a7269eSachartre status = 0; 93387a7269eSachartre } 93487a7269eSachartre 93587a7269eSachartre kmem_free(dkdevid, DEV_BSIZE); 93687a7269eSachartre return (status); 93787a7269eSachartre } 93887a7269eSachartre 93987a7269eSachartre /* 94087a7269eSachartre * Function: 94187a7269eSachartre * vd_scsi_rdwr 94287a7269eSachartre * 94387a7269eSachartre * Description: 94487a7269eSachartre * Read or write to a SCSI disk using an absolute disk offset. 94587a7269eSachartre * 94687a7269eSachartre * Parameters: 94787a7269eSachartre * vd - disk on which the operation is performed. 94887a7269eSachartre * operation - operation to execute: read (VD_OP_BREAD) or 94987a7269eSachartre * write (VD_OP_BWRITE). 95087a7269eSachartre * data - buffer where data are read to or written from. 95187a7269eSachartre * blk - starting block for the operation. 95287a7269eSachartre * len - number of bytes to read or write. 95387a7269eSachartre * 95487a7269eSachartre * Return Code: 95587a7269eSachartre * 0 - success 95687a7269eSachartre * n != 0 - error. 95787a7269eSachartre */ 95887a7269eSachartre static int 95987a7269eSachartre vd_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t blk, size_t len) 96087a7269eSachartre { 96187a7269eSachartre struct uscsi_cmd ucmd; 96287a7269eSachartre union scsi_cdb cdb; 96387a7269eSachartre int nsectors, nblk; 96487a7269eSachartre int max_sectors; 96587a7269eSachartre int status, rval; 96687a7269eSachartre 96787a7269eSachartre ASSERT(!vd->file); 96887a7269eSachartre 96987a7269eSachartre max_sectors = vd->max_xfer_sz; 97087a7269eSachartre nblk = (len / DEV_BSIZE); 97187a7269eSachartre 97287a7269eSachartre if (len % DEV_BSIZE != 0) 97387a7269eSachartre return (EINVAL); 97487a7269eSachartre 97587a7269eSachartre /* 97687a7269eSachartre * Build and execute the uscsi ioctl. We build a group0, group1 97787a7269eSachartre * or group4 command as necessary, since some targets 97887a7269eSachartre * do not support group1 commands. 97987a7269eSachartre */ 98087a7269eSachartre while (nblk) { 98187a7269eSachartre 98287a7269eSachartre bzero(&ucmd, sizeof (ucmd)); 98387a7269eSachartre bzero(&cdb, sizeof (cdb)); 98487a7269eSachartre 98587a7269eSachartre nsectors = (max_sectors < nblk) ? max_sectors : nblk; 98687a7269eSachartre 98787a7269eSachartre if (blk < (2 << 20) && nsectors <= 0xff) { 98887a7269eSachartre FORMG0ADDR(&cdb, blk); 98987a7269eSachartre FORMG0COUNT(&cdb, nsectors); 99087a7269eSachartre ucmd.uscsi_cdblen = CDB_GROUP0; 99187a7269eSachartre } else if (blk > 0xffffffff) { 99287a7269eSachartre FORMG4LONGADDR(&cdb, blk); 99387a7269eSachartre FORMG4COUNT(&cdb, nsectors); 99487a7269eSachartre ucmd.uscsi_cdblen = CDB_GROUP4; 99587a7269eSachartre cdb.scc_cmd |= SCMD_GROUP4; 99687a7269eSachartre } else { 99787a7269eSachartre FORMG1ADDR(&cdb, blk); 99887a7269eSachartre FORMG1COUNT(&cdb, nsectors); 99987a7269eSachartre ucmd.uscsi_cdblen = CDB_GROUP1; 100087a7269eSachartre cdb.scc_cmd |= SCMD_GROUP1; 100187a7269eSachartre } 100287a7269eSachartre 100387a7269eSachartre ucmd.uscsi_cdb = (caddr_t)&cdb; 100487a7269eSachartre ucmd.uscsi_bufaddr = data; 100587a7269eSachartre ucmd.uscsi_buflen = nsectors * DEV_BSIZE; 100687a7269eSachartre ucmd.uscsi_timeout = vd_scsi_rdwr_timeout; 100787a7269eSachartre /* 100887a7269eSachartre * Set flags so that the command is isolated from normal 100987a7269eSachartre * commands and no error message is printed. 101087a7269eSachartre */ 101187a7269eSachartre ucmd.uscsi_flags = USCSI_ISOLATE | USCSI_SILENT; 101287a7269eSachartre 101387a7269eSachartre if (operation == VD_OP_BREAD) { 101487a7269eSachartre cdb.scc_cmd |= SCMD_READ; 101587a7269eSachartre ucmd.uscsi_flags |= USCSI_READ; 101687a7269eSachartre } else { 101787a7269eSachartre cdb.scc_cmd |= SCMD_WRITE; 101887a7269eSachartre } 101987a7269eSachartre 102087a7269eSachartre status = ldi_ioctl(vd->ldi_handle[VD_ENTIRE_DISK_SLICE], 1021047ba61eSachartre USCSICMD, (intptr_t)&ucmd, (vd->open_flags | FKIOCTL), 102287a7269eSachartre kcred, &rval); 102387a7269eSachartre 102487a7269eSachartre if (status == 0) 102587a7269eSachartre status = ucmd.uscsi_status; 102687a7269eSachartre 102787a7269eSachartre if (status != 0) 102887a7269eSachartre break; 102987a7269eSachartre 103087a7269eSachartre /* 103187a7269eSachartre * Check if partial DMA breakup is required. If so, reduce 103287a7269eSachartre * the request size by half and retry the last request. 103387a7269eSachartre */ 103487a7269eSachartre if (ucmd.uscsi_resid == ucmd.uscsi_buflen) { 103587a7269eSachartre max_sectors >>= 1; 103687a7269eSachartre if (max_sectors <= 0) { 103787a7269eSachartre status = EIO; 103887a7269eSachartre break; 103987a7269eSachartre } 104087a7269eSachartre continue; 104187a7269eSachartre } 104287a7269eSachartre 104387a7269eSachartre if (ucmd.uscsi_resid != 0) { 104487a7269eSachartre status = EIO; 104587a7269eSachartre break; 104687a7269eSachartre } 104787a7269eSachartre 104887a7269eSachartre blk += nsectors; 104987a7269eSachartre nblk -= nsectors; 105087a7269eSachartre data += nsectors * DEV_BSIZE; /* SECSIZE */ 105187a7269eSachartre } 105287a7269eSachartre 105387a7269eSachartre return (status); 105487a7269eSachartre } 105587a7269eSachartre 1056205eeb1aSlm66018 /* 1057205eeb1aSlm66018 * Return Values 1058205eeb1aSlm66018 * EINPROGRESS - operation was successfully started 1059205eeb1aSlm66018 * EIO - encountered LDC (aka. task error) 1060205eeb1aSlm66018 * 0 - operation completed successfully 1061205eeb1aSlm66018 * 1062205eeb1aSlm66018 * Side Effect 1063205eeb1aSlm66018 * sets request->status = <disk operation status> 1064205eeb1aSlm66018 */ 10651ae08745Sheppo static int 1066d10e4ef2Snarayan vd_start_bio(vd_task_t *task) 10671ae08745Sheppo { 10684bac2208Snarayan int rv, status = 0; 1069d10e4ef2Snarayan vd_t *vd = task->vd; 1070d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 1071d10e4ef2Snarayan struct buf *buf = &task->buf; 10724bac2208Snarayan uint8_t mtype; 10733c96341aSnarayan int slice; 1074047ba61eSachartre char *bufaddr = 0; 1075047ba61eSachartre size_t buflen; 1076d10e4ef2Snarayan 1077d10e4ef2Snarayan ASSERT(vd != NULL); 1078d10e4ef2Snarayan ASSERT(request != NULL); 10793c96341aSnarayan 10803c96341aSnarayan slice = request->slice; 10813c96341aSnarayan 108287a7269eSachartre ASSERT(slice == VD_SLICE_NONE || slice < vd->nslices); 1083d10e4ef2Snarayan ASSERT((request->operation == VD_OP_BREAD) || 1084d10e4ef2Snarayan (request->operation == VD_OP_BWRITE)); 1085d10e4ef2Snarayan 1086205eeb1aSlm66018 if (request->nbytes == 0) { 1087205eeb1aSlm66018 /* no service for trivial requests */ 1088205eeb1aSlm66018 request->status = EINVAL; 1089205eeb1aSlm66018 return (0); 1090205eeb1aSlm66018 } 10911ae08745Sheppo 1092d10e4ef2Snarayan PR1("%s %lu bytes at block %lu", 1093d10e4ef2Snarayan (request->operation == VD_OP_BREAD) ? "Read" : "Write", 1094d10e4ef2Snarayan request->nbytes, request->addr); 10951ae08745Sheppo 1096047ba61eSachartre /* 1097047ba61eSachartre * We have to check the open flags because the functions processing 1098047ba61eSachartre * the read/write request will not do it. 1099047ba61eSachartre */ 1100047ba61eSachartre if (request->operation == VD_OP_BWRITE && !(vd->open_flags & FWRITE)) { 1101047ba61eSachartre PR0("write fails because backend is opened read-only"); 1102047ba61eSachartre request->nbytes = 0; 1103047ba61eSachartre request->status = EROFS; 1104047ba61eSachartre return (0); 1105047ba61eSachartre } 1106d10e4ef2Snarayan 11074bac2208Snarayan mtype = (&vd->inband_task == task) ? LDC_SHADOW_MAP : LDC_DIRECT_MAP; 11084bac2208Snarayan 11094bac2208Snarayan /* Map memory exported by client */ 11104bac2208Snarayan status = ldc_mem_map(task->mhdl, request->cookie, request->ncookies, 11114bac2208Snarayan mtype, (request->operation == VD_OP_BREAD) ? LDC_MEM_W : LDC_MEM_R, 1112047ba61eSachartre &bufaddr, NULL); 11134bac2208Snarayan if (status != 0) { 11143af08d82Slm66018 PR0("ldc_mem_map() returned err %d ", status); 1115205eeb1aSlm66018 return (EIO); 1116d10e4ef2Snarayan } 1117d10e4ef2Snarayan 1118047ba61eSachartre buflen = request->nbytes; 1119047ba61eSachartre 1120047ba61eSachartre status = ldc_mem_acquire(task->mhdl, 0, buflen); 11214bac2208Snarayan if (status != 0) { 11224bac2208Snarayan (void) ldc_mem_unmap(task->mhdl); 11233af08d82Slm66018 PR0("ldc_mem_acquire() returned err %d ", status); 1124205eeb1aSlm66018 return (EIO); 11254bac2208Snarayan } 11264bac2208Snarayan 1127d10e4ef2Snarayan /* Start the block I/O */ 11283c96341aSnarayan if (vd->file) { 1129047ba61eSachartre rv = vd_file_rw(vd, slice, request->operation, bufaddr, 1130690555a1Sachartre request->addr, request->nbytes); 1131690555a1Sachartre if (rv < 0) { 11323c96341aSnarayan request->nbytes = 0; 1133205eeb1aSlm66018 request->status = EIO; 1134690555a1Sachartre } else { 1135690555a1Sachartre request->nbytes = rv; 1136205eeb1aSlm66018 request->status = 0; 11373c96341aSnarayan } 11383c96341aSnarayan } else { 113987a7269eSachartre if (slice == VD_SLICE_NONE) { 114087a7269eSachartre /* 114187a7269eSachartre * This is not a disk image so it is a real disk. We 114287a7269eSachartre * assume that the underlying device driver supports 114387a7269eSachartre * USCSICMD ioctls. This is the case of all SCSI devices 114487a7269eSachartre * (sd, ssd...). 114587a7269eSachartre * 114687a7269eSachartre * In the future if we have non-SCSI disks we would need 114787a7269eSachartre * to invoke the appropriate function to do I/O using an 114887a7269eSachartre * absolute disk offset (for example using DKIOCTL_RWCMD 114987a7269eSachartre * for IDE disks). 115087a7269eSachartre */ 1151047ba61eSachartre rv = vd_scsi_rdwr(vd, request->operation, bufaddr, 1152047ba61eSachartre request->addr, request->nbytes); 115387a7269eSachartre if (rv != 0) { 115487a7269eSachartre request->nbytes = 0; 1155205eeb1aSlm66018 request->status = EIO; 115687a7269eSachartre } else { 1157205eeb1aSlm66018 request->status = 0; 115887a7269eSachartre } 115987a7269eSachartre } else { 1160047ba61eSachartre bioinit(buf); 1161047ba61eSachartre buf->b_flags = B_BUSY; 1162047ba61eSachartre buf->b_bcount = request->nbytes; 1163047ba61eSachartre buf->b_lblkno = request->addr; 1164047ba61eSachartre buf->b_edev = vd->dev[slice]; 1165047ba61eSachartre buf->b_un.b_addr = bufaddr; 1166047ba61eSachartre buf->b_flags |= (request->operation == VD_OP_BREAD)? 1167047ba61eSachartre B_READ : B_WRITE; 1168047ba61eSachartre 1169205eeb1aSlm66018 request->status = 1170205eeb1aSlm66018 ldi_strategy(vd->ldi_handle[slice], buf); 1171205eeb1aSlm66018 1172205eeb1aSlm66018 /* 1173205eeb1aSlm66018 * This is to indicate to the caller that the request 1174205eeb1aSlm66018 * needs to be finished by vd_complete_bio() by calling 1175205eeb1aSlm66018 * biowait() there and waiting for that to return before 1176205eeb1aSlm66018 * triggering the notification of the vDisk client. 1177205eeb1aSlm66018 * 1178205eeb1aSlm66018 * This is necessary when writing to real disks as 1179205eeb1aSlm66018 * otherwise calls to ldi_strategy() would be serialized 1180205eeb1aSlm66018 * behind the calls to biowait() and performance would 1181205eeb1aSlm66018 * suffer. 1182205eeb1aSlm66018 */ 1183205eeb1aSlm66018 if (request->status == 0) 118487a7269eSachartre return (EINPROGRESS); 1185047ba61eSachartre 1186047ba61eSachartre biofini(buf); 118787a7269eSachartre } 11883c96341aSnarayan } 11893c96341aSnarayan 1190d10e4ef2Snarayan /* Clean up after error */ 1191047ba61eSachartre rv = ldc_mem_release(task->mhdl, 0, buflen); 11924bac2208Snarayan if (rv) { 11933af08d82Slm66018 PR0("ldc_mem_release() returned err %d ", rv); 1194205eeb1aSlm66018 status = EIO; 11954bac2208Snarayan } 11964bac2208Snarayan rv = ldc_mem_unmap(task->mhdl); 11974bac2208Snarayan if (rv) { 1198205eeb1aSlm66018 PR0("ldc_mem_unmap() returned err %d ", rv); 1199205eeb1aSlm66018 status = EIO; 12004bac2208Snarayan } 12014bac2208Snarayan 1202d10e4ef2Snarayan return (status); 1203d10e4ef2Snarayan } 1204d10e4ef2Snarayan 1205205eeb1aSlm66018 /* 1206205eeb1aSlm66018 * This function should only be called from vd_notify to ensure that requests 1207205eeb1aSlm66018 * are responded to in the order that they are received. 1208205eeb1aSlm66018 */ 1209d10e4ef2Snarayan static int 1210d10e4ef2Snarayan send_msg(ldc_handle_t ldc_handle, void *msg, size_t msglen) 1211d10e4ef2Snarayan { 12123af08d82Slm66018 int status; 1213d10e4ef2Snarayan size_t nbytes; 1214d10e4ef2Snarayan 12153af08d82Slm66018 do { 1216d10e4ef2Snarayan nbytes = msglen; 1217d10e4ef2Snarayan status = ldc_write(ldc_handle, msg, &nbytes); 12183af08d82Slm66018 if (status != EWOULDBLOCK) 12193af08d82Slm66018 break; 12203af08d82Slm66018 drv_usecwait(vds_ldc_delay); 12213af08d82Slm66018 } while (status == EWOULDBLOCK); 1222d10e4ef2Snarayan 1223d10e4ef2Snarayan if (status != 0) { 12243af08d82Slm66018 if (status != ECONNRESET) 12253af08d82Slm66018 PR0("ldc_write() returned errno %d", status); 1226d10e4ef2Snarayan return (status); 1227d10e4ef2Snarayan } else if (nbytes != msglen) { 12283af08d82Slm66018 PR0("ldc_write() performed only partial write"); 1229d10e4ef2Snarayan return (EIO); 1230d10e4ef2Snarayan } 1231d10e4ef2Snarayan 1232d10e4ef2Snarayan PR1("SENT %lu bytes", msglen); 1233d10e4ef2Snarayan return (0); 1234d10e4ef2Snarayan } 1235d10e4ef2Snarayan 1236d10e4ef2Snarayan static void 1237d10e4ef2Snarayan vd_need_reset(vd_t *vd, boolean_t reset_ldc) 1238d10e4ef2Snarayan { 1239d10e4ef2Snarayan mutex_enter(&vd->lock); 1240d10e4ef2Snarayan vd->reset_state = B_TRUE; 1241d10e4ef2Snarayan vd->reset_ldc = reset_ldc; 1242d10e4ef2Snarayan mutex_exit(&vd->lock); 1243d10e4ef2Snarayan } 1244d10e4ef2Snarayan 1245d10e4ef2Snarayan /* 1246d10e4ef2Snarayan * Reset the state of the connection with a client, if needed; reset the LDC 1247d10e4ef2Snarayan * transport as well, if needed. This function should only be called from the 12483af08d82Slm66018 * "vd_recv_msg", as it waits for tasks - otherwise a deadlock can occur. 1249d10e4ef2Snarayan */ 1250d10e4ef2Snarayan static void 1251d10e4ef2Snarayan vd_reset_if_needed(vd_t *vd) 1252d10e4ef2Snarayan { 1253d10e4ef2Snarayan int status = 0; 1254d10e4ef2Snarayan 1255d10e4ef2Snarayan mutex_enter(&vd->lock); 1256d10e4ef2Snarayan if (!vd->reset_state) { 1257d10e4ef2Snarayan ASSERT(!vd->reset_ldc); 1258d10e4ef2Snarayan mutex_exit(&vd->lock); 1259d10e4ef2Snarayan return; 1260d10e4ef2Snarayan } 1261d10e4ef2Snarayan mutex_exit(&vd->lock); 1262d10e4ef2Snarayan 1263d10e4ef2Snarayan PR0("Resetting connection state with %s", VD_CLIENT(vd)); 1264d10e4ef2Snarayan 1265d10e4ef2Snarayan /* 1266d10e4ef2Snarayan * Let any asynchronous I/O complete before possibly pulling the rug 1267d10e4ef2Snarayan * out from under it; defer checking vd->reset_ldc, as one of the 1268d10e4ef2Snarayan * asynchronous tasks might set it 1269d10e4ef2Snarayan */ 1270d10e4ef2Snarayan ddi_taskq_wait(vd->completionq); 1271d10e4ef2Snarayan 12723c96341aSnarayan if (vd->file) { 1273*da6c28aaSamw status = VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL); 12743c96341aSnarayan if (status) { 12753c96341aSnarayan PR0("VOP_FSYNC returned errno %d", status); 12763c96341aSnarayan } 12773c96341aSnarayan } 12783c96341aSnarayan 1279d10e4ef2Snarayan if ((vd->initialized & VD_DRING) && 1280d10e4ef2Snarayan ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0)) 12813af08d82Slm66018 PR0("ldc_mem_dring_unmap() returned errno %d", status); 1282d10e4ef2Snarayan 12833af08d82Slm66018 vd_free_dring_task(vd); 12843af08d82Slm66018 12853af08d82Slm66018 /* Free the staging buffer for msgs */ 12863af08d82Slm66018 if (vd->vio_msgp != NULL) { 12873af08d82Slm66018 kmem_free(vd->vio_msgp, vd->max_msglen); 12883af08d82Slm66018 vd->vio_msgp = NULL; 1289d10e4ef2Snarayan } 1290d10e4ef2Snarayan 12913af08d82Slm66018 /* Free the inband message buffer */ 12923af08d82Slm66018 if (vd->inband_task.msg != NULL) { 12933af08d82Slm66018 kmem_free(vd->inband_task.msg, vd->max_msglen); 12943af08d82Slm66018 vd->inband_task.msg = NULL; 12953af08d82Slm66018 } 1296d10e4ef2Snarayan 1297d10e4ef2Snarayan mutex_enter(&vd->lock); 12983af08d82Slm66018 12993af08d82Slm66018 if (vd->reset_ldc) 13003af08d82Slm66018 PR0("taking down LDC channel"); 1301e1ebb9ecSlm66018 if (vd->reset_ldc && ((status = ldc_down(vd->ldc_handle)) != 0)) 13023af08d82Slm66018 PR0("ldc_down() returned errno %d", status); 1303d10e4ef2Snarayan 1304d10e4ef2Snarayan vd->initialized &= ~(VD_SID | VD_SEQ_NUM | VD_DRING); 1305d10e4ef2Snarayan vd->state = VD_STATE_INIT; 1306d10e4ef2Snarayan vd->max_msglen = sizeof (vio_msg_t); /* baseline vio message size */ 1307d10e4ef2Snarayan 13083af08d82Slm66018 /* Allocate the staging buffer */ 13093af08d82Slm66018 vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP); 13103af08d82Slm66018 13113af08d82Slm66018 PR0("calling ldc_up\n"); 13123af08d82Slm66018 (void) ldc_up(vd->ldc_handle); 13133af08d82Slm66018 1314d10e4ef2Snarayan vd->reset_state = B_FALSE; 1315d10e4ef2Snarayan vd->reset_ldc = B_FALSE; 13163af08d82Slm66018 1317d10e4ef2Snarayan mutex_exit(&vd->lock); 1318d10e4ef2Snarayan } 1319d10e4ef2Snarayan 13203af08d82Slm66018 static void vd_recv_msg(void *arg); 13213af08d82Slm66018 13223af08d82Slm66018 static void 13233af08d82Slm66018 vd_mark_in_reset(vd_t *vd) 13243af08d82Slm66018 { 13253af08d82Slm66018 int status; 13263af08d82Slm66018 13273af08d82Slm66018 PR0("vd_mark_in_reset: marking vd in reset\n"); 13283af08d82Slm66018 13293af08d82Slm66018 vd_need_reset(vd, B_FALSE); 13303af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, DDI_SLEEP); 13313af08d82Slm66018 if (status == DDI_FAILURE) { 13323af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 13333af08d82Slm66018 vd_need_reset(vd, B_TRUE); 13343af08d82Slm66018 return; 13353af08d82Slm66018 } 13363af08d82Slm66018 } 13373af08d82Slm66018 1338d10e4ef2Snarayan static int 13393c96341aSnarayan vd_mark_elem_done(vd_t *vd, int idx, int elem_status, int elem_nbytes) 1340d10e4ef2Snarayan { 1341d10e4ef2Snarayan boolean_t accepted; 1342d10e4ef2Snarayan int status; 1343d10e4ef2Snarayan vd_dring_entry_t *elem = VD_DRING_ELEM(idx); 1344d10e4ef2Snarayan 13453af08d82Slm66018 if (vd->reset_state) 13463af08d82Slm66018 return (0); 1347d10e4ef2Snarayan 1348d10e4ef2Snarayan /* Acquire the element */ 13493af08d82Slm66018 if (!vd->reset_state && 13503af08d82Slm66018 (status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) { 13513af08d82Slm66018 if (status == ECONNRESET) { 13523af08d82Slm66018 vd_mark_in_reset(vd); 13533af08d82Slm66018 return (0); 13543af08d82Slm66018 } else { 13553af08d82Slm66018 PR0("ldc_mem_dring_acquire() returned errno %d", 13563af08d82Slm66018 status); 1357d10e4ef2Snarayan return (status); 1358d10e4ef2Snarayan } 13593af08d82Slm66018 } 1360d10e4ef2Snarayan 1361d10e4ef2Snarayan /* Set the element's status and mark it done */ 1362d10e4ef2Snarayan accepted = (elem->hdr.dstate == VIO_DESC_ACCEPTED); 1363d10e4ef2Snarayan if (accepted) { 13643c96341aSnarayan elem->payload.nbytes = elem_nbytes; 1365d10e4ef2Snarayan elem->payload.status = elem_status; 1366d10e4ef2Snarayan elem->hdr.dstate = VIO_DESC_DONE; 1367d10e4ef2Snarayan } else { 1368d10e4ef2Snarayan /* Perhaps client timed out waiting for I/O... */ 13693af08d82Slm66018 PR0("element %u no longer \"accepted\"", idx); 1370d10e4ef2Snarayan VD_DUMP_DRING_ELEM(elem); 1371d10e4ef2Snarayan } 1372d10e4ef2Snarayan /* Release the element */ 13733af08d82Slm66018 if (!vd->reset_state && 13743af08d82Slm66018 (status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) { 13753af08d82Slm66018 if (status == ECONNRESET) { 13763af08d82Slm66018 vd_mark_in_reset(vd); 13773af08d82Slm66018 return (0); 13783af08d82Slm66018 } else { 13793af08d82Slm66018 PR0("ldc_mem_dring_release() returned errno %d", 13803af08d82Slm66018 status); 1381d10e4ef2Snarayan return (status); 1382d10e4ef2Snarayan } 13833af08d82Slm66018 } 1384d10e4ef2Snarayan 1385d10e4ef2Snarayan return (accepted ? 0 : EINVAL); 1386d10e4ef2Snarayan } 1387d10e4ef2Snarayan 1388205eeb1aSlm66018 /* 1389205eeb1aSlm66018 * Return Values 1390205eeb1aSlm66018 * 0 - operation completed successfully 1391205eeb1aSlm66018 * EIO - encountered LDC / task error 1392205eeb1aSlm66018 * 1393205eeb1aSlm66018 * Side Effect 1394205eeb1aSlm66018 * sets request->status = <disk operation status> 1395205eeb1aSlm66018 */ 1396205eeb1aSlm66018 static int 1397205eeb1aSlm66018 vd_complete_bio(vd_task_t *task) 1398d10e4ef2Snarayan { 1399d10e4ef2Snarayan int status = 0; 1400205eeb1aSlm66018 int rv = 0; 1401d10e4ef2Snarayan vd_t *vd = task->vd; 1402d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 1403d10e4ef2Snarayan struct buf *buf = &task->buf; 1404d10e4ef2Snarayan 1405d10e4ef2Snarayan 1406d10e4ef2Snarayan ASSERT(vd != NULL); 1407d10e4ef2Snarayan ASSERT(request != NULL); 1408d10e4ef2Snarayan ASSERT(task->msg != NULL); 1409d10e4ef2Snarayan ASSERT(task->msglen >= sizeof (*task->msg)); 14103c96341aSnarayan ASSERT(!vd->file); 1411205eeb1aSlm66018 ASSERT(request->slice != VD_SLICE_NONE); 1412d10e4ef2Snarayan 1413205eeb1aSlm66018 /* Wait for the I/O to complete [ call to ldi_strategy(9f) ] */ 1414d10e4ef2Snarayan request->status = biowait(buf); 1415d10e4ef2Snarayan 14163c96341aSnarayan /* return back the number of bytes read/written */ 14173c96341aSnarayan request->nbytes = buf->b_bcount - buf->b_resid; 14183c96341aSnarayan 14194bac2208Snarayan /* Release the buffer */ 14203af08d82Slm66018 if (!vd->reset_state) 14214bac2208Snarayan status = ldc_mem_release(task->mhdl, 0, buf->b_bcount); 14224bac2208Snarayan if (status) { 14233af08d82Slm66018 PR0("ldc_mem_release() returned errno %d copying to " 14243af08d82Slm66018 "client", status); 14253af08d82Slm66018 if (status == ECONNRESET) { 14263af08d82Slm66018 vd_mark_in_reset(vd); 14273af08d82Slm66018 } 1428205eeb1aSlm66018 rv = EIO; 14291ae08745Sheppo } 14301ae08745Sheppo 14313af08d82Slm66018 /* Unmap the memory, even if in reset */ 14324bac2208Snarayan status = ldc_mem_unmap(task->mhdl); 14334bac2208Snarayan if (status) { 14343af08d82Slm66018 PR0("ldc_mem_unmap() returned errno %d copying to client", 14354bac2208Snarayan status); 14363af08d82Slm66018 if (status == ECONNRESET) { 14373af08d82Slm66018 vd_mark_in_reset(vd); 14383af08d82Slm66018 } 1439205eeb1aSlm66018 rv = EIO; 14404bac2208Snarayan } 14414bac2208Snarayan 1442d10e4ef2Snarayan biofini(buf); 14431ae08745Sheppo 1444205eeb1aSlm66018 return (rv); 1445205eeb1aSlm66018 } 1446205eeb1aSlm66018 1447205eeb1aSlm66018 /* 1448205eeb1aSlm66018 * Description: 1449205eeb1aSlm66018 * This function is called by the two functions called by a taskq 1450205eeb1aSlm66018 * [ vd_complete_notify() and vd_serial_notify()) ] to send the 1451205eeb1aSlm66018 * message to the client. 1452205eeb1aSlm66018 * 1453205eeb1aSlm66018 * Parameters: 1454205eeb1aSlm66018 * arg - opaque pointer to structure containing task to be completed 1455205eeb1aSlm66018 * 1456205eeb1aSlm66018 * Return Values 1457205eeb1aSlm66018 * None 1458205eeb1aSlm66018 */ 1459205eeb1aSlm66018 static void 1460205eeb1aSlm66018 vd_notify(vd_task_t *task) 1461205eeb1aSlm66018 { 1462205eeb1aSlm66018 int status; 1463205eeb1aSlm66018 1464205eeb1aSlm66018 ASSERT(task != NULL); 1465205eeb1aSlm66018 ASSERT(task->vd != NULL); 1466205eeb1aSlm66018 1467205eeb1aSlm66018 if (task->vd->reset_state) 1468205eeb1aSlm66018 return; 1469205eeb1aSlm66018 1470205eeb1aSlm66018 /* 1471205eeb1aSlm66018 * Send the "ack" or "nack" back to the client; if sending the message 1472205eeb1aSlm66018 * via LDC fails, arrange to reset both the connection state and LDC 1473205eeb1aSlm66018 * itself 1474205eeb1aSlm66018 */ 1475205eeb1aSlm66018 PR2("Sending %s", 1476205eeb1aSlm66018 (task->msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK"); 1477205eeb1aSlm66018 1478205eeb1aSlm66018 status = send_msg(task->vd->ldc_handle, task->msg, task->msglen); 1479205eeb1aSlm66018 switch (status) { 1480205eeb1aSlm66018 case 0: 1481205eeb1aSlm66018 break; 1482205eeb1aSlm66018 case ECONNRESET: 1483205eeb1aSlm66018 vd_mark_in_reset(task->vd); 1484205eeb1aSlm66018 break; 1485205eeb1aSlm66018 default: 1486205eeb1aSlm66018 PR0("initiating full reset"); 1487205eeb1aSlm66018 vd_need_reset(task->vd, B_TRUE); 1488205eeb1aSlm66018 break; 1489205eeb1aSlm66018 } 1490205eeb1aSlm66018 1491205eeb1aSlm66018 DTRACE_PROBE1(task__end, vd_task_t *, task); 1492205eeb1aSlm66018 } 1493205eeb1aSlm66018 1494205eeb1aSlm66018 /* 1495205eeb1aSlm66018 * Description: 1496205eeb1aSlm66018 * Mark the Dring entry as Done and (if necessary) send an ACK/NACK to 1497205eeb1aSlm66018 * the vDisk client 1498205eeb1aSlm66018 * 1499205eeb1aSlm66018 * Parameters: 1500205eeb1aSlm66018 * task - structure containing the request sent from client 1501205eeb1aSlm66018 * 1502205eeb1aSlm66018 * Return Values 1503205eeb1aSlm66018 * None 1504205eeb1aSlm66018 */ 1505205eeb1aSlm66018 static void 1506205eeb1aSlm66018 vd_complete_notify(vd_task_t *task) 1507205eeb1aSlm66018 { 1508205eeb1aSlm66018 int status = 0; 1509205eeb1aSlm66018 vd_t *vd = task->vd; 1510205eeb1aSlm66018 vd_dring_payload_t *request = task->request; 1511205eeb1aSlm66018 1512d10e4ef2Snarayan /* Update the dring element for a dring client */ 1513205eeb1aSlm66018 if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE)) { 15143c96341aSnarayan status = vd_mark_elem_done(vd, task->index, 15153c96341aSnarayan request->status, request->nbytes); 15163af08d82Slm66018 if (status == ECONNRESET) 15173af08d82Slm66018 vd_mark_in_reset(vd); 15183af08d82Slm66018 } 15191ae08745Sheppo 1520d10e4ef2Snarayan /* 1521205eeb1aSlm66018 * If a transport error occurred while marking the element done or 1522205eeb1aSlm66018 * previously while executing the task, arrange to "nack" the message 1523205eeb1aSlm66018 * when the final task in the descriptor element range completes 1524d10e4ef2Snarayan */ 1525205eeb1aSlm66018 if ((status != 0) || (task->status != 0)) 1526d10e4ef2Snarayan task->msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 15271ae08745Sheppo 1528d10e4ef2Snarayan /* 1529d10e4ef2Snarayan * Only the final task for a range of elements will respond to and 1530d10e4ef2Snarayan * free the message 1531d10e4ef2Snarayan */ 15323af08d82Slm66018 if (task->type == VD_NONFINAL_RANGE_TASK) { 1533d10e4ef2Snarayan return; 15343af08d82Slm66018 } 15351ae08745Sheppo 1536205eeb1aSlm66018 vd_notify(task); 1537205eeb1aSlm66018 } 1538205eeb1aSlm66018 1539d10e4ef2Snarayan /* 1540205eeb1aSlm66018 * Description: 1541205eeb1aSlm66018 * This is the basic completion function called to handle inband data 1542205eeb1aSlm66018 * requests and handshake messages. All it needs to do is trigger a 1543205eeb1aSlm66018 * message to the client that the request is completed. 1544205eeb1aSlm66018 * 1545205eeb1aSlm66018 * Parameters: 1546205eeb1aSlm66018 * arg - opaque pointer to structure containing task to be completed 1547205eeb1aSlm66018 * 1548205eeb1aSlm66018 * Return Values 1549205eeb1aSlm66018 * None 1550d10e4ef2Snarayan */ 1551205eeb1aSlm66018 static void 1552205eeb1aSlm66018 vd_serial_notify(void *arg) 1553205eeb1aSlm66018 { 1554205eeb1aSlm66018 vd_task_t *task = (vd_task_t *)arg; 1555205eeb1aSlm66018 1556205eeb1aSlm66018 ASSERT(task != NULL); 1557205eeb1aSlm66018 vd_notify(task); 15581ae08745Sheppo } 15591ae08745Sheppo 15600a55fbb7Slm66018 static void 15610a55fbb7Slm66018 vd_geom2dk_geom(void *vd_buf, void *ioctl_arg) 15620a55fbb7Slm66018 { 15630a55fbb7Slm66018 VD_GEOM2DK_GEOM((vd_geom_t *)vd_buf, (struct dk_geom *)ioctl_arg); 15640a55fbb7Slm66018 } 15650a55fbb7Slm66018 15660a55fbb7Slm66018 static void 15670a55fbb7Slm66018 vd_vtoc2vtoc(void *vd_buf, void *ioctl_arg) 15680a55fbb7Slm66018 { 15690a55fbb7Slm66018 VD_VTOC2VTOC((vd_vtoc_t *)vd_buf, (struct vtoc *)ioctl_arg); 15700a55fbb7Slm66018 } 15710a55fbb7Slm66018 15720a55fbb7Slm66018 static void 15730a55fbb7Slm66018 dk_geom2vd_geom(void *ioctl_arg, void *vd_buf) 15740a55fbb7Slm66018 { 15750a55fbb7Slm66018 DK_GEOM2VD_GEOM((struct dk_geom *)ioctl_arg, (vd_geom_t *)vd_buf); 15760a55fbb7Slm66018 } 15770a55fbb7Slm66018 15780a55fbb7Slm66018 static void 15790a55fbb7Slm66018 vtoc2vd_vtoc(void *ioctl_arg, void *vd_buf) 15800a55fbb7Slm66018 { 15810a55fbb7Slm66018 VTOC2VD_VTOC((struct vtoc *)ioctl_arg, (vd_vtoc_t *)vd_buf); 15820a55fbb7Slm66018 } 15830a55fbb7Slm66018 15844bac2208Snarayan static void 15854bac2208Snarayan vd_get_efi_in(void *vd_buf, void *ioctl_arg) 15864bac2208Snarayan { 15874bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 15884bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 15894bac2208Snarayan 15904bac2208Snarayan dk_efi->dki_lba = vd_efi->lba; 15914bac2208Snarayan dk_efi->dki_length = vd_efi->length; 15924bac2208Snarayan dk_efi->dki_data = kmem_zalloc(vd_efi->length, KM_SLEEP); 15934bac2208Snarayan } 15944bac2208Snarayan 15954bac2208Snarayan static void 15964bac2208Snarayan vd_get_efi_out(void *ioctl_arg, void *vd_buf) 15974bac2208Snarayan { 15984bac2208Snarayan int len; 15994bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 16004bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 16014bac2208Snarayan 16024bac2208Snarayan len = vd_efi->length; 16034bac2208Snarayan DK_EFI2VD_EFI(dk_efi, vd_efi); 16044bac2208Snarayan kmem_free(dk_efi->dki_data, len); 16054bac2208Snarayan } 16064bac2208Snarayan 16074bac2208Snarayan static void 16084bac2208Snarayan vd_set_efi_in(void *vd_buf, void *ioctl_arg) 16094bac2208Snarayan { 16104bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 16114bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 16124bac2208Snarayan 16134bac2208Snarayan dk_efi->dki_data = kmem_alloc(vd_efi->length, KM_SLEEP); 16144bac2208Snarayan VD_EFI2DK_EFI(vd_efi, dk_efi); 16154bac2208Snarayan } 16164bac2208Snarayan 16174bac2208Snarayan static void 16184bac2208Snarayan vd_set_efi_out(void *ioctl_arg, void *vd_buf) 16194bac2208Snarayan { 16204bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 16214bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 16224bac2208Snarayan 16234bac2208Snarayan kmem_free(dk_efi->dki_data, vd_efi->length); 16244bac2208Snarayan } 16254bac2208Snarayan 162678fcd0a1Sachartre static vd_disk_label_t 1627047ba61eSachartre vd_read_vtoc(vd_t *vd, struct vtoc *vtoc) 16284bac2208Snarayan { 16294bac2208Snarayan int status, rval; 16304bac2208Snarayan struct dk_gpt *efi; 16314bac2208Snarayan size_t efi_len; 16324bac2208Snarayan 1633047ba61eSachartre ASSERT(vd->ldi_handle[0] != NULL); 1634047ba61eSachartre 1635047ba61eSachartre status = ldi_ioctl(vd->ldi_handle[0], DKIOCGVTOC, (intptr_t)vtoc, 1636047ba61eSachartre (vd->open_flags | FKIOCTL), kcred, &rval); 16374bac2208Snarayan 16384bac2208Snarayan if (status == 0) { 163978fcd0a1Sachartre return (VD_DISK_LABEL_VTOC); 16404bac2208Snarayan } else if (status != ENOTSUP) { 16413af08d82Slm66018 PR0("ldi_ioctl(DKIOCGVTOC) returned error %d", status); 164278fcd0a1Sachartre return (VD_DISK_LABEL_UNK); 16434bac2208Snarayan } 16444bac2208Snarayan 1645047ba61eSachartre status = vds_efi_alloc_and_read(vd->ldi_handle[0], &efi, &efi_len); 16464bac2208Snarayan 16474bac2208Snarayan if (status) { 16483af08d82Slm66018 PR0("vds_efi_alloc_and_read returned error %d", status); 164978fcd0a1Sachartre return (VD_DISK_LABEL_UNK); 16504bac2208Snarayan } 16514bac2208Snarayan 16524bac2208Snarayan vd_efi_to_vtoc(efi, vtoc); 16534bac2208Snarayan vd_efi_free(efi, efi_len); 16544bac2208Snarayan 165578fcd0a1Sachartre return (VD_DISK_LABEL_EFI); 16564bac2208Snarayan } 16574bac2208Snarayan 1658690555a1Sachartre static ushort_t 16593c96341aSnarayan vd_lbl2cksum(struct dk_label *label) 16603c96341aSnarayan { 16613c96341aSnarayan int count; 1662690555a1Sachartre ushort_t sum, *sp; 16633c96341aSnarayan 16643c96341aSnarayan count = (sizeof (struct dk_label)) / (sizeof (short)) - 1; 1665690555a1Sachartre sp = (ushort_t *)label; 16663c96341aSnarayan sum = 0; 16673c96341aSnarayan while (count--) { 16683c96341aSnarayan sum ^= *sp++; 16693c96341aSnarayan } 16703c96341aSnarayan 16713c96341aSnarayan return (sum); 16723c96341aSnarayan } 16733c96341aSnarayan 167487a7269eSachartre /* 167587a7269eSachartre * Handle ioctls to a disk slice. 1676205eeb1aSlm66018 * 1677205eeb1aSlm66018 * Return Values 1678205eeb1aSlm66018 * 0 - Indicates that there are no errors in disk operations 1679205eeb1aSlm66018 * ENOTSUP - Unknown disk label type or unsupported DKIO ioctl 1680205eeb1aSlm66018 * EINVAL - Not enough room to copy the EFI label 1681205eeb1aSlm66018 * 168287a7269eSachartre */ 16831ae08745Sheppo static int 16840a55fbb7Slm66018 vd_do_slice_ioctl(vd_t *vd, int cmd, void *ioctl_arg) 16851ae08745Sheppo { 16864bac2208Snarayan dk_efi_t *dk_ioc; 16874bac2208Snarayan 16884bac2208Snarayan switch (vd->vdisk_label) { 16894bac2208Snarayan 169087a7269eSachartre /* ioctls for a slice from a disk with a VTOC label */ 16914bac2208Snarayan case VD_DISK_LABEL_VTOC: 16924bac2208Snarayan 16931ae08745Sheppo switch (cmd) { 16941ae08745Sheppo case DKIOCGGEOM: 16950a55fbb7Slm66018 ASSERT(ioctl_arg != NULL); 16960a55fbb7Slm66018 bcopy(&vd->dk_geom, ioctl_arg, sizeof (vd->dk_geom)); 16971ae08745Sheppo return (0); 16981ae08745Sheppo case DKIOCGVTOC: 16990a55fbb7Slm66018 ASSERT(ioctl_arg != NULL); 17000a55fbb7Slm66018 bcopy(&vd->vtoc, ioctl_arg, sizeof (vd->vtoc)); 17011ae08745Sheppo return (0); 170287a7269eSachartre default: 17033c96341aSnarayan return (ENOTSUP); 170487a7269eSachartre } 170587a7269eSachartre 170687a7269eSachartre /* ioctls for a slice from a disk with an EFI label */ 170787a7269eSachartre case VD_DISK_LABEL_EFI: 170887a7269eSachartre 170987a7269eSachartre switch (cmd) { 171087a7269eSachartre case DKIOCGETEFI: 17113c96341aSnarayan ASSERT(ioctl_arg != NULL); 171287a7269eSachartre dk_ioc = (dk_efi_t *)ioctl_arg; 171387a7269eSachartre if (dk_ioc->dki_length < vd->dk_efi.dki_length) 171487a7269eSachartre return (EINVAL); 171587a7269eSachartre bcopy(vd->dk_efi.dki_data, dk_ioc->dki_data, 171687a7269eSachartre vd->dk_efi.dki_length); 171787a7269eSachartre return (0); 171887a7269eSachartre default: 171987a7269eSachartre return (ENOTSUP); 172087a7269eSachartre } 172187a7269eSachartre 172287a7269eSachartre default: 1723205eeb1aSlm66018 /* Unknown disk label type */ 172487a7269eSachartre return (ENOTSUP); 172587a7269eSachartre } 172687a7269eSachartre } 172787a7269eSachartre 172887a7269eSachartre /* 172978fcd0a1Sachartre * Function: 173078fcd0a1Sachartre * vd_file_validate_geometry 1731205eeb1aSlm66018 * 173278fcd0a1Sachartre * Description: 173378fcd0a1Sachartre * Read the label and validate the geometry of a disk image. The driver 173478fcd0a1Sachartre * label, vtoc and geometry information are updated according to the 173578fcd0a1Sachartre * label read from the disk image. 173678fcd0a1Sachartre * 173778fcd0a1Sachartre * If no valid label is found, the label is set to unknown and the 173878fcd0a1Sachartre * function returns EINVAL, but a default vtoc and geometry are provided 173978fcd0a1Sachartre * to the driver. 174078fcd0a1Sachartre * 174178fcd0a1Sachartre * Parameters: 174278fcd0a1Sachartre * vd - disk on which the operation is performed. 174378fcd0a1Sachartre * 174478fcd0a1Sachartre * Return Code: 174578fcd0a1Sachartre * 0 - success. 174678fcd0a1Sachartre * EIO - error reading the label from the disk image. 174778fcd0a1Sachartre * EINVAL - unknown disk label. 174887a7269eSachartre */ 174987a7269eSachartre static int 175078fcd0a1Sachartre vd_file_validate_geometry(vd_t *vd) 175187a7269eSachartre { 175287a7269eSachartre struct dk_label label; 175378fcd0a1Sachartre struct dk_geom *geom = &vd->dk_geom; 175478fcd0a1Sachartre struct vtoc *vtoc = &vd->vtoc; 175578fcd0a1Sachartre int i; 175678fcd0a1Sachartre int status = 0; 175787a7269eSachartre 175887a7269eSachartre ASSERT(vd->file); 175987a7269eSachartre 1760047ba61eSachartre if (vd->vdisk_type == VD_DISK_TYPE_SLICE) { 1761047ba61eSachartre /* 1762047ba61eSachartre * For single slice disk we always fake the geometry, and we 1763047ba61eSachartre * only need to do it once because the geometry will never 1764047ba61eSachartre * change. 1765047ba61eSachartre */ 1766047ba61eSachartre if (vd->vdisk_label == VD_DISK_LABEL_VTOC) 1767047ba61eSachartre /* geometry was already validated */ 1768047ba61eSachartre return (0); 1769047ba61eSachartre 1770047ba61eSachartre ASSERT(vd->vdisk_label == VD_DISK_LABEL_UNK); 1771047ba61eSachartre vd_file_build_default_label(vd, &label); 1772047ba61eSachartre vd->vdisk_label = VD_DISK_LABEL_VTOC; 1773047ba61eSachartre } else { 177487a7269eSachartre if (VD_FILE_LABEL_READ(vd, &label) < 0) 177587a7269eSachartre return (EIO); 177687a7269eSachartre 177787a7269eSachartre if (label.dkl_magic != DKL_MAGIC || 177878fcd0a1Sachartre label.dkl_cksum != vd_lbl2cksum(&label) || 177978fcd0a1Sachartre label.dkl_vtoc.v_sanity != VTOC_SANE || 178078fcd0a1Sachartre label.dkl_vtoc.v_nparts != V_NUMPAR) { 178178fcd0a1Sachartre vd->vdisk_label = VD_DISK_LABEL_UNK; 178278fcd0a1Sachartre vd_file_build_default_label(vd, &label); 178378fcd0a1Sachartre status = EINVAL; 178478fcd0a1Sachartre } else { 178578fcd0a1Sachartre vd->vdisk_label = VD_DISK_LABEL_VTOC; 178678fcd0a1Sachartre } 1787047ba61eSachartre } 178887a7269eSachartre 178978fcd0a1Sachartre /* Update the driver geometry */ 179087a7269eSachartre bzero(geom, sizeof (struct dk_geom)); 179178fcd0a1Sachartre 179287a7269eSachartre geom->dkg_ncyl = label.dkl_ncyl; 179387a7269eSachartre geom->dkg_acyl = label.dkl_acyl; 179487a7269eSachartre geom->dkg_nhead = label.dkl_nhead; 179587a7269eSachartre geom->dkg_nsect = label.dkl_nsect; 179687a7269eSachartre geom->dkg_intrlv = label.dkl_intrlv; 179787a7269eSachartre geom->dkg_apc = label.dkl_apc; 179887a7269eSachartre geom->dkg_rpm = label.dkl_rpm; 179987a7269eSachartre geom->dkg_pcyl = label.dkl_pcyl; 180087a7269eSachartre geom->dkg_write_reinstruct = label.dkl_write_reinstruct; 180187a7269eSachartre geom->dkg_read_reinstruct = label.dkl_read_reinstruct; 180287a7269eSachartre 180378fcd0a1Sachartre /* Update the driver vtoc */ 180487a7269eSachartre bzero(vtoc, sizeof (struct vtoc)); 180587a7269eSachartre 180687a7269eSachartre vtoc->v_sanity = label.dkl_vtoc.v_sanity; 180787a7269eSachartre vtoc->v_version = label.dkl_vtoc.v_version; 180887a7269eSachartre vtoc->v_sectorsz = DEV_BSIZE; 180987a7269eSachartre vtoc->v_nparts = label.dkl_vtoc.v_nparts; 181087a7269eSachartre 181187a7269eSachartre for (i = 0; i < vtoc->v_nparts; i++) { 181287a7269eSachartre vtoc->v_part[i].p_tag = 181387a7269eSachartre label.dkl_vtoc.v_part[i].p_tag; 181487a7269eSachartre vtoc->v_part[i].p_flag = 181587a7269eSachartre label.dkl_vtoc.v_part[i].p_flag; 181687a7269eSachartre vtoc->v_part[i].p_start = 181787a7269eSachartre label.dkl_map[i].dkl_cylno * 181887a7269eSachartre (label.dkl_nhead * label.dkl_nsect); 181987a7269eSachartre vtoc->v_part[i].p_size = label.dkl_map[i].dkl_nblk; 182087a7269eSachartre vtoc->timestamp[i] = 182187a7269eSachartre label.dkl_vtoc.v_timestamp[i]; 182287a7269eSachartre } 182387a7269eSachartre /* 182487a7269eSachartre * The bootinfo array can not be copied with bcopy() because 182587a7269eSachartre * elements are of type long in vtoc (so 64-bit) and of type 182687a7269eSachartre * int in dk_vtoc (so 32-bit). 182787a7269eSachartre */ 182887a7269eSachartre vtoc->v_bootinfo[0] = label.dkl_vtoc.v_bootinfo[0]; 182987a7269eSachartre vtoc->v_bootinfo[1] = label.dkl_vtoc.v_bootinfo[1]; 183087a7269eSachartre vtoc->v_bootinfo[2] = label.dkl_vtoc.v_bootinfo[2]; 183187a7269eSachartre bcopy(label.dkl_asciilabel, vtoc->v_asciilabel, 183287a7269eSachartre LEN_DKL_ASCII); 183387a7269eSachartre bcopy(label.dkl_vtoc.v_volume, vtoc->v_volume, 183487a7269eSachartre LEN_DKL_VVOL); 183587a7269eSachartre 183678fcd0a1Sachartre return (status); 183778fcd0a1Sachartre } 183878fcd0a1Sachartre 183978fcd0a1Sachartre /* 184078fcd0a1Sachartre * Handle ioctls to a disk image (file-based). 184178fcd0a1Sachartre * 184278fcd0a1Sachartre * Return Values 184378fcd0a1Sachartre * 0 - Indicates that there are no errors 184478fcd0a1Sachartre * != 0 - Disk operation returned an error 184578fcd0a1Sachartre */ 184678fcd0a1Sachartre static int 184778fcd0a1Sachartre vd_do_file_ioctl(vd_t *vd, int cmd, void *ioctl_arg) 184878fcd0a1Sachartre { 184978fcd0a1Sachartre struct dk_label label; 185078fcd0a1Sachartre struct dk_geom *geom; 185178fcd0a1Sachartre struct vtoc *vtoc; 185278fcd0a1Sachartre int i, rc; 185378fcd0a1Sachartre 185478fcd0a1Sachartre ASSERT(vd->file); 185578fcd0a1Sachartre 185678fcd0a1Sachartre switch (cmd) { 185778fcd0a1Sachartre 185878fcd0a1Sachartre case DKIOCGGEOM: 185978fcd0a1Sachartre ASSERT(ioctl_arg != NULL); 186078fcd0a1Sachartre geom = (struct dk_geom *)ioctl_arg; 186178fcd0a1Sachartre 186278fcd0a1Sachartre rc = vd_file_validate_geometry(vd); 1863047ba61eSachartre if (rc != 0 && rc != EINVAL) { 1864047ba61eSachartre ASSERT(vd->vdisk_type != VD_DISK_TYPE_SLICE); 186578fcd0a1Sachartre return (rc); 1866047ba61eSachartre } 186778fcd0a1Sachartre 186878fcd0a1Sachartre bcopy(&vd->dk_geom, geom, sizeof (struct dk_geom)); 186978fcd0a1Sachartre return (0); 187078fcd0a1Sachartre 187178fcd0a1Sachartre case DKIOCGVTOC: 187278fcd0a1Sachartre ASSERT(ioctl_arg != NULL); 187378fcd0a1Sachartre vtoc = (struct vtoc *)ioctl_arg; 187478fcd0a1Sachartre 187578fcd0a1Sachartre rc = vd_file_validate_geometry(vd); 1876047ba61eSachartre if (rc != 0 && rc != EINVAL) { 1877047ba61eSachartre ASSERT(vd->vdisk_type != VD_DISK_TYPE_SLICE); 187878fcd0a1Sachartre return (rc); 1879047ba61eSachartre } 188078fcd0a1Sachartre 188178fcd0a1Sachartre bcopy(&vd->vtoc, vtoc, sizeof (struct vtoc)); 188287a7269eSachartre return (0); 188387a7269eSachartre 188487a7269eSachartre case DKIOCSGEOM: 188587a7269eSachartre ASSERT(ioctl_arg != NULL); 188687a7269eSachartre geom = (struct dk_geom *)ioctl_arg; 188787a7269eSachartre 1888047ba61eSachartre /* geometry can only be changed for full disk */ 1889047ba61eSachartre if (vd->vdisk_type != VD_DISK_TYPE_DISK) 1890047ba61eSachartre return (ENOTSUP); 1891047ba61eSachartre 189287a7269eSachartre if (geom->dkg_nhead == 0 || geom->dkg_nsect == 0) 189387a7269eSachartre return (EINVAL); 189487a7269eSachartre 189587a7269eSachartre /* 189687a7269eSachartre * The current device geometry is not updated, just the driver 189787a7269eSachartre * "notion" of it. The device geometry will be effectively 189887a7269eSachartre * updated when a label is written to the device during a next 189987a7269eSachartre * DKIOCSVTOC. 190087a7269eSachartre */ 190187a7269eSachartre bcopy(ioctl_arg, &vd->dk_geom, sizeof (vd->dk_geom)); 190287a7269eSachartre return (0); 190387a7269eSachartre 190487a7269eSachartre case DKIOCSVTOC: 190587a7269eSachartre ASSERT(ioctl_arg != NULL); 190687a7269eSachartre ASSERT(vd->dk_geom.dkg_nhead != 0 && 190787a7269eSachartre vd->dk_geom.dkg_nsect != 0); 1908690555a1Sachartre vtoc = (struct vtoc *)ioctl_arg; 1909690555a1Sachartre 1910047ba61eSachartre /* vtoc can only be changed for full disk */ 1911047ba61eSachartre if (vd->vdisk_type != VD_DISK_TYPE_DISK) 1912047ba61eSachartre return (ENOTSUP); 1913047ba61eSachartre 1914690555a1Sachartre if (vtoc->v_sanity != VTOC_SANE || 1915690555a1Sachartre vtoc->v_sectorsz != DEV_BSIZE || 1916690555a1Sachartre vtoc->v_nparts != V_NUMPAR) 1917690555a1Sachartre return (EINVAL); 1918690555a1Sachartre 1919690555a1Sachartre bzero(&label, sizeof (label)); 1920690555a1Sachartre label.dkl_ncyl = vd->dk_geom.dkg_ncyl; 1921690555a1Sachartre label.dkl_acyl = vd->dk_geom.dkg_acyl; 1922690555a1Sachartre label.dkl_pcyl = vd->dk_geom.dkg_pcyl; 1923690555a1Sachartre label.dkl_nhead = vd->dk_geom.dkg_nhead; 1924690555a1Sachartre label.dkl_nsect = vd->dk_geom.dkg_nsect; 1925690555a1Sachartre label.dkl_intrlv = vd->dk_geom.dkg_intrlv; 1926690555a1Sachartre label.dkl_apc = vd->dk_geom.dkg_apc; 1927690555a1Sachartre label.dkl_rpm = vd->dk_geom.dkg_rpm; 192887a7269eSachartre label.dkl_write_reinstruct = vd->dk_geom.dkg_write_reinstruct; 192987a7269eSachartre label.dkl_read_reinstruct = vd->dk_geom.dkg_read_reinstruct; 1930690555a1Sachartre 193187a7269eSachartre label.dkl_vtoc.v_nparts = V_NUMPAR; 193287a7269eSachartre label.dkl_vtoc.v_sanity = VTOC_SANE; 1933690555a1Sachartre label.dkl_vtoc.v_version = vtoc->v_version; 193487a7269eSachartre for (i = 0; i < V_NUMPAR; i++) { 1935690555a1Sachartre label.dkl_vtoc.v_timestamp[i] = 1936690555a1Sachartre vtoc->timestamp[i]; 1937690555a1Sachartre label.dkl_vtoc.v_part[i].p_tag = 1938690555a1Sachartre vtoc->v_part[i].p_tag; 1939690555a1Sachartre label.dkl_vtoc.v_part[i].p_flag = 1940690555a1Sachartre vtoc->v_part[i].p_flag; 1941690555a1Sachartre label.dkl_map[i].dkl_cylno = 1942690555a1Sachartre vtoc->v_part[i].p_start / 1943690555a1Sachartre (label.dkl_nhead * label.dkl_nsect); 1944690555a1Sachartre label.dkl_map[i].dkl_nblk = 1945690555a1Sachartre vtoc->v_part[i].p_size; 19463c96341aSnarayan } 194787a7269eSachartre /* 194887a7269eSachartre * The bootinfo array can not be copied with bcopy() because 194987a7269eSachartre * elements are of type long in vtoc (so 64-bit) and of type 195087a7269eSachartre * int in dk_vtoc (so 32-bit). 195187a7269eSachartre */ 195287a7269eSachartre label.dkl_vtoc.v_bootinfo[0] = vtoc->v_bootinfo[0]; 195387a7269eSachartre label.dkl_vtoc.v_bootinfo[1] = vtoc->v_bootinfo[1]; 195487a7269eSachartre label.dkl_vtoc.v_bootinfo[2] = vtoc->v_bootinfo[2]; 1955690555a1Sachartre bcopy(vtoc->v_asciilabel, label.dkl_asciilabel, 1956690555a1Sachartre LEN_DKL_ASCII); 1957690555a1Sachartre bcopy(vtoc->v_volume, label.dkl_vtoc.v_volume, 1958690555a1Sachartre LEN_DKL_VVOL); 19593c96341aSnarayan 19603c96341aSnarayan /* re-compute checksum */ 1961690555a1Sachartre label.dkl_magic = DKL_MAGIC; 1962690555a1Sachartre label.dkl_cksum = vd_lbl2cksum(&label); 1963690555a1Sachartre 196487a7269eSachartre /* write label to the disk image */ 196587a7269eSachartre if ((rc = vd_file_set_vtoc(vd, &label)) != 0) 196687a7269eSachartre return (rc); 1967690555a1Sachartre 196878fcd0a1Sachartre /* check the geometry and update the driver info */ 196978fcd0a1Sachartre if ((rc = vd_file_validate_geometry(vd)) != 0) 197078fcd0a1Sachartre return (rc); 19713c96341aSnarayan 197287a7269eSachartre /* 197387a7269eSachartre * The disk geometry may have changed, so we need to write 197487a7269eSachartre * the devid (if there is one) so that it is stored at the 197587a7269eSachartre * right location. 197687a7269eSachartre */ 197787a7269eSachartre if (vd->file_devid != NULL && 197887a7269eSachartre vd_file_write_devid(vd, vd->file_devid) != 0) { 197987a7269eSachartre PR0("Fail to write devid"); 19801ae08745Sheppo } 19814bac2208Snarayan 19824bac2208Snarayan return (0); 19834bac2208Snarayan 19848259acd8Szk194757 case DKIOCFLUSHWRITECACHE: 1985*da6c28aaSamw return (VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL)); 19868259acd8Szk194757 19874bac2208Snarayan default: 19884bac2208Snarayan return (ENOTSUP); 19894bac2208Snarayan } 19901ae08745Sheppo } 19911ae08745Sheppo 1992205eeb1aSlm66018 /* 1993205eeb1aSlm66018 * Description: 1994205eeb1aSlm66018 * This is the function that processes the ioctl requests (farming it 1995205eeb1aSlm66018 * out to functions that handle slices, files or whole disks) 1996205eeb1aSlm66018 * 1997205eeb1aSlm66018 * Return Values 1998205eeb1aSlm66018 * 0 - ioctl operation completed successfully 1999205eeb1aSlm66018 * != 0 - The LDC error value encountered 2000205eeb1aSlm66018 * (propagated back up the call stack as a task error) 2001205eeb1aSlm66018 * 2002205eeb1aSlm66018 * Side Effect 2003205eeb1aSlm66018 * sets request->status to the return value of the ioctl function. 2004205eeb1aSlm66018 */ 20051ae08745Sheppo static int 20060a55fbb7Slm66018 vd_do_ioctl(vd_t *vd, vd_dring_payload_t *request, void* buf, vd_ioctl_t *ioctl) 20071ae08745Sheppo { 2008205eeb1aSlm66018 int rval = 0, status = 0; 20091ae08745Sheppo size_t nbytes = request->nbytes; /* modifiable copy */ 20101ae08745Sheppo 20111ae08745Sheppo 20121ae08745Sheppo ASSERT(request->slice < vd->nslices); 20131ae08745Sheppo PR0("Performing %s", ioctl->operation_name); 20141ae08745Sheppo 20150a55fbb7Slm66018 /* Get data from client and convert, if necessary */ 20160a55fbb7Slm66018 if (ioctl->copyin != NULL) { 20171ae08745Sheppo ASSERT(nbytes != 0 && buf != NULL); 20181ae08745Sheppo PR1("Getting \"arg\" data from client"); 20191ae08745Sheppo if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes, 20201ae08745Sheppo request->cookie, request->ncookies, 20211ae08745Sheppo LDC_COPY_IN)) != 0) { 20223af08d82Slm66018 PR0("ldc_mem_copy() returned errno %d " 20231ae08745Sheppo "copying from client", status); 20241ae08745Sheppo return (status); 20251ae08745Sheppo } 20260a55fbb7Slm66018 20270a55fbb7Slm66018 /* Convert client's data, if necessary */ 20280a55fbb7Slm66018 if (ioctl->copyin == VD_IDENTITY) /* use client buffer */ 20290a55fbb7Slm66018 ioctl->arg = buf; 20300a55fbb7Slm66018 else /* convert client vdisk operation data to ioctl data */ 20310a55fbb7Slm66018 (ioctl->copyin)(buf, (void *)ioctl->arg); 20321ae08745Sheppo } 20331ae08745Sheppo 20341ae08745Sheppo /* 20351ae08745Sheppo * Handle single-slice block devices internally; otherwise, have the 20361ae08745Sheppo * real driver perform the ioctl() 20371ae08745Sheppo */ 203887a7269eSachartre if (vd->file) { 2039205eeb1aSlm66018 request->status = 2040205eeb1aSlm66018 vd_do_file_ioctl(vd, ioctl->cmd, (void *)ioctl->arg); 2041205eeb1aSlm66018 204287a7269eSachartre } else if (vd->vdisk_type == VD_DISK_TYPE_SLICE && !vd->pseudo) { 2043205eeb1aSlm66018 request->status = 2044205eeb1aSlm66018 vd_do_slice_ioctl(vd, ioctl->cmd, (void *)ioctl->arg); 2045205eeb1aSlm66018 2046205eeb1aSlm66018 } else { 2047205eeb1aSlm66018 request->status = ldi_ioctl(vd->ldi_handle[request->slice], 2048047ba61eSachartre ioctl->cmd, (intptr_t)ioctl->arg, vd->open_flags | FKIOCTL, 2049205eeb1aSlm66018 kcred, &rval); 2050205eeb1aSlm66018 20511ae08745Sheppo #ifdef DEBUG 20521ae08745Sheppo if (rval != 0) { 2053205eeb1aSlm66018 PR0("%s set rval = %d, which is not being returned to" 2054205eeb1aSlm66018 " client", ioctl->cmd_name, rval); 20551ae08745Sheppo } 20561ae08745Sheppo #endif /* DEBUG */ 2057205eeb1aSlm66018 } 2058205eeb1aSlm66018 2059205eeb1aSlm66018 if (request->status != 0) { 2060205eeb1aSlm66018 PR0("ioctl(%s) = errno %d", ioctl->cmd_name, request->status); 2061205eeb1aSlm66018 return (0); 2062205eeb1aSlm66018 } 20631ae08745Sheppo 20640a55fbb7Slm66018 /* Convert data and send to client, if necessary */ 20650a55fbb7Slm66018 if (ioctl->copyout != NULL) { 20661ae08745Sheppo ASSERT(nbytes != 0 && buf != NULL); 20671ae08745Sheppo PR1("Sending \"arg\" data to client"); 20680a55fbb7Slm66018 20690a55fbb7Slm66018 /* Convert ioctl data to vdisk operation data, if necessary */ 20700a55fbb7Slm66018 if (ioctl->copyout != VD_IDENTITY) 20710a55fbb7Slm66018 (ioctl->copyout)((void *)ioctl->arg, buf); 20720a55fbb7Slm66018 20731ae08745Sheppo if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes, 20741ae08745Sheppo request->cookie, request->ncookies, 20751ae08745Sheppo LDC_COPY_OUT)) != 0) { 20763af08d82Slm66018 PR0("ldc_mem_copy() returned errno %d " 20771ae08745Sheppo "copying to client", status); 20781ae08745Sheppo return (status); 20791ae08745Sheppo } 20801ae08745Sheppo } 20811ae08745Sheppo 20821ae08745Sheppo return (status); 20831ae08745Sheppo } 20841ae08745Sheppo 20851ae08745Sheppo #define RNDSIZE(expr) P2ROUNDUP(sizeof (expr), sizeof (uint64_t)) 2086205eeb1aSlm66018 2087205eeb1aSlm66018 /* 2088205eeb1aSlm66018 * Description: 2089205eeb1aSlm66018 * This generic function is called by the task queue to complete 2090205eeb1aSlm66018 * the processing of the tasks. The specific completion function 2091205eeb1aSlm66018 * is passed in as a field in the task pointer. 2092205eeb1aSlm66018 * 2093205eeb1aSlm66018 * Parameters: 2094205eeb1aSlm66018 * arg - opaque pointer to structure containing task to be completed 2095205eeb1aSlm66018 * 2096205eeb1aSlm66018 * Return Values 2097205eeb1aSlm66018 * None 2098205eeb1aSlm66018 */ 2099205eeb1aSlm66018 static void 2100205eeb1aSlm66018 vd_complete(void *arg) 2101205eeb1aSlm66018 { 2102205eeb1aSlm66018 vd_task_t *task = (vd_task_t *)arg; 2103205eeb1aSlm66018 2104205eeb1aSlm66018 ASSERT(task != NULL); 2105205eeb1aSlm66018 ASSERT(task->status == EINPROGRESS); 2106205eeb1aSlm66018 ASSERT(task->completef != NULL); 2107205eeb1aSlm66018 2108205eeb1aSlm66018 task->status = task->completef(task); 2109205eeb1aSlm66018 if (task->status) 2110205eeb1aSlm66018 PR0("%s: Error %d completing task", __func__, task->status); 2111205eeb1aSlm66018 2112205eeb1aSlm66018 /* Now notify the vDisk client */ 2113205eeb1aSlm66018 vd_complete_notify(task); 2114205eeb1aSlm66018 } 2115205eeb1aSlm66018 21161ae08745Sheppo static int 2117d10e4ef2Snarayan vd_ioctl(vd_task_t *task) 21181ae08745Sheppo { 211987a7269eSachartre int i, status; 21201ae08745Sheppo void *buf = NULL; 21210a55fbb7Slm66018 struct dk_geom dk_geom = {0}; 21220a55fbb7Slm66018 struct vtoc vtoc = {0}; 21234bac2208Snarayan struct dk_efi dk_efi = {0}; 2124d10e4ef2Snarayan vd_t *vd = task->vd; 2125d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 21260a55fbb7Slm66018 vd_ioctl_t ioctl[] = { 21270a55fbb7Slm66018 /* Command (no-copy) operations */ 21280a55fbb7Slm66018 {VD_OP_FLUSH, STRINGIZE(VD_OP_FLUSH), 0, 21290a55fbb7Slm66018 DKIOCFLUSHWRITECACHE, STRINGIZE(DKIOCFLUSHWRITECACHE), 2130047ba61eSachartre NULL, NULL, NULL, B_TRUE}, 21310a55fbb7Slm66018 21320a55fbb7Slm66018 /* "Get" (copy-out) operations */ 21330a55fbb7Slm66018 {VD_OP_GET_WCE, STRINGIZE(VD_OP_GET_WCE), RNDSIZE(int), 21340a55fbb7Slm66018 DKIOCGETWCE, STRINGIZE(DKIOCGETWCE), 2135047ba61eSachartre NULL, VD_IDENTITY, VD_IDENTITY, B_FALSE}, 21360a55fbb7Slm66018 {VD_OP_GET_DISKGEOM, STRINGIZE(VD_OP_GET_DISKGEOM), 21370a55fbb7Slm66018 RNDSIZE(vd_geom_t), 21380a55fbb7Slm66018 DKIOCGGEOM, STRINGIZE(DKIOCGGEOM), 2139047ba61eSachartre &dk_geom, NULL, dk_geom2vd_geom, B_FALSE}, 21400a55fbb7Slm66018 {VD_OP_GET_VTOC, STRINGIZE(VD_OP_GET_VTOC), RNDSIZE(vd_vtoc_t), 21410a55fbb7Slm66018 DKIOCGVTOC, STRINGIZE(DKIOCGVTOC), 2142047ba61eSachartre &vtoc, NULL, vtoc2vd_vtoc, B_FALSE}, 21434bac2208Snarayan {VD_OP_GET_EFI, STRINGIZE(VD_OP_GET_EFI), RNDSIZE(vd_efi_t), 21444bac2208Snarayan DKIOCGETEFI, STRINGIZE(DKIOCGETEFI), 2145047ba61eSachartre &dk_efi, vd_get_efi_in, vd_get_efi_out, B_FALSE}, 21460a55fbb7Slm66018 21470a55fbb7Slm66018 /* "Set" (copy-in) operations */ 21480a55fbb7Slm66018 {VD_OP_SET_WCE, STRINGIZE(VD_OP_SET_WCE), RNDSIZE(int), 21490a55fbb7Slm66018 DKIOCSETWCE, STRINGIZE(DKIOCSETWCE), 2150047ba61eSachartre NULL, VD_IDENTITY, VD_IDENTITY, B_TRUE}, 21510a55fbb7Slm66018 {VD_OP_SET_DISKGEOM, STRINGIZE(VD_OP_SET_DISKGEOM), 21520a55fbb7Slm66018 RNDSIZE(vd_geom_t), 21530a55fbb7Slm66018 DKIOCSGEOM, STRINGIZE(DKIOCSGEOM), 2154047ba61eSachartre &dk_geom, vd_geom2dk_geom, NULL, B_TRUE}, 21550a55fbb7Slm66018 {VD_OP_SET_VTOC, STRINGIZE(VD_OP_SET_VTOC), RNDSIZE(vd_vtoc_t), 21560a55fbb7Slm66018 DKIOCSVTOC, STRINGIZE(DKIOCSVTOC), 2157047ba61eSachartre &vtoc, vd_vtoc2vtoc, NULL, B_TRUE}, 21584bac2208Snarayan {VD_OP_SET_EFI, STRINGIZE(VD_OP_SET_EFI), RNDSIZE(vd_efi_t), 21594bac2208Snarayan DKIOCSETEFI, STRINGIZE(DKIOCSETEFI), 2160047ba61eSachartre &dk_efi, vd_set_efi_in, vd_set_efi_out, B_TRUE}, 21610a55fbb7Slm66018 }; 21621ae08745Sheppo size_t nioctls = (sizeof (ioctl))/(sizeof (ioctl[0])); 21631ae08745Sheppo 21641ae08745Sheppo 2165d10e4ef2Snarayan ASSERT(vd != NULL); 2166d10e4ef2Snarayan ASSERT(request != NULL); 21671ae08745Sheppo ASSERT(request->slice < vd->nslices); 21681ae08745Sheppo 21691ae08745Sheppo /* 21701ae08745Sheppo * Determine ioctl corresponding to caller's "operation" and 21711ae08745Sheppo * validate caller's "nbytes" 21721ae08745Sheppo */ 21731ae08745Sheppo for (i = 0; i < nioctls; i++) { 21741ae08745Sheppo if (request->operation == ioctl[i].operation) { 21750a55fbb7Slm66018 /* LDC memory operations require 8-byte multiples */ 21760a55fbb7Slm66018 ASSERT(ioctl[i].nbytes % sizeof (uint64_t) == 0); 21770a55fbb7Slm66018 21784bac2208Snarayan if (request->operation == VD_OP_GET_EFI || 21794bac2208Snarayan request->operation == VD_OP_SET_EFI) { 21804bac2208Snarayan if (request->nbytes >= ioctl[i].nbytes) 21814bac2208Snarayan break; 21823af08d82Slm66018 PR0("%s: Expected at least nbytes = %lu, " 21834bac2208Snarayan "got %lu", ioctl[i].operation_name, 21844bac2208Snarayan ioctl[i].nbytes, request->nbytes); 21854bac2208Snarayan return (EINVAL); 21864bac2208Snarayan } 21874bac2208Snarayan 21880a55fbb7Slm66018 if (request->nbytes != ioctl[i].nbytes) { 21893af08d82Slm66018 PR0("%s: Expected nbytes = %lu, got %lu", 21900a55fbb7Slm66018 ioctl[i].operation_name, ioctl[i].nbytes, 21910a55fbb7Slm66018 request->nbytes); 21921ae08745Sheppo return (EINVAL); 21931ae08745Sheppo } 21941ae08745Sheppo 21951ae08745Sheppo break; 21961ae08745Sheppo } 21971ae08745Sheppo } 21981ae08745Sheppo ASSERT(i < nioctls); /* because "operation" already validated */ 21991ae08745Sheppo 2200047ba61eSachartre if (!(vd->open_flags & FWRITE) && ioctl[i].write) { 2201047ba61eSachartre PR0("%s fails because backend is opened read-only", 2202047ba61eSachartre ioctl[i].operation_name); 2203047ba61eSachartre request->status = EROFS; 2204047ba61eSachartre return (0); 2205047ba61eSachartre } 2206047ba61eSachartre 22071ae08745Sheppo if (request->nbytes) 22081ae08745Sheppo buf = kmem_zalloc(request->nbytes, KM_SLEEP); 22091ae08745Sheppo status = vd_do_ioctl(vd, request, buf, &ioctl[i]); 22101ae08745Sheppo if (request->nbytes) 22111ae08745Sheppo kmem_free(buf, request->nbytes); 221287a7269eSachartre 22131ae08745Sheppo return (status); 22141ae08745Sheppo } 22151ae08745Sheppo 22164bac2208Snarayan static int 22174bac2208Snarayan vd_get_devid(vd_task_t *task) 22184bac2208Snarayan { 22194bac2208Snarayan vd_t *vd = task->vd; 22204bac2208Snarayan vd_dring_payload_t *request = task->request; 22214bac2208Snarayan vd_devid_t *vd_devid; 22224bac2208Snarayan impl_devid_t *devid; 222387a7269eSachartre int status, bufid_len, devid_len, len, sz; 22243af08d82Slm66018 int bufbytes; 22254bac2208Snarayan 22263af08d82Slm66018 PR1("Get Device ID, nbytes=%ld", request->nbytes); 22274bac2208Snarayan 22283c96341aSnarayan if (vd->file) { 222987a7269eSachartre if (vd->file_devid == NULL) { 22303af08d82Slm66018 PR2("No Device ID"); 2231205eeb1aSlm66018 request->status = ENOENT; 2232205eeb1aSlm66018 return (0); 223387a7269eSachartre } else { 223487a7269eSachartre sz = ddi_devid_sizeof(vd->file_devid); 223587a7269eSachartre devid = kmem_alloc(sz, KM_SLEEP); 223687a7269eSachartre bcopy(vd->file_devid, devid, sz); 223787a7269eSachartre } 223887a7269eSachartre } else { 223987a7269eSachartre if (ddi_lyr_get_devid(vd->dev[request->slice], 224087a7269eSachartre (ddi_devid_t *)&devid) != DDI_SUCCESS) { 224187a7269eSachartre PR2("No Device ID"); 2242205eeb1aSlm66018 request->status = ENOENT; 2243205eeb1aSlm66018 return (0); 224487a7269eSachartre } 22454bac2208Snarayan } 22464bac2208Snarayan 22474bac2208Snarayan bufid_len = request->nbytes - sizeof (vd_devid_t) + 1; 22484bac2208Snarayan devid_len = DEVID_GETLEN(devid); 22494bac2208Snarayan 22503af08d82Slm66018 /* 22513af08d82Slm66018 * Save the buffer size here for use in deallocation. 22523af08d82Slm66018 * The actual number of bytes copied is returned in 22533af08d82Slm66018 * the 'nbytes' field of the request structure. 22543af08d82Slm66018 */ 22553af08d82Slm66018 bufbytes = request->nbytes; 22563af08d82Slm66018 22573af08d82Slm66018 vd_devid = kmem_zalloc(bufbytes, KM_SLEEP); 22584bac2208Snarayan vd_devid->length = devid_len; 22594bac2208Snarayan vd_devid->type = DEVID_GETTYPE(devid); 22604bac2208Snarayan 22614bac2208Snarayan len = (devid_len > bufid_len)? bufid_len : devid_len; 22624bac2208Snarayan 22634bac2208Snarayan bcopy(devid->did_id, vd_devid->id, len); 22644bac2208Snarayan 226578fcd0a1Sachartre request->status = 0; 226678fcd0a1Sachartre 22674bac2208Snarayan /* LDC memory operations require 8-byte multiples */ 22684bac2208Snarayan ASSERT(request->nbytes % sizeof (uint64_t) == 0); 22694bac2208Snarayan 22704bac2208Snarayan if ((status = ldc_mem_copy(vd->ldc_handle, (caddr_t)vd_devid, 0, 22714bac2208Snarayan &request->nbytes, request->cookie, request->ncookies, 22724bac2208Snarayan LDC_COPY_OUT)) != 0) { 22733af08d82Slm66018 PR0("ldc_mem_copy() returned errno %d copying to client", 22744bac2208Snarayan status); 22754bac2208Snarayan } 22763af08d82Slm66018 PR1("post mem_copy: nbytes=%ld", request->nbytes); 22774bac2208Snarayan 22783af08d82Slm66018 kmem_free(vd_devid, bufbytes); 22794bac2208Snarayan ddi_devid_free((ddi_devid_t)devid); 22804bac2208Snarayan 22814bac2208Snarayan return (status); 22824bac2208Snarayan } 22834bac2208Snarayan 22841ae08745Sheppo /* 22851ae08745Sheppo * Define the supported operations once the functions for performing them have 22861ae08745Sheppo * been defined 22871ae08745Sheppo */ 22881ae08745Sheppo static const vds_operation_t vds_operation[] = { 22893af08d82Slm66018 #define X(_s) #_s, _s 22903af08d82Slm66018 {X(VD_OP_BREAD), vd_start_bio, vd_complete_bio}, 22913af08d82Slm66018 {X(VD_OP_BWRITE), vd_start_bio, vd_complete_bio}, 22923af08d82Slm66018 {X(VD_OP_FLUSH), vd_ioctl, NULL}, 22933af08d82Slm66018 {X(VD_OP_GET_WCE), vd_ioctl, NULL}, 22943af08d82Slm66018 {X(VD_OP_SET_WCE), vd_ioctl, NULL}, 22953af08d82Slm66018 {X(VD_OP_GET_VTOC), vd_ioctl, NULL}, 22963af08d82Slm66018 {X(VD_OP_SET_VTOC), vd_ioctl, NULL}, 22973af08d82Slm66018 {X(VD_OP_GET_DISKGEOM), vd_ioctl, NULL}, 22983af08d82Slm66018 {X(VD_OP_SET_DISKGEOM), vd_ioctl, NULL}, 22993af08d82Slm66018 {X(VD_OP_GET_EFI), vd_ioctl, NULL}, 23003af08d82Slm66018 {X(VD_OP_SET_EFI), vd_ioctl, NULL}, 23013af08d82Slm66018 {X(VD_OP_GET_DEVID), vd_get_devid, NULL}, 23023af08d82Slm66018 #undef X 23031ae08745Sheppo }; 23041ae08745Sheppo 23051ae08745Sheppo static const size_t vds_noperations = 23061ae08745Sheppo (sizeof (vds_operation))/(sizeof (vds_operation[0])); 23071ae08745Sheppo 23081ae08745Sheppo /* 2309d10e4ef2Snarayan * Process a task specifying a client I/O request 2310205eeb1aSlm66018 * 2311205eeb1aSlm66018 * Parameters: 2312205eeb1aSlm66018 * task - structure containing the request sent from client 2313205eeb1aSlm66018 * 2314205eeb1aSlm66018 * Return Value 2315205eeb1aSlm66018 * 0 - success 2316205eeb1aSlm66018 * ENOTSUP - Unknown/Unsupported VD_OP_XXX operation 2317205eeb1aSlm66018 * EINVAL - Invalid disk slice 2318205eeb1aSlm66018 * != 0 - some other non-zero return value from start function 23191ae08745Sheppo */ 23201ae08745Sheppo static int 2321205eeb1aSlm66018 vd_do_process_task(vd_task_t *task) 23221ae08745Sheppo { 2323205eeb1aSlm66018 int i; 2324d10e4ef2Snarayan vd_t *vd = task->vd; 2325d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 23261ae08745Sheppo 2327d10e4ef2Snarayan ASSERT(vd != NULL); 2328d10e4ef2Snarayan ASSERT(request != NULL); 23291ae08745Sheppo 2330d10e4ef2Snarayan /* Find the requested operation */ 2331205eeb1aSlm66018 for (i = 0; i < vds_noperations; i++) { 2332205eeb1aSlm66018 if (request->operation == vds_operation[i].operation) { 2333205eeb1aSlm66018 /* all operations should have a start func */ 2334205eeb1aSlm66018 ASSERT(vds_operation[i].start != NULL); 2335205eeb1aSlm66018 2336205eeb1aSlm66018 task->completef = vds_operation[i].complete; 2337d10e4ef2Snarayan break; 2338205eeb1aSlm66018 } 2339205eeb1aSlm66018 } 2340d10e4ef2Snarayan if (i == vds_noperations) { 23413af08d82Slm66018 PR0("Unsupported operation %u", request->operation); 23421ae08745Sheppo return (ENOTSUP); 23431ae08745Sheppo } 23441ae08745Sheppo 23457636cb21Slm66018 /* Range-check slice */ 234687a7269eSachartre if (request->slice >= vd->nslices && 234787a7269eSachartre (vd->vdisk_type != VD_DISK_TYPE_DISK || 234887a7269eSachartre request->slice != VD_SLICE_NONE)) { 23493af08d82Slm66018 PR0("Invalid \"slice\" %u (max %u) for virtual disk", 23507636cb21Slm66018 request->slice, (vd->nslices - 1)); 23517636cb21Slm66018 return (EINVAL); 23527636cb21Slm66018 } 23537636cb21Slm66018 2354205eeb1aSlm66018 /* 2355205eeb1aSlm66018 * Call the function pointer that starts the operation. 2356205eeb1aSlm66018 */ 2357205eeb1aSlm66018 return (vds_operation[i].start(task)); 23581ae08745Sheppo } 23591ae08745Sheppo 2360205eeb1aSlm66018 /* 2361205eeb1aSlm66018 * Description: 2362205eeb1aSlm66018 * This function is called by both the in-band and descriptor ring 2363205eeb1aSlm66018 * message processing functions paths to actually execute the task 2364205eeb1aSlm66018 * requested by the vDisk client. It in turn calls its worker 2365205eeb1aSlm66018 * function, vd_do_process_task(), to carry our the request. 2366205eeb1aSlm66018 * 2367205eeb1aSlm66018 * Any transport errors (e.g. LDC errors, vDisk protocol errors) are 2368205eeb1aSlm66018 * saved in the 'status' field of the task and are propagated back 2369205eeb1aSlm66018 * up the call stack to trigger a NACK 2370205eeb1aSlm66018 * 2371205eeb1aSlm66018 * Any request errors (e.g. ENOTTY from an ioctl) are saved in 2372205eeb1aSlm66018 * the 'status' field of the request and result in an ACK being sent 2373205eeb1aSlm66018 * by the completion handler. 2374205eeb1aSlm66018 * 2375205eeb1aSlm66018 * Parameters: 2376205eeb1aSlm66018 * task - structure containing the request sent from client 2377205eeb1aSlm66018 * 2378205eeb1aSlm66018 * Return Value 2379205eeb1aSlm66018 * 0 - successful synchronous request. 2380205eeb1aSlm66018 * != 0 - transport error (e.g. LDC errors, vDisk protocol) 2381205eeb1aSlm66018 * EINPROGRESS - task will be finished in a completion handler 2382205eeb1aSlm66018 */ 2383205eeb1aSlm66018 static int 2384205eeb1aSlm66018 vd_process_task(vd_task_t *task) 2385205eeb1aSlm66018 { 2386205eeb1aSlm66018 vd_t *vd = task->vd; 2387205eeb1aSlm66018 int status; 23881ae08745Sheppo 2389205eeb1aSlm66018 DTRACE_PROBE1(task__start, vd_task_t *, task); 23903af08d82Slm66018 2391205eeb1aSlm66018 task->status = vd_do_process_task(task); 2392205eeb1aSlm66018 2393205eeb1aSlm66018 /* 2394205eeb1aSlm66018 * If the task processing function returned EINPROGRESS indicating 2395205eeb1aSlm66018 * that the task needs completing then schedule a taskq entry to 2396205eeb1aSlm66018 * finish it now. 2397205eeb1aSlm66018 * 2398205eeb1aSlm66018 * Otherwise the task processing function returned either zero 2399205eeb1aSlm66018 * indicating that the task was finished in the start function (and we 2400205eeb1aSlm66018 * don't need to wait in a completion function) or the start function 2401205eeb1aSlm66018 * returned an error - in both cases all that needs to happen is the 2402205eeb1aSlm66018 * notification to the vDisk client higher up the call stack. 2403205eeb1aSlm66018 * If the task was using a Descriptor Ring, we need to mark it as done 2404205eeb1aSlm66018 * at this stage. 2405205eeb1aSlm66018 */ 2406205eeb1aSlm66018 if (task->status == EINPROGRESS) { 2407d10e4ef2Snarayan /* Queue a task to complete the operation */ 2408205eeb1aSlm66018 (void) ddi_taskq_dispatch(vd->completionq, vd_complete, 2409d10e4ef2Snarayan task, DDI_SLEEP); 2410d10e4ef2Snarayan 2411205eeb1aSlm66018 } else if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE)) { 2412205eeb1aSlm66018 /* Update the dring element if it's a dring client */ 2413205eeb1aSlm66018 status = vd_mark_elem_done(vd, task->index, 2414205eeb1aSlm66018 task->request->status, task->request->nbytes); 2415205eeb1aSlm66018 if (status == ECONNRESET) 2416205eeb1aSlm66018 vd_mark_in_reset(vd); 2417205eeb1aSlm66018 } 2418205eeb1aSlm66018 2419205eeb1aSlm66018 return (task->status); 24201ae08745Sheppo } 24211ae08745Sheppo 24221ae08745Sheppo /* 24230a55fbb7Slm66018 * Return true if the "type", "subtype", and "env" fields of the "tag" first 24240a55fbb7Slm66018 * argument match the corresponding remaining arguments; otherwise, return false 24251ae08745Sheppo */ 24260a55fbb7Slm66018 boolean_t 24271ae08745Sheppo vd_msgtype(vio_msg_tag_t *tag, int type, int subtype, int env) 24281ae08745Sheppo { 24291ae08745Sheppo return ((tag->vio_msgtype == type) && 24301ae08745Sheppo (tag->vio_subtype == subtype) && 24310a55fbb7Slm66018 (tag->vio_subtype_env == env)) ? B_TRUE : B_FALSE; 24321ae08745Sheppo } 24331ae08745Sheppo 24340a55fbb7Slm66018 /* 24350a55fbb7Slm66018 * Check whether the major/minor version specified in "ver_msg" is supported 24360a55fbb7Slm66018 * by this server. 24370a55fbb7Slm66018 */ 24380a55fbb7Slm66018 static boolean_t 24390a55fbb7Slm66018 vds_supported_version(vio_ver_msg_t *ver_msg) 24400a55fbb7Slm66018 { 24410a55fbb7Slm66018 for (int i = 0; i < vds_num_versions; i++) { 24420a55fbb7Slm66018 ASSERT(vds_version[i].major > 0); 24430a55fbb7Slm66018 ASSERT((i == 0) || 24440a55fbb7Slm66018 (vds_version[i].major < vds_version[i-1].major)); 24450a55fbb7Slm66018 24460a55fbb7Slm66018 /* 24470a55fbb7Slm66018 * If the major versions match, adjust the minor version, if 24480a55fbb7Slm66018 * necessary, down to the highest value supported by this 24490a55fbb7Slm66018 * server and return true so this message will get "ack"ed; 24500a55fbb7Slm66018 * the client should also support all minor versions lower 24510a55fbb7Slm66018 * than the value it sent 24520a55fbb7Slm66018 */ 24530a55fbb7Slm66018 if (ver_msg->ver_major == vds_version[i].major) { 24540a55fbb7Slm66018 if (ver_msg->ver_minor > vds_version[i].minor) { 24550a55fbb7Slm66018 PR0("Adjusting minor version from %u to %u", 24560a55fbb7Slm66018 ver_msg->ver_minor, vds_version[i].minor); 24570a55fbb7Slm66018 ver_msg->ver_minor = vds_version[i].minor; 24580a55fbb7Slm66018 } 24590a55fbb7Slm66018 return (B_TRUE); 24600a55fbb7Slm66018 } 24610a55fbb7Slm66018 24620a55fbb7Slm66018 /* 24630a55fbb7Slm66018 * If the message contains a higher major version number, set 24640a55fbb7Slm66018 * the message's major/minor versions to the current values 24650a55fbb7Slm66018 * and return false, so this message will get "nack"ed with 24660a55fbb7Slm66018 * these values, and the client will potentially try again 24670a55fbb7Slm66018 * with the same or a lower version 24680a55fbb7Slm66018 */ 24690a55fbb7Slm66018 if (ver_msg->ver_major > vds_version[i].major) { 24700a55fbb7Slm66018 ver_msg->ver_major = vds_version[i].major; 24710a55fbb7Slm66018 ver_msg->ver_minor = vds_version[i].minor; 24720a55fbb7Slm66018 return (B_FALSE); 24730a55fbb7Slm66018 } 24740a55fbb7Slm66018 24750a55fbb7Slm66018 /* 24760a55fbb7Slm66018 * Otherwise, the message's major version is less than the 24770a55fbb7Slm66018 * current major version, so continue the loop to the next 24780a55fbb7Slm66018 * (lower) supported version 24790a55fbb7Slm66018 */ 24800a55fbb7Slm66018 } 24810a55fbb7Slm66018 24820a55fbb7Slm66018 /* 24830a55fbb7Slm66018 * No common version was found; "ground" the version pair in the 24840a55fbb7Slm66018 * message to terminate negotiation 24850a55fbb7Slm66018 */ 24860a55fbb7Slm66018 ver_msg->ver_major = 0; 24870a55fbb7Slm66018 ver_msg->ver_minor = 0; 24880a55fbb7Slm66018 return (B_FALSE); 24890a55fbb7Slm66018 } 24900a55fbb7Slm66018 24910a55fbb7Slm66018 /* 24920a55fbb7Slm66018 * Process a version message from a client. vds expects to receive version 24930a55fbb7Slm66018 * messages from clients seeking service, but never issues version messages 24940a55fbb7Slm66018 * itself; therefore, vds can ACK or NACK client version messages, but does 24950a55fbb7Slm66018 * not expect to receive version-message ACKs or NACKs (and will treat such 24960a55fbb7Slm66018 * messages as invalid). 24970a55fbb7Slm66018 */ 24981ae08745Sheppo static int 24990a55fbb7Slm66018 vd_process_ver_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 25001ae08745Sheppo { 25011ae08745Sheppo vio_ver_msg_t *ver_msg = (vio_ver_msg_t *)msg; 25021ae08745Sheppo 25031ae08745Sheppo 25041ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 25051ae08745Sheppo 25061ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 25071ae08745Sheppo VIO_VER_INFO)) { 25081ae08745Sheppo return (ENOMSG); /* not a version message */ 25091ae08745Sheppo } 25101ae08745Sheppo 25111ae08745Sheppo if (msglen != sizeof (*ver_msg)) { 25123af08d82Slm66018 PR0("Expected %lu-byte version message; " 25131ae08745Sheppo "received %lu bytes", sizeof (*ver_msg), msglen); 25141ae08745Sheppo return (EBADMSG); 25151ae08745Sheppo } 25161ae08745Sheppo 25171ae08745Sheppo if (ver_msg->dev_class != VDEV_DISK) { 25183af08d82Slm66018 PR0("Expected device class %u (disk); received %u", 25191ae08745Sheppo VDEV_DISK, ver_msg->dev_class); 25201ae08745Sheppo return (EBADMSG); 25211ae08745Sheppo } 25221ae08745Sheppo 25230a55fbb7Slm66018 /* 25240a55fbb7Slm66018 * We're talking to the expected kind of client; set our device class 25250a55fbb7Slm66018 * for "ack/nack" back to the client 25260a55fbb7Slm66018 */ 25271ae08745Sheppo ver_msg->dev_class = VDEV_DISK_SERVER; 25280a55fbb7Slm66018 25290a55fbb7Slm66018 /* 25300a55fbb7Slm66018 * Check whether the (valid) version message specifies a version 25310a55fbb7Slm66018 * supported by this server. If the version is not supported, return 25320a55fbb7Slm66018 * EBADMSG so the message will get "nack"ed; vds_supported_version() 25330a55fbb7Slm66018 * will have updated the message with a supported version for the 25340a55fbb7Slm66018 * client to consider 25350a55fbb7Slm66018 */ 25360a55fbb7Slm66018 if (!vds_supported_version(ver_msg)) 25370a55fbb7Slm66018 return (EBADMSG); 25380a55fbb7Slm66018 25390a55fbb7Slm66018 25400a55fbb7Slm66018 /* 25410a55fbb7Slm66018 * A version has been agreed upon; use the client's SID for 25420a55fbb7Slm66018 * communication on this channel now 25430a55fbb7Slm66018 */ 25440a55fbb7Slm66018 ASSERT(!(vd->initialized & VD_SID)); 25450a55fbb7Slm66018 vd->sid = ver_msg->tag.vio_sid; 25460a55fbb7Slm66018 vd->initialized |= VD_SID; 25470a55fbb7Slm66018 25480a55fbb7Slm66018 /* 25490a55fbb7Slm66018 * When multiple versions are supported, this function should store 25500a55fbb7Slm66018 * the negotiated major and minor version values in the "vd" data 25510a55fbb7Slm66018 * structure to govern further communication; in particular, note that 25520a55fbb7Slm66018 * the client might have specified a lower minor version for the 2553*da6c28aaSamw * agreed major version than specified in the vds_version[] array. The 25540a55fbb7Slm66018 * following assertions should help remind future maintainers to make 25550a55fbb7Slm66018 * the appropriate changes to support multiple versions. 25560a55fbb7Slm66018 */ 25570a55fbb7Slm66018 ASSERT(vds_num_versions == 1); 25580a55fbb7Slm66018 ASSERT(ver_msg->ver_major == vds_version[0].major); 25590a55fbb7Slm66018 ASSERT(ver_msg->ver_minor == vds_version[0].minor); 25600a55fbb7Slm66018 25610a55fbb7Slm66018 PR0("Using major version %u, minor version %u", 25620a55fbb7Slm66018 ver_msg->ver_major, ver_msg->ver_minor); 25631ae08745Sheppo return (0); 25641ae08745Sheppo } 25651ae08745Sheppo 25661ae08745Sheppo static int 25671ae08745Sheppo vd_process_attr_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 25681ae08745Sheppo { 25691ae08745Sheppo vd_attr_msg_t *attr_msg = (vd_attr_msg_t *)msg; 25703c96341aSnarayan int status, retry = 0; 25711ae08745Sheppo 25721ae08745Sheppo 25731ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 25741ae08745Sheppo 25751ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 25761ae08745Sheppo VIO_ATTR_INFO)) { 2577d10e4ef2Snarayan PR0("Message is not an attribute message"); 2578d10e4ef2Snarayan return (ENOMSG); 25791ae08745Sheppo } 25801ae08745Sheppo 25811ae08745Sheppo if (msglen != sizeof (*attr_msg)) { 25823af08d82Slm66018 PR0("Expected %lu-byte attribute message; " 25831ae08745Sheppo "received %lu bytes", sizeof (*attr_msg), msglen); 25841ae08745Sheppo return (EBADMSG); 25851ae08745Sheppo } 25861ae08745Sheppo 25871ae08745Sheppo if (attr_msg->max_xfer_sz == 0) { 25883af08d82Slm66018 PR0("Received maximum transfer size of 0 from client"); 25891ae08745Sheppo return (EBADMSG); 25901ae08745Sheppo } 25911ae08745Sheppo 25921ae08745Sheppo if ((attr_msg->xfer_mode != VIO_DESC_MODE) && 25931ae08745Sheppo (attr_msg->xfer_mode != VIO_DRING_MODE)) { 25943af08d82Slm66018 PR0("Client requested unsupported transfer mode"); 25951ae08745Sheppo return (EBADMSG); 25961ae08745Sheppo } 25971ae08745Sheppo 25983c96341aSnarayan /* 25993c96341aSnarayan * check if the underlying disk is ready, if not try accessing 26003c96341aSnarayan * the device again. Open the vdisk device and extract info 26013c96341aSnarayan * about it, as this is needed to respond to the attr info msg 26023c96341aSnarayan */ 26033c96341aSnarayan if ((vd->initialized & VD_DISK_READY) == 0) { 26043c96341aSnarayan PR0("Retry setting up disk (%s)", vd->device_path); 26053c96341aSnarayan do { 26063c96341aSnarayan status = vd_setup_vd(vd); 26073c96341aSnarayan if (status != EAGAIN || ++retry > vds_dev_retries) 26083c96341aSnarayan break; 26093c96341aSnarayan 26103c96341aSnarayan /* incremental delay */ 26113c96341aSnarayan delay(drv_usectohz(vds_dev_delay)); 26123c96341aSnarayan 26133c96341aSnarayan /* if vdisk is no longer enabled - return error */ 26143c96341aSnarayan if (!vd_enabled(vd)) 26153c96341aSnarayan return (ENXIO); 26163c96341aSnarayan 26173c96341aSnarayan } while (status == EAGAIN); 26183c96341aSnarayan 26193c96341aSnarayan if (status) 26203c96341aSnarayan return (ENXIO); 26213c96341aSnarayan 26223c96341aSnarayan vd->initialized |= VD_DISK_READY; 26233c96341aSnarayan ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR); 26243c96341aSnarayan PR0("vdisk_type = %s, pseudo = %s, file = %s, nslices = %u", 26253c96341aSnarayan ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"), 26263c96341aSnarayan (vd->pseudo ? "yes" : "no"), 26273c96341aSnarayan (vd->file ? "yes" : "no"), 26283c96341aSnarayan vd->nslices); 26293c96341aSnarayan } 26303c96341aSnarayan 26311ae08745Sheppo /* Success: valid message and transfer mode */ 26321ae08745Sheppo vd->xfer_mode = attr_msg->xfer_mode; 26333af08d82Slm66018 26341ae08745Sheppo if (vd->xfer_mode == VIO_DESC_MODE) { 26353af08d82Slm66018 26361ae08745Sheppo /* 26371ae08745Sheppo * The vd_dring_inband_msg_t contains one cookie; need room 26381ae08745Sheppo * for up to n-1 more cookies, where "n" is the number of full 26391ae08745Sheppo * pages plus possibly one partial page required to cover 26401ae08745Sheppo * "max_xfer_sz". Add room for one more cookie if 26411ae08745Sheppo * "max_xfer_sz" isn't an integral multiple of the page size. 26421ae08745Sheppo * Must first get the maximum transfer size in bytes. 26431ae08745Sheppo */ 26441ae08745Sheppo size_t max_xfer_bytes = attr_msg->vdisk_block_size ? 26451ae08745Sheppo attr_msg->vdisk_block_size*attr_msg->max_xfer_sz : 26461ae08745Sheppo attr_msg->max_xfer_sz; 26471ae08745Sheppo size_t max_inband_msglen = 26481ae08745Sheppo sizeof (vd_dring_inband_msg_t) + 26491ae08745Sheppo ((max_xfer_bytes/PAGESIZE + 26501ae08745Sheppo ((max_xfer_bytes % PAGESIZE) ? 1 : 0))* 26511ae08745Sheppo (sizeof (ldc_mem_cookie_t))); 26521ae08745Sheppo 26531ae08745Sheppo /* 26541ae08745Sheppo * Set the maximum expected message length to 26551ae08745Sheppo * accommodate in-band-descriptor messages with all 26561ae08745Sheppo * their cookies 26571ae08745Sheppo */ 26581ae08745Sheppo vd->max_msglen = MAX(vd->max_msglen, max_inband_msglen); 2659d10e4ef2Snarayan 2660d10e4ef2Snarayan /* 2661d10e4ef2Snarayan * Initialize the data structure for processing in-band I/O 2662d10e4ef2Snarayan * request descriptors 2663d10e4ef2Snarayan */ 2664d10e4ef2Snarayan vd->inband_task.vd = vd; 26653af08d82Slm66018 vd->inband_task.msg = kmem_alloc(vd->max_msglen, KM_SLEEP); 2666d10e4ef2Snarayan vd->inband_task.index = 0; 2667d10e4ef2Snarayan vd->inband_task.type = VD_FINAL_RANGE_TASK; /* range == 1 */ 26681ae08745Sheppo } 26691ae08745Sheppo 2670e1ebb9ecSlm66018 /* Return the device's block size and max transfer size to the client */ 2671e1ebb9ecSlm66018 attr_msg->vdisk_block_size = DEV_BSIZE; 2672e1ebb9ecSlm66018 attr_msg->max_xfer_sz = vd->max_xfer_sz; 2673e1ebb9ecSlm66018 26741ae08745Sheppo attr_msg->vdisk_size = vd->vdisk_size; 26751ae08745Sheppo attr_msg->vdisk_type = vd->vdisk_type; 26761ae08745Sheppo attr_msg->operations = vds_operations; 26771ae08745Sheppo PR0("%s", VD_CLIENT(vd)); 26783af08d82Slm66018 26793af08d82Slm66018 ASSERT(vd->dring_task == NULL); 26803af08d82Slm66018 26811ae08745Sheppo return (0); 26821ae08745Sheppo } 26831ae08745Sheppo 26841ae08745Sheppo static int 26851ae08745Sheppo vd_process_dring_reg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 26861ae08745Sheppo { 26871ae08745Sheppo int status; 26881ae08745Sheppo size_t expected; 26891ae08745Sheppo ldc_mem_info_t dring_minfo; 26901ae08745Sheppo vio_dring_reg_msg_t *reg_msg = (vio_dring_reg_msg_t *)msg; 26911ae08745Sheppo 26921ae08745Sheppo 26931ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 26941ae08745Sheppo 26951ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 26961ae08745Sheppo VIO_DRING_REG)) { 2697d10e4ef2Snarayan PR0("Message is not a register-dring message"); 2698d10e4ef2Snarayan return (ENOMSG); 26991ae08745Sheppo } 27001ae08745Sheppo 27011ae08745Sheppo if (msglen < sizeof (*reg_msg)) { 27023af08d82Slm66018 PR0("Expected at least %lu-byte register-dring message; " 27031ae08745Sheppo "received %lu bytes", sizeof (*reg_msg), msglen); 27041ae08745Sheppo return (EBADMSG); 27051ae08745Sheppo } 27061ae08745Sheppo 27071ae08745Sheppo expected = sizeof (*reg_msg) + 27081ae08745Sheppo (reg_msg->ncookies - 1)*(sizeof (reg_msg->cookie[0])); 27091ae08745Sheppo if (msglen != expected) { 27103af08d82Slm66018 PR0("Expected %lu-byte register-dring message; " 27111ae08745Sheppo "received %lu bytes", expected, msglen); 27121ae08745Sheppo return (EBADMSG); 27131ae08745Sheppo } 27141ae08745Sheppo 27151ae08745Sheppo if (vd->initialized & VD_DRING) { 27163af08d82Slm66018 PR0("A dring was previously registered; only support one"); 27171ae08745Sheppo return (EBADMSG); 27181ae08745Sheppo } 27191ae08745Sheppo 2720d10e4ef2Snarayan if (reg_msg->num_descriptors > INT32_MAX) { 27213af08d82Slm66018 PR0("reg_msg->num_descriptors = %u; must be <= %u (%s)", 2722d10e4ef2Snarayan reg_msg->ncookies, INT32_MAX, STRINGIZE(INT32_MAX)); 2723d10e4ef2Snarayan return (EBADMSG); 2724d10e4ef2Snarayan } 2725d10e4ef2Snarayan 27261ae08745Sheppo if (reg_msg->ncookies != 1) { 27271ae08745Sheppo /* 27281ae08745Sheppo * In addition to fixing the assertion in the success case 27291ae08745Sheppo * below, supporting drings which require more than one 27301ae08745Sheppo * "cookie" requires increasing the value of vd->max_msglen 27311ae08745Sheppo * somewhere in the code path prior to receiving the message 27321ae08745Sheppo * which results in calling this function. Note that without 27331ae08745Sheppo * making this change, the larger message size required to 27341ae08745Sheppo * accommodate multiple cookies cannot be successfully 27351ae08745Sheppo * received, so this function will not even get called. 27361ae08745Sheppo * Gracefully accommodating more dring cookies might 27371ae08745Sheppo * reasonably demand exchanging an additional attribute or 27381ae08745Sheppo * making a minor protocol adjustment 27391ae08745Sheppo */ 27403af08d82Slm66018 PR0("reg_msg->ncookies = %u != 1", reg_msg->ncookies); 27411ae08745Sheppo return (EBADMSG); 27421ae08745Sheppo } 27431ae08745Sheppo 27441ae08745Sheppo status = ldc_mem_dring_map(vd->ldc_handle, reg_msg->cookie, 27451ae08745Sheppo reg_msg->ncookies, reg_msg->num_descriptors, 27464bac2208Snarayan reg_msg->descriptor_size, LDC_DIRECT_MAP, &vd->dring_handle); 27471ae08745Sheppo if (status != 0) { 27483af08d82Slm66018 PR0("ldc_mem_dring_map() returned errno %d", status); 27491ae08745Sheppo return (status); 27501ae08745Sheppo } 27511ae08745Sheppo 27521ae08745Sheppo /* 27531ae08745Sheppo * To remove the need for this assertion, must call 27541ae08745Sheppo * ldc_mem_dring_nextcookie() successfully ncookies-1 times after a 27551ae08745Sheppo * successful call to ldc_mem_dring_map() 27561ae08745Sheppo */ 27571ae08745Sheppo ASSERT(reg_msg->ncookies == 1); 27581ae08745Sheppo 27591ae08745Sheppo if ((status = 27601ae08745Sheppo ldc_mem_dring_info(vd->dring_handle, &dring_minfo)) != 0) { 27613af08d82Slm66018 PR0("ldc_mem_dring_info() returned errno %d", status); 27621ae08745Sheppo if ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0) 27633af08d82Slm66018 PR0("ldc_mem_dring_unmap() returned errno %d", status); 27641ae08745Sheppo return (status); 27651ae08745Sheppo } 27661ae08745Sheppo 27671ae08745Sheppo if (dring_minfo.vaddr == NULL) { 27683af08d82Slm66018 PR0("Descriptor ring virtual address is NULL"); 27690a55fbb7Slm66018 return (ENXIO); 27701ae08745Sheppo } 27711ae08745Sheppo 27721ae08745Sheppo 2773d10e4ef2Snarayan /* Initialize for valid message and mapped dring */ 27741ae08745Sheppo PR1("descriptor size = %u, dring length = %u", 27751ae08745Sheppo vd->descriptor_size, vd->dring_len); 27761ae08745Sheppo vd->initialized |= VD_DRING; 27771ae08745Sheppo vd->dring_ident = 1; /* "There Can Be Only One" */ 27781ae08745Sheppo vd->dring = dring_minfo.vaddr; 27791ae08745Sheppo vd->descriptor_size = reg_msg->descriptor_size; 27801ae08745Sheppo vd->dring_len = reg_msg->num_descriptors; 27811ae08745Sheppo reg_msg->dring_ident = vd->dring_ident; 2782d10e4ef2Snarayan 2783d10e4ef2Snarayan /* 2784d10e4ef2Snarayan * Allocate and initialize a "shadow" array of data structures for 2785d10e4ef2Snarayan * tasks to process I/O requests in dring elements 2786d10e4ef2Snarayan */ 2787d10e4ef2Snarayan vd->dring_task = 2788d10e4ef2Snarayan kmem_zalloc((sizeof (*vd->dring_task)) * vd->dring_len, KM_SLEEP); 2789d10e4ef2Snarayan for (int i = 0; i < vd->dring_len; i++) { 2790d10e4ef2Snarayan vd->dring_task[i].vd = vd; 2791d10e4ef2Snarayan vd->dring_task[i].index = i; 2792d10e4ef2Snarayan vd->dring_task[i].request = &VD_DRING_ELEM(i)->payload; 27934bac2208Snarayan 27944bac2208Snarayan status = ldc_mem_alloc_handle(vd->ldc_handle, 27954bac2208Snarayan &(vd->dring_task[i].mhdl)); 27964bac2208Snarayan if (status) { 27973af08d82Slm66018 PR0("ldc_mem_alloc_handle() returned err %d ", status); 27984bac2208Snarayan return (ENXIO); 27994bac2208Snarayan } 28003af08d82Slm66018 28013af08d82Slm66018 vd->dring_task[i].msg = kmem_alloc(vd->max_msglen, KM_SLEEP); 2802d10e4ef2Snarayan } 2803d10e4ef2Snarayan 28041ae08745Sheppo return (0); 28051ae08745Sheppo } 28061ae08745Sheppo 28071ae08745Sheppo static int 28081ae08745Sheppo vd_process_dring_unreg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 28091ae08745Sheppo { 28101ae08745Sheppo vio_dring_unreg_msg_t *unreg_msg = (vio_dring_unreg_msg_t *)msg; 28111ae08745Sheppo 28121ae08745Sheppo 28131ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 28141ae08745Sheppo 28151ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 28161ae08745Sheppo VIO_DRING_UNREG)) { 2817d10e4ef2Snarayan PR0("Message is not an unregister-dring message"); 2818d10e4ef2Snarayan return (ENOMSG); 28191ae08745Sheppo } 28201ae08745Sheppo 28211ae08745Sheppo if (msglen != sizeof (*unreg_msg)) { 28223af08d82Slm66018 PR0("Expected %lu-byte unregister-dring message; " 28231ae08745Sheppo "received %lu bytes", sizeof (*unreg_msg), msglen); 28241ae08745Sheppo return (EBADMSG); 28251ae08745Sheppo } 28261ae08745Sheppo 28271ae08745Sheppo if (unreg_msg->dring_ident != vd->dring_ident) { 28283af08d82Slm66018 PR0("Expected dring ident %lu; received %lu", 28291ae08745Sheppo vd->dring_ident, unreg_msg->dring_ident); 28301ae08745Sheppo return (EBADMSG); 28311ae08745Sheppo } 28321ae08745Sheppo 28331ae08745Sheppo return (0); 28341ae08745Sheppo } 28351ae08745Sheppo 28361ae08745Sheppo static int 28371ae08745Sheppo process_rdx_msg(vio_msg_t *msg, size_t msglen) 28381ae08745Sheppo { 28391ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 28401ae08745Sheppo 2841d10e4ef2Snarayan if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX)) { 2842d10e4ef2Snarayan PR0("Message is not an RDX message"); 2843d10e4ef2Snarayan return (ENOMSG); 2844d10e4ef2Snarayan } 28451ae08745Sheppo 28461ae08745Sheppo if (msglen != sizeof (vio_rdx_msg_t)) { 28473af08d82Slm66018 PR0("Expected %lu-byte RDX message; received %lu bytes", 28481ae08745Sheppo sizeof (vio_rdx_msg_t), msglen); 28491ae08745Sheppo return (EBADMSG); 28501ae08745Sheppo } 28511ae08745Sheppo 2852d10e4ef2Snarayan PR0("Valid RDX message"); 28531ae08745Sheppo return (0); 28541ae08745Sheppo } 28551ae08745Sheppo 28561ae08745Sheppo static int 28571ae08745Sheppo vd_check_seq_num(vd_t *vd, uint64_t seq_num) 28581ae08745Sheppo { 28591ae08745Sheppo if ((vd->initialized & VD_SEQ_NUM) && (seq_num != vd->seq_num + 1)) { 28603af08d82Slm66018 PR0("Received seq_num %lu; expected %lu", 28611ae08745Sheppo seq_num, (vd->seq_num + 1)); 28623af08d82Slm66018 PR0("initiating soft reset"); 2863d10e4ef2Snarayan vd_need_reset(vd, B_FALSE); 28641ae08745Sheppo return (1); 28651ae08745Sheppo } 28661ae08745Sheppo 28671ae08745Sheppo vd->seq_num = seq_num; 28681ae08745Sheppo vd->initialized |= VD_SEQ_NUM; /* superfluous after first time... */ 28691ae08745Sheppo return (0); 28701ae08745Sheppo } 28711ae08745Sheppo 28721ae08745Sheppo /* 28731ae08745Sheppo * Return the expected size of an inband-descriptor message with all the 28741ae08745Sheppo * cookies it claims to include 28751ae08745Sheppo */ 28761ae08745Sheppo static size_t 28771ae08745Sheppo expected_inband_size(vd_dring_inband_msg_t *msg) 28781ae08745Sheppo { 28791ae08745Sheppo return ((sizeof (*msg)) + 28801ae08745Sheppo (msg->payload.ncookies - 1)*(sizeof (msg->payload.cookie[0]))); 28811ae08745Sheppo } 28821ae08745Sheppo 28831ae08745Sheppo /* 28841ae08745Sheppo * Process an in-band descriptor message: used with clients like OBP, with 28851ae08745Sheppo * which vds exchanges descriptors within VIO message payloads, rather than 28861ae08745Sheppo * operating on them within a descriptor ring 28871ae08745Sheppo */ 28881ae08745Sheppo static int 28893af08d82Slm66018 vd_process_desc_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 28901ae08745Sheppo { 28911ae08745Sheppo size_t expected; 28921ae08745Sheppo vd_dring_inband_msg_t *desc_msg = (vd_dring_inband_msg_t *)msg; 28931ae08745Sheppo 28941ae08745Sheppo 28951ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 28961ae08745Sheppo 28971ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO, 2898d10e4ef2Snarayan VIO_DESC_DATA)) { 2899d10e4ef2Snarayan PR1("Message is not an in-band-descriptor message"); 2900d10e4ef2Snarayan return (ENOMSG); 2901d10e4ef2Snarayan } 29021ae08745Sheppo 29031ae08745Sheppo if (msglen < sizeof (*desc_msg)) { 29043af08d82Slm66018 PR0("Expected at least %lu-byte descriptor message; " 29051ae08745Sheppo "received %lu bytes", sizeof (*desc_msg), msglen); 29061ae08745Sheppo return (EBADMSG); 29071ae08745Sheppo } 29081ae08745Sheppo 29091ae08745Sheppo if (msglen != (expected = expected_inband_size(desc_msg))) { 29103af08d82Slm66018 PR0("Expected %lu-byte descriptor message; " 29111ae08745Sheppo "received %lu bytes", expected, msglen); 29121ae08745Sheppo return (EBADMSG); 29131ae08745Sheppo } 29141ae08745Sheppo 2915d10e4ef2Snarayan if (vd_check_seq_num(vd, desc_msg->hdr.seq_num) != 0) 29161ae08745Sheppo return (EBADMSG); 29171ae08745Sheppo 2918d10e4ef2Snarayan /* 2919d10e4ef2Snarayan * Valid message: Set up the in-band descriptor task and process the 2920d10e4ef2Snarayan * request. Arrange to acknowledge the client's message, unless an 2921d10e4ef2Snarayan * error processing the descriptor task results in setting 2922d10e4ef2Snarayan * VIO_SUBTYPE_NACK 2923d10e4ef2Snarayan */ 2924d10e4ef2Snarayan PR1("Valid in-band-descriptor message"); 2925d10e4ef2Snarayan msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 29263af08d82Slm66018 29273af08d82Slm66018 ASSERT(vd->inband_task.msg != NULL); 29283af08d82Slm66018 29293af08d82Slm66018 bcopy(msg, vd->inband_task.msg, msglen); 2930d10e4ef2Snarayan vd->inband_task.msglen = msglen; 29313af08d82Slm66018 29323af08d82Slm66018 /* 29333af08d82Slm66018 * The task request is now the payload of the message 29343af08d82Slm66018 * that was just copied into the body of the task. 29353af08d82Slm66018 */ 29363af08d82Slm66018 desc_msg = (vd_dring_inband_msg_t *)vd->inband_task.msg; 2937d10e4ef2Snarayan vd->inband_task.request = &desc_msg->payload; 29383af08d82Slm66018 2939d10e4ef2Snarayan return (vd_process_task(&vd->inband_task)); 29401ae08745Sheppo } 29411ae08745Sheppo 29421ae08745Sheppo static int 2943d10e4ef2Snarayan vd_process_element(vd_t *vd, vd_task_type_t type, uint32_t idx, 29443af08d82Slm66018 vio_msg_t *msg, size_t msglen) 29451ae08745Sheppo { 29461ae08745Sheppo int status; 2947d10e4ef2Snarayan boolean_t ready; 2948d10e4ef2Snarayan vd_dring_entry_t *elem = VD_DRING_ELEM(idx); 29491ae08745Sheppo 29501ae08745Sheppo 2951d10e4ef2Snarayan /* Accept the updated dring element */ 2952d10e4ef2Snarayan if ((status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) { 29533af08d82Slm66018 PR0("ldc_mem_dring_acquire() returned errno %d", status); 29541ae08745Sheppo return (status); 29551ae08745Sheppo } 2956d10e4ef2Snarayan ready = (elem->hdr.dstate == VIO_DESC_READY); 2957d10e4ef2Snarayan if (ready) { 2958d10e4ef2Snarayan elem->hdr.dstate = VIO_DESC_ACCEPTED; 2959d10e4ef2Snarayan } else { 29603af08d82Slm66018 PR0("descriptor %u not ready", idx); 2961d10e4ef2Snarayan VD_DUMP_DRING_ELEM(elem); 2962d10e4ef2Snarayan } 2963d10e4ef2Snarayan if ((status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) { 29643af08d82Slm66018 PR0("ldc_mem_dring_release() returned errno %d", status); 29651ae08745Sheppo return (status); 29661ae08745Sheppo } 2967d10e4ef2Snarayan if (!ready) 2968d10e4ef2Snarayan return (EBUSY); 29691ae08745Sheppo 29701ae08745Sheppo 2971d10e4ef2Snarayan /* Initialize a task and process the accepted element */ 2972d10e4ef2Snarayan PR1("Processing dring element %u", idx); 2973d10e4ef2Snarayan vd->dring_task[idx].type = type; 29743af08d82Slm66018 29753af08d82Slm66018 /* duplicate msg buf for cookies etc. */ 29763af08d82Slm66018 bcopy(msg, vd->dring_task[idx].msg, msglen); 29773af08d82Slm66018 2978d10e4ef2Snarayan vd->dring_task[idx].msglen = msglen; 2979205eeb1aSlm66018 return (vd_process_task(&vd->dring_task[idx])); 29801ae08745Sheppo } 29811ae08745Sheppo 29821ae08745Sheppo static int 2983d10e4ef2Snarayan vd_process_element_range(vd_t *vd, int start, int end, 29843af08d82Slm66018 vio_msg_t *msg, size_t msglen) 2985d10e4ef2Snarayan { 2986d10e4ef2Snarayan int i, n, nelem, status = 0; 2987d10e4ef2Snarayan boolean_t inprogress = B_FALSE; 2988d10e4ef2Snarayan vd_task_type_t type; 2989d10e4ef2Snarayan 2990d10e4ef2Snarayan 2991d10e4ef2Snarayan ASSERT(start >= 0); 2992d10e4ef2Snarayan ASSERT(end >= 0); 2993d10e4ef2Snarayan 2994d10e4ef2Snarayan /* 2995d10e4ef2Snarayan * Arrange to acknowledge the client's message, unless an error 2996d10e4ef2Snarayan * processing one of the dring elements results in setting 2997d10e4ef2Snarayan * VIO_SUBTYPE_NACK 2998d10e4ef2Snarayan */ 2999d10e4ef2Snarayan msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 3000d10e4ef2Snarayan 3001d10e4ef2Snarayan /* 3002d10e4ef2Snarayan * Process the dring elements in the range 3003d10e4ef2Snarayan */ 3004d10e4ef2Snarayan nelem = ((end < start) ? end + vd->dring_len : end) - start + 1; 3005d10e4ef2Snarayan for (i = start, n = nelem; n > 0; i = (i + 1) % vd->dring_len, n--) { 3006d10e4ef2Snarayan ((vio_dring_msg_t *)msg)->end_idx = i; 3007d10e4ef2Snarayan type = (n == 1) ? VD_FINAL_RANGE_TASK : VD_NONFINAL_RANGE_TASK; 30083af08d82Slm66018 status = vd_process_element(vd, type, i, msg, msglen); 3009d10e4ef2Snarayan if (status == EINPROGRESS) 3010d10e4ef2Snarayan inprogress = B_TRUE; 3011d10e4ef2Snarayan else if (status != 0) 3012d10e4ef2Snarayan break; 3013d10e4ef2Snarayan } 3014d10e4ef2Snarayan 3015d10e4ef2Snarayan /* 3016d10e4ef2Snarayan * If some, but not all, operations of a multi-element range are in 3017d10e4ef2Snarayan * progress, wait for other operations to complete before returning 3018d10e4ef2Snarayan * (which will result in "ack" or "nack" of the message). Note that 3019d10e4ef2Snarayan * all outstanding operations will need to complete, not just the ones 3020d10e4ef2Snarayan * corresponding to the current range of dring elements; howevever, as 3021d10e4ef2Snarayan * this situation is an error case, performance is less critical. 3022d10e4ef2Snarayan */ 3023d10e4ef2Snarayan if ((nelem > 1) && (status != EINPROGRESS) && inprogress) 3024d10e4ef2Snarayan ddi_taskq_wait(vd->completionq); 3025d10e4ef2Snarayan 3026d10e4ef2Snarayan return (status); 3027d10e4ef2Snarayan } 3028d10e4ef2Snarayan 3029d10e4ef2Snarayan static int 30303af08d82Slm66018 vd_process_dring_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 30311ae08745Sheppo { 30321ae08745Sheppo vio_dring_msg_t *dring_msg = (vio_dring_msg_t *)msg; 30331ae08745Sheppo 30341ae08745Sheppo 30351ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 30361ae08745Sheppo 30371ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO, 30381ae08745Sheppo VIO_DRING_DATA)) { 3039d10e4ef2Snarayan PR1("Message is not a dring-data message"); 3040d10e4ef2Snarayan return (ENOMSG); 30411ae08745Sheppo } 30421ae08745Sheppo 30431ae08745Sheppo if (msglen != sizeof (*dring_msg)) { 30443af08d82Slm66018 PR0("Expected %lu-byte dring message; received %lu bytes", 30451ae08745Sheppo sizeof (*dring_msg), msglen); 30461ae08745Sheppo return (EBADMSG); 30471ae08745Sheppo } 30481ae08745Sheppo 3049d10e4ef2Snarayan if (vd_check_seq_num(vd, dring_msg->seq_num) != 0) 30501ae08745Sheppo return (EBADMSG); 30511ae08745Sheppo 30521ae08745Sheppo if (dring_msg->dring_ident != vd->dring_ident) { 30533af08d82Slm66018 PR0("Expected dring ident %lu; received ident %lu", 30541ae08745Sheppo vd->dring_ident, dring_msg->dring_ident); 30551ae08745Sheppo return (EBADMSG); 30561ae08745Sheppo } 30571ae08745Sheppo 3058d10e4ef2Snarayan if (dring_msg->start_idx >= vd->dring_len) { 30593af08d82Slm66018 PR0("\"start_idx\" = %u; must be less than %u", 3060d10e4ef2Snarayan dring_msg->start_idx, vd->dring_len); 3061d10e4ef2Snarayan return (EBADMSG); 3062d10e4ef2Snarayan } 30631ae08745Sheppo 3064d10e4ef2Snarayan if ((dring_msg->end_idx < 0) || 3065d10e4ef2Snarayan (dring_msg->end_idx >= vd->dring_len)) { 30663af08d82Slm66018 PR0("\"end_idx\" = %u; must be >= 0 and less than %u", 3067d10e4ef2Snarayan dring_msg->end_idx, vd->dring_len); 3068d10e4ef2Snarayan return (EBADMSG); 3069d10e4ef2Snarayan } 3070d10e4ef2Snarayan 3071d10e4ef2Snarayan /* Valid message; process range of updated dring elements */ 3072d10e4ef2Snarayan PR1("Processing descriptor range, start = %u, end = %u", 3073d10e4ef2Snarayan dring_msg->start_idx, dring_msg->end_idx); 3074d10e4ef2Snarayan return (vd_process_element_range(vd, dring_msg->start_idx, 30753af08d82Slm66018 dring_msg->end_idx, msg, msglen)); 30761ae08745Sheppo } 30771ae08745Sheppo 30781ae08745Sheppo static int 30791ae08745Sheppo recv_msg(ldc_handle_t ldc_handle, void *msg, size_t *nbytes) 30801ae08745Sheppo { 30811ae08745Sheppo int retry, status; 30821ae08745Sheppo size_t size = *nbytes; 30831ae08745Sheppo 30841ae08745Sheppo 30851ae08745Sheppo for (retry = 0, status = ETIMEDOUT; 30861ae08745Sheppo retry < vds_ldc_retries && status == ETIMEDOUT; 30871ae08745Sheppo retry++) { 30881ae08745Sheppo PR1("ldc_read() attempt %d", (retry + 1)); 30891ae08745Sheppo *nbytes = size; 30901ae08745Sheppo status = ldc_read(ldc_handle, msg, nbytes); 30911ae08745Sheppo } 30921ae08745Sheppo 30933af08d82Slm66018 if (status) { 30943af08d82Slm66018 PR0("ldc_read() returned errno %d", status); 30953af08d82Slm66018 if (status != ECONNRESET) 30963af08d82Slm66018 return (ENOMSG); 30971ae08745Sheppo return (status); 30981ae08745Sheppo } else if (*nbytes == 0) { 30991ae08745Sheppo PR1("ldc_read() returned 0 and no message read"); 31001ae08745Sheppo return (ENOMSG); 31011ae08745Sheppo } 31021ae08745Sheppo 31031ae08745Sheppo PR1("RCVD %lu-byte message", *nbytes); 31041ae08745Sheppo return (0); 31051ae08745Sheppo } 31061ae08745Sheppo 31071ae08745Sheppo static int 31083af08d82Slm66018 vd_do_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 31091ae08745Sheppo { 31101ae08745Sheppo int status; 31111ae08745Sheppo 31121ae08745Sheppo 31131ae08745Sheppo PR1("Processing (%x/%x/%x) message", msg->tag.vio_msgtype, 31141ae08745Sheppo msg->tag.vio_subtype, msg->tag.vio_subtype_env); 31153af08d82Slm66018 #ifdef DEBUG 31163af08d82Slm66018 vd_decode_tag(msg); 31173af08d82Slm66018 #endif 31181ae08745Sheppo 31191ae08745Sheppo /* 31201ae08745Sheppo * Validate session ID up front, since it applies to all messages 31211ae08745Sheppo * once set 31221ae08745Sheppo */ 31231ae08745Sheppo if ((msg->tag.vio_sid != vd->sid) && (vd->initialized & VD_SID)) { 31243af08d82Slm66018 PR0("Expected SID %u, received %u", vd->sid, 31251ae08745Sheppo msg->tag.vio_sid); 31261ae08745Sheppo return (EBADMSG); 31271ae08745Sheppo } 31281ae08745Sheppo 31293af08d82Slm66018 PR1("\tWhile in state %d (%s)", vd->state, vd_decode_state(vd->state)); 31301ae08745Sheppo 31311ae08745Sheppo /* 31321ae08745Sheppo * Process the received message based on connection state 31331ae08745Sheppo */ 31341ae08745Sheppo switch (vd->state) { 31351ae08745Sheppo case VD_STATE_INIT: /* expect version message */ 31360a55fbb7Slm66018 if ((status = vd_process_ver_msg(vd, msg, msglen)) != 0) 31371ae08745Sheppo return (status); 31381ae08745Sheppo 31391ae08745Sheppo /* Version negotiated, move to that state */ 31401ae08745Sheppo vd->state = VD_STATE_VER; 31411ae08745Sheppo return (0); 31421ae08745Sheppo 31431ae08745Sheppo case VD_STATE_VER: /* expect attribute message */ 31441ae08745Sheppo if ((status = vd_process_attr_msg(vd, msg, msglen)) != 0) 31451ae08745Sheppo return (status); 31461ae08745Sheppo 31471ae08745Sheppo /* Attributes exchanged, move to that state */ 31481ae08745Sheppo vd->state = VD_STATE_ATTR; 31491ae08745Sheppo return (0); 31501ae08745Sheppo 31511ae08745Sheppo case VD_STATE_ATTR: 31521ae08745Sheppo switch (vd->xfer_mode) { 31531ae08745Sheppo case VIO_DESC_MODE: /* expect RDX message */ 31541ae08745Sheppo if ((status = process_rdx_msg(msg, msglen)) != 0) 31551ae08745Sheppo return (status); 31561ae08745Sheppo 31571ae08745Sheppo /* Ready to receive in-band descriptors */ 31581ae08745Sheppo vd->state = VD_STATE_DATA; 31591ae08745Sheppo return (0); 31601ae08745Sheppo 31611ae08745Sheppo case VIO_DRING_MODE: /* expect register-dring message */ 31621ae08745Sheppo if ((status = 31631ae08745Sheppo vd_process_dring_reg_msg(vd, msg, msglen)) != 0) 31641ae08745Sheppo return (status); 31651ae08745Sheppo 31661ae08745Sheppo /* One dring negotiated, move to that state */ 31671ae08745Sheppo vd->state = VD_STATE_DRING; 31681ae08745Sheppo return (0); 31691ae08745Sheppo 31701ae08745Sheppo default: 31711ae08745Sheppo ASSERT("Unsupported transfer mode"); 31723af08d82Slm66018 PR0("Unsupported transfer mode"); 31731ae08745Sheppo return (ENOTSUP); 31741ae08745Sheppo } 31751ae08745Sheppo 31761ae08745Sheppo case VD_STATE_DRING: /* expect RDX, register-dring, or unreg-dring */ 31771ae08745Sheppo if ((status = process_rdx_msg(msg, msglen)) == 0) { 31781ae08745Sheppo /* Ready to receive data */ 31791ae08745Sheppo vd->state = VD_STATE_DATA; 31801ae08745Sheppo return (0); 31811ae08745Sheppo } else if (status != ENOMSG) { 31821ae08745Sheppo return (status); 31831ae08745Sheppo } 31841ae08745Sheppo 31851ae08745Sheppo 31861ae08745Sheppo /* 31871ae08745Sheppo * If another register-dring message is received, stay in 31881ae08745Sheppo * dring state in case the client sends RDX; although the 31891ae08745Sheppo * protocol allows multiple drings, this server does not 31901ae08745Sheppo * support using more than one 31911ae08745Sheppo */ 31921ae08745Sheppo if ((status = 31931ae08745Sheppo vd_process_dring_reg_msg(vd, msg, msglen)) != ENOMSG) 31941ae08745Sheppo return (status); 31951ae08745Sheppo 31961ae08745Sheppo /* 31971ae08745Sheppo * Acknowledge an unregister-dring message, but reset the 31981ae08745Sheppo * connection anyway: Although the protocol allows 31991ae08745Sheppo * unregistering drings, this server cannot serve a vdisk 32001ae08745Sheppo * without its only dring 32011ae08745Sheppo */ 32021ae08745Sheppo status = vd_process_dring_unreg_msg(vd, msg, msglen); 32031ae08745Sheppo return ((status == 0) ? ENOTSUP : status); 32041ae08745Sheppo 32051ae08745Sheppo case VD_STATE_DATA: 32061ae08745Sheppo switch (vd->xfer_mode) { 32071ae08745Sheppo case VIO_DESC_MODE: /* expect in-band-descriptor message */ 32083af08d82Slm66018 return (vd_process_desc_msg(vd, msg, msglen)); 32091ae08745Sheppo 32101ae08745Sheppo case VIO_DRING_MODE: /* expect dring-data or unreg-dring */ 32111ae08745Sheppo /* 32121ae08745Sheppo * Typically expect dring-data messages, so handle 32131ae08745Sheppo * them first 32141ae08745Sheppo */ 32151ae08745Sheppo if ((status = vd_process_dring_msg(vd, msg, 32163af08d82Slm66018 msglen)) != ENOMSG) 32171ae08745Sheppo return (status); 32181ae08745Sheppo 32191ae08745Sheppo /* 32201ae08745Sheppo * Acknowledge an unregister-dring message, but reset 32211ae08745Sheppo * the connection anyway: Although the protocol 32221ae08745Sheppo * allows unregistering drings, this server cannot 32231ae08745Sheppo * serve a vdisk without its only dring 32241ae08745Sheppo */ 32251ae08745Sheppo status = vd_process_dring_unreg_msg(vd, msg, msglen); 32261ae08745Sheppo return ((status == 0) ? ENOTSUP : status); 32271ae08745Sheppo 32281ae08745Sheppo default: 32291ae08745Sheppo ASSERT("Unsupported transfer mode"); 32303af08d82Slm66018 PR0("Unsupported transfer mode"); 32311ae08745Sheppo return (ENOTSUP); 32321ae08745Sheppo } 32331ae08745Sheppo 32341ae08745Sheppo default: 32351ae08745Sheppo ASSERT("Invalid client connection state"); 32363af08d82Slm66018 PR0("Invalid client connection state"); 32371ae08745Sheppo return (ENOTSUP); 32381ae08745Sheppo } 32391ae08745Sheppo } 32401ae08745Sheppo 3241d10e4ef2Snarayan static int 32423af08d82Slm66018 vd_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 32431ae08745Sheppo { 32441ae08745Sheppo int status; 32451ae08745Sheppo boolean_t reset_ldc = B_FALSE; 3246205eeb1aSlm66018 vd_task_t task; 32471ae08745Sheppo 32481ae08745Sheppo /* 32491ae08745Sheppo * Check that the message is at least big enough for a "tag", so that 32501ae08745Sheppo * message processing can proceed based on tag-specified message type 32511ae08745Sheppo */ 32521ae08745Sheppo if (msglen < sizeof (vio_msg_tag_t)) { 32533af08d82Slm66018 PR0("Received short (%lu-byte) message", msglen); 32541ae08745Sheppo /* Can't "nack" short message, so drop the big hammer */ 32553af08d82Slm66018 PR0("initiating full reset"); 3256d10e4ef2Snarayan vd_need_reset(vd, B_TRUE); 3257d10e4ef2Snarayan return (EBADMSG); 32581ae08745Sheppo } 32591ae08745Sheppo 32601ae08745Sheppo /* 32611ae08745Sheppo * Process the message 32621ae08745Sheppo */ 32633af08d82Slm66018 switch (status = vd_do_process_msg(vd, msg, msglen)) { 32641ae08745Sheppo case 0: 32651ae08745Sheppo /* "ack" valid, successfully-processed messages */ 32661ae08745Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 32671ae08745Sheppo break; 32681ae08745Sheppo 3269d10e4ef2Snarayan case EINPROGRESS: 3270d10e4ef2Snarayan /* The completion handler will "ack" or "nack" the message */ 3271d10e4ef2Snarayan return (EINPROGRESS); 32721ae08745Sheppo case ENOMSG: 32733af08d82Slm66018 PR0("Received unexpected message"); 32741ae08745Sheppo _NOTE(FALLTHROUGH); 32751ae08745Sheppo case EBADMSG: 32761ae08745Sheppo case ENOTSUP: 3277205eeb1aSlm66018 /* "transport" error will cause NACK of invalid messages */ 32781ae08745Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 32791ae08745Sheppo break; 32801ae08745Sheppo 32811ae08745Sheppo default: 3282205eeb1aSlm66018 /* "transport" error will cause NACK of invalid messages */ 32831ae08745Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 32841ae08745Sheppo /* An LDC error probably occurred, so try resetting it */ 32851ae08745Sheppo reset_ldc = B_TRUE; 32861ae08745Sheppo break; 32871ae08745Sheppo } 32881ae08745Sheppo 32893af08d82Slm66018 PR1("\tResulting in state %d (%s)", vd->state, 32903af08d82Slm66018 vd_decode_state(vd->state)); 32913af08d82Slm66018 3292205eeb1aSlm66018 /* populate the task so we can dispatch it on the taskq */ 3293205eeb1aSlm66018 task.vd = vd; 3294205eeb1aSlm66018 task.msg = msg; 3295205eeb1aSlm66018 task.msglen = msglen; 3296205eeb1aSlm66018 3297205eeb1aSlm66018 /* 3298205eeb1aSlm66018 * Queue a task to send the notification that the operation completed. 3299205eeb1aSlm66018 * We need to ensure that requests are responded to in the correct 3300205eeb1aSlm66018 * order and since the taskq is processed serially this ordering 3301205eeb1aSlm66018 * is maintained. 3302205eeb1aSlm66018 */ 3303205eeb1aSlm66018 (void) ddi_taskq_dispatch(vd->completionq, vd_serial_notify, 3304205eeb1aSlm66018 &task, DDI_SLEEP); 3305205eeb1aSlm66018 3306205eeb1aSlm66018 /* 3307205eeb1aSlm66018 * To ensure handshake negotiations do not happen out of order, such 3308205eeb1aSlm66018 * requests that come through this path should not be done in parallel 3309205eeb1aSlm66018 * so we need to wait here until the response is sent to the client. 3310205eeb1aSlm66018 */ 3311205eeb1aSlm66018 ddi_taskq_wait(vd->completionq); 33121ae08745Sheppo 3313d10e4ef2Snarayan /* Arrange to reset the connection for nack'ed or failed messages */ 33143af08d82Slm66018 if ((status != 0) || reset_ldc) { 33153af08d82Slm66018 PR0("initiating %s reset", 33163af08d82Slm66018 (reset_ldc) ? "full" : "soft"); 3317d10e4ef2Snarayan vd_need_reset(vd, reset_ldc); 33183af08d82Slm66018 } 3319d10e4ef2Snarayan 3320d10e4ef2Snarayan return (status); 3321d10e4ef2Snarayan } 3322d10e4ef2Snarayan 3323d10e4ef2Snarayan static boolean_t 3324d10e4ef2Snarayan vd_enabled(vd_t *vd) 3325d10e4ef2Snarayan { 3326d10e4ef2Snarayan boolean_t enabled; 3327d10e4ef2Snarayan 3328d10e4ef2Snarayan mutex_enter(&vd->lock); 3329d10e4ef2Snarayan enabled = vd->enabled; 3330d10e4ef2Snarayan mutex_exit(&vd->lock); 3331d10e4ef2Snarayan return (enabled); 33321ae08745Sheppo } 33331ae08745Sheppo 33341ae08745Sheppo static void 33350a55fbb7Slm66018 vd_recv_msg(void *arg) 33361ae08745Sheppo { 33371ae08745Sheppo vd_t *vd = (vd_t *)arg; 33383af08d82Slm66018 int rv = 0, status = 0; 33391ae08745Sheppo 33401ae08745Sheppo ASSERT(vd != NULL); 33413af08d82Slm66018 3342d10e4ef2Snarayan PR2("New task to receive incoming message(s)"); 33433af08d82Slm66018 33443af08d82Slm66018 3345d10e4ef2Snarayan while (vd_enabled(vd) && status == 0) { 3346d10e4ef2Snarayan size_t msglen, msgsize; 33473af08d82Slm66018 ldc_status_t lstatus; 3348d10e4ef2Snarayan 33490a55fbb7Slm66018 /* 3350d10e4ef2Snarayan * Receive and process a message 33510a55fbb7Slm66018 */ 3352d10e4ef2Snarayan vd_reset_if_needed(vd); /* can change vd->max_msglen */ 33533af08d82Slm66018 33543af08d82Slm66018 /* 33553af08d82Slm66018 * check if channel is UP - else break out of loop 33563af08d82Slm66018 */ 33573af08d82Slm66018 status = ldc_status(vd->ldc_handle, &lstatus); 33583af08d82Slm66018 if (lstatus != LDC_UP) { 33593af08d82Slm66018 PR0("channel not up (status=%d), exiting recv loop\n", 33603af08d82Slm66018 lstatus); 33613af08d82Slm66018 break; 33623af08d82Slm66018 } 33633af08d82Slm66018 33643af08d82Slm66018 ASSERT(vd->max_msglen != 0); 33653af08d82Slm66018 3366d10e4ef2Snarayan msgsize = vd->max_msglen; /* stable copy for alloc/free */ 33673af08d82Slm66018 msglen = msgsize; /* actual len after recv_msg() */ 33683af08d82Slm66018 33693af08d82Slm66018 status = recv_msg(vd->ldc_handle, vd->vio_msgp, &msglen); 33703af08d82Slm66018 switch (status) { 33713af08d82Slm66018 case 0: 33723af08d82Slm66018 rv = vd_process_msg(vd, (vio_msg_t *)vd->vio_msgp, 33733af08d82Slm66018 msglen); 33743af08d82Slm66018 /* check if max_msglen changed */ 33753af08d82Slm66018 if (msgsize != vd->max_msglen) { 33763af08d82Slm66018 PR0("max_msglen changed 0x%lx to 0x%lx bytes\n", 33773af08d82Slm66018 msgsize, vd->max_msglen); 33783af08d82Slm66018 kmem_free(vd->vio_msgp, msgsize); 33793af08d82Slm66018 vd->vio_msgp = 33803af08d82Slm66018 kmem_alloc(vd->max_msglen, KM_SLEEP); 33813af08d82Slm66018 } 33823af08d82Slm66018 if (rv == EINPROGRESS) 33833af08d82Slm66018 continue; 33843af08d82Slm66018 break; 33853af08d82Slm66018 33863af08d82Slm66018 case ENOMSG: 33873af08d82Slm66018 break; 33883af08d82Slm66018 33893af08d82Slm66018 case ECONNRESET: 33903af08d82Slm66018 PR0("initiating soft reset (ECONNRESET)\n"); 33913af08d82Slm66018 vd_need_reset(vd, B_FALSE); 33923af08d82Slm66018 status = 0; 33933af08d82Slm66018 break; 33943af08d82Slm66018 33953af08d82Slm66018 default: 3396d10e4ef2Snarayan /* Probably an LDC failure; arrange to reset it */ 33973af08d82Slm66018 PR0("initiating full reset (status=0x%x)", status); 3398d10e4ef2Snarayan vd_need_reset(vd, B_TRUE); 33993af08d82Slm66018 break; 34000a55fbb7Slm66018 } 34011ae08745Sheppo } 34023af08d82Slm66018 3403d10e4ef2Snarayan PR2("Task finished"); 34040a55fbb7Slm66018 } 34050a55fbb7Slm66018 34060a55fbb7Slm66018 static uint_t 34071ae08745Sheppo vd_handle_ldc_events(uint64_t event, caddr_t arg) 34081ae08745Sheppo { 34091ae08745Sheppo vd_t *vd = (vd_t *)(void *)arg; 34103af08d82Slm66018 int status; 34111ae08745Sheppo 34121ae08745Sheppo ASSERT(vd != NULL); 3413d10e4ef2Snarayan 3414d10e4ef2Snarayan if (!vd_enabled(vd)) 3415d10e4ef2Snarayan return (LDC_SUCCESS); 3416d10e4ef2Snarayan 34173af08d82Slm66018 if (event & LDC_EVT_DOWN) { 341834683adeSsg70180 PR0("LDC_EVT_DOWN: LDC channel went down"); 34193af08d82Slm66018 34203af08d82Slm66018 vd_need_reset(vd, B_TRUE); 34213af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, 34223af08d82Slm66018 DDI_SLEEP); 34233af08d82Slm66018 if (status == DDI_FAILURE) { 34243af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 34253af08d82Slm66018 vd_need_reset(vd, B_TRUE); 34263af08d82Slm66018 } 34273af08d82Slm66018 } 34283af08d82Slm66018 3429d10e4ef2Snarayan if (event & LDC_EVT_RESET) { 34303af08d82Slm66018 PR0("LDC_EVT_RESET: LDC channel was reset"); 34313af08d82Slm66018 34323af08d82Slm66018 if (vd->state != VD_STATE_INIT) { 34333af08d82Slm66018 PR0("scheduling full reset"); 34343af08d82Slm66018 vd_need_reset(vd, B_FALSE); 34353af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, 34363af08d82Slm66018 vd, DDI_SLEEP); 34373af08d82Slm66018 if (status == DDI_FAILURE) { 34383af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 34393af08d82Slm66018 vd_need_reset(vd, B_TRUE); 34403af08d82Slm66018 } 34413af08d82Slm66018 34423af08d82Slm66018 } else { 34433af08d82Slm66018 PR0("channel already reset, ignoring...\n"); 34443af08d82Slm66018 PR0("doing ldc up...\n"); 34453af08d82Slm66018 (void) ldc_up(vd->ldc_handle); 34463af08d82Slm66018 } 34473af08d82Slm66018 3448d10e4ef2Snarayan return (LDC_SUCCESS); 3449d10e4ef2Snarayan } 3450d10e4ef2Snarayan 3451d10e4ef2Snarayan if (event & LDC_EVT_UP) { 34523af08d82Slm66018 PR0("EVT_UP: LDC is up\nResetting client connection state"); 34533af08d82Slm66018 PR0("initiating soft reset"); 3454d10e4ef2Snarayan vd_need_reset(vd, B_FALSE); 34553af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, 34563af08d82Slm66018 vd, DDI_SLEEP); 34573af08d82Slm66018 if (status == DDI_FAILURE) { 34583af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 34593af08d82Slm66018 vd_need_reset(vd, B_TRUE); 34603af08d82Slm66018 return (LDC_SUCCESS); 34613af08d82Slm66018 } 3462d10e4ef2Snarayan } 3463d10e4ef2Snarayan 3464d10e4ef2Snarayan if (event & LDC_EVT_READ) { 3465d10e4ef2Snarayan int status; 3466d10e4ef2Snarayan 3467d10e4ef2Snarayan PR1("New data available"); 3468d10e4ef2Snarayan /* Queue a task to receive the new data */ 3469d10e4ef2Snarayan status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, 3470d10e4ef2Snarayan DDI_SLEEP); 34713af08d82Slm66018 34723af08d82Slm66018 if (status == DDI_FAILURE) { 34733af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 34743af08d82Slm66018 vd_need_reset(vd, B_TRUE); 34753af08d82Slm66018 } 3476d10e4ef2Snarayan } 3477d10e4ef2Snarayan 3478d10e4ef2Snarayan return (LDC_SUCCESS); 34791ae08745Sheppo } 34801ae08745Sheppo 34811ae08745Sheppo static uint_t 34821ae08745Sheppo vds_check_for_vd(mod_hash_key_t key, mod_hash_val_t *val, void *arg) 34831ae08745Sheppo { 34841ae08745Sheppo _NOTE(ARGUNUSED(key, val)) 34851ae08745Sheppo (*((uint_t *)arg))++; 34861ae08745Sheppo return (MH_WALK_TERMINATE); 34871ae08745Sheppo } 34881ae08745Sheppo 34891ae08745Sheppo 34901ae08745Sheppo static int 34911ae08745Sheppo vds_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 34921ae08745Sheppo { 34931ae08745Sheppo uint_t vd_present = 0; 34941ae08745Sheppo minor_t instance; 34951ae08745Sheppo vds_t *vds; 34961ae08745Sheppo 34971ae08745Sheppo 34981ae08745Sheppo switch (cmd) { 34991ae08745Sheppo case DDI_DETACH: 35001ae08745Sheppo /* the real work happens below */ 35011ae08745Sheppo break; 35021ae08745Sheppo case DDI_SUSPEND: 3503d10e4ef2Snarayan PR0("No action required for DDI_SUSPEND"); 35041ae08745Sheppo return (DDI_SUCCESS); 35051ae08745Sheppo default: 35063af08d82Slm66018 PR0("Unrecognized \"cmd\""); 35071ae08745Sheppo return (DDI_FAILURE); 35081ae08745Sheppo } 35091ae08745Sheppo 35101ae08745Sheppo ASSERT(cmd == DDI_DETACH); 35111ae08745Sheppo instance = ddi_get_instance(dip); 35121ae08745Sheppo if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) { 35133af08d82Slm66018 PR0("Could not get state for instance %u", instance); 35141ae08745Sheppo ddi_soft_state_free(vds_state, instance); 35151ae08745Sheppo return (DDI_FAILURE); 35161ae08745Sheppo } 35171ae08745Sheppo 35181ae08745Sheppo /* Do no detach when serving any vdisks */ 35191ae08745Sheppo mod_hash_walk(vds->vd_table, vds_check_for_vd, &vd_present); 35201ae08745Sheppo if (vd_present) { 35211ae08745Sheppo PR0("Not detaching because serving vdisks"); 35221ae08745Sheppo return (DDI_FAILURE); 35231ae08745Sheppo } 35241ae08745Sheppo 35251ae08745Sheppo PR0("Detaching"); 3526445b4c2eSsb155480 if (vds->initialized & VDS_MDEG) { 35271ae08745Sheppo (void) mdeg_unregister(vds->mdeg); 3528445b4c2eSsb155480 kmem_free(vds->ispecp->specp, sizeof (vds_prop_template)); 3529445b4c2eSsb155480 kmem_free(vds->ispecp, sizeof (mdeg_node_spec_t)); 3530445b4c2eSsb155480 vds->ispecp = NULL; 3531445b4c2eSsb155480 vds->mdeg = NULL; 3532445b4c2eSsb155480 } 3533445b4c2eSsb155480 35341ae08745Sheppo if (vds->initialized & VDS_LDI) 35351ae08745Sheppo (void) ldi_ident_release(vds->ldi_ident); 35361ae08745Sheppo mod_hash_destroy_hash(vds->vd_table); 35371ae08745Sheppo ddi_soft_state_free(vds_state, instance); 35381ae08745Sheppo return (DDI_SUCCESS); 35391ae08745Sheppo } 35401ae08745Sheppo 35411ae08745Sheppo static boolean_t 35421ae08745Sheppo is_pseudo_device(dev_info_t *dip) 35431ae08745Sheppo { 35441ae08745Sheppo dev_info_t *parent, *root = ddi_root_node(); 35451ae08745Sheppo 35461ae08745Sheppo 35471ae08745Sheppo for (parent = ddi_get_parent(dip); (parent != NULL) && (parent != root); 35481ae08745Sheppo parent = ddi_get_parent(parent)) { 35491ae08745Sheppo if (strcmp(ddi_get_name(parent), DEVI_PSEUDO_NEXNAME) == 0) 35501ae08745Sheppo return (B_TRUE); 35511ae08745Sheppo } 35521ae08745Sheppo 35531ae08745Sheppo return (B_FALSE); 35541ae08745Sheppo } 35551ae08745Sheppo 35561ae08745Sheppo static int 35570a55fbb7Slm66018 vd_setup_full_disk(vd_t *vd) 35580a55fbb7Slm66018 { 35590a55fbb7Slm66018 int rval, status; 35600a55fbb7Slm66018 major_t major = getmajor(vd->dev[0]); 35610a55fbb7Slm66018 minor_t minor = getminor(vd->dev[0]) - VD_ENTIRE_DISK_SLICE; 35624bac2208Snarayan struct dk_minfo dk_minfo; 35630a55fbb7Slm66018 3564047ba61eSachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK); 3565047ba61eSachartre 35664bac2208Snarayan /* 35674bac2208Snarayan * At this point, vdisk_size is set to the size of partition 2 but 35684bac2208Snarayan * this does not represent the size of the disk because partition 2 35694bac2208Snarayan * may not cover the entire disk and its size does not include reserved 35704bac2208Snarayan * blocks. So we update vdisk_size to be the size of the entire disk. 35714bac2208Snarayan */ 35724bac2208Snarayan if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCGMEDIAINFO, 3573047ba61eSachartre (intptr_t)&dk_minfo, (vd->open_flags | FKIOCTL), 35744bac2208Snarayan kcred, &rval)) != 0) { 3575690555a1Sachartre PRN("ldi_ioctl(DKIOCGMEDIAINFO) returned errno %d", 35764bac2208Snarayan status); 35770a55fbb7Slm66018 return (status); 35780a55fbb7Slm66018 } 35794bac2208Snarayan vd->vdisk_size = dk_minfo.dki_capacity; 35800a55fbb7Slm66018 35810a55fbb7Slm66018 /* Move dev number and LDI handle to entire-disk-slice array elements */ 35820a55fbb7Slm66018 vd->dev[VD_ENTIRE_DISK_SLICE] = vd->dev[0]; 35830a55fbb7Slm66018 vd->dev[0] = 0; 35840a55fbb7Slm66018 vd->ldi_handle[VD_ENTIRE_DISK_SLICE] = vd->ldi_handle[0]; 35850a55fbb7Slm66018 vd->ldi_handle[0] = NULL; 35860a55fbb7Slm66018 35870a55fbb7Slm66018 /* Initialize device numbers for remaining slices and open them */ 35880a55fbb7Slm66018 for (int slice = 0; slice < vd->nslices; slice++) { 35890a55fbb7Slm66018 /* 35900a55fbb7Slm66018 * Skip the entire-disk slice, as it's already open and its 35910a55fbb7Slm66018 * device known 35920a55fbb7Slm66018 */ 35930a55fbb7Slm66018 if (slice == VD_ENTIRE_DISK_SLICE) 35940a55fbb7Slm66018 continue; 35950a55fbb7Slm66018 ASSERT(vd->dev[slice] == 0); 35960a55fbb7Slm66018 ASSERT(vd->ldi_handle[slice] == NULL); 35970a55fbb7Slm66018 35980a55fbb7Slm66018 /* 35990a55fbb7Slm66018 * Construct the device number for the current slice 36000a55fbb7Slm66018 */ 36010a55fbb7Slm66018 vd->dev[slice] = makedevice(major, (minor + slice)); 36020a55fbb7Slm66018 36030a55fbb7Slm66018 /* 360434683adeSsg70180 * Open all slices of the disk to serve them to the client. 360534683adeSsg70180 * Slices are opened exclusively to prevent other threads or 360634683adeSsg70180 * processes in the service domain from performing I/O to 360734683adeSsg70180 * slices being accessed by a client. Failure to open a slice 360834683adeSsg70180 * results in vds not serving this disk, as the client could 360934683adeSsg70180 * attempt (and should be able) to access any slice immediately. 361034683adeSsg70180 * Any slices successfully opened before a failure will get 361134683adeSsg70180 * closed by vds_destroy_vd() as a result of the error returned 361234683adeSsg70180 * by this function. 361334683adeSsg70180 * 361434683adeSsg70180 * We need to do the open with FNDELAY so that opening an empty 361534683adeSsg70180 * slice does not fail. 36160a55fbb7Slm66018 */ 36170a55fbb7Slm66018 PR0("Opening device major %u, minor %u = slice %u", 36180a55fbb7Slm66018 major, minor, slice); 3619047ba61eSachartre 3620047ba61eSachartre /* 3621047ba61eSachartre * Try to open the device. This can fail for example if we are 3622047ba61eSachartre * opening an empty slice. So in case of a failure, we try the 3623047ba61eSachartre * open again but this time with the FNDELAY flag. 3624047ba61eSachartre */ 3625047ba61eSachartre status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK, 3626047ba61eSachartre vd->open_flags, kcred, &vd->ldi_handle[slice], 3627047ba61eSachartre vd->vds->ldi_ident); 3628047ba61eSachartre 3629047ba61eSachartre if (status != 0) { 3630047ba61eSachartre status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK, 3631047ba61eSachartre vd->open_flags | FNDELAY, kcred, 3632047ba61eSachartre &vd->ldi_handle[slice], vd->vds->ldi_ident); 3633047ba61eSachartre } 3634047ba61eSachartre 3635047ba61eSachartre if (status != 0) { 3636690555a1Sachartre PRN("ldi_open_by_dev() returned errno %d " 36370a55fbb7Slm66018 "for slice %u", status, slice); 36380a55fbb7Slm66018 /* vds_destroy_vd() will close any open slices */ 3639690555a1Sachartre vd->ldi_handle[slice] = NULL; 36400a55fbb7Slm66018 return (status); 36410a55fbb7Slm66018 } 36420a55fbb7Slm66018 } 36430a55fbb7Slm66018 36440a55fbb7Slm66018 return (0); 36450a55fbb7Slm66018 } 36460a55fbb7Slm66018 36470a55fbb7Slm66018 static int 364878fcd0a1Sachartre vd_setup_partition_vtoc(vd_t *vd) 364978fcd0a1Sachartre { 365078fcd0a1Sachartre int rval, status; 365178fcd0a1Sachartre char *device_path = vd->device_path; 365278fcd0a1Sachartre 365378fcd0a1Sachartre status = ldi_ioctl(vd->ldi_handle[0], DKIOCGGEOM, 3654047ba61eSachartre (intptr_t)&vd->dk_geom, (vd->open_flags | FKIOCTL), kcred, &rval); 365578fcd0a1Sachartre 365678fcd0a1Sachartre if (status != 0) { 365778fcd0a1Sachartre PRN("ldi_ioctl(DKIOCGEOM) returned errno %d for %s", 365878fcd0a1Sachartre status, device_path); 365978fcd0a1Sachartre return (status); 366078fcd0a1Sachartre } 366178fcd0a1Sachartre 366278fcd0a1Sachartre /* Initialize dk_geom structure for single-slice device */ 366378fcd0a1Sachartre if (vd->dk_geom.dkg_nsect == 0) { 366478fcd0a1Sachartre PRN("%s geometry claims 0 sectors per track", device_path); 366578fcd0a1Sachartre return (EIO); 366678fcd0a1Sachartre } 366778fcd0a1Sachartre if (vd->dk_geom.dkg_nhead == 0) { 366878fcd0a1Sachartre PRN("%s geometry claims 0 heads", device_path); 366978fcd0a1Sachartre return (EIO); 367078fcd0a1Sachartre } 367178fcd0a1Sachartre vd->dk_geom.dkg_ncyl = vd->vdisk_size / vd->dk_geom.dkg_nsect / 367278fcd0a1Sachartre vd->dk_geom.dkg_nhead; 367378fcd0a1Sachartre vd->dk_geom.dkg_acyl = 0; 367478fcd0a1Sachartre vd->dk_geom.dkg_pcyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl; 367578fcd0a1Sachartre 367678fcd0a1Sachartre 367778fcd0a1Sachartre /* Initialize vtoc structure for single-slice device */ 367878fcd0a1Sachartre bcopy(VD_VOLUME_NAME, vd->vtoc.v_volume, 367978fcd0a1Sachartre MIN(sizeof (VD_VOLUME_NAME), sizeof (vd->vtoc.v_volume))); 368078fcd0a1Sachartre bzero(vd->vtoc.v_part, sizeof (vd->vtoc.v_part)); 368178fcd0a1Sachartre vd->vtoc.v_nparts = 1; 368278fcd0a1Sachartre vd->vtoc.v_part[0].p_tag = V_UNASSIGNED; 368378fcd0a1Sachartre vd->vtoc.v_part[0].p_flag = 0; 368478fcd0a1Sachartre vd->vtoc.v_part[0].p_start = 0; 368578fcd0a1Sachartre vd->vtoc.v_part[0].p_size = vd->vdisk_size; 368678fcd0a1Sachartre bcopy(VD_ASCIILABEL, vd->vtoc.v_asciilabel, 368778fcd0a1Sachartre MIN(sizeof (VD_ASCIILABEL), sizeof (vd->vtoc.v_asciilabel))); 368878fcd0a1Sachartre 368978fcd0a1Sachartre return (0); 369078fcd0a1Sachartre } 369178fcd0a1Sachartre 369278fcd0a1Sachartre static int 36934bac2208Snarayan vd_setup_partition_efi(vd_t *vd) 36944bac2208Snarayan { 36954bac2208Snarayan efi_gpt_t *gpt; 36964bac2208Snarayan efi_gpe_t *gpe; 36974bac2208Snarayan struct uuid uuid = EFI_RESERVED; 36984bac2208Snarayan uint32_t crc; 36994bac2208Snarayan int length; 37004bac2208Snarayan 37014bac2208Snarayan length = sizeof (efi_gpt_t) + sizeof (efi_gpe_t); 37024bac2208Snarayan 37034bac2208Snarayan gpt = kmem_zalloc(length, KM_SLEEP); 37044bac2208Snarayan gpe = (efi_gpe_t *)(gpt + 1); 37054bac2208Snarayan 37064bac2208Snarayan gpt->efi_gpt_Signature = LE_64(EFI_SIGNATURE); 37074bac2208Snarayan gpt->efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 37084bac2208Snarayan gpt->efi_gpt_HeaderSize = LE_32(sizeof (efi_gpt_t)); 37094bac2208Snarayan gpt->efi_gpt_FirstUsableLBA = LE_64(0ULL); 37104bac2208Snarayan gpt->efi_gpt_LastUsableLBA = LE_64(vd->vdisk_size - 1); 37114bac2208Snarayan gpt->efi_gpt_NumberOfPartitionEntries = LE_32(1); 37124bac2208Snarayan gpt->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (efi_gpe_t)); 37134bac2208Snarayan 37144bac2208Snarayan UUID_LE_CONVERT(gpe->efi_gpe_PartitionTypeGUID, uuid); 37154bac2208Snarayan gpe->efi_gpe_StartingLBA = gpt->efi_gpt_FirstUsableLBA; 37164bac2208Snarayan gpe->efi_gpe_EndingLBA = gpt->efi_gpt_LastUsableLBA; 37174bac2208Snarayan 37184bac2208Snarayan CRC32(crc, gpe, sizeof (efi_gpe_t), -1U, crc32_table); 37194bac2208Snarayan gpt->efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 37204bac2208Snarayan 37214bac2208Snarayan CRC32(crc, gpt, sizeof (efi_gpt_t), -1U, crc32_table); 37224bac2208Snarayan gpt->efi_gpt_HeaderCRC32 = LE_32(~crc); 37234bac2208Snarayan 37244bac2208Snarayan vd->dk_efi.dki_lba = 0; 37254bac2208Snarayan vd->dk_efi.dki_length = length; 37264bac2208Snarayan vd->dk_efi.dki_data = gpt; 37274bac2208Snarayan 37284bac2208Snarayan return (0); 37294bac2208Snarayan } 37304bac2208Snarayan 3731047ba61eSachartre /* 3732047ba61eSachartre * Setup for a virtual disk whose backend is a file (exported as a single slice 3733047ba61eSachartre * or as a full disk) or a pseudo device (for example a ZFS, SVM or VxVM volume) 3734047ba61eSachartre * exported as a full disk. In these cases, the backend is accessed using the 3735047ba61eSachartre * vnode interface. 3736047ba61eSachartre */ 37374bac2208Snarayan static int 3738047ba61eSachartre vd_setup_backend_vnode(vd_t *vd) 37393c96341aSnarayan { 374078fcd0a1Sachartre int rval, status; 37413c96341aSnarayan vattr_t vattr; 37423c96341aSnarayan dev_t dev; 37433c96341aSnarayan char *file_path = vd->device_path; 37443c96341aSnarayan char dev_path[MAXPATHLEN + 1]; 37453c96341aSnarayan ldi_handle_t lhandle; 37463c96341aSnarayan struct dk_cinfo dk_cinfo; 37473c96341aSnarayan 3748047ba61eSachartre if ((status = vn_open(file_path, UIO_SYSSPACE, vd->open_flags | FOFFMAX, 37493c96341aSnarayan 0, &vd->file_vnode, 0, 0)) != 0) { 3750690555a1Sachartre PRN("vn_open(%s) = errno %d", file_path, status); 37513c96341aSnarayan return (status); 37523c96341aSnarayan } 37533c96341aSnarayan 3754690555a1Sachartre /* 3755690555a1Sachartre * We set vd->file now so that vds_destroy_vd will take care of 3756690555a1Sachartre * closing the file and releasing the vnode in case of an error. 3757690555a1Sachartre */ 3758690555a1Sachartre vd->file = B_TRUE; 3759690555a1Sachartre 37603c96341aSnarayan vattr.va_mask = AT_SIZE; 3761*da6c28aaSamw if ((status = VOP_GETATTR(vd->file_vnode, &vattr, 0, kcred, NULL)) 3762*da6c28aaSamw != 0) { 3763690555a1Sachartre PRN("VOP_GETATTR(%s) = errno %d", file_path, status); 37643c96341aSnarayan return (EIO); 37653c96341aSnarayan } 37663c96341aSnarayan 37673c96341aSnarayan vd->file_size = vattr.va_size; 37683c96341aSnarayan /* size should be at least sizeof(dk_label) */ 37693c96341aSnarayan if (vd->file_size < sizeof (struct dk_label)) { 37703c96341aSnarayan PRN("Size of file has to be at least %ld bytes", 37713c96341aSnarayan sizeof (struct dk_label)); 37723c96341aSnarayan return (EIO); 37733c96341aSnarayan } 37743c96341aSnarayan 3775690555a1Sachartre if (vd->file_vnode->v_flag & VNOMAP) { 3776690555a1Sachartre PRN("File %s cannot be mapped", file_path); 37773c96341aSnarayan return (EIO); 37783c96341aSnarayan } 37793c96341aSnarayan 3780047ba61eSachartre /* 3781047ba61eSachartre * Find and validate the geometry of a disk image. For a single slice 3782047ba61eSachartre * disk image, this will build a fake geometry and vtoc. 3783047ba61eSachartre */ 378478fcd0a1Sachartre status = vd_file_validate_geometry(vd); 378578fcd0a1Sachartre if (status != 0 && status != EINVAL) { 378678fcd0a1Sachartre PRN("Fail to read label from %s", file_path); 3787690555a1Sachartre return (EIO); 3788690555a1Sachartre } 37893c96341aSnarayan 37903c96341aSnarayan /* sector size = block size = DEV_BSIZE */ 379187a7269eSachartre vd->vdisk_size = vd->file_size / DEV_BSIZE; 37923c96341aSnarayan vd->max_xfer_sz = maxphys / DEV_BSIZE; /* default transfer size */ 37933c96341aSnarayan 3794047ba61eSachartre /* 3795047ba61eSachartre * Get max_xfer_sz from the device where the file is or from the device 3796047ba61eSachartre * itself if we have a pseudo device. 3797047ba61eSachartre */ 3798047ba61eSachartre dev_path[0] = '\0'; 3799047ba61eSachartre 3800047ba61eSachartre if (vd->pseudo) { 3801047ba61eSachartre status = ldi_open_by_name(file_path, FREAD, kcred, &lhandle, 3802047ba61eSachartre vd->vds->ldi_ident); 3803047ba61eSachartre } else { 38043c96341aSnarayan dev = vd->file_vnode->v_vfsp->vfs_dev; 38053c96341aSnarayan if (ddi_dev_pathname(dev, S_IFBLK, dev_path) == DDI_SUCCESS) { 38063c96341aSnarayan PR0("underlying device = %s\n", dev_path); 38073c96341aSnarayan } 38083c96341aSnarayan 3809047ba61eSachartre status = ldi_open_by_dev(&dev, OTYP_BLK, FREAD, kcred, &lhandle, 3810047ba61eSachartre vd->vds->ldi_ident); 3811047ba61eSachartre } 3812047ba61eSachartre 3813047ba61eSachartre if (status != 0) { 3814047ba61eSachartre PR0("ldi_open() returned errno %d for device %s", 3815047ba61eSachartre status, (dev_path[0] == '\0')? file_path : dev_path); 38163c96341aSnarayan } else { 38173c96341aSnarayan if ((status = ldi_ioctl(lhandle, DKIOCINFO, 3818047ba61eSachartre (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred, 38193c96341aSnarayan &rval)) != 0) { 38203c96341aSnarayan PR0("ldi_ioctl(DKIOCINFO) returned errno %d for %s", 38213c96341aSnarayan status, dev_path); 38223c96341aSnarayan } else { 38233c96341aSnarayan /* 38243c96341aSnarayan * Store the device's max transfer size for 38253c96341aSnarayan * return to the client 38263c96341aSnarayan */ 38273c96341aSnarayan vd->max_xfer_sz = dk_cinfo.dki_maxtransfer; 38283c96341aSnarayan } 38293c96341aSnarayan 38303c96341aSnarayan PR0("close the device %s", dev_path); 38313c96341aSnarayan (void) ldi_close(lhandle, FREAD, kcred); 38323c96341aSnarayan } 38333c96341aSnarayan 3834205eeb1aSlm66018 PR0("using file %s, dev %s, max_xfer = %u blks", 38353c96341aSnarayan file_path, dev_path, vd->max_xfer_sz); 38363c96341aSnarayan 383787a7269eSachartre /* Setup devid for the disk image */ 383887a7269eSachartre 3839047ba61eSachartre if (vd->vdisk_type == VD_DISK_TYPE_SLICE) 3840047ba61eSachartre return (0); 3841047ba61eSachartre 384278fcd0a1Sachartre if (vd->vdisk_label != VD_DISK_LABEL_UNK) { 384378fcd0a1Sachartre 384487a7269eSachartre status = vd_file_read_devid(vd, &vd->file_devid); 384587a7269eSachartre 384687a7269eSachartre if (status == 0) { 384787a7269eSachartre /* a valid devid was found */ 384887a7269eSachartre return (0); 384987a7269eSachartre } 385087a7269eSachartre 385187a7269eSachartre if (status != EINVAL) { 385287a7269eSachartre /* 385378fcd0a1Sachartre * There was an error while trying to read the devid. 385478fcd0a1Sachartre * So this disk image may have a devid but we are 385578fcd0a1Sachartre * unable to read it. 385687a7269eSachartre */ 385787a7269eSachartre PR0("can not read devid for %s", file_path); 385887a7269eSachartre vd->file_devid = NULL; 385987a7269eSachartre return (0); 386087a7269eSachartre } 386178fcd0a1Sachartre } 386287a7269eSachartre 386387a7269eSachartre /* 386487a7269eSachartre * No valid device id was found so we create one. Note that a failure 386587a7269eSachartre * to create a device id is not fatal and does not prevent the disk 386687a7269eSachartre * image from being attached. 386787a7269eSachartre */ 386887a7269eSachartre PR1("creating devid for %s", file_path); 386987a7269eSachartre 387087a7269eSachartre if (ddi_devid_init(vd->vds->dip, DEVID_FAB, NULL, 0, 387187a7269eSachartre &vd->file_devid) != DDI_SUCCESS) { 387287a7269eSachartre PR0("fail to create devid for %s", file_path); 387387a7269eSachartre vd->file_devid = NULL; 387487a7269eSachartre return (0); 387587a7269eSachartre } 387687a7269eSachartre 387778fcd0a1Sachartre /* 387878fcd0a1Sachartre * Write devid to the disk image. The devid is stored into the disk 387978fcd0a1Sachartre * image if we have a valid label; otherwise the devid will be stored 388078fcd0a1Sachartre * when the user writes a valid label. 388178fcd0a1Sachartre */ 388278fcd0a1Sachartre if (vd->vdisk_label != VD_DISK_LABEL_UNK) { 388387a7269eSachartre if (vd_file_write_devid(vd, vd->file_devid) != 0) { 388487a7269eSachartre PR0("fail to write devid for %s", file_path); 388587a7269eSachartre ddi_devid_free(vd->file_devid); 388687a7269eSachartre vd->file_devid = NULL; 388787a7269eSachartre } 388878fcd0a1Sachartre } 388987a7269eSachartre 38903c96341aSnarayan return (0); 38913c96341aSnarayan } 38923c96341aSnarayan 3893047ba61eSachartre /* 3894047ba61eSachartre * Setup for a virtual disk which backend is a device (a physical disk, 3895047ba61eSachartre * slice or pseudo device) that is directly exported either as a full disk 3896047ba61eSachartre * for a physical disk or as a slice for a pseudo device or a disk slice. 3897047ba61eSachartre * In these cases, the backend is accessed using the LDI interface. 3898047ba61eSachartre */ 38993c96341aSnarayan static int 3900047ba61eSachartre vd_setup_backend_ldi(vd_t *vd) 39011ae08745Sheppo { 3902e1ebb9ecSlm66018 int rval, status; 39031ae08745Sheppo struct dk_cinfo dk_cinfo; 39043c96341aSnarayan char *device_path = vd->device_path; 39051ae08745Sheppo 39064bac2208Snarayan /* 3907047ba61eSachartre * Try to open the device. This can fail for example if we are opening 3908047ba61eSachartre * an empty slice. So in case of a failure, we try the open again but 3909047ba61eSachartre * this time with the FNDELAY flag. 39104bac2208Snarayan */ 3911047ba61eSachartre status = ldi_open_by_name(device_path, vd->open_flags, kcred, 3912047ba61eSachartre &vd->ldi_handle[0], vd->vds->ldi_ident); 3913047ba61eSachartre 3914047ba61eSachartre if (status != 0) 3915047ba61eSachartre status = ldi_open_by_name(device_path, vd->open_flags | FNDELAY, 3916047ba61eSachartre kcred, &vd->ldi_handle[0], vd->vds->ldi_ident); 3917047ba61eSachartre 3918047ba61eSachartre if (status != 0) { 39193c96341aSnarayan PR0("ldi_open_by_name(%s) = errno %d", device_path, status); 3920690555a1Sachartre vd->ldi_handle[0] = NULL; 39210a55fbb7Slm66018 return (status); 39220a55fbb7Slm66018 } 39230a55fbb7Slm66018 39243c96341aSnarayan vd->file = B_FALSE; 39254bac2208Snarayan 3926047ba61eSachartre /* Get device number of backing device */ 39270a55fbb7Slm66018 if ((status = ldi_get_dev(vd->ldi_handle[0], &vd->dev[0])) != 0) { 39281ae08745Sheppo PRN("ldi_get_dev() returned errno %d for %s", 3929e1ebb9ecSlm66018 status, device_path); 39301ae08745Sheppo return (status); 39311ae08745Sheppo } 39321ae08745Sheppo 393378fcd0a1Sachartre /* Verify backing device supports dk_cinfo */ 3934e1ebb9ecSlm66018 if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO, 3935047ba61eSachartre (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred, 3936e1ebb9ecSlm66018 &rval)) != 0) { 3937e1ebb9ecSlm66018 PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s", 3938e1ebb9ecSlm66018 status, device_path); 3939e1ebb9ecSlm66018 return (status); 3940e1ebb9ecSlm66018 } 3941e1ebb9ecSlm66018 if (dk_cinfo.dki_partition >= V_NUMPAR) { 3942e1ebb9ecSlm66018 PRN("slice %u >= maximum slice %u for %s", 3943e1ebb9ecSlm66018 dk_cinfo.dki_partition, V_NUMPAR, device_path); 3944e1ebb9ecSlm66018 return (EIO); 3945e1ebb9ecSlm66018 } 39464bac2208Snarayan 3947047ba61eSachartre vd->vdisk_label = vd_read_vtoc(vd, &vd->vtoc); 3948e1ebb9ecSlm66018 3949e1ebb9ecSlm66018 /* Store the device's max transfer size for return to the client */ 3950e1ebb9ecSlm66018 vd->max_xfer_sz = dk_cinfo.dki_maxtransfer; 3951e1ebb9ecSlm66018 3952047ba61eSachartre /* 3953047ba61eSachartre * Export a full disk. 3954047ba61eSachartre * 3955047ba61eSachartre * When we use the LDI interface, we export a device as a full disk 3956047ba61eSachartre * if we have an entire disk slice (slice 2) and if this slice is 3957047ba61eSachartre * exported as a full disk and not as a single slice disk. 3958047ba61eSachartre * 3959047ba61eSachartre * Note that pseudo devices are exported as full disks using the vnode 3960047ba61eSachartre * interface, not the LDI interface. 3961047ba61eSachartre */ 3962047ba61eSachartre if (dk_cinfo.dki_partition == VD_ENTIRE_DISK_SLICE && 3963047ba61eSachartre vd->vdisk_type == VD_DISK_TYPE_DISK) { 3964047ba61eSachartre ASSERT(!vd->pseudo); 3965047ba61eSachartre return (vd_setup_full_disk(vd)); 3966047ba61eSachartre } 3967047ba61eSachartre 3968047ba61eSachartre /* 3969047ba61eSachartre * Export a single slice disk. 3970047ba61eSachartre * 3971047ba61eSachartre * The exported device can be either a pseudo device or a disk slice. If 3972047ba61eSachartre * it is a disk slice different from slice 2 then it is always exported 3973047ba61eSachartre * as a single slice disk even if the "slice" option is not specified. 3974047ba61eSachartre * If it is disk slice 2 or a pseudo device then it is exported as a 3975047ba61eSachartre * single slice disk only if the "slice" option is specified. 3976047ba61eSachartre */ 3977047ba61eSachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE || 3978047ba61eSachartre dk_cinfo.dki_partition == VD_ENTIRE_DISK_SLICE); 3979047ba61eSachartre return (vd_setup_single_slice_disk(vd)); 3980047ba61eSachartre } 3981047ba61eSachartre 3982047ba61eSachartre static int 3983047ba61eSachartre vd_setup_single_slice_disk(vd_t *vd) 3984047ba61eSachartre { 3985047ba61eSachartre int status; 3986047ba61eSachartre char *device_path = vd->device_path; 3987047ba61eSachartre 3988047ba61eSachartre /* Get size of backing device */ 3989047ba61eSachartre if (ldi_get_size(vd->ldi_handle[0], &vd->vdisk_size) != DDI_SUCCESS) { 3990047ba61eSachartre PRN("ldi_get_size() failed for %s", device_path); 39911ae08745Sheppo return (EIO); 39921ae08745Sheppo } 3993047ba61eSachartre vd->vdisk_size = lbtodb(vd->vdisk_size); /* convert to blocks */ 3994047ba61eSachartre 39951ae08745Sheppo if (vd->pseudo) { 3996047ba61eSachartre 3997047ba61eSachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 3998047ba61eSachartre 399978fcd0a1Sachartre /* 400078fcd0a1Sachartre * Currently we only support exporting pseudo devices which 400178fcd0a1Sachartre * provide a valid disk label. 400278fcd0a1Sachartre */ 400378fcd0a1Sachartre if (vd->vdisk_label == VD_DISK_LABEL_UNK) { 400478fcd0a1Sachartre PRN("%s is a pseudo device with an invalid disk " 400578fcd0a1Sachartre "label\n", device_path); 400678fcd0a1Sachartre return (EINVAL); 400778fcd0a1Sachartre } 40081ae08745Sheppo return (0); /* ...and we're done */ 40091ae08745Sheppo } 40101ae08745Sheppo 401178fcd0a1Sachartre /* We can only export a slice if the disk has a valid label */ 401278fcd0a1Sachartre if (vd->vdisk_label == VD_DISK_LABEL_UNK) { 401378fcd0a1Sachartre PRN("%s is a slice from a disk with an unknown disk label\n", 401478fcd0a1Sachartre device_path); 401578fcd0a1Sachartre return (EINVAL); 401678fcd0a1Sachartre } 40170a55fbb7Slm66018 4018047ba61eSachartre /* 4019047ba61eSachartre * We export the slice as a single slice disk even if the "slice" 4020047ba61eSachartre * option was not specified. 4021047ba61eSachartre */ 40221ae08745Sheppo vd->vdisk_type = VD_DISK_TYPE_SLICE; 40231ae08745Sheppo vd->nslices = 1; 40241ae08745Sheppo 40254bac2208Snarayan if (vd->vdisk_label == VD_DISK_LABEL_EFI) { 402678fcd0a1Sachartre /* Slice from a disk with an EFI label */ 40274bac2208Snarayan status = vd_setup_partition_efi(vd); 402878fcd0a1Sachartre } else { 402978fcd0a1Sachartre /* Slice from a disk with a VTOC label */ 403078fcd0a1Sachartre ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC); 403178fcd0a1Sachartre status = vd_setup_partition_vtoc(vd); 403278fcd0a1Sachartre } 403378fcd0a1Sachartre 40344bac2208Snarayan return (status); 40354bac2208Snarayan } 40361ae08745Sheppo 40371ae08745Sheppo static int 4038047ba61eSachartre vd_setup_vd(vd_t *vd) 4039047ba61eSachartre { 4040047ba61eSachartre int status; 4041047ba61eSachartre dev_info_t *dip; 4042047ba61eSachartre vnode_t *vnp; 4043047ba61eSachartre char *path = vd->device_path; 4044047ba61eSachartre 4045047ba61eSachartre /* make sure the vdisk backend is valid */ 4046047ba61eSachartre if ((status = lookupname(path, UIO_SYSSPACE, 4047047ba61eSachartre FOLLOW, NULLVPP, &vnp)) != 0) { 4048047ba61eSachartre PR0("Cannot lookup %s errno %d", path, status); 4049047ba61eSachartre goto done; 4050047ba61eSachartre } 4051047ba61eSachartre 4052047ba61eSachartre switch (vnp->v_type) { 4053047ba61eSachartre case VREG: 4054047ba61eSachartre /* 4055047ba61eSachartre * Backend is a file so it is exported as a full disk or as a 4056047ba61eSachartre * single slice disk using the vnode interface. 4057047ba61eSachartre */ 4058047ba61eSachartre VN_RELE(vnp); 4059047ba61eSachartre vd->pseudo = B_FALSE; 4060047ba61eSachartre status = vd_setup_backend_vnode(vd); 4061047ba61eSachartre break; 4062047ba61eSachartre 4063047ba61eSachartre case VBLK: 4064047ba61eSachartre case VCHR: 4065047ba61eSachartre /* 4066047ba61eSachartre * Backend is a device. The way it is exported depends on the 4067047ba61eSachartre * type of the device. 4068047ba61eSachartre * 4069047ba61eSachartre * - A pseudo device is exported as a full disk using the vnode 4070047ba61eSachartre * interface or as a single slice disk using the LDI 4071047ba61eSachartre * interface. 4072047ba61eSachartre * 4073047ba61eSachartre * - A disk (represented by the slice 2 of that disk) is 4074047ba61eSachartre * exported as a full disk using the LDI interface. 4075047ba61eSachartre * 4076047ba61eSachartre * - A disk slice (different from slice 2) is always exported 4077047ba61eSachartre * as a single slice disk using the LDI interface. 4078047ba61eSachartre * 4079047ba61eSachartre * - The slice 2 of a disk is exported as a single slice disk 4080047ba61eSachartre * if the "slice" option is specified, otherwise the entire 4081047ba61eSachartre * disk will be exported. In any case, the LDI interface is 4082047ba61eSachartre * used. 4083047ba61eSachartre */ 4084047ba61eSachartre 4085047ba61eSachartre /* check if this is a pseudo device */ 4086047ba61eSachartre if ((dip = ddi_hold_devi_by_instance(getmajor(vnp->v_rdev), 4087047ba61eSachartre dev_to_instance(vnp->v_rdev), 0)) == NULL) { 4088047ba61eSachartre PRN("%s is no longer accessible", path); 4089047ba61eSachartre VN_RELE(vnp); 4090047ba61eSachartre status = EIO; 4091047ba61eSachartre break; 4092047ba61eSachartre } 4093047ba61eSachartre vd->pseudo = is_pseudo_device(dip); 4094047ba61eSachartre ddi_release_devi(dip); 4095047ba61eSachartre VN_RELE(vnp); 4096047ba61eSachartre 4097047ba61eSachartre /* 4098047ba61eSachartre * If this is a pseudo device then its usage depends if the 4099047ba61eSachartre * "slice" option is set or not. If the "slice" option is set 4100047ba61eSachartre * then the pseudo device will be exported as a single slice, 4101047ba61eSachartre * otherwise it will be exported as a full disk. 4102047ba61eSachartre */ 4103047ba61eSachartre if (vd->pseudo && vd->vdisk_type == VD_DISK_TYPE_DISK) 4104047ba61eSachartre status = vd_setup_backend_vnode(vd); 4105047ba61eSachartre else 4106047ba61eSachartre status = vd_setup_backend_ldi(vd); 4107047ba61eSachartre break; 4108047ba61eSachartre 4109047ba61eSachartre default: 4110047ba61eSachartre PRN("Unsupported vdisk backend %s", path); 4111047ba61eSachartre VN_RELE(vnp); 4112047ba61eSachartre status = EBADF; 4113047ba61eSachartre } 4114047ba61eSachartre 4115047ba61eSachartre done: 4116047ba61eSachartre if (status != 0) { 4117047ba61eSachartre /* 4118047ba61eSachartre * If the error is retryable print an error message only 4119047ba61eSachartre * during the first try. 4120047ba61eSachartre */ 4121047ba61eSachartre if (status == ENXIO || status == ENODEV || 4122047ba61eSachartre status == ENOENT || status == EROFS) { 4123047ba61eSachartre if (!(vd->initialized & VD_SETUP_ERROR)) { 4124047ba61eSachartre PRN("%s is currently inaccessible (error %d)", 4125047ba61eSachartre path, status); 4126047ba61eSachartre } 4127047ba61eSachartre status = EAGAIN; 4128047ba61eSachartre } else { 4129047ba61eSachartre PRN("%s can not be exported as a virtual disk " 4130047ba61eSachartre "(error %d)", path, status); 4131047ba61eSachartre } 4132047ba61eSachartre vd->initialized |= VD_SETUP_ERROR; 4133047ba61eSachartre 4134047ba61eSachartre } else if (vd->initialized & VD_SETUP_ERROR) { 4135047ba61eSachartre /* print a message only if we previously had an error */ 4136047ba61eSachartre PRN("%s is now online", path); 4137047ba61eSachartre vd->initialized &= ~VD_SETUP_ERROR; 4138047ba61eSachartre } 4139047ba61eSachartre 4140047ba61eSachartre return (status); 4141047ba61eSachartre } 4142047ba61eSachartre 4143047ba61eSachartre static int 4144047ba61eSachartre vds_do_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options, 4145047ba61eSachartre uint64_t ldc_id, vd_t **vdp) 41461ae08745Sheppo { 41471ae08745Sheppo char tq_name[TASKQ_NAMELEN]; 41480a55fbb7Slm66018 int status; 41491ae08745Sheppo ddi_iblock_cookie_t iblock = NULL; 41501ae08745Sheppo ldc_attr_t ldc_attr; 41511ae08745Sheppo vd_t *vd; 41521ae08745Sheppo 41531ae08745Sheppo 41541ae08745Sheppo ASSERT(vds != NULL); 4155e1ebb9ecSlm66018 ASSERT(device_path != NULL); 41561ae08745Sheppo ASSERT(vdp != NULL); 4157e1ebb9ecSlm66018 PR0("Adding vdisk for %s", device_path); 41581ae08745Sheppo 41591ae08745Sheppo if ((vd = kmem_zalloc(sizeof (*vd), KM_NOSLEEP)) == NULL) { 41601ae08745Sheppo PRN("No memory for virtual disk"); 41611ae08745Sheppo return (EAGAIN); 41621ae08745Sheppo } 41631ae08745Sheppo *vdp = vd; /* assign here so vds_destroy_vd() can cleanup later */ 41641ae08745Sheppo vd->vds = vds; 41653c96341aSnarayan (void) strncpy(vd->device_path, device_path, MAXPATHLEN); 41661ae08745Sheppo 4167047ba61eSachartre /* Setup open flags */ 4168047ba61eSachartre vd->open_flags = FREAD; 4169047ba61eSachartre 4170047ba61eSachartre if (!(options & VD_OPT_RDONLY)) 4171047ba61eSachartre vd->open_flags |= FWRITE; 4172047ba61eSachartre 4173047ba61eSachartre if (options & VD_OPT_EXCLUSIVE) 4174047ba61eSachartre vd->open_flags |= FEXCL; 4175047ba61eSachartre 4176047ba61eSachartre /* Setup disk type */ 4177047ba61eSachartre if (options & VD_OPT_SLICE) { 4178047ba61eSachartre vd->vdisk_type = VD_DISK_TYPE_SLICE; 4179047ba61eSachartre vd->nslices = 1; 4180047ba61eSachartre } else { 4181047ba61eSachartre vd->vdisk_type = VD_DISK_TYPE_DISK; 4182047ba61eSachartre vd->nslices = V_NUMPAR; 4183047ba61eSachartre } 4184047ba61eSachartre 4185047ba61eSachartre /* default disk label */ 4186047ba61eSachartre vd->vdisk_label = VD_DISK_LABEL_UNK; 4187047ba61eSachartre 41880a55fbb7Slm66018 /* Open vdisk and initialize parameters */ 41893c96341aSnarayan if ((status = vd_setup_vd(vd)) == 0) { 41903c96341aSnarayan vd->initialized |= VD_DISK_READY; 41911ae08745Sheppo 41923c96341aSnarayan ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR); 41933c96341aSnarayan PR0("vdisk_type = %s, pseudo = %s, file = %s, nslices = %u", 41943c96341aSnarayan ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"), 41953c96341aSnarayan (vd->pseudo ? "yes" : "no"), (vd->file ? "yes" : "no"), 41963c96341aSnarayan vd->nslices); 41973c96341aSnarayan } else { 41983c96341aSnarayan if (status != EAGAIN) 41993c96341aSnarayan return (status); 42003c96341aSnarayan } 42011ae08745Sheppo 42021ae08745Sheppo /* Initialize locking */ 42031ae08745Sheppo if (ddi_get_soft_iblock_cookie(vds->dip, DDI_SOFTINT_MED, 42041ae08745Sheppo &iblock) != DDI_SUCCESS) { 42051ae08745Sheppo PRN("Could not get iblock cookie."); 42061ae08745Sheppo return (EIO); 42071ae08745Sheppo } 42081ae08745Sheppo 42091ae08745Sheppo mutex_init(&vd->lock, NULL, MUTEX_DRIVER, iblock); 42101ae08745Sheppo vd->initialized |= VD_LOCKING; 42111ae08745Sheppo 42121ae08745Sheppo 4213d10e4ef2Snarayan /* Create start and completion task queues for the vdisk */ 4214d10e4ef2Snarayan (void) snprintf(tq_name, sizeof (tq_name), "vd_startq%lu", id); 42151ae08745Sheppo PR1("tq_name = %s", tq_name); 4216d10e4ef2Snarayan if ((vd->startq = ddi_taskq_create(vds->dip, tq_name, 1, 42171ae08745Sheppo TASKQ_DEFAULTPRI, 0)) == NULL) { 42181ae08745Sheppo PRN("Could not create task queue"); 42191ae08745Sheppo return (EIO); 42201ae08745Sheppo } 4221d10e4ef2Snarayan (void) snprintf(tq_name, sizeof (tq_name), "vd_completionq%lu", id); 4222d10e4ef2Snarayan PR1("tq_name = %s", tq_name); 4223d10e4ef2Snarayan if ((vd->completionq = ddi_taskq_create(vds->dip, tq_name, 1, 4224d10e4ef2Snarayan TASKQ_DEFAULTPRI, 0)) == NULL) { 4225d10e4ef2Snarayan PRN("Could not create task queue"); 4226d10e4ef2Snarayan return (EIO); 4227d10e4ef2Snarayan } 4228d10e4ef2Snarayan vd->enabled = 1; /* before callback can dispatch to startq */ 42291ae08745Sheppo 42301ae08745Sheppo 42311ae08745Sheppo /* Bring up LDC */ 42321ae08745Sheppo ldc_attr.devclass = LDC_DEV_BLK_SVC; 42331ae08745Sheppo ldc_attr.instance = ddi_get_instance(vds->dip); 42341ae08745Sheppo ldc_attr.mode = LDC_MODE_UNRELIABLE; 4235e1ebb9ecSlm66018 ldc_attr.mtu = VD_LDC_MTU; 42361ae08745Sheppo if ((status = ldc_init(ldc_id, &ldc_attr, &vd->ldc_handle)) != 0) { 4237690555a1Sachartre PRN("Could not initialize LDC channel %lu, " 4238690555a1Sachartre "init failed with error %d", ldc_id, status); 42391ae08745Sheppo return (status); 42401ae08745Sheppo } 42411ae08745Sheppo vd->initialized |= VD_LDC; 42421ae08745Sheppo 42431ae08745Sheppo if ((status = ldc_reg_callback(vd->ldc_handle, vd_handle_ldc_events, 42441ae08745Sheppo (caddr_t)vd)) != 0) { 4245690555a1Sachartre PRN("Could not initialize LDC channel %lu," 4246690555a1Sachartre "reg_callback failed with error %d", ldc_id, status); 42471ae08745Sheppo return (status); 42481ae08745Sheppo } 42491ae08745Sheppo 42501ae08745Sheppo if ((status = ldc_open(vd->ldc_handle)) != 0) { 4251690555a1Sachartre PRN("Could not initialize LDC channel %lu," 4252690555a1Sachartre "open failed with error %d", ldc_id, status); 42531ae08745Sheppo return (status); 42541ae08745Sheppo } 42551ae08745Sheppo 42563af08d82Slm66018 if ((status = ldc_up(vd->ldc_handle)) != 0) { 425734683adeSsg70180 PR0("ldc_up() returned errno %d", status); 42583af08d82Slm66018 } 42593af08d82Slm66018 42604bac2208Snarayan /* Allocate the inband task memory handle */ 42614bac2208Snarayan status = ldc_mem_alloc_handle(vd->ldc_handle, &(vd->inband_task.mhdl)); 42624bac2208Snarayan if (status) { 4263690555a1Sachartre PRN("Could not initialize LDC channel %lu," 4264690555a1Sachartre "alloc_handle failed with error %d", ldc_id, status); 42654bac2208Snarayan return (ENXIO); 42664bac2208Snarayan } 42671ae08745Sheppo 42681ae08745Sheppo /* Add the successfully-initialized vdisk to the server's table */ 42691ae08745Sheppo if (mod_hash_insert(vds->vd_table, (mod_hash_key_t)id, vd) != 0) { 42701ae08745Sheppo PRN("Error adding vdisk ID %lu to table", id); 42711ae08745Sheppo return (EIO); 42721ae08745Sheppo } 42731ae08745Sheppo 42743af08d82Slm66018 /* Allocate the staging buffer */ 42753af08d82Slm66018 vd->max_msglen = sizeof (vio_msg_t); /* baseline vio message size */ 42763af08d82Slm66018 vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP); 42773af08d82Slm66018 42783af08d82Slm66018 /* store initial state */ 42793af08d82Slm66018 vd->state = VD_STATE_INIT; 42803af08d82Slm66018 42811ae08745Sheppo return (0); 42821ae08745Sheppo } 42831ae08745Sheppo 42843af08d82Slm66018 static void 42853af08d82Slm66018 vd_free_dring_task(vd_t *vdp) 42863af08d82Slm66018 { 42873af08d82Slm66018 if (vdp->dring_task != NULL) { 42883af08d82Slm66018 ASSERT(vdp->dring_len != 0); 42893af08d82Slm66018 /* Free all dring_task memory handles */ 42903af08d82Slm66018 for (int i = 0; i < vdp->dring_len; i++) { 42913af08d82Slm66018 (void) ldc_mem_free_handle(vdp->dring_task[i].mhdl); 42923af08d82Slm66018 kmem_free(vdp->dring_task[i].msg, vdp->max_msglen); 42933af08d82Slm66018 vdp->dring_task[i].msg = NULL; 42943af08d82Slm66018 } 42953af08d82Slm66018 kmem_free(vdp->dring_task, 42963af08d82Slm66018 (sizeof (*vdp->dring_task)) * vdp->dring_len); 42973af08d82Slm66018 vdp->dring_task = NULL; 42983af08d82Slm66018 } 42993af08d82Slm66018 } 43003af08d82Slm66018 43011ae08745Sheppo /* 43021ae08745Sheppo * Destroy the state associated with a virtual disk 43031ae08745Sheppo */ 43041ae08745Sheppo static void 43051ae08745Sheppo vds_destroy_vd(void *arg) 43061ae08745Sheppo { 43071ae08745Sheppo vd_t *vd = (vd_t *)arg; 430834683adeSsg70180 int retry = 0, rv; 43091ae08745Sheppo 43101ae08745Sheppo if (vd == NULL) 43111ae08745Sheppo return; 43121ae08745Sheppo 4313d10e4ef2Snarayan PR0("Destroying vdisk state"); 4314d10e4ef2Snarayan 43154bac2208Snarayan if (vd->dk_efi.dki_data != NULL) 43164bac2208Snarayan kmem_free(vd->dk_efi.dki_data, vd->dk_efi.dki_length); 43174bac2208Snarayan 43181ae08745Sheppo /* Disable queuing requests for the vdisk */ 43191ae08745Sheppo if (vd->initialized & VD_LOCKING) { 43201ae08745Sheppo mutex_enter(&vd->lock); 43211ae08745Sheppo vd->enabled = 0; 43221ae08745Sheppo mutex_exit(&vd->lock); 43231ae08745Sheppo } 43241ae08745Sheppo 4325d10e4ef2Snarayan /* Drain and destroy start queue (*before* destroying completionq) */ 4326d10e4ef2Snarayan if (vd->startq != NULL) 4327d10e4ef2Snarayan ddi_taskq_destroy(vd->startq); /* waits for queued tasks */ 4328d10e4ef2Snarayan 4329d10e4ef2Snarayan /* Drain and destroy completion queue (*before* shutting down LDC) */ 4330d10e4ef2Snarayan if (vd->completionq != NULL) 4331d10e4ef2Snarayan ddi_taskq_destroy(vd->completionq); /* waits for tasks */ 4332d10e4ef2Snarayan 43333af08d82Slm66018 vd_free_dring_task(vd); 43343af08d82Slm66018 433534683adeSsg70180 /* Free the inband task memory handle */ 433634683adeSsg70180 (void) ldc_mem_free_handle(vd->inband_task.mhdl); 433734683adeSsg70180 433834683adeSsg70180 /* Shut down LDC */ 433934683adeSsg70180 if (vd->initialized & VD_LDC) { 434034683adeSsg70180 /* unmap the dring */ 434134683adeSsg70180 if (vd->initialized & VD_DRING) 434234683adeSsg70180 (void) ldc_mem_dring_unmap(vd->dring_handle); 434334683adeSsg70180 434434683adeSsg70180 /* close LDC channel - retry on EAGAIN */ 434534683adeSsg70180 while ((rv = ldc_close(vd->ldc_handle)) == EAGAIN) { 434634683adeSsg70180 if (++retry > vds_ldc_retries) { 434734683adeSsg70180 PR0("Timed out closing channel"); 434834683adeSsg70180 break; 434934683adeSsg70180 } 435034683adeSsg70180 drv_usecwait(vds_ldc_delay); 435134683adeSsg70180 } 435234683adeSsg70180 if (rv == 0) { 435334683adeSsg70180 (void) ldc_unreg_callback(vd->ldc_handle); 435434683adeSsg70180 (void) ldc_fini(vd->ldc_handle); 435534683adeSsg70180 } else { 435634683adeSsg70180 /* 435734683adeSsg70180 * Closing the LDC channel has failed. Ideally we should 435834683adeSsg70180 * fail here but there is no Zeus level infrastructure 435934683adeSsg70180 * to handle this. The MD has already been changed and 436034683adeSsg70180 * we have to do the close. So we try to do as much 436134683adeSsg70180 * clean up as we can. 436234683adeSsg70180 */ 436334683adeSsg70180 (void) ldc_set_cb_mode(vd->ldc_handle, LDC_CB_DISABLE); 436434683adeSsg70180 while (ldc_unreg_callback(vd->ldc_handle) == EAGAIN) 436534683adeSsg70180 drv_usecwait(vds_ldc_delay); 436634683adeSsg70180 } 436734683adeSsg70180 } 436834683adeSsg70180 43693af08d82Slm66018 /* Free the staging buffer for msgs */ 43703af08d82Slm66018 if (vd->vio_msgp != NULL) { 43713af08d82Slm66018 kmem_free(vd->vio_msgp, vd->max_msglen); 43723af08d82Slm66018 vd->vio_msgp = NULL; 43733af08d82Slm66018 } 43743af08d82Slm66018 43753af08d82Slm66018 /* Free the inband message buffer */ 43763af08d82Slm66018 if (vd->inband_task.msg != NULL) { 43773af08d82Slm66018 kmem_free(vd->inband_task.msg, vd->max_msglen); 43783af08d82Slm66018 vd->inband_task.msg = NULL; 4379d10e4ef2Snarayan } 4380*da6c28aaSamw 43813c96341aSnarayan if (vd->file) { 4382690555a1Sachartre /* Close file */ 4383047ba61eSachartre (void) VOP_CLOSE(vd->file_vnode, vd->open_flags, 1, 4384*da6c28aaSamw 0, kcred, NULL); 43853c96341aSnarayan VN_RELE(vd->file_vnode); 438687a7269eSachartre if (vd->file_devid != NULL) 438787a7269eSachartre ddi_devid_free(vd->file_devid); 43883c96341aSnarayan } else { 43891ae08745Sheppo /* Close any open backing-device slices */ 43901ae08745Sheppo for (uint_t slice = 0; slice < vd->nslices; slice++) { 43911ae08745Sheppo if (vd->ldi_handle[slice] != NULL) { 43921ae08745Sheppo PR0("Closing slice %u", slice); 43931ae08745Sheppo (void) ldi_close(vd->ldi_handle[slice], 4394047ba61eSachartre vd->open_flags, kcred); 43951ae08745Sheppo } 43961ae08745Sheppo } 43973c96341aSnarayan } 43981ae08745Sheppo 43991ae08745Sheppo /* Free lock */ 44001ae08745Sheppo if (vd->initialized & VD_LOCKING) 44011ae08745Sheppo mutex_destroy(&vd->lock); 44021ae08745Sheppo 44031ae08745Sheppo /* Finally, free the vdisk structure itself */ 44041ae08745Sheppo kmem_free(vd, sizeof (*vd)); 44051ae08745Sheppo } 44061ae08745Sheppo 44071ae08745Sheppo static int 4408047ba61eSachartre vds_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options, 4409047ba61eSachartre uint64_t ldc_id) 44101ae08745Sheppo { 44111ae08745Sheppo int status; 44121ae08745Sheppo vd_t *vd = NULL; 44131ae08745Sheppo 44141ae08745Sheppo 4415047ba61eSachartre if ((status = vds_do_init_vd(vds, id, device_path, options, 4416047ba61eSachartre ldc_id, &vd)) != 0) 44171ae08745Sheppo vds_destroy_vd(vd); 44181ae08745Sheppo 44191ae08745Sheppo return (status); 44201ae08745Sheppo } 44211ae08745Sheppo 44221ae08745Sheppo static int 44231ae08745Sheppo vds_do_get_ldc_id(md_t *md, mde_cookie_t vd_node, mde_cookie_t *channel, 44241ae08745Sheppo uint64_t *ldc_id) 44251ae08745Sheppo { 44261ae08745Sheppo int num_channels; 44271ae08745Sheppo 44281ae08745Sheppo 44291ae08745Sheppo /* Look for channel endpoint child(ren) of the vdisk MD node */ 44301ae08745Sheppo if ((num_channels = md_scan_dag(md, vd_node, 44311ae08745Sheppo md_find_name(md, VD_CHANNEL_ENDPOINT), 44321ae08745Sheppo md_find_name(md, "fwd"), channel)) <= 0) { 44331ae08745Sheppo PRN("No \"%s\" found for virtual disk", VD_CHANNEL_ENDPOINT); 44341ae08745Sheppo return (-1); 44351ae08745Sheppo } 44361ae08745Sheppo 44371ae08745Sheppo /* Get the "id" value for the first channel endpoint node */ 44381ae08745Sheppo if (md_get_prop_val(md, channel[0], VD_ID_PROP, ldc_id) != 0) { 44391ae08745Sheppo PRN("No \"%s\" property found for \"%s\" of vdisk", 44401ae08745Sheppo VD_ID_PROP, VD_CHANNEL_ENDPOINT); 44411ae08745Sheppo return (-1); 44421ae08745Sheppo } 44431ae08745Sheppo 44441ae08745Sheppo if (num_channels > 1) { 44451ae08745Sheppo PRN("Using ID of first of multiple channels for this vdisk"); 44461ae08745Sheppo } 44471ae08745Sheppo 44481ae08745Sheppo return (0); 44491ae08745Sheppo } 44501ae08745Sheppo 44511ae08745Sheppo static int 44521ae08745Sheppo vds_get_ldc_id(md_t *md, mde_cookie_t vd_node, uint64_t *ldc_id) 44531ae08745Sheppo { 44541ae08745Sheppo int num_nodes, status; 44551ae08745Sheppo size_t size; 44561ae08745Sheppo mde_cookie_t *channel; 44571ae08745Sheppo 44581ae08745Sheppo 44591ae08745Sheppo if ((num_nodes = md_node_count(md)) <= 0) { 44601ae08745Sheppo PRN("Invalid node count in Machine Description subtree"); 44611ae08745Sheppo return (-1); 44621ae08745Sheppo } 44631ae08745Sheppo size = num_nodes*(sizeof (*channel)); 44641ae08745Sheppo channel = kmem_zalloc(size, KM_SLEEP); 44651ae08745Sheppo status = vds_do_get_ldc_id(md, vd_node, channel, ldc_id); 44661ae08745Sheppo kmem_free(channel, size); 44671ae08745Sheppo 44681ae08745Sheppo return (status); 44691ae08745Sheppo } 44701ae08745Sheppo 4471047ba61eSachartre /* 4472047ba61eSachartre * Function: 4473047ba61eSachartre * vds_get_options 4474047ba61eSachartre * 4475047ba61eSachartre * Description: 4476047ba61eSachartre * Parse the options of a vds node. Options are defined as an array 4477047ba61eSachartre * of strings in the vds-block-device-opts property of the vds node 4478047ba61eSachartre * in the machine description. Options are returned as a bitmask. The 4479047ba61eSachartre * mapping between the bitmask options and the options strings from the 4480047ba61eSachartre * machine description is defined in the vd_bdev_options[] array. 4481047ba61eSachartre * 4482047ba61eSachartre * The vds-block-device-opts property is optional. If a vds has no such 4483047ba61eSachartre * property then no option is defined. 4484047ba61eSachartre * 4485047ba61eSachartre * Parameters: 4486047ba61eSachartre * md - machine description. 4487047ba61eSachartre * vd_node - vds node in the machine description for which 4488047ba61eSachartre * options have to be parsed. 4489047ba61eSachartre * options - the returned options. 4490047ba61eSachartre * 4491047ba61eSachartre * Return Code: 4492047ba61eSachartre * none. 4493047ba61eSachartre */ 4494047ba61eSachartre static void 4495047ba61eSachartre vds_get_options(md_t *md, mde_cookie_t vd_node, uint64_t *options) 4496047ba61eSachartre { 4497047ba61eSachartre char *optstr, *opt; 4498047ba61eSachartre int len, n, i; 4499047ba61eSachartre 4500047ba61eSachartre *options = 0; 4501047ba61eSachartre 4502047ba61eSachartre if (md_get_prop_data(md, vd_node, VD_BLOCK_DEVICE_OPTS, 4503047ba61eSachartre (uint8_t **)&optstr, &len) != 0) { 4504047ba61eSachartre PR0("No options found"); 4505047ba61eSachartre return; 4506047ba61eSachartre } 4507047ba61eSachartre 4508047ba61eSachartre /* parse options */ 4509047ba61eSachartre opt = optstr; 4510047ba61eSachartre n = sizeof (vd_bdev_options) / sizeof (vd_option_t); 4511047ba61eSachartre 4512047ba61eSachartre while (opt < optstr + len) { 4513047ba61eSachartre for (i = 0; i < n; i++) { 4514047ba61eSachartre if (strncmp(vd_bdev_options[i].vdo_name, 4515047ba61eSachartre opt, VD_OPTION_NLEN) == 0) { 4516047ba61eSachartre *options |= vd_bdev_options[i].vdo_value; 4517047ba61eSachartre break; 4518047ba61eSachartre } 4519047ba61eSachartre } 4520047ba61eSachartre 4521047ba61eSachartre if (i < n) { 4522047ba61eSachartre PR0("option: %s", opt); 4523047ba61eSachartre } else { 4524047ba61eSachartre PRN("option %s is unknown or unsupported", opt); 4525047ba61eSachartre } 4526047ba61eSachartre 4527047ba61eSachartre opt += strlen(opt) + 1; 4528047ba61eSachartre } 4529047ba61eSachartre } 4530047ba61eSachartre 45311ae08745Sheppo static void 45321ae08745Sheppo vds_add_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node) 45331ae08745Sheppo { 4534e1ebb9ecSlm66018 char *device_path = NULL; 4535047ba61eSachartre uint64_t id = 0, ldc_id = 0, options = 0; 45361ae08745Sheppo 45371ae08745Sheppo if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) { 45381ae08745Sheppo PRN("Error getting vdisk \"%s\"", VD_ID_PROP); 45391ae08745Sheppo return; 45401ae08745Sheppo } 45411ae08745Sheppo PR0("Adding vdisk ID %lu", id); 45421ae08745Sheppo if (md_get_prop_str(md, vd_node, VD_BLOCK_DEVICE_PROP, 4543e1ebb9ecSlm66018 &device_path) != 0) { 45441ae08745Sheppo PRN("Error getting vdisk \"%s\"", VD_BLOCK_DEVICE_PROP); 45451ae08745Sheppo return; 45461ae08745Sheppo } 45471ae08745Sheppo 4548047ba61eSachartre vds_get_options(md, vd_node, &options); 4549047ba61eSachartre 45501ae08745Sheppo if (vds_get_ldc_id(md, vd_node, &ldc_id) != 0) { 45511ae08745Sheppo PRN("Error getting LDC ID for vdisk %lu", id); 45521ae08745Sheppo return; 45531ae08745Sheppo } 45541ae08745Sheppo 4555047ba61eSachartre if (vds_init_vd(vds, id, device_path, options, ldc_id) != 0) { 45561ae08745Sheppo PRN("Failed to add vdisk ID %lu", id); 45571ae08745Sheppo return; 45581ae08745Sheppo } 45591ae08745Sheppo } 45601ae08745Sheppo 45611ae08745Sheppo static void 45621ae08745Sheppo vds_remove_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node) 45631ae08745Sheppo { 45641ae08745Sheppo uint64_t id = 0; 45651ae08745Sheppo 45661ae08745Sheppo 45671ae08745Sheppo if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) { 45681ae08745Sheppo PRN("Unable to get \"%s\" property from vdisk's MD node", 45691ae08745Sheppo VD_ID_PROP); 45701ae08745Sheppo return; 45711ae08745Sheppo } 45721ae08745Sheppo PR0("Removing vdisk ID %lu", id); 45731ae08745Sheppo if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0) 45741ae08745Sheppo PRN("No vdisk entry found for vdisk ID %lu", id); 45751ae08745Sheppo } 45761ae08745Sheppo 45771ae08745Sheppo static void 45781ae08745Sheppo vds_change_vd(vds_t *vds, md_t *prev_md, mde_cookie_t prev_vd_node, 45791ae08745Sheppo md_t *curr_md, mde_cookie_t curr_vd_node) 45801ae08745Sheppo { 45811ae08745Sheppo char *curr_dev, *prev_dev; 4582047ba61eSachartre uint64_t curr_id = 0, curr_ldc_id = 0, curr_options = 0; 4583047ba61eSachartre uint64_t prev_id = 0, prev_ldc_id = 0, prev_options = 0; 45841ae08745Sheppo size_t len; 45851ae08745Sheppo 45861ae08745Sheppo 45871ae08745Sheppo /* Validate that vdisk ID has not changed */ 45881ae08745Sheppo if (md_get_prop_val(prev_md, prev_vd_node, VD_ID_PROP, &prev_id) != 0) { 45891ae08745Sheppo PRN("Error getting previous vdisk \"%s\" property", 45901ae08745Sheppo VD_ID_PROP); 45911ae08745Sheppo return; 45921ae08745Sheppo } 45931ae08745Sheppo if (md_get_prop_val(curr_md, curr_vd_node, VD_ID_PROP, &curr_id) != 0) { 45941ae08745Sheppo PRN("Error getting current vdisk \"%s\" property", VD_ID_PROP); 45951ae08745Sheppo return; 45961ae08745Sheppo } 45971ae08745Sheppo if (curr_id != prev_id) { 45981ae08745Sheppo PRN("Not changing vdisk: ID changed from %lu to %lu", 45991ae08745Sheppo prev_id, curr_id); 46001ae08745Sheppo return; 46011ae08745Sheppo } 46021ae08745Sheppo 46031ae08745Sheppo /* Validate that LDC ID has not changed */ 46041ae08745Sheppo if (vds_get_ldc_id(prev_md, prev_vd_node, &prev_ldc_id) != 0) { 46051ae08745Sheppo PRN("Error getting LDC ID for vdisk %lu", prev_id); 46061ae08745Sheppo return; 46071ae08745Sheppo } 46081ae08745Sheppo 46091ae08745Sheppo if (vds_get_ldc_id(curr_md, curr_vd_node, &curr_ldc_id) != 0) { 46101ae08745Sheppo PRN("Error getting LDC ID for vdisk %lu", curr_id); 46111ae08745Sheppo return; 46121ae08745Sheppo } 46131ae08745Sheppo if (curr_ldc_id != prev_ldc_id) { 46140a55fbb7Slm66018 _NOTE(NOTREACHED); /* lint is confused */ 46151ae08745Sheppo PRN("Not changing vdisk: " 46161ae08745Sheppo "LDC ID changed from %lu to %lu", prev_ldc_id, curr_ldc_id); 46171ae08745Sheppo return; 46181ae08745Sheppo } 46191ae08745Sheppo 46201ae08745Sheppo /* Determine whether device path has changed */ 46211ae08745Sheppo if (md_get_prop_str(prev_md, prev_vd_node, VD_BLOCK_DEVICE_PROP, 46221ae08745Sheppo &prev_dev) != 0) { 46231ae08745Sheppo PRN("Error getting previous vdisk \"%s\"", 46241ae08745Sheppo VD_BLOCK_DEVICE_PROP); 46251ae08745Sheppo return; 46261ae08745Sheppo } 46271ae08745Sheppo if (md_get_prop_str(curr_md, curr_vd_node, VD_BLOCK_DEVICE_PROP, 46281ae08745Sheppo &curr_dev) != 0) { 46291ae08745Sheppo PRN("Error getting current vdisk \"%s\"", VD_BLOCK_DEVICE_PROP); 46301ae08745Sheppo return; 46311ae08745Sheppo } 46321ae08745Sheppo if (((len = strlen(curr_dev)) == strlen(prev_dev)) && 46331ae08745Sheppo (strncmp(curr_dev, prev_dev, len) == 0)) 46341ae08745Sheppo return; /* no relevant (supported) change */ 46351ae08745Sheppo 4636047ba61eSachartre /* Validate that options have not changed */ 4637047ba61eSachartre vds_get_options(prev_md, prev_vd_node, &prev_options); 4638047ba61eSachartre vds_get_options(curr_md, curr_vd_node, &curr_options); 4639047ba61eSachartre if (prev_options != curr_options) { 4640047ba61eSachartre PRN("Not changing vdisk: options changed from %lx to %lx", 4641047ba61eSachartre prev_options, curr_options); 4642047ba61eSachartre return; 4643047ba61eSachartre } 4644047ba61eSachartre 46451ae08745Sheppo PR0("Changing vdisk ID %lu", prev_id); 46463af08d82Slm66018 46471ae08745Sheppo /* Remove old state, which will close vdisk and reset */ 46481ae08745Sheppo if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)prev_id) != 0) 46491ae08745Sheppo PRN("No entry found for vdisk ID %lu", prev_id); 46503af08d82Slm66018 46511ae08745Sheppo /* Re-initialize vdisk with new state */ 4652047ba61eSachartre if (vds_init_vd(vds, curr_id, curr_dev, curr_options, 4653047ba61eSachartre curr_ldc_id) != 0) { 46541ae08745Sheppo PRN("Failed to change vdisk ID %lu", curr_id); 46551ae08745Sheppo return; 46561ae08745Sheppo } 46571ae08745Sheppo } 46581ae08745Sheppo 46591ae08745Sheppo static int 46601ae08745Sheppo vds_process_md(void *arg, mdeg_result_t *md) 46611ae08745Sheppo { 46621ae08745Sheppo int i; 46631ae08745Sheppo vds_t *vds = arg; 46641ae08745Sheppo 46651ae08745Sheppo 46661ae08745Sheppo if (md == NULL) 46671ae08745Sheppo return (MDEG_FAILURE); 46681ae08745Sheppo ASSERT(vds != NULL); 46691ae08745Sheppo 46701ae08745Sheppo for (i = 0; i < md->removed.nelem; i++) 46711ae08745Sheppo vds_remove_vd(vds, md->removed.mdp, md->removed.mdep[i]); 46721ae08745Sheppo for (i = 0; i < md->match_curr.nelem; i++) 46731ae08745Sheppo vds_change_vd(vds, md->match_prev.mdp, md->match_prev.mdep[i], 46741ae08745Sheppo md->match_curr.mdp, md->match_curr.mdep[i]); 46751ae08745Sheppo for (i = 0; i < md->added.nelem; i++) 46761ae08745Sheppo vds_add_vd(vds, md->added.mdp, md->added.mdep[i]); 46771ae08745Sheppo 46781ae08745Sheppo return (MDEG_SUCCESS); 46791ae08745Sheppo } 46801ae08745Sheppo 46813c96341aSnarayan 46821ae08745Sheppo static int 46831ae08745Sheppo vds_do_attach(dev_info_t *dip) 46841ae08745Sheppo { 4685445b4c2eSsb155480 int status, sz; 4686445b4c2eSsb155480 int cfg_handle; 46871ae08745Sheppo minor_t instance = ddi_get_instance(dip); 46881ae08745Sheppo vds_t *vds; 4689445b4c2eSsb155480 mdeg_prop_spec_t *pspecp; 4690445b4c2eSsb155480 mdeg_node_spec_t *ispecp; 46911ae08745Sheppo 46921ae08745Sheppo /* 46931ae08745Sheppo * The "cfg-handle" property of a vds node in an MD contains the MD's 46941ae08745Sheppo * notion of "instance", or unique identifier, for that node; OBP 46951ae08745Sheppo * stores the value of the "cfg-handle" MD property as the value of 46961ae08745Sheppo * the "reg" property on the node in the device tree it builds from 46971ae08745Sheppo * the MD and passes to Solaris. Thus, we look up the devinfo node's 46981ae08745Sheppo * "reg" property value to uniquely identify this device instance when 46991ae08745Sheppo * registering with the MD event-generation framework. If the "reg" 47001ae08745Sheppo * property cannot be found, the device tree state is presumably so 47011ae08745Sheppo * broken that there is no point in continuing. 47021ae08745Sheppo */ 4703445b4c2eSsb155480 if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 4704445b4c2eSsb155480 VD_REG_PROP)) { 4705445b4c2eSsb155480 PRN("vds \"%s\" property does not exist", VD_REG_PROP); 47061ae08745Sheppo return (DDI_FAILURE); 47071ae08745Sheppo } 47081ae08745Sheppo 47091ae08745Sheppo /* Get the MD instance for later MDEG registration */ 47101ae08745Sheppo cfg_handle = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 4711445b4c2eSsb155480 VD_REG_PROP, -1); 47121ae08745Sheppo 47131ae08745Sheppo if (ddi_soft_state_zalloc(vds_state, instance) != DDI_SUCCESS) { 47141ae08745Sheppo PRN("Could not allocate state for instance %u", instance); 47151ae08745Sheppo return (DDI_FAILURE); 47161ae08745Sheppo } 47171ae08745Sheppo 47181ae08745Sheppo if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) { 47191ae08745Sheppo PRN("Could not get state for instance %u", instance); 47201ae08745Sheppo ddi_soft_state_free(vds_state, instance); 47211ae08745Sheppo return (DDI_FAILURE); 47221ae08745Sheppo } 47231ae08745Sheppo 47241ae08745Sheppo vds->dip = dip; 47251ae08745Sheppo vds->vd_table = mod_hash_create_ptrhash("vds_vd_table", VDS_NCHAINS, 472687a7269eSachartre vds_destroy_vd, sizeof (void *)); 472787a7269eSachartre 47281ae08745Sheppo ASSERT(vds->vd_table != NULL); 47291ae08745Sheppo 47301ae08745Sheppo if ((status = ldi_ident_from_dip(dip, &vds->ldi_ident)) != 0) { 47311ae08745Sheppo PRN("ldi_ident_from_dip() returned errno %d", status); 47321ae08745Sheppo return (DDI_FAILURE); 47331ae08745Sheppo } 47341ae08745Sheppo vds->initialized |= VDS_LDI; 47351ae08745Sheppo 47361ae08745Sheppo /* Register for MD updates */ 4737445b4c2eSsb155480 sz = sizeof (vds_prop_template); 4738445b4c2eSsb155480 pspecp = kmem_alloc(sz, KM_SLEEP); 4739445b4c2eSsb155480 bcopy(vds_prop_template, pspecp, sz); 4740445b4c2eSsb155480 4741445b4c2eSsb155480 VDS_SET_MDEG_PROP_INST(pspecp, cfg_handle); 4742445b4c2eSsb155480 4743445b4c2eSsb155480 /* initialize the complete prop spec structure */ 4744445b4c2eSsb155480 ispecp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP); 4745445b4c2eSsb155480 ispecp->namep = "virtual-device"; 4746445b4c2eSsb155480 ispecp->specp = pspecp; 4747445b4c2eSsb155480 4748445b4c2eSsb155480 if (mdeg_register(ispecp, &vd_match, vds_process_md, vds, 47491ae08745Sheppo &vds->mdeg) != MDEG_SUCCESS) { 47501ae08745Sheppo PRN("Unable to register for MD updates"); 4751445b4c2eSsb155480 kmem_free(ispecp, sizeof (mdeg_node_spec_t)); 4752445b4c2eSsb155480 kmem_free(pspecp, sz); 47531ae08745Sheppo return (DDI_FAILURE); 47541ae08745Sheppo } 4755445b4c2eSsb155480 4756445b4c2eSsb155480 vds->ispecp = ispecp; 47571ae08745Sheppo vds->initialized |= VDS_MDEG; 47581ae08745Sheppo 47590a55fbb7Slm66018 /* Prevent auto-detaching so driver is available whenever MD changes */ 47600a55fbb7Slm66018 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1) != 47610a55fbb7Slm66018 DDI_PROP_SUCCESS) { 47620a55fbb7Slm66018 PRN("failed to set \"%s\" property for instance %u", 47630a55fbb7Slm66018 DDI_NO_AUTODETACH, instance); 47640a55fbb7Slm66018 } 47650a55fbb7Slm66018 47661ae08745Sheppo ddi_report_dev(dip); 47671ae08745Sheppo return (DDI_SUCCESS); 47681ae08745Sheppo } 47691ae08745Sheppo 47701ae08745Sheppo static int 47711ae08745Sheppo vds_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 47721ae08745Sheppo { 47731ae08745Sheppo int status; 47741ae08745Sheppo 47751ae08745Sheppo switch (cmd) { 47761ae08745Sheppo case DDI_ATTACH: 4777d10e4ef2Snarayan PR0("Attaching"); 47781ae08745Sheppo if ((status = vds_do_attach(dip)) != DDI_SUCCESS) 47791ae08745Sheppo (void) vds_detach(dip, DDI_DETACH); 47801ae08745Sheppo return (status); 47811ae08745Sheppo case DDI_RESUME: 4782d10e4ef2Snarayan PR0("No action required for DDI_RESUME"); 47831ae08745Sheppo return (DDI_SUCCESS); 47841ae08745Sheppo default: 47851ae08745Sheppo return (DDI_FAILURE); 47861ae08745Sheppo } 47871ae08745Sheppo } 47881ae08745Sheppo 47891ae08745Sheppo static struct dev_ops vds_ops = { 47901ae08745Sheppo DEVO_REV, /* devo_rev */ 47911ae08745Sheppo 0, /* devo_refcnt */ 47921ae08745Sheppo ddi_no_info, /* devo_getinfo */ 47931ae08745Sheppo nulldev, /* devo_identify */ 47941ae08745Sheppo nulldev, /* devo_probe */ 47951ae08745Sheppo vds_attach, /* devo_attach */ 47961ae08745Sheppo vds_detach, /* devo_detach */ 47971ae08745Sheppo nodev, /* devo_reset */ 47981ae08745Sheppo NULL, /* devo_cb_ops */ 47991ae08745Sheppo NULL, /* devo_bus_ops */ 48001ae08745Sheppo nulldev /* devo_power */ 48011ae08745Sheppo }; 48021ae08745Sheppo 48031ae08745Sheppo static struct modldrv modldrv = { 48041ae08745Sheppo &mod_driverops, 4805205eeb1aSlm66018 "virtual disk server", 48061ae08745Sheppo &vds_ops, 48071ae08745Sheppo }; 48081ae08745Sheppo 48091ae08745Sheppo static struct modlinkage modlinkage = { 48101ae08745Sheppo MODREV_1, 48111ae08745Sheppo &modldrv, 48121ae08745Sheppo NULL 48131ae08745Sheppo }; 48141ae08745Sheppo 48151ae08745Sheppo 48161ae08745Sheppo int 48171ae08745Sheppo _init(void) 48181ae08745Sheppo { 48191ae08745Sheppo int i, status; 48201ae08745Sheppo 4821d10e4ef2Snarayan 48221ae08745Sheppo if ((status = ddi_soft_state_init(&vds_state, sizeof (vds_t), 1)) != 0) 48231ae08745Sheppo return (status); 48241ae08745Sheppo if ((status = mod_install(&modlinkage)) != 0) { 48251ae08745Sheppo ddi_soft_state_fini(&vds_state); 48261ae08745Sheppo return (status); 48271ae08745Sheppo } 48281ae08745Sheppo 48291ae08745Sheppo /* Fill in the bit-mask of server-supported operations */ 48301ae08745Sheppo for (i = 0; i < vds_noperations; i++) 48311ae08745Sheppo vds_operations |= 1 << (vds_operation[i].operation - 1); 48321ae08745Sheppo 48331ae08745Sheppo return (0); 48341ae08745Sheppo } 48351ae08745Sheppo 48361ae08745Sheppo int 48371ae08745Sheppo _info(struct modinfo *modinfop) 48381ae08745Sheppo { 48391ae08745Sheppo return (mod_info(&modlinkage, modinfop)); 48401ae08745Sheppo } 48411ae08745Sheppo 48421ae08745Sheppo int 48431ae08745Sheppo _fini(void) 48441ae08745Sheppo { 48451ae08745Sheppo int status; 48461ae08745Sheppo 4847d10e4ef2Snarayan 48481ae08745Sheppo if ((status = mod_remove(&modlinkage)) != 0) 48491ae08745Sheppo return (status); 48501ae08745Sheppo ddi_soft_state_fini(&vds_state); 48511ae08745Sheppo return (0); 48521ae08745Sheppo } 4853