xref: /netbsd-src/external/bsd/am-utils/dist/amd/ops_nfs.c (revision 8bae5d409deb915cf7c8f0539fae22ff2cb8a313)
1*8bae5d40Schristos /*	$NetBSD: ops_nfs.c,v 1.1.1.3 2015/01/17 16:34:15 christos Exp $	*/
2a53f50b9Schristos 
3a53f50b9Schristos /*
4*8bae5d40Schristos  * Copyright (c) 1997-2014 Erez Zadok
5a53f50b9Schristos  * Copyright (c) 1990 Jan-Simon Pendry
6a53f50b9Schristos  * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
7a53f50b9Schristos  * Copyright (c) 1990 The Regents of the University of California.
8a53f50b9Schristos  * All rights reserved.
9a53f50b9Schristos  *
10a53f50b9Schristos  * This code is derived from software contributed to Berkeley by
11a53f50b9Schristos  * Jan-Simon Pendry at Imperial College, London.
12a53f50b9Schristos  *
13a53f50b9Schristos  * Redistribution and use in source and binary forms, with or without
14a53f50b9Schristos  * modification, are permitted provided that the following conditions
15a53f50b9Schristos  * are met:
16a53f50b9Schristos  * 1. Redistributions of source code must retain the above copyright
17a53f50b9Schristos  *    notice, this list of conditions and the following disclaimer.
18a53f50b9Schristos  * 2. Redistributions in binary form must reproduce the above copyright
19a53f50b9Schristos  *    notice, this list of conditions and the following disclaimer in the
20a53f50b9Schristos  *    documentation and/or other materials provided with the distribution.
21*8bae5d40Schristos  * 3. Neither the name of the University nor the names of its contributors
22a53f50b9Schristos  *    may be used to endorse or promote products derived from this software
23a53f50b9Schristos  *    without specific prior written permission.
24a53f50b9Schristos  *
25a53f50b9Schristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26a53f50b9Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27a53f50b9Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28a53f50b9Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29a53f50b9Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30a53f50b9Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31a53f50b9Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32a53f50b9Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33a53f50b9Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34a53f50b9Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35a53f50b9Schristos  * SUCH DAMAGE.
36a53f50b9Schristos  *
37a53f50b9Schristos  *
38a53f50b9Schristos  * File: am-utils/amd/ops_nfs.c
39a53f50b9Schristos  *
40a53f50b9Schristos  */
41a53f50b9Schristos 
42a53f50b9Schristos /*
43a53f50b9Schristos  * Network file system
44a53f50b9Schristos  */
45a53f50b9Schristos 
46a53f50b9Schristos #ifdef HAVE_CONFIG_H
47a53f50b9Schristos # include <config.h>
48a53f50b9Schristos #endif /* HAVE_CONFIG_H */
49a53f50b9Schristos #include <am_defs.h>
50a53f50b9Schristos #include <amd.h>
51a53f50b9Schristos 
52a53f50b9Schristos /*
53a53f50b9Schristos  * Convert from nfsstat to UN*X error code
54a53f50b9Schristos  */
55a53f50b9Schristos #define unx_error(e)	((int)(e))
56a53f50b9Schristos 
57a53f50b9Schristos /*
58a53f50b9Schristos  * FH_TTL is the time a file handle will remain in the cache since
59a53f50b9Schristos  * last being used.  If the file handle becomes invalid, then it
60a53f50b9Schristos  * will be flushed anyway.
61a53f50b9Schristos  */
62a53f50b9Schristos #define	FH_TTL			(5 * 60) /* five minutes */
63a53f50b9Schristos #define	FH_TTL_ERROR		(30) /* 30 seconds */
64a53f50b9Schristos #define	FHID_ALLOC()		(++fh_id)
65a53f50b9Schristos 
66a53f50b9Schristos /*
67a53f50b9Schristos  * The NFS layer maintains a cache of file handles.
68a53f50b9Schristos  * This is *fundamental* to the implementation and
69a53f50b9Schristos  * also allows quick remounting when a filesystem
70a53f50b9Schristos  * is accessed soon after timing out.
71a53f50b9Schristos  *
72a53f50b9Schristos  * The NFS server layer knows to flush this cache
73a53f50b9Schristos  * when a server goes down so avoiding stale handles.
74a53f50b9Schristos  *
75a53f50b9Schristos  * Each cache entry keeps a hard reference to
76a53f50b9Schristos  * the corresponding server.  This ensures that
77a53f50b9Schristos  * the server keepalive information is maintained.
78a53f50b9Schristos  *
79a53f50b9Schristos  * The copy of the sockaddr_in here is taken so
80a53f50b9Schristos  * that the port can be twiddled to talk to mountd
81a53f50b9Schristos  * instead of portmap or the NFS server as used
82a53f50b9Schristos  * elsewhere.
83a53f50b9Schristos  * The port# is flushed if a server goes down.
84a53f50b9Schristos  * The IP address is never flushed - we assume
85a53f50b9Schristos  * that the address of a mounted machine never
86a53f50b9Schristos  * changes.  If it does, then you have other
87a53f50b9Schristos  * problems...
88a53f50b9Schristos  */
89a53f50b9Schristos typedef struct fh_cache fh_cache;
90a53f50b9Schristos struct fh_cache {
91a53f50b9Schristos   qelem			fh_q;		/* List header */
92a53f50b9Schristos   wchan_t		fh_wchan;	/* Wait channel */
93a53f50b9Schristos   int			fh_error;	/* Valid data? */
94a53f50b9Schristos   int			fh_id;		/* Unique id */
95a53f50b9Schristos   int			fh_cid;		/* Callout id */
96a53f50b9Schristos   u_long		fh_nfs_version;	/* highest NFS version on host */
97a53f50b9Schristos   am_nfs_handle_t	fh_nfs_handle;	/* Handle on filesystem */
98a53f50b9Schristos   int			fh_status;	/* Status of last rpc */
99a53f50b9Schristos   struct sockaddr_in	fh_sin;		/* Address of mountd */
100a53f50b9Schristos   fserver		*fh_fs;		/* Server holding filesystem */
101a53f50b9Schristos   char			*fh_path;	/* Filesystem on host */
102a53f50b9Schristos };
103a53f50b9Schristos 
104a53f50b9Schristos /* forward definitions */
105a53f50b9Schristos static int nfs_init(mntfs *mf);
106a53f50b9Schristos static char *nfs_match(am_opts *fo);
107a53f50b9Schristos static int nfs_mount(am_node *am, mntfs *mf);
108a53f50b9Schristos static int nfs_umount(am_node *am, mntfs *mf);
109a53f50b9Schristos static void nfs_umounted(mntfs *mf);
110a53f50b9Schristos static int call_mountd(fh_cache *fp, u_long proc, fwd_fun f, wchan_t wchan);
111a53f50b9Schristos static int webnfs_lookup(fh_cache *fp, fwd_fun f, wchan_t wchan);
112a53f50b9Schristos static int fh_id = 0;
113a53f50b9Schristos 
114*8bae5d40Schristos /*
115*8bae5d40Schristos  * clamp the filehandle version to 3, so that we can fail back to nfsv3
116*8bae5d40Schristos  * since nfsv4 does not have file handles
117*8bae5d40Schristos  */
118*8bae5d40Schristos #define SET_FH_VERSION(fs) \
119*8bae5d40Schristos     (fs)->fs_version > NFS_VERSION3 ? NFS_VERSION3 : (fs)->fs_version;
120*8bae5d40Schristos 
121a53f50b9Schristos /* globals */
122a53f50b9Schristos AUTH *nfs_auth;
123a53f50b9Schristos qelem fh_head = {&fh_head, &fh_head};
124a53f50b9Schristos 
125a53f50b9Schristos /*
126a53f50b9Schristos  * Network file system operations
127a53f50b9Schristos  */
128a53f50b9Schristos am_ops nfs_ops =
129a53f50b9Schristos {
130a53f50b9Schristos   "nfs",
131a53f50b9Schristos   nfs_match,
132a53f50b9Schristos   nfs_init,
133a53f50b9Schristos   nfs_mount,
134a53f50b9Schristos   nfs_umount,
135a53f50b9Schristos   amfs_error_lookup_child,
136a53f50b9Schristos   amfs_error_mount_child,
137a53f50b9Schristos   amfs_error_readdir,
138a53f50b9Schristos   0,				/* nfs_readlink */
139a53f50b9Schristos   0,				/* nfs_mounted */
140a53f50b9Schristos   nfs_umounted,
141a53f50b9Schristos   find_nfs_srvr,
142a53f50b9Schristos   0,				/* nfs_get_wchan */
143a53f50b9Schristos   FS_MKMNT | FS_BACKGROUND | FS_AMQINFO,	/* nfs_fs_flags */
144a53f50b9Schristos #ifdef HAVE_FS_AUTOFS
145a53f50b9Schristos   AUTOFS_NFS_FS_FLAGS,
146a53f50b9Schristos #endif /* HAVE_FS_AUTOFS */
147a53f50b9Schristos };
148a53f50b9Schristos 
149a53f50b9Schristos 
150a53f50b9Schristos static fh_cache *
find_nfs_fhandle_cache(opaque_t arg,int done)151a53f50b9Schristos find_nfs_fhandle_cache(opaque_t arg, int done)
152a53f50b9Schristos {
153a53f50b9Schristos   fh_cache *fp, *fp2 = NULL;
154a53f50b9Schristos   int id = (long) arg;		/* for 64-bit archs */
155a53f50b9Schristos 
156a53f50b9Schristos   ITER(fp, fh_cache, &fh_head) {
157a53f50b9Schristos     if (fp->fh_id == id) {
158a53f50b9Schristos       fp2 = fp;
159a53f50b9Schristos       break;
160a53f50b9Schristos     }
161a53f50b9Schristos   }
162a53f50b9Schristos 
163a53f50b9Schristos   if (fp2) {
164a53f50b9Schristos     dlog("fh cache gives fp %#lx, fs %s", (unsigned long) fp2, fp2->fh_path);
165a53f50b9Schristos   } else {
166a53f50b9Schristos     dlog("fh cache search failed");
167a53f50b9Schristos   }
168a53f50b9Schristos 
169a53f50b9Schristos   if (fp2 && !done) {
170a53f50b9Schristos     fp2->fh_error = ETIMEDOUT;
171a53f50b9Schristos     return 0;
172a53f50b9Schristos   }
173a53f50b9Schristos 
174a53f50b9Schristos   return fp2;
175a53f50b9Schristos }
176a53f50b9Schristos 
177a53f50b9Schristos 
178a53f50b9Schristos /*
179a53f50b9Schristos  * Called when a filehandle appears via the mount protocol
180a53f50b9Schristos  */
181a53f50b9Schristos static void
got_nfs_fh_mount(voidp pkt,int len,struct sockaddr_in * sa,struct sockaddr_in * ia,opaque_t arg,int done)182a53f50b9Schristos got_nfs_fh_mount(voidp pkt, int len, struct sockaddr_in *sa, struct sockaddr_in *ia, opaque_t arg, int done)
183a53f50b9Schristos {
184a53f50b9Schristos   fh_cache *fp;
185a53f50b9Schristos   struct fhstatus res;
186a53f50b9Schristos #ifdef HAVE_FS_NFS3
187a53f50b9Schristos   struct am_mountres3 res3;
188a53f50b9Schristos #endif /* HAVE_FS_NFS3 */
189a53f50b9Schristos 
190a53f50b9Schristos   fp = find_nfs_fhandle_cache(arg, done);
191a53f50b9Schristos   if (!fp)
192a53f50b9Schristos     return;
193a53f50b9Schristos 
194a53f50b9Schristos   /*
195a53f50b9Schristos    * retrieve the correct RPC reply for the file handle, based on the
196a53f50b9Schristos    * NFS protocol version.
197a53f50b9Schristos    */
198a53f50b9Schristos #ifdef HAVE_FS_NFS3
199a53f50b9Schristos   if (fp->fh_nfs_version == NFS_VERSION3) {
200a53f50b9Schristos     memset(&res3, 0, sizeof(res3));
201a53f50b9Schristos     fp->fh_error = pickup_rpc_reply(pkt, len, (voidp) &res3,
202a53f50b9Schristos 				    (XDRPROC_T_TYPE) xdr_am_mountres3);
203a53f50b9Schristos     fp->fh_status = unx_error(res3.fhs_status);
204a53f50b9Schristos     memset(&fp->fh_nfs_handle.v3, 0, sizeof(am_nfs_fh3));
205a53f50b9Schristos     fp->fh_nfs_handle.v3.am_fh3_length = res3.mountres3_u.mountinfo.fhandle.fhandle3_len;
206a53f50b9Schristos     memmove(fp->fh_nfs_handle.v3.am_fh3_data,
207a53f50b9Schristos 	    res3.mountres3_u.mountinfo.fhandle.fhandle3_val,
208a53f50b9Schristos 	    fp->fh_nfs_handle.v3.am_fh3_length);
209*8bae5d40Schristos 
210*8bae5d40Schristos     XFREE(res3.mountres3_u.mountinfo.fhandle.fhandle3_val);
211*8bae5d40Schristos     if (res3.mountres3_u.mountinfo.auth_flavors.auth_flavors_val)
212*8bae5d40Schristos       XFREE(res3.mountres3_u.mountinfo.auth_flavors.auth_flavors_val);
213a53f50b9Schristos   } else {
214a53f50b9Schristos #endif /* HAVE_FS_NFS3 */
215a53f50b9Schristos     memset(&res, 0, sizeof(res));
216a53f50b9Schristos     fp->fh_error = pickup_rpc_reply(pkt, len, (voidp) &res,
217a53f50b9Schristos 				    (XDRPROC_T_TYPE) xdr_fhstatus);
218a53f50b9Schristos     fp->fh_status = unx_error(res.fhs_status);
219a53f50b9Schristos     memmove(&fp->fh_nfs_handle.v2, &res.fhs_fh, NFS_FHSIZE);
220a53f50b9Schristos #ifdef HAVE_FS_NFS3
221a53f50b9Schristos   }
222a53f50b9Schristos #endif /* HAVE_FS_NFS3 */
223a53f50b9Schristos 
224a53f50b9Schristos   if (!fp->fh_error) {
225a53f50b9Schristos     dlog("got filehandle for %s:%s", fp->fh_fs->fs_host, fp->fh_path);
226a53f50b9Schristos   } else {
227a53f50b9Schristos     plog(XLOG_USER, "filehandle denied for %s:%s", fp->fh_fs->fs_host, fp->fh_path);
228a53f50b9Schristos     /*
229a53f50b9Schristos      * Force the error to be EACCES. It's debatable whether it should be
230a53f50b9Schristos      * ENOENT instead, but the server really doesn't give us any clues, and
231a53f50b9Schristos      * EACCES is more in line with the "filehandle denied" message.
232a53f50b9Schristos      */
233a53f50b9Schristos     fp->fh_error = EACCES;
234a53f50b9Schristos   }
235a53f50b9Schristos 
236a53f50b9Schristos   /*
237a53f50b9Schristos    * Wakeup anything sleeping on this filehandle
238a53f50b9Schristos    */
239a53f50b9Schristos   if (fp->fh_wchan) {
240a53f50b9Schristos     dlog("Calling wakeup on %#lx", (unsigned long) fp->fh_wchan);
241a53f50b9Schristos     wakeup(fp->fh_wchan);
242a53f50b9Schristos   }
243a53f50b9Schristos }
244a53f50b9Schristos 
245a53f50b9Schristos 
246a53f50b9Schristos /*
247a53f50b9Schristos  * Called when a filehandle appears via WebNFS
248a53f50b9Schristos  */
249a53f50b9Schristos static void
got_nfs_fh_webnfs(voidp pkt,int len,struct sockaddr_in * sa,struct sockaddr_in * ia,opaque_t arg,int done)250a53f50b9Schristos got_nfs_fh_webnfs(voidp pkt, int len, struct sockaddr_in *sa, struct sockaddr_in *ia, opaque_t arg, int done)
251a53f50b9Schristos {
252a53f50b9Schristos   fh_cache *fp;
253a53f50b9Schristos   nfsdiropres res;
254a53f50b9Schristos #ifdef HAVE_FS_NFS3
255a53f50b9Schristos   am_LOOKUP3res res3;
256a53f50b9Schristos #endif /* HAVE_FS_NFS3 */
257a53f50b9Schristos 
258a53f50b9Schristos   fp = find_nfs_fhandle_cache(arg, done);
259a53f50b9Schristos   if (!fp)
260a53f50b9Schristos     return;
261a53f50b9Schristos 
262a53f50b9Schristos   /*
263a53f50b9Schristos    * retrieve the correct RPC reply for the file handle, based on the
264a53f50b9Schristos    * NFS protocol version.
265a53f50b9Schristos    */
266a53f50b9Schristos #ifdef HAVE_FS_NFS3
267a53f50b9Schristos   if (fp->fh_nfs_version == NFS_VERSION3) {
268a53f50b9Schristos     memset(&res3, 0, sizeof(res3));
269a53f50b9Schristos     fp->fh_error = pickup_rpc_reply(pkt, len, (voidp) &res3,
270a53f50b9Schristos 				    (XDRPROC_T_TYPE) xdr_am_LOOKUP3res);
271a53f50b9Schristos     fp->fh_status = unx_error(res3.status);
272a53f50b9Schristos     memset(&fp->fh_nfs_handle.v3, 0, sizeof(am_nfs_fh3));
273a53f50b9Schristos     fp->fh_nfs_handle.v3.am_fh3_length = res3.res_u.ok.object.am_fh3_length;
274a53f50b9Schristos     memmove(fp->fh_nfs_handle.v3.am_fh3_data,
275a53f50b9Schristos 	    res3.res_u.ok.object.am_fh3_data,
276a53f50b9Schristos 	    fp->fh_nfs_handle.v3.am_fh3_length);
277a53f50b9Schristos   } else {
278a53f50b9Schristos #endif /* HAVE_FS_NFS3 */
279a53f50b9Schristos     memset(&res, 0, sizeof(res));
280a53f50b9Schristos     fp->fh_error = pickup_rpc_reply(pkt, len, (voidp) &res,
281a53f50b9Schristos 				    (XDRPROC_T_TYPE) xdr_diropres);
282a53f50b9Schristos     fp->fh_status = unx_error(res.dr_status);
283a53f50b9Schristos     memmove(&fp->fh_nfs_handle.v2, &res.dr_u.dr_drok_u.drok_fhandle, NFS_FHSIZE);
284a53f50b9Schristos #ifdef HAVE_FS_NFS3
285a53f50b9Schristos   }
286a53f50b9Schristos #endif /* HAVE_FS_NFS3 */
287a53f50b9Schristos 
288a53f50b9Schristos   if (!fp->fh_error) {
289a53f50b9Schristos     dlog("got filehandle for %s:%s", fp->fh_fs->fs_host, fp->fh_path);
290a53f50b9Schristos   } else {
291a53f50b9Schristos     plog(XLOG_USER, "filehandle denied for %s:%s", fp->fh_fs->fs_host, fp->fh_path);
292a53f50b9Schristos     /*
293a53f50b9Schristos      * Force the error to be EACCES. It's debatable whether it should be
294a53f50b9Schristos      * ENOENT instead, but the server really doesn't give us any clues, and
295a53f50b9Schristos      * EACCES is more in line with the "filehandle denied" message.
296a53f50b9Schristos      */
297a53f50b9Schristos     fp->fh_error = EACCES;
298a53f50b9Schristos   }
299a53f50b9Schristos 
300a53f50b9Schristos   /*
301a53f50b9Schristos    * Wakeup anything sleeping on this filehandle
302a53f50b9Schristos    */
303a53f50b9Schristos   if (fp->fh_wchan) {
304a53f50b9Schristos     dlog("Calling wakeup on %#lx", (unsigned long) fp->fh_wchan);
305a53f50b9Schristos     wakeup(fp->fh_wchan);
306a53f50b9Schristos   }
307a53f50b9Schristos }
308a53f50b9Schristos 
309a53f50b9Schristos 
310a53f50b9Schristos void
flush_nfs_fhandle_cache(fserver * fs)311a53f50b9Schristos flush_nfs_fhandle_cache(fserver *fs)
312a53f50b9Schristos {
313a53f50b9Schristos   fh_cache *fp;
314a53f50b9Schristos 
315a53f50b9Schristos   ITER(fp, fh_cache, &fh_head) {
316a53f50b9Schristos     if (fp->fh_fs == fs || fs == NULL) {
317a53f50b9Schristos       /*
318a53f50b9Schristos        * Only invalidate port info for non-WebNFS servers
319a53f50b9Schristos        */
320a53f50b9Schristos       if (!(fp->fh_fs->fs_flags & FSF_WEBNFS))
321a53f50b9Schristos 	fp->fh_sin.sin_port = (u_short) 0;
322a53f50b9Schristos       fp->fh_error = -1;
323a53f50b9Schristos     }
324a53f50b9Schristos   }
325a53f50b9Schristos }
326a53f50b9Schristos 
327a53f50b9Schristos 
328a53f50b9Schristos static void
discard_fh(opaque_t arg)329a53f50b9Schristos discard_fh(opaque_t arg)
330a53f50b9Schristos {
331a53f50b9Schristos   fh_cache *fp = (fh_cache *) arg;
332a53f50b9Schristos 
333a53f50b9Schristos   rem_que(&fp->fh_q);
334a53f50b9Schristos   if (fp->fh_fs) {
335a53f50b9Schristos     dlog("Discarding filehandle for %s:%s", fp->fh_fs->fs_host, fp->fh_path);
336a53f50b9Schristos     free_srvr(fp->fh_fs);
337a53f50b9Schristos   }
338a53f50b9Schristos   XFREE(fp->fh_path);
339a53f50b9Schristos   XFREE(fp);
340a53f50b9Schristos }
341a53f50b9Schristos 
342a53f50b9Schristos 
343a53f50b9Schristos /*
344a53f50b9Schristos  * Determine the file handle for a node
345a53f50b9Schristos  */
346a53f50b9Schristos static int
prime_nfs_fhandle_cache(char * path,fserver * fs,am_nfs_handle_t * fhbuf,mntfs * mf)347a53f50b9Schristos prime_nfs_fhandle_cache(char *path, fserver *fs, am_nfs_handle_t *fhbuf, mntfs *mf)
348a53f50b9Schristos {
349a53f50b9Schristos   fh_cache *fp, *fp_save = NULL;
350a53f50b9Schristos   int error;
351a53f50b9Schristos   int reuse_id = FALSE;
352a53f50b9Schristos 
353a53f50b9Schristos   dlog("Searching cache for %s:%s", fs->fs_host, path);
354a53f50b9Schristos 
355a53f50b9Schristos   /*
356a53f50b9Schristos    * First search the cache
357a53f50b9Schristos    */
358a53f50b9Schristos   ITER(fp, fh_cache, &fh_head) {
359a53f50b9Schristos     if (fs != fp->fh_fs  ||  !STREQ(path, fp->fh_path))
360a53f50b9Schristos       continue;			/* skip to next ITER item */
361a53f50b9Schristos     /* else we got a match */
362a53f50b9Schristos     switch (fp->fh_error) {
363a53f50b9Schristos     case 0:
364a53f50b9Schristos       plog(XLOG_INFO, "prime_nfs_fhandle_cache: NFS version %d", (int) fp->fh_nfs_version);
365a53f50b9Schristos 
366a53f50b9Schristos       error = fp->fh_error = fp->fh_status;
367a53f50b9Schristos 
368a53f50b9Schristos       if (error == 0) {
369a53f50b9Schristos 	if (mf->mf_flags & MFF_NFS_SCALEDOWN) {
370a53f50b9Schristos 	  fp_save = fp;
371a53f50b9Schristos 	  /* XXX: why reuse the ID? */
372a53f50b9Schristos 	  reuse_id = TRUE;
373a53f50b9Schristos 	  break;
374a53f50b9Schristos 	}
375a53f50b9Schristos 
376a53f50b9Schristos 	if (fhbuf) {
377a53f50b9Schristos #ifdef HAVE_FS_NFS3
378a53f50b9Schristos 	  if (fp->fh_nfs_version == NFS_VERSION3) {
379a53f50b9Schristos 	    memmove((voidp) &(fhbuf->v3), (voidp) &(fp->fh_nfs_handle.v3),
380a53f50b9Schristos 		    sizeof(fp->fh_nfs_handle.v3));
381a53f50b9Schristos 	  } else
382a53f50b9Schristos #endif /* HAVE_FS_NFS3 */
383a53f50b9Schristos 	    {
384a53f50b9Schristos 	      memmove((voidp) &(fhbuf->v2), (voidp) &(fp->fh_nfs_handle.v2),
385a53f50b9Schristos 		      sizeof(fp->fh_nfs_handle.v2));
386a53f50b9Schristos 	    }
387a53f50b9Schristos 	}
388a53f50b9Schristos 	if (fp->fh_cid)
389a53f50b9Schristos 	  untimeout(fp->fh_cid);
390a53f50b9Schristos 	fp->fh_cid = timeout(FH_TTL, discard_fh, (opaque_t) fp);
391a53f50b9Schristos       } else if (error == EACCES) {
392a53f50b9Schristos 	/*
393a53f50b9Schristos 	 * Now decode the file handle return code.
394a53f50b9Schristos 	 */
395a53f50b9Schristos 	plog(XLOG_INFO, "Filehandle denied for \"%s:%s\"",
396a53f50b9Schristos 	     fs->fs_host, path);
397a53f50b9Schristos       } else {
398a53f50b9Schristos 	errno = error;	/* XXX */
399a53f50b9Schristos 	plog(XLOG_INFO, "Filehandle error for \"%s:%s\": %m",
400a53f50b9Schristos 	     fs->fs_host, path);
401a53f50b9Schristos       }
402a53f50b9Schristos 
403a53f50b9Schristos       /*
404a53f50b9Schristos        * The error was returned from the remote mount daemon.
405a53f50b9Schristos        * Policy: this error will be cached for now...
406a53f50b9Schristos        */
407a53f50b9Schristos       return error;
408a53f50b9Schristos 
409a53f50b9Schristos     case -1:
410a53f50b9Schristos       /*
411a53f50b9Schristos        * Still thinking about it, but we can re-use.
412a53f50b9Schristos        */
413a53f50b9Schristos       fp_save = fp;
414a53f50b9Schristos       reuse_id = TRUE;
415a53f50b9Schristos       break;
416a53f50b9Schristos 
417a53f50b9Schristos     default:
418a53f50b9Schristos       /*
419a53f50b9Schristos        * Return the error.
420a53f50b9Schristos        * Policy: make sure we recompute if required again
421a53f50b9Schristos        * in case this was caused by a network failure.
422a53f50b9Schristos        * This can thrash mountd's though...  If you find
423a53f50b9Schristos        * your mountd going slowly then:
424a53f50b9Schristos        * 1.  Add a fork() loop to main.
425a53f50b9Schristos        * 2.  Remove the call to innetgr() and don't use
426a53f50b9Schristos        *     netgroups, especially if you don't use YP.
427a53f50b9Schristos        */
428a53f50b9Schristos       error = fp->fh_error;
429a53f50b9Schristos       fp->fh_error = -1;
430a53f50b9Schristos       return error;
431a53f50b9Schristos     }	/* end of switch statement */
432a53f50b9Schristos   } /* end of ITER loop */
433a53f50b9Schristos 
434a53f50b9Schristos   /*
435a53f50b9Schristos    * Not in cache
436a53f50b9Schristos    */
437a53f50b9Schristos   if (fp_save) {
438a53f50b9Schristos     fp = fp_save;
439a53f50b9Schristos     /*
440a53f50b9Schristos      * Re-use existing slot
441a53f50b9Schristos      */
442a53f50b9Schristos     untimeout(fp->fh_cid);
443a53f50b9Schristos     free_srvr(fp->fh_fs);
444a53f50b9Schristos     XFREE(fp->fh_path);
445a53f50b9Schristos   } else {
446a53f50b9Schristos     fp = ALLOC(struct fh_cache);
447a53f50b9Schristos     memset((voidp) fp, 0, sizeof(struct fh_cache));
448a53f50b9Schristos     ins_que(&fp->fh_q, &fh_head);
449a53f50b9Schristos   }
450a53f50b9Schristos   if (!reuse_id)
451a53f50b9Schristos     fp->fh_id = FHID_ALLOC();
452a53f50b9Schristos   fp->fh_wchan = get_mntfs_wchan(mf);
453a53f50b9Schristos   fp->fh_error = -1;
454a53f50b9Schristos   fp->fh_cid = timeout(FH_TTL, discard_fh, (opaque_t) fp);
455a53f50b9Schristos 
456a53f50b9Schristos   /*
457a53f50b9Schristos    * If fs->fs_ip is null, remote server is probably down.
458a53f50b9Schristos    */
459a53f50b9Schristos   if (!fs->fs_ip) {
460a53f50b9Schristos     /* Mark the fileserver down and invalid again */
461a53f50b9Schristos     fs->fs_flags &= ~FSF_VALID;
462a53f50b9Schristos     fs->fs_flags |= FSF_DOWN;
463a53f50b9Schristos     error = AM_ERRNO_HOST_DOWN;
464a53f50b9Schristos     return error;
465a53f50b9Schristos   }
466a53f50b9Schristos 
467a53f50b9Schristos   /*
468a53f50b9Schristos    * Either fp has been freshly allocated or the address has changed.
469a53f50b9Schristos    * Initialize address and nfs version.  Don't try to re-use the port
470a53f50b9Schristos    * information unless using WebNFS where the port is fixed either by
471a53f50b9Schristos    * the spec or the "port" mount option.
472a53f50b9Schristos    */
473a53f50b9Schristos   if (fp->fh_sin.sin_addr.s_addr != fs->fs_ip->sin_addr.s_addr) {
474a53f50b9Schristos     fp->fh_sin = *fs->fs_ip;
475a53f50b9Schristos     if (!(mf->mf_flags & MFF_WEBNFS))
476a53f50b9Schristos 	fp->fh_sin.sin_port = 0;
477*8bae5d40Schristos     fp->fh_nfs_version = SET_FH_VERSION(fs);
478a53f50b9Schristos   }
479a53f50b9Schristos 
480a53f50b9Schristos   fp->fh_fs = dup_srvr(fs);
481*8bae5d40Schristos   fp->fh_path = xstrdup(path);
482a53f50b9Schristos 
483a53f50b9Schristos   if (mf->mf_flags & MFF_WEBNFS)
484a53f50b9Schristos     error = webnfs_lookup(fp, got_nfs_fh_webnfs, get_mntfs_wchan(mf));
485a53f50b9Schristos   else
486a53f50b9Schristos     error = call_mountd(fp, MOUNTPROC_MNT, got_nfs_fh_mount, get_mntfs_wchan(mf));
487a53f50b9Schristos   if (error) {
488a53f50b9Schristos     /*
489a53f50b9Schristos      * Local error - cache for a short period
490a53f50b9Schristos      * just to prevent thrashing.
491a53f50b9Schristos      */
492a53f50b9Schristos     untimeout(fp->fh_cid);
493a53f50b9Schristos     fp->fh_cid = timeout(error < 0 ? 2 * ALLOWED_MOUNT_TIME : FH_TTL_ERROR,
494a53f50b9Schristos 			 discard_fh, (opaque_t) fp);
495a53f50b9Schristos     fp->fh_error = error;
496a53f50b9Schristos   } else {
497a53f50b9Schristos     error = fp->fh_error;
498a53f50b9Schristos   }
499a53f50b9Schristos 
500a53f50b9Schristos   return error;
501a53f50b9Schristos }
502a53f50b9Schristos 
503a53f50b9Schristos 
504a53f50b9Schristos int
make_nfs_auth(void)505a53f50b9Schristos make_nfs_auth(void)
506a53f50b9Schristos {
507a53f50b9Schristos   AUTH_CREATE_GIDLIST_TYPE group_wheel = 0;
508a53f50b9Schristos 
509a53f50b9Schristos   /* Some NFS mounts (particularly cross-domain) require FQDNs to succeed */
510a53f50b9Schristos 
511a53f50b9Schristos #ifdef HAVE_TRANSPORT_TYPE_TLI
512a53f50b9Schristos   if (gopt.flags & CFM_FULLY_QUALIFIED_HOSTS) {
513a53f50b9Schristos     plog(XLOG_INFO, "Using NFS auth for FQHN \"%s\"", hostd);
514a53f50b9Schristos     nfs_auth = authsys_create(hostd, 0, 0, 1, &group_wheel);
515a53f50b9Schristos   } else {
516a53f50b9Schristos     nfs_auth = authsys_create_default();
517a53f50b9Schristos   }
518a53f50b9Schristos #else /* not HAVE_TRANSPORT_TYPE_TLI */
519a53f50b9Schristos   if (gopt.flags & CFM_FULLY_QUALIFIED_HOSTS) {
520a53f50b9Schristos     plog(XLOG_INFO, "Using NFS auth for FQHN \"%s\"", hostd);
521a53f50b9Schristos     nfs_auth = authunix_create(hostd, 0, 0, 1, &group_wheel);
522a53f50b9Schristos   } else {
523a53f50b9Schristos     nfs_auth = authunix_create_default();
524a53f50b9Schristos   }
525a53f50b9Schristos #endif /* not HAVE_TRANSPORT_TYPE_TLI */
526a53f50b9Schristos 
527a53f50b9Schristos   if (!nfs_auth)
528a53f50b9Schristos     return ENOBUFS;
529a53f50b9Schristos 
530a53f50b9Schristos   return 0;
531a53f50b9Schristos }
532a53f50b9Schristos 
533a53f50b9Schristos 
534a53f50b9Schristos static int
call_mountd(fh_cache * fp,u_long proc,fwd_fun fun,wchan_t wchan)535a53f50b9Schristos call_mountd(fh_cache *fp, u_long proc, fwd_fun fun, wchan_t wchan)
536a53f50b9Schristos {
537a53f50b9Schristos   struct rpc_msg mnt_msg;
538a53f50b9Schristos   int len;
539a53f50b9Schristos   char iobuf[UDPMSGSIZE];
540a53f50b9Schristos   int error;
541a53f50b9Schristos   u_long mnt_version;
542a53f50b9Schristos 
543a53f50b9Schristos   if (!nfs_auth) {
544a53f50b9Schristos     error = make_nfs_auth();
545a53f50b9Schristos     if (error)
546a53f50b9Schristos       return error;
547a53f50b9Schristos   }
548a53f50b9Schristos 
549a53f50b9Schristos   if (fp->fh_sin.sin_port == 0) {
550a53f50b9Schristos     u_short mountd_port;
551a53f50b9Schristos     error = get_mountd_port(fp->fh_fs, &mountd_port, wchan);
552a53f50b9Schristos     if (error)
553a53f50b9Schristos       return error;
554a53f50b9Schristos     fp->fh_sin.sin_port = mountd_port;
555*8bae5d40Schristos     dlog("%s: New %d mountd port", __func__, fp->fh_sin.sin_port);
556*8bae5d40Schristos   } else
557*8bae5d40Schristos     dlog("%s: Already had %d mountd port", __func__, fp->fh_sin.sin_port);
558a53f50b9Schristos 
559a53f50b9Schristos   /* find the right version of the mount protocol */
560a53f50b9Schristos #ifdef HAVE_FS_NFS3
561a53f50b9Schristos   if (fp->fh_nfs_version == NFS_VERSION3)
562a53f50b9Schristos     mnt_version = AM_MOUNTVERS3;
563a53f50b9Schristos   else
564a53f50b9Schristos #endif /* HAVE_FS_NFS3 */
565a53f50b9Schristos     mnt_version = MOUNTVERS;
566a53f50b9Schristos   plog(XLOG_INFO, "call_mountd: NFS version %d, mount version %d",
567a53f50b9Schristos        (int) fp->fh_nfs_version, (int) mnt_version);
568a53f50b9Schristos 
569a53f50b9Schristos   rpc_msg_init(&mnt_msg, MOUNTPROG, mnt_version, MOUNTPROC_NULL);
570a53f50b9Schristos   len = make_rpc_packet(iobuf,
571a53f50b9Schristos 			sizeof(iobuf),
572a53f50b9Schristos 			proc,
573a53f50b9Schristos 			&mnt_msg,
574a53f50b9Schristos 			(voidp) &fp->fh_path,
575a53f50b9Schristos 			(XDRPROC_T_TYPE) xdr_nfspath,
576a53f50b9Schristos 			nfs_auth);
577a53f50b9Schristos 
578a53f50b9Schristos   if (len > 0) {
579a53f50b9Schristos     error = fwd_packet(MK_RPC_XID(RPC_XID_MOUNTD, fp->fh_id),
580a53f50b9Schristos 		       iobuf,
581a53f50b9Schristos 		       len,
582a53f50b9Schristos 		       &fp->fh_sin,
583a53f50b9Schristos 		       &fp->fh_sin,
584a53f50b9Schristos 		       (opaque_t) ((long) fp->fh_id), /* cast to long needed for 64-bit archs */
585a53f50b9Schristos 		       fun);
586a53f50b9Schristos   } else {
587a53f50b9Schristos     error = -len;
588a53f50b9Schristos   }
589a53f50b9Schristos 
590a53f50b9Schristos   /*
591a53f50b9Schristos    * It may be the case that we're sending to the wrong MOUNTD port.  This
592a53f50b9Schristos    * occurs if mountd is restarted on the server after the port has been
593a53f50b9Schristos    * looked up and stored in the filehandle cache somewhere.  The correct
594a53f50b9Schristos    * solution, if we're going to cache port numbers is to catch the ICMP
595a53f50b9Schristos    * port unreachable reply from the server and cause the portmap request
596a53f50b9Schristos    * to be redone.  The quick solution here is to invalidate the MOUNTD
597a53f50b9Schristos    * port.
598a53f50b9Schristos    */
599a53f50b9Schristos   fp->fh_sin.sin_port = 0;
600a53f50b9Schristos 
601a53f50b9Schristos   return error;
602a53f50b9Schristos }
603a53f50b9Schristos 
604a53f50b9Schristos 
605a53f50b9Schristos static int
webnfs_lookup(fh_cache * fp,fwd_fun fun,wchan_t wchan)606a53f50b9Schristos webnfs_lookup(fh_cache *fp, fwd_fun fun, wchan_t wchan)
607a53f50b9Schristos {
608a53f50b9Schristos   struct rpc_msg wnfs_msg;
609a53f50b9Schristos   int len;
610a53f50b9Schristos   char iobuf[UDPMSGSIZE];
611a53f50b9Schristos   int error;
612a53f50b9Schristos   u_long proc;
613a53f50b9Schristos   XDRPROC_T_TYPE xdr_fn;
614a53f50b9Schristos   voidp argp;
615a53f50b9Schristos   nfsdiropargs args;
616a53f50b9Schristos #ifdef HAVE_FS_NFS3
617a53f50b9Schristos   am_LOOKUP3args args3;
618a53f50b9Schristos #endif /* HAVE_FS_NFS3 */
619a53f50b9Schristos   char *wnfs_path;
620a53f50b9Schristos   size_t l;
621a53f50b9Schristos 
622a53f50b9Schristos   if (!nfs_auth) {
623a53f50b9Schristos     error = make_nfs_auth();
624a53f50b9Schristos     if (error)
625a53f50b9Schristos       return error;
626a53f50b9Schristos   }
627a53f50b9Schristos 
628a53f50b9Schristos   if (fp->fh_sin.sin_port == 0) {
629a53f50b9Schristos     /* FIXME: wrong, don't discard sin_port in the first place for WebNFS. */
630a53f50b9Schristos     plog(XLOG_WARNING, "webnfs_lookup: port == 0 for nfs on %s, fixed",
631a53f50b9Schristos 	 fp->fh_fs->fs_host);
632a53f50b9Schristos     fp->fh_sin.sin_port = htons(NFS_PORT);
633a53f50b9Schristos   }
634a53f50b9Schristos 
635a53f50b9Schristos   /*
636a53f50b9Schristos    * Use native path like the rest of amd (cf. RFC 2054, 6.1).
637a53f50b9Schristos    */
638a53f50b9Schristos   l = strlen(fp->fh_path) + 2;
639a53f50b9Schristos   wnfs_path = (char *) xmalloc(l);
640a53f50b9Schristos   wnfs_path[0] = 0x80;
641a53f50b9Schristos   xstrlcpy(wnfs_path + 1, fp->fh_path, l - 1);
642a53f50b9Schristos 
643a53f50b9Schristos   /* find the right program and lookup procedure */
644a53f50b9Schristos #ifdef HAVE_FS_NFS3
645a53f50b9Schristos   if (fp->fh_nfs_version == NFS_VERSION3) {
646a53f50b9Schristos     proc = AM_NFSPROC3_LOOKUP;
647a53f50b9Schristos     xdr_fn = (XDRPROC_T_TYPE) xdr_am_LOOKUP3args;
648a53f50b9Schristos     argp = &args3;
649a53f50b9Schristos     /* WebNFS public file handle */
650a53f50b9Schristos     args3.what.dir.am_fh3_length = 0;
651a53f50b9Schristos     args3.what.name = wnfs_path;
652a53f50b9Schristos   } else {
653a53f50b9Schristos #endif /* HAVE_FS_NFS3 */
654a53f50b9Schristos     proc = NFSPROC_LOOKUP;
655a53f50b9Schristos     xdr_fn = (XDRPROC_T_TYPE) xdr_diropargs;
656a53f50b9Schristos     argp = &args;
657a53f50b9Schristos     /* WebNFS public file handle */
658a53f50b9Schristos     memset(&args.da_fhandle, 0, NFS_FHSIZE);
659a53f50b9Schristos     args.da_name = wnfs_path;
660a53f50b9Schristos #ifdef HAVE_FS_NFS3
661a53f50b9Schristos   }
662a53f50b9Schristos #endif /* HAVE_FS_NFS3 */
663a53f50b9Schristos 
664a53f50b9Schristos   plog(XLOG_INFO, "webnfs_lookup: NFS version %d", (int) fp->fh_nfs_version);
665a53f50b9Schristos 
666a53f50b9Schristos   rpc_msg_init(&wnfs_msg, NFS_PROGRAM, fp->fh_nfs_version, proc);
667a53f50b9Schristos   len = make_rpc_packet(iobuf,
668a53f50b9Schristos 			sizeof(iobuf),
669a53f50b9Schristos 			proc,
670a53f50b9Schristos 			&wnfs_msg,
671a53f50b9Schristos 			argp,
672a53f50b9Schristos 			(XDRPROC_T_TYPE) xdr_fn,
673a53f50b9Schristos 			nfs_auth);
674a53f50b9Schristos 
675a53f50b9Schristos   if (len > 0) {
676a53f50b9Schristos     error = fwd_packet(MK_RPC_XID(RPC_XID_WEBNFS, fp->fh_id),
677a53f50b9Schristos 		       iobuf,
678a53f50b9Schristos 		       len,
679a53f50b9Schristos 		       &fp->fh_sin,
680a53f50b9Schristos 		       &fp->fh_sin,
681a53f50b9Schristos 		       (opaque_t) ((long) fp->fh_id), /* cast to long needed for 64-bit archs */
682a53f50b9Schristos 		       fun);
683a53f50b9Schristos   } else {
684a53f50b9Schristos     error = -len;
685a53f50b9Schristos   }
686a53f50b9Schristos 
687a53f50b9Schristos   XFREE(wnfs_path);
688a53f50b9Schristos   return error;
689a53f50b9Schristos }
690a53f50b9Schristos 
691a53f50b9Schristos 
692a53f50b9Schristos /*
693a53f50b9Schristos  * NFS needs the local filesystem, remote filesystem
694a53f50b9Schristos  * remote hostname.
695a53f50b9Schristos  * Local filesystem defaults to remote and vice-versa.
696a53f50b9Schristos  */
697a53f50b9Schristos static char *
nfs_match(am_opts * fo)698a53f50b9Schristos nfs_match(am_opts *fo)
699a53f50b9Schristos {
700a53f50b9Schristos   char *xmtab;
701a53f50b9Schristos   size_t l;
702a53f50b9Schristos 
703a53f50b9Schristos   if (fo->opt_fs && !fo->opt_rfs)
704a53f50b9Schristos     fo->opt_rfs = fo->opt_fs;
705a53f50b9Schristos   if (!fo->opt_rfs) {
706a53f50b9Schristos     plog(XLOG_USER, "nfs: no remote filesystem specified");
707a53f50b9Schristos     return NULL;
708a53f50b9Schristos   }
709a53f50b9Schristos   if (!fo->opt_rhost) {
710a53f50b9Schristos     plog(XLOG_USER, "nfs: no remote host specified");
711a53f50b9Schristos     return NULL;
712a53f50b9Schristos   }
713a53f50b9Schristos 
714a53f50b9Schristos   /*
715a53f50b9Schristos    * Determine magic cookie to put in mtab
716a53f50b9Schristos    */
717a53f50b9Schristos   l = strlen(fo->opt_rhost) + strlen(fo->opt_rfs) + 2;
718a53f50b9Schristos   xmtab = (char *) xmalloc(l);
719a53f50b9Schristos   xsnprintf(xmtab, l, "%s:%s", fo->opt_rhost, fo->opt_rfs);
720a53f50b9Schristos   dlog("NFS: mounting remote server \"%s\", remote fs \"%s\" on \"%s\"",
721a53f50b9Schristos        fo->opt_rhost, fo->opt_rfs, fo->opt_fs);
722a53f50b9Schristos 
723a53f50b9Schristos   return xmtab;
724a53f50b9Schristos }
725a53f50b9Schristos 
726a53f50b9Schristos 
727a53f50b9Schristos /*
728a53f50b9Schristos  * Initialize am structure for nfs
729a53f50b9Schristos  */
730a53f50b9Schristos static int
nfs_init(mntfs * mf)731a53f50b9Schristos nfs_init(mntfs *mf)
732a53f50b9Schristos {
733a53f50b9Schristos   int error;
734a53f50b9Schristos   am_nfs_handle_t fhs;
735a53f50b9Schristos   char *colon;
736a53f50b9Schristos 
737*8bae5d40Schristos #ifdef NO_FALLBACK
738*8bae5d40Schristos   /*
739*8bae5d40Schristos    * We don't need file handles for NFS version 4, but we can fall back to
740*8bae5d40Schristos    * version 3, so we allocate anyway
741*8bae5d40Schristos    */
742*8bae5d40Schristos #ifdef HAVE_FS_NFS4
743*8bae5d40Schristos   if (mf->mf_server->fs_version == NFS_VERSION4)
744*8bae5d40Schristos     return 0;
745*8bae5d40Schristos #endif /* HAVE_FS_NFS4 */
746*8bae5d40Schristos #endif /* NO_FALLBACK */
747*8bae5d40Schristos 
748a53f50b9Schristos   if (mf->mf_private) {
749a53f50b9Schristos     if (mf->mf_flags & MFF_NFS_SCALEDOWN) {
750a53f50b9Schristos       fserver *fs;
751a53f50b9Schristos 
752a53f50b9Schristos       /* tell remote mountd that we're done with this filehandle */
753a53f50b9Schristos       mf->mf_ops->umounted(mf);
754a53f50b9Schristos 
755a53f50b9Schristos       mf->mf_prfree(mf->mf_private);
756*8bae5d40Schristos       mf->mf_private = NULL;
757*8bae5d40Schristos       mf->mf_prfree = NULL;
758*8bae5d40Schristos 
759a53f50b9Schristos       fs = mf->mf_ops->ffserver(mf);
760a53f50b9Schristos       free_srvr(mf->mf_server);
761a53f50b9Schristos       mf->mf_server = fs;
762a53f50b9Schristos     } else
763a53f50b9Schristos       return 0;
764a53f50b9Schristos   }
765a53f50b9Schristos 
766a53f50b9Schristos   colon = strchr(mf->mf_info, ':');
767a53f50b9Schristos   if (colon == 0)
768a53f50b9Schristos     return ENOENT;
769a53f50b9Schristos 
770a53f50b9Schristos   error = prime_nfs_fhandle_cache(colon + 1, mf->mf_server, &fhs, mf);
771a53f50b9Schristos   if (!error) {
772a53f50b9Schristos     mf->mf_private = (opaque_t) ALLOC(am_nfs_handle_t);
773a53f50b9Schristos     mf->mf_prfree = (void (*)(opaque_t)) free;
774a53f50b9Schristos     memmove(mf->mf_private, (voidp) &fhs, sizeof(fhs));
775a53f50b9Schristos   }
776a53f50b9Schristos   return error;
777a53f50b9Schristos }
778a53f50b9Schristos 
779a53f50b9Schristos 
780a53f50b9Schristos int
mount_nfs_fh(am_nfs_handle_t * fhp,char * mntdir,char * fs_name,mntfs * mf)781a53f50b9Schristos mount_nfs_fh(am_nfs_handle_t *fhp, char *mntdir, char *fs_name, mntfs *mf)
782a53f50b9Schristos {
783a53f50b9Schristos   MTYPE_TYPE type;
784a53f50b9Schristos   char *colon;
785a53f50b9Schristos   char *xopts=NULL, transp_timeo_opts[40], transp_retrans_opts[40];
786a53f50b9Schristos   char host[MAXHOSTNAMELEN + MAXPATHLEN + 2];
787a53f50b9Schristos   fserver *fs = mf->mf_server;
788a53f50b9Schristos   u_long nfs_version = fs->fs_version;
789a53f50b9Schristos   char *nfs_proto = fs->fs_proto; /* "tcp" or "udp" */
790a53f50b9Schristos   int on_autofs = mf->mf_flags & MFF_ON_AUTOFS;
791a53f50b9Schristos   int error;
792a53f50b9Schristos   int genflags;
793a53f50b9Schristos   int retry;
794a53f50b9Schristos   int proto = AMU_TYPE_NONE;
795a53f50b9Schristos   mntent_t mnt;
796*8bae5d40Schristos   void *argsp;
797a53f50b9Schristos   nfs_args_t nfs_args;
798*8bae5d40Schristos #ifdef HAVE_FS_NFS4
799*8bae5d40Schristos   nfs4_args_t nfs4_args;
800*8bae5d40Schristos #endif /* HAVE_FS_NFS4 */
801a53f50b9Schristos 
802a53f50b9Schristos   /*
803a53f50b9Schristos    * Extract HOST name to give to kernel.
804a53f50b9Schristos    * Some systems like osf1/aix3/bsd44 variants may need old code
805a53f50b9Schristos    * for NFS_ARGS_NEEDS_PATH.
806a53f50b9Schristos    */
807a53f50b9Schristos   if (!(colon = strchr(fs_name, ':')))
808a53f50b9Schristos     return ENOENT;
809a53f50b9Schristos #ifdef MOUNT_TABLE_ON_FILE
810a53f50b9Schristos   *colon = '\0';
811a53f50b9Schristos #endif /* MOUNT_TABLE_ON_FILE */
812a53f50b9Schristos   xstrlcpy(host, fs_name, sizeof(host));
813a53f50b9Schristos #ifdef MOUNT_TABLE_ON_FILE
814a53f50b9Schristos   *colon = ':';
815a53f50b9Schristos #endif /* MOUNT_TABLE_ON_FILE */
816a53f50b9Schristos #ifdef MAXHOSTNAMELEN
817a53f50b9Schristos   /* most kernels have a name length restriction */
818a53f50b9Schristos   if (strlen(host) >= MAXHOSTNAMELEN)
819a53f50b9Schristos     xstrlcpy(host + MAXHOSTNAMELEN - 3, "..",
820a53f50b9Schristos 	     sizeof(host) - MAXHOSTNAMELEN + 3);
821a53f50b9Schristos #endif /* MAXHOSTNAMELEN */
822a53f50b9Schristos 
823a53f50b9Schristos   /*
824a53f50b9Schristos    * Create option=VAL for udp/tcp specific timeouts and retrans values, but
825a53f50b9Schristos    * only if these options were specified.
826a53f50b9Schristos    */
827a53f50b9Schristos 
828a53f50b9Schristos   transp_timeo_opts[0] = transp_retrans_opts[0] = '\0';	/* initialize */
829a53f50b9Schristos   if (STREQ(nfs_proto, "udp"))
830a53f50b9Schristos     proto = AMU_TYPE_UDP;
831a53f50b9Schristos   else if (STREQ(nfs_proto, "tcp"))
832a53f50b9Schristos     proto = AMU_TYPE_TCP;
833a53f50b9Schristos   if (proto != AMU_TYPE_NONE) {
834a53f50b9Schristos     if (gopt.amfs_auto_timeo[proto] > 0)
835a53f50b9Schristos       xsnprintf(transp_timeo_opts, sizeof(transp_timeo_opts), "%s=%d,",
836a53f50b9Schristos 		MNTTAB_OPT_TIMEO, gopt.amfs_auto_timeo[proto]);
837a53f50b9Schristos     if (gopt.amfs_auto_retrans[proto] > 0)
838a53f50b9Schristos       xsnprintf(transp_retrans_opts, sizeof(transp_retrans_opts), "%s=%d,",
839a53f50b9Schristos 		MNTTAB_OPT_RETRANS, gopt.amfs_auto_retrans[proto]);
840a53f50b9Schristos   }
841a53f50b9Schristos 
842a53f50b9Schristos   if (mf->mf_remopts && *mf->mf_remopts &&
843a53f50b9Schristos       !islocalnet(fs->fs_ip->sin_addr.s_addr)) {
844a53f50b9Schristos     plog(XLOG_INFO, "Using remopts=\"%s\"", mf->mf_remopts);
845a53f50b9Schristos     /* use transp_opts first, so map-specific opts will override */
846a53f50b9Schristos     xopts = str3cat(xopts, transp_timeo_opts, transp_retrans_opts, mf->mf_remopts);
847a53f50b9Schristos   } else {
848a53f50b9Schristos     /* use transp_opts first, so map-specific opts will override */
849a53f50b9Schristos     xopts = str3cat(xopts, transp_timeo_opts, transp_retrans_opts, mf->mf_mopts);
850a53f50b9Schristos   }
851a53f50b9Schristos 
852a53f50b9Schristos   memset((voidp) &mnt, 0, sizeof(mnt));
853a53f50b9Schristos   mnt.mnt_dir = mntdir;
854a53f50b9Schristos   mnt.mnt_fsname = fs_name;
855a53f50b9Schristos   mnt.mnt_opts = xopts;
856a53f50b9Schristos 
857a53f50b9Schristos   /*
858a53f50b9Schristos    * Set mount types accordingly
859a53f50b9Schristos    */
860*8bae5d40Schristos #ifdef HAVE_FS_NFS3
861a53f50b9Schristos   if (nfs_version == NFS_VERSION3) {
862a53f50b9Schristos     type = MOUNT_TYPE_NFS3;
863a53f50b9Schristos     /*
864a53f50b9Schristos      * Systems that include the mount table "vers" option generally do not
865a53f50b9Schristos      * set the mnttab entry to "nfs3", but to "nfs" and then they set
866a53f50b9Schristos      * "vers=3".  Setting it to "nfs3" works, but it may break some things
867a53f50b9Schristos      * like "df -t nfs" and the "quota" program (esp. on Solaris and Irix).
868a53f50b9Schristos      * So on those systems, set it to "nfs".
869a53f50b9Schristos      * Note: MNTTAB_OPT_VERS is always set for NFS3 (see am_compat.h).
870a53f50b9Schristos      */
871*8bae5d40Schristos     argsp = &nfs_args;
872a53f50b9Schristos # if defined(MNTTAB_OPT_VERS) && defined(MOUNT_TABLE_ON_FILE)
873a53f50b9Schristos     mnt.mnt_type = MNTTAB_TYPE_NFS;
874a53f50b9Schristos # else /* defined(MNTTAB_OPT_VERS) && defined(MOUNT_TABLE_ON_FILE) */
875a53f50b9Schristos     mnt.mnt_type = MNTTAB_TYPE_NFS3;
876a53f50b9Schristos # endif /* defined(MNTTAB_OPT_VERS) && defined(MOUNT_TABLE_ON_FILE) */
877*8bae5d40Schristos # ifdef HAVE_FS_NFS4
878*8bae5d40Schristos   } else if (nfs_version == NFS_VERSION4) {
879*8bae5d40Schristos     argsp = &nfs4_args;
880*8bae5d40Schristos     type = MOUNT_TYPE_NFS4;
881*8bae5d40Schristos     mnt.mnt_type = MNTTAB_TYPE_NFS4;
882*8bae5d40Schristos # endif /* HAVE_FS_NFS4 */
883*8bae5d40Schristos   } else
884*8bae5d40Schristos #endif /* HAVE_FS_NFS3 */
885*8bae5d40Schristos   {
886*8bae5d40Schristos     argsp = &nfs_args;
887a53f50b9Schristos     type = MOUNT_TYPE_NFS;
888a53f50b9Schristos     mnt.mnt_type = MNTTAB_TYPE_NFS;
889a53f50b9Schristos   }
890a53f50b9Schristos   plog(XLOG_INFO, "mount_nfs_fh: NFS version %d", (int) nfs_version);
891a53f50b9Schristos   plog(XLOG_INFO, "mount_nfs_fh: using NFS transport %s", nfs_proto);
892a53f50b9Schristos 
893a53f50b9Schristos   retry = hasmntval(&mnt, MNTTAB_OPT_RETRY);
894a53f50b9Schristos   if (retry <= 0)
895a53f50b9Schristos     retry = 1;			/* XXX */
896a53f50b9Schristos 
897a53f50b9Schristos   genflags = compute_mount_flags(&mnt);
898a53f50b9Schristos #ifdef HAVE_FS_AUTOFS
899a53f50b9Schristos   if (on_autofs)
900a53f50b9Schristos     genflags |= autofs_compute_mount_flags(&mnt);
901a53f50b9Schristos #endif /* HAVE_FS_AUTOFS */
902a53f50b9Schristos 
903a53f50b9Schristos    /* setup the many fields and flags within nfs_args */
904*8bae5d40Schristos    compute_nfs_args(argsp,
905a53f50b9Schristos 		    &mnt,
906a53f50b9Schristos 		    genflags,
907a53f50b9Schristos 		    NULL,	/* struct netconfig *nfsncp */
908a53f50b9Schristos 		    fs->fs_ip,
909a53f50b9Schristos 		    nfs_version,
910a53f50b9Schristos 		    nfs_proto,
911a53f50b9Schristos 		    fhp,
912a53f50b9Schristos 		    host,
913a53f50b9Schristos 		    fs_name);
914a53f50b9Schristos 
915a53f50b9Schristos   /* finally call the mounting function */
916a53f50b9Schristos   if (amuDebug(D_TRACE)) {
917*8bae5d40Schristos     print_nfs_args(argsp, nfs_version);
918a53f50b9Schristos     plog(XLOG_DEBUG, "Generic mount flags 0x%x used for NFS mount", genflags);
919a53f50b9Schristos   }
920*8bae5d40Schristos   error = mount_fs(&mnt, genflags, argsp, retry, type,
921a53f50b9Schristos 		   nfs_version, nfs_proto, mnttab_file_name, on_autofs);
922*8bae5d40Schristos   XFREE(mnt.mnt_opts);
923*8bae5d40Schristos   discard_nfs_args(argsp, nfs_version);
924a53f50b9Schristos 
925*8bae5d40Schristos #ifdef HAVE_FS_NFS4
926*8bae5d40Schristos # ifndef NO_FALLBACK
927*8bae5d40Schristos   /*
928*8bae5d40Schristos    * If we are using a v4 file handle, we try a v3 if we get back:
929*8bae5d40Schristos    * 	ENOENT: NFS v4 has a different export list than v3
930*8bae5d40Schristos    * 	EPERM: Kernels <= 2.6.18 return that, instead of ENOENT
931*8bae5d40Schristos    */
932*8bae5d40Schristos   if ((error == ENOENT || error == EPERM) && nfs_version == NFS_VERSION4) {
933*8bae5d40Schristos     plog(XLOG_DEBUG, "Could not find NFS 4 mount, trying again with NFS 3");
934*8bae5d40Schristos     fs->fs_version = NFS_VERSION3;
935*8bae5d40Schristos     error = mount_nfs_fh(fhp, mntdir, fs_name, mf);
936*8bae5d40Schristos     if (error)
937*8bae5d40Schristos       fs->fs_version = NFS_VERSION4;
938*8bae5d40Schristos   }
939*8bae5d40Schristos # endif /* NO_FALLBACK */
940*8bae5d40Schristos #endif /* HAVE_FS_NFS4 */
941a53f50b9Schristos 
942a53f50b9Schristos   return error;
943a53f50b9Schristos }
944a53f50b9Schristos 
945a53f50b9Schristos 
946a53f50b9Schristos static int
nfs_mount(am_node * am,mntfs * mf)947a53f50b9Schristos nfs_mount(am_node *am, mntfs *mf)
948a53f50b9Schristos {
949a53f50b9Schristos   int error = 0;
950a53f50b9Schristos   mntent_t mnt;
951a53f50b9Schristos 
952*8bae5d40Schristos   if (!mf->mf_private && mf->mf_server->fs_version != 4) {
953a53f50b9Schristos     plog(XLOG_ERROR, "Missing filehandle for %s", mf->mf_info);
954a53f50b9Schristos     return EINVAL;
955a53f50b9Schristos   }
956a53f50b9Schristos 
957*8bae5d40Schristos   if (mf->mf_mopts == NULL) {
958*8bae5d40Schristos     plog(XLOG_ERROR, "Missing mount options for %s", mf->mf_info);
959*8bae5d40Schristos     return EINVAL;
960*8bae5d40Schristos   }
961*8bae5d40Schristos 
962a53f50b9Schristos   mnt.mnt_opts = mf->mf_mopts;
963a53f50b9Schristos   if (amu_hasmntopt(&mnt, "softlookup") ||
964a53f50b9Schristos       (amu_hasmntopt(&mnt, "soft") && !amu_hasmntopt(&mnt, "nosoftlookup")))
965a53f50b9Schristos     am->am_flags |= AMF_SOFTLOOKUP;
966a53f50b9Schristos 
967a53f50b9Schristos   error = mount_nfs_fh((am_nfs_handle_t *) mf->mf_private,
968a53f50b9Schristos 		       mf->mf_mount,
969a53f50b9Schristos 		       mf->mf_info,
970a53f50b9Schristos 		       mf);
971a53f50b9Schristos 
972a53f50b9Schristos   if (error) {
973a53f50b9Schristos     errno = error;
974a53f50b9Schristos     dlog("mount_nfs: %m");
975a53f50b9Schristos   }
976a53f50b9Schristos 
977a53f50b9Schristos   return error;
978a53f50b9Schristos }
979a53f50b9Schristos 
980a53f50b9Schristos 
981a53f50b9Schristos static int
nfs_umount(am_node * am,mntfs * mf)982a53f50b9Schristos nfs_umount(am_node *am, mntfs *mf)
983a53f50b9Schristos {
984a53f50b9Schristos   int unmount_flags, new_unmount_flags, error;
985a53f50b9Schristos 
986*8bae5d40Schristos   dlog("attempting nfs umount");
987a53f50b9Schristos   unmount_flags = (mf->mf_flags & MFF_ON_AUTOFS) ? AMU_UMOUNT_AUTOFS : 0;
988a53f50b9Schristos   error = UMOUNT_FS(mf->mf_mount, mnttab_file_name, unmount_flags);
989a53f50b9Schristos 
990a53f50b9Schristos #if defined(HAVE_UMOUNT2) && (defined(MNT2_GEN_OPT_FORCE) || defined(MNT2_GEN_OPT_DETACH))
991a53f50b9Schristos   /*
992a53f50b9Schristos    * If the attempt to unmount failed with EBUSY, and this fserver was
993a53f50b9Schristos    * marked for forced unmounts, then use forced/lazy unmounts.
994a53f50b9Schristos    */
995a53f50b9Schristos   if (error == EBUSY &&
996a53f50b9Schristos       gopt.flags & CFM_FORCED_UNMOUNTS &&
997a53f50b9Schristos       mf->mf_server->fs_flags & FSF_FORCE_UNMOUNT) {
998a53f50b9Schristos     plog(XLOG_INFO, "EZK: nfs_umount: trying forced/lazy unmounts");
999a53f50b9Schristos     /*
1000a53f50b9Schristos      * XXX: turning off the FSF_FORCE_UNMOUNT may not be perfectly
1001a53f50b9Schristos      * incorrect.  Multiple nodes may need to be timed out and restarted for
1002a53f50b9Schristos      * a single hung fserver.
1003a53f50b9Schristos      */
1004a53f50b9Schristos     mf->mf_server->fs_flags &= ~FSF_FORCE_UNMOUNT;
1005a53f50b9Schristos     new_unmount_flags = unmount_flags | AMU_UMOUNT_FORCE | AMU_UMOUNT_DETACH;
1006a53f50b9Schristos     error = UMOUNT_FS(mf->mf_mount, mnttab_file_name, new_unmount_flags);
1007a53f50b9Schristos   }
1008a53f50b9Schristos #endif /* HAVE_UMOUNT2 && (MNT2_GEN_OPT_FORCE || MNT2_GEN_OPT_DETACH) */
1009a53f50b9Schristos 
1010a53f50b9Schristos   /*
1011a53f50b9Schristos    * Here is some code to unmount 'restarted' file systems.
1012a53f50b9Schristos    * The restarted file systems are marked as 'nfs', not
1013a53f50b9Schristos    * 'host', so we only have the map information for the
1014a53f50b9Schristos    * the top-level mount.  The unmount will fail (EBUSY)
1015a53f50b9Schristos    * if there are anything else from the NFS server mounted
1016a53f50b9Schristos    * below the mount-point.  This code checks to see if there
1017a53f50b9Schristos    * is anything mounted with the same prefix as the
1018a53f50b9Schristos    * file system to be unmounted ("/a/b/c" when unmounting "/a/b").
1019a53f50b9Schristos    * If there is, and it is a 'restarted' file system, we unmount
1020a53f50b9Schristos    * it.
1021a53f50b9Schristos    * Added by Mike Mitchell, mcm@unx.sas.com, 09/08/93
1022a53f50b9Schristos    */
1023a53f50b9Schristos   if (error == EBUSY) {
1024a53f50b9Schristos     mntfs *new_mf;
1025a53f50b9Schristos     int len = strlen(mf->mf_mount);
1026a53f50b9Schristos     int didsome = 0;
1027a53f50b9Schristos 
1028a53f50b9Schristos     ITER(new_mf, mntfs, &mfhead) {
1029a53f50b9Schristos       if (new_mf->mf_ops != mf->mf_ops ||
1030a53f50b9Schristos 	  new_mf->mf_refc > 1 ||
1031a53f50b9Schristos 	  mf == new_mf ||
1032a53f50b9Schristos 	  ((new_mf->mf_flags & (MFF_MOUNTED | MFF_UNMOUNTING | MFF_RESTART)) == (MFF_MOUNTED | MFF_RESTART)))
1033a53f50b9Schristos 	continue;
1034a53f50b9Schristos 
1035a53f50b9Schristos       if (NSTREQ(mf->mf_mount, new_mf->mf_mount, len) &&
1036a53f50b9Schristos 	  new_mf->mf_mount[len] == '/') {
1037a53f50b9Schristos 	new_unmount_flags =
1038a53f50b9Schristos 	  (new_mf->mf_flags & MFF_ON_AUTOFS) ? AMU_UMOUNT_AUTOFS : 0;
1039a53f50b9Schristos 	UMOUNT_FS(new_mf->mf_mount, mnttab_file_name, new_unmount_flags);
1040a53f50b9Schristos 	didsome = 1;
1041a53f50b9Schristos       }
1042a53f50b9Schristos     }
1043a53f50b9Schristos     if (didsome)
1044a53f50b9Schristos       error = UMOUNT_FS(mf->mf_mount, mnttab_file_name, unmount_flags);
1045a53f50b9Schristos   }
1046a53f50b9Schristos   if (error)
1047a53f50b9Schristos     return error;
1048a53f50b9Schristos 
1049a53f50b9Schristos   return 0;
1050a53f50b9Schristos }
1051a53f50b9Schristos 
1052a53f50b9Schristos 
1053a53f50b9Schristos static void
nfs_umounted(mntfs * mf)1054a53f50b9Schristos nfs_umounted(mntfs *mf)
1055a53f50b9Schristos {
1056a53f50b9Schristos   fserver *fs;
1057a53f50b9Schristos   char *colon, *path;
1058a53f50b9Schristos 
1059a53f50b9Schristos   if (mf->mf_error || mf->mf_refc > 1)
1060a53f50b9Schristos     return;
1061a53f50b9Schristos 
1062a53f50b9Schristos   /*
1063a53f50b9Schristos    * No need to inform mountd when WebNFS is in use.
1064a53f50b9Schristos    */
1065a53f50b9Schristos   if (mf->mf_flags & MFF_WEBNFS)
1066a53f50b9Schristos     return;
1067a53f50b9Schristos 
1068a53f50b9Schristos   /*
1069a53f50b9Schristos    * Call the mount daemon on the server to announce that we are not using
1070a53f50b9Schristos    * the fs any more.
1071a53f50b9Schristos    *
1072a53f50b9Schristos    * XXX: This is *wrong*.  The mountd should be called when the fhandle is
1073a53f50b9Schristos    * flushed from the cache, and a reference held to the cached entry while
1074a53f50b9Schristos    * the fs is mounted...
1075a53f50b9Schristos    */
1076a53f50b9Schristos   fs = mf->mf_server;
1077a53f50b9Schristos   colon = path = strchr(mf->mf_info, ':');
1078a53f50b9Schristos   if (fs && colon) {
1079a53f50b9Schristos     fh_cache f;
1080a53f50b9Schristos 
1081a53f50b9Schristos     dlog("calling mountd for %s", mf->mf_info);
1082a53f50b9Schristos     *path++ = '\0';
1083a53f50b9Schristos     f.fh_path = path;
1084a53f50b9Schristos     f.fh_sin = *fs->fs_ip;
1085a53f50b9Schristos     f.fh_sin.sin_port = (u_short) 0;
1086*8bae5d40Schristos     f.fh_nfs_version = SET_FH_VERSION(fs);
1087a53f50b9Schristos     f.fh_fs = fs;
1088a53f50b9Schristos     f.fh_id = 0;
1089a53f50b9Schristos     f.fh_error = 0;
1090a53f50b9Schristos     prime_nfs_fhandle_cache(colon + 1, mf->mf_server, (am_nfs_handle_t *) NULL, mf);
1091a53f50b9Schristos     call_mountd(&f, MOUNTPROC_UMNT, (fwd_fun *) NULL, (wchan_t) NULL);
1092a53f50b9Schristos     *colon = ':';
1093a53f50b9Schristos   }
1094a53f50b9Schristos }
1095