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