xref: /freebsd/lib/libbe/be.c (revision 920abf4df24e6f654af3698d28c58e481bcca6c3)
128f16a0fSKyle Evans /*
228f16a0fSKyle Evans  * be.c
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 
2928f16a0fSKyle Evans #include <sys/stat.h>
3028f16a0fSKyle Evans #include <sys/types.h>
3128f16a0fSKyle Evans 
3228f16a0fSKyle Evans #include <ctype.h>
3328f16a0fSKyle Evans #include <kenv.h>
3428f16a0fSKyle Evans #include <libgen.h>
3528f16a0fSKyle Evans #include <libzfs_core.h>
3628f16a0fSKyle Evans #include <stdio.h>
3728f16a0fSKyle Evans #include <stdlib.h>
3828f16a0fSKyle Evans #include <time.h>
3928f16a0fSKyle Evans #include <unistd.h>
4028f16a0fSKyle Evans 
4128f16a0fSKyle Evans #include "be.h"
4228f16a0fSKyle Evans #include "be_impl.h"
4328f16a0fSKyle Evans 
4428f16a0fSKyle Evans /*
45ee16b7c9SKyle Evans  * Iterator function for locating the rootfs amongst the children of the
46ee16b7c9SKyle Evans  * zfs_be_root set by loader(8).  data is expected to be a libbe_handle_t *.
47ee16b7c9SKyle Evans  */
48ee16b7c9SKyle Evans static int
49ee16b7c9SKyle Evans be_locate_rootfs(zfs_handle_t *chkds, void *data)
50ee16b7c9SKyle Evans {
51ee16b7c9SKyle Evans 	libbe_handle_t *lbh;
52ee16b7c9SKyle Evans 	char *mntpoint;
53ee16b7c9SKyle Evans 
54ee16b7c9SKyle Evans 	lbh = (libbe_handle_t *)data;
55ee16b7c9SKyle Evans 	if (lbh == NULL)
56ee16b7c9SKyle Evans 		return (1);
57ee16b7c9SKyle Evans 
58ee16b7c9SKyle Evans 	if (zfs_is_mounted(chkds, &mntpoint) && strcmp(mntpoint, "/") == 0) {
59ee16b7c9SKyle Evans 		strncpy(lbh->rootfs, zfs_get_name(chkds), BE_MAXPATHLEN);
60ee16b7c9SKyle Evans 		return (1);
61ee16b7c9SKyle Evans 	}
62ee16b7c9SKyle Evans 
63ee16b7c9SKyle Evans 	return (0);
64ee16b7c9SKyle Evans }
65ee16b7c9SKyle Evans 
66ee16b7c9SKyle Evans /*
6728f16a0fSKyle Evans  * Initializes the libbe context to operate in the root boot environment
6828f16a0fSKyle Evans  * dataset, for example, zroot/ROOT.
6928f16a0fSKyle Evans  */
7028f16a0fSKyle Evans libbe_handle_t *
7128f16a0fSKyle Evans libbe_init(void)
7228f16a0fSKyle Evans {
7328f16a0fSKyle Evans 	struct stat sb;
7428f16a0fSKyle Evans 	dev_t root_dev, boot_dev;
7528f16a0fSKyle Evans 	libbe_handle_t *lbh;
76ee16b7c9SKyle Evans 	zfs_handle_t *rootds;
77c3a34c08SKyle Evans 	char *poolname, *pos;
78c3a34c08SKyle Evans 	int pnamelen;
7928f16a0fSKyle Evans 
80c3a34c08SKyle Evans 	lbh = NULL;
81c3a34c08SKyle Evans 	poolname = pos = NULL;
82c3a34c08SKyle Evans 	pnamelen = 0;
83ee16b7c9SKyle Evans 	rootds = NULL;
84ee16b7c9SKyle Evans 
8528f16a0fSKyle Evans 	/* Verify that /boot and / are mounted on the same filesystem */
86c3a34c08SKyle Evans 	/* TODO: use errno here?? */
87c3a34c08SKyle Evans 	if (stat("/", &sb) != 0)
88c3a34c08SKyle Evans 		goto err;
8928f16a0fSKyle Evans 
9028f16a0fSKyle Evans 	root_dev = sb.st_dev;
9128f16a0fSKyle Evans 
92c3a34c08SKyle Evans 	if (stat("/boot", &sb) != 0)
93c3a34c08SKyle Evans 		goto err;
9428f16a0fSKyle Evans 
9528f16a0fSKyle Evans 	boot_dev = sb.st_dev;
9628f16a0fSKyle Evans 
9728f16a0fSKyle Evans 	if (root_dev != boot_dev) {
9828f16a0fSKyle Evans 		fprintf(stderr, "/ and /boot not on same device, quitting\n");
99c3a34c08SKyle Evans 		goto err;
10028f16a0fSKyle Evans 	}
10128f16a0fSKyle Evans 
102c3a34c08SKyle Evans 	if ((lbh = calloc(1, sizeof(libbe_handle_t))) == NULL)
103c3a34c08SKyle Evans 		goto err;
10428f16a0fSKyle Evans 
105c3a34c08SKyle Evans 	if ((lbh->lzh = libzfs_init()) == NULL)
106c3a34c08SKyle Evans 		goto err;
10728f16a0fSKyle Evans 
10828f16a0fSKyle Evans 	/* Obtain path to boot environment root */
109c3a34c08SKyle Evans 	if ((kenv(KENV_GET, "zfs_be_root", lbh->root, BE_MAXPATHLEN)) == -1)
110c3a34c08SKyle Evans 		goto err;
11128f16a0fSKyle Evans 
11228f16a0fSKyle Evans 	/* Remove leading 'zfs:' if present, otherwise use value as-is */
113c3a34c08SKyle Evans 	if (strcmp(lbh->root, "zfs:") == 0)
114c3a34c08SKyle Evans 		strncpy(lbh->root, strchr(lbh->root, ':') + sizeof(char),
115c3a34c08SKyle Evans 		    BE_MAXPATHLEN);
116c3a34c08SKyle Evans 
117c3a34c08SKyle Evans 	if ((pos = strchr(lbh->root, '/')) == NULL)
118c3a34c08SKyle Evans 		goto err;
119c3a34c08SKyle Evans 
120c3a34c08SKyle Evans 	pnamelen = pos - lbh->root;
121c3a34c08SKyle Evans 	poolname = malloc(pnamelen + 1);
122c3a34c08SKyle Evans 	if (poolname == NULL)
123c3a34c08SKyle Evans 		goto err;
124c3a34c08SKyle Evans 
125c3a34c08SKyle Evans 	strncpy(poolname, lbh->root, pnamelen);
126c3a34c08SKyle Evans 	poolname[pnamelen] = '\0';
127c3a34c08SKyle Evans 	if ((lbh->active_phandle = zpool_open(lbh->lzh, poolname)) == NULL)
128c3a34c08SKyle Evans 		goto err;
129c3a34c08SKyle Evans 
130c3a34c08SKyle Evans 	if (zpool_get_prop(lbh->active_phandle, ZPOOL_PROP_BOOTFS, lbh->bootfs,
131c3a34c08SKyle Evans 	    BE_MAXPATHLEN, NULL, true) != 0)
132c3a34c08SKyle Evans 		goto err;
133c3a34c08SKyle Evans 
134c3a34c08SKyle Evans 	/* Obtain path to boot environment rootfs (currently booted) */
135c3a34c08SKyle Evans 	/* XXX Get dataset mounted at / by kenv/GUID from mountroot? */
136ee16b7c9SKyle Evans 	if ((rootds = zfs_open(lbh->lzh, lbh->root, ZFS_TYPE_DATASET)) == NULL)
137c3a34c08SKyle Evans 		goto err;
138c3a34c08SKyle Evans 
139ee16b7c9SKyle Evans 	zfs_iter_filesystems(rootds, be_locate_rootfs, lbh);
140ee16b7c9SKyle Evans 	zfs_close(rootds);
141ff8676ccSKyle Evans 	rootds = NULL;
142b29bf2f8SKyle Evans 	if (*lbh->rootfs == '\0')
143ff8676ccSKyle Evans 		goto err;
14428f16a0fSKyle Evans 
14528f16a0fSKyle Evans 	return (lbh);
146c3a34c08SKyle Evans err:
147c3a34c08SKyle Evans 	if (lbh != NULL) {
148c3a34c08SKyle Evans 		if (lbh->active_phandle != NULL)
149c3a34c08SKyle Evans 			zpool_close(lbh->active_phandle);
150c3a34c08SKyle Evans 		if (lbh->lzh != NULL)
151c3a34c08SKyle Evans 			libzfs_fini(lbh->lzh);
152c3a34c08SKyle Evans 		free(lbh);
153c3a34c08SKyle Evans 	}
154ee16b7c9SKyle Evans 	if (rootds != NULL)
155ee16b7c9SKyle Evans 		zfs_close(rootds);
156c3a34c08SKyle Evans 	free(poolname);
157c3a34c08SKyle Evans 	return (NULL);
15828f16a0fSKyle Evans }
15928f16a0fSKyle Evans 
16028f16a0fSKyle Evans 
16128f16a0fSKyle Evans /*
16228f16a0fSKyle Evans  * Free memory allocated by libbe_init()
16328f16a0fSKyle Evans  */
16428f16a0fSKyle Evans void
16528f16a0fSKyle Evans libbe_close(libbe_handle_t *lbh)
16628f16a0fSKyle Evans {
167bfe0869cSKyle Evans 
168c3a34c08SKyle Evans 	if (lbh->active_phandle != NULL)
169c3a34c08SKyle Evans 		zpool_close(lbh->active_phandle);
17028f16a0fSKyle Evans 	libzfs_fini(lbh->lzh);
17128f16a0fSKyle Evans 	free(lbh);
17228f16a0fSKyle Evans }
17328f16a0fSKyle Evans 
1749b1662e6SKyle Evans /*
1759b1662e6SKyle Evans  * Proxy through to libzfs for the moment.
1769b1662e6SKyle Evans  */
1779b1662e6SKyle Evans void
1789b1662e6SKyle Evans be_nicenum(uint64_t num, char *buf, size_t buflen)
1799b1662e6SKyle Evans {
1809b1662e6SKyle Evans 
1819b1662e6SKyle Evans 	zfs_nicenum(num, buf, buflen);
1829b1662e6SKyle Evans }
18328f16a0fSKyle Evans 
184*920abf4dSKyle Evans static int
185*920abf4dSKyle Evans be_destroy_cb(zfs_handle_t *zfs_hdl, void *data)
186*920abf4dSKyle Evans {
187*920abf4dSKyle Evans 	int err;
188*920abf4dSKyle Evans 
189*920abf4dSKyle Evans 	if ((err = zfs_iter_children(zfs_hdl, be_destroy_cb, data)) != 0)
190*920abf4dSKyle Evans 		return (err);
191*920abf4dSKyle Evans 	if ((err = zfs_destroy(zfs_hdl, false)) != 0)
192*920abf4dSKyle Evans 		return (err);
193*920abf4dSKyle Evans 	return (0);
194*920abf4dSKyle Evans }
195*920abf4dSKyle Evans 
19628f16a0fSKyle Evans /*
19728f16a0fSKyle Evans  * Destroy the boot environment or snapshot specified by the name
19828f16a0fSKyle Evans  * parameter. Options are or'd together with the possible values:
19928f16a0fSKyle Evans  * BE_DESTROY_FORCE : forces operation on mounted datasets
20028f16a0fSKyle Evans  */
20128f16a0fSKyle Evans int
20228f16a0fSKyle Evans be_destroy(libbe_handle_t *lbh, char *name, int options)
20328f16a0fSKyle Evans {
20428f16a0fSKyle Evans 	zfs_handle_t *fs;
20528f16a0fSKyle Evans 	char path[BE_MAXPATHLEN];
206bfe0869cSKyle Evans 	char *p;
207bfe0869cSKyle Evans 	int err, force, mounted;
20828f16a0fSKyle Evans 
209bfe0869cSKyle Evans 	p = path;
210bfe0869cSKyle Evans 	force = options & BE_DESTROY_FORCE;
211bfe0869cSKyle Evans 	err = BE_ERR_SUCCESS;
21228f16a0fSKyle Evans 
21328f16a0fSKyle Evans 	be_root_concat(lbh, name, path);
21428f16a0fSKyle Evans 
21528f16a0fSKyle Evans 	if (strchr(name, '@') == NULL) {
216bfe0869cSKyle Evans 		if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_FILESYSTEM))
21728f16a0fSKyle Evans 			return (set_error(lbh, BE_ERR_NOENT));
21828f16a0fSKyle Evans 
219bfe0869cSKyle Evans 		if (strcmp(path, lbh->rootfs) == 0)
22028f16a0fSKyle Evans 			return (set_error(lbh, BE_ERR_DESTROYACT));
22128f16a0fSKyle Evans 
22228f16a0fSKyle Evans 		fs = zfs_open(lbh->lzh, p, ZFS_TYPE_FILESYSTEM);
22328f16a0fSKyle Evans 	} else {
22428f16a0fSKyle Evans 
225bfe0869cSKyle Evans 		if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_SNAPSHOT))
22628f16a0fSKyle Evans 			return (set_error(lbh, BE_ERR_NOENT));
22728f16a0fSKyle Evans 
22828f16a0fSKyle Evans 		fs = zfs_open(lbh->lzh, p, ZFS_TYPE_SNAPSHOT);
22928f16a0fSKyle Evans 	}
23028f16a0fSKyle Evans 
23128f16a0fSKyle Evans 	if (fs == NULL)
23228f16a0fSKyle Evans 		return (set_error(lbh, BE_ERR_ZFSOPEN));
23328f16a0fSKyle Evans 
23428f16a0fSKyle Evans 	/* Check if mounted, unmount if force is specified */
235b29bf2f8SKyle Evans 	if ((mounted = zfs_is_mounted(fs, NULL)) != 0) {
236bfe0869cSKyle Evans 		if (force)
23728f16a0fSKyle Evans 			zfs_unmount(fs, NULL, 0);
238bfe0869cSKyle Evans 		else
23928f16a0fSKyle Evans 			return (set_error(lbh, BE_ERR_DESTROYMNT));
24028f16a0fSKyle Evans 	}
24128f16a0fSKyle Evans 
242*920abf4dSKyle Evans 	if ((err = be_destroy_cb(fs, NULL)) != 0) {
243*920abf4dSKyle Evans 		/* Children are still present or the mount is referenced */
244*920abf4dSKyle Evans 		if (err == EBUSY)
245*920abf4dSKyle Evans 			return (set_error(lbh, BE_ERR_DESTROYMNT));
246*920abf4dSKyle Evans 		return (set_error(lbh, BE_ERR_UNKNOWN));
247*920abf4dSKyle Evans 	}
24828f16a0fSKyle Evans 
249*920abf4dSKyle Evans 	return (0);
25028f16a0fSKyle Evans }
25128f16a0fSKyle Evans 
25228f16a0fSKyle Evans 
25328f16a0fSKyle Evans int
254b29bf2f8SKyle Evans be_snapshot(libbe_handle_t *lbh, const char *source, const char *snap_name,
25528f16a0fSKyle Evans     bool recursive, char *result)
25628f16a0fSKyle Evans {
25728f16a0fSKyle Evans 	char buf[BE_MAXPATHLEN];
25828f16a0fSKyle Evans 	time_t rawtime;
25928f16a0fSKyle Evans 	int len, err;
26028f16a0fSKyle Evans 
26128f16a0fSKyle Evans 	be_root_concat(lbh, source, buf);
26228f16a0fSKyle Evans 
263bfe0869cSKyle Evans 	if (!be_exists(lbh, buf))
26428f16a0fSKyle Evans 		return (BE_ERR_NOENT);
26528f16a0fSKyle Evans 
26628f16a0fSKyle Evans 	if (snap_name != NULL) {
26728f16a0fSKyle Evans 		strcat(buf, "@");
26828f16a0fSKyle Evans 		strcat(buf, snap_name);
269bfe0869cSKyle Evans 		if (result != NULL)
27028f16a0fSKyle Evans 			snprintf(result, BE_MAXPATHLEN, "%s@%s", source,
27128f16a0fSKyle Evans 			    snap_name);
27228f16a0fSKyle Evans 	} else {
27328f16a0fSKyle Evans 		time(&rawtime);
27428f16a0fSKyle Evans 		len = strlen(buf);
27528f16a0fSKyle Evans 		strftime(buf + len, BE_MAXPATHLEN - len,
27628f16a0fSKyle Evans 		    "@%F-%T", localtime(&rawtime));
277bfe0869cSKyle Evans 		if (result != NULL)
27828f16a0fSKyle Evans 			strcpy(result, strrchr(buf, '/') + 1);
27928f16a0fSKyle Evans 	}
28028f16a0fSKyle Evans 
281b29bf2f8SKyle Evans 	if ((err = zfs_snapshot(lbh->lzh, buf, recursive, NULL)) != 0) {
28228f16a0fSKyle Evans 		switch (err) {
28328f16a0fSKyle Evans 		case EZFS_INVALIDNAME:
28428f16a0fSKyle Evans 			return (set_error(lbh, BE_ERR_INVALIDNAME));
28528f16a0fSKyle Evans 
28628f16a0fSKyle Evans 		default:
287bfe0869cSKyle Evans 			/* XXX TODO: elaborate return codes */
28828f16a0fSKyle Evans 			return (set_error(lbh, BE_ERR_UNKNOWN));
28928f16a0fSKyle Evans 		}
29028f16a0fSKyle Evans 	}
29128f16a0fSKyle Evans 
29228f16a0fSKyle Evans 	return (BE_ERR_SUCCESS);
29328f16a0fSKyle Evans }
29428f16a0fSKyle Evans 
29528f16a0fSKyle Evans 
29628f16a0fSKyle Evans /*
29728f16a0fSKyle Evans  * Create the boot environment specified by the name parameter
29828f16a0fSKyle Evans  */
29928f16a0fSKyle Evans int
30028f16a0fSKyle Evans be_create(libbe_handle_t *lbh, char *name)
30128f16a0fSKyle Evans {
30228f16a0fSKyle Evans 	int err;
30328f16a0fSKyle Evans 
304b29bf2f8SKyle Evans 	err = be_create_from_existing(lbh, name, be_active_path(lbh));
30528f16a0fSKyle Evans 
30628f16a0fSKyle Evans 	return (set_error(lbh, err));
30728f16a0fSKyle Evans }
30828f16a0fSKyle Evans 
30928f16a0fSKyle Evans 
31028f16a0fSKyle Evans static int
31128f16a0fSKyle Evans be_deep_clone_prop(int prop, void *cb)
31228f16a0fSKyle Evans {
31328f16a0fSKyle Evans 	int err;
314bfe0869cSKyle Evans         struct libbe_dccb *dccb;
31528f16a0fSKyle Evans 	zprop_source_t src;
31628f16a0fSKyle Evans 	char pval[BE_MAXPATHLEN];
31728f16a0fSKyle Evans 	char source[BE_MAXPATHLEN];
31828f16a0fSKyle Evans 
319bfe0869cSKyle Evans 	dccb = cb;
32028f16a0fSKyle Evans 	/* Skip some properties we don't want to touch */
32128f16a0fSKyle Evans 	switch (prop) {
32228f16a0fSKyle Evans 		case ZFS_PROP_CANMOUNT:
32328f16a0fSKyle Evans 			return (ZPROP_CONT);
32428f16a0fSKyle Evans 			break;
32528f16a0fSKyle Evans 	}
32628f16a0fSKyle Evans 
32728f16a0fSKyle Evans 	/* Don't copy readonly properties */
328bfe0869cSKyle Evans 	if (zfs_prop_readonly(prop))
32928f16a0fSKyle Evans 		return (ZPROP_CONT);
33028f16a0fSKyle Evans 
33128f16a0fSKyle Evans 	if ((err = zfs_prop_get(dccb->zhp, prop, (char *)&pval,
332bfe0869cSKyle Evans 	    sizeof(pval), &src, (char *)&source, sizeof(source), false)))
33328f16a0fSKyle Evans 		/* Just continue if we fail to read a property */
33428f16a0fSKyle Evans 		return (ZPROP_CONT);
335bfe0869cSKyle Evans 
33628f16a0fSKyle Evans 	/* Only copy locally defined properties */
337bfe0869cSKyle Evans 	if (src != ZPROP_SRC_LOCAL)
33828f16a0fSKyle Evans 		return (ZPROP_CONT);
33928f16a0fSKyle Evans 
34028f16a0fSKyle Evans 	nvlist_add_string(dccb->props, zfs_prop_to_name(prop), (char *)pval);
34128f16a0fSKyle Evans 
34228f16a0fSKyle Evans 	return (ZPROP_CONT);
34328f16a0fSKyle Evans }
34428f16a0fSKyle Evans 
34528f16a0fSKyle Evans static int
34628f16a0fSKyle Evans be_deep_clone(zfs_handle_t *ds, void *data)
34728f16a0fSKyle Evans {
34828f16a0fSKyle Evans 	int err;
34928f16a0fSKyle Evans 	char be_path[BE_MAXPATHLEN];
35028f16a0fSKyle Evans 	char snap_path[BE_MAXPATHLEN];
35128f16a0fSKyle Evans 	const char *dspath;
35228f16a0fSKyle Evans 	char *dsname;
35328f16a0fSKyle Evans 	zfs_handle_t *snap_hdl;
35428f16a0fSKyle Evans 	nvlist_t *props;
355bfe0869cSKyle Evans 	struct libbe_deep_clone *isdc, sdc;
35628f16a0fSKyle Evans 	struct libbe_dccb dccb;
35728f16a0fSKyle Evans 
358bfe0869cSKyle Evans 	isdc = (struct libbe_deep_clone *)data;
35928f16a0fSKyle Evans 	dspath = zfs_get_name(ds);
360bfe0869cSKyle Evans 	if ((dsname = strrchr(dspath, '/')) == NULL)
36128f16a0fSKyle Evans 		return (BE_ERR_UNKNOWN);
36228f16a0fSKyle Evans 	dsname++;
363bfe0869cSKyle Evans 
364bfe0869cSKyle Evans 	if (isdc->bename == NULL)
36528f16a0fSKyle Evans 		snprintf(be_path, sizeof(be_path), "%s/%s", isdc->be_root, dsname);
366bfe0869cSKyle Evans 	else
36728f16a0fSKyle Evans 		snprintf(be_path, sizeof(be_path), "%s/%s", isdc->be_root, isdc->bename);
368bfe0869cSKyle Evans 
36928f16a0fSKyle Evans 	snprintf(snap_path, sizeof(snap_path), "%s@%s", dspath, isdc->snapname);
37028f16a0fSKyle Evans 
371bfe0869cSKyle Evans 	if (zfs_dataset_exists(isdc->lbh->lzh, be_path, ZFS_TYPE_DATASET))
37228f16a0fSKyle Evans 		return (set_error(isdc->lbh, BE_ERR_EXISTS));
37328f16a0fSKyle Evans 
37428f16a0fSKyle Evans 	if ((snap_hdl =
375bfe0869cSKyle Evans 	    zfs_open(isdc->lbh->lzh, snap_path, ZFS_TYPE_SNAPSHOT)) == NULL)
37628f16a0fSKyle Evans 		return (set_error(isdc->lbh, BE_ERR_ZFSOPEN));
37728f16a0fSKyle Evans 
37828f16a0fSKyle Evans 	nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
37928f16a0fSKyle Evans 	nvlist_add_string(props, "canmount", "noauto");
38028f16a0fSKyle Evans 
38128f16a0fSKyle Evans 	dccb.zhp = ds;
38228f16a0fSKyle Evans 	dccb.props = props;
38328f16a0fSKyle Evans 	if (zprop_iter(be_deep_clone_prop, &dccb, B_FALSE, B_FALSE,
384bfe0869cSKyle Evans 	    ZFS_TYPE_FILESYSTEM) == ZPROP_INVAL)
38528f16a0fSKyle Evans 		return (-1);
38628f16a0fSKyle Evans 
387b29bf2f8SKyle Evans 	if ((err = zfs_clone(snap_hdl, be_path, props)) != 0) {
38828f16a0fSKyle Evans 		switch (err) {
38928f16a0fSKyle Evans 		case EZFS_SUCCESS:
39028f16a0fSKyle Evans 			err = BE_ERR_SUCCESS;
39128f16a0fSKyle Evans 			break;
39228f16a0fSKyle Evans 		default:
39328f16a0fSKyle Evans 			err = BE_ERR_ZFSCLONE;
39428f16a0fSKyle Evans 			break;
39528f16a0fSKyle Evans 		}
39628f16a0fSKyle Evans 	}
39728f16a0fSKyle Evans 
39828f16a0fSKyle Evans 	nvlist_free(props);
39928f16a0fSKyle Evans 	zfs_close(snap_hdl);
40028f16a0fSKyle Evans 
40128f16a0fSKyle Evans 	sdc.lbh = isdc->lbh;
40228f16a0fSKyle Evans 	sdc.bename = NULL;
40328f16a0fSKyle Evans 	sdc.snapname = isdc->snapname;
40428f16a0fSKyle Evans 	sdc.be_root = (char *)&be_path;
40528f16a0fSKyle Evans 
40628f16a0fSKyle Evans 	err = zfs_iter_filesystems(ds, be_deep_clone, &sdc);
40728f16a0fSKyle Evans 
40828f16a0fSKyle Evans 	return (err);
40928f16a0fSKyle Evans }
41028f16a0fSKyle Evans 
41128f16a0fSKyle Evans /*
41228f16a0fSKyle Evans  * Create the boot environment from pre-existing snapshot
41328f16a0fSKyle Evans  */
41428f16a0fSKyle Evans int
415b29bf2f8SKyle Evans be_create_from_existing_snap(libbe_handle_t *lbh, const char *name,
416b29bf2f8SKyle Evans     const char *snap)
41728f16a0fSKyle Evans {
41828f16a0fSKyle Evans 	int err;
41928f16a0fSKyle Evans 	char be_path[BE_MAXPATHLEN];
42028f16a0fSKyle Evans 	char snap_path[BE_MAXPATHLEN];
421b29bf2f8SKyle Evans 	const char *bename;
422b29bf2f8SKyle Evans 	char *parentname, *snapname;
42328f16a0fSKyle Evans 	zfs_handle_t *parent_hdl;
42428f16a0fSKyle Evans 	struct libbe_deep_clone sdc;
42528f16a0fSKyle Evans 
426b29bf2f8SKyle Evans 	if ((err = be_validate_name(lbh, name)) != 0)
42728f16a0fSKyle Evans 		return (set_error(lbh, err));
428b29bf2f8SKyle Evans 	if ((err = be_root_concat(lbh, snap, snap_path)) != 0)
42928f16a0fSKyle Evans 		return (set_error(lbh, err));
430b29bf2f8SKyle Evans 	if ((err = be_validate_snap(lbh, snap_path)) != 0)
43128f16a0fSKyle Evans 		return (set_error(lbh, err));
43228f16a0fSKyle Evans 
433b29bf2f8SKyle Evans 	if ((err = be_root_concat(lbh, name, be_path)) != 0)
43428f16a0fSKyle Evans 		return (set_error(lbh, err));
43528f16a0fSKyle Evans 
436bfe0869cSKyle Evans 	if ((bename = strrchr(name, '/')) == NULL)
43728f16a0fSKyle Evans 		bename = name;
438bfe0869cSKyle Evans 	else
43928f16a0fSKyle Evans 		bename++;
440bfe0869cSKyle Evans 
44128f16a0fSKyle Evans 	if ((parentname = strdup(snap_path)) == NULL) {
44228f16a0fSKyle Evans 		err = BE_ERR_UNKNOWN;
44328f16a0fSKyle Evans 		return (set_error(lbh, err));
44428f16a0fSKyle Evans 	}
44528f16a0fSKyle Evans 	snapname = strchr(parentname, '@');
44628f16a0fSKyle Evans 	if (snapname == NULL) {
44728f16a0fSKyle Evans 		err = BE_ERR_UNKNOWN;
44828f16a0fSKyle Evans 		return (set_error(lbh, err));
44928f16a0fSKyle Evans 	}
45028f16a0fSKyle Evans 	*snapname = '\0';
45128f16a0fSKyle Evans 	snapname++;
45228f16a0fSKyle Evans 
45328f16a0fSKyle Evans 	sdc.lbh = lbh;
45428f16a0fSKyle Evans 	sdc.bename = bename;
45528f16a0fSKyle Evans 	sdc.snapname = snapname;
45628f16a0fSKyle Evans 	sdc.be_root = lbh->root;
45728f16a0fSKyle Evans 
45828f16a0fSKyle Evans 	parent_hdl = zfs_open(lbh->lzh, parentname, ZFS_TYPE_DATASET);
45928f16a0fSKyle Evans 	err = be_deep_clone(parent_hdl, &sdc);
46028f16a0fSKyle Evans 
46128f16a0fSKyle Evans 	return (set_error(lbh, err));
46228f16a0fSKyle Evans }
46328f16a0fSKyle Evans 
46428f16a0fSKyle Evans 
46528f16a0fSKyle Evans /*
46628f16a0fSKyle Evans  * Create a boot environment from an existing boot environment
46728f16a0fSKyle Evans  */
46828f16a0fSKyle Evans int
469b29bf2f8SKyle Evans be_create_from_existing(libbe_handle_t *lbh, const char *name, const char *old)
47028f16a0fSKyle Evans {
47128f16a0fSKyle Evans 	int err;
47228f16a0fSKyle Evans 	char buf[BE_MAXPATHLEN];
47328f16a0fSKyle Evans 
474bfe0869cSKyle Evans 	if ((err = be_snapshot(lbh, old, NULL, true, (char *)&buf)))
47528f16a0fSKyle Evans 		return (set_error(lbh, err));
47628f16a0fSKyle Evans 
47728f16a0fSKyle Evans 	err = be_create_from_existing_snap(lbh, name, (char *)buf);
47828f16a0fSKyle Evans 
47928f16a0fSKyle Evans 	return (set_error(lbh, err));
48028f16a0fSKyle Evans }
48128f16a0fSKyle Evans 
48228f16a0fSKyle Evans 
48328f16a0fSKyle Evans /*
48428f16a0fSKyle Evans  * Verifies that a snapshot has a valid name, exists, and has a mountpoint of
48528f16a0fSKyle Evans  * '/'. Returns BE_ERR_SUCCESS (0), upon success, or the relevant BE_ERR_* upon
48628f16a0fSKyle Evans  * failure. Does not set the internal library error state.
48728f16a0fSKyle Evans  */
48828f16a0fSKyle Evans int
489b29bf2f8SKyle Evans be_validate_snap(libbe_handle_t *lbh, const char *snap_name)
49028f16a0fSKyle Evans {
49128f16a0fSKyle Evans 	zfs_handle_t *zfs_hdl;
49228f16a0fSKyle Evans 	char buf[BE_MAXPATHLEN];
49328f16a0fSKyle Evans 	char *delim_pos;
49428f16a0fSKyle Evans 	int err = BE_ERR_SUCCESS;
49528f16a0fSKyle Evans 
496bfe0869cSKyle Evans 	if (strlen(snap_name) >= BE_MAXPATHLEN)
49728f16a0fSKyle Evans 		return (BE_ERR_PATHLEN);
49828f16a0fSKyle Evans 
49928f16a0fSKyle Evans 	if (!zfs_dataset_exists(lbh->lzh, snap_name,
500bfe0869cSKyle Evans 	    ZFS_TYPE_SNAPSHOT))
50128f16a0fSKyle Evans 		return (BE_ERR_NOENT);
50228f16a0fSKyle Evans 
50328f16a0fSKyle Evans 	strncpy(buf, snap_name, BE_MAXPATHLEN);
50428f16a0fSKyle Evans 
50528f16a0fSKyle Evans 	/* Find the base filesystem of the snapshot */
506bfe0869cSKyle Evans 	if ((delim_pos = strchr(buf, '@')) == NULL)
50728f16a0fSKyle Evans 		return (BE_ERR_INVALIDNAME);
50828f16a0fSKyle Evans 	*delim_pos = '\0';
50928f16a0fSKyle Evans 
51028f16a0fSKyle Evans 	if ((zfs_hdl =
511bfe0869cSKyle Evans 	    zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL)
51228f16a0fSKyle Evans 		return (BE_ERR_NOORIGIN);
51328f16a0fSKyle Evans 
514b29bf2f8SKyle Evans 	if ((err = zfs_prop_get(zfs_hdl, ZFS_PROP_MOUNTPOINT, buf, BE_MAXPATHLEN,
515b29bf2f8SKyle Evans 	    NULL, NULL, 0, 1)) != 0)
51628f16a0fSKyle Evans 		err = BE_ERR_INVORIGIN;
51728f16a0fSKyle Evans 
518bfe0869cSKyle Evans 	if ((err != 0) && (strncmp(buf, "/", BE_MAXPATHLEN) != 0))
51928f16a0fSKyle Evans 		err = BE_ERR_INVORIGIN;
52028f16a0fSKyle Evans 
52128f16a0fSKyle Evans 	zfs_close(zfs_hdl);
52228f16a0fSKyle Evans 
52328f16a0fSKyle Evans 	return (err);
52428f16a0fSKyle Evans }
52528f16a0fSKyle Evans 
52628f16a0fSKyle Evans 
52728f16a0fSKyle Evans /*
52828f16a0fSKyle Evans  * Idempotently appends the name argument to the root boot environment path
52928f16a0fSKyle Evans  * and copies the resulting string into the result buffer (which is assumed
53028f16a0fSKyle Evans  * to be at least BE_MAXPATHLEN characters long. Returns BE_ERR_SUCCESS upon
53128f16a0fSKyle Evans  * success, BE_ERR_PATHLEN if the resulting path is longer than BE_MAXPATHLEN,
53228f16a0fSKyle Evans  * or BE_ERR_INVALIDNAME if the name is a path that does not begin with
53328f16a0fSKyle Evans  * zfs_be_root. Does not set internal library error state.
53428f16a0fSKyle Evans  */
53528f16a0fSKyle Evans int
536b29bf2f8SKyle Evans be_root_concat(libbe_handle_t *lbh, const char *name, char *result)
53728f16a0fSKyle Evans {
53828f16a0fSKyle Evans 	size_t name_len, root_len;
53928f16a0fSKyle Evans 
54028f16a0fSKyle Evans 	name_len = strlen(name);
54128f16a0fSKyle Evans 	root_len = strlen(lbh->root);
54228f16a0fSKyle Evans 
54328f16a0fSKyle Evans 	/* Act idempotently; return be name if it is already a full path */
54428f16a0fSKyle Evans 	if (strrchr(name, '/') != NULL) {
545bfe0869cSKyle Evans 		if (strstr(name, lbh->root) != name)
54628f16a0fSKyle Evans 			return (BE_ERR_INVALIDNAME);
54728f16a0fSKyle Evans 
548bfe0869cSKyle Evans 		if (name_len >= BE_MAXPATHLEN)
54928f16a0fSKyle Evans 			return (BE_ERR_PATHLEN);
55028f16a0fSKyle Evans 
55128f16a0fSKyle Evans 		strncpy(result, name, BE_MAXPATHLEN);
55228f16a0fSKyle Evans 		return (BE_ERR_SUCCESS);
55328f16a0fSKyle Evans 	} else if (name_len + root_len + 1 < BE_MAXPATHLEN) {
55428f16a0fSKyle Evans 		snprintf(result, BE_MAXPATHLEN, "%s/%s", lbh->root,
55528f16a0fSKyle Evans 		    name);
55628f16a0fSKyle Evans 		return (BE_ERR_SUCCESS);
55728f16a0fSKyle Evans 	}
55828f16a0fSKyle Evans 
55928f16a0fSKyle Evans 	return (BE_ERR_PATHLEN);
56028f16a0fSKyle Evans }
56128f16a0fSKyle Evans 
56228f16a0fSKyle Evans 
56328f16a0fSKyle Evans /*
56428f16a0fSKyle Evans  * Verifies the validity of a boot environment name (A-Za-z0-9-_.). Returns
56528f16a0fSKyle Evans  * BE_ERR_SUCCESS (0) if name is valid, otherwise returns BE_ERR_INVALIDNAME.
56628f16a0fSKyle Evans  * Does not set internal library error state.
56728f16a0fSKyle Evans  */
56828f16a0fSKyle Evans int
569b29bf2f8SKyle Evans be_validate_name(libbe_handle_t *lbh __unused, const char *name)
57028f16a0fSKyle Evans {
57128f16a0fSKyle Evans 	for (int i = 0; *name; i++) {
57228f16a0fSKyle Evans 		char c = *(name++);
573bfe0869cSKyle Evans 		if (isalnum(c) || (c == '-') || (c == '_') || (c == '.'))
57428f16a0fSKyle Evans 			continue;
57528f16a0fSKyle Evans 		return (BE_ERR_INVALIDNAME);
57628f16a0fSKyle Evans 	}
57728f16a0fSKyle Evans 
57828f16a0fSKyle Evans 	return (BE_ERR_SUCCESS);
57928f16a0fSKyle Evans }
58028f16a0fSKyle Evans 
58128f16a0fSKyle Evans 
58228f16a0fSKyle Evans /*
58328f16a0fSKyle Evans  * usage
58428f16a0fSKyle Evans  */
58528f16a0fSKyle Evans int
58628f16a0fSKyle Evans be_rename(libbe_handle_t *lbh, char *old, char *new)
58728f16a0fSKyle Evans {
58828f16a0fSKyle Evans 	char full_old[BE_MAXPATHLEN];
58928f16a0fSKyle Evans 	char full_new[BE_MAXPATHLEN];
59028f16a0fSKyle Evans 	zfs_handle_t *zfs_hdl;
59128f16a0fSKyle Evans 	int err;
59228f16a0fSKyle Evans 
593b29bf2f8SKyle Evans 	if ((err = be_root_concat(lbh, old, full_old)) != 0)
59428f16a0fSKyle Evans 		return (set_error(lbh, err));
595b29bf2f8SKyle Evans 	if ((err = be_root_concat(lbh, new, full_new)) != 0)
59628f16a0fSKyle Evans 		return (set_error(lbh, err));
59728f16a0fSKyle Evans 
598b29bf2f8SKyle Evans 	if (be_validate_name(lbh, new) != 0)
59928f16a0fSKyle Evans 		return (BE_ERR_UNKNOWN);
600bfe0869cSKyle Evans 		/* XXX TODO set and return correct error */
60128f16a0fSKyle Evans 
602bfe0869cSKyle Evans 	/* Check if old is active BE */
603bfe0869cSKyle Evans 	if (strcmp(full_new, be_active_path(lbh)) == 0)
60428f16a0fSKyle Evans 		return (BE_ERR_UNKNOWN);
605bfe0869cSKyle Evans 		/* XXX TODO set and return correct error */
60628f16a0fSKyle Evans 
607bfe0869cSKyle Evans 	if (!zfs_dataset_exists(lbh->lzh, full_old, ZFS_TYPE_DATASET))
60828f16a0fSKyle Evans 		return (BE_ERR_UNKNOWN);
609bfe0869cSKyle Evans 		/* XXX TODO set and return correct error */
61028f16a0fSKyle Evans 
611bfe0869cSKyle Evans 	if (zfs_dataset_exists(lbh->lzh, full_new, ZFS_TYPE_DATASET))
61228f16a0fSKyle Evans 		return (BE_ERR_UNKNOWN);
613bfe0869cSKyle Evans 		/* XXX TODO set and return correct error */
61428f16a0fSKyle Evans 
615bfe0869cSKyle Evans 	/* XXX TODO
616bfe0869cSKyle Evans 	 * - What about mounted BEs?
617bfe0869cSKyle Evans 	 * - if mounted error out unless a force flag is set?
618bfe0869cSKyle Evans 	 */
61928f16a0fSKyle Evans 	if ((zfs_hdl = zfs_open(lbh->lzh, full_old,
620bfe0869cSKyle Evans 	    ZFS_TYPE_FILESYSTEM)) == NULL)
62128f16a0fSKyle Evans 		return (BE_ERR_UNKNOWN);
622bfe0869cSKyle Evans 		/* XXX TODO set and return correct error */
62328f16a0fSKyle Evans 
62428f16a0fSKyle Evans 
625bfe0869cSKyle Evans 	/* recurse, nounmount, forceunmount */
62628f16a0fSKyle Evans 	struct renameflags flags = { 0, 0, 0 };
62728f16a0fSKyle Evans 
628bfe0869cSKyle Evans 	/* XXX TODO: error log on this call */
62928f16a0fSKyle Evans 	err = zfs_rename(zfs_hdl, NULL, full_new, flags);
63028f16a0fSKyle Evans 
63128f16a0fSKyle Evans 	zfs_close(zfs_hdl);
63228f16a0fSKyle Evans 
63328f16a0fSKyle Evans 	return (set_error(lbh, err));
63428f16a0fSKyle Evans }
63528f16a0fSKyle Evans 
63628f16a0fSKyle Evans 
63728f16a0fSKyle Evans int
63828f16a0fSKyle Evans be_export(libbe_handle_t *lbh, char *bootenv, int fd)
63928f16a0fSKyle Evans {
64028f16a0fSKyle Evans 	char snap_name[BE_MAXPATHLEN];
64128f16a0fSKyle Evans 	char buf[BE_MAXPATHLEN];
64228f16a0fSKyle Evans 	zfs_handle_t *zfs;
64328f16a0fSKyle Evans 	int err;
64428f16a0fSKyle Evans 
645b29bf2f8SKyle Evans 	if ((err = be_snapshot(lbh, bootenv, NULL, true, snap_name)) != 0)
646bfe0869cSKyle Evans 		/* XXX TODO error handle */
64728f16a0fSKyle Evans 		return (-1);
64828f16a0fSKyle Evans 
64928f16a0fSKyle Evans 	be_root_concat(lbh, snap_name, buf);
65028f16a0fSKyle Evans 
651bfe0869cSKyle Evans 	if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL)
65228f16a0fSKyle Evans 		return (BE_ERR_ZFSOPEN);
65328f16a0fSKyle Evans 
65428f16a0fSKyle Evans 	err = zfs_send_one(zfs, NULL, fd, 0);
65528f16a0fSKyle Evans 	return (err);
65628f16a0fSKyle Evans }
65728f16a0fSKyle Evans 
65828f16a0fSKyle Evans 
65928f16a0fSKyle Evans int
66028f16a0fSKyle Evans be_import(libbe_handle_t *lbh, char *bootenv, int fd)
66128f16a0fSKyle Evans {
66228f16a0fSKyle Evans 	char buf[BE_MAXPATHLEN];
66328f16a0fSKyle Evans 	time_t rawtime;
66428f16a0fSKyle Evans 	nvlist_t *props;
66528f16a0fSKyle Evans 	zfs_handle_t *zfs;
66628f16a0fSKyle Evans 	int err, len;
66728f16a0fSKyle Evans 
668bfe0869cSKyle Evans 	/*
669bfe0869cSKyle Evans 	 * XXX TODO: this is a very likely name for someone to already have
670bfe0869cSKyle Evans 	 * used... we should avoid it.
671bfe0869cSKyle Evans 	 */
672d06f7103SKyle Evans 	if ((err = be_root_concat(lbh, "libbe_import_temp", buf)) != 0)
673bfe0869cSKyle Evans 		/* XXX TODO error handle */
67428f16a0fSKyle Evans 		return (-1);
67528f16a0fSKyle Evans 
67628f16a0fSKyle Evans 	time(&rawtime);
67728f16a0fSKyle Evans 	len = strlen(buf);
67828f16a0fSKyle Evans 	strftime(buf + len, BE_MAXPATHLEN - len,
67928f16a0fSKyle Evans 	    "@%F-%T", localtime(&rawtime));
68028f16a0fSKyle Evans 
681bfe0869cSKyle Evans 	/* lzc_receive(SNAPNAME, PROPS, ORIGIN, FORCE, fd)) { */
682b29bf2f8SKyle Evans 	if ((err = lzc_receive(buf, NULL, NULL, false, fd)) != 0) {
68328f16a0fSKyle Evans 		/* TODO: go through libzfs_core's recv_impl and find returned
68428f16a0fSKyle Evans 		 * errors and set appropriate BE_ERR
68528f16a0fSKyle Evans 		 * edit: errors are not in libzfs_core, my assumption is
68628f16a0fSKyle Evans 		 *  that they use libzfs errors
687bfe0869cSKyle Evans 		 * note: 17 is err for dataset already existing
688bfe0869cSKyle Evans 		 */
68928f16a0fSKyle Evans 		return (err);
69028f16a0fSKyle Evans 	}
69128f16a0fSKyle Evans 
692bfe0869cSKyle Evans 	if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT)) == NULL)
693bfe0869cSKyle Evans 		/* XXX TODO correct error */
69428f16a0fSKyle Evans 		return (-1);
69528f16a0fSKyle Evans 
69628f16a0fSKyle Evans 	nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
69728f16a0fSKyle Evans 	nvlist_add_string(props, "canmount", "noauto");
69828f16a0fSKyle Evans 	nvlist_add_string(props, "mountpoint", "/");
69928f16a0fSKyle Evans 
70028f16a0fSKyle Evans 	be_root_concat(lbh, bootenv, buf);
70128f16a0fSKyle Evans 
70228f16a0fSKyle Evans 	err = zfs_clone(zfs, buf, props);
70328f16a0fSKyle Evans 	zfs_close(zfs);
70428f16a0fSKyle Evans 
70528f16a0fSKyle Evans 	nvlist_free(props);
70628f16a0fSKyle Evans 
707bfe0869cSKyle Evans 	/* XXX TODO: recursively delete be_import_temp dataset */
70828f16a0fSKyle Evans 	return (err);
70928f16a0fSKyle Evans }
71028f16a0fSKyle Evans 
71128f16a0fSKyle Evans 
71228f16a0fSKyle Evans int
71328f16a0fSKyle Evans be_add_child(libbe_handle_t *lbh, char *child_path, bool cp_if_exists)
71428f16a0fSKyle Evans {
71528f16a0fSKyle Evans 	char active[BE_MAXPATHLEN];
71628f16a0fSKyle Evans 	char buf[BE_MAXPATHLEN];
71728f16a0fSKyle Evans 	nvlist_t *props;
71828f16a0fSKyle Evans 	zfs_handle_t *zfs;
71928f16a0fSKyle Evans 	struct stat sb;
72028f16a0fSKyle Evans 	int err;
72128f16a0fSKyle Evans 
72228f16a0fSKyle Evans 	/* Require absolute paths */
723bfe0869cSKyle Evans 	if (*child_path != '/')
724bfe0869cSKyle Evans 		/* XXX TODO: create appropriate error */
72528f16a0fSKyle Evans 		return (-1);
72628f16a0fSKyle Evans 
72728f16a0fSKyle Evans 	strncpy(active, be_active_path(lbh), BE_MAXPATHLEN);
72828f16a0fSKyle Evans 	strcpy(buf, active);
72928f16a0fSKyle Evans 
73028f16a0fSKyle Evans 	/* Create non-mountable parent dataset(s) */
73128f16a0fSKyle Evans 	char *s = child_path;
73228f16a0fSKyle Evans 	for (char *p; (p = strchr(s+1, '/')) != NULL; s = p) {
73328f16a0fSKyle Evans 		size_t len = p - s;
73428f16a0fSKyle Evans 		strncat(buf, s, len);
73528f16a0fSKyle Evans 
73628f16a0fSKyle Evans 		nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
73728f16a0fSKyle Evans 		nvlist_add_string(props, "canmount", "off");
73828f16a0fSKyle Evans 		nvlist_add_string(props, "mountpoint", "none");
73928f16a0fSKyle Evans 		zfs_create(lbh->lzh, buf, ZFS_TYPE_DATASET, props);
74028f16a0fSKyle Evans 		nvlist_free(props);
74128f16a0fSKyle Evans 	}
74228f16a0fSKyle Evans 
74328f16a0fSKyle Evans 
74428f16a0fSKyle Evans 	/* Path does not exist as a descendent of / yet */
74528f16a0fSKyle Evans 	int pos = strlen(active);
74628f16a0fSKyle Evans 
747bfe0869cSKyle Evans 	/* XXX TODO: Verify that resulting str is less than BE_MAXPATHLEN */
74828f16a0fSKyle Evans 	strncpy(&active[pos], child_path, BE_MAXPATHLEN-pos);
74928f16a0fSKyle Evans 
75028f16a0fSKyle Evans 	if (stat(child_path, &sb) != 0) {
75128f16a0fSKyle Evans 		/* Verify that error is ENOENT */
752bfe0869cSKyle Evans 		if (errno != 2)
753bfe0869cSKyle Evans 			/* XXX TODO: create appropriate error */
75428f16a0fSKyle Evans 			return (-1);
75528f16a0fSKyle Evans 
75628f16a0fSKyle Evans 		nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
75728f16a0fSKyle Evans 		nvlist_add_string(props, "canmount", "noauto");
75828f16a0fSKyle Evans 		nvlist_add_string(props, "mountpoint", child_path);
75928f16a0fSKyle Evans 
760bfe0869cSKyle Evans 		/* Create */
761b29bf2f8SKyle Evans 		if ((err =
762b29bf2f8SKyle Evans 		    zfs_create(lbh->lzh, active, ZFS_TYPE_DATASET, props)) != 0)
763bfe0869cSKyle Evans 			/* XXX TODO handle error */
76428f16a0fSKyle Evans 			return (-1);
76528f16a0fSKyle Evans 		nvlist_free(props);
76628f16a0fSKyle Evans 
76728f16a0fSKyle Evans 		if ((zfs =
768bfe0869cSKyle Evans 		    zfs_open(lbh->lzh, active, ZFS_TYPE_DATASET)) == NULL)
769bfe0869cSKyle Evans 			/* XXX TODO handle error */
77028f16a0fSKyle Evans 			return (-1);
77128f16a0fSKyle Evans 
772bfe0869cSKyle Evans 		/* Set props */
773b29bf2f8SKyle Evans 		if ((err = zfs_prop_set(zfs, "canmount", "noauto")) != 0)
77428f16a0fSKyle Evans 			/* TODO handle error */
77528f16a0fSKyle Evans 			return (-1);
77628f16a0fSKyle Evans 	} else if (cp_if_exists) {
77728f16a0fSKyle Evans 		/* Path is already a descendent of / and should be copied */
77828f16a0fSKyle Evans 
779bfe0869cSKyle Evans 		/* XXX TODO ? */
78028f16a0fSKyle Evans 
78128f16a0fSKyle Evans 		/*
78228f16a0fSKyle Evans 		 * Establish if the existing path is a zfs dataset or just
78328f16a0fSKyle Evans 		 * the subdirectory of one
78428f16a0fSKyle Evans 		 */
78528f16a0fSKyle Evans 
786bfe0869cSKyle Evans 		/* XXX TODO: use mktemp */
78728f16a0fSKyle Evans 		long int snap_name = random();
78828f16a0fSKyle Evans 
78928f16a0fSKyle Evans 		snprintf(buf, BE_MAXPATHLEN, "%s@%ld", child_path, snap_name);
79028f16a0fSKyle Evans 
791b29bf2f8SKyle Evans 		if ((err = zfs_snapshot(lbh->lzh, buf, false, NULL)) != 0)
792bfe0869cSKyle Evans 			/* XXX TODO correct error */
79328f16a0fSKyle Evans 			return (-1);
79428f16a0fSKyle Evans 
795bfe0869cSKyle Evans 		/* Clone */
79628f16a0fSKyle Evans 		if ((zfs =
797bfe0869cSKyle Evans 		    zfs_open(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT)) == NULL)
798bfe0869cSKyle Evans 			/* XXX TODO correct error */
79928f16a0fSKyle Evans 			return (-1);
80028f16a0fSKyle Evans 
801b29bf2f8SKyle Evans 		if ((err = zfs_clone(zfs, active, NULL)) != 0)
802bfe0869cSKyle Evans 			/* XXX TODO correct error */
80328f16a0fSKyle Evans 			return (-1);
80428f16a0fSKyle Evans 
805bfe0869cSKyle Evans 		/* set props */
806bfe0869cSKyle Evans 	} else
80728f16a0fSKyle Evans 		/* TODO: error code for exists, but not cp? */
80828f16a0fSKyle Evans 		return (-1);
80928f16a0fSKyle Evans 
81028f16a0fSKyle Evans 	return (BE_ERR_SUCCESS);
81128f16a0fSKyle Evans }
81228f16a0fSKyle Evans 
813d06f7103SKyle Evans static int
814d06f7103SKyle Evans be_set_nextboot(libbe_handle_t *lbh, nvlist_t *config, uint64_t pool_guid,
815d06f7103SKyle Evans     const char *zfsdev)
816d06f7103SKyle Evans {
817d06f7103SKyle Evans 	nvlist_t **child;
818d06f7103SKyle Evans 	uint64_t vdev_guid;
819d06f7103SKyle Evans 	int c, children;
820d06f7103SKyle Evans 
821d06f7103SKyle Evans 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, &child,
822d06f7103SKyle Evans 	    &children) == 0) {
823d06f7103SKyle Evans 		for (c = 0; c < children; ++c)
824d06f7103SKyle Evans 			if (be_set_nextboot(lbh, child[c], pool_guid, zfsdev) != 0)
825d06f7103SKyle Evans 				return (1);
826d06f7103SKyle Evans 		return (0);
827d06f7103SKyle Evans 	}
828d06f7103SKyle Evans 
829d06f7103SKyle Evans 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
830d06f7103SKyle Evans 	    &vdev_guid) != 0) {
831d06f7103SKyle Evans 		return (1);
832d06f7103SKyle Evans 	}
833d06f7103SKyle Evans 
834d06f7103SKyle Evans 	if (zpool_nextboot(lbh->lzh, pool_guid, vdev_guid, zfsdev) != 0) {
835d06f7103SKyle Evans 		perror("ZFS_IOC_NEXTBOOT failed");
836d06f7103SKyle Evans 		return (1);
837d06f7103SKyle Evans 	}
838d06f7103SKyle Evans 
839d06f7103SKyle Evans 	return (0);
840d06f7103SKyle Evans }
841d06f7103SKyle Evans 
84228f16a0fSKyle Evans 
84328f16a0fSKyle Evans int
84428f16a0fSKyle Evans be_activate(libbe_handle_t *lbh, char *bootenv, bool temporary)
84528f16a0fSKyle Evans {
84628f16a0fSKyle Evans 	char be_path[BE_MAXPATHLEN];
84728f16a0fSKyle Evans 	char buf[BE_MAXPATHLEN];
84828f16a0fSKyle Evans 	uint64_t pool_guid;
849d06f7103SKyle Evans 	nvlist_t *config, *vdevs;
85028f16a0fSKyle Evans 	int err;
85128f16a0fSKyle Evans 
85228f16a0fSKyle Evans 	be_root_concat(lbh, bootenv, be_path);
85328f16a0fSKyle Evans 
85428f16a0fSKyle Evans 	/* Note: be_exists fails if mountpoint is not / */
855bfe0869cSKyle Evans 	if (!be_exists(lbh, be_path))
85628f16a0fSKyle Evans 		return (BE_ERR_NOENT);
85728f16a0fSKyle Evans 
85828f16a0fSKyle Evans 	if (temporary) {
859d06f7103SKyle Evans 		config = zpool_get_config(lbh->active_phandle, NULL);
860d06f7103SKyle Evans 		if (config == NULL) {
861d06f7103SKyle Evans 			printf("no config\n");
86228f16a0fSKyle Evans 			return (1);
86328f16a0fSKyle Evans 		}
86428f16a0fSKyle Evans 
865d06f7103SKyle Evans 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
866d06f7103SKyle Evans 		    &pool_guid) != 0)
867d06f7103SKyle Evans 			return (1);
868d06f7103SKyle Evans 
86928f16a0fSKyle Evans 		/* Expected format according to zfsbootcfg(8) man */
87028f16a0fSKyle Evans 		strcpy(buf, "zfs:");
87128f16a0fSKyle Evans 		strcat(buf, be_path);
87228f16a0fSKyle Evans 		strcat(buf, ":");
87328f16a0fSKyle Evans 
874d06f7103SKyle Evans 		if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &vdevs) != 0)
87528f16a0fSKyle Evans 			return (1);
87628f16a0fSKyle Evans 
877d06f7103SKyle Evans 		return (be_set_nextboot(lbh, vdevs, pool_guid, buf));
87828f16a0fSKyle Evans 	} else {
87928f16a0fSKyle Evans 		/* Obtain bootenv zpool */
880c3a34c08SKyle Evans 		err = zpool_set_prop(lbh->active_phandle, "bootfs", be_path);
88128f16a0fSKyle Evans 
88228f16a0fSKyle Evans 		switch (err) {
88328f16a0fSKyle Evans 		case 0:
88428f16a0fSKyle Evans 			return (BE_ERR_SUCCESS);
88528f16a0fSKyle Evans 
88628f16a0fSKyle Evans 		default:
887bfe0869cSKyle Evans 			/* XXX TODO correct errors */
88828f16a0fSKyle Evans 			return (-1);
88928f16a0fSKyle Evans 		}
89028f16a0fSKyle Evans 	}
89128f16a0fSKyle Evans }
892