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);
150 exports_update_one(nfs_export *exp, int verbose)
152 /* check mountpoint option */
153 if (exp->m_mayexport &&
154 exp->m_export.e_mountpoint &&
155 !is_mountpoint(exp->m_export.e_mountpoint[0]?
156 exp->m_export.e_mountpoint:
157 exp->m_export.e_path)) {
158 printf("%s not exported as %s not a mountpoint.\n",
159 exp->m_export.e_path, exp->m_export.e_mountpoint);
160 exp->m_mayexport = 0;
162 if (exp->m_mayexport && ((exp->m_exported<1) || exp->m_changed)) {
164 printf("%sexporting %s:%s to kernel\n",
165 exp->m_exported ?"re":"",
166 exp->m_client->m_hostname,
167 exp->m_export.e_path);
168 if (!export_export(exp))
171 if (exp->m_exported && ! exp->m_mayexport) {
173 printf("unexporting %s:%s from kernel\n",
174 exp->m_client->m_hostname,
175 exp->m_export.e_path);
176 if (!export_unexport(exp))
182 /* we synchronise intention with reality.
183 * entries with m_mayexport get exported
184 * entries with m_exported but not m_mayexport get unexported
185 * looking at m_client->m_type == MCL_FQDN and m_client->m_type == MCL_GSS only
188 exports_update(int verbose)
192 for (exp = exportlist[MCL_FQDN]; exp; exp=exp->m_next) {
193 exports_update_one(exp, verbose);
195 for (exp = exportlist[MCL_GSS]; exp; exp=exp->m_next) {
196 exports_update_one(exp, verbose);
201 * export_all finds all entries and
202 * marks them xtabent and mayexport so that they get exported
205 export_all(int verbose)
210 for (i = 0; i < MCL_MAXTYPES; i++) {
211 for (exp = exportlist[i]; exp; exp = exp->m_next) {
213 printf("exporting %s:%s\n",
214 exp->m_client->m_hostname,
215 exp->m_export.e_path);
217 exp->m_mayexport = 1;
225 exportfs(char *arg, char *options, int verbose)
227 struct exportent *eep;
229 struct hostent *hp = NULL;
234 if ((path = strchr(arg, ':')) != NULL)
237 if (!path || *path != '/') {
238 fprintf(stderr, "Invalid exporting option: %s\n", arg);
242 if ((htype = client_gettype(hname)) == MCL_FQDN &&
243 (hp = gethostbyname(hname)) != NULL) {
244 struct hostent *hp2 = hostent_dup (hp);
245 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
249 hp = hostent_dup(hp);
252 exp = export_find(hp, path);
255 exp = export_lookup(hname, path, 0);
259 if (!(eep = mkexportent(hname, path, options)) ||
260 !(exp = export_create(eep, 0))) {
264 } else if (!updateexportent(&exp->m_export, options)) {
270 printf("exporting %s:%s\n", exp->m_client->m_hostname,
271 exp->m_export.e_path);
273 exp->m_mayexport = 1;
279 unexportfs(char *arg, int verbose)
282 struct hostent *hp = NULL;
287 if ((path = strchr(arg, ':')) != NULL)
290 if (!path || *path != '/') {
291 fprintf(stderr, "Invalid unexporting option: %s\n",
296 if ((htype = client_gettype(hname)) == MCL_FQDN) {
297 if ((hp = gethostbyname(hname)) != 0) {
298 hp = hostent_dup (hp);
299 hname = (char *) hp->h_name;
303 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
304 if (path && strcmp(path, exp->m_export.e_path))
306 if (htype != exp->m_client->m_type)
308 if (htype == MCL_FQDN
309 && !matchhostname(exp->m_export.e_hostname,
312 if (htype != MCL_FQDN
313 && strcasecmp(exp->m_export.e_hostname, hname))
317 if (exp->m_exported) {
318 printf("unexporting %s:%s from kernel\n",
319 exp->m_client->m_hostname,
320 exp->m_export.e_path);
324 printf("unexporting %s:%s\n",
325 exp->m_client->m_hostname,
326 exp->m_export.e_path);
329 if (exp->m_exported && !export_unexport(exp))
333 exp->m_mayexport = 0;
340 dumpopt(char c, char *fmt, ...)
355 struct exportent *ep;
359 for (htype = 0; htype < MCL_MAXTYPES; htype++) {
360 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
363 continue; /* neilb */
364 if (htype == MCL_ANONYMOUS)
367 hname = ep->e_hostname;
368 if (strlen(ep->e_path) > 14)
369 printf("%-14s\n\t\t%s", ep->e_path, hname);
371 printf("%-14s\t%s", ep->e_path, hname);
377 if (ep->e_flags & NFSEXP_READONLY)
378 c = dumpopt(c, "ro");
380 c = dumpopt(c, "rw");
381 if (ep->e_flags & NFSEXP_ASYNC)
382 c = dumpopt(c, "async");
383 if (ep->e_flags & NFSEXP_GATHERED_WRITES)
384 c = dumpopt(c, "wdelay");
385 if (ep->e_flags & NFSEXP_NOHIDE)
386 c = dumpopt(c, "nohide");
387 if (ep->e_flags & NFSEXP_CROSSMOUNT)
388 c = dumpopt(c, "crossmnt");
389 if (ep->e_flags & NFSEXP_INSECURE_PORT)
390 c = dumpopt(c, "insecure");
391 if (ep->e_flags & NFSEXP_ROOTSQUASH)
392 c = dumpopt(c, "root_squash");
394 c = dumpopt(c, "no_root_squash");
395 if (ep->e_flags & NFSEXP_ALLSQUASH)
396 c = dumpopt(c, "all_squash");
397 if (ep->e_flags & NFSEXP_NOSUBTREECHECK)
398 c = dumpopt(c, "no_subtree_check");
399 if (ep->e_flags & NFSEXP_NOAUTHNLM)
400 c = dumpopt(c, "insecure_locks");
401 if (ep->e_flags & NFSEXP_FSID)
402 c = dumpopt(c, "fsid=%d", ep->e_fsid);
403 if (ep->e_mountpoint)
404 c = dumpopt(c, "mountpoint%s%s",
405 ep->e_mountpoint[0]?"=":"",
407 if (ep->e_maptype == CLE_MAP_UGIDD)
408 c = dumpopt(c, "mapping=ugidd");
409 else if (ep->e_maptype == CLE_MAP_FILE)
410 c = dumpopt(c, "mapping=file");
411 if (ep->e_anonuid != -2)
412 c = dumpopt(c, "anonuid=%d", ep->e_anonuid);
413 if (ep->e_anongid != -2)
414 c = dumpopt(c, "anongid=%d", ep->e_anongid);
416 printf("%c\n", (c != '(')? ')' : ' ');
422 error(nfs_export *exp, int err)
424 fprintf(stderr, "%s:%s: %s\n", exp->m_client->m_hostname,
425 exp->m_export.e_path, strerror(err));
431 fprintf(stderr, "usage: exportfs [-aruv] [host:/path]\n");