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