2 * Copyright (c) 1997-2014 Erez Zadok
3 * Copyright (c) 1990 Jan-Simon Pendry
4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1990 The Regents of the University of California.
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * File: am-utils/amd/amfs_host.c
41 * NFS host file system.
42 * Mounts all exported filesystems from a given host.
43 * This has now degenerated into a mess but will not
44 * be rewritten. Amd 6 will support the abstractions
45 * needed to make this work correctly.
50 #endif /* HAVE_CONFIG_H */
54 static char *amfs_host_match(am_opts *fo);
55 static int amfs_host_init(mntfs *mf);
56 static int amfs_host_mount(am_node *am, mntfs *mf);
57 static int amfs_host_umount(am_node *am, mntfs *mf);
58 static void amfs_host_umounted(mntfs *mf);
63 am_ops amfs_host_ops =
70 amfs_error_lookup_child,
71 amfs_error_mount_child,
73 0, /* amfs_host_readlink */
74 0, /* amfs_host_mounted */
77 0, /* amfs_host_get_wchan */
78 FS_MKMNT | FS_BACKGROUND | FS_AMQINFO,
81 #endif /* HAVE_FS_AUTOFS */
86 * Determine the mount point:
88 * The next change we put in to better handle PCs. This is a bit
89 * disgusting, so you'd better sit down. We change the make_mntpt function
90 * to look for exported file systems without a leading '/'. If they don't
91 * have a leading '/', we add one. If the export is 'a:' through 'z:'
92 * (without a leading slash), we change it to 'a%' (or b% or z%). This
93 * allows the entire PC disk to be mounted.
96 make_mntpt(char *mntpt, size_t l, const exports ex, const char *mf_mount)
98 if (ex->ex_dir[0] == '/') {
99 if (ex->ex_dir[1] == 0)
100 xstrlcpy(mntpt, mf_mount, l);
102 xsnprintf(mntpt, l, "%s%s", mf_mount, ex->ex_dir);
103 } else if (ex->ex_dir[0] >= 'a' &&
104 ex->ex_dir[0] <= 'z' &&
105 ex->ex_dir[1] == ':' &&
106 ex->ex_dir[2] == '/' &&
108 xsnprintf(mntpt, l, "%s/%c%%", mf_mount, ex->ex_dir[0]);
110 xsnprintf(mntpt, l, "%s/%s", mf_mount, ex->ex_dir);
115 * Execute needs the same as NFS plus a helper command
118 amfs_host_match(am_opts *fo)
120 extern am_ops nfs_ops;
123 * Make sure rfs is specified to keep nfs_match happy...
128 return (*nfs_ops.fs_match) (fo);
133 amfs_host_init(mntfs *mf)
137 if (strchr(mf->mf_info, ':') == 0)
141 * This is primarily to schedule a wakeup so that as soon
142 * as our fileserver is ready, we can continue setting up
143 * the host filesystem. If we don't do this, the standard
144 * amfs_auto code will set up a fileserver structure, but it will
145 * have to wait for another nfs request from the client to come
146 * in before finishing. Our way is faster since we don't have
147 * to wait for the client to resend its request (which could
148 * take a second or two).
151 * First, we find the fileserver for this mntfs and then call
152 * get_mountd_port with our mntfs passed as the wait channel.
153 * get_mountd_port will check some things and then schedule
154 * it so that when the fileserver is ready, a wakeup is done
155 * on this mntfs. amfs_cont() is already sleeping on this mntfs
156 * so as soon as that wakeup happens amfs_cont() is called and
157 * this mount is retried.
161 * We don't really care if there's an error returned.
162 * Since this is just to help speed things along, the
163 * error will get handled properly elsewhere.
165 get_mountd_port(mf->mf_server, &mountd_port, get_mntfs_wchan(mf));
172 do_mount(am_nfs_handle_t *fhp, char *mntdir, char *fs_name, mntfs *mf)
176 dlog("amfs_host: mounting fs %s on %s\n", fs_name, mntdir);
178 (void) mkdirs(mntdir, 0555);
179 if (stat(mntdir, &stb) < 0 || (stb.st_mode & S_IFMT) != S_IFDIR) {
180 plog(XLOG_ERROR, "No mount point for %s - skipping", mntdir);
184 return mount_nfs_fh(fhp, mntdir, fs_name, mf);
189 sortfun(const voidp x, const voidp y)
191 exports *a = (exports *) x;
192 exports *b = (exports *) y;
194 return strcmp((*a)->ex_dir, (*b)->ex_dir);
202 fetch_fhandle(CLIENT *client, char *dir, am_nfs_handle_t *fhp, u_long nfs_version)
205 enum clnt_stat clnt_stat;
208 struct am_mountres3 res3;
209 #endif /* HAVE_FS_NFS3 */
212 * Pick a number, any number...
217 dlog("Fetching fhandle for %s", dir);
220 * Call the mount daemon on the remote host to
221 * get the filehandle. Use NFS version specific call.
224 plog(XLOG_INFO, "fetch_fhandle: NFS version %d", (int) nfs_version);
226 if (nfs_version == NFS_VERSION3
229 || nfs_version == NFS_VERSION4
230 #endif /* NO_FALLBACK */
231 #endif /* HAVE_FS_NFS4 */
234 memset((char *) &res3, 0, sizeof(res3));
235 clnt_stat = clnt_call(client,
237 (XDRPROC_T_TYPE) xdr_dirpath,
238 (SVC_IN_ARG_TYPE) &dir,
239 (XDRPROC_T_TYPE) xdr_am_mountres3,
240 (SVC_IN_ARG_TYPE) &res3,
242 if (clnt_stat != RPC_SUCCESS) {
243 plog(XLOG_ERROR, "mountd rpc failed: %s", clnt_sperrno(clnt_stat));
246 /* Check the status of the filehandle */
247 if ((errno = res3.fhs_status)) {
248 dlog("fhandle fetch for mount version 3 failed: %m");
251 memset((voidp) &fhp->v3, 0, sizeof(am_nfs_fh3));
252 fhp->v3.am_fh3_length = res3.mountres3_u.mountinfo.fhandle.fhandle3_len;
253 memmove(fhp->v3.am_fh3_data,
254 res3.mountres3_u.mountinfo.fhandle.fhandle3_val,
255 fhp->v3.am_fh3_length);
256 } else { /* not NFS_VERSION3 mount */
257 #endif /* HAVE_FS_NFS3 */
258 clnt_stat = clnt_call(client,
260 (XDRPROC_T_TYPE) xdr_dirpath,
261 (SVC_IN_ARG_TYPE) &dir,
262 (XDRPROC_T_TYPE) xdr_fhstatus,
263 (SVC_IN_ARG_TYPE) &res,
265 if (clnt_stat != RPC_SUCCESS) {
266 plog(XLOG_ERROR, "mountd rpc failed: %s", clnt_sperrno(clnt_stat));
269 /* Check status of filehandle */
270 if (res.fhs_status) {
271 errno = res.fhs_status;
272 dlog("fhandle fetch for mount version 1 failed: %m");
275 memmove(&fhp->v2, &res.fhs_fh, NFS_FHSIZE);
277 } /* end of "if (nfs_version == NFS_VERSION3)" statement */
278 #endif /* HAVE_FS_NFS3 */
286 * Scan mount table to see if something already mounted
289 already_mounted(mntlist *mlist, char *dir)
293 for (ml = mlist; ml; ml = ml->mnext)
294 if (STREQ(ml->mnt->mnt_dir, dir))
301 amfs_host_mount(am_node *am, mntfs *mf)
305 enum clnt_stat clnt_stat;
308 exports exlist = 0, ex;
310 am_nfs_handle_t *fp = NULL;
313 struct sockaddr_in sin;
314 int sock = RPC_ANYSOCK;
317 char fs_name[MAXPATHLEN], *rfs_dir;
318 char mntpt[MAXPATHLEN];
323 * WebNFS servers don't necessarily run mountd.
325 if (mf->mf_flags & MFF_WEBNFS) {
326 plog(XLOG_ERROR, "amfs_host_mount: cannot support WebNFS");
331 * Read the mount list
333 mlist = read_mtab(mf->mf_mount, mnttab_file_name);
335 #ifdef MOUNT_TABLE_ON_FILE
337 * Unlock the mount list
340 #endif /* MOUNT_TABLE_ON_FILE */
343 * Take a copy of the server hostname, address, and nfs version
344 * to mount version conversion.
346 host = mf->mf_server->fs_host;
347 sin = *mf->mf_server->fs_ip;
348 plog(XLOG_INFO, "amfs_host_mount: NFS version %d", (int) mf->mf_server->fs_version);
350 if (mf->mf_server->fs_version == NFS_VERSION3)
351 mnt_version = AM_MOUNTVERS3;
353 #endif /* HAVE_FS_NFS3 */
354 mnt_version = MOUNTVERS;
357 * The original 10 second per try timeout is WAY too large, especially
358 * if we're only waiting 10 or 20 seconds max for the response.
359 * That would mean we'd try only once in 10 seconds, and we could
360 * lose the transmit or receive packet, and never try again.
361 * A 2-second per try timeout here is much more reasonable.
362 * 09/28/92 Mike Mitchell, mcm@unx.sas.com
368 * Create a client attached to mountd
370 client = get_mount_client(host, &sin, &tv, &sock, mnt_version);
371 if (client == NULL) {
372 #ifdef HAVE_CLNT_SPCREATEERROR
373 plog(XLOG_ERROR, "get_mount_client failed for %s: %s",
374 host, clnt_spcreateerror(""));
375 #else /* not HAVE_CLNT_SPCREATEERROR */
376 plog(XLOG_ERROR, "get_mount_client failed for %s", host);
377 #endif /* not HAVE_CLNT_SPCREATEERROR */
382 error = make_nfs_auth();
386 client->cl_auth = nfs_auth;
388 dlog("Fetching export list from %s", host);
391 * Fetch the export list
395 clnt_stat = clnt_call(client,
397 (XDRPROC_T_TYPE) xdr_void,
399 (XDRPROC_T_TYPE) xdr_exports,
400 (SVC_IN_ARG_TYPE) & exlist,
402 if (clnt_stat != RPC_SUCCESS) {
403 const char *msg = clnt_sperrno(clnt_stat);
404 plog(XLOG_ERROR, "host_mount rpc failed: %s", msg);
405 /* clnt_perror(client, "rpc"); */
411 * Figure out how many exports were returned
413 for (n_export = 0, ex = exlist; ex; ex = ex->ex_next) {
418 * Allocate an array of pointers into the list
419 * so that they can be sorted. If the filesystem
420 * is already mounted then ignore it.
422 ep = (exports *) xmalloc(n_export * sizeof(exports));
423 for (j = 0, ex = exlist; ex; ex = ex->ex_next) {
424 make_mntpt(mntpt, sizeof(mntpt), ex, mf->mf_mount);
425 if (already_mounted(mlist, mntpt))
426 /* we have at least one mounted f/s, so don't fail the mount */
435 * This way the mounts are done in order down the tree,
436 * instead of any random order returned by the mount
437 * daemon (the protocol doesn't specify...).
439 qsort(ep, n_export, sizeof(exports), sortfun);
442 * Allocate an array of filehandles
444 fp = (am_nfs_handle_t *) xmalloc(n_export * sizeof(am_nfs_handle_t));
447 * Try to obtain filehandles for each directory.
448 * If a fetch fails then just zero out the array
449 * reference but discard the error.
451 for (j = k = 0; j < n_export; j++) {
452 /* Check and avoid a duplicated export entry */
453 if (j > k && ep[k] && STREQ(ep[j]->ex_dir, ep[k]->ex_dir)) {
454 dlog("avoiding dup fhandle requested for %s", ep[j]->ex_dir);
458 error = fetch_fhandle(client, ep[j]->ex_dir, &fp[j],
459 mf->mf_server->fs_version);
466 * Mount each filesystem for which we have a filehandle.
467 * If any of the mounts succeed then mark "ok" and return
468 * error code 0 at the end. If they all fail then return
469 * the last error code.
471 xstrlcpy(fs_name, mf->mf_info, sizeof(fs_name));
472 if ((rfs_dir = strchr(fs_name, ':')) == (char *) NULL) {
473 plog(XLOG_FATAL, "amfs_host_mount: mf_info has no colon");
478 for (j = 0; j < n_export; j++) {
482 * Note: the sizeof space left in rfs_dir is what's left in fs_name
483 * after strchr() above returned a pointer _inside_ fs_name. The
484 * calculation below also takes into account that rfs_dir was
485 * incremented by the ++ above.
487 xstrlcpy(rfs_dir, ex->ex_dir, sizeof(fs_name) - (rfs_dir - fs_name));
488 make_mntpt(mntpt, sizeof(mntpt), ex, mf->mf_mount);
489 if (do_mount(&fp[j], mntpt, fs_name, mf) == 0)
498 discard_mntlist(mlist);
501 if (sock != RPC_ANYSOCK)
502 (void) amu_close(sock);
504 clnt_destroy(client);
506 xdr_pri_free((XDRPROC_T_TYPE) xdr_exports, (caddr_t) &exlist);
514 * Return true if pref is a directory prefix of dir.
517 * Does not work if pref is "/".
520 directory_prefix(char *pref, char *dir)
522 int len = strlen(pref);
524 if (!NSTREQ(pref, dir, len))
526 if (dir[len] == '/' || dir[len] == '\0')
533 * Unmount a mount tree
536 amfs_host_umount(am_node *am, mntfs *mf)
539 int unmount_flags = (mf->mf_flags & MFF_ON_AUTOFS) ? AMU_UMOUNT_AUTOFS : 0;
543 * Read the mount list
545 mntlist *mlist = read_mtab(mf->mf_mount, mnttab_file_name);
547 #ifdef MOUNT_TABLE_ON_FILE
549 * Unlock the mount list
552 #endif /* MOUNT_TABLE_ON_FILE */
560 mntlist *ml2 = ml->mnext;
568 * Unmount all filesystems...
570 for (ml = mlist; ml && !xerror; ml = ml->mnext) {
571 char *dir = ml->mnt->mnt_dir;
572 if (directory_prefix(mf->mf_mount, dir)) {
574 dlog("amfs_host: unmounts %s", dir);
578 error = UMOUNT_FS(dir, mnttab_file_name, unmount_flags);
580 * Keep track of errors
584 * If we have not already set xerror and error is not ENOENT,
585 * then set xerror equal to error and log it.
586 * 'xerror' is the return value for this function.
588 * We do not want to pass ENOENT as an error because if the
589 * directory does not exists our work is done anyway.
591 if (!xerror && error != ENOENT)
593 if (error != EBUSY) {
595 plog(XLOG_ERROR, "Tree unmount of %s failed: %m", ml->mnt->mnt_dir);
604 * Throw away mount list
606 discard_mntlist(mlist);
609 * Try to remount, except when we are shutting down.
611 if (xerror && amd_state != Finishing) {
612 xerror = amfs_host_mount(am, mf);
615 * Don't log this - it's usually too verbose
616 plog(XLOG_INFO, "Remounted host %s", mf->mf_info);
626 * Tell mountd we're done.
627 * This is not quite right, because we may still
628 * have other filesystems mounted, but the existing
629 * mountd protocol is badly broken anyway.
632 amfs_host_umounted(mntfs *mf)
636 enum clnt_stat clnt_stat;
637 struct sockaddr_in sin;
638 int sock = RPC_ANYSOCK;
642 if (mf->mf_error || mf->mf_refc > 1 || !mf->mf_server)
646 * WebNFS servers shouldn't ever get here.
648 if (mf->mf_flags & MFF_WEBNFS) {
649 plog(XLOG_ERROR, "amfs_host_umounted: cannot support WebNFS");
654 * Take a copy of the server hostname, address, and NFS version
655 * to mount version conversion.
657 host = mf->mf_server->fs_host;
658 sin = *mf->mf_server->fs_ip;
659 plog(XLOG_INFO, "amfs_host_umounted: NFS version %d", (int) mf->mf_server->fs_version);
661 if (mf->mf_server->fs_version == NFS_VERSION3)
662 mnt_version = AM_MOUNTVERS3;
664 #endif /* HAVE_FS_NFS3 */
665 mnt_version = MOUNTVERS;
668 * Create a client attached to mountd
672 client = get_mount_client(host, &sin, &tv, &sock, mnt_version);
673 if (client == NULL) {
674 #ifdef HAVE_CLNT_SPCREATEERROR
675 plog(XLOG_ERROR, "get_mount_client failed for %s: %s",
676 host, clnt_spcreateerror(""));
677 #else /* not HAVE_CLNT_SPCREATEERROR */
678 plog(XLOG_ERROR, "get_mount_client failed for %s", host);
679 #endif /* not HAVE_CLNT_SPCREATEERROR */
687 client->cl_auth = nfs_auth;
689 dlog("Unmounting all from %s", host);
691 clnt_stat = clnt_call(client,
693 (XDRPROC_T_TYPE) xdr_void,
695 (XDRPROC_T_TYPE) xdr_void,
698 if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_SYSTEMERROR) {
699 /* RPC_SYSTEMERROR seems to be returned for no good reason ... */
700 const char *msg = clnt_sperrno(clnt_stat);
701 plog(XLOG_ERROR, "unmount all from %s rpc failed: %s", host, msg);
706 if (sock != RPC_ANYSOCK)
707 (void) amu_close(sock);
709 clnt_destroy(client);