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>
28 static void export_all(int verbose);
29 static void exportfs(char *arg, char *options, int verbose);
30 static void unexportfs(char *arg, int verbose);
31 static void exports_update(int verbose);
32 static void dump(int verbose);
33 static void error(nfs_export *exp, int err);
34 static void usage(void);
38 main(int argc, char **argv)
50 xlog_open("exportfs");
54 while ((c = getopt(argc, argv, "aio:ruvf")) != EOF) {
84 if (optind != argc && f_all) {
85 fprintf(stderr,"exportfs: extra arguments are not permitted with -a or -r.\n");
88 if (f_ignore && (f_all || ! f_export)) {
89 fprintf(stderr,"exportfs: -i not meaningful with -a, -r or -u.\n");
92 if (f_reexport && ! f_export) {
93 fprintf(stderr, "exportfs: -r and -u are incompatible.\n");
96 new_cache = check_new_cache();
97 if (optind == argc && ! f_all) {
102 fprintf(stderr, "exportfs: -f: only available with new cache controls: mount /proc/fs/nfsd first\n");
113 if (f_export && ! f_ignore)
114 export_read(_PATH_EXPORTS);
117 export_all(f_verbose);
119 for (i = optind; i < argc ; i++)
120 exportfs(argv[i], options, f_verbose);
122 /* If we are unexporting everything, then
123 * don't care about what should be exported, as that
124 * may require DNS lookups..
126 if (! ( !f_export && f_all)) {
127 /* note: xtab_*_read does not update entries if they already exist,
128 * so this will not lose new options
133 for (i = optind ; i < argc ; i++)
134 unexportfs(argv[i], f_verbose);
140 exports_update(f_verbose);
144 cache_flush(force_flush);
152 exports_update_one(nfs_export *exp, int verbose)
154 /* check mountpoint option */
155 if (exp->m_mayexport &&
156 exp->m_export.e_mountpoint &&
157 !is_mountpoint(exp->m_export.e_mountpoint[0]?
158 exp->m_export.e_mountpoint:
159 exp->m_export.e_path)) {
160 printf("%s not exported as %s not a mountpoint.\n",
161 exp->m_export.e_path, exp->m_export.e_mountpoint);
162 exp->m_mayexport = 0;
164 if (exp->m_mayexport && ((exp->m_exported<1) || exp->m_changed)) {
166 printf("%sexporting %s:%s to kernel\n",
167 exp->m_exported ?"re":"",
168 exp->m_client->m_hostname,
169 exp->m_export.e_path);
170 if (!export_export(exp))
173 if (exp->m_exported && ! exp->m_mayexport) {
175 printf("unexporting %s:%s from kernel\n",
176 exp->m_client->m_hostname,
177 exp->m_export.e_path);
178 if (!export_unexport(exp))
184 /* we synchronise intention with reality.
185 * entries with m_mayexport get exported
186 * entries with m_exported but not m_mayexport get unexported
187 * looking at m_client->m_type == MCL_FQDN and m_client->m_type == MCL_GSS only
190 exports_update(int verbose)
194 for (exp = exportlist[MCL_FQDN]; exp; exp=exp->m_next) {
195 exports_update_one(exp, verbose);
197 for (exp = exportlist[MCL_GSS]; exp; exp=exp->m_next) {
198 exports_update_one(exp, verbose);
203 * export_all finds all entries and
204 * marks them xtabent and mayexport so that they get exported
207 export_all(int verbose)
212 for (i = 0; i < MCL_MAXTYPES; i++) {
213 for (exp = exportlist[i]; exp; exp = exp->m_next) {
215 printf("exporting %s:%s\n",
216 exp->m_client->m_hostname,
217 exp->m_export.e_path);
219 exp->m_mayexport = 1;
228 exportfs(char *arg, char *options, int verbose)
230 struct exportent *eep;
232 struct hostent *hp = NULL;
237 if ((path = strchr(arg, ':')) != NULL)
240 if (!path || *path != '/') {
241 fprintf(stderr, "Invalid exporting option: %s\n", arg);
245 if ((htype = client_gettype(hname)) == MCL_FQDN &&
246 (hp = gethostbyname(hname)) != NULL) {
247 struct hostent *hp2 = hostent_dup (hp);
248 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
252 hp = hostent_dup(hp);
255 exp = export_find(hp, path);
258 exp = export_lookup(hname, path, 0);
262 if (!(eep = mkexportent(hname, path, options)) ||
263 !(exp = export_create(eep, 0))) {
267 } else if (!updateexportent(&exp->m_export, options)) {
273 printf("exporting %s:%s\n", exp->m_client->m_hostname,
274 exp->m_export.e_path);
276 exp->m_mayexport = 1;
283 unexportfs(char *arg, int verbose)
286 struct hostent *hp = NULL;
291 if ((path = strchr(arg, ':')) != NULL)
294 if (!path || *path != '/') {
295 fprintf(stderr, "Invalid unexporting option: %s\n",
300 if ((htype = client_gettype(hname)) == MCL_FQDN) {
301 if ((hp = gethostbyname(hname)) != 0) {
302 hp = hostent_dup (hp);
303 hname = (char *) hp->h_name;
307 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
308 if (path && strcmp(path, exp->m_export.e_path))
310 if (htype != exp->m_client->m_type)
312 if (htype == MCL_FQDN
313 && !matchhostname(exp->m_export.e_hostname,
316 if (htype != MCL_FQDN
317 && strcasecmp(exp->m_export.e_hostname, hname))
321 if (exp->m_exported) {
322 printf("unexporting %s:%s from kernel\n",
323 exp->m_client->m_hostname,
324 exp->m_export.e_path);
328 printf("unexporting %s:%s\n",
329 exp->m_client->m_hostname,
330 exp->m_export.e_path);
333 if (exp->m_exported && !export_unexport(exp))
337 exp->m_mayexport = 0;
344 dumpopt(char c, char *fmt, ...)
359 struct exportent *ep;
363 for (htype = 0; htype < MCL_MAXTYPES; htype++) {
364 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
367 continue; /* neilb */
368 if (htype == MCL_ANONYMOUS)
371 hname = ep->e_hostname;
372 if (strlen(ep->e_path) > 14)
373 printf("%-14s\n\t\t%s", ep->e_path, hname);
375 printf("%-14s\t%s", ep->e_path, hname);
381 if (ep->e_flags & NFSEXP_READONLY)
382 c = dumpopt(c, "ro");
384 c = dumpopt(c, "rw");
385 if (ep->e_flags & NFSEXP_ASYNC)
386 c = dumpopt(c, "async");
387 if (ep->e_flags & NFSEXP_GATHERED_WRITES)
388 c = dumpopt(c, "wdelay");
389 if (ep->e_flags & NFSEXP_NOHIDE)
390 c = dumpopt(c, "nohide");
391 if (ep->e_flags & NFSEXP_CROSSMOUNT)
392 c = dumpopt(c, "crossmnt");
393 if (ep->e_flags & NFSEXP_INSECURE_PORT)
394 c = dumpopt(c, "insecure");
395 if (ep->e_flags & NFSEXP_ROOTSQUASH)
396 c = dumpopt(c, "root_squash");
398 c = dumpopt(c, "no_root_squash");
399 if (ep->e_flags & NFSEXP_ALLSQUASH)
400 c = dumpopt(c, "all_squash");
401 if (ep->e_flags & NFSEXP_NOSUBTREECHECK)
402 c = dumpopt(c, "no_subtree_check");
403 if (ep->e_flags & NFSEXP_NOAUTHNLM)
404 c = dumpopt(c, "insecure_locks");
405 if (ep->e_flags & NFSEXP_NOACL)
406 c = dumpopt(c, "no_acl");
407 if (ep->e_flags & NFSEXP_FSID)
408 c = dumpopt(c, "fsid=%d", ep->e_fsid);
410 c = dumpopt(c, "fsid=%s", ep->e_uuid);
411 if (ep->e_mountpoint)
412 c = dumpopt(c, "mountpoint%s%s",
413 ep->e_mountpoint[0]?"=":"",
415 if (ep->e_anonuid != 65534)
416 c = dumpopt(c, "anonuid=%d", ep->e_anonuid);
417 if (ep->e_anongid != 65534)
418 c = dumpopt(c, "anongid=%d", ep->e_anongid);
419 switch(ep->e_fslocmethod) {
423 c = dumpopt(c, "refer=%s", ep->e_fslocdata);
426 c = dumpopt(c, "replicas=%s", ep->e_fslocdata);
430 c = dumpopt(c, "fsloc=stub");
434 printf("%c\n", (c != '(')? ')' : ' ');
440 error(nfs_export *exp, int err)
442 fprintf(stderr, "%s:%s: %s\n", exp->m_client->m_hostname,
443 exp->m_export.e_path, strerror(err));
449 fprintf(stderr, "usage: exportfs [-aruv] [host:/path]\n");