libshare: nfs: commonify nfs_exports_[un]lock(), FILE_HEADER

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: George Wilson <gwilson@delphix.com>
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
Closes #11886
This commit is contained in:
наб
2021-04-11 19:37:48 +02:00
committed by Brian Behlendorf
parent 0f4d83117a
commit a3d387b31d
5 changed files with 95 additions and 104 deletions
+7 -51
View File
@@ -42,7 +42,6 @@
#include "libshare_impl.h"
#include "nfs.h"
#define FILE_HEADER "# !!! DO NOT EDIT THIS FILE MANUALLY !!!\n\n"
#define ZFS_EXPORTS_DIR "/etc/exports.d"
#define ZFS_EXPORTS_FILE ZFS_EXPORTS_DIR"/zfs.exports"
#define ZFS_EXPORTS_LOCK ZFS_EXPORTS_FILE".lock"
@@ -55,49 +54,6 @@ typedef int (*nfs_shareopt_callback_t)(const char *opt, const char *value,
typedef int (*nfs_host_callback_t)(const char *sharepath, const char *filename,
const char *host, const char *security, const char *access, void *cookie);
static int nfs_lock_fd = -1;
/*
* The nfs_exports_[lock|unlock] is used to guard against conconcurrent
* updates to the exports file. Each protocol is responsible for
* providing the necessary locking to ensure consistency.
*/
static int
nfs_exports_lock(void)
{
int err;
nfs_lock_fd = open(ZFS_EXPORTS_LOCK,
O_RDWR | O_CREAT | O_CLOEXEC, 0600);
if (nfs_lock_fd == -1) {
err = errno;
fprintf(stderr, "failed to lock %s: %s\n",
ZFS_EXPORTS_LOCK, strerror(err));
return (err);
}
if (flock(nfs_lock_fd, LOCK_EX) != 0) {
err = errno;
fprintf(stderr, "failed to lock %s: %s\n",
ZFS_EXPORTS_LOCK, strerror(err));
(void) close(nfs_lock_fd);
return (err);
}
return (0);
}
static void
nfs_exports_unlock(void)
{
verify(nfs_lock_fd > 0);
if (flock(nfs_lock_fd, LOCK_UN) != 0) {
fprintf(stderr, "failed to unlock %s: %s\n",
ZFS_EXPORTS_LOCK, strerror(errno));
}
close(nfs_lock_fd);
nfs_lock_fd = -1;
}
/*
* Invokes the specified callback function for each Solaris share option
* listed in the specified string.
@@ -563,7 +519,7 @@ nfs_enable_share(sa_share_impl_t impl_share)
if ((filename = nfs_init_tmpfile()) == NULL)
return (SA_SYSTEM_ERR);
error = nfs_exports_lock();
error = nfs_exports_lock(ZFS_EXPORTS_LOCK);
if (error != 0) {
unlink(filename);
free(filename);
@@ -574,7 +530,7 @@ nfs_enable_share(sa_share_impl_t impl_share)
if (error != SA_OK) {
unlink(filename);
free(filename);
nfs_exports_unlock();
nfs_exports_unlock(ZFS_EXPORTS_LOCK);
return (error);
}
@@ -583,7 +539,7 @@ nfs_enable_share(sa_share_impl_t impl_share)
if (error != SA_OK) {
unlink(filename);
free(filename);
nfs_exports_unlock();
nfs_exports_unlock(ZFS_EXPORTS_LOCK);
return (error);
}
@@ -596,7 +552,7 @@ nfs_enable_share(sa_share_impl_t impl_share)
unlink(filename);
free(filename);
}
nfs_exports_unlock();
nfs_exports_unlock(ZFS_EXPORTS_LOCK);
return (error);
}
@@ -612,7 +568,7 @@ nfs_disable_share(sa_share_impl_t impl_share)
if ((filename = nfs_init_tmpfile()) == NULL)
return (SA_SYSTEM_ERR);
error = nfs_exports_lock();
error = nfs_exports_lock(ZFS_EXPORTS_LOCK);
if (error != 0) {
unlink(filename);
free(filename);
@@ -623,11 +579,11 @@ nfs_disable_share(sa_share_impl_t impl_share)
if (error != SA_OK) {
unlink(filename);
free(filename);
nfs_exports_unlock();
nfs_exports_unlock(ZFS_EXPORTS_LOCK);
return (error);
}
error = nfs_fini_tmpfile(filename);
nfs_exports_unlock();
nfs_exports_unlock(ZFS_EXPORTS_LOCK);
return (error);
}