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