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