1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * Virtual disk server 29 */ 30 31 32 #include <sys/types.h> 33 #include <sys/conf.h> 34 #include <sys/crc32.h> 35 #include <sys/ddi.h> 36 #include <sys/dkio.h> 37 #include <sys/file.h> 38 #include <sys/fs/hsfs_isospec.h> 39 #include <sys/mdeg.h> 40 #include <sys/mhd.h> 41 #include <sys/modhash.h> 42 #include <sys/note.h> 43 #include <sys/pathname.h> 44 #include <sys/sdt.h> 45 #include <sys/sunddi.h> 46 #include <sys/sunldi.h> 47 #include <sys/sysmacros.h> 48 #include <sys/vio_common.h> 49 #include <sys/vio_util.h> 50 #include <sys/vdsk_mailbox.h> 51 #include <sys/vdsk_common.h> 52 #include <sys/vtoc.h> 53 #include <sys/vfs.h> 54 #include <sys/stat.h> 55 #include <sys/scsi/impl/uscsi.h> 56 #include <sys/ontrap.h> 57 #include <vm/seg_map.h> 58 59 #define ONE_MEGABYTE (1ULL << 20) 60 #define ONE_GIGABYTE (1ULL << 30) 61 #define ONE_TERABYTE (1ULL << 40) 62 63 /* Virtual disk server initialization flags */ 64 #define VDS_LDI 0x01 65 #define VDS_MDEG 0x02 66 67 /* Virtual disk server tunable parameters */ 68 #define VDS_RETRIES 5 69 #define VDS_LDC_DELAY 1000 /* 1 msecs */ 70 #define VDS_DEV_DELAY 10000000 /* 10 secs */ 71 #define VDS_NCHAINS 32 72 73 /* Identification parameters for MD, synthetic dkio(7i) structures, etc. */ 74 #define VDS_NAME "virtual-disk-server" 75 76 #define VD_NAME "vd" 77 #define VD_VOLUME_NAME "vdisk" 78 #define VD_ASCIILABEL "Virtual Disk" 79 80 #define VD_CHANNEL_ENDPOINT "channel-endpoint" 81 #define VD_ID_PROP "id" 82 #define VD_BLOCK_DEVICE_PROP "vds-block-device" 83 #define VD_BLOCK_DEVICE_OPTS "vds-block-device-opts" 84 #define VD_REG_PROP "reg" 85 86 /* Virtual disk initialization flags */ 87 #define VD_DISK_READY 0x01 88 #define VD_LOCKING 0x02 89 #define VD_LDC 0x04 90 #define VD_DRING 0x08 91 #define VD_SID 0x10 92 #define VD_SEQ_NUM 0x20 93 #define VD_SETUP_ERROR 0x40 94 95 /* Flags for writing to a vdisk which is a file */ 96 #define VD_FILE_WRITE_FLAGS SM_ASYNC 97 98 /* Number of backup labels */ 99 #define VD_FILE_NUM_BACKUP 5 100 101 /* Timeout for SCSI I/O */ 102 #define VD_SCSI_RDWR_TIMEOUT 30 /* 30 secs */ 103 104 /* Maximum number of logical partitions */ 105 #define VD_MAXPART (NDKMAP + 1) 106 107 /* 108 * By Solaris convention, slice/partition 2 represents the entire disk; 109 * unfortunately, this convention does not appear to be codified. 110 */ 111 #define VD_ENTIRE_DISK_SLICE 2 112 113 /* Logical block address for EFI */ 114 #define VD_EFI_LBA_GPT 1 /* LBA of the GPT */ 115 #define VD_EFI_LBA_GPE 2 /* LBA of the GPE */ 116 117 /* Driver types */ 118 typedef enum vd_driver { 119 VD_DRIVER_UNKNOWN = 0, /* driver type unknown */ 120 VD_DRIVER_DISK, /* disk driver */ 121 VD_DRIVER_VOLUME /* volume driver */ 122 } vd_driver_t; 123 124 #define VD_DRIVER_NAME_LEN 64 125 126 #define VDS_NUM_DRIVERS (sizeof (vds_driver_types) / sizeof (vd_driver_type_t)) 127 128 typedef struct vd_driver_type { 129 char name[VD_DRIVER_NAME_LEN]; /* driver name */ 130 vd_driver_t type; /* driver type (disk or volume) */ 131 } vd_driver_type_t; 132 133 /* 134 * There is no reliable way to determine if a device is representing a disk 135 * or a volume, especially with pseudo devices. So we maintain a list of well 136 * known drivers and the type of device they represent (either a disk or a 137 * volume). 138 * 139 * The list can be extended by adding a "driver-type-list" entry in vds.conf 140 * with the following syntax: 141 * 142 * driver-type-list="<driver>:<type>", ... ,"<driver>:<type>"; 143 * 144 * Where: 145 * <driver> is the name of a driver (limited to 64 characters) 146 * <type> is either the string "disk" or "volume" 147 * 148 * Invalid entries in "driver-type-list" will be ignored. 149 * 150 * For example, the following line in vds.conf: 151 * 152 * driver-type-list="foo:disk","bar:volume"; 153 * 154 * defines that "foo" is a disk driver, and driver "bar" is a volume driver. 155 * 156 * When a list is defined in vds.conf, it is checked before the built-in list 157 * (vds_driver_types[]) so that any definition from this list can be overriden 158 * using vds.conf. 159 */ 160 vd_driver_type_t vds_driver_types[] = { 161 { "dad", VD_DRIVER_DISK }, /* Solaris */ 162 { "did", VD_DRIVER_DISK }, /* Sun Cluster */ 163 { "emcp", VD_DRIVER_DISK }, /* EMC Powerpath */ 164 { "lofi", VD_DRIVER_VOLUME }, /* Solaris */ 165 { "md", VD_DRIVER_VOLUME }, /* Solaris - SVM */ 166 { "sd", VD_DRIVER_DISK }, /* Solaris */ 167 { "ssd", VD_DRIVER_DISK }, /* Solaris */ 168 { "vdc", VD_DRIVER_DISK }, /* Solaris */ 169 { "vxdmp", VD_DRIVER_DISK }, /* Veritas */ 170 { "vxio", VD_DRIVER_VOLUME }, /* Veritas - VxVM */ 171 { "zfs", VD_DRIVER_VOLUME } /* Solaris */ 172 }; 173 174 /* Return a cpp token as a string */ 175 #define STRINGIZE(token) #token 176 177 /* 178 * Print a message prefixed with the current function name to the message log 179 * (and optionally to the console for verbose boots); these macros use cpp's 180 * concatenation of string literals and C99 variable-length-argument-list 181 * macros 182 */ 183 #define PRN(...) _PRN("?%s(): "__VA_ARGS__, "") 184 #define _PRN(format, ...) \ 185 cmn_err(CE_CONT, format"%s", __func__, __VA_ARGS__) 186 187 /* Return a pointer to the "i"th vdisk dring element */ 188 #define VD_DRING_ELEM(i) ((vd_dring_entry_t *)(void *) \ 189 (vd->dring + (i)*vd->descriptor_size)) 190 191 /* Return the virtual disk client's type as a string (for use in messages) */ 192 #define VD_CLIENT(vd) \ 193 (((vd)->xfer_mode == VIO_DESC_MODE) ? "in-band client" : \ 194 (((vd)->xfer_mode == VIO_DRING_MODE_V1_0) ? "dring client" : \ 195 (((vd)->xfer_mode == 0) ? "null client" : \ 196 "unsupported client"))) 197 198 /* Read disk label from a disk on file */ 199 #define VD_FILE_LABEL_READ(vd, labelp) \ 200 vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)labelp, \ 201 0, sizeof (struct dk_label)) 202 203 /* Write disk label to a disk on file */ 204 #define VD_FILE_LABEL_WRITE(vd, labelp) \ 205 vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)labelp, \ 206 0, sizeof (struct dk_label)) 207 208 /* Message for disk access rights reset failure */ 209 #define VD_RESET_ACCESS_FAILURE_MSG \ 210 "Fail to reset disk access rights for disk %s" 211 212 /* 213 * Specification of an MD node passed to the MDEG to filter any 214 * 'vport' nodes that do not belong to the specified node. This 215 * template is copied for each vds instance and filled in with 216 * the appropriate 'cfg-handle' value before being passed to the MDEG. 217 */ 218 static mdeg_prop_spec_t vds_prop_template[] = { 219 { MDET_PROP_STR, "name", VDS_NAME }, 220 { MDET_PROP_VAL, "cfg-handle", NULL }, 221 { MDET_LIST_END, NULL, NULL } 222 }; 223 224 #define VDS_SET_MDEG_PROP_INST(specp, val) (specp)[1].ps_val = (val); 225 226 /* 227 * Matching criteria passed to the MDEG to register interest 228 * in changes to 'virtual-device-port' nodes identified by their 229 * 'id' property. 230 */ 231 static md_prop_match_t vd_prop_match[] = { 232 { MDET_PROP_VAL, VD_ID_PROP }, 233 { MDET_LIST_END, NULL } 234 }; 235 236 static mdeg_node_match_t vd_match = {"virtual-device-port", 237 vd_prop_match}; 238 239 /* 240 * Options for the VD_BLOCK_DEVICE_OPTS property. 241 */ 242 #define VD_OPT_RDONLY 0x1 /* read-only */ 243 #define VD_OPT_SLICE 0x2 /* single slice */ 244 #define VD_OPT_EXCLUSIVE 0x4 /* exclusive access */ 245 246 #define VD_OPTION_NLEN 128 247 248 typedef struct vd_option { 249 char vdo_name[VD_OPTION_NLEN]; 250 uint64_t vdo_value; 251 } vd_option_t; 252 253 vd_option_t vd_bdev_options[] = { 254 { "ro", VD_OPT_RDONLY }, 255 { "slice", VD_OPT_SLICE }, 256 { "excl", VD_OPT_EXCLUSIVE } 257 }; 258 259 /* Debugging macros */ 260 #ifdef DEBUG 261 262 static int vd_msglevel = 0; 263 264 #define PR0 if (vd_msglevel > 0) PRN 265 #define PR1 if (vd_msglevel > 1) PRN 266 #define PR2 if (vd_msglevel > 2) PRN 267 268 #define VD_DUMP_DRING_ELEM(elem) \ 269 PR0("dst:%x op:%x st:%u nb:%lx addr:%lx ncook:%u\n", \ 270 elem->hdr.dstate, \ 271 elem->payload.operation, \ 272 elem->payload.status, \ 273 elem->payload.nbytes, \ 274 elem->payload.addr, \ 275 elem->payload.ncookies); 276 277 char * 278 vd_decode_state(int state) 279 { 280 char *str; 281 282 #define CASE_STATE(_s) case _s: str = #_s; break; 283 284 switch (state) { 285 CASE_STATE(VD_STATE_INIT) 286 CASE_STATE(VD_STATE_VER) 287 CASE_STATE(VD_STATE_ATTR) 288 CASE_STATE(VD_STATE_DRING) 289 CASE_STATE(VD_STATE_RDX) 290 CASE_STATE(VD_STATE_DATA) 291 default: str = "unknown"; break; 292 } 293 294 #undef CASE_STATE 295 296 return (str); 297 } 298 299 void 300 vd_decode_tag(vio_msg_t *msg) 301 { 302 char *tstr, *sstr, *estr; 303 304 #define CASE_TYPE(_s) case _s: tstr = #_s; break; 305 306 switch (msg->tag.vio_msgtype) { 307 CASE_TYPE(VIO_TYPE_CTRL) 308 CASE_TYPE(VIO_TYPE_DATA) 309 CASE_TYPE(VIO_TYPE_ERR) 310 default: tstr = "unknown"; break; 311 } 312 313 #undef CASE_TYPE 314 315 #define CASE_SUBTYPE(_s) case _s: sstr = #_s; break; 316 317 switch (msg->tag.vio_subtype) { 318 CASE_SUBTYPE(VIO_SUBTYPE_INFO) 319 CASE_SUBTYPE(VIO_SUBTYPE_ACK) 320 CASE_SUBTYPE(VIO_SUBTYPE_NACK) 321 default: sstr = "unknown"; break; 322 } 323 324 #undef CASE_SUBTYPE 325 326 #define CASE_ENV(_s) case _s: estr = #_s; break; 327 328 switch (msg->tag.vio_subtype_env) { 329 CASE_ENV(VIO_VER_INFO) 330 CASE_ENV(VIO_ATTR_INFO) 331 CASE_ENV(VIO_DRING_REG) 332 CASE_ENV(VIO_DRING_UNREG) 333 CASE_ENV(VIO_RDX) 334 CASE_ENV(VIO_PKT_DATA) 335 CASE_ENV(VIO_DESC_DATA) 336 CASE_ENV(VIO_DRING_DATA) 337 default: estr = "unknown"; break; 338 } 339 340 #undef CASE_ENV 341 342 PR1("(%x/%x/%x) message : (%s/%s/%s)", 343 msg->tag.vio_msgtype, msg->tag.vio_subtype, 344 msg->tag.vio_subtype_env, tstr, sstr, estr); 345 } 346 347 #else /* !DEBUG */ 348 349 #define PR0(...) 350 #define PR1(...) 351 #define PR2(...) 352 353 #define VD_DUMP_DRING_ELEM(elem) 354 355 #define vd_decode_state(_s) (NULL) 356 #define vd_decode_tag(_s) (NULL) 357 358 #endif /* DEBUG */ 359 360 361 /* 362 * Soft state structure for a vds instance 363 */ 364 typedef struct vds { 365 uint_t initialized; /* driver inst initialization flags */ 366 dev_info_t *dip; /* driver inst devinfo pointer */ 367 ldi_ident_t ldi_ident; /* driver's identifier for LDI */ 368 mod_hash_t *vd_table; /* table of virtual disks served */ 369 mdeg_node_spec_t *ispecp; /* mdeg node specification */ 370 mdeg_handle_t mdeg; /* handle for MDEG operations */ 371 vd_driver_type_t *driver_types; /* extra driver types (from vds.conf) */ 372 int num_drivers; /* num of extra driver types */ 373 } vds_t; 374 375 /* 376 * Types of descriptor-processing tasks 377 */ 378 typedef enum vd_task_type { 379 VD_NONFINAL_RANGE_TASK, /* task for intermediate descriptor in range */ 380 VD_FINAL_RANGE_TASK, /* task for last in a range of descriptors */ 381 } vd_task_type_t; 382 383 /* 384 * Structure describing the task for processing a descriptor 385 */ 386 typedef struct vd_task { 387 struct vd *vd; /* vd instance task is for */ 388 vd_task_type_t type; /* type of descriptor task */ 389 int index; /* dring elem index for task */ 390 vio_msg_t *msg; /* VIO message task is for */ 391 size_t msglen; /* length of message content */ 392 vd_dring_payload_t *request; /* request task will perform */ 393 struct buf buf; /* buf(9s) for I/O request */ 394 ldc_mem_handle_t mhdl; /* task memory handle */ 395 int status; /* status of processing task */ 396 int (*completef)(struct vd_task *task); /* completion func ptr */ 397 } vd_task_t; 398 399 /* 400 * Soft state structure for a virtual disk instance 401 */ 402 typedef struct vd { 403 uint_t initialized; /* vdisk initialization flags */ 404 uint64_t operations; /* bitmask of VD_OPs exported */ 405 vio_ver_t version; /* ver negotiated with client */ 406 vds_t *vds; /* server for this vdisk */ 407 ddi_taskq_t *startq; /* queue for I/O start tasks */ 408 ddi_taskq_t *completionq; /* queue for completion tasks */ 409 ldi_handle_t ldi_handle[V_NUMPAR]; /* LDI slice handles */ 410 char device_path[MAXPATHLEN + 1]; /* vdisk device */ 411 dev_t dev[V_NUMPAR]; /* dev numbers for slices */ 412 int open_flags; /* open flags */ 413 uint_t nslices; /* number of slices we export */ 414 size_t vdisk_size; /* number of blocks in vdisk */ 415 size_t vdisk_block_size; /* size of each vdisk block */ 416 vd_disk_type_t vdisk_type; /* slice or entire disk */ 417 vd_disk_label_t vdisk_label; /* EFI or VTOC label */ 418 vd_media_t vdisk_media; /* media type of backing dev. */ 419 boolean_t is_atapi_dev; /* Is this an IDE CD-ROM dev? */ 420 ushort_t max_xfer_sz; /* max xfer size in DEV_BSIZE */ 421 size_t block_size; /* blk size of actual device */ 422 boolean_t volume; /* is vDisk backed by volume */ 423 boolean_t file; /* is vDisk backed by a file? */ 424 boolean_t scsi; /* is vDisk backed by scsi? */ 425 vnode_t *file_vnode; /* file vnode */ 426 size_t file_size; /* file size */ 427 ddi_devid_t file_devid; /* devid for disk image */ 428 int efi_reserved; /* EFI reserved slice */ 429 caddr_t flabel; /* fake label for slice type */ 430 uint_t flabel_size; /* fake label size */ 431 uint_t flabel_limit; /* limit of the fake label */ 432 struct dk_geom dk_geom; /* synthetic for slice type */ 433 struct extvtoc vtoc; /* synthetic for slice type */ 434 vd_slice_t slices[VD_MAXPART]; /* logical partitions */ 435 boolean_t ownership; /* disk ownership status */ 436 ldc_status_t ldc_state; /* LDC connection state */ 437 ldc_handle_t ldc_handle; /* handle for LDC comm */ 438 size_t max_msglen; /* largest LDC message len */ 439 vd_state_t state; /* client handshake state */ 440 uint8_t xfer_mode; /* transfer mode with client */ 441 uint32_t sid; /* client's session ID */ 442 uint64_t seq_num; /* message sequence number */ 443 uint64_t dring_ident; /* identifier of dring */ 444 ldc_dring_handle_t dring_handle; /* handle for dring ops */ 445 uint32_t descriptor_size; /* num bytes in desc */ 446 uint32_t dring_len; /* number of dring elements */ 447 uint8_t dring_mtype; /* dring mem map type */ 448 caddr_t dring; /* address of dring */ 449 caddr_t vio_msgp; /* vio msg staging buffer */ 450 vd_task_t inband_task; /* task for inband descriptor */ 451 vd_task_t *dring_task; /* tasks dring elements */ 452 453 kmutex_t lock; /* protects variables below */ 454 boolean_t enabled; /* is vdisk enabled? */ 455 boolean_t reset_state; /* reset connection state? */ 456 boolean_t reset_ldc; /* reset LDC channel? */ 457 } vd_t; 458 459 /* 460 * Macros to manipulate the fake label (flabel) for single slice disks. 461 * 462 * If we fake a VTOC label then the fake label consists of only one block 463 * containing the VTOC label (struct dk_label). 464 * 465 * If we fake an EFI label then the fake label consists of a blank block 466 * followed by a GPT (efi_gpt_t) and a GPE (efi_gpe_t). 467 * 468 */ 469 #define VD_LABEL_VTOC_SIZE \ 470 P2ROUNDUP(sizeof (struct dk_label), DEV_BSIZE) 471 472 #define VD_LABEL_EFI_SIZE \ 473 P2ROUNDUP(DEV_BSIZE + sizeof (efi_gpt_t) + \ 474 sizeof (efi_gpe_t) * VD_MAXPART, DEV_BSIZE) 475 476 #define VD_LABEL_VTOC(vd) \ 477 ((struct dk_label *)(void *)((vd)->flabel)) 478 479 #define VD_LABEL_EFI_GPT(vd) \ 480 ((efi_gpt_t *)(void *)((vd)->flabel + DEV_BSIZE)) 481 #define VD_LABEL_EFI_GPE(vd) \ 482 ((efi_gpe_t *)(void *)((vd)->flabel + DEV_BSIZE + \ 483 sizeof (efi_gpt_t))) 484 485 486 typedef struct vds_operation { 487 char *namep; 488 uint8_t operation; 489 int (*start)(vd_task_t *task); 490 int (*complete)(vd_task_t *task); 491 } vds_operation_t; 492 493 typedef struct vd_ioctl { 494 uint8_t operation; /* vdisk operation */ 495 const char *operation_name; /* vdisk operation name */ 496 size_t nbytes; /* size of operation buffer */ 497 int cmd; /* corresponding ioctl cmd */ 498 const char *cmd_name; /* ioctl cmd name */ 499 void *arg; /* ioctl cmd argument */ 500 /* convert input vd_buf to output ioctl_arg */ 501 int (*copyin)(void *vd_buf, size_t, void *ioctl_arg); 502 /* convert input ioctl_arg to output vd_buf */ 503 void (*copyout)(void *ioctl_arg, void *vd_buf); 504 /* write is true if the operation writes any data to the backend */ 505 boolean_t write; 506 } vd_ioctl_t; 507 508 /* Define trivial copyin/copyout conversion function flag */ 509 #define VD_IDENTITY_IN ((int (*)(void *, size_t, void *))-1) 510 #define VD_IDENTITY_OUT ((void (*)(void *, void *))-1) 511 512 513 static int vds_ldc_retries = VDS_RETRIES; 514 static int vds_ldc_delay = VDS_LDC_DELAY; 515 static int vds_dev_retries = VDS_RETRIES; 516 static int vds_dev_delay = VDS_DEV_DELAY; 517 static void *vds_state; 518 519 static uint_t vd_file_write_flags = VD_FILE_WRITE_FLAGS; 520 521 static short vd_scsi_rdwr_timeout = VD_SCSI_RDWR_TIMEOUT; 522 static int vd_scsi_debug = USCSI_SILENT; 523 524 /* 525 * Tunable to define the behavior of the service domain if the vdisk server 526 * fails to reset disk exclusive access when a LDC channel is reset. When a 527 * LDC channel is reset the vdisk server will try to reset disk exclusive 528 * access by releasing any SCSI-2 reservation or resetting the disk. If these 529 * actions fail then the default behavior (vd_reset_access_failure = 0) is to 530 * print a warning message. This default behavior can be changed by setting 531 * the vd_reset_access_failure variable to A_REBOOT (= 0x1) and that will 532 * cause the service domain to reboot, or A_DUMP (= 0x5) and that will cause 533 * the service domain to panic. In both cases, the reset of the service domain 534 * should trigger a reset SCSI buses and hopefully clear any SCSI-2 reservation. 535 */ 536 static int vd_reset_access_failure = 0; 537 538 /* 539 * Tunable for backward compatibility. When this variable is set to B_TRUE, 540 * all disk volumes (ZFS, SVM, VxvM volumes) will be exported as single 541 * slice disks whether or not they have the "slice" option set. This is 542 * to provide a simple backward compatibility mechanism when upgrading 543 * the vds driver and using a domain configuration created before the 544 * "slice" option was available. 545 */ 546 static boolean_t vd_volume_force_slice = B_FALSE; 547 548 /* 549 * The label of disk images created with some earlier versions of the virtual 550 * disk software is not entirely correct and have an incorrect v_sanity field 551 * (usually 0) instead of VTOC_SANE. This creates a compatibility problem with 552 * these images because we are now validating that the disk label (and the 553 * sanity) is correct when a disk image is opened. 554 * 555 * This tunable is set to false to not validate the sanity field and ensure 556 * compatibility. If the tunable is set to true, we will do a strict checking 557 * of the sanity but this can create compatibility problems with old disk 558 * images. 559 */ 560 static boolean_t vd_file_validate_sanity = B_FALSE; 561 562 /* 563 * Enables the use of LDC_DIRECT_MAP when mapping in imported descriptor rings. 564 */ 565 static boolean_t vd_direct_mapped_drings = B_TRUE; 566 567 /* 568 * When a backend is exported as a single-slice disk then we entirely fake 569 * its disk label. So it can be exported either with a VTOC label or with 570 * an EFI label. If vd_slice_label is set to VD_DISK_LABEL_VTOC then all 571 * single-slice disks will be exported with a VTOC label; and if it is set 572 * to VD_DISK_LABEL_EFI then all single-slice disks will be exported with 573 * an EFI label. 574 * 575 * If vd_slice_label is set to VD_DISK_LABEL_UNK and the backend is a disk 576 * or volume device then it will be exported with the same type of label as 577 * defined on the device. Otherwise if the backend is a file then it will 578 * exported with the disk label type set in the vd_file_slice_label variable. 579 * 580 * Note that if the backend size is greater than 1TB then it will always be 581 * exported with an EFI label no matter what the setting is. 582 */ 583 static vd_disk_label_t vd_slice_label = VD_DISK_LABEL_UNK; 584 585 static vd_disk_label_t vd_file_slice_label = VD_DISK_LABEL_VTOC; 586 587 /* 588 * Tunable for backward compatibility. If this variable is set to B_TRUE then 589 * single-slice disks are exported as disks with only one slice instead of 590 * faking a complete disk partitioning. 591 */ 592 static boolean_t vd_slice_single_slice = B_FALSE; 593 594 /* 595 * Supported protocol version pairs, from highest (newest) to lowest (oldest) 596 * 597 * Each supported major version should appear only once, paired with (and only 598 * with) its highest supported minor version number (as the protocol requires 599 * supporting all lower minor version numbers as well) 600 */ 601 static const vio_ver_t vds_version[] = {{1, 1}}; 602 static const size_t vds_num_versions = 603 sizeof (vds_version)/sizeof (vds_version[0]); 604 605 static void vd_free_dring_task(vd_t *vdp); 606 static int vd_setup_vd(vd_t *vd); 607 static int vd_setup_single_slice_disk(vd_t *vd); 608 static int vd_backend_check_size(vd_t *vd); 609 static boolean_t vd_enabled(vd_t *vd); 610 static ushort_t vd_lbl2cksum(struct dk_label *label); 611 static int vd_file_validate_geometry(vd_t *vd); 612 static boolean_t vd_file_is_iso_image(vd_t *vd); 613 static void vd_set_exported_operations(vd_t *vd); 614 static void vd_reset_access(vd_t *vd); 615 static int vd_backend_ioctl(vd_t *vd, int cmd, caddr_t arg); 616 static int vds_efi_alloc_and_read(vd_t *, efi_gpt_t **, efi_gpe_t **); 617 static void vds_efi_free(vd_t *, efi_gpt_t *, efi_gpe_t *); 618 static void vds_driver_types_free(vds_t *vds); 619 static void vd_vtocgeom_to_label(struct extvtoc *vtoc, struct dk_geom *geom, 620 struct dk_label *label); 621 static void vd_label_to_vtocgeom(struct dk_label *label, struct extvtoc *vtoc, 622 struct dk_geom *geom); 623 static boolean_t vd_slice_geom_isvalid(vd_t *vd, struct dk_geom *geom); 624 static boolean_t vd_slice_vtoc_isvalid(vd_t *vd, struct extvtoc *vtoc); 625 626 extern int is_pseudo_device(dev_info_t *); 627 628 /* 629 * Function: 630 * vd_get_readable_size 631 * 632 * Description: 633 * Convert a given size in bytes to a human readable format in 634 * kilobytes, megabytes, gigabytes or terabytes. 635 * 636 * Parameters: 637 * full_size - the size to convert in bytes. 638 * size - the converted size. 639 * unit - the unit of the converted size: 'K' (kilobyte), 640 * 'M' (Megabyte), 'G' (Gigabyte), 'T' (Terabyte). 641 * 642 * Return Code: 643 * none 644 */ 645 void 646 vd_get_readable_size(size_t full_size, size_t *size, char *unit) 647 { 648 if (full_size < (1ULL << 20)) { 649 *size = full_size >> 10; 650 *unit = 'K'; /* Kilobyte */ 651 } else if (full_size < (1ULL << 30)) { 652 *size = full_size >> 20; 653 *unit = 'M'; /* Megabyte */ 654 } else if (full_size < (1ULL << 40)) { 655 *size = full_size >> 30; 656 *unit = 'G'; /* Gigabyte */ 657 } else { 658 *size = full_size >> 40; 659 *unit = 'T'; /* Terabyte */ 660 } 661 } 662 663 /* 664 * Function: 665 * vd_file_rw 666 * 667 * Description: 668 * Read or write to a disk on file. 669 * 670 * Parameters: 671 * vd - disk on which the operation is performed. 672 * slice - slice on which the operation is performed, 673 * VD_SLICE_NONE indicates that the operation 674 * is done using an absolute disk offset. 675 * operation - operation to execute: read (VD_OP_BREAD) or 676 * write (VD_OP_BWRITE). 677 * data - buffer where data are read to or written from. 678 * blk - starting block for the operation. 679 * len - number of bytes to read or write. 680 * 681 * Return Code: 682 * n >= 0 - success, n indicates the number of bytes read 683 * or written. 684 * -1 - error. 685 */ 686 static ssize_t 687 vd_file_rw(vd_t *vd, int slice, int operation, caddr_t data, size_t blk, 688 size_t len) 689 { 690 caddr_t maddr; 691 size_t offset, maxlen, moffset, mlen, n; 692 uint_t smflags; 693 enum seg_rw srw; 694 695 ASSERT(vd->file); 696 ASSERT(len > 0); 697 698 /* 699 * If a file is exported as a slice then we don't care about the vtoc. 700 * In that case, the vtoc is a fake mainly to make newfs happy and we 701 * handle any I/O as a raw disk access so that we can have access to the 702 * entire backend. 703 */ 704 if (vd->vdisk_type == VD_DISK_TYPE_SLICE || slice == VD_SLICE_NONE) { 705 /* raw disk access */ 706 offset = blk * DEV_BSIZE; 707 if (offset >= vd->file_size) { 708 /* offset past the end of the disk */ 709 PR0("offset (0x%lx) >= fsize (0x%lx)", 710 offset, vd->file_size); 711 return (0); 712 } 713 maxlen = vd->file_size - offset; 714 } else { 715 ASSERT(slice >= 0 && slice < V_NUMPAR); 716 717 /* 718 * v1.0 vDisk clients depended on the server not verifying 719 * the label of a unformatted disk. This "feature" is 720 * maintained for backward compatibility but all versions 721 * from v1.1 onwards must do the right thing. 722 */ 723 if (vd->vdisk_label == VD_DISK_LABEL_UNK && 724 vio_ver_is_supported(vd->version, 1, 1)) { 725 (void) vd_file_validate_geometry(vd); 726 if (vd->vdisk_label == VD_DISK_LABEL_UNK) { 727 PR0("Unknown disk label, can't do I/O " 728 "from slice %d", slice); 729 return (-1); 730 } 731 } 732 733 if (vd->vdisk_label == VD_DISK_LABEL_VTOC) { 734 ASSERT(vd->vtoc.v_sectorsz == DEV_BSIZE); 735 } else { 736 ASSERT(vd->vdisk_label == VD_DISK_LABEL_EFI); 737 ASSERT(vd->vdisk_block_size == DEV_BSIZE); 738 } 739 740 if (blk >= vd->slices[slice].nblocks) { 741 /* address past the end of the slice */ 742 PR0("req_addr (0x%lx) >= psize (0x%lx)", 743 blk, vd->slices[slice].nblocks); 744 return (0); 745 } 746 747 offset = (vd->slices[slice].start + blk) * DEV_BSIZE; 748 maxlen = (vd->slices[slice].nblocks - blk) * DEV_BSIZE; 749 } 750 751 /* 752 * If the requested size is greater than the size 753 * of the partition, truncate the read/write. 754 */ 755 if (len > maxlen) { 756 PR0("I/O size truncated to %lu bytes from %lu bytes", 757 maxlen, len); 758 len = maxlen; 759 } 760 761 /* 762 * We have to ensure that we are reading/writing into the mmap 763 * range. If we have a partial disk image (e.g. an image of 764 * s0 instead s2) the system can try to access slices that 765 * are not included into the disk image. 766 */ 767 if ((offset + len) > vd->file_size) { 768 PR0("offset + nbytes (0x%lx + 0x%lx) > " 769 "file_size (0x%lx)", offset, len, vd->file_size); 770 return (-1); 771 } 772 773 srw = (operation == VD_OP_BREAD)? S_READ : S_WRITE; 774 smflags = (operation == VD_OP_BREAD)? 0 : 775 (SM_WRITE | vd_file_write_flags); 776 n = len; 777 778 do { 779 /* 780 * segmap_getmapflt() returns a MAXBSIZE chunk which is 781 * MAXBSIZE aligned. 782 */ 783 moffset = offset & MAXBOFFSET; 784 mlen = MIN(MAXBSIZE - moffset, n); 785 maddr = segmap_getmapflt(segkmap, vd->file_vnode, offset, 786 mlen, 1, srw); 787 /* 788 * Fault in the pages so we can check for error and ensure 789 * that we can safely used the mapped address. 790 */ 791 if (segmap_fault(kas.a_hat, segkmap, maddr, mlen, 792 F_SOFTLOCK, srw) != 0) { 793 (void) segmap_release(segkmap, maddr, 0); 794 return (-1); 795 } 796 797 if (operation == VD_OP_BREAD) 798 bcopy(maddr + moffset, data, mlen); 799 else 800 bcopy(data, maddr + moffset, mlen); 801 802 if (segmap_fault(kas.a_hat, segkmap, maddr, mlen, 803 F_SOFTUNLOCK, srw) != 0) { 804 (void) segmap_release(segkmap, maddr, 0); 805 return (-1); 806 } 807 if (segmap_release(segkmap, maddr, smflags) != 0) 808 return (-1); 809 n -= mlen; 810 offset += mlen; 811 data += mlen; 812 813 } while (n > 0); 814 815 return (len); 816 } 817 818 /* 819 * Function: 820 * vd_build_default_label 821 * 822 * Description: 823 * Return a default label for a given disk size. This is used when the disk 824 * does not have a valid VTOC so that the user can get a valid default 825 * configuration. The default label has all slice sizes set to 0 (except 826 * slice 2 which is the entire disk) to force the user to write a valid 827 * label onto the disk image. 828 * 829 * Parameters: 830 * disk_size - the disk size in bytes 831 * label - the returned default label. 832 * 833 * Return Code: 834 * none. 835 */ 836 static void 837 vd_build_default_label(size_t disk_size, struct dk_label *label) 838 { 839 size_t size; 840 char unit; 841 842 bzero(label, sizeof (struct dk_label)); 843 844 /* 845 * Ideally we would like the cylinder size (nsect * nhead) to be the 846 * same whatever the disk size is. That way the VTOC label could be 847 * easily updated in case the disk size is increased (keeping the 848 * same cylinder size allows to preserve the existing partitioning 849 * when updating the VTOC label). But it is not possible to have 850 * a fixed cylinder size and to cover all disk size. 851 * 852 * So we define different cylinder sizes depending on the disk size. 853 * The cylinder size is chosen so that we don't have too few cylinders 854 * for a small disk image, or so many on a big disk image that you 855 * waste space for backup superblocks or cylinder group structures. 856 * Also we must have a resonable number of cylinders and sectors so 857 * that newfs can run using default values. 858 * 859 * +-----------+--------+---------+--------+ 860 * | disk_size | < 2MB | 2MB-4GB | >= 8GB | 861 * +-----------+--------+---------+--------+ 862 * | nhead | 1 | 1 | 96 | 863 * | nsect | 200 | 600 | 768 | 864 * +-----------+--------+---------+--------+ 865 * 866 * Other parameters are computed from these values: 867 * 868 * pcyl = disk_size / (nhead * nsect * 512) 869 * acyl = (pcyl > 2)? 2 : 0 870 * ncyl = pcyl - acyl 871 * 872 * The maximum number of cylinder is 65535 so this allows to define a 873 * geometry for a disk size up to 65535 * 96 * 768 * 512 = 2.24 TB 874 * which is more than enough to cover the maximum size allowed by the 875 * extended VTOC format (2TB). 876 */ 877 878 if (disk_size >= 8 * ONE_GIGABYTE) { 879 880 label->dkl_nhead = 96; 881 label->dkl_nsect = 768; 882 883 } else if (disk_size >= 2 * ONE_MEGABYTE) { 884 885 label->dkl_nhead = 1; 886 label->dkl_nsect = 600; 887 888 } else { 889 890 label->dkl_nhead = 1; 891 label->dkl_nsect = 200; 892 } 893 894 label->dkl_pcyl = disk_size / 895 (label->dkl_nsect * label->dkl_nhead * DEV_BSIZE); 896 897 if (label->dkl_pcyl == 0) 898 label->dkl_pcyl = 1; 899 900 label->dkl_acyl = 0; 901 902 if (label->dkl_pcyl > 2) 903 label->dkl_acyl = 2; 904 905 label->dkl_ncyl = label->dkl_pcyl - label->dkl_acyl; 906 label->dkl_write_reinstruct = 0; 907 label->dkl_read_reinstruct = 0; 908 label->dkl_rpm = 7200; 909 label->dkl_apc = 0; 910 label->dkl_intrlv = 0; 911 912 PR0("requested disk size: %ld bytes\n", disk_size); 913 PR0("setup: ncyl=%d nhead=%d nsec=%d\n", label->dkl_pcyl, 914 label->dkl_nhead, label->dkl_nsect); 915 PR0("provided disk size: %ld bytes\n", (uint64_t) 916 (label->dkl_pcyl * label->dkl_nhead * 917 label->dkl_nsect * DEV_BSIZE)); 918 919 vd_get_readable_size(disk_size, &size, &unit); 920 921 /* 922 * We must have a correct label name otherwise format(1m) will 923 * not recognized the disk as labeled. 924 */ 925 (void) snprintf(label->dkl_asciilabel, LEN_DKL_ASCII, 926 "SUN-DiskImage-%ld%cB cyl %d alt %d hd %d sec %d", 927 size, unit, 928 label->dkl_ncyl, label->dkl_acyl, label->dkl_nhead, 929 label->dkl_nsect); 930 931 /* default VTOC */ 932 label->dkl_vtoc.v_version = V_EXTVERSION; 933 label->dkl_vtoc.v_nparts = V_NUMPAR; 934 label->dkl_vtoc.v_sanity = VTOC_SANE; 935 label->dkl_vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_tag = V_BACKUP; 936 label->dkl_map[VD_ENTIRE_DISK_SLICE].dkl_cylno = 0; 937 label->dkl_map[VD_ENTIRE_DISK_SLICE].dkl_nblk = label->dkl_ncyl * 938 label->dkl_nhead * label->dkl_nsect; 939 label->dkl_magic = DKL_MAGIC; 940 label->dkl_cksum = vd_lbl2cksum(label); 941 } 942 943 /* 944 * Function: 945 * vd_file_set_vtoc 946 * 947 * Description: 948 * Set the vtoc of a disk image by writing the label and backup 949 * labels into the disk image backend. 950 * 951 * Parameters: 952 * vd - disk on which the operation is performed. 953 * label - the data to be written. 954 * 955 * Return Code: 956 * 0 - success. 957 * n > 0 - error, n indicates the errno code. 958 */ 959 static int 960 vd_file_set_vtoc(vd_t *vd, struct dk_label *label) 961 { 962 size_t blk, sec, cyl, head, cnt; 963 964 ASSERT(vd->file); 965 966 if (VD_FILE_LABEL_WRITE(vd, label) < 0) { 967 PR0("fail to write disk label"); 968 return (EIO); 969 } 970 971 /* 972 * Backup labels are on the last alternate cylinder's 973 * first five odd sectors. 974 */ 975 if (label->dkl_acyl == 0) { 976 PR0("no alternate cylinder, can not store backup labels"); 977 return (0); 978 } 979 980 cyl = label->dkl_ncyl + label->dkl_acyl - 1; 981 head = label->dkl_nhead - 1; 982 983 blk = (cyl * ((label->dkl_nhead * label->dkl_nsect) - label->dkl_apc)) + 984 (head * label->dkl_nsect); 985 986 /* 987 * Write the backup labels. Make sure we don't try to write past 988 * the last cylinder. 989 */ 990 sec = 1; 991 992 for (cnt = 0; cnt < VD_FILE_NUM_BACKUP; cnt++) { 993 994 if (sec >= label->dkl_nsect) { 995 PR0("not enough sector to store all backup labels"); 996 return (0); 997 } 998 999 if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)label, 1000 blk + sec, sizeof (struct dk_label)) < 0) { 1001 PR0("error writing backup label at block %lu\n", 1002 blk + sec); 1003 return (EIO); 1004 } 1005 1006 PR1("wrote backup label at block %lu\n", blk + sec); 1007 1008 sec += 2; 1009 } 1010 1011 return (0); 1012 } 1013 1014 /* 1015 * Function: 1016 * vd_file_get_devid_block 1017 * 1018 * Description: 1019 * Return the block number where the device id is stored. 1020 * 1021 * Parameters: 1022 * vd - disk on which the operation is performed. 1023 * blkp - pointer to the block number 1024 * 1025 * Return Code: 1026 * 0 - success 1027 * ENOSPC - disk has no space to store a device id 1028 */ 1029 static int 1030 vd_file_get_devid_block(vd_t *vd, size_t *blkp) 1031 { 1032 diskaddr_t spc, head, cyl; 1033 1034 ASSERT(vd->file); 1035 1036 if (vd->vdisk_label == VD_DISK_LABEL_UNK) { 1037 /* 1038 * If no label is defined we don't know where to find 1039 * a device id. 1040 */ 1041 return (ENOSPC); 1042 } 1043 1044 if (vd->vdisk_label == VD_DISK_LABEL_EFI) { 1045 /* 1046 * For an EFI disk, the devid is at the beginning of 1047 * the reserved slice 1048 */ 1049 if (vd->efi_reserved == -1) { 1050 PR0("EFI disk has no reserved slice"); 1051 return (ENOSPC); 1052 } 1053 1054 *blkp = vd->slices[vd->efi_reserved].start; 1055 return (0); 1056 } 1057 1058 ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC); 1059 1060 /* this geometry doesn't allow us to have a devid */ 1061 if (vd->dk_geom.dkg_acyl < 2) { 1062 PR0("not enough alternate cylinder available for devid " 1063 "(acyl=%u)", vd->dk_geom.dkg_acyl); 1064 return (ENOSPC); 1065 } 1066 1067 /* the devid is in on the track next to the last cylinder */ 1068 cyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl - 2; 1069 spc = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect; 1070 head = vd->dk_geom.dkg_nhead - 1; 1071 1072 *blkp = (cyl * (spc - vd->dk_geom.dkg_apc)) + 1073 (head * vd->dk_geom.dkg_nsect) + 1; 1074 1075 return (0); 1076 } 1077 1078 /* 1079 * Return the checksum of a disk block containing an on-disk devid. 1080 */ 1081 static uint_t 1082 vd_dkdevid2cksum(struct dk_devid *dkdevid) 1083 { 1084 uint_t chksum, *ip; 1085 int i; 1086 1087 chksum = 0; 1088 ip = (void *)dkdevid; 1089 for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int)); i++) 1090 chksum ^= ip[i]; 1091 1092 return (chksum); 1093 } 1094 1095 /* 1096 * Function: 1097 * vd_file_read_devid 1098 * 1099 * Description: 1100 * Read the device id stored on a disk image. 1101 * 1102 * Parameters: 1103 * vd - disk on which the operation is performed. 1104 * devid - the return address of the device ID. 1105 * 1106 * Return Code: 1107 * 0 - success 1108 * EIO - I/O error while trying to access the disk image 1109 * EINVAL - no valid device id was found 1110 * ENOSPC - disk has no space to store a device id 1111 */ 1112 static int 1113 vd_file_read_devid(vd_t *vd, ddi_devid_t *devid) 1114 { 1115 struct dk_devid *dkdevid; 1116 size_t blk; 1117 uint_t chksum; 1118 int status, sz; 1119 1120 if ((status = vd_file_get_devid_block(vd, &blk)) != 0) 1121 return (status); 1122 1123 dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP); 1124 1125 /* get the devid */ 1126 if ((vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)dkdevid, blk, 1127 DEV_BSIZE)) < 0) { 1128 PR0("error reading devid block at %lu", blk); 1129 status = EIO; 1130 goto done; 1131 } 1132 1133 /* validate the revision */ 1134 if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) || 1135 (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) { 1136 PR0("invalid devid found at block %lu (bad revision)", blk); 1137 status = EINVAL; 1138 goto done; 1139 } 1140 1141 /* compute checksum */ 1142 chksum = vd_dkdevid2cksum(dkdevid); 1143 1144 /* compare the checksums */ 1145 if (DKD_GETCHKSUM(dkdevid) != chksum) { 1146 PR0("invalid devid found at block %lu (bad checksum)", blk); 1147 status = EINVAL; 1148 goto done; 1149 } 1150 1151 /* validate the device id */ 1152 if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) { 1153 PR0("invalid devid found at block %lu", blk); 1154 status = EINVAL; 1155 goto done; 1156 } 1157 1158 PR1("devid read at block %lu", blk); 1159 1160 sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid); 1161 *devid = kmem_alloc(sz, KM_SLEEP); 1162 bcopy(&dkdevid->dkd_devid, *devid, sz); 1163 1164 done: 1165 kmem_free(dkdevid, DEV_BSIZE); 1166 return (status); 1167 1168 } 1169 1170 /* 1171 * Function: 1172 * vd_file_write_devid 1173 * 1174 * Description: 1175 * Write a device id into disk image. 1176 * 1177 * Parameters: 1178 * vd - disk on which the operation is performed. 1179 * devid - the device ID to store. 1180 * 1181 * Return Code: 1182 * 0 - success 1183 * EIO - I/O error while trying to access the disk image 1184 * ENOSPC - disk has no space to store a device id 1185 */ 1186 static int 1187 vd_file_write_devid(vd_t *vd, ddi_devid_t devid) 1188 { 1189 struct dk_devid *dkdevid; 1190 uint_t chksum; 1191 size_t blk; 1192 int status; 1193 1194 if (devid == NULL) { 1195 /* nothing to write */ 1196 return (0); 1197 } 1198 1199 if ((status = vd_file_get_devid_block(vd, &blk)) != 0) 1200 return (status); 1201 1202 dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP); 1203 1204 /* set revision */ 1205 dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB; 1206 dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB; 1207 1208 /* copy devid */ 1209 bcopy(devid, &dkdevid->dkd_devid, ddi_devid_sizeof(devid)); 1210 1211 /* compute checksum */ 1212 chksum = vd_dkdevid2cksum(dkdevid); 1213 1214 /* set checksum */ 1215 DKD_FORMCHKSUM(chksum, dkdevid); 1216 1217 /* store the devid */ 1218 if ((status = vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, 1219 (caddr_t)dkdevid, blk, DEV_BSIZE)) < 0) { 1220 PR0("Error writing devid block at %lu", blk); 1221 status = EIO; 1222 } else { 1223 PR1("devid written at block %lu", blk); 1224 status = 0; 1225 } 1226 1227 kmem_free(dkdevid, DEV_BSIZE); 1228 return (status); 1229 } 1230 1231 /* 1232 * Function: 1233 * vd_do_scsi_rdwr 1234 * 1235 * Description: 1236 * Read or write to a SCSI disk using an absolute disk offset. 1237 * 1238 * Parameters: 1239 * vd - disk on which the operation is performed. 1240 * operation - operation to execute: read (VD_OP_BREAD) or 1241 * write (VD_OP_BWRITE). 1242 * data - buffer where data are read to or written from. 1243 * blk - starting block for the operation. 1244 * len - number of bytes to read or write. 1245 * 1246 * Return Code: 1247 * 0 - success 1248 * n != 0 - error. 1249 */ 1250 static int 1251 vd_do_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t blk, size_t len) 1252 { 1253 struct uscsi_cmd ucmd; 1254 union scsi_cdb cdb; 1255 int nsectors, nblk; 1256 int max_sectors; 1257 int status, rval; 1258 1259 ASSERT(!vd->file); 1260 ASSERT(vd->vdisk_block_size > 0); 1261 1262 max_sectors = vd->max_xfer_sz; 1263 nblk = (len / vd->vdisk_block_size); 1264 1265 if (len % vd->vdisk_block_size != 0) 1266 return (EINVAL); 1267 1268 /* 1269 * Build and execute the uscsi ioctl. We build a group0, group1 1270 * or group4 command as necessary, since some targets 1271 * do not support group1 commands. 1272 */ 1273 while (nblk) { 1274 1275 bzero(&ucmd, sizeof (ucmd)); 1276 bzero(&cdb, sizeof (cdb)); 1277 1278 nsectors = (max_sectors < nblk) ? max_sectors : nblk; 1279 1280 /* 1281 * Some of the optical drives on sun4v machines are ATAPI 1282 * devices which use Group 1 Read/Write commands so we need 1283 * to explicitly check a flag which is set when a domain 1284 * is bound. 1285 */ 1286 if (blk < (2 << 20) && nsectors <= 0xff && !vd->is_atapi_dev) { 1287 FORMG0ADDR(&cdb, blk); 1288 FORMG0COUNT(&cdb, (uchar_t)nsectors); 1289 ucmd.uscsi_cdblen = CDB_GROUP0; 1290 } else if (blk > 0xffffffff) { 1291 FORMG4LONGADDR(&cdb, blk); 1292 FORMG4COUNT(&cdb, nsectors); 1293 ucmd.uscsi_cdblen = CDB_GROUP4; 1294 cdb.scc_cmd |= SCMD_GROUP4; 1295 } else { 1296 FORMG1ADDR(&cdb, blk); 1297 FORMG1COUNT(&cdb, nsectors); 1298 ucmd.uscsi_cdblen = CDB_GROUP1; 1299 cdb.scc_cmd |= SCMD_GROUP1; 1300 } 1301 ucmd.uscsi_cdb = (caddr_t)&cdb; 1302 ucmd.uscsi_bufaddr = data; 1303 ucmd.uscsi_buflen = nsectors * vd->block_size; 1304 ucmd.uscsi_timeout = vd_scsi_rdwr_timeout; 1305 /* 1306 * Set flags so that the command is isolated from normal 1307 * commands and no error message is printed. 1308 */ 1309 ucmd.uscsi_flags = USCSI_ISOLATE | USCSI_SILENT; 1310 1311 if (operation == VD_OP_BREAD) { 1312 cdb.scc_cmd |= SCMD_READ; 1313 ucmd.uscsi_flags |= USCSI_READ; 1314 } else { 1315 cdb.scc_cmd |= SCMD_WRITE; 1316 } 1317 1318 status = ldi_ioctl(vd->ldi_handle[VD_ENTIRE_DISK_SLICE], 1319 USCSICMD, (intptr_t)&ucmd, (vd->open_flags | FKIOCTL), 1320 kcred, &rval); 1321 1322 if (status == 0) 1323 status = ucmd.uscsi_status; 1324 1325 if (status != 0) 1326 break; 1327 1328 /* 1329 * Check if partial DMA breakup is required. If so, reduce 1330 * the request size by half and retry the last request. 1331 */ 1332 if (ucmd.uscsi_resid == ucmd.uscsi_buflen) { 1333 max_sectors >>= 1; 1334 if (max_sectors <= 0) { 1335 status = EIO; 1336 break; 1337 } 1338 continue; 1339 } 1340 1341 if (ucmd.uscsi_resid != 0) { 1342 status = EIO; 1343 break; 1344 } 1345 1346 blk += nsectors; 1347 nblk -= nsectors; 1348 data += nsectors * vd->vdisk_block_size; /* SECSIZE */ 1349 } 1350 1351 return (status); 1352 } 1353 1354 /* 1355 * Function: 1356 * vd_scsi_rdwr 1357 * 1358 * Description: 1359 * Wrapper function to read or write to a SCSI disk using an absolute 1360 * disk offset. It checks the blocksize of the underlying device and, 1361 * if necessary, adjusts the buffers accordingly before calling 1362 * vd_do_scsi_rdwr() to do the actual read or write. 1363 * 1364 * Parameters: 1365 * vd - disk on which the operation is performed. 1366 * operation - operation to execute: read (VD_OP_BREAD) or 1367 * write (VD_OP_BWRITE). 1368 * data - buffer where data are read to or written from. 1369 * blk - starting block for the operation. 1370 * len - number of bytes to read or write. 1371 * 1372 * Return Code: 1373 * 0 - success 1374 * n != 0 - error. 1375 */ 1376 static int 1377 vd_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t vblk, size_t vlen) 1378 { 1379 int rv; 1380 1381 size_t pblk; /* physical device block number of data on device */ 1382 size_t delta; /* relative offset between pblk and vblk */ 1383 size_t pnblk; /* number of physical blocks to be read from device */ 1384 size_t plen; /* length of data to be read from physical device */ 1385 char *buf; /* buffer area to fit physical device's block size */ 1386 1387 if (vd->block_size == 0) { 1388 /* 1389 * The block size was not available during the attach, 1390 * try to update it now. 1391 */ 1392 if (vd_backend_check_size(vd) != 0) 1393 return (EIO); 1394 } 1395 1396 /* 1397 * If the vdisk block size and the block size of the underlying device 1398 * match we can skip straight to vd_do_scsi_rdwr(), otherwise we need 1399 * to create a buffer large enough to handle the device's block size 1400 * and adjust the block to be read from and the amount of data to 1401 * read to correspond with the device's block size. 1402 */ 1403 if (vd->vdisk_block_size == vd->block_size) 1404 return (vd_do_scsi_rdwr(vd, operation, data, vblk, vlen)); 1405 1406 if (vd->vdisk_block_size > vd->block_size) 1407 return (EINVAL); 1408 1409 /* 1410 * Writing of physical block sizes larger than the virtual block size 1411 * is not supported. This would be added if/when support for guests 1412 * writing to DVDs is implemented. 1413 */ 1414 if (operation == VD_OP_BWRITE) 1415 return (ENOTSUP); 1416 1417 /* BEGIN CSTYLED */ 1418 /* 1419 * Below is a diagram showing the relationship between the physical 1420 * and virtual blocks. If the virtual blocks marked by 'X' below are 1421 * requested, then the physical blocks denoted by 'Y' are read. 1422 * 1423 * vblk 1424 * | vlen 1425 * |<--------------->| 1426 * v v 1427 * --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+- virtual disk: 1428 * | | | |XX|XX|XX|XX|XX|XX| | | | | | } block size is 1429 * --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+- vd->vdisk_block_size 1430 * : : : : 1431 * >:==:< delta : : 1432 * : : : : 1433 * --+-----+-----+-----+-----+-----+-----+-----+-- physical disk: 1434 * | |YY:YY|YYYYY|YYYYY|YY:YY| | | } block size is 1435 * --+-----+-----+-----+-----+-----+-----+-----+-- vd->block_size 1436 * ^ ^ 1437 * |<--------------------->| 1438 * | plen 1439 * pblk 1440 */ 1441 /* END CSTYLED */ 1442 pblk = (vblk * vd->vdisk_block_size) / vd->block_size; 1443 delta = (vblk * vd->vdisk_block_size) - (pblk * vd->block_size); 1444 pnblk = ((delta + vlen - 1) / vd->block_size) + 1; 1445 plen = pnblk * vd->block_size; 1446 1447 PR2("vblk %lx:pblk %lx: vlen %ld:plen %ld", vblk, pblk, vlen, plen); 1448 1449 buf = kmem_zalloc(sizeof (caddr_t) * plen, KM_SLEEP); 1450 rv = vd_do_scsi_rdwr(vd, operation, (caddr_t)buf, pblk, plen); 1451 bcopy(buf + delta, data, vlen); 1452 1453 kmem_free(buf, sizeof (caddr_t) * plen); 1454 1455 return (rv); 1456 } 1457 1458 /* 1459 * Function: 1460 * vd_slice_flabel_read 1461 * 1462 * Description: 1463 * This function simulates a read operation from the fake label of 1464 * a single-slice disk. 1465 * 1466 * Parameters: 1467 * vd - single-slice disk to read from 1468 * data - buffer where data should be read to 1469 * offset - offset in byte where the read should start 1470 * length - number of bytes to read 1471 * 1472 * Return Code: 1473 * n >= 0 - success, n indicates the number of bytes read 1474 * -1 - error 1475 */ 1476 static ssize_t 1477 vd_slice_flabel_read(vd_t *vd, caddr_t data, size_t offset, size_t length) 1478 { 1479 size_t n = 0; 1480 uint_t limit = vd->flabel_limit * DEV_BSIZE; 1481 1482 ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 1483 ASSERT(vd->flabel != NULL); 1484 1485 /* if offset is past the fake label limit there's nothing to read */ 1486 if (offset >= limit) 1487 return (0); 1488 1489 /* data with offset 0 to flabel_size are read from flabel */ 1490 if (offset < vd->flabel_size) { 1491 1492 if (offset + length <= vd->flabel_size) { 1493 bcopy(vd->flabel + offset, data, length); 1494 return (length); 1495 } 1496 1497 n = vd->flabel_size - offset; 1498 bcopy(vd->flabel + offset, data, n); 1499 data += n; 1500 } 1501 1502 /* data with offset from flabel_size to flabel_limit are all zeros */ 1503 if (offset + length <= limit) { 1504 bzero(data, length - n); 1505 return (length); 1506 } 1507 1508 bzero(data, limit - offset - n); 1509 return (limit - offset); 1510 } 1511 1512 /* 1513 * Function: 1514 * vd_slice_flabel_write 1515 * 1516 * Description: 1517 * This function simulates a write operation to the fake label of 1518 * a single-slice disk. Write operations are actually faked and return 1519 * success although the label is never changed. This is mostly to 1520 * simulate a successful label update. 1521 * 1522 * Parameters: 1523 * vd - single-slice disk to write to 1524 * data - buffer where data should be written from 1525 * offset - offset in byte where the write should start 1526 * length - number of bytes to written 1527 * 1528 * Return Code: 1529 * n >= 0 - success, n indicates the number of bytes written 1530 * -1 - error 1531 */ 1532 static ssize_t 1533 vd_slice_flabel_write(vd_t *vd, caddr_t data, size_t offset, size_t length) 1534 { 1535 uint_t limit = vd->flabel_limit * DEV_BSIZE; 1536 struct dk_label *label; 1537 struct dk_geom geom; 1538 struct extvtoc vtoc; 1539 1540 ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 1541 ASSERT(vd->flabel != NULL); 1542 1543 if (offset >= limit) 1544 return (0); 1545 1546 /* 1547 * If this is a request to overwrite the VTOC disk label, check that 1548 * the new label is similar to the previous one and return that the 1549 * write was successful, but note that nothing is actually overwritten. 1550 */ 1551 if (vd->vdisk_label == VD_DISK_LABEL_VTOC && 1552 offset == 0 && length == DEV_BSIZE) { 1553 label = (void *)data; 1554 1555 /* check that this is a valid label */ 1556 if (label->dkl_magic != DKL_MAGIC || 1557 label->dkl_cksum != vd_lbl2cksum(label)) 1558 return (-1); 1559 1560 /* check the vtoc and geometry */ 1561 vd_label_to_vtocgeom(label, &vtoc, &geom); 1562 if (vd_slice_geom_isvalid(vd, &geom) && 1563 vd_slice_vtoc_isvalid(vd, &vtoc)) 1564 return (length); 1565 } 1566 1567 /* fail any other write */ 1568 return (-1); 1569 } 1570 1571 /* 1572 * Function: 1573 * vd_slice_fake_rdwr 1574 * 1575 * Description: 1576 * This function simulates a raw read or write operation to a single-slice 1577 * disk. It only handles the faked part of the operation i.e. I/Os to 1578 * blocks which have no mapping with the vdisk backend (I/Os to the 1579 * beginning and to the end of the vdisk). 1580 * 1581 * The function returns 0 is the operation is completed and it has been 1582 * entirely handled as a fake read or write. In that case, lengthp points 1583 * to the number of bytes not read or written. Values returned by datap 1584 * and blkp are undefined. 1585 * 1586 * If the fake operation has succeeded but the read or write is not 1587 * complete (i.e. the read/write operation extends beyond the blocks 1588 * we fake) then the function returns EAGAIN and datap, blkp and lengthp 1589 * pointers points to the parameters for completing the operation. 1590 * 1591 * In case of an error, for example if the slice is empty or parameters 1592 * are invalid, then the function returns a non-zero value different 1593 * from EAGAIN. In that case, the returned values of datap, blkp and 1594 * lengthp are undefined. 1595 * 1596 * Parameters: 1597 * vd - single-slice disk on which the operation is performed 1598 * slice - slice on which the operation is performed, 1599 * VD_SLICE_NONE indicates that the operation 1600 * is done using an absolute disk offset. 1601 * operation - operation to execute: read (VD_OP_BREAD) or 1602 * write (VD_OP_BWRITE). 1603 * datap - pointer to the buffer where data are read to 1604 * or written from. Return the pointer where remaining 1605 * data have to be read to or written from. 1606 * blkp - pointer to the starting block for the operation. 1607 * Return the starting block relative to the vdisk 1608 * backend for the remaining operation. 1609 * lengthp - pointer to the number of bytes to read or write. 1610 * This should be a multiple of DEV_BSIZE. Return the 1611 * remaining number of bytes to read or write. 1612 * 1613 * Return Code: 1614 * 0 - read/write operation is completed 1615 * EAGAIN - read/write operation is not completed 1616 * other values - error 1617 */ 1618 static int 1619 vd_slice_fake_rdwr(vd_t *vd, int slice, int operation, caddr_t *datap, 1620 size_t *blkp, size_t *lengthp) 1621 { 1622 struct dk_label *label; 1623 caddr_t data; 1624 size_t blk, length, csize; 1625 size_t ablk, asize, aoff, alen; 1626 ssize_t n; 1627 int sec, status; 1628 1629 ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 1630 ASSERT(slice != 0); 1631 1632 data = *datap; 1633 blk = *blkp; 1634 length = *lengthp; 1635 1636 /* 1637 * If this is not a raw I/O or an I/O from a full disk slice then 1638 * this is an I/O to/from an empty slice. 1639 */ 1640 if (slice != VD_SLICE_NONE && 1641 (slice != VD_ENTIRE_DISK_SLICE || 1642 vd->vdisk_label != VD_DISK_LABEL_VTOC) && 1643 (slice != VD_EFI_WD_SLICE || 1644 vd->vdisk_label != VD_DISK_LABEL_EFI)) { 1645 return (EIO); 1646 } 1647 1648 if (length % DEV_BSIZE != 0) 1649 return (EINVAL); 1650 1651 /* handle any I/O with the fake label */ 1652 if (operation == VD_OP_BWRITE) 1653 n = vd_slice_flabel_write(vd, data, blk * DEV_BSIZE, length); 1654 else 1655 n = vd_slice_flabel_read(vd, data, blk * DEV_BSIZE, length); 1656 1657 if (n == -1) 1658 return (EINVAL); 1659 1660 ASSERT(n % DEV_BSIZE == 0); 1661 1662 /* adjust I/O arguments */ 1663 data += n; 1664 blk += n / DEV_BSIZE; 1665 length -= n; 1666 1667 /* check if there's something else to process */ 1668 if (length == 0) { 1669 status = 0; 1670 goto done; 1671 } 1672 1673 if (vd->vdisk_label == VD_DISK_LABEL_VTOC && 1674 slice == VD_ENTIRE_DISK_SLICE) { 1675 status = EAGAIN; 1676 goto done; 1677 } 1678 1679 if (vd->vdisk_label == VD_DISK_LABEL_EFI) { 1680 asize = EFI_MIN_RESV_SIZE + 33; 1681 ablk = vd->vdisk_size - asize; 1682 } else { 1683 ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC); 1684 ASSERT(vd->dk_geom.dkg_apc == 0); 1685 1686 csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect; 1687 ablk = vd->dk_geom.dkg_ncyl * csize; 1688 asize = vd->dk_geom.dkg_acyl * csize; 1689 } 1690 1691 alen = length / DEV_BSIZE; 1692 aoff = blk; 1693 1694 /* if we have reached the last block then the I/O is completed */ 1695 if (aoff == ablk + asize) { 1696 status = 0; 1697 goto done; 1698 } 1699 1700 /* if we are past the last block then return an error */ 1701 if (aoff > ablk + asize) 1702 return (EIO); 1703 1704 /* check if there is any I/O to end of the disk */ 1705 if (aoff + alen < ablk) { 1706 status = EAGAIN; 1707 goto done; 1708 } 1709 1710 /* we don't allow any write to the end of the disk */ 1711 if (operation == VD_OP_BWRITE) 1712 return (EIO); 1713 1714 if (aoff < ablk) { 1715 alen -= (ablk - aoff); 1716 aoff = ablk; 1717 } 1718 1719 if (aoff + alen > ablk + asize) { 1720 alen = ablk + asize - aoff; 1721 } 1722 1723 alen *= DEV_BSIZE; 1724 1725 if (operation == VD_OP_BREAD) { 1726 bzero(data + (aoff - blk) * DEV_BSIZE, alen); 1727 1728 if (vd->vdisk_label == VD_DISK_LABEL_VTOC) { 1729 /* check if we read backup labels */ 1730 label = VD_LABEL_VTOC(vd); 1731 ablk += (label->dkl_acyl - 1) * csize + 1732 (label->dkl_nhead - 1) * label->dkl_nsect; 1733 1734 for (sec = 1; (sec < 5 * 2 + 1); sec += 2) { 1735 1736 if (ablk + sec >= blk && 1737 ablk + sec < blk + (length / DEV_BSIZE)) { 1738 bcopy(label, data + 1739 (ablk + sec - blk) * DEV_BSIZE, 1740 sizeof (struct dk_label)); 1741 } 1742 } 1743 } 1744 } 1745 1746 length -= alen; 1747 1748 status = (length == 0)? 0: EAGAIN; 1749 1750 done: 1751 ASSERT(length == 0 || blk >= vd->flabel_limit); 1752 1753 /* 1754 * Return the parameters for the remaining I/O. The starting block is 1755 * adjusted so that it is relative to the vdisk backend. 1756 */ 1757 *datap = data; 1758 *blkp = blk - vd->flabel_limit; 1759 *lengthp = length; 1760 1761 return (status); 1762 } 1763 1764 /* 1765 * Return Values 1766 * EINPROGRESS - operation was successfully started 1767 * EIO - encountered LDC (aka. task error) 1768 * 0 - operation completed successfully 1769 * 1770 * Side Effect 1771 * sets request->status = <disk operation status> 1772 */ 1773 static int 1774 vd_start_bio(vd_task_t *task) 1775 { 1776 int rv, status = 0; 1777 vd_t *vd = task->vd; 1778 vd_dring_payload_t *request = task->request; 1779 struct buf *buf = &task->buf; 1780 uint8_t mtype; 1781 int slice; 1782 char *bufaddr = 0; 1783 size_t buflen; 1784 size_t offset, length, nbytes; 1785 1786 ASSERT(vd != NULL); 1787 ASSERT(request != NULL); 1788 1789 slice = request->slice; 1790 1791 ASSERT(slice == VD_SLICE_NONE || slice < vd->nslices); 1792 ASSERT((request->operation == VD_OP_BREAD) || 1793 (request->operation == VD_OP_BWRITE)); 1794 1795 if (request->nbytes == 0) { 1796 /* no service for trivial requests */ 1797 request->status = EINVAL; 1798 return (0); 1799 } 1800 1801 PR1("%s %lu bytes at block %lu", 1802 (request->operation == VD_OP_BREAD) ? "Read" : "Write", 1803 request->nbytes, request->addr); 1804 1805 /* 1806 * We have to check the open flags because the functions processing 1807 * the read/write request will not do it. 1808 */ 1809 if (request->operation == VD_OP_BWRITE && !(vd->open_flags & FWRITE)) { 1810 PR0("write fails because backend is opened read-only"); 1811 request->nbytes = 0; 1812 request->status = EROFS; 1813 return (0); 1814 } 1815 1816 mtype = (&vd->inband_task == task) ? LDC_SHADOW_MAP : LDC_DIRECT_MAP; 1817 1818 /* Map memory exported by client */ 1819 status = ldc_mem_map(task->mhdl, request->cookie, request->ncookies, 1820 mtype, (request->operation == VD_OP_BREAD) ? LDC_MEM_W : LDC_MEM_R, 1821 &bufaddr, NULL); 1822 if (status != 0) { 1823 PR0("ldc_mem_map() returned err %d ", status); 1824 return (EIO); 1825 } 1826 1827 /* 1828 * The buffer size has to be 8-byte aligned, so the client should have 1829 * sent a buffer which size is roundup to the next 8-byte aligned value. 1830 */ 1831 buflen = P2ROUNDUP(request->nbytes, 8); 1832 1833 status = ldc_mem_acquire(task->mhdl, 0, buflen); 1834 if (status != 0) { 1835 (void) ldc_mem_unmap(task->mhdl); 1836 PR0("ldc_mem_acquire() returned err %d ", status); 1837 return (EIO); 1838 } 1839 1840 offset = request->addr; 1841 nbytes = request->nbytes; 1842 length = nbytes; 1843 1844 /* default number of byte returned by the I/O */ 1845 request->nbytes = 0; 1846 1847 if (vd->vdisk_type == VD_DISK_TYPE_SLICE) { 1848 1849 if (slice != 0) { 1850 /* handle any fake I/O */ 1851 rv = vd_slice_fake_rdwr(vd, slice, request->operation, 1852 &bufaddr, &offset, &length); 1853 1854 /* record the number of bytes from the fake I/O */ 1855 request->nbytes = nbytes - length; 1856 1857 if (rv == 0) { 1858 request->status = 0; 1859 goto io_done; 1860 } 1861 1862 if (rv != EAGAIN) { 1863 request->nbytes = 0; 1864 request->status = EIO; 1865 goto io_done; 1866 } 1867 1868 /* 1869 * If we return with EAGAIN then this means that there 1870 * are still data to read or write. 1871 */ 1872 ASSERT(length != 0); 1873 1874 /* 1875 * We need to continue the I/O from the slice backend to 1876 * complete the request. The variables bufaddr, offset 1877 * and length have been adjusted to have the right 1878 * information to do the remaining I/O from the backend. 1879 * The backend is entirely mapped to slice 0 so we just 1880 * have to complete the I/O from that slice. 1881 */ 1882 slice = 0; 1883 } 1884 1885 } else if ((slice == VD_SLICE_NONE) && (!vd->file)) { 1886 1887 /* 1888 * This is not a disk image so it is a real disk. We 1889 * assume that the underlying device driver supports 1890 * USCSICMD ioctls. This is the case of all SCSI devices 1891 * (sd, ssd...). 1892 * 1893 * In the future if we have non-SCSI disks we would need 1894 * to invoke the appropriate function to do I/O using an 1895 * absolute disk offset (for example using DIOCTL_RWCMD 1896 * for IDE disks). 1897 */ 1898 rv = vd_scsi_rdwr(vd, request->operation, bufaddr, offset, 1899 length); 1900 if (rv != 0) { 1901 request->status = EIO; 1902 } else { 1903 request->nbytes = length; 1904 request->status = 0; 1905 } 1906 goto io_done; 1907 } 1908 1909 /* Start the block I/O */ 1910 if (vd->file) { 1911 rv = vd_file_rw(vd, slice, request->operation, bufaddr, offset, 1912 length); 1913 if (rv < 0) { 1914 request->nbytes = 0; 1915 request->status = EIO; 1916 } else { 1917 request->nbytes += rv; 1918 request->status = 0; 1919 } 1920 } else { 1921 bioinit(buf); 1922 buf->b_flags = B_BUSY; 1923 buf->b_bcount = length; 1924 buf->b_lblkno = offset; 1925 buf->b_bufsize = buflen; 1926 buf->b_edev = vd->dev[slice]; 1927 buf->b_un.b_addr = bufaddr; 1928 buf->b_flags |= (request->operation == VD_OP_BREAD)? 1929 B_READ : B_WRITE; 1930 1931 request->status = ldi_strategy(vd->ldi_handle[slice], buf); 1932 1933 /* 1934 * This is to indicate to the caller that the request 1935 * needs to be finished by vd_complete_bio() by calling 1936 * biowait() there and waiting for that to return before 1937 * triggering the notification of the vDisk client. 1938 * 1939 * This is necessary when writing to real disks as 1940 * otherwise calls to ldi_strategy() would be serialized 1941 * behind the calls to biowait() and performance would 1942 * suffer. 1943 */ 1944 if (request->status == 0) 1945 return (EINPROGRESS); 1946 1947 biofini(buf); 1948 } 1949 1950 io_done: 1951 /* Clean up after error or completion */ 1952 rv = ldc_mem_release(task->mhdl, 0, buflen); 1953 if (rv) { 1954 PR0("ldc_mem_release() returned err %d ", rv); 1955 status = EIO; 1956 } 1957 rv = ldc_mem_unmap(task->mhdl); 1958 if (rv) { 1959 PR0("ldc_mem_unmap() returned err %d ", rv); 1960 status = EIO; 1961 } 1962 1963 return (status); 1964 } 1965 1966 /* 1967 * This function should only be called from vd_notify to ensure that requests 1968 * are responded to in the order that they are received. 1969 */ 1970 static int 1971 send_msg(ldc_handle_t ldc_handle, void *msg, size_t msglen) 1972 { 1973 int status; 1974 size_t nbytes; 1975 1976 do { 1977 nbytes = msglen; 1978 status = ldc_write(ldc_handle, msg, &nbytes); 1979 if (status != EWOULDBLOCK) 1980 break; 1981 drv_usecwait(vds_ldc_delay); 1982 } while (status == EWOULDBLOCK); 1983 1984 if (status != 0) { 1985 if (status != ECONNRESET) 1986 PR0("ldc_write() returned errno %d", status); 1987 return (status); 1988 } else if (nbytes != msglen) { 1989 PR0("ldc_write() performed only partial write"); 1990 return (EIO); 1991 } 1992 1993 PR1("SENT %lu bytes", msglen); 1994 return (0); 1995 } 1996 1997 static void 1998 vd_need_reset(vd_t *vd, boolean_t reset_ldc) 1999 { 2000 mutex_enter(&vd->lock); 2001 vd->reset_state = B_TRUE; 2002 vd->reset_ldc = reset_ldc; 2003 mutex_exit(&vd->lock); 2004 } 2005 2006 /* 2007 * Reset the state of the connection with a client, if needed; reset the LDC 2008 * transport as well, if needed. This function should only be called from the 2009 * "vd_recv_msg", as it waits for tasks - otherwise a deadlock can occur. 2010 */ 2011 static void 2012 vd_reset_if_needed(vd_t *vd) 2013 { 2014 int status = 0; 2015 2016 mutex_enter(&vd->lock); 2017 if (!vd->reset_state) { 2018 ASSERT(!vd->reset_ldc); 2019 mutex_exit(&vd->lock); 2020 return; 2021 } 2022 mutex_exit(&vd->lock); 2023 2024 PR0("Resetting connection state with %s", VD_CLIENT(vd)); 2025 2026 /* 2027 * Let any asynchronous I/O complete before possibly pulling the rug 2028 * out from under it; defer checking vd->reset_ldc, as one of the 2029 * asynchronous tasks might set it 2030 */ 2031 ddi_taskq_wait(vd->completionq); 2032 2033 if (vd->file) { 2034 status = VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL); 2035 if (status) { 2036 PR0("VOP_FSYNC returned errno %d", status); 2037 } 2038 } 2039 2040 if ((vd->initialized & VD_DRING) && 2041 ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0)) 2042 PR0("ldc_mem_dring_unmap() returned errno %d", status); 2043 2044 vd_free_dring_task(vd); 2045 2046 /* Free the staging buffer for msgs */ 2047 if (vd->vio_msgp != NULL) { 2048 kmem_free(vd->vio_msgp, vd->max_msglen); 2049 vd->vio_msgp = NULL; 2050 } 2051 2052 /* Free the inband message buffer */ 2053 if (vd->inband_task.msg != NULL) { 2054 kmem_free(vd->inband_task.msg, vd->max_msglen); 2055 vd->inband_task.msg = NULL; 2056 } 2057 2058 mutex_enter(&vd->lock); 2059 2060 if (vd->reset_ldc) 2061 PR0("taking down LDC channel"); 2062 if (vd->reset_ldc && ((status = ldc_down(vd->ldc_handle)) != 0)) 2063 PR0("ldc_down() returned errno %d", status); 2064 2065 /* Reset exclusive access rights */ 2066 vd_reset_access(vd); 2067 2068 vd->initialized &= ~(VD_SID | VD_SEQ_NUM | VD_DRING); 2069 vd->state = VD_STATE_INIT; 2070 vd->max_msglen = sizeof (vio_msg_t); /* baseline vio message size */ 2071 2072 /* Allocate the staging buffer */ 2073 vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP); 2074 2075 PR0("calling ldc_up\n"); 2076 (void) ldc_up(vd->ldc_handle); 2077 2078 vd->reset_state = B_FALSE; 2079 vd->reset_ldc = B_FALSE; 2080 2081 mutex_exit(&vd->lock); 2082 } 2083 2084 static void vd_recv_msg(void *arg); 2085 2086 static void 2087 vd_mark_in_reset(vd_t *vd) 2088 { 2089 int status; 2090 2091 PR0("vd_mark_in_reset: marking vd in reset\n"); 2092 2093 vd_need_reset(vd, B_FALSE); 2094 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, DDI_SLEEP); 2095 if (status == DDI_FAILURE) { 2096 PR0("cannot schedule task to recv msg\n"); 2097 vd_need_reset(vd, B_TRUE); 2098 return; 2099 } 2100 } 2101 2102 static int 2103 vd_mark_elem_done(vd_t *vd, int idx, int elem_status, int elem_nbytes) 2104 { 2105 boolean_t accepted; 2106 int status; 2107 on_trap_data_t otd; 2108 vd_dring_entry_t *elem = VD_DRING_ELEM(idx); 2109 2110 if (vd->reset_state) 2111 return (0); 2112 2113 /* Acquire the element */ 2114 if ((status = VIO_DRING_ACQUIRE(&otd, vd->dring_mtype, 2115 vd->dring_handle, idx, idx)) != 0) { 2116 if (status == ECONNRESET) { 2117 vd_mark_in_reset(vd); 2118 return (0); 2119 } else { 2120 return (status); 2121 } 2122 } 2123 2124 /* Set the element's status and mark it done */ 2125 accepted = (elem->hdr.dstate == VIO_DESC_ACCEPTED); 2126 if (accepted) { 2127 elem->payload.nbytes = elem_nbytes; 2128 elem->payload.status = elem_status; 2129 elem->hdr.dstate = VIO_DESC_DONE; 2130 } else { 2131 /* Perhaps client timed out waiting for I/O... */ 2132 PR0("element %u no longer \"accepted\"", idx); 2133 VD_DUMP_DRING_ELEM(elem); 2134 } 2135 /* Release the element */ 2136 if ((status = VIO_DRING_RELEASE(vd->dring_mtype, 2137 vd->dring_handle, idx, idx)) != 0) { 2138 if (status == ECONNRESET) { 2139 vd_mark_in_reset(vd); 2140 return (0); 2141 } else { 2142 PR0("VIO_DRING_RELEASE() returned errno %d", 2143 status); 2144 return (status); 2145 } 2146 } 2147 2148 return (accepted ? 0 : EINVAL); 2149 } 2150 2151 /* 2152 * Return Values 2153 * 0 - operation completed successfully 2154 * EIO - encountered LDC / task error 2155 * 2156 * Side Effect 2157 * sets request->status = <disk operation status> 2158 */ 2159 static int 2160 vd_complete_bio(vd_task_t *task) 2161 { 2162 int status = 0; 2163 int rv = 0; 2164 vd_t *vd = task->vd; 2165 vd_dring_payload_t *request = task->request; 2166 struct buf *buf = &task->buf; 2167 2168 2169 ASSERT(vd != NULL); 2170 ASSERT(request != NULL); 2171 ASSERT(task->msg != NULL); 2172 ASSERT(task->msglen >= sizeof (*task->msg)); 2173 ASSERT(!vd->file); 2174 ASSERT(request->slice != VD_SLICE_NONE || (!vd_slice_single_slice && 2175 vd->vdisk_type == VD_DISK_TYPE_SLICE)); 2176 2177 /* Wait for the I/O to complete [ call to ldi_strategy(9f) ] */ 2178 request->status = biowait(buf); 2179 2180 /* Update the number of bytes read/written */ 2181 request->nbytes += buf->b_bcount - buf->b_resid; 2182 2183 /* Release the buffer */ 2184 if (!vd->reset_state) 2185 status = ldc_mem_release(task->mhdl, 0, buf->b_bufsize); 2186 if (status) { 2187 PR0("ldc_mem_release() returned errno %d copying to " 2188 "client", status); 2189 if (status == ECONNRESET) { 2190 vd_mark_in_reset(vd); 2191 } 2192 rv = EIO; 2193 } 2194 2195 /* Unmap the memory, even if in reset */ 2196 status = ldc_mem_unmap(task->mhdl); 2197 if (status) { 2198 PR0("ldc_mem_unmap() returned errno %d copying to client", 2199 status); 2200 if (status == ECONNRESET) { 2201 vd_mark_in_reset(vd); 2202 } 2203 rv = EIO; 2204 } 2205 2206 biofini(buf); 2207 2208 return (rv); 2209 } 2210 2211 /* 2212 * Description: 2213 * This function is called by the two functions called by a taskq 2214 * [ vd_complete_notify() and vd_serial_notify()) ] to send the 2215 * message to the client. 2216 * 2217 * Parameters: 2218 * arg - opaque pointer to structure containing task to be completed 2219 * 2220 * Return Values 2221 * None 2222 */ 2223 static void 2224 vd_notify(vd_task_t *task) 2225 { 2226 int status; 2227 2228 ASSERT(task != NULL); 2229 ASSERT(task->vd != NULL); 2230 2231 /* 2232 * Send the "ack" or "nack" back to the client; if sending the message 2233 * via LDC fails, arrange to reset both the connection state and LDC 2234 * itself 2235 */ 2236 PR2("Sending %s", 2237 (task->msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK"); 2238 2239 status = send_msg(task->vd->ldc_handle, task->msg, task->msglen); 2240 switch (status) { 2241 case 0: 2242 break; 2243 case ECONNRESET: 2244 vd_mark_in_reset(task->vd); 2245 break; 2246 default: 2247 PR0("initiating full reset"); 2248 vd_need_reset(task->vd, B_TRUE); 2249 break; 2250 } 2251 2252 DTRACE_PROBE1(task__end, vd_task_t *, task); 2253 } 2254 2255 /* 2256 * Description: 2257 * Mark the Dring entry as Done and (if necessary) send an ACK/NACK to 2258 * the vDisk client 2259 * 2260 * Parameters: 2261 * task - structure containing the request sent from client 2262 * 2263 * Return Values 2264 * None 2265 */ 2266 static void 2267 vd_complete_notify(vd_task_t *task) 2268 { 2269 int status = 0; 2270 vd_t *vd = task->vd; 2271 vd_dring_payload_t *request = task->request; 2272 2273 /* Update the dring element for a dring client */ 2274 if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE_V1_0)) { 2275 status = vd_mark_elem_done(vd, task->index, 2276 request->status, request->nbytes); 2277 if (status == ECONNRESET) 2278 vd_mark_in_reset(vd); 2279 else if (status == EACCES) 2280 vd_need_reset(vd, B_TRUE); 2281 } 2282 2283 /* 2284 * If a transport error occurred while marking the element done or 2285 * previously while executing the task, arrange to "nack" the message 2286 * when the final task in the descriptor element range completes 2287 */ 2288 if ((status != 0) || (task->status != 0)) 2289 task->msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 2290 2291 /* 2292 * Only the final task for a range of elements will respond to and 2293 * free the message 2294 */ 2295 if (task->type == VD_NONFINAL_RANGE_TASK) { 2296 return; 2297 } 2298 2299 /* 2300 * We should only send an ACK/NACK here if we are not currently in 2301 * reset as, depending on how we reset, the dring may have been 2302 * blown away and we don't want to ACK/NACK a message that isn't 2303 * there. 2304 */ 2305 if (!vd->reset_state) 2306 vd_notify(task); 2307 } 2308 2309 /* 2310 * Description: 2311 * This is the basic completion function called to handle inband data 2312 * requests and handshake messages. All it needs to do is trigger a 2313 * message to the client that the request is completed. 2314 * 2315 * Parameters: 2316 * arg - opaque pointer to structure containing task to be completed 2317 * 2318 * Return Values 2319 * None 2320 */ 2321 static void 2322 vd_serial_notify(void *arg) 2323 { 2324 vd_task_t *task = (vd_task_t *)arg; 2325 2326 ASSERT(task != NULL); 2327 vd_notify(task); 2328 } 2329 2330 /* ARGSUSED */ 2331 static int 2332 vd_geom2dk_geom(void *vd_buf, size_t vd_buf_len, void *ioctl_arg) 2333 { 2334 VD_GEOM2DK_GEOM((vd_geom_t *)vd_buf, (struct dk_geom *)ioctl_arg); 2335 return (0); 2336 } 2337 2338 /* ARGSUSED */ 2339 static int 2340 vd_vtoc2vtoc(void *vd_buf, size_t vd_buf_len, void *ioctl_arg) 2341 { 2342 VD_VTOC2VTOC((vd_vtoc_t *)vd_buf, (struct extvtoc *)ioctl_arg); 2343 return (0); 2344 } 2345 2346 static void 2347 dk_geom2vd_geom(void *ioctl_arg, void *vd_buf) 2348 { 2349 DK_GEOM2VD_GEOM((struct dk_geom *)ioctl_arg, (vd_geom_t *)vd_buf); 2350 } 2351 2352 static void 2353 vtoc2vd_vtoc(void *ioctl_arg, void *vd_buf) 2354 { 2355 VTOC2VD_VTOC((struct extvtoc *)ioctl_arg, (vd_vtoc_t *)vd_buf); 2356 } 2357 2358 static int 2359 vd_get_efi_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg) 2360 { 2361 vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 2362 dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 2363 size_t data_len; 2364 2365 data_len = vd_buf_len - (sizeof (vd_efi_t) - sizeof (uint64_t)); 2366 if (vd_efi->length > data_len) 2367 return (EINVAL); 2368 2369 dk_efi->dki_lba = vd_efi->lba; 2370 dk_efi->dki_length = vd_efi->length; 2371 dk_efi->dki_data = kmem_zalloc(vd_efi->length, KM_SLEEP); 2372 return (0); 2373 } 2374 2375 static void 2376 vd_get_efi_out(void *ioctl_arg, void *vd_buf) 2377 { 2378 int len; 2379 vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 2380 dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 2381 2382 len = vd_efi->length; 2383 DK_EFI2VD_EFI(dk_efi, vd_efi); 2384 kmem_free(dk_efi->dki_data, len); 2385 } 2386 2387 static int 2388 vd_set_efi_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg) 2389 { 2390 vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 2391 dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 2392 size_t data_len; 2393 2394 data_len = vd_buf_len - (sizeof (vd_efi_t) - sizeof (uint64_t)); 2395 if (vd_efi->length > data_len) 2396 return (EINVAL); 2397 2398 dk_efi->dki_data = kmem_alloc(vd_efi->length, KM_SLEEP); 2399 VD_EFI2DK_EFI(vd_efi, dk_efi); 2400 return (0); 2401 } 2402 2403 static void 2404 vd_set_efi_out(void *ioctl_arg, void *vd_buf) 2405 { 2406 vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 2407 dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 2408 2409 kmem_free(dk_efi->dki_data, vd_efi->length); 2410 } 2411 2412 static int 2413 vd_scsicmd_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg) 2414 { 2415 size_t vd_scsi_len; 2416 vd_scsi_t *vd_scsi = (vd_scsi_t *)vd_buf; 2417 struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl_arg; 2418 2419 /* check buffer size */ 2420 vd_scsi_len = VD_SCSI_SIZE; 2421 vd_scsi_len += P2ROUNDUP(vd_scsi->cdb_len, sizeof (uint64_t)); 2422 vd_scsi_len += P2ROUNDUP(vd_scsi->sense_len, sizeof (uint64_t)); 2423 vd_scsi_len += P2ROUNDUP(vd_scsi->datain_len, sizeof (uint64_t)); 2424 vd_scsi_len += P2ROUNDUP(vd_scsi->dataout_len, sizeof (uint64_t)); 2425 2426 ASSERT(vd_scsi_len % sizeof (uint64_t) == 0); 2427 2428 if (vd_buf_len < vd_scsi_len) 2429 return (EINVAL); 2430 2431 /* set flags */ 2432 uscsi->uscsi_flags = vd_scsi_debug; 2433 2434 if (vd_scsi->options & VD_SCSI_OPT_NORETRY) { 2435 uscsi->uscsi_flags |= USCSI_ISOLATE; 2436 uscsi->uscsi_flags |= USCSI_DIAGNOSE; 2437 } 2438 2439 /* task attribute */ 2440 switch (vd_scsi->task_attribute) { 2441 case VD_SCSI_TASK_ACA: 2442 uscsi->uscsi_flags |= USCSI_HEAD; 2443 break; 2444 case VD_SCSI_TASK_HQUEUE: 2445 uscsi->uscsi_flags |= USCSI_HTAG; 2446 break; 2447 case VD_SCSI_TASK_ORDERED: 2448 uscsi->uscsi_flags |= USCSI_OTAG; 2449 break; 2450 default: 2451 uscsi->uscsi_flags |= USCSI_NOTAG; 2452 break; 2453 } 2454 2455 /* timeout */ 2456 uscsi->uscsi_timeout = vd_scsi->timeout; 2457 2458 /* cdb data */ 2459 uscsi->uscsi_cdb = (caddr_t)VD_SCSI_DATA_CDB(vd_scsi); 2460 uscsi->uscsi_cdblen = vd_scsi->cdb_len; 2461 2462 /* sense buffer */ 2463 if (vd_scsi->sense_len != 0) { 2464 uscsi->uscsi_flags |= USCSI_RQENABLE; 2465 uscsi->uscsi_rqbuf = (caddr_t)VD_SCSI_DATA_SENSE(vd_scsi); 2466 uscsi->uscsi_rqlen = vd_scsi->sense_len; 2467 } 2468 2469 if (vd_scsi->datain_len != 0 && vd_scsi->dataout_len != 0) { 2470 /* uscsi does not support read/write request */ 2471 return (EINVAL); 2472 } 2473 2474 /* request data-in */ 2475 if (vd_scsi->datain_len != 0) { 2476 uscsi->uscsi_flags |= USCSI_READ; 2477 uscsi->uscsi_buflen = vd_scsi->datain_len; 2478 uscsi->uscsi_bufaddr = (char *)VD_SCSI_DATA_IN(vd_scsi); 2479 } 2480 2481 /* request data-out */ 2482 if (vd_scsi->dataout_len != 0) { 2483 uscsi->uscsi_buflen = vd_scsi->dataout_len; 2484 uscsi->uscsi_bufaddr = (char *)VD_SCSI_DATA_OUT(vd_scsi); 2485 } 2486 2487 return (0); 2488 } 2489 2490 static void 2491 vd_scsicmd_out(void *ioctl_arg, void *vd_buf) 2492 { 2493 vd_scsi_t *vd_scsi = (vd_scsi_t *)vd_buf; 2494 struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl_arg; 2495 2496 /* output fields */ 2497 vd_scsi->cmd_status = uscsi->uscsi_status; 2498 2499 /* sense data */ 2500 if ((uscsi->uscsi_flags & USCSI_RQENABLE) && 2501 (uscsi->uscsi_status == STATUS_CHECK || 2502 uscsi->uscsi_status == STATUS_TERMINATED)) { 2503 vd_scsi->sense_status = uscsi->uscsi_rqstatus; 2504 if (uscsi->uscsi_rqstatus == STATUS_GOOD) 2505 vd_scsi->sense_len -= uscsi->uscsi_rqresid; 2506 else 2507 vd_scsi->sense_len = 0; 2508 } else { 2509 vd_scsi->sense_len = 0; 2510 } 2511 2512 if (uscsi->uscsi_status != STATUS_GOOD) { 2513 vd_scsi->dataout_len = 0; 2514 vd_scsi->datain_len = 0; 2515 return; 2516 } 2517 2518 if (uscsi->uscsi_flags & USCSI_READ) { 2519 /* request data (read) */ 2520 vd_scsi->datain_len -= uscsi->uscsi_resid; 2521 vd_scsi->dataout_len = 0; 2522 } else { 2523 /* request data (write) */ 2524 vd_scsi->datain_len = 0; 2525 vd_scsi->dataout_len -= uscsi->uscsi_resid; 2526 } 2527 } 2528 2529 static ushort_t 2530 vd_lbl2cksum(struct dk_label *label) 2531 { 2532 int count; 2533 ushort_t sum, *sp; 2534 2535 count = (sizeof (struct dk_label)) / (sizeof (short)) - 1; 2536 sp = (ushort_t *)label; 2537 sum = 0; 2538 while (count--) { 2539 sum ^= *sp++; 2540 } 2541 2542 return (sum); 2543 } 2544 2545 /* 2546 * Copy information from a vtoc and dk_geom structures to a dk_label structure. 2547 */ 2548 static void 2549 vd_vtocgeom_to_label(struct extvtoc *vtoc, struct dk_geom *geom, 2550 struct dk_label *label) 2551 { 2552 int i; 2553 2554 ASSERT(vtoc->v_nparts == V_NUMPAR); 2555 ASSERT(vtoc->v_sanity == VTOC_SANE); 2556 2557 bzero(label, sizeof (struct dk_label)); 2558 2559 label->dkl_ncyl = geom->dkg_ncyl; 2560 label->dkl_acyl = geom->dkg_acyl; 2561 label->dkl_pcyl = geom->dkg_pcyl; 2562 label->dkl_nhead = geom->dkg_nhead; 2563 label->dkl_nsect = geom->dkg_nsect; 2564 label->dkl_intrlv = geom->dkg_intrlv; 2565 label->dkl_apc = geom->dkg_apc; 2566 label->dkl_rpm = geom->dkg_rpm; 2567 label->dkl_write_reinstruct = geom->dkg_write_reinstruct; 2568 label->dkl_read_reinstruct = geom->dkg_read_reinstruct; 2569 2570 label->dkl_vtoc.v_nparts = V_NUMPAR; 2571 label->dkl_vtoc.v_sanity = VTOC_SANE; 2572 label->dkl_vtoc.v_version = vtoc->v_version; 2573 for (i = 0; i < V_NUMPAR; i++) { 2574 label->dkl_vtoc.v_timestamp[i] = vtoc->timestamp[i]; 2575 label->dkl_vtoc.v_part[i].p_tag = vtoc->v_part[i].p_tag; 2576 label->dkl_vtoc.v_part[i].p_flag = vtoc->v_part[i].p_flag; 2577 label->dkl_map[i].dkl_cylno = vtoc->v_part[i].p_start / 2578 (label->dkl_nhead * label->dkl_nsect); 2579 label->dkl_map[i].dkl_nblk = vtoc->v_part[i].p_size; 2580 } 2581 2582 /* 2583 * The bootinfo array can not be copied with bcopy() because 2584 * elements are of type long in vtoc (so 64-bit) and of type 2585 * int in dk_vtoc (so 32-bit). 2586 */ 2587 label->dkl_vtoc.v_bootinfo[0] = vtoc->v_bootinfo[0]; 2588 label->dkl_vtoc.v_bootinfo[1] = vtoc->v_bootinfo[1]; 2589 label->dkl_vtoc.v_bootinfo[2] = vtoc->v_bootinfo[2]; 2590 bcopy(vtoc->v_asciilabel, label->dkl_asciilabel, LEN_DKL_ASCII); 2591 bcopy(vtoc->v_volume, label->dkl_vtoc.v_volume, LEN_DKL_VVOL); 2592 2593 /* re-compute checksum */ 2594 label->dkl_magic = DKL_MAGIC; 2595 label->dkl_cksum = vd_lbl2cksum(label); 2596 } 2597 2598 /* 2599 * Copy information from a dk_label structure to a vtoc and dk_geom structures. 2600 */ 2601 static void 2602 vd_label_to_vtocgeom(struct dk_label *label, struct extvtoc *vtoc, 2603 struct dk_geom *geom) 2604 { 2605 int i; 2606 2607 bzero(vtoc, sizeof (struct vtoc)); 2608 bzero(geom, sizeof (struct dk_geom)); 2609 2610 geom->dkg_ncyl = label->dkl_ncyl; 2611 geom->dkg_acyl = label->dkl_acyl; 2612 geom->dkg_nhead = label->dkl_nhead; 2613 geom->dkg_nsect = label->dkl_nsect; 2614 geom->dkg_intrlv = label->dkl_intrlv; 2615 geom->dkg_apc = label->dkl_apc; 2616 geom->dkg_rpm = label->dkl_rpm; 2617 geom->dkg_pcyl = label->dkl_pcyl; 2618 geom->dkg_write_reinstruct = label->dkl_write_reinstruct; 2619 geom->dkg_read_reinstruct = label->dkl_read_reinstruct; 2620 2621 vtoc->v_sanity = label->dkl_vtoc.v_sanity; 2622 vtoc->v_version = label->dkl_vtoc.v_version; 2623 vtoc->v_sectorsz = DEV_BSIZE; 2624 vtoc->v_nparts = label->dkl_vtoc.v_nparts; 2625 2626 for (i = 0; i < vtoc->v_nparts; i++) { 2627 vtoc->v_part[i].p_tag = label->dkl_vtoc.v_part[i].p_tag; 2628 vtoc->v_part[i].p_flag = label->dkl_vtoc.v_part[i].p_flag; 2629 vtoc->v_part[i].p_start = label->dkl_map[i].dkl_cylno * 2630 (label->dkl_nhead * label->dkl_nsect); 2631 vtoc->v_part[i].p_size = label->dkl_map[i].dkl_nblk; 2632 vtoc->timestamp[i] = label->dkl_vtoc.v_timestamp[i]; 2633 } 2634 2635 /* 2636 * The bootinfo array can not be copied with bcopy() because 2637 * elements are of type long in vtoc (so 64-bit) and of type 2638 * int in dk_vtoc (so 32-bit). 2639 */ 2640 vtoc->v_bootinfo[0] = label->dkl_vtoc.v_bootinfo[0]; 2641 vtoc->v_bootinfo[1] = label->dkl_vtoc.v_bootinfo[1]; 2642 vtoc->v_bootinfo[2] = label->dkl_vtoc.v_bootinfo[2]; 2643 bcopy(label->dkl_asciilabel, vtoc->v_asciilabel, LEN_DKL_ASCII); 2644 bcopy(label->dkl_vtoc.v_volume, vtoc->v_volume, LEN_DKL_VVOL); 2645 } 2646 2647 /* 2648 * Check if a geometry is valid for a single-slice disk. A geometry is 2649 * considered valid if the main attributes of the geometry match with the 2650 * attributes of the fake geometry we have created. 2651 */ 2652 static boolean_t 2653 vd_slice_geom_isvalid(vd_t *vd, struct dk_geom *geom) 2654 { 2655 ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 2656 ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC); 2657 2658 if (geom->dkg_ncyl != vd->dk_geom.dkg_ncyl || 2659 geom->dkg_acyl != vd->dk_geom.dkg_acyl || 2660 geom->dkg_nsect != vd->dk_geom.dkg_nsect || 2661 geom->dkg_pcyl != vd->dk_geom.dkg_pcyl) 2662 return (B_FALSE); 2663 2664 return (B_TRUE); 2665 } 2666 2667 /* 2668 * Check if a vtoc is valid for a single-slice disk. A vtoc is considered 2669 * valid if the main attributes of the vtoc match with the attributes of the 2670 * fake vtoc we have created. 2671 */ 2672 static boolean_t 2673 vd_slice_vtoc_isvalid(vd_t *vd, struct extvtoc *vtoc) 2674 { 2675 size_t csize; 2676 int i; 2677 2678 ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 2679 ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC); 2680 2681 if (vtoc->v_sanity != vd->vtoc.v_sanity || 2682 vtoc->v_version != vd->vtoc.v_version || 2683 vtoc->v_nparts != vd->vtoc.v_nparts || 2684 strcmp(vtoc->v_volume, vd->vtoc.v_volume) != 0 || 2685 strcmp(vtoc->v_asciilabel, vd->vtoc.v_asciilabel) != 0) 2686 return (B_FALSE); 2687 2688 /* slice 2 should be unchanged */ 2689 if (vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_start != 2690 vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_start || 2691 vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_size != 2692 vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_size) 2693 return (B_FALSE); 2694 2695 /* 2696 * Slice 0 should be mostly unchanged and cover most of the disk. 2697 * However we allow some flexibility wrt to the start and the size 2698 * of this slice mainly because we can't exactly know how it will 2699 * be defined by the OS installer. 2700 * 2701 * We allow slice 0 to be defined as starting on any of the first 2702 * 4 cylinders. 2703 */ 2704 csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect; 2705 2706 if (vtoc->v_part[0].p_start > 4 * csize || 2707 vtoc->v_part[0].p_size > vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_size) 2708 return (B_FALSE); 2709 2710 if (vd->vtoc.v_part[0].p_size >= 4 * csize && 2711 vtoc->v_part[0].p_size < vd->vtoc.v_part[0].p_size - 4 *csize) 2712 return (B_FALSE); 2713 2714 /* any other slice should have a size of 0 */ 2715 for (i = 1; i < vtoc->v_nparts; i++) { 2716 if (i != VD_ENTIRE_DISK_SLICE && 2717 vtoc->v_part[i].p_size != 0) 2718 return (B_FALSE); 2719 } 2720 2721 return (B_TRUE); 2722 } 2723 2724 /* 2725 * Handle ioctls to a disk slice. 2726 * 2727 * Return Values 2728 * 0 - Indicates that there are no errors in disk operations 2729 * ENOTSUP - Unknown disk label type or unsupported DKIO ioctl 2730 * EINVAL - Not enough room to copy the EFI label 2731 * 2732 */ 2733 static int 2734 vd_do_slice_ioctl(vd_t *vd, int cmd, void *ioctl_arg) 2735 { 2736 dk_efi_t *dk_ioc; 2737 struct extvtoc *vtoc; 2738 struct dk_geom *geom; 2739 size_t len, lba; 2740 int rval; 2741 2742 ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 2743 2744 if (cmd == DKIOCFLUSHWRITECACHE) { 2745 if (vd->file) { 2746 return (VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL)); 2747 } else { 2748 return (ldi_ioctl(vd->ldi_handle[0], cmd, 2749 (intptr_t)ioctl_arg, vd->open_flags | FKIOCTL, 2750 kcred, &rval)); 2751 } 2752 } 2753 2754 switch (vd->vdisk_label) { 2755 2756 /* ioctls for a single slice disk with a VTOC label */ 2757 case VD_DISK_LABEL_VTOC: 2758 2759 switch (cmd) { 2760 2761 case DKIOCGGEOM: 2762 ASSERT(ioctl_arg != NULL); 2763 bcopy(&vd->dk_geom, ioctl_arg, sizeof (vd->dk_geom)); 2764 return (0); 2765 2766 case DKIOCGEXTVTOC: 2767 ASSERT(ioctl_arg != NULL); 2768 bcopy(&vd->vtoc, ioctl_arg, sizeof (vd->vtoc)); 2769 return (0); 2770 2771 case DKIOCSGEOM: 2772 ASSERT(ioctl_arg != NULL); 2773 if (vd_slice_single_slice) 2774 return (ENOTSUP); 2775 2776 /* fake success only if new geometry is valid */ 2777 geom = (struct dk_geom *)ioctl_arg; 2778 if (!vd_slice_geom_isvalid(vd, geom)) 2779 return (EINVAL); 2780 2781 return (0); 2782 2783 case DKIOCSEXTVTOC: 2784 ASSERT(ioctl_arg != NULL); 2785 if (vd_slice_single_slice) 2786 return (ENOTSUP); 2787 2788 /* fake sucess only if the new vtoc is valid */ 2789 vtoc = (struct extvtoc *)ioctl_arg; 2790 if (!vd_slice_vtoc_isvalid(vd, vtoc)) 2791 return (EINVAL); 2792 2793 return (0); 2794 2795 default: 2796 return (ENOTSUP); 2797 } 2798 2799 /* ioctls for a single slice disk with an EFI label */ 2800 case VD_DISK_LABEL_EFI: 2801 2802 if (cmd != DKIOCGETEFI && cmd != DKIOCSETEFI) 2803 return (ENOTSUP); 2804 2805 ASSERT(ioctl_arg != NULL); 2806 dk_ioc = (dk_efi_t *)ioctl_arg; 2807 2808 len = dk_ioc->dki_length; 2809 lba = dk_ioc->dki_lba; 2810 2811 if ((lba != VD_EFI_LBA_GPT && lba != VD_EFI_LBA_GPE) || 2812 (lba == VD_EFI_LBA_GPT && len < sizeof (efi_gpt_t)) || 2813 (lba == VD_EFI_LBA_GPE && len < sizeof (efi_gpe_t))) 2814 return (EINVAL); 2815 2816 switch (cmd) { 2817 case DKIOCGETEFI: 2818 len = vd_slice_flabel_read(vd, 2819 (caddr_t)dk_ioc->dki_data, lba * DEV_BSIZE, len); 2820 2821 ASSERT(len > 0); 2822 2823 return (0); 2824 2825 case DKIOCSETEFI: 2826 if (vd_slice_single_slice) 2827 return (ENOTSUP); 2828 2829 /* we currently don't support writing EFI */ 2830 return (EIO); 2831 } 2832 2833 default: 2834 /* Unknown disk label type */ 2835 return (ENOTSUP); 2836 } 2837 } 2838 2839 static int 2840 vds_efi_alloc_and_read(vd_t *vd, efi_gpt_t **gpt, efi_gpe_t **gpe) 2841 { 2842 vd_efi_dev_t edev; 2843 int status; 2844 2845 VD_EFI_DEV_SET(edev, vd, (vd_efi_ioctl_func)vd_backend_ioctl); 2846 2847 status = vd_efi_alloc_and_read(&edev, gpt, gpe); 2848 2849 return (status); 2850 } 2851 2852 static void 2853 vds_efi_free(vd_t *vd, efi_gpt_t *gpt, efi_gpe_t *gpe) 2854 { 2855 vd_efi_dev_t edev; 2856 2857 VD_EFI_DEV_SET(edev, vd, (vd_efi_ioctl_func)vd_backend_ioctl); 2858 2859 vd_efi_free(&edev, gpt, gpe); 2860 } 2861 2862 static int 2863 vd_file_validate_efi(vd_t *vd) 2864 { 2865 efi_gpt_t *gpt; 2866 efi_gpe_t *gpe; 2867 int i, nparts, status; 2868 struct uuid efi_reserved = EFI_RESERVED; 2869 2870 if ((status = vds_efi_alloc_and_read(vd, &gpt, &gpe)) != 0) 2871 return (status); 2872 2873 bzero(&vd->vtoc, sizeof (struct extvtoc)); 2874 bzero(&vd->dk_geom, sizeof (struct dk_geom)); 2875 bzero(vd->slices, sizeof (vd_slice_t) * VD_MAXPART); 2876 2877 vd->efi_reserved = -1; 2878 2879 nparts = gpt->efi_gpt_NumberOfPartitionEntries; 2880 2881 for (i = 0; i < nparts && i < VD_MAXPART; i++) { 2882 2883 if (gpe[i].efi_gpe_StartingLBA == 0 || 2884 gpe[i].efi_gpe_EndingLBA == 0) { 2885 continue; 2886 } 2887 2888 vd->slices[i].start = gpe[i].efi_gpe_StartingLBA; 2889 vd->slices[i].nblocks = gpe[i].efi_gpe_EndingLBA - 2890 gpe[i].efi_gpe_StartingLBA + 1; 2891 2892 if (bcmp(&gpe[i].efi_gpe_PartitionTypeGUID, &efi_reserved, 2893 sizeof (struct uuid)) == 0) 2894 vd->efi_reserved = i; 2895 2896 } 2897 2898 ASSERT(vd->vdisk_size != 0); 2899 vd->slices[VD_EFI_WD_SLICE].start = 0; 2900 vd->slices[VD_EFI_WD_SLICE].nblocks = vd->vdisk_size; 2901 2902 vds_efi_free(vd, gpt, gpe); 2903 2904 return (status); 2905 } 2906 2907 /* 2908 * Function: 2909 * vd_file_validate_geometry 2910 * 2911 * Description: 2912 * Read the label and validate the geometry of a disk image. The driver 2913 * label, vtoc and geometry information are updated according to the 2914 * label read from the disk image. 2915 * 2916 * If no valid label is found, the label is set to unknown and the 2917 * function returns EINVAL, but a default vtoc and geometry are provided 2918 * to the driver. If an EFI label is found, ENOTSUP is returned. 2919 * 2920 * Parameters: 2921 * vd - disk on which the operation is performed. 2922 * 2923 * Return Code: 2924 * 0 - success. 2925 * EIO - error reading the label from the disk image. 2926 * EINVAL - unknown disk label. 2927 * ENOTSUP - geometry not applicable (EFI label). 2928 */ 2929 static int 2930 vd_file_validate_geometry(vd_t *vd) 2931 { 2932 struct dk_label label; 2933 struct dk_geom *geom = &vd->dk_geom; 2934 struct extvtoc *vtoc = &vd->vtoc; 2935 int i; 2936 int status = 0; 2937 2938 ASSERT(vd->file); 2939 ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK); 2940 2941 if (VD_FILE_LABEL_READ(vd, &label) < 0) 2942 return (EIO); 2943 2944 if (label.dkl_magic != DKL_MAGIC || 2945 label.dkl_cksum != vd_lbl2cksum(&label) || 2946 (vd_file_validate_sanity && label.dkl_vtoc.v_sanity != VTOC_SANE) || 2947 label.dkl_vtoc.v_nparts != V_NUMPAR) { 2948 2949 if (vd_file_validate_efi(vd) == 0) { 2950 vd->vdisk_label = VD_DISK_LABEL_EFI; 2951 return (ENOTSUP); 2952 } 2953 2954 vd->vdisk_label = VD_DISK_LABEL_UNK; 2955 vd_build_default_label(vd->file_size, &label); 2956 status = EINVAL; 2957 } else { 2958 vd->vdisk_label = VD_DISK_LABEL_VTOC; 2959 } 2960 2961 /* Update the driver geometry and vtoc */ 2962 vd_label_to_vtocgeom(&label, vtoc, geom); 2963 2964 /* Update logical partitions */ 2965 bzero(vd->slices, sizeof (vd_slice_t) * VD_MAXPART); 2966 if (vd->vdisk_label != VD_DISK_LABEL_UNK) { 2967 for (i = 0; i < vtoc->v_nparts; i++) { 2968 vd->slices[i].start = vtoc->v_part[i].p_start; 2969 vd->slices[i].nblocks = vtoc->v_part[i].p_size; 2970 } 2971 } 2972 2973 return (status); 2974 } 2975 2976 /* 2977 * Handle ioctls to a disk image (file-based). 2978 * 2979 * Return Values 2980 * 0 - Indicates that there are no errors 2981 * != 0 - Disk operation returned an error 2982 */ 2983 static int 2984 vd_do_file_ioctl(vd_t *vd, int cmd, void *ioctl_arg) 2985 { 2986 struct dk_label label; 2987 struct dk_geom *geom; 2988 struct extvtoc *vtoc; 2989 dk_efi_t *efi; 2990 int rc; 2991 2992 ASSERT(vd->file); 2993 ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK); 2994 2995 switch (cmd) { 2996 2997 case DKIOCGGEOM: 2998 ASSERT(ioctl_arg != NULL); 2999 geom = (struct dk_geom *)ioctl_arg; 3000 3001 rc = vd_file_validate_geometry(vd); 3002 if (rc != 0 && rc != EINVAL) 3003 return (rc); 3004 bcopy(&vd->dk_geom, geom, sizeof (struct dk_geom)); 3005 return (0); 3006 3007 case DKIOCGEXTVTOC: 3008 ASSERT(ioctl_arg != NULL); 3009 vtoc = (struct extvtoc *)ioctl_arg; 3010 3011 rc = vd_file_validate_geometry(vd); 3012 if (rc != 0 && rc != EINVAL) 3013 return (rc); 3014 bcopy(&vd->vtoc, vtoc, sizeof (struct extvtoc)); 3015 return (0); 3016 3017 case DKIOCSGEOM: 3018 ASSERT(ioctl_arg != NULL); 3019 geom = (struct dk_geom *)ioctl_arg; 3020 3021 if (geom->dkg_nhead == 0 || geom->dkg_nsect == 0) 3022 return (EINVAL); 3023 3024 /* 3025 * The current device geometry is not updated, just the driver 3026 * "notion" of it. The device geometry will be effectively 3027 * updated when a label is written to the device during a next 3028 * DKIOCSEXTVTOC. 3029 */ 3030 bcopy(ioctl_arg, &vd->dk_geom, sizeof (vd->dk_geom)); 3031 return (0); 3032 3033 case DKIOCSEXTVTOC: 3034 ASSERT(ioctl_arg != NULL); 3035 ASSERT(vd->dk_geom.dkg_nhead != 0 && 3036 vd->dk_geom.dkg_nsect != 0); 3037 vtoc = (struct extvtoc *)ioctl_arg; 3038 3039 if (vtoc->v_sanity != VTOC_SANE || 3040 vtoc->v_sectorsz != DEV_BSIZE || 3041 vtoc->v_nparts != V_NUMPAR) 3042 return (EINVAL); 3043 3044 vd_vtocgeom_to_label(vtoc, &vd->dk_geom, &label); 3045 3046 /* write label to the disk image */ 3047 if ((rc = vd_file_set_vtoc(vd, &label)) != 0) 3048 return (rc); 3049 3050 break; 3051 3052 case DKIOCFLUSHWRITECACHE: 3053 return (VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL)); 3054 3055 case DKIOCGETEFI: 3056 ASSERT(ioctl_arg != NULL); 3057 efi = (dk_efi_t *)ioctl_arg; 3058 3059 if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, 3060 (caddr_t)efi->dki_data, efi->dki_lba, efi->dki_length) < 0) 3061 return (EIO); 3062 3063 return (0); 3064 3065 case DKIOCSETEFI: 3066 ASSERT(ioctl_arg != NULL); 3067 efi = (dk_efi_t *)ioctl_arg; 3068 3069 if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, 3070 (caddr_t)efi->dki_data, efi->dki_lba, efi->dki_length) < 0) 3071 return (EIO); 3072 3073 break; 3074 3075 3076 default: 3077 return (ENOTSUP); 3078 } 3079 3080 ASSERT(cmd == DKIOCSEXTVTOC || cmd == DKIOCSETEFI); 3081 3082 /* label has changed, revalidate the geometry */ 3083 (void) vd_file_validate_geometry(vd); 3084 3085 /* 3086 * The disk geometry may have changed, so we need to write 3087 * the devid (if there is one) so that it is stored at the 3088 * right location. 3089 */ 3090 if (vd_file_write_devid(vd, vd->file_devid) != 0) { 3091 PR0("Fail to write devid"); 3092 } 3093 3094 return (0); 3095 } 3096 3097 static int 3098 vd_backend_ioctl(vd_t *vd, int cmd, caddr_t arg) 3099 { 3100 int rval = 0, status; 3101 struct vtoc vtoc; 3102 3103 /* 3104 * Call the appropriate function to execute the ioctl depending 3105 * on the type of vdisk. 3106 */ 3107 if (vd->vdisk_type == VD_DISK_TYPE_SLICE) { 3108 3109 /* slice, file or volume exported as a single slice disk */ 3110 status = vd_do_slice_ioctl(vd, cmd, arg); 3111 3112 } else if (vd->file) { 3113 3114 /* file or volume exported as a full disk */ 3115 status = vd_do_file_ioctl(vd, cmd, arg); 3116 3117 } else { 3118 3119 /* disk device exported as a full disk */ 3120 status = ldi_ioctl(vd->ldi_handle[0], cmd, (intptr_t)arg, 3121 vd->open_flags | FKIOCTL, kcred, &rval); 3122 3123 /* 3124 * By default VTOC ioctls are done using ioctls for the 3125 * extended VTOC. Some drivers (in particular non-Sun drivers) 3126 * may not support these ioctls. In that case, we fallback to 3127 * the regular VTOC ioctls. 3128 */ 3129 if (status == ENOTTY) { 3130 switch (cmd) { 3131 3132 case DKIOCGEXTVTOC: 3133 cmd = DKIOCGVTOC; 3134 status = ldi_ioctl(vd->ldi_handle[0], cmd, 3135 (intptr_t)&vtoc, vd->open_flags | FKIOCTL, 3136 kcred, &rval); 3137 vtoctoextvtoc(vtoc, 3138 (*(struct extvtoc *)(void *)arg)); 3139 break; 3140 3141 case DKIOCSEXTVTOC: 3142 cmd = DKIOCSVTOC; 3143 extvtoctovtoc((*(struct extvtoc *)(void *)arg), 3144 vtoc); 3145 status = ldi_ioctl(vd->ldi_handle[0], cmd, 3146 (intptr_t)&vtoc, vd->open_flags | FKIOCTL, 3147 kcred, &rval); 3148 break; 3149 } 3150 } 3151 } 3152 3153 #ifdef DEBUG 3154 if (rval != 0) { 3155 PR0("ioctl %x set rval = %d, which is not being returned" 3156 " to caller", cmd, rval); 3157 } 3158 #endif /* DEBUG */ 3159 3160 return (status); 3161 } 3162 3163 /* 3164 * Description: 3165 * This is the function that processes the ioctl requests (farming it 3166 * out to functions that handle slices, files or whole disks) 3167 * 3168 * Return Values 3169 * 0 - ioctl operation completed successfully 3170 * != 0 - The LDC error value encountered 3171 * (propagated back up the call stack as a task error) 3172 * 3173 * Side Effect 3174 * sets request->status to the return value of the ioctl function. 3175 */ 3176 static int 3177 vd_do_ioctl(vd_t *vd, vd_dring_payload_t *request, void* buf, vd_ioctl_t *ioctl) 3178 { 3179 int status = 0; 3180 size_t nbytes = request->nbytes; /* modifiable copy */ 3181 3182 3183 ASSERT(request->slice < vd->nslices); 3184 PR0("Performing %s", ioctl->operation_name); 3185 3186 /* Get data from client and convert, if necessary */ 3187 if (ioctl->copyin != NULL) { 3188 ASSERT(nbytes != 0 && buf != NULL); 3189 PR1("Getting \"arg\" data from client"); 3190 if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes, 3191 request->cookie, request->ncookies, 3192 LDC_COPY_IN)) != 0) { 3193 PR0("ldc_mem_copy() returned errno %d " 3194 "copying from client", status); 3195 return (status); 3196 } 3197 3198 /* Convert client's data, if necessary */ 3199 if (ioctl->copyin == VD_IDENTITY_IN) { 3200 /* use client buffer */ 3201 ioctl->arg = buf; 3202 } else { 3203 /* convert client vdisk operation data to ioctl data */ 3204 status = (ioctl->copyin)(buf, nbytes, 3205 (void *)ioctl->arg); 3206 if (status != 0) { 3207 request->status = status; 3208 return (0); 3209 } 3210 } 3211 } 3212 3213 if (ioctl->operation == VD_OP_SCSICMD) { 3214 struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl->arg; 3215 3216 /* check write permission */ 3217 if (!(vd->open_flags & FWRITE) && 3218 !(uscsi->uscsi_flags & USCSI_READ)) { 3219 PR0("uscsi fails because backend is opened read-only"); 3220 request->status = EROFS; 3221 return (0); 3222 } 3223 } 3224 3225 /* 3226 * Send the ioctl to the disk backend. 3227 */ 3228 request->status = vd_backend_ioctl(vd, ioctl->cmd, ioctl->arg); 3229 3230 if (request->status != 0) { 3231 PR0("ioctl(%s) = errno %d", ioctl->cmd_name, request->status); 3232 if (ioctl->operation == VD_OP_SCSICMD && 3233 ((struct uscsi_cmd *)ioctl->arg)->uscsi_status != 0) 3234 /* 3235 * USCSICMD has reported an error and the uscsi_status 3236 * field is not zero. This means that the SCSI command 3237 * has completed but it has an error. So we should 3238 * mark the VD operation has succesfully completed 3239 * and clients can check the SCSI status field for 3240 * SCSI errors. 3241 */ 3242 request->status = 0; 3243 else 3244 return (0); 3245 } 3246 3247 /* Convert data and send to client, if necessary */ 3248 if (ioctl->copyout != NULL) { 3249 ASSERT(nbytes != 0 && buf != NULL); 3250 PR1("Sending \"arg\" data to client"); 3251 3252 /* Convert ioctl data to vdisk operation data, if necessary */ 3253 if (ioctl->copyout != VD_IDENTITY_OUT) 3254 (ioctl->copyout)((void *)ioctl->arg, buf); 3255 3256 if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes, 3257 request->cookie, request->ncookies, 3258 LDC_COPY_OUT)) != 0) { 3259 PR0("ldc_mem_copy() returned errno %d " 3260 "copying to client", status); 3261 return (status); 3262 } 3263 } 3264 3265 return (status); 3266 } 3267 3268 #define RNDSIZE(expr) P2ROUNDUP(sizeof (expr), sizeof (uint64_t)) 3269 3270 /* 3271 * Description: 3272 * This generic function is called by the task queue to complete 3273 * the processing of the tasks. The specific completion function 3274 * is passed in as a field in the task pointer. 3275 * 3276 * Parameters: 3277 * arg - opaque pointer to structure containing task to be completed 3278 * 3279 * Return Values 3280 * None 3281 */ 3282 static void 3283 vd_complete(void *arg) 3284 { 3285 vd_task_t *task = (vd_task_t *)arg; 3286 3287 ASSERT(task != NULL); 3288 ASSERT(task->status == EINPROGRESS); 3289 ASSERT(task->completef != NULL); 3290 3291 task->status = task->completef(task); 3292 if (task->status) 3293 PR0("%s: Error %d completing task", __func__, task->status); 3294 3295 /* Now notify the vDisk client */ 3296 vd_complete_notify(task); 3297 } 3298 3299 static int 3300 vd_ioctl(vd_task_t *task) 3301 { 3302 int i, status; 3303 void *buf = NULL; 3304 struct dk_geom dk_geom = {0}; 3305 struct extvtoc vtoc = {0}; 3306 struct dk_efi dk_efi = {0}; 3307 struct uscsi_cmd uscsi = {0}; 3308 vd_t *vd = task->vd; 3309 vd_dring_payload_t *request = task->request; 3310 vd_ioctl_t ioctl[] = { 3311 /* Command (no-copy) operations */ 3312 {VD_OP_FLUSH, STRINGIZE(VD_OP_FLUSH), 0, 3313 DKIOCFLUSHWRITECACHE, STRINGIZE(DKIOCFLUSHWRITECACHE), 3314 NULL, NULL, NULL, B_TRUE}, 3315 3316 /* "Get" (copy-out) operations */ 3317 {VD_OP_GET_WCE, STRINGIZE(VD_OP_GET_WCE), RNDSIZE(int), 3318 DKIOCGETWCE, STRINGIZE(DKIOCGETWCE), 3319 NULL, VD_IDENTITY_IN, VD_IDENTITY_OUT, B_FALSE}, 3320 {VD_OP_GET_DISKGEOM, STRINGIZE(VD_OP_GET_DISKGEOM), 3321 RNDSIZE(vd_geom_t), 3322 DKIOCGGEOM, STRINGIZE(DKIOCGGEOM), 3323 &dk_geom, NULL, dk_geom2vd_geom, B_FALSE}, 3324 {VD_OP_GET_VTOC, STRINGIZE(VD_OP_GET_VTOC), RNDSIZE(vd_vtoc_t), 3325 DKIOCGEXTVTOC, STRINGIZE(DKIOCGEXTVTOC), 3326 &vtoc, NULL, vtoc2vd_vtoc, B_FALSE}, 3327 {VD_OP_GET_EFI, STRINGIZE(VD_OP_GET_EFI), RNDSIZE(vd_efi_t), 3328 DKIOCGETEFI, STRINGIZE(DKIOCGETEFI), 3329 &dk_efi, vd_get_efi_in, vd_get_efi_out, B_FALSE}, 3330 3331 /* "Set" (copy-in) operations */ 3332 {VD_OP_SET_WCE, STRINGIZE(VD_OP_SET_WCE), RNDSIZE(int), 3333 DKIOCSETWCE, STRINGIZE(DKIOCSETWCE), 3334 NULL, VD_IDENTITY_IN, VD_IDENTITY_OUT, B_TRUE}, 3335 {VD_OP_SET_DISKGEOM, STRINGIZE(VD_OP_SET_DISKGEOM), 3336 RNDSIZE(vd_geom_t), 3337 DKIOCSGEOM, STRINGIZE(DKIOCSGEOM), 3338 &dk_geom, vd_geom2dk_geom, NULL, B_TRUE}, 3339 {VD_OP_SET_VTOC, STRINGIZE(VD_OP_SET_VTOC), RNDSIZE(vd_vtoc_t), 3340 DKIOCSEXTVTOC, STRINGIZE(DKIOCSEXTVTOC), 3341 &vtoc, vd_vtoc2vtoc, NULL, B_TRUE}, 3342 {VD_OP_SET_EFI, STRINGIZE(VD_OP_SET_EFI), RNDSIZE(vd_efi_t), 3343 DKIOCSETEFI, STRINGIZE(DKIOCSETEFI), 3344 &dk_efi, vd_set_efi_in, vd_set_efi_out, B_TRUE}, 3345 3346 {VD_OP_SCSICMD, STRINGIZE(VD_OP_SCSICMD), RNDSIZE(vd_scsi_t), 3347 USCSICMD, STRINGIZE(USCSICMD), 3348 &uscsi, vd_scsicmd_in, vd_scsicmd_out, B_FALSE}, 3349 }; 3350 size_t nioctls = (sizeof (ioctl))/(sizeof (ioctl[0])); 3351 3352 3353 ASSERT(vd != NULL); 3354 ASSERT(request != NULL); 3355 ASSERT(request->slice < vd->nslices); 3356 3357 /* 3358 * Determine ioctl corresponding to caller's "operation" and 3359 * validate caller's "nbytes" 3360 */ 3361 for (i = 0; i < nioctls; i++) { 3362 if (request->operation == ioctl[i].operation) { 3363 /* LDC memory operations require 8-byte multiples */ 3364 ASSERT(ioctl[i].nbytes % sizeof (uint64_t) == 0); 3365 3366 if (request->operation == VD_OP_GET_EFI || 3367 request->operation == VD_OP_SET_EFI || 3368 request->operation == VD_OP_SCSICMD) { 3369 if (request->nbytes >= ioctl[i].nbytes) 3370 break; 3371 PR0("%s: Expected at least nbytes = %lu, " 3372 "got %lu", ioctl[i].operation_name, 3373 ioctl[i].nbytes, request->nbytes); 3374 return (EINVAL); 3375 } 3376 3377 if (request->nbytes != ioctl[i].nbytes) { 3378 PR0("%s: Expected nbytes = %lu, got %lu", 3379 ioctl[i].operation_name, ioctl[i].nbytes, 3380 request->nbytes); 3381 return (EINVAL); 3382 } 3383 3384 break; 3385 } 3386 } 3387 ASSERT(i < nioctls); /* because "operation" already validated */ 3388 3389 if (!(vd->open_flags & FWRITE) && ioctl[i].write) { 3390 PR0("%s fails because backend is opened read-only", 3391 ioctl[i].operation_name); 3392 request->status = EROFS; 3393 return (0); 3394 } 3395 3396 if (request->nbytes) 3397 buf = kmem_zalloc(request->nbytes, KM_SLEEP); 3398 status = vd_do_ioctl(vd, request, buf, &ioctl[i]); 3399 if (request->nbytes) 3400 kmem_free(buf, request->nbytes); 3401 3402 return (status); 3403 } 3404 3405 static int 3406 vd_get_devid(vd_task_t *task) 3407 { 3408 vd_t *vd = task->vd; 3409 vd_dring_payload_t *request = task->request; 3410 vd_devid_t *vd_devid; 3411 impl_devid_t *devid; 3412 int status, bufid_len, devid_len, len, sz; 3413 int bufbytes; 3414 3415 PR1("Get Device ID, nbytes=%ld", request->nbytes); 3416 3417 if (vd->vdisk_type == VD_DISK_TYPE_SLICE) { 3418 /* 3419 * We don't support devid for single-slice disks because we 3420 * have no space to store a fabricated devid and for physical 3421 * disk slices, we can't use the devid of the disk otherwise 3422 * exporting multiple slices from the same disk will produce 3423 * the same devids. 3424 */ 3425 PR2("No Device ID for slices"); 3426 request->status = ENOTSUP; 3427 return (0); 3428 } 3429 3430 if (vd->file) { 3431 if (vd->file_devid == NULL) { 3432 PR2("No Device ID"); 3433 request->status = ENOENT; 3434 return (0); 3435 } else { 3436 sz = ddi_devid_sizeof(vd->file_devid); 3437 devid = kmem_alloc(sz, KM_SLEEP); 3438 bcopy(vd->file_devid, devid, sz); 3439 } 3440 } else { 3441 if (ddi_lyr_get_devid(vd->dev[request->slice], 3442 (ddi_devid_t *)&devid) != DDI_SUCCESS) { 3443 PR2("No Device ID"); 3444 request->status = ENOENT; 3445 return (0); 3446 } 3447 } 3448 3449 bufid_len = request->nbytes - sizeof (vd_devid_t) + 1; 3450 devid_len = DEVID_GETLEN(devid); 3451 3452 /* 3453 * Save the buffer size here for use in deallocation. 3454 * The actual number of bytes copied is returned in 3455 * the 'nbytes' field of the request structure. 3456 */ 3457 bufbytes = request->nbytes; 3458 3459 vd_devid = kmem_zalloc(bufbytes, KM_SLEEP); 3460 vd_devid->length = devid_len; 3461 vd_devid->type = DEVID_GETTYPE(devid); 3462 3463 len = (devid_len > bufid_len)? bufid_len : devid_len; 3464 3465 bcopy(devid->did_id, vd_devid->id, len); 3466 3467 request->status = 0; 3468 3469 /* LDC memory operations require 8-byte multiples */ 3470 ASSERT(request->nbytes % sizeof (uint64_t) == 0); 3471 3472 if ((status = ldc_mem_copy(vd->ldc_handle, (caddr_t)vd_devid, 0, 3473 &request->nbytes, request->cookie, request->ncookies, 3474 LDC_COPY_OUT)) != 0) { 3475 PR0("ldc_mem_copy() returned errno %d copying to client", 3476 status); 3477 } 3478 PR1("post mem_copy: nbytes=%ld", request->nbytes); 3479 3480 kmem_free(vd_devid, bufbytes); 3481 ddi_devid_free((ddi_devid_t)devid); 3482 3483 return (status); 3484 } 3485 3486 static int 3487 vd_scsi_reset(vd_t *vd) 3488 { 3489 int rval, status; 3490 struct uscsi_cmd uscsi = { 0 }; 3491 3492 uscsi.uscsi_flags = vd_scsi_debug | USCSI_RESET; 3493 uscsi.uscsi_timeout = vd_scsi_rdwr_timeout; 3494 3495 status = ldi_ioctl(vd->ldi_handle[0], USCSICMD, (intptr_t)&uscsi, 3496 (vd->open_flags | FKIOCTL), kcred, &rval); 3497 3498 return (status); 3499 } 3500 3501 static int 3502 vd_reset(vd_task_t *task) 3503 { 3504 vd_t *vd = task->vd; 3505 vd_dring_payload_t *request = task->request; 3506 3507 ASSERT(request->operation == VD_OP_RESET); 3508 ASSERT(vd->scsi); 3509 3510 PR0("Performing VD_OP_RESET"); 3511 3512 if (request->nbytes != 0) { 3513 PR0("VD_OP_RESET: Expected nbytes = 0, got %lu", 3514 request->nbytes); 3515 return (EINVAL); 3516 } 3517 3518 request->status = vd_scsi_reset(vd); 3519 3520 return (0); 3521 } 3522 3523 static int 3524 vd_get_capacity(vd_task_t *task) 3525 { 3526 int rv; 3527 size_t nbytes; 3528 vd_t *vd = task->vd; 3529 vd_dring_payload_t *request = task->request; 3530 vd_capacity_t vd_cap = { 0 }; 3531 3532 ASSERT(request->operation == VD_OP_GET_CAPACITY); 3533 3534 PR0("Performing VD_OP_GET_CAPACITY"); 3535 3536 nbytes = request->nbytes; 3537 3538 if (nbytes != RNDSIZE(vd_capacity_t)) { 3539 PR0("VD_OP_GET_CAPACITY: Expected nbytes = %lu, got %lu", 3540 RNDSIZE(vd_capacity_t), nbytes); 3541 return (EINVAL); 3542 } 3543 3544 /* 3545 * Check the backend size in case it has changed. If the check fails 3546 * then we will return the last known size. 3547 */ 3548 3549 (void) vd_backend_check_size(vd); 3550 ASSERT(vd->vdisk_size != 0); 3551 3552 request->status = 0; 3553 3554 vd_cap.vdisk_block_size = vd->vdisk_block_size; 3555 vd_cap.vdisk_size = vd->vdisk_size; 3556 3557 if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&vd_cap, 0, &nbytes, 3558 request->cookie, request->ncookies, LDC_COPY_OUT)) != 0) { 3559 PR0("ldc_mem_copy() returned errno %d copying to client", rv); 3560 return (rv); 3561 } 3562 3563 return (0); 3564 } 3565 3566 static int 3567 vd_get_access(vd_task_t *task) 3568 { 3569 uint64_t access; 3570 int rv, rval = 0; 3571 size_t nbytes; 3572 vd_t *vd = task->vd; 3573 vd_dring_payload_t *request = task->request; 3574 3575 ASSERT(request->operation == VD_OP_GET_ACCESS); 3576 ASSERT(vd->scsi); 3577 3578 PR0("Performing VD_OP_GET_ACCESS"); 3579 3580 nbytes = request->nbytes; 3581 3582 if (nbytes != sizeof (uint64_t)) { 3583 PR0("VD_OP_GET_ACCESS: Expected nbytes = %lu, got %lu", 3584 sizeof (uint64_t), nbytes); 3585 return (EINVAL); 3586 } 3587 3588 request->status = ldi_ioctl(vd->ldi_handle[request->slice], MHIOCSTATUS, 3589 NULL, (vd->open_flags | FKIOCTL), kcred, &rval); 3590 3591 if (request->status != 0) 3592 return (0); 3593 3594 access = (rval == 0)? VD_ACCESS_ALLOWED : VD_ACCESS_DENIED; 3595 3596 if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&access, 0, &nbytes, 3597 request->cookie, request->ncookies, LDC_COPY_OUT)) != 0) { 3598 PR0("ldc_mem_copy() returned errno %d copying to client", rv); 3599 return (rv); 3600 } 3601 3602 return (0); 3603 } 3604 3605 static int 3606 vd_set_access(vd_task_t *task) 3607 { 3608 uint64_t flags; 3609 int rv, rval; 3610 size_t nbytes; 3611 vd_t *vd = task->vd; 3612 vd_dring_payload_t *request = task->request; 3613 3614 ASSERT(request->operation == VD_OP_SET_ACCESS); 3615 ASSERT(vd->scsi); 3616 3617 nbytes = request->nbytes; 3618 3619 if (nbytes != sizeof (uint64_t)) { 3620 PR0("VD_OP_SET_ACCESS: Expected nbytes = %lu, got %lu", 3621 sizeof (uint64_t), nbytes); 3622 return (EINVAL); 3623 } 3624 3625 if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&flags, 0, &nbytes, 3626 request->cookie, request->ncookies, LDC_COPY_IN)) != 0) { 3627 PR0("ldc_mem_copy() returned errno %d copying from client", rv); 3628 return (rv); 3629 } 3630 3631 if (flags == VD_ACCESS_SET_CLEAR) { 3632 PR0("Performing VD_OP_SET_ACCESS (CLEAR)"); 3633 request->status = ldi_ioctl(vd->ldi_handle[request->slice], 3634 MHIOCRELEASE, NULL, (vd->open_flags | FKIOCTL), kcred, 3635 &rval); 3636 if (request->status == 0) 3637 vd->ownership = B_FALSE; 3638 return (0); 3639 } 3640 3641 /* 3642 * As per the VIO spec, the PREEMPT and PRESERVE flags are only valid 3643 * when the EXCLUSIVE flag is set. 3644 */ 3645 if (!(flags & VD_ACCESS_SET_EXCLUSIVE)) { 3646 PR0("Invalid VD_OP_SET_ACCESS flags: 0x%lx", flags); 3647 request->status = EINVAL; 3648 return (0); 3649 } 3650 3651 switch (flags & (VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE)) { 3652 3653 case VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE: 3654 /* 3655 * Flags EXCLUSIVE and PREEMPT and PRESERVE. We have to 3656 * acquire exclusive access rights, preserve them and we 3657 * can use preemption. So we can use the MHIOCTKNOWN ioctl. 3658 */ 3659 PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PREEMPT|PRESERVE)"); 3660 request->status = ldi_ioctl(vd->ldi_handle[request->slice], 3661 MHIOCTKOWN, NULL, (vd->open_flags | FKIOCTL), kcred, &rval); 3662 break; 3663 3664 case VD_ACCESS_SET_PRESERVE: 3665 /* 3666 * Flags EXCLUSIVE and PRESERVE. We have to acquire exclusive 3667 * access rights and preserve them, but not preempt any other 3668 * host. So we need to use the MHIOCTKOWN ioctl to enable the 3669 * "preserve" feature but we can not called it directly 3670 * because it uses preemption. So before that, we use the 3671 * MHIOCQRESERVE ioctl to ensure we can get exclusive rights 3672 * without preempting anyone. 3673 */ 3674 PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PRESERVE)"); 3675 request->status = ldi_ioctl(vd->ldi_handle[request->slice], 3676 MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred, 3677 &rval); 3678 if (request->status != 0) 3679 break; 3680 request->status = ldi_ioctl(vd->ldi_handle[request->slice], 3681 MHIOCTKOWN, NULL, (vd->open_flags | FKIOCTL), kcred, &rval); 3682 break; 3683 3684 case VD_ACCESS_SET_PREEMPT: 3685 /* 3686 * Flags EXCLUSIVE and PREEMPT. We have to acquire exclusive 3687 * access rights and we can use preemption. So we try to do 3688 * a SCSI reservation, if it fails we reset the disk to clear 3689 * any reservation and we try to reserve again. 3690 */ 3691 PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PREEMPT)"); 3692 request->status = ldi_ioctl(vd->ldi_handle[request->slice], 3693 MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred, 3694 &rval); 3695 if (request->status == 0) 3696 break; 3697 3698 /* reset the disk */ 3699 (void) vd_scsi_reset(vd); 3700 3701 /* try again even if the reset has failed */ 3702 request->status = ldi_ioctl(vd->ldi_handle[request->slice], 3703 MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred, 3704 &rval); 3705 break; 3706 3707 case 0: 3708 /* Flag EXCLUSIVE only. Just issue a SCSI reservation */ 3709 PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE)"); 3710 request->status = ldi_ioctl(vd->ldi_handle[request->slice], 3711 MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred, 3712 &rval); 3713 break; 3714 } 3715 3716 if (request->status == 0) 3717 vd->ownership = B_TRUE; 3718 else 3719 PR0("VD_OP_SET_ACCESS: error %d", request->status); 3720 3721 return (0); 3722 } 3723 3724 static void 3725 vd_reset_access(vd_t *vd) 3726 { 3727 int status, rval; 3728 3729 if (vd->file || !vd->ownership) 3730 return; 3731 3732 PR0("Releasing disk ownership"); 3733 status = ldi_ioctl(vd->ldi_handle[0], MHIOCRELEASE, NULL, 3734 (vd->open_flags | FKIOCTL), kcred, &rval); 3735 3736 /* 3737 * An EACCES failure means that there is a reservation conflict, 3738 * so we are not the owner of the disk anymore. 3739 */ 3740 if (status == 0 || status == EACCES) { 3741 vd->ownership = B_FALSE; 3742 return; 3743 } 3744 3745 PR0("Fail to release ownership, error %d", status); 3746 3747 /* 3748 * We have failed to release the ownership, try to reset the disk 3749 * to release reservations. 3750 */ 3751 PR0("Resetting disk"); 3752 status = vd_scsi_reset(vd); 3753 3754 if (status != 0) 3755 PR0("Fail to reset disk, error %d", status); 3756 3757 /* whatever the result of the reset is, we try the release again */ 3758 status = ldi_ioctl(vd->ldi_handle[0], MHIOCRELEASE, NULL, 3759 (vd->open_flags | FKIOCTL), kcred, &rval); 3760 3761 if (status == 0 || status == EACCES) { 3762 vd->ownership = B_FALSE; 3763 return; 3764 } 3765 3766 PR0("Fail to release ownership, error %d", status); 3767 3768 /* 3769 * At this point we have done our best to try to reset the 3770 * access rights to the disk and we don't know if we still 3771 * own a reservation and if any mechanism to preserve the 3772 * ownership is still in place. The ultimate solution would 3773 * be to reset the system but this is usually not what we 3774 * want to happen. 3775 */ 3776 3777 if (vd_reset_access_failure == A_REBOOT) { 3778 cmn_err(CE_WARN, VD_RESET_ACCESS_FAILURE_MSG 3779 ", rebooting the system", vd->device_path); 3780 (void) uadmin(A_SHUTDOWN, AD_BOOT, NULL); 3781 } else if (vd_reset_access_failure == A_DUMP) { 3782 panic(VD_RESET_ACCESS_FAILURE_MSG, vd->device_path); 3783 } 3784 3785 cmn_err(CE_WARN, VD_RESET_ACCESS_FAILURE_MSG, vd->device_path); 3786 } 3787 3788 /* 3789 * Define the supported operations once the functions for performing them have 3790 * been defined 3791 */ 3792 static const vds_operation_t vds_operation[] = { 3793 #define X(_s) #_s, _s 3794 {X(VD_OP_BREAD), vd_start_bio, vd_complete_bio}, 3795 {X(VD_OP_BWRITE), vd_start_bio, vd_complete_bio}, 3796 {X(VD_OP_FLUSH), vd_ioctl, NULL}, 3797 {X(VD_OP_GET_WCE), vd_ioctl, NULL}, 3798 {X(VD_OP_SET_WCE), vd_ioctl, NULL}, 3799 {X(VD_OP_GET_VTOC), vd_ioctl, NULL}, 3800 {X(VD_OP_SET_VTOC), vd_ioctl, NULL}, 3801 {X(VD_OP_GET_DISKGEOM), vd_ioctl, NULL}, 3802 {X(VD_OP_SET_DISKGEOM), vd_ioctl, NULL}, 3803 {X(VD_OP_GET_EFI), vd_ioctl, NULL}, 3804 {X(VD_OP_SET_EFI), vd_ioctl, NULL}, 3805 {X(VD_OP_GET_DEVID), vd_get_devid, NULL}, 3806 {X(VD_OP_SCSICMD), vd_ioctl, NULL}, 3807 {X(VD_OP_RESET), vd_reset, NULL}, 3808 {X(VD_OP_GET_CAPACITY), vd_get_capacity, NULL}, 3809 {X(VD_OP_SET_ACCESS), vd_set_access, NULL}, 3810 {X(VD_OP_GET_ACCESS), vd_get_access, NULL}, 3811 #undef X 3812 }; 3813 3814 static const size_t vds_noperations = 3815 (sizeof (vds_operation))/(sizeof (vds_operation[0])); 3816 3817 /* 3818 * Process a task specifying a client I/O request 3819 * 3820 * Parameters: 3821 * task - structure containing the request sent from client 3822 * 3823 * Return Value 3824 * 0 - success 3825 * ENOTSUP - Unknown/Unsupported VD_OP_XXX operation 3826 * EINVAL - Invalid disk slice 3827 * != 0 - some other non-zero return value from start function 3828 */ 3829 static int 3830 vd_do_process_task(vd_task_t *task) 3831 { 3832 int i; 3833 vd_t *vd = task->vd; 3834 vd_dring_payload_t *request = task->request; 3835 3836 ASSERT(vd != NULL); 3837 ASSERT(request != NULL); 3838 3839 /* Find the requested operation */ 3840 for (i = 0; i < vds_noperations; i++) { 3841 if (request->operation == vds_operation[i].operation) { 3842 /* all operations should have a start func */ 3843 ASSERT(vds_operation[i].start != NULL); 3844 3845 task->completef = vds_operation[i].complete; 3846 break; 3847 } 3848 } 3849 3850 /* 3851 * We need to check that the requested operation is permitted 3852 * for the particular client that sent it or that the loop above 3853 * did not complete without finding the operation type (indicating 3854 * that the requested operation is unknown/unimplemented) 3855 */ 3856 if ((VD_OP_SUPPORTED(vd->operations, request->operation) == B_FALSE) || 3857 (i == vds_noperations)) { 3858 PR0("Unsupported operation %u", request->operation); 3859 request->status = ENOTSUP; 3860 return (0); 3861 } 3862 3863 /* Range-check slice */ 3864 if (request->slice >= vd->nslices && 3865 ((vd->vdisk_type != VD_DISK_TYPE_DISK && vd_slice_single_slice) || 3866 request->slice != VD_SLICE_NONE)) { 3867 PR0("Invalid \"slice\" %u (max %u) for virtual disk", 3868 request->slice, (vd->nslices - 1)); 3869 request->status = EINVAL; 3870 return (0); 3871 } 3872 3873 /* 3874 * Call the function pointer that starts the operation. 3875 */ 3876 return (vds_operation[i].start(task)); 3877 } 3878 3879 /* 3880 * Description: 3881 * This function is called by both the in-band and descriptor ring 3882 * message processing functions paths to actually execute the task 3883 * requested by the vDisk client. It in turn calls its worker 3884 * function, vd_do_process_task(), to carry our the request. 3885 * 3886 * Any transport errors (e.g. LDC errors, vDisk protocol errors) are 3887 * saved in the 'status' field of the task and are propagated back 3888 * up the call stack to trigger a NACK 3889 * 3890 * Any request errors (e.g. ENOTTY from an ioctl) are saved in 3891 * the 'status' field of the request and result in an ACK being sent 3892 * by the completion handler. 3893 * 3894 * Parameters: 3895 * task - structure containing the request sent from client 3896 * 3897 * Return Value 3898 * 0 - successful synchronous request. 3899 * != 0 - transport error (e.g. LDC errors, vDisk protocol) 3900 * EINPROGRESS - task will be finished in a completion handler 3901 */ 3902 static int 3903 vd_process_task(vd_task_t *task) 3904 { 3905 vd_t *vd = task->vd; 3906 int status; 3907 3908 DTRACE_PROBE1(task__start, vd_task_t *, task); 3909 3910 task->status = vd_do_process_task(task); 3911 3912 /* 3913 * If the task processing function returned EINPROGRESS indicating 3914 * that the task needs completing then schedule a taskq entry to 3915 * finish it now. 3916 * 3917 * Otherwise the task processing function returned either zero 3918 * indicating that the task was finished in the start function (and we 3919 * don't need to wait in a completion function) or the start function 3920 * returned an error - in both cases all that needs to happen is the 3921 * notification to the vDisk client higher up the call stack. 3922 * If the task was using a Descriptor Ring, we need to mark it as done 3923 * at this stage. 3924 */ 3925 if (task->status == EINPROGRESS) { 3926 /* Queue a task to complete the operation */ 3927 (void) ddi_taskq_dispatch(vd->completionq, vd_complete, 3928 task, DDI_SLEEP); 3929 3930 } else if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE_V1_0)) { 3931 /* Update the dring element if it's a dring client */ 3932 status = vd_mark_elem_done(vd, task->index, 3933 task->request->status, task->request->nbytes); 3934 if (status == ECONNRESET) 3935 vd_mark_in_reset(vd); 3936 else if (status == EACCES) 3937 vd_need_reset(vd, B_TRUE); 3938 } 3939 3940 return (task->status); 3941 } 3942 3943 /* 3944 * Return true if the "type", "subtype", and "env" fields of the "tag" first 3945 * argument match the corresponding remaining arguments; otherwise, return false 3946 */ 3947 boolean_t 3948 vd_msgtype(vio_msg_tag_t *tag, int type, int subtype, int env) 3949 { 3950 return ((tag->vio_msgtype == type) && 3951 (tag->vio_subtype == subtype) && 3952 (tag->vio_subtype_env == env)) ? B_TRUE : B_FALSE; 3953 } 3954 3955 /* 3956 * Check whether the major/minor version specified in "ver_msg" is supported 3957 * by this server. 3958 */ 3959 static boolean_t 3960 vds_supported_version(vio_ver_msg_t *ver_msg) 3961 { 3962 for (int i = 0; i < vds_num_versions; i++) { 3963 ASSERT(vds_version[i].major > 0); 3964 ASSERT((i == 0) || 3965 (vds_version[i].major < vds_version[i-1].major)); 3966 3967 /* 3968 * If the major versions match, adjust the minor version, if 3969 * necessary, down to the highest value supported by this 3970 * server and return true so this message will get "ack"ed; 3971 * the client should also support all minor versions lower 3972 * than the value it sent 3973 */ 3974 if (ver_msg->ver_major == vds_version[i].major) { 3975 if (ver_msg->ver_minor > vds_version[i].minor) { 3976 PR0("Adjusting minor version from %u to %u", 3977 ver_msg->ver_minor, vds_version[i].minor); 3978 ver_msg->ver_minor = vds_version[i].minor; 3979 } 3980 return (B_TRUE); 3981 } 3982 3983 /* 3984 * If the message contains a higher major version number, set 3985 * the message's major/minor versions to the current values 3986 * and return false, so this message will get "nack"ed with 3987 * these values, and the client will potentially try again 3988 * with the same or a lower version 3989 */ 3990 if (ver_msg->ver_major > vds_version[i].major) { 3991 ver_msg->ver_major = vds_version[i].major; 3992 ver_msg->ver_minor = vds_version[i].minor; 3993 return (B_FALSE); 3994 } 3995 3996 /* 3997 * Otherwise, the message's major version is less than the 3998 * current major version, so continue the loop to the next 3999 * (lower) supported version 4000 */ 4001 } 4002 4003 /* 4004 * No common version was found; "ground" the version pair in the 4005 * message to terminate negotiation 4006 */ 4007 ver_msg->ver_major = 0; 4008 ver_msg->ver_minor = 0; 4009 return (B_FALSE); 4010 } 4011 4012 /* 4013 * Process a version message from a client. vds expects to receive version 4014 * messages from clients seeking service, but never issues version messages 4015 * itself; therefore, vds can ACK or NACK client version messages, but does 4016 * not expect to receive version-message ACKs or NACKs (and will treat such 4017 * messages as invalid). 4018 */ 4019 static int 4020 vd_process_ver_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 4021 { 4022 vio_ver_msg_t *ver_msg = (vio_ver_msg_t *)msg; 4023 4024 4025 ASSERT(msglen >= sizeof (msg->tag)); 4026 4027 if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 4028 VIO_VER_INFO)) { 4029 return (ENOMSG); /* not a version message */ 4030 } 4031 4032 if (msglen != sizeof (*ver_msg)) { 4033 PR0("Expected %lu-byte version message; " 4034 "received %lu bytes", sizeof (*ver_msg), msglen); 4035 return (EBADMSG); 4036 } 4037 4038 if (ver_msg->dev_class != VDEV_DISK) { 4039 PR0("Expected device class %u (disk); received %u", 4040 VDEV_DISK, ver_msg->dev_class); 4041 return (EBADMSG); 4042 } 4043 4044 /* 4045 * We're talking to the expected kind of client; set our device class 4046 * for "ack/nack" back to the client 4047 */ 4048 ver_msg->dev_class = VDEV_DISK_SERVER; 4049 4050 /* 4051 * Check whether the (valid) version message specifies a version 4052 * supported by this server. If the version is not supported, return 4053 * EBADMSG so the message will get "nack"ed; vds_supported_version() 4054 * will have updated the message with a supported version for the 4055 * client to consider 4056 */ 4057 if (!vds_supported_version(ver_msg)) 4058 return (EBADMSG); 4059 4060 4061 /* 4062 * A version has been agreed upon; use the client's SID for 4063 * communication on this channel now 4064 */ 4065 ASSERT(!(vd->initialized & VD_SID)); 4066 vd->sid = ver_msg->tag.vio_sid; 4067 vd->initialized |= VD_SID; 4068 4069 /* 4070 * Store the negotiated major and minor version values in the "vd" data 4071 * structure so that we can check if certain operations are supported 4072 * by the client. 4073 */ 4074 vd->version.major = ver_msg->ver_major; 4075 vd->version.minor = ver_msg->ver_minor; 4076 4077 PR0("Using major version %u, minor version %u", 4078 ver_msg->ver_major, ver_msg->ver_minor); 4079 return (0); 4080 } 4081 4082 static void 4083 vd_set_exported_operations(vd_t *vd) 4084 { 4085 vd->operations = 0; /* clear field */ 4086 4087 /* 4088 * We need to check from the highest version supported to the 4089 * lowest because versions with a higher minor number implicitly 4090 * support versions with a lower minor number. 4091 */ 4092 if (vio_ver_is_supported(vd->version, 1, 1)) { 4093 ASSERT(vd->open_flags & FREAD); 4094 vd->operations |= VD_OP_MASK_READ | (1 << VD_OP_GET_CAPACITY); 4095 4096 if (vd->open_flags & FWRITE) 4097 vd->operations |= VD_OP_MASK_WRITE; 4098 4099 if (vd->scsi) 4100 vd->operations |= VD_OP_MASK_SCSI; 4101 4102 if (vd->file && vd_file_is_iso_image(vd)) { 4103 /* 4104 * can't write to ISO images, make sure that write 4105 * support is not set in case administrator did not 4106 * use "options=ro" when doing an ldm add-vdsdev 4107 */ 4108 vd->operations &= ~VD_OP_MASK_WRITE; 4109 } 4110 } else if (vio_ver_is_supported(vd->version, 1, 0)) { 4111 vd->operations = VD_OP_MASK_READ | VD_OP_MASK_WRITE; 4112 } 4113 4114 /* we should have already agreed on a version */ 4115 ASSERT(vd->operations != 0); 4116 } 4117 4118 static int 4119 vd_process_attr_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 4120 { 4121 vd_attr_msg_t *attr_msg = (vd_attr_msg_t *)msg; 4122 int status, retry = 0; 4123 4124 4125 ASSERT(msglen >= sizeof (msg->tag)); 4126 4127 if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 4128 VIO_ATTR_INFO)) { 4129 PR0("Message is not an attribute message"); 4130 return (ENOMSG); 4131 } 4132 4133 if (msglen != sizeof (*attr_msg)) { 4134 PR0("Expected %lu-byte attribute message; " 4135 "received %lu bytes", sizeof (*attr_msg), msglen); 4136 return (EBADMSG); 4137 } 4138 4139 if (attr_msg->max_xfer_sz == 0) { 4140 PR0("Received maximum transfer size of 0 from client"); 4141 return (EBADMSG); 4142 } 4143 4144 if ((attr_msg->xfer_mode != VIO_DESC_MODE) && 4145 (attr_msg->xfer_mode != VIO_DRING_MODE_V1_0)) { 4146 PR0("Client requested unsupported transfer mode"); 4147 return (EBADMSG); 4148 } 4149 4150 /* 4151 * check if the underlying disk is ready, if not try accessing 4152 * the device again. Open the vdisk device and extract info 4153 * about it, as this is needed to respond to the attr info msg 4154 */ 4155 if ((vd->initialized & VD_DISK_READY) == 0) { 4156 PR0("Retry setting up disk (%s)", vd->device_path); 4157 do { 4158 status = vd_setup_vd(vd); 4159 if (status != EAGAIN || ++retry > vds_dev_retries) 4160 break; 4161 4162 /* incremental delay */ 4163 delay(drv_usectohz(vds_dev_delay)); 4164 4165 /* if vdisk is no longer enabled - return error */ 4166 if (!vd_enabled(vd)) 4167 return (ENXIO); 4168 4169 } while (status == EAGAIN); 4170 4171 if (status) 4172 return (ENXIO); 4173 4174 vd->initialized |= VD_DISK_READY; 4175 ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR); 4176 PR0("vdisk_type = %s, volume = %s, file = %s, nslices = %u", 4177 ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"), 4178 (vd->volume ? "yes" : "no"), 4179 (vd->file ? "yes" : "no"), 4180 vd->nslices); 4181 } 4182 4183 /* Success: valid message and transfer mode */ 4184 vd->xfer_mode = attr_msg->xfer_mode; 4185 4186 if (vd->xfer_mode == VIO_DESC_MODE) { 4187 4188 /* 4189 * The vd_dring_inband_msg_t contains one cookie; need room 4190 * for up to n-1 more cookies, where "n" is the number of full 4191 * pages plus possibly one partial page required to cover 4192 * "max_xfer_sz". Add room for one more cookie if 4193 * "max_xfer_sz" isn't an integral multiple of the page size. 4194 * Must first get the maximum transfer size in bytes. 4195 */ 4196 size_t max_xfer_bytes = attr_msg->vdisk_block_size ? 4197 attr_msg->vdisk_block_size*attr_msg->max_xfer_sz : 4198 attr_msg->max_xfer_sz; 4199 size_t max_inband_msglen = 4200 sizeof (vd_dring_inband_msg_t) + 4201 ((max_xfer_bytes/PAGESIZE + 4202 ((max_xfer_bytes % PAGESIZE) ? 1 : 0))* 4203 (sizeof (ldc_mem_cookie_t))); 4204 4205 /* 4206 * Set the maximum expected message length to 4207 * accommodate in-band-descriptor messages with all 4208 * their cookies 4209 */ 4210 vd->max_msglen = MAX(vd->max_msglen, max_inband_msglen); 4211 4212 /* 4213 * Initialize the data structure for processing in-band I/O 4214 * request descriptors 4215 */ 4216 vd->inband_task.vd = vd; 4217 vd->inband_task.msg = kmem_alloc(vd->max_msglen, KM_SLEEP); 4218 vd->inband_task.index = 0; 4219 vd->inband_task.type = VD_FINAL_RANGE_TASK; /* range == 1 */ 4220 } 4221 4222 /* Return the device's block size and max transfer size to the client */ 4223 attr_msg->vdisk_block_size = vd->vdisk_block_size; 4224 attr_msg->max_xfer_sz = vd->max_xfer_sz; 4225 4226 attr_msg->vdisk_size = vd->vdisk_size; 4227 attr_msg->vdisk_type = (vd_slice_single_slice)? vd->vdisk_type : 4228 VD_DISK_TYPE_DISK; 4229 attr_msg->vdisk_media = vd->vdisk_media; 4230 4231 /* Discover and save the list of supported VD_OP_XXX operations */ 4232 vd_set_exported_operations(vd); 4233 attr_msg->operations = vd->operations; 4234 4235 PR0("%s", VD_CLIENT(vd)); 4236 4237 ASSERT(vd->dring_task == NULL); 4238 4239 return (0); 4240 } 4241 4242 static int 4243 vd_process_dring_reg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 4244 { 4245 int status; 4246 size_t expected; 4247 ldc_mem_info_t dring_minfo; 4248 uint8_t mtype; 4249 vio_dring_reg_msg_t *reg_msg = (vio_dring_reg_msg_t *)msg; 4250 4251 4252 ASSERT(msglen >= sizeof (msg->tag)); 4253 4254 if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 4255 VIO_DRING_REG)) { 4256 PR0("Message is not a register-dring message"); 4257 return (ENOMSG); 4258 } 4259 4260 if (msglen < sizeof (*reg_msg)) { 4261 PR0("Expected at least %lu-byte register-dring message; " 4262 "received %lu bytes", sizeof (*reg_msg), msglen); 4263 return (EBADMSG); 4264 } 4265 4266 expected = sizeof (*reg_msg) + 4267 (reg_msg->ncookies - 1)*(sizeof (reg_msg->cookie[0])); 4268 if (msglen != expected) { 4269 PR0("Expected %lu-byte register-dring message; " 4270 "received %lu bytes", expected, msglen); 4271 return (EBADMSG); 4272 } 4273 4274 if (vd->initialized & VD_DRING) { 4275 PR0("A dring was previously registered; only support one"); 4276 return (EBADMSG); 4277 } 4278 4279 if (reg_msg->num_descriptors > INT32_MAX) { 4280 PR0("reg_msg->num_descriptors = %u; must be <= %u (%s)", 4281 reg_msg->ncookies, INT32_MAX, STRINGIZE(INT32_MAX)); 4282 return (EBADMSG); 4283 } 4284 4285 if (reg_msg->ncookies != 1) { 4286 /* 4287 * In addition to fixing the assertion in the success case 4288 * below, supporting drings which require more than one 4289 * "cookie" requires increasing the value of vd->max_msglen 4290 * somewhere in the code path prior to receiving the message 4291 * which results in calling this function. Note that without 4292 * making this change, the larger message size required to 4293 * accommodate multiple cookies cannot be successfully 4294 * received, so this function will not even get called. 4295 * Gracefully accommodating more dring cookies might 4296 * reasonably demand exchanging an additional attribute or 4297 * making a minor protocol adjustment 4298 */ 4299 PR0("reg_msg->ncookies = %u != 1", reg_msg->ncookies); 4300 return (EBADMSG); 4301 } 4302 4303 if (vd_direct_mapped_drings) 4304 mtype = LDC_DIRECT_MAP; 4305 else 4306 mtype = LDC_SHADOW_MAP; 4307 4308 status = ldc_mem_dring_map(vd->ldc_handle, reg_msg->cookie, 4309 reg_msg->ncookies, reg_msg->num_descriptors, 4310 reg_msg->descriptor_size, mtype, &vd->dring_handle); 4311 if (status != 0) { 4312 PR0("ldc_mem_dring_map() returned errno %d", status); 4313 return (status); 4314 } 4315 4316 /* 4317 * To remove the need for this assertion, must call 4318 * ldc_mem_dring_nextcookie() successfully ncookies-1 times after a 4319 * successful call to ldc_mem_dring_map() 4320 */ 4321 ASSERT(reg_msg->ncookies == 1); 4322 4323 if ((status = 4324 ldc_mem_dring_info(vd->dring_handle, &dring_minfo)) != 0) { 4325 PR0("ldc_mem_dring_info() returned errno %d", status); 4326 if ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0) 4327 PR0("ldc_mem_dring_unmap() returned errno %d", status); 4328 return (status); 4329 } 4330 4331 if (dring_minfo.vaddr == NULL) { 4332 PR0("Descriptor ring virtual address is NULL"); 4333 return (ENXIO); 4334 } 4335 4336 4337 /* Initialize for valid message and mapped dring */ 4338 vd->initialized |= VD_DRING; 4339 vd->dring_ident = 1; /* "There Can Be Only One" */ 4340 vd->dring = dring_minfo.vaddr; 4341 vd->descriptor_size = reg_msg->descriptor_size; 4342 vd->dring_len = reg_msg->num_descriptors; 4343 vd->dring_mtype = dring_minfo.mtype; 4344 reg_msg->dring_ident = vd->dring_ident; 4345 PR1("descriptor size = %u, dring length = %u", 4346 vd->descriptor_size, vd->dring_len); 4347 4348 /* 4349 * Allocate and initialize a "shadow" array of data structures for 4350 * tasks to process I/O requests in dring elements 4351 */ 4352 vd->dring_task = 4353 kmem_zalloc((sizeof (*vd->dring_task)) * vd->dring_len, KM_SLEEP); 4354 for (int i = 0; i < vd->dring_len; i++) { 4355 vd->dring_task[i].vd = vd; 4356 vd->dring_task[i].index = i; 4357 4358 status = ldc_mem_alloc_handle(vd->ldc_handle, 4359 &(vd->dring_task[i].mhdl)); 4360 if (status) { 4361 PR0("ldc_mem_alloc_handle() returned err %d ", status); 4362 return (ENXIO); 4363 } 4364 4365 /* 4366 * The descriptor payload varies in length. Calculate its 4367 * size by subtracting the header size from the total 4368 * descriptor size. 4369 */ 4370 vd->dring_task[i].request = kmem_zalloc((vd->descriptor_size - 4371 sizeof (vio_dring_entry_hdr_t)), KM_SLEEP); 4372 vd->dring_task[i].msg = kmem_alloc(vd->max_msglen, KM_SLEEP); 4373 } 4374 4375 return (0); 4376 } 4377 4378 static int 4379 vd_process_dring_unreg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 4380 { 4381 vio_dring_unreg_msg_t *unreg_msg = (vio_dring_unreg_msg_t *)msg; 4382 4383 4384 ASSERT(msglen >= sizeof (msg->tag)); 4385 4386 if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 4387 VIO_DRING_UNREG)) { 4388 PR0("Message is not an unregister-dring message"); 4389 return (ENOMSG); 4390 } 4391 4392 if (msglen != sizeof (*unreg_msg)) { 4393 PR0("Expected %lu-byte unregister-dring message; " 4394 "received %lu bytes", sizeof (*unreg_msg), msglen); 4395 return (EBADMSG); 4396 } 4397 4398 if (unreg_msg->dring_ident != vd->dring_ident) { 4399 PR0("Expected dring ident %lu; received %lu", 4400 vd->dring_ident, unreg_msg->dring_ident); 4401 return (EBADMSG); 4402 } 4403 4404 return (0); 4405 } 4406 4407 static int 4408 process_rdx_msg(vio_msg_t *msg, size_t msglen) 4409 { 4410 ASSERT(msglen >= sizeof (msg->tag)); 4411 4412 if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX)) { 4413 PR0("Message is not an RDX message"); 4414 return (ENOMSG); 4415 } 4416 4417 if (msglen != sizeof (vio_rdx_msg_t)) { 4418 PR0("Expected %lu-byte RDX message; received %lu bytes", 4419 sizeof (vio_rdx_msg_t), msglen); 4420 return (EBADMSG); 4421 } 4422 4423 PR0("Valid RDX message"); 4424 return (0); 4425 } 4426 4427 static int 4428 vd_check_seq_num(vd_t *vd, uint64_t seq_num) 4429 { 4430 if ((vd->initialized & VD_SEQ_NUM) && (seq_num != vd->seq_num + 1)) { 4431 PR0("Received seq_num %lu; expected %lu", 4432 seq_num, (vd->seq_num + 1)); 4433 PR0("initiating soft reset"); 4434 vd_need_reset(vd, B_FALSE); 4435 return (1); 4436 } 4437 4438 vd->seq_num = seq_num; 4439 vd->initialized |= VD_SEQ_NUM; /* superfluous after first time... */ 4440 return (0); 4441 } 4442 4443 /* 4444 * Return the expected size of an inband-descriptor message with all the 4445 * cookies it claims to include 4446 */ 4447 static size_t 4448 expected_inband_size(vd_dring_inband_msg_t *msg) 4449 { 4450 return ((sizeof (*msg)) + 4451 (msg->payload.ncookies - 1)*(sizeof (msg->payload.cookie[0]))); 4452 } 4453 4454 /* 4455 * Process an in-band descriptor message: used with clients like OBP, with 4456 * which vds exchanges descriptors within VIO message payloads, rather than 4457 * operating on them within a descriptor ring 4458 */ 4459 static int 4460 vd_process_desc_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 4461 { 4462 size_t expected; 4463 vd_dring_inband_msg_t *desc_msg = (vd_dring_inband_msg_t *)msg; 4464 4465 4466 ASSERT(msglen >= sizeof (msg->tag)); 4467 4468 if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO, 4469 VIO_DESC_DATA)) { 4470 PR1("Message is not an in-band-descriptor message"); 4471 return (ENOMSG); 4472 } 4473 4474 if (msglen < sizeof (*desc_msg)) { 4475 PR0("Expected at least %lu-byte descriptor message; " 4476 "received %lu bytes", sizeof (*desc_msg), msglen); 4477 return (EBADMSG); 4478 } 4479 4480 if (msglen != (expected = expected_inband_size(desc_msg))) { 4481 PR0("Expected %lu-byte descriptor message; " 4482 "received %lu bytes", expected, msglen); 4483 return (EBADMSG); 4484 } 4485 4486 if (vd_check_seq_num(vd, desc_msg->hdr.seq_num) != 0) 4487 return (EBADMSG); 4488 4489 /* 4490 * Valid message: Set up the in-band descriptor task and process the 4491 * request. Arrange to acknowledge the client's message, unless an 4492 * error processing the descriptor task results in setting 4493 * VIO_SUBTYPE_NACK 4494 */ 4495 PR1("Valid in-band-descriptor message"); 4496 msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 4497 4498 ASSERT(vd->inband_task.msg != NULL); 4499 4500 bcopy(msg, vd->inband_task.msg, msglen); 4501 vd->inband_task.msglen = msglen; 4502 4503 /* 4504 * The task request is now the payload of the message 4505 * that was just copied into the body of the task. 4506 */ 4507 desc_msg = (vd_dring_inband_msg_t *)vd->inband_task.msg; 4508 vd->inband_task.request = &desc_msg->payload; 4509 4510 return (vd_process_task(&vd->inband_task)); 4511 } 4512 4513 static int 4514 vd_process_element(vd_t *vd, vd_task_type_t type, uint32_t idx, 4515 vio_msg_t *msg, size_t msglen) 4516 { 4517 int status; 4518 boolean_t ready; 4519 on_trap_data_t otd; 4520 vd_dring_entry_t *elem = VD_DRING_ELEM(idx); 4521 4522 /* Accept the updated dring element */ 4523 if ((status = VIO_DRING_ACQUIRE(&otd, vd->dring_mtype, 4524 vd->dring_handle, idx, idx)) != 0) { 4525 return (status); 4526 } 4527 ready = (elem->hdr.dstate == VIO_DESC_READY); 4528 if (ready) { 4529 elem->hdr.dstate = VIO_DESC_ACCEPTED; 4530 bcopy(&elem->payload, vd->dring_task[idx].request, 4531 (vd->descriptor_size - sizeof (vio_dring_entry_hdr_t))); 4532 } else { 4533 PR0("descriptor %u not ready", idx); 4534 VD_DUMP_DRING_ELEM(elem); 4535 } 4536 if ((status = VIO_DRING_RELEASE(vd->dring_mtype, 4537 vd->dring_handle, idx, idx)) != 0) { 4538 PR0("VIO_DRING_RELEASE() returned errno %d", status); 4539 return (status); 4540 } 4541 if (!ready) 4542 return (EBUSY); 4543 4544 4545 /* Initialize a task and process the accepted element */ 4546 PR1("Processing dring element %u", idx); 4547 vd->dring_task[idx].type = type; 4548 4549 /* duplicate msg buf for cookies etc. */ 4550 bcopy(msg, vd->dring_task[idx].msg, msglen); 4551 4552 vd->dring_task[idx].msglen = msglen; 4553 return (vd_process_task(&vd->dring_task[idx])); 4554 } 4555 4556 static int 4557 vd_process_element_range(vd_t *vd, int start, int end, 4558 vio_msg_t *msg, size_t msglen) 4559 { 4560 int i, n, nelem, status = 0; 4561 boolean_t inprogress = B_FALSE; 4562 vd_task_type_t type; 4563 4564 4565 ASSERT(start >= 0); 4566 ASSERT(end >= 0); 4567 4568 /* 4569 * Arrange to acknowledge the client's message, unless an error 4570 * processing one of the dring elements results in setting 4571 * VIO_SUBTYPE_NACK 4572 */ 4573 msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 4574 4575 /* 4576 * Process the dring elements in the range 4577 */ 4578 nelem = ((end < start) ? end + vd->dring_len : end) - start + 1; 4579 for (i = start, n = nelem; n > 0; i = (i + 1) % vd->dring_len, n--) { 4580 ((vio_dring_msg_t *)msg)->end_idx = i; 4581 type = (n == 1) ? VD_FINAL_RANGE_TASK : VD_NONFINAL_RANGE_TASK; 4582 status = vd_process_element(vd, type, i, msg, msglen); 4583 if (status == EINPROGRESS) 4584 inprogress = B_TRUE; 4585 else if (status != 0) 4586 break; 4587 } 4588 4589 /* 4590 * If some, but not all, operations of a multi-element range are in 4591 * progress, wait for other operations to complete before returning 4592 * (which will result in "ack" or "nack" of the message). Note that 4593 * all outstanding operations will need to complete, not just the ones 4594 * corresponding to the current range of dring elements; howevever, as 4595 * this situation is an error case, performance is less critical. 4596 */ 4597 if ((nelem > 1) && (status != EINPROGRESS) && inprogress) 4598 ddi_taskq_wait(vd->completionq); 4599 4600 return (status); 4601 } 4602 4603 static int 4604 vd_process_dring_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 4605 { 4606 vio_dring_msg_t *dring_msg = (vio_dring_msg_t *)msg; 4607 4608 4609 ASSERT(msglen >= sizeof (msg->tag)); 4610 4611 if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO, 4612 VIO_DRING_DATA)) { 4613 PR1("Message is not a dring-data message"); 4614 return (ENOMSG); 4615 } 4616 4617 if (msglen != sizeof (*dring_msg)) { 4618 PR0("Expected %lu-byte dring message; received %lu bytes", 4619 sizeof (*dring_msg), msglen); 4620 return (EBADMSG); 4621 } 4622 4623 if (vd_check_seq_num(vd, dring_msg->seq_num) != 0) 4624 return (EBADMSG); 4625 4626 if (dring_msg->dring_ident != vd->dring_ident) { 4627 PR0("Expected dring ident %lu; received ident %lu", 4628 vd->dring_ident, dring_msg->dring_ident); 4629 return (EBADMSG); 4630 } 4631 4632 if (dring_msg->start_idx >= vd->dring_len) { 4633 PR0("\"start_idx\" = %u; must be less than %u", 4634 dring_msg->start_idx, vd->dring_len); 4635 return (EBADMSG); 4636 } 4637 4638 if ((dring_msg->end_idx < 0) || 4639 (dring_msg->end_idx >= vd->dring_len)) { 4640 PR0("\"end_idx\" = %u; must be >= 0 and less than %u", 4641 dring_msg->end_idx, vd->dring_len); 4642 return (EBADMSG); 4643 } 4644 4645 /* Valid message; process range of updated dring elements */ 4646 PR1("Processing descriptor range, start = %u, end = %u", 4647 dring_msg->start_idx, dring_msg->end_idx); 4648 return (vd_process_element_range(vd, dring_msg->start_idx, 4649 dring_msg->end_idx, msg, msglen)); 4650 } 4651 4652 static int 4653 recv_msg(ldc_handle_t ldc_handle, void *msg, size_t *nbytes) 4654 { 4655 int retry, status; 4656 size_t size = *nbytes; 4657 4658 4659 for (retry = 0, status = ETIMEDOUT; 4660 retry < vds_ldc_retries && status == ETIMEDOUT; 4661 retry++) { 4662 PR1("ldc_read() attempt %d", (retry + 1)); 4663 *nbytes = size; 4664 status = ldc_read(ldc_handle, msg, nbytes); 4665 } 4666 4667 if (status) { 4668 PR0("ldc_read() returned errno %d", status); 4669 if (status != ECONNRESET) 4670 return (ENOMSG); 4671 return (status); 4672 } else if (*nbytes == 0) { 4673 PR1("ldc_read() returned 0 and no message read"); 4674 return (ENOMSG); 4675 } 4676 4677 PR1("RCVD %lu-byte message", *nbytes); 4678 return (0); 4679 } 4680 4681 static int 4682 vd_do_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 4683 { 4684 int status; 4685 4686 4687 PR1("Processing (%x/%x/%x) message", msg->tag.vio_msgtype, 4688 msg->tag.vio_subtype, msg->tag.vio_subtype_env); 4689 #ifdef DEBUG 4690 vd_decode_tag(msg); 4691 #endif 4692 4693 /* 4694 * Validate session ID up front, since it applies to all messages 4695 * once set 4696 */ 4697 if ((msg->tag.vio_sid != vd->sid) && (vd->initialized & VD_SID)) { 4698 PR0("Expected SID %u, received %u", vd->sid, 4699 msg->tag.vio_sid); 4700 return (EBADMSG); 4701 } 4702 4703 PR1("\tWhile in state %d (%s)", vd->state, vd_decode_state(vd->state)); 4704 4705 /* 4706 * Process the received message based on connection state 4707 */ 4708 switch (vd->state) { 4709 case VD_STATE_INIT: /* expect version message */ 4710 if ((status = vd_process_ver_msg(vd, msg, msglen)) != 0) 4711 return (status); 4712 4713 /* Version negotiated, move to that state */ 4714 vd->state = VD_STATE_VER; 4715 return (0); 4716 4717 case VD_STATE_VER: /* expect attribute message */ 4718 if ((status = vd_process_attr_msg(vd, msg, msglen)) != 0) 4719 return (status); 4720 4721 /* Attributes exchanged, move to that state */ 4722 vd->state = VD_STATE_ATTR; 4723 return (0); 4724 4725 case VD_STATE_ATTR: 4726 switch (vd->xfer_mode) { 4727 case VIO_DESC_MODE: /* expect RDX message */ 4728 if ((status = process_rdx_msg(msg, msglen)) != 0) 4729 return (status); 4730 4731 /* Ready to receive in-band descriptors */ 4732 vd->state = VD_STATE_DATA; 4733 return (0); 4734 4735 case VIO_DRING_MODE_V1_0: /* expect register-dring message */ 4736 if ((status = 4737 vd_process_dring_reg_msg(vd, msg, msglen)) != 0) 4738 return (status); 4739 4740 /* One dring negotiated, move to that state */ 4741 vd->state = VD_STATE_DRING; 4742 return (0); 4743 4744 default: 4745 ASSERT("Unsupported transfer mode"); 4746 PR0("Unsupported transfer mode"); 4747 return (ENOTSUP); 4748 } 4749 4750 case VD_STATE_DRING: /* expect RDX, register-dring, or unreg-dring */ 4751 if ((status = process_rdx_msg(msg, msglen)) == 0) { 4752 /* Ready to receive data */ 4753 vd->state = VD_STATE_DATA; 4754 return (0); 4755 } else if (status != ENOMSG) { 4756 return (status); 4757 } 4758 4759 4760 /* 4761 * If another register-dring message is received, stay in 4762 * dring state in case the client sends RDX; although the 4763 * protocol allows multiple drings, this server does not 4764 * support using more than one 4765 */ 4766 if ((status = 4767 vd_process_dring_reg_msg(vd, msg, msglen)) != ENOMSG) 4768 return (status); 4769 4770 /* 4771 * Acknowledge an unregister-dring message, but reset the 4772 * connection anyway: Although the protocol allows 4773 * unregistering drings, this server cannot serve a vdisk 4774 * without its only dring 4775 */ 4776 status = vd_process_dring_unreg_msg(vd, msg, msglen); 4777 return ((status == 0) ? ENOTSUP : status); 4778 4779 case VD_STATE_DATA: 4780 switch (vd->xfer_mode) { 4781 case VIO_DESC_MODE: /* expect in-band-descriptor message */ 4782 return (vd_process_desc_msg(vd, msg, msglen)); 4783 4784 case VIO_DRING_MODE_V1_0: /* expect dring-data or unreg-dring */ 4785 /* 4786 * Typically expect dring-data messages, so handle 4787 * them first 4788 */ 4789 if ((status = vd_process_dring_msg(vd, msg, 4790 msglen)) != ENOMSG) 4791 return (status); 4792 4793 /* 4794 * Acknowledge an unregister-dring message, but reset 4795 * the connection anyway: Although the protocol 4796 * allows unregistering drings, this server cannot 4797 * serve a vdisk without its only dring 4798 */ 4799 status = vd_process_dring_unreg_msg(vd, msg, msglen); 4800 return ((status == 0) ? ENOTSUP : status); 4801 4802 default: 4803 ASSERT("Unsupported transfer mode"); 4804 PR0("Unsupported transfer mode"); 4805 return (ENOTSUP); 4806 } 4807 4808 default: 4809 ASSERT("Invalid client connection state"); 4810 PR0("Invalid client connection state"); 4811 return (ENOTSUP); 4812 } 4813 } 4814 4815 static int 4816 vd_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 4817 { 4818 int status; 4819 boolean_t reset_ldc = B_FALSE; 4820 vd_task_t task; 4821 4822 /* 4823 * Check that the message is at least big enough for a "tag", so that 4824 * message processing can proceed based on tag-specified message type 4825 */ 4826 if (msglen < sizeof (vio_msg_tag_t)) { 4827 PR0("Received short (%lu-byte) message", msglen); 4828 /* Can't "nack" short message, so drop the big hammer */ 4829 PR0("initiating full reset"); 4830 vd_need_reset(vd, B_TRUE); 4831 return (EBADMSG); 4832 } 4833 4834 /* 4835 * Process the message 4836 */ 4837 switch (status = vd_do_process_msg(vd, msg, msglen)) { 4838 case 0: 4839 /* "ack" valid, successfully-processed messages */ 4840 msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 4841 break; 4842 4843 case EINPROGRESS: 4844 /* The completion handler will "ack" or "nack" the message */ 4845 return (EINPROGRESS); 4846 case ENOMSG: 4847 PR0("Received unexpected message"); 4848 _NOTE(FALLTHROUGH); 4849 case EBADMSG: 4850 case ENOTSUP: 4851 /* "transport" error will cause NACK of invalid messages */ 4852 msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 4853 break; 4854 4855 default: 4856 /* "transport" error will cause NACK of invalid messages */ 4857 msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 4858 /* An LDC error probably occurred, so try resetting it */ 4859 reset_ldc = B_TRUE; 4860 break; 4861 } 4862 4863 PR1("\tResulting in state %d (%s)", vd->state, 4864 vd_decode_state(vd->state)); 4865 4866 /* populate the task so we can dispatch it on the taskq */ 4867 task.vd = vd; 4868 task.msg = msg; 4869 task.msglen = msglen; 4870 4871 /* 4872 * Queue a task to send the notification that the operation completed. 4873 * We need to ensure that requests are responded to in the correct 4874 * order and since the taskq is processed serially this ordering 4875 * is maintained. 4876 */ 4877 (void) ddi_taskq_dispatch(vd->completionq, vd_serial_notify, 4878 &task, DDI_SLEEP); 4879 4880 /* 4881 * To ensure handshake negotiations do not happen out of order, such 4882 * requests that come through this path should not be done in parallel 4883 * so we need to wait here until the response is sent to the client. 4884 */ 4885 ddi_taskq_wait(vd->completionq); 4886 4887 /* Arrange to reset the connection for nack'ed or failed messages */ 4888 if ((status != 0) || reset_ldc) { 4889 PR0("initiating %s reset", 4890 (reset_ldc) ? "full" : "soft"); 4891 vd_need_reset(vd, reset_ldc); 4892 } 4893 4894 return (status); 4895 } 4896 4897 static boolean_t 4898 vd_enabled(vd_t *vd) 4899 { 4900 boolean_t enabled; 4901 4902 mutex_enter(&vd->lock); 4903 enabled = vd->enabled; 4904 mutex_exit(&vd->lock); 4905 return (enabled); 4906 } 4907 4908 static void 4909 vd_recv_msg(void *arg) 4910 { 4911 vd_t *vd = (vd_t *)arg; 4912 int rv = 0, status = 0; 4913 4914 ASSERT(vd != NULL); 4915 4916 PR2("New task to receive incoming message(s)"); 4917 4918 4919 while (vd_enabled(vd) && status == 0) { 4920 size_t msglen, msgsize; 4921 ldc_status_t lstatus; 4922 4923 /* 4924 * Receive and process a message 4925 */ 4926 vd_reset_if_needed(vd); /* can change vd->max_msglen */ 4927 4928 /* 4929 * check if channel is UP - else break out of loop 4930 */ 4931 status = ldc_status(vd->ldc_handle, &lstatus); 4932 if (lstatus != LDC_UP) { 4933 PR0("channel not up (status=%d), exiting recv loop\n", 4934 lstatus); 4935 break; 4936 } 4937 4938 ASSERT(vd->max_msglen != 0); 4939 4940 msgsize = vd->max_msglen; /* stable copy for alloc/free */ 4941 msglen = msgsize; /* actual len after recv_msg() */ 4942 4943 status = recv_msg(vd->ldc_handle, vd->vio_msgp, &msglen); 4944 switch (status) { 4945 case 0: 4946 rv = vd_process_msg(vd, (void *)vd->vio_msgp, msglen); 4947 /* check if max_msglen changed */ 4948 if (msgsize != vd->max_msglen) { 4949 PR0("max_msglen changed 0x%lx to 0x%lx bytes\n", 4950 msgsize, vd->max_msglen); 4951 kmem_free(vd->vio_msgp, msgsize); 4952 vd->vio_msgp = 4953 kmem_alloc(vd->max_msglen, KM_SLEEP); 4954 } 4955 if (rv == EINPROGRESS) 4956 continue; 4957 break; 4958 4959 case ENOMSG: 4960 break; 4961 4962 case ECONNRESET: 4963 PR0("initiating soft reset (ECONNRESET)\n"); 4964 vd_need_reset(vd, B_FALSE); 4965 status = 0; 4966 break; 4967 4968 default: 4969 /* Probably an LDC failure; arrange to reset it */ 4970 PR0("initiating full reset (status=0x%x)", status); 4971 vd_need_reset(vd, B_TRUE); 4972 break; 4973 } 4974 } 4975 4976 PR2("Task finished"); 4977 } 4978 4979 static uint_t 4980 vd_handle_ldc_events(uint64_t event, caddr_t arg) 4981 { 4982 vd_t *vd = (vd_t *)(void *)arg; 4983 int status; 4984 4985 ASSERT(vd != NULL); 4986 4987 if (!vd_enabled(vd)) 4988 return (LDC_SUCCESS); 4989 4990 if (event & LDC_EVT_DOWN) { 4991 PR0("LDC_EVT_DOWN: LDC channel went down"); 4992 4993 vd_need_reset(vd, B_TRUE); 4994 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, 4995 DDI_SLEEP); 4996 if (status == DDI_FAILURE) { 4997 PR0("cannot schedule task to recv msg\n"); 4998 vd_need_reset(vd, B_TRUE); 4999 } 5000 } 5001 5002 if (event & LDC_EVT_RESET) { 5003 PR0("LDC_EVT_RESET: LDC channel was reset"); 5004 5005 if (vd->state != VD_STATE_INIT) { 5006 PR0("scheduling full reset"); 5007 vd_need_reset(vd, B_FALSE); 5008 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, 5009 vd, DDI_SLEEP); 5010 if (status == DDI_FAILURE) { 5011 PR0("cannot schedule task to recv msg\n"); 5012 vd_need_reset(vd, B_TRUE); 5013 } 5014 5015 } else { 5016 PR0("channel already reset, ignoring...\n"); 5017 PR0("doing ldc up...\n"); 5018 (void) ldc_up(vd->ldc_handle); 5019 } 5020 5021 return (LDC_SUCCESS); 5022 } 5023 5024 if (event & LDC_EVT_UP) { 5025 PR0("EVT_UP: LDC is up\nResetting client connection state"); 5026 PR0("initiating soft reset"); 5027 vd_need_reset(vd, B_FALSE); 5028 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, 5029 vd, DDI_SLEEP); 5030 if (status == DDI_FAILURE) { 5031 PR0("cannot schedule task to recv msg\n"); 5032 vd_need_reset(vd, B_TRUE); 5033 return (LDC_SUCCESS); 5034 } 5035 } 5036 5037 if (event & LDC_EVT_READ) { 5038 int status; 5039 5040 PR1("New data available"); 5041 /* Queue a task to receive the new data */ 5042 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, 5043 DDI_SLEEP); 5044 5045 if (status == DDI_FAILURE) { 5046 PR0("cannot schedule task to recv msg\n"); 5047 vd_need_reset(vd, B_TRUE); 5048 } 5049 } 5050 5051 return (LDC_SUCCESS); 5052 } 5053 5054 static uint_t 5055 vds_check_for_vd(mod_hash_key_t key, mod_hash_val_t *val, void *arg) 5056 { 5057 _NOTE(ARGUNUSED(key, val)) 5058 (*((uint_t *)arg))++; 5059 return (MH_WALK_TERMINATE); 5060 } 5061 5062 5063 static int 5064 vds_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 5065 { 5066 uint_t vd_present = 0; 5067 minor_t instance; 5068 vds_t *vds; 5069 5070 5071 switch (cmd) { 5072 case DDI_DETACH: 5073 /* the real work happens below */ 5074 break; 5075 case DDI_SUSPEND: 5076 PR0("No action required for DDI_SUSPEND"); 5077 return (DDI_SUCCESS); 5078 default: 5079 PR0("Unrecognized \"cmd\""); 5080 return (DDI_FAILURE); 5081 } 5082 5083 ASSERT(cmd == DDI_DETACH); 5084 instance = ddi_get_instance(dip); 5085 if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) { 5086 PR0("Could not get state for instance %u", instance); 5087 ddi_soft_state_free(vds_state, instance); 5088 return (DDI_FAILURE); 5089 } 5090 5091 /* Do no detach when serving any vdisks */ 5092 mod_hash_walk(vds->vd_table, vds_check_for_vd, &vd_present); 5093 if (vd_present) { 5094 PR0("Not detaching because serving vdisks"); 5095 return (DDI_FAILURE); 5096 } 5097 5098 PR0("Detaching"); 5099 if (vds->initialized & VDS_MDEG) { 5100 (void) mdeg_unregister(vds->mdeg); 5101 kmem_free(vds->ispecp->specp, sizeof (vds_prop_template)); 5102 kmem_free(vds->ispecp, sizeof (mdeg_node_spec_t)); 5103 vds->ispecp = NULL; 5104 vds->mdeg = NULL; 5105 } 5106 5107 vds_driver_types_free(vds); 5108 5109 if (vds->initialized & VDS_LDI) 5110 (void) ldi_ident_release(vds->ldi_ident); 5111 mod_hash_destroy_hash(vds->vd_table); 5112 ddi_soft_state_free(vds_state, instance); 5113 return (DDI_SUCCESS); 5114 } 5115 5116 /* 5117 * Description: 5118 * This function checks to see if the file being used as a 5119 * virtual disk is an ISO image. An ISO image is a special 5120 * case which can be booted/installed from like a CD/DVD 5121 * 5122 * Parameters: 5123 * vd - disk on which the operation is performed. 5124 * 5125 * Return Code: 5126 * B_TRUE - The file is an ISO 9660 compliant image 5127 * B_FALSE - just a regular disk image file 5128 */ 5129 static boolean_t 5130 vd_file_is_iso_image(vd_t *vd) 5131 { 5132 char iso_buf[ISO_SECTOR_SIZE]; 5133 int i, rv; 5134 uint_t sec; 5135 5136 ASSERT(vd->file); 5137 5138 /* 5139 * If we have already discovered and saved this info we can 5140 * short-circuit the check and avoid reading the file. 5141 */ 5142 if (vd->vdisk_media == VD_MEDIA_DVD || vd->vdisk_media == VD_MEDIA_CD) 5143 return (B_TRUE); 5144 5145 /* 5146 * We wish to read the sector that should contain the 2nd ISO volume 5147 * descriptor. The second field in this descriptor is called the 5148 * Standard Identifier and is set to CD001 for a CD-ROM compliant 5149 * to the ISO 9660 standard. 5150 */ 5151 sec = (ISO_VOLDESC_SEC * ISO_SECTOR_SIZE) / vd->vdisk_block_size; 5152 rv = vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)iso_buf, 5153 sec, ISO_SECTOR_SIZE); 5154 5155 if (rv < 0) 5156 return (B_FALSE); 5157 5158 for (i = 0; i < ISO_ID_STRLEN; i++) { 5159 if (ISO_STD_ID(iso_buf)[i] != ISO_ID_STRING[i]) 5160 return (B_FALSE); 5161 } 5162 5163 return (B_TRUE); 5164 } 5165 5166 /* 5167 * Description: 5168 * This function checks to see if the virtual device is an ATAPI 5169 * device. ATAPI devices use Group 1 Read/Write commands, so 5170 * any USCSI calls vds makes need to take this into account. 5171 * 5172 * Parameters: 5173 * vd - disk on which the operation is performed. 5174 * 5175 * Return Code: 5176 * B_TRUE - The virtual disk is backed by an ATAPI device 5177 * B_FALSE - not an ATAPI device (presumably SCSI) 5178 */ 5179 static boolean_t 5180 vd_is_atapi_device(vd_t *vd) 5181 { 5182 boolean_t is_atapi = B_FALSE; 5183 char *variantp; 5184 int rv; 5185 5186 ASSERT(vd->ldi_handle[0] != NULL); 5187 ASSERT(!vd->file); 5188 5189 rv = ldi_prop_lookup_string(vd->ldi_handle[0], 5190 (LDI_DEV_T_ANY | DDI_PROP_DONTPASS), "variant", &variantp); 5191 if (rv == DDI_PROP_SUCCESS) { 5192 PR0("'variant' property exists for %s", vd->device_path); 5193 if (strcmp(variantp, "atapi") == 0) 5194 is_atapi = B_TRUE; 5195 ddi_prop_free(variantp); 5196 } 5197 5198 rv = ldi_prop_exists(vd->ldi_handle[0], LDI_DEV_T_ANY, "atapi"); 5199 if (rv) { 5200 PR0("'atapi' property exists for %s", vd->device_path); 5201 is_atapi = B_TRUE; 5202 } 5203 5204 return (is_atapi); 5205 } 5206 5207 static int 5208 vd_setup_full_disk(vd_t *vd) 5209 { 5210 int status; 5211 major_t major = getmajor(vd->dev[0]); 5212 minor_t minor = getminor(vd->dev[0]) - VD_ENTIRE_DISK_SLICE; 5213 5214 ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK); 5215 5216 vd->vdisk_block_size = DEV_BSIZE; 5217 5218 /* set the disk size, block size and the media type of the disk */ 5219 status = vd_backend_check_size(vd); 5220 5221 if (status != 0) { 5222 if (!vd->scsi) { 5223 /* unexpected failure */ 5224 PRN("ldi_ioctl(DKIOCGMEDIAINFO) returned errno %d", 5225 status); 5226 return (status); 5227 } 5228 5229 /* 5230 * The function can fail for SCSI disks which are present but 5231 * reserved by another system. In that case, we don't know the 5232 * size of the disk and the block size. 5233 */ 5234 vd->vdisk_size = VD_SIZE_UNKNOWN; 5235 vd->block_size = 0; 5236 vd->vdisk_media = VD_MEDIA_FIXED; 5237 } 5238 5239 /* Move dev number and LDI handle to entire-disk-slice array elements */ 5240 vd->dev[VD_ENTIRE_DISK_SLICE] = vd->dev[0]; 5241 vd->dev[0] = 0; 5242 vd->ldi_handle[VD_ENTIRE_DISK_SLICE] = vd->ldi_handle[0]; 5243 vd->ldi_handle[0] = NULL; 5244 5245 /* Initialize device numbers for remaining slices and open them */ 5246 for (int slice = 0; slice < vd->nslices; slice++) { 5247 /* 5248 * Skip the entire-disk slice, as it's already open and its 5249 * device known 5250 */ 5251 if (slice == VD_ENTIRE_DISK_SLICE) 5252 continue; 5253 ASSERT(vd->dev[slice] == 0); 5254 ASSERT(vd->ldi_handle[slice] == NULL); 5255 5256 /* 5257 * Construct the device number for the current slice 5258 */ 5259 vd->dev[slice] = makedevice(major, (minor + slice)); 5260 5261 /* 5262 * Open all slices of the disk to serve them to the client. 5263 * Slices are opened exclusively to prevent other threads or 5264 * processes in the service domain from performing I/O to 5265 * slices being accessed by a client. Failure to open a slice 5266 * results in vds not serving this disk, as the client could 5267 * attempt (and should be able) to access any slice immediately. 5268 * Any slices successfully opened before a failure will get 5269 * closed by vds_destroy_vd() as a result of the error returned 5270 * by this function. 5271 * 5272 * We need to do the open with FNDELAY so that opening an empty 5273 * slice does not fail. 5274 */ 5275 PR0("Opening device major %u, minor %u = slice %u", 5276 major, minor, slice); 5277 5278 /* 5279 * Try to open the device. This can fail for example if we are 5280 * opening an empty slice. So in case of a failure, we try the 5281 * open again but this time with the FNDELAY flag. 5282 */ 5283 status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK, 5284 vd->open_flags, kcred, &vd->ldi_handle[slice], 5285 vd->vds->ldi_ident); 5286 5287 if (status != 0) { 5288 status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK, 5289 vd->open_flags | FNDELAY, kcred, 5290 &vd->ldi_handle[slice], vd->vds->ldi_ident); 5291 } 5292 5293 if (status != 0) { 5294 PRN("ldi_open_by_dev() returned errno %d " 5295 "for slice %u", status, slice); 5296 /* vds_destroy_vd() will close any open slices */ 5297 vd->ldi_handle[slice] = NULL; 5298 return (status); 5299 } 5300 } 5301 5302 return (0); 5303 } 5304 5305 /* 5306 * When a slice or a volume is exported as a single-slice disk, we want 5307 * the disk backend (i.e. the slice or volume) to be entirely mapped as 5308 * a slice without the addition of any metadata. 5309 * 5310 * So when exporting the disk as a VTOC disk, we fake a disk with the following 5311 * layout: 5312 * flabel +--- flabel_limit 5313 * <-> V 5314 * 0 1 C D E 5315 * +-+---+--------------------------+--+ 5316 * virtual disk: |L|XXX| slice 0 |AA| 5317 * +-+---+--------------------------+--+ 5318 * ^ : : 5319 * | : : 5320 * VTOC LABEL--+ : : 5321 * +--------------------------+ 5322 * disk backend: | slice/volume/file | 5323 * +--------------------------+ 5324 * 0 N 5325 * 5326 * N is the number of blocks in the slice/volume/file. 5327 * 5328 * We simulate a disk with N+M blocks, where M is the number of blocks 5329 * simluated at the beginning and at the end of the disk (blocks 0-C 5330 * and D-E). 5331 * 5332 * The first blocks (0 to C-1) are emulated and can not be changed. Blocks C 5333 * to D defines slice 0 and are mapped to the backend. Finally we emulate 2 5334 * alternate cylinders at the end of the disk (blocks D-E). In summary we have: 5335 * 5336 * - block 0 (L) returns a fake VTOC label 5337 * - blocks 1 to C-1 (X) are unused and return 0 5338 * - blocks C to D-1 are mapped to the exported slice or volume 5339 * - blocks D and E (A) are blocks defining alternate cylinders (2 cylinders) 5340 * 5341 * Note: because we define a fake disk geometry, it is possible that the length 5342 * of the backend is not a multiple of the size of cylinder, in that case the 5343 * very end of the backend will not map to any block of the virtual disk. 5344 */ 5345 static int 5346 vd_setup_partition_vtoc(vd_t *vd) 5347 { 5348 char *device_path = vd->device_path; 5349 char unit; 5350 size_t size, csize; 5351 5352 /* Initialize dk_geom structure for single-slice device */ 5353 if (vd->dk_geom.dkg_nsect == 0) { 5354 PRN("%s geometry claims 0 sectors per track", device_path); 5355 return (EIO); 5356 } 5357 if (vd->dk_geom.dkg_nhead == 0) { 5358 PRN("%s geometry claims 0 heads", device_path); 5359 return (EIO); 5360 } 5361 5362 /* size of a cylinder in block */ 5363 csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect; 5364 5365 /* 5366 * Add extra cylinders: we emulate the first cylinder (which contains 5367 * the disk label). 5368 */ 5369 vd->dk_geom.dkg_ncyl = vd->vdisk_size / csize + 1; 5370 5371 /* we emulate 2 alternate cylinders */ 5372 vd->dk_geom.dkg_acyl = 2; 5373 vd->dk_geom.dkg_pcyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl; 5374 5375 5376 /* Initialize vtoc structure for single-slice device */ 5377 bzero(vd->vtoc.v_part, sizeof (vd->vtoc.v_part)); 5378 vd->vtoc.v_part[0].p_tag = V_UNASSIGNED; 5379 vd->vtoc.v_part[0].p_flag = 0; 5380 /* 5381 * Partition 0 starts on cylinder 1 and its size has to be 5382 * a multiple of a number of cylinder. 5383 */ 5384 vd->vtoc.v_part[0].p_start = csize; /* start on cylinder 1 */ 5385 vd->vtoc.v_part[0].p_size = (vd->vdisk_size / csize) * csize; 5386 5387 if (vd_slice_single_slice) { 5388 vd->vtoc.v_nparts = 1; 5389 bcopy(VD_ASCIILABEL, vd->vtoc.v_asciilabel, 5390 MIN(sizeof (VD_ASCIILABEL), 5391 sizeof (vd->vtoc.v_asciilabel))); 5392 bcopy(VD_VOLUME_NAME, vd->vtoc.v_volume, 5393 MIN(sizeof (VD_VOLUME_NAME), sizeof (vd->vtoc.v_volume))); 5394 } else { 5395 /* adjust the number of slices */ 5396 vd->nslices = V_NUMPAR; 5397 vd->vtoc.v_nparts = V_NUMPAR; 5398 5399 /* define slice 2 representing the entire disk */ 5400 vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_tag = V_BACKUP; 5401 vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_flag = 0; 5402 vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_start = 0; 5403 vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_size = 5404 vd->dk_geom.dkg_ncyl * csize; 5405 5406 vd_get_readable_size(vd->vdisk_size * vd->vdisk_block_size, 5407 &size, &unit); 5408 5409 /* 5410 * Set some attributes of the geometry to what format(1m) uses 5411 * so that writing a default label using format(1m) does not 5412 * produce any error. 5413 */ 5414 vd->dk_geom.dkg_bcyl = 0; 5415 vd->dk_geom.dkg_intrlv = 1; 5416 vd->dk_geom.dkg_write_reinstruct = 0; 5417 vd->dk_geom.dkg_read_reinstruct = 0; 5418 5419 /* 5420 * We must have a correct label name otherwise format(1m) will 5421 * not recognized the disk as labeled. 5422 */ 5423 (void) snprintf(vd->vtoc.v_asciilabel, LEN_DKL_ASCII, 5424 "SUN-DiskSlice-%ld%cB cyl %d alt %d hd %d sec %d", 5425 size, unit, 5426 vd->dk_geom.dkg_ncyl, vd->dk_geom.dkg_acyl, 5427 vd->dk_geom.dkg_nhead, vd->dk_geom.dkg_nsect); 5428 bzero(vd->vtoc.v_volume, sizeof (vd->vtoc.v_volume)); 5429 5430 /* create a fake label from the vtoc and geometry */ 5431 vd->flabel_limit = (uint_t)csize; 5432 vd->flabel_size = VD_LABEL_VTOC_SIZE; 5433 vd->flabel = kmem_zalloc(vd->flabel_size, KM_SLEEP); 5434 vd_vtocgeom_to_label(&vd->vtoc, &vd->dk_geom, 5435 VD_LABEL_VTOC(vd)); 5436 } 5437 5438 /* adjust the vdisk_size, we emulate 3 cylinders */ 5439 vd->vdisk_size += csize * 3; 5440 5441 return (0); 5442 } 5443 5444 /* 5445 * When a slice, volume or file is exported as a single-slice disk, we want 5446 * the disk backend (i.e. the slice, volume or file) to be entirely mapped 5447 * as a slice without the addition of any metadata. 5448 * 5449 * So when exporting the disk as an EFI disk, we fake a disk with the following 5450 * layout: 5451 * 5452 * flabel +--- flabel_limit 5453 * <------> v 5454 * 0 1 2 L 34 34+N P 5455 * +-+-+--+-------+--------------------------+-------+ 5456 * virtual disk: |X|T|EE|XXXXXXX| slice 0 |RRRRRRR| 5457 * +-+-+--+-------+--------------------------+-------+ 5458 * ^ ^ : : 5459 * | | : : 5460 * GPT-+ +-GPE : : 5461 * +--------------------------+ 5462 * disk backend: | slice/volume/file | 5463 * +--------------------------+ 5464 * 0 N 5465 * 5466 * N is the number of blocks in the slice/volume/file. 5467 * 5468 * We simulate a disk with N+M blocks, where M is the number of blocks 5469 * simluated at the beginning and at the end of the disk (blocks 0-34 5470 * and 34+N-P). 5471 * 5472 * The first 34 blocks (0 to 33) are emulated and can not be changed. Blocks 34 5473 * to 34+N defines slice 0 and are mapped to the exported backend, and we 5474 * emulate some blocks at the end of the disk (blocks 34+N to P) as a the EFI 5475 * reserved partition. 5476 * 5477 * - block 0 (X) is unused and return 0 5478 * - block 1 (T) returns a fake EFI GPT (via DKIOCGETEFI) 5479 * - blocks 2 to L-1 (E) defines a fake EFI GPE (via DKIOCGETEFI) 5480 * - blocks L to 33 (X) are unused and return 0 5481 * - blocks 34 to 34+N are mapped to the exported slice, volume or file 5482 * - blocks 34+N+1 to P define a fake reserved partition and backup label, it 5483 * returns 0 5484 * 5485 * Note: if the backend size is not a multiple of the vdisk block size 5486 * (DEV_BSIZE = 512 byte) then the very end of the backend will not map to 5487 * any block of the virtual disk. 5488 */ 5489 static int 5490 vd_setup_partition_efi(vd_t *vd) 5491 { 5492 efi_gpt_t *gpt; 5493 efi_gpe_t *gpe; 5494 struct uuid uuid = EFI_USR; 5495 struct uuid efi_reserved = EFI_RESERVED; 5496 uint32_t crc; 5497 uint64_t s0_start, s0_end; 5498 5499 vd->flabel_limit = 34; 5500 vd->flabel_size = VD_LABEL_EFI_SIZE; 5501 vd->flabel = kmem_zalloc(vd->flabel_size, KM_SLEEP); 5502 gpt = VD_LABEL_EFI_GPT(vd); 5503 gpe = VD_LABEL_EFI_GPE(vd); 5504 5505 /* adjust the vdisk_size, we emulate the first 34 blocks */ 5506 vd->vdisk_size += 34; 5507 s0_start = 34; 5508 s0_end = vd->vdisk_size - 1; 5509 5510 gpt->efi_gpt_Signature = LE_64(EFI_SIGNATURE); 5511 gpt->efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 5512 gpt->efi_gpt_HeaderSize = LE_32(sizeof (efi_gpt_t)); 5513 gpt->efi_gpt_FirstUsableLBA = LE_64(34ULL); 5514 gpt->efi_gpt_PartitionEntryLBA = LE_64(2ULL); 5515 gpt->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (efi_gpe_t)); 5516 5517 UUID_LE_CONVERT(gpe[0].efi_gpe_PartitionTypeGUID, uuid); 5518 gpe[0].efi_gpe_StartingLBA = LE_64(s0_start); 5519 gpe[0].efi_gpe_EndingLBA = LE_64(s0_end); 5520 5521 if (vd_slice_single_slice) { 5522 gpt->efi_gpt_NumberOfPartitionEntries = LE_32(1); 5523 } else { 5524 /* adjust the number of slices */ 5525 gpt->efi_gpt_NumberOfPartitionEntries = LE_32(VD_MAXPART); 5526 vd->nslices = V_NUMPAR; 5527 5528 /* define a fake reserved partition */ 5529 UUID_LE_CONVERT(gpe[VD_MAXPART - 1].efi_gpe_PartitionTypeGUID, 5530 efi_reserved); 5531 gpe[VD_MAXPART - 1].efi_gpe_StartingLBA = 5532 LE_64(s0_end + 1); 5533 gpe[VD_MAXPART - 1].efi_gpe_EndingLBA = 5534 LE_64(s0_end + EFI_MIN_RESV_SIZE); 5535 5536 /* adjust the vdisk_size to include the reserved slice */ 5537 vd->vdisk_size += EFI_MIN_RESV_SIZE; 5538 } 5539 5540 gpt->efi_gpt_LastUsableLBA = LE_64(vd->vdisk_size - 1); 5541 5542 /* adjust the vdisk size for the backup GPT and GPE */ 5543 vd->vdisk_size += 33; 5544 5545 CRC32(crc, gpe, sizeof (efi_gpe_t) * VD_MAXPART, -1U, crc32_table); 5546 gpt->efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 5547 5548 CRC32(crc, gpt, sizeof (efi_gpt_t), -1U, crc32_table); 5549 gpt->efi_gpt_HeaderCRC32 = LE_32(~crc); 5550 5551 return (0); 5552 } 5553 5554 /* 5555 * Setup for a virtual disk whose backend is a file (exported as a single slice 5556 * or as a full disk) or a volume device (for example a ZFS, SVM or VxVM volume) 5557 * exported as a full disk. In these cases, the backend is accessed using the 5558 * vnode interface. 5559 */ 5560 static int 5561 vd_setup_backend_vnode(vd_t *vd) 5562 { 5563 int rval, status; 5564 vattr_t vattr; 5565 dev_t dev; 5566 char *file_path = vd->device_path; 5567 ldi_handle_t lhandle; 5568 struct dk_cinfo dk_cinfo; 5569 struct dk_label label; 5570 5571 if ((status = vn_open(file_path, UIO_SYSSPACE, vd->open_flags | FOFFMAX, 5572 0, &vd->file_vnode, 0, 0)) != 0) { 5573 PRN("vn_open(%s) = errno %d", file_path, status); 5574 return (status); 5575 } 5576 5577 /* 5578 * We set vd->file now so that vds_destroy_vd will take care of 5579 * closing the file and releasing the vnode in case of an error. 5580 */ 5581 vd->file = B_TRUE; 5582 5583 vattr.va_mask = AT_SIZE; 5584 if ((status = VOP_GETATTR(vd->file_vnode, &vattr, 0, kcred, NULL)) 5585 != 0) { 5586 PRN("VOP_GETATTR(%s) = errno %d", file_path, status); 5587 return (EIO); 5588 } 5589 5590 vd->file_size = vattr.va_size; 5591 /* size should be at least sizeof(dk_label) */ 5592 if (vd->file_size < sizeof (struct dk_label)) { 5593 PRN("Size of file has to be at least %ld bytes", 5594 sizeof (struct dk_label)); 5595 return (EIO); 5596 } 5597 5598 if (vd->file_vnode->v_flag & VNOMAP) { 5599 PRN("File %s cannot be mapped", file_path); 5600 return (EIO); 5601 } 5602 5603 /* sector size = block size = DEV_BSIZE */ 5604 vd->block_size = DEV_BSIZE; 5605 vd->vdisk_block_size = DEV_BSIZE; 5606 vd->vdisk_size = vd->file_size / DEV_BSIZE; 5607 vd->max_xfer_sz = maxphys / DEV_BSIZE; /* default transfer size */ 5608 5609 /* 5610 * Get max_xfer_sz from the device where the file is or from the device 5611 * itself if we have a volume device. 5612 */ 5613 if (vd->volume) { 5614 status = ldi_open_by_name(file_path, FREAD, kcred, &lhandle, 5615 vd->vds->ldi_ident); 5616 } else { 5617 dev = vd->file_vnode->v_vfsp->vfs_dev; 5618 PR0("underlying device of %s = (%d, %d)\n", file_path, 5619 getmajor(dev), getminor(dev)); 5620 5621 status = ldi_open_by_dev(&dev, OTYP_BLK, FREAD, kcred, &lhandle, 5622 vd->vds->ldi_ident); 5623 } 5624 5625 if (status != 0) { 5626 PR0("ldi_open() returned errno %d for underlying device", 5627 status); 5628 } else { 5629 if ((status = ldi_ioctl(lhandle, DKIOCINFO, 5630 (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred, 5631 &rval)) != 0) { 5632 PR0("ldi_ioctl(DKIOCINFO) returned errno %d for " 5633 "underlying device", status); 5634 } else { 5635 /* 5636 * Store the device's max transfer size for 5637 * return to the client 5638 */ 5639 vd->max_xfer_sz = dk_cinfo.dki_maxtransfer; 5640 } 5641 5642 PR0("close the underlying device"); 5643 (void) ldi_close(lhandle, FREAD, kcred); 5644 } 5645 5646 if (vd->volume) { 5647 PR0("using volume %s, max_xfer = %u blks", file_path, 5648 vd->max_xfer_sz); 5649 } else { 5650 PR0("using file %s on device (%d, %d), max_xfer = %u blks", 5651 file_path, getmajor(dev), getminor(dev), vd->max_xfer_sz); 5652 } 5653 5654 if (vd->vdisk_type == VD_DISK_TYPE_SLICE) { 5655 ASSERT(!vd->volume); 5656 vd->vdisk_media = VD_MEDIA_FIXED; 5657 vd->vdisk_label = (vd_slice_label == VD_DISK_LABEL_UNK)? 5658 vd_file_slice_label : vd_slice_label; 5659 if (vd->vdisk_label == VD_DISK_LABEL_EFI || 5660 vd->file_size >= 2 * ONE_TERABYTE) { 5661 status = vd_setup_partition_efi(vd); 5662 } else { 5663 /* 5664 * We build a default label to get a geometry for 5665 * the vdisk. Then the partition setup function will 5666 * adjust the vtoc so that it defines a single-slice 5667 * disk. 5668 */ 5669 vd_build_default_label(vd->file_size, &label); 5670 vd_label_to_vtocgeom(&label, &vd->vtoc, &vd->dk_geom); 5671 status = vd_setup_partition_vtoc(vd); 5672 } 5673 return (status); 5674 } 5675 5676 /* 5677 * Find and validate the geometry of a disk image. 5678 */ 5679 status = vd_file_validate_geometry(vd); 5680 if (status != 0 && status != EINVAL && status != ENOTSUP) { 5681 PRN("Failed to read label from %s", file_path); 5682 return (EIO); 5683 } 5684 5685 if (vd_file_is_iso_image(vd)) { 5686 /* 5687 * Indicate whether to call this a CD or DVD from the size 5688 * of the ISO image (images for both drive types are stored 5689 * in the ISO-9600 format). CDs can store up to just under 1Gb 5690 */ 5691 if ((vd->vdisk_size * vd->vdisk_block_size) > ONE_GIGABYTE) 5692 vd->vdisk_media = VD_MEDIA_DVD; 5693 else 5694 vd->vdisk_media = VD_MEDIA_CD; 5695 } else { 5696 vd->vdisk_media = VD_MEDIA_FIXED; 5697 } 5698 5699 /* Setup devid for the disk image */ 5700 5701 if (vd->vdisk_label != VD_DISK_LABEL_UNK) { 5702 5703 status = vd_file_read_devid(vd, &vd->file_devid); 5704 5705 if (status == 0) { 5706 /* a valid devid was found */ 5707 return (0); 5708 } 5709 5710 if (status != EINVAL) { 5711 /* 5712 * There was an error while trying to read the devid. 5713 * So this disk image may have a devid but we are 5714 * unable to read it. 5715 */ 5716 PR0("can not read devid for %s", file_path); 5717 vd->file_devid = NULL; 5718 return (0); 5719 } 5720 } 5721 5722 /* 5723 * No valid device id was found so we create one. Note that a failure 5724 * to create a device id is not fatal and does not prevent the disk 5725 * image from being attached. 5726 */ 5727 PR1("creating devid for %s", file_path); 5728 5729 if (ddi_devid_init(vd->vds->dip, DEVID_FAB, NULL, 0, 5730 &vd->file_devid) != DDI_SUCCESS) { 5731 PR0("fail to create devid for %s", file_path); 5732 vd->file_devid = NULL; 5733 return (0); 5734 } 5735 5736 /* 5737 * Write devid to the disk image. The devid is stored into the disk 5738 * image if we have a valid label; otherwise the devid will be stored 5739 * when the user writes a valid label. 5740 */ 5741 if (vd->vdisk_label != VD_DISK_LABEL_UNK) { 5742 if (vd_file_write_devid(vd, vd->file_devid) != 0) { 5743 PR0("fail to write devid for %s", file_path); 5744 ddi_devid_free(vd->file_devid); 5745 vd->file_devid = NULL; 5746 } 5747 } 5748 5749 return (0); 5750 } 5751 5752 5753 /* 5754 * Description: 5755 * Open a device using its device path (supplied by ldm(1m)) 5756 * 5757 * Parameters: 5758 * vd - pointer to structure containing the vDisk info 5759 * flags - open flags 5760 * 5761 * Return Value 5762 * 0 - success 5763 * != 0 - some other non-zero return value from ldi(9F) functions 5764 */ 5765 static int 5766 vd_open_using_ldi_by_name(vd_t *vd, int flags) 5767 { 5768 int status; 5769 char *device_path = vd->device_path; 5770 5771 /* Attempt to open device */ 5772 status = ldi_open_by_name(device_path, flags, kcred, 5773 &vd->ldi_handle[0], vd->vds->ldi_ident); 5774 5775 /* 5776 * The open can fail for example if we are opening an empty slice. 5777 * In case of a failure, we try the open again but this time with 5778 * the FNDELAY flag. 5779 */ 5780 if (status != 0) 5781 status = ldi_open_by_name(device_path, flags | FNDELAY, 5782 kcred, &vd->ldi_handle[0], vd->vds->ldi_ident); 5783 5784 if (status != 0) { 5785 PR0("ldi_open_by_name(%s) = errno %d", device_path, status); 5786 vd->ldi_handle[0] = NULL; 5787 return (status); 5788 } 5789 5790 return (0); 5791 } 5792 5793 /* 5794 * Setup for a virtual disk which backend is a device (a physical disk, 5795 * slice or volume device) that is directly exported either as a full disk 5796 * for a physical disk or as a slice for a volume device or a disk slice. 5797 * In these cases, the backend is accessed using the LDI interface. 5798 */ 5799 static int 5800 vd_setup_backend_ldi(vd_t *vd) 5801 { 5802 int rval, status; 5803 struct dk_cinfo dk_cinfo; 5804 char *device_path = vd->device_path; 5805 5806 /* device has been opened by vd_identify_dev() */ 5807 ASSERT(vd->ldi_handle[0] != NULL); 5808 ASSERT(vd->dev[0] != NULL); 5809 5810 vd->file = B_FALSE; 5811 5812 /* Verify backing device supports dk_cinfo */ 5813 if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO, 5814 (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred, 5815 &rval)) != 0) { 5816 PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s", 5817 status, device_path); 5818 return (status); 5819 } 5820 if (dk_cinfo.dki_partition >= V_NUMPAR) { 5821 PRN("slice %u >= maximum slice %u for %s", 5822 dk_cinfo.dki_partition, V_NUMPAR, device_path); 5823 return (EIO); 5824 } 5825 5826 /* 5827 * The device has been opened read-only by vd_identify_dev(), re-open 5828 * it read-write if the write flag is set and we don't have an optical 5829 * device such as a CD-ROM, which, for now, we do not permit writes to 5830 * and thus should not export write operations to the client. 5831 * 5832 * Future: if/when we implement support for guest domains writing to 5833 * optical devices we will need to do further checking of the media type 5834 * to distinguish between read-only and writable discs. 5835 */ 5836 if (dk_cinfo.dki_ctype == DKC_CDROM) { 5837 5838 vd->open_flags &= ~FWRITE; 5839 5840 } else if (vd->open_flags & FWRITE) { 5841 5842 (void) ldi_close(vd->ldi_handle[0], vd->open_flags & ~FWRITE, 5843 kcred); 5844 status = vd_open_using_ldi_by_name(vd, vd->open_flags); 5845 if (status != 0) { 5846 PR0("Failed to open (%s) = errno %d", 5847 device_path, status); 5848 return (status); 5849 } 5850 } 5851 5852 /* Store the device's max transfer size for return to the client */ 5853 vd->max_xfer_sz = dk_cinfo.dki_maxtransfer; 5854 5855 /* 5856 * We need to work out if it's an ATAPI (IDE CD-ROM) or SCSI device so 5857 * that we can use the correct CDB group when sending USCSI commands. 5858 */ 5859 vd->is_atapi_dev = vd_is_atapi_device(vd); 5860 5861 /* 5862 * Export a full disk. 5863 * 5864 * When we use the LDI interface, we export a device as a full disk 5865 * if we have an entire disk slice (slice 2) and if this slice is 5866 * exported as a full disk and not as a single slice disk. 5867 * Similarly, we want to use LDI if we are accessing a CD or DVD 5868 * device (even if it isn't s2) 5869 * 5870 * Note that volume devices are exported as full disks using the vnode 5871 * interface, not the LDI interface. 5872 */ 5873 if ((dk_cinfo.dki_partition == VD_ENTIRE_DISK_SLICE && 5874 vd->vdisk_type == VD_DISK_TYPE_DISK) || 5875 dk_cinfo.dki_ctype == DKC_CDROM) { 5876 ASSERT(!vd->volume); 5877 if (dk_cinfo.dki_ctype == DKC_SCSI_CCS) 5878 vd->scsi = B_TRUE; 5879 return (vd_setup_full_disk(vd)); 5880 } 5881 5882 /* 5883 * Export a single slice disk. 5884 * 5885 * The exported device can be either a volume device or a disk slice. If 5886 * it is a disk slice different from slice 2 then it is always exported 5887 * as a single slice disk even if the "slice" option is not specified. 5888 * If it is disk slice 2 or a volume device then it is exported as a 5889 * single slice disk only if the "slice" option is specified. 5890 */ 5891 return (vd_setup_single_slice_disk(vd)); 5892 } 5893 5894 static int 5895 vd_setup_single_slice_disk(vd_t *vd) 5896 { 5897 int status, rval; 5898 struct dk_label label; 5899 char *device_path = vd->device_path; 5900 struct vtoc vtoc; 5901 5902 /* Get size of backing device */ 5903 if (ldi_get_size(vd->ldi_handle[0], &vd->vdisk_size) != DDI_SUCCESS) { 5904 PRN("ldi_get_size() failed for %s", device_path); 5905 return (EIO); 5906 } 5907 vd->vdisk_size = lbtodb(vd->vdisk_size); /* convert to blocks */ 5908 vd->block_size = DEV_BSIZE; 5909 vd->vdisk_block_size = DEV_BSIZE; 5910 vd->vdisk_media = VD_MEDIA_FIXED; 5911 5912 if (vd->volume) { 5913 ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 5914 } 5915 5916 /* 5917 * We export the slice as a single slice disk even if the "slice" 5918 * option was not specified. 5919 */ 5920 vd->vdisk_type = VD_DISK_TYPE_SLICE; 5921 vd->nslices = 1; 5922 5923 /* 5924 * When exporting a slice or a device as a single slice disk, we don't 5925 * care about any partitioning exposed by the backend. The goal is just 5926 * to export the backend as a flat storage. We provide a fake partition 5927 * table (either a VTOC or EFI), which presents only one slice, to 5928 * accommodate tools expecting a disk label. The selection of the label 5929 * type (VTOC or EFI) depends on the value of the vd_slice_label 5930 * variable. 5931 */ 5932 if (vd_slice_label == VD_DISK_LABEL_EFI || 5933 vd->vdisk_size >= ONE_TERABYTE / DEV_BSIZE) { 5934 vd->vdisk_label = VD_DISK_LABEL_EFI; 5935 } else { 5936 status = ldi_ioctl(vd->ldi_handle[0], DKIOCGEXTVTOC, 5937 (intptr_t)&vd->vtoc, (vd->open_flags | FKIOCTL), 5938 kcred, &rval); 5939 5940 if (status == ENOTTY) { 5941 /* try with the non-extended vtoc ioctl */ 5942 status = ldi_ioctl(vd->ldi_handle[0], DKIOCGVTOC, 5943 (intptr_t)&vtoc, (vd->open_flags | FKIOCTL), 5944 kcred, &rval); 5945 vtoctoextvtoc(vtoc, vd->vtoc); 5946 } 5947 5948 if (status == 0) { 5949 status = ldi_ioctl(vd->ldi_handle[0], DKIOCGGEOM, 5950 (intptr_t)&vd->dk_geom, (vd->open_flags | FKIOCTL), 5951 kcred, &rval); 5952 5953 if (status != 0) { 5954 PRN("ldi_ioctl(DKIOCGEOM) returned errno %d " 5955 "for %s", status, device_path); 5956 return (status); 5957 } 5958 vd->vdisk_label = VD_DISK_LABEL_VTOC; 5959 5960 } else if (vd_slice_label == VD_DISK_LABEL_VTOC) { 5961 5962 vd->vdisk_label = VD_DISK_LABEL_VTOC; 5963 vd_build_default_label(vd->vdisk_size * DEV_BSIZE, 5964 &label); 5965 vd_label_to_vtocgeom(&label, &vd->vtoc, &vd->dk_geom); 5966 5967 } else { 5968 vd->vdisk_label = VD_DISK_LABEL_EFI; 5969 } 5970 } 5971 5972 if (vd->vdisk_label == VD_DISK_LABEL_VTOC) { 5973 /* export with a fake VTOC label */ 5974 status = vd_setup_partition_vtoc(vd); 5975 5976 } else { 5977 /* export with a fake EFI label */ 5978 status = vd_setup_partition_efi(vd); 5979 } 5980 5981 return (status); 5982 } 5983 5984 static int 5985 vd_backend_check_size(vd_t *vd) 5986 { 5987 size_t backend_size, old_size, new_size; 5988 struct dk_minfo minfo; 5989 vattr_t vattr; 5990 int rval, rv; 5991 5992 if (vd->file) { 5993 5994 /* file (slice or full disk) */ 5995 vattr.va_mask = AT_SIZE; 5996 rv = VOP_GETATTR(vd->file_vnode, &vattr, 0, kcred, NULL); 5997 if (rv != 0) { 5998 PR0("VOP_GETATTR(%s) = errno %d", vd->device_path, rv); 5999 return (rv); 6000 } 6001 backend_size = vattr.va_size; 6002 6003 } else if (vd->vdisk_type == VD_DISK_TYPE_SLICE) { 6004 6005 /* slice or device exported as a slice */ 6006 rv = ldi_get_size(vd->ldi_handle[0], &backend_size); 6007 if (rv != DDI_SUCCESS) { 6008 PR0("ldi_get_size() failed for %s", vd->device_path); 6009 return (EIO); 6010 } 6011 6012 } else { 6013 6014 /* disk or device exported as a disk */ 6015 ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK); 6016 rv = ldi_ioctl(vd->ldi_handle[0], DKIOCGMEDIAINFO, 6017 (intptr_t)&minfo, (vd->open_flags | FKIOCTL), 6018 kcred, &rval); 6019 if (rv != 0) { 6020 PR0("DKIOCGMEDIAINFO failed for %s (err=%d)", 6021 vd->device_path, rv); 6022 return (rv); 6023 } 6024 backend_size = minfo.dki_capacity * minfo.dki_lbsize; 6025 } 6026 6027 old_size = vd->vdisk_size; 6028 new_size = backend_size / DEV_BSIZE; 6029 6030 /* check if size has changed */ 6031 if (old_size != VD_SIZE_UNKNOWN && old_size == new_size) 6032 return (0); 6033 6034 vd->vdisk_size = new_size; 6035 6036 if (vd->file) 6037 vd->file_size = backend_size; 6038 6039 /* 6040 * If we are exporting a single-slice disk and the size of the backend 6041 * has changed then we regenerate the partition setup so that the 6042 * partitioning matches with the new disk backend size. 6043 */ 6044 6045 if (vd->vdisk_type == VD_DISK_TYPE_SLICE) { 6046 /* slice or file or device exported as a slice */ 6047 if (vd->vdisk_label == VD_DISK_LABEL_VTOC) { 6048 rv = vd_setup_partition_vtoc(vd); 6049 if (rv != 0) { 6050 PR0("vd_setup_partition_vtoc() failed for %s " 6051 "(err = %d)", vd->device_path, rv); 6052 return (rv); 6053 } 6054 } else { 6055 rv = vd_setup_partition_efi(vd); 6056 if (rv != 0) { 6057 PR0("vd_setup_partition_efi() failed for %s " 6058 "(err = %d)", vd->device_path, rv); 6059 return (rv); 6060 } 6061 } 6062 6063 } else if (!vd->file) { 6064 /* disk or device exported as a disk */ 6065 ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK); 6066 vd->block_size = minfo.dki_lbsize; 6067 vd->vdisk_media = 6068 DK_MEDIATYPE2VD_MEDIATYPE(minfo.dki_media_type); 6069 } 6070 6071 return (0); 6072 } 6073 6074 /* 6075 * Description: 6076 * Open a device using its device path and identify if this is 6077 * a disk device or a volume device. 6078 * 6079 * Parameters: 6080 * vd - pointer to structure containing the vDisk info 6081 * dtype - return the driver type of the device 6082 * 6083 * Return Value 6084 * 0 - success 6085 * != 0 - some other non-zero return value from ldi(9F) functions 6086 */ 6087 static int 6088 vd_identify_dev(vd_t *vd, int *dtype) 6089 { 6090 int status, i; 6091 char *device_path = vd->device_path; 6092 char *drv_name; 6093 int drv_type; 6094 vds_t *vds = vd->vds; 6095 6096 status = vd_open_using_ldi_by_name(vd, vd->open_flags & ~FWRITE); 6097 if (status != 0) { 6098 PR0("Failed to open (%s) = errno %d", device_path, status); 6099 return (status); 6100 } 6101 6102 /* Get device number of backing device */ 6103 if ((status = ldi_get_dev(vd->ldi_handle[0], &vd->dev[0])) != 0) { 6104 PRN("ldi_get_dev() returned errno %d for %s", 6105 status, device_path); 6106 return (status); 6107 } 6108 6109 /* 6110 * We start by looking if the driver is in the list from vds.conf 6111 * so that we can override the built-in list using vds.conf. 6112 */ 6113 drv_name = ddi_major_to_name(getmajor(vd->dev[0])); 6114 drv_type = VD_DRIVER_UNKNOWN; 6115 6116 /* check vds.conf list */ 6117 for (i = 0; i < vds->num_drivers; i++) { 6118 if (vds->driver_types[i].type == VD_DRIVER_UNKNOWN) { 6119 /* ignore invalid entries */ 6120 continue; 6121 } 6122 if (strcmp(drv_name, vds->driver_types[i].name) == 0) { 6123 drv_type = vds->driver_types[i].type; 6124 goto done; 6125 } 6126 } 6127 6128 /* check built-in list */ 6129 for (i = 0; i < VDS_NUM_DRIVERS; i++) { 6130 if (strcmp(drv_name, vds_driver_types[i].name) == 0) { 6131 drv_type = vds_driver_types[i].type; 6132 goto done; 6133 } 6134 } 6135 6136 done: 6137 PR0("driver %s identified as %s", drv_name, 6138 (drv_type == VD_DRIVER_DISK)? "DISK" : 6139 (drv_type == VD_DRIVER_VOLUME)? "VOLUME" : "UNKNOWN"); 6140 6141 *dtype = drv_type; 6142 6143 return (0); 6144 } 6145 6146 static int 6147 vd_setup_vd(vd_t *vd) 6148 { 6149 int status, drv_type, pseudo; 6150 dev_info_t *dip; 6151 vnode_t *vnp; 6152 char *path = vd->device_path; 6153 6154 /* make sure the vdisk backend is valid */ 6155 if ((status = lookupname(path, UIO_SYSSPACE, 6156 FOLLOW, NULLVPP, &vnp)) != 0) { 6157 PR0("Cannot lookup %s errno %d", path, status); 6158 goto done; 6159 } 6160 6161 switch (vnp->v_type) { 6162 case VREG: 6163 /* 6164 * Backend is a file so it is exported as a full disk or as a 6165 * single slice disk using the vnode interface. 6166 */ 6167 VN_RELE(vnp); 6168 vd->volume = B_FALSE; 6169 status = vd_setup_backend_vnode(vd); 6170 break; 6171 6172 case VBLK: 6173 case VCHR: 6174 /* 6175 * Backend is a device. The way it is exported depends on the 6176 * type of the device. 6177 * 6178 * - A volume device is exported as a full disk using the vnode 6179 * interface or as a single slice disk using the LDI 6180 * interface. 6181 * 6182 * - A disk (represented by the slice 2 of that disk) is 6183 * exported as a full disk using the LDI interface. 6184 * 6185 * - A disk slice (different from slice 2) is always exported 6186 * as a single slice disk using the LDI interface. 6187 * 6188 * - The slice 2 of a disk is exported as a single slice disk 6189 * if the "slice" option is specified, otherwise the entire 6190 * disk will be exported. In any case, the LDI interface is 6191 * used. 6192 */ 6193 6194 /* check if this is a pseudo device */ 6195 if ((dip = ddi_hold_devi_by_instance(getmajor(vnp->v_rdev), 6196 dev_to_instance(vnp->v_rdev), 0)) == NULL) { 6197 PRN("%s is no longer accessible", path); 6198 VN_RELE(vnp); 6199 status = EIO; 6200 break; 6201 } 6202 pseudo = is_pseudo_device(dip); 6203 ddi_release_devi(dip); 6204 VN_RELE(vnp); 6205 6206 if (vd_identify_dev(vd, &drv_type) != 0) { 6207 PRN("%s identification failed", path); 6208 status = EIO; 6209 break; 6210 } 6211 6212 /* 6213 * If the driver hasn't been identified then we consider that 6214 * pseudo devices are volumes and other devices are disks. 6215 */ 6216 if (drv_type == VD_DRIVER_VOLUME || 6217 (drv_type == VD_DRIVER_UNKNOWN && pseudo)) { 6218 vd->volume = B_TRUE; 6219 } else { 6220 status = vd_setup_backend_ldi(vd); 6221 break; 6222 } 6223 6224 /* 6225 * If this is a volume device then its usage depends if the 6226 * "slice" option is set or not. If the "slice" option is set 6227 * then the volume device will be exported as a single slice, 6228 * otherwise it will be exported as a full disk. 6229 * 6230 * For backward compatibility, if vd_volume_force_slice is set 6231 * then we always export volume devices as slices. 6232 */ 6233 if (vd_volume_force_slice) { 6234 vd->vdisk_type = VD_DISK_TYPE_SLICE; 6235 vd->nslices = 1; 6236 } 6237 6238 if (vd->vdisk_type == VD_DISK_TYPE_DISK) { 6239 /* close device opened during identification */ 6240 (void) ldi_close(vd->ldi_handle[0], 6241 vd->open_flags & ~FWRITE, kcred); 6242 vd->ldi_handle[0] = NULL; 6243 vd->dev[0] = 0; 6244 status = vd_setup_backend_vnode(vd); 6245 } else { 6246 status = vd_setup_backend_ldi(vd); 6247 } 6248 break; 6249 6250 default: 6251 PRN("Unsupported vdisk backend %s", path); 6252 VN_RELE(vnp); 6253 status = EBADF; 6254 } 6255 6256 done: 6257 if (status != 0) { 6258 /* 6259 * If the error is retryable print an error message only 6260 * during the first try. 6261 */ 6262 if (status == ENXIO || status == ENODEV || 6263 status == ENOENT || status == EROFS) { 6264 if (!(vd->initialized & VD_SETUP_ERROR)) { 6265 PRN("%s is currently inaccessible (error %d)", 6266 path, status); 6267 } 6268 status = EAGAIN; 6269 } else { 6270 PRN("%s can not be exported as a virtual disk " 6271 "(error %d)", path, status); 6272 } 6273 vd->initialized |= VD_SETUP_ERROR; 6274 6275 } else if (vd->initialized & VD_SETUP_ERROR) { 6276 /* print a message only if we previously had an error */ 6277 PRN("%s is now online", path); 6278 vd->initialized &= ~VD_SETUP_ERROR; 6279 } 6280 6281 return (status); 6282 } 6283 6284 static int 6285 vds_do_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options, 6286 uint64_t ldc_id, vd_t **vdp) 6287 { 6288 char tq_name[TASKQ_NAMELEN]; 6289 int status; 6290 ddi_iblock_cookie_t iblock = NULL; 6291 ldc_attr_t ldc_attr; 6292 vd_t *vd; 6293 6294 6295 ASSERT(vds != NULL); 6296 ASSERT(device_path != NULL); 6297 ASSERT(vdp != NULL); 6298 PR0("Adding vdisk for %s", device_path); 6299 6300 if ((vd = kmem_zalloc(sizeof (*vd), KM_NOSLEEP)) == NULL) { 6301 PRN("No memory for virtual disk"); 6302 return (EAGAIN); 6303 } 6304 *vdp = vd; /* assign here so vds_destroy_vd() can cleanup later */ 6305 vd->vds = vds; 6306 (void) strncpy(vd->device_path, device_path, MAXPATHLEN); 6307 6308 /* Setup open flags */ 6309 vd->open_flags = FREAD; 6310 6311 if (!(options & VD_OPT_RDONLY)) 6312 vd->open_flags |= FWRITE; 6313 6314 if (options & VD_OPT_EXCLUSIVE) 6315 vd->open_flags |= FEXCL; 6316 6317 /* Setup disk type */ 6318 if (options & VD_OPT_SLICE) { 6319 vd->vdisk_type = VD_DISK_TYPE_SLICE; 6320 vd->nslices = 1; 6321 } else { 6322 vd->vdisk_type = VD_DISK_TYPE_DISK; 6323 vd->nslices = V_NUMPAR; 6324 } 6325 6326 /* default disk label */ 6327 vd->vdisk_label = VD_DISK_LABEL_UNK; 6328 6329 /* Open vdisk and initialize parameters */ 6330 if ((status = vd_setup_vd(vd)) == 0) { 6331 vd->initialized |= VD_DISK_READY; 6332 6333 ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR); 6334 PR0("vdisk_type = %s, volume = %s, file = %s, nslices = %u", 6335 ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"), 6336 (vd->volume ? "yes" : "no"), (vd->file ? "yes" : "no"), 6337 vd->nslices); 6338 } else { 6339 if (status != EAGAIN) 6340 return (status); 6341 } 6342 6343 /* Initialize locking */ 6344 if (ddi_get_soft_iblock_cookie(vds->dip, DDI_SOFTINT_MED, 6345 &iblock) != DDI_SUCCESS) { 6346 PRN("Could not get iblock cookie."); 6347 return (EIO); 6348 } 6349 6350 mutex_init(&vd->lock, NULL, MUTEX_DRIVER, iblock); 6351 vd->initialized |= VD_LOCKING; 6352 6353 6354 /* Create start and completion task queues for the vdisk */ 6355 (void) snprintf(tq_name, sizeof (tq_name), "vd_startq%lu", id); 6356 PR1("tq_name = %s", tq_name); 6357 if ((vd->startq = ddi_taskq_create(vds->dip, tq_name, 1, 6358 TASKQ_DEFAULTPRI, 0)) == NULL) { 6359 PRN("Could not create task queue"); 6360 return (EIO); 6361 } 6362 (void) snprintf(tq_name, sizeof (tq_name), "vd_completionq%lu", id); 6363 PR1("tq_name = %s", tq_name); 6364 if ((vd->completionq = ddi_taskq_create(vds->dip, tq_name, 1, 6365 TASKQ_DEFAULTPRI, 0)) == NULL) { 6366 PRN("Could not create task queue"); 6367 return (EIO); 6368 } 6369 6370 /* Allocate the staging buffer */ 6371 vd->max_msglen = sizeof (vio_msg_t); /* baseline vio message size */ 6372 vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP); 6373 6374 vd->enabled = 1; /* before callback can dispatch to startq */ 6375 6376 6377 /* Bring up LDC */ 6378 ldc_attr.devclass = LDC_DEV_BLK_SVC; 6379 ldc_attr.instance = ddi_get_instance(vds->dip); 6380 ldc_attr.mode = LDC_MODE_UNRELIABLE; 6381 ldc_attr.mtu = VD_LDC_MTU; 6382 if ((status = ldc_init(ldc_id, &ldc_attr, &vd->ldc_handle)) != 0) { 6383 PRN("Could not initialize LDC channel %lx, " 6384 "init failed with error %d", ldc_id, status); 6385 return (status); 6386 } 6387 vd->initialized |= VD_LDC; 6388 6389 if ((status = ldc_reg_callback(vd->ldc_handle, vd_handle_ldc_events, 6390 (caddr_t)vd)) != 0) { 6391 PRN("Could not initialize LDC channel %lu," 6392 "reg_callback failed with error %d", ldc_id, status); 6393 return (status); 6394 } 6395 6396 if ((status = ldc_open(vd->ldc_handle)) != 0) { 6397 PRN("Could not initialize LDC channel %lu," 6398 "open failed with error %d", ldc_id, status); 6399 return (status); 6400 } 6401 6402 if ((status = ldc_up(vd->ldc_handle)) != 0) { 6403 PR0("ldc_up() returned errno %d", status); 6404 } 6405 6406 /* Allocate the inband task memory handle */ 6407 status = ldc_mem_alloc_handle(vd->ldc_handle, &(vd->inband_task.mhdl)); 6408 if (status) { 6409 PRN("Could not initialize LDC channel %lu," 6410 "alloc_handle failed with error %d", ldc_id, status); 6411 return (ENXIO); 6412 } 6413 6414 /* Add the successfully-initialized vdisk to the server's table */ 6415 if (mod_hash_insert(vds->vd_table, (mod_hash_key_t)id, vd) != 0) { 6416 PRN("Error adding vdisk ID %lu to table", id); 6417 return (EIO); 6418 } 6419 6420 /* store initial state */ 6421 vd->state = VD_STATE_INIT; 6422 6423 return (0); 6424 } 6425 6426 static void 6427 vd_free_dring_task(vd_t *vdp) 6428 { 6429 if (vdp->dring_task != NULL) { 6430 ASSERT(vdp->dring_len != 0); 6431 /* Free all dring_task memory handles */ 6432 for (int i = 0; i < vdp->dring_len; i++) { 6433 (void) ldc_mem_free_handle(vdp->dring_task[i].mhdl); 6434 kmem_free(vdp->dring_task[i].request, 6435 (vdp->descriptor_size - 6436 sizeof (vio_dring_entry_hdr_t))); 6437 vdp->dring_task[i].request = NULL; 6438 kmem_free(vdp->dring_task[i].msg, vdp->max_msglen); 6439 vdp->dring_task[i].msg = NULL; 6440 } 6441 kmem_free(vdp->dring_task, 6442 (sizeof (*vdp->dring_task)) * vdp->dring_len); 6443 vdp->dring_task = NULL; 6444 } 6445 } 6446 6447 /* 6448 * Destroy the state associated with a virtual disk 6449 */ 6450 static void 6451 vds_destroy_vd(void *arg) 6452 { 6453 vd_t *vd = (vd_t *)arg; 6454 int retry = 0, rv; 6455 6456 if (vd == NULL) 6457 return; 6458 6459 PR0("Destroying vdisk state"); 6460 6461 /* Disable queuing requests for the vdisk */ 6462 if (vd->initialized & VD_LOCKING) { 6463 mutex_enter(&vd->lock); 6464 vd->enabled = 0; 6465 mutex_exit(&vd->lock); 6466 } 6467 6468 /* Drain and destroy start queue (*before* destroying completionq) */ 6469 if (vd->startq != NULL) 6470 ddi_taskq_destroy(vd->startq); /* waits for queued tasks */ 6471 6472 /* Drain and destroy completion queue (*before* shutting down LDC) */ 6473 if (vd->completionq != NULL) 6474 ddi_taskq_destroy(vd->completionq); /* waits for tasks */ 6475 6476 vd_free_dring_task(vd); 6477 6478 /* Free the inband task memory handle */ 6479 (void) ldc_mem_free_handle(vd->inband_task.mhdl); 6480 6481 /* Shut down LDC */ 6482 if (vd->initialized & VD_LDC) { 6483 /* unmap the dring */ 6484 if (vd->initialized & VD_DRING) 6485 (void) ldc_mem_dring_unmap(vd->dring_handle); 6486 6487 /* close LDC channel - retry on EAGAIN */ 6488 while ((rv = ldc_close(vd->ldc_handle)) == EAGAIN) { 6489 if (++retry > vds_ldc_retries) { 6490 PR0("Timed out closing channel"); 6491 break; 6492 } 6493 drv_usecwait(vds_ldc_delay); 6494 } 6495 if (rv == 0) { 6496 (void) ldc_unreg_callback(vd->ldc_handle); 6497 (void) ldc_fini(vd->ldc_handle); 6498 } else { 6499 /* 6500 * Closing the LDC channel has failed. Ideally we should 6501 * fail here but there is no Zeus level infrastructure 6502 * to handle this. The MD has already been changed and 6503 * we have to do the close. So we try to do as much 6504 * clean up as we can. 6505 */ 6506 (void) ldc_set_cb_mode(vd->ldc_handle, LDC_CB_DISABLE); 6507 while (ldc_unreg_callback(vd->ldc_handle) == EAGAIN) 6508 drv_usecwait(vds_ldc_delay); 6509 } 6510 } 6511 6512 /* Free the staging buffer for msgs */ 6513 if (vd->vio_msgp != NULL) { 6514 kmem_free(vd->vio_msgp, vd->max_msglen); 6515 vd->vio_msgp = NULL; 6516 } 6517 6518 /* Free the inband message buffer */ 6519 if (vd->inband_task.msg != NULL) { 6520 kmem_free(vd->inband_task.msg, vd->max_msglen); 6521 vd->inband_task.msg = NULL; 6522 } 6523 6524 if (vd->file) { 6525 /* Close file */ 6526 (void) VOP_CLOSE(vd->file_vnode, vd->open_flags, 1, 6527 0, kcred, NULL); 6528 VN_RELE(vd->file_vnode); 6529 if (vd->file_devid != NULL) 6530 ddi_devid_free(vd->file_devid); 6531 } else { 6532 /* Close any open backing-device slices */ 6533 for (uint_t slice = 0; slice < V_NUMPAR; slice++) { 6534 if (vd->ldi_handle[slice] != NULL) { 6535 PR0("Closing slice %u", slice); 6536 (void) ldi_close(vd->ldi_handle[slice], 6537 vd->open_flags, kcred); 6538 } 6539 } 6540 } 6541 6542 /* Free any fake label */ 6543 if (vd->flabel) { 6544 kmem_free(vd->flabel, vd->flabel_size); 6545 vd->flabel = NULL; 6546 vd->flabel_size = 0; 6547 } 6548 6549 /* Free lock */ 6550 if (vd->initialized & VD_LOCKING) 6551 mutex_destroy(&vd->lock); 6552 6553 /* Finally, free the vdisk structure itself */ 6554 kmem_free(vd, sizeof (*vd)); 6555 } 6556 6557 static int 6558 vds_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options, 6559 uint64_t ldc_id) 6560 { 6561 int status; 6562 vd_t *vd = NULL; 6563 6564 6565 if ((status = vds_do_init_vd(vds, id, device_path, options, 6566 ldc_id, &vd)) != 0) 6567 vds_destroy_vd(vd); 6568 6569 return (status); 6570 } 6571 6572 static int 6573 vds_do_get_ldc_id(md_t *md, mde_cookie_t vd_node, mde_cookie_t *channel, 6574 uint64_t *ldc_id) 6575 { 6576 int num_channels; 6577 6578 6579 /* Look for channel endpoint child(ren) of the vdisk MD node */ 6580 if ((num_channels = md_scan_dag(md, vd_node, 6581 md_find_name(md, VD_CHANNEL_ENDPOINT), 6582 md_find_name(md, "fwd"), channel)) <= 0) { 6583 PRN("No \"%s\" found for virtual disk", VD_CHANNEL_ENDPOINT); 6584 return (-1); 6585 } 6586 6587 /* Get the "id" value for the first channel endpoint node */ 6588 if (md_get_prop_val(md, channel[0], VD_ID_PROP, ldc_id) != 0) { 6589 PRN("No \"%s\" property found for \"%s\" of vdisk", 6590 VD_ID_PROP, VD_CHANNEL_ENDPOINT); 6591 return (-1); 6592 } 6593 6594 if (num_channels > 1) { 6595 PRN("Using ID of first of multiple channels for this vdisk"); 6596 } 6597 6598 return (0); 6599 } 6600 6601 static int 6602 vds_get_ldc_id(md_t *md, mde_cookie_t vd_node, uint64_t *ldc_id) 6603 { 6604 int num_nodes, status; 6605 size_t size; 6606 mde_cookie_t *channel; 6607 6608 6609 if ((num_nodes = md_node_count(md)) <= 0) { 6610 PRN("Invalid node count in Machine Description subtree"); 6611 return (-1); 6612 } 6613 size = num_nodes*(sizeof (*channel)); 6614 channel = kmem_zalloc(size, KM_SLEEP); 6615 status = vds_do_get_ldc_id(md, vd_node, channel, ldc_id); 6616 kmem_free(channel, size); 6617 6618 return (status); 6619 } 6620 6621 /* 6622 * Function: 6623 * vds_get_options 6624 * 6625 * Description: 6626 * Parse the options of a vds node. Options are defined as an array 6627 * of strings in the vds-block-device-opts property of the vds node 6628 * in the machine description. Options are returned as a bitmask. The 6629 * mapping between the bitmask options and the options strings from the 6630 * machine description is defined in the vd_bdev_options[] array. 6631 * 6632 * The vds-block-device-opts property is optional. If a vds has no such 6633 * property then no option is defined. 6634 * 6635 * Parameters: 6636 * md - machine description. 6637 * vd_node - vds node in the machine description for which 6638 * options have to be parsed. 6639 * options - the returned options. 6640 * 6641 * Return Code: 6642 * none. 6643 */ 6644 static void 6645 vds_get_options(md_t *md, mde_cookie_t vd_node, uint64_t *options) 6646 { 6647 char *optstr, *opt; 6648 int len, n, i; 6649 6650 *options = 0; 6651 6652 if (md_get_prop_data(md, vd_node, VD_BLOCK_DEVICE_OPTS, 6653 (uint8_t **)&optstr, &len) != 0) { 6654 PR0("No options found"); 6655 return; 6656 } 6657 6658 /* parse options */ 6659 opt = optstr; 6660 n = sizeof (vd_bdev_options) / sizeof (vd_option_t); 6661 6662 while (opt < optstr + len) { 6663 for (i = 0; i < n; i++) { 6664 if (strncmp(vd_bdev_options[i].vdo_name, 6665 opt, VD_OPTION_NLEN) == 0) { 6666 *options |= vd_bdev_options[i].vdo_value; 6667 break; 6668 } 6669 } 6670 6671 if (i < n) { 6672 PR0("option: %s", opt); 6673 } else { 6674 PRN("option %s is unknown or unsupported", opt); 6675 } 6676 6677 opt += strlen(opt) + 1; 6678 } 6679 } 6680 6681 static void 6682 vds_driver_types_free(vds_t *vds) 6683 { 6684 if (vds->driver_types != NULL) { 6685 kmem_free(vds->driver_types, sizeof (vd_driver_type_t) * 6686 vds->num_drivers); 6687 vds->driver_types = NULL; 6688 vds->num_drivers = 0; 6689 } 6690 } 6691 6692 /* 6693 * Update the driver type list with information from vds.conf. 6694 */ 6695 static void 6696 vds_driver_types_update(vds_t *vds) 6697 { 6698 char **list, *s; 6699 uint_t i, num, count = 0, len; 6700 6701 if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, vds->dip, 6702 DDI_PROP_DONTPASS, "driver-type-list", &list, &num) != 6703 DDI_PROP_SUCCESS) 6704 return; 6705 6706 /* 6707 * We create a driver_types list with as many as entries as there 6708 * is in the driver-type-list from vds.conf. However only valid 6709 * entries will be populated (i.e. entries from driver-type-list 6710 * with a valid syntax). Invalid entries will be left blank so 6711 * they will have no driver name and the driver type will be 6712 * VD_DRIVER_UNKNOWN (= 0). 6713 */ 6714 vds->num_drivers = num; 6715 vds->driver_types = kmem_zalloc(sizeof (vd_driver_type_t) * num, 6716 KM_SLEEP); 6717 6718 for (i = 0; i < num; i++) { 6719 6720 s = strchr(list[i], ':'); 6721 6722 if (s == NULL) { 6723 PRN("vds.conf: driver-type-list, entry %d (%s): " 6724 "a colon is expected in the entry", 6725 i, list[i]); 6726 continue; 6727 } 6728 6729 len = (uintptr_t)s - (uintptr_t)list[i]; 6730 6731 if (len == 0) { 6732 PRN("vds.conf: driver-type-list, entry %d (%s): " 6733 "the driver name is empty", 6734 i, list[i]); 6735 continue; 6736 } 6737 6738 if (len >= VD_DRIVER_NAME_LEN) { 6739 PRN("vds.conf: driver-type-list, entry %d (%s): " 6740 "the driver name is too long", 6741 i, list[i]); 6742 continue; 6743 } 6744 6745 if (strcmp(s + 1, "disk") == 0) { 6746 6747 vds->driver_types[i].type = VD_DRIVER_DISK; 6748 6749 } else if (strcmp(s + 1, "volume") == 0) { 6750 6751 vds->driver_types[i].type = VD_DRIVER_VOLUME; 6752 6753 } else { 6754 PRN("vds.conf: driver-type-list, entry %d (%s): " 6755 "the driver type is invalid", 6756 i, list[i]); 6757 continue; 6758 } 6759 6760 (void) strncpy(vds->driver_types[i].name, list[i], len); 6761 6762 PR0("driver-type-list, entry %d (%s) added", 6763 i, list[i]); 6764 6765 count++; 6766 } 6767 6768 ddi_prop_free(list); 6769 6770 if (count == 0) { 6771 /* nothing was added, clean up */ 6772 vds_driver_types_free(vds); 6773 } 6774 } 6775 6776 static void 6777 vds_add_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node) 6778 { 6779 char *device_path = NULL; 6780 uint64_t id = 0, ldc_id = 0, options = 0; 6781 6782 if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) { 6783 PRN("Error getting vdisk \"%s\"", VD_ID_PROP); 6784 return; 6785 } 6786 PR0("Adding vdisk ID %lu", id); 6787 if (md_get_prop_str(md, vd_node, VD_BLOCK_DEVICE_PROP, 6788 &device_path) != 0) { 6789 PRN("Error getting vdisk \"%s\"", VD_BLOCK_DEVICE_PROP); 6790 return; 6791 } 6792 6793 vds_get_options(md, vd_node, &options); 6794 6795 if (vds_get_ldc_id(md, vd_node, &ldc_id) != 0) { 6796 PRN("Error getting LDC ID for vdisk %lu", id); 6797 return; 6798 } 6799 6800 if (vds_init_vd(vds, id, device_path, options, ldc_id) != 0) { 6801 PRN("Failed to add vdisk ID %lu", id); 6802 if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0) 6803 PRN("No vDisk entry found for vdisk ID %lu", id); 6804 return; 6805 } 6806 } 6807 6808 static void 6809 vds_remove_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node) 6810 { 6811 uint64_t id = 0; 6812 6813 6814 if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) { 6815 PRN("Unable to get \"%s\" property from vdisk's MD node", 6816 VD_ID_PROP); 6817 return; 6818 } 6819 PR0("Removing vdisk ID %lu", id); 6820 if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0) 6821 PRN("No vdisk entry found for vdisk ID %lu", id); 6822 } 6823 6824 static void 6825 vds_change_vd(vds_t *vds, md_t *prev_md, mde_cookie_t prev_vd_node, 6826 md_t *curr_md, mde_cookie_t curr_vd_node) 6827 { 6828 char *curr_dev, *prev_dev; 6829 uint64_t curr_id = 0, curr_ldc_id = 0, curr_options = 0; 6830 uint64_t prev_id = 0, prev_ldc_id = 0, prev_options = 0; 6831 size_t len; 6832 6833 6834 /* Validate that vdisk ID has not changed */ 6835 if (md_get_prop_val(prev_md, prev_vd_node, VD_ID_PROP, &prev_id) != 0) { 6836 PRN("Error getting previous vdisk \"%s\" property", 6837 VD_ID_PROP); 6838 return; 6839 } 6840 if (md_get_prop_val(curr_md, curr_vd_node, VD_ID_PROP, &curr_id) != 0) { 6841 PRN("Error getting current vdisk \"%s\" property", VD_ID_PROP); 6842 return; 6843 } 6844 if (curr_id != prev_id) { 6845 PRN("Not changing vdisk: ID changed from %lu to %lu", 6846 prev_id, curr_id); 6847 return; 6848 } 6849 6850 /* Validate that LDC ID has not changed */ 6851 if (vds_get_ldc_id(prev_md, prev_vd_node, &prev_ldc_id) != 0) { 6852 PRN("Error getting LDC ID for vdisk %lu", prev_id); 6853 return; 6854 } 6855 6856 if (vds_get_ldc_id(curr_md, curr_vd_node, &curr_ldc_id) != 0) { 6857 PRN("Error getting LDC ID for vdisk %lu", curr_id); 6858 return; 6859 } 6860 if (curr_ldc_id != prev_ldc_id) { 6861 _NOTE(NOTREACHED); /* lint is confused */ 6862 PRN("Not changing vdisk: " 6863 "LDC ID changed from %lu to %lu", prev_ldc_id, curr_ldc_id); 6864 return; 6865 } 6866 6867 /* Determine whether device path has changed */ 6868 if (md_get_prop_str(prev_md, prev_vd_node, VD_BLOCK_DEVICE_PROP, 6869 &prev_dev) != 0) { 6870 PRN("Error getting previous vdisk \"%s\"", 6871 VD_BLOCK_DEVICE_PROP); 6872 return; 6873 } 6874 if (md_get_prop_str(curr_md, curr_vd_node, VD_BLOCK_DEVICE_PROP, 6875 &curr_dev) != 0) { 6876 PRN("Error getting current vdisk \"%s\"", VD_BLOCK_DEVICE_PROP); 6877 return; 6878 } 6879 if (((len = strlen(curr_dev)) == strlen(prev_dev)) && 6880 (strncmp(curr_dev, prev_dev, len) == 0)) 6881 return; /* no relevant (supported) change */ 6882 6883 /* Validate that options have not changed */ 6884 vds_get_options(prev_md, prev_vd_node, &prev_options); 6885 vds_get_options(curr_md, curr_vd_node, &curr_options); 6886 if (prev_options != curr_options) { 6887 PRN("Not changing vdisk: options changed from %lx to %lx", 6888 prev_options, curr_options); 6889 return; 6890 } 6891 6892 PR0("Changing vdisk ID %lu", prev_id); 6893 6894 /* Remove old state, which will close vdisk and reset */ 6895 if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)prev_id) != 0) 6896 PRN("No entry found for vdisk ID %lu", prev_id); 6897 6898 /* Re-initialize vdisk with new state */ 6899 if (vds_init_vd(vds, curr_id, curr_dev, curr_options, 6900 curr_ldc_id) != 0) { 6901 PRN("Failed to change vdisk ID %lu", curr_id); 6902 return; 6903 } 6904 } 6905 6906 static int 6907 vds_process_md(void *arg, mdeg_result_t *md) 6908 { 6909 int i; 6910 vds_t *vds = arg; 6911 6912 6913 if (md == NULL) 6914 return (MDEG_FAILURE); 6915 ASSERT(vds != NULL); 6916 6917 for (i = 0; i < md->removed.nelem; i++) 6918 vds_remove_vd(vds, md->removed.mdp, md->removed.mdep[i]); 6919 for (i = 0; i < md->match_curr.nelem; i++) 6920 vds_change_vd(vds, md->match_prev.mdp, md->match_prev.mdep[i], 6921 md->match_curr.mdp, md->match_curr.mdep[i]); 6922 for (i = 0; i < md->added.nelem; i++) 6923 vds_add_vd(vds, md->added.mdp, md->added.mdep[i]); 6924 6925 return (MDEG_SUCCESS); 6926 } 6927 6928 6929 static int 6930 vds_do_attach(dev_info_t *dip) 6931 { 6932 int status, sz; 6933 int cfg_handle; 6934 minor_t instance = ddi_get_instance(dip); 6935 vds_t *vds; 6936 mdeg_prop_spec_t *pspecp; 6937 mdeg_node_spec_t *ispecp; 6938 6939 /* 6940 * The "cfg-handle" property of a vds node in an MD contains the MD's 6941 * notion of "instance", or unique identifier, for that node; OBP 6942 * stores the value of the "cfg-handle" MD property as the value of 6943 * the "reg" property on the node in the device tree it builds from 6944 * the MD and passes to Solaris. Thus, we look up the devinfo node's 6945 * "reg" property value to uniquely identify this device instance when 6946 * registering with the MD event-generation framework. If the "reg" 6947 * property cannot be found, the device tree state is presumably so 6948 * broken that there is no point in continuing. 6949 */ 6950 if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 6951 VD_REG_PROP)) { 6952 PRN("vds \"%s\" property does not exist", VD_REG_PROP); 6953 return (DDI_FAILURE); 6954 } 6955 6956 /* Get the MD instance for later MDEG registration */ 6957 cfg_handle = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 6958 VD_REG_PROP, -1); 6959 6960 if (ddi_soft_state_zalloc(vds_state, instance) != DDI_SUCCESS) { 6961 PRN("Could not allocate state for instance %u", instance); 6962 return (DDI_FAILURE); 6963 } 6964 6965 if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) { 6966 PRN("Could not get state for instance %u", instance); 6967 ddi_soft_state_free(vds_state, instance); 6968 return (DDI_FAILURE); 6969 } 6970 6971 vds->dip = dip; 6972 vds->vd_table = mod_hash_create_ptrhash("vds_vd_table", VDS_NCHAINS, 6973 vds_destroy_vd, sizeof (void *)); 6974 6975 ASSERT(vds->vd_table != NULL); 6976 6977 if ((status = ldi_ident_from_dip(dip, &vds->ldi_ident)) != 0) { 6978 PRN("ldi_ident_from_dip() returned errno %d", status); 6979 return (DDI_FAILURE); 6980 } 6981 vds->initialized |= VDS_LDI; 6982 6983 /* Register for MD updates */ 6984 sz = sizeof (vds_prop_template); 6985 pspecp = kmem_alloc(sz, KM_SLEEP); 6986 bcopy(vds_prop_template, pspecp, sz); 6987 6988 VDS_SET_MDEG_PROP_INST(pspecp, cfg_handle); 6989 6990 /* initialize the complete prop spec structure */ 6991 ispecp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP); 6992 ispecp->namep = "virtual-device"; 6993 ispecp->specp = pspecp; 6994 6995 if (mdeg_register(ispecp, &vd_match, vds_process_md, vds, 6996 &vds->mdeg) != MDEG_SUCCESS) { 6997 PRN("Unable to register for MD updates"); 6998 kmem_free(ispecp, sizeof (mdeg_node_spec_t)); 6999 kmem_free(pspecp, sz); 7000 return (DDI_FAILURE); 7001 } 7002 7003 vds->ispecp = ispecp; 7004 vds->initialized |= VDS_MDEG; 7005 7006 /* Prevent auto-detaching so driver is available whenever MD changes */ 7007 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1) != 7008 DDI_PROP_SUCCESS) { 7009 PRN("failed to set \"%s\" property for instance %u", 7010 DDI_NO_AUTODETACH, instance); 7011 } 7012 7013 /* read any user defined driver types from conf file and update list */ 7014 vds_driver_types_update(vds); 7015 7016 ddi_report_dev(dip); 7017 return (DDI_SUCCESS); 7018 } 7019 7020 static int 7021 vds_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 7022 { 7023 int status; 7024 7025 switch (cmd) { 7026 case DDI_ATTACH: 7027 PR0("Attaching"); 7028 if ((status = vds_do_attach(dip)) != DDI_SUCCESS) 7029 (void) vds_detach(dip, DDI_DETACH); 7030 return (status); 7031 case DDI_RESUME: 7032 PR0("No action required for DDI_RESUME"); 7033 return (DDI_SUCCESS); 7034 default: 7035 return (DDI_FAILURE); 7036 } 7037 } 7038 7039 static struct dev_ops vds_ops = { 7040 DEVO_REV, /* devo_rev */ 7041 0, /* devo_refcnt */ 7042 ddi_no_info, /* devo_getinfo */ 7043 nulldev, /* devo_identify */ 7044 nulldev, /* devo_probe */ 7045 vds_attach, /* devo_attach */ 7046 vds_detach, /* devo_detach */ 7047 nodev, /* devo_reset */ 7048 NULL, /* devo_cb_ops */ 7049 NULL, /* devo_bus_ops */ 7050 nulldev /* devo_power */ 7051 }; 7052 7053 static struct modldrv modldrv = { 7054 &mod_driverops, 7055 "virtual disk server", 7056 &vds_ops, 7057 }; 7058 7059 static struct modlinkage modlinkage = { 7060 MODREV_1, 7061 &modldrv, 7062 NULL 7063 }; 7064 7065 7066 int 7067 _init(void) 7068 { 7069 int status; 7070 7071 if ((status = ddi_soft_state_init(&vds_state, sizeof (vds_t), 1)) != 0) 7072 return (status); 7073 7074 if ((status = mod_install(&modlinkage)) != 0) { 7075 ddi_soft_state_fini(&vds_state); 7076 return (status); 7077 } 7078 7079 return (0); 7080 } 7081 7082 int 7083 _info(struct modinfo *modinfop) 7084 { 7085 return (mod_info(&modlinkage, modinfop)); 7086 } 7087 7088 int 7089 _fini(void) 7090 { 7091 int status; 7092 7093 if ((status = mod_remove(&modlinkage)) != 0) 7094 return (status); 7095 ddi_soft_state_fini(&vds_state); 7096 return (0); 7097 } 7098