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 * $FreeBSD$ 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 /* 33 * Stand-alone file reading package. 34 */ 35 36 #include <stand.h> 37 #include <sys/disk.h> 38 #include <sys/param.h> 39 #include <sys/time.h> 40 #include <sys/queue.h> 41 #include <disk.h> 42 #include <part.h> 43 #include <stddef.h> 44 #include <stdarg.h> 45 #include <string.h> 46 #include <bootstrap.h> 47 48 #include "libzfs.h" 49 50 #include "zfsimpl.c" 51 52 /* Define the range of indexes to be populated with ZFS Boot Environments */ 53 #define ZFS_BE_FIRST 4 54 #define ZFS_BE_LAST 8 55 56 static int zfs_open(const char *path, struct open_file *f); 57 static int zfs_close(struct open_file *f); 58 static int zfs_read(struct open_file *f, void *buf, size_t size, size_t *resid); 59 static off_t zfs_seek(struct open_file *f, off_t offset, int where); 60 static int zfs_stat(struct open_file *f, struct stat *sb); 61 static int zfs_readdir(struct open_file *f, struct dirent *d); 62 63 static void zfs_bootenv_initial(const char *); 64 65 struct devsw zfs_dev; 66 67 struct fs_ops zfs_fsops = { 68 "zfs", 69 zfs_open, 70 zfs_close, 71 zfs_read, 72 null_write, 73 zfs_seek, 74 zfs_stat, 75 zfs_readdir 76 }; 77 78 /* 79 * In-core open file. 80 */ 81 struct file { 82 off_t f_seekp; /* seek pointer */ 83 dnode_phys_t f_dnode; 84 uint64_t f_zap_type; /* zap type for readdir */ 85 uint64_t f_num_leafs; /* number of fzap leaf blocks */ 86 zap_leaf_phys_t *f_zap_leaf; /* zap leaf buffer */ 87 }; 88 89 static int zfs_env_index; 90 static int zfs_env_count; 91 92 SLIST_HEAD(zfs_be_list, zfs_be_entry) zfs_be_head = SLIST_HEAD_INITIALIZER(zfs_be_head); 93 struct zfs_be_list *zfs_be_headp; 94 struct zfs_be_entry { 95 const char *name; 96 SLIST_ENTRY(zfs_be_entry) entries; 97 } *zfs_be, *zfs_be_tmp; 98 99 /* 100 * Open a file. 101 */ 102 static int 103 zfs_open(const char *upath, struct open_file *f) 104 { 105 struct zfsmount *mount = (struct zfsmount *)f->f_devdata; 106 struct file *fp; 107 int rc; 108 109 if (f->f_dev != &zfs_dev) 110 return (EINVAL); 111 112 /* allocate file system specific data structure */ 113 fp = calloc(1, sizeof(struct file)); 114 if (fp == NULL) 115 return (ENOMEM); 116 f->f_fsdata = fp; 117 118 rc = zfs_lookup(mount, upath, &fp->f_dnode); 119 fp->f_seekp = 0; 120 if (rc) { 121 f->f_fsdata = NULL; 122 free(fp); 123 } 124 return (rc); 125 } 126 127 static int 128 zfs_close(struct open_file *f) 129 { 130 struct file *fp = (struct file *)f->f_fsdata; 131 132 dnode_cache_obj = NULL; 133 f->f_fsdata = NULL; 134 135 free(fp); 136 return (0); 137 } 138 139 /* 140 * Copy a portion of a file into kernel memory. 141 * Cross block boundaries when necessary. 142 */ 143 static int 144 zfs_read(struct open_file *f, void *start, size_t size, size_t *resid /* out */) 145 { 146 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa; 147 struct file *fp = (struct file *)f->f_fsdata; 148 struct stat sb; 149 size_t n; 150 int rc; 151 152 rc = zfs_stat(f, &sb); 153 if (rc) 154 return (rc); 155 n = size; 156 if (fp->f_seekp + n > sb.st_size) 157 n = sb.st_size - fp->f_seekp; 158 159 rc = dnode_read(spa, &fp->f_dnode, fp->f_seekp, start, n); 160 if (rc) 161 return (rc); 162 163 if (0) { 164 int i; 165 for (i = 0; i < n; i++) 166 putchar(((char*) start)[i]); 167 } 168 fp->f_seekp += n; 169 if (resid) 170 *resid = size - n; 171 172 return (0); 173 } 174 175 static off_t 176 zfs_seek(struct open_file *f, off_t offset, int where) 177 { 178 struct file *fp = (struct file *)f->f_fsdata; 179 180 switch (where) { 181 case SEEK_SET: 182 fp->f_seekp = offset; 183 break; 184 case SEEK_CUR: 185 fp->f_seekp += offset; 186 break; 187 case SEEK_END: 188 { 189 struct stat sb; 190 int error; 191 192 error = zfs_stat(f, &sb); 193 if (error != 0) { 194 errno = error; 195 return (-1); 196 } 197 fp->f_seekp = sb.st_size - offset; 198 break; 199 } 200 default: 201 errno = EINVAL; 202 return (-1); 203 } 204 return (fp->f_seekp); 205 } 206 207 static int 208 zfs_stat(struct open_file *f, struct stat *sb) 209 { 210 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa; 211 struct file *fp = (struct file *)f->f_fsdata; 212 213 return (zfs_dnode_stat(spa, &fp->f_dnode, sb)); 214 } 215 216 static int 217 zfs_readdir(struct open_file *f, struct dirent *d) 218 { 219 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa; 220 struct file *fp = (struct file *)f->f_fsdata; 221 mzap_ent_phys_t mze; 222 struct stat sb; 223 size_t bsize = fp->f_dnode.dn_datablkszsec << SPA_MINBLOCKSHIFT; 224 int rc; 225 226 rc = zfs_stat(f, &sb); 227 if (rc) 228 return (rc); 229 if (!S_ISDIR(sb.st_mode)) 230 return (ENOTDIR); 231 232 /* 233 * If this is the first read, get the zap type. 234 */ 235 if (fp->f_seekp == 0) { 236 rc = dnode_read(spa, &fp->f_dnode, 237 0, &fp->f_zap_type, sizeof(fp->f_zap_type)); 238 if (rc) 239 return (rc); 240 241 if (fp->f_zap_type == ZBT_MICRO) { 242 fp->f_seekp = offsetof(mzap_phys_t, mz_chunk); 243 } else { 244 rc = dnode_read(spa, &fp->f_dnode, 245 offsetof(zap_phys_t, zap_num_leafs), 246 &fp->f_num_leafs, 247 sizeof(fp->f_num_leafs)); 248 if (rc) 249 return (rc); 250 251 fp->f_seekp = bsize; 252 fp->f_zap_leaf = malloc(bsize); 253 if (fp->f_zap_leaf == NULL) 254 return (ENOMEM); 255 rc = dnode_read(spa, &fp->f_dnode, 256 fp->f_seekp, 257 fp->f_zap_leaf, 258 bsize); 259 if (rc) 260 return (rc); 261 } 262 } 263 264 if (fp->f_zap_type == ZBT_MICRO) { 265 mzap_next: 266 if (fp->f_seekp >= bsize) 267 return (ENOENT); 268 269 rc = dnode_read(spa, &fp->f_dnode, 270 fp->f_seekp, &mze, sizeof(mze)); 271 if (rc) 272 return (rc); 273 fp->f_seekp += sizeof(mze); 274 275 if (!mze.mze_name[0]) 276 goto mzap_next; 277 278 d->d_fileno = ZFS_DIRENT_OBJ(mze.mze_value); 279 d->d_type = ZFS_DIRENT_TYPE(mze.mze_value); 280 strcpy(d->d_name, mze.mze_name); 281 d->d_namlen = strlen(d->d_name); 282 return (0); 283 } else { 284 zap_leaf_t zl; 285 zap_leaf_chunk_t *zc, *nc; 286 int chunk; 287 size_t namelen; 288 char *p; 289 uint64_t value; 290 291 /* 292 * Initialise this so we can use the ZAP size 293 * calculating macros. 294 */ 295 zl.l_bs = ilog2(bsize); 296 zl.l_phys = fp->f_zap_leaf; 297 298 /* 299 * Figure out which chunk we are currently looking at 300 * and consider seeking to the next leaf. We use the 301 * low bits of f_seekp as a simple chunk index. 302 */ 303 fzap_next: 304 chunk = fp->f_seekp & (bsize - 1); 305 if (chunk == ZAP_LEAF_NUMCHUNKS(&zl)) { 306 fp->f_seekp = rounddown2(fp->f_seekp, bsize) + bsize; 307 chunk = 0; 308 309 /* 310 * Check for EOF and read the new leaf. 311 */ 312 if (fp->f_seekp >= bsize * fp->f_num_leafs) 313 return (ENOENT); 314 315 rc = dnode_read(spa, &fp->f_dnode, 316 fp->f_seekp, 317 fp->f_zap_leaf, 318 bsize); 319 if (rc) 320 return (rc); 321 } 322 323 zc = &ZAP_LEAF_CHUNK(&zl, chunk); 324 fp->f_seekp++; 325 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY) 326 goto fzap_next; 327 328 namelen = zc->l_entry.le_name_numints; 329 if (namelen > sizeof(d->d_name)) 330 namelen = sizeof(d->d_name); 331 332 /* 333 * Paste the name back together. 334 */ 335 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk); 336 p = d->d_name; 337 while (namelen > 0) { 338 int len; 339 len = namelen; 340 if (len > ZAP_LEAF_ARRAY_BYTES) 341 len = ZAP_LEAF_ARRAY_BYTES; 342 memcpy(p, nc->l_array.la_array, len); 343 p += len; 344 namelen -= len; 345 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next); 346 } 347 d->d_name[sizeof(d->d_name) - 1] = 0; 348 349 /* 350 * Assume the first eight bytes of the value are 351 * a uint64_t. 352 */ 353 value = fzap_leaf_value(&zl, zc); 354 355 d->d_fileno = ZFS_DIRENT_OBJ(value); 356 d->d_type = ZFS_DIRENT_TYPE(value); 357 d->d_namlen = strlen(d->d_name); 358 359 return (0); 360 } 361 } 362 363 static int 364 vdev_read(vdev_t *vdev, void *priv, off_t offset, void *buf, size_t bytes) 365 { 366 int fd, ret; 367 size_t res, head, tail, total_size, full_sec_size; 368 unsigned secsz, do_tail_read; 369 off_t start_sec; 370 char *outbuf, *bouncebuf; 371 372 fd = (uintptr_t) priv; 373 outbuf = (char *) buf; 374 bouncebuf = NULL; 375 376 ret = ioctl(fd, DIOCGSECTORSIZE, &secsz); 377 if (ret != 0) 378 return (ret); 379 380 /* 381 * Handling reads of arbitrary offset and size - multi-sector case 382 * and single-sector case. 383 * 384 * Multi-sector Case 385 * (do_tail_read = true if tail > 0) 386 * 387 * |<----------------------total_size--------------------->| 388 * | | 389 * |<--head-->|<--------------bytes------------>|<--tail-->| 390 * | | | | 391 * | | |<~full_sec_size~>| | | 392 * +------------------+ +------------------+ 393 * | |0101010| . . . |0101011| | 394 * +------------------+ +------------------+ 395 * start_sec start_sec + n 396 * 397 * 398 * Single-sector Case 399 * (do_tail_read = false) 400 * 401 * |<------total_size = secsz----->| 402 * | | 403 * |<-head->|<---bytes--->|<-tail->| 404 * +-------------------------------+ 405 * | |0101010101010| | 406 * +-------------------------------+ 407 * start_sec 408 */ 409 start_sec = offset / secsz; 410 head = offset % secsz; 411 total_size = roundup2(head + bytes, secsz); 412 tail = total_size - (head + bytes); 413 do_tail_read = ((tail > 0) && (head + bytes > secsz)); 414 full_sec_size = total_size; 415 if (head > 0) 416 full_sec_size -= secsz; 417 if (do_tail_read) 418 full_sec_size -= secsz; 419 420 /* Return of partial sector data requires a bounce buffer. */ 421 if ((head > 0) || do_tail_read) { 422 bouncebuf = malloc(secsz); 423 if (bouncebuf == NULL) { 424 printf("vdev_read: out of memory\n"); 425 return (ENOMEM); 426 } 427 } 428 429 if (lseek(fd, start_sec * secsz, SEEK_SET) == -1) { 430 ret = errno; 431 goto error; 432 } 433 434 /* Partial data return from first sector */ 435 if (head > 0) { 436 res = read(fd, bouncebuf, secsz); 437 if (res != secsz) { 438 ret = EIO; 439 goto error; 440 } 441 memcpy(outbuf, bouncebuf + head, min(secsz - head, bytes)); 442 outbuf += min(secsz - head, bytes); 443 } 444 445 /* Full data return from read sectors */ 446 if (full_sec_size > 0) { 447 res = read(fd, outbuf, full_sec_size); 448 if (res != full_sec_size) { 449 ret = EIO; 450 goto error; 451 } 452 outbuf += full_sec_size; 453 } 454 455 /* Partial data return from last sector */ 456 if (do_tail_read) { 457 res = read(fd, bouncebuf, secsz); 458 if (res != secsz) { 459 ret = EIO; 460 goto error; 461 } 462 memcpy(outbuf, bouncebuf, secsz - tail); 463 } 464 465 ret = 0; 466 error: 467 free(bouncebuf); 468 return (ret); 469 } 470 471 static int 472 zfs_dev_init(void) 473 { 474 spa_t *spa; 475 spa_t *next; 476 spa_t *prev; 477 478 zfs_init(); 479 if (archsw.arch_zfs_probe == NULL) 480 return (ENXIO); 481 archsw.arch_zfs_probe(); 482 483 prev = NULL; 484 spa = STAILQ_FIRST(&zfs_pools); 485 while (spa != NULL) { 486 next = STAILQ_NEXT(spa, spa_link); 487 if (zfs_spa_init(spa)) { 488 if (prev == NULL) 489 STAILQ_REMOVE_HEAD(&zfs_pools, spa_link); 490 else 491 STAILQ_REMOVE_AFTER(&zfs_pools, prev, spa_link); 492 } else 493 prev = spa; 494 spa = next; 495 } 496 return (0); 497 } 498 499 struct zfs_probe_args { 500 int fd; 501 const char *devname; 502 uint64_t *pool_guid; 503 u_int secsz; 504 }; 505 506 static int 507 zfs_diskread(void *arg, void *buf, size_t blocks, uint64_t offset) 508 { 509 struct zfs_probe_args *ppa; 510 511 ppa = (struct zfs_probe_args *)arg; 512 return (vdev_read(NULL, (void *)(uintptr_t)ppa->fd, 513 offset * ppa->secsz, buf, blocks * ppa->secsz)); 514 } 515 516 static int 517 zfs_probe(int fd, uint64_t *pool_guid) 518 { 519 spa_t *spa; 520 int ret; 521 522 spa = NULL; 523 ret = vdev_probe(vdev_read, (void *)(uintptr_t)fd, &spa); 524 if (ret == 0 && pool_guid != NULL) 525 *pool_guid = spa->spa_guid; 526 return (ret); 527 } 528 529 static int 530 zfs_probe_partition(void *arg, const char *partname, 531 const struct ptable_entry *part) 532 { 533 struct zfs_probe_args *ppa, pa; 534 struct ptable *table; 535 char devname[32]; 536 int ret; 537 538 /* Probe only freebsd-zfs and freebsd partitions */ 539 if (part->type != PART_FREEBSD && 540 part->type != PART_FREEBSD_ZFS) 541 return (0); 542 543 ppa = (struct zfs_probe_args *)arg; 544 strncpy(devname, ppa->devname, strlen(ppa->devname) - 1); 545 devname[strlen(ppa->devname) - 1] = '\0'; 546 sprintf(devname, "%s%s:", devname, partname); 547 pa.fd = open(devname, O_RDONLY); 548 if (pa.fd == -1) 549 return (0); 550 ret = zfs_probe(pa.fd, ppa->pool_guid); 551 if (ret == 0) 552 return (0); 553 /* Do we have BSD label here? */ 554 if (part->type == PART_FREEBSD) { 555 pa.devname = devname; 556 pa.pool_guid = ppa->pool_guid; 557 pa.secsz = ppa->secsz; 558 table = ptable_open(&pa, part->end - part->start + 1, 559 ppa->secsz, zfs_diskread); 560 if (table != NULL) { 561 ptable_iterate(table, &pa, zfs_probe_partition); 562 ptable_close(table); 563 } 564 } 565 close(pa.fd); 566 return (0); 567 } 568 569 int 570 zfs_probe_dev(const char *devname, uint64_t *pool_guid) 571 { 572 struct disk_devdesc *dev; 573 struct ptable *table; 574 struct zfs_probe_args pa; 575 uint64_t mediasz; 576 int ret; 577 578 if (pool_guid) 579 *pool_guid = 0; 580 pa.fd = open(devname, O_RDONLY); 581 if (pa.fd == -1) 582 return (ENXIO); 583 /* 584 * We will not probe the whole disk, we can not boot from such 585 * disks and some systems will misreport the disk sizes and will 586 * hang while accessing the disk. 587 */ 588 if (archsw.arch_getdev((void **)&dev, devname, NULL) == 0) { 589 int partition = dev->d_partition; 590 int slice = dev->d_slice; 591 592 free(dev); 593 if (partition != D_PARTNONE && slice != D_SLICENONE) { 594 ret = zfs_probe(pa.fd, pool_guid); 595 if (ret == 0) 596 return (0); 597 } 598 } 599 600 /* Probe each partition */ 601 ret = ioctl(pa.fd, DIOCGMEDIASIZE, &mediasz); 602 if (ret == 0) 603 ret = ioctl(pa.fd, DIOCGSECTORSIZE, &pa.secsz); 604 if (ret == 0) { 605 pa.devname = devname; 606 pa.pool_guid = pool_guid; 607 table = ptable_open(&pa, mediasz / pa.secsz, pa.secsz, 608 zfs_diskread); 609 if (table != NULL) { 610 ptable_iterate(table, &pa, zfs_probe_partition); 611 ptable_close(table); 612 } 613 } 614 close(pa.fd); 615 if (pool_guid && *pool_guid == 0) 616 ret = ENXIO; 617 return (ret); 618 } 619 620 /* 621 * Print information about ZFS pools 622 */ 623 static int 624 zfs_dev_print(int verbose) 625 { 626 spa_t *spa; 627 char line[80]; 628 int ret = 0; 629 630 if (STAILQ_EMPTY(&zfs_pools)) 631 return (0); 632 633 printf("%s devices:", zfs_dev.dv_name); 634 if ((ret = pager_output("\n")) != 0) 635 return (ret); 636 637 if (verbose) { 638 return (spa_all_status()); 639 } 640 STAILQ_FOREACH(spa, &zfs_pools, spa_link) { 641 snprintf(line, sizeof(line), " zfs:%s\n", spa->spa_name); 642 ret = pager_output(line); 643 if (ret != 0) 644 break; 645 } 646 return (ret); 647 } 648 649 /* 650 * Attempt to open the pool described by (dev) for use by (f). 651 */ 652 static int 653 zfs_dev_open(struct open_file *f, ...) 654 { 655 va_list args; 656 struct zfs_devdesc *dev; 657 struct zfsmount *mount; 658 spa_t *spa; 659 int rv; 660 661 va_start(args, f); 662 dev = va_arg(args, struct zfs_devdesc *); 663 va_end(args); 664 665 if (dev->pool_guid == 0) 666 spa = STAILQ_FIRST(&zfs_pools); 667 else 668 spa = spa_find_by_guid(dev->pool_guid); 669 if (!spa) 670 return (ENXIO); 671 mount = malloc(sizeof(*mount)); 672 if (mount == NULL) 673 rv = ENOMEM; 674 else 675 rv = zfs_mount(spa, dev->root_guid, mount); 676 if (rv != 0) { 677 free(mount); 678 return (rv); 679 } 680 if (mount->objset.os_type != DMU_OST_ZFS) { 681 printf("Unexpected object set type %ju\n", 682 (uintmax_t)mount->objset.os_type); 683 free(mount); 684 return (EIO); 685 } 686 f->f_devdata = mount; 687 free(dev); 688 return (0); 689 } 690 691 static int 692 zfs_dev_close(struct open_file *f) 693 { 694 695 free(f->f_devdata); 696 f->f_devdata = NULL; 697 return (0); 698 } 699 700 static int 701 zfs_dev_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize) 702 { 703 704 return (ENOSYS); 705 } 706 707 struct devsw zfs_dev = { 708 .dv_name = "zfs", 709 .dv_type = DEVT_ZFS, 710 .dv_init = zfs_dev_init, 711 .dv_strategy = zfs_dev_strategy, 712 .dv_open = zfs_dev_open, 713 .dv_close = zfs_dev_close, 714 .dv_ioctl = noioctl, 715 .dv_print = zfs_dev_print, 716 .dv_cleanup = NULL 717 }; 718 719 int 720 zfs_parsedev(struct zfs_devdesc *dev, const char *devspec, const char **path) 721 { 722 static char rootname[ZFS_MAXNAMELEN]; 723 static char poolname[ZFS_MAXNAMELEN]; 724 spa_t *spa; 725 const char *end; 726 const char *np; 727 const char *sep; 728 int rv; 729 730 np = devspec; 731 if (*np != ':') 732 return (EINVAL); 733 np++; 734 end = strrchr(np, ':'); 735 if (end == NULL) 736 return (EINVAL); 737 sep = strchr(np, '/'); 738 if (sep == NULL || sep >= end) 739 sep = end; 740 memcpy(poolname, np, sep - np); 741 poolname[sep - np] = '\0'; 742 if (sep < end) { 743 sep++; 744 memcpy(rootname, sep, end - sep); 745 rootname[end - sep] = '\0'; 746 } 747 else 748 rootname[0] = '\0'; 749 750 spa = spa_find_by_name(poolname); 751 if (!spa) 752 return (ENXIO); 753 dev->pool_guid = spa->spa_guid; 754 rv = zfs_lookup_dataset(spa, rootname, &dev->root_guid); 755 if (rv != 0) 756 return (rv); 757 if (path != NULL) 758 *path = (*end == '\0') ? end : end + 1; 759 dev->dd.d_dev = &zfs_dev; 760 return (0); 761 } 762 763 char * 764 zfs_fmtdev(void *vdev) 765 { 766 static char rootname[ZFS_MAXNAMELEN]; 767 static char buf[2 * ZFS_MAXNAMELEN + 8]; 768 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev; 769 spa_t *spa; 770 771 buf[0] = '\0'; 772 if (dev->dd.d_dev->dv_type != DEVT_ZFS) 773 return (buf); 774 775 /* Do we have any pools? */ 776 spa = STAILQ_FIRST(&zfs_pools); 777 if (spa == NULL) 778 return (buf); 779 780 if (dev->pool_guid == 0) 781 dev->pool_guid = spa->spa_guid; 782 else 783 spa = spa_find_by_guid(dev->pool_guid); 784 785 if (spa == NULL) { 786 printf("ZFS: can't find pool by guid\n"); 787 return (buf); 788 } 789 if (dev->root_guid == 0 && zfs_get_root(spa, &dev->root_guid)) { 790 printf("ZFS: can't find root filesystem\n"); 791 return (buf); 792 } 793 if (zfs_rlookup(spa, dev->root_guid, rootname)) { 794 printf("ZFS: can't find filesystem by guid\n"); 795 return (buf); 796 } 797 798 if (rootname[0] == '\0') 799 sprintf(buf, "%s:%s:", dev->dd.d_dev->dv_name, spa->spa_name); 800 else 801 sprintf(buf, "%s:%s/%s:", dev->dd.d_dev->dv_name, spa->spa_name, 802 rootname); 803 return (buf); 804 } 805 806 int 807 zfs_list(const char *name) 808 { 809 static char poolname[ZFS_MAXNAMELEN]; 810 uint64_t objid; 811 spa_t *spa; 812 const char *dsname; 813 int len; 814 int rv; 815 816 len = strlen(name); 817 dsname = strchr(name, '/'); 818 if (dsname != NULL) { 819 len = dsname - name; 820 dsname++; 821 } else 822 dsname = ""; 823 memcpy(poolname, name, len); 824 poolname[len] = '\0'; 825 826 spa = spa_find_by_name(poolname); 827 if (!spa) 828 return (ENXIO); 829 rv = zfs_lookup_dataset(spa, dsname, &objid); 830 if (rv != 0) 831 return (rv); 832 833 return (zfs_list_dataset(spa, objid)); 834 } 835 836 void 837 init_zfs_bootenv(const char *currdev_in) 838 { 839 char *beroot, *currdev; 840 int currdev_len; 841 842 currdev = NULL; 843 currdev_len = strlen(currdev_in); 844 if (currdev_len == 0) 845 return; 846 if (strncmp(currdev_in, "zfs:", 4) != 0) 847 return; 848 currdev = strdup(currdev_in); 849 if (currdev == NULL) 850 return; 851 /* Remove the trailing : */ 852 currdev[currdev_len - 1] = '\0'; 853 setenv("zfs_be_active", currdev, 1); 854 setenv("zfs_be_currpage", "1", 1); 855 /* Remove the last element (current bootenv) */ 856 beroot = strrchr(currdev, '/'); 857 if (beroot != NULL) 858 beroot[0] = '\0'; 859 beroot = strchr(currdev, ':') + 1; 860 setenv("zfs_be_root", beroot, 1); 861 zfs_bootenv_initial(beroot); 862 free(currdev); 863 } 864 865 static void 866 zfs_bootenv_initial(const char *name) 867 { 868 char poolname[ZFS_MAXNAMELEN], *dsname; 869 char envname[32], envval[256]; 870 uint64_t objid; 871 spa_t *spa; 872 int bootenvs_idx, len, rv; 873 874 SLIST_INIT(&zfs_be_head); 875 zfs_env_count = 0; 876 len = strlen(name); 877 dsname = strchr(name, '/'); 878 if (dsname != NULL) { 879 len = dsname - name; 880 dsname++; 881 } else 882 dsname = ""; 883 strlcpy(poolname, name, len + 1); 884 spa = spa_find_by_name(poolname); 885 if (spa == NULL) 886 return; 887 rv = zfs_lookup_dataset(spa, dsname, &objid); 888 if (rv != 0) 889 return; 890 rv = zfs_callback_dataset(spa, objid, zfs_belist_add); 891 bootenvs_idx = 0; 892 /* Populate the initial environment variables */ 893 SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) { 894 /* Enumerate all bootenvs for general usage */ 895 snprintf(envname, sizeof(envname), "bootenvs[%d]", bootenvs_idx); 896 snprintf(envval, sizeof(envval), "zfs:%s/%s", name, zfs_be->name); 897 rv = setenv(envname, envval, 1); 898 if (rv != 0) 899 break; 900 bootenvs_idx++; 901 } 902 snprintf(envval, sizeof(envval), "%d", bootenvs_idx); 903 setenv("bootenvs_count", envval, 1); 904 905 /* Clean up the SLIST of ZFS BEs */ 906 while (!SLIST_EMPTY(&zfs_be_head)) { 907 zfs_be = SLIST_FIRST(&zfs_be_head); 908 SLIST_REMOVE_HEAD(&zfs_be_head, entries); 909 free(zfs_be); 910 } 911 912 return; 913 914 } 915 916 int 917 zfs_bootenv(const char *name) 918 { 919 static char poolname[ZFS_MAXNAMELEN], *dsname, *root; 920 char becount[4]; 921 uint64_t objid; 922 spa_t *spa; 923 int len, rv, pages, perpage, currpage; 924 925 if (name == NULL) 926 return (EINVAL); 927 if ((root = getenv("zfs_be_root")) == NULL) 928 return (EINVAL); 929 930 if (strcmp(name, root) != 0) { 931 if (setenv("zfs_be_root", name, 1) != 0) 932 return (ENOMEM); 933 } 934 935 SLIST_INIT(&zfs_be_head); 936 zfs_env_count = 0; 937 len = strlen(name); 938 dsname = strchr(name, '/'); 939 if (dsname != NULL) { 940 len = dsname - name; 941 dsname++; 942 } else 943 dsname = ""; 944 memcpy(poolname, name, len); 945 poolname[len] = '\0'; 946 947 spa = spa_find_by_name(poolname); 948 if (!spa) 949 return (ENXIO); 950 rv = zfs_lookup_dataset(spa, dsname, &objid); 951 if (rv != 0) 952 return (rv); 953 rv = zfs_callback_dataset(spa, objid, zfs_belist_add); 954 955 /* Calculate and store the number of pages of BEs */ 956 perpage = (ZFS_BE_LAST - ZFS_BE_FIRST + 1); 957 pages = (zfs_env_count / perpage) + ((zfs_env_count % perpage) > 0 ? 1 : 0); 958 snprintf(becount, 4, "%d", pages); 959 if (setenv("zfs_be_pages", becount, 1) != 0) 960 return (ENOMEM); 961 962 /* Roll over the page counter if it has exceeded the maximum */ 963 currpage = strtol(getenv("zfs_be_currpage"), NULL, 10); 964 if (currpage > pages) { 965 if (setenv("zfs_be_currpage", "1", 1) != 0) 966 return (ENOMEM); 967 } 968 969 /* Populate the menu environment variables */ 970 zfs_set_env(); 971 972 /* Clean up the SLIST of ZFS BEs */ 973 while (!SLIST_EMPTY(&zfs_be_head)) { 974 zfs_be = SLIST_FIRST(&zfs_be_head); 975 SLIST_REMOVE_HEAD(&zfs_be_head, entries); 976 free(zfs_be); 977 } 978 979 return (rv); 980 } 981 982 int 983 zfs_belist_add(const char *name, uint64_t value __unused) 984 { 985 986 /* Skip special datasets that start with a $ character */ 987 if (strncmp(name, "$", 1) == 0) { 988 return (0); 989 } 990 /* Add the boot environment to the head of the SLIST */ 991 zfs_be = malloc(sizeof(struct zfs_be_entry)); 992 if (zfs_be == NULL) { 993 return (ENOMEM); 994 } 995 zfs_be->name = name; 996 SLIST_INSERT_HEAD(&zfs_be_head, zfs_be, entries); 997 zfs_env_count++; 998 999 return (0); 1000 } 1001 1002 int 1003 zfs_set_env(void) 1004 { 1005 char envname[32], envval[256]; 1006 char *beroot, *pagenum; 1007 int rv, page, ctr; 1008 1009 beroot = getenv("zfs_be_root"); 1010 if (beroot == NULL) { 1011 return (1); 1012 } 1013 1014 pagenum = getenv("zfs_be_currpage"); 1015 if (pagenum != NULL) { 1016 page = strtol(pagenum, NULL, 10); 1017 } else { 1018 page = 1; 1019 } 1020 1021 ctr = 1; 1022 rv = 0; 1023 zfs_env_index = ZFS_BE_FIRST; 1024 SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) { 1025 /* Skip to the requested page number */ 1026 if (ctr <= ((ZFS_BE_LAST - ZFS_BE_FIRST + 1) * (page - 1))) { 1027 ctr++; 1028 continue; 1029 } 1030 1031 snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index); 1032 snprintf(envval, sizeof(envval), "%s", zfs_be->name); 1033 rv = setenv(envname, envval, 1); 1034 if (rv != 0) { 1035 break; 1036 } 1037 1038 snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index); 1039 rv = setenv(envname, envval, 1); 1040 if (rv != 0){ 1041 break; 1042 } 1043 1044 snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index); 1045 rv = setenv(envname, "set_bootenv", 1); 1046 if (rv != 0){ 1047 break; 1048 } 1049 1050 snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index); 1051 snprintf(envval, sizeof(envval), "zfs:%s/%s", beroot, zfs_be->name); 1052 rv = setenv(envname, envval, 1); 1053 if (rv != 0){ 1054 break; 1055 } 1056 1057 zfs_env_index++; 1058 if (zfs_env_index > ZFS_BE_LAST) { 1059 break; 1060 } 1061 1062 } 1063 1064 for (; zfs_env_index <= ZFS_BE_LAST; zfs_env_index++) { 1065 snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index); 1066 (void)unsetenv(envname); 1067 snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index); 1068 (void)unsetenv(envname); 1069 snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index); 1070 (void)unsetenv(envname); 1071 snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index); 1072 (void)unsetenv(envname); 1073 } 1074 1075 return (rv); 1076 } 1077