xref: /freebsd/lib/libbe/be.h (revision be7dd423764c16ee320770f715fea5da105bad13)
1b179da01SKyle Evans /*-
2b179da01SKyle Evans  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
328f16a0fSKyle Evans  *
428f16a0fSKyle Evans  * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in>
528f16a0fSKyle Evans  * All rights reserved.
628f16a0fSKyle Evans  *
728f16a0fSKyle Evans  * Redistribution and use in source and binary forms, with or without
828f16a0fSKyle Evans  * modification, are permitted provided that the following conditions
928f16a0fSKyle Evans  * are met:
1028f16a0fSKyle Evans  * 1. Redistributions of source code must retain the above copyright
1128f16a0fSKyle Evans  *    notice, this list of conditions and the following disclaimer.
1228f16a0fSKyle Evans  * 2. Redistributions in binary form must reproduce the above copyright
1328f16a0fSKyle Evans  *    notice, this list of conditions and the following disclaimer in the
1428f16a0fSKyle Evans  *    documentation and/or other materials provided with the distribution.
1528f16a0fSKyle Evans  *
1628f16a0fSKyle Evans  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1728f16a0fSKyle Evans  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1828f16a0fSKyle Evans  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1928f16a0fSKyle Evans  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2028f16a0fSKyle Evans  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2128f16a0fSKyle Evans  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2228f16a0fSKyle Evans  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2328f16a0fSKyle Evans  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2428f16a0fSKyle Evans  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2528f16a0fSKyle Evans  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2628f16a0fSKyle Evans  * SUCH DAMAGE.
27b6e7c421SKyle Evans  *
28b6e7c421SKyle Evans  * $FreeBSD$
2928f16a0fSKyle Evans  */
3028f16a0fSKyle Evans 
3128f16a0fSKyle Evans #ifndef _LIBBE_H
3228f16a0fSKyle Evans #define _LIBBE_H
3328f16a0fSKyle Evans 
343682d5e9SKyle Evans #include <libnvpair.h>
3528f16a0fSKyle Evans #include <stdbool.h>
3628f16a0fSKyle Evans 
3728f16a0fSKyle Evans #define BE_MAXPATHLEN    512
3828f16a0fSKyle Evans 
3928f16a0fSKyle Evans typedef struct libbe_handle libbe_handle_t;
4028f16a0fSKyle Evans 
4128f16a0fSKyle Evans typedef enum be_error {
4228f16a0fSKyle Evans 	BE_ERR_SUCCESS = 0,     /* No error */
4328f16a0fSKyle Evans 	BE_ERR_INVALIDNAME,     /* invalid boot env name */
4428f16a0fSKyle Evans 	BE_ERR_EXISTS,          /* boot env name already taken */
4528f16a0fSKyle Evans 	BE_ERR_NOENT,           /* boot env doesn't exist */
4628f16a0fSKyle Evans 	BE_ERR_PERMS,           /* insufficient permissions */
4728f16a0fSKyle Evans 	BE_ERR_DESTROYACT,      /* cannot destroy active boot env */
4828f16a0fSKyle Evans 	BE_ERR_DESTROYMNT,      /* destroying a mounted be requires force */
49f1ca70d3SKyle Evans 	BE_ERR_BADPATH,		/* path not suitable for operation */
50f1ca70d3SKyle Evans 	BE_ERR_PATHBUSY,	/* requested path is busy */
5128f16a0fSKyle Evans 	BE_ERR_PATHLEN,         /* provided name exceeds maximum length limit */
52162ec569SKyle Evans 	BE_ERR_BADMOUNT,        /* mountpoint is not '/' */
5328f16a0fSKyle Evans 	BE_ERR_NOORIGIN,        /* could not open snapshot's origin */
5428f16a0fSKyle Evans 	BE_ERR_MOUNTED,         /* boot environment is already mounted */
5528f16a0fSKyle Evans 	BE_ERR_NOMOUNT,         /* boot environment is not mounted */
5628f16a0fSKyle Evans 	BE_ERR_ZFSOPEN,         /* calling zfs_open() failed */
5728f16a0fSKyle Evans 	BE_ERR_ZFSCLONE,        /* error when calling zfs_clone to create be */
58f1ca70d3SKyle Evans 	BE_ERR_IO,		/* error when doing some I/O operation */
592989df09SKyle Evans 	BE_ERR_NOPOOL,		/* operation not supported on this pool */
60c65a2111SKyle Evans 	BE_ERR_NOMEM,		/* insufficient memory */
6128f16a0fSKyle Evans 	BE_ERR_UNKNOWN,         /* unknown error */
62*be7dd423SKyle Evans 	BE_ERR_INVORIGIN,       /* invalid origin */
6328f16a0fSKyle Evans } be_error_t;
6428f16a0fSKyle Evans 
6528f16a0fSKyle Evans 
6628f16a0fSKyle Evans /* Library handling functions: be.c */
67cc624025SKyle Evans libbe_handle_t *libbe_init(const char *root);
6828f16a0fSKyle Evans void libbe_close(libbe_handle_t *);
6928f16a0fSKyle Evans 
7028f16a0fSKyle Evans /* Bootenv information functions: be_info.c */
7128f16a0fSKyle Evans const char *be_active_name(libbe_handle_t *);
7228f16a0fSKyle Evans const char *be_active_path(libbe_handle_t *);
73ff8676ccSKyle Evans const char *be_nextboot_name(libbe_handle_t *);
74ff8676ccSKyle Evans const char *be_nextboot_path(libbe_handle_t *);
7528f16a0fSKyle Evans const char *be_root_path(libbe_handle_t *);
7628f16a0fSKyle Evans 
773682d5e9SKyle Evans int be_get_bootenv_props(libbe_handle_t *, nvlist_t *);
789b1662e6SKyle Evans int be_get_dataset_props(libbe_handle_t *, const char *, nvlist_t *);
7996c5db58SKyle Evans int be_get_dataset_snapshots(libbe_handle_t *, const char *, nvlist_t *);
80734e362fSKyle Evans int be_prop_list_alloc(nvlist_t **be_list);
81b29bf2f8SKyle Evans void be_prop_list_free(nvlist_t *be_list);
8228f16a0fSKyle Evans 
8373c3d608SKyle Evans int be_activate(libbe_handle_t *, const char *, bool);
8428f16a0fSKyle Evans 
8528f16a0fSKyle Evans /* Bootenv creation functions */
8673c3d608SKyle Evans int be_create(libbe_handle_t *, const char *);
87b29bf2f8SKyle Evans int be_create_from_existing(libbe_handle_t *, const char *, const char *);
88b29bf2f8SKyle Evans int be_create_from_existing_snap(libbe_handle_t *, const char *, const char *);
89b29bf2f8SKyle Evans int be_snapshot(libbe_handle_t *, const char *, const char *, bool, char *);
9028f16a0fSKyle Evans 
9128f16a0fSKyle Evans /* Bootenv manipulation functions */
9273c3d608SKyle Evans int be_rename(libbe_handle_t *, const char *, const char *);
9328f16a0fSKyle Evans 
9428f16a0fSKyle Evans /* Bootenv removal functions */
9528f16a0fSKyle Evans 
9628f16a0fSKyle Evans typedef enum {
9728f16a0fSKyle Evans 	BE_DESTROY_FORCE	= 1 << 0,
9813c62c50SKyle Evans 	BE_DESTROY_ORIGIN	= 1 << 1,
9928f16a0fSKyle Evans } be_destroy_opt_t;
10028f16a0fSKyle Evans 
10173c3d608SKyle Evans int be_destroy(libbe_handle_t *, const char *, int);
10228f16a0fSKyle Evans 
10328f16a0fSKyle Evans /* Bootenv mounting functions: be_access.c */
10428f16a0fSKyle Evans 
10528f16a0fSKyle Evans typedef enum {
10628f16a0fSKyle Evans 	BE_MNT_FORCE		= 1 << 0,
10728f16a0fSKyle Evans 	BE_MNT_DEEP		= 1 << 1,
10828f16a0fSKyle Evans } be_mount_opt_t;
10928f16a0fSKyle Evans 
11028f16a0fSKyle Evans int be_mount(libbe_handle_t *, char *, char *, int, char *);
11128f16a0fSKyle Evans int be_unmount(libbe_handle_t *, char *, int);
112843e39ceSKyle Evans int be_mounted_at(libbe_handle_t *, const char *path, nvlist_t *);
11328f16a0fSKyle Evans 
11428f16a0fSKyle Evans /* Error related functions: be_error.c */
11528f16a0fSKyle Evans int libbe_errno(libbe_handle_t *);
11628f16a0fSKyle Evans const char *libbe_error_description(libbe_handle_t *);
11728f16a0fSKyle Evans void libbe_print_on_error(libbe_handle_t *, bool);
11828f16a0fSKyle Evans 
11928f16a0fSKyle Evans /* Utility Functions */
120b29bf2f8SKyle Evans int be_root_concat(libbe_handle_t *, const char *, char *);
121b29bf2f8SKyle Evans int be_validate_name(libbe_handle_t * __unused, const char *);
122b29bf2f8SKyle Evans int be_validate_snap(libbe_handle_t *, const char *);
123162ec569SKyle Evans int be_exists(libbe_handle_t *, char *);
12428f16a0fSKyle Evans 
12573c3d608SKyle Evans int be_export(libbe_handle_t *, const char *, int fd);
12673c3d608SKyle Evans int be_import(libbe_handle_t *, const char *, int fd);
12728f16a0fSKyle Evans 
1283d1a1f2cSKyle Evans #if SOON
12973c3d608SKyle Evans int be_add_child(libbe_handle_t *, const char *, bool);
1303d1a1f2cSKyle Evans #endif
1319b1662e6SKyle Evans void be_nicenum(uint64_t num, char *buf, size_t buflen);
13228f16a0fSKyle Evans 
13328f16a0fSKyle Evans #endif  /* _LIBBE_H */
134