libshare: nfs: commonify nfs_enable_share()

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-16 00:40:22 +02:00 committed by Brian Behlendorf
parent 62866fc96c
commit dc3a56d38b
4 changed files with 62 additions and 91 deletions

View File

@ -38,7 +38,7 @@ static int nfs_lock_fd = -1;
* updates to the exports file. Each protocol is responsible for * updates to the exports file. Each protocol is responsible for
* providing the necessary locking to ensure consistency. * providing the necessary locking to ensure consistency.
*/ */
__attribute__((visibility("hidden"))) int static int
nfs_exports_lock(const char *name) nfs_exports_lock(const char *name)
{ {
int err; int err;
@ -61,7 +61,7 @@ nfs_exports_lock(const char *name)
return (0); return (0);
} }
__attribute__((visibility("hidden"))) void static void
nfs_exports_unlock(const char *name) nfs_exports_unlock(const char *name)
{ {
verify(nfs_lock_fd > 0); verify(nfs_lock_fd > 0);
@ -75,7 +75,7 @@ nfs_exports_unlock(const char *name)
nfs_lock_fd = -1; nfs_lock_fd = -1;
} }
__attribute__((visibility("hidden"))) char * static char *
nfs_init_tmpfile(const char *prefix, const char *mdir) nfs_init_tmpfile(const char *prefix, const char *mdir)
{ {
char *tmpfile = NULL; char *tmpfile = NULL;
@ -105,7 +105,7 @@ nfs_init_tmpfile(const char *prefix, const char *mdir)
return (tmpfile); return (tmpfile);
} }
__attribute__((visibility("hidden"))) int static int
nfs_fini_tmpfile(const char *exports, char *tmpfile) nfs_fini_tmpfile(const char *exports, char *tmpfile)
{ {
if (rename(tmpfile, exports) == -1) { if (rename(tmpfile, exports) == -1) {
@ -120,8 +120,9 @@ nfs_fini_tmpfile(const char *exports, char *tmpfile)
} }
__attribute__((visibility("hidden"))) int __attribute__((visibility("hidden"))) int
nfs_disable_share_impl(const char *lockfile, const char *exports, nfs_toggle_share(const char *lockfile, const char *exports,
const char *expdir, sa_share_impl_t impl_share) const char *expdir, sa_share_impl_t impl_share,
int(*cbk)(sa_share_impl_t impl_share, char *filename))
{ {
int error; int error;
char *filename; char *filename;
@ -137,14 +138,20 @@ nfs_disable_share_impl(const char *lockfile, const char *exports,
} }
error = nfs_copy_entries(filename, impl_share->sa_mountpoint); error = nfs_copy_entries(filename, impl_share->sa_mountpoint);
if (error != SA_OK) { if (error != SA_OK)
unlink(filename); goto fullerr;
free(filename);
nfs_exports_unlock(lockfile); error = cbk(impl_share, filename);
return (error); if (error != SA_OK)
} goto fullerr;
error = nfs_fini_tmpfile(exports, filename); error = nfs_fini_tmpfile(exports, filename);
nfs_exports_unlock(lockfile); nfs_exports_unlock(lockfile);
return (error); return (error);
fullerr:
unlink(filename);
free(filename);
nfs_exports_unlock(lockfile);
return (error);
} }

View File

@ -30,12 +30,7 @@
void libshare_nfs_init(void); void libshare_nfs_init(void);
int nfs_exports_lock(const char *name);
void nfs_exports_unlock(const char *name);
char *nfs_init_tmpfile(const char *prefix, const char *mdir);
int nfs_fini_tmpfile(const char *exports, char *tmpfile);
int nfs_copy_entries(char *filename, const char *mountpoint); int nfs_copy_entries(char *filename, const char *mountpoint);
int nfs_disable_share_impl(const char *lockfile, const char *exports, int nfs_toggle_share(const char *lockfile, const char *exports,
const char *expdir, sa_share_impl_t impl_share); const char *expdir, sa_share_impl_t impl_share,
int(*cbk)(sa_share_impl_t impl_share, char *filename));

View File

@ -193,38 +193,15 @@ nfs_copy_entries(char *filename, const char *mountpoint)
} }
static int static int
nfs_enable_share(sa_share_impl_t impl_share) nfs_enable_share_impl(sa_share_impl_t impl_share, char *filename)
{ {
char *filename = NULL;
int error;
if ((filename = nfs_init_tmpfile(ZFS_EXPORTS_FILE, NULL)) == NULL)
return (SA_SYSTEM_ERR);
error = nfs_exports_lock(ZFS_EXPORTS_LOCK);
if (error != 0) {
unlink(filename);
free(filename);
return (error);
}
error = nfs_copy_entries(filename, impl_share->sa_mountpoint);
if (error != SA_OK) {
unlink(filename);
free(filename);
nfs_exports_unlock(ZFS_EXPORTS_LOCK);
return (error);
}
FILE *fp = fopen(filename, "a+e"); FILE *fp = fopen(filename, "a+e");
if (fp == NULL) { if (fp == NULL) {
fprintf(stderr, "failed to open %s file: %s", filename, fprintf(stderr, "failed to open %s file: %s", filename,
strerror(errno)); strerror(errno));
unlink(filename);
free(filename);
nfs_exports_unlock(ZFS_EXPORTS_LOCK);
return (SA_SYSTEM_ERR); return (SA_SYSTEM_ERR);
} }
char *shareopts = FSINFO(impl_share, nfs_fstype)->shareopts; char *shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
if (strcmp(shareopts, "on") == 0) if (strcmp(shareopts, "on") == 0)
shareopts = ""; shareopts = "";
@ -233,30 +210,38 @@ nfs_enable_share(sa_share_impl_t impl_share)
translate_opts(shareopts)) < 0) { translate_opts(shareopts)) < 0) {
fprintf(stderr, "failed to write to %s\n", filename); fprintf(stderr, "failed to write to %s\n", filename);
fclose(fp); fclose(fp);
unlink(filename);
free(filename);
nfs_exports_unlock(ZFS_EXPORTS_LOCK);
return (SA_SYSTEM_ERR); return (SA_SYSTEM_ERR);
} }
if (fclose(fp) != 0) { if (fclose(fp) != 0) {
fprintf(stderr, "Unable to close file %s: %s\n", fprintf(stderr, "Unable to close file %s: %s\n",
filename, strerror(errno)); filename, strerror(errno));
unlink(filename);
free(filename);
nfs_exports_unlock(ZFS_EXPORTS_LOCK);
return (SA_SYSTEM_ERR); return (SA_SYSTEM_ERR);
} }
error = nfs_fini_tmpfile(ZFS_EXPORTS_FILE, filename);
nfs_exports_unlock(ZFS_EXPORTS_LOCK); return (SA_OK);
return (error); }
static int
nfs_enable_share(sa_share_impl_t impl_share)
{
return (nfs_toggle_share(
ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, NULL, impl_share,
nfs_enable_share_impl));
}
static int
nfs_disable_share_impl(sa_share_impl_t impl_share, char *filename)
{
return (SA_OK);
} }
static int static int
nfs_disable_share(sa_share_impl_t impl_share) nfs_disable_share(sa_share_impl_t impl_share)
{ {
return (nfs_disable_share_impl( return (nfs_toggle_share(
ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, NULL, impl_share)); ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, NULL, impl_share,
nfs_disable_share_impl));
} }
static boolean_t static boolean_t

