]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/amd/amd/ops_nfs.c
MFC r308493, r308619: Update amd from am-utils 6.1.5 to 6.2.
[FreeBSD/stable/10.git] / contrib / amd / amd / ops_nfs.c
1 /*
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.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Jan-Simon Pendry at Imperial College, London.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
22  *
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
33  * SUCH DAMAGE.
34  *
35  *
36  * File: am-utils/amd/ops_nfs.c
37  *
38  */
39
40 /*
41  * Network file system
42  */
43
44 #ifdef HAVE_CONFIG_H
45 # include <config.h>
46 #endif /* HAVE_CONFIG_H */
47 #include <am_defs.h>
48 #include <amd.h>
49
50 /*
51  * Convert from nfsstat to UN*X error code
52  */
53 #define unx_error(e)    ((int)(e))
54
55 /*
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.
59  */
60 #define FH_TTL                  (5 * 60) /* five minutes */
61 #define FH_TTL_ERROR            (30) /* 30 seconds */
62 #define FHID_ALLOC()            (++fh_id)
63
64 /*
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.
69  *
70  * The NFS server layer knows to flush this cache
71  * when a server goes down so avoiding stale handles.
72  *
73  * Each cache entry keeps a hard reference to
74  * the corresponding server.  This ensures that
75  * the server keepalive information is maintained.
76  *
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
80  * elsewhere.
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
85  * problems...
86  */
87 typedef struct fh_cache fh_cache;
88 struct 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 */
100 };
101
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;
111
112 /*
113  * clamp the filehandle version to 3, so that we can fail back to nfsv3
114  * since nfsv4 does not have file handles
115  */
116 #define SET_FH_VERSION(fs) \
117     (fs)->fs_version > NFS_VERSION3 ? NFS_VERSION3 : (fs)->fs_version;
118
119 /* globals */
120 AUTH *nfs_auth;
121 qelem fh_head = {&fh_head, &fh_head};
122
123 /*
124  * Network file system operations
125  */
126 am_ops nfs_ops =
127 {
128   "nfs",
129   nfs_match,
130   nfs_init,
131   nfs_mount,
132   nfs_umount,
133   amfs_error_lookup_child,
134   amfs_error_mount_child,
135   amfs_error_readdir,
136   0,                            /* nfs_readlink */
137   0,                            /* nfs_mounted */
138   nfs_umounted,
139   find_nfs_srvr,
140   0,                            /* nfs_get_wchan */
141   FS_MKMNT | FS_BACKGROUND | FS_AMQINFO,        /* nfs_fs_flags */
142 #ifdef HAVE_FS_AUTOFS
143   AUTOFS_NFS_FS_FLAGS,
144 #endif /* HAVE_FS_AUTOFS */
145 };
146
147
148 static fh_cache *
149 find_nfs_fhandle_cache(opaque_t arg, int done)
150 {
151   fh_cache *fp, *fp2 = NULL;
152   int id = (long) arg;          /* for 64-bit archs */
153
154   ITER(fp, fh_cache, &fh_head) {
155     if (fp->fh_id == id) {
156       fp2 = fp;
157       break;
158     }
159   }
160
161   if (fp2) {
162     dlog("fh cache gives fp %#lx, fs %s", (unsigned long) fp2, fp2->fh_path);
163   } else {
164     dlog("fh cache search failed");
165   }
166
167   if (fp2 && !done) {
168     fp2->fh_error = ETIMEDOUT;
169     return 0;
170   }
171
172   return fp2;
173 }
174
175
176 /*
177  * Called when a filehandle appears via the mount protocol
178  */
179 static void
180 got_nfs_fh_mount(voidp pkt, int len, struct sockaddr_in *sa, struct sockaddr_in *ia, opaque_t arg, int done)
181 {
182   fh_cache *fp;
183   struct fhstatus res;
184 #ifdef HAVE_FS_NFS3
185   struct am_mountres3 res3;
186 #endif /* HAVE_FS_NFS3 */
187
188   fp = find_nfs_fhandle_cache(arg, done);
189   if (!fp)
190     return;
191
192   /*
193    * retrieve the correct RPC reply for the file handle, based on the
194    * NFS protocol version.
195    */
196 #ifdef HAVE_FS_NFS3
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);
207
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);
211   } else {
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);
218 #ifdef HAVE_FS_NFS3
219   }
220 #endif /* HAVE_FS_NFS3 */
221
222   if (!fp->fh_error) {
223     dlog("got filehandle for %s:%s", fp->fh_fs->fs_host, fp->fh_path);
224   } else {
225     plog(XLOG_USER, "filehandle denied for %s:%s", fp->fh_fs->fs_host, fp->fh_path);
226     /*
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.
230      */
231     fp->fh_error = EACCES;
232   }
233
234   /*
235    * Wakeup anything sleeping on this filehandle
236    */
237   if (fp->fh_wchan) {
238     dlog("Calling wakeup on %#lx", (unsigned long) fp->fh_wchan);
239     wakeup(fp->fh_wchan);
240   }
241 }
242
243
244 /*
245  * Called when a filehandle appears via WebNFS
246  */
247 static void
248 got_nfs_fh_webnfs(voidp pkt, int len, struct sockaddr_in *sa, struct sockaddr_in *ia, opaque_t arg, int done)
249 {
250   fh_cache *fp;
251   nfsdiropres res;
252 #ifdef HAVE_FS_NFS3
253   am_LOOKUP3res res3;
254 #endif /* HAVE_FS_NFS3 */
255
256   fp = find_nfs_fhandle_cache(arg, done);
257   if (!fp)
258     return;
259
260   /*
261    * retrieve the correct RPC reply for the file handle, based on the
262    * NFS protocol version.
263    */
264 #ifdef HAVE_FS_NFS3
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);
275   } else {
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);
282 #ifdef HAVE_FS_NFS3
283   }
284 #endif /* HAVE_FS_NFS3 */
285
286   if (!fp->fh_error) {
287     dlog("got filehandle for %s:%s", fp->fh_fs->fs_host, fp->fh_path);
288   } else {
289     plog(XLOG_USER, "filehandle denied for %s:%s", fp->fh_fs->fs_host, fp->fh_path);
290     /*
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.
294      */
295     fp->fh_error = EACCES;
296   }
297
298   /*
299    * Wakeup anything sleeping on this filehandle
300    */
301   if (fp->fh_wchan) {
302     dlog("Calling wakeup on %#lx", (unsigned long) fp->fh_wchan);
303     wakeup(fp->fh_wchan);
304   }
305 }
306
307
308 void
309 flush_nfs_fhandle_cache(fserver *fs)
310 {
311   fh_cache *fp;
312
313   ITER(fp, fh_cache, &fh_head) {
314     if (fp->fh_fs == fs || fs == NULL) {
315       /*
316        * Only invalidate port info for non-WebNFS servers
317        */
318       if (!(fp->fh_fs->fs_flags & FSF_WEBNFS))
319         fp->fh_sin.sin_port = (u_short) 0;
320       fp->fh_error = -1;
321     }
322   }
323 }
324
325
326 static void
327 discard_fh(opaque_t arg)
328 {
329   fh_cache *fp = (fh_cache *) arg;
330
331   rem_que(&fp->fh_q);
332   if (fp->fh_fs) {
333     dlog("Discarding filehandle for %s:%s", fp->fh_fs->fs_host, fp->fh_path);
334     free_srvr(fp->fh_fs);
335   }
336   XFREE(fp->fh_path);
337   XFREE(fp);
338 }
339
340
341 /*
342  * Determine the file handle for a node
343  */
344 static int
345 prime_nfs_fhandle_cache(char *path, fserver *fs, am_nfs_handle_t *fhbuf, mntfs *mf)
346 {
347   fh_cache *fp, *fp_save = NULL;
348   int error;
349   int reuse_id = FALSE;
350
351   dlog("Searching cache for %s:%s", fs->fs_host, path);
352
353   /*
354    * First search the cache
355    */
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) {
361     case 0:
362       plog(XLOG_INFO, "prime_nfs_fhandle_cache: NFS version %d", (int) fp->fh_nfs_version);
363
364       error = fp->fh_error = fp->fh_status;
365
366       if (error == 0) {
367         if (mf->mf_flags & MFF_NFS_SCALEDOWN) {
368           fp_save = fp;
369           /* XXX: why reuse the ID? */
370           reuse_id = TRUE;
371           break;
372         }
373
374         if (fhbuf) {
375 #ifdef HAVE_FS_NFS3
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));
379           } else
380 #endif /* HAVE_FS_NFS3 */
381             {
382               memmove((voidp) &(fhbuf->v2), (voidp) &(fp->fh_nfs_handle.v2),
383                       sizeof(fp->fh_nfs_handle.v2));
384             }
385         }
386         if (fp->fh_cid)
387           untimeout(fp->fh_cid);
388         fp->fh_cid = timeout(FH_TTL, discard_fh, (opaque_t) fp);
389       } else if (error == EACCES) {
390         /*
391          * Now decode the file handle return code.
392          */
393         plog(XLOG_INFO, "Filehandle denied for \"%s:%s\"",
394              fs->fs_host, path);
395       } else {
396         errno = error;  /* XXX */
397         plog(XLOG_INFO, "Filehandle error for \"%s:%s\": %m",
398              fs->fs_host, path);
399       }
400
401       /*
402        * The error was returned from the remote mount daemon.
403        * Policy: this error will be cached for now...
404        */
405       return error;
406
407     case -1:
408       /*
409        * Still thinking about it, but we can re-use.
410        */
411       fp_save = fp;
412       reuse_id = TRUE;
413       break;
414
415     default:
416       /*
417        * Return the error.
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.
425        */
426       error = fp->fh_error;
427       fp->fh_error = -1;
428       return error;
429     }   /* end of switch statement */
430   } /* end of ITER loop */
431
432   /*
433    * Not in cache
434    */
435   if (fp_save) {
436     fp = fp_save;
437     /*
438      * Re-use existing slot
439      */
440     untimeout(fp->fh_cid);
441     free_srvr(fp->fh_fs);
442     XFREE(fp->fh_path);
443   } else {
444     fp = ALLOC(struct fh_cache);
445     memset((voidp) fp, 0, sizeof(struct fh_cache));
446     ins_que(&fp->fh_q, &fh_head);
447   }
448   if (!reuse_id)
449     fp->fh_id = FHID_ALLOC();
450   fp->fh_wchan = get_mntfs_wchan(mf);
451   fp->fh_error = -1;
452   fp->fh_cid = timeout(FH_TTL, discard_fh, (opaque_t) fp);
453
454   /*
455    * If fs->fs_ip is null, remote server is probably down.
456    */
457   if (!fs->fs_ip) {
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;
462     return error;
463   }
464
465   /*
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.
470    */
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);
476   }
477
478   fp->fh_fs = dup_srvr(fs);
479   fp->fh_path = xstrdup(path);
480
481   if (mf->mf_flags & MFF_WEBNFS)
482     error = webnfs_lookup(fp, got_nfs_fh_webnfs, get_mntfs_wchan(mf));
483   else
484     error = call_mountd(fp, MOUNTPROC_MNT, got_nfs_fh_mount, get_mntfs_wchan(mf));
485   if (error) {
486     /*
487      * Local error - cache for a short period
488      * just to prevent thrashing.
489      */
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;
494   } else {
495     error = fp->fh_error;
496   }
497
498   return error;
499 }
500
501
502 int
503 make_nfs_auth(void)
504 {
505   AUTH_CREATE_GIDLIST_TYPE group_wheel = 0;
506
507   /* Some NFS mounts (particularly cross-domain) require FQDNs to succeed */
508
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);
513   } else {
514     nfs_auth = authsys_create_default();
515   }
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);
520   } else {
521     nfs_auth = authunix_create_default();
522   }
523 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
524
525   if (!nfs_auth)
526     return ENOBUFS;
527
528   return 0;
529 }
530
531
532 static int
533 call_mountd(fh_cache *fp, u_long proc, fwd_fun fun, wchan_t wchan)
534 {
535   struct rpc_msg mnt_msg;
536   int len;
537   char iobuf[UDPMSGSIZE];
538   int error;
539   u_long mnt_version;
540
541   if (!nfs_auth) {
542     error = make_nfs_auth();
543     if (error)
544       return error;
545   }
546
547   if (fp->fh_sin.sin_port == 0) {
548     u_short mountd_port;
549     error = get_mountd_port(fp->fh_fs, &mountd_port, wchan);
550     if (error)
551       return error;
552     fp->fh_sin.sin_port = mountd_port;
553     dlog("%s: New %d mountd port", __func__, fp->fh_sin.sin_port);
554   } else
555     dlog("%s: Already had %d mountd port", __func__, fp->fh_sin.sin_port);
556
557   /* find the right version of the mount protocol */
558 #ifdef HAVE_FS_NFS3
559   if (fp->fh_nfs_version == NFS_VERSION3)
560     mnt_version = AM_MOUNTVERS3;
561   else
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);
566
567   rpc_msg_init(&mnt_msg, MOUNTPROG, mnt_version, MOUNTPROC_NULL);
568   len = make_rpc_packet(iobuf,
569                         sizeof(iobuf),
570                         proc,
571                         &mnt_msg,
572                         (voidp) &fp->fh_path,
573                         (XDRPROC_T_TYPE) xdr_nfspath,
574                         nfs_auth);
575
576   if (len > 0) {
577     error = fwd_packet(MK_RPC_XID(RPC_XID_MOUNTD, fp->fh_id),
578                        iobuf,
579                        len,
580                        &fp->fh_sin,
581                        &fp->fh_sin,
582                        (opaque_t) ((long) fp->fh_id), /* cast to long needed for 64-bit archs */
583                        fun);
584   } else {
585     error = -len;
586   }
587
588   /*
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
595    * port.
596    */
597   fp->fh_sin.sin_port = 0;
598
599   return error;
600 }
601
602
603 static int
604 webnfs_lookup(fh_cache *fp, fwd_fun fun, wchan_t wchan)
605 {
606   struct rpc_msg wnfs_msg;
607   int len;
608   char iobuf[UDPMSGSIZE];
609   int error;
610   u_long proc;
611   XDRPROC_T_TYPE xdr_fn;
612   voidp argp;
613   nfsdiropargs args;
614 #ifdef HAVE_FS_NFS3
615   am_LOOKUP3args args3;
616 #endif /* HAVE_FS_NFS3 */
617   char *wnfs_path;
618   size_t l;
619
620   if (!nfs_auth) {
621     error = make_nfs_auth();
622     if (error)
623       return error;
624   }
625
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",
629          fp->fh_fs->fs_host);
630     fp->fh_sin.sin_port = htons(NFS_PORT);
631   }
632
633   /*
634    * Use native path like the rest of amd (cf. RFC 2054, 6.1).
635    */
636   l = strlen(fp->fh_path) + 2;
637   wnfs_path = (char *) xmalloc(l);
638   wnfs_path[0] = 0x80;
639   xstrlcpy(wnfs_path + 1, fp->fh_path, l - 1);
640
641   /* find the right program and lookup procedure */
642 #ifdef HAVE_FS_NFS3
643   if (fp->fh_nfs_version == NFS_VERSION3) {
644     proc = AM_NFSPROC3_LOOKUP;
645     xdr_fn = (XDRPROC_T_TYPE) xdr_am_LOOKUP3args;
646     argp = &args3;
647     /* WebNFS public file handle */
648     args3.what.dir.am_fh3_length = 0;
649     args3.what.name = wnfs_path;
650   } else {
651 #endif /* HAVE_FS_NFS3 */
652     proc = NFSPROC_LOOKUP;
653     xdr_fn = (XDRPROC_T_TYPE) xdr_diropargs;
654     argp = &args;
655     /* WebNFS public file handle */
656     memset(&args.da_fhandle, 0, NFS_FHSIZE);
657     args.da_name = wnfs_path;
658 #ifdef HAVE_FS_NFS3
659   }
660 #endif /* HAVE_FS_NFS3 */
661
662   plog(XLOG_INFO, "webnfs_lookup: NFS version %d", (int) fp->fh_nfs_version);
663
664   rpc_msg_init(&wnfs_msg, NFS_PROGRAM, fp->fh_nfs_version, proc);
665   len = make_rpc_packet(iobuf,
666                         sizeof(iobuf),
667                         proc,
668                         &wnfs_msg,
669                         argp,
670                         (XDRPROC_T_TYPE) xdr_fn,
671                         nfs_auth);
672
673   if (len > 0) {
674     error = fwd_packet(MK_RPC_XID(RPC_XID_WEBNFS, fp->fh_id),
675                        iobuf,
676                        len,
677                        &fp->fh_sin,
678                        &fp->fh_sin,
679                        (opaque_t) ((long) fp->fh_id), /* cast to long needed for 64-bit archs */
680                        fun);
681   } else {
682     error = -len;
683   }
684
685   XFREE(wnfs_path);
686   return error;
687 }
688
689
690 /*
691  * NFS needs the local filesystem, remote filesystem
692  * remote hostname.
693  * Local filesystem defaults to remote and vice-versa.
694  */
695 static char *
696 nfs_match(am_opts *fo)
697 {
698   char *xmtab;
699   size_t l;
700
701   if (fo->opt_fs && !fo->opt_rfs)
702     fo->opt_rfs = fo->opt_fs;
703   if (!fo->opt_rfs) {
704     plog(XLOG_USER, "nfs: no remote filesystem specified");
705     return NULL;
706   }
707   if (!fo->opt_rhost) {
708     plog(XLOG_USER, "nfs: no remote host specified");
709     return NULL;
710   }
711
712   /*
713    * Determine magic cookie to put in mtab
714    */
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);
720
721   return xmtab;
722 }
723
724
725 /*
726  * Initialize am structure for nfs
727  */
728 static int
729 nfs_init(mntfs *mf)
730 {
731   int error;
732   am_nfs_handle_t fhs;
733   char *colon;
734
735 #ifdef NO_FALLBACK
736   /*
737    * We don't need file handles for NFS version 4, but we can fall back to
738    * version 3, so we allocate anyway
739    */
740 #ifdef HAVE_FS_NFS4
741   if (mf->mf_server->fs_version == NFS_VERSION4)
742     return 0;
743 #endif /* HAVE_FS_NFS4 */
744 #endif /* NO_FALLBACK */
745
746   if (mf->mf_private) {
747     if (mf->mf_flags & MFF_NFS_SCALEDOWN) {
748       fserver *fs;
749
750       /* tell remote mountd that we're done with this filehandle */
751       mf->mf_ops->umounted(mf);
752
753       mf->mf_prfree(mf->mf_private);
754       mf->mf_private = NULL;
755       mf->mf_prfree = NULL;
756
757       fs = mf->mf_ops->ffserver(mf);
758       free_srvr(mf->mf_server);
759       mf->mf_server = fs;
760     } else
761       return 0;
762   }
763
764   colon = strchr(mf->mf_info, ':');
765   if (colon == 0)
766     return ENOENT;
767
768   error = prime_nfs_fhandle_cache(colon + 1, mf->mf_server, &fhs, mf);
769   if (!error) {
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));
773   }
774   return error;
775 }
776
777
778 int
779 mount_nfs_fh(am_nfs_handle_t *fhp, char *mntdir, char *fs_name, mntfs *mf)
780 {
781   MTYPE_TYPE type;
782   char *colon;
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;
789   int error;
790   int genflags;
791   int retry;
792   int proto = AMU_TYPE_NONE;
793   mntent_t mnt;
794   void *argsp;
795   nfs_args_t nfs_args;
796 #ifdef HAVE_FS_NFS4
797   nfs4_args_t nfs4_args;
798 #endif /* HAVE_FS_NFS4 */
799
800   /*
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.
804    */
805   if (!(colon = strchr(fs_name, ':')))
806     return ENOENT;
807 #ifdef MOUNT_TABLE_ON_FILE
808   *colon = '\0';
809 #endif /* MOUNT_TABLE_ON_FILE */
810   xstrlcpy(host, fs_name, sizeof(host));
811 #ifdef MOUNT_TABLE_ON_FILE
812   *colon = ':';
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 */
820
821   /*
822    * Create option=VAL for udp/tcp specific timeouts and retrans values, but
823    * only if these options were specified.
824    */
825
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]);
838   }
839
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);
845   } else {
846     /* use transp_opts first, so map-specific opts will override */
847     xopts = str3cat(xopts, transp_timeo_opts, transp_retrans_opts, mf->mf_mopts);
848   }
849
850   memset((voidp) &mnt, 0, sizeof(mnt));
851   mnt.mnt_dir = mntdir;
852   mnt.mnt_fsname = fs_name;
853   mnt.mnt_opts = xopts;
854
855   /*
856    * Set mount types accordingly
857    */
858 #ifdef HAVE_FS_NFS3
859   if (nfs_version == NFS_VERSION3) {
860     type = MOUNT_TYPE_NFS3;
861     /*
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).
868      */
869     argsp = &nfs_args;
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) */
875 # ifdef HAVE_FS_NFS4
876   } else if (nfs_version == NFS_VERSION4) {
877     argsp = &nfs4_args;
878     type = MOUNT_TYPE_NFS4;
879     mnt.mnt_type = MNTTAB_TYPE_NFS4;
880 # endif /* HAVE_FS_NFS4 */
881   } else
882 #endif /* HAVE_FS_NFS3 */
883   {
884     argsp = &nfs_args;
885     type = MOUNT_TYPE_NFS;
886     mnt.mnt_type = MNTTAB_TYPE_NFS;
887   }
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);
890
891   retry = hasmntval(&mnt, MNTTAB_OPT_RETRY);
892   if (retry <= 0)
893     retry = 1;                  /* XXX */
894
895   genflags = compute_mount_flags(&mnt);
896 #ifdef HAVE_FS_AUTOFS
897   if (on_autofs)
898     genflags |= autofs_compute_mount_flags(&mnt);
899 #endif /* HAVE_FS_AUTOFS */
900
901    /* setup the many fields and flags within nfs_args */
902    compute_nfs_args(argsp,
903                     &mnt,
904                     genflags,
905                     NULL,       /* struct netconfig *nfsncp */
906                     fs->fs_ip,
907                     nfs_version,
908                     nfs_proto,
909                     fhp,
910                     host,
911                     fs_name);
912
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);
917   }
918   error = mount_fs(&mnt, genflags, argsp, retry, type,
919                    nfs_version, nfs_proto, mnttab_file_name, on_autofs);
920   XFREE(mnt.mnt_opts);
921   discard_nfs_args(argsp, nfs_version);
922
923 #ifdef HAVE_FS_NFS4
924 # ifndef NO_FALLBACK
925   /*
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
929    */
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);
934     if (error)
935       fs->fs_version = NFS_VERSION4;
936   }
937 # endif /* NO_FALLBACK */
938 #endif /* HAVE_FS_NFS4 */
939
940   return error;
941 }
942
943
944 static int
945 nfs_mount(am_node *am, mntfs *mf)
946 {
947   int error = 0;
948   mntent_t mnt;
949
950   if (!mf->mf_private && mf->mf_server->fs_version != 4) {
951     plog(XLOG_ERROR, "Missing filehandle for %s", mf->mf_info);
952     return EINVAL;
953   }
954
955   if (mf->mf_mopts == NULL) {
956     plog(XLOG_ERROR, "Missing mount options for %s", mf->mf_info);
957     return EINVAL;
958   }
959
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;
964
965   error = mount_nfs_fh((am_nfs_handle_t *) mf->mf_private,
966                        mf->mf_mount,
967                        mf->mf_info,
968                        mf);
969
970   if (error) {
971     errno = error;
972     dlog("mount_nfs: %m");
973   }
974
975   return error;
976 }
977
978
979 static int
980 nfs_umount(am_node *am, mntfs *mf)
981 {
982   int unmount_flags, new_unmount_flags, error;
983
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);
987
988 #if defined(HAVE_UMOUNT2) && (defined(MNT2_GEN_OPT_FORCE) || defined(MNT2_GEN_OPT_DETACH))
989   /*
990    * If the attempt to unmount failed with EBUSY, and this fserver was
991    * marked for forced unmounts, then use forced/lazy unmounts.
992    */
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");
997     /*
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.
1001      */
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);
1005   }
1006 #endif /* HAVE_UMOUNT2 && (MNT2_GEN_OPT_FORCE || MNT2_GEN_OPT_DETACH) */
1007
1008   /*
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
1018    * it.
1019    * Added by Mike Mitchell, mcm@unx.sas.com, 09/08/93
1020    */
1021   if (error == EBUSY) {
1022     mntfs *new_mf;
1023     int len = strlen(mf->mf_mount);
1024     int didsome = 0;
1025
1026     ITER(new_mf, mntfs, &mfhead) {
1027       if (new_mf->mf_ops != mf->mf_ops ||
1028           new_mf->mf_refc > 1 ||
1029           mf == new_mf ||
1030           ((new_mf->mf_flags & (MFF_MOUNTED | MFF_UNMOUNTING | MFF_RESTART)) == (MFF_MOUNTED | MFF_RESTART)))
1031         continue;
1032
1033       if (NSTREQ(mf->mf_mount, new_mf->mf_mount, len) &&
1034           new_mf->mf_mount[len] == '/') {
1035         new_unmount_flags =
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);
1038         didsome = 1;
1039       }
1040     }
1041     if (didsome)
1042       error = UMOUNT_FS(mf->mf_mount, mnttab_file_name, unmount_flags);
1043   }
1044   if (error)
1045     return error;
1046
1047   return 0;
1048 }
1049
1050
1051 static void
1052 nfs_umounted(mntfs *mf)
1053 {
1054   fserver *fs;
1055   char *colon, *path;
1056
1057   if (mf->mf_error || mf->mf_refc > 1)
1058     return;
1059
1060   /*
1061    * No need to inform mountd when WebNFS is in use.
1062    */
1063   if (mf->mf_flags & MFF_WEBNFS)
1064     return;
1065
1066   /*
1067    * Call the mount daemon on the server to announce that we are not using
1068    * the fs any more.
1069    *
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...
1073    */
1074   fs = mf->mf_server;
1075   colon = path = strchr(mf->mf_info, ':');
1076   if (fs && colon) {
1077     fh_cache f;
1078
1079     dlog("calling mountd for %s", mf->mf_info);
1080     *path++ = '\0';
1081     f.fh_path = path;
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);
1085     f.fh_fs = fs;
1086     f.fh_id = 0;
1087     f.fh_error = 0;
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);
1090     *colon = ':';
1091   }
1092 }