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