xref: /freebsd/lib/libbe/be.c (revision af43c24d3d8bebf0ec88c2c449886cf14039a501)
1b179da01SKyle Evans /*-
2b179da01SKyle Evans  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
328f16a0fSKyle Evans  *
428f16a0fSKyle Evans  * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in>
528f16a0fSKyle Evans  * All rights reserved.
628f16a0fSKyle Evans  *
728f16a0fSKyle Evans  * Redistribution and use in source and binary forms, with or without
828f16a0fSKyle Evans  * modification, are permitted provided that the following conditions
928f16a0fSKyle Evans  * are met:
1028f16a0fSKyle Evans  * 1. Redistributions of source code must retain the above copyright
1128f16a0fSKyle Evans  *    notice, this list of conditions and the following disclaimer.
1228f16a0fSKyle Evans  * 2. Redistributions in binary form must reproduce the above copyright
1328f16a0fSKyle Evans  *    notice, this list of conditions and the following disclaimer in the
1428f16a0fSKyle Evans  *    documentation and/or other materials provided with the distribution.
1528f16a0fSKyle Evans  *
1628f16a0fSKyle Evans  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1728f16a0fSKyle Evans  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1828f16a0fSKyle Evans  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1928f16a0fSKyle Evans  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2028f16a0fSKyle Evans  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2128f16a0fSKyle Evans  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2228f16a0fSKyle Evans  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2328f16a0fSKyle Evans  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2428f16a0fSKyle Evans  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2528f16a0fSKyle Evans  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2628f16a0fSKyle Evans  * SUCH DAMAGE.
2728f16a0fSKyle Evans  */
2828f16a0fSKyle Evans 
29b6e7c421SKyle Evans #include <sys/cdefs.h>
30b6e7c421SKyle Evans __FBSDID("$FreeBSD$");
31b6e7c421SKyle Evans 
3251aecc89SKyle Evans #include <sys/param.h>
3351aecc89SKyle Evans #include <sys/mount.h>
3428f16a0fSKyle Evans #include <sys/stat.h>
3551aecc89SKyle Evans #include <sys/ucred.h>
3628f16a0fSKyle Evans 
3728f16a0fSKyle Evans #include <ctype.h>
3828f16a0fSKyle Evans #include <libgen.h>
3928f16a0fSKyle Evans #include <libzfs_core.h>
4028f16a0fSKyle Evans #include <stdio.h>
4128f16a0fSKyle Evans #include <stdlib.h>
4228f16a0fSKyle Evans #include <time.h>
4328f16a0fSKyle Evans #include <unistd.h>
4428f16a0fSKyle Evans 
4528f16a0fSKyle Evans #include "be.h"
4628f16a0fSKyle Evans #include "be_impl.h"
4728f16a0fSKyle Evans 
483d1a1f2cSKyle Evans #if SOON
49c65a2111SKyle Evans static int be_create_child_noent(libbe_handle_t *lbh, const char *active,
50c65a2111SKyle Evans     const char *child_path);
51c65a2111SKyle Evans static int be_create_child_cloned(libbe_handle_t *lbh, const char *active);
523d1a1f2cSKyle Evans #endif
53c65a2111SKyle Evans 
5428f16a0fSKyle Evans /*
55ee16b7c9SKyle Evans  * Iterator function for locating the rootfs amongst the children of the
56ee16b7c9SKyle Evans  * zfs_be_root set by loader(8).  data is expected to be a libbe_handle_t *.
57ee16b7c9SKyle Evans  */
58ee16b7c9SKyle Evans static int
5951aecc89SKyle Evans be_locate_rootfs(libbe_handle_t *lbh)
60ee16b7c9SKyle Evans {
6151aecc89SKyle Evans 	zfs_handle_t *zfs;
62ee16b7c9SKyle Evans 
6351aecc89SKyle Evans 	zfs = zfs_path_to_zhandle(lbh->lzh, "/", ZFS_TYPE_FILESYSTEM);
6451aecc89SKyle Evans 	if (zfs == NULL)
65ee16b7c9SKyle Evans 		return (1);
66ee16b7c9SKyle Evans 
6751aecc89SKyle Evans 	strlcpy(lbh->rootfs, zfs_get_name(zfs), sizeof(lbh->rootfs));
6851aecc89SKyle Evans 	zfs_close(zfs);
69ee16b7c9SKyle Evans 	return (0);
70ee16b7c9SKyle Evans }
71ee16b7c9SKyle Evans 
72ee16b7c9SKyle Evans /*
7328f16a0fSKyle Evans  * Initializes the libbe context to operate in the root boot environment
7428f16a0fSKyle Evans  * dataset, for example, zroot/ROOT.
7528f16a0fSKyle Evans  */
7628f16a0fSKyle Evans libbe_handle_t *
77cc624025SKyle Evans libbe_init(const char *root)
7828f16a0fSKyle Evans {
7928f16a0fSKyle Evans 	libbe_handle_t *lbh;
80c3a34c08SKyle Evans 	char *poolname, *pos;
81c3a34c08SKyle Evans 	int pnamelen;
8228f16a0fSKyle Evans 
83c3a34c08SKyle Evans 	lbh = NULL;
84c3a34c08SKyle Evans 	poolname = pos = NULL;
8528f16a0fSKyle Evans 
86c3a34c08SKyle Evans 	if ((lbh = calloc(1, sizeof(libbe_handle_t))) == NULL)
87c3a34c08SKyle Evans 		goto err;
8828f16a0fSKyle Evans 
89c3a34c08SKyle Evans 	if ((lbh->lzh = libzfs_init()) == NULL)
90c3a34c08SKyle Evans 		goto err;
9128f16a0fSKyle Evans 
92cc624025SKyle Evans 	/*
93cc624025SKyle Evans 	 * Grab rootfs, we'll work backwards from there if an optional BE root
94cc624025SKyle Evans 	 * has not been passed in.
95cc624025SKyle Evans 	 */
9651aecc89SKyle Evans 	if (be_locate_rootfs(lbh) != 0)
97c3a34c08SKyle Evans 		goto err;
98cc624025SKyle Evans 	if (root == NULL) {
99cc624025SKyle Evans 		/* Strip off the final slash from rootfs to get the be root */
10051aecc89SKyle Evans 		strlcpy(lbh->root, lbh->rootfs, sizeof(lbh->root));
10151aecc89SKyle Evans 		pos = strrchr(lbh->root, '/');
10251aecc89SKyle Evans 		if (pos == NULL)
10351aecc89SKyle Evans 			goto err;
10451aecc89SKyle Evans 		*pos = '\0';
105cc624025SKyle Evans 	} else
106cc624025SKyle Evans 		strlcpy(lbh->root, root, sizeof(lbh->root));
107c3a34c08SKyle Evans 
108c3a34c08SKyle Evans 	if ((pos = strchr(lbh->root, '/')) == NULL)
109c3a34c08SKyle Evans 		goto err;
110c3a34c08SKyle Evans 
111c3a34c08SKyle Evans 	pnamelen = pos - lbh->root;
112c3a34c08SKyle Evans 	poolname = malloc(pnamelen + 1);
113c3a34c08SKyle Evans 	if (poolname == NULL)
114c3a34c08SKyle Evans 		goto err;
115c3a34c08SKyle Evans 
11655b0e92bSKyle Evans 	strlcpy(poolname, lbh->root, pnamelen + 1);
117c3a34c08SKyle Evans 	if ((lbh->active_phandle = zpool_open(lbh->lzh, poolname)) == NULL)
118c3a34c08SKyle Evans 		goto err;
119a8e44f4dSKyle Evans 	free(poolname);
120a8e44f4dSKyle Evans 	poolname = NULL;
121c3a34c08SKyle Evans 
122c3a34c08SKyle Evans 	if (zpool_get_prop(lbh->active_phandle, ZPOOL_PROP_BOOTFS, lbh->bootfs,
12355b0e92bSKyle Evans 	    sizeof(lbh->bootfs), NULL, true) != 0)
124c3a34c08SKyle Evans 		goto err;
125c3a34c08SKyle Evans 
12628f16a0fSKyle Evans 	return (lbh);
127c3a34c08SKyle Evans err:
128c3a34c08SKyle Evans 	if (lbh != NULL) {
129c3a34c08SKyle Evans 		if (lbh->active_phandle != NULL)
130c3a34c08SKyle Evans 			zpool_close(lbh->active_phandle);
131c3a34c08SKyle Evans 		if (lbh->lzh != NULL)
132c3a34c08SKyle Evans 			libzfs_fini(lbh->lzh);
133c3a34c08SKyle Evans 		free(lbh);
134c3a34c08SKyle Evans 	}
135c3a34c08SKyle Evans 	free(poolname);
136c3a34c08SKyle Evans 	return (NULL);
13728f16a0fSKyle Evans }
13828f16a0fSKyle Evans 
13928f16a0fSKyle Evans 
14028f16a0fSKyle Evans /*
14128f16a0fSKyle Evans  * Free memory allocated by libbe_init()
14228f16a0fSKyle Evans  */
14328f16a0fSKyle Evans void
14428f16a0fSKyle Evans libbe_close(libbe_handle_t *lbh)
14528f16a0fSKyle Evans {
146bfe0869cSKyle Evans 
147c3a34c08SKyle Evans 	if (lbh->active_phandle != NULL)
148c3a34c08SKyle Evans 		zpool_close(lbh->active_phandle);
14928f16a0fSKyle Evans 	libzfs_fini(lbh->lzh);
15028f16a0fSKyle Evans 	free(lbh);
15128f16a0fSKyle Evans }
15228f16a0fSKyle Evans 
1539b1662e6SKyle Evans /*
1549b1662e6SKyle Evans  * Proxy through to libzfs for the moment.
1559b1662e6SKyle Evans  */
1569b1662e6SKyle Evans void
1579b1662e6SKyle Evans be_nicenum(uint64_t num, char *buf, size_t buflen)
1589b1662e6SKyle Evans {
1599b1662e6SKyle Evans 
1609b1662e6SKyle Evans 	zfs_nicenum(num, buf, buflen);
1619b1662e6SKyle Evans }
16228f16a0fSKyle Evans 
163920abf4dSKyle Evans static int
164920abf4dSKyle Evans be_destroy_cb(zfs_handle_t *zfs_hdl, void *data)
165920abf4dSKyle Evans {
166920abf4dSKyle Evans 	int err;
167920abf4dSKyle Evans 
168920abf4dSKyle Evans 	if ((err = zfs_iter_children(zfs_hdl, be_destroy_cb, data)) != 0)
169920abf4dSKyle Evans 		return (err);
170920abf4dSKyle Evans 	if ((err = zfs_destroy(zfs_hdl, false)) != 0)
171920abf4dSKyle Evans 		return (err);
172920abf4dSKyle Evans 	return (0);
173920abf4dSKyle Evans }
174920abf4dSKyle Evans 
17528f16a0fSKyle Evans /*
17628f16a0fSKyle Evans  * Destroy the boot environment or snapshot specified by the name
17728f16a0fSKyle Evans  * parameter. Options are or'd together with the possible values:
17828f16a0fSKyle Evans  * BE_DESTROY_FORCE : forces operation on mounted datasets
17928f16a0fSKyle Evans  */
18028f16a0fSKyle Evans int
18173c3d608SKyle Evans be_destroy(libbe_handle_t *lbh, const char *name, int options)
18228f16a0fSKyle Evans {
18328f16a0fSKyle Evans 	zfs_handle_t *fs;
18428f16a0fSKyle Evans 	char path[BE_MAXPATHLEN];
185bfe0869cSKyle Evans 	char *p;
186bfe0869cSKyle Evans 	int err, force, mounted;
18728f16a0fSKyle Evans 
188bfe0869cSKyle Evans 	p = path;
189bfe0869cSKyle Evans 	force = options & BE_DESTROY_FORCE;
19028f16a0fSKyle Evans 
19128f16a0fSKyle Evans 	be_root_concat(lbh, name, path);
19228f16a0fSKyle Evans 
19328f16a0fSKyle Evans 	if (strchr(name, '@') == NULL) {
194bfe0869cSKyle Evans 		if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_FILESYSTEM))
19528f16a0fSKyle Evans 			return (set_error(lbh, BE_ERR_NOENT));
19628f16a0fSKyle Evans 
197bfe0869cSKyle Evans 		if (strcmp(path, lbh->rootfs) == 0)
19828f16a0fSKyle Evans 			return (set_error(lbh, BE_ERR_DESTROYACT));
19928f16a0fSKyle Evans 
20028f16a0fSKyle Evans 		fs = zfs_open(lbh->lzh, p, ZFS_TYPE_FILESYSTEM);
20128f16a0fSKyle Evans 	} else {
20228f16a0fSKyle Evans 
203bfe0869cSKyle Evans 		if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_SNAPSHOT))
20428f16a0fSKyle Evans 			return (set_error(lbh, BE_ERR_NOENT));
20528f16a0fSKyle Evans 
20628f16a0fSKyle Evans 		fs = zfs_open(lbh->lzh, p, ZFS_TYPE_SNAPSHOT);
20728f16a0fSKyle Evans 	}
20828f16a0fSKyle Evans 
20928f16a0fSKyle Evans 	if (fs == NULL)
21028f16a0fSKyle Evans 		return (set_error(lbh, BE_ERR_ZFSOPEN));
21128f16a0fSKyle Evans 
21228f16a0fSKyle Evans 	/* Check if mounted, unmount if force is specified */
213b29bf2f8SKyle Evans 	if ((mounted = zfs_is_mounted(fs, NULL)) != 0) {
214bfe0869cSKyle Evans 		if (force)
21528f16a0fSKyle Evans 			zfs_unmount(fs, NULL, 0);
216bfe0869cSKyle Evans 		else
21728f16a0fSKyle Evans 			return (set_error(lbh, BE_ERR_DESTROYMNT));
21828f16a0fSKyle Evans 	}
21928f16a0fSKyle Evans 
220920abf4dSKyle Evans 	if ((err = be_destroy_cb(fs, NULL)) != 0) {
221920abf4dSKyle Evans 		/* Children are still present or the mount is referenced */
222920abf4dSKyle Evans 		if (err == EBUSY)
223920abf4dSKyle Evans 			return (set_error(lbh, BE_ERR_DESTROYMNT));
224920abf4dSKyle Evans 		return (set_error(lbh, BE_ERR_UNKNOWN));
225920abf4dSKyle Evans 	}
22628f16a0fSKyle Evans 
227920abf4dSKyle Evans 	return (0);
22828f16a0fSKyle Evans }
22928f16a0fSKyle Evans 
23028f16a0fSKyle Evans 
23128f16a0fSKyle Evans int
232b29bf2f8SKyle Evans be_snapshot(libbe_handle_t *lbh, const char *source, const char *snap_name,
23328f16a0fSKyle Evans     bool recursive, char *result)
23428f16a0fSKyle Evans {
23528f16a0fSKyle Evans 	char buf[BE_MAXPATHLEN];
23628f16a0fSKyle Evans 	time_t rawtime;
23728f16a0fSKyle Evans 	int len, err;
23828f16a0fSKyle Evans 
23928f16a0fSKyle Evans 	be_root_concat(lbh, source, buf);
24028f16a0fSKyle Evans 
241162ec569SKyle Evans 	if ((err = be_exists(lbh, buf)) != 0)
242162ec569SKyle Evans 		return (set_error(lbh, err));
24328f16a0fSKyle Evans 
24428f16a0fSKyle Evans 	if (snap_name != NULL) {
245a8e44f4dSKyle Evans 		if (strlcat(buf, "@", sizeof(buf)) >= sizeof(buf))
246a8e44f4dSKyle Evans 			return (set_error(lbh, BE_ERR_INVALIDNAME));
247a8e44f4dSKyle Evans 
248a8e44f4dSKyle Evans 		if (strlcat(buf, snap_name, sizeof(buf)) >= sizeof(buf))
249a8e44f4dSKyle Evans 			return (set_error(lbh, BE_ERR_INVALIDNAME));
250a8e44f4dSKyle Evans 
251bfe0869cSKyle Evans 		if (result != NULL)
25228f16a0fSKyle Evans 			snprintf(result, BE_MAXPATHLEN, "%s@%s", source,
25328f16a0fSKyle Evans 			    snap_name);
25428f16a0fSKyle Evans 	} else {
25528f16a0fSKyle Evans 		time(&rawtime);
25628f16a0fSKyle Evans 		len = strlen(buf);
25755b0e92bSKyle Evans 		strftime(buf + len, sizeof(buf) - len,
25828f16a0fSKyle Evans 		    "@%F-%T", localtime(&rawtime));
259a8e44f4dSKyle Evans 		if (result != NULL && strlcpy(result, strrchr(buf, '/') + 1,
260a8e44f4dSKyle Evans 		    sizeof(buf)) >= sizeof(buf))
261a8e44f4dSKyle Evans 			return (set_error(lbh, BE_ERR_INVALIDNAME));
26228f16a0fSKyle Evans 	}
26328f16a0fSKyle Evans 
264b29bf2f8SKyle Evans 	if ((err = zfs_snapshot(lbh->lzh, buf, recursive, NULL)) != 0) {
26528f16a0fSKyle Evans 		switch (err) {
26628f16a0fSKyle Evans 		case EZFS_INVALIDNAME:
26728f16a0fSKyle Evans 			return (set_error(lbh, BE_ERR_INVALIDNAME));
26828f16a0fSKyle Evans 
26928f16a0fSKyle Evans 		default:
2702989df09SKyle Evans 			/*
2712989df09SKyle Evans 			 * The other errors that zfs_ioc_snapshot might return
2722989df09SKyle Evans 			 * shouldn't happen if we've set things up properly, so
2732989df09SKyle Evans 			 * we'll gloss over them and call it UNKNOWN as it will
2742989df09SKyle Evans 			 * require further triage.
2752989df09SKyle Evans 			 */
2762989df09SKyle Evans 			if (errno == ENOTSUP)
2772989df09SKyle Evans 				return (set_error(lbh, BE_ERR_NOPOOL));
27828f16a0fSKyle Evans 			return (set_error(lbh, BE_ERR_UNKNOWN));
27928f16a0fSKyle Evans 		}
28028f16a0fSKyle Evans 	}
28128f16a0fSKyle Evans 
28228f16a0fSKyle Evans 	return (BE_ERR_SUCCESS);
28328f16a0fSKyle Evans }
28428f16a0fSKyle Evans 
28528f16a0fSKyle Evans 
28628f16a0fSKyle Evans /*
28728f16a0fSKyle Evans  * Create the boot environment specified by the name parameter
28828f16a0fSKyle Evans  */
28928f16a0fSKyle Evans int
29073c3d608SKyle Evans be_create(libbe_handle_t *lbh, const char *name)
29128f16a0fSKyle Evans {
29228f16a0fSKyle Evans 	int err;
29328f16a0fSKyle Evans 
294b29bf2f8SKyle Evans 	err = be_create_from_existing(lbh, name, be_active_path(lbh));
29528f16a0fSKyle Evans 
29628f16a0fSKyle Evans 	return (set_error(lbh, err));
29728f16a0fSKyle Evans }
29828f16a0fSKyle Evans 
29928f16a0fSKyle Evans 
30028f16a0fSKyle Evans static int
30128f16a0fSKyle Evans be_deep_clone_prop(int prop, void *cb)
30228f16a0fSKyle Evans {
30328f16a0fSKyle Evans 	int err;
304bfe0869cSKyle Evans         struct libbe_dccb *dccb;
30528f16a0fSKyle Evans 	zprop_source_t src;
30628f16a0fSKyle Evans 	char pval[BE_MAXPATHLEN];
30728f16a0fSKyle Evans 	char source[BE_MAXPATHLEN];
308*af43c24dSKyle Evans 	char *val;
30928f16a0fSKyle Evans 
310bfe0869cSKyle Evans 	dccb = cb;
31128f16a0fSKyle Evans 	/* Skip some properties we don't want to touch */
31273c3d608SKyle Evans 	if (prop == ZFS_PROP_CANMOUNT)
31328f16a0fSKyle Evans 		return (ZPROP_CONT);
31428f16a0fSKyle Evans 
31528f16a0fSKyle Evans 	/* Don't copy readonly properties */
316bfe0869cSKyle Evans 	if (zfs_prop_readonly(prop))
31728f16a0fSKyle Evans 		return (ZPROP_CONT);
31828f16a0fSKyle Evans 
31928f16a0fSKyle Evans 	if ((err = zfs_prop_get(dccb->zhp, prop, (char *)&pval,
320bfe0869cSKyle Evans 	    sizeof(pval), &src, (char *)&source, sizeof(source), false)))
32128f16a0fSKyle Evans 		/* Just continue if we fail to read a property */
32228f16a0fSKyle Evans 		return (ZPROP_CONT);
323bfe0869cSKyle Evans 
32428f16a0fSKyle Evans 	/* Only copy locally defined properties */
325bfe0869cSKyle Evans 	if (src != ZPROP_SRC_LOCAL)
32628f16a0fSKyle Evans 		return (ZPROP_CONT);
32728f16a0fSKyle Evans 
328*af43c24dSKyle Evans 	/* Augment mountpoint with altroot, if needed */
329*af43c24dSKyle Evans 	val = pval;
330*af43c24dSKyle Evans 	if (prop == ZFS_PROP_MOUNTPOINT && *dccb->altroot != '\0') {
331*af43c24dSKyle Evans 		if (pval[strlen(dccb->altroot)] == '\0')
332*af43c24dSKyle Evans 			strlcpy(pval, "/", sizeof(pval));
333*af43c24dSKyle Evans 		else
334*af43c24dSKyle Evans 			val = pval + strlen(dccb->altroot);
335*af43c24dSKyle Evans 	}
336*af43c24dSKyle Evans 	nvlist_add_string(dccb->props, zfs_prop_to_name(prop), val);
33728f16a0fSKyle Evans 
33828f16a0fSKyle Evans 	return (ZPROP_CONT);
33928f16a0fSKyle Evans }
34028f16a0fSKyle Evans 
34128f16a0fSKyle Evans static int
34228f16a0fSKyle Evans be_deep_clone(zfs_handle_t *ds, void *data)
34328f16a0fSKyle Evans {
34428f16a0fSKyle Evans 	int err;
34528f16a0fSKyle Evans 	char be_path[BE_MAXPATHLEN];
34628f16a0fSKyle Evans 	char snap_path[BE_MAXPATHLEN];
34728f16a0fSKyle Evans 	const char *dspath;
34828f16a0fSKyle Evans 	char *dsname;
34928f16a0fSKyle Evans 	zfs_handle_t *snap_hdl;
35028f16a0fSKyle Evans 	nvlist_t *props;
351bfe0869cSKyle Evans 	struct libbe_deep_clone *isdc, sdc;
35228f16a0fSKyle Evans 	struct libbe_dccb dccb;
35328f16a0fSKyle Evans 
354bfe0869cSKyle Evans 	isdc = (struct libbe_deep_clone *)data;
35528f16a0fSKyle Evans 	dspath = zfs_get_name(ds);
356bfe0869cSKyle Evans 	if ((dsname = strrchr(dspath, '/')) == NULL)
35728f16a0fSKyle Evans 		return (BE_ERR_UNKNOWN);
35828f16a0fSKyle Evans 	dsname++;
359bfe0869cSKyle Evans 
360bfe0869cSKyle Evans 	if (isdc->bename == NULL)
36128f16a0fSKyle Evans 		snprintf(be_path, sizeof(be_path), "%s/%s", isdc->be_root, dsname);
362bfe0869cSKyle Evans 	else
36328f16a0fSKyle Evans 		snprintf(be_path, sizeof(be_path), "%s/%s", isdc->be_root, isdc->bename);
364bfe0869cSKyle Evans 
36528f16a0fSKyle Evans 	snprintf(snap_path, sizeof(snap_path), "%s@%s", dspath, isdc->snapname);
36628f16a0fSKyle Evans 
367bfe0869cSKyle Evans 	if (zfs_dataset_exists(isdc->lbh->lzh, be_path, ZFS_TYPE_DATASET))
36828f16a0fSKyle Evans 		return (set_error(isdc->lbh, BE_ERR_EXISTS));
36928f16a0fSKyle Evans 
37028f16a0fSKyle Evans 	if ((snap_hdl =
371bfe0869cSKyle Evans 	    zfs_open(isdc->lbh->lzh, snap_path, ZFS_TYPE_SNAPSHOT)) == NULL)
37228f16a0fSKyle Evans 		return (set_error(isdc->lbh, BE_ERR_ZFSOPEN));
37328f16a0fSKyle Evans 
37428f16a0fSKyle Evans 	nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
37528f16a0fSKyle Evans 	nvlist_add_string(props, "canmount", "noauto");
37628f16a0fSKyle Evans 
37728f16a0fSKyle Evans 	dccb.zhp = ds;
37828f16a0fSKyle Evans 	dccb.props = props;
379*af43c24dSKyle Evans 	if (zpool_get_prop(isdc->lbh->active_phandle, ZPOOL_PROP_ALTROOT,
380*af43c24dSKyle Evans 	    dccb.altroot, sizeof(dccb.altroot), NULL, true) != 0 ||
381*af43c24dSKyle Evans 	    strcmp(dccb.altroot, "-") == 0)
382*af43c24dSKyle Evans 		*dccb.altroot = '\0';
38328f16a0fSKyle Evans 	if (zprop_iter(be_deep_clone_prop, &dccb, B_FALSE, B_FALSE,
384bfe0869cSKyle Evans 	    ZFS_TYPE_FILESYSTEM) == ZPROP_INVAL)
38528f16a0fSKyle Evans 		return (-1);
38628f16a0fSKyle Evans 
387cc4deabcSKyle Evans 	if ((err = zfs_clone(snap_hdl, be_path, props)) != 0)
38828f16a0fSKyle Evans 		err = BE_ERR_ZFSCLONE;
38928f16a0fSKyle Evans 
39028f16a0fSKyle Evans 	nvlist_free(props);
39128f16a0fSKyle Evans 	zfs_close(snap_hdl);
39228f16a0fSKyle Evans 
393cc4deabcSKyle Evans 	/* Failed to clone */
394cc4deabcSKyle Evans 	if (err != BE_ERR_SUCCESS)
395cc4deabcSKyle Evans 		return (set_error(isdc->lbh, err));
396cc4deabcSKyle Evans 
39728f16a0fSKyle Evans 	sdc.lbh = isdc->lbh;
39828f16a0fSKyle Evans 	sdc.bename = NULL;
39928f16a0fSKyle Evans 	sdc.snapname = isdc->snapname;
40028f16a0fSKyle Evans 	sdc.be_root = (char *)&be_path;
40128f16a0fSKyle Evans 
40228f16a0fSKyle Evans 	err = zfs_iter_filesystems(ds, be_deep_clone, &sdc);
40328f16a0fSKyle Evans 
40428f16a0fSKyle Evans 	return (err);
40528f16a0fSKyle Evans }
40628f16a0fSKyle Evans 
40728f16a0fSKyle Evans /*
40828f16a0fSKyle Evans  * Create the boot environment from pre-existing snapshot
40928f16a0fSKyle Evans  */
41028f16a0fSKyle Evans int
411b29bf2f8SKyle Evans be_create_from_existing_snap(libbe_handle_t *lbh, const char *name,
412b29bf2f8SKyle Evans     const char *snap)
41328f16a0fSKyle Evans {
41428f16a0fSKyle Evans 	int err;
41528f16a0fSKyle Evans 	char be_path[BE_MAXPATHLEN];
41628f16a0fSKyle Evans 	char snap_path[BE_MAXPATHLEN];
417b29bf2f8SKyle Evans 	const char *bename;
418b29bf2f8SKyle Evans 	char *parentname, *snapname;
41928f16a0fSKyle Evans 	zfs_handle_t *parent_hdl;
42028f16a0fSKyle Evans 	struct libbe_deep_clone sdc;
42128f16a0fSKyle Evans 
422b29bf2f8SKyle Evans 	if ((err = be_validate_name(lbh, name)) != 0)
42328f16a0fSKyle Evans 		return (set_error(lbh, err));
424b29bf2f8SKyle Evans 	if ((err = be_root_concat(lbh, snap, snap_path)) != 0)
42528f16a0fSKyle Evans 		return (set_error(lbh, err));
426b29bf2f8SKyle Evans 	if ((err = be_validate_snap(lbh, snap_path)) != 0)
42728f16a0fSKyle Evans 		return (set_error(lbh, err));
42828f16a0fSKyle Evans 
429b29bf2f8SKyle Evans 	if ((err = be_root_concat(lbh, name, be_path)) != 0)
43028f16a0fSKyle Evans 		return (set_error(lbh, err));
43128f16a0fSKyle Evans 
432bfe0869cSKyle Evans 	if ((bename = strrchr(name, '/')) == NULL)
43328f16a0fSKyle Evans 		bename = name;
434bfe0869cSKyle Evans 	else
43528f16a0fSKyle Evans 		bename++;
436bfe0869cSKyle Evans 
437cc4deabcSKyle Evans 	if ((parentname = strdup(snap_path)) == NULL)
438cc4deabcSKyle Evans 		return (set_error(lbh, BE_ERR_UNKNOWN));
439cc4deabcSKyle Evans 
44028f16a0fSKyle Evans 	snapname = strchr(parentname, '@');
44128f16a0fSKyle Evans 	if (snapname == NULL) {
442cc4deabcSKyle Evans 		free(parentname);
443cc4deabcSKyle Evans 		return (set_error(lbh, BE_ERR_UNKNOWN));
44428f16a0fSKyle Evans 	}
44528f16a0fSKyle Evans 	*snapname = '\0';
44628f16a0fSKyle Evans 	snapname++;
44728f16a0fSKyle Evans 
44828f16a0fSKyle Evans 	sdc.lbh = lbh;
44928f16a0fSKyle Evans 	sdc.bename = bename;
45028f16a0fSKyle Evans 	sdc.snapname = snapname;
45128f16a0fSKyle Evans 	sdc.be_root = lbh->root;
45228f16a0fSKyle Evans 
45328f16a0fSKyle Evans 	parent_hdl = zfs_open(lbh->lzh, parentname, ZFS_TYPE_DATASET);
45428f16a0fSKyle Evans 	err = be_deep_clone(parent_hdl, &sdc);
45528f16a0fSKyle Evans 
456cc4deabcSKyle Evans 	free(parentname);
45728f16a0fSKyle Evans 	return (set_error(lbh, err));
45828f16a0fSKyle Evans }
45928f16a0fSKyle Evans 
46028f16a0fSKyle Evans 
46128f16a0fSKyle Evans /*
46228f16a0fSKyle Evans  * Create a boot environment from an existing boot environment
46328f16a0fSKyle Evans  */
46428f16a0fSKyle Evans int
465b29bf2f8SKyle Evans be_create_from_existing(libbe_handle_t *lbh, const char *name, const char *old)
46628f16a0fSKyle Evans {
46728f16a0fSKyle Evans 	int err;
46828f16a0fSKyle Evans 	char buf[BE_MAXPATHLEN];
46928f16a0fSKyle Evans 
4705b7803a9SKyle Evans 	if ((err = be_snapshot(lbh, old, NULL, true, (char *)&buf)) != 0)
47128f16a0fSKyle Evans 		return (set_error(lbh, err));
47228f16a0fSKyle Evans 
47328f16a0fSKyle Evans 	err = be_create_from_existing_snap(lbh, name, (char *)buf);
47428f16a0fSKyle Evans 
47528f16a0fSKyle Evans 	return (set_error(lbh, err));
47628f16a0fSKyle Evans }
47728f16a0fSKyle Evans 
47828f16a0fSKyle Evans 
47928f16a0fSKyle Evans /*
48028f16a0fSKyle Evans  * Verifies that a snapshot has a valid name, exists, and has a mountpoint of
48128f16a0fSKyle Evans  * '/'. Returns BE_ERR_SUCCESS (0), upon success, or the relevant BE_ERR_* upon
48228f16a0fSKyle Evans  * failure. Does not set the internal library error state.
48328f16a0fSKyle Evans  */
48428f16a0fSKyle Evans int
485b29bf2f8SKyle Evans be_validate_snap(libbe_handle_t *lbh, const char *snap_name)
48628f16a0fSKyle Evans {
48728f16a0fSKyle Evans 
488bfe0869cSKyle Evans 	if (strlen(snap_name) >= BE_MAXPATHLEN)
48928f16a0fSKyle Evans 		return (BE_ERR_PATHLEN);
49028f16a0fSKyle Evans 
49128f16a0fSKyle Evans 	if (!zfs_dataset_exists(lbh->lzh, snap_name,
492bfe0869cSKyle Evans 	    ZFS_TYPE_SNAPSHOT))
49328f16a0fSKyle Evans 		return (BE_ERR_NOENT);
49428f16a0fSKyle Evans 
49551aecc89SKyle Evans 	return (BE_ERR_SUCCESS);
49628f16a0fSKyle Evans }
49728f16a0fSKyle Evans 
49828f16a0fSKyle Evans 
49928f16a0fSKyle Evans /*
50028f16a0fSKyle Evans  * Idempotently appends the name argument to the root boot environment path
50128f16a0fSKyle Evans  * and copies the resulting string into the result buffer (which is assumed
50228f16a0fSKyle Evans  * to be at least BE_MAXPATHLEN characters long. Returns BE_ERR_SUCCESS upon
50328f16a0fSKyle Evans  * success, BE_ERR_PATHLEN if the resulting path is longer than BE_MAXPATHLEN,
50428f16a0fSKyle Evans  * or BE_ERR_INVALIDNAME if the name is a path that does not begin with
50528f16a0fSKyle Evans  * zfs_be_root. Does not set internal library error state.
50628f16a0fSKyle Evans  */
50728f16a0fSKyle Evans int
508b29bf2f8SKyle Evans be_root_concat(libbe_handle_t *lbh, const char *name, char *result)
50928f16a0fSKyle Evans {
51028f16a0fSKyle Evans 	size_t name_len, root_len;
51128f16a0fSKyle Evans 
51228f16a0fSKyle Evans 	name_len = strlen(name);
51328f16a0fSKyle Evans 	root_len = strlen(lbh->root);
51428f16a0fSKyle Evans 
51528f16a0fSKyle Evans 	/* Act idempotently; return be name if it is already a full path */
51628f16a0fSKyle Evans 	if (strrchr(name, '/') != NULL) {
517bfe0869cSKyle Evans 		if (strstr(name, lbh->root) != name)
51828f16a0fSKyle Evans 			return (BE_ERR_INVALIDNAME);
51928f16a0fSKyle Evans 
520bfe0869cSKyle Evans 		if (name_len >= BE_MAXPATHLEN)
52128f16a0fSKyle Evans 			return (BE_ERR_PATHLEN);
52228f16a0fSKyle Evans 
52355b0e92bSKyle Evans 		strlcpy(result, name, BE_MAXPATHLEN);
52428f16a0fSKyle Evans 		return (BE_ERR_SUCCESS);
52528f16a0fSKyle Evans 	} else if (name_len + root_len + 1 < BE_MAXPATHLEN) {
52628f16a0fSKyle Evans 		snprintf(result, BE_MAXPATHLEN, "%s/%s", lbh->root,
52728f16a0fSKyle Evans 		    name);
52828f16a0fSKyle Evans 		return (BE_ERR_SUCCESS);
52928f16a0fSKyle Evans 	}
53028f16a0fSKyle Evans 
53128f16a0fSKyle Evans 	return (BE_ERR_PATHLEN);
53228f16a0fSKyle Evans }
53328f16a0fSKyle Evans 
53428f16a0fSKyle Evans 
53528f16a0fSKyle Evans /*
53628f16a0fSKyle Evans  * Verifies the validity of a boot environment name (A-Za-z0-9-_.). Returns
5375b7803a9SKyle Evans  * BE_ERR_SUCCESS (0) if name is valid, otherwise returns BE_ERR_INVALIDNAME
5385b7803a9SKyle Evans  * or BE_ERR_PATHLEN.
53928f16a0fSKyle Evans  * Does not set internal library error state.
54028f16a0fSKyle Evans  */
54128f16a0fSKyle Evans int
5425b7803a9SKyle Evans be_validate_name(libbe_handle_t *lbh, const char *name)
54328f16a0fSKyle Evans {
54428f16a0fSKyle Evans 	for (int i = 0; *name; i++) {
54528f16a0fSKyle Evans 		char c = *(name++);
546bfe0869cSKyle Evans 		if (isalnum(c) || (c == '-') || (c == '_') || (c == '.'))
54728f16a0fSKyle Evans 			continue;
54828f16a0fSKyle Evans 		return (BE_ERR_INVALIDNAME);
54928f16a0fSKyle Evans 	}
55028f16a0fSKyle Evans 
5515b7803a9SKyle Evans 	/*
5525b7803a9SKyle Evans 	 * Impose the additional restriction that the entire dataset name must
5535b7803a9SKyle Evans 	 * not exceed the maximum length of a dataset, i.e. MAXNAMELEN.
5545b7803a9SKyle Evans 	 */
5555b7803a9SKyle Evans 	if (strlen(lbh->root) + 1 + strlen(name) > MAXNAMELEN)
5565b7803a9SKyle Evans 		return (BE_ERR_PATHLEN);
55728f16a0fSKyle Evans 	return (BE_ERR_SUCCESS);
55828f16a0fSKyle Evans }
55928f16a0fSKyle Evans 
56028f16a0fSKyle Evans 
56128f16a0fSKyle Evans /*
56228f16a0fSKyle Evans  * usage
56328f16a0fSKyle Evans  */
56428f16a0fSKyle Evans int
56573c3d608SKyle Evans be_rename(libbe_handle_t *lbh, const char *old, const char *new)
56628f16a0fSKyle Evans {
56728f16a0fSKyle Evans 	char full_old[BE_MAXPATHLEN];
56828f16a0fSKyle Evans 	char full_new[BE_MAXPATHLEN];
56928f16a0fSKyle Evans 	zfs_handle_t *zfs_hdl;
57028f16a0fSKyle Evans 	int err;
57128f16a0fSKyle Evans 
5725b7803a9SKyle Evans 	/*
5735b7803a9SKyle Evans 	 * be_validate_name is documented not to set error state, so we should
5745b7803a9SKyle Evans 	 * do so here.
5755b7803a9SKyle Evans 	 */
5765b7803a9SKyle Evans 	if ((err = be_validate_name(lbh, new)) != 0)
5775b7803a9SKyle Evans 		return (set_error(lbh, err));
578b29bf2f8SKyle Evans 	if ((err = be_root_concat(lbh, old, full_old)) != 0)
57928f16a0fSKyle Evans 		return (set_error(lbh, err));
580b29bf2f8SKyle Evans 	if ((err = be_root_concat(lbh, new, full_new)) != 0)
58128f16a0fSKyle Evans 		return (set_error(lbh, err));
58228f16a0fSKyle Evans 
583bfe0869cSKyle Evans 	if (!zfs_dataset_exists(lbh->lzh, full_old, ZFS_TYPE_DATASET))
5842989df09SKyle Evans 		return (set_error(lbh, BE_ERR_NOENT));
58528f16a0fSKyle Evans 
586bfe0869cSKyle Evans 	if (zfs_dataset_exists(lbh->lzh, full_new, ZFS_TYPE_DATASET))
5872989df09SKyle Evans 		return (set_error(lbh, BE_ERR_EXISTS));
58828f16a0fSKyle Evans 
58928f16a0fSKyle Evans 	if ((zfs_hdl = zfs_open(lbh->lzh, full_old,
590bfe0869cSKyle Evans 	    ZFS_TYPE_FILESYSTEM)) == NULL)
5912989df09SKyle Evans 		return (set_error(lbh, BE_ERR_ZFSOPEN));
59228f16a0fSKyle Evans 
593bfe0869cSKyle Evans 	/* recurse, nounmount, forceunmount */
5948369ba42SKyle Evans 	struct renameflags flags = {
5958369ba42SKyle Evans 		.nounmount = 1,
5968369ba42SKyle Evans 	};
59728f16a0fSKyle Evans 
59828f16a0fSKyle Evans 	err = zfs_rename(zfs_hdl, NULL, full_new, flags);
59928f16a0fSKyle Evans 
60028f16a0fSKyle Evans 	zfs_close(zfs_hdl);
6015b7803a9SKyle Evans 	if (err != 0)
6025b7803a9SKyle Evans 		return (set_error(lbh, BE_ERR_UNKNOWN));
6035b7803a9SKyle Evans 	return (0);
60428f16a0fSKyle Evans }
60528f16a0fSKyle Evans 
60628f16a0fSKyle Evans 
60728f16a0fSKyle Evans int
60873c3d608SKyle Evans be_export(libbe_handle_t *lbh, const char *bootenv, int fd)
60928f16a0fSKyle Evans {
61028f16a0fSKyle Evans 	char snap_name[BE_MAXPATHLEN];
61128f16a0fSKyle Evans 	char buf[BE_MAXPATHLEN];
61228f16a0fSKyle Evans 	zfs_handle_t *zfs;
61328f16a0fSKyle Evans 	int err;
61428f16a0fSKyle Evans 
615b29bf2f8SKyle Evans 	if ((err = be_snapshot(lbh, bootenv, NULL, true, snap_name)) != 0)
6166d4b1d24SKyle Evans 		/* Use the error set by be_snapshot */
6176d4b1d24SKyle Evans 		return (err);
61828f16a0fSKyle Evans 
61928f16a0fSKyle Evans 	be_root_concat(lbh, snap_name, buf);
62028f16a0fSKyle Evans 
621bfe0869cSKyle Evans 	if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL)
622506f5fdfSKyle Evans 		return (set_error(lbh, BE_ERR_ZFSOPEN));
62328f16a0fSKyle Evans 
62428f16a0fSKyle Evans 	err = zfs_send_one(zfs, NULL, fd, 0);
6256d4b1d24SKyle Evans 	zfs_close(zfs);
6266d4b1d24SKyle Evans 
62728f16a0fSKyle Evans 	return (err);
62828f16a0fSKyle Evans }
62928f16a0fSKyle Evans 
63028f16a0fSKyle Evans 
63128f16a0fSKyle Evans int
63273c3d608SKyle Evans be_import(libbe_handle_t *lbh, const char *bootenv, int fd)
63328f16a0fSKyle Evans {
63428f16a0fSKyle Evans 	char buf[BE_MAXPATHLEN];
63528f16a0fSKyle Evans 	time_t rawtime;
63628f16a0fSKyle Evans 	nvlist_t *props;
63728f16a0fSKyle Evans 	zfs_handle_t *zfs;
63828f16a0fSKyle Evans 	int err, len;
639506f5fdfSKyle Evans 	char nbuf[24];
64028f16a0fSKyle Evans 
641bfe0869cSKyle Evans 	/*
642506f5fdfSKyle Evans 	 * We don't need this to be incredibly random, just unique enough that
643506f5fdfSKyle Evans 	 * it won't conflict with an existing dataset name.  Chopping time
644506f5fdfSKyle Evans 	 * down to 32 bits is probably good enough for this.
645bfe0869cSKyle Evans 	 */
646506f5fdfSKyle Evans 	snprintf(nbuf, 24, "tmp%u",
647506f5fdfSKyle Evans 	    (uint32_t)(time(NULL) & 0xFFFFFFFF));
648506f5fdfSKyle Evans 	if ((err = be_root_concat(lbh, nbuf, buf)) != 0)
649506f5fdfSKyle Evans 		/*
650506f5fdfSKyle Evans 		 * Technically this is our problem, but we try to use short
651506f5fdfSKyle Evans 		 * enough names that we won't run into problems except in
652506f5fdfSKyle Evans 		 * worst-case BE root approaching MAXPATHLEN.
653506f5fdfSKyle Evans 		 */
654506f5fdfSKyle Evans 		return (set_error(lbh, BE_ERR_PATHLEN));
65528f16a0fSKyle Evans 
65628f16a0fSKyle Evans 	time(&rawtime);
65728f16a0fSKyle Evans 	len = strlen(buf);
65855b0e92bSKyle Evans 	strftime(buf + len, sizeof(buf) - len, "@%F-%T", localtime(&rawtime));
65928f16a0fSKyle Evans 
660b29bf2f8SKyle Evans 	if ((err = lzc_receive(buf, NULL, NULL, false, fd)) != 0) {
661506f5fdfSKyle Evans 		switch (err) {
662506f5fdfSKyle Evans 		case EINVAL:
663506f5fdfSKyle Evans 			return (set_error(lbh, BE_ERR_NOORIGIN));
664506f5fdfSKyle Evans 		case ENOENT:
665506f5fdfSKyle Evans 			return (set_error(lbh, BE_ERR_NOENT));
666506f5fdfSKyle Evans 		case EIO:
667506f5fdfSKyle Evans 			return (set_error(lbh, BE_ERR_IO));
668506f5fdfSKyle Evans 		default:
669506f5fdfSKyle Evans 			return (set_error(lbh, BE_ERR_UNKNOWN));
670506f5fdfSKyle Evans 		}
67128f16a0fSKyle Evans 	}
67228f16a0fSKyle Evans 
673bfe0869cSKyle Evans 	if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT)) == NULL)
674506f5fdfSKyle Evans 		return (set_error(lbh, BE_ERR_ZFSOPEN));
67528f16a0fSKyle Evans 
67628f16a0fSKyle Evans 	nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
67728f16a0fSKyle Evans 	nvlist_add_string(props, "canmount", "noauto");
67828f16a0fSKyle Evans 	nvlist_add_string(props, "mountpoint", "/");
67928f16a0fSKyle Evans 
68028f16a0fSKyle Evans 	be_root_concat(lbh, bootenv, buf);
68128f16a0fSKyle Evans 
68228f16a0fSKyle Evans 	err = zfs_clone(zfs, buf, props);
68328f16a0fSKyle Evans 	zfs_close(zfs);
68428f16a0fSKyle Evans 	nvlist_free(props);
68528f16a0fSKyle Evans 
6861b057aacSKyle Evans 	if (err != 0)
6871b057aacSKyle Evans 		return (set_error(lbh, BE_ERR_UNKNOWN));
6881b057aacSKyle Evans 
6891b057aacSKyle Evans 	/*
69000ece7c2SKyle Evans 	 * Finally, we open up the dataset we just cloned the snapshot so that
6911b057aacSKyle Evans 	 * we may promote it.  This is necessary in order to clean up the ghost
69200ece7c2SKyle Evans 	 * snapshot that doesn't need to be seen after the operation is
69300ece7c2SKyle Evans 	 * complete.
6941b057aacSKyle Evans 	 */
6951b057aacSKyle Evans 	if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL)
6961b057aacSKyle Evans 		return (set_error(lbh, BE_ERR_ZFSOPEN));
6971b057aacSKyle Evans 
6981b057aacSKyle Evans 	err = zfs_promote(zfs);
6991b057aacSKyle Evans 	zfs_close(zfs);
7001b057aacSKyle Evans 
7011b057aacSKyle Evans 	if (err != 0)
7021b057aacSKyle Evans 		return (set_error(lbh, BE_ERR_UNKNOWN));
7031b057aacSKyle Evans 
7041b057aacSKyle Evans 	/* Clean up the temporary snapshot */
7051b057aacSKyle Evans 	return (be_destroy(lbh, nbuf, 0));
70628f16a0fSKyle Evans }
70728f16a0fSKyle Evans 
7083d1a1f2cSKyle Evans #if SOON
709c65a2111SKyle Evans static int
710c65a2111SKyle Evans be_create_child_noent(libbe_handle_t *lbh, const char *active,
711c65a2111SKyle Evans     const char *child_path)
712c65a2111SKyle Evans {
713c65a2111SKyle Evans 	nvlist_t *props;
714c65a2111SKyle Evans 	zfs_handle_t *zfs;
715c65a2111SKyle Evans 	int err;
716c65a2111SKyle Evans 
717c65a2111SKyle Evans 	nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
718c65a2111SKyle Evans 	nvlist_add_string(props, "canmount", "noauto");
719c65a2111SKyle Evans 	nvlist_add_string(props, "mountpoint", child_path);
720c65a2111SKyle Evans 
721c65a2111SKyle Evans 	/* Create */
722c65a2111SKyle Evans 	if ((err = zfs_create(lbh->lzh, active, ZFS_TYPE_DATASET,
723c65a2111SKyle Evans 	    props)) != 0) {
724c65a2111SKyle Evans 		switch (err) {
725c65a2111SKyle Evans 		case EZFS_EXISTS:
726c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_EXISTS));
727c65a2111SKyle Evans 		case EZFS_NOENT:
728c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_NOENT));
729c65a2111SKyle Evans 		case EZFS_BADTYPE:
730c65a2111SKyle Evans 		case EZFS_BADVERSION:
731c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_NOPOOL));
732c65a2111SKyle Evans 		case EZFS_BADPROP:
733c65a2111SKyle Evans 		default:
734c65a2111SKyle Evans 			/* We set something up wrong, probably... */
735c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_UNKNOWN));
736c65a2111SKyle Evans 		}
737c65a2111SKyle Evans 	}
738c65a2111SKyle Evans 	nvlist_free(props);
739c65a2111SKyle Evans 
740c65a2111SKyle Evans 	if ((zfs = zfs_open(lbh->lzh, active, ZFS_TYPE_DATASET)) == NULL)
741c65a2111SKyle Evans 		return (set_error(lbh, BE_ERR_ZFSOPEN));
742c65a2111SKyle Evans 
743c65a2111SKyle Evans 	/* Set props */
744c65a2111SKyle Evans 	if ((err = zfs_prop_set(zfs, "canmount", "noauto")) != 0) {
745c65a2111SKyle Evans 		zfs_close(zfs);
746c65a2111SKyle Evans 		/*
747c65a2111SKyle Evans 		 * Similar to other cases, this shouldn't fail unless we've
748c65a2111SKyle Evans 		 * done something wrong.  This is a new dataset that shouldn't
749c65a2111SKyle Evans 		 * have been mounted anywhere between creation and now.
750c65a2111SKyle Evans 		 */
751c65a2111SKyle Evans 		if (err == EZFS_NOMEM)
752c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_NOMEM));
753c65a2111SKyle Evans 		return (set_error(lbh, BE_ERR_UNKNOWN));
754c65a2111SKyle Evans 	}
755c65a2111SKyle Evans 	zfs_close(zfs);
756c65a2111SKyle Evans 	return (BE_ERR_SUCCESS);
757c65a2111SKyle Evans }
758c65a2111SKyle Evans 
759c65a2111SKyle Evans static int
760c65a2111SKyle Evans be_create_child_cloned(libbe_handle_t *lbh, const char *active)
761c65a2111SKyle Evans {
7623d1a1f2cSKyle Evans 	char buf[BE_MAXPATHLEN], tmp[BE_MAXPATHLEN];;
763c65a2111SKyle Evans 	zfs_handle_t *zfs;
764c65a2111SKyle Evans 	int err;
765c65a2111SKyle Evans 
766c65a2111SKyle Evans 	/* XXX TODO ? */
767c65a2111SKyle Evans 
768c65a2111SKyle Evans 	/*
769c65a2111SKyle Evans 	 * Establish if the existing path is a zfs dataset or just
770c65a2111SKyle Evans 	 * the subdirectory of one
771c65a2111SKyle Evans 	 */
7723d1a1f2cSKyle Evans 	strlcpy(tmp, "tmp/be_snap.XXXXX", sizeof(tmp));
7733d1a1f2cSKyle Evans 	if (mktemp(tmp) == NULL)
774c65a2111SKyle Evans 		return (set_error(lbh, BE_ERR_UNKNOWN));
775c65a2111SKyle Evans 
7763d1a1f2cSKyle Evans 	be_root_concat(lbh, tmp, buf);
7773d1a1f2cSKyle Evans 	printf("Here %s?\n", buf);
778c65a2111SKyle Evans 	if ((err = zfs_snapshot(lbh->lzh, buf, false, NULL)) != 0) {
779c65a2111SKyle Evans 		switch (err) {
780c65a2111SKyle Evans 		case EZFS_INVALIDNAME:
781c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_INVALIDNAME));
782c65a2111SKyle Evans 
783c65a2111SKyle Evans 		default:
784c65a2111SKyle Evans 			/*
785c65a2111SKyle Evans 			 * The other errors that zfs_ioc_snapshot might return
786c65a2111SKyle Evans 			 * shouldn't happen if we've set things up properly, so
787c65a2111SKyle Evans 			 * we'll gloss over them and call it UNKNOWN as it will
788c65a2111SKyle Evans 			 * require further triage.
789c65a2111SKyle Evans 			 */
790c65a2111SKyle Evans 			if (errno == ENOTSUP)
791c65a2111SKyle Evans 				return (set_error(lbh, BE_ERR_NOPOOL));
792c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_UNKNOWN));
793c65a2111SKyle Evans 		}
794c65a2111SKyle Evans 	}
795c65a2111SKyle Evans 
796c65a2111SKyle Evans 	/* Clone */
797c65a2111SKyle Evans 	if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT)) == NULL)
798c65a2111SKyle Evans 		return (BE_ERR_ZFSOPEN);
799c65a2111SKyle Evans 
800c65a2111SKyle Evans 	if ((err = zfs_clone(zfs, active, NULL)) != 0)
801c65a2111SKyle Evans 		/* XXX TODO correct error */
802c65a2111SKyle Evans 		return (set_error(lbh, BE_ERR_UNKNOWN));
803c65a2111SKyle Evans 
804c65a2111SKyle Evans 	/* set props */
805c65a2111SKyle Evans 	zfs_close(zfs);
806c65a2111SKyle Evans 	return (BE_ERR_SUCCESS);
807c65a2111SKyle Evans }
80828f16a0fSKyle Evans 
80928f16a0fSKyle Evans int
81073c3d608SKyle Evans be_add_child(libbe_handle_t *lbh, const char *child_path, bool cp_if_exists)
81128f16a0fSKyle Evans {
81273c3d608SKyle Evans 	struct stat sb;
813c65a2111SKyle Evans 	char active[BE_MAXPATHLEN], buf[BE_MAXPATHLEN];
81428f16a0fSKyle Evans 	nvlist_t *props;
81573c3d608SKyle Evans 	const char *s;
81628f16a0fSKyle Evans 
81728f16a0fSKyle Evans 	/* Require absolute paths */
818bfe0869cSKyle Evans 	if (*child_path != '/')
8196d4b1d24SKyle Evans 		return (set_error(lbh, BE_ERR_BADPATH));
82028f16a0fSKyle Evans 
8216d4b1d24SKyle Evans 	strlcpy(active, be_active_path(lbh), BE_MAXPATHLEN);
82228f16a0fSKyle Evans 	strcpy(buf, active);
82328f16a0fSKyle Evans 
82428f16a0fSKyle Evans 	/* Create non-mountable parent dataset(s) */
82573c3d608SKyle Evans 	s = child_path;
82628f16a0fSKyle Evans 	for (char *p; (p = strchr(s+1, '/')) != NULL; s = p) {
82728f16a0fSKyle Evans 		size_t len = p - s;
82828f16a0fSKyle Evans 		strncat(buf, s, len);
82928f16a0fSKyle Evans 
83028f16a0fSKyle Evans 		nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
83128f16a0fSKyle Evans 		nvlist_add_string(props, "canmount", "off");
83228f16a0fSKyle Evans 		nvlist_add_string(props, "mountpoint", "none");
83328f16a0fSKyle Evans 		zfs_create(lbh->lzh, buf, ZFS_TYPE_DATASET, props);
83428f16a0fSKyle Evans 		nvlist_free(props);
83528f16a0fSKyle Evans 	}
83628f16a0fSKyle Evans 
83728f16a0fSKyle Evans 	/* Path does not exist as a descendent of / yet */
8386d4b1d24SKyle Evans 	if (strlcat(active, child_path, BE_MAXPATHLEN) >= BE_MAXPATHLEN)
8396d4b1d24SKyle Evans 		return (set_error(lbh, BE_ERR_PATHLEN));
84028f16a0fSKyle Evans 
84128f16a0fSKyle Evans 	if (stat(child_path, &sb) != 0) {
84228f16a0fSKyle Evans 		/* Verify that error is ENOENT */
8436d4b1d24SKyle Evans 		if (errno != ENOENT)
8446d4b1d24SKyle Evans 			return (set_error(lbh, BE_ERR_UNKNOWN));
845c65a2111SKyle Evans 		return (be_create_child_noent(lbh, active, child_path));
846c65a2111SKyle Evans 	} else if (cp_if_exists)
84728f16a0fSKyle Evans 		/* Path is already a descendent of / and should be copied */
848c65a2111SKyle Evans 		return (be_create_child_cloned(lbh, active));
8496d4b1d24SKyle Evans 	return (set_error(lbh, BE_ERR_EXISTS));
85028f16a0fSKyle Evans }
8513d1a1f2cSKyle Evans #endif	/* SOON */
85228f16a0fSKyle Evans 
853d06f7103SKyle Evans static int
854d06f7103SKyle Evans be_set_nextboot(libbe_handle_t *lbh, nvlist_t *config, uint64_t pool_guid,
855d06f7103SKyle Evans     const char *zfsdev)
856d06f7103SKyle Evans {
857d06f7103SKyle Evans 	nvlist_t **child;
858d06f7103SKyle Evans 	uint64_t vdev_guid;
859d06f7103SKyle Evans 	int c, children;
860d06f7103SKyle Evans 
861d06f7103SKyle Evans 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, &child,
862d06f7103SKyle Evans 	    &children) == 0) {
863d06f7103SKyle Evans 		for (c = 0; c < children; ++c)
864d06f7103SKyle Evans 			if (be_set_nextboot(lbh, child[c], pool_guid, zfsdev) != 0)
865d06f7103SKyle Evans 				return (1);
866d06f7103SKyle Evans 		return (0);
867d06f7103SKyle Evans 	}
868d06f7103SKyle Evans 
869d06f7103SKyle Evans 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
870d06f7103SKyle Evans 	    &vdev_guid) != 0) {
871d06f7103SKyle Evans 		return (1);
872d06f7103SKyle Evans 	}
873d06f7103SKyle Evans 
874d06f7103SKyle Evans 	if (zpool_nextboot(lbh->lzh, pool_guid, vdev_guid, zfsdev) != 0) {
875d06f7103SKyle Evans 		perror("ZFS_IOC_NEXTBOOT failed");
876d06f7103SKyle Evans 		return (1);
877d06f7103SKyle Evans 	}
878d06f7103SKyle Evans 
879d06f7103SKyle Evans 	return (0);
880d06f7103SKyle Evans }
881d06f7103SKyle Evans 
8828d4ce358SKyle Evans /*
8838d4ce358SKyle Evans  * Deactivate old BE dataset; currently just sets canmount=noauto
8848d4ce358SKyle Evans  */
8858d4ce358SKyle Evans static int
8868d4ce358SKyle Evans be_deactivate(libbe_handle_t *lbh, const char *ds)
8878d4ce358SKyle Evans {
8888d4ce358SKyle Evans 	zfs_handle_t *zfs;
8898d4ce358SKyle Evans 
8908d4ce358SKyle Evans 	if ((zfs = zfs_open(lbh->lzh, ds, ZFS_TYPE_DATASET)) == NULL)
8918d4ce358SKyle Evans 		return (1);
8928d4ce358SKyle Evans 	if (zfs_prop_set(zfs, "canmount", "noauto") != 0)
8938d4ce358SKyle Evans 		return (1);
8948d4ce358SKyle Evans 	zfs_close(zfs);
8958d4ce358SKyle Evans 	return (0);
8968d4ce358SKyle Evans }
89728f16a0fSKyle Evans 
89828f16a0fSKyle Evans int
89973c3d608SKyle Evans be_activate(libbe_handle_t *lbh, const char *bootenv, bool temporary)
90028f16a0fSKyle Evans {
90128f16a0fSKyle Evans 	char be_path[BE_MAXPATHLEN];
90228f16a0fSKyle Evans 	char buf[BE_MAXPATHLEN];
9034635676dSKyle Evans 	nvlist_t *config, *dsprops, *vdevs;
9044635676dSKyle Evans 	char *origin;
9050cadc427SKyle Evans 	uint64_t pool_guid;
9060cadc427SKyle Evans 	zfs_handle_t *zhp;
90728f16a0fSKyle Evans 	int err;
90828f16a0fSKyle Evans 
90928f16a0fSKyle Evans 	be_root_concat(lbh, bootenv, be_path);
91028f16a0fSKyle Evans 
91128f16a0fSKyle Evans 	/* Note: be_exists fails if mountpoint is not / */
912162ec569SKyle Evans 	if ((err = be_exists(lbh, be_path)) != 0)
913162ec569SKyle Evans 		return (set_error(lbh, err));
91428f16a0fSKyle Evans 
91528f16a0fSKyle Evans 	if (temporary) {
916d06f7103SKyle Evans 		config = zpool_get_config(lbh->active_phandle, NULL);
917c65a2111SKyle Evans 		if (config == NULL)
918c65a2111SKyle Evans 			/* config should be fetchable... */
919c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_UNKNOWN));
92028f16a0fSKyle Evans 
921d06f7103SKyle Evans 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
922d06f7103SKyle Evans 		    &pool_guid) != 0)
923c65a2111SKyle Evans 			/* Similarly, it shouldn't be possible */
924c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_UNKNOWN));
925d06f7103SKyle Evans 
92628f16a0fSKyle Evans 		/* Expected format according to zfsbootcfg(8) man */
927a8e44f4dSKyle Evans 		snprintf(buf, sizeof(buf), "zfs:%s:", be_path);
92828f16a0fSKyle Evans 
929c65a2111SKyle Evans 		/* We have no config tree */
930c65a2111SKyle Evans 		if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
931c65a2111SKyle Evans 		    &vdevs) != 0)
932c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_NOPOOL));
93328f16a0fSKyle Evans 
934d06f7103SKyle Evans 		return (be_set_nextboot(lbh, vdevs, pool_guid, buf));
93528f16a0fSKyle Evans 	} else {
9368d4ce358SKyle Evans 		if (be_deactivate(lbh, lbh->bootfs) != 0)
9378d4ce358SKyle Evans 			return (-1);
9388d4ce358SKyle Evans 
93928f16a0fSKyle Evans 		/* Obtain bootenv zpool */
940c3a34c08SKyle Evans 		err = zpool_set_prop(lbh->active_phandle, "bootfs", be_path);
9410cadc427SKyle Evans 		if (err)
9420cadc427SKyle Evans 			return (-1);
94328f16a0fSKyle Evans 
9440cadc427SKyle Evans 		zhp = zfs_open(lbh->lzh, be_path, ZFS_TYPE_FILESYSTEM);
9450cadc427SKyle Evans 		if (zhp == NULL)
9460cadc427SKyle Evans 			return (-1);
94728f16a0fSKyle Evans 
9484635676dSKyle Evans 		if (be_prop_list_alloc(&dsprops) != 0)
9494635676dSKyle Evans 			return (-1);
9504635676dSKyle Evans 
9514635676dSKyle Evans 		if (be_get_dataset_props(lbh, be_path, dsprops) != 0) {
9524635676dSKyle Evans 			nvlist_free(dsprops);
9534635676dSKyle Evans 			return (-1);
9544635676dSKyle Evans 		}
9554635676dSKyle Evans 
9564635676dSKyle Evans 		if (nvlist_lookup_string(dsprops, "origin", &origin) == 0)
9570cadc427SKyle Evans 			err = zfs_promote(zhp);
9584635676dSKyle Evans 		nvlist_free(dsprops);
9594635676dSKyle Evans 
9600cadc427SKyle Evans 		zfs_close(zhp);
9610cadc427SKyle Evans 
9620cadc427SKyle Evans 		if (err)
96328f16a0fSKyle Evans 			return (-1);
96428f16a0fSKyle Evans 	}
9650cadc427SKyle Evans 
9660cadc427SKyle Evans 	return (BE_ERR_SUCCESS);
96728f16a0fSKyle Evans }
968