]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libbe/be.c
Merge ^/vendor/llvm/dist up to its last change, and resolve conflicts.
[FreeBSD/FreeBSD.git] / lib / libbe / be.c
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/param.h>
33 #include <sys/mount.h>
34 #include <sys/stat.h>
35 #include <sys/ucred.h>
36 #include <sys/queue.h>
37 #include <sys/zfs_context.h>
38 #include <sys/mntent.h>
39
40 #include <ctype.h>
41 #include <libgen.h>
42 #include <libzfs_core.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <time.h>
46 #include <unistd.h>
47
48 #include "be.h"
49 #include "be_impl.h"
50
51 struct promote_entry {
52         char                            name[BE_MAXPATHLEN];
53         SLIST_ENTRY(promote_entry)      link;
54 };
55
56 struct be_destroy_data {
57         libbe_handle_t                  *lbh;
58         char                            target_name[BE_MAXPATHLEN];
59         char                            *snapname;
60         SLIST_HEAD(, promote_entry)     promotelist;
61 };
62
63 #if SOON
64 static int be_create_child_noent(libbe_handle_t *lbh, const char *active,
65     const char *child_path);
66 static int be_create_child_cloned(libbe_handle_t *lbh, const char *active);
67 #endif
68
69 /* Arbitrary... should tune */
70 #define BE_SNAP_SERIAL_MAX      1024
71
72 /*
73  * Iterator function for locating the rootfs amongst the children of the
74  * zfs_be_root set by loader(8).  data is expected to be a libbe_handle_t *.
75  */
76 static int
77 be_locate_rootfs(libbe_handle_t *lbh)
78 {
79         struct statfs sfs;
80         struct mnttab entry;
81         zfs_handle_t *zfs;
82
83         /*
84          * Check first if root is ZFS; if not, we'll bail on rootfs capture.
85          * Unfortunately needed because zfs_path_to_zhandle will emit to
86          * stderr if / isn't actually a ZFS filesystem, which we'd like
87          * to avoid.
88          */
89         if (statfs("/", &sfs) == 0) {
90                 statfs2mnttab(&sfs, &entry);
91                 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
92                         return (1);
93         } else
94                 return (1);
95         zfs = zfs_path_to_zhandle(lbh->lzh, "/", ZFS_TYPE_FILESYSTEM);
96         if (zfs == NULL)
97                 return (1);
98
99         strlcpy(lbh->rootfs, zfs_get_name(zfs), sizeof(lbh->rootfs));
100         zfs_close(zfs);
101         return (0);
102 }
103
104 /*
105  * Initializes the libbe context to operate in the root boot environment
106  * dataset, for example, zroot/ROOT.
107  */
108 libbe_handle_t *
109 libbe_init(const char *root)
110 {
111         char altroot[MAXPATHLEN];
112         libbe_handle_t *lbh;
113         char *poolname, *pos;
114         int pnamelen;
115
116         lbh = NULL;
117         poolname = pos = NULL;
118
119         if ((lbh = calloc(1, sizeof(libbe_handle_t))) == NULL)
120                 goto err;
121
122         if ((lbh->lzh = libzfs_init()) == NULL)
123                 goto err;
124
125         /*
126          * Grab rootfs, we'll work backwards from there if an optional BE root
127          * has not been passed in.
128          */
129         if (be_locate_rootfs(lbh) != 0) {
130                 if (root == NULL)
131                         goto err;
132                 *lbh->rootfs = '\0';
133         }
134         if (root == NULL) {
135                 /* Strip off the final slash from rootfs to get the be root */
136                 strlcpy(lbh->root, lbh->rootfs, sizeof(lbh->root));
137                 pos = strrchr(lbh->root, '/');
138                 if (pos == NULL)
139                         goto err;
140                 *pos = '\0';
141         } else
142                 strlcpy(lbh->root, root, sizeof(lbh->root));
143
144         if ((pos = strchr(lbh->root, '/')) == NULL)
145                 goto err;
146
147         pnamelen = pos - lbh->root;
148         poolname = malloc(pnamelen + 1);
149         if (poolname == NULL)
150                 goto err;
151
152         strlcpy(poolname, lbh->root, pnamelen + 1);
153         if ((lbh->active_phandle = zpool_open(lbh->lzh, poolname)) == NULL)
154                 goto err;
155         free(poolname);
156         poolname = NULL;
157
158         if (zpool_get_prop(lbh->active_phandle, ZPOOL_PROP_BOOTFS, lbh->bootfs,
159             sizeof(lbh->bootfs), NULL, true) != 0)
160                 goto err;
161
162         if (zpool_get_prop(lbh->active_phandle, ZPOOL_PROP_ALTROOT,
163             altroot, sizeof(altroot), NULL, true) == 0 &&
164             strcmp(altroot, "-") != 0)
165                 lbh->altroot_len = strlen(altroot);
166
167         return (lbh);
168 err:
169         if (lbh != NULL) {
170                 if (lbh->active_phandle != NULL)
171                         zpool_close(lbh->active_phandle);
172                 if (lbh->lzh != NULL)
173                         libzfs_fini(lbh->lzh);
174                 free(lbh);
175         }
176         free(poolname);
177         return (NULL);
178 }
179
180
181 /*
182  * Free memory allocated by libbe_init()
183  */
184 void
185 libbe_close(libbe_handle_t *lbh)
186 {
187
188         if (lbh->active_phandle != NULL)
189                 zpool_close(lbh->active_phandle);
190         libzfs_fini(lbh->lzh);
191         free(lbh);
192 }
193
194 /*
195  * Proxy through to libzfs for the moment.
196  */
197 void
198 be_nicenum(uint64_t num, char *buf, size_t buflen)
199 {
200
201         zfs_nicenum(num, buf, buflen);
202 }
203
204 static bool
205 be_should_promote_clones(zfs_handle_t *zfs_hdl, struct be_destroy_data *bdd)
206 {
207         char *atpos;
208
209         if (zfs_get_type(zfs_hdl) != ZFS_TYPE_SNAPSHOT)
210                 return (false);
211
212         /*
213          * If we're deleting a snapshot, we need to make sure we only promote
214          * clones that are derived from one of the snapshots we're deleting,
215          * rather than that of a snapshot we're not touching.  This keeps stuff
216          * in a consistent state, making sure that we don't error out unless
217          * we really need to.
218          */
219         if (bdd->snapname == NULL)
220                 return (true);
221
222         atpos = strchr(zfs_get_name(zfs_hdl), '@');
223         return (strcmp(atpos + 1, bdd->snapname) == 0);
224 }
225
226 /*
227  * This is executed from be_promote_dependent_clones via zfs_iter_dependents,
228  * It checks if the dependent type is a snapshot then attempts to find any
229  * clones associated with it. Any clones not related to the destroy target are
230  * added to the promote list.
231  */
232 static int
233 be_dependent_clone_cb(zfs_handle_t *zfs_hdl, void *data)
234 {
235         int err;
236         bool found;
237         char *name;
238         struct nvlist *nvl;
239         struct nvpair *nvp;
240         struct be_destroy_data *bdd;
241         struct promote_entry *entry, *newentry;
242
243         nvp = NULL;
244         err = 0;
245         bdd = (struct be_destroy_data *)data;
246
247         if (be_should_promote_clones(zfs_hdl, bdd) &&
248             (nvl = zfs_get_clones_nvl(zfs_hdl)) != NULL) {
249                 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
250                         name = nvpair_name(nvp);
251
252                         /*
253                          * Skip if the clone is equal to, or a child of, the
254                          * destroy target.
255                          */
256                         if (strncmp(name, bdd->target_name,
257                             strlen(bdd->target_name)) == 0 ||
258                             strstr(name, bdd->target_name) == name) {
259                                 continue;
260                         }
261
262                         found = false;
263                         SLIST_FOREACH(entry, &bdd->promotelist, link) {
264                                 if (strcmp(entry->name, name) == 0) {
265                                         found = true;
266                                         break;
267                                 }
268                         }
269
270                         if (found)
271                                 continue;
272
273                         newentry = malloc(sizeof(struct promote_entry));
274                         if (newentry == NULL) {
275                                 err = ENOMEM;
276                                 break;
277                         }
278
279 #define BE_COPY_NAME(entry, src)        \
280         strlcpy((entry)->name, (src), sizeof((entry)->name))
281                         if (BE_COPY_NAME(newentry, name) >=
282                             sizeof(newentry->name)) {
283                                 /* Shouldn't happen. */
284                                 free(newentry);
285                                 err = ENAMETOOLONG;
286                                 break;
287                         }
288 #undef BE_COPY_NAME
289
290                         /*
291                          * We're building up a SLIST here to make sure both that
292                          * we get the order right and so that we don't
293                          * inadvertently observe the wrong state by promoting
294                          * datasets while we're still walking the tree.  The
295                          * latter can lead to situations where we promote a BE
296                          * then effectively demote it again.
297                          */
298                         SLIST_INSERT_HEAD(&bdd->promotelist, newentry, link);
299                 }
300                 nvlist_free(nvl);
301         }
302         zfs_close(zfs_hdl);
303         return (err);
304 }
305
306 /*
307  * This is called before a destroy, so that any datasets(environments) that are
308  * dependent on this one get promoted before destroying the target.
309  */
310 static int
311 be_promote_dependent_clones(zfs_handle_t *zfs_hdl, struct be_destroy_data *bdd)
312 {
313         int err;
314         zfs_handle_t *clone;
315         struct promote_entry *entry;
316
317         snprintf(bdd->target_name, BE_MAXPATHLEN, "%s/", zfs_get_name(zfs_hdl));
318         err = zfs_iter_dependents(zfs_hdl, true, be_dependent_clone_cb, bdd);
319
320         /*
321          * Drain the list and walk away from it if we're only deleting a
322          * snapshot.
323          */
324         if (bdd->snapname != NULL && !SLIST_EMPTY(&bdd->promotelist))
325                 err = BE_ERR_HASCLONES;
326         while (!SLIST_EMPTY(&bdd->promotelist)) {
327                 entry = SLIST_FIRST(&bdd->promotelist);
328                 SLIST_REMOVE_HEAD(&bdd->promotelist, link);
329
330 #define ZFS_GRAB_CLONE()        \
331         zfs_open(bdd->lbh->lzh, entry->name, ZFS_TYPE_FILESYSTEM)
332                 /*
333                  * Just skip this part on error, we still want to clean up the
334                  * promotion list after the first error.  We'll then preserve it
335                  * all the way back.
336                  */
337                 if (err == 0 && (clone = ZFS_GRAB_CLONE()) != NULL) {
338                         err = zfs_promote(clone);
339                         if (err != 0)
340                                 err = BE_ERR_DESTROYMNT;
341                         zfs_close(clone);
342                 }
343 #undef ZFS_GRAB_CLONE
344                 free(entry);
345         }
346
347         return (err);
348 }
349
350 static int
351 be_destroy_cb(zfs_handle_t *zfs_hdl, void *data)
352 {
353         char path[BE_MAXPATHLEN];
354         struct be_destroy_data *bdd;
355         zfs_handle_t *snap;
356         int err;
357
358         bdd = (struct be_destroy_data *)data;
359         if (bdd->snapname == NULL) {
360                 err = zfs_iter_children(zfs_hdl, be_destroy_cb, data);
361                 if (err != 0)
362                         return (err);
363                 return (zfs_destroy(zfs_hdl, false));
364         }
365         /* If we're dealing with snapshots instead, delete that one alone */
366         err = zfs_iter_filesystems(zfs_hdl, be_destroy_cb, data);
367         if (err != 0)
368                 return (err);
369         /*
370          * This part is intentionally glossing over any potential errors,
371          * because there's a lot less potential for errors when we're cleaning
372          * up snapshots rather than a full deep BE.  The primary error case
373          * here being if the snapshot doesn't exist in the first place, which
374          * the caller will likely deem insignificant as long as it doesn't
375          * exist after the call.  Thus, such a missing snapshot shouldn't jam
376          * up the destruction.
377          */
378         snprintf(path, sizeof(path), "%s@%s", zfs_get_name(zfs_hdl),
379             bdd->snapname);
380         if (!zfs_dataset_exists(bdd->lbh->lzh, path, ZFS_TYPE_SNAPSHOT))
381                 return (0);
382         snap = zfs_open(bdd->lbh->lzh, path, ZFS_TYPE_SNAPSHOT);
383         if (snap != NULL)
384                 zfs_destroy(snap, false);
385         return (0);
386 }
387
388 #define BE_DESTROY_WANTORIGIN   (BE_DESTROY_ORIGIN | BE_DESTROY_AUTOORIGIN)
389 /*
390  * Destroy the boot environment or snapshot specified by the name
391  * parameter. Options are or'd together with the possible values:
392  * BE_DESTROY_FORCE : forces operation on mounted datasets
393  * BE_DESTROY_ORIGIN: destroy the origin snapshot as well
394  */
395 static int
396 be_destroy_internal(libbe_handle_t *lbh, const char *name, int options,
397     bool odestroyer)
398 {
399         struct be_destroy_data bdd;
400         char origin[BE_MAXPATHLEN], path[BE_MAXPATHLEN];
401         zfs_handle_t *fs;
402         char *snapdelim;
403         int err, force, mounted;
404         size_t rootlen;
405
406         bdd.lbh = lbh;
407         bdd.snapname = NULL;
408         SLIST_INIT(&bdd.promotelist);
409         force = options & BE_DESTROY_FORCE;
410         *origin = '\0';
411
412         be_root_concat(lbh, name, path);
413
414         if ((snapdelim = strchr(path, '@')) == NULL) {
415                 if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_FILESYSTEM))
416                         return (set_error(lbh, BE_ERR_NOENT));
417
418                 if (strcmp(path, lbh->rootfs) == 0 ||
419                     strcmp(path, lbh->bootfs) == 0)
420                         return (set_error(lbh, BE_ERR_DESTROYACT));
421
422                 fs = zfs_open(lbh->lzh, path, ZFS_TYPE_FILESYSTEM);
423                 if (fs == NULL)
424                         return (set_error(lbh, BE_ERR_ZFSOPEN));
425
426                 /* Don't destroy a mounted dataset unless force is specified */
427                 if ((mounted = zfs_is_mounted(fs, NULL)) != 0) {
428                         if (force) {
429                                 zfs_unmount(fs, NULL, 0);
430                         } else {
431                                 free(bdd.snapname);
432                                 return (set_error(lbh, BE_ERR_DESTROYMNT));
433                         }
434                 }
435         } else {
436                 /*
437                  * If we're initially destroying a snapshot, origin options do
438                  * not make sense.  If we're destroying the origin snapshot of
439                  * a BE, we want to maintain the options in case we need to
440                  * fake success after failing to promote.
441                  */
442                 if (!odestroyer)
443                         options &= ~BE_DESTROY_WANTORIGIN;
444                 if (!zfs_dataset_exists(lbh->lzh, path, ZFS_TYPE_SNAPSHOT))
445                         return (set_error(lbh, BE_ERR_NOENT));
446
447                 bdd.snapname = strdup(snapdelim + 1);
448                 if (bdd.snapname == NULL)
449                         return (set_error(lbh, BE_ERR_NOMEM));
450                 *snapdelim = '\0';
451                 fs = zfs_open(lbh->lzh, path, ZFS_TYPE_DATASET);
452                 if (fs == NULL) {
453                         free(bdd.snapname);
454                         return (set_error(lbh, BE_ERR_ZFSOPEN));
455                 }
456         }
457
458         /*
459          * Whether we're destroying a BE or a single snapshot, we need to walk
460          * the tree of what we're going to destroy and promote everything in our
461          * path so that we can make it happen.
462          */
463         if ((err = be_promote_dependent_clones(fs, &bdd)) != 0) {
464                 free(bdd.snapname);
465
466                 /*
467                  * If we're just destroying the origin of some other dataset
468                  * we were invoked to destroy, then we just ignore
469                  * BE_ERR_HASCLONES and return success unless the caller wanted
470                  * to force the issue.
471                  */
472                 if (odestroyer && err == BE_ERR_HASCLONES &&
473                     (options & BE_DESTROY_AUTOORIGIN) != 0)
474                         return (0);
475                 return (set_error(lbh, err));
476         }
477
478         /*
479          * This was deferred until after we promote all of the derivatives so
480          * that we grab the new origin after everything's settled down.
481          */
482         if ((options & BE_DESTROY_WANTORIGIN) != 0 &&
483             zfs_prop_get(fs, ZFS_PROP_ORIGIN, origin, sizeof(origin),
484             NULL, NULL, 0, 1) != 0 &&
485             (options & BE_DESTROY_ORIGIN) != 0)
486                 return (set_error(lbh, BE_ERR_NOORIGIN));
487
488         /*
489          * If the caller wants auto-origin destruction and the origin
490          * name matches one of our automatically created snapshot names
491          * (i.e. strftime("%F-%T") with a serial at the end), then
492          * we'll set the DESTROY_ORIGIN flag and nuke it
493          * be_is_auto_snapshot_name is exported from libbe(3) so that
494          * the caller can determine if it needs to warn about the origin
495          * not being destroyed or not.
496          */
497         if ((options & BE_DESTROY_AUTOORIGIN) != 0 && *origin != '\0' &&
498             be_is_auto_snapshot_name(lbh, origin))
499                 options |= BE_DESTROY_ORIGIN;
500
501         err = be_destroy_cb(fs, &bdd);
502         zfs_close(fs);
503         free(bdd.snapname);
504         if (err != 0) {
505                 /* Children are still present or the mount is referenced */
506                 if (err == EBUSY)
507                         return (set_error(lbh, BE_ERR_DESTROYMNT));
508                 return (set_error(lbh, BE_ERR_UNKNOWN));
509         }
510
511         if ((options & BE_DESTROY_ORIGIN) == 0)
512                 return (0);
513
514         /* The origin can't possibly be shorter than the BE root */
515         rootlen = strlen(lbh->root);
516         if (*origin == '\0' || strlen(origin) <= rootlen + 1)
517                 return (set_error(lbh, BE_ERR_INVORIGIN));
518
519         /*
520          * We'll be chopping off the BE root and running this back through
521          * be_destroy, so that we properly handle the origin snapshot whether
522          * it be that of a deep BE or not.
523          */
524         if (strncmp(origin, lbh->root, rootlen) != 0 || origin[rootlen] != '/')
525                 return (0);
526
527         return (be_destroy_internal(lbh, origin + rootlen + 1,
528             options & ~BE_DESTROY_ORIGIN, true));
529 }
530
531 int
532 be_destroy(libbe_handle_t *lbh, const char *name, int options)
533 {
534
535         /*
536          * The consumer must not set both BE_DESTROY_AUTOORIGIN and
537          * BE_DESTROY_ORIGIN.  Internally, we'll set the latter from the former.
538          * The latter should imply that we must succeed at destroying the
539          * origin, or complain otherwise.
540          */
541         if ((options & BE_DESTROY_WANTORIGIN) == BE_DESTROY_WANTORIGIN)
542                 return (set_error(lbh, BE_ERR_UNKNOWN));
543         return (be_destroy_internal(lbh, name, options, false));
544 }
545
546 static void
547 be_setup_snapshot_name(libbe_handle_t *lbh, char *buf, size_t buflen)
548 {
549         time_t rawtime;
550         int len, serial;
551
552         time(&rawtime);
553         len = strlen(buf);
554         len += strftime(buf + len, buflen - len, "@%F-%T", localtime(&rawtime));
555         /* No room for serial... caller will do its best */
556         if (buflen - len < 2)
557                 return;
558
559         for (serial = 0; serial < BE_SNAP_SERIAL_MAX; ++serial) {
560                 snprintf(buf + len, buflen - len, "-%d", serial);
561                 if (!zfs_dataset_exists(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT))
562                         return;
563         }
564 }
565
566 bool
567 be_is_auto_snapshot_name(libbe_handle_t *lbh, const char *name)
568 {
569         const char *snap;
570         int day, hour, minute, month, second, serial, year;
571
572         if ((snap = strchr(name, '@')) == NULL)
573                 return (false);
574         ++snap;
575         /* We'll grab the individual components and do some light validation. */
576         if (sscanf(snap, "%d-%d-%d-%d:%d:%d-%d", &year, &month, &day, &hour,
577             &minute, &second, &serial) != 7)
578                 return (false);
579         return (year >= 1970) && (month >= 1 && month <= 12) &&
580             (day >= 1 && day <= 31) && (hour >= 0 && hour <= 23) &&
581             (minute >= 0 && minute <= 59) && (second >= 0 && second <= 60) &&
582             serial >= 0;
583 }
584
585 int
586 be_snapshot(libbe_handle_t *lbh, const char *source, const char *snap_name,
587     bool recursive, char *result)
588 {
589         char buf[BE_MAXPATHLEN];
590         int err;
591
592         be_root_concat(lbh, source, buf);
593
594         if ((err = be_exists(lbh, buf)) != 0)
595                 return (set_error(lbh, err));
596
597         if (snap_name != NULL) {
598                 if (strlcat(buf, "@", sizeof(buf)) >= sizeof(buf))
599                         return (set_error(lbh, BE_ERR_INVALIDNAME));
600
601                 if (strlcat(buf, snap_name, sizeof(buf)) >= sizeof(buf))
602                         return (set_error(lbh, BE_ERR_INVALIDNAME));
603
604                 if (result != NULL)
605                         snprintf(result, BE_MAXPATHLEN, "%s@%s", source,
606                             snap_name);
607         } else {
608                 be_setup_snapshot_name(lbh, buf, sizeof(buf));
609
610                 if (result != NULL && strlcpy(result, strrchr(buf, '/') + 1,
611                     sizeof(buf)) >= sizeof(buf))
612                         return (set_error(lbh, BE_ERR_INVALIDNAME));
613         }
614         if ((err = zfs_snapshot(lbh->lzh, buf, recursive, NULL)) != 0) {
615                 switch (err) {
616                 case EZFS_INVALIDNAME:
617                         return (set_error(lbh, BE_ERR_INVALIDNAME));
618
619                 default:
620                         /*
621                          * The other errors that zfs_ioc_snapshot might return
622                          * shouldn't happen if we've set things up properly, so
623                          * we'll gloss over them and call it UNKNOWN as it will
624                          * require further triage.
625                          */
626                         if (errno == ENOTSUP)
627                                 return (set_error(lbh, BE_ERR_NOPOOL));
628                         return (set_error(lbh, BE_ERR_UNKNOWN));
629                 }
630         }
631
632         return (BE_ERR_SUCCESS);
633 }
634
635
636 /*
637  * Create the boot environment specified by the name parameter
638  */
639 int
640 be_create(libbe_handle_t *lbh, const char *name)
641 {
642         int err;
643
644         err = be_create_from_existing(lbh, name, be_active_path(lbh));
645
646         return (set_error(lbh, err));
647 }
648
649 static int
650 be_deep_clone_prop(int prop, void *cb)
651 {
652         int err;
653         struct libbe_dccb *dccb;
654         zprop_source_t src;
655         char pval[BE_MAXPATHLEN];
656         char source[BE_MAXPATHLEN];
657         char *val;
658
659         dccb = cb;
660         /* Skip some properties we don't want to touch */
661         if (prop == ZFS_PROP_CANMOUNT)
662                 return (ZPROP_CONT);
663
664         /* Don't copy readonly properties */
665         if (zfs_prop_readonly(prop))
666                 return (ZPROP_CONT);
667
668         if ((err = zfs_prop_get(dccb->zhp, prop, (char *)&pval,
669             sizeof(pval), &src, (char *)&source, sizeof(source), false)))
670                 /* Just continue if we fail to read a property */
671                 return (ZPROP_CONT);
672
673         /*
674          * Only copy locally defined or received properties.  This continues
675          * to avoid temporary/default/local properties intentionally without
676          * breaking received datasets.
677          */
678         if (src != ZPROP_SRC_LOCAL && src != ZPROP_SRC_RECEIVED)
679                 return (ZPROP_CONT);
680
681         /* Augment mountpoint with altroot, if needed */
682         val = pval;
683         if (prop == ZFS_PROP_MOUNTPOINT)
684                 val = be_mountpoint_augmented(dccb->lbh, val);
685
686         nvlist_add_string(dccb->props, zfs_prop_to_name(prop), val);
687
688         return (ZPROP_CONT);
689 }
690
691 /*
692  * Return the corresponding boot environment path for a given
693  * dataset path, the constructed path is placed in 'result'.
694  *
695  * example: say our new boot environment name is 'bootenv' and
696  *          the dataset path is 'zroot/ROOT/default/data/set'.
697  *
698  * result should produce: 'zroot/ROOT/bootenv/data/set'
699  */
700 static int
701 be_get_path(struct libbe_deep_clone *ldc, const char *dspath, char *result, int result_size)
702 {
703         char *pos;
704         char *child_dataset;
705
706         /* match the root path for the boot environments */
707         pos = strstr(dspath, ldc->lbh->root);
708
709         /* no match, different pools? */
710         if (pos == NULL)
711                 return (BE_ERR_BADPATH);
712
713         /* root path of the new boot environment */
714         snprintf(result, result_size, "%s/%s", ldc->lbh->root, ldc->bename);
715
716         /* gets us to the parent dataset, the +1 consumes a trailing slash */
717         pos += strlen(ldc->lbh->root) + 1;
718
719         /* skip the parent dataset */
720         if ((child_dataset = strchr(pos, '/')) != NULL)
721                 strlcat(result, child_dataset, result_size);
722
723         return (BE_ERR_SUCCESS);
724 }
725
726 static int
727 be_clone_cb(zfs_handle_t *ds, void *data)
728 {
729         int err;
730         char be_path[BE_MAXPATHLEN];
731         char snap_path[BE_MAXPATHLEN];
732         const char *dspath;
733         zfs_handle_t *snap_hdl;
734         nvlist_t *props;
735         struct libbe_deep_clone *ldc;
736         struct libbe_dccb dccb;
737
738         ldc = (struct libbe_deep_clone *)data;
739         dspath = zfs_get_name(ds);
740
741         snprintf(snap_path, sizeof(snap_path), "%s@%s", dspath, ldc->snapname);
742
743         /* construct the boot environment path from the dataset we're cloning */
744         if (be_get_path(ldc, dspath, be_path, sizeof(be_path)) != BE_ERR_SUCCESS)
745                 return (set_error(ldc->lbh, BE_ERR_UNKNOWN));
746
747         /* the dataset to be created (i.e. the boot environment) already exists */
748         if (zfs_dataset_exists(ldc->lbh->lzh, be_path, ZFS_TYPE_DATASET))
749                 return (set_error(ldc->lbh, BE_ERR_EXISTS));
750
751         /* no snapshot found for this dataset, silently skip it */
752         if (!zfs_dataset_exists(ldc->lbh->lzh, snap_path, ZFS_TYPE_SNAPSHOT))
753                 return (0);
754
755         if ((snap_hdl =
756             zfs_open(ldc->lbh->lzh, snap_path, ZFS_TYPE_SNAPSHOT)) == NULL)
757                 return (set_error(ldc->lbh, BE_ERR_ZFSOPEN));
758
759         nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
760         nvlist_add_string(props, "canmount", "noauto");
761
762         dccb.lbh = ldc->lbh;
763         dccb.zhp = ds;
764         dccb.props = props;
765         if (zprop_iter(be_deep_clone_prop, &dccb, B_FALSE, B_FALSE,
766             ZFS_TYPE_FILESYSTEM) == ZPROP_INVAL)
767                 return (-1);
768
769         if ((err = zfs_clone(snap_hdl, be_path, props)) != 0)
770                 return (set_error(ldc->lbh, BE_ERR_ZFSCLONE));
771
772         nvlist_free(props);
773         zfs_close(snap_hdl);
774
775         if (ldc->depth_limit == -1 || ldc->depth < ldc->depth_limit) {
776                 ldc->depth++;
777                 err = zfs_iter_filesystems(ds, be_clone_cb, ldc);
778                 ldc->depth--;
779         }
780
781         return (set_error(ldc->lbh, err));
782 }
783
784 /*
785  * Create a boot environment with a given name from a given snapshot.
786  * Snapshots can be in the format 'zroot/ROOT/default@snapshot' or
787  * 'default@snapshot'. In the latter case, 'default@snapshot' will be prepended
788  * with the root path that libbe was initailized with.
789 */
790 static int
791 be_clone(libbe_handle_t *lbh, const char *bename, const char *snapshot, int depth)
792 {
793         int err;
794         char snap_path[BE_MAXPATHLEN];
795         char *parentname, *snapname;
796         zfs_handle_t *parent_hdl;
797         struct libbe_deep_clone ldc;
798
799         /* ensure the boot environment name is valid */
800         if ((err = be_validate_name(lbh, bename)) != 0)
801                 return (set_error(lbh, err));
802
803         /*
804          * prepend the boot environment root path if we're
805          * given a partial snapshot name.
806          */
807         if ((err = be_root_concat(lbh, snapshot, snap_path)) != 0)
808                 return (set_error(lbh, err));
809
810         /* ensure the snapshot exists */
811         if ((err = be_validate_snap(lbh, snap_path)) != 0)
812                 return (set_error(lbh, err));
813
814         /* get a copy of the snapshot path so we can disect it */
815         if ((parentname = strdup(snap_path)) == NULL)
816                 return (set_error(lbh, BE_ERR_UNKNOWN));
817
818         /* split dataset name from snapshot name */
819         snapname = strchr(parentname, '@');
820         if (snapname == NULL) {
821                 free(parentname);
822                 return (set_error(lbh, BE_ERR_UNKNOWN));
823         }
824         *snapname = '\0';
825         snapname++;
826
827         /* set-up the boot environment */
828         ldc.lbh = lbh;
829         ldc.bename = bename;
830         ldc.snapname = snapname;
831         ldc.depth = 0;
832         ldc.depth_limit = depth;
833
834         /* the boot environment will be cloned from this dataset */
835         parent_hdl = zfs_open(lbh->lzh, parentname, ZFS_TYPE_DATASET);
836
837         /* create the boot environment */
838         err = be_clone_cb(parent_hdl, &ldc);
839
840         free(parentname);
841         return (set_error(lbh, err));
842 }
843
844 /*
845  * Create a boot environment from pre-existing snapshot, specifying a depth.
846  */
847 int be_create_depth(libbe_handle_t *lbh, const char *bename,
848                     const char *snap, int depth)
849 {
850         return (be_clone(lbh, bename, snap, depth));
851 }
852
853 /*
854  * Create the boot environment from pre-existing snapshot
855  */
856 int
857 be_create_from_existing_snap(libbe_handle_t *lbh, const char *bename,
858     const char *snap)
859 {
860         return (be_clone(lbh, bename, snap, -1));
861 }
862
863
864 /*
865  * Create a boot environment from an existing boot environment
866  */
867 int
868 be_create_from_existing(libbe_handle_t *lbh, const char *bename, const char *old)
869 {
870         int err;
871         char snap[BE_MAXPATHLEN];
872
873         if ((err = be_snapshot(lbh, old, NULL, true, snap)) != 0)
874                 return (set_error(lbh, err));
875
876         err = be_clone(lbh, bename, snap, -1);
877
878         return (set_error(lbh, err));
879 }
880
881
882 /*
883  * Verifies that a snapshot has a valid name, exists, and has a mountpoint of
884  * '/'. Returns BE_ERR_SUCCESS (0), upon success, or the relevant BE_ERR_* upon
885  * failure. Does not set the internal library error state.
886  */
887 int
888 be_validate_snap(libbe_handle_t *lbh, const char *snap_name)
889 {
890
891         if (strlen(snap_name) >= BE_MAXPATHLEN)
892                 return (BE_ERR_PATHLEN);
893
894         if (!zfs_name_valid(snap_name, ZFS_TYPE_SNAPSHOT))
895                 return (BE_ERR_INVALIDNAME);
896
897         if (!zfs_dataset_exists(lbh->lzh, snap_name,
898             ZFS_TYPE_SNAPSHOT))
899                 return (BE_ERR_NOENT);
900
901         return (BE_ERR_SUCCESS);
902 }
903
904
905 /*
906  * Idempotently appends the name argument to the root boot environment path
907  * and copies the resulting string into the result buffer (which is assumed
908  * to be at least BE_MAXPATHLEN characters long. Returns BE_ERR_SUCCESS upon
909  * success, BE_ERR_PATHLEN if the resulting path is longer than BE_MAXPATHLEN,
910  * or BE_ERR_INVALIDNAME if the name is a path that does not begin with
911  * zfs_be_root. Does not set internal library error state.
912  */
913 int
914 be_root_concat(libbe_handle_t *lbh, const char *name, char *result)
915 {
916         size_t name_len, root_len;
917
918         name_len = strlen(name);
919         root_len = strlen(lbh->root);
920
921         /* Act idempotently; return be name if it is already a full path */
922         if (strrchr(name, '/') != NULL) {
923                 if (strstr(name, lbh->root) != name)
924                         return (BE_ERR_INVALIDNAME);
925
926                 if (name_len >= BE_MAXPATHLEN)
927                         return (BE_ERR_PATHLEN);
928
929                 strlcpy(result, name, BE_MAXPATHLEN);
930                 return (BE_ERR_SUCCESS);
931         } else if (name_len + root_len + 1 < BE_MAXPATHLEN) {
932                 snprintf(result, BE_MAXPATHLEN, "%s/%s", lbh->root,
933                     name);
934                 return (BE_ERR_SUCCESS);
935         }
936
937         return (BE_ERR_PATHLEN);
938 }
939
940
941 /*
942  * Verifies the validity of a boot environment name (A-Za-z0-9-_.). Returns
943  * BE_ERR_SUCCESS (0) if name is valid, otherwise returns BE_ERR_INVALIDNAME
944  * or BE_ERR_PATHLEN.
945  * Does not set internal library error state.
946  */
947 int
948 be_validate_name(libbe_handle_t *lbh, const char *name)
949 {
950
951         /*
952          * Impose the additional restriction that the entire dataset name must
953          * not exceed the maximum length of a dataset, i.e. MAXNAMELEN.
954          */
955         if (strlen(lbh->root) + 1 + strlen(name) > MAXNAMELEN)
956                 return (BE_ERR_PATHLEN);
957
958         if (!zfs_name_valid(name, ZFS_TYPE_DATASET))
959                 return (BE_ERR_INVALIDNAME);
960
961         return (BE_ERR_SUCCESS);
962 }
963
964
965 /*
966  * usage
967  */
968 int
969 be_rename(libbe_handle_t *lbh, const char *old, const char *new)
970 {
971         char full_old[BE_MAXPATHLEN];
972         char full_new[BE_MAXPATHLEN];
973         zfs_handle_t *zfs_hdl;
974         int err;
975
976         /*
977          * be_validate_name is documented not to set error state, so we should
978          * do so here.
979          */
980         if ((err = be_validate_name(lbh, new)) != 0)
981                 return (set_error(lbh, err));
982         if ((err = be_root_concat(lbh, old, full_old)) != 0)
983                 return (set_error(lbh, err));
984         if ((err = be_root_concat(lbh, new, full_new)) != 0)
985                 return (set_error(lbh, err));
986
987         if (!zfs_dataset_exists(lbh->lzh, full_old, ZFS_TYPE_DATASET))
988                 return (set_error(lbh, BE_ERR_NOENT));
989
990         if (zfs_dataset_exists(lbh->lzh, full_new, ZFS_TYPE_DATASET))
991                 return (set_error(lbh, BE_ERR_EXISTS));
992
993         if ((zfs_hdl = zfs_open(lbh->lzh, full_old,
994             ZFS_TYPE_FILESYSTEM)) == NULL)
995                 return (set_error(lbh, BE_ERR_ZFSOPEN));
996
997         /* recurse, nounmount, forceunmount */
998         struct renameflags flags = {
999                 .nounmount = 1,
1000         };
1001
1002         err = zfs_rename(zfs_hdl, NULL, full_new, flags);
1003
1004         zfs_close(zfs_hdl);
1005         if (err != 0)
1006                 return (set_error(lbh, BE_ERR_UNKNOWN));
1007         return (0);
1008 }
1009
1010
1011 int
1012 be_export(libbe_handle_t *lbh, const char *bootenv, int fd)
1013 {
1014         char snap_name[BE_MAXPATHLEN];
1015         char buf[BE_MAXPATHLEN];
1016         zfs_handle_t *zfs;
1017         sendflags_t flags = { 0 };
1018         int err;
1019
1020         if ((err = be_snapshot(lbh, bootenv, NULL, true, snap_name)) != 0)
1021                 /* Use the error set by be_snapshot */
1022                 return (err);
1023
1024         be_root_concat(lbh, snap_name, buf);
1025
1026         if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_DATASET)) == NULL)
1027                 return (set_error(lbh, BE_ERR_ZFSOPEN));
1028
1029         err = zfs_send_one(zfs, NULL, fd, flags);
1030         zfs_close(zfs);
1031
1032         return (err);
1033 }
1034
1035
1036 int
1037 be_import(libbe_handle_t *lbh, const char *bootenv, int fd)
1038 {
1039         char buf[BE_MAXPATHLEN];
1040         nvlist_t *props;
1041         zfs_handle_t *zfs;
1042         recvflags_t flags = { .nomount = 1 };
1043         int err;
1044
1045         be_root_concat(lbh, bootenv, buf);
1046
1047         if ((err = zfs_receive(lbh->lzh, buf, NULL, &flags, fd, NULL)) != 0) {
1048                 switch (err) {
1049                 case EINVAL:
1050                         return (set_error(lbh, BE_ERR_NOORIGIN));
1051                 case ENOENT:
1052                         return (set_error(lbh, BE_ERR_NOENT));
1053                 case EIO:
1054                         return (set_error(lbh, BE_ERR_IO));
1055                 default:
1056                         return (set_error(lbh, BE_ERR_UNKNOWN));
1057                 }
1058         }
1059
1060         if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_FILESYSTEM)) == NULL)
1061                 return (set_error(lbh, BE_ERR_ZFSOPEN));
1062
1063         nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
1064         nvlist_add_string(props, "canmount", "noauto");
1065         nvlist_add_string(props, "mountpoint", "none");
1066
1067         err = zfs_prop_set_list(zfs, props);
1068         nvlist_free(props);
1069
1070         zfs_close(zfs);
1071
1072         if (err != 0)
1073                 return (set_error(lbh, BE_ERR_UNKNOWN));
1074
1075         return (0);
1076 }
1077
1078 #if SOON
1079 static int
1080 be_create_child_noent(libbe_handle_t *lbh, const char *active,
1081     const char *child_path)
1082 {
1083         nvlist_t *props;
1084         zfs_handle_t *zfs;
1085         int err;
1086
1087         nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
1088         nvlist_add_string(props, "canmount", "noauto");
1089         nvlist_add_string(props, "mountpoint", child_path);
1090
1091         /* Create */
1092         if ((err = zfs_create(lbh->lzh, active, ZFS_TYPE_DATASET,
1093             props)) != 0) {
1094                 switch (err) {
1095                 case EZFS_EXISTS:
1096                         return (set_error(lbh, BE_ERR_EXISTS));
1097                 case EZFS_NOENT:
1098                         return (set_error(lbh, BE_ERR_NOENT));
1099                 case EZFS_BADTYPE:
1100                 case EZFS_BADVERSION:
1101                         return (set_error(lbh, BE_ERR_NOPOOL));
1102                 case EZFS_BADPROP:
1103                 default:
1104                         /* We set something up wrong, probably... */
1105                         return (set_error(lbh, BE_ERR_UNKNOWN));
1106                 }
1107         }
1108         nvlist_free(props);
1109
1110         if ((zfs = zfs_open(lbh->lzh, active, ZFS_TYPE_DATASET)) == NULL)
1111                 return (set_error(lbh, BE_ERR_ZFSOPEN));
1112
1113         /* Set props */
1114         if ((err = zfs_prop_set(zfs, "canmount", "noauto")) != 0) {
1115                 zfs_close(zfs);
1116                 /*
1117                  * Similar to other cases, this shouldn't fail unless we've
1118                  * done something wrong.  This is a new dataset that shouldn't
1119                  * have been mounted anywhere between creation and now.
1120                  */
1121                 if (err == EZFS_NOMEM)
1122                         return (set_error(lbh, BE_ERR_NOMEM));
1123                 return (set_error(lbh, BE_ERR_UNKNOWN));
1124         }
1125         zfs_close(zfs);
1126         return (BE_ERR_SUCCESS);
1127 }
1128
1129 static int
1130 be_create_child_cloned(libbe_handle_t *lbh, const char *active)
1131 {
1132         char buf[BE_MAXPATHLEN], tmp[BE_MAXPATHLEN];;
1133         zfs_handle_t *zfs;
1134         int err;
1135
1136         /* XXX TODO ? */
1137
1138         /*
1139          * Establish if the existing path is a zfs dataset or just
1140          * the subdirectory of one
1141          */
1142         strlcpy(tmp, "tmp/be_snap.XXXXX", sizeof(tmp));
1143         if (mktemp(tmp) == NULL)
1144                 return (set_error(lbh, BE_ERR_UNKNOWN));
1145
1146         be_root_concat(lbh, tmp, buf);
1147         printf("Here %s?\n", buf);
1148         if ((err = zfs_snapshot(lbh->lzh, buf, false, NULL)) != 0) {
1149                 switch (err) {
1150                 case EZFS_INVALIDNAME:
1151                         return (set_error(lbh, BE_ERR_INVALIDNAME));
1152
1153                 default:
1154                         /*
1155                          * The other errors that zfs_ioc_snapshot might return
1156                          * shouldn't happen if we've set things up properly, so
1157                          * we'll gloss over them and call it UNKNOWN as it will
1158                          * require further triage.
1159                          */
1160                         if (errno == ENOTSUP)
1161                                 return (set_error(lbh, BE_ERR_NOPOOL));
1162                         return (set_error(lbh, BE_ERR_UNKNOWN));
1163                 }
1164         }
1165
1166         /* Clone */
1167         if ((zfs = zfs_open(lbh->lzh, buf, ZFS_TYPE_SNAPSHOT)) == NULL)
1168                 return (BE_ERR_ZFSOPEN);
1169
1170         if ((err = zfs_clone(zfs, active, NULL)) != 0)
1171                 /* XXX TODO correct error */
1172                 return (set_error(lbh, BE_ERR_UNKNOWN));
1173
1174         /* set props */
1175         zfs_close(zfs);
1176         return (BE_ERR_SUCCESS);
1177 }
1178
1179 int
1180 be_add_child(libbe_handle_t *lbh, const char *child_path, bool cp_if_exists)
1181 {
1182         struct stat sb;
1183         char active[BE_MAXPATHLEN], buf[BE_MAXPATHLEN];
1184         nvlist_t *props;
1185         const char *s;
1186
1187         /* Require absolute paths */
1188         if (*child_path != '/')
1189                 return (set_error(lbh, BE_ERR_BADPATH));
1190
1191         strlcpy(active, be_active_path(lbh), BE_MAXPATHLEN);
1192         strcpy(buf, active);
1193
1194         /* Create non-mountable parent dataset(s) */
1195         s = child_path;
1196         for (char *p; (p = strchr(s+1, '/')) != NULL; s = p) {
1197                 size_t len = p - s;
1198                 strncat(buf, s, len);
1199
1200                 nvlist_alloc(&props, NV_UNIQUE_NAME, KM_SLEEP);
1201                 nvlist_add_string(props, "canmount", "off");
1202                 nvlist_add_string(props, "mountpoint", "none");
1203                 zfs_create(lbh->lzh, buf, ZFS_TYPE_DATASET, props);
1204                 nvlist_free(props);
1205         }
1206
1207         /* Path does not exist as a descendent of / yet */
1208         if (strlcat(active, child_path, BE_MAXPATHLEN) >= BE_MAXPATHLEN)
1209                 return (set_error(lbh, BE_ERR_PATHLEN));
1210
1211         if (stat(child_path, &sb) != 0) {
1212                 /* Verify that error is ENOENT */
1213                 if (errno != ENOENT)
1214                         return (set_error(lbh, BE_ERR_UNKNOWN));
1215                 return (be_create_child_noent(lbh, active, child_path));
1216         } else if (cp_if_exists)
1217                 /* Path is already a descendent of / and should be copied */
1218                 return (be_create_child_cloned(lbh, active));
1219         return (set_error(lbh, BE_ERR_EXISTS));
1220 }
1221 #endif  /* SOON */
1222
1223 static int
1224 be_set_nextboot(libbe_handle_t *lbh, nvlist_t *config, uint64_t pool_guid,
1225     const char *zfsdev)
1226 {
1227         nvlist_t **child;
1228         uint64_t vdev_guid;
1229         int c, children;
1230
1231         if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN, &child,
1232             &children) == 0) {
1233                 for (c = 0; c < children; ++c)
1234                         if (be_set_nextboot(lbh, child[c], pool_guid, zfsdev) != 0)
1235                                 return (1);
1236                 return (0);
1237         }
1238
1239         if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
1240             &vdev_guid) != 0) {
1241                 return (1);
1242         }
1243
1244         if (zpool_nextboot(lbh->lzh, pool_guid, vdev_guid, zfsdev) != 0) {
1245                 perror("ZFS_IOC_NEXTBOOT failed");
1246                 return (1);
1247         }
1248
1249         return (0);
1250 }
1251
1252 /*
1253  * Deactivate old BE dataset; currently just sets canmount=noauto
1254  */
1255 static int
1256 be_deactivate(libbe_handle_t *lbh, const char *ds)
1257 {
1258         zfs_handle_t *zfs;
1259
1260         if ((zfs = zfs_open(lbh->lzh, ds, ZFS_TYPE_DATASET)) == NULL)
1261                 return (1);
1262         if (zfs_prop_set(zfs, "canmount", "noauto") != 0)
1263                 return (1);
1264         zfs_close(zfs);
1265         return (0);
1266 }
1267
1268 int
1269 be_activate(libbe_handle_t *lbh, const char *bootenv, bool temporary)
1270 {
1271         char be_path[BE_MAXPATHLEN];
1272         char buf[BE_MAXPATHLEN];
1273         nvlist_t *config, *dsprops, *vdevs;
1274         char *origin;
1275         uint64_t pool_guid;
1276         zfs_handle_t *zhp;
1277         int err;
1278
1279         be_root_concat(lbh, bootenv, be_path);
1280
1281         /* Note: be_exists fails if mountpoint is not / */
1282         if ((err = be_exists(lbh, be_path)) != 0)
1283                 return (set_error(lbh, err));
1284
1285         if (temporary) {
1286                 config = zpool_get_config(lbh->active_phandle, NULL);
1287                 if (config == NULL)
1288                         /* config should be fetchable... */
1289                         return (set_error(lbh, BE_ERR_UNKNOWN));
1290
1291                 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1292                     &pool_guid) != 0)
1293                         /* Similarly, it shouldn't be possible */
1294                         return (set_error(lbh, BE_ERR_UNKNOWN));
1295
1296                 /* Expected format according to zfsbootcfg(8) man */
1297                 snprintf(buf, sizeof(buf), "zfs:%s:", be_path);
1298
1299                 /* We have no config tree */
1300                 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
1301                     &vdevs) != 0)
1302                         return (set_error(lbh, BE_ERR_NOPOOL));
1303
1304                 return (be_set_nextboot(lbh, vdevs, pool_guid, buf));
1305         } else {
1306                 if (be_deactivate(lbh, lbh->bootfs) != 0)
1307                         return (-1);
1308
1309                 /* Obtain bootenv zpool */
1310                 err = zpool_set_prop(lbh->active_phandle, "bootfs", be_path);
1311                 if (err)
1312                         return (-1);
1313
1314                 zhp = zfs_open(lbh->lzh, be_path, ZFS_TYPE_FILESYSTEM);
1315                 if (zhp == NULL)
1316                         return (-1);
1317
1318                 if (be_prop_list_alloc(&dsprops) != 0)
1319                         return (-1);
1320
1321                 if (be_get_dataset_props(lbh, be_path, dsprops) != 0) {
1322                         nvlist_free(dsprops);
1323                         return (-1);
1324                 }
1325
1326                 if (nvlist_lookup_string(dsprops, "origin", &origin) == 0)
1327                         err = zfs_promote(zhp);
1328                 nvlist_free(dsprops);
1329
1330                 zfs_close(zhp);
1331
1332                 if (err)
1333                         return (-1);
1334         }
1335
1336         return (BE_ERR_SUCCESS);
1337 }