]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/exportfs/exportfs.c
Fix cache flushing problem.
[nfs-utils.git] / utils / exportfs / exportfs.c
1 /*
2  * utils/exportfs/exportfs.c
3  *
4  * Export file systems to knfsd
5  *
6  * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
7  *
8  * Extensive changes, 1999, Neil Brown <neilb@cse.unsw.edu.au>
9  */
10
11 #include "config.h"
12
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdarg.h>
16 #include <getopt.h>
17 #include <netdb.h>
18 #include <errno.h>
19 #include "xmalloc.h"
20 #include "misc.h"
21 #include "nfslib.h"
22 #include "exportfs.h"
23 #include "xmalloc.h"
24 #include "xlog.h"
25
26 static void     export_all(int verbose);
27 static void     exportfs(char *arg, char *options, int verbose);
28 static void     unexportfs(char *arg, int verbose);
29 static void     exports_update(int verbose);
30 static void     dump(int verbose);
31 static void     error(nfs_export *exp, int err);
32 static void     usage(void);
33
34
35 int
36 main(int argc, char **argv)
37 {
38         char    *options = NULL;
39         int     f_export = 1;
40         int     f_all = 0;
41         int     f_verbose = 0;
42         int     f_reexport = 0;
43         int     f_ignore = 0;
44         int     i, c;
45         int     new_cache = 0;
46         int     force_flush = 0;
47
48         xlog_open("exportfs");
49
50         export_errno = 0;
51
52         while ((c = getopt(argc, argv, "aio:ruvf")) != EOF) {
53                 switch(c) {
54                 case 'a':
55                         f_all = 1;
56                         break;
57                 case 'i':
58                         f_ignore = 1;
59                         break;
60                 case 'o':
61                         options = optarg;
62                         break;
63                 case 'r':
64                         f_reexport = 1;
65                         f_all = 1;
66                         break;
67                 case 'u':
68                         f_export = 0;
69                         break;
70                 case 'v':
71                         f_verbose = 1;
72                         break;
73                 case 'f':
74                         force_flush = 1;
75                         break;
76                 default:
77                         usage();
78                         break;
79                 }
80         }
81
82         if (optind != argc && f_all) {
83                 fprintf(stderr,"exportfs: extra arguments are not permitted with -a or -r.\n");
84                 return 1;
85         }
86         if (f_ignore && (f_all || ! f_export)) {
87                 fprintf(stderr,"exportfs: -i not meaningful with -a, -r or -u.\n");
88                 return 1;
89         }
90         if (f_reexport && ! f_export) {
91                 fprintf(stderr, "exportfs: -r and -u are incompatible.\n");
92                 return 1;
93         }
94         new_cache = check_new_cache();
95         if (optind == argc && ! f_all) {
96                 if (force_flush) {
97                         if (new_cache)
98                                 cache_flush(1);
99                         else {
100                                 fprintf(stderr, "exportfs: -f: only available with new cache controls: mount /proc/fs/nfsd first\n");
101                                 exit(1);
102                         }
103                         return 0;
104                 } else {
105                         xtab_export_read();
106                         dump(f_verbose);
107                         return 0;
108                 }
109         }
110
111         if (f_export && ! f_ignore)
112                 export_read(_PATH_EXPORTS);
113         if (f_export) {
114                 if (f_all)
115                         export_all(f_verbose);
116                 else
117                         for (i = optind; i < argc ; i++)
118                                 exportfs(argv[i], options, f_verbose);
119         }
120         /* If we are unexporting everything, then
121          * don't care about what should be exported, as that
122          * may require DNS lookups..
123          */
124         if (! ( !f_export && f_all)) {
125                 /* note: xtab_*_read does not update entries if they already exist,
126                  * so this will not lose new options
127                  */
128                 if (!f_reexport)
129                         xtab_export_read();
130                 if (!f_export)
131                         for (i = optind ; i < argc ; i++)
132                                 unexportfs(argv[i], f_verbose);
133                 if (!new_cache)
134                         rmtab_read();
135         }
136         if (!new_cache) {
137                 xtab_mount_read();
138                 exports_update(f_verbose);
139         }
140         xtab_export_write();
141         if (new_cache)
142                 cache_flush(force_flush);
143         if (!new_cache)
144                 xtab_mount_write();
145
146         return export_errno;
147 }
148
149 /* we synchronise intention with reality.
150  * entries with m_mayexport get exported
151  * entries with m_exported but not m_mayexport get unexported
152  * looking at m_client->m_type == MCL_FQDN only
153  */
154 static void
155 exports_update(int verbose)
156 {
157         nfs_export      *exp;
158
159         for (exp = exportlist[MCL_FQDN]; exp; exp=exp->m_next) {
160                 /* check mountpoint option */
161                 if (exp->m_mayexport && 
162                     exp->m_export.e_mountpoint &&
163                     !is_mountpoint(exp->m_export.e_mountpoint[0]?
164                                    exp->m_export.e_mountpoint:
165                                    exp->m_export.e_path)) {
166                         printf("%s not exported as %s not a mountpoint.\n",
167                                exp->m_export.e_path, exp->m_export.e_mountpoint);
168                         exp->m_mayexport = 0;
169                 }
170                 if (exp->m_mayexport && ((exp->m_exported<1) || exp->m_changed)) {
171                         if (verbose)
172                                 printf("%sexporting %s:%s to kernel\n",
173                                        exp->m_exported ?"re":"",
174                                        exp->m_client->m_hostname,
175                                        exp->m_export.e_path);
176                         if (!export_export(exp))
177                                 error(exp, errno);
178                 }
179                 if (exp->m_exported && ! exp->m_mayexport) {
180                         if (verbose)
181                                 printf("unexporting %s:%s from kernel\n",
182                                        exp->m_client->m_hostname,
183                                        exp->m_export.e_path);
184                         if (!export_unexport(exp))
185                                 error(exp, errno);
186                 }
187         }
188 }
189                         
190 /*
191  * export_all finds all entries and
192  *    marks them xtabent and mayexport so that they get exported
193  */
194 static void
195 export_all(int verbose)
196 {
197         nfs_export      *exp;
198         int             i;
199
200         for (i = 0; i < MCL_MAXTYPES; i++) {
201                 for (exp = exportlist[i]; exp; exp = exp->m_next) {
202                         if (verbose)
203                                 printf("exporting %s:%s\n",
204                                        exp->m_client->m_hostname, 
205                                        exp->m_export.e_path);
206                         exp->m_xtabent = 1;
207                         exp->m_mayexport = 1;
208                         exp->m_changed = 1;
209                 }
210         }
211 }
212
213
214 static void
215 exportfs(char *arg, char *options, int verbose)
216 {
217         struct exportent *eep;
218         nfs_export      *exp;
219         struct hostent  *hp = NULL;
220         char            *path;
221         char            *hname = arg;
222         int             htype;
223
224         if ((path = strchr(arg, ':')) != NULL)
225                 *path++ = '\0';
226
227         if (!path || *path != '/') {
228                 fprintf(stderr, "Invalid exporting option: %s\n", arg);
229                 return;
230         }
231
232         if ((htype = client_gettype(hname)) == MCL_FQDN &&
233             (hp = gethostbyname(hname)) != NULL) {
234                 struct hostent *hp2 = hostent_dup (hp);
235                 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
236                                    hp2->h_addrtype);
237                 if (hp) {
238                         free(hp2);
239                         hp = hostent_dup(hp);
240                 } else
241                         hp = hp2;
242                 exp = export_find(hp, path);
243                 hname = hp->h_name;
244         } else {
245                 exp = export_lookup(hname, path, 0);
246         }
247
248         if (!exp) {
249                 if (!(eep = mkexportent(hname, path, options)) ||
250                     !(exp = export_create(eep, 0))) {
251                         if (hp) free (hp);
252                         return;
253                 }
254         } else if (!updateexportent(&exp->m_export, options)) {
255                 if (hp) free (hp);
256                 return;
257         }
258
259         if (verbose)
260                 printf("exporting %s:%s\n", exp->m_client->m_hostname, 
261                         exp->m_export.e_path);
262         exp->m_xtabent = 1;
263         exp->m_mayexport = 1;
264         exp->m_changed = 1;
265         if (hp) free (hp);
266 }
267
268 static void
269 unexportfs(char *arg, int verbose)
270 {
271         nfs_export      *exp;
272         struct hostent  *hp = NULL;
273         char            *path;
274         char            *hname = arg;
275         int             htype;
276
277         if ((path = strchr(arg, ':')) != NULL)
278                 *path++ = '\0';
279
280         if (!path || *path != '/') {
281                 fprintf(stderr, "Invalid unexporting option: %s\n",
282                         arg);
283                 return;
284         }
285
286         if ((htype = client_gettype(hname)) == MCL_FQDN) {
287                 if ((hp = gethostbyname(hname)) != 0) {
288                         hp = hostent_dup (hp);
289                         hname = (char *) hp->h_name;
290                 }
291         }
292
293         for (exp = exportlist[htype]; exp; exp = exp->m_next) {
294                 if (path && strcmp(path, exp->m_export.e_path))
295                         continue;
296                 if (htype != exp->m_client->m_type)
297                         continue;
298                 if (htype == MCL_FQDN
299                     && !matchhostname(exp->m_export.e_hostname,
300                                           hname))
301                         continue;
302                 if (htype != MCL_FQDN
303                     && strcasecmp(exp->m_export.e_hostname, hname))
304                         continue;
305                 if (verbose) {
306 #if 0
307                         if (exp->m_exported) {
308                                 printf("unexporting %s:%s from kernel\n",
309                                        exp->m_client->m_hostname,
310                                        exp->m_export.e_path);
311                         }
312                         else
313 #endif
314                                 printf("unexporting %s:%s\n",
315                                         exp->m_client->m_hostname, 
316                                         exp->m_export.e_path);
317                 }
318 #if 0
319                 if (exp->m_exported && !export_unexport(exp))
320                         error(exp, errno);
321 #endif
322                 exp->m_xtabent = 0;
323                 exp->m_mayexport = 0;
324         }
325
326         if (hp) free (hp);
327 }
328
329 static char
330 dumpopt(char c, char *fmt, ...)
331 {
332         va_list ap;
333
334         va_start(ap, fmt);
335         printf("%c", c);
336         vprintf(fmt, ap);
337         va_end(ap);
338         return ',';
339 }
340
341 static void
342 dump(int verbose)
343 {
344         nfs_export      *exp;
345         struct exportent *ep;
346         int             htype;
347         char            *hname, c;
348
349         for (htype = 0; htype < MCL_MAXTYPES; htype++) {
350                 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
351                         ep = &exp->m_export;
352                         if (!exp->m_xtabent)
353                             continue; /* neilb */
354                         if (htype == MCL_ANONYMOUS)
355                                 hname = "<world>";
356                         else
357                                 hname = ep->e_hostname;
358                         if (strlen(ep->e_path) > 14)
359                                 printf("%-14s\n\t\t%s", ep->e_path, hname);
360                         else
361                                 printf("%-14s\t%s", ep->e_path, hname);
362                         if (!verbose) {
363                                 printf("\n");
364                                 continue;
365                         }
366                         c = '(';
367                         if (ep->e_flags & NFSEXP_READONLY)
368                                 c = dumpopt(c, "ro");
369                         else
370                                 c = dumpopt(c, "rw");
371                         if (ep->e_flags & NFSEXP_ASYNC)
372                                 c = dumpopt(c, "async");
373                         if (ep->e_flags & NFSEXP_GATHERED_WRITES)
374                                 c = dumpopt(c, "wdelay");
375                         if (ep->e_flags & NFSEXP_NOHIDE)
376                                 c = dumpopt(c, "nohide");
377                         if (ep->e_flags & NFSEXP_CROSSMOUNT)
378                                 c = dumpopt(c, "crossmnt");
379                         if (ep->e_flags & NFSEXP_INSECURE_PORT)
380                                 c = dumpopt(c, "insecure");
381                         if (ep->e_flags & NFSEXP_ROOTSQUASH)
382                                 c = dumpopt(c, "root_squash");
383                         else
384                                 c = dumpopt(c, "no_root_squash");
385                         if (ep->e_flags & NFSEXP_ALLSQUASH)
386                                 c = dumpopt(c, "all_squash");
387                         if (ep->e_flags & NFSEXP_NOSUBTREECHECK)
388                                 c = dumpopt(c, "no_subtree_check");
389                         if (ep->e_flags & NFSEXP_NOAUTHNLM)
390                                 c = dumpopt(c, "insecure_locks");
391                         if (ep->e_flags & NFSEXP_FSID)
392                                 c = dumpopt(c, "fsid=%d", ep->e_fsid);
393                         if (ep->e_mountpoint)
394                                 c = dumpopt(c, "mountpoint%s%s", 
395                                             ep->e_mountpoint[0]?"=":"", 
396                                             ep->e_mountpoint);
397                         if (ep->e_maptype == CLE_MAP_UGIDD)
398                                 c = dumpopt(c, "mapping=ugidd");
399                         else if (ep->e_maptype == CLE_MAP_FILE)
400                                 c = dumpopt(c, "mapping=file");
401                         if (ep->e_anonuid != -2)
402                                 c = dumpopt(c, "anonuid=%d", ep->e_anonuid);
403                         if (ep->e_anongid != -2)
404                                 c = dumpopt(c, "anongid=%d", ep->e_anongid);
405
406                         printf("%c\n", (c != '(')? ')' : ' ');
407                 }
408         }
409 }
410
411 static void
412 error(nfs_export *exp, int err)
413 {
414         fprintf(stderr, "%s:%s: %s\n", exp->m_client->m_hostname, 
415                 exp->m_export.e_path, strerror(err));
416 }
417
418 static void
419 usage(void)
420 {
421         fprintf(stderr, "usage: exportfs [-aruv] [host:/path]\n");
422         exit(1);
423 }