xref: /freebsd/lib/libbe/be.c (revision d3d381b2b194b4d24853e92eecef55f262688d1a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 
35 #include <ctype.h>
36 #include <kenv.h>
37 #include <libgen.h>
38 #include <libzfs_core.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <time.h>
42 #include <unistd.h>
43 
44 #include "be.h"
45 #include "be_impl.h"
46 
47 #if SOON
48 static int be_create_child_noent(libbe_handle_t *lbh, const char *active,
49     const char *child_path);
50 static int be_create_child_cloned(libbe_handle_t *lbh, const char *active);
51 #endif
52 
53 /*
54  * Iterator function for locating the rootfs amongst the children of the
55  * zfs_be_root set by loader(8).  data is expected to be a libbe_handle_t *.
56  */
57 static int
58 be_locate_rootfs(zfs_handle_t *chkds, void *data)
59 {
60 	libbe_handle_t *lbh;
61 	char *mntpoint;
62 
63 	lbh = (libbe_handle_t *)data;
64 	if (lbh == NULL)
65 		return (1);
66 
67 	mntpoint = NULL;
68 	if (zfs_is_mounted(chkds, &mntpoint) && strcmp(mntpoint, "/") == 0) {
69 		strlcpy(lbh->rootfs, zfs_get_name(chkds), sizeof(lbh->rootfs));
70 		free(mntpoint);
71 		return (1);
72 	} else if(mntpoint != NULL)
73 		free(mntpoint);
74 
75 	return (0);
76 }
77 
78 /*
79  * Initializes the libbe context to operate in the root boot environment
80  * dataset, for example, zroot/ROOT.
81  */
82 libbe_handle_t *
83 libbe_init(void)
84 {
85 	struct stat sb;
86 	dev_t root_dev, boot_dev;
87 	libbe_handle_t *lbh;
88 	zfs_handle_t *rootds;
89 	char *poolname, *pos;
90 	int pnamelen;
91 
92 	lbh = NULL;
93 	poolname = pos = NULL;
94 	rootds = NULL;
95 
96 	/* Verify that /boot and / are mounted on the same filesystem */
97 	/* TODO: use errno here?? */
98 	if (stat("/", &sb) != 0)
99 		goto err;
100 
101 	root_dev = sb.st_dev;
102 
103 	if (stat("/boot", &sb) != 0)
104 		goto err;
105 
106 	boot_dev = sb.st_dev;
107 
108 	if (root_dev != boot_dev) {
109 		fprintf(stderr, "/ and /boot not on same device, quitting\n");
110 		goto err;
111 	}
112 
113 	if ((lbh = calloc(1, sizeof(libbe_handle_t))) == NULL)
114 		goto err;
115 
116 	if ((lbh->lzh = libzfs_init()) == NULL)
117 		goto err;
118 
119 	/* Obtain path to boot environment root */
120 	if ((kenv(KENV_GET, "zfs_be_root", lbh->root,
121 	    sizeof(lbh->root))) == -1)
122 		goto err;
123 
124 	/* Remove leading 'zfs:' if present, otherwise use value as-is */
125 	if (strcmp(lbh->root, "zfs:") == 0)
126 		strlcpy(lbh->root, strchr(lbh->root, ':') + sizeof(char),
127 		    sizeof(lbh->root));
128 
129 	if ((pos = strchr(lbh->root, '/')) == NULL)
130 		goto err;
131 
132 	pnamelen = pos - lbh->root;
133 	poolname = malloc(pnamelen + 1);
134 	if (poolname == NULL)
135 		goto err;
136 
137 	strlcpy(poolname, lbh->root, pnamelen + 1);
138 	if ((lbh->active_phandle = zpool_open(lbh->lzh, poolname)) == NULL)
139 		goto err;
140 	free(poolname);
141 	poolname = NULL;
142 
143 	if (zpool_get_prop(lbh->active_phandle, ZPOOL_PROP_BOOTFS, lbh->bootfs,
144 	    sizeof(lbh->bootfs), NULL, true) != 0)
145 		goto err;
146 
147 	/* Obtain path to boot environment rootfs (currently booted) */
148 	/* XXX Get dataset mounted at / by kenv/GUID from mountroot? */
149 	if ((rootds = zfs_open(lbh->lzh, lbh->root, ZFS_TYPE_DATASET)) == NULL)
150 		goto err;
151 
152 	zfs_iter_filesystems(rootds, be_locate_rootfs, lbh);
153 	zfs_close(rootds);
154 	rootds = NULL;
155 	if (*lbh->rootfs == '\0')
156 		goto err;
157 
158 	return (lbh);
159 err:
160 	if (lbh != NULL) {
161 		if (lbh->active_phandle != NULL)
162 			zpool_close(lbh->active_phandle);
163 		if (lbh->lzh != NULL)
164 			libzfs_fini(lbh->lzh);
165 		free(lbh);
166 	}
167 	free(poolname);
168 	return (NULL);
169 }
170 
171 
172 /*
173  * Free memory allocated by libbe_init()
174  */
175 void
176 libbe_close(libbe_handle_t *lbh)
177 {
178 
179 	if (lbh->active_phandle != NULL)
180 		zpool_close(lbh->active_phandle);
181 	libzfs_fini(lbh->lzh);
182 	free(lbh);
183 }
184 
185 /*
186  * Proxy through to libzfs for the moment.
187  */
188 void
189 be_nicenum(uint64_t num, char *buf, size_t buflen)
190 {
191 
192 	zfs_nicenum(num, buf, buflen);
193 }
194 
195 static int
196 be_destroy_cb(zfs_handle_t *zfs_hdl, void *data)
197 {
198 	int err;
199 
200 	if ((err = zfs_iter_children(zfs_hdl, be_destroy_cb, data)) != 0)
201 		return (err);
202 	if ((err = zfs_destroy(zfs_hdl, false)) != 0)
203 		return (err);
204 	return (0);
205 }
206 
207 /*
208  * Destroy the boot environment or snapshot specified by the name
209  * parameter. Options are or'd together with the possible values:
210  * BE_DESTROY_FORCE : forces operation on mounted datasets
211  */
212 int
213 be_destroy(libbe_handle_t *lbh, const char *name, int options)
214 {
215 	zfs_handle_t *fs;
216 	char path[BE_MAXPATHLEN];
217 	char *p;
218 	int err, force, mounted;
219 
220 	p = path;
221 	force = options & BE_DESTROY_FORCE;
222 
223 	be_root_concat(lbh, name, path);
224 
225 	if (strchr(name, '@') == NULL) {
226 		if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_FILESYSTEM))
227 			return (set_error(lbh, BE_ERR_NOENT));
228 
229 		if (strcmp(path, lbh->rootfs) == 0)
230 			return (set_error(lbh, BE_ERR_DESTROYACT));
231 
232 		fs = zfs_open(lbh->lzh, p, ZFS_TYPE_FILESYSTEM);
233 	} else {
234 
235 		if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_SNAPSHOT))
236 			return (set_error(lbh, BE_ERR_NOENT));
237 
238 		fs = zfs_open(lbh->lzh, p, ZFS_TYPE_SNAPSHOT);
239 	}
240 
241 	if (fs == NULL)
242 		return (set_error(lbh, BE_ERR_ZFSOPEN));
243 
244 	/* Check if mounted, unmount if force is specified */
245 	if ((mounted = zfs_is_mounted(fs, NULL)) != 0) {
246 		if (force)
247 			zfs_unmount(fs, NULL, 0);
248 		else
249 			return (set_error(lbh, BE_ERR_DESTROYMNT));
250 	}
251 
252 	if ((err = be_destroy_cb(fs, NULL)) != 0) {
253 		/* Children are still present or the mount is referenced */
254 		if (err == EBUSY)
255 			return (set_error(lbh, BE_ERR_DESTROYMNT));
256 		return (set_error(lbh, BE_ERR_UNKNOWN));
257 	}
258 
259 	return (0);
260 }
261 
262 
263 int
264 be_snapshot(libbe_handle_t *lbh, const char *source, const char *snap_name,
265     bool recursive, char *result)
266 {
267 	char buf[BE_MAXPATHLEN];
268 	time_t rawtime;
269 	int len, err;
270 
271 	be_root_concat(lbh, source, buf);
272 
273 	if ((err = be_exists(lbh, buf)) != 0)
274 		return (set_error(lbh, err));
275 
276 	if (snap_name != NULL) {
277 		if (strlcat(buf, "@", sizeof(buf)) >= sizeof(buf))
278 			return (set_error(lbh, BE_ERR_INVALIDNAME));
279 
280 		if (strlcat(buf, snap_name, sizeof(buf)) >= sizeof(buf))
281 			return (set_error(lbh, BE_ERR_INVALIDNAME));
282 
283 		if (result != NULL)
284 			snprintf(result, BE_MAXPATHLEN, "%s@%s", source,
285 			    snap_name);
286 	} else {
287 		time(&rawtime);
288 		len = strlen(buf);
289 		strftime(buf + len, sizeof(buf) - len,
290 		    "@%F-%T", localtime(&rawtime));
291 		if (result != NULL && strlcpy(result, strrchr(buf, '/') + 1,
292 		    sizeof(buf)) >= sizeof(buf))
293 			return (set_error(lbh, BE_ERR_INVALIDNAME));
294 	}
295 
296 	if ((err = zfs_snapshot(lbh->lzh, buf, recursive, NULL)) != 0) {
297 		switch (err) {
298 		case EZFS_INVALIDNAME:
299 			return (set_error(lbh, BE_ERR_INVALIDNAME));
300 
301 		default:
302 			/*
303 			 * The other errors that zfs_ioc_snapshot might return
304 			 * shouldn't happen if we've set things up properly, so
305 			 * we'll gloss over them and call it UNKNOWN as it will
306 			 * require further triage.
307 			 */
308 			if (errno == ENOTSUP)
309 				return (set_error(lbh, BE_ERR_NOPOOL));
310 			return (set_error(lbh, BE_ERR_UNKNOWN));
311 		}
312 	}
313 
314 	return (BE_ERR_SUCCESS);
315 }
316 
317 
318 /*
319  * Create the boot environment specified by the name parameter
320  */
321 int
322 be_create(libbe_handle_t *lbh, const char *name)
323 {
324 	int err;
325 
326 	err = be_create_from_existing(lbh, name, be_active_path(lbh));
327 
328 	return (set_error(lbh, err));
329 }
330 
331 
332 static int
333 be_deep_clone_prop(int prop, void *cb)
334 {
335 	int err;
336         struct libbe_dccb *dccb;
337 	zprop_source_t src;
338 	char pval[BE_MAXPATHLEN];
339 	char source[BE_MAXPATHLEN];
340 
341 	dccb = cb;
342 	/* Skip some properties we don't want to touch */
343 	if (prop == ZFS_PROP_CANMOUNT)
344 		return (ZPROP_CONT);
345 
346 	/* Don't copy readonly properties */
347 	if (zfs_prop_readonly(prop))
348 		return (ZPROP_CONT);
349 
350 	if ((err = zfs_prop_get(dccb->zhp, prop, (char *)&pval,
351 	    sizeof(pval), &src, (char *)&source, sizeof(source), false)))
352 		/* Just continue if we fail to read a property */
353 		return (ZPROP_CONT);
354 
355 	/* Only copy locally defined properties */
356 	if (src != ZPROP_SRC_LOCAL)
357 		return (ZPROP_CONT);
358 
359 	nvlist_add_string(dccb->props, zfs_prop_to_name(prop), (char *)pval);
360 
361 	return (ZPROP_CONT);
362 }
363 
364 static int
365 be_deep_clone(zfs_handle_t *ds, void *data)
366 {
367 	int err;
368 	char be_path[BE_MAXPATHLEN];
369 	char snap_path[BE_MAXPATHLEN];
370 	const char *dspath;
371 	char *dsname;
372 	zfs_handle_t *snap_hdl;
373 	nvlist_t *props;
374 	struct libbe_deep_clone *isdc, sdc;
375 	struct libbe_dccb dccb;
376 
377 	isdc = (struct libbe_deep_clone *)data;
378 	dspath = zfs_get_name(ds);
379 	if ((dsname = strrchr(dspath, '/')) == NULL)
380 		return (BE_ERR_UNKNOWN);
381 	dsname++;
382 
383 	if (isdc->bename == NULL)
384 		snprintf(be_path, sizeof(be_path), "%s/%s", isdc->be_root, dsname);
385 	else
386 		snprintf(be_path, sizeof(be_path), "%s/%s", isdc->be_root, isdc->bename);
387 
388 	snprintf(snap_path, sizeof(snap_path), "%s@%s", dspath, isdc->snapname);
389 
390 	if (zfs_dataset_exists(isdc->lbh->lzh, be_path, ZFS_TYPE_DATASET))
391 		return (set_error(isdc->lbh, BE_ERR_EXISTS));
392 
393 	if ((snap_hdl =
394 	    zfs_open(isdc->lbh->lzh, snap_path, ZFS_TYPE_SNAPSHOT)) == NULL)
395 		return (set_error(isdc->lbh, BE_ERR_ZFSOPEN));
396 
397 	nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
398 	nvlist_add_string(props, "canmount", "noauto");
399 
400 	dccb.zhp = ds;
401 	dccb.props = props;
402 	if (zprop_iter(be_deep_clone_prop, &dccb, B_FALSE, B_FALSE,
403 	    ZFS_TYPE_FILESYSTEM) == ZPROP_INVAL)
404 		return (-1);
405 
406 	if ((err = zfs_clone(snap_hdl, be_path, props)) != 0)
407 		err = BE_ERR_ZFSCLONE;
408 
409 	nvlist_free(props);
410 	zfs_close(snap_hdl);
411 
412 	/* Failed to clone */
413 	if (err != BE_ERR_SUCCESS)
414 		return (set_error(isdc->lbh, err));
415 
416 	sdc.lbh = isdc->lbh;
417 	sdc.bename = NULL;
418 	sdc.snapname = isdc->snapname;
419 	sdc.be_root = (char *)&be_path;
420 
421 	err = zfs_iter_filesystems(ds, be_deep_clone, &sdc);
422 
423 	return (err);
424 }
425 
426 /*
427  * Create the boot environment from pre-existing snapshot
428  */
429 int
430 be_create_from_existing_snap(libbe_handle_t *lbh, const char *name,
431     const char *snap)
432 {
433 	int err;
434 	char be_path[BE_MAXPATHLEN];
435 	char snap_path[BE_MAXPATHLEN];
436 	const char *bename;
437 	char *parentname, *snapname;
438 	zfs_handle_t *parent_hdl;
439 	struct libbe_deep_clone sdc;
440 
441 	if ((err = be_validate_name(lbh, name)) != 0)
442 		return (set_error(lbh, err));
443 	if ((err = be_root_concat(lbh, snap, snap_path)) != 0)
444 		return (set_error(lbh, err));
445 	if ((err = be_validate_snap(lbh, snap_path)) != 0)
446 		return (set_error(lbh, err));
447 
448 	if ((err = be_root_concat(lbh, name, be_path)) != 0)
449 		return (set_error(lbh, err));
450 
451 	if ((bename = strrchr(name, '/')) == NULL)
452 		bename = name;
453 	else
454 		bename++;
455 
456 	if ((parentname = strdup(snap_path)) == NULL)
457 		return (set_error(lbh, BE_ERR_UNKNOWN));
458 
459 	snapname = strchr(parentname, '@');
460 	if (snapname == NULL) {
461 		free(parentname);
462 		return (set_error(lbh, BE_ERR_UNKNOWN));
463 	}
464 	*snapname = '\0';
465 	snapname++;
466 
467 	sdc.lbh = lbh;
468 	sdc.bename = bename;
469 	sdc.snapname = snapname;
470 	sdc.be_root = lbh->root;
471 
472 	parent_hdl = zfs_open(lbh->lzh, parentname, ZFS_TYPE_DATASET);
473 	err = be_deep_clone(parent_hdl, &sdc);
474 
475 	free(parentname);
476 	return (set_error(lbh, err));
477 }
478 
479 
480 /*
481  * Create a boot environment from an existing boot environment
482  */
483 int
484 be_create_from_existing(libbe_handle_t *lbh, const char *name, const char *old)
485 {
486 	int err;
487 	char buf[BE_MAXPATHLEN];
488 
489 	if ((err = be_snapshot(lbh, old, NULL, true, (char *)&buf)) != 0)
490 		return (set_error(lbh, err));
491 
492 	err = be_create_from_existing_snap(lbh, name, (char *)buf);
493 
494 	return (set_error(lbh, err));
495 }
496 
497 
498 /*
499  * Verifies that a snapshot has a valid name, exists, and has a mountpoint of
500  * '/'. Returns BE_ERR_SUCCESS (0), upon success, or the relevant BE_ERR_* upon
501  * failure. Does not set the internal library error state.
502  */
503 int
504 be_validate_snap(libbe_handle_t *lbh, const char *snap_name)
505 {
506 	zfs_handle_t *zfs_hdl;
507 	char buf[BE_MAXPATHLEN];
508 	char *delim_pos;
509 	int err = BE_ERR_SUCCESS;
510 
511 	if (strlen(snap_name) >= BE_MAXPATHLEN)
512 		return (BE_ERR_PATHLEN);
513 
514 	if (!zfs_dataset_exists(lbh->lzh, snap_name,
515 	    ZFS_TYPE_SNAPSHOT))
516 		return (BE_ERR_NOENT);
517 
518 	strlcpy(buf, snap_name, sizeof(buf));
519 
520 	/* Find the base filesystem of the snapshot */
521 	if ((delim_pos = strchr(buf, '@')) == NULL)
522 		return (BE_ERR_INVALIDNAME);
523 	*delim_pos = '\0';
524 
525 	if ((zfs_hdl =
526 	    zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL)
527 		return (BE_ERR_NOORIGIN);
528 
529 	if ((err = zfs_prop_get(zfs_hdl, ZFS_PROP_MOUNTPOINT, buf,
530 	    sizeof(buf), NULL, NULL, 0, 1)) != 0)
531 		err = BE_ERR_BADMOUNT;
532 
533 	if ((err != 0) && (strncmp(buf, "/", sizeof(buf)) != 0))
534 		err = BE_ERR_BADMOUNT;
535 
536 	zfs_close(zfs_hdl);
537 
538 	return (err);
539 }
540 
541 
542 /*
543  * Idempotently appends the name argument to the root boot environment path
544  * and copies the resulting string into the result buffer (which is assumed
545  * to be at least BE_MAXPATHLEN characters long. Returns BE_ERR_SUCCESS upon
546  * success, BE_ERR_PATHLEN if the resulting path is longer than BE_MAXPATHLEN,
547  * or BE_ERR_INVALIDNAME if the name is a path that does not begin with
548  * zfs_be_root. Does not set internal library error state.
549  */
550 int
551 be_root_concat(libbe_handle_t *lbh, const char *name, char *result)
552 {
553 	size_t name_len, root_len;
554 
555 	name_len = strlen(name);
556 	root_len = strlen(lbh->root);
557 
558 	/* Act idempotently; return be name if it is already a full path */
559 	if (strrchr(name, '/') != NULL) {
560 		if (strstr(name, lbh->root) != name)
561 			return (BE_ERR_INVALIDNAME);
562 
563 		if (name_len >= BE_MAXPATHLEN)
564 			return (BE_ERR_PATHLEN);
565 
566 		strlcpy(result, name, BE_MAXPATHLEN);
567 		return (BE_ERR_SUCCESS);
568 	} else if (name_len + root_len + 1 < BE_MAXPATHLEN) {
569 		snprintf(result, BE_MAXPATHLEN, "%s/%s", lbh->root,
570 		    name);
571 		return (BE_ERR_SUCCESS);
572 	}
573 
574 	return (BE_ERR_PATHLEN);
575 }
576 
577 
578 /*
579  * Verifies the validity of a boot environment name (A-Za-z0-9-_.). Returns
580  * BE_ERR_SUCCESS (0) if name is valid, otherwise returns BE_ERR_INVALIDNAME
581  * or BE_ERR_PATHLEN.
582  * Does not set internal library error state.
583  */
584 int
585 be_validate_name(libbe_handle_t *lbh, const char *name)
586 {
587 	for (int i = 0; *name; i++) {
588 		char c = *(name++);
589 		if (isalnum(c) || (c == '-') || (c == '_') || (c == '.'))
590 			continue;
591 		return (BE_ERR_INVALIDNAME);
592 	}
593 
594 	/*
595 	 * Impose the additional restriction that the entire dataset name must
596 	 * not exceed the maximum length of a dataset, i.e. MAXNAMELEN.
597 	 */
598 	if (strlen(lbh->root) + 1 + strlen(name) > MAXNAMELEN)
599 		return (BE_ERR_PATHLEN);
600 	return (BE_ERR_SUCCESS);
601 }
602 
603 
604 /*
605  * usage
606  */
607 int
608 be_rename(libbe_handle_t *lbh, const char *old, const char *new)
609 {
610 	char full_old[BE_MAXPATHLEN];
611 	char full_new[BE_MAXPATHLEN];
612 	zfs_handle_t *zfs_hdl;
613 	int err;
614 
615 	/*
616 	 * be_validate_name is documented not to set error state, so we should
617 	 * do so here.
618 	 */
619 	if ((err = be_validate_name(lbh, new)) != 0)
620 		return (set_error(lbh, err));
621 	if ((err = be_root_concat(lbh, old, full_old)) != 0)
622 		return (set_error(lbh, err));
623 	if ((err = be_root_concat(lbh, new, full_new)) != 0)
624 		return (set_error(lbh, err));
625 
626 	if (!zfs_dataset_exists(lbh->lzh, full_old, ZFS_TYPE_DATASET))
627 		return (set_error(lbh, BE_ERR_NOENT));
628 
629 	if (zfs_dataset_exists(lbh->lzh, full_new, ZFS_TYPE_DATASET))
630 		return (set_error(lbh, BE_ERR_EXISTS));
631 
632 	if ((zfs_hdl = zfs_open(lbh->lzh, full_old,
633 	    ZFS_TYPE_FILESYSTEM)) == NULL)
634 		return (set_error(lbh, BE_ERR_ZFSOPEN));
635 
636 	/* recurse, nounmount, forceunmount */
637 	struct renameflags flags = {
638 		.nounmount = 1,
639 	};
640 
641 	err = zfs_rename(zfs_hdl, NULL, full_new, flags);
642 
643 	zfs_close(zfs_hdl);
644 	if (err != 0)
645 		return (set_error(lbh, BE_ERR_UNKNOWN));
646 	return (0);
647 }
648 
649 
650 int
651 be_export(libbe_handle_t *lbh, const char *bootenv, int fd)
652 {
653 	char snap_name[BE_MAXPATHLEN];
654 	char buf[BE_MAXPATHLEN];
655 	zfs_handle_t *zfs;
656 	int err;
657 
658 	if ((err = be_snapshot(lbh, bootenv, NULL, true, snap_name)) != 0)
659 		/* Use the error set by be_snapshot */
660 		return (err);
661 
662 	be_root_concat(lbh, snap_name, buf);
663 
664 	if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL)
665 		return (set_error(lbh, BE_ERR_ZFSOPEN));
666 
667 	err = zfs_send_one(zfs, NULL, fd, 0);
668 	zfs_close(zfs);
669 
670 	return (err);
671 }
672 
673 
674 int
675 be_import(libbe_handle_t *lbh, const char *bootenv, int fd)
676 {
677 	char buf[BE_MAXPATHLEN];
678 	time_t rawtime;
679 	nvlist_t *props;
680 	zfs_handle_t *zfs;
681 	int err, len;
682 	char nbuf[24];
683 
684 	/*
685 	 * We don't need this to be incredibly random, just unique enough that
686 	 * it won't conflict with an existing dataset name.  Chopping time
687 	 * down to 32 bits is probably good enough for this.
688 	 */
689 	snprintf(nbuf, 24, "tmp%u",
690 	    (uint32_t)(time(NULL) & 0xFFFFFFFF));
691 	if ((err = be_root_concat(lbh, nbuf, buf)) != 0)
692 		/*
693 		 * Technically this is our problem, but we try to use short
694 		 * enough names that we won't run into problems except in
695 		 * worst-case BE root approaching MAXPATHLEN.
696 		 */
697 		return (set_error(lbh, BE_ERR_PATHLEN));
698 
699 	time(&rawtime);
700 	len = strlen(buf);
701 	strftime(buf + len, sizeof(buf) - len, "@%F-%T", localtime(&rawtime));
702 
703 	if ((err = lzc_receive(buf, NULL, NULL, false, fd)) != 0) {
704 		switch (err) {
705 		case EINVAL:
706 			return (set_error(lbh, BE_ERR_NOORIGIN));
707 		case ENOENT:
708 			return (set_error(lbh, BE_ERR_NOENT));
709 		case EIO:
710 			return (set_error(lbh, BE_ERR_IO));
711 		default:
712 			return (set_error(lbh, BE_ERR_UNKNOWN));
713 		}
714 	}
715 
716 	if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT)) == NULL)
717 		return (set_error(lbh, BE_ERR_ZFSOPEN));
718 
719 	nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
720 	nvlist_add_string(props, "canmount", "noauto");
721 	nvlist_add_string(props, "mountpoint", "/");
722 
723 	be_root_concat(lbh, bootenv, buf);
724 
725 	err = zfs_clone(zfs, buf, props);
726 	zfs_close(zfs);
727 	nvlist_free(props);
728 
729 	if (err != 0)
730 		return (set_error(lbh, BE_ERR_UNKNOWN));
731 
732 	/*
733 	 * Finally, we open up the dataset we just cloned the snapshot so that
734 	 * we may promote it.  This is necessary in order to clean up the ghost
735 	 * snapshot that doesn't need to be seen after the operation is
736 	 * complete.
737 	 */
738 	if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL)
739 		return (set_error(lbh, BE_ERR_ZFSOPEN));
740 
741 	err = zfs_promote(zfs);
742 	zfs_close(zfs);
743 
744 	if (err != 0)
745 		return (set_error(lbh, BE_ERR_UNKNOWN));
746 
747 	/* Clean up the temporary snapshot */
748 	return (be_destroy(lbh, nbuf, 0));
749 }
750 
751 #if SOON
752 static int
753 be_create_child_noent(libbe_handle_t *lbh, const char *active,
754     const char *child_path)
755 {
756 	nvlist_t *props;
757 	zfs_handle_t *zfs;
758 	int err;
759 
760 	nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
761 	nvlist_add_string(props, "canmount", "noauto");
762 	nvlist_add_string(props, "mountpoint", child_path);
763 
764 	/* Create */
765 	if ((err = zfs_create(lbh->lzh, active, ZFS_TYPE_DATASET,
766 	    props)) != 0) {
767 		switch (err) {
768 		case EZFS_EXISTS:
769 			return (set_error(lbh, BE_ERR_EXISTS));
770 		case EZFS_NOENT:
771 			return (set_error(lbh, BE_ERR_NOENT));
772 		case EZFS_BADTYPE:
773 		case EZFS_BADVERSION:
774 			return (set_error(lbh, BE_ERR_NOPOOL));
775 		case EZFS_BADPROP:
776 		default:
777 			/* We set something up wrong, probably... */
778 			return (set_error(lbh, BE_ERR_UNKNOWN));
779 		}
780 	}
781 	nvlist_free(props);
782 
783 	if ((zfs = zfs_open(lbh->lzh, active, ZFS_TYPE_DATASET)) == NULL)
784 		return (set_error(lbh, BE_ERR_ZFSOPEN));
785 
786 	/* Set props */
787 	if ((err = zfs_prop_set(zfs, "canmount", "noauto")) != 0) {
788 		zfs_close(zfs);
789 		/*
790 		 * Similar to other cases, this shouldn't fail unless we've
791 		 * done something wrong.  This is a new dataset that shouldn't
792 		 * have been mounted anywhere between creation and now.
793 		 */
794 		if (err == EZFS_NOMEM)
795 			return (set_error(lbh, BE_ERR_NOMEM));
796 		return (set_error(lbh, BE_ERR_UNKNOWN));
797 	}
798 	zfs_close(zfs);
799 	return (BE_ERR_SUCCESS);
800 }
801 
802 static int
803 be_create_child_cloned(libbe_handle_t *lbh, const char *active)
804 {
805 	char buf[BE_MAXPATHLEN], tmp[BE_MAXPATHLEN];;
806 	zfs_handle_t *zfs;
807 	int err;
808 
809 	/* XXX TODO ? */
810 
811 	/*
812 	 * Establish if the existing path is a zfs dataset or just
813 	 * the subdirectory of one
814 	 */
815 	strlcpy(tmp, "tmp/be_snap.XXXXX", sizeof(tmp));
816 	if (mktemp(tmp) == NULL)
817 		return (set_error(lbh, BE_ERR_UNKNOWN));
818 
819 	be_root_concat(lbh, tmp, buf);
820 	printf("Here %s?\n", buf);
821 	if ((err = zfs_snapshot(lbh->lzh, buf, false, NULL)) != 0) {
822 		switch (err) {
823 		case EZFS_INVALIDNAME:
824 			return (set_error(lbh, BE_ERR_INVALIDNAME));
825 
826 		default:
827 			/*
828 			 * The other errors that zfs_ioc_snapshot might return
829 			 * shouldn't happen if we've set things up properly, so
830 			 * we'll gloss over them and call it UNKNOWN as it will
831 			 * require further triage.
832 			 */
833 			if (errno == ENOTSUP)
834 				return (set_error(lbh, BE_ERR_NOPOOL));
835 			return (set_error(lbh, BE_ERR_UNKNOWN));
836 		}
837 	}
838 
839 	/* Clone */
840 	if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT)) == NULL)
841 		return (BE_ERR_ZFSOPEN);
842 
843 	if ((err = zfs_clone(zfs, active, NULL)) != 0)
844 		/* XXX TODO correct error */
845 		return (set_error(lbh, BE_ERR_UNKNOWN));
846 
847 	/* set props */
848 	zfs_close(zfs);
849 	return (BE_ERR_SUCCESS);
850 }
851 
852 int
853 be_add_child(libbe_handle_t *lbh, const char *child_path, bool cp_if_exists)
854 {
855 	struct stat sb;
856 	char active[BE_MAXPATHLEN], buf[BE_MAXPATHLEN];
857 	nvlist_t *props;
858 	const char *s;
859 
860 	/* Require absolute paths */
861 	if (*child_path != '/')
862 		return (set_error(lbh, BE_ERR_BADPATH));
863 
864 	strlcpy(active, be_active_path(lbh), BE_MAXPATHLEN);
865 	strcpy(buf, active);
866 
867 	/* Create non-mountable parent dataset(s) */
868 	s = child_path;
869 	for (char *p; (p = strchr(s+1, '/')) != NULL; s = p) {
870 		size_t len = p - s;
871 		strncat(buf, s, len);
872 
873 		nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
874 		nvlist_add_string(props, "canmount", "off");
875 		nvlist_add_string(props, "mountpoint", "none");
876 		zfs_create(lbh->lzh, buf, ZFS_TYPE_DATASET, props);
877 		nvlist_free(props);
878 	}
879 
880 	/* Path does not exist as a descendent of / yet */
881 	if (strlcat(active, child_path, BE_MAXPATHLEN) >= BE_MAXPATHLEN)
882 		return (set_error(lbh, BE_ERR_PATHLEN));
883 
884 	if (stat(child_path, &sb) != 0) {
885 		/* Verify that error is ENOENT */
886 		if (errno != ENOENT)
887 			return (set_error(lbh, BE_ERR_UNKNOWN));
888 		return (be_create_child_noent(lbh, active, child_path));
889 	} else if (cp_if_exists)
890 		/* Path is already a descendent of / and should be copied */
891 		return (be_create_child_cloned(lbh, active));
892 	return (set_error(lbh, BE_ERR_EXISTS));
893 }
894 #endif	/* SOON */
895 
896 static int
897 be_set_nextboot(libbe_handle_t *lbh, nvlist_t *config, uint64_t pool_guid,
898     const char *zfsdev)
899 {
900 	nvlist_t **child;
901 	uint64_t vdev_guid;
902 	int c, children;
903 
904 	if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, &child,
905 	    &children) == 0) {
906 		for (c = 0; c < children; ++c)
907 			if (be_set_nextboot(lbh, child[c], pool_guid, zfsdev) != 0)
908 				return (1);
909 		return (0);
910 	}
911 
912 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
913 	    &vdev_guid) != 0) {
914 		return (1);
915 	}
916 
917 	if (zpool_nextboot(lbh->lzh, pool_guid, vdev_guid, zfsdev) != 0) {
918 		perror("ZFS_IOC_NEXTBOOT failed");
919 		return (1);
920 	}
921 
922 	return (0);
923 }
924 
925 
926 int
927 be_activate(libbe_handle_t *lbh, const char *bootenv, bool temporary)
928 {
929 	char be_path[BE_MAXPATHLEN];
930 	char buf[BE_MAXPATHLEN];
931 	uint64_t pool_guid;
932 	nvlist_t *config, *vdevs;
933 	int err;
934 
935 	be_root_concat(lbh, bootenv, be_path);
936 
937 	/* Note: be_exists fails if mountpoint is not / */
938 	if ((err = be_exists(lbh, be_path)) != 0)
939 		return (set_error(lbh, err));
940 
941 	if (temporary) {
942 		config = zpool_get_config(lbh->active_phandle, NULL);
943 		if (config == NULL)
944 			/* config should be fetchable... */
945 			return (set_error(lbh, BE_ERR_UNKNOWN));
946 
947 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
948 		    &pool_guid) != 0)
949 			/* Similarly, it shouldn't be possible */
950 			return (set_error(lbh, BE_ERR_UNKNOWN));
951 
952 		/* Expected format according to zfsbootcfg(8) man */
953 		snprintf(buf, sizeof(buf), "zfs:%s:", be_path);
954 
955 		/* We have no config tree */
956 		if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
957 		    &vdevs) != 0)
958 			return (set_error(lbh, BE_ERR_NOPOOL));
959 
960 		return (be_set_nextboot(lbh, vdevs, pool_guid, buf));
961 	} else {
962 		/* Obtain bootenv zpool */
963 		err = zpool_set_prop(lbh->active_phandle, "bootfs", be_path);
964 
965 		switch (err) {
966 		case 0:
967 			return (BE_ERR_SUCCESS);
968 
969 		default:
970 			/* XXX TODO correct errors */
971 			return (-1);
972 		}
973 	}
974 }
975