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