]> git.proxmox.com Git - mirror_zfs.git/blame - lib/libshare/os/freebsd/nfs.c
Make zfs-share service resilient to stale exports
[mirror_zfs.git] / lib / libshare / os / freebsd / nfs.c
CommitLineData
c15d36c6
GW
1/*
2 * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
ede037cd 26 * Copyright (c) 2020, 2022 by Delphix. All rights reserved.
c15d36c6
GW
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32#include <sys/param.h>
33#include <sys/vfs.h>
34
35#include <assert.h>
36#include <errno.h>
37#include <fcntl.h>
38#include <libutil.h>
39#include <signal.h>
40#include <stdio.h>
41#include <string.h>
42#include <unistd.h>
43#include <libintl.h>
44
eefaa55f 45#include <libshare.h>
c15d36c6
GW
46#include "libshare_impl.h"
47#include "nfs.h"
48
49#define _PATH_MOUNTDPID "/var/run/mountd.pid"
c15d36c6
GW
50#define OPTSSIZE 1024
51#define MAXLINESIZE (PATH_MAX + OPTSSIZE)
52#define ZFS_EXPORTS_FILE "/etc/zfs/exports"
53#define ZFS_EXPORTS_LOCK ZFS_EXPORTS_FILE".lock"
54
c15d36c6 55/*
ee668b83 56 * This function translates options to a format acceptable by exports(5), eg.
c15d36c6
GW
57 *
58 * -ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 \
59 * zfs.freebsd.org 69.147.83.54
60 *
61 * Accepted input formats:
62 *
63 * ro,network=192.168.0.0,mask=255.255.255.0,maproot=0,zfs.freebsd.org
64 * ro network=192.168.0.0 mask=255.255.255.0 maproot=0 zfs.freebsd.org
65 * -ro,-network=192.168.0.0,-mask=255.255.255.0,-maproot=0,zfs.freebsd.org
66 * -ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 \
67 * zfs.freebsd.org
68 *
69 * Recognized keywords:
70 *
71 * ro, maproot, mapall, mask, network, sec, alldirs, public, webnfs,
72 * index, quiet
c15d36c6 73 */
ee668b83
AZ
74static int
75translate_opts(const char *shareopts, FILE *out)
c15d36c6 76{
5f0c1c4e
AZ
77 static const char *const known_opts[] = { "ro", "maproot", "mapall",
78 "mask", "network", "sec", "alldirs", "public", "webnfs", "index",
79 "quiet" };
ee668b83 80 char oldopts[OPTSSIZE], newopts[OPTSSIZE];
c15d36c6
GW
81 char *o, *s = NULL;
82 unsigned int i;
83 size_t len;
84
85 strlcpy(oldopts, shareopts, sizeof (oldopts));
86 newopts[0] = '\0';
87 s = oldopts;
88 while ((o = strsep(&s, "-, ")) != NULL) {
89 if (o[0] == '\0')
90 continue;
5f0c1c4e 91 for (i = 0; i < ARRAY_SIZE(known_opts); ++i) {
c15d36c6
GW
92 len = strlen(known_opts[i]);
93 if (strncmp(known_opts[i], o, len) == 0 &&
94 (o[len] == '\0' || o[len] == '=')) {
95 strlcat(newopts, "-", sizeof (newopts));
96 break;
97 }
98 }
99 strlcat(newopts, o, sizeof (newopts));
100 strlcat(newopts, " ", sizeof (newopts));
101 }
ee668b83 102 return (fputs(newopts, out));
c15d36c6
GW
103}
104
c15d36c6 105static int
c53f2e9b 106nfs_enable_share_impl(sa_share_impl_t impl_share, FILE *tmpfile)
c15d36c6 107{
63ce6dd9 108 const char *shareopts = impl_share->sa_shareopts;
c15d36c6
GW
109 if (strcmp(shareopts, "on") == 0)
110 shareopts = "";
111
9b06aa63
AZ
112 boolean_t need_free;
113 char *mp;
114 int rc = nfs_escape_mountpoint(impl_share->sa_mountpoint, &mp,
115 &need_free);
116 if (rc != SA_OK)
117 return (rc);
118
119 if (fputs(mp, tmpfile) == EOF ||
ee668b83
AZ
120 fputc('\t', tmpfile) == EOF ||
121 translate_opts(shareopts, tmpfile) == EOF ||
122 fputc('\n', tmpfile) == EOF) {
c53f2e9b 123 fprintf(stderr, "failed to write to temporary file\n");
9b06aa63 124 rc = SA_SYSTEM_ERR;
c15d36c6 125 }
dc3a56d3 126
9b06aa63
AZ
127 if (need_free)
128 free(mp);
129 return (rc);
dc3a56d3
AZ
130}
131
132static int
133nfs_enable_share(sa_share_impl_t impl_share)
134{
135 return (nfs_toggle_share(
136 ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, NULL, impl_share,
137 nfs_enable_share_impl));
138}
139
140static int
c53f2e9b 141nfs_disable_share_impl(sa_share_impl_t impl_share, FILE *tmpfile)
dc3a56d3 142{
57bff80e 143 (void) impl_share, (void) tmpfile;
dc3a56d3 144 return (SA_OK);
c15d36c6
GW
145}
146
147static int
148nfs_disable_share(sa_share_impl_t impl_share)
149{
dc3a56d3
AZ
150 return (nfs_toggle_share(
151 ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, NULL, impl_share,
152 nfs_disable_share_impl));
c15d36c6
GW
153}
154
c15d36c6
GW
155static boolean_t
156nfs_is_shared(sa_share_impl_t impl_share)
157{
605e03e5 158 return (nfs_is_shared_impl(ZFS_EXPORTS_FILE, impl_share));
c15d36c6
GW
159}
160
161static int
162nfs_validate_shareopts(const char *shareopts)
163{
46c7a802 164 (void) shareopts;
c15d36c6
GW
165 return (SA_OK);
166}
167
c15d36c6
GW
168/*
169 * Commit the shares by restarting mountd.
170 */
171static int
172nfs_commit_shares(void)
173{
174 struct pidfh *pfh;
175 pid_t mountdpid;
176
bdf6464c 177start:
c15d36c6
GW
178 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
179 if (pfh != NULL) {
bdf6464c 180 /* mountd(8) is not running. */
c15d36c6
GW
181 pidfile_remove(pfh);
182 return (SA_OK);
183 }
184 if (errno != EEXIST) {
185 /* Cannot open pidfile for some reason. */
186 return (SA_SYSTEM_ERR);
187 }
bdf6464c
AZ
188 if (mountdpid == -1) {
189 /* mountd(8) exists, but didn't write the PID yet */
190 usleep(500);
191 goto start;
192 }
c15d36c6
GW
193 /* We have mountd(8) PID in mountdpid variable. */
194 kill(mountdpid, SIGHUP);
195 return (SA_OK);
196}
197
ede037cd
DB
198static void
199nfs_truncate_shares(void)
200{
201 nfs_reset_shares(ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE);
202}
203
471e9a10 204const sa_fstype_t libshare_nfs_type = {
c15d36c6
GW
205 .enable_share = nfs_enable_share,
206 .disable_share = nfs_disable_share,
207 .is_shared = nfs_is_shared,
208
209 .validate_shareopts = nfs_validate_shareopts,
c15d36c6 210 .commit_shares = nfs_commit_shares,
ede037cd 211 .truncate_shares = nfs_truncate_shares,
c15d36c6 212};