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