1 /*- 2 * Copyright (c) 2007 Doug Rabson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 /* 28 * Stand-alone file reading package. 29 */ 30 31 #include <stand.h> 32 #include <sys/disk.h> 33 #include <sys/param.h> 34 #include <sys/time.h> 35 #include <sys/queue.h> 36 #include <part.h> 37 #include <stddef.h> 38 #include <stdarg.h> 39 #include <string.h> 40 #include <bootstrap.h> 41 42 #include "libzfs.h" 43 44 #include "zfsimpl.c" 45 46 /* Define the range of indexes to be populated with ZFS Boot Environments */ 47 #define ZFS_BE_FIRST 4 48 #define ZFS_BE_LAST 8 49 50 static int zfs_open(const char *path, struct open_file *f); 51 static int zfs_close(struct open_file *f); 52 static int zfs_read(struct open_file *f, void *buf, size_t size, size_t *resid); 53 static off_t zfs_seek(struct open_file *f, off_t offset, int where); 54 static int zfs_stat(struct open_file *f, struct stat *sb); 55 static int zfs_readdir(struct open_file *f, struct dirent *d); 56 static int zfs_mount(const char *dev, const char *path, void **data); 57 static int zfs_unmount(const char *dev, void *data); 58 59 static void zfs_bootenv_initial(const char *envname, spa_t *spa, 60 const char *name, const char *dsname, int checkpoint); 61 static void zfs_checkpoints_initial(spa_t *spa, const char *name, 62 const char *dsname); 63 64 static int zfs_parsedev(struct devdesc **idev, const char *devspec, 65 const char **path); 66 67 struct devsw zfs_dev; 68 69 struct fs_ops zfs_fsops = { 70 .fs_name = "zfs", 71 .fo_open = zfs_open, 72 .fo_close = zfs_close, 73 .fo_read = zfs_read, 74 .fo_write = null_write, 75 .fo_seek = zfs_seek, 76 .fo_stat = zfs_stat, 77 .fo_readdir = zfs_readdir, 78 .fo_mount = zfs_mount, 79 .fo_unmount = zfs_unmount 80 }; 81 82 /* 83 * In-core open file. 84 */ 85 struct file { 86 off_t f_seekp; /* seek pointer */ 87 dnode_phys_t f_dnode; 88 uint64_t f_zap_type; /* zap type for readdir */ 89 uint64_t f_num_leafs; /* number of fzap leaf blocks */ 90 zap_leaf_phys_t *f_zap_leaf; /* zap leaf buffer */ 91 }; 92 93 static int zfs_env_index; 94 static int zfs_env_count; 95 96 SLIST_HEAD(zfs_be_list, zfs_be_entry) zfs_be_head = SLIST_HEAD_INITIALIZER(zfs_be_head); 97 struct zfs_be_list *zfs_be_headp; 98 struct zfs_be_entry { 99 char *name; 100 SLIST_ENTRY(zfs_be_entry) entries; 101 } *zfs_be, *zfs_be_tmp; 102 103 /* 104 * Open a file. 105 */ 106 static int 107 zfs_open(const char *upath, struct open_file *f) 108 { 109 struct devdesc *dev = f->f_devdata; 110 struct zfsmount *mount = dev->d_opendata; 111 struct file *fp; 112 int rc; 113 114 if (f->f_dev != &zfs_dev) 115 return (EINVAL); 116 117 /* allocate file system specific data structure */ 118 fp = calloc(1, sizeof(struct file)); 119 if (fp == NULL) 120 return (ENOMEM); 121 f->f_fsdata = fp; 122 123 rc = zfs_lookup(mount, upath, &fp->f_dnode); 124 fp->f_seekp = 0; 125 if (rc) { 126 f->f_fsdata = NULL; 127 free(fp); 128 } 129 return (rc); 130 } 131 132 static int 133 zfs_close(struct open_file *f) 134 { 135 struct file *fp = (struct file *)f->f_fsdata; 136 137 dnode_cache_obj = NULL; 138 f->f_fsdata = NULL; 139 140 free(fp); 141 return (0); 142 } 143 144 /* 145 * Copy a portion of a file into kernel memory. 146 * Cross block boundaries when necessary. 147 */ 148 static int 149 zfs_read(struct open_file *f, void *start, size_t size, size_t *resid /* out */) 150 { 151 struct devdesc *dev = f->f_devdata; 152 const spa_t *spa = ((struct zfsmount *)dev->d_opendata)->spa; 153 struct file *fp = (struct file *)f->f_fsdata; 154 struct stat sb; 155 size_t n; 156 int rc; 157 158 rc = zfs_stat(f, &sb); 159 if (rc) 160 return (rc); 161 n = size; 162 if (fp->f_seekp + n > sb.st_size) 163 n = sb.st_size - fp->f_seekp; 164 165 rc = dnode_read(spa, &fp->f_dnode, fp->f_seekp, start, n); 166 if (rc) 167 return (rc); 168 169 if (0) { 170 int i; 171 for (i = 0; i < n; i++) 172 putchar(((char*) start)[i]); 173 } 174 fp->f_seekp += n; 175 if (resid) 176 *resid = size - n; 177 178 return (0); 179 } 180 181 static off_t 182 zfs_seek(struct open_file *f, off_t offset, int where) 183 { 184 struct file *fp = (struct file *)f->f_fsdata; 185 186 switch (where) { 187 case SEEK_SET: 188 fp->f_seekp = offset; 189 break; 190 case SEEK_CUR: 191 fp->f_seekp += offset; 192 break; 193 case SEEK_END: 194 { 195 struct stat sb; 196 int error; 197 198 error = zfs_stat(f, &sb); 199 if (error != 0) { 200 errno = error; 201 return (-1); 202 } 203 fp->f_seekp = sb.st_size - offset; 204 break; 205 } 206 default: 207 errno = EINVAL; 208 return (-1); 209 } 210 return (fp->f_seekp); 211 } 212 213 static int 214 zfs_stat(struct open_file *f, struct stat *sb) 215 { 216 struct devdesc *dev = f->f_devdata; 217 const spa_t *spa = ((struct zfsmount *)dev->d_opendata)->spa; 218 struct file *fp = (struct file *)f->f_fsdata; 219 220 return (zfs_dnode_stat(spa, &fp->f_dnode, sb)); 221 } 222 223 static int 224 zfs_readdir(struct open_file *f, struct dirent *d) 225 { 226 struct devdesc *dev = f->f_devdata; 227 const spa_t *spa = ((struct zfsmount *)dev->d_opendata)->spa; 228 struct file *fp = (struct file *)f->f_fsdata; 229 mzap_ent_phys_t mze; 230 struct stat sb; 231 size_t bsize = fp->f_dnode.dn_datablkszsec << SPA_MINBLOCKSHIFT; 232 int rc; 233 234 rc = zfs_stat(f, &sb); 235 if (rc) 236 return (rc); 237 if (!S_ISDIR(sb.st_mode)) 238 return (ENOTDIR); 239 240 /* 241 * If this is the first read, get the zap type. 242 */ 243 if (fp->f_seekp == 0) { 244 rc = dnode_read(spa, &fp->f_dnode, 245 0, &fp->f_zap_type, sizeof(fp->f_zap_type)); 246 if (rc) 247 return (rc); 248 249 if (fp->f_zap_type == ZBT_MICRO) { 250 fp->f_seekp = offsetof(mzap_phys_t, mz_chunk); 251 } else { 252 rc = dnode_read(spa, &fp->f_dnode, 253 offsetof(zap_phys_t, zap_num_leafs), 254 &fp->f_num_leafs, 255 sizeof(fp->f_num_leafs)); 256 if (rc) 257 return (rc); 258 259 fp->f_seekp = bsize; 260 fp->f_zap_leaf = malloc(bsize); 261 if (fp->f_zap_leaf == NULL) 262 return (ENOMEM); 263 rc = dnode_read(spa, &fp->f_dnode, 264 fp->f_seekp, 265 fp->f_zap_leaf, 266 bsize); 267 if (rc) 268 return (rc); 269 } 270 } 271 272 if (fp->f_zap_type == ZBT_MICRO) { 273 mzap_next: 274 if (fp->f_seekp >= bsize) 275 return (ENOENT); 276 277 rc = dnode_read(spa, &fp->f_dnode, 278 fp->f_seekp, &mze, sizeof(mze)); 279 if (rc) 280 return (rc); 281 fp->f_seekp += sizeof(mze); 282 283 if (!mze.mze_name[0]) 284 goto mzap_next; 285 286 d->d_fileno = ZFS_DIRENT_OBJ(mze.mze_value); 287 d->d_type = ZFS_DIRENT_TYPE(mze.mze_value); 288 strcpy(d->d_name, mze.mze_name); 289 d->d_namlen = strlen(d->d_name); 290 return (0); 291 } else { 292 zap_leaf_t zl; 293 zap_leaf_chunk_t *zc, *nc; 294 int chunk; 295 size_t namelen; 296 char *p; 297 uint64_t value; 298 299 /* 300 * Initialise this so we can use the ZAP size 301 * calculating macros. 302 */ 303 zl.l_bs = ilog2(bsize); 304 zl.l_phys = fp->f_zap_leaf; 305 306 /* 307 * Figure out which chunk we are currently looking at 308 * and consider seeking to the next leaf. We use the 309 * low bits of f_seekp as a simple chunk index. 310 */ 311 fzap_next: 312 chunk = fp->f_seekp & (bsize - 1); 313 if (chunk == ZAP_LEAF_NUMCHUNKS(&zl)) { 314 fp->f_seekp = rounddown2(fp->f_seekp, bsize) + bsize; 315 chunk = 0; 316 317 /* 318 * Check for EOF and read the new leaf. 319 */ 320 if (fp->f_seekp >= bsize * fp->f_num_leafs) 321 return (ENOENT); 322 323 rc = dnode_read(spa, &fp->f_dnode, 324 fp->f_seekp, 325 fp->f_zap_leaf, 326 bsize); 327 if (rc) 328 return (rc); 329 } 330 331 zc = &ZAP_LEAF_CHUNK(&zl, chunk); 332 fp->f_seekp++; 333 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY) 334 goto fzap_next; 335 336 namelen = zc->l_entry.le_name_numints; 337 if (namelen > sizeof(d->d_name)) 338 namelen = sizeof(d->d_name); 339 340 /* 341 * Paste the name back together. 342 */ 343 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk); 344 p = d->d_name; 345 while (namelen > 0) { 346 int len; 347 len = namelen; 348 if (len > ZAP_LEAF_ARRAY_BYTES) 349 len = ZAP_LEAF_ARRAY_BYTES; 350 memcpy(p, nc->l_array.la_array, len); 351 p += len; 352 namelen -= len; 353 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next); 354 } 355 d->d_name[sizeof(d->d_name) - 1] = 0; 356 357 /* 358 * Assume the first eight bytes of the value are 359 * a uint64_t. 360 */ 361 value = fzap_leaf_value(&zl, zc); 362 363 d->d_fileno = ZFS_DIRENT_OBJ(value); 364 d->d_type = ZFS_DIRENT_TYPE(value); 365 d->d_namlen = strlen(d->d_name); 366 367 return (0); 368 } 369 } 370 371 static spa_t * 372 spa_find_by_dev(struct zfs_devdesc *dev) 373 { 374 375 if (dev->dd.d_dev->dv_type != DEVT_ZFS) 376 return (NULL); 377 378 if (dev->pool_guid == 0) 379 return (STAILQ_FIRST(&zfs_pools)); 380 381 return (spa_find_by_guid(dev->pool_guid)); 382 } 383 384 /* 385 * if path is NULL, create mount structure, but do not add it to list. 386 */ 387 static int 388 zfs_mount(const char *dev, const char *path, void **data) 389 { 390 struct zfs_devdesc *zfsdev = NULL; 391 spa_t *spa; 392 struct zfsmount *mnt = NULL; 393 int rv; 394 395 errno = 0; 396 rv = zfs_parsedev((struct devdesc **)&zfsdev, dev, NULL); 397 if (rv != 0) { 398 return (rv); 399 } 400 401 spa = spa_find_by_dev(zfsdev); 402 if (spa == NULL) { 403 rv = ENXIO; 404 goto err; 405 } 406 407 mnt = calloc(1, sizeof(*mnt)); 408 if (mnt == NULL) { 409 rv = ENOMEM; 410 goto err; 411 } 412 413 if (mnt->path != NULL) { 414 mnt->path = strdup(path); 415 if (mnt->path == NULL) { 416 rv = ENOMEM; 417 goto err; 418 } 419 } 420 421 rv = zfs_mount_impl(spa, zfsdev->root_guid, mnt); 422 423 if (rv == 0 && mnt->objset.os_type != DMU_OST_ZFS) { 424 printf("Unexpected object set type %ju\n", 425 (uintmax_t)mnt->objset.os_type); 426 rv = EIO; 427 } 428 err: 429 if (rv != 0) { 430 if (mnt != NULL) 431 free(mnt->path); 432 free(mnt); 433 free(zfsdev); 434 return (rv); 435 } 436 437 *data = mnt; 438 if (path != NULL) 439 STAILQ_INSERT_TAIL(&zfsmount, mnt, next); 440 441 free(zfsdev); 442 443 return (rv); 444 } 445 446 static int 447 zfs_unmount(const char *dev, void *data) 448 { 449 struct zfsmount *mnt = data; 450 451 STAILQ_REMOVE(&zfsmount, mnt, zfsmount, next); 452 free(mnt->path); 453 free(mnt); 454 return (0); 455 } 456 457 static int 458 vdev_read(vdev_t *vdev, void *priv, off_t offset, void *buf, size_t bytes) 459 { 460 int fd, ret; 461 size_t res, head, tail, total_size, full_sec_size; 462 unsigned secsz, do_tail_read; 463 off_t start_sec; 464 char *outbuf, *bouncebuf; 465 466 fd = (uintptr_t) priv; 467 outbuf = (char *) buf; 468 bouncebuf = NULL; 469 470 ret = ioctl(fd, DIOCGSECTORSIZE, &secsz); 471 if (ret != 0) 472 return (ret); 473 474 /* 475 * Handling reads of arbitrary offset and size - multi-sector case 476 * and single-sector case. 477 * 478 * Multi-sector Case 479 * (do_tail_read = true if tail > 0) 480 * 481 * |<----------------------total_size--------------------->| 482 * | | 483 * |<--head-->|<--------------bytes------------>|<--tail-->| 484 * | | | | 485 * | | |<~full_sec_size~>| | | 486 * +------------------+ +------------------+ 487 * | |0101010| . . . |0101011| | 488 * +------------------+ +------------------+ 489 * start_sec start_sec + n 490 * 491 * 492 * Single-sector Case 493 * (do_tail_read = false) 494 * 495 * |<------total_size = secsz----->| 496 * | | 497 * |<-head->|<---bytes--->|<-tail->| 498 * +-------------------------------+ 499 * | |0101010101010| | 500 * +-------------------------------+ 501 * start_sec 502 */ 503 start_sec = offset / secsz; 504 head = offset % secsz; 505 total_size = roundup2(head + bytes, secsz); 506 tail = total_size - (head + bytes); 507 do_tail_read = ((tail > 0) && (head + bytes > secsz)); 508 full_sec_size = total_size; 509 if (head > 0) 510 full_sec_size -= secsz; 511 if (do_tail_read) 512 full_sec_size -= secsz; 513 514 /* Return of partial sector data requires a bounce buffer. */ 515 if ((head > 0) || do_tail_read || bytes < secsz) { 516 bouncebuf = malloc(secsz); 517 if (bouncebuf == NULL) { 518 printf("vdev_read: out of memory\n"); 519 return (ENOMEM); 520 } 521 } 522 523 if (lseek(fd, start_sec * secsz, SEEK_SET) == -1) { 524 ret = errno; 525 goto error; 526 } 527 528 /* Partial data return from first sector */ 529 if (head > 0) { 530 res = read(fd, bouncebuf, secsz); 531 if (res != secsz) { 532 ret = EIO; 533 goto error; 534 } 535 memcpy(outbuf, bouncebuf + head, min(secsz - head, bytes)); 536 outbuf += min(secsz - head, bytes); 537 } 538 539 /* 540 * Full data return from read sectors. 541 * Note, there is still corner case where we read 542 * from sector boundary, but less than sector size, e.g. reading 512B 543 * from 4k sector. 544 */ 545 if (full_sec_size > 0) { 546 if (bytes < full_sec_size) { 547 res = read(fd, bouncebuf, secsz); 548 if (res != secsz) { 549 ret = EIO; 550 goto error; 551 } 552 memcpy(outbuf, bouncebuf, bytes); 553 } else { 554 res = read(fd, outbuf, full_sec_size); 555 if (res != full_sec_size) { 556 ret = EIO; 557 goto error; 558 } 559 outbuf += full_sec_size; 560 } 561 } 562 563 /* Partial data return from last sector */ 564 if (do_tail_read) { 565 res = read(fd, bouncebuf, secsz); 566 if (res != secsz) { 567 ret = EIO; 568 goto error; 569 } 570 memcpy(outbuf, bouncebuf, secsz - tail); 571 } 572 573 ret = 0; 574 error: 575 free(bouncebuf); 576 return (ret); 577 } 578 579 static int 580 vdev_write(vdev_t *vdev, off_t offset, void *buf, size_t bytes) 581 { 582 int fd, ret; 583 size_t head, tail, total_size, full_sec_size; 584 unsigned secsz, do_tail_write; 585 off_t start_sec; 586 ssize_t res; 587 char *outbuf, *bouncebuf; 588 589 fd = (uintptr_t)vdev->v_priv; 590 outbuf = (char *)buf; 591 bouncebuf = NULL; 592 593 ret = ioctl(fd, DIOCGSECTORSIZE, &secsz); 594 if (ret != 0) 595 return (ret); 596 597 start_sec = offset / secsz; 598 head = offset % secsz; 599 total_size = roundup2(head + bytes, secsz); 600 tail = total_size - (head + bytes); 601 do_tail_write = ((tail > 0) && (head + bytes > secsz)); 602 full_sec_size = total_size; 603 if (head > 0) 604 full_sec_size -= secsz; 605 if (do_tail_write) 606 full_sec_size -= secsz; 607 608 /* Partial sector write requires a bounce buffer. */ 609 if ((head > 0) || do_tail_write || bytes < secsz) { 610 bouncebuf = malloc(secsz); 611 if (bouncebuf == NULL) { 612 printf("vdev_write: out of memory\n"); 613 return (ENOMEM); 614 } 615 } 616 617 if (lseek(fd, start_sec * secsz, SEEK_SET) == -1) { 618 ret = errno; 619 goto error; 620 } 621 622 /* Partial data for first sector */ 623 if (head > 0) { 624 res = read(fd, bouncebuf, secsz); 625 if ((unsigned)res != secsz) { 626 ret = EIO; 627 goto error; 628 } 629 memcpy(bouncebuf + head, outbuf, min(secsz - head, bytes)); 630 (void) lseek(fd, -secsz, SEEK_CUR); 631 res = write(fd, bouncebuf, secsz); 632 if ((unsigned)res != secsz) { 633 ret = EIO; 634 goto error; 635 } 636 outbuf += min(secsz - head, bytes); 637 } 638 639 /* 640 * Full data write to sectors. 641 * Note, there is still corner case where we write 642 * to sector boundary, but less than sector size, e.g. write 512B 643 * to 4k sector. 644 */ 645 if (full_sec_size > 0) { 646 if (bytes < full_sec_size) { 647 res = read(fd, bouncebuf, secsz); 648 if ((unsigned)res != secsz) { 649 ret = EIO; 650 goto error; 651 } 652 memcpy(bouncebuf, outbuf, bytes); 653 (void) lseek(fd, -secsz, SEEK_CUR); 654 res = write(fd, bouncebuf, secsz); 655 if ((unsigned)res != secsz) { 656 ret = EIO; 657 goto error; 658 } 659 } else { 660 res = write(fd, outbuf, full_sec_size); 661 if ((unsigned)res != full_sec_size) { 662 ret = EIO; 663 goto error; 664 } 665 outbuf += full_sec_size; 666 } 667 } 668 669 /* Partial data write to last sector */ 670 if (do_tail_write) { 671 res = read(fd, bouncebuf, secsz); 672 if ((unsigned)res != secsz) { 673 ret = EIO; 674 goto error; 675 } 676 memcpy(bouncebuf, outbuf, secsz - tail); 677 (void) lseek(fd, -secsz, SEEK_CUR); 678 res = write(fd, bouncebuf, secsz); 679 if ((unsigned)res != secsz) { 680 ret = EIO; 681 goto error; 682 } 683 } 684 685 ret = 0; 686 error: 687 free(bouncebuf); 688 return (ret); 689 } 690 691 static int 692 zfs_dev_init(void) 693 { 694 spa_t *spa; 695 spa_t *next; 696 spa_t *prev; 697 698 zfs_init(); 699 if (archsw.arch_zfs_probe == NULL) 700 return (ENXIO); 701 archsw.arch_zfs_probe(); 702 703 prev = NULL; 704 spa = STAILQ_FIRST(&zfs_pools); 705 while (spa != NULL) { 706 next = STAILQ_NEXT(spa, spa_link); 707 if (zfs_spa_init(spa)) { 708 if (prev == NULL) 709 STAILQ_REMOVE_HEAD(&zfs_pools, spa_link); 710 else 711 STAILQ_REMOVE_AFTER(&zfs_pools, prev, spa_link); 712 } else 713 prev = spa; 714 spa = next; 715 } 716 return (0); 717 } 718 719 struct zfs_probe_args { 720 int fd; 721 const char *devname; 722 uint64_t *pool_guid; 723 u_int secsz; 724 }; 725 726 static int 727 zfs_diskread(void *arg, void *buf, size_t blocks, uint64_t offset) 728 { 729 struct zfs_probe_args *ppa; 730 731 ppa = (struct zfs_probe_args *)arg; 732 return (vdev_read(NULL, (void *)(uintptr_t)ppa->fd, 733 offset * ppa->secsz, buf, blocks * ppa->secsz)); 734 } 735 736 static int 737 zfs_probe(int fd, uint64_t *pool_guid) 738 { 739 spa_t *spa; 740 int ret; 741 742 spa = NULL; 743 ret = vdev_probe(vdev_read, vdev_write, (void *)(uintptr_t)fd, &spa); 744 if (ret == 0 && pool_guid != NULL) 745 if (*pool_guid == 0) 746 *pool_guid = spa->spa_guid; 747 return (ret); 748 } 749 750 static int 751 zfs_probe_partition(void *arg, const char *partname, 752 const struct ptable_entry *part) 753 { 754 struct zfs_probe_args *ppa, pa; 755 struct ptable *table; 756 char devname[32]; 757 int ret; 758 759 /* Probe only freebsd-zfs and freebsd partitions */ 760 if (part->type != PART_FREEBSD && 761 part->type != PART_FREEBSD_ZFS) 762 return (0); 763 764 ppa = (struct zfs_probe_args *)arg; 765 strncpy(devname, ppa->devname, strlen(ppa->devname) - 1); 766 devname[strlen(ppa->devname) - 1] = '\0'; 767 snprintf(devname, sizeof(devname), "%s%s:", devname, partname); 768 pa.fd = open(devname, O_RDWR); 769 if (pa.fd == -1) 770 return (0); 771 ret = zfs_probe(pa.fd, ppa->pool_guid); 772 if (ret == 0) 773 return (0); 774 /* Do we have BSD label here? */ 775 if (part->type == PART_FREEBSD) { 776 pa.devname = devname; 777 pa.pool_guid = ppa->pool_guid; 778 pa.secsz = ppa->secsz; 779 table = ptable_open(&pa, part->end - part->start + 1, 780 ppa->secsz, zfs_diskread); 781 if (table != NULL) { 782 ptable_iterate(table, &pa, zfs_probe_partition); 783 ptable_close(table); 784 } 785 } 786 close(pa.fd); 787 return (0); 788 } 789 790 /* 791 * Return bootenv nvlist from pool label. 792 */ 793 int 794 zfs_get_bootenv(void *vdev, nvlist_t **benvp) 795 { 796 spa_t *spa; 797 798 if ((spa = spa_find_by_dev((struct zfs_devdesc *)vdev)) == NULL) 799 return (ENXIO); 800 801 return (zfs_get_bootenv_spa(spa, benvp)); 802 } 803 804 /* 805 * Store nvlist to pool label bootenv area. Also updates cached pointer in spa. 806 */ 807 int 808 zfs_set_bootenv(void *vdev, nvlist_t *benv) 809 { 810 spa_t *spa; 811 812 if ((spa = spa_find_by_dev((struct zfs_devdesc *)vdev)) == NULL) 813 return (ENXIO); 814 815 return (zfs_set_bootenv_spa(spa, benv)); 816 } 817 818 /* 819 * Get bootonce value by key. The bootonce <key, value> pair is removed 820 * from the bootenv nvlist and the remaining nvlist is committed back to disk. 821 */ 822 int 823 zfs_get_bootonce(void *vdev, const char *key, char *buf, size_t size) 824 { 825 spa_t *spa; 826 827 if ((spa = spa_find_by_dev((struct zfs_devdesc *)vdev)) == NULL) 828 return (ENXIO); 829 830 return (zfs_get_bootonce_spa(spa, key, buf, size)); 831 } 832 833 /* 834 * nvstore backend. 835 */ 836 837 static int zfs_nvstore_setter(void *, int, const char *, 838 const void *, size_t); 839 static int zfs_nvstore_setter_str(void *, const char *, const char *, 840 const char *); 841 static int zfs_nvstore_unset_impl(void *, const char *, bool); 842 static int zfs_nvstore_setenv(void *, void *); 843 844 /* 845 * nvstore is only present for current rootfs pool. 846 */ 847 static int 848 zfs_nvstore_sethook(struct env_var *ev, int flags __unused, const void *value) 849 { 850 struct zfs_devdesc *dev; 851 int rv; 852 853 archsw.arch_getdev((void **)&dev, NULL, NULL); 854 if (dev == NULL) 855 return (ENXIO); 856 857 rv = zfs_nvstore_setter_str(dev, NULL, ev->ev_name, value); 858 859 free(dev); 860 return (rv); 861 } 862 863 /* 864 * nvstore is only present for current rootfs pool. 865 */ 866 static int 867 zfs_nvstore_unsethook(struct env_var *ev) 868 { 869 struct zfs_devdesc *dev; 870 int rv; 871 872 archsw.arch_getdev((void **)&dev, NULL, NULL); 873 if (dev == NULL) 874 return (ENXIO); 875 876 rv = zfs_nvstore_unset_impl(dev, ev->ev_name, false); 877 878 free(dev); 879 return (rv); 880 } 881 882 static int 883 zfs_nvstore_getter(void *vdev, const char *name, void **data) 884 { 885 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev; 886 spa_t *spa; 887 nvlist_t *nv; 888 char *str, **ptr; 889 int size; 890 int rv; 891 892 if (dev->dd.d_dev->dv_type != DEVT_ZFS) 893 return (ENOTSUP); 894 895 if ((spa = spa_find_by_dev(dev)) == NULL) 896 return (ENXIO); 897 898 if (spa->spa_bootenv == NULL) 899 return (ENXIO); 900 901 if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST, 902 NULL, &nv, NULL) != 0) 903 return (ENOENT); 904 905 rv = nvlist_find(nv, name, DATA_TYPE_STRING, NULL, &str, &size); 906 if (rv == 0) { 907 ptr = (char **)data; 908 asprintf(ptr, "%.*s", size, str); 909 if (*data == NULL) 910 rv = ENOMEM; 911 } 912 nvlist_destroy(nv); 913 return (rv); 914 } 915 916 static int 917 zfs_nvstore_setter(void *vdev, int type, const char *name, 918 const void *data, size_t size) 919 { 920 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev; 921 spa_t *spa; 922 nvlist_t *nv; 923 int rv; 924 bool env_set = true; 925 926 if (dev->dd.d_dev->dv_type != DEVT_ZFS) 927 return (ENOTSUP); 928 929 if ((spa = spa_find_by_dev(dev)) == NULL) 930 return (ENXIO); 931 932 if (spa->spa_bootenv == NULL) 933 return (ENXIO); 934 935 if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST, 936 NULL, &nv, NULL) != 0) { 937 nv = nvlist_create(NV_UNIQUE_NAME); 938 if (nv == NULL) 939 return (ENOMEM); 940 } 941 942 rv = 0; 943 switch (type) { 944 case DATA_TYPE_INT8: 945 if (size != sizeof (int8_t)) { 946 rv = EINVAL; 947 break; 948 } 949 rv = nvlist_add_int8(nv, name, *(int8_t *)data); 950 break; 951 952 case DATA_TYPE_INT16: 953 if (size != sizeof (int16_t)) { 954 rv = EINVAL; 955 break; 956 } 957 rv = nvlist_add_int16(nv, name, *(int16_t *)data); 958 break; 959 960 case DATA_TYPE_INT32: 961 if (size != sizeof (int32_t)) { 962 rv = EINVAL; 963 break; 964 } 965 rv = nvlist_add_int32(nv, name, *(int32_t *)data); 966 break; 967 968 case DATA_TYPE_INT64: 969 if (size != sizeof (int64_t)) { 970 rv = EINVAL; 971 break; 972 } 973 rv = nvlist_add_int64(nv, name, *(int64_t *)data); 974 break; 975 976 case DATA_TYPE_BYTE: 977 if (size != sizeof (uint8_t)) { 978 rv = EINVAL; 979 break; 980 } 981 rv = nvlist_add_byte(nv, name, *(int8_t *)data); 982 break; 983 984 case DATA_TYPE_UINT8: 985 if (size != sizeof (uint8_t)) { 986 rv = EINVAL; 987 break; 988 } 989 rv = nvlist_add_uint8(nv, name, *(int8_t *)data); 990 break; 991 992 case DATA_TYPE_UINT16: 993 if (size != sizeof (uint16_t)) { 994 rv = EINVAL; 995 break; 996 } 997 rv = nvlist_add_uint16(nv, name, *(uint16_t *)data); 998 break; 999 1000 case DATA_TYPE_UINT32: 1001 if (size != sizeof (uint32_t)) { 1002 rv = EINVAL; 1003 break; 1004 } 1005 rv = nvlist_add_uint32(nv, name, *(uint32_t *)data); 1006 break; 1007 1008 case DATA_TYPE_UINT64: 1009 if (size != sizeof (uint64_t)) { 1010 rv = EINVAL; 1011 break; 1012 } 1013 rv = nvlist_add_uint64(nv, name, *(uint64_t *)data); 1014 break; 1015 1016 case DATA_TYPE_STRING: 1017 rv = nvlist_add_string(nv, name, data); 1018 break; 1019 1020 case DATA_TYPE_BOOLEAN_VALUE: 1021 if (size != sizeof (boolean_t)) { 1022 rv = EINVAL; 1023 break; 1024 } 1025 rv = nvlist_add_boolean_value(nv, name, *(boolean_t *)data); 1026 break; 1027 1028 default: 1029 rv = EINVAL; 1030 break; 1031 } 1032 1033 if (rv == 0) { 1034 rv = nvlist_add_nvlist(spa->spa_bootenv, OS_NVSTORE, nv); 1035 if (rv == 0) { 1036 rv = zfs_set_bootenv(vdev, spa->spa_bootenv); 1037 } 1038 if (rv == 0) { 1039 if (env_set) { 1040 rv = zfs_nvstore_setenv(vdev, 1041 nvpair_find(nv, name)); 1042 } else { 1043 env_discard(env_getenv(name)); 1044 rv = 0; 1045 } 1046 } 1047 } 1048 1049 nvlist_destroy(nv); 1050 return (rv); 1051 } 1052 1053 static int 1054 get_int64(const char *data, int64_t *ip) 1055 { 1056 char *end; 1057 int64_t val; 1058 1059 errno = 0; 1060 val = strtoll(data, &end, 0); 1061 if (errno != 0 || *data == '\0' || *end != '\0') 1062 return (EINVAL); 1063 1064 *ip = val; 1065 return (0); 1066 } 1067 1068 static int 1069 get_uint64(const char *data, uint64_t *ip) 1070 { 1071 char *end; 1072 uint64_t val; 1073 1074 errno = 0; 1075 val = strtoull(data, &end, 0); 1076 if (errno != 0 || *data == '\0' || *end != '\0') 1077 return (EINVAL); 1078 1079 *ip = val; 1080 return (0); 1081 } 1082 1083 /* 1084 * Translate textual data to data type. If type is not set, and we are 1085 * creating new pair, use DATA_TYPE_STRING. 1086 */ 1087 static int 1088 zfs_nvstore_setter_str(void *vdev, const char *type, const char *name, 1089 const char *data) 1090 { 1091 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev; 1092 spa_t *spa; 1093 nvlist_t *nv; 1094 int rv; 1095 data_type_t dt; 1096 int64_t val; 1097 uint64_t uval; 1098 1099 if (dev->dd.d_dev->dv_type != DEVT_ZFS) 1100 return (ENOTSUP); 1101 1102 if ((spa = spa_find_by_dev(dev)) == NULL) 1103 return (ENXIO); 1104 1105 if (spa->spa_bootenv == NULL) 1106 return (ENXIO); 1107 1108 if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST, 1109 NULL, &nv, NULL) != 0) { 1110 nv = NULL; 1111 } 1112 1113 if (type == NULL) { 1114 nvp_header_t *nvh; 1115 1116 /* 1117 * if there is no existing pair, default to string. 1118 * Otherwise, use type from existing pair. 1119 */ 1120 nvh = nvpair_find(nv, name); 1121 if (nvh == NULL) { 1122 dt = DATA_TYPE_STRING; 1123 } else { 1124 nv_string_t *nvp_name; 1125 nv_pair_data_t *nvp_data; 1126 1127 nvp_name = (nv_string_t *)(nvh + 1); 1128 nvp_data = (nv_pair_data_t *)(&nvp_name->nv_data[0] + 1129 NV_ALIGN4(nvp_name->nv_size)); 1130 dt = nvp_data->nv_type; 1131 } 1132 } else { 1133 dt = nvpair_type_from_name(type); 1134 } 1135 nvlist_destroy(nv); 1136 1137 rv = 0; 1138 switch (dt) { 1139 case DATA_TYPE_INT8: 1140 rv = get_int64(data, &val); 1141 if (rv == 0) { 1142 int8_t v = val; 1143 1144 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v)); 1145 } 1146 break; 1147 case DATA_TYPE_INT16: 1148 rv = get_int64(data, &val); 1149 if (rv == 0) { 1150 int16_t v = val; 1151 1152 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v)); 1153 } 1154 break; 1155 case DATA_TYPE_INT32: 1156 rv = get_int64(data, &val); 1157 if (rv == 0) { 1158 int32_t v = val; 1159 1160 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v)); 1161 } 1162 break; 1163 case DATA_TYPE_INT64: 1164 rv = get_int64(data, &val); 1165 if (rv == 0) { 1166 rv = zfs_nvstore_setter(vdev, dt, name, &val, 1167 sizeof (val)); 1168 } 1169 break; 1170 1171 case DATA_TYPE_BYTE: 1172 rv = get_uint64(data, &uval); 1173 if (rv == 0) { 1174 uint8_t v = uval; 1175 1176 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v)); 1177 } 1178 break; 1179 1180 case DATA_TYPE_UINT8: 1181 rv = get_uint64(data, &uval); 1182 if (rv == 0) { 1183 uint8_t v = uval; 1184 1185 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v)); 1186 } 1187 break; 1188 1189 case DATA_TYPE_UINT16: 1190 rv = get_uint64(data, &uval); 1191 if (rv == 0) { 1192 uint16_t v = uval; 1193 1194 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v)); 1195 } 1196 break; 1197 1198 case DATA_TYPE_UINT32: 1199 rv = get_uint64(data, &uval); 1200 if (rv == 0) { 1201 uint32_t v = uval; 1202 1203 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v)); 1204 } 1205 break; 1206 1207 case DATA_TYPE_UINT64: 1208 rv = get_uint64(data, &uval); 1209 if (rv == 0) { 1210 rv = zfs_nvstore_setter(vdev, dt, name, &uval, 1211 sizeof (uval)); 1212 } 1213 break; 1214 1215 case DATA_TYPE_STRING: 1216 rv = zfs_nvstore_setter(vdev, dt, name, data, strlen(data) + 1); 1217 break; 1218 1219 case DATA_TYPE_BOOLEAN_VALUE: 1220 rv = get_int64(data, &val); 1221 if (rv == 0) { 1222 boolean_t v = val; 1223 1224 rv = zfs_nvstore_setter(vdev, dt, name, &v, sizeof (v)); 1225 } 1226 1227 default: 1228 rv = EINVAL; 1229 } 1230 return (rv); 1231 } 1232 1233 static int 1234 zfs_nvstore_unset_impl(void *vdev, const char *name, bool unset_env) 1235 { 1236 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev; 1237 spa_t *spa; 1238 nvlist_t *nv; 1239 int rv; 1240 1241 if (dev->dd.d_dev->dv_type != DEVT_ZFS) 1242 return (ENOTSUP); 1243 1244 if ((spa = spa_find_by_dev(dev)) == NULL) 1245 return (ENXIO); 1246 1247 if (spa->spa_bootenv == NULL) 1248 return (ENXIO); 1249 1250 if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST, 1251 NULL, &nv, NULL) != 0) 1252 return (ENOENT); 1253 1254 rv = nvlist_remove(nv, name, DATA_TYPE_UNKNOWN); 1255 if (rv == 0) { 1256 if (nvlist_next_nvpair(nv, NULL) == NULL) { 1257 rv = nvlist_remove(spa->spa_bootenv, OS_NVSTORE, 1258 DATA_TYPE_NVLIST); 1259 } else { 1260 rv = nvlist_add_nvlist(spa->spa_bootenv, 1261 OS_NVSTORE, nv); 1262 } 1263 if (rv == 0) 1264 rv = zfs_set_bootenv(vdev, spa->spa_bootenv); 1265 } 1266 1267 if (unset_env) 1268 env_discard(env_getenv(name)); 1269 return (rv); 1270 } 1271 1272 static int 1273 zfs_nvstore_unset(void *vdev, const char *name) 1274 { 1275 return (zfs_nvstore_unset_impl(vdev, name, true)); 1276 } 1277 1278 static int 1279 zfs_nvstore_print(void *vdev __unused, void *ptr) 1280 { 1281 1282 nvpair_print(ptr, 0); 1283 return (0); 1284 } 1285 1286 /* 1287 * Create environment variable from nvpair. 1288 * set hook will update nvstore with new value, unset hook will remove 1289 * variable from nvstore. 1290 */ 1291 static int 1292 zfs_nvstore_setenv(void *vdev __unused, void *ptr) 1293 { 1294 nvp_header_t *nvh = ptr; 1295 nv_string_t *nvp_name, *nvp_value; 1296 nv_pair_data_t *nvp_data; 1297 char *name, *value; 1298 int rv = 0; 1299 1300 if (nvh == NULL) 1301 return (ENOENT); 1302 1303 nvp_name = (nv_string_t *)(nvh + 1); 1304 nvp_data = (nv_pair_data_t *)(&nvp_name->nv_data[0] + 1305 NV_ALIGN4(nvp_name->nv_size)); 1306 1307 if ((name = nvstring_get(nvp_name)) == NULL) 1308 return (ENOMEM); 1309 1310 value = NULL; 1311 switch (nvp_data->nv_type) { 1312 case DATA_TYPE_BYTE: 1313 case DATA_TYPE_UINT8: 1314 (void) asprintf(&value, "%uc", 1315 *(unsigned *)&nvp_data->nv_data[0]); 1316 if (value == NULL) 1317 rv = ENOMEM; 1318 break; 1319 1320 case DATA_TYPE_INT8: 1321 (void) asprintf(&value, "%c", *(int *)&nvp_data->nv_data[0]); 1322 if (value == NULL) 1323 rv = ENOMEM; 1324 break; 1325 1326 case DATA_TYPE_INT16: 1327 (void) asprintf(&value, "%hd", *(short *)&nvp_data->nv_data[0]); 1328 if (value == NULL) 1329 rv = ENOMEM; 1330 break; 1331 1332 case DATA_TYPE_UINT16: 1333 (void) asprintf(&value, "%hu", 1334 *(unsigned short *)&nvp_data->nv_data[0]); 1335 if (value == NULL) 1336 rv = ENOMEM; 1337 break; 1338 1339 case DATA_TYPE_BOOLEAN_VALUE: 1340 case DATA_TYPE_INT32: 1341 (void) asprintf(&value, "%d", *(int *)&nvp_data->nv_data[0]); 1342 if (value == NULL) 1343 rv = ENOMEM; 1344 break; 1345 1346 case DATA_TYPE_UINT32: 1347 (void) asprintf(&value, "%u", 1348 *(unsigned *)&nvp_data->nv_data[0]); 1349 if (value == NULL) 1350 rv = ENOMEM; 1351 break; 1352 1353 case DATA_TYPE_INT64: 1354 (void) asprintf(&value, "%jd", 1355 (intmax_t)*(int64_t *)&nvp_data->nv_data[0]); 1356 if (value == NULL) 1357 rv = ENOMEM; 1358 break; 1359 1360 case DATA_TYPE_UINT64: 1361 (void) asprintf(&value, "%ju", 1362 (uintmax_t)*(uint64_t *)&nvp_data->nv_data[0]); 1363 if (value == NULL) 1364 rv = ENOMEM; 1365 break; 1366 1367 case DATA_TYPE_STRING: 1368 nvp_value = (nv_string_t *)&nvp_data->nv_data[0]; 1369 if ((value = nvstring_get(nvp_value)) == NULL) { 1370 rv = ENOMEM; 1371 break; 1372 } 1373 break; 1374 1375 default: 1376 rv = EINVAL; 1377 break; 1378 } 1379 1380 if (value != NULL) { 1381 rv = env_setenv(name, EV_VOLATILE | EV_NOHOOK, value, 1382 zfs_nvstore_sethook, zfs_nvstore_unsethook); 1383 free(value); 1384 } 1385 free(name); 1386 return (rv); 1387 } 1388 1389 static int 1390 zfs_nvstore_iterate(void *vdev, int (*cb)(void *, void *)) 1391 { 1392 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev; 1393 spa_t *spa; 1394 nvlist_t *nv; 1395 nvp_header_t *nvh; 1396 int rv; 1397 1398 if (dev->dd.d_dev->dv_type != DEVT_ZFS) 1399 return (ENOTSUP); 1400 1401 if ((spa = spa_find_by_dev(dev)) == NULL) 1402 return (ENXIO); 1403 1404 if (spa->spa_bootenv == NULL) 1405 return (ENXIO); 1406 1407 if (nvlist_find(spa->spa_bootenv, OS_NVSTORE, DATA_TYPE_NVLIST, 1408 NULL, &nv, NULL) != 0) 1409 return (ENOENT); 1410 1411 rv = 0; 1412 nvh = NULL; 1413 while ((nvh = nvlist_next_nvpair(nv, nvh)) != NULL) { 1414 rv = cb(vdev, nvh); 1415 if (rv != 0) 1416 break; 1417 } 1418 return (rv); 1419 } 1420 1421 nvs_callbacks_t nvstore_zfs_cb = { 1422 .nvs_getter = zfs_nvstore_getter, 1423 .nvs_setter = zfs_nvstore_setter, 1424 .nvs_setter_str = zfs_nvstore_setter_str, 1425 .nvs_unset = zfs_nvstore_unset, 1426 .nvs_print = zfs_nvstore_print, 1427 .nvs_iterate = zfs_nvstore_iterate 1428 }; 1429 1430 int 1431 zfs_attach_nvstore(void *vdev) 1432 { 1433 struct zfs_devdesc *dev = vdev; 1434 spa_t *spa; 1435 uint64_t version; 1436 int rv; 1437 1438 if (dev->dd.d_dev->dv_type != DEVT_ZFS) 1439 return (ENOTSUP); 1440 1441 if ((spa = spa_find_by_dev(dev)) == NULL) 1442 return (ENXIO); 1443 1444 rv = nvlist_find(spa->spa_bootenv, BOOTENV_VERSION, DATA_TYPE_UINT64, 1445 NULL, &version, NULL); 1446 1447 if (rv != 0 || version != VB_NVLIST) { 1448 return (ENXIO); 1449 } 1450 1451 dev = malloc(sizeof (*dev)); 1452 if (dev == NULL) 1453 return (ENOMEM); 1454 memcpy(dev, vdev, sizeof (*dev)); 1455 1456 rv = nvstore_init(spa->spa_name, &nvstore_zfs_cb, dev); 1457 if (rv != 0) 1458 free(dev); 1459 else 1460 rv = zfs_nvstore_iterate(dev, zfs_nvstore_setenv); 1461 return (rv); 1462 } 1463 1464 int 1465 zfs_probe_dev(const char *devname, uint64_t *pool_guid, bool parts_too) 1466 { 1467 struct ptable *table; 1468 struct zfs_probe_args pa; 1469 uint64_t mediasz; 1470 int ret; 1471 1472 if (pool_guid) 1473 *pool_guid = 0; 1474 pa.fd = open(devname, O_RDWR); 1475 if (pa.fd == -1) 1476 return (ENXIO); 1477 /* Probe the whole disk */ 1478 ret = zfs_probe(pa.fd, pool_guid); 1479 if (ret == 0) 1480 return (0); 1481 if (!parts_too) 1482 return (ENXIO); 1483 1484 /* Probe each partition */ 1485 ret = ioctl(pa.fd, DIOCGMEDIASIZE, &mediasz); 1486 if (ret == 0) 1487 ret = ioctl(pa.fd, DIOCGSECTORSIZE, &pa.secsz); 1488 if (ret == 0) { 1489 pa.devname = devname; 1490 pa.pool_guid = pool_guid; 1491 table = ptable_open(&pa, mediasz / pa.secsz, pa.secsz, 1492 zfs_diskread); 1493 if (table != NULL) { 1494 ptable_iterate(table, &pa, zfs_probe_partition); 1495 ptable_close(table); 1496 } 1497 } 1498 close(pa.fd); 1499 if (pool_guid && *pool_guid == 0) 1500 ret = ENXIO; 1501 return (ret); 1502 } 1503 1504 /* 1505 * Print information about ZFS pools 1506 */ 1507 static int 1508 zfs_dev_print(int verbose) 1509 { 1510 spa_t *spa; 1511 char line[80]; 1512 int ret = 0; 1513 1514 if (STAILQ_EMPTY(&zfs_pools)) 1515 return (0); 1516 1517 printf("%s devices:", zfs_dev.dv_name); 1518 if ((ret = pager_output("\n")) != 0) 1519 return (ret); 1520 1521 if (verbose) { 1522 return (spa_all_status()); 1523 } 1524 STAILQ_FOREACH(spa, &zfs_pools, spa_link) { 1525 snprintf(line, sizeof(line), " zfs:%s\n", spa->spa_name); 1526 ret = pager_output(line); 1527 if (ret != 0) 1528 break; 1529 } 1530 return (ret); 1531 } 1532 1533 /* 1534 * Attempt to open the pool described by (dev) for use by (f). 1535 */ 1536 static int 1537 zfs_dev_open(struct open_file *f, ...) 1538 { 1539 va_list args; 1540 struct zfs_devdesc *dev; 1541 struct zfsmount *mount; 1542 spa_t *spa; 1543 int rv; 1544 1545 va_start(args, f); 1546 dev = va_arg(args, struct zfs_devdesc *); 1547 va_end(args); 1548 1549 if ((spa = spa_find_by_dev(dev)) == NULL) 1550 return (ENXIO); 1551 1552 STAILQ_FOREACH(mount, &zfsmount, next) { 1553 if (spa->spa_guid == mount->spa->spa_guid) 1554 break; 1555 } 1556 1557 rv = 0; 1558 /* This device is not set as currdev, mount us private copy. */ 1559 if (mount == NULL) 1560 rv = zfs_mount(devformat(&dev->dd), NULL, (void **)&mount); 1561 1562 if (rv == 0) { 1563 dev->dd.d_opendata = mount; 1564 } 1565 return (rv); 1566 } 1567 1568 static int 1569 zfs_dev_close(struct open_file *f) 1570 { 1571 struct devdesc *dev; 1572 struct zfsmount *mnt, *mount; 1573 1574 dev = f->f_devdata; 1575 mnt = dev->d_opendata; 1576 1577 STAILQ_FOREACH(mount, &zfsmount, next) { 1578 if (mnt->spa->spa_guid == mount->spa->spa_guid) 1579 break; 1580 } 1581 1582 /* XXX */ 1583 return (0); 1584 } 1585 1586 static int 1587 zfs_dev_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize) 1588 { 1589 1590 return (ENOSYS); 1591 } 1592 1593 struct devsw zfs_dev = { 1594 .dv_name = "zfs", 1595 .dv_type = DEVT_ZFS, 1596 .dv_init = zfs_dev_init, 1597 .dv_strategy = zfs_dev_strategy, 1598 .dv_open = zfs_dev_open, 1599 .dv_close = zfs_dev_close, 1600 .dv_ioctl = noioctl, 1601 .dv_print = zfs_dev_print, 1602 .dv_cleanup = nullsys, 1603 .dv_fmtdev = zfs_fmtdev, 1604 .dv_parsedev = zfs_parsedev, 1605 }; 1606 1607 static int 1608 zfs_parsedev(struct devdesc **idev, const char *devspec, const char **path) 1609 { 1610 static char rootname[ZFS_MAXNAMELEN]; 1611 static char poolname[ZFS_MAXNAMELEN]; 1612 spa_t *spa; 1613 const char *end; 1614 const char *np; 1615 const char *sep; 1616 int rv; 1617 struct zfs_devdesc *dev; 1618 1619 np = devspec + 3; /* Skip the leading 'zfs' */ 1620 if (*np != ':') 1621 return (EINVAL); 1622 np++; 1623 end = strrchr(np, ':'); 1624 if (end == NULL) 1625 return (EINVAL); 1626 sep = strchr(np, '/'); 1627 if (sep == NULL || sep >= end) 1628 sep = end; 1629 memcpy(poolname, np, sep - np); 1630 poolname[sep - np] = '\0'; 1631 if (sep < end) { 1632 sep++; 1633 memcpy(rootname, sep, end - sep); 1634 rootname[end - sep] = '\0'; 1635 } 1636 else 1637 rootname[0] = '\0'; 1638 1639 spa = spa_find_by_name(poolname); 1640 if (!spa) 1641 return (ENXIO); 1642 dev = malloc(sizeof(*dev)); 1643 if (dev == NULL) 1644 return (ENOMEM); 1645 dev->pool_guid = spa->spa_guid; 1646 rv = zfs_lookup_dataset(spa, rootname, &dev->root_guid); 1647 if (rv != 0) { 1648 free(dev); 1649 return (rv); 1650 } 1651 if (path != NULL) 1652 *path = (*end == '\0') ? end : end + 1; 1653 dev->dd.d_dev = &zfs_dev; 1654 *idev = &dev->dd; 1655 return (0); 1656 } 1657 1658 char * 1659 zfs_fmtdev(struct devdesc *vdev) 1660 { 1661 static char rootname[ZFS_MAXNAMELEN]; 1662 static char buf[2 * ZFS_MAXNAMELEN + 8]; 1663 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev; 1664 spa_t *spa; 1665 1666 buf[0] = '\0'; 1667 if (vdev->d_dev->dv_type != DEVT_ZFS) 1668 return (buf); 1669 1670 /* Do we have any pools? */ 1671 spa = STAILQ_FIRST(&zfs_pools); 1672 if (spa == NULL) 1673 return (buf); 1674 1675 if (dev->pool_guid == 0) 1676 dev->pool_guid = spa->spa_guid; 1677 else 1678 spa = spa_find_by_guid(dev->pool_guid); 1679 1680 if (spa == NULL) { 1681 printf("ZFS: can't find pool by guid\n"); 1682 return (buf); 1683 } 1684 if (dev->root_guid == 0 && zfs_get_root(spa, &dev->root_guid)) { 1685 printf("ZFS: can't find root filesystem\n"); 1686 return (buf); 1687 } 1688 if (zfs_rlookup(spa, dev->root_guid, rootname)) { 1689 printf("ZFS: can't find filesystem by guid\n"); 1690 return (buf); 1691 } 1692 1693 if (rootname[0] == '\0') 1694 snprintf(buf, sizeof(buf), "%s:%s:", dev->dd.d_dev->dv_name, 1695 spa->spa_name); 1696 else 1697 snprintf(buf, sizeof(buf), "%s:%s/%s:", dev->dd.d_dev->dv_name, 1698 spa->spa_name, rootname); 1699 return (buf); 1700 } 1701 1702 static int 1703 split_devname(const char *name, char *poolname, size_t size, 1704 const char **dsnamep) 1705 { 1706 const char *dsname; 1707 size_t len; 1708 1709 ASSERT(name != NULL); 1710 ASSERT(poolname != NULL); 1711 1712 len = strlen(name); 1713 dsname = strchr(name, '/'); 1714 if (dsname != NULL) { 1715 len = dsname - name; 1716 dsname++; 1717 } else 1718 dsname = ""; 1719 1720 if (len + 1 > size) 1721 return (EINVAL); 1722 1723 strlcpy(poolname, name, len + 1); 1724 1725 if (dsnamep != NULL) 1726 *dsnamep = dsname; 1727 1728 return (0); 1729 } 1730 1731 int 1732 zfs_list(const char *name) 1733 { 1734 static char poolname[ZFS_MAXNAMELEN]; 1735 uint64_t objid; 1736 spa_t *spa; 1737 const char *dsname; 1738 int rv; 1739 1740 if (split_devname(name, poolname, sizeof(poolname), &dsname) != 0) 1741 return (EINVAL); 1742 1743 spa = spa_find_by_name(poolname); 1744 if (!spa) 1745 return (ENXIO); 1746 rv = zfs_lookup_dataset(spa, dsname, &objid); 1747 if (rv != 0) 1748 return (rv); 1749 1750 return (zfs_list_dataset(spa, objid)); 1751 } 1752 1753 void 1754 init_zfs_boot_options(const char *currdev_in) 1755 { 1756 char poolname[ZFS_MAXNAMELEN]; 1757 char *beroot, *currdev; 1758 spa_t *spa; 1759 int currdev_len; 1760 const char *dsname; 1761 1762 currdev = NULL; 1763 currdev_len = strlen(currdev_in); 1764 if (currdev_len == 0) 1765 return; 1766 if (strncmp(currdev_in, "zfs:", 4) != 0) 1767 return; 1768 currdev = strdup(currdev_in); 1769 if (currdev == NULL) 1770 return; 1771 /* Remove the trailing : */ 1772 currdev[currdev_len - 1] = '\0'; 1773 1774 setenv("zfs_be_active", currdev, 1); 1775 setenv("zfs_be_currpage", "1", 1); 1776 /* Remove the last element (current bootenv) */ 1777 beroot = strrchr(currdev, '/'); 1778 if (beroot != NULL) 1779 beroot[0] = '\0'; 1780 beroot = strchr(currdev, ':') + 1; 1781 setenv("zfs_be_root", beroot, 1); 1782 1783 if (split_devname(beroot, poolname, sizeof(poolname), &dsname) != 0) 1784 return; 1785 1786 spa = spa_find_by_name(poolname); 1787 if (spa == NULL) 1788 return; 1789 1790 zfs_bootenv_initial("bootenvs", spa, beroot, dsname, 0); 1791 zfs_checkpoints_initial(spa, beroot, dsname); 1792 1793 free(currdev); 1794 } 1795 1796 static void 1797 zfs_checkpoints_initial(spa_t *spa, const char *name, const char *dsname) 1798 { 1799 char envname[32]; 1800 1801 if (spa->spa_uberblock_checkpoint.ub_checkpoint_txg != 0) { 1802 snprintf(envname, sizeof(envname), "zpool_checkpoint"); 1803 setenv(envname, name, 1); 1804 1805 spa->spa_uberblock = &spa->spa_uberblock_checkpoint; 1806 spa->spa_mos = &spa->spa_mos_checkpoint; 1807 1808 zfs_bootenv_initial("bootenvs_check", spa, name, dsname, 1); 1809 1810 spa->spa_uberblock = &spa->spa_uberblock_master; 1811 spa->spa_mos = &spa->spa_mos_master; 1812 } 1813 } 1814 1815 static void 1816 zfs_bootenv_initial(const char *envprefix, spa_t *spa, const char *rootname, 1817 const char *dsname, int checkpoint) 1818 { 1819 char envname[32], envval[256]; 1820 uint64_t objid; 1821 int bootenvs_idx, rv; 1822 1823 SLIST_INIT(&zfs_be_head); 1824 zfs_env_count = 0; 1825 1826 rv = zfs_lookup_dataset(spa, dsname, &objid); 1827 if (rv != 0) 1828 return; 1829 1830 rv = zfs_callback_dataset(spa, objid, zfs_belist_add); 1831 bootenvs_idx = 0; 1832 /* Populate the initial environment variables */ 1833 SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) { 1834 /* Enumerate all bootenvs for general usage */ 1835 snprintf(envname, sizeof(envname), "%s[%d]", 1836 envprefix, bootenvs_idx); 1837 snprintf(envval, sizeof(envval), "zfs:%s%s/%s", 1838 checkpoint ? "!" : "", rootname, zfs_be->name); 1839 rv = setenv(envname, envval, 1); 1840 if (rv != 0) 1841 break; 1842 bootenvs_idx++; 1843 } 1844 snprintf(envname, sizeof(envname), "%s_count", envprefix); 1845 snprintf(envval, sizeof(envval), "%d", bootenvs_idx); 1846 setenv(envname, envval, 1); 1847 1848 /* Clean up the SLIST of ZFS BEs */ 1849 while (!SLIST_EMPTY(&zfs_be_head)) { 1850 zfs_be = SLIST_FIRST(&zfs_be_head); 1851 SLIST_REMOVE_HEAD(&zfs_be_head, entries); 1852 free(zfs_be->name); 1853 free(zfs_be); 1854 } 1855 } 1856 1857 int 1858 zfs_bootenv(const char *name) 1859 { 1860 char poolname[ZFS_MAXNAMELEN], *root; 1861 const char *dsname; 1862 char becount[4]; 1863 uint64_t objid; 1864 spa_t *spa; 1865 int rv, pages, perpage, currpage; 1866 1867 if (name == NULL) 1868 return (EINVAL); 1869 if ((root = getenv("zfs_be_root")) == NULL) 1870 return (EINVAL); 1871 1872 if (strcmp(name, root) != 0) { 1873 if (setenv("zfs_be_root", name, 1) != 0) 1874 return (ENOMEM); 1875 } 1876 1877 SLIST_INIT(&zfs_be_head); 1878 zfs_env_count = 0; 1879 1880 if (split_devname(name, poolname, sizeof(poolname), &dsname) != 0) 1881 return (EINVAL); 1882 1883 spa = spa_find_by_name(poolname); 1884 if (!spa) 1885 return (ENXIO); 1886 rv = zfs_lookup_dataset(spa, dsname, &objid); 1887 if (rv != 0) 1888 return (rv); 1889 rv = zfs_callback_dataset(spa, objid, zfs_belist_add); 1890 1891 /* Calculate and store the number of pages of BEs */ 1892 perpage = (ZFS_BE_LAST - ZFS_BE_FIRST + 1); 1893 pages = (zfs_env_count / perpage) + ((zfs_env_count % perpage) > 0 ? 1 : 0); 1894 snprintf(becount, 4, "%d", pages); 1895 if (setenv("zfs_be_pages", becount, 1) != 0) 1896 return (ENOMEM); 1897 1898 /* Roll over the page counter if it has exceeded the maximum */ 1899 currpage = strtol(getenv("zfs_be_currpage"), NULL, 10); 1900 if (currpage > pages) { 1901 if (setenv("zfs_be_currpage", "1", 1) != 0) 1902 return (ENOMEM); 1903 } 1904 1905 /* Populate the menu environment variables */ 1906 zfs_set_env(); 1907 1908 /* Clean up the SLIST of ZFS BEs */ 1909 while (!SLIST_EMPTY(&zfs_be_head)) { 1910 zfs_be = SLIST_FIRST(&zfs_be_head); 1911 SLIST_REMOVE_HEAD(&zfs_be_head, entries); 1912 free(zfs_be->name); 1913 free(zfs_be); 1914 } 1915 1916 return (rv); 1917 } 1918 1919 int 1920 zfs_belist_add(const char *name, uint64_t value __unused) 1921 { 1922 1923 /* Skip special datasets that start with a $ character */ 1924 if (strncmp(name, "$", 1) == 0) { 1925 return (0); 1926 } 1927 /* Add the boot environment to the head of the SLIST */ 1928 zfs_be = malloc(sizeof(struct zfs_be_entry)); 1929 if (zfs_be == NULL) { 1930 return (ENOMEM); 1931 } 1932 zfs_be->name = strdup(name); 1933 if (zfs_be->name == NULL) { 1934 free(zfs_be); 1935 return (ENOMEM); 1936 } 1937 SLIST_INSERT_HEAD(&zfs_be_head, zfs_be, entries); 1938 zfs_env_count++; 1939 1940 return (0); 1941 } 1942 1943 int 1944 zfs_set_env(void) 1945 { 1946 char envname[32], envval[256]; 1947 char *beroot, *pagenum; 1948 int rv, page, ctr; 1949 1950 beroot = getenv("zfs_be_root"); 1951 if (beroot == NULL) { 1952 return (1); 1953 } 1954 1955 pagenum = getenv("zfs_be_currpage"); 1956 if (pagenum != NULL) { 1957 page = strtol(pagenum, NULL, 10); 1958 } else { 1959 page = 1; 1960 } 1961 1962 ctr = 1; 1963 rv = 0; 1964 zfs_env_index = ZFS_BE_FIRST; 1965 SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) { 1966 /* Skip to the requested page number */ 1967 if (ctr <= ((ZFS_BE_LAST - ZFS_BE_FIRST + 1) * (page - 1))) { 1968 ctr++; 1969 continue; 1970 } 1971 1972 snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index); 1973 snprintf(envval, sizeof(envval), "%s", zfs_be->name); 1974 rv = setenv(envname, envval, 1); 1975 if (rv != 0) { 1976 break; 1977 } 1978 1979 snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index); 1980 rv = setenv(envname, envval, 1); 1981 if (rv != 0){ 1982 break; 1983 } 1984 1985 snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index); 1986 rv = setenv(envname, "set_bootenv", 1); 1987 if (rv != 0){ 1988 break; 1989 } 1990 1991 snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index); 1992 snprintf(envval, sizeof(envval), "zfs:%s/%s", beroot, zfs_be->name); 1993 rv = setenv(envname, envval, 1); 1994 if (rv != 0){ 1995 break; 1996 } 1997 1998 zfs_env_index++; 1999 if (zfs_env_index > ZFS_BE_LAST) { 2000 break; 2001 } 2002 2003 } 2004 2005 for (; zfs_env_index <= ZFS_BE_LAST; zfs_env_index++) { 2006 snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index); 2007 (void)unsetenv(envname); 2008 snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index); 2009 (void)unsetenv(envname); 2010 snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index); 2011 (void)unsetenv(envname); 2012 snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index); 2013 (void)unsetenv(envname); 2014 } 2015 2016 return (rv); 2017 } 2018