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