mirror of
https://git.proxmox.com/git/mirror_zfs.git
synced 2026-05-26 20:22:14 +03:00
libzfs/mnttab: lift mnttab cache into separate file
Sponsored-by: TrueNAS Reviewed-by: Ameer Hamza <ahamza@ixsystems.com> Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov> Signed-off-by: Rob Norris <rob.norris@truenas.com> Closes #18296
This commit is contained in:
committed by
Brian Behlendorf
parent
ae7fcd5f92
commit
02224bca40
@@ -806,190 +806,6 @@ zfs_close(zfs_handle_t *zhp)
|
||||
free(zhp);
|
||||
}
|
||||
|
||||
typedef struct mnttab_node {
|
||||
struct mnttab mtn_mt;
|
||||
avl_node_t mtn_node;
|
||||
} mnttab_node_t;
|
||||
|
||||
static int
|
||||
libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
|
||||
{
|
||||
const mnttab_node_t *mtn1 = (const mnttab_node_t *)arg1;
|
||||
const mnttab_node_t *mtn2 = (const mnttab_node_t *)arg2;
|
||||
int rv;
|
||||
|
||||
rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
|
||||
|
||||
return (TREE_ISIGN(rv));
|
||||
}
|
||||
|
||||
void
|
||||
libzfs_mnttab_init(libzfs_handle_t *hdl)
|
||||
{
|
||||
pthread_mutex_init(&hdl->libzfs_mnttab_cache_lock, NULL);
|
||||
assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
|
||||
avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
|
||||
sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
|
||||
}
|
||||
|
||||
static int
|
||||
libzfs_mnttab_update(libzfs_handle_t *hdl)
|
||||
{
|
||||
FILE *mnttab;
|
||||
struct mnttab entry;
|
||||
|
||||
if ((mnttab = fopen(MNTTAB, "re")) == NULL)
|
||||
return (ENOENT);
|
||||
|
||||
while (getmntent(mnttab, &entry) == 0) {
|
||||
mnttab_node_t *mtn;
|
||||
avl_index_t where;
|
||||
|
||||
if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
|
||||
continue;
|
||||
|
||||
mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
|
||||
mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
|
||||
mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
|
||||
mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
|
||||
mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
|
||||
|
||||
/* Exclude duplicate mounts */
|
||||
if (avl_find(&hdl->libzfs_mnttab_cache, mtn, &where) != NULL) {
|
||||
free(mtn->mtn_mt.mnt_special);
|
||||
free(mtn->mtn_mt.mnt_mountp);
|
||||
free(mtn->mtn_mt.mnt_fstype);
|
||||
free(mtn->mtn_mt.mnt_mntopts);
|
||||
free(mtn);
|
||||
continue;
|
||||
}
|
||||
|
||||
avl_add(&hdl->libzfs_mnttab_cache, mtn);
|
||||
}
|
||||
|
||||
(void) fclose(mnttab);
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
libzfs_mnttab_fini(libzfs_handle_t *hdl)
|
||||
{
|
||||
void *cookie = NULL;
|
||||
mnttab_node_t *mtn;
|
||||
|
||||
while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))
|
||||
!= NULL) {
|
||||
free(mtn->mtn_mt.mnt_special);
|
||||
free(mtn->mtn_mt.mnt_mountp);
|
||||
free(mtn->mtn_mt.mnt_fstype);
|
||||
free(mtn->mtn_mt.mnt_mntopts);
|
||||
free(mtn);
|
||||
}
|
||||
avl_destroy(&hdl->libzfs_mnttab_cache);
|
||||
(void) pthread_mutex_destroy(&hdl->libzfs_mnttab_cache_lock);
|
||||
}
|
||||
|
||||
void
|
||||
libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
|
||||
{
|
||||
hdl->libzfs_mnttab_enable = enable;
|
||||
}
|
||||
|
||||
int
|
||||
libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
|
||||
struct mnttab *entry)
|
||||
{
|
||||
FILE *mnttab;
|
||||
mnttab_node_t find;
|
||||
mnttab_node_t *mtn;
|
||||
int ret = ENOENT;
|
||||
|
||||
if (!hdl->libzfs_mnttab_enable) {
|
||||
struct mnttab srch = { 0 };
|
||||
|
||||
if (avl_numnodes(&hdl->libzfs_mnttab_cache))
|
||||
libzfs_mnttab_fini(hdl);
|
||||
|
||||
if ((mnttab = fopen(MNTTAB, "re")) == NULL)
|
||||
return (ENOENT);
|
||||
|
||||
srch.mnt_special = (char *)fsname;
|
||||
srch.mnt_fstype = (char *)MNTTYPE_ZFS;
|
||||
ret = getmntany(mnttab, entry, &srch) ? ENOENT : 0;
|
||||
(void) fclose(mnttab);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&hdl->libzfs_mnttab_cache_lock);
|
||||
if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) {
|
||||
int error;
|
||||
|
||||
if ((error = libzfs_mnttab_update(hdl)) != 0) {
|
||||
pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock);
|
||||
return (error);
|
||||
}
|
||||
}
|
||||
|
||||
find.mtn_mt.mnt_special = (char *)fsname;
|
||||
mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
|
||||
if (mtn) {
|
||||
*entry = mtn->mtn_mt;
|
||||
ret = 0;
|
||||
}
|
||||
pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
void
|
||||
libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
|
||||
const char *mountp, const char *mntopts)
|
||||
{
|
||||
mnttab_node_t *mtn;
|
||||
|
||||
pthread_mutex_lock(&hdl->libzfs_mnttab_cache_lock);
|
||||
if (avl_numnodes(&hdl->libzfs_mnttab_cache) != 0) {
|
||||
mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
|
||||
mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
|
||||
mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
|
||||
mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
|
||||
mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
|
||||
/*
|
||||
* Another thread may have already added this entry
|
||||
* via libzfs_mnttab_update. If so we should skip it.
|
||||
*/
|
||||
if (avl_find(&hdl->libzfs_mnttab_cache, mtn, NULL) != NULL) {
|
||||
free(mtn->mtn_mt.mnt_special);
|
||||
free(mtn->mtn_mt.mnt_mountp);
|
||||
free(mtn->mtn_mt.mnt_fstype);
|
||||
free(mtn->mtn_mt.mnt_mntopts);
|
||||
free(mtn);
|
||||
} else {
|
||||
avl_add(&hdl->libzfs_mnttab_cache, mtn);
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock);
|
||||
}
|
||||
|
||||
void
|
||||
libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
|
||||
{
|
||||
mnttab_node_t find;
|
||||
mnttab_node_t *ret;
|
||||
|
||||
pthread_mutex_lock(&hdl->libzfs_mnttab_cache_lock);
|
||||
find.mtn_mt.mnt_special = (char *)fsname;
|
||||
if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))
|
||||
!= NULL) {
|
||||
avl_remove(&hdl->libzfs_mnttab_cache, ret);
|
||||
free(ret->mtn_mt.mnt_special);
|
||||
free(ret->mtn_mt.mnt_mountp);
|
||||
free(ret->mtn_mt.mnt_fstype);
|
||||
free(ret->mtn_mt.mnt_mntopts);
|
||||
free(ret);
|
||||
}
|
||||
pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock);
|
||||
}
|
||||
|
||||
int
|
||||
zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user