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/ops_nfs.c
46 #endif /* HAVE_CONFIG_H */
51 * Convert from nfsstat to UN*X error code
53 #define unx_error(e) ((int)(e))
56 * FH_TTL is the time a file handle will remain in the cache since
57 * last being used. If the file handle becomes invalid, then it
58 * will be flushed anyway.
60 #define FH_TTL (5 * 60) /* five minutes */
61 #define FH_TTL_ERROR (30) /* 30 seconds */
62 #define FHID_ALLOC() (++fh_id)
65 * The NFS layer maintains a cache of file handles.
66 * This is *fundamental* to the implementation and
67 * also allows quick remounting when a filesystem
68 * is accessed soon after timing out.
70 * The NFS server layer knows to flush this cache
71 * when a server goes down so avoiding stale handles.
73 * Each cache entry keeps a hard reference to
74 * the corresponding server. This ensures that
75 * the server keepalive information is maintained.
77 * The copy of the sockaddr_in here is taken so
78 * that the port can be twiddled to talk to mountd
79 * instead of portmap or the NFS server as used
81 * The port# is flushed if a server goes down.
82 * The IP address is never flushed - we assume
83 * that the address of a mounted machine never
84 * changes. If it does, then you have other
87 typedef struct fh_cache fh_cache;
89 qelem fh_q; /* List header */
90 wchan_t fh_wchan; /* Wait channel */
91 int fh_error; /* Valid data? */
92 int fh_id; /* Unique id */
93 int fh_cid; /* Callout id */
94 u_long fh_nfs_version; /* highest NFS version on host */
95 am_nfs_handle_t fh_nfs_handle; /* Handle on filesystem */
96 int fh_status; /* Status of last rpc */
97 struct sockaddr_in fh_sin; /* Address of mountd */
98 fserver *fh_fs; /* Server holding filesystem */
99 char *fh_path; /* Filesystem on host */
102 /* forward definitions */
103 static int nfs_init(mntfs *mf);
104 static char *nfs_match(am_opts *fo);
105 static int nfs_mount(am_node *am, mntfs *mf);
106 static int nfs_umount(am_node *am, mntfs *mf);
107 static void nfs_umounted(mntfs *mf);
108 static int call_mountd(fh_cache *fp, u_long proc, fwd_fun f, wchan_t wchan);
109 static int webnfs_lookup(fh_cache *fp, fwd_fun f, wchan_t wchan);
110 static int fh_id = 0;
113 * clamp the filehandle version to 3, so that we can fail back to nfsv3
114 * since nfsv4 does not have file handles
116 #define SET_FH_VERSION(fs) \
117 (fs)->fs_version > NFS_VERSION3 ? NFS_VERSION3 : (fs)->fs_version;
121 qelem fh_head = {&fh_head, &fh_head};
124 * Network file system operations
133 amfs_error_lookup_child,
134 amfs_error_mount_child,
136 0, /* nfs_readlink */
140 0, /* nfs_get_wchan */
141 FS_MKMNT | FS_BACKGROUND | FS_AMQINFO, /* nfs_fs_flags */
142 #ifdef HAVE_FS_AUTOFS
144 #endif /* HAVE_FS_AUTOFS */
149 find_nfs_fhandle_cache(opaque_t arg, int done)
151 fh_cache *fp, *fp2 = NULL;
152 int id = (long) arg; /* for 64-bit archs */
154 ITER(fp, fh_cache, &fh_head) {
155 if (fp->fh_id == id) {
162 dlog("fh cache gives fp %#lx, fs %s", (unsigned long) fp2, fp2->fh_path);
164 dlog("fh cache search failed");
168 fp2->fh_error = ETIMEDOUT;
177 * Called when a filehandle appears via the mount protocol
180 got_nfs_fh_mount(voidp pkt, int len, struct sockaddr_in *sa, struct sockaddr_in *ia, opaque_t arg, int done)
185 struct am_mountres3 res3;
186 #endif /* HAVE_FS_NFS3 */
188 fp = find_nfs_fhandle_cache(arg, done);
193 * retrieve the correct RPC reply for the file handle, based on the
194 * NFS protocol version.
197 if (fp->fh_nfs_version == NFS_VERSION3) {
198 memset(&res3, 0, sizeof(res3));
199 fp->fh_error = pickup_rpc_reply(pkt, len, (voidp) &res3,
200 (XDRPROC_T_TYPE) xdr_am_mountres3);
201 fp->fh_status = unx_error(res3.fhs_status);
202 memset(&fp->fh_nfs_handle.v3, 0, sizeof(am_nfs_fh3));
203 fp->fh_nfs_handle.v3.am_fh3_length = res3.mountres3_u.mountinfo.fhandle.fhandle3_len;
204 memmove(fp->fh_nfs_handle.v3.am_fh3_data,
205 res3.mountres3_u.mountinfo.fhandle.fhandle3_val,
206 fp->fh_nfs_handle.v3.am_fh3_length);
208 XFREE(res3.mountres3_u.mountinfo.fhandle.fhandle3_val);
209 if (res3.mountres3_u.mountinfo.auth_flavors.auth_flavors_val)
210 XFREE(res3.mountres3_u.mountinfo.auth_flavors.auth_flavors_val);
212 #endif /* HAVE_FS_NFS3 */
213 memset(&res, 0, sizeof(res));
214 fp->fh_error = pickup_rpc_reply(pkt, len, (voidp) &res,
215 (XDRPROC_T_TYPE) xdr_fhstatus);
216 fp->fh_status = unx_error(res.fhs_status);
217 memmove(&fp->fh_nfs_handle.v2, &res.fhs_fh, NFS_FHSIZE);
220 #endif /* HAVE_FS_NFS3 */
223 dlog("got filehandle for %s:%s", fp->fh_fs->fs_host, fp->fh_path);
225 plog(XLOG_USER, "filehandle denied for %s:%s", fp->fh_fs->fs_host, fp->fh_path);
227 * Force the error to be EACCES. It's debatable whether it should be
228 * ENOENT instead, but the server really doesn't give us any clues, and
229 * EACCES is more in line with the "filehandle denied" message.
231 fp->fh_error = EACCES;
235 * Wakeup anything sleeping on this filehandle
238 dlog("Calling wakeup on %#lx", (unsigned long) fp->fh_wchan);
239 wakeup(fp->fh_wchan);
245 * Called when a filehandle appears via WebNFS
248 got_nfs_fh_webnfs(voidp pkt, int len, struct sockaddr_in *sa, struct sockaddr_in *ia, opaque_t arg, int done)
254 #endif /* HAVE_FS_NFS3 */
256 fp = find_nfs_fhandle_cache(arg, done);
261 * retrieve the correct RPC reply for the file handle, based on the
262 * NFS protocol version.
265 if (fp->fh_nfs_version == NFS_VERSION3) {
266 memset(&res3, 0, sizeof(res3));
267 fp->fh_error = pickup_rpc_reply(pkt, len, (voidp) &res3,
268 (XDRPROC_T_TYPE) xdr_am_LOOKUP3res);
269 fp->fh_status = unx_error(res3.status);
270 memset(&fp->fh_nfs_handle.v3, 0, sizeof(am_nfs_fh3));
271 fp->fh_nfs_handle.v3.am_fh3_length = res3.res_u.ok.object.am_fh3_length;
272 memmove(fp->fh_nfs_handle.v3.am_fh3_data,
273 res3.res_u.ok.object.am_fh3_data,
274 fp->fh_nfs_handle.v3.am_fh3_length);
276 #endif /* HAVE_FS_NFS3 */
277 memset(&res, 0, sizeof(res));
278 fp->fh_error = pickup_rpc_reply(pkt, len, (voidp) &res,
279 (XDRPROC_T_TYPE) xdr_diropres);
280 fp->fh_status = unx_error(res.dr_status);
281 memmove(&fp->fh_nfs_handle.v2, &res.dr_u.dr_drok_u.drok_fhandle, NFS_FHSIZE);
284 #endif /* HAVE_FS_NFS3 */
287 dlog("got filehandle for %s:%s", fp->fh_fs->fs_host, fp->fh_path);
289 plog(XLOG_USER, "filehandle denied for %s:%s", fp->fh_fs->fs_host, fp->fh_path);
291 * Force the error to be EACCES. It's debatable whether it should be
292 * ENOENT instead, but the server really doesn't give us any clues, and
293 * EACCES is more in line with the "filehandle denied" message.
295 fp->fh_error = EACCES;
299 * Wakeup anything sleeping on this filehandle
302 dlog("Calling wakeup on %#lx", (unsigned long) fp->fh_wchan);
303 wakeup(fp->fh_wchan);
309 flush_nfs_fhandle_cache(fserver *fs)
313 ITER(fp, fh_cache, &fh_head) {
314 if (fp->fh_fs == fs || fs == NULL) {
316 * Only invalidate port info for non-WebNFS servers
318 if (!(fp->fh_fs->fs_flags & FSF_WEBNFS))
319 fp->fh_sin.sin_port = (u_short) 0;
327 discard_fh(opaque_t arg)
329 fh_cache *fp = (fh_cache *) arg;
333 dlog("Discarding filehandle for %s:%s", fp->fh_fs->fs_host, fp->fh_path);
334 free_srvr(fp->fh_fs);
342 * Determine the file handle for a node
345 prime_nfs_fhandle_cache(char *path, fserver *fs, am_nfs_handle_t *fhbuf, mntfs *mf)
347 fh_cache *fp, *fp_save = NULL;
349 int reuse_id = FALSE;
351 dlog("Searching cache for %s:%s", fs->fs_host, path);
354 * First search the cache
356 ITER(fp, fh_cache, &fh_head) {
357 if (fs != fp->fh_fs || !STREQ(path, fp->fh_path))
358 continue; /* skip to next ITER item */
359 /* else we got a match */
360 switch (fp->fh_error) {
362 plog(XLOG_INFO, "prime_nfs_fhandle_cache: NFS version %d", (int) fp->fh_nfs_version);
364 error = fp->fh_error = fp->fh_status;
367 if (mf->mf_flags & MFF_NFS_SCALEDOWN) {
369 /* XXX: why reuse the ID? */
376 if (fp->fh_nfs_version == NFS_VERSION3) {
377 memmove((voidp) &(fhbuf->v3), (voidp) &(fp->fh_nfs_handle.v3),
378 sizeof(fp->fh_nfs_handle.v3));
380 #endif /* HAVE_FS_NFS3 */
382 memmove((voidp) &(fhbuf->v2), (voidp) &(fp->fh_nfs_handle.v2),
383 sizeof(fp->fh_nfs_handle.v2));
387 untimeout(fp->fh_cid);
388 fp->fh_cid = timeout(FH_TTL, discard_fh, (opaque_t) fp);
389 } else if (error == EACCES) {
391 * Now decode the file handle return code.
393 plog(XLOG_INFO, "Filehandle denied for \"%s:%s\"",
396 errno = error; /* XXX */
397 plog(XLOG_INFO, "Filehandle error for \"%s:%s\": %m",
402 * The error was returned from the remote mount daemon.
403 * Policy: this error will be cached for now...
409 * Still thinking about it, but we can re-use.
418 * Policy: make sure we recompute if required again
419 * in case this was caused by a network failure.
420 * This can thrash mountd's though... If you find
421 * your mountd going slowly then:
422 * 1. Add a fork() loop to main.
423 * 2. Remove the call to innetgr() and don't use
424 * netgroups, especially if you don't use YP.
426 error = fp->fh_error;
429 } /* end of switch statement */
430 } /* end of ITER loop */
438 * Re-use existing slot
440 untimeout(fp->fh_cid);
441 free_srvr(fp->fh_fs);
444 fp = ALLOC(struct fh_cache);
445 memset((voidp) fp, 0, sizeof(struct fh_cache));
446 ins_que(&fp->fh_q, &fh_head);
449 fp->fh_id = FHID_ALLOC();
450 fp->fh_wchan = get_mntfs_wchan(mf);
452 fp->fh_cid = timeout(FH_TTL, discard_fh, (opaque_t) fp);
455 * If fs->fs_ip is null, remote server is probably down.
458 /* Mark the fileserver down and invalid again */
459 fs->fs_flags &= ~FSF_VALID;
460 fs->fs_flags |= FSF_DOWN;
461 error = AM_ERRNO_HOST_DOWN;
466 * Either fp has been freshly allocated or the address has changed.
467 * Initialize address and nfs version. Don't try to re-use the port
468 * information unless using WebNFS where the port is fixed either by
469 * the spec or the "port" mount option.
471 if (fp->fh_sin.sin_addr.s_addr != fs->fs_ip->sin_addr.s_addr) {
472 fp->fh_sin = *fs->fs_ip;
473 if (!(mf->mf_flags & MFF_WEBNFS))
474 fp->fh_sin.sin_port = 0;
475 fp->fh_nfs_version = SET_FH_VERSION(fs);
478 fp->fh_fs = dup_srvr(fs);
479 fp->fh_path = xstrdup(path);
481 if (mf->mf_flags & MFF_WEBNFS)
482 error = webnfs_lookup(fp, got_nfs_fh_webnfs, get_mntfs_wchan(mf));
484 error = call_mountd(fp, MOUNTPROC_MNT, got_nfs_fh_mount, get_mntfs_wchan(mf));
487 * Local error - cache for a short period
488 * just to prevent thrashing.
490 untimeout(fp->fh_cid);
491 fp->fh_cid = timeout(error < 0 ? 2 * ALLOWED_MOUNT_TIME : FH_TTL_ERROR,
492 discard_fh, (opaque_t) fp);
493 fp->fh_error = error;
495 error = fp->fh_error;
505 AUTH_CREATE_GIDLIST_TYPE group_wheel = 0;
507 /* Some NFS mounts (particularly cross-domain) require FQDNs to succeed */
509 #ifdef HAVE_TRANSPORT_TYPE_TLI
510 if (gopt.flags & CFM_FULLY_QUALIFIED_HOSTS) {
511 plog(XLOG_INFO, "Using NFS auth for FQHN \"%s\"", hostd);
512 nfs_auth = authsys_create(hostd, 0, 0, 1, &group_wheel);
514 nfs_auth = authsys_create_default();
516 #else /* not HAVE_TRANSPORT_TYPE_TLI */
517 if (gopt.flags & CFM_FULLY_QUALIFIED_HOSTS) {
518 plog(XLOG_INFO, "Using NFS auth for FQHN \"%s\"", hostd);
519 nfs_auth = authunix_create(hostd, 0, 0, 1, &group_wheel);
521 nfs_auth = authunix_create_default();
523 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
533 call_mountd(fh_cache *fp, u_long proc, fwd_fun fun, wchan_t wchan)
535 struct rpc_msg mnt_msg;
537 char iobuf[UDPMSGSIZE];
542 error = make_nfs_auth();
547 if (fp->fh_sin.sin_port == 0) {
549 error = get_mountd_port(fp->fh_fs, &mountd_port, wchan);
552 fp->fh_sin.sin_port = mountd_port;
553 dlog("%s: New %d mountd port", __func__, fp->fh_sin.sin_port);
555 dlog("%s: Already had %d mountd port", __func__, fp->fh_sin.sin_port);
557 /* find the right version of the mount protocol */
559 if (fp->fh_nfs_version == NFS_VERSION3)
560 mnt_version = AM_MOUNTVERS3;
562 #endif /* HAVE_FS_NFS3 */
563 mnt_version = MOUNTVERS;
564 plog(XLOG_INFO, "call_mountd: NFS version %d, mount version %d",
565 (int) fp->fh_nfs_version, (int) mnt_version);
567 rpc_msg_init(&mnt_msg, MOUNTPROG, mnt_version, MOUNTPROC_NULL);
568 len = make_rpc_packet(iobuf,
572 (voidp) &fp->fh_path,
573 (XDRPROC_T_TYPE) xdr_nfspath,
577 error = fwd_packet(MK_RPC_XID(RPC_XID_MOUNTD, fp->fh_id),
582 (opaque_t) ((long) fp->fh_id), /* cast to long needed for 64-bit archs */
589 * It may be the case that we're sending to the wrong MOUNTD port. This
590 * occurs if mountd is restarted on the server after the port has been
591 * looked up and stored in the filehandle cache somewhere. The correct
592 * solution, if we're going to cache port numbers is to catch the ICMP
593 * port unreachable reply from the server and cause the portmap request
594 * to be redone. The quick solution here is to invalidate the MOUNTD
597 fp->fh_sin.sin_port = 0;
604 webnfs_lookup(fh_cache *fp, fwd_fun fun, wchan_t wchan)
606 struct rpc_msg wnfs_msg;
608 char iobuf[UDPMSGSIZE];
611 XDRPROC_T_TYPE xdr_fn;
615 am_LOOKUP3args args3;
616 #endif /* HAVE_FS_NFS3 */
621 error = make_nfs_auth();
626 if (fp->fh_sin.sin_port == 0) {
627 /* FIXME: wrong, don't discard sin_port in the first place for WebNFS. */
628 plog(XLOG_WARNING, "webnfs_lookup: port == 0 for nfs on %s, fixed",
630 fp->fh_sin.sin_port = htons(NFS_PORT);
634 * Use native path like the rest of amd (cf. RFC 2054, 6.1).
636 l = strlen(fp->fh_path) + 2;
637 wnfs_path = (char *) xmalloc(l);
639 xstrlcpy(wnfs_path + 1, fp->fh_path, l - 1);
641 /* find the right program and lookup procedure */
643 if (fp->fh_nfs_version == NFS_VERSION3) {
644 proc = AM_NFSPROC3_LOOKUP;
645 xdr_fn = (XDRPROC_T_TYPE) xdr_am_LOOKUP3args;
647 /* WebNFS public file handle */
648 args3.what.dir.am_fh3_length = 0;
649 args3.what.name = wnfs_path;
651 #endif /* HAVE_FS_NFS3 */
652 proc = NFSPROC_LOOKUP;
653 xdr_fn = (XDRPROC_T_TYPE) xdr_diropargs;
655 /* WebNFS public file handle */
656 memset(&args.da_fhandle, 0, NFS_FHSIZE);
657 args.da_name = wnfs_path;
660 #endif /* HAVE_FS_NFS3 */
662 plog(XLOG_INFO, "webnfs_lookup: NFS version %d", (int) fp->fh_nfs_version);
664 rpc_msg_init(&wnfs_msg, NFS_PROGRAM, fp->fh_nfs_version, proc);
665 len = make_rpc_packet(iobuf,
670 (XDRPROC_T_TYPE) xdr_fn,
674 error = fwd_packet(MK_RPC_XID(RPC_XID_WEBNFS, fp->fh_id),
679 (opaque_t) ((long) fp->fh_id), /* cast to long needed for 64-bit archs */
691 * NFS needs the local filesystem, remote filesystem
693 * Local filesystem defaults to remote and vice-versa.
696 nfs_match(am_opts *fo)
701 if (fo->opt_fs && !fo->opt_rfs)
702 fo->opt_rfs = fo->opt_fs;
704 plog(XLOG_USER, "nfs: no remote filesystem specified");
707 if (!fo->opt_rhost) {
708 plog(XLOG_USER, "nfs: no remote host specified");
713 * Determine magic cookie to put in mtab
715 l = strlen(fo->opt_rhost) + strlen(fo->opt_rfs) + 2;
716 xmtab = (char *) xmalloc(l);
717 xsnprintf(xmtab, l, "%s:%s", fo->opt_rhost, fo->opt_rfs);
718 dlog("NFS: mounting remote server \"%s\", remote fs \"%s\" on \"%s\"",
719 fo->opt_rhost, fo->opt_rfs, fo->opt_fs);
726 * Initialize am structure for nfs
737 * We don't need file handles for NFS version 4, but we can fall back to
738 * version 3, so we allocate anyway
741 if (mf->mf_server->fs_version == NFS_VERSION4)
743 #endif /* HAVE_FS_NFS4 */
744 #endif /* NO_FALLBACK */
746 if (mf->mf_private) {
747 if (mf->mf_flags & MFF_NFS_SCALEDOWN) {
750 /* tell remote mountd that we're done with this filehandle */
751 mf->mf_ops->umounted(mf);
753 mf->mf_prfree(mf->mf_private);
754 mf->mf_private = NULL;
755 mf->mf_prfree = NULL;
757 fs = mf->mf_ops->ffserver(mf);
758 free_srvr(mf->mf_server);
764 colon = strchr(mf->mf_info, ':');
768 error = prime_nfs_fhandle_cache(colon + 1, mf->mf_server, &fhs, mf);
770 mf->mf_private = (opaque_t) ALLOC(am_nfs_handle_t);
771 mf->mf_prfree = (void (*)(opaque_t)) free;
772 memmove(mf->mf_private, (voidp) &fhs, sizeof(fhs));
779 mount_nfs_fh(am_nfs_handle_t *fhp, char *mntdir, char *fs_name, mntfs *mf)
783 char *xopts=NULL, transp_timeo_opts[40], transp_retrans_opts[40];
784 char host[MAXHOSTNAMELEN + MAXPATHLEN + 2];
785 fserver *fs = mf->mf_server;
786 u_long nfs_version = fs->fs_version;
787 char *nfs_proto = fs->fs_proto; /* "tcp" or "udp" */
788 int on_autofs = mf->mf_flags & MFF_ON_AUTOFS;
792 int proto = AMU_TYPE_NONE;
797 nfs4_args_t nfs4_args;
798 #endif /* HAVE_FS_NFS4 */
801 * Extract HOST name to give to kernel.
802 * Some systems like osf1/aix3/bsd44 variants may need old code
803 * for NFS_ARGS_NEEDS_PATH.
805 if (!(colon = strchr(fs_name, ':')))
807 #ifdef MOUNT_TABLE_ON_FILE
809 #endif /* MOUNT_TABLE_ON_FILE */
810 xstrlcpy(host, fs_name, sizeof(host));
811 #ifdef MOUNT_TABLE_ON_FILE
813 #endif /* MOUNT_TABLE_ON_FILE */
814 #ifdef MAXHOSTNAMELEN
815 /* most kernels have a name length restriction */
816 if (strlen(host) >= MAXHOSTNAMELEN)
817 xstrlcpy(host + MAXHOSTNAMELEN - 3, "..",
818 sizeof(host) - MAXHOSTNAMELEN + 3);
819 #endif /* MAXHOSTNAMELEN */
822 * Create option=VAL for udp/tcp specific timeouts and retrans values, but
823 * only if these options were specified.
826 transp_timeo_opts[0] = transp_retrans_opts[0] = '\0'; /* initialize */
827 if (STREQ(nfs_proto, "udp"))
828 proto = AMU_TYPE_UDP;
829 else if (STREQ(nfs_proto, "tcp"))
830 proto = AMU_TYPE_TCP;
831 if (proto != AMU_TYPE_NONE) {
832 if (gopt.amfs_auto_timeo[proto] > 0)
833 xsnprintf(transp_timeo_opts, sizeof(transp_timeo_opts), "%s=%d,",
834 MNTTAB_OPT_TIMEO, gopt.amfs_auto_timeo[proto]);
835 if (gopt.amfs_auto_retrans[proto] > 0)
836 xsnprintf(transp_retrans_opts, sizeof(transp_retrans_opts), "%s=%d,",
837 MNTTAB_OPT_RETRANS, gopt.amfs_auto_retrans[proto]);
840 if (mf->mf_remopts && *mf->mf_remopts &&
841 !islocalnet(fs->fs_ip->sin_addr.s_addr)) {
842 plog(XLOG_INFO, "Using remopts=\"%s\"", mf->mf_remopts);
843 /* use transp_opts first, so map-specific opts will override */
844 xopts = str3cat(xopts, transp_timeo_opts, transp_retrans_opts, mf->mf_remopts);
846 /* use transp_opts first, so map-specific opts will override */
847 xopts = str3cat(xopts, transp_timeo_opts, transp_retrans_opts, mf->mf_mopts);
850 memset((voidp) &mnt, 0, sizeof(mnt));
851 mnt.mnt_dir = mntdir;
852 mnt.mnt_fsname = fs_name;
853 mnt.mnt_opts = xopts;
856 * Set mount types accordingly
859 if (nfs_version == NFS_VERSION3) {
860 type = MOUNT_TYPE_NFS3;
862 * Systems that include the mount table "vers" option generally do not
863 * set the mnttab entry to "nfs3", but to "nfs" and then they set
864 * "vers=3". Setting it to "nfs3" works, but it may break some things
865 * like "df -t nfs" and the "quota" program (esp. on Solaris and Irix).
866 * So on those systems, set it to "nfs".
867 * Note: MNTTAB_OPT_VERS is always set for NFS3 (see am_compat.h).
870 # if defined(MNTTAB_OPT_VERS) && defined(MOUNT_TABLE_ON_FILE)
871 mnt.mnt_type = MNTTAB_TYPE_NFS;
872 # else /* defined(MNTTAB_OPT_VERS) && defined(MOUNT_TABLE_ON_FILE) */
873 mnt.mnt_type = MNTTAB_TYPE_NFS3;
874 # endif /* defined(MNTTAB_OPT_VERS) && defined(MOUNT_TABLE_ON_FILE) */
876 } else if (nfs_version == NFS_VERSION4) {
878 type = MOUNT_TYPE_NFS4;
879 mnt.mnt_type = MNTTAB_TYPE_NFS4;
880 # endif /* HAVE_FS_NFS4 */
882 #endif /* HAVE_FS_NFS3 */
885 type = MOUNT_TYPE_NFS;
886 mnt.mnt_type = MNTTAB_TYPE_NFS;
888 plog(XLOG_INFO, "mount_nfs_fh: NFS version %d", (int) nfs_version);
889 plog(XLOG_INFO, "mount_nfs_fh: using NFS transport %s", nfs_proto);
891 retry = hasmntval(&mnt, MNTTAB_OPT_RETRY);
895 genflags = compute_mount_flags(&mnt);
896 #ifdef HAVE_FS_AUTOFS
898 genflags |= autofs_compute_mount_flags(&mnt);
899 #endif /* HAVE_FS_AUTOFS */
901 /* setup the many fields and flags within nfs_args */
902 compute_nfs_args(argsp,
905 NULL, /* struct netconfig *nfsncp */
913 /* finally call the mounting function */
914 if (amuDebug(D_TRACE)) {
915 print_nfs_args(argsp, nfs_version);
916 plog(XLOG_DEBUG, "Generic mount flags 0x%x used for NFS mount", genflags);
918 error = mount_fs(&mnt, genflags, argsp, retry, type,
919 nfs_version, nfs_proto, mnttab_file_name, on_autofs);
921 discard_nfs_args(argsp, nfs_version);
926 * If we are using a v4 file handle, we try a v3 if we get back:
927 * ENOENT: NFS v4 has a different export list than v3
928 * EPERM: Kernels <= 2.6.18 return that, instead of ENOENT
930 if ((error == ENOENT || error == EPERM) && nfs_version == NFS_VERSION4) {
931 plog(XLOG_DEBUG, "Could not find NFS 4 mount, trying again with NFS 3");
932 fs->fs_version = NFS_VERSION3;
933 error = mount_nfs_fh(fhp, mntdir, fs_name, mf);
935 fs->fs_version = NFS_VERSION4;
937 # endif /* NO_FALLBACK */
938 #endif /* HAVE_FS_NFS4 */
945 nfs_mount(am_node *am, mntfs *mf)
950 if (!mf->mf_private && mf->mf_server->fs_version != 4) {
951 plog(XLOG_ERROR, "Missing filehandle for %s", mf->mf_info);
955 if (mf->mf_mopts == NULL) {
956 plog(XLOG_ERROR, "Missing mount options for %s", mf->mf_info);
960 mnt.mnt_opts = mf->mf_mopts;
961 if (amu_hasmntopt(&mnt, "softlookup") ||
962 (amu_hasmntopt(&mnt, "soft") && !amu_hasmntopt(&mnt, "nosoftlookup")))
963 am->am_flags |= AMF_SOFTLOOKUP;
965 error = mount_nfs_fh((am_nfs_handle_t *) mf->mf_private,
972 dlog("mount_nfs: %m");
980 nfs_umount(am_node *am, mntfs *mf)
982 int unmount_flags, new_unmount_flags, error;
984 dlog("attempting nfs umount");
985 unmount_flags = (mf->mf_flags & MFF_ON_AUTOFS) ? AMU_UMOUNT_AUTOFS : 0;
986 error = UMOUNT_FS(mf->mf_mount, mnttab_file_name, unmount_flags);
988 #if defined(HAVE_UMOUNT2) && (defined(MNT2_GEN_OPT_FORCE) || defined(MNT2_GEN_OPT_DETACH))
990 * If the attempt to unmount failed with EBUSY, and this fserver was
991 * marked for forced unmounts, then use forced/lazy unmounts.
993 if (error == EBUSY &&
994 gopt.flags & CFM_FORCED_UNMOUNTS &&
995 mf->mf_server->fs_flags & FSF_FORCE_UNMOUNT) {
996 plog(XLOG_INFO, "EZK: nfs_umount: trying forced/lazy unmounts");
998 * XXX: turning off the FSF_FORCE_UNMOUNT may not be perfectly
999 * incorrect. Multiple nodes may need to be timed out and restarted for
1000 * a single hung fserver.
1002 mf->mf_server->fs_flags &= ~FSF_FORCE_UNMOUNT;
1003 new_unmount_flags = unmount_flags | AMU_UMOUNT_FORCE | AMU_UMOUNT_DETACH;
1004 error = UMOUNT_FS(mf->mf_mount, mnttab_file_name, new_unmount_flags);
1006 #endif /* HAVE_UMOUNT2 && (MNT2_GEN_OPT_FORCE || MNT2_GEN_OPT_DETACH) */
1009 * Here is some code to unmount 'restarted' file systems.
1010 * The restarted file systems are marked as 'nfs', not
1011 * 'host', so we only have the map information for the
1012 * the top-level mount. The unmount will fail (EBUSY)
1013 * if there are anything else from the NFS server mounted
1014 * below the mount-point. This code checks to see if there
1015 * is anything mounted with the same prefix as the
1016 * file system to be unmounted ("/a/b/c" when unmounting "/a/b").
1017 * If there is, and it is a 'restarted' file system, we unmount
1019 * Added by Mike Mitchell, mcm@unx.sas.com, 09/08/93
1021 if (error == EBUSY) {
1023 int len = strlen(mf->mf_mount);
1026 ITER(new_mf, mntfs, &mfhead) {
1027 if (new_mf->mf_ops != mf->mf_ops ||
1028 new_mf->mf_refc > 1 ||
1030 ((new_mf->mf_flags & (MFF_MOUNTED | MFF_UNMOUNTING | MFF_RESTART)) == (MFF_MOUNTED | MFF_RESTART)))
1033 if (NSTREQ(mf->mf_mount, new_mf->mf_mount, len) &&
1034 new_mf->mf_mount[len] == '/') {
1036 (new_mf->mf_flags & MFF_ON_AUTOFS) ? AMU_UMOUNT_AUTOFS : 0;
1037 UMOUNT_FS(new_mf->mf_mount, mnttab_file_name, new_unmount_flags);
1042 error = UMOUNT_FS(mf->mf_mount, mnttab_file_name, unmount_flags);
1052 nfs_umounted(mntfs *mf)
1057 if (mf->mf_error || mf->mf_refc > 1)
1061 * No need to inform mountd when WebNFS is in use.
1063 if (mf->mf_flags & MFF_WEBNFS)
1067 * Call the mount daemon on the server to announce that we are not using
1070 * XXX: This is *wrong*. The mountd should be called when the fhandle is
1071 * flushed from the cache, and a reference held to the cached entry while
1072 * the fs is mounted...
1075 colon = path = strchr(mf->mf_info, ':');
1079 dlog("calling mountd for %s", mf->mf_info);
1082 f.fh_sin = *fs->fs_ip;
1083 f.fh_sin.sin_port = (u_short) 0;
1084 f.fh_nfs_version = SET_FH_VERSION(fs);
1088 prime_nfs_fhandle_cache(colon + 1, mf->mf_server, (am_nfs_handle_t *) NULL, mf);
1089 call_mountd(&f, MOUNTPROC_UMNT, (fwd_fun *) NULL, (wchan_t) NULL);