1 /*-
2 * Copyright (c) 1989, 1991, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD: releng/8.4/sys/fs/nfs/nfs_commonkrpc.c 247503 2013-02-28 21:58:07Z jhb $");
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
62 #include <kgssapi/krb5/kcrypto.h>
63
64 #include <fs/nfs/nfsport.h>
65
66 NFSSTATESPINLOCK;
67 NFSREQSPINLOCK;
68 extern struct nfsstats newnfsstats;
69 extern struct nfsreqhead nfsd_reqq;
70 extern int nfscl_ticks;
71 extern void (*ncl_call_invalcaches)(struct vnode *);
72
73 static int nfsrv_gsscallbackson = 0;
74 static int nfs_bufpackets = 4;
75 static int nfs_reconnects;
76 static int nfs3_jukebox_delay = 10;
77 static int nfs_skip_wcc_data_onerr = 1;
78 static int nfs_keytab_enctype = ETYPE_DES_CBC_CRC;
79
80 SYSCTL_DECL(_vfs_newnfs);
81
82 SYSCTL_INT(_vfs_newnfs, OID_AUTO, bufpackets, CTLFLAG_RW, &nfs_bufpackets, 0,
83 "Buffer reservation size 2 < x < 64");
84 SYSCTL_INT(_vfs_newnfs, OID_AUTO, reconnects, CTLFLAG_RD, &nfs_reconnects, 0,
85 "Number of times the nfs client has had to reconnect");
86 SYSCTL_INT(_vfs_newnfs, OID_AUTO, nfs3_jukebox_delay, CTLFLAG_RW, &nfs3_jukebox_delay, 0,
87 "Number of seconds to delay a retry after receiving EJUKEBOX");
88 SYSCTL_INT(_vfs_newnfs, OID_AUTO, skip_wcc_data_onerr, CTLFLAG_RW, &nfs_skip_wcc_data_onerr, 0,
89 "Disable weak cache consistency checking when server returns an error");
90 SYSCTL_INT(_vfs_newnfs, OID_AUTO, keytab_enctype, CTLFLAG_RW, &nfs_keytab_enctype, 0,
91 "Encryption type for the keytab entry used by nfs");
92
93 static void nfs_down(struct nfsmount *, struct thread *, const char *,
94 int, int);
95 static void nfs_up(struct nfsmount *, struct thread *, const char *,
96 int, int);
97 static int nfs_msg(struct thread *, const char *, const char *, int);
98
99 struct nfs_cached_auth {
100 int ca_refs; /* refcount, including 1 from the cache */
101 uid_t ca_uid; /* uid that corresponds to this auth */
102 AUTH *ca_auth; /* RPC auth handle */
103 };
104
105 static int nfsv2_procid[NFS_V3NPROCS] = {
106 NFSV2PROC_NULL,
107 NFSV2PROC_GETATTR,
108 NFSV2PROC_SETATTR,
109 NFSV2PROC_LOOKUP,
110 NFSV2PROC_NOOP,
111 NFSV2PROC_READLINK,
112 NFSV2PROC_READ,
113 NFSV2PROC_WRITE,
114 NFSV2PROC_CREATE,
115 NFSV2PROC_MKDIR,
116 NFSV2PROC_SYMLINK,
117 NFSV2PROC_CREATE,
118 NFSV2PROC_REMOVE,
119 NFSV2PROC_RMDIR,
120 NFSV2PROC_RENAME,
121 NFSV2PROC_LINK,
122 NFSV2PROC_READDIR,
123 NFSV2PROC_NOOP,
124 NFSV2PROC_STATFS,
125 NFSV2PROC_NOOP,
126 NFSV2PROC_NOOP,
127 NFSV2PROC_NOOP,
128 };
129
130 /*
131 * Initialize sockets and congestion for a new NFS connection.
132 * We do not free the sockaddr if error.
133 */
134 int
135 newnfs_connect(struct nfsmount *nmp, struct nfssockreq *nrp,
136 struct ucred *cred, NFSPROC_T *p, int callback_retry_mult)
137 {
138 int rcvreserve, sndreserve;
139 int pktscale;
140 struct sockaddr *saddr;
141 struct ucred *origcred;
142 CLIENT *client;
143 struct netconfig *nconf;
144 struct socket *so;
145 int one = 1, retries, error = 0;
146 struct thread *td = curthread;
147 struct timeval timo;
148
149 /*
150 * We need to establish the socket using the credentials of
151 * the mountpoint. Some parts of this process (such as
152 * sobind() and soconnect()) will use the curent thread's
153 * credential instead of the socket credential. To work
154 * around this, temporarily change the current thread's
155 * credential to that of the mountpoint.
156 *
157 * XXX: It would be better to explicitly pass the correct
158 * credential to sobind() and soconnect().
159 */
160 origcred = td->td_ucred;
161
162 /*
163 * Use the credential in nr_cred, if not NULL.
164 */
165 if (nrp->nr_cred != NULL)
166 td->td_ucred = nrp->nr_cred;
167 else
168 td->td_ucred = cred;
169 saddr = nrp->nr_nam;
170
171 if (saddr->sa_family == AF_INET)
172 if (nrp->nr_sotype == SOCK_DGRAM)
173 nconf = getnetconfigent("udp");
174 else
175 nconf = getnetconfigent("tcp");
176 else
177 if (nrp->nr_sotype == SOCK_DGRAM)
178 nconf = getnetconfigent("udp6");
179 else
180 nconf = getnetconfigent("tcp6");
181
182 pktscale = nfs_bufpackets;
183 if (pktscale < 2)
184 pktscale = 2;
185 if (pktscale > 64)
186 pktscale = 64;
187 /*
188 * soreserve() can fail if sb_max is too small, so shrink pktscale
189 * and try again if there is an error.
190 * Print a log message suggesting increasing sb_max.
191 * Creating a socket and doing this is necessary since, if the
192 * reservation sizes are too large and will make soreserve() fail,
193 * the connection will work until a large send is attempted and
194 * then it will loop in the krpc code.
195 */
196 so = NULL;
197 saddr = NFSSOCKADDR(nrp->nr_nam, struct sockaddr *);
198 error = socreate(saddr->sa_family, &so, nrp->nr_sotype,
199 nrp->nr_soproto, td->td_ucred, td);
200 if (error) {
201 td->td_ucred = origcred;
202 goto out;
203 }
204 do {
205 if (error != 0 && pktscale > 2)
206 pktscale--;
207 if (nrp->nr_sotype == SOCK_DGRAM) {
208 if (nmp != NULL) {
209 sndreserve = (NFS_MAXDGRAMDATA + NFS_MAXPKTHDR) *
210 pktscale;
211 rcvreserve = (NFS_MAXDGRAMDATA + NFS_MAXPKTHDR) *
212 pktscale;
213 } else {
214 sndreserve = rcvreserve = 1024 * pktscale;
215 }
216 } else {
217 if (nrp->nr_sotype != SOCK_STREAM)
218 panic("nfscon sotype");
219 if (nmp != NULL) {
220 sndreserve = (NFS_MAXBSIZE + NFS_MAXPKTHDR +
221 sizeof (u_int32_t)) * pktscale;
222 rcvreserve = (NFS_MAXBSIZE + NFS_MAXPKTHDR +
223 sizeof (u_int32_t)) * pktscale;
224 } else {
225 sndreserve = rcvreserve = 1024 * pktscale;
226 }
227 }
228 error = soreserve(so, sndreserve, rcvreserve);
229 } while (error != 0 && pktscale > 2);
230 soclose(so);
231 if (error) {
232 td->td_ucred = origcred;
233 goto out;
234 }
235
236 client = clnt_reconnect_create(nconf, saddr, nrp->nr_prog,
237 nrp->nr_vers, sndreserve, rcvreserve);
238 CLNT_CONTROL(client, CLSET_WAITCHAN, "newnfsreq");
239 if (nmp != NULL) {
240 if ((nmp->nm_flag & NFSMNT_INT))
241 CLNT_CONTROL(client, CLSET_INTERRUPTIBLE, &one);
242 if ((nmp->nm_flag & NFSMNT_RESVPORT))
243 CLNT_CONTROL(client, CLSET_PRIVPORT, &one);
244 if (NFSHASSOFT(nmp)) {
245 if (nmp->nm_sotype == SOCK_DGRAM)
246 /*
247 * For UDP, the large timeout for a reconnect
248 * will be set to "nm_retry * nm_timeo / 2", so
249 * we only want to do 2 reconnect timeout
250 * retries.
251 */
252 retries = 2;
253 else
254 retries = nmp->nm_retry;
255 } else
256 retries = INT_MAX;
257 } else {
258 /*
259 * Three cases:
260 * - Null RPC callback to client
261 * - Non-Null RPC callback to client, wait a little longer
262 * - upcalls to nfsuserd and gssd (clp == NULL)
263 */
264 if (callback_retry_mult == 0) {
265 retries = NFSV4_UPCALLRETRY;
266 CLNT_CONTROL(client, CLSET_PRIVPORT, &one);
267 } else {
268 retries = NFSV4_CALLBACKRETRY * callback_retry_mult;
269 }
270 }
271 CLNT_CONTROL(client, CLSET_RETRIES, &retries);
272
273 if (nmp != NULL) {
274 /*
275 * For UDP, there are 2 timeouts:
276 * - CLSET_RETRY_TIMEOUT sets the initial timeout for the timer
277 * that does a retransmit of an RPC request using the same
278 * socket and xid. This is what you normally want to do,
279 * since NFS servers depend on "same xid" for their
280 * Duplicate Request Cache.
281 * - timeout specified in CLNT_CALL_MBUF(), which specifies when
282 * retransmits on the same socket should fail and a fresh
283 * socket created. Each of these timeouts counts as one
284 * CLSET_RETRIES as set above.
285 * Set the initial retransmit timeout for UDP. This timeout
286 * doesn't exist for TCP and the following call just fails,
287 * which is ok.
288 */
289 timo.tv_sec = nmp->nm_timeo / NFS_HZ;
290 timo.tv_usec = (nmp->nm_timeo % NFS_HZ) * 1000000 / NFS_HZ;
291 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, &timo);
292 }
293
294 mtx_lock(&nrp->nr_mtx);
295 if (nrp->nr_client != NULL) {
296 /*
297 * Someone else already connected.
298 */
299 CLNT_RELEASE(client);
300 } else {
301 nrp->nr_client = client;
302 }
303
304 /*
305 * Protocols that do not require connections may be optionally left
306 * unconnected for servers that reply from a port other than NFS_PORT.
307 */
308 if (nmp == NULL || (nmp->nm_flag & NFSMNT_NOCONN) == 0) {
309 mtx_unlock(&nrp->nr_mtx);
310 CLNT_CONTROL(client, CLSET_CONNECT, &one);
311 } else {
312 mtx_unlock(&nrp->nr_mtx);
313 }
314
315 /* Restore current thread's credentials. */
316 td->td_ucred = origcred;
317
318 out:
319 NFSEXITCODE(error);
320 return (error);
321 }
322
323 /*
324 * NFS disconnect. Clean up and unlink.
325 */
326 void
327 newnfs_disconnect(struct nfssockreq *nrp)
328 {
329 CLIENT *client;
330
331 mtx_lock(&nrp->nr_mtx);
332 if (nrp->nr_client != NULL) {
333 client = nrp->nr_client;
334 nrp->nr_client = NULL;
335 mtx_unlock(&nrp->nr_mtx);
336 rpc_gss_secpurge_call(client);
337 CLNT_CLOSE(client);
338 CLNT_RELEASE(client);
339 } else {
340 mtx_unlock(&nrp->nr_mtx);
341 }
342 }
343
344 static AUTH *
345 nfs_getauth(struct nfssockreq *nrp, int secflavour, char *clnt_principal,
346 char *srv_principal, gss_OID mech_oid, struct ucred *cred)
347 {
348 rpc_gss_service_t svc;
349 AUTH *auth;
350 #ifdef notyet
351 rpc_gss_options_req_t req_options;
352 #endif
353
354 switch (secflavour) {
355 case RPCSEC_GSS_KRB5:
356 case RPCSEC_GSS_KRB5I:
357 case RPCSEC_GSS_KRB5P:
358 if (!mech_oid) {
359 if (!rpc_gss_mech_to_oid_call("kerberosv5", &mech_oid))
360 return (NULL);
361 }
362 if (secflavour == RPCSEC_GSS_KRB5)
363 svc = rpc_gss_svc_none;
364 else if (secflavour == RPCSEC_GSS_KRB5I)
365 svc = rpc_gss_svc_integrity;
366 else
367 svc = rpc_gss_svc_privacy;
368 #ifdef notyet
369 req_options.req_flags = GSS_C_MUTUAL_FLAG;
370 req_options.time_req = 0;
371 req_options.my_cred = GSS_C_NO_CREDENTIAL;
372 req_options.input_channel_bindings = NULL;
373 req_options.enc_type = nfs_keytab_enctype;
374
375 auth = rpc_gss_secfind_call(nrp->nr_client, cred,
376 clnt_principal, srv_principal, mech_oid, svc,
377 &req_options);
378 #else
379 /*
380 * Until changes to the rpcsec_gss code are committed,
381 * there is no support for host based initiator
382 * principals. As such, that case cannot yet be handled.
383 */
384 if (clnt_principal == NULL)
385 auth = rpc_gss_secfind_call(nrp->nr_client, cred,
386 srv_principal, mech_oid, svc);
387 else
388 auth = NULL;
389 #endif
390 if (auth != NULL)
391 return (auth);
392 /* fallthrough */
393 case AUTH_SYS:
394 default:
395 return (authunix_create(cred));
396
397 }
398 }
399
400 /*
401 * Callback from the RPC code to generate up/down notifications.
402 */
403
404 struct nfs_feedback_arg {
405 struct nfsmount *nf_mount;
406 int nf_lastmsg; /* last tprintf */
407 int nf_tprintfmsg;
408 struct thread *nf_td;
409 };
410
411 static void
412 nfs_feedback(int type, int proc, void *arg)
413 {
414 struct nfs_feedback_arg *nf = (struct nfs_feedback_arg *) arg;
415 struct nfsmount *nmp = nf->nf_mount;
416 time_t now;
417
418 switch (type) {
419 case FEEDBACK_REXMIT2:
420 case FEEDBACK_RECONNECT:
421 now = NFSD_MONOSEC;
422 if (nf->nf_lastmsg + nmp->nm_tprintf_delay < now) {
423 nfs_down(nmp, nf->nf_td,
424 "not responding", 0, NFSSTA_TIMEO);
425 nf->nf_tprintfmsg = TRUE;
426 nf->nf_lastmsg = now;
427 }
428 break;
429
430 case FEEDBACK_OK:
431 nfs_up(nf->nf_mount, nf->nf_td,
432 "is alive again", NFSSTA_TIMEO, nf->nf_tprintfmsg);
433 break;
434 }
435 }
436
437 /*
438 * newnfs_request - goes something like this
439 * - does the rpc by calling the krpc layer
440 * - break down rpc header and return with nfs reply
441 * nb: always frees up nd_mreq mbuf list
442 */
443 int
444 newnfs_request(struct nfsrv_descript *nd, struct nfsmount *nmp,
445 struct nfsclient *clp, struct nfssockreq *nrp, vnode_t vp,
446 struct thread *td, struct ucred *cred, u_int32_t prog, u_int32_t vers,
447 u_char *retsum, int toplevel, u_int64_t *xidp)
448 {
449 u_int32_t *tl;
450 time_t waituntil;
451 int i, j, set_sigset = 0, timeo;
452 int trycnt, error = 0, usegssname = 0, secflavour = AUTH_SYS;
453 u_int16_t procnum;
454 u_int trylater_delay = 1;
455 struct nfs_feedback_arg nf;
456 struct timeval timo;
457 AUTH *auth;
458 struct rpc_callextra ext;
459 enum clnt_stat stat;
460 struct nfsreq *rep = NULL;
461 char *srv_principal = NULL;
462 sigset_t oldset;
463 struct ucred *authcred;
464
465 if (xidp != NULL)
466 *xidp = 0;
467 /* Reject requests while attempting a forced unmount. */
468 if (nmp != NULL && (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)) {
469 m_freem(nd->nd_mreq);
470 return (ESTALE);
471 }
472
473 /*
474 * Set authcred, which is used to acquire RPC credentials to
475 * the cred argument, by default. The crhold() should not be
476 * necessary, but will ensure that some future code change
477 * doesn't result in the credential being free'd prematurely.
478 */
479 authcred = crhold(cred);
480
481 /* For client side interruptible mounts, mask off the signals. */
482 if (nmp != NULL && td != NULL && NFSHASINT(nmp)) {
483 newnfs_set_sigmask(td, &oldset);
484 set_sigset = 1;
485 }
486
487 /*
488 * XXX if not already connected call nfs_connect now. Longer
489 * term, change nfs_mount to call nfs_connect unconditionally
490 * and let clnt_reconnect_create handle reconnects.
491 */
492 if (nrp->nr_client == NULL)
493 newnfs_connect(nmp, nrp, cred, td, 0);
494
495 /*
496 * For a client side mount, nmp is != NULL and clp == NULL. For
497 * server calls (callbacks or upcalls), nmp == NULL.
498 */
499 if (clp != NULL) {
500 NFSLOCKSTATE();
501 if ((clp->lc_flags & LCL_GSS) && nfsrv_gsscallbackson) {
502 secflavour = RPCSEC_GSS_KRB5;
503 if (nd->nd_procnum != NFSPROC_NULL) {
504 if (clp->lc_flags & LCL_GSSINTEGRITY)
505 secflavour = RPCSEC_GSS_KRB5I;
506 else if (clp->lc_flags & LCL_GSSPRIVACY)
507 secflavour = RPCSEC_GSS_KRB5P;
508 }
509 }
510 NFSUNLOCKSTATE();
511 } else if (nmp != NULL && NFSHASKERB(nmp) &&
512 nd->nd_procnum != NFSPROC_NULL) {
513 if (NFSHASALLGSSNAME(nmp) && nmp->nm_krbnamelen > 0)
514 nd->nd_flag |= ND_USEGSSNAME;
515 if ((nd->nd_flag & ND_USEGSSNAME) != 0) {
516 /*
517 * If there is a client side host based credential,
518 * use that, otherwise use the system uid, if set.
519 * The system uid is in the nmp->nm_sockreq.nr_cred
520 * credentials.
521 */
522 if (nmp->nm_krbnamelen > 0) {
523 usegssname = 1;
524 } else if (nmp->nm_uid != (uid_t)-1) {
525 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
526 ("newnfs_request: NULL nr_cred"));
527 crfree(authcred);
528 authcred = crhold(nmp->nm_sockreq.nr_cred);
529 }
530 } else if (nmp->nm_krbnamelen == 0 &&
531 nmp->nm_uid != (uid_t)-1 && cred->cr_uid == (uid_t)0) {
532 /*
533 * If there is no host based principal name and
534 * the system uid is set and this is root, use the
535 * system uid, since root won't have user
536 * credentials in a credentials cache file.
537 * The system uid is in the nmp->nm_sockreq.nr_cred
538 * credentials.
539 */
540 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
541 ("newnfs_request: NULL nr_cred"));
542 crfree(authcred);
543 authcred = crhold(nmp->nm_sockreq.nr_cred);
544 }
545 if (NFSHASINTEGRITY(nmp))
546 secflavour = RPCSEC_GSS_KRB5I;
547 else if (NFSHASPRIVACY(nmp))
548 secflavour = RPCSEC_GSS_KRB5P;
549 else
550 secflavour = RPCSEC_GSS_KRB5;
551 srv_principal = NFSMNT_SRVKRBNAME(nmp);
552 } else if (nmp != NULL && !NFSHASKERB(nmp) &&
553 nd->nd_procnum != NFSPROC_NULL &&
554 (nd->nd_flag & ND_USEGSSNAME) != 0) {
555 /*
556 * Use the uid that did the mount when the RPC is doing
557 * NFSv4 system operations, as indicated by the
558 * ND_USEGSSNAME flag, for the AUTH_SYS case.
559 * The credentials in nm_sockreq.nr_cred were used for the
560 * mount.
561 */
562 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
563 ("newnfs_request: NULL nr_cred"));
564 crfree(authcred);
565 authcred = crhold(nmp->nm_sockreq.nr_cred);
566 }
567
568 if (nmp != NULL) {
569 bzero(&nf, sizeof(struct nfs_feedback_arg));
570 nf.nf_mount = nmp;
571 nf.nf_td = td;
572 nf.nf_lastmsg = NFSD_MONOSEC -
573 ((nmp->nm_tprintf_delay)-(nmp->nm_tprintf_initial_delay));
574 }
575
576 if (nd->nd_procnum == NFSPROC_NULL)
577 auth = authnone_create();
578 else if (usegssname)
579 auth = nfs_getauth(nrp, secflavour, nmp->nm_krbname,
580 srv_principal, NULL, authcred);
581 else
582 auth = nfs_getauth(nrp, secflavour, NULL,
583 srv_principal, NULL, authcred);
584 crfree(authcred);
585 if (auth == NULL) {
586 m_freem(nd->nd_mreq);
587 if (set_sigset)
588 newnfs_restore_sigmask(td, &oldset);
589 return (EACCES);
590 }
591 bzero(&ext, sizeof(ext));
592 ext.rc_auth = auth;
593 if (nmp != NULL) {
594 ext.rc_feedback = nfs_feedback;
595 ext.rc_feedback_arg = &nf;
596 }
597
598 procnum = nd->nd_procnum;
599 if ((nd->nd_flag & ND_NFSV4) &&
600 nd->nd_procnum != NFSPROC_NULL &&
601 nd->nd_procnum != NFSV4PROC_CBCOMPOUND)
602 procnum = NFSV4PROC_COMPOUND;
603
604 if (nmp != NULL) {
605 NFSINCRGLOBAL(newnfsstats.rpcrequests);
606
607 /* Map the procnum to the old NFSv2 one, as required. */
608 if ((nd->nd_flag & ND_NFSV2) != 0) {
609 if (nd->nd_procnum < NFS_V3NPROCS)
610 procnum = nfsv2_procid[nd->nd_procnum];
611 else
612 procnum = NFSV2PROC_NOOP;
613 }
614
615 /*
616 * Now only used for the R_DONTRECOVER case, but until that is
617 * supported within the krpc code, I need to keep a queue of
618 * outstanding RPCs for nfsv4 client requests.
619 */
620 if ((nd->nd_flag & ND_NFSV4) && procnum == NFSV4PROC_COMPOUND)
621 MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq),
622 M_NFSDREQ, M_WAITOK);
623 }
624 trycnt = 0;
625 tryagain:
626 /*
627 * This timeout specifies when a new socket should be created,
628 * along with new xid values. For UDP, this should be done
629 * infrequently, since retransmits of RPC requests should normally
630 * use the same xid.
631 */
632 if (nmp == NULL) {
633 timo.tv_usec = 0;
634 if (clp == NULL)
635 timo.tv_sec = NFSV4_UPCALLTIMEO;
636 else
637 timo.tv_sec = NFSV4_CALLBACKTIMEO;
638 } else {
639 if (nrp->nr_sotype != SOCK_DGRAM) {
640 timo.tv_usec = 0;
641 if ((nmp->nm_flag & NFSMNT_NFSV4))
642 timo.tv_sec = INT_MAX;
643 else
644 timo.tv_sec = NFS_TCPTIMEO;
645 } else {
646 if (NFSHASSOFT(nmp)) {
647 /*
648 * CLSET_RETRIES is set to 2, so this should be
649 * half of the total timeout required.
650 */
651 timeo = nmp->nm_retry * nmp->nm_timeo / 2;
652 if (timeo < 1)
653 timeo = 1;
654 timo.tv_sec = timeo / NFS_HZ;
655 timo.tv_usec = (timeo % NFS_HZ) * 1000000 /
656 NFS_HZ;
657 } else {
658 /* For UDP hard mounts, use a large value. */
659 timo.tv_sec = NFS_MAXTIMEO / NFS_HZ;
660 timo.tv_usec = 0;
661 }
662 }
663
664 if (rep != NULL) {
665 rep->r_flags = 0;
666 rep->r_nmp = nmp;
667 /*
668 * Chain request into list of outstanding requests.
669 */
670 NFSLOCKREQ();
671 TAILQ_INSERT_TAIL(&nfsd_reqq, rep, r_chain);
672 NFSUNLOCKREQ();
673 }
674 }
675
676 nd->nd_mrep = NULL;
677 stat = CLNT_CALL_MBUF(nrp->nr_client, &ext, procnum, nd->nd_mreq,
678 &nd->nd_mrep, timo);
679
680 if (rep != NULL) {
681 /*
682 * RPC done, unlink the request.
683 */
684 NFSLOCKREQ();
685 TAILQ_REMOVE(&nfsd_reqq, rep, r_chain);
686 NFSUNLOCKREQ();
687 }
688
689 /*
690 * If there was a successful reply and a tprintf msg.
691 * tprintf a response.
692 */
693 if (stat == RPC_SUCCESS) {
694 error = 0;
695 } else if (stat == RPC_TIMEDOUT) {
696 NFSINCRGLOBAL(newnfsstats.rpctimeouts);
697 error = ETIMEDOUT;
698 } else if (stat == RPC_VERSMISMATCH) {
699 NFSINCRGLOBAL(newnfsstats.rpcinvalid);
700 error = EOPNOTSUPP;
701 } else if (stat == RPC_PROGVERSMISMATCH) {
702 NFSINCRGLOBAL(newnfsstats.rpcinvalid);
703 error = EPROTONOSUPPORT;
704 } else if (stat == RPC_INTR) {
705 error = EINTR;
706 } else {
707 NFSINCRGLOBAL(newnfsstats.rpcinvalid);
708 error = EACCES;
709 }
710 if (error) {
711 m_freem(nd->nd_mreq);
712 AUTH_DESTROY(auth);
713 if (rep != NULL)
714 FREE((caddr_t)rep, M_NFSDREQ);
715 if (set_sigset)
716 newnfs_restore_sigmask(td, &oldset);
717 return (error);
718 }
719
720 KASSERT(nd->nd_mrep != NULL, ("mrep shouldn't be NULL if no error\n"));
721
722 /*
723 * Search for any mbufs that are not a multiple of 4 bytes long
724 * or with m_data not longword aligned.
725 * These could cause pointer alignment problems, so copy them to
726 * well aligned mbufs.
727 */
728 newnfs_realign(&nd->nd_mrep);
729 nd->nd_md = nd->nd_mrep;
730 nd->nd_dpos = NFSMTOD(nd->nd_md, caddr_t);
731 nd->nd_repstat = 0;
732 if (nd->nd_procnum != NFSPROC_NULL) {
733 /*
734 * and now the actual NFS xdr.
735 */
736 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
737 nd->nd_repstat = fxdr_unsigned(u_int32_t, *tl);
738 if (nd->nd_repstat != 0) {
739 if (((nd->nd_repstat == NFSERR_DELAY ||
740 nd->nd_repstat == NFSERR_GRACE) &&
741 (nd->nd_flag & ND_NFSV4) &&
742 nd->nd_procnum != NFSPROC_DELEGRETURN &&
743 nd->nd_procnum != NFSPROC_SETATTR &&
744 nd->nd_procnum != NFSPROC_READ &&
745 nd->nd_procnum != NFSPROC_WRITE &&
746 nd->nd_procnum != NFSPROC_OPEN &&
747 nd->nd_procnum != NFSPROC_CREATE &&
748 nd->nd_procnum != NFSPROC_OPENCONFIRM &&
749 nd->nd_procnum != NFSPROC_OPENDOWNGRADE &&
750 nd->nd_procnum != NFSPROC_CLOSE &&
751 nd->nd_procnum != NFSPROC_LOCK &&
752 nd->nd_procnum != NFSPROC_LOCKU) ||
753 (nd->nd_repstat == NFSERR_DELAY &&
754 (nd->nd_flag & ND_NFSV4) == 0) ||
755 nd->nd_repstat == NFSERR_RESOURCE) {
756 if (trylater_delay > NFS_TRYLATERDEL)
757 trylater_delay = NFS_TRYLATERDEL;
758 waituntil = NFSD_MONOSEC + trylater_delay;
759 while (NFSD_MONOSEC < waituntil)
760 (void) nfs_catnap(PZERO, 0, "nfstry");
761 trylater_delay *= 2;
762 m_freem(nd->nd_mrep);
763 nd->nd_mrep = NULL;
764 goto tryagain;
765 }
766
767 /*
768 * If the File Handle was stale, invalidate the
769 * lookup cache, just in case.
770 * (vp != NULL implies a client side call)
771 */
772 if (nd->nd_repstat == ESTALE && vp != NULL) {
773 cache_purge(vp);
774 if (ncl_call_invalcaches != NULL)
775 (*ncl_call_invalcaches)(vp);
776 }
777 }
778
779 /*
780 * Get rid of the tag, return count, and PUTFH result for V4.
781 */
782 if (nd->nd_flag & ND_NFSV4) {
783 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
784 i = fxdr_unsigned(int, *tl);
785 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
786 if (error)
787 goto nfsmout;
788 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
789 i = fxdr_unsigned(int, *++tl);
790
791 /*
792 * If the first op's status is non-zero, mark that
793 * there is no more data to process.
794 */
795 if (*++tl)
796 nd->nd_flag |= ND_NOMOREDATA;
797
798 /*
799 * If the first op is Putfh, throw its results away
800 * and toss the op# and status for the first op.
801 */
802 if (nmp != NULL && i == NFSV4OP_PUTFH && *tl == 0) {
803 NFSM_DISSECT(tl,u_int32_t *,2 * NFSX_UNSIGNED);
804 i = fxdr_unsigned(int, *tl++);
805 j = fxdr_unsigned(int, *tl);
806 /*
807 * All Compounds that do an Op that must
808 * be in sequence consist of NFSV4OP_PUTFH
809 * followed by one of these. As such, we
810 * can determine if the seqid# should be
811 * incremented, here.
812 */
813 if ((i == NFSV4OP_OPEN ||
814 i == NFSV4OP_OPENCONFIRM ||
815 i == NFSV4OP_OPENDOWNGRADE ||
816 i == NFSV4OP_CLOSE ||
817 i == NFSV4OP_LOCK ||
818 i == NFSV4OP_LOCKU) &&
819 (j == 0 ||
820 (j != NFSERR_STALECLIENTID &&
821 j != NFSERR_STALESTATEID &&
822 j != NFSERR_BADSTATEID &&
823 j != NFSERR_BADSEQID &&
824 j != NFSERR_BADXDR &&
825 j != NFSERR_RESOURCE &&
826 j != NFSERR_NOFILEHANDLE)))
827 nd->nd_flag |= ND_INCRSEQID;
828 /*
829 * If the first op's status is non-zero, mark
830 * that there is no more data to process.
831 */
832 if (j)
833 nd->nd_flag |= ND_NOMOREDATA;
834 }
835
836 /*
837 * If R_DONTRECOVER is set, replace the stale error
838 * reply, so that recovery isn't initiated.
839 */
840 if ((nd->nd_repstat == NFSERR_STALECLIENTID ||
841 nd->nd_repstat == NFSERR_STALESTATEID) &&
842 rep != NULL && (rep->r_flags & R_DONTRECOVER))
843 nd->nd_repstat = NFSERR_STALEDONTRECOVER;
844 }
845 }
846
847 m_freem(nd->nd_mreq);
848 AUTH_DESTROY(auth);
849 if (rep != NULL)
850 FREE((caddr_t)rep, M_NFSDREQ);
851 if (set_sigset)
852 newnfs_restore_sigmask(td, &oldset);
853 return (0);
854 nfsmout:
855 mbuf_freem(nd->nd_mrep);
856 mbuf_freem(nd->nd_mreq);
857 AUTH_DESTROY(auth);
858 if (rep != NULL)
859 FREE((caddr_t)rep, M_NFSDREQ);
860 if (set_sigset)
861 newnfs_restore_sigmask(td, &oldset);
862 return (error);
863 }
864
865 /*
866 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
867 * wait for all requests to complete. This is used by forced unmounts
868 * to terminate any outstanding RPCs.
869 */
870 int
871 newnfs_nmcancelreqs(struct nfsmount *nmp)
872 {
873
874 if (nmp->nm_sockreq.nr_client != NULL)
875 CLNT_CLOSE(nmp->nm_sockreq.nr_client);
876 return (0);
877 }
878
879 /*
880 * Any signal that can interrupt an NFS operation in an intr mount
881 * should be added to this set. SIGSTOP and SIGKILL cannot be masked.
882 */
883 int newnfs_sig_set[] = {
884 SIGINT,
885 SIGTERM,
886 SIGHUP,
887 SIGKILL,
888 SIGSTOP,
889 SIGQUIT
890 };
891
892 /*
893 * Check to see if one of the signals in our subset is pending on
894 * the process (in an intr mount).
895 */
896 static int
897 nfs_sig_pending(sigset_t set)
898 {
899 int i;
900
901 for (i = 0 ; i < sizeof(newnfs_sig_set)/sizeof(int) ; i++)
902 if (SIGISMEMBER(set, newnfs_sig_set[i]))
903 return (1);
904 return (0);
905 }
906
907 /*
908 * The set/restore sigmask functions are used to (temporarily) overwrite
909 * the process p_sigmask during an RPC call (for example). These are also
910 * used in other places in the NFS client that might tsleep().
911 */
912 void
913 newnfs_set_sigmask(struct thread *td, sigset_t *oldset)
914 {
915 sigset_t newset;
916 int i;
917 struct proc *p;
918
919 SIGFILLSET(newset);
920 if (td == NULL)
921 td = curthread; /* XXX */
922 p = td->td_proc;
923 /* Remove the NFS set of signals from newset */
924 PROC_LOCK(p);
925 mtx_lock(&p->p_sigacts->ps_mtx);
926 for (i = 0 ; i < sizeof(newnfs_sig_set)/sizeof(int) ; i++) {
927 /*
928 * But make sure we leave the ones already masked
929 * by the process, ie. remove the signal from the
930 * temporary signalmask only if it wasn't already
931 * in p_sigmask.
932 */
933 if (!SIGISMEMBER(td->td_sigmask, newnfs_sig_set[i]) &&
934 !SIGISMEMBER(p->p_sigacts->ps_sigignore, newnfs_sig_set[i]))
935 SIGDELSET(newset, newnfs_sig_set[i]);
936 }
937 mtx_unlock(&p->p_sigacts->ps_mtx);
938 PROC_UNLOCK(p);
939 kern_sigprocmask(td, SIG_SETMASK, &newset, oldset, 0);
940 }
941
942 void
943 newnfs_restore_sigmask(struct thread *td, sigset_t *set)
944 {
945 if (td == NULL)
946 td = curthread; /* XXX */
947 kern_sigprocmask(td, SIG_SETMASK, set, NULL, 0);
948 }
949
950 /*
951 * NFS wrapper to msleep(), that shoves a new p_sigmask and restores the
952 * old one after msleep() returns.
953 */
954 int
955 newnfs_msleep(struct thread *td, void *ident, struct mtx *mtx, int priority, char *wmesg, int timo)
956 {
957 sigset_t oldset;
958 int error;
959 struct proc *p;
960
961 if ((priority & PCATCH) == 0)
962 return msleep(ident, mtx, priority, wmesg, timo);
963 if (td == NULL)
964 td = curthread; /* XXX */
965 newnfs_set_sigmask(td, &oldset);
966 error = msleep(ident, mtx, priority, wmesg, timo);
967 newnfs_restore_sigmask(td, &oldset);
968 p = td->td_proc;
969 return (error);
970 }
971
972 /*
973 * Test for a termination condition pending on the process.
974 * This is used for NFSMNT_INT mounts.
975 */
976 int
977 newnfs_sigintr(struct nfsmount *nmp, struct thread *td)
978 {
979 struct proc *p;
980 sigset_t tmpset;
981
982 /* Terminate all requests while attempting a forced unmount. */
983 if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)
984 return (EIO);
985 if (!(nmp->nm_flag & NFSMNT_INT))
986 return (0);
987 if (td == NULL)
988 return (0);
989 p = td->td_proc;
990 PROC_LOCK(p);
991 tmpset = p->p_siglist;
992 SIGSETOR(tmpset, td->td_siglist);
993 SIGSETNAND(tmpset, td->td_sigmask);
994 mtx_lock(&p->p_sigacts->ps_mtx);
995 SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore);
996 mtx_unlock(&p->p_sigacts->ps_mtx);
997 if ((SIGNOTEMPTY(p->p_siglist) || SIGNOTEMPTY(td->td_siglist))
998 && nfs_sig_pending(tmpset)) {
999 PROC_UNLOCK(p);
1000 return (EINTR);
1001 }
1002 PROC_UNLOCK(p);
1003 return (0);
1004 }
1005
1006 static int
1007 nfs_msg(struct thread *td, const char *server, const char *msg, int error)
1008 {
1009 struct proc *p;
1010
1011 p = td ? td->td_proc : NULL;
1012 if (error) {
1013 tprintf(p, LOG_INFO, "newnfs server %s: %s, error %d\n",
1014 server, msg, error);
1015 } else {
1016 tprintf(p, LOG_INFO, "newnfs server %s: %s\n", server, msg);
1017 }
1018 return (0);
1019 }
1020
1021 static void
1022 nfs_down(struct nfsmount *nmp, struct thread *td, const char *msg,
1023 int error, int flags)
1024 {
1025 if (nmp == NULL)
1026 return;
1027 mtx_lock(&nmp->nm_mtx);
1028 if ((flags & NFSSTA_TIMEO) && !(nmp->nm_state & NFSSTA_TIMEO)) {
1029 nmp->nm_state |= NFSSTA_TIMEO;
1030 mtx_unlock(&nmp->nm_mtx);
1031 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1032 VQ_NOTRESP, 0);
1033 } else
1034 mtx_unlock(&nmp->nm_mtx);
1035 mtx_lock(&nmp->nm_mtx);
1036 if ((flags & NFSSTA_LOCKTIMEO) && !(nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1037 nmp->nm_state |= NFSSTA_LOCKTIMEO;
1038 mtx_unlock(&nmp->nm_mtx);
1039 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1040 VQ_NOTRESPLOCK, 0);
1041 } else
1042 mtx_unlock(&nmp->nm_mtx);
1043 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error);
1044 }
1045
1046 static void
1047 nfs_up(struct nfsmount *nmp, struct thread *td, const char *msg,
1048 int flags, int tprintfmsg)
1049 {
1050 if (nmp == NULL)
1051 return;
1052 if (tprintfmsg) {
1053 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0);
1054 }
1055
1056 mtx_lock(&nmp->nm_mtx);
1057 if ((flags & NFSSTA_TIMEO) && (nmp->nm_state & NFSSTA_TIMEO)) {
1058 nmp->nm_state &= ~NFSSTA_TIMEO;
1059 mtx_unlock(&nmp->nm_mtx);
1060 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1061 VQ_NOTRESP, 1);
1062 } else
1063 mtx_unlock(&nmp->nm_mtx);
1064
1065 mtx_lock(&nmp->nm_mtx);
1066 if ((flags & NFSSTA_LOCKTIMEO) && (nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1067 nmp->nm_state &= ~NFSSTA_LOCKTIMEO;
1068 mtx_unlock(&nmp->nm_mtx);
1069 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1070 VQ_NOTRESPLOCK, 1);
1071 } else
1072 mtx_unlock(&nmp->nm_mtx);
1073 }
1074
Cache object: ac9555e55a6ec2576808280944a86c79
|