2 * Copyright (c) 1997-2006 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. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgment:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * File: am-utils/amd/ops_nfs.c
50 #endif /* HAVE_CONFIG_H */
55 * Convert from nfsstat to UN*X error code
57 #define unx_error(e) ((int)(e))
60 * FH_TTL is the time a file handle will remain in the cache since
61 * last being used. If the file handle becomes invalid, then it
62 * will be flushed anyway.
64 #define FH_TTL (5 * 60) /* five minutes */
65 #define FH_TTL_ERROR (30) /* 30 seconds */
66 #define FHID_ALLOC() (++fh_id)
69 * The NFS layer maintains a cache of file handles.
70 * This is *fundamental* to the implementation and
71 * also allows quick remounting when a filesystem
72 * is accessed soon after timing out.
74 * The NFS server layer knows to flush this cache
75 * when a server goes down so avoiding stale handles.
77 * Each cache entry keeps a hard reference to
78 * the corresponding server. This ensures that
79 * the server keepalive information is maintained.
81 * The copy of the sockaddr_in here is taken so
82 * that the port can be twiddled to talk to mountd
83 * instead of portmap or the NFS server as used
85 * The port# is flushed if a server goes down.
86 * The IP address is never flushed - we assume
87 * that the address of a mounted machine never
88 * changes. If it does, then you have other
91 typedef struct fh_cache fh_cache;
93 qelem fh_q; /* List header */
94 wchan_t fh_wchan; /* Wait channel */
95 int fh_error; /* Valid data? */
96 int fh_id; /* Unique id */
97 int fh_cid; /* Callout id */
98 u_long fh_nfs_version; /* highest NFS version on host */
99 am_nfs_handle_t fh_nfs_handle; /* Handle on filesystem */
100 int fh_status; /* Status of last rpc */
101 struct sockaddr_in fh_sin; /* Address of mountd */
102 fserver *fh_fs; /* Server holding filesystem */
103 char *fh_path; /* Filesystem on host */
106 /* forward definitions */
107 static int nfs_init(mntfs *mf);
108 static char *nfs_match(am_opts *fo);
109 static int nfs_mount(am_node *am, mntfs *mf);
110 static int nfs_umount(am_node *am, mntfs *mf);
111 static void nfs_umounted(mntfs *mf);
112 static int call_mountd(fh_cache *fp, u_long proc, fwd_fun f, wchan_t wchan);
113 static int webnfs_lookup(fh_cache *fp, fwd_fun f, wchan_t wchan);
114 static int fh_id = 0;
118 qelem fh_head = {&fh_head, &fh_head};
121 * Network file system operations
130 amfs_error_lookup_child,
131 amfs_error_mount_child,
133 0, /* nfs_readlink */
137 0, /* nfs_get_wchan */
138 FS_MKMNT | FS_BACKGROUND | FS_AMQINFO, /* nfs_fs_flags */
139 #ifdef HAVE_FS_AUTOFS
141 #endif /* HAVE_FS_AUTOFS */
146 find_nfs_fhandle_cache(opaque_t arg, int done)
148 fh_cache *fp, *fp2 = 0;
149 int id = (long) arg; /* for 64-bit archs */
151 ITER(fp, fh_cache, &fh_head) {
152 if (fp->fh_id == id) {
159 dlog("fh cache gives fp %#lx, fs %s", (unsigned long) fp2, fp2->fh_path);
161 dlog("fh cache search failed");
165 fp2->fh_error = ETIMEDOUT;
174 * Called when a filehandle appears via the mount protocol
177 got_nfs_fh_mount(voidp pkt, int len, struct sockaddr_in *sa, struct sockaddr_in *ia, opaque_t arg, int done)
182 struct am_mountres3 res3;
183 #endif /* HAVE_FS_NFS3 */
185 fp = find_nfs_fhandle_cache(arg, done);
190 * retrieve the correct RPC reply for the file handle, based on the
191 * NFS protocol version.
194 if (fp->fh_nfs_version == NFS_VERSION3) {
195 memset(&res3, 0, sizeof(res3));
196 fp->fh_error = pickup_rpc_reply(pkt, len, (voidp) &res3,
197 (XDRPROC_T_TYPE) xdr_am_mountres3);
198 fp->fh_status = unx_error(res3.fhs_status);
199 memset(&fp->fh_nfs_handle.v3, 0, sizeof(am_nfs_fh3));
200 fp->fh_nfs_handle.v3.am_fh3_length = res3.mountres3_u.mountinfo.fhandle.fhandle3_len;
201 memmove(fp->fh_nfs_handle.v3.am_fh3_data,
202 res3.mountres3_u.mountinfo.fhandle.fhandle3_val,
203 fp->fh_nfs_handle.v3.am_fh3_length);
205 #endif /* HAVE_FS_NFS3 */
206 memset(&res, 0, sizeof(res));
207 fp->fh_error = pickup_rpc_reply(pkt, len, (voidp) &res,
208 (XDRPROC_T_TYPE) xdr_fhstatus);
209 fp->fh_status = unx_error(res.fhs_status);
210 memmove(&fp->fh_nfs_handle.v2, &res.fhs_fh, NFS_FHSIZE);
213 #endif /* HAVE_FS_NFS3 */
216 dlog("got filehandle for %s:%s", fp->fh_fs->fs_host, fp->fh_path);
218 plog(XLOG_USER, "filehandle denied for %s:%s", fp->fh_fs->fs_host, fp->fh_path);
220 * Force the error to be EACCES. It's debatable whether it should be
221 * ENOENT instead, but the server really doesn't give us any clues, and
222 * EACCES is more in line with the "filehandle denied" message.
224 fp->fh_error = EACCES;
228 * Wakeup anything sleeping on this filehandle
231 dlog("Calling wakeup on %#lx", (unsigned long) fp->fh_wchan);
232 wakeup(fp->fh_wchan);
238 * Called when a filehandle appears via WebNFS
241 got_nfs_fh_webnfs(voidp pkt, int len, struct sockaddr_in *sa, struct sockaddr_in *ia, opaque_t arg, int done)
247 #endif /* HAVE_FS_NFS3 */
249 fp = find_nfs_fhandle_cache(arg, done);
254 * retrieve the correct RPC reply for the file handle, based on the
255 * NFS protocol version.
258 if (fp->fh_nfs_version == NFS_VERSION3) {
259 memset(&res3, 0, sizeof(res3));
260 fp->fh_error = pickup_rpc_reply(pkt, len, (voidp) &res3,
261 (XDRPROC_T_TYPE) xdr_am_LOOKUP3res);
262 fp->fh_status = unx_error(res3.status);
263 memset(&fp->fh_nfs_handle.v3, 0, sizeof(am_nfs_fh3));
264 fp->fh_nfs_handle.v3.am_fh3_length = res3.res_u.ok.object.am_fh3_length;
265 memmove(fp->fh_nfs_handle.v3.am_fh3_data,
266 res3.res_u.ok.object.am_fh3_data,
267 fp->fh_nfs_handle.v3.am_fh3_length);
269 #endif /* HAVE_FS_NFS3 */
270 memset(&res, 0, sizeof(res));
271 fp->fh_error = pickup_rpc_reply(pkt, len, (voidp) &res,
272 (XDRPROC_T_TYPE) xdr_diropres);
273 fp->fh_status = unx_error(res.dr_status);
274 memmove(&fp->fh_nfs_handle.v2, &res.dr_u.dr_drok_u.drok_fhandle, NFS_FHSIZE);
277 #endif /* HAVE_FS_NFS3 */
280 dlog("got filehandle for %s:%s", fp->fh_fs->fs_host, fp->fh_path);
282 plog(XLOG_USER, "filehandle denied for %s:%s", fp->fh_fs->fs_host, fp->fh_path);
284 * Force the error to be EACCES. It's debatable whether it should be
285 * ENOENT instead, but the server really doesn't give us any clues, and
286 * EACCES is more in line with the "filehandle denied" message.
288 fp->fh_error = EACCES;
292 * Wakeup anything sleeping on this filehandle
295 dlog("Calling wakeup on %#lx", (unsigned long) fp->fh_wchan);
296 wakeup(fp->fh_wchan);
302 flush_nfs_fhandle_cache(fserver *fs)
306 ITER(fp, fh_cache, &fh_head) {
307 if (fp->fh_fs == fs || fs == NULL) {
309 * Only invalidate port info for non-WebNFS servers
311 if (!(fp->fh_fs->fs_flags & FSF_WEBNFS))
312 fp->fh_sin.sin_port = (u_short) 0;
320 discard_fh(opaque_t arg)
322 fh_cache *fp = (fh_cache *) arg;
326 dlog("Discarding filehandle for %s:%s", fp->fh_fs->fs_host, fp->fh_path);
327 free_srvr(fp->fh_fs);
336 * Determine the file handle for a node
339 prime_nfs_fhandle_cache(char *path, fserver *fs, am_nfs_handle_t *fhbuf, mntfs *mf)
341 fh_cache *fp, *fp_save = 0;
343 int reuse_id = FALSE;
345 dlog("Searching cache for %s:%s", fs->fs_host, path);
348 * First search the cache
350 ITER(fp, fh_cache, &fh_head) {
351 if (fs != fp->fh_fs || !STREQ(path, fp->fh_path))
352 continue; /* skip to next ITER item */
353 /* else we got a match */
354 switch (fp->fh_error) {
356 plog(XLOG_INFO, "prime_nfs_fhandle_cache: NFS version %d", (int) fp->fh_nfs_version);
358 error = fp->fh_error = fp->fh_status;
361 if (mf->mf_flags & MFF_NFS_SCALEDOWN) {
363 /* XXX: why reuse the ID? */
370 if (fp->fh_nfs_version == NFS_VERSION3) {
371 memmove((voidp) &(fhbuf->v3), (voidp) &(fp->fh_nfs_handle.v3),
372 sizeof(fp->fh_nfs_handle.v3));
374 #endif /* HAVE_FS_NFS3 */
376 memmove((voidp) &(fhbuf->v2), (voidp) &(fp->fh_nfs_handle.v2),
377 sizeof(fp->fh_nfs_handle.v2));
381 untimeout(fp->fh_cid);
382 fp->fh_cid = timeout(FH_TTL, discard_fh, (opaque_t) fp);
383 } else if (error == EACCES) {
385 * Now decode the file handle return code.
387 plog(XLOG_INFO, "Filehandle denied for \"%s:%s\"",
390 errno = error; /* XXX */
391 plog(XLOG_INFO, "Filehandle error for \"%s:%s\": %m",
396 * The error was returned from the remote mount daemon.
397 * Policy: this error will be cached for now...
403 * Still thinking about it, but we can re-use.
412 * Policy: make sure we recompute if required again
413 * in case this was caused by a network failure.
414 * This can thrash mountd's though... If you find
415 * your mountd going slowly then:
416 * 1. Add a fork() loop to main.
417 * 2. Remove the call to innetgr() and don't use
418 * netgroups, especially if you don't use YP.
420 error = fp->fh_error;
423 } /* end of switch statement */
424 } /* end of ITER loop */
432 * Re-use existing slot
434 untimeout(fp->fh_cid);
435 free_srvr(fp->fh_fs);
438 fp = ALLOC(struct fh_cache);
439 memset((voidp) fp, 0, sizeof(struct fh_cache));
440 ins_que(&fp->fh_q, &fh_head);
443 fp->fh_id = FHID_ALLOC();
444 fp->fh_wchan = get_mntfs_wchan(mf);
446 fp->fh_cid = timeout(FH_TTL, discard_fh, (opaque_t) fp);
449 * If fs->fs_ip is null, remote server is probably down.
452 /* Mark the fileserver down and invalid again */
453 fs->fs_flags &= ~FSF_VALID;
454 fs->fs_flags |= FSF_DOWN;
455 error = AM_ERRNO_HOST_DOWN;
460 * Either fp has been freshly allocated or the address has changed.
461 * Initialize address and nfs version. Don't try to re-use the port
462 * information unless using WebNFS where the port is fixed either by
463 * the spec or the "port" mount option.
465 if (fp->fh_sin.sin_addr.s_addr != fs->fs_ip->sin_addr.s_addr) {
466 fp->fh_sin = *fs->fs_ip;
467 if (!(mf->mf_flags & MFF_WEBNFS))
468 fp->fh_sin.sin_port = 0;
469 fp->fh_nfs_version = fs->fs_version;
472 fp->fh_fs = dup_srvr(fs);
473 fp->fh_path = strdup(path);
475 if (mf->mf_flags & MFF_WEBNFS)
476 error = webnfs_lookup(fp, got_nfs_fh_webnfs, get_mntfs_wchan(mf));
478 error = call_mountd(fp, MOUNTPROC_MNT, got_nfs_fh_mount, get_mntfs_wchan(mf));
481 * Local error - cache for a short period
482 * just to prevent thrashing.
484 untimeout(fp->fh_cid);
485 fp->fh_cid = timeout(error < 0 ? 2 * ALLOWED_MOUNT_TIME : FH_TTL_ERROR,
486 discard_fh, (opaque_t) fp);
487 fp->fh_error = error;
489 error = fp->fh_error;
499 AUTH_CREATE_GIDLIST_TYPE group_wheel = 0;
501 /* Some NFS mounts (particularly cross-domain) require FQDNs to succeed */
503 #ifdef HAVE_TRANSPORT_TYPE_TLI
504 if (gopt.flags & CFM_FULLY_QUALIFIED_HOSTS) {
505 plog(XLOG_INFO, "Using NFS auth for FQHN \"%s\"", hostd);
506 nfs_auth = authsys_create(hostd, 0, 0, 1, &group_wheel);
508 nfs_auth = authsys_create_default();
510 #else /* not HAVE_TRANSPORT_TYPE_TLI */
511 if (gopt.flags & CFM_FULLY_QUALIFIED_HOSTS) {
512 plog(XLOG_INFO, "Using NFS auth for FQHN \"%s\"", hostd);
513 nfs_auth = authunix_create(hostd, 0, 0, 1, &group_wheel);
515 nfs_auth = authunix_create_default();
517 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
527 call_mountd(fh_cache *fp, u_long proc, fwd_fun fun, wchan_t wchan)
529 struct rpc_msg mnt_msg;
531 char iobuf[UDPMSGSIZE];
536 error = make_nfs_auth();
541 if (fp->fh_sin.sin_port == 0) {
543 error = get_mountd_port(fp->fh_fs, &mountd_port, wchan);
546 fp->fh_sin.sin_port = mountd_port;
549 /* find the right version of the mount protocol */
551 if (fp->fh_nfs_version == NFS_VERSION3)
552 mnt_version = AM_MOUNTVERS3;
554 #endif /* HAVE_FS_NFS3 */
555 mnt_version = MOUNTVERS;
556 plog(XLOG_INFO, "call_mountd: NFS version %d, mount version %d",
557 (int) fp->fh_nfs_version, (int) mnt_version);
559 rpc_msg_init(&mnt_msg, MOUNTPROG, mnt_version, MOUNTPROC_NULL);
560 len = make_rpc_packet(iobuf,
564 (voidp) &fp->fh_path,
565 (XDRPROC_T_TYPE) xdr_nfspath,
569 error = fwd_packet(MK_RPC_XID(RPC_XID_MOUNTD, fp->fh_id),
574 (opaque_t) ((long) fp->fh_id), /* cast to long needed for 64-bit archs */
581 * It may be the case that we're sending to the wrong MOUNTD port. This
582 * occurs if mountd is restarted on the server after the port has been
583 * looked up and stored in the filehandle cache somewhere. The correct
584 * solution, if we're going to cache port numbers is to catch the ICMP
585 * port unreachable reply from the server and cause the portmap request
586 * to be redone. The quick solution here is to invalidate the MOUNTD
589 fp->fh_sin.sin_port = 0;
596 webnfs_lookup(fh_cache *fp, fwd_fun fun, wchan_t wchan)
598 struct rpc_msg wnfs_msg;
600 char iobuf[UDPMSGSIZE];
603 XDRPROC_T_TYPE xdr_fn;
607 am_LOOKUP3args args3;
613 error = make_nfs_auth();
618 if (fp->fh_sin.sin_port == 0) {
619 /* FIXME: wrong, don't discard sin_port in the first place for WebNFS. */
620 plog(XLOG_WARNING, "webnfs_lookup: port == 0 for nfs on %s, fixed",
622 fp->fh_sin.sin_port = htons(NFS_PORT);
626 * Use native path like the rest of amd (cf. RFC 2054, 6.1).
628 l = strlen(fp->fh_path) + 2;
629 wnfs_path = (char *) xmalloc(l);
631 xstrlcpy(wnfs_path + 1, fp->fh_path, l - 1);
633 /* find the right program and lookup procedure */
635 if (fp->fh_nfs_version == NFS_VERSION3) {
636 proc = AM_NFSPROC3_LOOKUP;
637 xdr_fn = (XDRPROC_T_TYPE) xdr_am_LOOKUP3args;
639 /* WebNFS public file handle */
640 args3.what.dir.am_fh3_length = 0;
641 args3.what.name = wnfs_path;
643 #endif /* HAVE_FS_NFS3 */
644 proc = NFSPROC_LOOKUP;
645 xdr_fn = (XDRPROC_T_TYPE) xdr_diropargs;
647 /* WebNFS public file handle */
648 memset(&args.da_fhandle, 0, NFS_FHSIZE);
649 args.da_name = wnfs_path;
652 #endif /* HAVE_FS_NFS3 */
654 plog(XLOG_INFO, "webnfs_lookup: NFS version %d", (int) fp->fh_nfs_version);
656 rpc_msg_init(&wnfs_msg, NFS_PROGRAM, fp->fh_nfs_version, proc);
657 len = make_rpc_packet(iobuf,
662 (XDRPROC_T_TYPE) xdr_fn,
666 error = fwd_packet(MK_RPC_XID(RPC_XID_WEBNFS, fp->fh_id),
671 (opaque_t) ((long) fp->fh_id), /* cast to long needed for 64-bit archs */
683 * NFS needs the local filesystem, remote filesystem
685 * Local filesystem defaults to remote and vice-versa.
688 nfs_match(am_opts *fo)
693 if (fo->opt_fs && !fo->opt_rfs)
694 fo->opt_rfs = fo->opt_fs;
696 plog(XLOG_USER, "nfs: no remote filesystem specified");
699 if (!fo->opt_rhost) {
700 plog(XLOG_USER, "nfs: no remote host specified");
705 * Determine magic cookie to put in mtab
707 l = strlen(fo->opt_rhost) + strlen(fo->opt_rfs) + 2;
708 xmtab = (char *) xmalloc(l);
709 xsnprintf(xmtab, l, "%s:%s", fo->opt_rhost, fo->opt_rfs);
710 dlog("NFS: mounting remote server \"%s\", remote fs \"%s\" on \"%s\"",
711 fo->opt_rhost, fo->opt_rfs, fo->opt_fs);
718 * Initialize am structure for nfs
727 if (mf->mf_private) {
728 if (mf->mf_flags & MFF_NFS_SCALEDOWN) {
731 /* tell remote mountd that we're done with this filehandle */
732 mf->mf_ops->umounted(mf);
734 mf->mf_prfree(mf->mf_private);
735 fs = mf->mf_ops->ffserver(mf);
736 free_srvr(mf->mf_server);
742 colon = strchr(mf->mf_info, ':');
746 error = prime_nfs_fhandle_cache(colon + 1, mf->mf_server, &fhs, mf);
748 mf->mf_private = (opaque_t) ALLOC(am_nfs_handle_t);
749 mf->mf_prfree = (void (*)(opaque_t)) free;
750 memmove(mf->mf_private, (voidp) &fhs, sizeof(fhs));
757 mount_nfs_fh(am_nfs_handle_t *fhp, char *mntdir, char *fs_name, mntfs *mf)
761 char *xopts=NULL, transp_timeo_opts[40], transp_retrans_opts[40];
762 char host[MAXHOSTNAMELEN + MAXPATHLEN + 2];
763 fserver *fs = mf->mf_server;
764 u_long nfs_version = fs->fs_version;
765 char *nfs_proto = fs->fs_proto; /* "tcp" or "udp" */
766 int on_autofs = mf->mf_flags & MFF_ON_AUTOFS;
770 int proto = AMU_TYPE_NONE;
775 * Extract HOST name to give to kernel.
776 * Some systems like osf1/aix3/bsd44 variants may need old code
777 * for NFS_ARGS_NEEDS_PATH.
779 if (!(colon = strchr(fs_name, ':')))
781 #ifdef MOUNT_TABLE_ON_FILE
783 #endif /* MOUNT_TABLE_ON_FILE */
784 xstrlcpy(host, fs_name, sizeof(host));
785 #ifdef MOUNT_TABLE_ON_FILE
787 #endif /* MOUNT_TABLE_ON_FILE */
788 #ifdef MAXHOSTNAMELEN
789 /* most kernels have a name length restriction */
790 if (strlen(host) >= MAXHOSTNAMELEN)
791 xstrlcpy(host + MAXHOSTNAMELEN - 3, "..",
792 sizeof(host) - MAXHOSTNAMELEN + 3);
793 #endif /* MAXHOSTNAMELEN */
796 * Create option=VAL for udp/tcp specific timeouts and retrans values, but
797 * only if these options were specified.
800 transp_timeo_opts[0] = transp_retrans_opts[0] = '\0'; /* initialize */
801 if (STREQ(nfs_proto, "udp"))
802 proto = AMU_TYPE_UDP;
803 else if (STREQ(nfs_proto, "tcp"))
804 proto = AMU_TYPE_TCP;
805 if (proto != AMU_TYPE_NONE) {
806 if (gopt.amfs_auto_timeo[proto] > 0)
807 xsnprintf(transp_timeo_opts, sizeof(transp_timeo_opts), "%s=%d,",
808 MNTTAB_OPT_TIMEO, gopt.amfs_auto_timeo[proto]);
809 if (gopt.amfs_auto_retrans[proto] > 0)
810 xsnprintf(transp_retrans_opts, sizeof(transp_retrans_opts), "%s=%d,",
811 MNTTAB_OPT_RETRANS, gopt.amfs_auto_retrans[proto]);
814 if (mf->mf_remopts && *mf->mf_remopts &&
815 !islocalnet(fs->fs_ip->sin_addr.s_addr)) {
816 plog(XLOG_INFO, "Using remopts=\"%s\"", mf->mf_remopts);
817 /* use transp_opts first, so map-specific opts will override */
818 xopts = str3cat(xopts, transp_timeo_opts, transp_retrans_opts, mf->mf_remopts);
820 /* use transp_opts first, so map-specific opts will override */
821 xopts = str3cat(xopts, transp_timeo_opts, transp_retrans_opts, mf->mf_mopts);
824 memset((voidp) &mnt, 0, sizeof(mnt));
825 mnt.mnt_dir = mntdir;
826 mnt.mnt_fsname = fs_name;
827 mnt.mnt_opts = xopts;
830 * Set mount types accordingly
833 type = MOUNT_TYPE_NFS;
834 mnt.mnt_type = MNTTAB_TYPE_NFS;
835 #else /* HAVE_FS_NFS3 */
836 if (nfs_version == NFS_VERSION3) {
837 type = MOUNT_TYPE_NFS3;
839 * Systems that include the mount table "vers" option generally do not
840 * set the mnttab entry to "nfs3", but to "nfs" and then they set
841 * "vers=3". Setting it to "nfs3" works, but it may break some things
842 * like "df -t nfs" and the "quota" program (esp. on Solaris and Irix).
843 * So on those systems, set it to "nfs".
844 * Note: MNTTAB_OPT_VERS is always set for NFS3 (see am_compat.h).
846 # if defined(MNTTAB_OPT_VERS) && defined(MOUNT_TABLE_ON_FILE)
847 mnt.mnt_type = MNTTAB_TYPE_NFS;
848 # else /* defined(MNTTAB_OPT_VERS) && defined(MOUNT_TABLE_ON_FILE) */
849 mnt.mnt_type = MNTTAB_TYPE_NFS3;
850 # endif /* defined(MNTTAB_OPT_VERS) && defined(MOUNT_TABLE_ON_FILE) */
852 type = MOUNT_TYPE_NFS;
853 mnt.mnt_type = MNTTAB_TYPE_NFS;
855 #endif /* HAVE_FS_NFS3 */
856 plog(XLOG_INFO, "mount_nfs_fh: NFS version %d", (int) nfs_version);
857 plog(XLOG_INFO, "mount_nfs_fh: using NFS transport %s", nfs_proto);
859 retry = hasmntval(&mnt, MNTTAB_OPT_RETRY);
863 genflags = compute_mount_flags(&mnt);
864 #ifdef HAVE_FS_AUTOFS
866 genflags |= autofs_compute_mount_flags(&mnt);
867 #endif /* HAVE_FS_AUTOFS */
869 /* setup the many fields and flags within nfs_args */
870 compute_nfs_args(&nfs_args,
873 NULL, /* struct netconfig *nfsncp */
881 /* finally call the mounting function */
882 if (amuDebug(D_TRACE)) {
883 print_nfs_args(&nfs_args, nfs_version);
884 plog(XLOG_DEBUG, "Generic mount flags 0x%x used for NFS mount", genflags);
886 error = mount_fs(&mnt, genflags, (caddr_t) &nfs_args, retry, type,
887 nfs_version, nfs_proto, mnttab_file_name, on_autofs);
890 #ifdef HAVE_TRANSPORT_TYPE_TLI
891 free_knetconfig(nfs_args.knconf);
893 XFREE(nfs_args.addr); /* allocated in compute_nfs_args() */
894 #endif /* HAVE_TRANSPORT_TYPE_TLI */
901 nfs_mount(am_node *am, mntfs *mf)
906 if (!mf->mf_private) {
907 plog(XLOG_ERROR, "Missing filehandle for %s", mf->mf_info);
911 mnt.mnt_opts = mf->mf_mopts;
912 if (amu_hasmntopt(&mnt, "softlookup") ||
913 (amu_hasmntopt(&mnt, "soft") && !amu_hasmntopt(&mnt, "nosoftlookup")))
914 am->am_flags |= AMF_SOFTLOOKUP;
916 error = mount_nfs_fh((am_nfs_handle_t *) mf->mf_private,
923 dlog("mount_nfs: %m");
931 nfs_umount(am_node *am, mntfs *mf)
933 int unmount_flags, new_unmount_flags, error;
935 unmount_flags = (mf->mf_flags & MFF_ON_AUTOFS) ? AMU_UMOUNT_AUTOFS : 0;
936 error = UMOUNT_FS(mf->mf_mount, mnttab_file_name, unmount_flags);
938 #if defined(HAVE_UMOUNT2) && (defined(MNT2_GEN_OPT_FORCE) || defined(MNT2_GEN_OPT_DETACH))
940 * If the attempt to unmount failed with EBUSY, and this fserver was
941 * marked for forced unmounts, then use forced/lazy unmounts.
943 if (error == EBUSY &&
944 gopt.flags & CFM_FORCED_UNMOUNTS &&
945 mf->mf_server->fs_flags & FSF_FORCE_UNMOUNT) {
946 plog(XLOG_INFO, "EZK: nfs_umount: trying forced/lazy unmounts");
948 * XXX: turning off the FSF_FORCE_UNMOUNT may not be perfectly
949 * incorrect. Multiple nodes may need to be timed out and restarted for
950 * a single hung fserver.
952 mf->mf_server->fs_flags &= ~FSF_FORCE_UNMOUNT;
953 new_unmount_flags = unmount_flags | AMU_UMOUNT_FORCE | AMU_UMOUNT_DETACH;
954 error = UMOUNT_FS(mf->mf_mount, mnttab_file_name, new_unmount_flags);
956 #endif /* HAVE_UMOUNT2 && (MNT2_GEN_OPT_FORCE || MNT2_GEN_OPT_DETACH) */
959 * Here is some code to unmount 'restarted' file systems.
960 * The restarted file systems are marked as 'nfs', not
961 * 'host', so we only have the map information for the
962 * the top-level mount. The unmount will fail (EBUSY)
963 * if there are anything else from the NFS server mounted
964 * below the mount-point. This code checks to see if there
965 * is anything mounted with the same prefix as the
966 * file system to be unmounted ("/a/b/c" when unmounting "/a/b").
967 * If there is, and it is a 'restarted' file system, we unmount
969 * Added by Mike Mitchell, mcm@unx.sas.com, 09/08/93
971 if (error == EBUSY) {
973 int len = strlen(mf->mf_mount);
976 ITER(new_mf, mntfs, &mfhead) {
977 if (new_mf->mf_ops != mf->mf_ops ||
978 new_mf->mf_refc > 1 ||
980 ((new_mf->mf_flags & (MFF_MOUNTED | MFF_UNMOUNTING | MFF_RESTART)) == (MFF_MOUNTED | MFF_RESTART)))
983 if (NSTREQ(mf->mf_mount, new_mf->mf_mount, len) &&
984 new_mf->mf_mount[len] == '/') {
986 (new_mf->mf_flags & MFF_ON_AUTOFS) ? AMU_UMOUNT_AUTOFS : 0;
987 UMOUNT_FS(new_mf->mf_mount, mnttab_file_name, new_unmount_flags);
992 error = UMOUNT_FS(mf->mf_mount, mnttab_file_name, unmount_flags);
1002 nfs_umounted(mntfs *mf)
1007 if (mf->mf_error || mf->mf_refc > 1)
1011 * No need to inform mountd when WebNFS is in use.
1013 if (mf->mf_flags & MFF_WEBNFS)
1017 * Call the mount daemon on the server to announce that we are not using
1020 * XXX: This is *wrong*. The mountd should be called when the fhandle is
1021 * flushed from the cache, and a reference held to the cached entry while
1022 * the fs is mounted...
1025 colon = path = strchr(mf->mf_info, ':');
1029 dlog("calling mountd for %s", mf->mf_info);
1032 f.fh_sin = *fs->fs_ip;
1033 f.fh_sin.sin_port = (u_short) 0;
1034 f.fh_nfs_version = fs->fs_version;
1038 prime_nfs_fhandle_cache(colon + 1, mf->mf_server, (am_nfs_handle_t *) 0, mf);
1039 call_mountd(&f, MOUNTPROC_UMNT, (fwd_fun *) 0, (wchan_t) 0);