]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/nfsclient/nfs_krpc.c
MFV r268121:
[FreeBSD/FreeBSD.git] / sys / nfsclient / nfs_krpc.c
1 /*-
2  * Copyright (c) 1989, 1991, 1993, 1995
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Macklem at The University of Guelph.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *      @(#)nfs_socket.c        8.5 (Berkeley) 3/30/95
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 /*
39  * Socket operations for use by nfs
40  */
41
42 #include "opt_inet6.h"
43 #include "opt_kgssapi.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/limits.h>
49 #include <sys/lock.h>
50 #include <sys/malloc.h>
51 #include <sys/mbuf.h>
52 #include <sys/mount.h>
53 #include <sys/mutex.h>
54 #include <sys/proc.h>
55 #include <sys/signalvar.h>
56 #include <sys/syscallsubr.h>
57 #include <sys/sysctl.h>
58 #include <sys/syslog.h>
59 #include <sys/vnode.h>
60
61 #include <rpc/rpc.h>
62
63 #include <nfs/nfsproto.h>
64 #include <nfsclient/nfs.h>
65 #include <nfs/xdr_subs.h>
66 #include <nfsclient/nfsm_subs.h>
67 #include <nfsclient/nfsmount.h>
68 #include <nfsclient/nfsnode.h>
69
70 #ifdef KDTRACE_HOOKS
71 #include <sys/dtrace_bsd.h>
72
73 dtrace_nfsclient_nfs23_start_probe_func_t
74     dtrace_nfsclient_nfs23_start_probe;
75
76 dtrace_nfsclient_nfs23_done_probe_func_t
77     dtrace_nfsclient_nfs23_done_probe;
78
79 /*
80  * Registered probes by RPC type.
81  */
82 uint32_t        nfsclient_nfs2_start_probes[NFS_NPROCS];
83 uint32_t        nfsclient_nfs2_done_probes[NFS_NPROCS];
84
85 uint32_t        nfsclient_nfs3_start_probes[NFS_NPROCS];
86 uint32_t        nfsclient_nfs3_done_probes[NFS_NPROCS];
87 #endif
88
89 static int      nfs_bufpackets = 4;
90 static int      nfs_reconnects;
91 static int      nfs3_jukebox_delay = 10;
92 static int      nfs_skip_wcc_data_onerr = 1;
93 static int      fake_wchan;
94
95 SYSCTL_DECL(_vfs_oldnfs);
96
97 SYSCTL_INT(_vfs_oldnfs, OID_AUTO, bufpackets, CTLFLAG_RW, &nfs_bufpackets, 0,
98     "Buffer reservation size 2 < x < 64");
99 SYSCTL_INT(_vfs_oldnfs, OID_AUTO, reconnects, CTLFLAG_RD, &nfs_reconnects, 0,
100     "Number of times the nfs client has had to reconnect");
101 SYSCTL_INT(_vfs_oldnfs, OID_AUTO, nfs3_jukebox_delay, CTLFLAG_RW,
102     &nfs3_jukebox_delay, 0,
103     "Number of seconds to delay a retry after receiving EJUKEBOX");
104 SYSCTL_INT(_vfs_oldnfs, OID_AUTO, skip_wcc_data_onerr, CTLFLAG_RW,
105     &nfs_skip_wcc_data_onerr, 0,
106     "Disable weak cache consistency checking when server returns an error");
107
108 static void     nfs_down(struct nfsmount *, struct thread *, const char *,
109     int, int);
110 static void     nfs_up(struct nfsmount *, struct thread *, const char *,
111     int, int);
112 static int      nfs_msg(struct thread *, const char *, const char *, int);
113
114 extern int nfsv2_procid[];
115
116 struct nfs_cached_auth {
117         int             ca_refs; /* refcount, including 1 from the cache */
118         uid_t           ca_uid;  /* uid that corresponds to this auth */
119         AUTH            *ca_auth; /* RPC auth handle */
120 };
121
122 /*
123  * RTT estimator
124  */
125
126 static enum nfs_rto_timer_t nfs_proct[NFS_NPROCS] = {
127         NFS_DEFAULT_TIMER,      /* NULL */
128         NFS_GETATTR_TIMER,      /* GETATTR */
129         NFS_DEFAULT_TIMER,      /* SETATTR */
130         NFS_LOOKUP_TIMER,       /* LOOKUP */
131         NFS_GETATTR_TIMER,      /* ACCESS */
132         NFS_READ_TIMER,         /* READLINK */
133         NFS_READ_TIMER,         /* READ */
134         NFS_WRITE_TIMER,        /* WRITE */
135         NFS_DEFAULT_TIMER,      /* CREATE */
136         NFS_DEFAULT_TIMER,      /* MKDIR */
137         NFS_DEFAULT_TIMER,      /* SYMLINK */
138         NFS_DEFAULT_TIMER,      /* MKNOD */
139         NFS_DEFAULT_TIMER,      /* REMOVE */
140         NFS_DEFAULT_TIMER,      /* RMDIR */
141         NFS_DEFAULT_TIMER,      /* RENAME */
142         NFS_DEFAULT_TIMER,      /* LINK */
143         NFS_READ_TIMER,         /* READDIR */
144         NFS_READ_TIMER,         /* READDIRPLUS */
145         NFS_DEFAULT_TIMER,      /* FSSTAT */
146         NFS_DEFAULT_TIMER,      /* FSINFO */
147         NFS_DEFAULT_TIMER,      /* PATHCONF */
148         NFS_DEFAULT_TIMER,      /* COMMIT */
149         NFS_DEFAULT_TIMER,      /* NOOP */
150 };
151
152 /*
153  * Choose the correct RTT timer for this NFS procedure.
154  */
155 static inline enum nfs_rto_timer_t
156 nfs_rto_timer(u_int32_t procnum)
157 {
158
159         return (nfs_proct[procnum]);
160 }
161
162 /*
163  * Initialize the RTT estimator state for a new mount point.
164  */
165 static void
166 nfs_init_rtt(struct nfsmount *nmp)
167 {
168         int i;
169
170         for (i = 0; i < NFS_MAX_TIMER; i++) {
171                 nmp->nm_timers[i].rt_srtt = hz;
172                 nmp->nm_timers[i].rt_deviate = 0;
173                 nmp->nm_timers[i].rt_rtxcur = hz;
174         }
175 }
176
177 /*
178  * Initialize sockets and congestion for a new NFS connection.
179  * We do not free the sockaddr if error.
180  */
181 int
182 nfs_connect(struct nfsmount *nmp)
183 {
184         int rcvreserve, sndreserve;
185         int pktscale;
186         struct sockaddr *saddr;
187         struct ucred *origcred;
188         struct thread *td = curthread;
189         CLIENT *client;
190         struct netconfig *nconf;
191         rpcvers_t vers;
192         int one = 1, retries;
193         struct timeval timo;
194
195         /*
196          * We need to establish the socket using the credentials of
197          * the mountpoint.  Some parts of this process (such as
198          * sobind() and soconnect()) will use the curent thread's
199          * credential instead of the socket credential.  To work
200          * around this, temporarily change the current thread's
201          * credential to that of the mountpoint.
202          *
203          * XXX: It would be better to explicitly pass the correct
204          * credential to sobind() and soconnect().
205          */
206         origcred = td->td_ucred;
207         td->td_ucred = nmp->nm_mountp->mnt_cred;
208         saddr = nmp->nm_nam;
209
210         vers = NFS_VER2;
211         if (nmp->nm_flag & NFSMNT_NFSV3)
212                 vers = NFS_VER3;
213         else if (nmp->nm_flag & NFSMNT_NFSV4)
214                 vers = NFS_VER4;
215         if (saddr->sa_family == AF_INET)
216                 if (nmp->nm_sotype == SOCK_DGRAM)
217                         nconf = getnetconfigent("udp");
218                 else
219                         nconf = getnetconfigent("tcp");
220         else
221                 if (nmp->nm_sotype == SOCK_DGRAM)
222                         nconf = getnetconfigent("udp6");
223                 else
224                         nconf = getnetconfigent("tcp6");
225
226         /*
227          * Get buffer reservation size from sysctl, but impose reasonable
228          * limits.
229          */
230         pktscale = nfs_bufpackets;
231         if (pktscale < 2)
232                 pktscale = 2;
233         if (pktscale > 64)
234                 pktscale = 64;
235         mtx_lock(&nmp->nm_mtx);
236         if (nmp->nm_sotype == SOCK_DGRAM) {
237                 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
238                 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
239                     NFS_MAXPKTHDR) * pktscale;
240         } else if (nmp->nm_sotype == SOCK_SEQPACKET) {
241                 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
242                 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
243                     NFS_MAXPKTHDR) * pktscale;
244         } else {
245                 if (nmp->nm_sotype != SOCK_STREAM)
246                         panic("nfscon sotype");
247                 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
248                     sizeof (u_int32_t)) * pktscale;
249                 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
250                     sizeof (u_int32_t)) * pktscale;
251         }
252         mtx_unlock(&nmp->nm_mtx);
253
254         client = clnt_reconnect_create(nconf, saddr, NFS_PROG, vers,
255             sndreserve, rcvreserve);
256         CLNT_CONTROL(client, CLSET_WAITCHAN, "nfsreq");
257         if (nmp->nm_flag & NFSMNT_INT)
258                 CLNT_CONTROL(client, CLSET_INTERRUPTIBLE, &one);
259         if (nmp->nm_flag & NFSMNT_RESVPORT)
260                 CLNT_CONTROL(client, CLSET_PRIVPORT, &one);
261         if ((nmp->nm_flag & NFSMNT_SOFT) != 0) {
262                 if (nmp->nm_sotype == SOCK_DGRAM)
263                         /*
264                          * For UDP, the large timeout for a reconnect will
265                          * be set to "nm_retry * nm_timeo / 2", so we only
266                          * want to do 2 reconnect timeout retries.
267                          */
268                         retries = 2;
269                 else
270                         retries = nmp->nm_retry;
271         } else
272                 retries = INT_MAX;
273         CLNT_CONTROL(client, CLSET_RETRIES, &retries);
274
275         /*
276          * For UDP, there are 2 timeouts:
277          * - CLSET_RETRY_TIMEOUT sets the initial timeout for the timer
278          *   that does a retransmit of an RPC request using the same socket
279          *   and xid. This is what you normally want to do, since NFS
280          *   servers depend on "same xid" for their Duplicate Request Cache.
281          * - timeout specified in CLNT_CALL_MBUF(), which specifies when
282          *   retransmits on the same socket should fail and a fresh socket
283          *   created. Each of these timeouts counts as one CLSET_RETRIES,
284          *   as set above.
285          * Set the initial retransmit timeout for UDP. This timeout doesn't
286          * exist for TCP and the following call just fails, which is ok.
287          */
288         timo.tv_sec = nmp->nm_timeo / NFS_HZ;
289         timo.tv_usec = (nmp->nm_timeo % NFS_HZ) * 1000000 / NFS_HZ;
290         CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, &timo);
291
292         mtx_lock(&nmp->nm_mtx);
293         if (nmp->nm_client) {
294                 /*
295                  * Someone else already connected.
296                  */
297                 CLNT_RELEASE(client);
298         } else
299                 nmp->nm_client = client;
300
301         /*
302          * Protocols that do not require connections may be optionally left
303          * unconnected for servers that reply from a port other than NFS_PORT.
304          */
305         if (!(nmp->nm_flag & NFSMNT_NOCONN)) {
306                 mtx_unlock(&nmp->nm_mtx);
307                 CLNT_CONTROL(client, CLSET_CONNECT, &one);
308         } else
309                 mtx_unlock(&nmp->nm_mtx);
310
311         /* Restore current thread's credentials. */
312         td->td_ucred = origcred;
313
314         mtx_lock(&nmp->nm_mtx);
315         /* Initialize other non-zero congestion variables. */
316         nfs_init_rtt(nmp);
317         mtx_unlock(&nmp->nm_mtx);
318         return (0);
319 }
320
321 /*
322  * NFS disconnect.  Clean up and unlink.
323  */
324 void
325 nfs_disconnect(struct nfsmount *nmp)
326 {
327         CLIENT *client;
328
329         mtx_lock(&nmp->nm_mtx);
330         if (nmp->nm_client) {
331                 client = nmp->nm_client;
332                 nmp->nm_client = NULL;
333                 mtx_unlock(&nmp->nm_mtx);
334                 rpc_gss_secpurge_call(client);
335                 CLNT_CLOSE(client);
336                 CLNT_RELEASE(client);
337         } else
338                 mtx_unlock(&nmp->nm_mtx);
339 }
340
341 void
342 nfs_safedisconnect(struct nfsmount *nmp)
343 {
344
345         nfs_disconnect(nmp);
346 }
347
348 static AUTH *
349 nfs_getauth(struct nfsmount *nmp, struct ucred *cred)
350 {
351         rpc_gss_service_t svc;
352         AUTH *auth;
353
354         switch (nmp->nm_secflavor) {
355         case RPCSEC_GSS_KRB5:
356         case RPCSEC_GSS_KRB5I:
357         case RPCSEC_GSS_KRB5P:
358                 if (!nmp->nm_mech_oid)
359                         if (!rpc_gss_mech_to_oid_call("kerberosv5",
360                             &nmp->nm_mech_oid))
361                                 return (NULL);
362                 if (nmp->nm_secflavor == RPCSEC_GSS_KRB5)
363                         svc = rpc_gss_svc_none;
364                 else if (nmp->nm_secflavor == RPCSEC_GSS_KRB5I)
365                         svc = rpc_gss_svc_integrity;
366                 else
367                         svc = rpc_gss_svc_privacy;
368                 auth = rpc_gss_secfind_call(nmp->nm_client, cred,
369                     nmp->nm_principal, nmp->nm_mech_oid, svc);
370                 if (auth)
371                         return (auth);
372                 /* fallthrough */
373         case AUTH_SYS:
374         default:
375                 return (authunix_create(cred));
376
377         }
378 }
379
380 /*
381  * Callback from the RPC code to generate up/down notifications.
382  */
383
384 struct nfs_feedback_arg {
385         struct nfsmount *nf_mount;
386         int             nf_lastmsg;     /* last tprintf */
387         int             nf_tprintfmsg;
388         struct thread   *nf_td;
389 };
390
391 static void
392 nfs_feedback(int type, int proc, void *arg)
393 {
394         struct nfs_feedback_arg *nf = (struct nfs_feedback_arg *) arg;
395         struct nfsmount *nmp = nf->nf_mount;
396         time_t now;
397
398         switch (type) {
399         case FEEDBACK_REXMIT2:
400         case FEEDBACK_RECONNECT:
401                 now = time_uptime;
402                 if (nf->nf_lastmsg + nmp->nm_tprintf_delay < now) {
403                         nfs_down(nmp, nf->nf_td,
404                             "not responding", 0, NFSSTA_TIMEO);
405                         nf->nf_tprintfmsg = TRUE;
406                         nf->nf_lastmsg = now;
407                 }
408                 break;
409
410         case FEEDBACK_OK:
411                 nfs_up(nf->nf_mount, nf->nf_td,
412                     "is alive again", NFSSTA_TIMEO, nf->nf_tprintfmsg);
413                 break;
414         }
415 }
416
417 /*
418  * nfs_request - goes something like this
419  *      - fill in request struct
420  *      - links it into list
421  *      - calls nfs_send() for first transmit
422  *      - calls nfs_receive() to get reply
423  *      - break down rpc header and return with nfs reply pointed to
424  *        by mrep or error
425  * nb: always frees up mreq mbuf list
426  */
427 int
428 nfs_request(struct vnode *vp, struct mbuf *mreq, int procnum,
429     struct thread *td, struct ucred *cred, struct mbuf **mrp,
430     struct mbuf **mdp, caddr_t *dposp)
431 {
432         struct mbuf *mrep;
433         u_int32_t *tl;
434         struct nfsmount *nmp;
435         struct mbuf *md;
436         time_t waituntil;
437         caddr_t dpos;
438         int error = 0, timeo;
439         AUTH *auth = NULL;
440         enum nfs_rto_timer_t timer;
441         struct nfs_feedback_arg nf;
442         struct rpc_callextra ext;
443         enum clnt_stat stat;
444         struct timeval timo;
445
446         /* Reject requests while attempting a forced unmount. */
447         if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) {
448                 m_freem(mreq);
449                 return (ESTALE);
450         }
451         nmp = VFSTONFS(vp->v_mount);
452         bzero(&nf, sizeof(struct nfs_feedback_arg));
453         nf.nf_mount = nmp;
454         nf.nf_td = td;
455         nf.nf_lastmsg = time_uptime -
456             ((nmp->nm_tprintf_delay) - (nmp->nm_tprintf_initial_delay));
457
458         /*
459          * XXX if not already connected call nfs_connect now.  Longer
460          * term, change nfs_mount to call nfs_connect unconditionally
461          * and let clnt_reconnect_create handle reconnects.
462          */
463         if (!nmp->nm_client)
464                 nfs_connect(nmp);
465
466         auth = nfs_getauth(nmp, cred);
467         if (!auth) {
468                 m_freem(mreq);
469                 return (EACCES);
470         }
471         bzero(&ext, sizeof(ext));
472         ext.rc_auth = auth;
473
474         ext.rc_feedback = nfs_feedback;
475         ext.rc_feedback_arg = &nf;
476
477         /*
478          * Use a conservative timeout for RPCs other than getattr,
479          * lookup, read or write.  The justification for doing "other"
480          * this way is that these RPCs happen so infrequently that
481          * timer est. would probably be stale.  Also, since many of
482          * these RPCs are non-idempotent, a conservative timeout is
483          * desired.
484          */
485         timer = nfs_rto_timer(procnum);
486         if (timer != NFS_DEFAULT_TIMER)
487                 ext.rc_timers = &nmp->nm_timers[timer - 1];
488         else
489                 ext.rc_timers = NULL;
490
491 #ifdef KDTRACE_HOOKS
492         if (dtrace_nfsclient_nfs23_start_probe != NULL) {
493                 uint32_t probe_id;
494                 int probe_procnum;
495
496                 if (nmp->nm_flag & NFSMNT_NFSV3) {
497                         probe_id = nfsclient_nfs3_start_probes[procnum];
498                         probe_procnum = procnum;
499                 } else {
500                         probe_id = nfsclient_nfs2_start_probes[procnum];
501                         probe_procnum = nfsv2_procid[procnum];
502                 }
503                 if (probe_id != 0)
504                         (dtrace_nfsclient_nfs23_start_probe)(probe_id, vp,
505                             mreq, cred, probe_procnum);
506         }
507 #endif
508
509         nfsstats.rpcrequests++;
510 tryagain:
511         /*
512          * This timeout specifies when a new socket should be created,
513          * along with new xid values. For UDP, this should be done
514          * infrequently, since retransmits of RPC requests should normally
515          * use the same xid.
516          */
517         if (nmp->nm_sotype == SOCK_DGRAM) {
518                 if ((nmp->nm_flag & NFSMNT_SOFT) != 0) {
519                         /*
520                          * CLSET_RETRIES is set to 2, so this should be half
521                          * of the total timeout required.
522                          */
523                         timeo = nmp->nm_retry * nmp->nm_timeo / 2;
524                         if (timeo < 1)
525                                 timeo = 1;
526                         timo.tv_sec = timeo / NFS_HZ;
527                         timo.tv_usec = (timeo % NFS_HZ) * 1000000 / NFS_HZ;
528                 } else {
529                         /* For UDP hard mounts, use a large value. */
530                         timo.tv_sec = NFS_MAXTIMEO / NFS_HZ;
531                         timo.tv_usec = 0;
532                 }
533         } else {
534                 timo.tv_sec = nmp->nm_timeo / NFS_HZ;
535                 timo.tv_usec = (nmp->nm_timeo % NFS_HZ) * 1000000 / NFS_HZ;
536         }
537         mrep = NULL;
538         stat = CLNT_CALL_MBUF(nmp->nm_client, &ext,
539             (nmp->nm_flag & NFSMNT_NFSV3) ? procnum : nfsv2_procid[procnum],
540             mreq, &mrep, timo);
541
542         /*
543          * If there was a successful reply and a tprintf msg.
544          * tprintf a response.
545          */
546         if (stat == RPC_SUCCESS)
547                 error = 0;
548         else if (stat == RPC_TIMEDOUT) {
549                 nfsstats.rpctimeouts++;
550                 error = ETIMEDOUT;
551         } else if (stat == RPC_VERSMISMATCH) {
552                 nfsstats.rpcinvalid++;
553                 error = EOPNOTSUPP;
554         } else if (stat == RPC_PROGVERSMISMATCH) {
555                 nfsstats.rpcinvalid++;
556                 error = EPROTONOSUPPORT;
557         } else if (stat == RPC_INTR) {
558                 error = EINTR;
559         } else {
560                 nfsstats.rpcinvalid++;
561                 error = EACCES;
562         }
563         if (error)
564                 goto nfsmout;
565
566         KASSERT(mrep != NULL, ("mrep shouldn't be NULL if no error\n"));
567
568         /*
569          * Search for any mbufs that are not a multiple of 4 bytes long
570          * or with m_data not longword aligned.
571          * These could cause pointer alignment problems, so copy them to
572          * well aligned mbufs.
573          */
574         error = nfs_realign(&mrep, M_NOWAIT);
575         if (error == ENOMEM) {
576                 m_freem(mrep);
577                 AUTH_DESTROY(auth);
578                 nfsstats.rpcinvalid++;
579                 return (error);
580         }
581
582         md = mrep;
583         dpos = mtod(mrep, caddr_t);
584         tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
585         if (*tl != 0) {
586                 error = fxdr_unsigned(int, *tl);
587                 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
588                     error == NFSERR_TRYLATER) {
589                         m_freem(mrep);
590                         error = 0;
591                         waituntil = time_second + nfs3_jukebox_delay;
592                         while (time_second < waituntil)
593                                 (void)tsleep(&fake_wchan, PSOCK, "nqnfstry",
594                                     hz);
595                         goto tryagain;
596                 }
597                 /*
598                  * Make sure NFSERR_RETERR isn't bogusly set by a server
599                  * such as amd. (No actual NFS error has bit 31 set.)
600                  */
601                 error &= ~NFSERR_RETERR;
602
603                 /*
604                  * If the File Handle was stale, invalidate the lookup
605                  * cache, just in case.
606                  */
607                 if (error == ESTALE)
608                         nfs_purgecache(vp);
609                 /*
610                  * Skip wcc data on non-ENOENT NFS errors for now.
611                  * NetApp filers return corrupt postop attrs in the
612                  * wcc data for NFS err EROFS.  Not sure if they could
613                  * return corrupt postop attrs for others errors.
614                  * Blocking ENOENT post-op attributes breaks negative
615                  * name caching, so always allow it through.
616                  */
617                 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
618                     (!nfs_skip_wcc_data_onerr || error == ENOENT)) {
619                         *mrp = mrep;
620                         *mdp = md;
621                         *dposp = dpos;
622                         error |= NFSERR_RETERR;
623                 } else
624                         m_freem(mrep);
625                 goto nfsmout;
626         }
627
628 #ifdef KDTRACE_HOOKS
629         if (dtrace_nfsclient_nfs23_done_probe != NULL) {
630                 uint32_t probe_id;
631                 int probe_procnum;
632
633                 if (nmp->nm_flag & NFSMNT_NFSV3) {
634                         probe_id = nfsclient_nfs3_done_probes[procnum];
635                         probe_procnum = procnum;
636                 } else {
637                         probe_id = nfsclient_nfs2_done_probes[procnum];
638                         probe_procnum = (nmp->nm_flag & NFSMNT_NFSV3) ?
639                             procnum : nfsv2_procid[procnum];
640                 }
641                 if (probe_id != 0)
642                         (dtrace_nfsclient_nfs23_done_probe)(probe_id, vp,
643                             mreq, cred, probe_procnum, 0);
644         }
645 #endif
646         m_freem(mreq);
647         *mrp = mrep;
648         *mdp = md;
649         *dposp = dpos;
650         AUTH_DESTROY(auth);
651         return (0);
652
653 nfsmout:
654 #ifdef KDTRACE_HOOKS
655         if (dtrace_nfsclient_nfs23_done_probe != NULL) {
656                 uint32_t probe_id;
657                 int probe_procnum;
658
659                 if (nmp->nm_flag & NFSMNT_NFSV3) {
660                         probe_id = nfsclient_nfs3_done_probes[procnum];
661                         probe_procnum = procnum;
662                 } else {
663                         probe_id = nfsclient_nfs2_done_probes[procnum];
664                         probe_procnum = (nmp->nm_flag & NFSMNT_NFSV3) ?
665                             procnum : nfsv2_procid[procnum];
666                 }
667                 if (probe_id != 0)
668                         (dtrace_nfsclient_nfs23_done_probe)(probe_id, vp,
669                             mreq, cred, probe_procnum, error);
670         }
671 #endif
672         m_freem(mreq);
673         if (auth)
674                 AUTH_DESTROY(auth);
675         return (error);
676 }
677
678 /*
679  * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
680  * wait for all requests to complete.  This is used by forced unmounts
681  * to terminate any outstanding RPCs.
682  */
683 int
684 nfs_nmcancelreqs(struct nfsmount *nmp)
685 {
686
687         if (nmp->nm_client)
688                 CLNT_CLOSE(nmp->nm_client);
689         return (0);
690 }
691
692 /*
693  * Any signal that can interrupt an NFS operation in an intr mount
694  * should be added to this set.  SIGSTOP and SIGKILL cannot be masked.
695  */
696 int nfs_sig_set[] = {
697         SIGINT,
698         SIGTERM,
699         SIGHUP,
700         SIGKILL,
701         SIGQUIT
702 };
703
704 /*
705  * Check to see if one of the signals in our subset is pending on
706  * the process (in an intr mount).
707  */
708 static int
709 nfs_sig_pending(sigset_t set)
710 {
711         int i;
712
713         for (i = 0 ; i < sizeof(nfs_sig_set)/sizeof(int) ; i++)
714                 if (SIGISMEMBER(set, nfs_sig_set[i]))
715                         return (1);
716         return (0);
717 }
718
719 /*
720  * The set/restore sigmask functions are used to (temporarily) overwrite
721  * the thread td_sigmask during an RPC call (for example).  These are also
722  * used in other places in the NFS client that might tsleep().
723  */
724 void
725 nfs_set_sigmask(struct thread *td, sigset_t *oldset)
726 {
727         sigset_t newset;
728         int i;
729         struct proc *p;
730
731         SIGFILLSET(newset);
732         if (td == NULL)
733                 td = curthread; /* XXX */
734         p = td->td_proc;
735         /* Remove the NFS set of signals from newset. */
736         PROC_LOCK(p);
737         mtx_lock(&p->p_sigacts->ps_mtx);
738         for (i = 0 ; i < sizeof(nfs_sig_set)/sizeof(int) ; i++) {
739                 /*
740                  * But make sure we leave the ones already masked
741                  * by the process, i.e. remove the signal from the
742                  * temporary signalmask only if it wasn't already
743                  * in p_sigmask.
744                  */
745                 if (!SIGISMEMBER(td->td_sigmask, nfs_sig_set[i]) &&
746                     !SIGISMEMBER(p->p_sigacts->ps_sigignore, nfs_sig_set[i]))
747                         SIGDELSET(newset, nfs_sig_set[i]);
748         }
749         mtx_unlock(&p->p_sigacts->ps_mtx);
750         kern_sigprocmask(td, SIG_SETMASK, &newset, oldset,
751             SIGPROCMASK_PROC_LOCKED);
752         PROC_UNLOCK(p);
753 }
754
755 void
756 nfs_restore_sigmask(struct thread *td, sigset_t *set)
757 {
758         if (td == NULL)
759                 td = curthread; /* XXX */
760         kern_sigprocmask(td, SIG_SETMASK, set, NULL, 0);
761 }
762
763 /*
764  * NFS wrapper to msleep(), that shoves a new p_sigmask and restores the
765  * old one after msleep() returns.
766  */
767 int
768 nfs_msleep(struct thread *td, void *ident, struct mtx *mtx, int priority,
769     char *wmesg, int timo)
770 {
771         sigset_t oldset;
772         int error;
773         struct proc *p;
774
775         if ((priority & PCATCH) == 0)
776                 return msleep(ident, mtx, priority, wmesg, timo);
777         if (td == NULL)
778                 td = curthread; /* XXX */
779         nfs_set_sigmask(td, &oldset);
780         error = msleep(ident, mtx, priority, wmesg, timo);
781         nfs_restore_sigmask(td, &oldset);
782         p = td->td_proc;
783         return (error);
784 }
785
786 /*
787  * Test for a termination condition pending on the process.
788  * This is used for NFSMNT_INT mounts.
789  */
790 int
791 nfs_sigintr(struct nfsmount *nmp, struct thread *td)
792 {
793         struct proc *p;
794         sigset_t tmpset;
795
796         /* Terminate all requests while attempting a forced unmount. */
797         if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)
798                 return (EIO);
799         if (!(nmp->nm_flag & NFSMNT_INT))
800                 return (0);
801         if (td == NULL)
802                 return (0);
803         p = td->td_proc;
804         PROC_LOCK(p);
805         tmpset = p->p_siglist;
806         SIGSETOR(tmpset, td->td_siglist);
807         SIGSETNAND(tmpset, td->td_sigmask);
808         mtx_lock(&p->p_sigacts->ps_mtx);
809         SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore);
810         mtx_unlock(&p->p_sigacts->ps_mtx);
811         if ((SIGNOTEMPTY(p->p_siglist) || SIGNOTEMPTY(td->td_siglist))
812             && nfs_sig_pending(tmpset)) {
813                 PROC_UNLOCK(p);
814                 return (EINTR);
815         }
816         PROC_UNLOCK(p);
817         return (0);
818 }
819
820 static int
821 nfs_msg(struct thread *td, const char *server, const char *msg, int error)
822 {
823         struct proc *p;
824
825         p = td ? td->td_proc : NULL;
826         if (error)
827                 tprintf(p, LOG_INFO, "nfs server %s: %s, error %d\n", server,
828                     msg, error);
829         else
830                 tprintf(p, LOG_INFO, "nfs server %s: %s\n", server, msg);
831         return (0);
832 }
833
834 static void
835 nfs_down(struct nfsmount *nmp, struct thread *td, const char *msg,
836     int error, int flags)
837 {
838         if (nmp == NULL)
839                 return;
840         mtx_lock(&nmp->nm_mtx);
841         if ((flags & NFSSTA_TIMEO) && !(nmp->nm_state & NFSSTA_TIMEO)) {
842                 nmp->nm_state |= NFSSTA_TIMEO;
843                 mtx_unlock(&nmp->nm_mtx);
844                 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
845                     VQ_NOTRESP, 0);
846         } else
847                 mtx_unlock(&nmp->nm_mtx);
848         mtx_lock(&nmp->nm_mtx);
849         if ((flags & NFSSTA_LOCKTIMEO) &&
850             !(nmp->nm_state & NFSSTA_LOCKTIMEO)) {
851                 nmp->nm_state |= NFSSTA_LOCKTIMEO;
852                 mtx_unlock(&nmp->nm_mtx);
853                 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
854                     VQ_NOTRESPLOCK, 0);
855         } else
856                 mtx_unlock(&nmp->nm_mtx);
857         nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error);
858 }
859
860 static void
861 nfs_up(struct nfsmount *nmp, struct thread *td, const char *msg,
862     int flags, int tprintfmsg)
863 {
864         if (nmp == NULL)
865                 return;
866         if (tprintfmsg)
867                 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0);
868
869         mtx_lock(&nmp->nm_mtx);
870         if ((flags & NFSSTA_TIMEO) && (nmp->nm_state & NFSSTA_TIMEO)) {
871                 nmp->nm_state &= ~NFSSTA_TIMEO;
872                 mtx_unlock(&nmp->nm_mtx);
873                 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
874                     VQ_NOTRESP, 1);
875         } else
876                 mtx_unlock(&nmp->nm_mtx);
877
878         mtx_lock(&nmp->nm_mtx);
879         if ((flags & NFSSTA_LOCKTIMEO) &&
880             (nmp->nm_state & NFSSTA_LOCKTIMEO)) {
881                 nmp->nm_state &= ~NFSSTA_LOCKTIMEO;
882                 mtx_unlock(&nmp->nm_mtx);
883                 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
884                     VQ_NOTRESPLOCK, 1);
885         } else
886                 mtx_unlock(&nmp->nm_mtx);
887 }