xref: /freebsd/lib/libbe/be.c (revision cc4deabc28a0f094e9523c3e88ca2631a80e39e4)
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 
3228f16a0fSKyle Evans #include <sys/stat.h>
3328f16a0fSKyle Evans #include <sys/types.h>
3428f16a0fSKyle Evans 
3528f16a0fSKyle Evans #include <ctype.h>
3628f16a0fSKyle Evans #include <kenv.h>
3728f16a0fSKyle Evans #include <libgen.h>
3828f16a0fSKyle Evans #include <libzfs_core.h>
3928f16a0fSKyle Evans #include <stdio.h>
4028f16a0fSKyle Evans #include <stdlib.h>
4128f16a0fSKyle Evans #include <time.h>
4228f16a0fSKyle Evans #include <unistd.h>
4328f16a0fSKyle Evans 
4428f16a0fSKyle Evans #include "be.h"
4528f16a0fSKyle Evans #include "be_impl.h"
4628f16a0fSKyle Evans 
473d1a1f2cSKyle Evans #if SOON
48c65a2111SKyle Evans static int be_create_child_noent(libbe_handle_t *lbh, const char *active,
49c65a2111SKyle Evans     const char *child_path);
50c65a2111SKyle Evans static int be_create_child_cloned(libbe_handle_t *lbh, const char *active);
513d1a1f2cSKyle Evans #endif
52c65a2111SKyle Evans 
5328f16a0fSKyle Evans /*
54ee16b7c9SKyle Evans  * Iterator function for locating the rootfs amongst the children of the
55ee16b7c9SKyle Evans  * zfs_be_root set by loader(8).  data is expected to be a libbe_handle_t *.
56ee16b7c9SKyle Evans  */
57ee16b7c9SKyle Evans static int
58ee16b7c9SKyle Evans be_locate_rootfs(zfs_handle_t *chkds, void *data)
59ee16b7c9SKyle Evans {
60ee16b7c9SKyle Evans 	libbe_handle_t *lbh;
61ee16b7c9SKyle Evans 	char *mntpoint;
62ee16b7c9SKyle Evans 
63ee16b7c9SKyle Evans 	lbh = (libbe_handle_t *)data;
64ee16b7c9SKyle Evans 	if (lbh == NULL)
65ee16b7c9SKyle Evans 		return (1);
66ee16b7c9SKyle Evans 
67*cc4deabcSKyle Evans 	mntpoint = NULL;
68ee16b7c9SKyle Evans 	if (zfs_is_mounted(chkds, &mntpoint) && strcmp(mntpoint, "/") == 0) {
69*cc4deabcSKyle Evans 		strlcpy(lbh->rootfs, zfs_get_name(chkds), BE_MAXPATHLEN + 1);
70*cc4deabcSKyle Evans 		free(mntpoint);
71ee16b7c9SKyle Evans 		return (1);
72*cc4deabcSKyle Evans 	} else if(mntpoint != NULL)
73*cc4deabcSKyle Evans 		free(mntpoint);
74ee16b7c9SKyle Evans 
75ee16b7c9SKyle Evans 	return (0);
76ee16b7c9SKyle Evans }
77ee16b7c9SKyle Evans 
78ee16b7c9SKyle Evans /*
7928f16a0fSKyle Evans  * Initializes the libbe context to operate in the root boot environment
8028f16a0fSKyle Evans  * dataset, for example, zroot/ROOT.
8128f16a0fSKyle Evans  */
8228f16a0fSKyle Evans libbe_handle_t *
8328f16a0fSKyle Evans libbe_init(void)
8428f16a0fSKyle Evans {
8528f16a0fSKyle Evans 	struct stat sb;
8628f16a0fSKyle Evans 	dev_t root_dev, boot_dev;
8728f16a0fSKyle Evans 	libbe_handle_t *lbh;
88ee16b7c9SKyle Evans 	zfs_handle_t *rootds;
89c3a34c08SKyle Evans 	char *poolname, *pos;
90c3a34c08SKyle Evans 	int pnamelen;
9128f16a0fSKyle Evans 
92c3a34c08SKyle Evans 	lbh = NULL;
93c3a34c08SKyle Evans 	poolname = pos = NULL;
94c3a34c08SKyle Evans 	pnamelen = 0;
95ee16b7c9SKyle Evans 	rootds = NULL;
96ee16b7c9SKyle Evans 
9728f16a0fSKyle Evans 	/* Verify that /boot and / are mounted on the same filesystem */
98c3a34c08SKyle Evans 	/* TODO: use errno here?? */
99c3a34c08SKyle Evans 	if (stat("/", &sb) != 0)
100c3a34c08SKyle Evans 		goto err;
10128f16a0fSKyle Evans 
10228f16a0fSKyle Evans 	root_dev = sb.st_dev;
10328f16a0fSKyle Evans 
104c3a34c08SKyle Evans 	if (stat("/boot", &sb) != 0)
105c3a34c08SKyle Evans 		goto err;
10628f16a0fSKyle Evans 
10728f16a0fSKyle Evans 	boot_dev = sb.st_dev;
10828f16a0fSKyle Evans 
10928f16a0fSKyle Evans 	if (root_dev != boot_dev) {
11028f16a0fSKyle Evans 		fprintf(stderr, "/ and /boot not on same device, quitting\n");
111c3a34c08SKyle Evans 		goto err;
11228f16a0fSKyle Evans 	}
11328f16a0fSKyle Evans 
114c3a34c08SKyle Evans 	if ((lbh = calloc(1, sizeof(libbe_handle_t))) == NULL)
115c3a34c08SKyle Evans 		goto err;
11628f16a0fSKyle Evans 
117c3a34c08SKyle Evans 	if ((lbh->lzh = libzfs_init()) == NULL)
118c3a34c08SKyle Evans 		goto err;
11928f16a0fSKyle Evans 
12028f16a0fSKyle Evans 	/* Obtain path to boot environment root */
121c3a34c08SKyle Evans 	if ((kenv(KENV_GET, "zfs_be_root", lbh->root, BE_MAXPATHLEN)) == -1)
122c3a34c08SKyle Evans 		goto err;
12328f16a0fSKyle Evans 
12428f16a0fSKyle Evans 	/* Remove leading 'zfs:' if present, otherwise use value as-is */
125c3a34c08SKyle Evans 	if (strcmp(lbh->root, "zfs:") == 0)
126*cc4deabcSKyle Evans 		strlcpy(lbh->root, strchr(lbh->root, ':') + sizeof(char),
127*cc4deabcSKyle Evans 		    BE_MAXPATHLEN + 1);
128c3a34c08SKyle Evans 
129c3a34c08SKyle Evans 	if ((pos = strchr(lbh->root, '/')) == NULL)
130c3a34c08SKyle Evans 		goto err;
131c3a34c08SKyle Evans 
132c3a34c08SKyle Evans 	pnamelen = pos - lbh->root;
133c3a34c08SKyle Evans 	poolname = malloc(pnamelen + 1);
134c3a34c08SKyle Evans 	if (poolname == NULL)
135c3a34c08SKyle Evans 		goto err;
136c3a34c08SKyle Evans 
137c3a34c08SKyle Evans 	strncpy(poolname, lbh->root, pnamelen);
138c3a34c08SKyle Evans 	poolname[pnamelen] = '\0';
139c3a34c08SKyle Evans 	if ((lbh->active_phandle = zpool_open(lbh->lzh, poolname)) == NULL)
140c3a34c08SKyle Evans 		goto err;
141c3a34c08SKyle Evans 
142c3a34c08SKyle Evans 	if (zpool_get_prop(lbh->active_phandle, ZPOOL_PROP_BOOTFS, lbh->bootfs,
143c3a34c08SKyle Evans 	    BE_MAXPATHLEN, NULL, true) != 0)
144c3a34c08SKyle Evans 		goto err;
145c3a34c08SKyle Evans 
146c3a34c08SKyle Evans 	/* Obtain path to boot environment rootfs (currently booted) */
147c3a34c08SKyle Evans 	/* XXX Get dataset mounted at / by kenv/GUID from mountroot? */
148ee16b7c9SKyle Evans 	if ((rootds = zfs_open(lbh->lzh, lbh->root, ZFS_TYPE_DATASET)) == NULL)
149c3a34c08SKyle Evans 		goto err;
150c3a34c08SKyle Evans 
151ee16b7c9SKyle Evans 	zfs_iter_filesystems(rootds, be_locate_rootfs, lbh);
152ee16b7c9SKyle Evans 	zfs_close(rootds);
153ff8676ccSKyle Evans 	rootds = NULL;
154b29bf2f8SKyle Evans 	if (*lbh->rootfs == '\0')
155ff8676ccSKyle Evans 		goto err;
15628f16a0fSKyle Evans 
15728f16a0fSKyle Evans 	return (lbh);
158c3a34c08SKyle Evans err:
159c3a34c08SKyle Evans 	if (lbh != NULL) {
160c3a34c08SKyle Evans 		if (lbh->active_phandle != NULL)
161c3a34c08SKyle Evans 			zpool_close(lbh->active_phandle);
162c3a34c08SKyle Evans 		if (lbh->lzh != NULL)
163c3a34c08SKyle Evans 			libzfs_fini(lbh->lzh);
164c3a34c08SKyle Evans 		free(lbh);
165c3a34c08SKyle Evans 	}
166c3a34c08SKyle Evans 	free(poolname);
167c3a34c08SKyle Evans 	return (NULL);
16828f16a0fSKyle Evans }
16928f16a0fSKyle Evans 
17028f16a0fSKyle Evans 
17128f16a0fSKyle Evans /*
17228f16a0fSKyle Evans  * Free memory allocated by libbe_init()
17328f16a0fSKyle Evans  */
17428f16a0fSKyle Evans void
17528f16a0fSKyle Evans libbe_close(libbe_handle_t *lbh)
17628f16a0fSKyle Evans {
177bfe0869cSKyle Evans 
178c3a34c08SKyle Evans 	if (lbh->active_phandle != NULL)
179c3a34c08SKyle Evans 		zpool_close(lbh->active_phandle);
18028f16a0fSKyle Evans 	libzfs_fini(lbh->lzh);
18128f16a0fSKyle Evans 	free(lbh);
18228f16a0fSKyle Evans }
18328f16a0fSKyle Evans 
1849b1662e6SKyle Evans /*
1859b1662e6SKyle Evans  * Proxy through to libzfs for the moment.
1869b1662e6SKyle Evans  */
1879b1662e6SKyle Evans void
1889b1662e6SKyle Evans be_nicenum(uint64_t num, char *buf, size_t buflen)
1899b1662e6SKyle Evans {
1909b1662e6SKyle Evans 
1919b1662e6SKyle Evans 	zfs_nicenum(num, buf, buflen);
1929b1662e6SKyle Evans }
19328f16a0fSKyle Evans 
194920abf4dSKyle Evans static int
195920abf4dSKyle Evans be_destroy_cb(zfs_handle_t *zfs_hdl, void *data)
196920abf4dSKyle Evans {
197920abf4dSKyle Evans 	int err;
198920abf4dSKyle Evans 
199920abf4dSKyle Evans 	if ((err = zfs_iter_children(zfs_hdl, be_destroy_cb, data)) != 0)
200920abf4dSKyle Evans 		return (err);
201920abf4dSKyle Evans 	if ((err = zfs_destroy(zfs_hdl, false)) != 0)
202920abf4dSKyle Evans 		return (err);
203920abf4dSKyle Evans 	return (0);
204920abf4dSKyle Evans }
205920abf4dSKyle Evans 
20628f16a0fSKyle Evans /*
20728f16a0fSKyle Evans  * Destroy the boot environment or snapshot specified by the name
20828f16a0fSKyle Evans  * parameter. Options are or'd together with the possible values:
20928f16a0fSKyle Evans  * BE_DESTROY_FORCE : forces operation on mounted datasets
21028f16a0fSKyle Evans  */
21128f16a0fSKyle Evans int
21273c3d608SKyle Evans be_destroy(libbe_handle_t *lbh, const char *name, int options)
21328f16a0fSKyle Evans {
21428f16a0fSKyle Evans 	zfs_handle_t *fs;
21528f16a0fSKyle Evans 	char path[BE_MAXPATHLEN];
216bfe0869cSKyle Evans 	char *p;
217bfe0869cSKyle Evans 	int err, force, mounted;
21828f16a0fSKyle Evans 
219bfe0869cSKyle Evans 	p = path;
220bfe0869cSKyle Evans 	force = options & BE_DESTROY_FORCE;
221bfe0869cSKyle Evans 	err = BE_ERR_SUCCESS;
22228f16a0fSKyle Evans 
22328f16a0fSKyle Evans 	be_root_concat(lbh, name, path);
22428f16a0fSKyle Evans 
22528f16a0fSKyle Evans 	if (strchr(name, '@') == NULL) {
226bfe0869cSKyle Evans 		if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_FILESYSTEM))
22728f16a0fSKyle Evans 			return (set_error(lbh, BE_ERR_NOENT));
22828f16a0fSKyle Evans 
229bfe0869cSKyle Evans 		if (strcmp(path, lbh->rootfs) == 0)
23028f16a0fSKyle Evans 			return (set_error(lbh, BE_ERR_DESTROYACT));
23128f16a0fSKyle Evans 
23228f16a0fSKyle Evans 		fs = zfs_open(lbh->lzh, p, ZFS_TYPE_FILESYSTEM);
23328f16a0fSKyle Evans 	} else {
23428f16a0fSKyle Evans 
235bfe0869cSKyle Evans 		if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_SNAPSHOT))
23628f16a0fSKyle Evans 			return (set_error(lbh, BE_ERR_NOENT));
23728f16a0fSKyle Evans 
23828f16a0fSKyle Evans 		fs = zfs_open(lbh->lzh, p, ZFS_TYPE_SNAPSHOT);
23928f16a0fSKyle Evans 	}
24028f16a0fSKyle Evans 
24128f16a0fSKyle Evans 	if (fs == NULL)
24228f16a0fSKyle Evans 		return (set_error(lbh, BE_ERR_ZFSOPEN));
24328f16a0fSKyle Evans 
24428f16a0fSKyle Evans 	/* Check if mounted, unmount if force is specified */
245b29bf2f8SKyle Evans 	if ((mounted = zfs_is_mounted(fs, NULL)) != 0) {
246bfe0869cSKyle Evans 		if (force)
24728f16a0fSKyle Evans 			zfs_unmount(fs, NULL, 0);
248bfe0869cSKyle Evans 		else
24928f16a0fSKyle Evans 			return (set_error(lbh, BE_ERR_DESTROYMNT));
25028f16a0fSKyle Evans 	}
25128f16a0fSKyle Evans 
252920abf4dSKyle Evans 	if ((err = be_destroy_cb(fs, NULL)) != 0) {
253920abf4dSKyle Evans 		/* Children are still present or the mount is referenced */
254920abf4dSKyle Evans 		if (err == EBUSY)
255920abf4dSKyle Evans 			return (set_error(lbh, BE_ERR_DESTROYMNT));
256920abf4dSKyle Evans 		return (set_error(lbh, BE_ERR_UNKNOWN));
257920abf4dSKyle Evans 	}
25828f16a0fSKyle Evans 
259920abf4dSKyle Evans 	return (0);
26028f16a0fSKyle Evans }
26128f16a0fSKyle Evans 
26228f16a0fSKyle Evans 
26328f16a0fSKyle Evans int
264b29bf2f8SKyle Evans be_snapshot(libbe_handle_t *lbh, const char *source, const char *snap_name,
26528f16a0fSKyle Evans     bool recursive, char *result)
26628f16a0fSKyle Evans {
26728f16a0fSKyle Evans 	char buf[BE_MAXPATHLEN];
26828f16a0fSKyle Evans 	time_t rawtime;
26928f16a0fSKyle Evans 	int len, err;
27028f16a0fSKyle Evans 
27128f16a0fSKyle Evans 	be_root_concat(lbh, source, buf);
27228f16a0fSKyle Evans 
273bfe0869cSKyle Evans 	if (!be_exists(lbh, buf))
27428f16a0fSKyle Evans 		return (BE_ERR_NOENT);
27528f16a0fSKyle Evans 
27628f16a0fSKyle Evans 	if (snap_name != NULL) {
27728f16a0fSKyle Evans 		strcat(buf, "@");
27828f16a0fSKyle Evans 		strcat(buf, snap_name);
279bfe0869cSKyle Evans 		if (result != NULL)
28028f16a0fSKyle Evans 			snprintf(result, BE_MAXPATHLEN, "%s@%s", source,
28128f16a0fSKyle Evans 			    snap_name);
28228f16a0fSKyle Evans 	} else {
28328f16a0fSKyle Evans 		time(&rawtime);
28428f16a0fSKyle Evans 		len = strlen(buf);
28528f16a0fSKyle Evans 		strftime(buf + len, BE_MAXPATHLEN - len,
28628f16a0fSKyle Evans 		    "@%F-%T", localtime(&rawtime));
287bfe0869cSKyle Evans 		if (result != NULL)
28828f16a0fSKyle Evans 			strcpy(result, strrchr(buf, '/') + 1);
28928f16a0fSKyle Evans 	}
29028f16a0fSKyle Evans 
291b29bf2f8SKyle Evans 	if ((err = zfs_snapshot(lbh->lzh, buf, recursive, NULL)) != 0) {
29228f16a0fSKyle Evans 		switch (err) {
29328f16a0fSKyle Evans 		case EZFS_INVALIDNAME:
29428f16a0fSKyle Evans 			return (set_error(lbh, BE_ERR_INVALIDNAME));
29528f16a0fSKyle Evans 
29628f16a0fSKyle Evans 		default:
2972989df09SKyle Evans 			/*
2982989df09SKyle Evans 			 * The other errors that zfs_ioc_snapshot might return
2992989df09SKyle Evans 			 * shouldn't happen if we've set things up properly, so
3002989df09SKyle Evans 			 * we'll gloss over them and call it UNKNOWN as it will
3012989df09SKyle Evans 			 * require further triage.
3022989df09SKyle Evans 			 */
3032989df09SKyle Evans 			if (errno == ENOTSUP)
3042989df09SKyle Evans 				return (set_error(lbh, BE_ERR_NOPOOL));
30528f16a0fSKyle Evans 			return (set_error(lbh, BE_ERR_UNKNOWN));
30628f16a0fSKyle Evans 		}
30728f16a0fSKyle Evans 	}
30828f16a0fSKyle Evans 
30928f16a0fSKyle Evans 	return (BE_ERR_SUCCESS);
31028f16a0fSKyle Evans }
31128f16a0fSKyle Evans 
31228f16a0fSKyle Evans 
31328f16a0fSKyle Evans /*
31428f16a0fSKyle Evans  * Create the boot environment specified by the name parameter
31528f16a0fSKyle Evans  */
31628f16a0fSKyle Evans int
31773c3d608SKyle Evans be_create(libbe_handle_t *lbh, const char *name)
31828f16a0fSKyle Evans {
31928f16a0fSKyle Evans 	int err;
32028f16a0fSKyle Evans 
321b29bf2f8SKyle Evans 	err = be_create_from_existing(lbh, name, be_active_path(lbh));
32228f16a0fSKyle Evans 
32328f16a0fSKyle Evans 	return (set_error(lbh, err));
32428f16a0fSKyle Evans }
32528f16a0fSKyle Evans 
32628f16a0fSKyle Evans 
32728f16a0fSKyle Evans static int
32828f16a0fSKyle Evans be_deep_clone_prop(int prop, void *cb)
32928f16a0fSKyle Evans {
33028f16a0fSKyle Evans 	int err;
331bfe0869cSKyle Evans         struct libbe_dccb *dccb;
33228f16a0fSKyle Evans 	zprop_source_t src;
33328f16a0fSKyle Evans 	char pval[BE_MAXPATHLEN];
33428f16a0fSKyle Evans 	char source[BE_MAXPATHLEN];
33528f16a0fSKyle Evans 
336bfe0869cSKyle Evans 	dccb = cb;
33728f16a0fSKyle Evans 	/* Skip some properties we don't want to touch */
33873c3d608SKyle Evans 	if (prop == ZFS_PROP_CANMOUNT)
33928f16a0fSKyle Evans 		return (ZPROP_CONT);
34028f16a0fSKyle Evans 
34128f16a0fSKyle Evans 	/* Don't copy readonly properties */
342bfe0869cSKyle Evans 	if (zfs_prop_readonly(prop))
34328f16a0fSKyle Evans 		return (ZPROP_CONT);
34428f16a0fSKyle Evans 
34528f16a0fSKyle Evans 	if ((err = zfs_prop_get(dccb->zhp, prop, (char *)&pval,
346bfe0869cSKyle Evans 	    sizeof(pval), &src, (char *)&source, sizeof(source), false)))
34728f16a0fSKyle Evans 		/* Just continue if we fail to read a property */
34828f16a0fSKyle Evans 		return (ZPROP_CONT);
349bfe0869cSKyle Evans 
35028f16a0fSKyle Evans 	/* Only copy locally defined properties */
351bfe0869cSKyle Evans 	if (src != ZPROP_SRC_LOCAL)
35228f16a0fSKyle Evans 		return (ZPROP_CONT);
35328f16a0fSKyle Evans 
35428f16a0fSKyle Evans 	nvlist_add_string(dccb->props, zfs_prop_to_name(prop), (char *)pval);
35528f16a0fSKyle Evans 
35628f16a0fSKyle Evans 	return (ZPROP_CONT);
35728f16a0fSKyle Evans }
35828f16a0fSKyle Evans 
35928f16a0fSKyle Evans static int
36028f16a0fSKyle Evans be_deep_clone(zfs_handle_t *ds, void *data)
36128f16a0fSKyle Evans {
36228f16a0fSKyle Evans 	int err;
36328f16a0fSKyle Evans 	char be_path[BE_MAXPATHLEN];
36428f16a0fSKyle Evans 	char snap_path[BE_MAXPATHLEN];
36528f16a0fSKyle Evans 	const char *dspath;
36628f16a0fSKyle Evans 	char *dsname;
36728f16a0fSKyle Evans 	zfs_handle_t *snap_hdl;
36828f16a0fSKyle Evans 	nvlist_t *props;
369bfe0869cSKyle Evans 	struct libbe_deep_clone *isdc, sdc;
37028f16a0fSKyle Evans 	struct libbe_dccb dccb;
37128f16a0fSKyle Evans 
372bfe0869cSKyle Evans 	isdc = (struct libbe_deep_clone *)data;
37328f16a0fSKyle Evans 	dspath = zfs_get_name(ds);
374bfe0869cSKyle Evans 	if ((dsname = strrchr(dspath, '/')) == NULL)
37528f16a0fSKyle Evans 		return (BE_ERR_UNKNOWN);
37628f16a0fSKyle Evans 	dsname++;
377bfe0869cSKyle Evans 
378bfe0869cSKyle Evans 	if (isdc->bename == NULL)
37928f16a0fSKyle Evans 		snprintf(be_path, sizeof(be_path), "%s/%s", isdc->be_root, dsname);
380bfe0869cSKyle Evans 	else
38128f16a0fSKyle Evans 		snprintf(be_path, sizeof(be_path), "%s/%s", isdc->be_root, isdc->bename);
382bfe0869cSKyle Evans 
38328f16a0fSKyle Evans 	snprintf(snap_path, sizeof(snap_path), "%s@%s", dspath, isdc->snapname);
38428f16a0fSKyle Evans 
385bfe0869cSKyle Evans 	if (zfs_dataset_exists(isdc->lbh->lzh, be_path, ZFS_TYPE_DATASET))
38628f16a0fSKyle Evans 		return (set_error(isdc->lbh, BE_ERR_EXISTS));
38728f16a0fSKyle Evans 
38828f16a0fSKyle Evans 	if ((snap_hdl =
389bfe0869cSKyle Evans 	    zfs_open(isdc->lbh->lzh, snap_path, ZFS_TYPE_SNAPSHOT)) == NULL)
39028f16a0fSKyle Evans 		return (set_error(isdc->lbh, BE_ERR_ZFSOPEN));
39128f16a0fSKyle Evans 
39228f16a0fSKyle Evans 	nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
39328f16a0fSKyle Evans 	nvlist_add_string(props, "canmount", "noauto");
39428f16a0fSKyle Evans 
39528f16a0fSKyle Evans 	dccb.zhp = ds;
39628f16a0fSKyle Evans 	dccb.props = props;
39728f16a0fSKyle Evans 	if (zprop_iter(be_deep_clone_prop, &dccb, B_FALSE, B_FALSE,
398bfe0869cSKyle Evans 	    ZFS_TYPE_FILESYSTEM) == ZPROP_INVAL)
39928f16a0fSKyle Evans 		return (-1);
40028f16a0fSKyle Evans 
401*cc4deabcSKyle Evans 	if ((err = zfs_clone(snap_hdl, be_path, props)) != 0)
40228f16a0fSKyle Evans 		err = BE_ERR_ZFSCLONE;
40328f16a0fSKyle Evans 
40428f16a0fSKyle Evans 	nvlist_free(props);
40528f16a0fSKyle Evans 	zfs_close(snap_hdl);
40628f16a0fSKyle Evans 
407*cc4deabcSKyle Evans 	/* Failed to clone */
408*cc4deabcSKyle Evans 	if (err != BE_ERR_SUCCESS)
409*cc4deabcSKyle Evans 		return (set_error(isdc->lbh, err));
410*cc4deabcSKyle Evans 
41128f16a0fSKyle Evans 	sdc.lbh = isdc->lbh;
41228f16a0fSKyle Evans 	sdc.bename = NULL;
41328f16a0fSKyle Evans 	sdc.snapname = isdc->snapname;
41428f16a0fSKyle Evans 	sdc.be_root = (char *)&be_path;
41528f16a0fSKyle Evans 
41628f16a0fSKyle Evans 	err = zfs_iter_filesystems(ds, be_deep_clone, &sdc);
41728f16a0fSKyle Evans 
41828f16a0fSKyle Evans 	return (err);
41928f16a0fSKyle Evans }
42028f16a0fSKyle Evans 
42128f16a0fSKyle Evans /*
42228f16a0fSKyle Evans  * Create the boot environment from pre-existing snapshot
42328f16a0fSKyle Evans  */
42428f16a0fSKyle Evans int
425b29bf2f8SKyle Evans be_create_from_existing_snap(libbe_handle_t *lbh, const char *name,
426b29bf2f8SKyle Evans     const char *snap)
42728f16a0fSKyle Evans {
42828f16a0fSKyle Evans 	int err;
42928f16a0fSKyle Evans 	char be_path[BE_MAXPATHLEN];
43028f16a0fSKyle Evans 	char snap_path[BE_MAXPATHLEN];
431b29bf2f8SKyle Evans 	const char *bename;
432b29bf2f8SKyle Evans 	char *parentname, *snapname;
43328f16a0fSKyle Evans 	zfs_handle_t *parent_hdl;
43428f16a0fSKyle Evans 	struct libbe_deep_clone sdc;
43528f16a0fSKyle Evans 
436b29bf2f8SKyle Evans 	if ((err = be_validate_name(lbh, name)) != 0)
43728f16a0fSKyle Evans 		return (set_error(lbh, err));
438b29bf2f8SKyle Evans 	if ((err = be_root_concat(lbh, snap, snap_path)) != 0)
43928f16a0fSKyle Evans 		return (set_error(lbh, err));
440b29bf2f8SKyle Evans 	if ((err = be_validate_snap(lbh, snap_path)) != 0)
44128f16a0fSKyle Evans 		return (set_error(lbh, err));
44228f16a0fSKyle Evans 
443b29bf2f8SKyle Evans 	if ((err = be_root_concat(lbh, name, be_path)) != 0)
44428f16a0fSKyle Evans 		return (set_error(lbh, err));
44528f16a0fSKyle Evans 
446bfe0869cSKyle Evans 	if ((bename = strrchr(name, '/')) == NULL)
44728f16a0fSKyle Evans 		bename = name;
448bfe0869cSKyle Evans 	else
44928f16a0fSKyle Evans 		bename++;
450bfe0869cSKyle Evans 
451*cc4deabcSKyle Evans 	if ((parentname = strdup(snap_path)) == NULL)
452*cc4deabcSKyle Evans 		return (set_error(lbh, BE_ERR_UNKNOWN));
453*cc4deabcSKyle Evans 
45428f16a0fSKyle Evans 	snapname = strchr(parentname, '@');
45528f16a0fSKyle Evans 	if (snapname == NULL) {
456*cc4deabcSKyle Evans 		free(parentname);
457*cc4deabcSKyle Evans 		return (set_error(lbh, BE_ERR_UNKNOWN));
45828f16a0fSKyle Evans 	}
45928f16a0fSKyle Evans 	*snapname = '\0';
46028f16a0fSKyle Evans 	snapname++;
46128f16a0fSKyle Evans 
46228f16a0fSKyle Evans 	sdc.lbh = lbh;
46328f16a0fSKyle Evans 	sdc.bename = bename;
46428f16a0fSKyle Evans 	sdc.snapname = snapname;
46528f16a0fSKyle Evans 	sdc.be_root = lbh->root;
46628f16a0fSKyle Evans 
46728f16a0fSKyle Evans 	parent_hdl = zfs_open(lbh->lzh, parentname, ZFS_TYPE_DATASET);
46828f16a0fSKyle Evans 	err = be_deep_clone(parent_hdl, &sdc);
46928f16a0fSKyle Evans 
470*cc4deabcSKyle Evans 	free(parentname);
47128f16a0fSKyle Evans 	return (set_error(lbh, err));
47228f16a0fSKyle Evans }
47328f16a0fSKyle Evans 
47428f16a0fSKyle Evans 
47528f16a0fSKyle Evans /*
47628f16a0fSKyle Evans  * Create a boot environment from an existing boot environment
47728f16a0fSKyle Evans  */
47828f16a0fSKyle Evans int
479b29bf2f8SKyle Evans be_create_from_existing(libbe_handle_t *lbh, const char *name, const char *old)
48028f16a0fSKyle Evans {
48128f16a0fSKyle Evans 	int err;
48228f16a0fSKyle Evans 	char buf[BE_MAXPATHLEN];
48328f16a0fSKyle Evans 
484bfe0869cSKyle Evans 	if ((err = be_snapshot(lbh, old, NULL, true, (char *)&buf)))
48528f16a0fSKyle Evans 		return (set_error(lbh, err));
48628f16a0fSKyle Evans 
48728f16a0fSKyle Evans 	err = be_create_from_existing_snap(lbh, name, (char *)buf);
48828f16a0fSKyle Evans 
48928f16a0fSKyle Evans 	return (set_error(lbh, err));
49028f16a0fSKyle Evans }
49128f16a0fSKyle Evans 
49228f16a0fSKyle Evans 
49328f16a0fSKyle Evans /*
49428f16a0fSKyle Evans  * Verifies that a snapshot has a valid name, exists, and has a mountpoint of
49528f16a0fSKyle Evans  * '/'. Returns BE_ERR_SUCCESS (0), upon success, or the relevant BE_ERR_* upon
49628f16a0fSKyle Evans  * failure. Does not set the internal library error state.
49728f16a0fSKyle Evans  */
49828f16a0fSKyle Evans int
499b29bf2f8SKyle Evans be_validate_snap(libbe_handle_t *lbh, const char *snap_name)
50028f16a0fSKyle Evans {
50128f16a0fSKyle Evans 	zfs_handle_t *zfs_hdl;
502*cc4deabcSKyle Evans 	char buf[BE_MAXPATHLEN + 1];
50328f16a0fSKyle Evans 	char *delim_pos;
50428f16a0fSKyle Evans 	int err = BE_ERR_SUCCESS;
50528f16a0fSKyle Evans 
506bfe0869cSKyle Evans 	if (strlen(snap_name) >= BE_MAXPATHLEN)
50728f16a0fSKyle Evans 		return (BE_ERR_PATHLEN);
50828f16a0fSKyle Evans 
50928f16a0fSKyle Evans 	if (!zfs_dataset_exists(lbh->lzh, snap_name,
510bfe0869cSKyle Evans 	    ZFS_TYPE_SNAPSHOT))
51128f16a0fSKyle Evans 		return (BE_ERR_NOENT);
51228f16a0fSKyle Evans 
513*cc4deabcSKyle Evans 	strlcpy(buf, snap_name, BE_MAXPATHLEN + 1);
51428f16a0fSKyle Evans 
51528f16a0fSKyle Evans 	/* Find the base filesystem of the snapshot */
516bfe0869cSKyle Evans 	if ((delim_pos = strchr(buf, '@')) == NULL)
51728f16a0fSKyle Evans 		return (BE_ERR_INVALIDNAME);
51828f16a0fSKyle Evans 	*delim_pos = '\0';
51928f16a0fSKyle Evans 
52028f16a0fSKyle Evans 	if ((zfs_hdl =
521bfe0869cSKyle Evans 	    zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL)
52228f16a0fSKyle Evans 		return (BE_ERR_NOORIGIN);
52328f16a0fSKyle Evans 
524b29bf2f8SKyle Evans 	if ((err = zfs_prop_get(zfs_hdl, ZFS_PROP_MOUNTPOINT, buf, BE_MAXPATHLEN,
525b29bf2f8SKyle Evans 	    NULL, NULL, 0, 1)) != 0)
52628f16a0fSKyle Evans 		err = BE_ERR_INVORIGIN;
52728f16a0fSKyle Evans 
528bfe0869cSKyle Evans 	if ((err != 0) && (strncmp(buf, "/", BE_MAXPATHLEN) != 0))
52928f16a0fSKyle Evans 		err = BE_ERR_INVORIGIN;
53028f16a0fSKyle Evans 
53128f16a0fSKyle Evans 	zfs_close(zfs_hdl);
53228f16a0fSKyle Evans 
53328f16a0fSKyle Evans 	return (err);
53428f16a0fSKyle Evans }
53528f16a0fSKyle Evans 
53628f16a0fSKyle Evans 
53728f16a0fSKyle Evans /*
53828f16a0fSKyle Evans  * Idempotently appends the name argument to the root boot environment path
53928f16a0fSKyle Evans  * and copies the resulting string into the result buffer (which is assumed
54028f16a0fSKyle Evans  * to be at least BE_MAXPATHLEN characters long. Returns BE_ERR_SUCCESS upon
54128f16a0fSKyle Evans  * success, BE_ERR_PATHLEN if the resulting path is longer than BE_MAXPATHLEN,
54228f16a0fSKyle Evans  * or BE_ERR_INVALIDNAME if the name is a path that does not begin with
54328f16a0fSKyle Evans  * zfs_be_root. Does not set internal library error state.
54428f16a0fSKyle Evans  */
54528f16a0fSKyle Evans int
546b29bf2f8SKyle Evans be_root_concat(libbe_handle_t *lbh, const char *name, char *result)
54728f16a0fSKyle Evans {
54828f16a0fSKyle Evans 	size_t name_len, root_len;
54928f16a0fSKyle Evans 
55028f16a0fSKyle Evans 	name_len = strlen(name);
55128f16a0fSKyle Evans 	root_len = strlen(lbh->root);
55228f16a0fSKyle Evans 
55328f16a0fSKyle Evans 	/* Act idempotently; return be name if it is already a full path */
55428f16a0fSKyle Evans 	if (strrchr(name, '/') != NULL) {
555bfe0869cSKyle Evans 		if (strstr(name, lbh->root) != name)
55628f16a0fSKyle Evans 			return (BE_ERR_INVALIDNAME);
55728f16a0fSKyle Evans 
558bfe0869cSKyle Evans 		if (name_len >= BE_MAXPATHLEN)
55928f16a0fSKyle Evans 			return (BE_ERR_PATHLEN);
56028f16a0fSKyle Evans 
56128f16a0fSKyle Evans 		strncpy(result, name, BE_MAXPATHLEN);
56228f16a0fSKyle Evans 		return (BE_ERR_SUCCESS);
56328f16a0fSKyle Evans 	} else if (name_len + root_len + 1 < BE_MAXPATHLEN) {
56428f16a0fSKyle Evans 		snprintf(result, BE_MAXPATHLEN, "%s/%s", lbh->root,
56528f16a0fSKyle Evans 		    name);
56628f16a0fSKyle Evans 		return (BE_ERR_SUCCESS);
56728f16a0fSKyle Evans 	}
56828f16a0fSKyle Evans 
56928f16a0fSKyle Evans 	return (BE_ERR_PATHLEN);
57028f16a0fSKyle Evans }
57128f16a0fSKyle Evans 
57228f16a0fSKyle Evans 
57328f16a0fSKyle Evans /*
57428f16a0fSKyle Evans  * Verifies the validity of a boot environment name (A-Za-z0-9-_.). Returns
57528f16a0fSKyle Evans  * BE_ERR_SUCCESS (0) if name is valid, otherwise returns BE_ERR_INVALIDNAME.
57628f16a0fSKyle Evans  * Does not set internal library error state.
57728f16a0fSKyle Evans  */
57828f16a0fSKyle Evans int
579b29bf2f8SKyle Evans be_validate_name(libbe_handle_t *lbh __unused, const char *name)
58028f16a0fSKyle Evans {
58128f16a0fSKyle Evans 	for (int i = 0; *name; i++) {
58228f16a0fSKyle Evans 		char c = *(name++);
583bfe0869cSKyle Evans 		if (isalnum(c) || (c == '-') || (c == '_') || (c == '.'))
58428f16a0fSKyle Evans 			continue;
58528f16a0fSKyle Evans 		return (BE_ERR_INVALIDNAME);
58628f16a0fSKyle Evans 	}
58728f16a0fSKyle Evans 
58828f16a0fSKyle Evans 	return (BE_ERR_SUCCESS);
58928f16a0fSKyle Evans }
59028f16a0fSKyle Evans 
59128f16a0fSKyle Evans 
59228f16a0fSKyle Evans /*
59328f16a0fSKyle Evans  * usage
59428f16a0fSKyle Evans  */
59528f16a0fSKyle Evans int
59673c3d608SKyle Evans be_rename(libbe_handle_t *lbh, const char *old, const char *new)
59728f16a0fSKyle Evans {
59828f16a0fSKyle Evans 	char full_old[BE_MAXPATHLEN];
59928f16a0fSKyle Evans 	char full_new[BE_MAXPATHLEN];
60028f16a0fSKyle Evans 	zfs_handle_t *zfs_hdl;
60128f16a0fSKyle Evans 	int err;
60228f16a0fSKyle Evans 
603b29bf2f8SKyle Evans 	if ((err = be_root_concat(lbh, old, full_old)) != 0)
60428f16a0fSKyle Evans 		return (set_error(lbh, err));
605b29bf2f8SKyle Evans 	if ((err = be_root_concat(lbh, new, full_new)) != 0)
60628f16a0fSKyle Evans 		return (set_error(lbh, err));
60728f16a0fSKyle Evans 
6082989df09SKyle Evans 	if ((err = be_validate_name(lbh, new)) != 0)
6092989df09SKyle Evans 		return (err);
61028f16a0fSKyle Evans 
611bfe0869cSKyle Evans 	/* Check if old is active BE */
6122989df09SKyle Evans 	if (strcmp(full_old, be_active_path(lbh)) == 0)
6132989df09SKyle Evans 		return (set_error(lbh, BE_ERR_MOUNTED));
61428f16a0fSKyle Evans 
615bfe0869cSKyle Evans 	if (!zfs_dataset_exists(lbh->lzh, full_old, ZFS_TYPE_DATASET))
6162989df09SKyle Evans 		return (set_error(lbh, BE_ERR_NOENT));
61728f16a0fSKyle Evans 
618bfe0869cSKyle Evans 	if (zfs_dataset_exists(lbh->lzh, full_new, ZFS_TYPE_DATASET))
6192989df09SKyle Evans 		return (set_error(lbh, BE_ERR_EXISTS));
62028f16a0fSKyle Evans 
62128f16a0fSKyle Evans 	if ((zfs_hdl = zfs_open(lbh->lzh, full_old,
622bfe0869cSKyle Evans 	    ZFS_TYPE_FILESYSTEM)) == NULL)
6232989df09SKyle Evans 		return (set_error(lbh, BE_ERR_ZFSOPEN));
62428f16a0fSKyle Evans 
6256d4b1d24SKyle Evans 	/* XXX TODO: Allow a force flag */
6266d4b1d24SKyle Evans 	if (zfs_is_mounted(zfs_hdl, NULL)) {
6276d4b1d24SKyle Evans 		zfs_close(zfs_hdl);
6286d4b1d24SKyle Evans 		return (set_error(lbh, BE_ERR_MOUNTED));
6296d4b1d24SKyle Evans 	}
6306d4b1d24SKyle Evans 
631bfe0869cSKyle Evans 	/* recurse, nounmount, forceunmount */
63228f16a0fSKyle Evans 	struct renameflags flags = { 0, 0, 0 };
63328f16a0fSKyle Evans 
63428f16a0fSKyle Evans 	err = zfs_rename(zfs_hdl, NULL, full_new, flags);
63528f16a0fSKyle Evans 
63628f16a0fSKyle Evans 	zfs_close(zfs_hdl);
63728f16a0fSKyle Evans 
63828f16a0fSKyle Evans 	return (set_error(lbh, err));
63928f16a0fSKyle Evans }
64028f16a0fSKyle Evans 
64128f16a0fSKyle Evans 
64228f16a0fSKyle Evans int
64373c3d608SKyle Evans be_export(libbe_handle_t *lbh, const char *bootenv, int fd)
64428f16a0fSKyle Evans {
64528f16a0fSKyle Evans 	char snap_name[BE_MAXPATHLEN];
64628f16a0fSKyle Evans 	char buf[BE_MAXPATHLEN];
64728f16a0fSKyle Evans 	zfs_handle_t *zfs;
64828f16a0fSKyle Evans 	int err;
64928f16a0fSKyle Evans 
650b29bf2f8SKyle Evans 	if ((err = be_snapshot(lbh, bootenv, NULL, true, snap_name)) != 0)
6516d4b1d24SKyle Evans 		/* Use the error set by be_snapshot */
6526d4b1d24SKyle Evans 		return (err);
65328f16a0fSKyle Evans 
65428f16a0fSKyle Evans 	be_root_concat(lbh, snap_name, buf);
65528f16a0fSKyle Evans 
656bfe0869cSKyle Evans 	if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL)
657506f5fdfSKyle Evans 		return (set_error(lbh, BE_ERR_ZFSOPEN));
65828f16a0fSKyle Evans 
65928f16a0fSKyle Evans 	err = zfs_send_one(zfs, NULL, fd, 0);
6606d4b1d24SKyle Evans 	zfs_close(zfs);
6616d4b1d24SKyle Evans 
66228f16a0fSKyle Evans 	return (err);
66328f16a0fSKyle Evans }
66428f16a0fSKyle Evans 
66528f16a0fSKyle Evans 
66628f16a0fSKyle Evans int
66773c3d608SKyle Evans be_import(libbe_handle_t *lbh, const char *bootenv, int fd)
66828f16a0fSKyle Evans {
66928f16a0fSKyle Evans 	char buf[BE_MAXPATHLEN];
67028f16a0fSKyle Evans 	time_t rawtime;
67128f16a0fSKyle Evans 	nvlist_t *props;
67228f16a0fSKyle Evans 	zfs_handle_t *zfs;
67328f16a0fSKyle Evans 	int err, len;
674506f5fdfSKyle Evans 	char nbuf[24];
67528f16a0fSKyle Evans 
676bfe0869cSKyle Evans 	/*
677506f5fdfSKyle Evans 	 * We don't need this to be incredibly random, just unique enough that
678506f5fdfSKyle Evans 	 * it won't conflict with an existing dataset name.  Chopping time
679506f5fdfSKyle Evans 	 * down to 32 bits is probably good enough for this.
680bfe0869cSKyle Evans 	 */
681506f5fdfSKyle Evans 	snprintf(nbuf, 24, "tmp%u",
682506f5fdfSKyle Evans 	    (uint32_t)(time(NULL) & 0xFFFFFFFF));
683506f5fdfSKyle Evans 	if ((err = be_root_concat(lbh, nbuf, buf)) != 0)
684506f5fdfSKyle Evans 		/*
685506f5fdfSKyle Evans 		 * Technically this is our problem, but we try to use short
686506f5fdfSKyle Evans 		 * enough names that we won't run into problems except in
687506f5fdfSKyle Evans 		 * worst-case BE root approaching MAXPATHLEN.
688506f5fdfSKyle Evans 		 */
689506f5fdfSKyle Evans 		return (set_error(lbh, BE_ERR_PATHLEN));
69028f16a0fSKyle Evans 
69128f16a0fSKyle Evans 	time(&rawtime);
69228f16a0fSKyle Evans 	len = strlen(buf);
69328f16a0fSKyle Evans 	strftime(buf + len, BE_MAXPATHLEN - len,
69428f16a0fSKyle Evans 	    "@%F-%T", localtime(&rawtime));
69528f16a0fSKyle Evans 
696b29bf2f8SKyle Evans 	if ((err = lzc_receive(buf, NULL, NULL, false, fd)) != 0) {
697506f5fdfSKyle Evans 		switch (err) {
698506f5fdfSKyle Evans 		case EINVAL:
699506f5fdfSKyle Evans 			return (set_error(lbh, BE_ERR_NOORIGIN));
700506f5fdfSKyle Evans 		case ENOENT:
701506f5fdfSKyle Evans 			return (set_error(lbh, BE_ERR_NOENT));
702506f5fdfSKyle Evans 		case EIO:
703506f5fdfSKyle Evans 			return (set_error(lbh, BE_ERR_IO));
704506f5fdfSKyle Evans 		default:
705506f5fdfSKyle Evans 			return (set_error(lbh, BE_ERR_UNKNOWN));
706506f5fdfSKyle Evans 		}
70728f16a0fSKyle Evans 	}
70828f16a0fSKyle Evans 
709bfe0869cSKyle Evans 	if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT)) == NULL)
710506f5fdfSKyle Evans 		return (set_error(lbh, BE_ERR_ZFSOPEN));
71128f16a0fSKyle Evans 
71228f16a0fSKyle Evans 	nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
71328f16a0fSKyle Evans 	nvlist_add_string(props, "canmount", "noauto");
71428f16a0fSKyle Evans 	nvlist_add_string(props, "mountpoint", "/");
71528f16a0fSKyle Evans 
71628f16a0fSKyle Evans 	be_root_concat(lbh, bootenv, buf);
71728f16a0fSKyle Evans 
71828f16a0fSKyle Evans 	err = zfs_clone(zfs, buf, props);
71928f16a0fSKyle Evans 	zfs_close(zfs);
72028f16a0fSKyle Evans 	nvlist_free(props);
72128f16a0fSKyle Evans 
7221b057aacSKyle Evans 	if (err != 0)
7231b057aacSKyle Evans 		return (set_error(lbh, BE_ERR_UNKNOWN));
7241b057aacSKyle Evans 
7251b057aacSKyle Evans 	/*
72600ece7c2SKyle Evans 	 * Finally, we open up the dataset we just cloned the snapshot so that
7271b057aacSKyle Evans 	 * we may promote it.  This is necessary in order to clean up the ghost
72800ece7c2SKyle Evans 	 * snapshot that doesn't need to be seen after the operation is
72900ece7c2SKyle Evans 	 * complete.
7301b057aacSKyle Evans 	 */
7311b057aacSKyle Evans 	if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL)
7321b057aacSKyle Evans 		return (set_error(lbh, BE_ERR_ZFSOPEN));
7331b057aacSKyle Evans 
7341b057aacSKyle Evans 	err = zfs_promote(zfs);
7351b057aacSKyle Evans 	zfs_close(zfs);
7361b057aacSKyle Evans 
7371b057aacSKyle Evans 	if (err != 0)
7381b057aacSKyle Evans 		return (set_error(lbh, BE_ERR_UNKNOWN));
7391b057aacSKyle Evans 
7401b057aacSKyle Evans 	/* Clean up the temporary snapshot */
7411b057aacSKyle Evans 	return (be_destroy(lbh, nbuf, 0));
74228f16a0fSKyle Evans }
74328f16a0fSKyle Evans 
7443d1a1f2cSKyle Evans #if SOON
745c65a2111SKyle Evans static int
746c65a2111SKyle Evans be_create_child_noent(libbe_handle_t *lbh, const char *active,
747c65a2111SKyle Evans     const char *child_path)
748c65a2111SKyle Evans {
749c65a2111SKyle Evans 	nvlist_t *props;
750c65a2111SKyle Evans 	zfs_handle_t *zfs;
751c65a2111SKyle Evans 	int err;
752c65a2111SKyle Evans 
753c65a2111SKyle Evans 	nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
754c65a2111SKyle Evans 	nvlist_add_string(props, "canmount", "noauto");
755c65a2111SKyle Evans 	nvlist_add_string(props, "mountpoint", child_path);
756c65a2111SKyle Evans 
757c65a2111SKyle Evans 	/* Create */
758c65a2111SKyle Evans 	if ((err = zfs_create(lbh->lzh, active, ZFS_TYPE_DATASET,
759c65a2111SKyle Evans 	    props)) != 0) {
760c65a2111SKyle Evans 		switch (err) {
761c65a2111SKyle Evans 		case EZFS_EXISTS:
762c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_EXISTS));
763c65a2111SKyle Evans 		case EZFS_NOENT:
764c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_NOENT));
765c65a2111SKyle Evans 		case EZFS_BADTYPE:
766c65a2111SKyle Evans 		case EZFS_BADVERSION:
767c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_NOPOOL));
768c65a2111SKyle Evans 		case EZFS_BADPROP:
769c65a2111SKyle Evans 		default:
770c65a2111SKyle Evans 			/* We set something up wrong, probably... */
771c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_UNKNOWN));
772c65a2111SKyle Evans 		}
773c65a2111SKyle Evans 	}
774c65a2111SKyle Evans 	nvlist_free(props);
775c65a2111SKyle Evans 
776c65a2111SKyle Evans 	if ((zfs = zfs_open(lbh->lzh, active, ZFS_TYPE_DATASET)) == NULL)
777c65a2111SKyle Evans 		return (set_error(lbh, BE_ERR_ZFSOPEN));
778c65a2111SKyle Evans 
779c65a2111SKyle Evans 	/* Set props */
780c65a2111SKyle Evans 	if ((err = zfs_prop_set(zfs, "canmount", "noauto")) != 0) {
781c65a2111SKyle Evans 		zfs_close(zfs);
782c65a2111SKyle Evans 		/*
783c65a2111SKyle Evans 		 * Similar to other cases, this shouldn't fail unless we've
784c65a2111SKyle Evans 		 * done something wrong.  This is a new dataset that shouldn't
785c65a2111SKyle Evans 		 * have been mounted anywhere between creation and now.
786c65a2111SKyle Evans 		 */
787c65a2111SKyle Evans 		if (err == EZFS_NOMEM)
788c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_NOMEM));
789c65a2111SKyle Evans 		return (set_error(lbh, BE_ERR_UNKNOWN));
790c65a2111SKyle Evans 	}
791c65a2111SKyle Evans 	zfs_close(zfs);
792c65a2111SKyle Evans 	return (BE_ERR_SUCCESS);
793c65a2111SKyle Evans }
794c65a2111SKyle Evans 
795c65a2111SKyle Evans static int
796c65a2111SKyle Evans be_create_child_cloned(libbe_handle_t *lbh, const char *active)
797c65a2111SKyle Evans {
7983d1a1f2cSKyle Evans 	char buf[BE_MAXPATHLEN], tmp[BE_MAXPATHLEN];;
799c65a2111SKyle Evans 	zfs_handle_t *zfs;
800c65a2111SKyle Evans 	int err;
801c65a2111SKyle Evans 
802c65a2111SKyle Evans 	/* XXX TODO ? */
803c65a2111SKyle Evans 
804c65a2111SKyle Evans 	/*
805c65a2111SKyle Evans 	 * Establish if the existing path is a zfs dataset or just
806c65a2111SKyle Evans 	 * the subdirectory of one
807c65a2111SKyle Evans 	 */
8083d1a1f2cSKyle Evans 	strlcpy(tmp, "tmp/be_snap.XXXXX", sizeof(tmp));
8093d1a1f2cSKyle Evans 	if (mktemp(tmp) == NULL)
810c65a2111SKyle Evans 		return (set_error(lbh, BE_ERR_UNKNOWN));
811c65a2111SKyle Evans 
8123d1a1f2cSKyle Evans 	be_root_concat(lbh, tmp, buf);
8133d1a1f2cSKyle Evans 	printf("Here %s?\n", buf);
814c65a2111SKyle Evans 	if ((err = zfs_snapshot(lbh->lzh, buf, false, NULL)) != 0) {
815c65a2111SKyle Evans 		switch (err) {
816c65a2111SKyle Evans 		case EZFS_INVALIDNAME:
817c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_INVALIDNAME));
818c65a2111SKyle Evans 
819c65a2111SKyle Evans 		default:
820c65a2111SKyle Evans 			/*
821c65a2111SKyle Evans 			 * The other errors that zfs_ioc_snapshot might return
822c65a2111SKyle Evans 			 * shouldn't happen if we've set things up properly, so
823c65a2111SKyle Evans 			 * we'll gloss over them and call it UNKNOWN as it will
824c65a2111SKyle Evans 			 * require further triage.
825c65a2111SKyle Evans 			 */
826c65a2111SKyle Evans 			if (errno == ENOTSUP)
827c65a2111SKyle Evans 				return (set_error(lbh, BE_ERR_NOPOOL));
828c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_UNKNOWN));
829c65a2111SKyle Evans 		}
830c65a2111SKyle Evans 	}
831c65a2111SKyle Evans 
832c65a2111SKyle Evans 	/* Clone */
833c65a2111SKyle Evans 	if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT)) == NULL)
834c65a2111SKyle Evans 		return (BE_ERR_ZFSOPEN);
835c65a2111SKyle Evans 
836c65a2111SKyle Evans 	if ((err = zfs_clone(zfs, active, NULL)) != 0)
837c65a2111SKyle Evans 		/* XXX TODO correct error */
838c65a2111SKyle Evans 		return (set_error(lbh, BE_ERR_UNKNOWN));
839c65a2111SKyle Evans 
840c65a2111SKyle Evans 	/* set props */
841c65a2111SKyle Evans 	zfs_close(zfs);
842c65a2111SKyle Evans 	return (BE_ERR_SUCCESS);
843c65a2111SKyle Evans }
84428f16a0fSKyle Evans 
84528f16a0fSKyle Evans int
84673c3d608SKyle Evans be_add_child(libbe_handle_t *lbh, const char *child_path, bool cp_if_exists)
84728f16a0fSKyle Evans {
84873c3d608SKyle Evans 	struct stat sb;
849c65a2111SKyle Evans 	char active[BE_MAXPATHLEN], buf[BE_MAXPATHLEN];
85028f16a0fSKyle Evans 	nvlist_t *props;
85173c3d608SKyle Evans 	const char *s;
85228f16a0fSKyle Evans 
85328f16a0fSKyle Evans 	/* Require absolute paths */
854bfe0869cSKyle Evans 	if (*child_path != '/')
8556d4b1d24SKyle Evans 		return (set_error(lbh, BE_ERR_BADPATH));
85628f16a0fSKyle Evans 
8576d4b1d24SKyle Evans 	strlcpy(active, be_active_path(lbh), BE_MAXPATHLEN);
85828f16a0fSKyle Evans 	strcpy(buf, active);
85928f16a0fSKyle Evans 
86028f16a0fSKyle Evans 	/* Create non-mountable parent dataset(s) */
86173c3d608SKyle Evans 	s = child_path;
86228f16a0fSKyle Evans 	for (char *p; (p = strchr(s+1, '/')) != NULL; s = p) {
86328f16a0fSKyle Evans 		size_t len = p - s;
86428f16a0fSKyle Evans 		strncat(buf, s, len);
86528f16a0fSKyle Evans 
86628f16a0fSKyle Evans 		nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
86728f16a0fSKyle Evans 		nvlist_add_string(props, "canmount", "off");
86828f16a0fSKyle Evans 		nvlist_add_string(props, "mountpoint", "none");
86928f16a0fSKyle Evans 		zfs_create(lbh->lzh, buf, ZFS_TYPE_DATASET, props);
87028f16a0fSKyle Evans 		nvlist_free(props);
87128f16a0fSKyle Evans 	}
87228f16a0fSKyle Evans 
87328f16a0fSKyle Evans 	/* Path does not exist as a descendent of / yet */
8746d4b1d24SKyle Evans 	if (strlcat(active, child_path, BE_MAXPATHLEN) >= BE_MAXPATHLEN)
8756d4b1d24SKyle Evans 		return (set_error(lbh, BE_ERR_PATHLEN));
87628f16a0fSKyle Evans 
87728f16a0fSKyle Evans 	if (stat(child_path, &sb) != 0) {
87828f16a0fSKyle Evans 		/* Verify that error is ENOENT */
8796d4b1d24SKyle Evans 		if (errno != ENOENT)
8806d4b1d24SKyle Evans 			return (set_error(lbh, BE_ERR_UNKNOWN));
881c65a2111SKyle Evans 		return (be_create_child_noent(lbh, active, child_path));
882c65a2111SKyle Evans 	} else if (cp_if_exists)
88328f16a0fSKyle Evans 		/* Path is already a descendent of / and should be copied */
884c65a2111SKyle Evans 		return (be_create_child_cloned(lbh, active));
8856d4b1d24SKyle Evans 	return (set_error(lbh, BE_ERR_EXISTS));
88628f16a0fSKyle Evans }
8873d1a1f2cSKyle Evans #endif	/* SOON */
88828f16a0fSKyle Evans 
889d06f7103SKyle Evans static int
890d06f7103SKyle Evans be_set_nextboot(libbe_handle_t *lbh, nvlist_t *config, uint64_t pool_guid,
891d06f7103SKyle Evans     const char *zfsdev)
892d06f7103SKyle Evans {
893d06f7103SKyle Evans 	nvlist_t **child;
894d06f7103SKyle Evans 	uint64_t vdev_guid;
895d06f7103SKyle Evans 	int c, children;
896d06f7103SKyle Evans 
897d06f7103SKyle Evans 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, &child,
898d06f7103SKyle Evans 	    &children) == 0) {
899d06f7103SKyle Evans 		for (c = 0; c < children; ++c)
900d06f7103SKyle Evans 			if (be_set_nextboot(lbh, child[c], pool_guid, zfsdev) != 0)
901d06f7103SKyle Evans 				return (1);
902d06f7103SKyle Evans 		return (0);
903d06f7103SKyle Evans 	}
904d06f7103SKyle Evans 
905d06f7103SKyle Evans 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
906d06f7103SKyle Evans 	    &vdev_guid) != 0) {
907d06f7103SKyle Evans 		return (1);
908d06f7103SKyle Evans 	}
909d06f7103SKyle Evans 
910d06f7103SKyle Evans 	if (zpool_nextboot(lbh->lzh, pool_guid, vdev_guid, zfsdev) != 0) {
911d06f7103SKyle Evans 		perror("ZFS_IOC_NEXTBOOT failed");
912d06f7103SKyle Evans 		return (1);
913d06f7103SKyle Evans 	}
914d06f7103SKyle Evans 
915d06f7103SKyle Evans 	return (0);
916d06f7103SKyle Evans }
917d06f7103SKyle Evans 
91828f16a0fSKyle Evans 
91928f16a0fSKyle Evans int
92073c3d608SKyle Evans be_activate(libbe_handle_t *lbh, const char *bootenv, bool temporary)
92128f16a0fSKyle Evans {
92228f16a0fSKyle Evans 	char be_path[BE_MAXPATHLEN];
92328f16a0fSKyle Evans 	char buf[BE_MAXPATHLEN];
92428f16a0fSKyle Evans 	uint64_t pool_guid;
925d06f7103SKyle Evans 	nvlist_t *config, *vdevs;
92628f16a0fSKyle Evans 	int err;
92728f16a0fSKyle Evans 
92828f16a0fSKyle Evans 	be_root_concat(lbh, bootenv, be_path);
92928f16a0fSKyle Evans 
93028f16a0fSKyle Evans 	/* Note: be_exists fails if mountpoint is not / */
931bfe0869cSKyle Evans 	if (!be_exists(lbh, be_path))
93228f16a0fSKyle Evans 		return (BE_ERR_NOENT);
93328f16a0fSKyle Evans 
93428f16a0fSKyle Evans 	if (temporary) {
935d06f7103SKyle Evans 		config = zpool_get_config(lbh->active_phandle, NULL);
936c65a2111SKyle Evans 		if (config == NULL)
937c65a2111SKyle Evans 			/* config should be fetchable... */
938c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_UNKNOWN));
93928f16a0fSKyle Evans 
940d06f7103SKyle Evans 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
941d06f7103SKyle Evans 		    &pool_guid) != 0)
942c65a2111SKyle Evans 			/* Similarly, it shouldn't be possible */
943c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_UNKNOWN));
944d06f7103SKyle Evans 
94528f16a0fSKyle Evans 		/* Expected format according to zfsbootcfg(8) man */
94628f16a0fSKyle Evans 		strcpy(buf, "zfs:");
94728f16a0fSKyle Evans 		strcat(buf, be_path);
94828f16a0fSKyle Evans 		strcat(buf, ":");
94928f16a0fSKyle Evans 
950c65a2111SKyle Evans 		/* We have no config tree */
951c65a2111SKyle Evans 		if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
952c65a2111SKyle Evans 		    &vdevs) != 0)
953c65a2111SKyle Evans 			return (set_error(lbh, BE_ERR_NOPOOL));
95428f16a0fSKyle Evans 
955d06f7103SKyle Evans 		return (be_set_nextboot(lbh, vdevs, pool_guid, buf));
95628f16a0fSKyle Evans 	} else {
95728f16a0fSKyle Evans 		/* Obtain bootenv zpool */
958c3a34c08SKyle Evans 		err = zpool_set_prop(lbh->active_phandle, "bootfs", be_path);
95928f16a0fSKyle Evans 
96028f16a0fSKyle Evans 		switch (err) {
96128f16a0fSKyle Evans 		case 0:
96228f16a0fSKyle Evans 			return (BE_ERR_SUCCESS);
96328f16a0fSKyle Evans 
96428f16a0fSKyle Evans 		default:
965bfe0869cSKyle Evans 			/* XXX TODO correct errors */
96628f16a0fSKyle Evans 			return (-1);
96728f16a0fSKyle Evans 		}
96828f16a0fSKyle Evans 	}
96928f16a0fSKyle Evans }
970