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 if (optind == argc && ! f_all) {
100 new_cache = check_new_cache();
102 if (f_export && ! f_ignore)
103 export_read(_PATH_EXPORTS);
106 export_all(f_verbose);
108 for (i = optind; i < argc ; i++)
109 exportfs(argv[i], options, f_verbose);
111 /* If we are unexporting everything, then
112 * don't care about what should be exported, as that
113 * may require DNS lookups..
115 if (! ( !f_export && f_all)) {
116 /* note: xtab_*_read does not update entries if they already exist,
117 * so this will not lose new options
122 for (i = optind ; i < argc ; i++)
123 unexportfs(argv[i], f_verbose);
128 exports_update(f_verbose);
132 cache_flush(force_flush);
139 /* we synchronise intention with reality.
140 * entries with m_mayexport get exported
141 * entries with m_exported but not m_mayexport get unexported
142 * looking at m_client->m_type == MCL_FQDN only
145 exports_update(int verbose)
149 for (exp = exportlist[MCL_FQDN]; exp; exp=exp->m_next) {
150 /* check mountpoint option */
151 if (exp->m_mayexport &&
152 exp->m_export.e_mountpoint &&
153 !is_mountpoint(exp->m_export.e_mountpoint[0]?
154 exp->m_export.e_mountpoint:
155 exp->m_export.e_path)) {
156 printf("%s not exported as %s not a mountpoint.\n",
157 exp->m_export.e_path, exp->m_export.e_mountpoint);
158 exp->m_mayexport = 0;
160 if (exp->m_mayexport && ((exp->m_exported<1) || exp->m_changed)) {
162 printf("%sexporting %s:%s to kernel\n",
163 exp->m_exported ?"re":"",
164 exp->m_client->m_hostname,
165 exp->m_export.e_path);
166 if (!export_export(exp))
169 if (exp->m_exported && ! exp->m_mayexport) {
171 printf("unexporting %s:%s from kernel\n",
172 exp->m_client->m_hostname,
173 exp->m_export.e_path);
174 if (!export_unexport(exp))
181 * export_all finds all entries and
182 * marks them xtabent and mayexport so that they get exported
185 export_all(int verbose)
190 for (i = 0; i < MCL_MAXTYPES; i++) {
191 for (exp = exportlist[i]; exp; exp = exp->m_next) {
193 printf("exporting %s:%s\n",
194 exp->m_client->m_hostname,
195 exp->m_export.e_path);
197 exp->m_mayexport = 1;
205 exportfs(char *arg, char *options, int verbose)
207 struct exportent *eep;
209 struct hostent *hp = NULL;
214 if ((path = strchr(arg, ':')) != NULL)
217 if (!path || *path != '/') {
218 fprintf(stderr, "Invalid exporting option: %s\n", arg);
222 if ((htype = client_gettype(hname)) == MCL_FQDN &&
223 (hp = gethostbyname(hname)) != NULL) {
224 struct hostent *hp2 = hostent_dup (hp);
225 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
229 hp = hostent_dup(hp);
232 exp = export_find(hp, path);
235 exp = export_lookup(hname, path, 0);
239 if (!(eep = mkexportent(hname, path, options)) ||
240 !(exp = export_create(eep, 0))) {
244 } else if (!updateexportent(&exp->m_export, options)) {
250 printf("exporting %s:%s\n", exp->m_client->m_hostname,
251 exp->m_export.e_path);
253 exp->m_mayexport = 1;
259 unexportfs(char *arg, int verbose)
262 struct hostent *hp = NULL;
267 if ((path = strchr(arg, ':')) != NULL)
270 if (!path || *path != '/') {
271 fprintf(stderr, "Invalid unexporting option: %s\n",
276 if ((htype = client_gettype(hname)) == MCL_FQDN) {
277 if ((hp = gethostbyname(hname)) != 0) {
278 hp = hostent_dup (hp);
279 hname = (char *) hp->h_name;
283 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
284 if (path && strcmp(path, exp->m_export.e_path))
286 if (htype != exp->m_client->m_type)
288 if (htype == MCL_FQDN
289 && !matchhostname(exp->m_export.e_hostname,
292 if (htype != MCL_FQDN
293 && strcasecmp(exp->m_export.e_hostname, hname))
297 if (exp->m_exported) {
298 printf("unexporting %s:%s from kernel\n",
299 exp->m_client->m_hostname,
300 exp->m_export.e_path);
304 printf("unexporting %s:%s\n",
305 exp->m_client->m_hostname,
306 exp->m_export.e_path);
309 if (exp->m_exported && !export_unexport(exp))
313 exp->m_mayexport = 0;
320 dumpopt(char c, char *fmt, ...)
335 struct exportent *ep;
339 for (htype = 0; htype < MCL_MAXTYPES; htype++) {
340 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
343 continue; /* neilb */
344 if (htype == MCL_ANONYMOUS)
347 hname = ep->e_hostname;
348 if (strlen(ep->e_path) > 14)
349 printf("%-14s\n\t\t%s", ep->e_path, hname);
351 printf("%-14s\t%s", ep->e_path, hname);
357 if (ep->e_flags & NFSEXP_READONLY)
358 c = dumpopt(c, "ro");
360 c = dumpopt(c, "rw");
361 if (ep->e_flags & NFSEXP_ASYNC)
362 c = dumpopt(c, "async");
363 if (ep->e_flags & NFSEXP_GATHERED_WRITES)
364 c = dumpopt(c, "wdelay");
365 if (ep->e_flags & NFSEXP_NOHIDE)
366 c = dumpopt(c, "nohide");
367 if (ep->e_flags & NFSEXP_CROSSMNT)
368 c = dumpopt(c, "crossmnt");
369 if (ep->e_flags & NFSEXP_INSECURE_PORT)
370 c = dumpopt(c, "insecure");
371 if (ep->e_flags & NFSEXP_ROOTSQUASH)
372 c = dumpopt(c, "root_squash");
374 c = dumpopt(c, "no_root_squash");
375 if (ep->e_flags & NFSEXP_ALLSQUASH)
376 c = dumpopt(c, "all_squash");
377 if (ep->e_flags & NFSEXP_NOSUBTREECHECK)
378 c = dumpopt(c, "no_subtree_check");
379 if (ep->e_flags & NFSEXP_NOAUTHNLM)
380 c = dumpopt(c, "insecure_locks");
381 if (ep->e_flags & NFSEXP_FSID)
382 c = dumpopt(c, "fsid=%d", ep->e_fsid);
383 if (ep->e_mountpoint)
384 c = dumpopt(c, "mountpoint%s%s",
385 ep->e_mountpoint[0]?"=":"",
387 if (ep->e_maptype == CLE_MAP_UGIDD)
388 c = dumpopt(c, "mapping=ugidd");
389 else if (ep->e_maptype == CLE_MAP_FILE)
390 c = dumpopt(c, "mapping=file");
391 if (ep->e_anonuid != -2)
392 c = dumpopt(c, "anonuid=%d", ep->e_anonuid);
393 if (ep->e_anongid != -2)
394 c = dumpopt(c, "anongid=%d", ep->e_anongid);
396 printf("%c\n", (c != '(')? ')' : ' ');
402 error(nfs_export *exp, int err)
404 fprintf(stderr, "%s:%s: %s\n", exp->m_client->m_hostname,
405 exp->m_export.e_path, strerror(err));
411 fprintf(stderr, "usage: exportfs [-aruv] [host:/path]\n");