xref: /freebsd/lib/libbe/be.c (revision 16ac0705815ba661a3c1a693ff3a7562e932fb47)
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 {
614ab5187dSKyle Evans 	struct statfs sfs;
624ab5187dSKyle Evans 	struct extmnttab entry;
6351aecc89SKyle Evans 	zfs_handle_t *zfs;
64ee16b7c9SKyle Evans 
654ab5187dSKyle Evans 	/*
664ab5187dSKyle Evans 	 * Check first if root is ZFS; if not, we'll bail on rootfs capture.
674ab5187dSKyle Evans 	 * Unfortunately needed because zfs_path_to_zhandle will emit to
684ab5187dSKyle Evans 	 * stderr if / isn't actually a ZFS filesystem, which we'd like
694ab5187dSKyle Evans 	 * to avoid.
704ab5187dSKyle Evans 	 */
714ab5187dSKyle Evans 	if (statfs("/", &sfs) == 0) {
724ab5187dSKyle Evans 		statfs2mnttab(&sfs, &entry);
734ab5187dSKyle Evans 		if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
744ab5187dSKyle Evans 			return (1);
754ab5187dSKyle Evans 	} else
764ab5187dSKyle Evans 		return (1);
7751aecc89SKyle Evans 	zfs = zfs_path_to_zhandle(lbh->lzh, "/", ZFS_TYPE_FILESYSTEM);
7851aecc89SKyle Evans 	if (zfs == NULL)
79ee16b7c9SKyle Evans 		return (1);
80ee16b7c9SKyle Evans 
8151aecc89SKyle Evans 	strlcpy(lbh->rootfs, zfs_get_name(zfs), sizeof(lbh->rootfs));
8251aecc89SKyle Evans 	zfs_close(zfs);
83ee16b7c9SKyle Evans 	return (0);
84ee16b7c9SKyle Evans }
85ee16b7c9SKyle Evans 
86ee16b7c9SKyle Evans /*
8728f16a0fSKyle Evans  * Initializes the libbe context to operate in the root boot environment
8828f16a0fSKyle Evans  * dataset, for example, zroot/ROOT.
8928f16a0fSKyle Evans  */
9028f16a0fSKyle Evans libbe_handle_t *
91cc624025SKyle Evans libbe_init(const char *root)
9228f16a0fSKyle Evans {
93fc13fc1cSKyle Evans 	char altroot[MAXPATHLEN];
9428f16a0fSKyle Evans 	libbe_handle_t *lbh;
95c3a34c08SKyle Evans 	char *poolname, *pos;
96c3a34c08SKyle Evans 	int pnamelen;
9728f16a0fSKyle Evans 
98c3a34c08SKyle Evans 	lbh = NULL;
99c3a34c08SKyle Evans 	poolname = pos = NULL;
10028f16a0fSKyle Evans 
101c3a34c08SKyle Evans 	if ((lbh = calloc(1, sizeof(libbe_handle_t))) == NULL)
102c3a34c08SKyle Evans 		goto err;
10328f16a0fSKyle Evans 
104c3a34c08SKyle Evans 	if ((lbh->lzh = libzfs_init()) == NULL)
105c3a34c08SKyle Evans 		goto err;
10628f16a0fSKyle Evans 
107cc624025SKyle Evans 	/*
108cc624025SKyle Evans 	 * Grab rootfs, we'll work backwards from there if an optional BE root
109cc624025SKyle Evans 	 * has not been passed in.
110cc624025SKyle Evans 	 */
1114ab5187dSKyle Evans 	if (be_locate_rootfs(lbh) != 0) {
1124ab5187dSKyle Evans 		if (root == NULL)
113c3a34c08SKyle Evans 			goto err;
1144ab5187dSKyle Evans 		*lbh->rootfs = '\0';
1154ab5187dSKyle Evans 	}
116cc624025SKyle Evans 	if (root == NULL) {
117cc624025SKyle Evans 		/* Strip off the final slash from rootfs to get the be root */
11851aecc89SKyle Evans 		strlcpy(lbh->root, lbh->rootfs, sizeof(lbh->root));
11951aecc89SKyle Evans 		pos = strrchr(lbh->root, '/');
12051aecc89SKyle Evans 		if (pos == NULL)
12151aecc89SKyle Evans 			goto err;
12251aecc89SKyle Evans 		*pos = '\0';
123cc624025SKyle Evans 	} else
124cc624025SKyle Evans 		strlcpy(lbh->root, root, sizeof(lbh->root));
125c3a34c08SKyle Evans 
126c3a34c08SKyle Evans 	if ((pos = strchr(lbh->root, '/')) == NULL)
127c3a34c08SKyle Evans 		goto err;
128c3a34c08SKyle Evans 
129c3a34c08SKyle Evans 	pnamelen = pos - lbh->root;
130c3a34c08SKyle Evans 	poolname = malloc(pnamelen + 1);
131c3a34c08SKyle Evans 	if (poolname == NULL)
132c3a34c08SKyle Evans 		goto err;
133c3a34c08SKyle Evans 
13455b0e92bSKyle Evans 	strlcpy(poolname, lbh->root, pnamelen + 1);
135c3a34c08SKyle Evans 	if ((lbh->active_phandle = zpool_open(lbh->lzh, poolname)) == NULL)
136c3a34c08SKyle Evans 		goto err;
137a8e44f4dSKyle Evans 	free(poolname);
138a8e44f4dSKyle Evans 	poolname = NULL;
139c3a34c08SKyle Evans 
140c3a34c08SKyle Evans 	if (zpool_get_prop(lbh->active_phandle, ZPOOL_PROP_BOOTFS, lbh->bootfs,
14155b0e92bSKyle Evans 	    sizeof(lbh->bootfs), NULL, true) != 0)
142c3a34c08SKyle Evans 		goto err;
143c3a34c08SKyle Evans 
144fc13fc1cSKyle Evans 	if (zpool_get_prop(lbh->active_phandle, ZPOOL_PROP_ALTROOT,
145fc13fc1cSKyle Evans 	    altroot, sizeof(altroot), NULL, true) == 0 &&
146fc13fc1cSKyle Evans 	    strcmp(altroot, "-") != 0)
147fc13fc1cSKyle Evans 		lbh->altroot_len = strlen(altroot);
148fc13fc1cSKyle Evans 
14928f16a0fSKyle Evans 	return (lbh);
150c3a34c08SKyle Evans err:
151c3a34c08SKyle Evans 	if (lbh != NULL) {
152c3a34c08SKyle Evans 		if (lbh->active_phandle != NULL)
153c3a34c08SKyle Evans 			zpool_close(lbh->active_phandle);
154c3a34c08SKyle Evans 		if (lbh->lzh != NULL)
155c3a34c08SKyle Evans 			libzfs_fini(lbh->lzh);
156c3a34c08SKyle Evans 		free(lbh);
157c3a34c08SKyle Evans 	}
158c3a34c08SKyle Evans 	free(poolname);
159c3a34c08SKyle Evans 	return (NULL);
16028f16a0fSKyle Evans }
16128f16a0fSKyle Evans 
16228f16a0fSKyle Evans 
16328f16a0fSKyle Evans /*
16428f16a0fSKyle Evans  * Free memory allocated by libbe_init()
16528f16a0fSKyle Evans  */
16628f16a0fSKyle Evans void
16728f16a0fSKyle Evans libbe_close(libbe_handle_t *lbh)
16828f16a0fSKyle Evans {
169bfe0869cSKyle Evans 
170c3a34c08SKyle Evans 	if (lbh->active_phandle != NULL)
171c3a34c08SKyle Evans 		zpool_close(lbh->active_phandle);
17228f16a0fSKyle Evans 	libzfs_fini(lbh->lzh);
17328f16a0fSKyle Evans 	free(lbh);
17428f16a0fSKyle Evans }
17528f16a0fSKyle Evans 
1769b1662e6SKyle Evans /*
1779b1662e6SKyle Evans  * Proxy through to libzfs for the moment.
1789b1662e6SKyle Evans  */
1799b1662e6SKyle Evans void
1809b1662e6SKyle Evans be_nicenum(uint64_t num, char *buf, size_t buflen)
1819b1662e6SKyle Evans {
1829b1662e6SKyle Evans 
1839b1662e6SKyle Evans 	zfs_nicenum(num, buf, buflen);
1849b1662e6SKyle Evans }
18528f16a0fSKyle Evans 
186920abf4dSKyle Evans static int
187920abf4dSKyle Evans be_destroy_cb(zfs_handle_t *zfs_hdl, void *data)
188920abf4dSKyle Evans {
189920abf4dSKyle Evans 	int err;
190920abf4dSKyle Evans 
191920abf4dSKyle Evans 	if ((err = zfs_iter_children(zfs_hdl, be_destroy_cb, data)) != 0)
192920abf4dSKyle Evans 		return (err);
193920abf4dSKyle Evans 	if ((err = zfs_destroy(zfs_hdl, false)) != 0)
194920abf4dSKyle Evans 		return (err);
195920abf4dSKyle Evans 	return (0);
196920abf4dSKyle Evans }
197920abf4dSKyle Evans 
19828f16a0fSKyle Evans /*
19928f16a0fSKyle Evans  * Destroy the boot environment or snapshot specified by the name
20028f16a0fSKyle Evans  * parameter. Options are or'd together with the possible values:
20128f16a0fSKyle Evans  * BE_DESTROY_FORCE : forces operation on mounted datasets
20228f16a0fSKyle Evans  */
20328f16a0fSKyle Evans int
20473c3d608SKyle Evans be_destroy(libbe_handle_t *lbh, const char *name, int options)
20528f16a0fSKyle Evans {
20628f16a0fSKyle Evans 	zfs_handle_t *fs;
20728f16a0fSKyle Evans 	char path[BE_MAXPATHLEN];
208bfe0869cSKyle Evans 	char *p;
209bfe0869cSKyle Evans 	int err, force, mounted;
21028f16a0fSKyle Evans 
211bfe0869cSKyle Evans 	p = path;
212bfe0869cSKyle Evans 	force = options & BE_DESTROY_FORCE;
21328f16a0fSKyle Evans 
21428f16a0fSKyle Evans 	be_root_concat(lbh, name, path);
21528f16a0fSKyle Evans 
21628f16a0fSKyle Evans 	if (strchr(name, '@') == NULL) {
217bfe0869cSKyle Evans 		if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_FILESYSTEM))
21828f16a0fSKyle Evans 			return (set_error(lbh, BE_ERR_NOENT));
21928f16a0fSKyle Evans 
220f08dac4eSKyle Evans 		if (strcmp(path, lbh->rootfs) == 0 ||
221f08dac4eSKyle Evans 		    strcmp(path, lbh->bootfs) == 0)
22228f16a0fSKyle Evans 			return (set_error(lbh, BE_ERR_DESTROYACT));
22328f16a0fSKyle Evans 
22428f16a0fSKyle Evans 		fs = zfs_open(lbh->lzh, p, ZFS_TYPE_FILESYSTEM);
22528f16a0fSKyle Evans 	} else {
22628f16a0fSKyle Evans 
227bfe0869cSKyle Evans 		if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_SNAPSHOT))
22828f16a0fSKyle Evans 			return (set_error(lbh, BE_ERR_NOENT));
22928f16a0fSKyle Evans 
23028f16a0fSKyle Evans 		fs = zfs_open(lbh->lzh, p, ZFS_TYPE_SNAPSHOT);
23128f16a0fSKyle Evans 	}
23228f16a0fSKyle Evans 
23328f16a0fSKyle Evans 	if (fs == NULL)
23428f16a0fSKyle Evans 		return (set_error(lbh, BE_ERR_ZFSOPEN));
23528f16a0fSKyle Evans 
23628f16a0fSKyle Evans 	/* Check if mounted, unmount if force is specified */
237b29bf2f8SKyle Evans 	if ((mounted = zfs_is_mounted(fs, NULL)) != 0) {
238bfe0869cSKyle Evans 		if (force)
23928f16a0fSKyle Evans 			zfs_unmount(fs, NULL, 0);
240bfe0869cSKyle Evans 		else
24128f16a0fSKyle Evans 			return (set_error(lbh, BE_ERR_DESTROYMNT));
24228f16a0fSKyle Evans 	}
24328f16a0fSKyle Evans 
244920abf4dSKyle Evans 	if ((err = be_destroy_cb(fs, NULL)) != 0) {
245920abf4dSKyle Evans 		/* Children are still present or the mount is referenced */
246920abf4dSKyle Evans 		if (err == EBUSY)
247920abf4dSKyle Evans 			return (set_error(lbh, BE_ERR_DESTROYMNT));
248920abf4dSKyle Evans 		return (set_error(lbh, BE_ERR_UNKNOWN));
249920abf4dSKyle Evans 	}
25028f16a0fSKyle Evans 
251920abf4dSKyle Evans 	return (0);
25228f16a0fSKyle Evans }
25328f16a0fSKyle Evans 
25428f16a0fSKyle Evans 
25528f16a0fSKyle Evans int
256b29bf2f8SKyle Evans be_snapshot(libbe_handle_t *lbh, const char *source, const char *snap_name,
25728f16a0fSKyle Evans     bool recursive, char *result)
25828f16a0fSKyle Evans {
25928f16a0fSKyle Evans 	char buf[BE_MAXPATHLEN];
26028f16a0fSKyle Evans 	time_t rawtime;
26128f16a0fSKyle Evans 	int len, err;
26228f16a0fSKyle Evans 
26328f16a0fSKyle Evans 	be_root_concat(lbh, source, buf);
26428f16a0fSKyle Evans 
265162ec569SKyle Evans 	if ((err = be_exists(lbh, buf)) != 0)
266162ec569SKyle Evans 		return (set_error(lbh, err));
26728f16a0fSKyle Evans 
26828f16a0fSKyle Evans 	if (snap_name != NULL) {
269a8e44f4dSKyle Evans 		if (strlcat(buf, "@", sizeof(buf)) >= sizeof(buf))
270a8e44f4dSKyle Evans 			return (set_error(lbh, BE_ERR_INVALIDNAME));
271a8e44f4dSKyle Evans 
272a8e44f4dSKyle Evans 		if (strlcat(buf, snap_name, sizeof(buf)) >= sizeof(buf))
273a8e44f4dSKyle Evans 			return (set_error(lbh, BE_ERR_INVALIDNAME));
274a8e44f4dSKyle Evans 
275bfe0869cSKyle Evans 		if (result != NULL)
27628f16a0fSKyle Evans 			snprintf(result, BE_MAXPATHLEN, "%s@%s", source,
27728f16a0fSKyle Evans 			    snap_name);
27828f16a0fSKyle Evans 	} else {
27928f16a0fSKyle Evans 		time(&rawtime);
28028f16a0fSKyle Evans 		len = strlen(buf);
28155b0e92bSKyle Evans 		strftime(buf + len, sizeof(buf) - len,
28228f16a0fSKyle Evans 		    "@%F-%T", localtime(&rawtime));
283a8e44f4dSKyle Evans 		if (result != NULL && strlcpy(result, strrchr(buf, '/') + 1,
284a8e44f4dSKyle Evans 		    sizeof(buf)) >= sizeof(buf))
285a8e44f4dSKyle Evans 			return (set_error(lbh, BE_ERR_INVALIDNAME));
28628f16a0fSKyle Evans 	}
28728f16a0fSKyle Evans 
288b29bf2f8SKyle Evans 	if ((err = zfs_snapshot(lbh->lzh, buf, recursive, NULL)) != 0) {
28928f16a0fSKyle Evans 		switch (err) {
29028f16a0fSKyle Evans 		case EZFS_INVALIDNAME:
29128f16a0fSKyle Evans 			return (set_error(lbh, BE_ERR_INVALIDNAME));
29228f16a0fSKyle Evans 
29328f16a0fSKyle Evans 		default:
2942989df09SKyle Evans 			/*
2952989df09SKyle Evans 			 * The other errors that zfs_ioc_snapshot might return
2962989df09SKyle Evans 			 * shouldn't happen if we've set things up properly, so
2972989df09SKyle Evans 			 * we'll gloss over them and call it UNKNOWN as it will
2982989df09SKyle Evans 			 * require further triage.
2992989df09SKyle Evans 			 */
3002989df09SKyle Evans 			if (errno == ENOTSUP)
3012989df09SKyle Evans 				return (set_error(lbh, BE_ERR_NOPOOL));
30228f16a0fSKyle Evans 			return (set_error(lbh, BE_ERR_UNKNOWN));
30328f16a0fSKyle Evans 		}
30428f16a0fSKyle Evans 	}
30528f16a0fSKyle Evans 
30628f16a0fSKyle Evans 	return (BE_ERR_SUCCESS);
30728f16a0fSKyle Evans }
30828f16a0fSKyle Evans 
30928f16a0fSKyle Evans 
31028f16a0fSKyle Evans /*
31128f16a0fSKyle Evans  * Create the boot environment specified by the name parameter
31228f16a0fSKyle Evans  */
31328f16a0fSKyle Evans int
31473c3d608SKyle Evans be_create(libbe_handle_t *lbh, const char *name)
31528f16a0fSKyle Evans {
31628f16a0fSKyle Evans 	int err;
31728f16a0fSKyle Evans 
318b29bf2f8SKyle Evans 	err = be_create_from_existing(lbh, name, be_active_path(lbh));
31928f16a0fSKyle Evans 
32028f16a0fSKyle Evans 	return (set_error(lbh, err));
32128f16a0fSKyle Evans }
32228f16a0fSKyle Evans 
32328f16a0fSKyle Evans static int
32428f16a0fSKyle Evans be_deep_clone_prop(int prop, void *cb)
32528f16a0fSKyle Evans {
32628f16a0fSKyle Evans 	int err;
327bfe0869cSKyle Evans         struct libbe_dccb *dccb;
32828f16a0fSKyle Evans 	zprop_source_t src;
32928f16a0fSKyle Evans 	char pval[BE_MAXPATHLEN];
33028f16a0fSKyle Evans 	char source[BE_MAXPATHLEN];
331af43c24dSKyle Evans 	char *val;
33228f16a0fSKyle Evans 
333bfe0869cSKyle Evans 	dccb = cb;
33428f16a0fSKyle Evans 	/* Skip some properties we don't want to touch */
33573c3d608SKyle Evans 	if (prop == ZFS_PROP_CANMOUNT)
33628f16a0fSKyle Evans 		return (ZPROP_CONT);
33728f16a0fSKyle Evans 
33828f16a0fSKyle Evans 	/* Don't copy readonly properties */
339bfe0869cSKyle Evans 	if (zfs_prop_readonly(prop))
34028f16a0fSKyle Evans 		return (ZPROP_CONT);
34128f16a0fSKyle Evans 
34228f16a0fSKyle Evans 	if ((err = zfs_prop_get(dccb->zhp, prop, (char *)&pval,
343bfe0869cSKyle Evans 	    sizeof(pval), &src, (char *)&source, sizeof(source), false)))
34428f16a0fSKyle Evans 		/* Just continue if we fail to read a property */
34528f16a0fSKyle Evans 		return (ZPROP_CONT);
346bfe0869cSKyle Evans 
34728f16a0fSKyle Evans 	/* Only copy locally defined properties */
348bfe0869cSKyle Evans 	if (src != ZPROP_SRC_LOCAL)
34928f16a0fSKyle Evans 		return (ZPROP_CONT);
35028f16a0fSKyle Evans 
351af43c24dSKyle Evans 	/* Augment mountpoint with altroot, if needed */
352af43c24dSKyle Evans 	val = pval;
353fc13fc1cSKyle Evans 	if (prop == ZFS_PROP_MOUNTPOINT)
354fc13fc1cSKyle Evans 		val = be_mountpoint_augmented(dccb->lbh, val);
355fc13fc1cSKyle Evans 
356af43c24dSKyle Evans 	nvlist_add_string(dccb->props, zfs_prop_to_name(prop), val);
35728f16a0fSKyle Evans 
35828f16a0fSKyle Evans 	return (ZPROP_CONT);
35928f16a0fSKyle Evans }
36028f16a0fSKyle Evans 
36128f16a0fSKyle Evans static int
36228f16a0fSKyle Evans be_deep_clone(zfs_handle_t *ds, void *data)
36328f16a0fSKyle Evans {
36428f16a0fSKyle Evans 	int err;
36528f16a0fSKyle Evans 	char be_path[BE_MAXPATHLEN];
36628f16a0fSKyle Evans 	char snap_path[BE_MAXPATHLEN];
36728f16a0fSKyle Evans 	const char *dspath;
36828f16a0fSKyle Evans 	char *dsname;
36928f16a0fSKyle Evans 	zfs_handle_t *snap_hdl;
37028f16a0fSKyle Evans 	nvlist_t *props;
371bfe0869cSKyle Evans 	struct libbe_deep_clone *isdc, sdc;
37228f16a0fSKyle Evans 	struct libbe_dccb dccb;
37328f16a0fSKyle Evans 
374bfe0869cSKyle Evans 	isdc = (struct libbe_deep_clone *)data;
37528f16a0fSKyle Evans 	dspath = zfs_get_name(ds);
376bfe0869cSKyle Evans 	if ((dsname = strrchr(dspath, '/')) == NULL)
37728f16a0fSKyle Evans 		return (BE_ERR_UNKNOWN);
37828f16a0fSKyle Evans 	dsname++;
379bfe0869cSKyle Evans 
380bfe0869cSKyle Evans 	if (isdc->bename == NULL)
38128f16a0fSKyle Evans 		snprintf(be_path, sizeof(be_path), "%s/%s", isdc->be_root, dsname);
382bfe0869cSKyle Evans 	else
38328f16a0fSKyle Evans 		snprintf(be_path, sizeof(be_path), "%s/%s", isdc->be_root, isdc->bename);
384bfe0869cSKyle Evans 
38528f16a0fSKyle Evans 	snprintf(snap_path, sizeof(snap_path), "%s@%s", dspath, isdc->snapname);
38628f16a0fSKyle Evans 
387bfe0869cSKyle Evans 	if (zfs_dataset_exists(isdc->lbh->lzh, be_path, ZFS_TYPE_DATASET))
38828f16a0fSKyle Evans 		return (set_error(isdc->lbh, BE_ERR_EXISTS));
38928f16a0fSKyle Evans 
39028f16a0fSKyle Evans 	if ((snap_hdl =
391bfe0869cSKyle Evans 	    zfs_open(isdc->lbh->lzh, snap_path, ZFS_TYPE_SNAPSHOT)) == NULL)
39228f16a0fSKyle Evans 		return (set_error(isdc->lbh, BE_ERR_ZFSOPEN));
39328f16a0fSKyle Evans 
39428f16a0fSKyle Evans 	nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
39528f16a0fSKyle Evans 	nvlist_add_string(props, "canmount", "noauto");
39628f16a0fSKyle Evans 
397fc13fc1cSKyle Evans 	dccb.lbh = isdc->lbh;
39828f16a0fSKyle Evans 	dccb.zhp = ds;
39928f16a0fSKyle Evans 	dccb.props = props;
40028f16a0fSKyle Evans 	if (zprop_iter(be_deep_clone_prop, &dccb, B_FALSE, B_FALSE,
401bfe0869cSKyle Evans 	    ZFS_TYPE_FILESYSTEM) == ZPROP_INVAL)
40228f16a0fSKyle Evans 		return (-1);
40328f16a0fSKyle Evans 
404cc4deabcSKyle Evans 	if ((err = zfs_clone(snap_hdl, be_path, props)) != 0)
40528f16a0fSKyle Evans 		err = BE_ERR_ZFSCLONE;
40628f16a0fSKyle Evans 
40728f16a0fSKyle Evans 	nvlist_free(props);
40828f16a0fSKyle Evans 	zfs_close(snap_hdl);
40928f16a0fSKyle Evans 
410cc4deabcSKyle Evans 	/* Failed to clone */
411cc4deabcSKyle Evans 	if (err != BE_ERR_SUCCESS)
412cc4deabcSKyle Evans 		return (set_error(isdc->lbh, err));
413cc4deabcSKyle Evans 
41428f16a0fSKyle Evans 	sdc.lbh = isdc->lbh;
41528f16a0fSKyle Evans 	sdc.bename = NULL;
41628f16a0fSKyle Evans 	sdc.snapname = isdc->snapname;
41728f16a0fSKyle Evans 	sdc.be_root = (char *)&be_path;
41828f16a0fSKyle Evans 
41928f16a0fSKyle Evans 	err = zfs_iter_filesystems(ds, be_deep_clone, &sdc);
42028f16a0fSKyle Evans 
42128f16a0fSKyle Evans 	return (err);
42228f16a0fSKyle Evans }
42328f16a0fSKyle Evans 
42428f16a0fSKyle Evans /*
42528f16a0fSKyle Evans  * Create the boot environment from pre-existing snapshot
42628f16a0fSKyle Evans  */
42728f16a0fSKyle Evans int
428b29bf2f8SKyle Evans be_create_from_existing_snap(libbe_handle_t *lbh, const char *name,
429b29bf2f8SKyle Evans     const char *snap)
43028f16a0fSKyle Evans {
43128f16a0fSKyle Evans 	int err;
43228f16a0fSKyle Evans 	char be_path[BE_MAXPATHLEN];
43328f16a0fSKyle Evans 	char snap_path[BE_MAXPATHLEN];
434b29bf2f8SKyle Evans 	const char *bename;
435b29bf2f8SKyle Evans 	char *parentname, *snapname;
43628f16a0fSKyle Evans 	zfs_handle_t *parent_hdl;
43728f16a0fSKyle Evans 	struct libbe_deep_clone sdc;
43828f16a0fSKyle Evans 
439b29bf2f8SKyle Evans 	if ((err = be_validate_name(lbh, name)) != 0)
44028f16a0fSKyle Evans 		return (set_error(lbh, err));
441b29bf2f8SKyle Evans 	if ((err = be_root_concat(lbh, snap, snap_path)) != 0)
44228f16a0fSKyle Evans 		return (set_error(lbh, err));
443b29bf2f8SKyle Evans 	if ((err = be_validate_snap(lbh, snap_path)) != 0)
44428f16a0fSKyle Evans 		return (set_error(lbh, err));
44528f16a0fSKyle Evans 
446b29bf2f8SKyle Evans 	if ((err = be_root_concat(lbh, name, be_path)) != 0)
44728f16a0fSKyle Evans 		return (set_error(lbh, err));
44828f16a0fSKyle Evans 
449bfe0869cSKyle Evans 	if ((bename = strrchr(name, '/')) == NULL)
45028f16a0fSKyle Evans 		bename = name;
451bfe0869cSKyle Evans 	else
45228f16a0fSKyle Evans 		bename++;
453bfe0869cSKyle Evans 
454cc4deabcSKyle Evans 	if ((parentname = strdup(snap_path)) == NULL)
455cc4deabcSKyle Evans 		return (set_error(lbh, BE_ERR_UNKNOWN));
456cc4deabcSKyle Evans 
45728f16a0fSKyle Evans 	snapname = strchr(parentname, '@');
45828f16a0fSKyle Evans 	if (snapname == NULL) {
459cc4deabcSKyle Evans 		free(parentname);
460cc4deabcSKyle Evans 		return (set_error(lbh, BE_ERR_UNKNOWN));
46128f16a0fSKyle Evans 	}
46228f16a0fSKyle Evans 	*snapname = '\0';
46328f16a0fSKyle Evans 	snapname++;
46428f16a0fSKyle Evans 
46528f16a0fSKyle Evans 	sdc.lbh = lbh;
46628f16a0fSKyle Evans 	sdc.bename = bename;
46728f16a0fSKyle Evans 	sdc.snapname = snapname;
46828f16a0fSKyle Evans 	sdc.be_root = lbh->root;
46928f16a0fSKyle Evans 
47028f16a0fSKyle Evans 	parent_hdl = zfs_open(lbh->lzh, parentname, ZFS_TYPE_DATASET);
47128f16a0fSKyle Evans 	err = be_deep_clone(parent_hdl, &sdc);
47228f16a0fSKyle Evans 
473cc4deabcSKyle Evans 	free(parentname);
47428f16a0fSKyle Evans 	return (set_error(lbh, err));
47528f16a0fSKyle Evans }
47628f16a0fSKyle Evans 
47728f16a0fSKyle Evans 
47828f16a0fSKyle Evans /*
47928f16a0fSKyle Evans  * Create a boot environment from an existing boot environment
48028f16a0fSKyle Evans  */
48128f16a0fSKyle Evans int
482b29bf2f8SKyle Evans be_create_from_existing(libbe_handle_t *lbh, const char *name, const char *old)
48328f16a0fSKyle Evans {
48428f16a0fSKyle Evans 	int err;
48528f16a0fSKyle Evans 	char buf[BE_MAXPATHLEN];
48628f16a0fSKyle Evans 
4875b7803a9SKyle Evans 	if ((err = be_snapshot(lbh, old, NULL, true, (char *)&buf)) != 0)
48828f16a0fSKyle Evans 		return (set_error(lbh, err));
48928f16a0fSKyle Evans 
49028f16a0fSKyle Evans 	err = be_create_from_existing_snap(lbh, name, (char *)buf);
49128f16a0fSKyle Evans 
49228f16a0fSKyle Evans 	return (set_error(lbh, err));
49328f16a0fSKyle Evans }
49428f16a0fSKyle Evans 
49528f16a0fSKyle Evans 
49628f16a0fSKyle Evans /*
49728f16a0fSKyle Evans  * Verifies that a snapshot has a valid name, exists, and has a mountpoint of
49828f16a0fSKyle Evans  * '/'. Returns BE_ERR_SUCCESS (0), upon success, or the relevant BE_ERR_* upon
49928f16a0fSKyle Evans  * failure. Does not set the internal library error state.
50028f16a0fSKyle Evans  */
50128f16a0fSKyle Evans int
502b29bf2f8SKyle Evans be_validate_snap(libbe_handle_t *lbh, const char *snap_name)
50328f16a0fSKyle Evans {
50428f16a0fSKyle Evans 
505bfe0869cSKyle Evans 	if (strlen(snap_name) >= BE_MAXPATHLEN)
50628f16a0fSKyle Evans 		return (BE_ERR_PATHLEN);
50728f16a0fSKyle Evans 
50828f16a0fSKyle Evans 	if (!zfs_dataset_exists(lbh->lzh, snap_name,
509bfe0869cSKyle Evans 	    ZFS_TYPE_SNAPSHOT))
51028f16a0fSKyle Evans 		return (BE_ERR_NOENT);
51128f16a0fSKyle Evans 
51251aecc89SKyle Evans 	return (BE_ERR_SUCCESS);
51328f16a0fSKyle Evans }
51428f16a0fSKyle Evans 
51528f16a0fSKyle Evans 
51628f16a0fSKyle Evans /*
51728f16a0fSKyle Evans  * Idempotently appends the name argument to the root boot environment path
51828f16a0fSKyle Evans  * and copies the resulting string into the result buffer (which is assumed
51928f16a0fSKyle Evans  * to be at least BE_MAXPATHLEN characters long. Returns BE_ERR_SUCCESS upon
52028f16a0fSKyle Evans  * success, BE_ERR_PATHLEN if the resulting path is longer than BE_MAXPATHLEN,
52128f16a0fSKyle Evans  * or BE_ERR_INVALIDNAME if the name is a path that does not begin with
52228f16a0fSKyle Evans  * zfs_be_root. Does not set internal library error state.
52328f16a0fSKyle Evans  */
52428f16a0fSKyle Evans int
525b29bf2f8SKyle Evans be_root_concat(libbe_handle_t *lbh, const char *name, char *result)
52628f16a0fSKyle Evans {
52728f16a0fSKyle Evans 	size_t name_len, root_len;
52828f16a0fSKyle Evans 
52928f16a0fSKyle Evans 	name_len = strlen(name);
53028f16a0fSKyle Evans 	root_len = strlen(lbh->root);
53128f16a0fSKyle Evans 
53228f16a0fSKyle Evans 	/* Act idempotently; return be name if it is already a full path */
53328f16a0fSKyle Evans 	if (strrchr(name, '/') != NULL) {
534bfe0869cSKyle Evans 		if (strstr(name, lbh->root) != name)
53528f16a0fSKyle Evans 			return (BE_ERR_INVALIDNAME);
53628f16a0fSKyle Evans 
537bfe0869cSKyle Evans 		if (name_len >= BE_MAXPATHLEN)
53828f16a0fSKyle Evans 			return (BE_ERR_PATHLEN);
53928f16a0fSKyle Evans 
54055b0e92bSKyle Evans 		strlcpy(result, name, BE_MAXPATHLEN);
54128f16a0fSKyle Evans 		return (BE_ERR_SUCCESS);
54228f16a0fSKyle Evans 	} else if (name_len + root_len + 1 < BE_MAXPATHLEN) {
54328f16a0fSKyle Evans 		snprintf(result, BE_MAXPATHLEN, "%s/%s", lbh->root,
54428f16a0fSKyle Evans 		    name);
54528f16a0fSKyle Evans 		return (BE_ERR_SUCCESS);
54628f16a0fSKyle Evans 	}
54728f16a0fSKyle Evans 
54828f16a0fSKyle Evans 	return (BE_ERR_PATHLEN);
54928f16a0fSKyle Evans }
55028f16a0fSKyle Evans 
55128f16a0fSKyle Evans 
55228f16a0fSKyle Evans /*
55328f16a0fSKyle Evans  * Verifies the validity of a boot environment name (A-Za-z0-9-_.). Returns
5545b7803a9SKyle Evans  * BE_ERR_SUCCESS (0) if name is valid, otherwise returns BE_ERR_INVALIDNAME
5555b7803a9SKyle Evans  * or BE_ERR_PATHLEN.
55628f16a0fSKyle Evans  * Does not set internal library error state.
55728f16a0fSKyle Evans  */
55828f16a0fSKyle Evans int
5595b7803a9SKyle Evans be_validate_name(libbe_handle_t *lbh, const char *name)
56028f16a0fSKyle Evans {
56128f16a0fSKyle Evans 	for (int i = 0; *name; i++) {
56228f16a0fSKyle Evans 		char c = *(name++);
563bfe0869cSKyle Evans 		if (isalnum(c) || (c == '-') || (c == '_') || (c == '.'))
56428f16a0fSKyle Evans 			continue;
56528f16a0fSKyle Evans 		return (BE_ERR_INVALIDNAME);
56628f16a0fSKyle Evans 	}
56728f16a0fSKyle Evans 
5685b7803a9SKyle Evans 	/*
5695b7803a9SKyle Evans 	 * Impose the additional restriction that the entire dataset name must
5705b7803a9SKyle Evans 	 * not exceed the maximum length of a dataset, i.e. MAXNAMELEN.
5715b7803a9SKyle Evans 	 */
5725b7803a9SKyle Evans 	if (strlen(lbh->root) + 1 + strlen(name) > MAXNAMELEN)
5735b7803a9SKyle Evans 		return (BE_ERR_PATHLEN);
57428f16a0fSKyle Evans 	return (BE_ERR_SUCCESS);
57528f16a0fSKyle Evans }
57628f16a0fSKyle Evans 
57728f16a0fSKyle Evans 
57828f16a0fSKyle Evans /*
57928f16a0fSKyle Evans  * usage
58028f16a0fSKyle Evans  */
58128f16a0fSKyle Evans int
58273c3d608SKyle Evans be_rename(libbe_handle_t *lbh, const char *old, const char *new)
58328f16a0fSKyle Evans {
58428f16a0fSKyle Evans 	char full_old[BE_MAXPATHLEN];
58528f16a0fSKyle Evans 	char full_new[BE_MAXPATHLEN];
58628f16a0fSKyle Evans 	zfs_handle_t *zfs_hdl;
58728f16a0fSKyle Evans 	int err;
58828f16a0fSKyle Evans 
5895b7803a9SKyle Evans 	/*
5905b7803a9SKyle Evans 	 * be_validate_name is documented not to set error state, so we should
5915b7803a9SKyle Evans 	 * do so here.
5925b7803a9SKyle Evans 	 */
5935b7803a9SKyle Evans 	if ((err = be_validate_name(lbh, new)) != 0)
5945b7803a9SKyle Evans 		return (set_error(lbh, err));
595b29bf2f8SKyle Evans 	if ((err = be_root_concat(lbh, old, full_old)) != 0)
59628f16a0fSKyle Evans 		return (set_error(lbh, err));
597b29bf2f8SKyle Evans 	if ((err = be_root_concat(lbh, new, full_new)) != 0)
59828f16a0fSKyle Evans 		return (set_error(lbh, err));
59928f16a0fSKyle Evans 
600bfe0869cSKyle Evans 	if (!zfs_dataset_exists(lbh->lzh, full_old, ZFS_TYPE_DATASET))
6012989df09SKyle Evans 		return (set_error(lbh, BE_ERR_NOENT));
60228f16a0fSKyle Evans 
603bfe0869cSKyle Evans 	if (zfs_dataset_exists(lbh->lzh, full_new, ZFS_TYPE_DATASET))
6042989df09SKyle Evans 		return (set_error(lbh, BE_ERR_EXISTS));
60528f16a0fSKyle Evans 
60628f16a0fSKyle Evans 	if ((zfs_hdl = zfs_open(lbh->lzh, full_old,
607bfe0869cSKyle Evans 	    ZFS_TYPE_FILESYSTEM)) == NULL)
6082989df09SKyle Evans 		return (set_error(lbh, BE_ERR_ZFSOPEN));
60928f16a0fSKyle Evans 
610bfe0869cSKyle Evans 	/* recurse, nounmount, forceunmount */
6118369ba42SKyle Evans 	struct renameflags flags = {
6128369ba42SKyle Evans 		.nounmount = 1,
6138369ba42SKyle Evans 	};
61428f16a0fSKyle Evans 
61528f16a0fSKyle Evans 	err = zfs_rename(zfs_hdl, NULL, full_new, flags);
61628f16a0fSKyle Evans 
61728f16a0fSKyle Evans 	zfs_close(zfs_hdl);
6185b7803a9SKyle Evans 	if (err != 0)
6195b7803a9SKyle Evans 		return (set_error(lbh, BE_ERR_UNKNOWN));
6205b7803a9SKyle Evans 	return (0);
62128f16a0fSKyle Evans }
62228f16a0fSKyle Evans 
62328f16a0fSKyle Evans 
62428f16a0fSKyle Evans int
62573c3d608SKyle Evans be_export(libbe_handle_t *lbh, const char *bootenv, int fd)
62628f16a0fSKyle Evans {
62728f16a0fSKyle Evans 	char snap_name[BE_MAXPATHLEN];
62828f16a0fSKyle Evans 	char buf[BE_MAXPATHLEN];
62928f16a0fSKyle Evans 	zfs_handle_t *zfs;
63028f16a0fSKyle Evans 	int err;
63128f16a0fSKyle Evans 
632b29bf2f8SKyle Evans 	if ((err = be_snapshot(lbh, bootenv, NULL, true, snap_name)) != 0)
6336d4b1d24SKyle Evans 		/* Use the error set by be_snapshot */
6346d4b1d24SKyle Evans 		return (err);
63528f16a0fSKyle Evans 
63628f16a0fSKyle Evans 	be_root_concat(lbh, snap_name, buf);
63728f16a0fSKyle Evans 
638bfe0869cSKyle Evans 	if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL)
639506f5fdfSKyle Evans 		return (set_error(lbh, BE_ERR_ZFSOPEN));
64028f16a0fSKyle Evans 
64128f16a0fSKyle Evans 	err = zfs_send_one(zfs, NULL, fd, 0);
6426d4b1d24SKyle Evans 	zfs_close(zfs);
6436d4b1d24SKyle Evans 
64428f16a0fSKyle Evans 	return (err);
64528f16a0fSKyle Evans }
64628f16a0fSKyle Evans 
64728f16a0fSKyle Evans 
64828f16a0fSKyle Evans int
64973c3d608SKyle Evans be_import(libbe_handle_t *lbh, const char *bootenv, int fd)
65028f16a0fSKyle Evans {
65128f16a0fSKyle Evans 	char buf[BE_MAXPATHLEN];
65228f16a0fSKyle Evans 	nvlist_t *props;
65328f16a0fSKyle Evans 	zfs_handle_t *zfs;
654*16ac0705SKyle Evans 	recvflags_t flags = { .nomount = 1 };
655*16ac0705SKyle Evans 	int err;
65628f16a0fSKyle Evans 
657*16ac0705SKyle Evans 	be_root_concat(lbh, bootenv, buf);
65828f16a0fSKyle Evans 
659*16ac0705SKyle Evans 	if ((err = zfs_receive(lbh->lzh, buf, NULL, &flags, fd, NULL)) != 0) {
660506f5fdfSKyle Evans 		switch (err) {
661506f5fdfSKyle Evans 		case EINVAL:
662506f5fdfSKyle Evans 			return (set_error(lbh, BE_ERR_NOORIGIN));
663506f5fdfSKyle Evans 		case ENOENT:
664506f5fdfSKyle Evans 			return (set_error(lbh, BE_ERR_NOENT));
665506f5fdfSKyle Evans 		case EIO:
666506f5fdfSKyle Evans 			return (set_error(lbh, BE_ERR_IO));
667506f5fdfSKyle Evans 		default:
668506f5fdfSKyle Evans 			return (set_error(lbh, BE_ERR_UNKNOWN));
669506f5fdfSKyle Evans 		}
67028f16a0fSKyle Evans 	}
67128f16a0fSKyle Evans 
672*16ac0705SKyle Evans 	if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_FILESYSTEM)) == NULL)
673506f5fdfSKyle Evans 		return (set_error(lbh, BE_ERR_ZFSOPEN));
67428f16a0fSKyle Evans 
67528f16a0fSKyle Evans 	nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
67628f16a0fSKyle Evans 	nvlist_add_string(props, "canmount", "noauto");
67728f16a0fSKyle Evans 	nvlist_add_string(props, "mountpoint", "/");
67828f16a0fSKyle Evans 
679*16ac0705SKyle Evans 	err = zfs_prop_set_list(zfs, props);
68028f16a0fSKyle Evans 	nvlist_free(props);
68128f16a0fSKyle Evans 
6821b057aacSKyle Evans 	zfs_close(zfs);
6831b057aacSKyle Evans 
6841b057aacSKyle Evans 	if (err != 0)
6851b057aacSKyle Evans 		return (set_error(lbh, BE_ERR_UNKNOWN));
6861b057aacSKyle Evans 
687*16ac0705SKyle Evans 	return (0);
68828f16a0fSKyle Evans }
68928f16a0fSKyle Evans 
6903d1a1f2cSKyle Evans #if SOON
691c65a2111SKyle Evans static int
692c65a2111SKyle Evans be_create_child_noent(libbe_handle_t *lbh, const char *active,
693c65a2111SKyle Evans     const char *child_path)
694c65a2111SKyle Evans {
695c65a2111SKyle Evans 	nvlist_t *props;
696c65a2111SKyle Evans 	zfs_handle_t *zfs;
697c65a2111SKyle Evans 	int err;
698c65a2111SKyle Evans 
699c65a2111SKyle Evans 	nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
700c65a2111SKyle Evans 	nvlist_add_string(props, "canmount", "noauto");
701c65a2111SKyle Evans 	nvlist_add_string(props, "mountpoint", child_path);
702c65a2111SKyle Evans 
703c65a2111SKyle Evans 	/* Create */
704c65a2111SKyle Evans 	if ((err = zfs_create(lbh->lzh, active, ZFS_TYPE_DATASET,
705c65a2111SKyle Evans 	    props)) != 0) {
706c65a2111SKyle Evans 		switch (err) {
707c65a2111SKyle Evans 		case EZFS_EXISTS:
708c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_EXISTS));
709c65a2111SKyle Evans 		case EZFS_NOENT:
710c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_NOENT));
711c65a2111SKyle Evans 		case EZFS_BADTYPE:
712c65a2111SKyle Evans 		case EZFS_BADVERSION:
713c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_NOPOOL));
714c65a2111SKyle Evans 		case EZFS_BADPROP:
715c65a2111SKyle Evans 		default:
716c65a2111SKyle Evans 			/* We set something up wrong, probably... */
717c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_UNKNOWN));
718c65a2111SKyle Evans 		}
719c65a2111SKyle Evans 	}
720c65a2111SKyle Evans 	nvlist_free(props);
721c65a2111SKyle Evans 
722c65a2111SKyle Evans 	if ((zfs = zfs_open(lbh->lzh, active, ZFS_TYPE_DATASET)) == NULL)
723c65a2111SKyle Evans 		return (set_error(lbh, BE_ERR_ZFSOPEN));
724c65a2111SKyle Evans 
725c65a2111SKyle Evans 	/* Set props */
726c65a2111SKyle Evans 	if ((err = zfs_prop_set(zfs, "canmount", "noauto")) != 0) {
727c65a2111SKyle Evans 		zfs_close(zfs);
728c65a2111SKyle Evans 		/*
729c65a2111SKyle Evans 		 * Similar to other cases, this shouldn't fail unless we've
730c65a2111SKyle Evans 		 * done something wrong.  This is a new dataset that shouldn't
731c65a2111SKyle Evans 		 * have been mounted anywhere between creation and now.
732c65a2111SKyle Evans 		 */
733c65a2111SKyle Evans 		if (err == EZFS_NOMEM)
734c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_NOMEM));
735c65a2111SKyle Evans 		return (set_error(lbh, BE_ERR_UNKNOWN));
736c65a2111SKyle Evans 	}
737c65a2111SKyle Evans 	zfs_close(zfs);
738c65a2111SKyle Evans 	return (BE_ERR_SUCCESS);
739c65a2111SKyle Evans }
740c65a2111SKyle Evans 
741c65a2111SKyle Evans static int
742c65a2111SKyle Evans be_create_child_cloned(libbe_handle_t *lbh, const char *active)
743c65a2111SKyle Evans {
7443d1a1f2cSKyle Evans 	char buf[BE_MAXPATHLEN], tmp[BE_MAXPATHLEN];;
745c65a2111SKyle Evans 	zfs_handle_t *zfs;
746c65a2111SKyle Evans 	int err;
747c65a2111SKyle Evans 
748c65a2111SKyle Evans 	/* XXX TODO ? */
749c65a2111SKyle Evans 
750c65a2111SKyle Evans 	/*
751c65a2111SKyle Evans 	 * Establish if the existing path is a zfs dataset or just
752c65a2111SKyle Evans 	 * the subdirectory of one
753c65a2111SKyle Evans 	 */
7543d1a1f2cSKyle Evans 	strlcpy(tmp, "tmp/be_snap.XXXXX", sizeof(tmp));
7553d1a1f2cSKyle Evans 	if (mktemp(tmp) == NULL)
756c65a2111SKyle Evans 		return (set_error(lbh, BE_ERR_UNKNOWN));
757c65a2111SKyle Evans 
7583d1a1f2cSKyle Evans 	be_root_concat(lbh, tmp, buf);
7593d1a1f2cSKyle Evans 	printf("Here %s?\n", buf);
760c65a2111SKyle Evans 	if ((err = zfs_snapshot(lbh->lzh, buf, false, NULL)) != 0) {
761c65a2111SKyle Evans 		switch (err) {
762c65a2111SKyle Evans 		case EZFS_INVALIDNAME:
763c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_INVALIDNAME));
764c65a2111SKyle Evans 
765c65a2111SKyle Evans 		default:
766c65a2111SKyle Evans 			/*
767c65a2111SKyle Evans 			 * The other errors that zfs_ioc_snapshot might return
768c65a2111SKyle Evans 			 * shouldn't happen if we've set things up properly, so
769c65a2111SKyle Evans 			 * we'll gloss over them and call it UNKNOWN as it will
770c65a2111SKyle Evans 			 * require further triage.
771c65a2111SKyle Evans 			 */
772c65a2111SKyle Evans 			if (errno == ENOTSUP)
773c65a2111SKyle Evans 				return (set_error(lbh, BE_ERR_NOPOOL));
774c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_UNKNOWN));
775c65a2111SKyle Evans 		}
776c65a2111SKyle Evans 	}
777c65a2111SKyle Evans 
778c65a2111SKyle Evans 	/* Clone */
779c65a2111SKyle Evans 	if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT)) == NULL)
780c65a2111SKyle Evans 		return (BE_ERR_ZFSOPEN);
781c65a2111SKyle Evans 
782c65a2111SKyle Evans 	if ((err = zfs_clone(zfs, active, NULL)) != 0)
783c65a2111SKyle Evans 		/* XXX TODO correct error */
784c65a2111SKyle Evans 		return (set_error(lbh, BE_ERR_UNKNOWN));
785c65a2111SKyle Evans 
786c65a2111SKyle Evans 	/* set props */
787c65a2111SKyle Evans 	zfs_close(zfs);
788c65a2111SKyle Evans 	return (BE_ERR_SUCCESS);
789c65a2111SKyle Evans }
79028f16a0fSKyle Evans 
79128f16a0fSKyle Evans int
79273c3d608SKyle Evans be_add_child(libbe_handle_t *lbh, const char *child_path, bool cp_if_exists)
79328f16a0fSKyle Evans {
79473c3d608SKyle Evans 	struct stat sb;
795c65a2111SKyle Evans 	char active[BE_MAXPATHLEN], buf[BE_MAXPATHLEN];
79628f16a0fSKyle Evans 	nvlist_t *props;
79773c3d608SKyle Evans 	const char *s;
79828f16a0fSKyle Evans 
79928f16a0fSKyle Evans 	/* Require absolute paths */
800bfe0869cSKyle Evans 	if (*child_path != '/')
8016d4b1d24SKyle Evans 		return (set_error(lbh, BE_ERR_BADPATH));
80228f16a0fSKyle Evans 
8036d4b1d24SKyle Evans 	strlcpy(active, be_active_path(lbh), BE_MAXPATHLEN);
80428f16a0fSKyle Evans 	strcpy(buf, active);
80528f16a0fSKyle Evans 
80628f16a0fSKyle Evans 	/* Create non-mountable parent dataset(s) */
80773c3d608SKyle Evans 	s = child_path;
80828f16a0fSKyle Evans 	for (char *p; (p = strchr(s+1, '/')) != NULL; s = p) {
80928f16a0fSKyle Evans 		size_t len = p - s;
81028f16a0fSKyle Evans 		strncat(buf, s, len);
81128f16a0fSKyle Evans 
81228f16a0fSKyle Evans 		nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
81328f16a0fSKyle Evans 		nvlist_add_string(props, "canmount", "off");
81428f16a0fSKyle Evans 		nvlist_add_string(props, "mountpoint", "none");
81528f16a0fSKyle Evans 		zfs_create(lbh->lzh, buf, ZFS_TYPE_DATASET, props);
81628f16a0fSKyle Evans 		nvlist_free(props);
81728f16a0fSKyle Evans 	}
81828f16a0fSKyle Evans 
81928f16a0fSKyle Evans 	/* Path does not exist as a descendent of / yet */
8206d4b1d24SKyle Evans 	if (strlcat(active, child_path, BE_MAXPATHLEN) >= BE_MAXPATHLEN)
8216d4b1d24SKyle Evans 		return (set_error(lbh, BE_ERR_PATHLEN));
82228f16a0fSKyle Evans 
82328f16a0fSKyle Evans 	if (stat(child_path, &sb) != 0) {
82428f16a0fSKyle Evans 		/* Verify that error is ENOENT */
8256d4b1d24SKyle Evans 		if (errno != ENOENT)
8266d4b1d24SKyle Evans 			return (set_error(lbh, BE_ERR_UNKNOWN));
827c65a2111SKyle Evans 		return (be_create_child_noent(lbh, active, child_path));
828c65a2111SKyle Evans 	} else if (cp_if_exists)
82928f16a0fSKyle Evans 		/* Path is already a descendent of / and should be copied */
830c65a2111SKyle Evans 		return (be_create_child_cloned(lbh, active));
8316d4b1d24SKyle Evans 	return (set_error(lbh, BE_ERR_EXISTS));
83228f16a0fSKyle Evans }
8333d1a1f2cSKyle Evans #endif	/* SOON */
83428f16a0fSKyle Evans 
835d06f7103SKyle Evans static int
836d06f7103SKyle Evans be_set_nextboot(libbe_handle_t *lbh, nvlist_t *config, uint64_t pool_guid,
837d06f7103SKyle Evans     const char *zfsdev)
838d06f7103SKyle Evans {
839d06f7103SKyle Evans 	nvlist_t **child;
840d06f7103SKyle Evans 	uint64_t vdev_guid;
841d06f7103SKyle Evans 	int c, children;
842d06f7103SKyle Evans 
843d06f7103SKyle Evans 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, &child,
844d06f7103SKyle Evans 	    &children) == 0) {
845d06f7103SKyle Evans 		for (c = 0; c < children; ++c)
846d06f7103SKyle Evans 			if (be_set_nextboot(lbh, child[c], pool_guid, zfsdev) != 0)
847d06f7103SKyle Evans 				return (1);
848d06f7103SKyle Evans 		return (0);
849d06f7103SKyle Evans 	}
850d06f7103SKyle Evans 
851d06f7103SKyle Evans 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
852d06f7103SKyle Evans 	    &vdev_guid) != 0) {
853d06f7103SKyle Evans 		return (1);
854d06f7103SKyle Evans 	}
855d06f7103SKyle Evans 
856d06f7103SKyle Evans 	if (zpool_nextboot(lbh->lzh, pool_guid, vdev_guid, zfsdev) != 0) {
857d06f7103SKyle Evans 		perror("ZFS_IOC_NEXTBOOT failed");
858d06f7103SKyle Evans 		return (1);
859d06f7103SKyle Evans 	}
860d06f7103SKyle Evans 
861d06f7103SKyle Evans 	return (0);
862d06f7103SKyle Evans }
863d06f7103SKyle Evans 
8648d4ce358SKyle Evans /*
8658d4ce358SKyle Evans  * Deactivate old BE dataset; currently just sets canmount=noauto
8668d4ce358SKyle Evans  */
8678d4ce358SKyle Evans static int
8688d4ce358SKyle Evans be_deactivate(libbe_handle_t *lbh, const char *ds)
8698d4ce358SKyle Evans {
8708d4ce358SKyle Evans 	zfs_handle_t *zfs;
8718d4ce358SKyle Evans 
8728d4ce358SKyle Evans 	if ((zfs = zfs_open(lbh->lzh, ds, ZFS_TYPE_DATASET)) == NULL)
8738d4ce358SKyle Evans 		return (1);
8748d4ce358SKyle Evans 	if (zfs_prop_set(zfs, "canmount", "noauto") != 0)
8758d4ce358SKyle Evans 		return (1);
8768d4ce358SKyle Evans 	zfs_close(zfs);
8778d4ce358SKyle Evans 	return (0);
8788d4ce358SKyle Evans }
87928f16a0fSKyle Evans 
88028f16a0fSKyle Evans int
88173c3d608SKyle Evans be_activate(libbe_handle_t *lbh, const char *bootenv, bool temporary)
88228f16a0fSKyle Evans {
88328f16a0fSKyle Evans 	char be_path[BE_MAXPATHLEN];
88428f16a0fSKyle Evans 	char buf[BE_MAXPATHLEN];
8854635676dSKyle Evans 	nvlist_t *config, *dsprops, *vdevs;
8864635676dSKyle Evans 	char *origin;
8870cadc427SKyle Evans 	uint64_t pool_guid;
8880cadc427SKyle Evans 	zfs_handle_t *zhp;
88928f16a0fSKyle Evans 	int err;
89028f16a0fSKyle Evans 
89128f16a0fSKyle Evans 	be_root_concat(lbh, bootenv, be_path);
89228f16a0fSKyle Evans 
89328f16a0fSKyle Evans 	/* Note: be_exists fails if mountpoint is not / */
894162ec569SKyle Evans 	if ((err = be_exists(lbh, be_path)) != 0)
895162ec569SKyle Evans 		return (set_error(lbh, err));
89628f16a0fSKyle Evans 
89728f16a0fSKyle Evans 	if (temporary) {
898d06f7103SKyle Evans 		config = zpool_get_config(lbh->active_phandle, NULL);
899c65a2111SKyle Evans 		if (config == NULL)
900c65a2111SKyle Evans 			/* config should be fetchable... */
901c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_UNKNOWN));
90228f16a0fSKyle Evans 
903d06f7103SKyle Evans 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
904d06f7103SKyle Evans 		    &pool_guid) != 0)
905c65a2111SKyle Evans 			/* Similarly, it shouldn't be possible */
906c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_UNKNOWN));
907d06f7103SKyle Evans 
90828f16a0fSKyle Evans 		/* Expected format according to zfsbootcfg(8) man */
909a8e44f4dSKyle Evans 		snprintf(buf, sizeof(buf), "zfs:%s:", be_path);
91028f16a0fSKyle Evans 
911c65a2111SKyle Evans 		/* We have no config tree */
912c65a2111SKyle Evans 		if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
913c65a2111SKyle Evans 		    &vdevs) != 0)
914c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_NOPOOL));
91528f16a0fSKyle Evans 
916d06f7103SKyle Evans 		return (be_set_nextboot(lbh, vdevs, pool_guid, buf));
91728f16a0fSKyle Evans 	} else {
9188d4ce358SKyle Evans 		if (be_deactivate(lbh, lbh->bootfs) != 0)
9198d4ce358SKyle Evans 			return (-1);
9208d4ce358SKyle Evans 
92128f16a0fSKyle Evans 		/* Obtain bootenv zpool */
922c3a34c08SKyle Evans 		err = zpool_set_prop(lbh->active_phandle, "bootfs", be_path);
9230cadc427SKyle Evans 		if (err)
9240cadc427SKyle Evans 			return (-1);
92528f16a0fSKyle Evans 
9260cadc427SKyle Evans 		zhp = zfs_open(lbh->lzh, be_path, ZFS_TYPE_FILESYSTEM);
9270cadc427SKyle Evans 		if (zhp == NULL)
9280cadc427SKyle Evans 			return (-1);
92928f16a0fSKyle Evans 
9304635676dSKyle Evans 		if (be_prop_list_alloc(&dsprops) != 0)
9314635676dSKyle Evans 			return (-1);
9324635676dSKyle Evans 
9334635676dSKyle Evans 		if (be_get_dataset_props(lbh, be_path, dsprops) != 0) {
9344635676dSKyle Evans 			nvlist_free(dsprops);
9354635676dSKyle Evans 			return (-1);
9364635676dSKyle Evans 		}
9374635676dSKyle Evans 
9384635676dSKyle Evans 		if (nvlist_lookup_string(dsprops, "origin", &origin) == 0)
9390cadc427SKyle Evans 			err = zfs_promote(zhp);
9404635676dSKyle Evans 		nvlist_free(dsprops);
9414635676dSKyle Evans 
9420cadc427SKyle Evans 		zfs_close(zhp);
9430cadc427SKyle Evans 
9440cadc427SKyle Evans 		if (err)
94528f16a0fSKyle Evans 			return (-1);
94628f16a0fSKyle Evans 	}
9470cadc427SKyle Evans 
9480cadc427SKyle Evans 	return (BE_ERR_SUCCESS);
94928f16a0fSKyle Evans }
950