1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/param.h> 29 #include <sys/module.h> 30 #include <sys/mount.h> 31 #include <sys/stat.h> 32 #include <sys/ucred.h> 33 #include <sys/queue.h> 34 #include <sys/zfs_context.h> 35 #include <sys/mntent.h> 36 #include <sys/zfs_ioctl.h> 37 38 #include <libzutil.h> 39 #include <ctype.h> 40 #include <libgen.h> 41 #include <libzfs_core.h> 42 #include <libzfs_impl.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <time.h> 46 #include <unistd.h> 47 #include <libzfsbootenv.h> 48 49 #include "be.h" 50 #include "be_impl.h" 51 52 struct promote_entry { 53 char name[BE_MAXPATHLEN]; 54 SLIST_ENTRY(promote_entry) link; 55 }; 56 57 struct be_destroy_data { 58 libbe_handle_t *lbh; 59 char target_name[BE_MAXPATHLEN]; 60 char *snapname; 61 SLIST_HEAD(, promote_entry) promotelist; 62 }; 63 64 #if SOON 65 static int be_create_child_noent(libbe_handle_t *lbh, const char *active, 66 const char *child_path); 67 static int be_create_child_cloned(libbe_handle_t *lbh, const char *active); 68 #endif 69 70 /* Arbitrary... should tune */ 71 #define BE_SNAP_SERIAL_MAX 1024 72 73 /* 74 * Iterator function for locating the rootfs amongst the children of the 75 * zfs_be_root set by loader(8). data is expected to be a libbe_handle_t *. 76 */ 77 static int 78 be_locate_rootfs(libbe_handle_t *lbh) 79 { 80 struct statfs sfs; 81 struct mnttab entry; 82 zfs_handle_t *zfs; 83 84 /* 85 * Check first if root is ZFS; if not, we'll bail on rootfs capture. 86 * Unfortunately needed because zfs_path_to_zhandle will emit to 87 * stderr if / isn't actually a ZFS filesystem, which we'd like 88 * to avoid. 89 */ 90 if (statfs("/", &sfs) == 0) { 91 statfs2mnttab(&sfs, &entry); 92 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 93 return (1); 94 } else 95 return (1); 96 zfs = zfs_path_to_zhandle(lbh->lzh, "/", ZFS_TYPE_FILESYSTEM); 97 if (zfs == NULL) 98 return (1); 99 100 strlcpy(lbh->rootfs, zfs_get_name(zfs), sizeof(lbh->rootfs)); 101 zfs_close(zfs); 102 return (0); 103 } 104 105 /* 106 * Initializes the libbe context to operate in the root boot environment 107 * dataset, for example, zroot/ROOT. 108 */ 109 libbe_handle_t * 110 libbe_init(const char *root) 111 { 112 char altroot[MAXPATHLEN]; 113 libbe_handle_t *lbh; 114 char *poolname, *pos; 115 int pnamelen; 116 117 lbh = NULL; 118 poolname = pos = NULL; 119 120 /* 121 * If the zfs kmod's not loaded then the later libzfs_init() will load 122 * the module for us, but that's not desirable for a couple reasons. If 123 * the module's not loaded, there's no pool imported and we're going to 124 * fail anyways. We also don't really want libbe consumers to have that 125 * kind of side-effect (module loading) in the general case. 126 */ 127 if (modfind("zfs") < 0) 128 goto err; 129 130 if ((lbh = calloc(1, sizeof(libbe_handle_t))) == NULL) 131 goto err; 132 133 if ((lbh->lzh = libzfs_init()) == NULL) 134 goto err; 135 136 /* 137 * Grab rootfs, we'll work backwards from there if an optional BE root 138 * has not been passed in. 139 */ 140 if (be_locate_rootfs(lbh) != 0) { 141 if (root == NULL) 142 goto err; 143 *lbh->rootfs = '\0'; 144 } 145 if (root == NULL) { 146 /* Strip off the final slash from rootfs to get the be root */ 147 strlcpy(lbh->root, lbh->rootfs, sizeof(lbh->root)); 148 pos = strrchr(lbh->root, '/'); 149 if (pos == NULL) 150 goto err; 151 *pos = '\0'; 152 } else 153 strlcpy(lbh->root, root, sizeof(lbh->root)); 154 155 if ((pos = strchr(lbh->root, '/')) == NULL) 156 goto err; 157 158 pnamelen = pos - lbh->root; 159 poolname = malloc(pnamelen + 1); 160 if (poolname == NULL) 161 goto err; 162 163 strlcpy(poolname, lbh->root, pnamelen + 1); 164 if ((lbh->active_phandle = zpool_open(lbh->lzh, poolname)) == NULL) 165 goto err; 166 free(poolname); 167 poolname = NULL; 168 169 if (zpool_get_prop(lbh->active_phandle, ZPOOL_PROP_BOOTFS, lbh->bootfs, 170 sizeof(lbh->bootfs), NULL, true) != 0) 171 goto err; 172 173 if (zpool_get_prop(lbh->active_phandle, ZPOOL_PROP_ALTROOT, 174 altroot, sizeof(altroot), NULL, true) == 0 && 175 strcmp(altroot, "-") != 0) 176 lbh->altroot_len = strlen(altroot); 177 178 return (lbh); 179 err: 180 if (lbh != NULL) { 181 if (lbh->active_phandle != NULL) 182 zpool_close(lbh->active_phandle); 183 if (lbh->lzh != NULL) 184 libzfs_fini(lbh->lzh); 185 free(lbh); 186 } 187 free(poolname); 188 return (NULL); 189 } 190 191 192 /* 193 * Free memory allocated by libbe_init() 194 */ 195 void 196 libbe_close(libbe_handle_t *lbh) 197 { 198 199 if (lbh->active_phandle != NULL) 200 zpool_close(lbh->active_phandle); 201 libzfs_fini(lbh->lzh); 202 free(lbh); 203 } 204 205 /* 206 * Proxy through to libzfs for the moment. 207 */ 208 void 209 be_nicenum(uint64_t num, char *buf, size_t buflen) 210 { 211 212 zfs_nicenum(num, buf, buflen); 213 } 214 215 static bool 216 be_should_promote_clones(zfs_handle_t *zfs_hdl, struct be_destroy_data *bdd) 217 { 218 char *atpos; 219 220 if (zfs_get_type(zfs_hdl) != ZFS_TYPE_SNAPSHOT) 221 return (false); 222 223 /* 224 * If we're deleting a snapshot, we need to make sure we only promote 225 * clones that are derived from one of the snapshots we're deleting, 226 * rather than that of a snapshot we're not touching. This keeps stuff 227 * in a consistent state, making sure that we don't error out unless 228 * we really need to. 229 */ 230 if (bdd->snapname == NULL) 231 return (true); 232 233 atpos = strchr(zfs_get_name(zfs_hdl), '@'); 234 return (strcmp(atpos + 1, bdd->snapname) == 0); 235 } 236 237 /* 238 * This is executed from be_promote_dependent_clones via zfs_iter_dependents, 239 * It checks if the dependent type is a snapshot then attempts to find any 240 * clones associated with it. Any clones not related to the destroy target are 241 * added to the promote list. 242 */ 243 static int 244 be_dependent_clone_cb(zfs_handle_t *zfs_hdl, void *data) 245 { 246 int err; 247 bool found; 248 const char *name; 249 struct nvlist *nvl; 250 struct nvpair *nvp; 251 struct be_destroy_data *bdd; 252 struct promote_entry *entry, *newentry; 253 254 nvp = NULL; 255 err = 0; 256 bdd = (struct be_destroy_data *)data; 257 258 if (be_should_promote_clones(zfs_hdl, bdd) && 259 (nvl = zfs_get_clones_nvl(zfs_hdl)) != NULL) { 260 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 261 name = nvpair_name(nvp); 262 263 /* 264 * Skip if the clone is equal to, or a child of, the 265 * destroy target. 266 */ 267 if (strncmp(name, bdd->target_name, 268 strlen(bdd->target_name)) == 0 || 269 strstr(name, bdd->target_name) == name) { 270 continue; 271 } 272 273 found = false; 274 SLIST_FOREACH(entry, &bdd->promotelist, link) { 275 if (strcmp(entry->name, name) == 0) { 276 found = true; 277 break; 278 } 279 } 280 281 if (found) 282 continue; 283 284 newentry = malloc(sizeof(struct promote_entry)); 285 if (newentry == NULL) { 286 err = ENOMEM; 287 break; 288 } 289 290 #define BE_COPY_NAME(entry, src) \ 291 strlcpy((entry)->name, (src), sizeof((entry)->name)) 292 if (BE_COPY_NAME(newentry, name) >= 293 sizeof(newentry->name)) { 294 /* Shouldn't happen. */ 295 free(newentry); 296 err = ENAMETOOLONG; 297 break; 298 } 299 #undef BE_COPY_NAME 300 301 /* 302 * We're building up a SLIST here to make sure both that 303 * we get the order right and so that we don't 304 * inadvertently observe the wrong state by promoting 305 * datasets while we're still walking the tree. The 306 * latter can lead to situations where we promote a BE 307 * then effectively demote it again. 308 */ 309 SLIST_INSERT_HEAD(&bdd->promotelist, newentry, link); 310 } 311 nvlist_free(nvl); 312 } 313 zfs_close(zfs_hdl); 314 return (err); 315 } 316 317 /* 318 * This is called before a destroy, so that any datasets(environments) that are 319 * dependent on this one get promoted before destroying the target. 320 */ 321 static int 322 be_promote_dependent_clones(zfs_handle_t *zfs_hdl, struct be_destroy_data *bdd) 323 { 324 int err; 325 zfs_handle_t *clone; 326 struct promote_entry *entry; 327 328 snprintf(bdd->target_name, BE_MAXPATHLEN, "%s/", zfs_get_name(zfs_hdl)); 329 err = zfs_iter_dependents(zfs_hdl, true, be_dependent_clone_cb, bdd); 330 331 /* 332 * Drain the list and walk away from it if we're only deleting a 333 * snapshot. 334 */ 335 if (bdd->snapname != NULL && !SLIST_EMPTY(&bdd->promotelist)) 336 err = BE_ERR_HASCLONES; 337 while (!SLIST_EMPTY(&bdd->promotelist)) { 338 entry = SLIST_FIRST(&bdd->promotelist); 339 SLIST_REMOVE_HEAD(&bdd->promotelist, link); 340 341 #define ZFS_GRAB_CLONE() \ 342 zfs_open(bdd->lbh->lzh, entry->name, ZFS_TYPE_FILESYSTEM) 343 /* 344 * Just skip this part on error, we still want to clean up the 345 * promotion list after the first error. We'll then preserve it 346 * all the way back. 347 */ 348 if (err == 0 && (clone = ZFS_GRAB_CLONE()) != NULL) { 349 err = zfs_promote(clone); 350 if (err != 0) 351 err = BE_ERR_DESTROYMNT; 352 zfs_close(clone); 353 } 354 #undef ZFS_GRAB_CLONE 355 free(entry); 356 } 357 358 return (err); 359 } 360 361 static int 362 be_destroy_cb(zfs_handle_t *zfs_hdl, void *data) 363 { 364 char path[BE_MAXPATHLEN]; 365 struct be_destroy_data *bdd; 366 zfs_handle_t *snap; 367 int err; 368 369 bdd = (struct be_destroy_data *)data; 370 if (bdd->snapname == NULL) { 371 err = zfs_iter_children(zfs_hdl, be_destroy_cb, data); 372 if (err != 0) 373 return (err); 374 return (zfs_destroy(zfs_hdl, false)); 375 } 376 /* If we're dealing with snapshots instead, delete that one alone */ 377 err = zfs_iter_filesystems(zfs_hdl, be_destroy_cb, data); 378 if (err != 0) 379 return (err); 380 /* 381 * This part is intentionally glossing over any potential errors, 382 * because there's a lot less potential for errors when we're cleaning 383 * up snapshots rather than a full deep BE. The primary error case 384 * here being if the snapshot doesn't exist in the first place, which 385 * the caller will likely deem insignificant as long as it doesn't 386 * exist after the call. Thus, such a missing snapshot shouldn't jam 387 * up the destruction. 388 */ 389 snprintf(path, sizeof(path), "%s@%s", zfs_get_name(zfs_hdl), 390 bdd->snapname); 391 if (!zfs_dataset_exists(bdd->lbh->lzh, path, ZFS_TYPE_SNAPSHOT)) 392 return (0); 393 snap = zfs_open(bdd->lbh->lzh, path, ZFS_TYPE_SNAPSHOT); 394 if (snap != NULL) 395 zfs_destroy(snap, false); 396 return (0); 397 } 398 399 #define BE_DESTROY_WANTORIGIN (BE_DESTROY_ORIGIN | BE_DESTROY_AUTOORIGIN) 400 /* 401 * Destroy the boot environment or snapshot specified by the name 402 * parameter. Options are or'd together with the possible values: 403 * BE_DESTROY_FORCE : forces operation on mounted datasets 404 * BE_DESTROY_ORIGIN: destroy the origin snapshot as well 405 */ 406 static int 407 be_destroy_internal(libbe_handle_t *lbh, const char *name, int options, 408 bool odestroyer) 409 { 410 struct be_destroy_data bdd; 411 char origin[BE_MAXPATHLEN], path[BE_MAXPATHLEN]; 412 zfs_handle_t *fs; 413 char *snapdelim; 414 int err, force, mounted; 415 size_t rootlen; 416 417 bdd.lbh = lbh; 418 bdd.snapname = NULL; 419 SLIST_INIT(&bdd.promotelist); 420 force = options & BE_DESTROY_FORCE; 421 *origin = '\0'; 422 423 be_root_concat(lbh, name, path); 424 425 if ((snapdelim = strchr(path, '@')) == NULL) { 426 if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_FILESYSTEM)) 427 return (set_error(lbh, BE_ERR_NOENT)); 428 429 if (strcmp(path, lbh->rootfs) == 0 || 430 strcmp(path, lbh->bootfs) == 0) 431 return (set_error(lbh, BE_ERR_DESTROYACT)); 432 433 fs = zfs_open(lbh->lzh, path, ZFS_TYPE_FILESYSTEM); 434 if (fs == NULL) 435 return (set_error(lbh, BE_ERR_ZFSOPEN)); 436 437 /* Don't destroy a mounted dataset unless force is specified */ 438 if ((mounted = zfs_is_mounted(fs, NULL)) != 0) { 439 if (force) { 440 zfs_unmount(fs, NULL, 0); 441 } else { 442 free(bdd.snapname); 443 return (set_error(lbh, BE_ERR_DESTROYMNT)); 444 } 445 } 446 } else { 447 /* 448 * If we're initially destroying a snapshot, origin options do 449 * not make sense. If we're destroying the origin snapshot of 450 * a BE, we want to maintain the options in case we need to 451 * fake success after failing to promote. 452 */ 453 if (!odestroyer) 454 options &= ~BE_DESTROY_WANTORIGIN; 455 if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_SNAPSHOT)) 456 return (set_error(lbh, BE_ERR_NOENT)); 457 458 bdd.snapname = strdup(snapdelim + 1); 459 if (bdd.snapname == NULL) 460 return (set_error(lbh, BE_ERR_NOMEM)); 461 *snapdelim = '\0'; 462 fs = zfs_open(lbh->lzh, path, ZFS_TYPE_DATASET); 463 if (fs == NULL) { 464 free(bdd.snapname); 465 return (set_error(lbh, BE_ERR_ZFSOPEN)); 466 } 467 } 468 469 /* 470 * Whether we're destroying a BE or a single snapshot, we need to walk 471 * the tree of what we're going to destroy and promote everything in our 472 * path so that we can make it happen. 473 */ 474 if ((err = be_promote_dependent_clones(fs, &bdd)) != 0) { 475 free(bdd.snapname); 476 477 /* 478 * If we're just destroying the origin of some other dataset 479 * we were invoked to destroy, then we just ignore 480 * BE_ERR_HASCLONES and return success unless the caller wanted 481 * to force the issue. 482 */ 483 if (odestroyer && err == BE_ERR_HASCLONES && 484 (options & BE_DESTROY_AUTOORIGIN) != 0) 485 return (0); 486 return (set_error(lbh, err)); 487 } 488 489 /* 490 * This was deferred until after we promote all of the derivatives so 491 * that we grab the new origin after everything's settled down. 492 */ 493 if ((options & BE_DESTROY_WANTORIGIN) != 0 && 494 zfs_prop_get(fs, ZFS_PROP_ORIGIN, origin, sizeof(origin), 495 NULL, NULL, 0, 1) != 0 && 496 (options & BE_DESTROY_ORIGIN) != 0) 497 return (set_error(lbh, BE_ERR_NOORIGIN)); 498 499 /* 500 * If the caller wants auto-origin destruction and the origin 501 * name matches one of our automatically created snapshot names 502 * (i.e. strftime("%F-%T") with a serial at the end), then 503 * we'll set the DESTROY_ORIGIN flag and nuke it 504 * be_is_auto_snapshot_name is exported from libbe(3) so that 505 * the caller can determine if it needs to warn about the origin 506 * not being destroyed or not. 507 */ 508 if ((options & BE_DESTROY_AUTOORIGIN) != 0 && *origin != '\0' && 509 be_is_auto_snapshot_name(lbh, origin)) 510 options |= BE_DESTROY_ORIGIN; 511 512 err = be_destroy_cb(fs, &bdd); 513 zfs_close(fs); 514 free(bdd.snapname); 515 if (err != 0) { 516 /* Children are still present or the mount is referenced */ 517 if (err == EBUSY) 518 return (set_error(lbh, BE_ERR_DESTROYMNT)); 519 return (set_error(lbh, BE_ERR_UNKNOWN)); 520 } 521 522 if ((options & BE_DESTROY_ORIGIN) == 0) 523 return (0); 524 525 /* The origin can't possibly be shorter than the BE root */ 526 rootlen = strlen(lbh->root); 527 if (*origin == '\0' || strlen(origin) <= rootlen + 1) 528 return (set_error(lbh, BE_ERR_INVORIGIN)); 529 530 /* 531 * We'll be chopping off the BE root and running this back through 532 * be_destroy, so that we properly handle the origin snapshot whether 533 * it be that of a deep BE or not. 534 */ 535 if (strncmp(origin, lbh->root, rootlen) != 0 || origin[rootlen] != '/') 536 return (0); 537 538 return (be_destroy_internal(lbh, origin + rootlen + 1, 539 options & ~BE_DESTROY_ORIGIN, true)); 540 } 541 542 int 543 be_destroy(libbe_handle_t *lbh, const char *name, int options) 544 { 545 546 /* 547 * The consumer must not set both BE_DESTROY_AUTOORIGIN and 548 * BE_DESTROY_ORIGIN. Internally, we'll set the latter from the former. 549 * The latter should imply that we must succeed at destroying the 550 * origin, or complain otherwise. 551 */ 552 if ((options & BE_DESTROY_WANTORIGIN) == BE_DESTROY_WANTORIGIN) 553 return (set_error(lbh, BE_ERR_UNKNOWN)); 554 return (be_destroy_internal(lbh, name, options, false)); 555 } 556 557 static void 558 be_setup_snapshot_name(libbe_handle_t *lbh, char *buf, size_t buflen) 559 { 560 time_t rawtime; 561 int len, serial; 562 563 time(&rawtime); 564 len = strlen(buf); 565 len += strftime(buf + len, buflen - len, "@%F-%T", localtime(&rawtime)); 566 /* No room for serial... caller will do its best */ 567 if (buflen - len < 2) 568 return; 569 570 for (serial = 0; serial < BE_SNAP_SERIAL_MAX; ++serial) { 571 snprintf(buf + len, buflen - len, "-%d", serial); 572 if (!zfs_dataset_exists(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT)) 573 return; 574 } 575 } 576 577 bool 578 be_is_auto_snapshot_name(libbe_handle_t *lbh __unused, const char *name) 579 { 580 const char *snap; 581 int day, hour, minute, month, second, serial, year; 582 583 if ((snap = strchr(name, '@')) == NULL) 584 return (false); 585 ++snap; 586 /* We'll grab the individual components and do some light validation. */ 587 if (sscanf(snap, "%d-%d-%d-%d:%d:%d-%d", &year, &month, &day, &hour, 588 &minute, &second, &serial) != 7) 589 return (false); 590 return (year >= 1970) && (month >= 1 && month <= 12) && 591 (day >= 1 && day <= 31) && (hour >= 0 && hour <= 23) && 592 (minute >= 0 && minute <= 59) && (second >= 0 && second <= 60) && 593 serial >= 0; 594 } 595 596 int 597 be_snapshot(libbe_handle_t *lbh, const char *source, const char *snap_name, 598 bool recursive, char *result) 599 { 600 char buf[BE_MAXPATHLEN]; 601 int err; 602 603 be_root_concat(lbh, source, buf); 604 605 if ((err = be_exists(lbh, buf)) != 0) 606 return (set_error(lbh, err)); 607 608 if (snap_name != NULL) { 609 if (strlcat(buf, "@", sizeof(buf)) >= sizeof(buf)) 610 return (set_error(lbh, BE_ERR_INVALIDNAME)); 611 612 if (strlcat(buf, snap_name, sizeof(buf)) >= sizeof(buf)) 613 return (set_error(lbh, BE_ERR_INVALIDNAME)); 614 615 if (result != NULL) 616 snprintf(result, BE_MAXPATHLEN, "%s@%s", source, 617 snap_name); 618 } else { 619 be_setup_snapshot_name(lbh, buf, sizeof(buf)); 620 621 if (result != NULL && strlcpy(result, strrchr(buf, '/') + 1, 622 sizeof(buf)) >= sizeof(buf)) 623 return (set_error(lbh, BE_ERR_INVALIDNAME)); 624 } 625 if ((err = zfs_snapshot(lbh->lzh, buf, recursive, NULL)) != 0) { 626 switch (err) { 627 case EZFS_INVALIDNAME: 628 return (set_error(lbh, BE_ERR_INVALIDNAME)); 629 630 default: 631 /* 632 * The other errors that zfs_ioc_snapshot might return 633 * shouldn't happen if we've set things up properly, so 634 * we'll gloss over them and call it UNKNOWN as it will 635 * require further triage. 636 */ 637 if (errno == ENOTSUP) 638 return (set_error(lbh, BE_ERR_NOPOOL)); 639 return (set_error(lbh, BE_ERR_UNKNOWN)); 640 } 641 } 642 643 return (BE_ERR_SUCCESS); 644 } 645 646 647 /* 648 * Create the boot environment specified by the name parameter 649 */ 650 int 651 be_create(libbe_handle_t *lbh, const char *name) 652 { 653 int err; 654 655 err = be_create_from_existing(lbh, name, be_active_path(lbh)); 656 657 return (set_error(lbh, err)); 658 } 659 660 static int 661 be_deep_clone_prop(int prop, void *cb) 662 { 663 int err; 664 struct libbe_dccb *dccb; 665 zprop_source_t src; 666 char pval[BE_MAXPATHLEN]; 667 char source[BE_MAXPATHLEN]; 668 char *val; 669 670 dccb = cb; 671 /* Skip some properties we don't want to touch */ 672 if (prop == ZFS_PROP_CANMOUNT) 673 return (ZPROP_CONT); 674 675 /* Don't copy readonly properties */ 676 if (zfs_prop_readonly(prop)) 677 return (ZPROP_CONT); 678 679 if ((err = zfs_prop_get(dccb->zhp, prop, (char *)&pval, 680 sizeof(pval), &src, (char *)&source, sizeof(source), false))) 681 /* Just continue if we fail to read a property */ 682 return (ZPROP_CONT); 683 684 /* 685 * Only copy locally defined or received properties. This continues 686 * to avoid temporary/default/local properties intentionally without 687 * breaking received datasets. 688 */ 689 if (src != ZPROP_SRC_LOCAL && src != ZPROP_SRC_RECEIVED) 690 return (ZPROP_CONT); 691 692 /* Augment mountpoint with altroot, if needed */ 693 val = pval; 694 if (prop == ZFS_PROP_MOUNTPOINT) 695 val = be_mountpoint_augmented(dccb->lbh, val); 696 697 nvlist_add_string(dccb->props, zfs_prop_to_name(prop), val); 698 699 return (ZPROP_CONT); 700 } 701 702 /* 703 * Return the corresponding boot environment path for a given 704 * dataset path, the constructed path is placed in 'result'. 705 * 706 * example: say our new boot environment name is 'bootenv' and 707 * the dataset path is 'zroot/ROOT/default/data/set'. 708 * 709 * result should produce: 'zroot/ROOT/bootenv/data/set' 710 */ 711 static int 712 be_get_path(struct libbe_deep_clone *ldc, const char *dspath, char *result, int result_size) 713 { 714 char *pos; 715 char *child_dataset; 716 717 /* match the root path for the boot environments */ 718 pos = strstr(dspath, ldc->lbh->root); 719 720 /* no match, different pools? */ 721 if (pos == NULL) 722 return (BE_ERR_BADPATH); 723 724 /* root path of the new boot environment */ 725 snprintf(result, result_size, "%s/%s", ldc->lbh->root, ldc->bename); 726 727 /* gets us to the parent dataset, the +1 consumes a trailing slash */ 728 pos += strlen(ldc->lbh->root) + 1; 729 730 /* skip the parent dataset */ 731 if ((child_dataset = strchr(pos, '/')) != NULL) 732 strlcat(result, child_dataset, result_size); 733 734 return (BE_ERR_SUCCESS); 735 } 736 737 static int 738 be_clone_cb(zfs_handle_t *ds, void *data) 739 { 740 int err; 741 char be_path[BE_MAXPATHLEN]; 742 char snap_path[BE_MAXPATHLEN]; 743 const char *dspath; 744 zfs_handle_t *snap_hdl; 745 nvlist_t *props; 746 struct libbe_deep_clone *ldc; 747 struct libbe_dccb dccb; 748 749 ldc = (struct libbe_deep_clone *)data; 750 dspath = zfs_get_name(ds); 751 752 snprintf(snap_path, sizeof(snap_path), "%s@%s", dspath, ldc->snapname); 753 754 /* construct the boot environment path from the dataset we're cloning */ 755 if (be_get_path(ldc, dspath, be_path, sizeof(be_path)) != BE_ERR_SUCCESS) 756 return (BE_ERR_UNKNOWN); 757 758 /* the dataset to be created (i.e. the boot environment) already exists */ 759 if (zfs_dataset_exists(ldc->lbh->lzh, be_path, ZFS_TYPE_DATASET)) 760 return (BE_ERR_EXISTS); 761 762 /* no snapshot found for this dataset, silently skip it */ 763 if (!zfs_dataset_exists(ldc->lbh->lzh, snap_path, ZFS_TYPE_SNAPSHOT)) 764 return (0); 765 766 if ((snap_hdl = 767 zfs_open(ldc->lbh->lzh, snap_path, ZFS_TYPE_SNAPSHOT)) == NULL) 768 return (BE_ERR_ZFSOPEN); 769 770 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP); 771 nvlist_add_string(props, "canmount", "noauto"); 772 773 dccb.lbh = ldc->lbh; 774 dccb.zhp = ds; 775 dccb.props = props; 776 if (zprop_iter(be_deep_clone_prop, &dccb, B_FALSE, B_FALSE, 777 ZFS_TYPE_FILESYSTEM) == ZPROP_INVAL) 778 return (-1); 779 780 if ((err = zfs_clone(snap_hdl, be_path, props)) != 0) 781 return (BE_ERR_ZFSCLONE); 782 783 nvlist_free(props); 784 zfs_close(snap_hdl); 785 786 if (ldc->depth_limit == -1 || ldc->depth < ldc->depth_limit) { 787 ldc->depth++; 788 err = zfs_iter_filesystems(ds, be_clone_cb, ldc); 789 ldc->depth--; 790 } 791 792 return (err); 793 } 794 795 /* 796 * Create a boot environment with a given name from a given snapshot. 797 * Snapshots can be in the format 'zroot/ROOT/default@snapshot' or 798 * 'default@snapshot'. In the latter case, 'default@snapshot' will be prepended 799 * with the root path that libbe was initailized with. 800 */ 801 static int 802 be_clone(libbe_handle_t *lbh, const char *bename, const char *snapshot, int depth) 803 { 804 int err; 805 char snap_path[BE_MAXPATHLEN]; 806 char *parentname, *snapname; 807 zfs_handle_t *parent_hdl; 808 struct libbe_deep_clone ldc; 809 810 /* ensure the boot environment name is valid */ 811 if ((err = be_validate_name(lbh, bename)) != 0) 812 return (set_error(lbh, err)); 813 814 /* 815 * prepend the boot environment root path if we're 816 * given a partial snapshot name. 817 */ 818 if ((err = be_root_concat(lbh, snapshot, snap_path)) != 0) 819 return (set_error(lbh, err)); 820 821 /* ensure the snapshot exists */ 822 if ((err = be_validate_snap(lbh, snap_path)) != 0) 823 return (set_error(lbh, err)); 824 825 /* get a copy of the snapshot path so we can disect it */ 826 if ((parentname = strdup(snap_path)) == NULL) 827 return (set_error(lbh, BE_ERR_UNKNOWN)); 828 829 /* split dataset name from snapshot name */ 830 snapname = strchr(parentname, '@'); 831 if (snapname == NULL) { 832 free(parentname); 833 return (set_error(lbh, BE_ERR_UNKNOWN)); 834 } 835 *snapname = '\0'; 836 snapname++; 837 838 /* set-up the boot environment */ 839 ldc.lbh = lbh; 840 ldc.bename = bename; 841 ldc.snapname = snapname; 842 ldc.depth = 0; 843 ldc.depth_limit = depth; 844 845 /* the boot environment will be cloned from this dataset */ 846 parent_hdl = zfs_open(lbh->lzh, parentname, ZFS_TYPE_DATASET); 847 848 /* create the boot environment */ 849 err = be_clone_cb(parent_hdl, &ldc); 850 851 free(parentname); 852 return (set_error(lbh, err)); 853 } 854 855 /* 856 * Create a boot environment from pre-existing snapshot, specifying a depth. 857 */ 858 int be_create_depth(libbe_handle_t *lbh, const char *bename, 859 const char *snap, int depth) 860 { 861 return (be_clone(lbh, bename, snap, depth)); 862 } 863 864 /* 865 * Create the boot environment from pre-existing snapshot 866 */ 867 int 868 be_create_from_existing_snap(libbe_handle_t *lbh, const char *bename, 869 const char *snap) 870 { 871 return (be_clone(lbh, bename, snap, -1)); 872 } 873 874 875 /* 876 * Create a boot environment from an existing boot environment 877 */ 878 int 879 be_create_from_existing(libbe_handle_t *lbh, const char *bename, const char *old) 880 { 881 int err; 882 char snap[BE_MAXPATHLEN]; 883 884 if ((err = be_snapshot(lbh, old, NULL, true, snap)) != 0) 885 return (set_error(lbh, err)); 886 887 err = be_clone(lbh, bename, snap, -1); 888 889 return (set_error(lbh, err)); 890 } 891 892 893 /* 894 * Verifies that a snapshot has a valid name, exists, and has a mountpoint of 895 * '/'. Returns BE_ERR_SUCCESS (0), upon success, or the relevant BE_ERR_* upon 896 * failure. Does not set the internal library error state. 897 */ 898 int 899 be_validate_snap(libbe_handle_t *lbh, const char *snap_name) 900 { 901 902 if (strlen(snap_name) >= BE_MAXPATHLEN) 903 return (BE_ERR_PATHLEN); 904 905 if (!zfs_name_valid(snap_name, ZFS_TYPE_SNAPSHOT)) 906 return (BE_ERR_INVALIDNAME); 907 908 if (!zfs_dataset_exists(lbh->lzh, snap_name, 909 ZFS_TYPE_SNAPSHOT)) 910 return (BE_ERR_NOENT); 911 912 return (BE_ERR_SUCCESS); 913 } 914 915 916 /* 917 * Idempotently appends the name argument to the root boot environment path 918 * and copies the resulting string into the result buffer (which is assumed 919 * to be at least BE_MAXPATHLEN characters long. Returns BE_ERR_SUCCESS upon 920 * success, BE_ERR_PATHLEN if the resulting path is longer than BE_MAXPATHLEN, 921 * or BE_ERR_INVALIDNAME if the name is a path that does not begin with 922 * zfs_be_root. Does not set internal library error state. 923 */ 924 int 925 be_root_concat(libbe_handle_t *lbh, const char *name, char *result) 926 { 927 size_t name_len, root_len; 928 929 name_len = strlen(name); 930 root_len = strlen(lbh->root); 931 932 /* Act idempotently; return be name if it is already a full path */ 933 if (strrchr(name, '/') != NULL) { 934 if (strstr(name, lbh->root) != name) 935 return (BE_ERR_INVALIDNAME); 936 937 if (name_len >= BE_MAXPATHLEN) 938 return (BE_ERR_PATHLEN); 939 940 strlcpy(result, name, BE_MAXPATHLEN); 941 return (BE_ERR_SUCCESS); 942 } else if (name_len + root_len + 1 < BE_MAXPATHLEN) { 943 snprintf(result, BE_MAXPATHLEN, "%s/%s", lbh->root, 944 name); 945 return (BE_ERR_SUCCESS); 946 } 947 948 return (BE_ERR_PATHLEN); 949 } 950 951 952 /* 953 * Verifies the validity of a boot environment name (A-Za-z0-9-_.). Returns 954 * BE_ERR_SUCCESS (0) if name is valid, otherwise returns BE_ERR_INVALIDNAME 955 * or BE_ERR_PATHLEN. 956 * Does not set internal library error state. 957 */ 958 int 959 be_validate_name(libbe_handle_t *lbh, const char *name) 960 { 961 962 /* 963 * Impose the additional restriction that the entire dataset name must 964 * not exceed the maximum length of a dataset, i.e. MAXNAMELEN. 965 */ 966 if (strlen(lbh->root) + 1 + strlen(name) > MAXNAMELEN) 967 return (BE_ERR_PATHLEN); 968 969 if (!zfs_name_valid(name, ZFS_TYPE_DATASET)) 970 return (BE_ERR_INVALIDNAME); 971 972 /* 973 * ZFS allows spaces in boot environment names, but the kernel can't 974 * handle booting from such a dataset right now. vfs.root.mountfrom 975 * is defined to be a space-separated list, and there's no protocol for 976 * escaping whitespace in the path component of a dev:path spec. So 977 * while loader can handle this situation alright, it can't safely pass 978 * it on to mountroot. 979 */ 980 if (strchr(name, ' ') != NULL) 981 return (BE_ERR_INVALIDNAME); 982 983 return (BE_ERR_SUCCESS); 984 } 985 986 987 /* 988 * usage 989 */ 990 int 991 be_rename(libbe_handle_t *lbh, const char *old, const char *new) 992 { 993 char full_old[BE_MAXPATHLEN]; 994 char full_new[BE_MAXPATHLEN]; 995 zfs_handle_t *zfs_hdl; 996 int err; 997 998 /* 999 * be_validate_name is documented not to set error state, so we should 1000 * do so here. 1001 */ 1002 if ((err = be_validate_name(lbh, new)) != 0) 1003 return (set_error(lbh, err)); 1004 if ((err = be_root_concat(lbh, old, full_old)) != 0) 1005 return (set_error(lbh, err)); 1006 if ((err = be_root_concat(lbh, new, full_new)) != 0) 1007 return (set_error(lbh, err)); 1008 1009 if (!zfs_dataset_exists(lbh->lzh, full_old, ZFS_TYPE_DATASET)) 1010 return (set_error(lbh, BE_ERR_NOENT)); 1011 1012 if (zfs_dataset_exists(lbh->lzh, full_new, ZFS_TYPE_DATASET)) 1013 return (set_error(lbh, BE_ERR_EXISTS)); 1014 1015 if ((zfs_hdl = zfs_open(lbh->lzh, full_old, 1016 ZFS_TYPE_FILESYSTEM)) == NULL) 1017 return (set_error(lbh, BE_ERR_ZFSOPEN)); 1018 1019 /* recurse, nounmount, forceunmount */ 1020 struct renameflags flags = { 1021 .nounmount = 1, 1022 }; 1023 err = zfs_rename(zfs_hdl, full_new, flags); 1024 1025 zfs_close(zfs_hdl); 1026 if (err != 0) 1027 return (set_error(lbh, BE_ERR_UNKNOWN)); 1028 return (0); 1029 } 1030 1031 1032 int 1033 be_export(libbe_handle_t *lbh, const char *bootenv, int fd) 1034 { 1035 char snap_name[BE_MAXPATHLEN]; 1036 char buf[BE_MAXPATHLEN]; 1037 zfs_handle_t *zfs; 1038 sendflags_t flags = { 0 }; 1039 int err; 1040 1041 if ((err = be_snapshot(lbh, bootenv, NULL, true, snap_name)) != 0) 1042 /* Use the error set by be_snapshot */ 1043 return (err); 1044 1045 be_root_concat(lbh, snap_name, buf); 1046 1047 if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL) 1048 return (set_error(lbh, BE_ERR_ZFSOPEN)); 1049 1050 err = zfs_send_one(zfs, NULL, fd, &flags, /* redactbook */ NULL); 1051 zfs_close(zfs); 1052 1053 return (err); 1054 } 1055 1056 1057 int 1058 be_import(libbe_handle_t *lbh, const char *bootenv, int fd) 1059 { 1060 char buf[BE_MAXPATHLEN]; 1061 nvlist_t *props; 1062 zfs_handle_t *zfs; 1063 recvflags_t flags = { .nomount = 1 }; 1064 int err; 1065 1066 be_root_concat(lbh, bootenv, buf); 1067 1068 if ((err = zfs_receive(lbh->lzh, buf, NULL, &flags, fd, NULL)) != 0) { 1069 switch (err) { 1070 case EINVAL: 1071 return (set_error(lbh, BE_ERR_NOORIGIN)); 1072 case ENOENT: 1073 return (set_error(lbh, BE_ERR_NOENT)); 1074 case EIO: 1075 return (set_error(lbh, BE_ERR_IO)); 1076 default: 1077 return (set_error(lbh, BE_ERR_UNKNOWN)); 1078 } 1079 } 1080 1081 if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_FILESYSTEM)) == NULL) 1082 return (set_error(lbh, BE_ERR_ZFSOPEN)); 1083 1084 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP); 1085 nvlist_add_string(props, "canmount", "noauto"); 1086 nvlist_add_string(props, "mountpoint", "none"); 1087 1088 err = zfs_prop_set_list(zfs, props); 1089 nvlist_free(props); 1090 1091 zfs_close(zfs); 1092 1093 if (err != 0) 1094 return (set_error(lbh, BE_ERR_UNKNOWN)); 1095 1096 return (0); 1097 } 1098 1099 #if SOON 1100 static int 1101 be_create_child_noent(libbe_handle_t *lbh, const char *active, 1102 const char *child_path) 1103 { 1104 nvlist_t *props; 1105 zfs_handle_t *zfs; 1106 int err; 1107 1108 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP); 1109 nvlist_add_string(props, "canmount", "noauto"); 1110 nvlist_add_string(props, "mountpoint", child_path); 1111 1112 /* Create */ 1113 if ((err = zfs_create(lbh->lzh, active, ZFS_TYPE_DATASET, 1114 props)) != 0) { 1115 switch (err) { 1116 case EZFS_EXISTS: 1117 return (set_error(lbh, BE_ERR_EXISTS)); 1118 case EZFS_NOENT: 1119 return (set_error(lbh, BE_ERR_NOENT)); 1120 case EZFS_BADTYPE: 1121 case EZFS_BADVERSION: 1122 return (set_error(lbh, BE_ERR_NOPOOL)); 1123 case EZFS_BADPROP: 1124 default: 1125 /* We set something up wrong, probably... */ 1126 return (set_error(lbh, BE_ERR_UNKNOWN)); 1127 } 1128 } 1129 nvlist_free(props); 1130 1131 if ((zfs = zfs_open(lbh->lzh, active, ZFS_TYPE_DATASET)) == NULL) 1132 return (set_error(lbh, BE_ERR_ZFSOPEN)); 1133 1134 /* Set props */ 1135 if ((err = zfs_prop_set(zfs, "canmount", "noauto")) != 0) { 1136 zfs_close(zfs); 1137 /* 1138 * Similar to other cases, this shouldn't fail unless we've 1139 * done something wrong. This is a new dataset that shouldn't 1140 * have been mounted anywhere between creation and now. 1141 */ 1142 if (err == EZFS_NOMEM) 1143 return (set_error(lbh, BE_ERR_NOMEM)); 1144 return (set_error(lbh, BE_ERR_UNKNOWN)); 1145 } 1146 zfs_close(zfs); 1147 return (BE_ERR_SUCCESS); 1148 } 1149 1150 static int 1151 be_create_child_cloned(libbe_handle_t *lbh, const char *active) 1152 { 1153 char buf[BE_MAXPATHLEN], tmp[BE_MAXPATHLEN];; 1154 zfs_handle_t *zfs; 1155 int err; 1156 1157 /* XXX TODO ? */ 1158 1159 /* 1160 * Establish if the existing path is a zfs dataset or just 1161 * the subdirectory of one 1162 */ 1163 strlcpy(tmp, "tmp/be_snap.XXXXX", sizeof(tmp)); 1164 if (mktemp(tmp) == NULL) 1165 return (set_error(lbh, BE_ERR_UNKNOWN)); 1166 1167 be_root_concat(lbh, tmp, buf); 1168 printf("Here %s?\n", buf); 1169 if ((err = zfs_snapshot(lbh->lzh, buf, false, NULL)) != 0) { 1170 switch (err) { 1171 case EZFS_INVALIDNAME: 1172 return (set_error(lbh, BE_ERR_INVALIDNAME)); 1173 1174 default: 1175 /* 1176 * The other errors that zfs_ioc_snapshot might return 1177 * shouldn't happen if we've set things up properly, so 1178 * we'll gloss over them and call it UNKNOWN as it will 1179 * require further triage. 1180 */ 1181 if (errno == ENOTSUP) 1182 return (set_error(lbh, BE_ERR_NOPOOL)); 1183 return (set_error(lbh, BE_ERR_UNKNOWN)); 1184 } 1185 } 1186 1187 /* Clone */ 1188 if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT)) == NULL) 1189 return (BE_ERR_ZFSOPEN); 1190 1191 if ((err = zfs_clone(zfs, active, NULL)) != 0) 1192 /* XXX TODO correct error */ 1193 return (set_error(lbh, BE_ERR_UNKNOWN)); 1194 1195 /* set props */ 1196 zfs_close(zfs); 1197 return (BE_ERR_SUCCESS); 1198 } 1199 1200 int 1201 be_add_child(libbe_handle_t *lbh, const char *child_path, bool cp_if_exists) 1202 { 1203 struct stat sb; 1204 char active[BE_MAXPATHLEN], buf[BE_MAXPATHLEN]; 1205 nvlist_t *props; 1206 const char *s; 1207 1208 /* Require absolute paths */ 1209 if (*child_path != '/') 1210 return (set_error(lbh, BE_ERR_BADPATH)); 1211 1212 strlcpy(active, be_active_path(lbh), BE_MAXPATHLEN); 1213 strcpy(buf, active); 1214 1215 /* Create non-mountable parent dataset(s) */ 1216 s = child_path; 1217 for (char *p; (p = strchr(s+1, '/')) != NULL; s = p) { 1218 size_t len = p - s; 1219 strncat(buf, s, len); 1220 1221 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP); 1222 nvlist_add_string(props, "canmount", "off"); 1223 nvlist_add_string(props, "mountpoint", "none"); 1224 zfs_create(lbh->lzh, buf, ZFS_TYPE_DATASET, props); 1225 nvlist_free(props); 1226 } 1227 1228 /* Path does not exist as a descendent of / yet */ 1229 if (strlcat(active, child_path, BE_MAXPATHLEN) >= BE_MAXPATHLEN) 1230 return (set_error(lbh, BE_ERR_PATHLEN)); 1231 1232 if (stat(child_path, &sb) != 0) { 1233 /* Verify that error is ENOENT */ 1234 if (errno != ENOENT) 1235 return (set_error(lbh, BE_ERR_UNKNOWN)); 1236 return (be_create_child_noent(lbh, active, child_path)); 1237 } else if (cp_if_exists) 1238 /* Path is already a descendent of / and should be copied */ 1239 return (be_create_child_cloned(lbh, active)); 1240 return (set_error(lbh, BE_ERR_EXISTS)); 1241 } 1242 #endif /* SOON */ 1243 1244 /* 1245 * Deactivate old BE dataset; currently just sets canmount=noauto or 1246 * resets boot once configuration. 1247 */ 1248 int 1249 be_deactivate(libbe_handle_t *lbh, const char *ds, bool temporary) 1250 { 1251 zfs_handle_t *zfs; 1252 1253 if (temporary) { 1254 return (lzbe_set_boot_device( 1255 zpool_get_name(lbh->active_phandle), lzbe_add, NULL)); 1256 } 1257 1258 if ((zfs = zfs_open(lbh->lzh, ds, ZFS_TYPE_DATASET)) == NULL) 1259 return (1); 1260 if (zfs_prop_set(zfs, "canmount", "noauto") != 0) 1261 return (1); 1262 zfs_close(zfs); 1263 return (0); 1264 } 1265 1266 static int 1267 be_zfs_promote_cb(zfs_handle_t *zhp, void *data) 1268 { 1269 char origin[BE_MAXPATHLEN]; 1270 bool *found_origin = (bool *)data; 1271 int err; 1272 1273 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin, sizeof(origin), 1274 NULL, NULL, 0, true) == 0) { 1275 *found_origin = true; 1276 err = zfs_promote(zhp); 1277 if (err) 1278 return (err); 1279 } 1280 1281 return (zfs_iter_filesystems(zhp, be_zfs_promote_cb, data)); 1282 } 1283 1284 static int 1285 be_zfs_promote(zfs_handle_t *zhp, bool *found_origin) 1286 { 1287 *found_origin = false; 1288 return (be_zfs_promote_cb(zhp, (void *)found_origin)); 1289 } 1290 1291 int 1292 be_activate(libbe_handle_t *lbh, const char *bootenv, bool temporary) 1293 { 1294 char be_path[BE_MAXPATHLEN]; 1295 zfs_handle_t *zhp; 1296 int err; 1297 bool found_origin; 1298 1299 be_root_concat(lbh, bootenv, be_path); 1300 1301 /* Note: be_exists fails if mountpoint is not / */ 1302 if ((err = be_exists(lbh, be_path)) != 0) 1303 return (set_error(lbh, err)); 1304 1305 if (temporary) { 1306 return (lzbe_set_boot_device( 1307 zpool_get_name(lbh->active_phandle), lzbe_add, be_path)); 1308 } else { 1309 if (strncmp(lbh->bootfs, "-", 1) != 0 && 1310 be_deactivate(lbh, lbh->bootfs, false) != 0) 1311 return (-1); 1312 1313 /* Obtain bootenv zpool */ 1314 err = zpool_set_prop(lbh->active_phandle, "bootfs", be_path); 1315 if (err) 1316 return (-1); 1317 1318 for (;;) { 1319 zhp = zfs_open(lbh->lzh, be_path, ZFS_TYPE_FILESYSTEM); 1320 if (zhp == NULL) 1321 return (-1); 1322 1323 err = be_zfs_promote(zhp, &found_origin); 1324 1325 zfs_close(zhp); 1326 if (!found_origin) 1327 break; 1328 if (err) 1329 return (err); 1330 } 1331 1332 if (err) 1333 return (-1); 1334 } 1335 1336 return (BE_ERR_SUCCESS); 1337 } 1338