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);
138 exports_update(f_verbose);
142 cache_flush(force_flush);
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
155 exports_update(int verbose)
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;
170 if (exp->m_mayexport && ((exp->m_exported<1) || exp->m_changed)) {
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))
179 if (exp->m_exported && ! exp->m_mayexport) {
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))
191 * export_all finds all entries and
192 * marks them xtabent and mayexport so that they get exported
195 export_all(int verbose)
200 for (i = 0; i < MCL_MAXTYPES; i++) {
201 for (exp = exportlist[i]; exp; exp = exp->m_next) {
203 printf("exporting %s:%s\n",
204 exp->m_client->m_hostname,
205 exp->m_export.e_path);
207 exp->m_mayexport = 1;
215 exportfs(char *arg, char *options, int verbose)
217 struct exportent *eep;
219 struct hostent *hp = NULL;
224 if ((path = strchr(arg, ':')) != NULL)
227 if (!path || *path != '/') {
228 fprintf(stderr, "Invalid exporting option: %s\n", arg);
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,
239 hp = hostent_dup(hp);
242 exp = export_find(hp, path);
245 exp = export_lookup(hname, path, 0);
249 if (!(eep = mkexportent(hname, path, options)) ||
250 !(exp = export_create(eep, 0))) {
254 } else if (!updateexportent(&exp->m_export, options)) {
260 printf("exporting %s:%s\n", exp->m_client->m_hostname,
261 exp->m_export.e_path);
263 exp->m_mayexport = 1;
269 unexportfs(char *arg, int verbose)
272 struct hostent *hp = NULL;
277 if ((path = strchr(arg, ':')) != NULL)
280 if (!path || *path != '/') {
281 fprintf(stderr, "Invalid unexporting option: %s\n",
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;
293 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
294 if (path && strcmp(path, exp->m_export.e_path))
296 if (htype != exp->m_client->m_type)
298 if (htype == MCL_FQDN
299 && !matchhostname(exp->m_export.e_hostname,
302 if (htype != MCL_FQDN
303 && strcasecmp(exp->m_export.e_hostname, hname))
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);
314 printf("unexporting %s:%s\n",
315 exp->m_client->m_hostname,
316 exp->m_export.e_path);
319 if (exp->m_exported && !export_unexport(exp))
323 exp->m_mayexport = 0;
330 dumpopt(char c, char *fmt, ...)
345 struct exportent *ep;
349 for (htype = 0; htype < MCL_MAXTYPES; htype++) {
350 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
353 continue; /* neilb */
354 if (htype == MCL_ANONYMOUS)
357 hname = ep->e_hostname;
358 if (strlen(ep->e_path) > 14)
359 printf("%-14s\n\t\t%s", ep->e_path, hname);
361 printf("%-14s\t%s", ep->e_path, hname);
367 if (ep->e_flags & NFSEXP_READONLY)
368 c = dumpopt(c, "ro");
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");
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]?"=":"",
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);
406 printf("%c\n", (c != '(')? ')' : ' ');
412 error(nfs_export *exp, int err)
414 fprintf(stderr, "%s:%s: %s\n", exp->m_client->m_hostname,
415 exp->m_export.e_path, strerror(err));
421 fprintf(stderr, "usage: exportfs [-aruv] [host:/path]\n");