mirror of
https://git.proxmox.com/git/mirror_zfs.git
synced 2026-05-22 02:27:36 +03:00
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:
@@ -47,7 +47,6 @@ __FBSDID("$FreeBSD$");
|
||||
#include "nfs.h"
|
||||
|
||||
#define _PATH_MOUNTDPID "/var/run/mountd.pid"
|
||||
#define FILE_HEADER "# !!! DO NOT EDIT THIS FILE MANUALLY !!!\n\n"
|
||||
#define OPTSSIZE 1024
|
||||
#define MAXLINESIZE (PATH_MAX + OPTSSIZE)
|
||||
#define ZFS_EXPORTS_FILE "/etc/zfs/exports"
|
||||
@@ -55,49 +54,6 @@ __FBSDID("$FreeBSD$");
|
||||
|
||||
static sa_fstype_t *nfs_fstype;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/*
|
||||
* Read one line from a file. Skip comments, empty lines and a line with a
|
||||
* mountpoint specified in the 'skip' argument.
|
||||
@@ -281,7 +237,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);
|
||||
@@ -292,7 +248,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);
|
||||
}
|
||||
|
||||
@@ -302,7 +258,7 @@ nfs_enable_share(sa_share_impl_t impl_share)
|
||||
strerror(errno));
|
||||
unlink(filename);
|
||||
free(filename);
|
||||
nfs_exports_unlock();
|
||||
nfs_exports_unlock(ZFS_EXPORTS_LOCK);
|
||||
return (SA_SYSTEM_ERR);
|
||||
}
|
||||
char *shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
|
||||
@@ -315,7 +271,7 @@ nfs_enable_share(sa_share_impl_t impl_share)
|
||||
fclose(fp);
|
||||
unlink(filename);
|
||||
free(filename);
|
||||
nfs_exports_unlock();
|
||||
nfs_exports_unlock(ZFS_EXPORTS_LOCK);
|
||||
return (SA_SYSTEM_ERR);
|
||||
}
|
||||
|
||||
@@ -324,11 +280,11 @@ nfs_enable_share(sa_share_impl_t impl_share)
|
||||
filename, strerror(errno));
|
||||
unlink(filename);
|
||||
free(filename);
|
||||
nfs_exports_unlock();
|
||||
nfs_exports_unlock(ZFS_EXPORTS_LOCK);
|
||||
return (SA_SYSTEM_ERR);
|
||||
}
|
||||
error = nfs_fini_tmpfile(filename);
|
||||
nfs_exports_unlock();
|
||||
nfs_exports_unlock(ZFS_EXPORTS_LOCK);
|
||||
return (error);
|
||||
}
|
||||
|
||||
@@ -341,7 +297,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);
|
||||
@@ -352,12 +308,12 @@ 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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user