1b179da01SKyle Evans /*- 2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 328f16a0fSKyle Evans * 428f16a0fSKyle Evans * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in> 528f16a0fSKyle Evans * 628f16a0fSKyle Evans * Redistribution and use in source and binary forms, with or without 728f16a0fSKyle Evans * modification, are permitted provided that the following conditions 828f16a0fSKyle Evans * are met: 928f16a0fSKyle Evans * 1. Redistributions of source code must retain the above copyright 1028f16a0fSKyle Evans * notice, this list of conditions and the following disclaimer. 1128f16a0fSKyle Evans * 2. Redistributions in binary form must reproduce the above copyright 1228f16a0fSKyle Evans * notice, this list of conditions and the following disclaimer in the 1328f16a0fSKyle Evans * documentation and/or other materials provided with the distribution. 1428f16a0fSKyle Evans * 1528f16a0fSKyle Evans * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1628f16a0fSKyle Evans * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1728f16a0fSKyle Evans * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1828f16a0fSKyle Evans * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 1928f16a0fSKyle Evans * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2028f16a0fSKyle Evans * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2128f16a0fSKyle Evans * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2228f16a0fSKyle Evans * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2328f16a0fSKyle Evans * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2428f16a0fSKyle Evans * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2528f16a0fSKyle Evans * SUCH DAMAGE. 2628f16a0fSKyle Evans */ 2728f16a0fSKyle Evans 2828f16a0fSKyle Evans #ifndef _LIBBE_H 2928f16a0fSKyle Evans #define _LIBBE_H 3028f16a0fSKyle Evans 313682d5e9SKyle Evans #include <libnvpair.h> 3228f16a0fSKyle Evans #include <stdbool.h> 3328f16a0fSKyle Evans 3428f16a0fSKyle Evans #define BE_MAXPATHLEN 512 3528f16a0fSKyle Evans 3628f16a0fSKyle Evans typedef struct libbe_handle libbe_handle_t; 3728f16a0fSKyle Evans 3828f16a0fSKyle Evans typedef enum be_error { 3928f16a0fSKyle Evans BE_ERR_SUCCESS = 0, /* No error */ 4028f16a0fSKyle Evans BE_ERR_INVALIDNAME, /* invalid boot env name */ 4128f16a0fSKyle Evans BE_ERR_EXISTS, /* boot env name already taken */ 4228f16a0fSKyle Evans BE_ERR_NOENT, /* boot env doesn't exist */ 4328f16a0fSKyle Evans BE_ERR_PERMS, /* insufficient permissions */ 4428f16a0fSKyle Evans BE_ERR_DESTROYACT, /* cannot destroy active boot env */ 4528f16a0fSKyle Evans BE_ERR_DESTROYMNT, /* destroying a mounted be requires force */ 46f1ca70d3SKyle Evans BE_ERR_BADPATH, /* path not suitable for operation */ 47f1ca70d3SKyle Evans BE_ERR_PATHBUSY, /* requested path is busy */ 4828f16a0fSKyle Evans BE_ERR_PATHLEN, /* provided name exceeds maximum length limit */ 49162ec569SKyle Evans BE_ERR_BADMOUNT, /* mountpoint is not '/' */ 5028f16a0fSKyle Evans BE_ERR_NOORIGIN, /* could not open snapshot's origin */ 5128f16a0fSKyle Evans BE_ERR_MOUNTED, /* boot environment is already mounted */ 5228f16a0fSKyle Evans BE_ERR_NOMOUNT, /* boot environment is not mounted */ 5328f16a0fSKyle Evans BE_ERR_ZFSOPEN, /* calling zfs_open() failed */ 5428f16a0fSKyle Evans BE_ERR_ZFSCLONE, /* error when calling zfs_clone to create be */ 55f1ca70d3SKyle Evans BE_ERR_IO, /* error when doing some I/O operation */ 562989df09SKyle Evans BE_ERR_NOPOOL, /* operation not supported on this pool */ 57c65a2111SKyle Evans BE_ERR_NOMEM, /* insufficient memory */ 5828f16a0fSKyle Evans BE_ERR_UNKNOWN, /* unknown error */ 59be7dd423SKyle Evans BE_ERR_INVORIGIN, /* invalid origin */ 608f5c6c31SKyle Evans BE_ERR_HASCLONES, /* snapshot has clones */ 6128f16a0fSKyle Evans } be_error_t; 6228f16a0fSKyle Evans 6328f16a0fSKyle Evans 6428f16a0fSKyle Evans /* Library handling functions: be.c */ 65cc624025SKyle Evans libbe_handle_t *libbe_init(const char *root); 6628f16a0fSKyle Evans void libbe_close(libbe_handle_t *); 6728f16a0fSKyle Evans 6828f16a0fSKyle Evans /* Bootenv information functions: be_info.c */ 6928f16a0fSKyle Evans const char *be_active_name(libbe_handle_t *); 7028f16a0fSKyle Evans const char *be_active_path(libbe_handle_t *); 71ff8676ccSKyle Evans const char *be_nextboot_name(libbe_handle_t *); 72ff8676ccSKyle Evans const char *be_nextboot_path(libbe_handle_t *); 7328f16a0fSKyle Evans const char *be_root_path(libbe_handle_t *); 7428f16a0fSKyle Evans 753682d5e9SKyle Evans int be_get_bootenv_props(libbe_handle_t *, nvlist_t *); 769b1662e6SKyle Evans int be_get_dataset_props(libbe_handle_t *, const char *, nvlist_t *); 7796c5db58SKyle Evans int be_get_dataset_snapshots(libbe_handle_t *, const char *, nvlist_t *); 78734e362fSKyle Evans int be_prop_list_alloc(nvlist_t **be_list); 79b29bf2f8SKyle Evans void be_prop_list_free(nvlist_t *be_list); 8028f16a0fSKyle Evans 8173c3d608SKyle Evans int be_activate(libbe_handle_t *, const char *, bool); 82e307eb94SToomas Soome int be_deactivate(libbe_handle_t *, const char *, bool); 8328f16a0fSKyle Evans 84455d8009SKyle Evans bool be_is_auto_snapshot_name(libbe_handle_t *, const char *); 85455d8009SKyle Evans 8628f16a0fSKyle Evans /* Bootenv creation functions */ 8773c3d608SKyle Evans int be_create(libbe_handle_t *, const char *); 88fa30d9edSKyle Evans int be_create_depth(libbe_handle_t *, const char *, const char *, int); 89b29bf2f8SKyle Evans int be_create_from_existing(libbe_handle_t *, const char *, const char *); 90b29bf2f8SKyle Evans int be_create_from_existing_snap(libbe_handle_t *, const char *, const char *); 91b29bf2f8SKyle Evans int be_snapshot(libbe_handle_t *, const char *, const char *, bool, char *); 9228f16a0fSKyle Evans 9328f16a0fSKyle Evans /* Bootenv manipulation functions */ 9473c3d608SKyle Evans int be_rename(libbe_handle_t *, const char *, const char *); 9528f16a0fSKyle Evans 9628f16a0fSKyle Evans /* Bootenv removal functions */ 9728f16a0fSKyle Evans 9828f16a0fSKyle Evans typedef enum { 9928f16a0fSKyle Evans BE_DESTROY_FORCE = 1 << 0, 10013c62c50SKyle Evans BE_DESTROY_ORIGIN = 1 << 1, 101455d8009SKyle Evans BE_DESTROY_AUTOORIGIN = 1 << 2, 10228f16a0fSKyle Evans } be_destroy_opt_t; 10328f16a0fSKyle Evans 10473c3d608SKyle Evans int be_destroy(libbe_handle_t *, const char *, int); 10528f16a0fSKyle Evans 10628f16a0fSKyle Evans /* Bootenv mounting functions: be_access.c */ 10728f16a0fSKyle Evans 10828f16a0fSKyle Evans typedef enum { 10928f16a0fSKyle Evans BE_MNT_FORCE = 1 << 0, 11028f16a0fSKyle Evans BE_MNT_DEEP = 1 << 1, 11128f16a0fSKyle Evans } be_mount_opt_t; 11228f16a0fSKyle Evans 1135773e924SKyle Evans int be_mount(libbe_handle_t *, const char *, const char *, int, char *); 1145773e924SKyle Evans int be_unmount(libbe_handle_t *, const char *, int); 115843e39ceSKyle Evans int be_mounted_at(libbe_handle_t *, const char *path, nvlist_t *); 11628f16a0fSKyle Evans 11728f16a0fSKyle Evans /* Error related functions: be_error.c */ 11828f16a0fSKyle Evans int libbe_errno(libbe_handle_t *); 11928f16a0fSKyle Evans const char *libbe_error_description(libbe_handle_t *); 12028f16a0fSKyle Evans void libbe_print_on_error(libbe_handle_t *, bool); 12128f16a0fSKyle Evans 12228f16a0fSKyle Evans /* Utility Functions */ 123b29bf2f8SKyle Evans int be_root_concat(libbe_handle_t *, const char *, char *); 124b29bf2f8SKyle Evans int be_validate_name(libbe_handle_t * __unused, const char *); 125b29bf2f8SKyle Evans int be_validate_snap(libbe_handle_t *, const char *); 1265773e924SKyle Evans int be_exists(libbe_handle_t *, const char *); 12728f16a0fSKyle Evans 12873c3d608SKyle Evans int be_export(libbe_handle_t *, const char *, int fd); 12973c3d608SKyle Evans int be_import(libbe_handle_t *, const char *, int fd); 13028f16a0fSKyle Evans 1313d1a1f2cSKyle Evans #if SOON 13273c3d608SKyle Evans int be_add_child(libbe_handle_t *, const char *, bool); 1333d1a1f2cSKyle Evans #endif 1349b1662e6SKyle Evans void be_nicenum(uint64_t num, char *buf, size_t buflen); 13528f16a0fSKyle Evans 13628f16a0fSKyle Evans #endif /* _LIBBE_H */ 137