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