1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/mount.h> 34 #include <sys/stat.h> 35 #include <sys/ucred.h> 36 37 #include <ctype.h> 38 #include <libgen.h> 39 #include <libzfs_core.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <time.h> 43 #include <unistd.h> 44 45 #include "be.h" 46 #include "be_impl.h" 47 48 struct be_destroy_data { 49 libbe_handle_t *lbh; 50 char *snapname; 51 }; 52 53 #if SOON 54 static int be_create_child_noent(libbe_handle_t *lbh, const char *active, 55 const char *child_path); 56 static int be_create_child_cloned(libbe_handle_t *lbh, const char *active); 57 #endif 58 59 /* Arbitrary... should tune */ 60 #define BE_SNAP_SERIAL_MAX 1024 61 62 /* 63 * Iterator function for locating the rootfs amongst the children of the 64 * zfs_be_root set by loader(8). data is expected to be a libbe_handle_t *. 65 */ 66 static int 67 be_locate_rootfs(libbe_handle_t *lbh) 68 { 69 struct statfs sfs; 70 struct extmnttab entry; 71 zfs_handle_t *zfs; 72 73 /* 74 * Check first if root is ZFS; if not, we'll bail on rootfs capture. 75 * Unfortunately needed because zfs_path_to_zhandle will emit to 76 * stderr if / isn't actually a ZFS filesystem, which we'd like 77 * to avoid. 78 */ 79 if (statfs("/", &sfs) == 0) { 80 statfs2mnttab(&sfs, &entry); 81 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 82 return (1); 83 } else 84 return (1); 85 zfs = zfs_path_to_zhandle(lbh->lzh, "/", ZFS_TYPE_FILESYSTEM); 86 if (zfs == NULL) 87 return (1); 88 89 strlcpy(lbh->rootfs, zfs_get_name(zfs), sizeof(lbh->rootfs)); 90 zfs_close(zfs); 91 return (0); 92 } 93 94 /* 95 * Initializes the libbe context to operate in the root boot environment 96 * dataset, for example, zroot/ROOT. 97 */ 98 libbe_handle_t * 99 libbe_init(const char *root) 100 { 101 char altroot[MAXPATHLEN]; 102 libbe_handle_t *lbh; 103 char *poolname, *pos; 104 int pnamelen; 105 106 lbh = NULL; 107 poolname = pos = NULL; 108 109 if ((lbh = calloc(1, sizeof(libbe_handle_t))) == NULL) 110 goto err; 111 112 if ((lbh->lzh = libzfs_init()) == NULL) 113 goto err; 114 115 /* 116 * Grab rootfs, we'll work backwards from there if an optional BE root 117 * has not been passed in. 118 */ 119 if (be_locate_rootfs(lbh) != 0) { 120 if (root == NULL) 121 goto err; 122 *lbh->rootfs = '\0'; 123 } 124 if (root == NULL) { 125 /* Strip off the final slash from rootfs to get the be root */ 126 strlcpy(lbh->root, lbh->rootfs, sizeof(lbh->root)); 127 pos = strrchr(lbh->root, '/'); 128 if (pos == NULL) 129 goto err; 130 *pos = '\0'; 131 } else 132 strlcpy(lbh->root, root, sizeof(lbh->root)); 133 134 if ((pos = strchr(lbh->root, '/')) == NULL) 135 goto err; 136 137 pnamelen = pos - lbh->root; 138 poolname = malloc(pnamelen + 1); 139 if (poolname == NULL) 140 goto err; 141 142 strlcpy(poolname, lbh->root, pnamelen + 1); 143 if ((lbh->active_phandle = zpool_open(lbh->lzh, poolname)) == NULL) 144 goto err; 145 free(poolname); 146 poolname = NULL; 147 148 if (zpool_get_prop(lbh->active_phandle, ZPOOL_PROP_BOOTFS, lbh->bootfs, 149 sizeof(lbh->bootfs), NULL, true) != 0) 150 goto err; 151 152 if (zpool_get_prop(lbh->active_phandle, ZPOOL_PROP_ALTROOT, 153 altroot, sizeof(altroot), NULL, true) == 0 && 154 strcmp(altroot, "-") != 0) 155 lbh->altroot_len = strlen(altroot); 156 157 return (lbh); 158 err: 159 if (lbh != NULL) { 160 if (lbh->active_phandle != NULL) 161 zpool_close(lbh->active_phandle); 162 if (lbh->lzh != NULL) 163 libzfs_fini(lbh->lzh); 164 free(lbh); 165 } 166 free(poolname); 167 return (NULL); 168 } 169 170 171 /* 172 * Free memory allocated by libbe_init() 173 */ 174 void 175 libbe_close(libbe_handle_t *lbh) 176 { 177 178 if (lbh->active_phandle != NULL) 179 zpool_close(lbh->active_phandle); 180 libzfs_fini(lbh->lzh); 181 free(lbh); 182 } 183 184 /* 185 * Proxy through to libzfs for the moment. 186 */ 187 void 188 be_nicenum(uint64_t num, char *buf, size_t buflen) 189 { 190 191 zfs_nicenum(num, buf, buflen); 192 } 193 194 static int 195 be_destroy_cb(zfs_handle_t *zfs_hdl, void *data) 196 { 197 char path[BE_MAXPATHLEN]; 198 struct be_destroy_data *bdd; 199 zfs_handle_t *snap; 200 int err; 201 202 bdd = (struct be_destroy_data *)data; 203 if (bdd->snapname == NULL) { 204 err = zfs_iter_children(zfs_hdl, be_destroy_cb, data); 205 if (err != 0) 206 return (err); 207 return (zfs_destroy(zfs_hdl, false)); 208 } 209 /* If we're dealing with snapshots instead, delete that one alone */ 210 err = zfs_iter_filesystems(zfs_hdl, be_destroy_cb, data); 211 if (err != 0) 212 return (err); 213 /* 214 * This part is intentionally glossing over any potential errors, 215 * because there's a lot less potential for errors when we're cleaning 216 * up snapshots rather than a full deep BE. The primary error case 217 * here being if the snapshot doesn't exist in the first place, which 218 * the caller will likely deem insignificant as long as it doesn't 219 * exist after the call. Thus, such a missing snapshot shouldn't jam 220 * up the destruction. 221 */ 222 snprintf(path, sizeof(path), "%s@%s", zfs_get_name(zfs_hdl), 223 bdd->snapname); 224 if (!zfs_dataset_exists(bdd->lbh->lzh, path, ZFS_TYPE_SNAPSHOT)) 225 return (0); 226 snap = zfs_open(bdd->lbh->lzh, path, ZFS_TYPE_SNAPSHOT); 227 if (snap != NULL) 228 zfs_destroy(snap, false); 229 return (0); 230 } 231 232 /* 233 * Destroy the boot environment or snapshot specified by the name 234 * parameter. Options are or'd together with the possible values: 235 * BE_DESTROY_FORCE : forces operation on mounted datasets 236 * BE_DESTROY_ORIGIN: destroy the origin snapshot as well 237 */ 238 int 239 be_destroy(libbe_handle_t *lbh, const char *name, int options) 240 { 241 struct be_destroy_data bdd; 242 char origin[BE_MAXPATHLEN], path[BE_MAXPATHLEN]; 243 zfs_handle_t *fs; 244 char *snapdelim; 245 int err, force, mounted; 246 size_t rootlen; 247 248 bdd.lbh = lbh; 249 bdd.snapname = NULL; 250 force = options & BE_DESTROY_FORCE; 251 *origin = '\0'; 252 253 be_root_concat(lbh, name, path); 254 255 if ((snapdelim = strchr(path, '@')) == NULL) { 256 if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_FILESYSTEM)) 257 return (set_error(lbh, BE_ERR_NOENT)); 258 259 if (strcmp(path, lbh->rootfs) == 0 || 260 strcmp(path, lbh->bootfs) == 0) 261 return (set_error(lbh, BE_ERR_DESTROYACT)); 262 263 fs = zfs_open(lbh->lzh, path, ZFS_TYPE_FILESYSTEM); 264 if (fs == NULL) 265 return (set_error(lbh, BE_ERR_ZFSOPEN)); 266 267 if ((options & BE_DESTROY_ORIGIN) != 0 && 268 zfs_prop_get(fs, ZFS_PROP_ORIGIN, origin, sizeof(origin), 269 NULL, NULL, 0, 1) != 0) 270 return (set_error(lbh, BE_ERR_NOORIGIN)); 271 272 /* Don't destroy a mounted dataset unless force is specified */ 273 if ((mounted = zfs_is_mounted(fs, NULL)) != 0) { 274 if (force) { 275 zfs_unmount(fs, NULL, 0); 276 } else { 277 free(bdd.snapname); 278 return (set_error(lbh, BE_ERR_DESTROYMNT)); 279 } 280 } 281 } else { 282 if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_SNAPSHOT)) 283 return (set_error(lbh, BE_ERR_NOENT)); 284 285 bdd.snapname = strdup(snapdelim + 1); 286 if (bdd.snapname == NULL) 287 return (set_error(lbh, BE_ERR_NOMEM)); 288 *snapdelim = '\0'; 289 fs = zfs_open(lbh->lzh, path, ZFS_TYPE_DATASET); 290 if (fs == NULL) { 291 free(bdd.snapname); 292 return (set_error(lbh, BE_ERR_ZFSOPEN)); 293 } 294 } 295 296 err = be_destroy_cb(fs, &bdd); 297 zfs_close(fs); 298 free(bdd.snapname); 299 if (err != 0) { 300 /* Children are still present or the mount is referenced */ 301 if (err == EBUSY) 302 return (set_error(lbh, BE_ERR_DESTROYMNT)); 303 return (set_error(lbh, BE_ERR_UNKNOWN)); 304 } 305 306 if ((options & BE_DESTROY_ORIGIN) == 0) 307 return (0); 308 309 /* The origin can't possibly be shorter than the BE root */ 310 rootlen = strlen(lbh->root); 311 if (*origin == '\0' || strlen(origin) <= rootlen + 1) 312 return (set_error(lbh, BE_ERR_INVORIGIN)); 313 314 /* 315 * We'll be chopping off the BE root and running this back through 316 * be_destroy, so that we properly handle the origin snapshot whether 317 * it be that of a deep BE or not. 318 */ 319 if (strncmp(origin, lbh->root, rootlen) != 0 || origin[rootlen] != '/') 320 return (0); 321 322 return (be_destroy(lbh, origin + rootlen + 1, 323 options & ~BE_DESTROY_ORIGIN)); 324 } 325 326 static void 327 be_setup_snapshot_name(libbe_handle_t *lbh, char *buf, size_t buflen) 328 { 329 time_t rawtime; 330 int len, serial; 331 332 time(&rawtime); 333 len = strlen(buf); 334 len += strftime(buf + len, buflen - len, "@%F-%T", localtime(&rawtime)); 335 /* No room for serial... caller will do its best */ 336 if (buflen - len < 2) 337 return; 338 339 for (serial = 0; serial < BE_SNAP_SERIAL_MAX; ++serial) { 340 snprintf(buf + len, buflen - len, "-%d", serial); 341 if (!zfs_dataset_exists(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT)) 342 return; 343 } 344 } 345 346 int 347 be_snapshot(libbe_handle_t *lbh, const char *source, const char *snap_name, 348 bool recursive, char *result) 349 { 350 char buf[BE_MAXPATHLEN]; 351 int err; 352 353 be_root_concat(lbh, source, buf); 354 355 if ((err = be_exists(lbh, buf)) != 0) 356 return (set_error(lbh, err)); 357 358 if (snap_name != NULL) { 359 if (strlcat(buf, "@", sizeof(buf)) >= sizeof(buf)) 360 return (set_error(lbh, BE_ERR_INVALIDNAME)); 361 362 if (strlcat(buf, snap_name, sizeof(buf)) >= sizeof(buf)) 363 return (set_error(lbh, BE_ERR_INVALIDNAME)); 364 365 if (result != NULL) 366 snprintf(result, BE_MAXPATHLEN, "%s@%s", source, 367 snap_name); 368 } else { 369 be_setup_snapshot_name(lbh, buf, sizeof(buf)); 370 371 if (result != NULL && strlcpy(result, strrchr(buf, '/') + 1, 372 sizeof(buf)) >= sizeof(buf)) 373 return (set_error(lbh, BE_ERR_INVALIDNAME)); 374 } 375 376 if ((err = zfs_snapshot(lbh->lzh, buf, recursive, NULL)) != 0) { 377 switch (err) { 378 case EZFS_INVALIDNAME: 379 return (set_error(lbh, BE_ERR_INVALIDNAME)); 380 381 default: 382 /* 383 * The other errors that zfs_ioc_snapshot might return 384 * shouldn't happen if we've set things up properly, so 385 * we'll gloss over them and call it UNKNOWN as it will 386 * require further triage. 387 */ 388 if (errno == ENOTSUP) 389 return (set_error(lbh, BE_ERR_NOPOOL)); 390 return (set_error(lbh, BE_ERR_UNKNOWN)); 391 } 392 } 393 394 return (BE_ERR_SUCCESS); 395 } 396 397 398 /* 399 * Create the boot environment specified by the name parameter 400 */ 401 int 402 be_create(libbe_handle_t *lbh, const char *name) 403 { 404 int err; 405 406 err = be_create_from_existing(lbh, name, be_active_path(lbh)); 407 408 return (set_error(lbh, err)); 409 } 410 411 static int 412 be_deep_clone_prop(int prop, void *cb) 413 { 414 int err; 415 struct libbe_dccb *dccb; 416 zprop_source_t src; 417 char pval[BE_MAXPATHLEN]; 418 char source[BE_MAXPATHLEN]; 419 char *val; 420 421 dccb = cb; 422 /* Skip some properties we don't want to touch */ 423 if (prop == ZFS_PROP_CANMOUNT) 424 return (ZPROP_CONT); 425 426 /* Don't copy readonly properties */ 427 if (zfs_prop_readonly(prop)) 428 return (ZPROP_CONT); 429 430 if ((err = zfs_prop_get(dccb->zhp, prop, (char *)&pval, 431 sizeof(pval), &src, (char *)&source, sizeof(source), false))) 432 /* Just continue if we fail to read a property */ 433 return (ZPROP_CONT); 434 435 /* Only copy locally defined properties */ 436 if (src != ZPROP_SRC_LOCAL) 437 return (ZPROP_CONT); 438 439 /* Augment mountpoint with altroot, if needed */ 440 val = pval; 441 if (prop == ZFS_PROP_MOUNTPOINT) 442 val = be_mountpoint_augmented(dccb->lbh, val); 443 444 nvlist_add_string(dccb->props, zfs_prop_to_name(prop), val); 445 446 return (ZPROP_CONT); 447 } 448 449 static int 450 be_deep_clone(zfs_handle_t *ds, void *data) 451 { 452 int err; 453 char be_path[BE_MAXPATHLEN]; 454 char snap_path[BE_MAXPATHLEN]; 455 const char *dspath; 456 char *dsname; 457 zfs_handle_t *snap_hdl; 458 nvlist_t *props; 459 struct libbe_deep_clone *isdc, sdc; 460 struct libbe_dccb dccb; 461 462 isdc = (struct libbe_deep_clone *)data; 463 dspath = zfs_get_name(ds); 464 if ((dsname = strrchr(dspath, '/')) == NULL) 465 return (BE_ERR_UNKNOWN); 466 dsname++; 467 468 if (isdc->bename == NULL) 469 snprintf(be_path, sizeof(be_path), "%s/%s", isdc->be_root, dsname); 470 else 471 snprintf(be_path, sizeof(be_path), "%s/%s", isdc->be_root, isdc->bename); 472 473 snprintf(snap_path, sizeof(snap_path), "%s@%s", dspath, isdc->snapname); 474 475 if (zfs_dataset_exists(isdc->lbh->lzh, be_path, ZFS_TYPE_DATASET)) 476 return (set_error(isdc->lbh, BE_ERR_EXISTS)); 477 478 if ((snap_hdl = 479 zfs_open(isdc->lbh->lzh, snap_path, ZFS_TYPE_SNAPSHOT)) == NULL) 480 return (set_error(isdc->lbh, BE_ERR_ZFSOPEN)); 481 482 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP); 483 nvlist_add_string(props, "canmount", "noauto"); 484 485 dccb.lbh = isdc->lbh; 486 dccb.zhp = ds; 487 dccb.props = props; 488 if (zprop_iter(be_deep_clone_prop, &dccb, B_FALSE, B_FALSE, 489 ZFS_TYPE_FILESYSTEM) == ZPROP_INVAL) 490 return (-1); 491 492 if ((err = zfs_clone(snap_hdl, be_path, props)) != 0) 493 err = BE_ERR_ZFSCLONE; 494 495 nvlist_free(props); 496 zfs_close(snap_hdl); 497 498 /* Failed to clone */ 499 if (err != BE_ERR_SUCCESS) 500 return (set_error(isdc->lbh, err)); 501 502 sdc.lbh = isdc->lbh; 503 sdc.bename = NULL; 504 sdc.snapname = isdc->snapname; 505 sdc.be_root = (char *)&be_path; 506 507 err = zfs_iter_filesystems(ds, be_deep_clone, &sdc); 508 509 return (err); 510 } 511 512 /* 513 * Create the boot environment from pre-existing snapshot 514 */ 515 int 516 be_create_from_existing_snap(libbe_handle_t *lbh, const char *name, 517 const char *snap) 518 { 519 int err; 520 char be_path[BE_MAXPATHLEN]; 521 char snap_path[BE_MAXPATHLEN]; 522 const char *bename; 523 char *parentname, *snapname; 524 zfs_handle_t *parent_hdl; 525 struct libbe_deep_clone sdc; 526 527 if ((err = be_validate_name(lbh, name)) != 0) 528 return (set_error(lbh, err)); 529 if ((err = be_root_concat(lbh, snap, snap_path)) != 0) 530 return (set_error(lbh, err)); 531 if ((err = be_validate_snap(lbh, snap_path)) != 0) 532 return (set_error(lbh, err)); 533 534 if ((err = be_root_concat(lbh, name, be_path)) != 0) 535 return (set_error(lbh, err)); 536 537 if ((bename = strrchr(name, '/')) == NULL) 538 bename = name; 539 else 540 bename++; 541 542 if ((parentname = strdup(snap_path)) == NULL) 543 return (set_error(lbh, BE_ERR_UNKNOWN)); 544 545 snapname = strchr(parentname, '@'); 546 if (snapname == NULL) { 547 free(parentname); 548 return (set_error(lbh, BE_ERR_UNKNOWN)); 549 } 550 *snapname = '\0'; 551 snapname++; 552 553 sdc.lbh = lbh; 554 sdc.bename = bename; 555 sdc.snapname = snapname; 556 sdc.be_root = lbh->root; 557 558 parent_hdl = zfs_open(lbh->lzh, parentname, ZFS_TYPE_DATASET); 559 err = be_deep_clone(parent_hdl, &sdc); 560 561 free(parentname); 562 return (set_error(lbh, err)); 563 } 564 565 566 /* 567 * Create a boot environment from an existing boot environment 568 */ 569 int 570 be_create_from_existing(libbe_handle_t *lbh, const char *name, const char *old) 571 { 572 int err; 573 char buf[BE_MAXPATHLEN]; 574 575 if ((err = be_snapshot(lbh, old, NULL, true, (char *)&buf)) != 0) 576 return (set_error(lbh, err)); 577 578 err = be_create_from_existing_snap(lbh, name, (char *)buf); 579 580 return (set_error(lbh, err)); 581 } 582 583 584 /* 585 * Verifies that a snapshot has a valid name, exists, and has a mountpoint of 586 * '/'. Returns BE_ERR_SUCCESS (0), upon success, or the relevant BE_ERR_* upon 587 * failure. Does not set the internal library error state. 588 */ 589 int 590 be_validate_snap(libbe_handle_t *lbh, const char *snap_name) 591 { 592 593 if (strlen(snap_name) >= BE_MAXPATHLEN) 594 return (BE_ERR_PATHLEN); 595 596 if (!zfs_dataset_exists(lbh->lzh, snap_name, 597 ZFS_TYPE_SNAPSHOT)) 598 return (BE_ERR_NOENT); 599 600 return (BE_ERR_SUCCESS); 601 } 602 603 604 /* 605 * Idempotently appends the name argument to the root boot environment path 606 * and copies the resulting string into the result buffer (which is assumed 607 * to be at least BE_MAXPATHLEN characters long. Returns BE_ERR_SUCCESS upon 608 * success, BE_ERR_PATHLEN if the resulting path is longer than BE_MAXPATHLEN, 609 * or BE_ERR_INVALIDNAME if the name is a path that does not begin with 610 * zfs_be_root. Does not set internal library error state. 611 */ 612 int 613 be_root_concat(libbe_handle_t *lbh, const char *name, char *result) 614 { 615 size_t name_len, root_len; 616 617 name_len = strlen(name); 618 root_len = strlen(lbh->root); 619 620 /* Act idempotently; return be name if it is already a full path */ 621 if (strrchr(name, '/') != NULL) { 622 if (strstr(name, lbh->root) != name) 623 return (BE_ERR_INVALIDNAME); 624 625 if (name_len >= BE_MAXPATHLEN) 626 return (BE_ERR_PATHLEN); 627 628 strlcpy(result, name, BE_MAXPATHLEN); 629 return (BE_ERR_SUCCESS); 630 } else if (name_len + root_len + 1 < BE_MAXPATHLEN) { 631 snprintf(result, BE_MAXPATHLEN, "%s/%s", lbh->root, 632 name); 633 return (BE_ERR_SUCCESS); 634 } 635 636 return (BE_ERR_PATHLEN); 637 } 638 639 640 /* 641 * Verifies the validity of a boot environment name (A-Za-z0-9-_.). Returns 642 * BE_ERR_SUCCESS (0) if name is valid, otherwise returns BE_ERR_INVALIDNAME 643 * or BE_ERR_PATHLEN. 644 * Does not set internal library error state. 645 */ 646 int 647 be_validate_name(libbe_handle_t *lbh, const char *name) 648 { 649 for (int i = 0; *name; i++) { 650 char c = *(name++); 651 if (isalnum(c) || (c == '-') || (c == '_') || (c == '.')) 652 continue; 653 return (BE_ERR_INVALIDNAME); 654 } 655 656 /* 657 * Impose the additional restriction that the entire dataset name must 658 * not exceed the maximum length of a dataset, i.e. MAXNAMELEN. 659 */ 660 if (strlen(lbh->root) + 1 + strlen(name) > MAXNAMELEN) 661 return (BE_ERR_PATHLEN); 662 return (BE_ERR_SUCCESS); 663 } 664 665 666 /* 667 * usage 668 */ 669 int 670 be_rename(libbe_handle_t *lbh, const char *old, const char *new) 671 { 672 char full_old[BE_MAXPATHLEN]; 673 char full_new[BE_MAXPATHLEN]; 674 zfs_handle_t *zfs_hdl; 675 int err; 676 677 /* 678 * be_validate_name is documented not to set error state, so we should 679 * do so here. 680 */ 681 if ((err = be_validate_name(lbh, new)) != 0) 682 return (set_error(lbh, err)); 683 if ((err = be_root_concat(lbh, old, full_old)) != 0) 684 return (set_error(lbh, err)); 685 if ((err = be_root_concat(lbh, new, full_new)) != 0) 686 return (set_error(lbh, err)); 687 688 if (!zfs_dataset_exists(lbh->lzh, full_old, ZFS_TYPE_DATASET)) 689 return (set_error(lbh, BE_ERR_NOENT)); 690 691 if (zfs_dataset_exists(lbh->lzh, full_new, ZFS_TYPE_DATASET)) 692 return (set_error(lbh, BE_ERR_EXISTS)); 693 694 if ((zfs_hdl = zfs_open(lbh->lzh, full_old, 695 ZFS_TYPE_FILESYSTEM)) == NULL) 696 return (set_error(lbh, BE_ERR_ZFSOPEN)); 697 698 /* recurse, nounmount, forceunmount */ 699 struct renameflags flags = { 700 .nounmount = 1, 701 }; 702 703 err = zfs_rename(zfs_hdl, NULL, full_new, flags); 704 705 zfs_close(zfs_hdl); 706 if (err != 0) 707 return (set_error(lbh, BE_ERR_UNKNOWN)); 708 return (0); 709 } 710 711 712 int 713 be_export(libbe_handle_t *lbh, const char *bootenv, int fd) 714 { 715 char snap_name[BE_MAXPATHLEN]; 716 char buf[BE_MAXPATHLEN]; 717 zfs_handle_t *zfs; 718 int err; 719 720 if ((err = be_snapshot(lbh, bootenv, NULL, true, snap_name)) != 0) 721 /* Use the error set by be_snapshot */ 722 return (err); 723 724 be_root_concat(lbh, snap_name, buf); 725 726 if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL) 727 return (set_error(lbh, BE_ERR_ZFSOPEN)); 728 729 err = zfs_send_one(zfs, NULL, fd, 0); 730 zfs_close(zfs); 731 732 return (err); 733 } 734 735 736 int 737 be_import(libbe_handle_t *lbh, const char *bootenv, int fd) 738 { 739 char buf[BE_MAXPATHLEN]; 740 nvlist_t *props; 741 zfs_handle_t *zfs; 742 recvflags_t flags = { .nomount = 1 }; 743 int err; 744 745 be_root_concat(lbh, bootenv, buf); 746 747 if ((err = zfs_receive(lbh->lzh, buf, NULL, &flags, fd, NULL)) != 0) { 748 switch (err) { 749 case EINVAL: 750 return (set_error(lbh, BE_ERR_NOORIGIN)); 751 case ENOENT: 752 return (set_error(lbh, BE_ERR_NOENT)); 753 case EIO: 754 return (set_error(lbh, BE_ERR_IO)); 755 default: 756 return (set_error(lbh, BE_ERR_UNKNOWN)); 757 } 758 } 759 760 if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_FILESYSTEM)) == NULL) 761 return (set_error(lbh, BE_ERR_ZFSOPEN)); 762 763 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP); 764 nvlist_add_string(props, "canmount", "noauto"); 765 nvlist_add_string(props, "mountpoint", "/"); 766 767 err = zfs_prop_set_list(zfs, props); 768 nvlist_free(props); 769 770 zfs_close(zfs); 771 772 if (err != 0) 773 return (set_error(lbh, BE_ERR_UNKNOWN)); 774 775 return (0); 776 } 777 778 #if SOON 779 static int 780 be_create_child_noent(libbe_handle_t *lbh, const char *active, 781 const char *child_path) 782 { 783 nvlist_t *props; 784 zfs_handle_t *zfs; 785 int err; 786 787 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP); 788 nvlist_add_string(props, "canmount", "noauto"); 789 nvlist_add_string(props, "mountpoint", child_path); 790 791 /* Create */ 792 if ((err = zfs_create(lbh->lzh, active, ZFS_TYPE_DATASET, 793 props)) != 0) { 794 switch (err) { 795 case EZFS_EXISTS: 796 return (set_error(lbh, BE_ERR_EXISTS)); 797 case EZFS_NOENT: 798 return (set_error(lbh, BE_ERR_NOENT)); 799 case EZFS_BADTYPE: 800 case EZFS_BADVERSION: 801 return (set_error(lbh, BE_ERR_NOPOOL)); 802 case EZFS_BADPROP: 803 default: 804 /* We set something up wrong, probably... */ 805 return (set_error(lbh, BE_ERR_UNKNOWN)); 806 } 807 } 808 nvlist_free(props); 809 810 if ((zfs = zfs_open(lbh->lzh, active, ZFS_TYPE_DATASET)) == NULL) 811 return (set_error(lbh, BE_ERR_ZFSOPEN)); 812 813 /* Set props */ 814 if ((err = zfs_prop_set(zfs, "canmount", "noauto")) != 0) { 815 zfs_close(zfs); 816 /* 817 * Similar to other cases, this shouldn't fail unless we've 818 * done something wrong. This is a new dataset that shouldn't 819 * have been mounted anywhere between creation and now. 820 */ 821 if (err == EZFS_NOMEM) 822 return (set_error(lbh, BE_ERR_NOMEM)); 823 return (set_error(lbh, BE_ERR_UNKNOWN)); 824 } 825 zfs_close(zfs); 826 return (BE_ERR_SUCCESS); 827 } 828 829 static int 830 be_create_child_cloned(libbe_handle_t *lbh, const char *active) 831 { 832 char buf[BE_MAXPATHLEN], tmp[BE_MAXPATHLEN];; 833 zfs_handle_t *zfs; 834 int err; 835 836 /* XXX TODO ? */ 837 838 /* 839 * Establish if the existing path is a zfs dataset or just 840 * the subdirectory of one 841 */ 842 strlcpy(tmp, "tmp/be_snap.XXXXX", sizeof(tmp)); 843 if (mktemp(tmp) == NULL) 844 return (set_error(lbh, BE_ERR_UNKNOWN)); 845 846 be_root_concat(lbh, tmp, buf); 847 printf("Here %s?\n", buf); 848 if ((err = zfs_snapshot(lbh->lzh, buf, false, NULL)) != 0) { 849 switch (err) { 850 case EZFS_INVALIDNAME: 851 return (set_error(lbh, BE_ERR_INVALIDNAME)); 852 853 default: 854 /* 855 * The other errors that zfs_ioc_snapshot might return 856 * shouldn't happen if we've set things up properly, so 857 * we'll gloss over them and call it UNKNOWN as it will 858 * require further triage. 859 */ 860 if (errno == ENOTSUP) 861 return (set_error(lbh, BE_ERR_NOPOOL)); 862 return (set_error(lbh, BE_ERR_UNKNOWN)); 863 } 864 } 865 866 /* Clone */ 867 if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT)) == NULL) 868 return (BE_ERR_ZFSOPEN); 869 870 if ((err = zfs_clone(zfs, active, NULL)) != 0) 871 /* XXX TODO correct error */ 872 return (set_error(lbh, BE_ERR_UNKNOWN)); 873 874 /* set props */ 875 zfs_close(zfs); 876 return (BE_ERR_SUCCESS); 877 } 878 879 int 880 be_add_child(libbe_handle_t *lbh, const char *child_path, bool cp_if_exists) 881 { 882 struct stat sb; 883 char active[BE_MAXPATHLEN], buf[BE_MAXPATHLEN]; 884 nvlist_t *props; 885 const char *s; 886 887 /* Require absolute paths */ 888 if (*child_path != '/') 889 return (set_error(lbh, BE_ERR_BADPATH)); 890 891 strlcpy(active, be_active_path(lbh), BE_MAXPATHLEN); 892 strcpy(buf, active); 893 894 /* Create non-mountable parent dataset(s) */ 895 s = child_path; 896 for (char *p; (p = strchr(s+1, '/')) != NULL; s = p) { 897 size_t len = p - s; 898 strncat(buf, s, len); 899 900 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP); 901 nvlist_add_string(props, "canmount", "off"); 902 nvlist_add_string(props, "mountpoint", "none"); 903 zfs_create(lbh->lzh, buf, ZFS_TYPE_DATASET, props); 904 nvlist_free(props); 905 } 906 907 /* Path does not exist as a descendent of / yet */ 908 if (strlcat(active, child_path, BE_MAXPATHLEN) >= BE_MAXPATHLEN) 909 return (set_error(lbh, BE_ERR_PATHLEN)); 910 911 if (stat(child_path, &sb) != 0) { 912 /* Verify that error is ENOENT */ 913 if (errno != ENOENT) 914 return (set_error(lbh, BE_ERR_UNKNOWN)); 915 return (be_create_child_noent(lbh, active, child_path)); 916 } else if (cp_if_exists) 917 /* Path is already a descendent of / and should be copied */ 918 return (be_create_child_cloned(lbh, active)); 919 return (set_error(lbh, BE_ERR_EXISTS)); 920 } 921 #endif /* SOON */ 922 923 static int 924 be_set_nextboot(libbe_handle_t *lbh, nvlist_t *config, uint64_t pool_guid, 925 const char *zfsdev) 926 { 927 nvlist_t **child; 928 uint64_t vdev_guid; 929 int c, children; 930 931 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, &child, 932 &children) == 0) { 933 for (c = 0; c < children; ++c) 934 if (be_set_nextboot(lbh, child[c], pool_guid, zfsdev) != 0) 935 return (1); 936 return (0); 937 } 938 939 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, 940 &vdev_guid) != 0) { 941 return (1); 942 } 943 944 if (zpool_nextboot(lbh->lzh, pool_guid, vdev_guid, zfsdev) != 0) { 945 perror("ZFS_IOC_NEXTBOOT failed"); 946 return (1); 947 } 948 949 return (0); 950 } 951 952 /* 953 * Deactivate old BE dataset; currently just sets canmount=noauto 954 */ 955 static int 956 be_deactivate(libbe_handle_t *lbh, const char *ds) 957 { 958 zfs_handle_t *zfs; 959 960 if ((zfs = zfs_open(lbh->lzh, ds, ZFS_TYPE_DATASET)) == NULL) 961 return (1); 962 if (zfs_prop_set(zfs, "canmount", "noauto") != 0) 963 return (1); 964 zfs_close(zfs); 965 return (0); 966 } 967 968 int 969 be_activate(libbe_handle_t *lbh, const char *bootenv, bool temporary) 970 { 971 char be_path[BE_MAXPATHLEN]; 972 char buf[BE_MAXPATHLEN]; 973 nvlist_t *config, *dsprops, *vdevs; 974 char *origin; 975 uint64_t pool_guid; 976 zfs_handle_t *zhp; 977 int err; 978 979 be_root_concat(lbh, bootenv, be_path); 980 981 /* Note: be_exists fails if mountpoint is not / */ 982 if ((err = be_exists(lbh, be_path)) != 0) 983 return (set_error(lbh, err)); 984 985 if (temporary) { 986 config = zpool_get_config(lbh->active_phandle, NULL); 987 if (config == NULL) 988 /* config should be fetchable... */ 989 return (set_error(lbh, BE_ERR_UNKNOWN)); 990 991 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 992 &pool_guid) != 0) 993 /* Similarly, it shouldn't be possible */ 994 return (set_error(lbh, BE_ERR_UNKNOWN)); 995 996 /* Expected format according to zfsbootcfg(8) man */ 997 snprintf(buf, sizeof(buf), "zfs:%s:", be_path); 998 999 /* We have no config tree */ 1000 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 1001 &vdevs) != 0) 1002 return (set_error(lbh, BE_ERR_NOPOOL)); 1003 1004 return (be_set_nextboot(lbh, vdevs, pool_guid, buf)); 1005 } else { 1006 if (be_deactivate(lbh, lbh->bootfs) != 0) 1007 return (-1); 1008 1009 /* Obtain bootenv zpool */ 1010 err = zpool_set_prop(lbh->active_phandle, "bootfs", be_path); 1011 if (err) 1012 return (-1); 1013 1014 zhp = zfs_open(lbh->lzh, be_path, ZFS_TYPE_FILESYSTEM); 1015 if (zhp == NULL) 1016 return (-1); 1017 1018 if (be_prop_list_alloc(&dsprops) != 0) 1019 return (-1); 1020 1021 if (be_get_dataset_props(lbh, be_path, dsprops) != 0) { 1022 nvlist_free(dsprops); 1023 return (-1); 1024 } 1025 1026 if (nvlist_lookup_string(dsprops, "origin", &origin) == 0) 1027 err = zfs_promote(zhp); 1028 nvlist_free(dsprops); 1029 1030 zfs_close(zhp); 1031 1032 if (err) 1033 return (-1); 1034 } 1035 1036 return (BE_ERR_SUCCESS); 1037 } 1038