]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/nfs/nfs_commonkrpc.c
Merge OpenSSL 1.0.2l.
[FreeBSD/FreeBSD.git] / sys / fs / nfs / nfs_commonkrpc.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  * 3. 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  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 /*
38  * Socket operations for use by nfs
39  */
40
41 #include "opt_kgssapi.h"
42 #include "opt_nfs.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/limits.h>
48 #include <sys/lock.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <sys/mount.h>
52 #include <sys/mutex.h>
53 #include <sys/proc.h>
54 #include <sys/signalvar.h>
55 #include <sys/syscallsubr.h>
56 #include <sys/sysctl.h>
57 #include <sys/syslog.h>
58 #include <sys/vnode.h>
59
60 #include <rpc/rpc.h>
61 #include <rpc/krpc.h>
62
63 #include <kgssapi/krb5/kcrypto.h>
64
65 #include <fs/nfs/nfsport.h>
66
67 #ifdef KDTRACE_HOOKS
68 #include <sys/dtrace_bsd.h>
69
70 dtrace_nfsclient_nfs23_start_probe_func_t
71                 dtrace_nfscl_nfs234_start_probe;
72
73 dtrace_nfsclient_nfs23_done_probe_func_t
74                 dtrace_nfscl_nfs234_done_probe;
75
76 /*
77  * Registered probes by RPC type.
78  */
79 uint32_t        nfscl_nfs2_start_probes[NFSV41_NPROCS + 1];
80 uint32_t        nfscl_nfs2_done_probes[NFSV41_NPROCS + 1];
81
82 uint32_t        nfscl_nfs3_start_probes[NFSV41_NPROCS + 1];
83 uint32_t        nfscl_nfs3_done_probes[NFSV41_NPROCS + 1];
84
85 uint32_t        nfscl_nfs4_start_probes[NFSV41_NPROCS + 1];
86 uint32_t        nfscl_nfs4_done_probes[NFSV41_NPROCS + 1];
87 #endif
88
89 NFSSTATESPINLOCK;
90 NFSREQSPINLOCK;
91 NFSDLOCKMUTEX;
92 NFSCLSTATEMUTEX;
93 extern struct nfsstatsv1 nfsstatsv1;
94 extern struct nfsreqhead nfsd_reqq;
95 extern int nfscl_ticks;
96 extern void (*ncl_call_invalcaches)(struct vnode *);
97 extern int nfs_numnfscbd;
98 extern int nfscl_debuglevel;
99
100 SVCPOOL         *nfscbd_pool;
101 static int      nfsrv_gsscallbackson = 0;
102 static int      nfs_bufpackets = 4;
103 static int      nfs_reconnects;
104 static int      nfs3_jukebox_delay = 10;
105 static int      nfs_skip_wcc_data_onerr = 1;
106
107 SYSCTL_DECL(_vfs_nfs);
108
109 SYSCTL_INT(_vfs_nfs, OID_AUTO, bufpackets, CTLFLAG_RW, &nfs_bufpackets, 0,
110     "Buffer reservation size 2 < x < 64");
111 SYSCTL_INT(_vfs_nfs, OID_AUTO, reconnects, CTLFLAG_RD, &nfs_reconnects, 0,
112     "Number of times the nfs client has had to reconnect");
113 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs3_jukebox_delay, CTLFLAG_RW, &nfs3_jukebox_delay, 0,
114     "Number of seconds to delay a retry after receiving EJUKEBOX");
115 SYSCTL_INT(_vfs_nfs, OID_AUTO, skip_wcc_data_onerr, CTLFLAG_RW, &nfs_skip_wcc_data_onerr, 0,
116     "Disable weak cache consistency checking when server returns an error");
117
118 static void     nfs_down(struct nfsmount *, struct thread *, const char *,
119     int, int);
120 static void     nfs_up(struct nfsmount *, struct thread *, const char *,
121     int, int);
122 static int      nfs_msg(struct thread *, const char *, const char *, int);
123
124 struct nfs_cached_auth {
125         int             ca_refs; /* refcount, including 1 from the cache */
126         uid_t           ca_uid;  /* uid that corresponds to this auth */
127         AUTH            *ca_auth; /* RPC auth handle */
128 };
129
130 static int nfsv2_procid[NFS_V3NPROCS] = {
131         NFSV2PROC_NULL,
132         NFSV2PROC_GETATTR,
133         NFSV2PROC_SETATTR,
134         NFSV2PROC_LOOKUP,
135         NFSV2PROC_NOOP,
136         NFSV2PROC_READLINK,
137         NFSV2PROC_READ,
138         NFSV2PROC_WRITE,
139         NFSV2PROC_CREATE,
140         NFSV2PROC_MKDIR,
141         NFSV2PROC_SYMLINK,
142         NFSV2PROC_CREATE,
143         NFSV2PROC_REMOVE,
144         NFSV2PROC_RMDIR,
145         NFSV2PROC_RENAME,
146         NFSV2PROC_LINK,
147         NFSV2PROC_READDIR,
148         NFSV2PROC_NOOP,
149         NFSV2PROC_STATFS,
150         NFSV2PROC_NOOP,
151         NFSV2PROC_NOOP,
152         NFSV2PROC_NOOP,
153 };
154
155 /*
156  * Initialize sockets and congestion for a new NFS connection.
157  * We do not free the sockaddr if error.
158  */
159 int
160 newnfs_connect(struct nfsmount *nmp, struct nfssockreq *nrp,
161     struct ucred *cred, NFSPROC_T *p, int callback_retry_mult)
162 {
163         int rcvreserve, sndreserve;
164         int pktscale;
165         struct sockaddr *saddr;
166         struct ucred *origcred;
167         CLIENT *client;
168         struct netconfig *nconf;
169         struct socket *so;
170         int one = 1, retries, error = 0;
171         struct thread *td = curthread;
172         SVCXPRT *xprt;
173         struct timeval timo;
174
175         /*
176          * We need to establish the socket using the credentials of
177          * the mountpoint.  Some parts of this process (such as
178          * sobind() and soconnect()) will use the curent thread's
179          * credential instead of the socket credential.  To work
180          * around this, temporarily change the current thread's
181          * credential to that of the mountpoint.
182          *
183          * XXX: It would be better to explicitly pass the correct
184          * credential to sobind() and soconnect().
185          */
186         origcred = td->td_ucred;
187
188         /*
189          * Use the credential in nr_cred, if not NULL.
190          */
191         if (nrp->nr_cred != NULL)
192                 td->td_ucred = nrp->nr_cred;
193         else
194                 td->td_ucred = cred;
195         saddr = nrp->nr_nam;
196
197         if (saddr->sa_family == AF_INET)
198                 if (nrp->nr_sotype == SOCK_DGRAM)
199                         nconf = getnetconfigent("udp");
200                 else
201                         nconf = getnetconfigent("tcp");
202         else
203                 if (nrp->nr_sotype == SOCK_DGRAM)
204                         nconf = getnetconfigent("udp6");
205                 else
206                         nconf = getnetconfigent("tcp6");
207                         
208         pktscale = nfs_bufpackets;
209         if (pktscale < 2)
210                 pktscale = 2;
211         if (pktscale > 64)
212                 pktscale = 64;
213         /*
214          * soreserve() can fail if sb_max is too small, so shrink pktscale
215          * and try again if there is an error.
216          * Print a log message suggesting increasing sb_max.
217          * Creating a socket and doing this is necessary since, if the
218          * reservation sizes are too large and will make soreserve() fail,
219          * the connection will work until a large send is attempted and
220          * then it will loop in the krpc code.
221          */
222         so = NULL;
223         saddr = NFSSOCKADDR(nrp->nr_nam, struct sockaddr *);
224         error = socreate(saddr->sa_family, &so, nrp->nr_sotype, 
225             nrp->nr_soproto, td->td_ucred, td);
226         if (error) {
227                 td->td_ucred = origcred;
228                 goto out;
229         }
230         do {
231             if (error != 0 && pktscale > 2)
232                 pktscale--;
233             if (nrp->nr_sotype == SOCK_DGRAM) {
234                 if (nmp != NULL) {
235                         sndreserve = (NFS_MAXDGRAMDATA + NFS_MAXPKTHDR) *
236                             pktscale;
237                         rcvreserve = (NFS_MAXDGRAMDATA + NFS_MAXPKTHDR) *
238                             pktscale;
239                 } else {
240                         sndreserve = rcvreserve = 1024 * pktscale;
241                 }
242             } else {
243                 if (nrp->nr_sotype != SOCK_STREAM)
244                         panic("nfscon sotype");
245                 if (nmp != NULL) {
246                         sndreserve = (NFS_MAXBSIZE + NFS_MAXPKTHDR +
247                             sizeof (u_int32_t)) * pktscale;
248                         rcvreserve = (NFS_MAXBSIZE + NFS_MAXPKTHDR +
249                             sizeof (u_int32_t)) * pktscale;
250                 } else {
251                         sndreserve = rcvreserve = 1024 * pktscale;
252                 }
253             }
254             error = soreserve(so, sndreserve, rcvreserve);
255         } while (error != 0 && pktscale > 2);
256         soclose(so);
257         if (error) {
258                 td->td_ucred = origcred;
259                 goto out;
260         }
261
262         client = clnt_reconnect_create(nconf, saddr, nrp->nr_prog,
263             nrp->nr_vers, sndreserve, rcvreserve);
264         CLNT_CONTROL(client, CLSET_WAITCHAN, "nfsreq");
265         if (nmp != NULL) {
266                 if ((nmp->nm_flag & NFSMNT_INT))
267                         CLNT_CONTROL(client, CLSET_INTERRUPTIBLE, &one);
268                 if ((nmp->nm_flag & NFSMNT_RESVPORT))
269                         CLNT_CONTROL(client, CLSET_PRIVPORT, &one);
270                 if (NFSHASSOFT(nmp)) {
271                         if (nmp->nm_sotype == SOCK_DGRAM)
272                                 /*
273                                  * For UDP, the large timeout for a reconnect
274                                  * will be set to "nm_retry * nm_timeo / 2", so
275                                  * we only want to do 2 reconnect timeout
276                                  * retries.
277                                  */
278                                 retries = 2;
279                         else
280                                 retries = nmp->nm_retry;
281                 } else
282                         retries = INT_MAX;
283                 /* cred == NULL for DS connects. */
284                 if (NFSHASNFSV4N(nmp) && cred != NULL) {
285                         /*
286                          * Make sure the nfscbd_pool doesn't get destroyed
287                          * while doing this.
288                          */
289                         NFSD_LOCK();
290                         if (nfs_numnfscbd > 0) {
291                                 nfs_numnfscbd++;
292                                 NFSD_UNLOCK();
293                                 xprt = svc_vc_create_backchannel(nfscbd_pool);
294                                 CLNT_CONTROL(client, CLSET_BACKCHANNEL, xprt);
295                                 NFSD_LOCK();
296                                 nfs_numnfscbd--;
297                                 if (nfs_numnfscbd == 0)
298                                         wakeup(&nfs_numnfscbd);
299                         }
300                         NFSD_UNLOCK();
301                 }
302         } else {
303                 /*
304                  * Three cases:
305                  * - Null RPC callback to client
306                  * - Non-Null RPC callback to client, wait a little longer
307                  * - upcalls to nfsuserd and gssd (clp == NULL)
308                  */
309                 if (callback_retry_mult == 0) {
310                         retries = NFSV4_UPCALLRETRY;
311                         CLNT_CONTROL(client, CLSET_PRIVPORT, &one);
312                 } else {
313                         retries = NFSV4_CALLBACKRETRY * callback_retry_mult;
314                 }
315         }
316         CLNT_CONTROL(client, CLSET_RETRIES, &retries);
317
318         if (nmp != NULL) {
319                 /*
320                  * For UDP, there are 2 timeouts:
321                  * - CLSET_RETRY_TIMEOUT sets the initial timeout for the timer
322                  *   that does a retransmit of an RPC request using the same 
323                  *   socket and xid. This is what you normally want to do,
324                  *   since NFS servers depend on "same xid" for their
325                  *   Duplicate Request Cache.
326                  * - timeout specified in CLNT_CALL_MBUF(), which specifies when
327                  *   retransmits on the same socket should fail and a fresh
328                  *   socket created. Each of these timeouts counts as one
329                  *   CLSET_RETRIES as set above.
330                  * Set the initial retransmit timeout for UDP. This timeout
331                  * doesn't exist for TCP and the following call just fails,
332                  * which is ok.
333                  */
334                 timo.tv_sec = nmp->nm_timeo / NFS_HZ;
335                 timo.tv_usec = (nmp->nm_timeo % NFS_HZ) * 1000000 / NFS_HZ;
336                 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, &timo);
337         }
338
339         mtx_lock(&nrp->nr_mtx);
340         if (nrp->nr_client != NULL) {
341                 mtx_unlock(&nrp->nr_mtx);
342                 /*
343                  * Someone else already connected.
344                  */
345                 CLNT_RELEASE(client);
346         } else {
347                 nrp->nr_client = client;
348                 /*
349                  * Protocols that do not require connections may be optionally
350                  * left unconnected for servers that reply from a port other
351                  * than NFS_PORT.
352                  */
353                 if (nmp == NULL || (nmp->nm_flag & NFSMNT_NOCONN) == 0) {
354                         mtx_unlock(&nrp->nr_mtx);
355                         CLNT_CONTROL(client, CLSET_CONNECT, &one);
356                 } else
357                         mtx_unlock(&nrp->nr_mtx);
358         }
359
360
361         /* Restore current thread's credentials. */
362         td->td_ucred = origcred;
363
364 out:
365         NFSEXITCODE(error);
366         return (error);
367 }
368
369 /*
370  * NFS disconnect. Clean up and unlink.
371  */
372 void
373 newnfs_disconnect(struct nfssockreq *nrp)
374 {
375         CLIENT *client;
376
377         mtx_lock(&nrp->nr_mtx);
378         if (nrp->nr_client != NULL) {
379                 client = nrp->nr_client;
380                 nrp->nr_client = NULL;
381                 mtx_unlock(&nrp->nr_mtx);
382                 rpc_gss_secpurge_call(client);
383                 CLNT_CLOSE(client);
384                 CLNT_RELEASE(client);
385         } else {
386                 mtx_unlock(&nrp->nr_mtx);
387         }
388 }
389
390 static AUTH *
391 nfs_getauth(struct nfssockreq *nrp, int secflavour, char *clnt_principal,
392     char *srv_principal, gss_OID mech_oid, struct ucred *cred)
393 {
394         rpc_gss_service_t svc;
395         AUTH *auth;
396
397         switch (secflavour) {
398         case RPCSEC_GSS_KRB5:
399         case RPCSEC_GSS_KRB5I:
400         case RPCSEC_GSS_KRB5P:
401                 if (!mech_oid) {
402                         if (!rpc_gss_mech_to_oid_call("kerberosv5", &mech_oid))
403                                 return (NULL);
404                 }
405                 if (secflavour == RPCSEC_GSS_KRB5)
406                         svc = rpc_gss_svc_none;
407                 else if (secflavour == RPCSEC_GSS_KRB5I)
408                         svc = rpc_gss_svc_integrity;
409                 else
410                         svc = rpc_gss_svc_privacy;
411
412                 if (clnt_principal == NULL)
413                         auth = rpc_gss_secfind_call(nrp->nr_client, cred,
414                             srv_principal, mech_oid, svc);
415                 else {
416                         auth = rpc_gss_seccreate_call(nrp->nr_client, cred,
417                             clnt_principal, srv_principal, "kerberosv5",
418                             svc, NULL, NULL, NULL);
419                         return (auth);
420                 }
421                 if (auth != NULL)
422                         return (auth);
423                 /* fallthrough */
424         case AUTH_SYS:
425         default:
426                 return (authunix_create(cred));
427
428         }
429 }
430
431 /*
432  * Callback from the RPC code to generate up/down notifications.
433  */
434
435 struct nfs_feedback_arg {
436         struct nfsmount *nf_mount;
437         int             nf_lastmsg;     /* last tprintf */
438         int             nf_tprintfmsg;
439         struct thread   *nf_td;
440 };
441
442 static void
443 nfs_feedback(int type, int proc, void *arg)
444 {
445         struct nfs_feedback_arg *nf = (struct nfs_feedback_arg *) arg;
446         struct nfsmount *nmp = nf->nf_mount;
447         time_t now;
448
449         switch (type) {
450         case FEEDBACK_REXMIT2:
451         case FEEDBACK_RECONNECT:
452                 now = NFSD_MONOSEC;
453                 if (nf->nf_lastmsg + nmp->nm_tprintf_delay < now) {
454                         nfs_down(nmp, nf->nf_td,
455                             "not responding", 0, NFSSTA_TIMEO);
456                         nf->nf_tprintfmsg = TRUE;
457                         nf->nf_lastmsg = now;
458                 }
459                 break;
460
461         case FEEDBACK_OK:
462                 nfs_up(nf->nf_mount, nf->nf_td,
463                     "is alive again", NFSSTA_TIMEO, nf->nf_tprintfmsg);
464                 break;
465         }
466 }
467
468 /*
469  * newnfs_request - goes something like this
470  *      - does the rpc by calling the krpc layer
471  *      - break down rpc header and return with nfs reply
472  * nb: always frees up nd_mreq mbuf list
473  */
474 int
475 newnfs_request(struct nfsrv_descript *nd, struct nfsmount *nmp,
476     struct nfsclient *clp, struct nfssockreq *nrp, vnode_t vp,
477     struct thread *td, struct ucred *cred, u_int32_t prog, u_int32_t vers,
478     u_char *retsum, int toplevel, u_int64_t *xidp, struct nfsclsession *dssep)
479 {
480         uint32_t retseq, retval, slotseq, *tl;
481         time_t waituntil;
482         int i = 0, j = 0, opcnt, set_sigset = 0, slot;
483         int trycnt, error = 0, usegssname = 0, secflavour = AUTH_SYS;
484         int freeslot, maxslot, reterr, slotpos, timeo;
485         u_int16_t procnum;
486         u_int trylater_delay = 1;
487         struct nfs_feedback_arg nf;
488         struct timeval timo;
489         AUTH *auth;
490         struct rpc_callextra ext;
491         enum clnt_stat stat;
492         struct nfsreq *rep = NULL;
493         char *srv_principal = NULL, *clnt_principal = NULL;
494         sigset_t oldset;
495         struct ucred *authcred;
496         struct nfsclsession *sep;
497         uint8_t sessionid[NFSX_V4SESSIONID];
498
499         sep = dssep;
500         if (xidp != NULL)
501                 *xidp = 0;
502         /* Reject requests while attempting a forced unmount. */
503         if (nmp != NULL && (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)) {
504                 m_freem(nd->nd_mreq);
505                 return (ESTALE);
506         }
507
508         /*
509          * Set authcred, which is used to acquire RPC credentials to
510          * the cred argument, by default. The crhold() should not be
511          * necessary, but will ensure that some future code change
512          * doesn't result in the credential being free'd prematurely.
513          */
514         authcred = crhold(cred);
515
516         /* For client side interruptible mounts, mask off the signals. */
517         if (nmp != NULL && td != NULL && NFSHASINT(nmp)) {
518                 newnfs_set_sigmask(td, &oldset);
519                 set_sigset = 1;
520         }
521
522         /*
523          * XXX if not already connected call nfs_connect now. Longer
524          * term, change nfs_mount to call nfs_connect unconditionally
525          * and let clnt_reconnect_create handle reconnects.
526          */
527         if (nrp->nr_client == NULL)
528                 newnfs_connect(nmp, nrp, cred, td, 0);
529
530         /*
531          * For a client side mount, nmp is != NULL and clp == NULL. For
532          * server calls (callbacks or upcalls), nmp == NULL.
533          */
534         if (clp != NULL) {
535                 NFSLOCKSTATE();
536                 if ((clp->lc_flags & LCL_GSS) && nfsrv_gsscallbackson) {
537                         secflavour = RPCSEC_GSS_KRB5;
538                         if (nd->nd_procnum != NFSPROC_NULL) {
539                                 if (clp->lc_flags & LCL_GSSINTEGRITY)
540                                         secflavour = RPCSEC_GSS_KRB5I;
541                                 else if (clp->lc_flags & LCL_GSSPRIVACY)
542                                         secflavour = RPCSEC_GSS_KRB5P;
543                         }
544                 }
545                 NFSUNLOCKSTATE();
546         } else if (nmp != NULL && NFSHASKERB(nmp) &&
547              nd->nd_procnum != NFSPROC_NULL) {
548                 if (NFSHASALLGSSNAME(nmp) && nmp->nm_krbnamelen > 0)
549                         nd->nd_flag |= ND_USEGSSNAME;
550                 if ((nd->nd_flag & ND_USEGSSNAME) != 0) {
551                         /*
552                          * If there is a client side host based credential,
553                          * use that, otherwise use the system uid, if set.
554                          * The system uid is in the nmp->nm_sockreq.nr_cred
555                          * credentials.
556                          */
557                         if (nmp->nm_krbnamelen > 0) {
558                                 usegssname = 1;
559                                 clnt_principal = nmp->nm_krbname;
560                         } else if (nmp->nm_uid != (uid_t)-1) {
561                                 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
562                                     ("newnfs_request: NULL nr_cred"));
563                                 crfree(authcred);
564                                 authcred = crhold(nmp->nm_sockreq.nr_cred);
565                         }
566                 } else if (nmp->nm_krbnamelen == 0 &&
567                     nmp->nm_uid != (uid_t)-1 && cred->cr_uid == (uid_t)0) {
568                         /*
569                          * If there is no host based principal name and
570                          * the system uid is set and this is root, use the
571                          * system uid, since root won't have user
572                          * credentials in a credentials cache file.
573                          * The system uid is in the nmp->nm_sockreq.nr_cred
574                          * credentials.
575                          */
576                         KASSERT(nmp->nm_sockreq.nr_cred != NULL,
577                             ("newnfs_request: NULL nr_cred"));
578                         crfree(authcred);
579                         authcred = crhold(nmp->nm_sockreq.nr_cred);
580                 }
581                 if (NFSHASINTEGRITY(nmp))
582                         secflavour = RPCSEC_GSS_KRB5I;
583                 else if (NFSHASPRIVACY(nmp))
584                         secflavour = RPCSEC_GSS_KRB5P;
585                 else
586                         secflavour = RPCSEC_GSS_KRB5;
587                 srv_principal = NFSMNT_SRVKRBNAME(nmp);
588         } else if (nmp != NULL && !NFSHASKERB(nmp) &&
589             nd->nd_procnum != NFSPROC_NULL &&
590             (nd->nd_flag & ND_USEGSSNAME) != 0) {
591                 /*
592                  * Use the uid that did the mount when the RPC is doing
593                  * NFSv4 system operations, as indicated by the
594                  * ND_USEGSSNAME flag, for the AUTH_SYS case.
595                  * The credentials in nm_sockreq.nr_cred were used for the
596                  * mount.
597                  */
598                 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
599                     ("newnfs_request: NULL nr_cred"));
600                 crfree(authcred);
601                 authcred = crhold(nmp->nm_sockreq.nr_cred);
602         }
603
604         if (nmp != NULL) {
605                 bzero(&nf, sizeof(struct nfs_feedback_arg));
606                 nf.nf_mount = nmp;
607                 nf.nf_td = td;
608                 nf.nf_lastmsg = NFSD_MONOSEC -
609                     ((nmp->nm_tprintf_delay)-(nmp->nm_tprintf_initial_delay));
610         }
611
612         if (nd->nd_procnum == NFSPROC_NULL)
613                 auth = authnone_create();
614         else if (usegssname) {
615                 /*
616                  * For this case, the authenticator is held in the
617                  * nfssockreq structure, so don't release the reference count
618                  * held on it. --> Don't AUTH_DESTROY() it in this function.
619                  */
620                 if (nrp->nr_auth == NULL)
621                         nrp->nr_auth = nfs_getauth(nrp, secflavour,
622                             clnt_principal, srv_principal, NULL, authcred);
623                 else
624                         rpc_gss_refresh_auth_call(nrp->nr_auth);
625                 auth = nrp->nr_auth;
626         } else
627                 auth = nfs_getauth(nrp, secflavour, NULL,
628                     srv_principal, NULL, authcred);
629         crfree(authcred);
630         if (auth == NULL) {
631                 m_freem(nd->nd_mreq);
632                 if (set_sigset)
633                         newnfs_restore_sigmask(td, &oldset);
634                 return (EACCES);
635         }
636         bzero(&ext, sizeof(ext));
637         ext.rc_auth = auth;
638         if (nmp != NULL) {
639                 ext.rc_feedback = nfs_feedback;
640                 ext.rc_feedback_arg = &nf;
641         }
642
643         procnum = nd->nd_procnum;
644         if ((nd->nd_flag & ND_NFSV4) &&
645             nd->nd_procnum != NFSPROC_NULL &&
646             nd->nd_procnum != NFSV4PROC_CBCOMPOUND)
647                 procnum = NFSV4PROC_COMPOUND;
648
649         if (nmp != NULL) {
650                 NFSINCRGLOBAL(nfsstatsv1.rpcrequests);
651
652                 /* Map the procnum to the old NFSv2 one, as required. */
653                 if ((nd->nd_flag & ND_NFSV2) != 0) {
654                         if (nd->nd_procnum < NFS_V3NPROCS)
655                                 procnum = nfsv2_procid[nd->nd_procnum];
656                         else
657                                 procnum = NFSV2PROC_NOOP;
658                 }
659
660                 /*
661                  * Now only used for the R_DONTRECOVER case, but until that is
662                  * supported within the krpc code, I need to keep a queue of
663                  * outstanding RPCs for nfsv4 client requests.
664                  */
665                 if ((nd->nd_flag & ND_NFSV4) && procnum == NFSV4PROC_COMPOUND)
666                         MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq),
667                             M_NFSDREQ, M_WAITOK);
668 #ifdef KDTRACE_HOOKS
669                 if (dtrace_nfscl_nfs234_start_probe != NULL) {
670                         uint32_t probe_id;
671                         int probe_procnum;
672         
673                         if (nd->nd_flag & ND_NFSV4) {
674                                 probe_id =
675                                     nfscl_nfs4_start_probes[nd->nd_procnum];
676                                 probe_procnum = nd->nd_procnum;
677                         } else if (nd->nd_flag & ND_NFSV3) {
678                                 probe_id = nfscl_nfs3_start_probes[procnum];
679                                 probe_procnum = procnum;
680                         } else {
681                                 probe_id =
682                                     nfscl_nfs2_start_probes[nd->nd_procnum];
683                                 probe_procnum = procnum;
684                         }
685                         if (probe_id != 0)
686                                 (dtrace_nfscl_nfs234_start_probe)
687                                     (probe_id, vp, nd->nd_mreq, cred,
688                                      probe_procnum);
689                 }
690 #endif
691         }
692         trycnt = 0;
693         freeslot = -1;          /* Set to slot that needs to be free'd */
694 tryagain:
695         slot = -1;              /* Slot that needs a sequence# increment. */
696         /*
697          * This timeout specifies when a new socket should be created,
698          * along with new xid values. For UDP, this should be done
699          * infrequently, since retransmits of RPC requests should normally
700          * use the same xid.
701          */
702         if (nmp == NULL) {
703                 timo.tv_usec = 0;
704                 if (clp == NULL)
705                         timo.tv_sec = NFSV4_UPCALLTIMEO;
706                 else
707                         timo.tv_sec = NFSV4_CALLBACKTIMEO;
708         } else {
709                 if (nrp->nr_sotype != SOCK_DGRAM) {
710                         timo.tv_usec = 0;
711                         if ((nmp->nm_flag & NFSMNT_NFSV4))
712                                 timo.tv_sec = INT_MAX;
713                         else
714                                 timo.tv_sec = NFS_TCPTIMEO;
715                 } else {
716                         if (NFSHASSOFT(nmp)) {
717                                 /*
718                                  * CLSET_RETRIES is set to 2, so this should be
719                                  * half of the total timeout required.
720                                  */
721                                 timeo = nmp->nm_retry * nmp->nm_timeo / 2;
722                                 if (timeo < 1)
723                                         timeo = 1;
724                                 timo.tv_sec = timeo / NFS_HZ;
725                                 timo.tv_usec = (timeo % NFS_HZ) * 1000000 /
726                                     NFS_HZ;
727                         } else {
728                                 /* For UDP hard mounts, use a large value. */
729                                 timo.tv_sec = NFS_MAXTIMEO / NFS_HZ;
730                                 timo.tv_usec = 0;
731                         }
732                 }
733
734                 if (rep != NULL) {
735                         rep->r_flags = 0;
736                         rep->r_nmp = nmp;
737                         /*
738                          * Chain request into list of outstanding requests.
739                          */
740                         NFSLOCKREQ();
741                         TAILQ_INSERT_TAIL(&nfsd_reqq, rep, r_chain);
742                         NFSUNLOCKREQ();
743                 }
744         }
745
746         nd->nd_mrep = NULL;
747         if (clp != NULL && sep != NULL)
748                 stat = clnt_bck_call(nrp->nr_client, &ext, procnum,
749                     nd->nd_mreq, &nd->nd_mrep, timo, sep->nfsess_xprt);
750         else
751                 stat = CLNT_CALL_MBUF(nrp->nr_client, &ext, procnum,
752                     nd->nd_mreq, &nd->nd_mrep, timo);
753
754         if (rep != NULL) {
755                 /*
756                  * RPC done, unlink the request.
757                  */
758                 NFSLOCKREQ();
759                 TAILQ_REMOVE(&nfsd_reqq, rep, r_chain);
760                 NFSUNLOCKREQ();
761         }
762
763         /*
764          * If there was a successful reply and a tprintf msg.
765          * tprintf a response.
766          */
767         if (stat == RPC_SUCCESS) {
768                 error = 0;
769         } else if (stat == RPC_TIMEDOUT) {
770                 NFSINCRGLOBAL(nfsstatsv1.rpctimeouts);
771                 error = ETIMEDOUT;
772         } else if (stat == RPC_VERSMISMATCH) {
773                 NFSINCRGLOBAL(nfsstatsv1.rpcinvalid);
774                 error = EOPNOTSUPP;
775         } else if (stat == RPC_PROGVERSMISMATCH) {
776                 NFSINCRGLOBAL(nfsstatsv1.rpcinvalid);
777                 error = EPROTONOSUPPORT;
778         } else if (stat == RPC_INTR) {
779                 error = EINTR;
780         } else {
781                 NFSINCRGLOBAL(nfsstatsv1.rpcinvalid);
782                 error = EACCES;
783         }
784         if (error) {
785                 m_freem(nd->nd_mreq);
786                 if (usegssname == 0)
787                         AUTH_DESTROY(auth);
788                 if (rep != NULL)
789                         FREE((caddr_t)rep, M_NFSDREQ);
790                 if (set_sigset)
791                         newnfs_restore_sigmask(td, &oldset);
792                 return (error);
793         }
794
795         KASSERT(nd->nd_mrep != NULL, ("mrep shouldn't be NULL if no error\n"));
796
797         /*
798          * Search for any mbufs that are not a multiple of 4 bytes long
799          * or with m_data not longword aligned.
800          * These could cause pointer alignment problems, so copy them to
801          * well aligned mbufs.
802          */
803         newnfs_realign(&nd->nd_mrep, M_WAITOK);
804         nd->nd_md = nd->nd_mrep;
805         nd->nd_dpos = NFSMTOD(nd->nd_md, caddr_t);
806         nd->nd_repstat = 0;
807         if (nd->nd_procnum != NFSPROC_NULL &&
808             nd->nd_procnum != NFSV4PROC_CBNULL) {
809                 /* If sep == NULL, set it to the default in nmp. */
810                 if (sep == NULL && nmp != NULL)
811                         sep = nfsmnt_mdssession(nmp);
812                 /*
813                  * and now the actual NFS xdr.
814                  */
815                 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
816                 nd->nd_repstat = fxdr_unsigned(u_int32_t, *tl);
817                 if (nd->nd_repstat >= 10000)
818                         NFSCL_DEBUG(1, "proc=%d reps=%d\n", (int)nd->nd_procnum,
819                             (int)nd->nd_repstat);
820
821                 /*
822                  * Get rid of the tag, return count and SEQUENCE result for
823                  * NFSv4.
824                  */
825                 if ((nd->nd_flag & ND_NFSV4) != 0) {
826                         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
827                         i = fxdr_unsigned(int, *tl);
828                         error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
829                         if (error)
830                                 goto nfsmout;
831                         NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
832                         opcnt = fxdr_unsigned(int, *tl++);
833                         i = fxdr_unsigned(int, *tl++);
834                         j = fxdr_unsigned(int, *tl);
835                         if (j >= 10000)
836                                 NFSCL_DEBUG(1, "fop=%d fst=%d\n", i, j);
837                         /*
838                          * If the first op is Sequence, free up the slot.
839                          */
840                         if ((nmp != NULL && i == NFSV4OP_SEQUENCE && j != 0) ||
841                             (clp != NULL && i == NFSV4OP_CBSEQUENCE && j != 0))
842                                 NFSCL_DEBUG(1, "failed seq=%d\n", j);
843                         if ((nmp != NULL && i == NFSV4OP_SEQUENCE && j == 0) ||
844                             (clp != NULL && i == NFSV4OP_CBSEQUENCE && j == 0)
845                             ) {
846                                 if (i == NFSV4OP_SEQUENCE)
847                                         NFSM_DISSECT(tl, uint32_t *,
848                                             NFSX_V4SESSIONID +
849                                             5 * NFSX_UNSIGNED);
850                                 else
851                                         NFSM_DISSECT(tl, uint32_t *,
852                                             NFSX_V4SESSIONID +
853                                             4 * NFSX_UNSIGNED);
854                                 mtx_lock(&sep->nfsess_mtx);
855                                 if (bcmp(tl, sep->nfsess_sessionid,
856                                     NFSX_V4SESSIONID) == 0) {
857                                         tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
858                                         retseq = fxdr_unsigned(uint32_t, *tl++);
859                                         slot = fxdr_unsigned(int, *tl++);
860                                         freeslot = slot;
861                                         if (retseq != sep->nfsess_slotseq[slot])
862                                                 printf("retseq diff 0x%x\n",
863                                                     retseq);
864                                         retval = fxdr_unsigned(uint32_t, *++tl);
865                                         if ((retval + 1) < sep->nfsess_foreslots
866                                             )
867                                                 sep->nfsess_foreslots = (retval
868                                                     + 1);
869                                         else if ((retval + 1) >
870                                             sep->nfsess_foreslots)
871                                                 sep->nfsess_foreslots = (retval
872                                                     < 64) ? (retval + 1) : 64;
873                                 }
874                                 mtx_unlock(&sep->nfsess_mtx);
875
876                                 /* Grab the op and status for the next one. */
877                                 if (opcnt > 1) {
878                                         NFSM_DISSECT(tl, uint32_t *,
879                                             2 * NFSX_UNSIGNED);
880                                         i = fxdr_unsigned(int, *tl++);
881                                         j = fxdr_unsigned(int, *tl);
882                                 }
883                         }
884                 }
885                 if (nd->nd_repstat != 0) {
886                         if (nd->nd_repstat == NFSERR_BADSESSION &&
887                             nmp != NULL && dssep == NULL) {
888                                 /*
889                                  * If this is a client side MDS RPC, mark
890                                  * the MDS session defunct and initiate
891                                  * recovery, as required.
892                                  * The nfsess_defunct field is protected by
893                                  * the NFSLOCKMNT()/nm_mtx lock and not the
894                                  * nfsess_mtx lock to simplify its handling,
895                                  * for the MDS session. This lock is also
896                                  * sufficient for nfsess_sessionid, since it
897                                  * never changes in the structure.
898                                  */
899                                 NFSCL_DEBUG(1, "Got badsession\n");
900                                 NFSLOCKCLSTATE();
901                                 NFSLOCKMNT(nmp);
902                                 sep = NFSMNT_MDSSESSION(nmp);
903                                 if (bcmp(sep->nfsess_sessionid, nd->nd_sequence,
904                                     NFSX_V4SESSIONID) == 0) {
905                                         /* Initiate recovery. */
906                                         sep->nfsess_defunct = 1;
907                                         NFSCL_DEBUG(1, "Marked defunct\n");
908                                         if (nmp->nm_clp != NULL) {
909                                                 nmp->nm_clp->nfsc_flags |=
910                                                     NFSCLFLAGS_RECOVER;
911                                                 wakeup(nmp->nm_clp);
912                                         }
913                                 }
914                                 NFSUNLOCKCLSTATE();
915                                 /*
916                                  * Sleep for up to 1sec waiting for a new
917                                  * session.
918                                  */
919                                 mtx_sleep(&nmp->nm_sess, &nmp->nm_mtx, PZERO,
920                                     "nfsbadsess", hz);
921                                 /*
922                                  * Get the session again, in case a new one
923                                  * has been created during the sleep.
924                                  */
925                                 sep = NFSMNT_MDSSESSION(nmp);
926                                 NFSUNLOCKMNT(nmp);
927                                 if ((nd->nd_flag & ND_LOOPBADSESS) != 0) {
928                                         reterr = nfsv4_sequencelookup(nmp, sep,
929                                             &slotpos, &maxslot, &slotseq,
930                                             sessionid);
931                                         if (reterr == 0) {
932                                                 /* Fill in new session info. */
933                                                 NFSCL_DEBUG(1,
934                                                   "Filling in new sequence\n");
935                                                 tl = nd->nd_sequence;
936                                                 bcopy(sessionid, tl,
937                                                     NFSX_V4SESSIONID);
938                                                 tl += NFSX_V4SESSIONID /
939                                                     NFSX_UNSIGNED;
940                                                 *tl++ = txdr_unsigned(slotseq);
941                                                 *tl++ = txdr_unsigned(slotpos);
942                                                 *tl = txdr_unsigned(maxslot);
943                                         }
944                                         if (reterr == NFSERR_BADSESSION ||
945                                             reterr == 0) {
946                                                 NFSCL_DEBUG(1,
947                                                     "Badsession looping\n");
948                                                 m_freem(nd->nd_mrep);
949                                                 nd->nd_mrep = NULL;
950                                                 goto tryagain;
951                                         }
952                                         nd->nd_repstat = reterr;
953                                         NFSCL_DEBUG(1, "Got err=%d\n", reterr);
954                                 }
955                         }
956                         if (((nd->nd_repstat == NFSERR_DELAY ||
957                               nd->nd_repstat == NFSERR_GRACE) &&
958                              (nd->nd_flag & ND_NFSV4) &&
959                              nd->nd_procnum != NFSPROC_DELEGRETURN &&
960                              nd->nd_procnum != NFSPROC_SETATTR &&
961                              nd->nd_procnum != NFSPROC_READ &&
962                              nd->nd_procnum != NFSPROC_READDS &&
963                              nd->nd_procnum != NFSPROC_WRITE &&
964                              nd->nd_procnum != NFSPROC_WRITEDS &&
965                              nd->nd_procnum != NFSPROC_OPEN &&
966                              nd->nd_procnum != NFSPROC_CREATE &&
967                              nd->nd_procnum != NFSPROC_OPENCONFIRM &&
968                              nd->nd_procnum != NFSPROC_OPENDOWNGRADE &&
969                              nd->nd_procnum != NFSPROC_CLOSE &&
970                              nd->nd_procnum != NFSPROC_LOCK &&
971                              nd->nd_procnum != NFSPROC_LOCKU) ||
972                             (nd->nd_repstat == NFSERR_DELAY &&
973                              (nd->nd_flag & ND_NFSV4) == 0) ||
974                             nd->nd_repstat == NFSERR_RESOURCE) {
975                                 if (trylater_delay > NFS_TRYLATERDEL)
976                                         trylater_delay = NFS_TRYLATERDEL;
977                                 waituntil = NFSD_MONOSEC + trylater_delay;
978                                 while (NFSD_MONOSEC < waituntil)
979                                         (void) nfs_catnap(PZERO, 0, "nfstry");
980                                 trylater_delay *= 2;
981                                 if (slot != -1) {
982                                         mtx_lock(&sep->nfsess_mtx);
983                                         sep->nfsess_slotseq[slot]++;
984                                         *nd->nd_slotseq = txdr_unsigned(
985                                             sep->nfsess_slotseq[slot]);
986                                         mtx_unlock(&sep->nfsess_mtx);
987                                 }
988                                 m_freem(nd->nd_mrep);
989                                 nd->nd_mrep = NULL;
990                                 goto tryagain;
991                         }
992
993                         /*
994                          * If the File Handle was stale, invalidate the
995                          * lookup cache, just in case.
996                          * (vp != NULL implies a client side call)
997                          */
998                         if (nd->nd_repstat == ESTALE && vp != NULL) {
999                                 cache_purge(vp);
1000                                 if (ncl_call_invalcaches != NULL)
1001                                         (*ncl_call_invalcaches)(vp);
1002                         }
1003                 }
1004                 if ((nd->nd_flag & ND_NFSV4) != 0) {
1005                         /* Free the slot, as required. */
1006                         if (freeslot != -1)
1007                                 nfsv4_freeslot(sep, freeslot);
1008                         /*
1009                          * If this op is Putfh, throw its results away.
1010                          */
1011                         if (j >= 10000)
1012                                 NFSCL_DEBUG(1, "nop=%d nst=%d\n", i, j);
1013                         if (nmp != NULL && i == NFSV4OP_PUTFH && j == 0) {
1014                                 NFSM_DISSECT(tl,u_int32_t *,2 * NFSX_UNSIGNED);
1015                                 i = fxdr_unsigned(int, *tl++);
1016                                 j = fxdr_unsigned(int, *tl);
1017                                 if (j >= 10000)
1018                                         NFSCL_DEBUG(1, "n2op=%d n2st=%d\n", i,
1019                                             j);
1020                                 /*
1021                                  * All Compounds that do an Op that must
1022                                  * be in sequence consist of NFSV4OP_PUTFH
1023                                  * followed by one of these. As such, we
1024                                  * can determine if the seqid# should be
1025                                  * incremented, here.
1026                                  */
1027                                 if ((i == NFSV4OP_OPEN ||
1028                                      i == NFSV4OP_OPENCONFIRM ||
1029                                      i == NFSV4OP_OPENDOWNGRADE ||
1030                                      i == NFSV4OP_CLOSE ||
1031                                      i == NFSV4OP_LOCK ||
1032                                      i == NFSV4OP_LOCKU) &&
1033                                     (j == 0 ||
1034                                      (j != NFSERR_STALECLIENTID &&
1035                                       j != NFSERR_STALESTATEID &&
1036                                       j != NFSERR_BADSTATEID &&
1037                                       j != NFSERR_BADSEQID &&
1038                                       j != NFSERR_BADXDR &&      
1039                                       j != NFSERR_RESOURCE &&
1040                                       j != NFSERR_NOFILEHANDLE)))                
1041                                         nd->nd_flag |= ND_INCRSEQID;
1042                         }
1043                         /*
1044                          * If this op's status is non-zero, mark
1045                          * that there is no more data to process.
1046                          * The exception is Setattr, which always has xdr
1047                          * when it has failed.
1048                          */
1049                         if (j != 0 && i != NFSV4OP_SETATTR)
1050                                 nd->nd_flag |= ND_NOMOREDATA;
1051
1052                         /*
1053                          * If R_DONTRECOVER is set, replace the stale error
1054                          * reply, so that recovery isn't initiated.
1055                          */
1056                         if ((nd->nd_repstat == NFSERR_STALECLIENTID ||
1057                              nd->nd_repstat == NFSERR_BADSESSION ||
1058                              nd->nd_repstat == NFSERR_STALESTATEID) &&
1059                             rep != NULL && (rep->r_flags & R_DONTRECOVER))
1060                                 nd->nd_repstat = NFSERR_STALEDONTRECOVER;
1061                 }
1062         }
1063
1064 #ifdef KDTRACE_HOOKS
1065         if (nmp != NULL && dtrace_nfscl_nfs234_done_probe != NULL) {
1066                 uint32_t probe_id;
1067                 int probe_procnum;
1068
1069                 if (nd->nd_flag & ND_NFSV4) {
1070                         probe_id = nfscl_nfs4_done_probes[nd->nd_procnum];
1071                         probe_procnum = nd->nd_procnum;
1072                 } else if (nd->nd_flag & ND_NFSV3) {
1073                         probe_id = nfscl_nfs3_done_probes[procnum];
1074                         probe_procnum = procnum;
1075                 } else {
1076                         probe_id = nfscl_nfs2_done_probes[nd->nd_procnum];
1077                         probe_procnum = procnum;
1078                 }
1079                 if (probe_id != 0)
1080                         (dtrace_nfscl_nfs234_done_probe)(probe_id, vp,
1081                             nd->nd_mreq, cred, probe_procnum, 0);
1082         }
1083 #endif
1084
1085         m_freem(nd->nd_mreq);
1086         if (usegssname == 0)
1087                 AUTH_DESTROY(auth);
1088         if (rep != NULL)
1089                 FREE((caddr_t)rep, M_NFSDREQ);
1090         if (set_sigset)
1091                 newnfs_restore_sigmask(td, &oldset);
1092         return (0);
1093 nfsmout:
1094         mbuf_freem(nd->nd_mrep);
1095         mbuf_freem(nd->nd_mreq);
1096         if (usegssname == 0)
1097                 AUTH_DESTROY(auth);
1098         if (rep != NULL)
1099                 FREE((caddr_t)rep, M_NFSDREQ);
1100         if (set_sigset)
1101                 newnfs_restore_sigmask(td, &oldset);
1102         return (error);
1103 }
1104
1105 /*
1106  * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
1107  * wait for all requests to complete. This is used by forced unmounts
1108  * to terminate any outstanding RPCs.
1109  */
1110 int
1111 newnfs_nmcancelreqs(struct nfsmount *nmp)
1112 {
1113
1114         if (nmp->nm_sockreq.nr_client != NULL)
1115                 CLNT_CLOSE(nmp->nm_sockreq.nr_client);
1116         return (0);
1117 }
1118
1119 /*
1120  * Any signal that can interrupt an NFS operation in an intr mount
1121  * should be added to this set. SIGSTOP and SIGKILL cannot be masked.
1122  */
1123 int newnfs_sig_set[] = {
1124         SIGINT,
1125         SIGTERM,
1126         SIGHUP,
1127         SIGKILL,
1128         SIGQUIT
1129 };
1130
1131 /*
1132  * Check to see if one of the signals in our subset is pending on
1133  * the process (in an intr mount).
1134  */
1135 static int
1136 nfs_sig_pending(sigset_t set)
1137 {
1138         int i;
1139         
1140         for (i = 0 ; i < nitems(newnfs_sig_set); i++)
1141                 if (SIGISMEMBER(set, newnfs_sig_set[i]))
1142                         return (1);
1143         return (0);
1144 }
1145  
1146 /*
1147  * The set/restore sigmask functions are used to (temporarily) overwrite
1148  * the thread td_sigmask during an RPC call (for example). These are also
1149  * used in other places in the NFS client that might tsleep().
1150  */
1151 void
1152 newnfs_set_sigmask(struct thread *td, sigset_t *oldset)
1153 {
1154         sigset_t newset;
1155         int i;
1156         struct proc *p;
1157         
1158         SIGFILLSET(newset);
1159         if (td == NULL)
1160                 td = curthread; /* XXX */
1161         p = td->td_proc;
1162         /* Remove the NFS set of signals from newset */
1163         PROC_LOCK(p);
1164         mtx_lock(&p->p_sigacts->ps_mtx);
1165         for (i = 0 ; i < nitems(newnfs_sig_set); i++) {
1166                 /*
1167                  * But make sure we leave the ones already masked
1168                  * by the process, ie. remove the signal from the
1169                  * temporary signalmask only if it wasn't already
1170                  * in p_sigmask.
1171                  */
1172                 if (!SIGISMEMBER(td->td_sigmask, newnfs_sig_set[i]) &&
1173                     !SIGISMEMBER(p->p_sigacts->ps_sigignore, newnfs_sig_set[i]))
1174                         SIGDELSET(newset, newnfs_sig_set[i]);
1175         }
1176         mtx_unlock(&p->p_sigacts->ps_mtx);
1177         kern_sigprocmask(td, SIG_SETMASK, &newset, oldset,
1178             SIGPROCMASK_PROC_LOCKED);
1179         PROC_UNLOCK(p);
1180 }
1181
1182 void
1183 newnfs_restore_sigmask(struct thread *td, sigset_t *set)
1184 {
1185         if (td == NULL)
1186                 td = curthread; /* XXX */
1187         kern_sigprocmask(td, SIG_SETMASK, set, NULL, 0);
1188 }
1189
1190 /*
1191  * NFS wrapper to msleep(), that shoves a new p_sigmask and restores the
1192  * old one after msleep() returns.
1193  */
1194 int
1195 newnfs_msleep(struct thread *td, void *ident, struct mtx *mtx, int priority, char *wmesg, int timo)
1196 {
1197         sigset_t oldset;
1198         int error;
1199         struct proc *p;
1200         
1201         if ((priority & PCATCH) == 0)
1202                 return msleep(ident, mtx, priority, wmesg, timo);
1203         if (td == NULL)
1204                 td = curthread; /* XXX */
1205         newnfs_set_sigmask(td, &oldset);
1206         error = msleep(ident, mtx, priority, wmesg, timo);
1207         newnfs_restore_sigmask(td, &oldset);
1208         p = td->td_proc;
1209         return (error);
1210 }
1211
1212 /*
1213  * Test for a termination condition pending on the process.
1214  * This is used for NFSMNT_INT mounts.
1215  */
1216 int
1217 newnfs_sigintr(struct nfsmount *nmp, struct thread *td)
1218 {
1219         struct proc *p;
1220         sigset_t tmpset;
1221         
1222         /* Terminate all requests while attempting a forced unmount. */
1223         if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)
1224                 return (EIO);
1225         if (!(nmp->nm_flag & NFSMNT_INT))
1226                 return (0);
1227         if (td == NULL)
1228                 return (0);
1229         p = td->td_proc;
1230         PROC_LOCK(p);
1231         tmpset = p->p_siglist;
1232         SIGSETOR(tmpset, td->td_siglist);
1233         SIGSETNAND(tmpset, td->td_sigmask);
1234         mtx_lock(&p->p_sigacts->ps_mtx);
1235         SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore);
1236         mtx_unlock(&p->p_sigacts->ps_mtx);
1237         if ((SIGNOTEMPTY(p->p_siglist) || SIGNOTEMPTY(td->td_siglist))
1238             && nfs_sig_pending(tmpset)) {
1239                 PROC_UNLOCK(p);
1240                 return (EINTR);
1241         }
1242         PROC_UNLOCK(p);
1243         return (0);
1244 }
1245
1246 static int
1247 nfs_msg(struct thread *td, const char *server, const char *msg, int error)
1248 {
1249         struct proc *p;
1250
1251         p = td ? td->td_proc : NULL;
1252         if (error) {
1253                 tprintf(p, LOG_INFO, "nfs server %s: %s, error %d\n",
1254                     server, msg, error);
1255         } else {
1256                 tprintf(p, LOG_INFO, "nfs server %s: %s\n", server, msg);
1257         }
1258         return (0);
1259 }
1260
1261 static void
1262 nfs_down(struct nfsmount *nmp, struct thread *td, const char *msg,
1263     int error, int flags)
1264 {
1265         if (nmp == NULL)
1266                 return;
1267         mtx_lock(&nmp->nm_mtx);
1268         if ((flags & NFSSTA_TIMEO) && !(nmp->nm_state & NFSSTA_TIMEO)) {
1269                 nmp->nm_state |= NFSSTA_TIMEO;
1270                 mtx_unlock(&nmp->nm_mtx);
1271                 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1272                     VQ_NOTRESP, 0);
1273         } else
1274                 mtx_unlock(&nmp->nm_mtx);
1275         mtx_lock(&nmp->nm_mtx);
1276         if ((flags & NFSSTA_LOCKTIMEO) && !(nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1277                 nmp->nm_state |= NFSSTA_LOCKTIMEO;
1278                 mtx_unlock(&nmp->nm_mtx);
1279                 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1280                     VQ_NOTRESPLOCK, 0);
1281         } else
1282                 mtx_unlock(&nmp->nm_mtx);
1283         nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error);
1284 }
1285
1286 static void
1287 nfs_up(struct nfsmount *nmp, struct thread *td, const char *msg,
1288     int flags, int tprintfmsg)
1289 {
1290         if (nmp == NULL)
1291                 return;
1292         if (tprintfmsg) {
1293                 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0);
1294         }
1295
1296         mtx_lock(&nmp->nm_mtx);
1297         if ((flags & NFSSTA_TIMEO) && (nmp->nm_state & NFSSTA_TIMEO)) {
1298                 nmp->nm_state &= ~NFSSTA_TIMEO;
1299                 mtx_unlock(&nmp->nm_mtx);
1300                 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1301                     VQ_NOTRESP, 1);
1302         } else
1303                 mtx_unlock(&nmp->nm_mtx);
1304         
1305         mtx_lock(&nmp->nm_mtx);
1306         if ((flags & NFSSTA_LOCKTIMEO) && (nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1307                 nmp->nm_state &= ~NFSSTA_LOCKTIMEO;
1308                 mtx_unlock(&nmp->nm_mtx);
1309                 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1310                     VQ_NOTRESPLOCK, 1);
1311         } else
1312                 mtx_unlock(&nmp->nm_mtx);
1313 }
1314