Skip to content

Commit

Permalink
libbe(3): Add be_mounted_at to check a mount point
Browse files Browse the repository at this point in the history
At a bare minimum, this function will return 0 if a BE is mounted at the
given path or non-zero otherwise.  If the optional 'details' nvlist is
supplied, it is filled with an nvpair containing just the information about
the BE mounted at the path.  This nvpair is structured just as it is for
be_get_bootenv_props, except limited to just the single mount point.
  • Loading branch information
kevans91 committed Jul 26, 2018
1 parent b854d08 commit 7d3d9bc
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 11 deletions.
1 change: 1 addition & 0 deletions lib/libbe/be.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ typedef enum {

int be_mount(libbe_handle_t *, char *, char *, int, char *);
int be_unmount(libbe_handle_t *, char *, int);
int be_mounted_at(libbe_handle_t *, const char *path, nvlist_t *);

/* Error related functions: be_error.c */
int libbe_errno(libbe_handle_t *);
Expand Down
63 changes: 63 additions & 0 deletions lib/libbe/be_access.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,69 @@
#include "be.h"
#include "be_impl.h"

struct be_mountcheck_info {
const char *path;
char *name;
};

static int
be_mountcheck_cb(zfs_handle_t *zfs_hdl, void *data)
{
struct be_mountcheck_info *info;
char *mountpoint;

if (data == NULL)
return (1);
info = (struct be_mountcheck_info *)data;
if (!zfs_is_mounted(zfs_hdl, &mountpoint))
return (0);
if (strcmp(mountpoint, info->path) == 0) {
info->name = strdup(zfs_get_name(zfs_hdl));
return (1);
}
return (0);
}

/*
* usage
*/
int
be_mounted_at(libbe_handle_t *lbh, const char *path, nvlist_t *details)
{
char be[BE_MAXPATHLEN + 1];
zfs_handle_t *root_hdl;
struct be_mountcheck_info info;
prop_data_t propinfo;

bzero(&be, BE_MAXPATHLEN + 1);
if ((root_hdl = zfs_open(lbh->lzh, lbh->root,
ZFS_TYPE_FILESYSTEM)) == NULL)
return (BE_ERR_ZFSOPEN);

info.path = path;
info.name = NULL;
zfs_iter_filesystems(root_hdl, be_mountcheck_cb, &info);
zfs_close(root_hdl);

if (info.name != NULL) {
if (details != NULL) {
if ((root_hdl = zfs_open(lbh->lzh, lbh->root,
ZFS_TYPE_FILESYSTEM)) == NULL) {
free(info.name);
return (BE_ERR_ZFSOPEN);
}

propinfo.lbh = lbh;
propinfo.list = details;
prop_list_builder_cb(root_hdl, &propinfo);
zfs_close(root_hdl);
}
free(info.name);
return (0);
}
return (1);
}

/*
* usage
*/
Expand Down
9 changes: 8 additions & 1 deletion lib/libbe/be_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

#include "be.h"


struct libbe_handle {
libzfs_handle_t *lzh;
zpool_handle_t *active_phandle;
Expand All @@ -56,6 +55,14 @@ struct libbe_dccb {
nvlist_t *props;
};

typedef struct prop_data {
nvlist_t *list;
libbe_handle_t *lbh;
} prop_data_t;

int prop_list_builder_cb(zfs_handle_t *, void *);
int prop_list_builder(prop_data_t *);

int set_error(libbe_handle_t *, be_error_t);

#endif /* _LIBBE_IMPL_H */
12 changes: 2 additions & 10 deletions lib/libbe/be_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@
#include "be.h"
#include "be_impl.h"

typedef struct prop_data {
nvlist_t *list;
libbe_handle_t *lbh;
} prop_data_t;

static int prop_list_builder_cb(zfs_handle_t *, void *);
static int prop_list_builder(prop_data_t *);

/*
* Returns the name of the active boot environment
*/
Expand Down Expand Up @@ -111,7 +103,7 @@ be_get_bootenv_props(libbe_handle_t *lbh, nvlist_t *dsnvl)
* the bootenv root, populate an nvlist_t of its relevant properties.
* TODO: should any other properties be included?
*/
static int
int
prop_list_builder_cb(zfs_handle_t *zfs_hdl, void *data_p)
{
char buf[512], *mountpoint;
Expand Down Expand Up @@ -189,7 +181,7 @@ prop_list_builder_cb(zfs_handle_t *zfs_hdl, void *data_p)
* XXX TODO: ensure that this is always consistent (run after adds, deletes,
* renames,etc
*/
static int
int
prop_list_builder(prop_data_t *data)
{
zfs_handle_t *root_hdl;
Expand Down
3 changes: 3 additions & 0 deletions lib/libbe/libbe.3
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ of state to be retained, such as errors from previous operations.
.Fn be_mount "libbe_handle_t *, char *, char *, int" ;
.Pp
.Ft int
.Fn be_mounted_at "libbe_handle_t *, const char *, nvlist_t" ;
.Pp
.Ft int
.Fn be_unmount "libbe_handle_t *, char *, int" ;
.Pp
.Ft int
Expand Down

0 comments on commit 7d3d9bc

Please sign in to comment.