View File

@ -467,61 +467,45 @@ nfs_copy_entries(char *filename, const char *mountpoint)
* Enables NFS sharing for the specified share. * Enables NFS sharing for the specified share.
*/ */
static int static int
nfs_enable_share(sa_share_impl_t impl_share) nfs_enable_share_impl(sa_share_impl_t impl_share, char *filename)
{ {
char *shareopts, *linux_opts; char *shareopts, *linux_opts;
char *filename = NULL;
int error; int error;
if ((filename =
nfs_init_tmpfile(ZFS_EXPORTS_FILE, ZFS_EXPORTS_DIR)) == NULL)
return (SA_SYSTEM_ERR);
error = nfs_exports_lock(ZFS_EXPORTS_LOCK);
if (error != 0) {
unlink(filename);
free(filename);
return (error);
}
error = nfs_copy_entries(filename, impl_share->sa_mountpoint);
if (error != SA_OK) {
unlink(filename);
free(filename);
nfs_exports_unlock(ZFS_EXPORTS_LOCK);
return (error);
}
shareopts = FSINFO(impl_share, nfs_fstype)->shareopts; shareopts = FSINFO(impl_share, nfs_fstype)->shareopts;
error = get_linux_shareopts(shareopts, &linux_opts); error = get_linux_shareopts(shareopts, &linux_opts);
if (error != SA_OK) { if (error != SA_OK)
unlink(filename);
free(filename);
nfs_exports_unlock(ZFS_EXPORTS_LOCK);
return (error); return (error);
}
error = foreach_nfs_host(impl_share, filename, nfs_add_entry, error = foreach_nfs_host(impl_share, filename, nfs_add_entry,
linux_opts); linux_opts);
free(linux_opts); free(linux_opts);
if (error == 0) {
error = nfs_fini_tmpfile(ZFS_EXPORTS_FILE, filename);
} else {
unlink(filename);
free(filename);
}
nfs_exports_unlock(ZFS_EXPORTS_LOCK);
return (error); return (error);
} }
static int
nfs_enable_share(sa_share_impl_t impl_share)
{
return (nfs_toggle_share(
ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, ZFS_EXPORTS_DIR, impl_share,
nfs_enable_share_impl));
}
/* /*
* Disables NFS sharing for the specified share. * Disables NFS sharing for the specified share.
*/ */
static int
nfs_disable_share_impl(sa_share_impl_t impl_share, char *filename)
{
return (SA_OK);
}
static int static int
nfs_disable_share(sa_share_impl_t impl_share) nfs_disable_share(sa_share_impl_t impl_share)
{ {
return (nfs_disable_share_impl( return (nfs_toggle_share(
ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, ZFS_EXPORTS_DIR, impl_share)); ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, ZFS_EXPORTS_DIR, impl_share,
nfs_disable_share_impl));
} }
static boolean_t static boolean_t