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;
227 exportfs(char *arg, char *options, int verbose)
229 struct exportent *eep;
231 struct hostent *hp = NULL;
236 if ((path = strchr(arg, ':')) != NULL)
239 if (!path || *path != '/') {
240 fprintf(stderr, "Invalid exporting option: %s\n", arg);
244 if ((htype = client_gettype(hname)) == MCL_FQDN &&
245 (hp = gethostbyname(hname)) != NULL) {
246 struct hostent *hp2 = hostent_dup (hp);
247 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
251 hp = hostent_dup(hp);
254 exp = export_find(hp, path);
257 exp = export_lookup(hname, path, 0);
261 if (!(eep = mkexportent(hname, path, options)) ||
262 !(exp = export_create(eep, 0))) {
266 } else if (!updateexportent(&exp->m_export, options)) {
272 printf("exporting %s:%s\n", exp->m_client->m_hostname,
273 exp->m_export.e_path);
275 exp->m_mayexport = 1;
281 unexportfs(char *arg, int verbose)
284 struct hostent *hp = NULL;
289 if ((path = strchr(arg, ':')) != NULL)
292 if (!path || *path != '/') {
293 fprintf(stderr, "Invalid unexporting option: %s\n",
298 if ((htype = client_gettype(hname)) == MCL_FQDN) {
299 if ((hp = gethostbyname(hname)) != 0) {
300 hp = hostent_dup (hp);
301 hname = (char *) hp->h_name;
305 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
306 if (path && strcmp(path, exp->m_export.e_path))
308 if (htype != exp->m_client->m_type)
310 if (htype == MCL_FQDN
311 && !matchhostname(exp->m_export.e_hostname,
314 if (htype != MCL_FQDN
315 && strcasecmp(exp->m_export.e_hostname, hname))
319 if (exp->m_exported) {
320 printf("unexporting %s:%s from kernel\n",
321 exp->m_client->m_hostname,
322 exp->m_export.e_path);
326 printf("unexporting %s:%s\n",
327 exp->m_client->m_hostname,
328 exp->m_export.e_path);
331 if (exp->m_exported && !export_unexport(exp))
335 exp->m_mayexport = 0;
342 dumpopt(char c, char *fmt, ...)
357 struct exportent *ep;
361 for (htype = 0; htype < MCL_MAXTYPES; htype++) {
362 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
365 continue; /* neilb */
366 if (htype == MCL_ANONYMOUS)
369 hname = ep->e_hostname;
370 if (strlen(ep->e_path) > 14)
371 printf("%-14s\n\t\t%s", ep->e_path, hname);
373 printf("%-14s\t%s", ep->e_path, hname);
379 if (ep->e_flags & NFSEXP_READONLY)
380 c = dumpopt(c, "ro");
382 c = dumpopt(c, "rw");
383 if (ep->e_flags & NFSEXP_ASYNC)
384 c = dumpopt(c, "async");
385 if (ep->e_flags & NFSEXP_GATHERED_WRITES)
386 c = dumpopt(c, "wdelay");
387 if (ep->e_flags & NFSEXP_NOHIDE)
388 c = dumpopt(c, "nohide");
389 if (ep->e_flags & NFSEXP_CROSSMOUNT)
390 c = dumpopt(c, "crossmnt");
391 if (ep->e_flags & NFSEXP_INSECURE_PORT)
392 c = dumpopt(c, "insecure");
393 if (ep->e_flags & NFSEXP_ROOTSQUASH)
394 c = dumpopt(c, "root_squash");
396 c = dumpopt(c, "no_root_squash");
397 if (ep->e_flags & NFSEXP_ALLSQUASH)
398 c = dumpopt(c, "all_squash");
399 if (ep->e_flags & NFSEXP_NOSUBTREECHECK)
400 c = dumpopt(c, "no_subtree_check");
401 if (ep->e_flags & NFSEXP_NOAUTHNLM)
402 c = dumpopt(c, "insecure_locks");
403 if (ep->e_flags & NFSEXP_NOACL)
404 c = dumpopt(c, "no_acl");
405 if (ep->e_flags & NFSEXP_FSID)
406 c = dumpopt(c, "fsid=%d", ep->e_fsid);
408 c = dumpopt(c, "fsid=%s", ep->e_uuid);
409 if (ep->e_mountpoint)
410 c = dumpopt(c, "mountpoint%s%s",
411 ep->e_mountpoint[0]?"=":"",
413 if (ep->e_anonuid != 65534)
414 c = dumpopt(c, "anonuid=%d", ep->e_anonuid);
415 if (ep->e_anongid != 65534)
416 c = dumpopt(c, "anongid=%d", ep->e_anongid);
417 switch(ep->e_fslocmethod) {
421 c = dumpopt(c, "refer=%s", ep->e_fslocdata);
424 c = dumpopt(c, "replicas=%s", ep->e_fslocdata);
428 c = dumpopt(c, "fsloc=stub");
432 printf("%c\n", (c != '(')? ')' : ' ');
438 error(nfs_export *exp, int err)
440 fprintf(stderr, "%s:%s: %s\n", exp->m_client->m_hostname,
441 exp->m_export.e_path, strerror(err));
447 fprintf(stderr, "usage: exportfs [-aruv] [host:/path]\n");