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