2 * utils/exportfs/exportfs.c
4 * Export file systems to knfsd
6 * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
8 * Extensive changes, 1999, Neil Brown <neilb@cse.unsw.edu.au>
31 static void export_all(int verbose);
32 static void exportfs(char *arg, char *options, int verbose);
33 static void unexportfs(char *arg, int verbose);
34 static void exports_update(int verbose);
35 static void dump(int verbose);
36 static void error(nfs_export *exp, int err);
37 static void usage(void);
38 static void validate_export(nfs_export *exp);
41 main(int argc, char **argv)
53 xlog_open("exportfs");
57 while ((c = getopt(argc, argv, "aio:ruvf")) != EOF) {
87 if (optind != argc && f_all) {
88 fprintf(stderr,"exportfs: extra arguments are not permitted with -a or -r.\n");
91 if (f_ignore && (f_all || ! f_export)) {
92 fprintf(stderr,"exportfs: -i not meaningful with -a, -r or -u.\n");
95 if (f_reexport && ! f_export) {
96 fprintf(stderr, "exportfs: -r and -u are incompatible.\n");
99 new_cache = check_new_cache();
100 if (optind == argc && ! f_all) {
105 fprintf(stderr, "exportfs: -f: only available with new cache controls: mount /proc/fs/nfsd first\n");
115 if (f_export && ! f_ignore)
116 export_read(_PATH_EXPORTS);
119 export_all(f_verbose);
121 for (i = optind; i < argc ; i++)
122 exportfs(argv[i], options, f_verbose);
124 /* If we are unexporting everything, then
125 * don't care about what should be exported, as that
126 * may require DNS lookups..
128 if (! ( !f_export && f_all)) {
129 /* note: xtab_*_read does not update entries if they already exist,
130 * so this will not lose new options
135 for (i = optind ; i < argc ; i++)
136 unexportfs(argv[i], f_verbose);
142 exports_update(f_verbose);
146 cache_flush(force_flush);
154 exports_update_one(nfs_export *exp, int verbose)
156 /* check mountpoint option */
157 if (exp->m_mayexport &&
158 exp->m_export.e_mountpoint &&
159 !is_mountpoint(exp->m_export.e_mountpoint[0]?
160 exp->m_export.e_mountpoint:
161 exp->m_export.e_path)) {
162 printf("%s not exported as %s not a mountpoint.\n",
163 exp->m_export.e_path, exp->m_export.e_mountpoint);
164 exp->m_mayexport = 0;
166 if (exp->m_mayexport && ((exp->m_exported<1) || exp->m_changed)) {
168 printf("%sexporting %s:%s to kernel\n",
169 exp->m_exported ?"re":"",
170 exp->m_client->m_hostname,
171 exp->m_export.e_path);
172 if (!export_export(exp))
175 if (exp->m_exported && ! exp->m_mayexport) {
177 printf("unexporting %s:%s from kernel\n",
178 exp->m_client->m_hostname,
179 exp->m_export.e_path);
180 if (!export_unexport(exp))
186 /* we synchronise intention with reality.
187 * entries with m_mayexport get exported
188 * entries with m_exported but not m_mayexport get unexported
189 * looking at m_client->m_type == MCL_FQDN and m_client->m_type == MCL_GSS only
192 exports_update(int verbose)
196 for (exp = exportlist[MCL_FQDN].p_head; exp; exp=exp->m_next) {
197 exports_update_one(exp, verbose);
199 for (exp = exportlist[MCL_GSS].p_head; exp; exp=exp->m_next) {
200 exports_update_one(exp, verbose);
205 * export_all finds all entries and
206 * marks them xtabent and mayexport so that they get exported
209 export_all(int verbose)
214 for (i = 0; i < MCL_MAXTYPES; i++) {
215 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
217 printf("exporting %s:%s\n",
218 exp->m_client->m_hostname,
219 exp->m_export.e_path);
221 exp->m_mayexport = 1;
224 validate_export(exp);
231 exportfs(char *arg, char *options, int verbose)
233 struct exportent *eep;
235 struct hostent *hp = NULL;
240 if ((path = strchr(arg, ':')) != NULL)
243 if (!path || *path != '/') {
244 fprintf(stderr, "Invalid exporting option: %s\n", arg);
248 if ((htype = client_gettype(hname)) == MCL_FQDN &&
249 (hp = gethostbyname(hname)) != NULL) {
250 struct hostent *hp2 = hostent_dup (hp);
251 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
255 hp = hostent_dup(hp);
258 exp = export_find(hp, path);
261 exp = export_lookup(hname, path, 0);
265 if (!(eep = mkexportent(hname, path, options)) ||
266 !(exp = export_create(eep, 0)))
268 } else if (!updateexportent(&exp->m_export, options))
272 printf("exporting %s:%s\n", exp->m_client->m_hostname,
273 exp->m_export.e_path);
275 exp->m_mayexport = 1;
278 validate_export(exp);
285 unexportfs(char *arg, int verbose)
288 struct addrinfo *ai = NULL;
293 if ((path = strchr(arg, ':')) != NULL)
296 if (!path || *path != '/') {
297 fprintf(stderr, "Invalid unexporting option: %s\n",
302 if ((htype = client_gettype(hname)) == MCL_FQDN) {
303 ai = host_addrinfo(hname);
305 hname = ai->ai_canonname;
308 for (exp = exportlist[htype].p_head; exp; exp = exp->m_next) {
309 if (path && strcmp(path, exp->m_export.e_path))
311 if (htype != exp->m_client->m_type)
313 if (htype == MCL_FQDN
314 && !matchhostname(exp->m_export.e_hostname,
317 if (htype != MCL_FQDN
318 && strcasecmp(exp->m_export.e_hostname, hname))
322 if (exp->m_exported) {
323 printf("unexporting %s:%s from kernel\n",
324 exp->m_client->m_hostname,
325 exp->m_export.e_path);
329 printf("unexporting %s:%s\n",
330 exp->m_client->m_hostname,
331 exp->m_export.e_path);
334 if (exp->m_exported && !export_unexport(exp))
338 exp->m_mayexport = 0;
344 static int can_test(void)
348 char *setup = "nfsd 0.0.0.0 2147483647 -test-client-\n";
349 fd = open("/proc/net/rpc/auth.unix.ip/channel", O_WRONLY);
350 if ( fd < 0) return 0;
351 n = write(fd, setup, strlen(setup));
355 fd = open("/proc/net/rpc/nfsd.export/channel", O_WRONLY);
356 if ( fd < 0) return 0;
361 static int test_export(char *path, int with_fsid)
366 sprintf(buf, "-test-client- %s 3 %d -1 -1 0\n",
368 with_fsid ? NFSEXP_FSID : 0);
369 fd = open("/proc/net/rpc/nfsd.export/channel", O_WRONLY);
372 n = write(fd, buf, strlen(buf));
380 validate_export(nfs_export *exp)
382 /* Check that the given export point is potentially exportable.
383 * We just give warnings here, don't cause anything to fail.
384 * If a path doesn't exist, or is not a dir or file, give an warning
385 * otherwise trial-export to '-test-client-' and check for failure.
388 char *path = exp->m_export.e_path;
392 if (stat(path, &stb) < 0) {
393 fprintf(stderr, "exportfs: Warning: %s does not exist\n",
397 if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
398 fprintf(stderr, "exportfs: Warning: %s is neither "
399 "a directory nor a file.\n"
400 " remote access will fail\n", path);
406 if (!statfs64(path, &stf) &&
407 (stf.f_fsid.__val[0] || stf.f_fsid.__val[1]))
410 if ((exp->m_export.e_flags & NFSEXP_FSID) || exp->m_export.e_uuid ||
412 if ( !test_export(path, 1)) {
413 fprintf(stderr, "exportfs: Warning: %s does not "
414 "support NFS export.\n",
418 } else if ( ! test_export(path, 0)) {
419 if (test_export(path, 1))
420 fprintf(stderr, "exportfs: Warning: %s requires fsid= "
421 "for NFS export\n", path);
423 fprintf(stderr, "exportfs: Warning: %s does not "
424 "support NFS export.\n",
433 dumpopt(char c, char *fmt, ...)
448 struct exportent *ep;
452 for (htype = 0; htype < MCL_MAXTYPES; htype++) {
453 for (exp = exportlist[htype].p_head; exp; exp = exp->m_next) {
456 continue; /* neilb */
457 if (htype == MCL_ANONYMOUS)
460 hname = ep->e_hostname;
461 if (strlen(ep->e_path) > 14)
462 printf("%-14s\n\t\t%s", ep->e_path, hname);
464 printf("%-14s\t%s", ep->e_path, hname);
470 if (ep->e_flags & NFSEXP_READONLY)
471 c = dumpopt(c, "ro");
473 c = dumpopt(c, "rw");
474 if (ep->e_flags & NFSEXP_ASYNC)
475 c = dumpopt(c, "async");
476 if (ep->e_flags & NFSEXP_GATHERED_WRITES)
477 c = dumpopt(c, "wdelay");
478 if (ep->e_flags & NFSEXP_NOHIDE)
479 c = dumpopt(c, "nohide");
480 if (ep->e_flags & NFSEXP_CROSSMOUNT)
481 c = dumpopt(c, "crossmnt");
482 if (ep->e_flags & NFSEXP_INSECURE_PORT)
483 c = dumpopt(c, "insecure");
484 if (ep->e_flags & NFSEXP_ROOTSQUASH)
485 c = dumpopt(c, "root_squash");
487 c = dumpopt(c, "no_root_squash");
488 if (ep->e_flags & NFSEXP_ALLSQUASH)
489 c = dumpopt(c, "all_squash");
490 if (ep->e_flags & NFSEXP_NOSUBTREECHECK)
491 c = dumpopt(c, "no_subtree_check");
492 if (ep->e_flags & NFSEXP_NOAUTHNLM)
493 c = dumpopt(c, "insecure_locks");
494 if (ep->e_flags & NFSEXP_NOACL)
495 c = dumpopt(c, "no_acl");
496 if (ep->e_flags & NFSEXP_FSID)
497 c = dumpopt(c, "fsid=%d", ep->e_fsid);
499 c = dumpopt(c, "fsid=%s", ep->e_uuid);
500 if (ep->e_mountpoint)
501 c = dumpopt(c, "mountpoint%s%s",
502 ep->e_mountpoint[0]?"=":"",
504 if (ep->e_anonuid != 65534)
505 c = dumpopt(c, "anonuid=%d", ep->e_anonuid);
506 if (ep->e_anongid != 65534)
507 c = dumpopt(c, "anongid=%d", ep->e_anongid);
508 switch(ep->e_fslocmethod) {
512 c = dumpopt(c, "refer=%s", ep->e_fslocdata);
515 c = dumpopt(c, "replicas=%s", ep->e_fslocdata);
519 c = dumpopt(c, "fsloc=stub");
523 secinfo_show(stdout, ep);
524 printf("%c\n", (c != '(')? ')' : ' ');
530 error(nfs_export *exp, int err)
532 fprintf(stderr, "%s:%s: %s\n", exp->m_client->m_hostname,
533 exp->m_export.e_path, strerror(err));
539 fprintf(stderr, "usage: exportfs [-aruv] [host:/path]\n");