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>
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);
36 main(int argc, char **argv)
48 xlog_open("exportfs");
52 while ((c = getopt(argc, argv, "aio:ruvf")) != EOF) {
82 if (optind != argc && f_all) {
83 fprintf(stderr,"exportfs: extra arguments are not permitted with -a or -r.\n");
86 if (f_ignore && (f_all || ! f_export)) {
87 fprintf(stderr,"exportfs: -i not meaningful with -a, -r or -u.\n");
90 if (f_reexport && ! f_export) {
91 fprintf(stderr, "exportfs: -r and -u are incompatible.\n");
94 new_cache = check_new_cache();
95 if (optind == argc && ! f_all) {
100 fprintf(stderr, "exportfs: -f: only available with new cache controls: mount /proc/fs/nfsd first\n");
111 if (f_export && ! f_ignore)
112 export_read(_PATH_EXPORTS);
115 export_all(f_verbose);
117 for (i = optind; i < argc ; i++)
118 exportfs(argv[i], options, f_verbose);
120 /* If we are unexporting everything, then
121 * don't care about what should be exported, as that
122 * may require DNS lookups..
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
131 for (i = optind ; i < argc ; i++)
132 unexportfs(argv[i], f_verbose);
137 exports_update(f_verbose);
141 cache_flush(force_flush);
148 /* we synchronise intention with reality.
149 * entries with m_mayexport get exported
150 * entries with m_exported but not m_mayexport get unexported
151 * looking at m_client->m_type == MCL_FQDN only
154 exports_update(int verbose)
158 for (exp = exportlist[MCL_FQDN]; exp; exp=exp->m_next) {
159 /* check mountpoint option */
160 if (exp->m_mayexport &&
161 exp->m_export.e_mountpoint &&
162 !is_mountpoint(exp->m_export.e_mountpoint[0]?
163 exp->m_export.e_mountpoint:
164 exp->m_export.e_path)) {
165 printf("%s not exported as %s not a mountpoint.\n",
166 exp->m_export.e_path, exp->m_export.e_mountpoint);
167 exp->m_mayexport = 0;
169 if (exp->m_mayexport && ((exp->m_exported<1) || exp->m_changed)) {
171 printf("%sexporting %s:%s to kernel\n",
172 exp->m_exported ?"re":"",
173 exp->m_client->m_hostname,
174 exp->m_export.e_path);
175 if (!export_export(exp))
178 if (exp->m_exported && ! exp->m_mayexport) {
180 printf("unexporting %s:%s from kernel\n",
181 exp->m_client->m_hostname,
182 exp->m_export.e_path);
183 if (!export_unexport(exp))
190 * export_all finds all entries and
191 * marks them xtabent and mayexport so that they get exported
194 export_all(int verbose)
199 for (i = 0; i < MCL_MAXTYPES; i++) {
200 for (exp = exportlist[i]; exp; exp = exp->m_next) {
202 printf("exporting %s:%s\n",
203 exp->m_client->m_hostname,
204 exp->m_export.e_path);
206 exp->m_mayexport = 1;
214 exportfs(char *arg, char *options, int verbose)
216 struct exportent *eep;
218 struct hostent *hp = NULL;
223 if ((path = strchr(arg, ':')) != NULL)
226 if (!path || *path != '/') {
227 fprintf(stderr, "Invalid exporting option: %s\n", arg);
231 if ((htype = client_gettype(hname)) == MCL_FQDN &&
232 (hp = gethostbyname(hname)) != NULL) {
233 struct hostent *hp2 = hostent_dup (hp);
234 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
238 hp = hostent_dup(hp);
241 exp = export_find(hp, path);
244 exp = export_lookup(hname, path, 0);
248 if (!(eep = mkexportent(hname, path, options)) ||
249 !(exp = export_create(eep, 0))) {
253 } else if (!updateexportent(&exp->m_export, options)) {
259 printf("exporting %s:%s\n", exp->m_client->m_hostname,
260 exp->m_export.e_path);
262 exp->m_mayexport = 1;
268 unexportfs(char *arg, int verbose)
271 struct hostent *hp = NULL;
276 if ((path = strchr(arg, ':')) != NULL)
279 if (!path || *path != '/') {
280 fprintf(stderr, "Invalid unexporting option: %s\n",
285 if ((htype = client_gettype(hname)) == MCL_FQDN) {
286 if ((hp = gethostbyname(hname)) != 0) {
287 hp = hostent_dup (hp);
288 hname = (char *) hp->h_name;
292 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
293 if (path && strcmp(path, exp->m_export.e_path))
295 if (htype != exp->m_client->m_type)
297 if (htype == MCL_FQDN
298 && !matchhostname(exp->m_export.e_hostname,
301 if (htype != MCL_FQDN
302 && strcasecmp(exp->m_export.e_hostname, hname))
306 if (exp->m_exported) {
307 printf("unexporting %s:%s from kernel\n",
308 exp->m_client->m_hostname,
309 exp->m_export.e_path);
313 printf("unexporting %s:%s\n",
314 exp->m_client->m_hostname,
315 exp->m_export.e_path);
318 if (exp->m_exported && !export_unexport(exp))
322 exp->m_mayexport = 0;
329 dumpopt(char c, char *fmt, ...)
344 struct exportent *ep;
348 for (htype = 0; htype < MCL_MAXTYPES; htype++) {
349 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
352 continue; /* neilb */
353 if (htype == MCL_ANONYMOUS)
356 hname = ep->e_hostname;
357 if (strlen(ep->e_path) > 14)
358 printf("%-14s\n\t\t%s", ep->e_path, hname);
360 printf("%-14s\t%s", ep->e_path, hname);
366 if (ep->e_flags & NFSEXP_READONLY)
367 c = dumpopt(c, "ro");
369 c = dumpopt(c, "rw");
370 if (ep->e_flags & NFSEXP_ASYNC)
371 c = dumpopt(c, "async");
372 if (ep->e_flags & NFSEXP_GATHERED_WRITES)
373 c = dumpopt(c, "wdelay");
374 if (ep->e_flags & NFSEXP_NOHIDE)
375 c = dumpopt(c, "nohide");
376 if (ep->e_flags & NFSEXP_CROSSMOUNT)
377 c = dumpopt(c, "crossmnt");
378 if (ep->e_flags & NFSEXP_INSECURE_PORT)
379 c = dumpopt(c, "insecure");
380 if (ep->e_flags & NFSEXP_ROOTSQUASH)
381 c = dumpopt(c, "root_squash");
383 c = dumpopt(c, "no_root_squash");
384 if (ep->e_flags & NFSEXP_ALLSQUASH)
385 c = dumpopt(c, "all_squash");
386 if (ep->e_flags & NFSEXP_NOSUBTREECHECK)
387 c = dumpopt(c, "no_subtree_check");
388 if (ep->e_flags & NFSEXP_NOAUTHNLM)
389 c = dumpopt(c, "insecure_locks");
390 if (ep->e_flags & NFSEXP_FSID)
391 c = dumpopt(c, "fsid=%d", ep->e_fsid);
392 if (ep->e_mountpoint)
393 c = dumpopt(c, "mountpoint%s%s",
394 ep->e_mountpoint[0]?"=":"",
396 if (ep->e_maptype == CLE_MAP_UGIDD)
397 c = dumpopt(c, "mapping=ugidd");
398 else if (ep->e_maptype == CLE_MAP_FILE)
399 c = dumpopt(c, "mapping=file");
400 if (ep->e_anonuid != -2)
401 c = dumpopt(c, "anonuid=%d", ep->e_anonuid);
402 if (ep->e_anongid != -2)
403 c = dumpopt(c, "anongid=%d", ep->e_anongid);
405 printf("%c\n", (c != '(')? ')' : ' ');
411 error(nfs_export *exp, int err)
413 fprintf(stderr, "%s:%s: %s\n", exp->m_client->m_hostname,
414 exp->m_export.e_path, strerror(err));
420 fprintf(stderr, "usage: exportfs [-aruv] [host:/path]\n");