1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1989, 1991, 1993, 1995 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Rick Macklem at The University of Guelph. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 */ 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 /* 40 * Socket operations for use by nfs 41 */ 42 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 #include <rpc/krpc.h> 64 65 #include <kgssapi/krb5/kcrypto.h> 66 67 #include <fs/nfs/nfsport.h> 68 69 #ifdef KDTRACE_HOOKS 70 #include <sys/dtrace_bsd.h> 71 72 dtrace_nfsclient_nfs23_start_probe_func_t 73 dtrace_nfscl_nfs234_start_probe; 74 75 dtrace_nfsclient_nfs23_done_probe_func_t 76 dtrace_nfscl_nfs234_done_probe; 77 78 /* 79 * Registered probes by RPC type. 80 */ 81 uint32_t nfscl_nfs2_start_probes[NFSV41_NPROCS + 1]; 82 uint32_t nfscl_nfs2_done_probes[NFSV41_NPROCS + 1]; 83 84 uint32_t nfscl_nfs3_start_probes[NFSV41_NPROCS + 1]; 85 uint32_t nfscl_nfs3_done_probes[NFSV41_NPROCS + 1]; 86 87 uint32_t nfscl_nfs4_start_probes[NFSV41_NPROCS + 1]; 88 uint32_t nfscl_nfs4_done_probes[NFSV41_NPROCS + 1]; 89 #endif 90 91 NFSSTATESPINLOCK; 92 NFSREQSPINLOCK; 93 NFSDLOCKMUTEX; 94 NFSCLSTATEMUTEX; 95 extern struct nfsstatsv1 nfsstatsv1; 96 extern struct nfsreqhead nfsd_reqq; 97 extern int nfscl_ticks; 98 extern void (*ncl_call_invalcaches)(struct vnode *); 99 extern int nfs_numnfscbd; 100 extern int nfscl_debuglevel; 101 extern int nfsrv_lease; 102 103 SVCPOOL *nfscbd_pool; 104 int nfs_bufpackets = 4; 105 static int nfsrv_gsscallbackson = 0; 106 static int nfs_reconnects; 107 static int nfs3_jukebox_delay = 10; 108 static int nfs_skip_wcc_data_onerr = 1; 109 static int nfs_dsretries = 2; 110 static struct timespec nfs_trylater_max = { 111 .tv_sec = NFS_TRYLATERDEL, 112 .tv_nsec = 0, 113 }; 114 115 SYSCTL_DECL(_vfs_nfs); 116 117 SYSCTL_INT(_vfs_nfs, OID_AUTO, bufpackets, CTLFLAG_RW, &nfs_bufpackets, 0, 118 "Buffer reservation size 2 < x < 64"); 119 SYSCTL_INT(_vfs_nfs, OID_AUTO, reconnects, CTLFLAG_RD, &nfs_reconnects, 0, 120 "Number of times the nfs client has had to reconnect"); 121 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs3_jukebox_delay, CTLFLAG_RW, &nfs3_jukebox_delay, 0, 122 "Number of seconds to delay a retry after receiving EJUKEBOX"); 123 SYSCTL_INT(_vfs_nfs, OID_AUTO, skip_wcc_data_onerr, CTLFLAG_RW, &nfs_skip_wcc_data_onerr, 0, 124 "Disable weak cache consistency checking when server returns an error"); 125 SYSCTL_INT(_vfs_nfs, OID_AUTO, dsretries, CTLFLAG_RW, &nfs_dsretries, 0, 126 "Number of retries for a DS RPC before failure"); 127 128 static void nfs_down(struct nfsmount *, struct thread *, const char *, 129 int, int); 130 static void nfs_up(struct nfsmount *, struct thread *, const char *, 131 int, int); 132 static int nfs_msg(struct thread *, const char *, const char *, int); 133 134 struct nfs_cached_auth { 135 int ca_refs; /* refcount, including 1 from the cache */ 136 uid_t ca_uid; /* uid that corresponds to this auth */ 137 AUTH *ca_auth; /* RPC auth handle */ 138 }; 139 140 static int nfsv2_procid[NFS_V3NPROCS] = { 141 NFSV2PROC_NULL, 142 NFSV2PROC_GETATTR, 143 NFSV2PROC_SETATTR, 144 NFSV2PROC_LOOKUP, 145 NFSV2PROC_NOOP, 146 NFSV2PROC_READLINK, 147 NFSV2PROC_READ, 148 NFSV2PROC_WRITE, 149 NFSV2PROC_CREATE, 150 NFSV2PROC_MKDIR, 151 NFSV2PROC_SYMLINK, 152 NFSV2PROC_CREATE, 153 NFSV2PROC_REMOVE, 154 NFSV2PROC_RMDIR, 155 NFSV2PROC_RENAME, 156 NFSV2PROC_LINK, 157 NFSV2PROC_READDIR, 158 NFSV2PROC_NOOP, 159 NFSV2PROC_STATFS, 160 NFSV2PROC_NOOP, 161 NFSV2PROC_NOOP, 162 NFSV2PROC_NOOP, 163 }; 164 165 /* 166 * Initialize sockets and congestion for a new NFS connection. 167 * We do not free the sockaddr if error. 168 * Which arguments are set to NULL indicate what kind of call it is. 169 * cred == NULL --> a call to connect to a pNFS DS 170 * nmp == NULL --> indicates an upcall to userland or a NFSv4.0 callback 171 */ 172 int 173 newnfs_connect(struct nfsmount *nmp, struct nfssockreq *nrp, 174 struct ucred *cred, NFSPROC_T *p, int callback_retry_mult, bool dotls, 175 struct __rpc_client **clipp) 176 { 177 int rcvreserve, sndreserve; 178 int pktscale, pktscalesav; 179 struct sockaddr *saddr; 180 struct ucred *origcred; 181 CLIENT *client; 182 struct netconfig *nconf; 183 struct socket *so; 184 int one = 1, retries, error = 0; 185 struct thread *td = curthread; 186 SVCXPRT *xprt; 187 struct timeval timo; 188 uint64_t tval; 189 190 /* 191 * We need to establish the socket using the credentials of 192 * the mountpoint. Some parts of this process (such as 193 * sobind() and soconnect()) will use the curent thread's 194 * credential instead of the socket credential. To work 195 * around this, temporarily change the current thread's 196 * credential to that of the mountpoint. 197 * 198 * XXX: It would be better to explicitly pass the correct 199 * credential to sobind() and soconnect(). 200 */ 201 origcred = td->td_ucred; 202 203 /* 204 * Use the credential in nr_cred, if not NULL. 205 */ 206 if (nrp->nr_cred != NULL) 207 td->td_ucred = nrp->nr_cred; 208 else 209 td->td_ucred = cred; 210 saddr = nrp->nr_nam; 211 212 if (saddr->sa_family == AF_INET) 213 if (nrp->nr_sotype == SOCK_DGRAM) 214 nconf = getnetconfigent("udp"); 215 else 216 nconf = getnetconfigent("tcp"); 217 else 218 if (nrp->nr_sotype == SOCK_DGRAM) 219 nconf = getnetconfigent("udp6"); 220 else 221 nconf = getnetconfigent("tcp6"); 222 223 pktscale = nfs_bufpackets; 224 if (pktscale < 2) 225 pktscale = 2; 226 if (pktscale > 64) 227 pktscale = 64; 228 pktscalesav = pktscale; 229 /* 230 * soreserve() can fail if sb_max is too small, so shrink pktscale 231 * and try again if there is an error. 232 * Print a log message suggesting increasing sb_max. 233 * Creating a socket and doing this is necessary since, if the 234 * reservation sizes are too large and will make soreserve() fail, 235 * the connection will work until a large send is attempted and 236 * then it will loop in the krpc code. 237 */ 238 so = NULL; 239 saddr = NFSSOCKADDR(nrp->nr_nam, struct sockaddr *); 240 error = socreate(saddr->sa_family, &so, nrp->nr_sotype, 241 nrp->nr_soproto, td->td_ucred, td); 242 if (error != 0) 243 goto out; 244 do { 245 if (error != 0 && pktscale > 2) { 246 if (nmp != NULL && nrp->nr_sotype == SOCK_STREAM && 247 pktscale == pktscalesav) { 248 /* 249 * Suggest vfs.nfs.bufpackets * maximum RPC message, 250 * adjusted for the sb_max->sb_max_adj conversion of 251 * MCLBYTES / (MSIZE + MCLBYTES) as the minimum setting 252 * for kern.ipc.maxsockbuf. 253 */ 254 tval = (NFS_MAXBSIZE + NFS_MAXXDR) * nfs_bufpackets; 255 tval *= MSIZE + MCLBYTES; 256 tval += MCLBYTES - 1; /* Round up divide by MCLBYTES. */ 257 tval /= MCLBYTES; 258 printf("Consider increasing kern.ipc.maxsockbuf to a " 259 "minimum of %ju to support %ubyte NFS I/O\n", 260 (uintmax_t)tval, NFS_MAXBSIZE); 261 } 262 pktscale--; 263 } 264 if (nrp->nr_sotype == SOCK_DGRAM) { 265 if (nmp != NULL) { 266 sndreserve = (NFS_MAXDGRAMDATA + NFS_MAXPKTHDR) * 267 pktscale; 268 rcvreserve = (NFS_MAXDGRAMDATA + NFS_MAXPKTHDR) * 269 pktscale; 270 } else { 271 sndreserve = rcvreserve = 1024 * pktscale; 272 } 273 } else { 274 if (nrp->nr_sotype != SOCK_STREAM) 275 panic("nfscon sotype"); 276 if (nmp != NULL) { 277 sndreserve = (NFS_MAXBSIZE + NFS_MAXXDR) * 278 pktscale; 279 rcvreserve = (NFS_MAXBSIZE + NFS_MAXXDR) * 280 pktscale; 281 } else { 282 sndreserve = rcvreserve = 1024 * pktscale; 283 } 284 } 285 error = soreserve(so, sndreserve, rcvreserve); 286 if (error != 0 && nmp != NULL && nrp->nr_sotype == SOCK_STREAM && 287 pktscale <= 2) 288 printf("Must increase kern.ipc.maxsockbuf or reduce" 289 " rsize, wsize\n"); 290 } while (error != 0 && pktscale > 2); 291 soclose(so); 292 if (error != 0) 293 goto out; 294 295 client = clnt_reconnect_create(nconf, saddr, nrp->nr_prog, 296 nrp->nr_vers, sndreserve, rcvreserve); 297 CLNT_CONTROL(client, CLSET_WAITCHAN, "nfsreq"); 298 if (nmp != NULL) { 299 if ((nmp->nm_flag & NFSMNT_INT)) 300 CLNT_CONTROL(client, CLSET_INTERRUPTIBLE, &one); 301 if ((nmp->nm_flag & NFSMNT_RESVPORT)) 302 CLNT_CONTROL(client, CLSET_PRIVPORT, &one); 303 if (NFSHASTLS(nmp)) { 304 CLNT_CONTROL(client, CLSET_TLS, &one); 305 if (nmp->nm_tlscertname != NULL) 306 CLNT_CONTROL(client, CLSET_TLSCERTNAME, 307 nmp->nm_tlscertname); 308 } 309 if (NFSHASSOFT(nmp)) { 310 if (nmp->nm_sotype == SOCK_DGRAM) 311 /* 312 * For UDP, the large timeout for a reconnect 313 * will be set to "nm_retry * nm_timeo / 2", so 314 * we only want to do 2 reconnect timeout 315 * retries. 316 */ 317 retries = 2; 318 else 319 retries = nmp->nm_retry; 320 } else 321 retries = INT_MAX; 322 if (NFSHASNFSV4N(nmp)) { 323 if (cred != NULL) { 324 if (NFSHASSOFT(nmp)) { 325 /* 326 * This should be a DS mount. 327 * Use CLSET_TIMEOUT to set the timeout 328 * for connections to DSs instead of 329 * specifying a timeout on each RPC. 330 * This is done so that SO_SNDTIMEO 331 * is set on the TCP socket as well 332 * as specifying a time limit when 333 * waiting for an RPC reply. Useful 334 * if the send queue for the TCP 335 * connection has become constipated, 336 * due to a failed DS. 337 * The choice of lease_duration / 4 is 338 * fairly arbitrary, but seems to work 339 * ok, with a lower bound of 10sec. 340 */ 341 timo.tv_sec = nfsrv_lease / 4; 342 if (timo.tv_sec < 10) 343 timo.tv_sec = 10; 344 timo.tv_usec = 0; 345 CLNT_CONTROL(client, CLSET_TIMEOUT, 346 &timo); 347 } 348 /* 349 * Make sure the nfscbd_pool doesn't get 350 * destroyed while doing this. 351 */ 352 NFSD_LOCK(); 353 if (nfs_numnfscbd > 0) { 354 nfs_numnfscbd++; 355 NFSD_UNLOCK(); 356 xprt = svc_vc_create_backchannel( 357 nfscbd_pool); 358 CLNT_CONTROL(client, CLSET_BACKCHANNEL, 359 xprt); 360 NFSD_LOCK(); 361 nfs_numnfscbd--; 362 if (nfs_numnfscbd == 0) 363 wakeup(&nfs_numnfscbd); 364 } 365 NFSD_UNLOCK(); 366 } else { 367 /* 368 * cred == NULL for a DS connect. 369 * For connects to a DS, set a retry limit 370 * so that failed DSs will be detected. 371 * This is ok for NFSv4.1, since a DS does 372 * not maintain open/lock state and is the 373 * only case where using a "soft" mount is 374 * recommended for NFSv4. 375 * For mounts from the MDS to DS, this is done 376 * via mount options, but that is not the case 377 * here. The retry limit here can be adjusted 378 * via the sysctl vfs.nfs.dsretries. 379 * See the comment above w.r.t. timeout. 380 */ 381 timo.tv_sec = nfsrv_lease / 4; 382 if (timo.tv_sec < 10) 383 timo.tv_sec = 10; 384 timo.tv_usec = 0; 385 CLNT_CONTROL(client, CLSET_TIMEOUT, &timo); 386 retries = nfs_dsretries; 387 } 388 } 389 } else { 390 /* 391 * Three cases: 392 * - Null RPC callback to client 393 * - Non-Null RPC callback to client, wait a little longer 394 * - upcalls to nfsuserd and gssd (clp == NULL) 395 */ 396 if (callback_retry_mult == 0) { 397 retries = NFSV4_UPCALLRETRY; 398 CLNT_CONTROL(client, CLSET_PRIVPORT, &one); 399 } else { 400 retries = NFSV4_CALLBACKRETRY * callback_retry_mult; 401 } 402 if (dotls) 403 CLNT_CONTROL(client, CLSET_TLS, &one); 404 } 405 CLNT_CONTROL(client, CLSET_RETRIES, &retries); 406 407 if (nmp != NULL) { 408 /* 409 * For UDP, there are 2 timeouts: 410 * - CLSET_RETRY_TIMEOUT sets the initial timeout for the timer 411 * that does a retransmit of an RPC request using the same 412 * socket and xid. This is what you normally want to do, 413 * since NFS servers depend on "same xid" for their 414 * Duplicate Request Cache. 415 * - timeout specified in CLNT_CALL_MBUF(), which specifies when 416 * retransmits on the same socket should fail and a fresh 417 * socket created. Each of these timeouts counts as one 418 * CLSET_RETRIES as set above. 419 * Set the initial retransmit timeout for UDP. This timeout 420 * doesn't exist for TCP and the following call just fails, 421 * which is ok. 422 */ 423 timo.tv_sec = nmp->nm_timeo / NFS_HZ; 424 timo.tv_usec = (nmp->nm_timeo % NFS_HZ) * 1000000 / NFS_HZ; 425 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, &timo); 426 } 427 428 /* 429 * *clipp is &nrp->nr_client or &nm_aconn[nmp->nm_nextaconn]. 430 * The latter case is for additional connections specified by the 431 * "nconnect" mount option. nr_mtx etc is used for these additional 432 * connections, as well as nr_client in the nfssockreq 433 * structure for the mount. 434 */ 435 mtx_lock(&nrp->nr_mtx); 436 if (*clipp != NULL) { 437 mtx_unlock(&nrp->nr_mtx); 438 /* 439 * Someone else already connected. 440 */ 441 CLNT_RELEASE(client); 442 } else { 443 *clipp = client; 444 /* 445 * Protocols that do not require connections may be optionally 446 * left unconnected for servers that reply from a port other 447 * than NFS_PORT. 448 */ 449 if (nmp == NULL || (nmp->nm_flag & NFSMNT_NOCONN) == 0) { 450 mtx_unlock(&nrp->nr_mtx); 451 CLNT_CONTROL(client, CLSET_CONNECT, &one); 452 } else 453 mtx_unlock(&nrp->nr_mtx); 454 } 455 456 out: 457 /* Restore current thread's credentials. */ 458 td->td_ucred = origcred; 459 460 NFSEXITCODE(error); 461 return (error); 462 } 463 464 /* 465 * NFS disconnect. Clean up and unlink. 466 */ 467 void 468 newnfs_disconnect(struct nfsmount *nmp, struct nfssockreq *nrp) 469 { 470 CLIENT *client, *aconn[NFS_MAXNCONN - 1]; 471 int i; 472 473 mtx_lock(&nrp->nr_mtx); 474 if (nrp->nr_client != NULL) { 475 client = nrp->nr_client; 476 nrp->nr_client = NULL; 477 if (nmp != NULL && nmp->nm_aconnect > 0) { 478 for (i = 0; i < nmp->nm_aconnect; i++) { 479 aconn[i] = nmp->nm_aconn[i]; 480 nmp->nm_aconn[i] = NULL; 481 } 482 } 483 mtx_unlock(&nrp->nr_mtx); 484 rpc_gss_secpurge_call(client); 485 CLNT_CLOSE(client); 486 CLNT_RELEASE(client); 487 if (nmp != NULL && nmp->nm_aconnect > 0) { 488 for (i = 0; i < nmp->nm_aconnect; i++) { 489 if (aconn[i] != NULL) { 490 rpc_gss_secpurge_call(aconn[i]); 491 CLNT_CLOSE(aconn[i]); 492 CLNT_RELEASE(aconn[i]); 493 } 494 } 495 } 496 } else { 497 mtx_unlock(&nrp->nr_mtx); 498 } 499 } 500 501 static AUTH * 502 nfs_getauth(struct nfssockreq *nrp, int secflavour, char *clnt_principal, 503 char *srv_principal, gss_OID mech_oid, struct ucred *cred) 504 { 505 rpc_gss_service_t svc; 506 AUTH *auth; 507 508 switch (secflavour) { 509 case RPCSEC_GSS_KRB5: 510 case RPCSEC_GSS_KRB5I: 511 case RPCSEC_GSS_KRB5P: 512 if (!mech_oid) { 513 if (!rpc_gss_mech_to_oid_call("kerberosv5", &mech_oid)) 514 return (NULL); 515 } 516 if (secflavour == RPCSEC_GSS_KRB5) 517 svc = rpc_gss_svc_none; 518 else if (secflavour == RPCSEC_GSS_KRB5I) 519 svc = rpc_gss_svc_integrity; 520 else 521 svc = rpc_gss_svc_privacy; 522 523 if (clnt_principal == NULL) 524 auth = rpc_gss_secfind_call(nrp->nr_client, cred, 525 srv_principal, mech_oid, svc); 526 else { 527 auth = rpc_gss_seccreate_call(nrp->nr_client, cred, 528 clnt_principal, srv_principal, "kerberosv5", 529 svc, NULL, NULL, NULL); 530 return (auth); 531 } 532 if (auth != NULL) 533 return (auth); 534 /* fallthrough */ 535 case AUTH_SYS: 536 default: 537 return (authunix_create(cred)); 538 } 539 } 540 541 /* 542 * Callback from the RPC code to generate up/down notifications. 543 */ 544 545 struct nfs_feedback_arg { 546 struct nfsmount *nf_mount; 547 int nf_lastmsg; /* last tprintf */ 548 int nf_tprintfmsg; 549 struct thread *nf_td; 550 }; 551 552 static void 553 nfs_feedback(int type, int proc, void *arg) 554 { 555 struct nfs_feedback_arg *nf = (struct nfs_feedback_arg *) arg; 556 struct nfsmount *nmp = nf->nf_mount; 557 time_t now; 558 559 switch (type) { 560 case FEEDBACK_REXMIT2: 561 case FEEDBACK_RECONNECT: 562 now = NFSD_MONOSEC; 563 if (nf->nf_lastmsg + nmp->nm_tprintf_delay < now) { 564 nfs_down(nmp, nf->nf_td, 565 "not responding", 0, NFSSTA_TIMEO); 566 nf->nf_tprintfmsg = TRUE; 567 nf->nf_lastmsg = now; 568 } 569 break; 570 571 case FEEDBACK_OK: 572 nfs_up(nf->nf_mount, nf->nf_td, 573 "is alive again", NFSSTA_TIMEO, nf->nf_tprintfmsg); 574 break; 575 } 576 } 577 578 /* 579 * newnfs_request - goes something like this 580 * - does the rpc by calling the krpc layer 581 * - break down rpc header and return with nfs reply 582 * nb: always frees up nd_mreq mbuf list 583 */ 584 int 585 newnfs_request(struct nfsrv_descript *nd, struct nfsmount *nmp, 586 struct nfsclient *clp, struct nfssockreq *nrp, vnode_t vp, 587 struct thread *td, struct ucred *cred, u_int32_t prog, u_int32_t vers, 588 u_char *retsum, int toplevel, u_int64_t *xidp, struct nfsclsession *dssep) 589 { 590 uint32_t retseq, retval, slotseq, *tl; 591 int i = 0, j = 0, opcnt, set_sigset = 0, slot; 592 int error = 0, usegssname = 0, secflavour = AUTH_SYS; 593 int freeslot, maxslot, reterr, slotpos, timeo; 594 u_int16_t procnum; 595 u_int nextconn; 596 struct nfs_feedback_arg nf; 597 struct timeval timo; 598 AUTH *auth; 599 struct rpc_callextra ext; 600 enum clnt_stat stat; 601 struct nfsreq *rep = NULL; 602 char *srv_principal = NULL, *clnt_principal = NULL; 603 sigset_t oldset; 604 struct ucred *authcred; 605 struct nfsclsession *sep; 606 uint8_t sessionid[NFSX_V4SESSIONID]; 607 bool nextconn_set; 608 struct timespec trylater_delay, ts, waituntil; 609 610 /* Initially 1msec. */ 611 trylater_delay.tv_sec = 0; 612 trylater_delay.tv_nsec = 1000000; 613 sep = dssep; 614 if (xidp != NULL) 615 *xidp = 0; 616 /* Reject requests while attempting a forced unmount. */ 617 if (nmp != NULL && NFSCL_FORCEDISM(nmp->nm_mountp)) { 618 m_freem(nd->nd_mreq); 619 return (ESTALE); 620 } 621 622 /* 623 * Set authcred, which is used to acquire RPC credentials to 624 * the cred argument, by default. The crhold() should not be 625 * necessary, but will ensure that some future code change 626 * doesn't result in the credential being free'd prematurely. 627 */ 628 authcred = crhold(cred); 629 630 /* For client side interruptible mounts, mask off the signals. */ 631 if (nmp != NULL && td != NULL && NFSHASINT(nmp)) { 632 newnfs_set_sigmask(td, &oldset); 633 set_sigset = 1; 634 } 635 636 /* 637 * If not already connected call newnfs_connect now. 638 */ 639 if (nrp->nr_client == NULL) 640 newnfs_connect(nmp, nrp, cred, td, 0, false, &nrp->nr_client); 641 642 nextconn_set = false; 643 if (nmp != NULL && nmp->nm_aconnect > 0 && 644 (nd->nd_procnum == NFSPROC_READ || 645 nd->nd_procnum == NFSPROC_READDIR || 646 nd->nd_procnum == NFSPROC_READDIRPLUS || 647 nd->nd_procnum == NFSPROC_WRITE)) { 648 nextconn = atomic_fetchadd_int(&nmp->nm_nextaconn, 1); 649 nextconn %= nmp->nm_aconnect; 650 nextconn_set = true; 651 if (nmp->nm_aconn[nextconn] == NULL) 652 newnfs_connect(nmp, nrp, cred, td, 0, false, 653 &nmp->nm_aconn[nextconn]); 654 } 655 656 /* 657 * For a client side mount, nmp is != NULL and clp == NULL. For 658 * server calls (callbacks or upcalls), nmp == NULL. 659 */ 660 if (clp != NULL) { 661 NFSLOCKSTATE(); 662 if ((clp->lc_flags & LCL_GSS) && nfsrv_gsscallbackson) { 663 secflavour = RPCSEC_GSS_KRB5; 664 if (nd->nd_procnum != NFSPROC_NULL) { 665 if (clp->lc_flags & LCL_GSSINTEGRITY) 666 secflavour = RPCSEC_GSS_KRB5I; 667 else if (clp->lc_flags & LCL_GSSPRIVACY) 668 secflavour = RPCSEC_GSS_KRB5P; 669 } 670 } 671 NFSUNLOCKSTATE(); 672 } else if (nmp != NULL && NFSHASKERB(nmp) && 673 nd->nd_procnum != NFSPROC_NULL) { 674 if (NFSHASALLGSSNAME(nmp) && nmp->nm_krbnamelen > 0) 675 nd->nd_flag |= ND_USEGSSNAME; 676 if ((nd->nd_flag & ND_USEGSSNAME) != 0) { 677 /* 678 * If there is a client side host based credential, 679 * use that, otherwise use the system uid, if set. 680 * The system uid is in the nmp->nm_sockreq.nr_cred 681 * credentials. 682 */ 683 if (nmp->nm_krbnamelen > 0) { 684 usegssname = 1; 685 clnt_principal = nmp->nm_krbname; 686 } else if (nmp->nm_uid != (uid_t)-1) { 687 KASSERT(nmp->nm_sockreq.nr_cred != NULL, 688 ("newnfs_request: NULL nr_cred")); 689 crfree(authcred); 690 authcred = crhold(nmp->nm_sockreq.nr_cred); 691 } 692 } else if (nmp->nm_krbnamelen == 0 && 693 nmp->nm_uid != (uid_t)-1 && cred->cr_uid == (uid_t)0) { 694 /* 695 * If there is no host based principal name and 696 * the system uid is set and this is root, use the 697 * system uid, since root won't have user 698 * credentials in a credentials cache file. 699 * The system uid is in the nmp->nm_sockreq.nr_cred 700 * credentials. 701 */ 702 KASSERT(nmp->nm_sockreq.nr_cred != NULL, 703 ("newnfs_request: NULL nr_cred")); 704 crfree(authcred); 705 authcred = crhold(nmp->nm_sockreq.nr_cred); 706 } 707 if (NFSHASINTEGRITY(nmp)) 708 secflavour = RPCSEC_GSS_KRB5I; 709 else if (NFSHASPRIVACY(nmp)) 710 secflavour = RPCSEC_GSS_KRB5P; 711 else 712 secflavour = RPCSEC_GSS_KRB5; 713 srv_principal = NFSMNT_SRVKRBNAME(nmp); 714 } else if (nmp != NULL && !NFSHASKERB(nmp) && 715 nd->nd_procnum != NFSPROC_NULL && 716 (nd->nd_flag & ND_USEGSSNAME) != 0) { 717 /* 718 * Use the uid that did the mount when the RPC is doing 719 * NFSv4 system operations, as indicated by the 720 * ND_USEGSSNAME flag, for the AUTH_SYS case. 721 * The credentials in nm_sockreq.nr_cred were used for the 722 * mount. 723 */ 724 KASSERT(nmp->nm_sockreq.nr_cred != NULL, 725 ("newnfs_request: NULL nr_cred")); 726 crfree(authcred); 727 authcred = crhold(nmp->nm_sockreq.nr_cred); 728 } 729 730 if (nmp != NULL) { 731 bzero(&nf, sizeof(struct nfs_feedback_arg)); 732 nf.nf_mount = nmp; 733 nf.nf_td = td; 734 nf.nf_lastmsg = NFSD_MONOSEC - 735 ((nmp->nm_tprintf_delay)-(nmp->nm_tprintf_initial_delay)); 736 } 737 738 if (nd->nd_procnum == NFSPROC_NULL) 739 auth = authnone_create(); 740 else if (usegssname) { 741 /* 742 * For this case, the authenticator is held in the 743 * nfssockreq structure, so don't release the reference count 744 * held on it. --> Don't AUTH_DESTROY() it in this function. 745 */ 746 if (nrp->nr_auth == NULL) 747 nrp->nr_auth = nfs_getauth(nrp, secflavour, 748 clnt_principal, srv_principal, NULL, authcred); 749 else 750 rpc_gss_refresh_auth_call(nrp->nr_auth); 751 auth = nrp->nr_auth; 752 } else 753 auth = nfs_getauth(nrp, secflavour, NULL, 754 srv_principal, NULL, authcred); 755 crfree(authcred); 756 if (auth == NULL) { 757 m_freem(nd->nd_mreq); 758 if (set_sigset) 759 newnfs_restore_sigmask(td, &oldset); 760 return (EACCES); 761 } 762 bzero(&ext, sizeof(ext)); 763 ext.rc_auth = auth; 764 if (nmp != NULL) { 765 ext.rc_feedback = nfs_feedback; 766 ext.rc_feedback_arg = &nf; 767 } 768 769 procnum = nd->nd_procnum; 770 if ((nd->nd_flag & ND_NFSV4) && 771 nd->nd_procnum != NFSPROC_NULL && 772 nd->nd_procnum != NFSV4PROC_CBCOMPOUND) 773 procnum = NFSV4PROC_COMPOUND; 774 775 if (nmp != NULL) { 776 NFSINCRGLOBAL(nfsstatsv1.rpcrequests); 777 778 /* Map the procnum to the old NFSv2 one, as required. */ 779 if ((nd->nd_flag & ND_NFSV2) != 0) { 780 if (nd->nd_procnum < NFS_V3NPROCS) 781 procnum = nfsv2_procid[nd->nd_procnum]; 782 else 783 procnum = NFSV2PROC_NOOP; 784 } 785 786 /* 787 * Now only used for the R_DONTRECOVER case, but until that is 788 * supported within the krpc code, I need to keep a queue of 789 * outstanding RPCs for nfsv4 client requests. 790 */ 791 if ((nd->nd_flag & ND_NFSV4) && procnum == NFSV4PROC_COMPOUND) 792 rep = malloc(sizeof(struct nfsreq), 793 M_NFSDREQ, M_WAITOK); 794 #ifdef KDTRACE_HOOKS 795 if (dtrace_nfscl_nfs234_start_probe != NULL) { 796 uint32_t probe_id; 797 int probe_procnum; 798 799 if (nd->nd_flag & ND_NFSV4) { 800 probe_id = 801 nfscl_nfs4_start_probes[nd->nd_procnum]; 802 probe_procnum = nd->nd_procnum; 803 } else if (nd->nd_flag & ND_NFSV3) { 804 probe_id = nfscl_nfs3_start_probes[procnum]; 805 probe_procnum = procnum; 806 } else { 807 probe_id = 808 nfscl_nfs2_start_probes[nd->nd_procnum]; 809 probe_procnum = procnum; 810 } 811 if (probe_id != 0) 812 (dtrace_nfscl_nfs234_start_probe) 813 (probe_id, vp, nd->nd_mreq, cred, 814 probe_procnum); 815 } 816 #endif 817 } 818 freeslot = -1; /* Set to slot that needs to be free'd */ 819 tryagain: 820 slot = -1; /* Slot that needs a sequence# increment. */ 821 /* 822 * This timeout specifies when a new socket should be created, 823 * along with new xid values. For UDP, this should be done 824 * infrequently, since retransmits of RPC requests should normally 825 * use the same xid. 826 */ 827 if (nmp == NULL) { 828 if (clp == NULL) { 829 timo.tv_sec = NFSV4_UPCALLTIMEO; 830 timo.tv_usec = 0; 831 } else { 832 timo.tv_sec = NFSV4_CALLBACKTIMEO / 1000; 833 timo.tv_usec = NFSV4_CALLBACKTIMEO * 1000; 834 } 835 } else { 836 if (nrp->nr_sotype != SOCK_DGRAM) { 837 timo.tv_usec = 0; 838 if ((nmp->nm_flag & NFSMNT_NFSV4)) 839 timo.tv_sec = INT_MAX; 840 else 841 timo.tv_sec = NFS_TCPTIMEO; 842 } else { 843 if (NFSHASSOFT(nmp)) { 844 /* 845 * CLSET_RETRIES is set to 2, so this should be 846 * half of the total timeout required. 847 */ 848 timeo = nmp->nm_retry * nmp->nm_timeo / 2; 849 if (timeo < 1) 850 timeo = 1; 851 timo.tv_sec = timeo / NFS_HZ; 852 timo.tv_usec = (timeo % NFS_HZ) * 1000000 / 853 NFS_HZ; 854 } else { 855 /* For UDP hard mounts, use a large value. */ 856 timo.tv_sec = NFS_MAXTIMEO / NFS_HZ; 857 timo.tv_usec = 0; 858 } 859 } 860 861 if (rep != NULL) { 862 rep->r_flags = 0; 863 rep->r_nmp = nmp; 864 /* 865 * Chain request into list of outstanding requests. 866 */ 867 NFSLOCKREQ(); 868 TAILQ_INSERT_TAIL(&nfsd_reqq, rep, r_chain); 869 NFSUNLOCKREQ(); 870 } 871 } 872 873 nd->nd_mrep = NULL; 874 if (clp != NULL && sep != NULL) 875 stat = clnt_bck_call(nrp->nr_client, &ext, procnum, 876 nd->nd_mreq, &nd->nd_mrep, timo, sep->nfsess_xprt); 877 else if (nextconn_set) 878 /* 879 * When there are multiple TCP connections, send the 880 * RPCs with large messages on the alternate TCP 881 * connection(s) in a round robin fashion. 882 * The small RPC messages are sent on the default 883 * TCP connection because they do not require much 884 * network bandwidth and separating them from the 885 * large RPC messages avoids them getting "log jammed" 886 * behind several large RPC messages. 887 */ 888 stat = CLNT_CALL_MBUF(nmp->nm_aconn[nextconn], 889 &ext, procnum, nd->nd_mreq, &nd->nd_mrep, timo); 890 else 891 stat = CLNT_CALL_MBUF(nrp->nr_client, &ext, procnum, 892 nd->nd_mreq, &nd->nd_mrep, timo); 893 NFSCL_DEBUG(2, "clnt call=%d\n", stat); 894 895 if (rep != NULL) { 896 /* 897 * RPC done, unlink the request. 898 */ 899 NFSLOCKREQ(); 900 TAILQ_REMOVE(&nfsd_reqq, rep, r_chain); 901 NFSUNLOCKREQ(); 902 } 903 904 /* 905 * If there was a successful reply and a tprintf msg. 906 * tprintf a response. 907 */ 908 if (stat == RPC_SUCCESS) { 909 error = 0; 910 } else if (stat == RPC_TIMEDOUT) { 911 NFSINCRGLOBAL(nfsstatsv1.rpctimeouts); 912 error = ETIMEDOUT; 913 } else if (stat == RPC_VERSMISMATCH) { 914 NFSINCRGLOBAL(nfsstatsv1.rpcinvalid); 915 error = EOPNOTSUPP; 916 } else if (stat == RPC_PROGVERSMISMATCH) { 917 NFSINCRGLOBAL(nfsstatsv1.rpcinvalid); 918 error = EPROTONOSUPPORT; 919 } else if (stat == RPC_INTR) { 920 error = EINTR; 921 } else if (stat == RPC_CANTSEND || stat == RPC_CANTRECV || 922 stat == RPC_SYSTEMERROR) { 923 /* Check for a session slot that needs to be free'd. */ 924 if ((nd->nd_flag & (ND_NFSV41 | ND_HASSLOTID)) == 925 (ND_NFSV41 | ND_HASSLOTID) && nmp != NULL && 926 nd->nd_procnum != NFSPROC_NULL) { 927 /* 928 * This should only occur when either the MDS or 929 * a client has an RPC against a DS fail. 930 * This happens because these cases use "soft" 931 * connections that can time out and fail. 932 * The slot used for this RPC is now in a 933 * non-deterministic state, but if the slot isn't 934 * free'd, threads can get stuck waiting for a slot. 935 */ 936 if (sep == NULL) 937 sep = nfsmnt_mdssession(nmp); 938 /* 939 * Bump the sequence# out of range, so that reuse of 940 * this slot will result in an NFSERR_SEQMISORDERED 941 * error and not a bogus cached RPC reply. 942 */ 943 mtx_lock(&sep->nfsess_mtx); 944 sep->nfsess_slotseq[nd->nd_slotid] += 10; 945 mtx_unlock(&sep->nfsess_mtx); 946 /* And free the slot. */ 947 nfsv4_freeslot(sep, nd->nd_slotid, false); 948 } 949 NFSINCRGLOBAL(nfsstatsv1.rpcinvalid); 950 error = ENXIO; 951 } else { 952 NFSINCRGLOBAL(nfsstatsv1.rpcinvalid); 953 error = EACCES; 954 } 955 if (error) { 956 m_freem(nd->nd_mreq); 957 if (usegssname == 0) 958 AUTH_DESTROY(auth); 959 if (rep != NULL) 960 free(rep, M_NFSDREQ); 961 if (set_sigset) 962 newnfs_restore_sigmask(td, &oldset); 963 return (error); 964 } 965 966 KASSERT(nd->nd_mrep != NULL, ("mrep shouldn't be NULL if no error\n")); 967 968 /* 969 * Search for any mbufs that are not a multiple of 4 bytes long 970 * or with m_data not longword aligned. 971 * These could cause pointer alignment problems, so copy them to 972 * well aligned mbufs. 973 */ 974 newnfs_realign(&nd->nd_mrep, M_WAITOK); 975 nd->nd_md = nd->nd_mrep; 976 nd->nd_dpos = mtod(nd->nd_md, caddr_t); 977 nd->nd_repstat = 0; 978 if (nd->nd_procnum != NFSPROC_NULL && 979 nd->nd_procnum != NFSV4PROC_CBNULL) { 980 /* If sep == NULL, set it to the default in nmp. */ 981 if (sep == NULL && nmp != NULL) 982 sep = nfsmnt_mdssession(nmp); 983 /* 984 * and now the actual NFS xdr. 985 */ 986 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 987 nd->nd_repstat = fxdr_unsigned(u_int32_t, *tl); 988 if (nd->nd_repstat >= 10000) 989 NFSCL_DEBUG(1, "proc=%d reps=%d\n", (int)nd->nd_procnum, 990 (int)nd->nd_repstat); 991 992 /* 993 * Get rid of the tag, return count and SEQUENCE result for 994 * NFSv4. 995 */ 996 if ((nd->nd_flag & ND_NFSV4) != 0 && nd->nd_repstat != 997 NFSERR_MINORVERMISMATCH) { 998 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 999 i = fxdr_unsigned(int, *tl); 1000 error = nfsm_advance(nd, NFSM_RNDUP(i), -1); 1001 if (error) 1002 goto nfsmout; 1003 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1004 opcnt = fxdr_unsigned(int, *tl++); 1005 i = fxdr_unsigned(int, *tl++); 1006 j = fxdr_unsigned(int, *tl); 1007 if (j >= 10000) 1008 NFSCL_DEBUG(1, "fop=%d fst=%d\n", i, j); 1009 /* 1010 * If the first op is Sequence, free up the slot. 1011 */ 1012 if ((nmp != NULL && i == NFSV4OP_SEQUENCE && j != 0) || 1013 (clp != NULL && i == NFSV4OP_CBSEQUENCE && j != 0)) 1014 NFSCL_DEBUG(1, "failed seq=%d\n", j); 1015 if (((nmp != NULL && i == NFSV4OP_SEQUENCE && j == 0) || 1016 (clp != NULL && i == NFSV4OP_CBSEQUENCE && 1017 j == 0)) && sep != NULL) { 1018 if (i == NFSV4OP_SEQUENCE) 1019 NFSM_DISSECT(tl, uint32_t *, 1020 NFSX_V4SESSIONID + 1021 5 * NFSX_UNSIGNED); 1022 else 1023 NFSM_DISSECT(tl, uint32_t *, 1024 NFSX_V4SESSIONID + 1025 4 * NFSX_UNSIGNED); 1026 mtx_lock(&sep->nfsess_mtx); 1027 if (bcmp(tl, sep->nfsess_sessionid, 1028 NFSX_V4SESSIONID) == 0) { 1029 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED; 1030 retseq = fxdr_unsigned(uint32_t, *tl++); 1031 slot = fxdr_unsigned(int, *tl++); 1032 freeslot = slot; 1033 if (retseq != sep->nfsess_slotseq[slot]) 1034 printf("retseq diff 0x%x\n", 1035 retseq); 1036 retval = fxdr_unsigned(uint32_t, *++tl); 1037 if ((retval + 1) < sep->nfsess_foreslots 1038 ) 1039 sep->nfsess_foreslots = (retval 1040 + 1); 1041 else if ((retval + 1) > 1042 sep->nfsess_foreslots) 1043 sep->nfsess_foreslots = (retval 1044 < 64) ? (retval + 1) : 64; 1045 } 1046 mtx_unlock(&sep->nfsess_mtx); 1047 1048 /* Grab the op and status for the next one. */ 1049 if (opcnt > 1) { 1050 NFSM_DISSECT(tl, uint32_t *, 1051 2 * NFSX_UNSIGNED); 1052 i = fxdr_unsigned(int, *tl++); 1053 j = fxdr_unsigned(int, *tl); 1054 } 1055 } 1056 } 1057 if (nd->nd_repstat != 0) { 1058 if (nd->nd_repstat == NFSERR_BADSESSION && 1059 nmp != NULL && dssep == NULL && 1060 (nd->nd_flag & ND_NFSV41) != 0) { 1061 /* 1062 * If this is a client side MDS RPC, mark 1063 * the MDS session defunct and initiate 1064 * recovery, as required. 1065 * The nfsess_defunct field is protected by 1066 * the NFSLOCKMNT()/nm_mtx lock and not the 1067 * nfsess_mtx lock to simplify its handling, 1068 * for the MDS session. This lock is also 1069 * sufficient for nfsess_sessionid, since it 1070 * never changes in the structure. 1071 */ 1072 NFSCL_DEBUG(1, "Got badsession\n"); 1073 NFSLOCKCLSTATE(); 1074 NFSLOCKMNT(nmp); 1075 sep = NFSMNT_MDSSESSION(nmp); 1076 if (bcmp(sep->nfsess_sessionid, nd->nd_sequence, 1077 NFSX_V4SESSIONID) == 0) { 1078 /* Initiate recovery. */ 1079 sep->nfsess_defunct = 1; 1080 NFSCL_DEBUG(1, "Marked defunct\n"); 1081 if (nmp->nm_clp != NULL) { 1082 nmp->nm_clp->nfsc_flags |= 1083 NFSCLFLAGS_RECOVER; 1084 wakeup(nmp->nm_clp); 1085 } 1086 } 1087 NFSUNLOCKCLSTATE(); 1088 /* 1089 * Sleep for up to 1sec waiting for a new 1090 * session. 1091 */ 1092 mtx_sleep(&nmp->nm_sess, &nmp->nm_mtx, PZERO, 1093 "nfsbadsess", hz); 1094 /* 1095 * Get the session again, in case a new one 1096 * has been created during the sleep. 1097 */ 1098 sep = NFSMNT_MDSSESSION(nmp); 1099 NFSUNLOCKMNT(nmp); 1100 if ((nd->nd_flag & ND_LOOPBADSESS) != 0) { 1101 reterr = nfsv4_sequencelookup(nmp, sep, 1102 &slotpos, &maxslot, &slotseq, 1103 sessionid); 1104 if (reterr == 0) { 1105 /* Fill in new session info. */ 1106 NFSCL_DEBUG(1, 1107 "Filling in new sequence\n"); 1108 tl = nd->nd_sequence; 1109 bcopy(sessionid, tl, 1110 NFSX_V4SESSIONID); 1111 tl += NFSX_V4SESSIONID / 1112 NFSX_UNSIGNED; 1113 *tl++ = txdr_unsigned(slotseq); 1114 *tl++ = txdr_unsigned(slotpos); 1115 *tl = txdr_unsigned(maxslot); 1116 } 1117 if (reterr == NFSERR_BADSESSION || 1118 reterr == 0) { 1119 NFSCL_DEBUG(1, 1120 "Badsession looping\n"); 1121 m_freem(nd->nd_mrep); 1122 nd->nd_mrep = NULL; 1123 goto tryagain; 1124 } 1125 nd->nd_repstat = reterr; 1126 NFSCL_DEBUG(1, "Got err=%d\n", reterr); 1127 } 1128 } 1129 /* 1130 * When clp != NULL, it is a callback and all 1131 * callback operations can be retried for NFSERR_DELAY. 1132 */ 1133 if (((nd->nd_repstat == NFSERR_DELAY || 1134 nd->nd_repstat == NFSERR_GRACE) && 1135 (nd->nd_flag & ND_NFSV4) && (clp != NULL || 1136 (nd->nd_procnum != NFSPROC_DELEGRETURN && 1137 nd->nd_procnum != NFSPROC_SETATTR && 1138 nd->nd_procnum != NFSPROC_READ && 1139 nd->nd_procnum != NFSPROC_READDS && 1140 nd->nd_procnum != NFSPROC_WRITE && 1141 nd->nd_procnum != NFSPROC_WRITEDS && 1142 nd->nd_procnum != NFSPROC_OPEN && 1143 nd->nd_procnum != NFSPROC_OPENLAYGET && 1144 nd->nd_procnum != NFSPROC_CREATE && 1145 nd->nd_procnum != NFSPROC_CREATELAYGET && 1146 nd->nd_procnum != NFSPROC_OPENCONFIRM && 1147 nd->nd_procnum != NFSPROC_OPENDOWNGRADE && 1148 nd->nd_procnum != NFSPROC_CLOSE && 1149 nd->nd_procnum != NFSPROC_LOCK && 1150 nd->nd_procnum != NFSPROC_LOCKU))) || 1151 (nd->nd_repstat == NFSERR_DELAY && 1152 (nd->nd_flag & ND_NFSV4) == 0) || 1153 nd->nd_repstat == NFSERR_RESOURCE) { 1154 /* Clip at NFS_TRYLATERDEL. */ 1155 if (timespeccmp(&trylater_delay, 1156 &nfs_trylater_max, >)) 1157 trylater_delay = nfs_trylater_max; 1158 getnanouptime(&waituntil); 1159 timespecadd(&waituntil, &trylater_delay, 1160 &waituntil); 1161 do { 1162 nfs_catnap(PZERO, 0, "nfstry"); 1163 getnanouptime(&ts); 1164 } while (timespeccmp(&ts, &waituntil, <)); 1165 timespecadd(&trylater_delay, &trylater_delay, 1166 &trylater_delay); /* Double each time. */ 1167 if (slot != -1) { 1168 mtx_lock(&sep->nfsess_mtx); 1169 sep->nfsess_slotseq[slot]++; 1170 *nd->nd_slotseq = txdr_unsigned( 1171 sep->nfsess_slotseq[slot]); 1172 mtx_unlock(&sep->nfsess_mtx); 1173 } 1174 m_freem(nd->nd_mrep); 1175 nd->nd_mrep = NULL; 1176 goto tryagain; 1177 } 1178 1179 /* 1180 * If the File Handle was stale, invalidate the 1181 * lookup cache, just in case. 1182 * (vp != NULL implies a client side call) 1183 */ 1184 if (nd->nd_repstat == ESTALE && vp != NULL) { 1185 cache_purge(vp); 1186 if (ncl_call_invalcaches != NULL) 1187 (*ncl_call_invalcaches)(vp); 1188 } 1189 } 1190 if ((nd->nd_flag & ND_NFSV4) != 0) { 1191 /* Free the slot, as required. */ 1192 if (freeslot != -1) 1193 nfsv4_freeslot(sep, freeslot, false); 1194 /* 1195 * If this op is Putfh, throw its results away. 1196 */ 1197 if (j >= 10000) 1198 NFSCL_DEBUG(1, "nop=%d nst=%d\n", i, j); 1199 if (nmp != NULL && i == NFSV4OP_PUTFH && j == 0) { 1200 NFSM_DISSECT(tl,u_int32_t *,2 * NFSX_UNSIGNED); 1201 i = fxdr_unsigned(int, *tl++); 1202 j = fxdr_unsigned(int, *tl); 1203 if (j >= 10000) 1204 NFSCL_DEBUG(1, "n2op=%d n2st=%d\n", i, 1205 j); 1206 /* 1207 * All Compounds that do an Op that must 1208 * be in sequence consist of NFSV4OP_PUTFH 1209 * followed by one of these. As such, we 1210 * can determine if the seqid# should be 1211 * incremented, here. 1212 */ 1213 if ((i == NFSV4OP_OPEN || 1214 i == NFSV4OP_OPENCONFIRM || 1215 i == NFSV4OP_OPENDOWNGRADE || 1216 i == NFSV4OP_CLOSE || 1217 i == NFSV4OP_LOCK || 1218 i == NFSV4OP_LOCKU) && 1219 (j == 0 || 1220 (j != NFSERR_STALECLIENTID && 1221 j != NFSERR_STALESTATEID && 1222 j != NFSERR_BADSTATEID && 1223 j != NFSERR_BADSEQID && 1224 j != NFSERR_BADXDR && 1225 j != NFSERR_RESOURCE && 1226 j != NFSERR_NOFILEHANDLE))) 1227 nd->nd_flag |= ND_INCRSEQID; 1228 } 1229 /* 1230 * If this op's status is non-zero, mark 1231 * that there is no more data to process. 1232 * The exception is Setattr, which always has xdr 1233 * when it has failed. 1234 */ 1235 if (j != 0 && i != NFSV4OP_SETATTR) 1236 nd->nd_flag |= ND_NOMOREDATA; 1237 1238 /* 1239 * If R_DONTRECOVER is set, replace the stale error 1240 * reply, so that recovery isn't initiated. 1241 */ 1242 if ((nd->nd_repstat == NFSERR_STALECLIENTID || 1243 nd->nd_repstat == NFSERR_BADSESSION || 1244 nd->nd_repstat == NFSERR_STALESTATEID) && 1245 rep != NULL && (rep->r_flags & R_DONTRECOVER)) 1246 nd->nd_repstat = NFSERR_STALEDONTRECOVER; 1247 } 1248 } 1249 1250 #ifdef KDTRACE_HOOKS 1251 if (nmp != NULL && dtrace_nfscl_nfs234_done_probe != NULL) { 1252 uint32_t probe_id; 1253 int probe_procnum; 1254 1255 if (nd->nd_flag & ND_NFSV4) { 1256 probe_id = nfscl_nfs4_done_probes[nd->nd_procnum]; 1257 probe_procnum = nd->nd_procnum; 1258 } else if (nd->nd_flag & ND_NFSV3) { 1259 probe_id = nfscl_nfs3_done_probes[procnum]; 1260 probe_procnum = procnum; 1261 } else { 1262 probe_id = nfscl_nfs2_done_probes[nd->nd_procnum]; 1263 probe_procnum = procnum; 1264 } 1265 if (probe_id != 0) 1266 (dtrace_nfscl_nfs234_done_probe)(probe_id, vp, 1267 nd->nd_mreq, cred, probe_procnum, 0); 1268 } 1269 #endif 1270 1271 m_freem(nd->nd_mreq); 1272 if (usegssname == 0) 1273 AUTH_DESTROY(auth); 1274 if (rep != NULL) 1275 free(rep, M_NFSDREQ); 1276 if (set_sigset) 1277 newnfs_restore_sigmask(td, &oldset); 1278 return (0); 1279 nfsmout: 1280 m_freem(nd->nd_mrep); 1281 m_freem(nd->nd_mreq); 1282 if (usegssname == 0) 1283 AUTH_DESTROY(auth); 1284 if (rep != NULL) 1285 free(rep, M_NFSDREQ); 1286 if (set_sigset) 1287 newnfs_restore_sigmask(td, &oldset); 1288 return (error); 1289 } 1290 1291 /* 1292 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and 1293 * wait for all requests to complete. This is used by forced unmounts 1294 * to terminate any outstanding RPCs. 1295 */ 1296 int 1297 newnfs_nmcancelreqs(struct nfsmount *nmp) 1298 { 1299 struct nfsclds *dsp; 1300 struct __rpc_client *cl; 1301 int i; 1302 1303 if (nmp->nm_sockreq.nr_client != NULL) 1304 CLNT_CLOSE(nmp->nm_sockreq.nr_client); 1305 for (i = 0; i < nmp->nm_aconnect; i++) 1306 if (nmp->nm_aconn[i] != NULL) 1307 CLNT_CLOSE(nmp->nm_aconn[i]); 1308 lookformore: 1309 NFSLOCKMNT(nmp); 1310 TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) { 1311 NFSLOCKDS(dsp); 1312 if (dsp != TAILQ_FIRST(&nmp->nm_sess) && 1313 (dsp->nfsclds_flags & NFSCLDS_CLOSED) == 0 && 1314 dsp->nfsclds_sockp != NULL && 1315 dsp->nfsclds_sockp->nr_client != NULL) { 1316 dsp->nfsclds_flags |= NFSCLDS_CLOSED; 1317 cl = dsp->nfsclds_sockp->nr_client; 1318 NFSUNLOCKDS(dsp); 1319 NFSUNLOCKMNT(nmp); 1320 CLNT_CLOSE(cl); 1321 goto lookformore; 1322 } 1323 NFSUNLOCKDS(dsp); 1324 } 1325 NFSUNLOCKMNT(nmp); 1326 return (0); 1327 } 1328 1329 /* 1330 * Any signal that can interrupt an NFS operation in an intr mount 1331 * should be added to this set. SIGSTOP and SIGKILL cannot be masked. 1332 */ 1333 int newnfs_sig_set[] = { 1334 SIGINT, 1335 SIGTERM, 1336 SIGHUP, 1337 SIGKILL, 1338 SIGQUIT 1339 }; 1340 1341 /* 1342 * Check to see if one of the signals in our subset is pending on 1343 * the process (in an intr mount). 1344 */ 1345 static int 1346 nfs_sig_pending(sigset_t set) 1347 { 1348 int i; 1349 1350 for (i = 0 ; i < nitems(newnfs_sig_set); i++) 1351 if (SIGISMEMBER(set, newnfs_sig_set[i])) 1352 return (1); 1353 return (0); 1354 } 1355 1356 /* 1357 * The set/restore sigmask functions are used to (temporarily) overwrite 1358 * the thread td_sigmask during an RPC call (for example). These are also 1359 * used in other places in the NFS client that might tsleep(). 1360 */ 1361 void 1362 newnfs_set_sigmask(struct thread *td, sigset_t *oldset) 1363 { 1364 sigset_t newset; 1365 int i; 1366 struct proc *p; 1367 1368 SIGFILLSET(newset); 1369 if (td == NULL) 1370 td = curthread; /* XXX */ 1371 p = td->td_proc; 1372 /* Remove the NFS set of signals from newset */ 1373 PROC_LOCK(p); 1374 mtx_lock(&p->p_sigacts->ps_mtx); 1375 for (i = 0 ; i < nitems(newnfs_sig_set); i++) { 1376 /* 1377 * But make sure we leave the ones already masked 1378 * by the process, ie. remove the signal from the 1379 * temporary signalmask only if it wasn't already 1380 * in p_sigmask. 1381 */ 1382 if (!SIGISMEMBER(td->td_sigmask, newnfs_sig_set[i]) && 1383 !SIGISMEMBER(p->p_sigacts->ps_sigignore, newnfs_sig_set[i])) 1384 SIGDELSET(newset, newnfs_sig_set[i]); 1385 } 1386 mtx_unlock(&p->p_sigacts->ps_mtx); 1387 kern_sigprocmask(td, SIG_SETMASK, &newset, oldset, 1388 SIGPROCMASK_PROC_LOCKED); 1389 PROC_UNLOCK(p); 1390 } 1391 1392 void 1393 newnfs_restore_sigmask(struct thread *td, sigset_t *set) 1394 { 1395 if (td == NULL) 1396 td = curthread; /* XXX */ 1397 kern_sigprocmask(td, SIG_SETMASK, set, NULL, 0); 1398 } 1399 1400 /* 1401 * NFS wrapper to msleep(), that shoves a new p_sigmask and restores the 1402 * old one after msleep() returns. 1403 */ 1404 int 1405 newnfs_msleep(struct thread *td, void *ident, struct mtx *mtx, int priority, char *wmesg, int timo) 1406 { 1407 sigset_t oldset; 1408 int error; 1409 1410 if ((priority & PCATCH) == 0) 1411 return msleep(ident, mtx, priority, wmesg, timo); 1412 if (td == NULL) 1413 td = curthread; /* XXX */ 1414 newnfs_set_sigmask(td, &oldset); 1415 error = msleep(ident, mtx, priority, wmesg, timo); 1416 newnfs_restore_sigmask(td, &oldset); 1417 return (error); 1418 } 1419 1420 /* 1421 * Test for a termination condition pending on the process. 1422 * This is used for NFSMNT_INT mounts. 1423 */ 1424 int 1425 newnfs_sigintr(struct nfsmount *nmp, struct thread *td) 1426 { 1427 struct proc *p; 1428 sigset_t tmpset; 1429 1430 /* Terminate all requests while attempting a forced unmount. */ 1431 if (NFSCL_FORCEDISM(nmp->nm_mountp)) 1432 return (EIO); 1433 if (!(nmp->nm_flag & NFSMNT_INT)) 1434 return (0); 1435 if (td == NULL) 1436 return (0); 1437 p = td->td_proc; 1438 PROC_LOCK(p); 1439 tmpset = p->p_siglist; 1440 SIGSETOR(tmpset, td->td_siglist); 1441 SIGSETNAND(tmpset, td->td_sigmask); 1442 mtx_lock(&p->p_sigacts->ps_mtx); 1443 SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore); 1444 mtx_unlock(&p->p_sigacts->ps_mtx); 1445 if ((SIGNOTEMPTY(p->p_siglist) || SIGNOTEMPTY(td->td_siglist)) 1446 && nfs_sig_pending(tmpset)) { 1447 PROC_UNLOCK(p); 1448 return (EINTR); 1449 } 1450 PROC_UNLOCK(p); 1451 return (0); 1452 } 1453 1454 static int 1455 nfs_msg(struct thread *td, const char *server, const char *msg, int error) 1456 { 1457 struct proc *p; 1458 1459 p = td ? td->td_proc : NULL; 1460 if (error) { 1461 tprintf(p, LOG_INFO, "nfs server %s: %s, error %d\n", 1462 server, msg, error); 1463 } else { 1464 tprintf(p, LOG_INFO, "nfs server %s: %s\n", server, msg); 1465 } 1466 return (0); 1467 } 1468 1469 static void 1470 nfs_down(struct nfsmount *nmp, struct thread *td, const char *msg, 1471 int error, int flags) 1472 { 1473 if (nmp == NULL) 1474 return; 1475 mtx_lock(&nmp->nm_mtx); 1476 if ((flags & NFSSTA_TIMEO) && !(nmp->nm_state & NFSSTA_TIMEO)) { 1477 nmp->nm_state |= NFSSTA_TIMEO; 1478 mtx_unlock(&nmp->nm_mtx); 1479 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid, 1480 VQ_NOTRESP, 0); 1481 } else 1482 mtx_unlock(&nmp->nm_mtx); 1483 mtx_lock(&nmp->nm_mtx); 1484 if ((flags & NFSSTA_LOCKTIMEO) && !(nmp->nm_state & NFSSTA_LOCKTIMEO)) { 1485 nmp->nm_state |= NFSSTA_LOCKTIMEO; 1486 mtx_unlock(&nmp->nm_mtx); 1487 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid, 1488 VQ_NOTRESPLOCK, 0); 1489 } else 1490 mtx_unlock(&nmp->nm_mtx); 1491 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error); 1492 } 1493 1494 static void 1495 nfs_up(struct nfsmount *nmp, struct thread *td, const char *msg, 1496 int flags, int tprintfmsg) 1497 { 1498 if (nmp == NULL) 1499 return; 1500 if (tprintfmsg) { 1501 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0); 1502 } 1503 1504 mtx_lock(&nmp->nm_mtx); 1505 if ((flags & NFSSTA_TIMEO) && (nmp->nm_state & NFSSTA_TIMEO)) { 1506 nmp->nm_state &= ~NFSSTA_TIMEO; 1507 mtx_unlock(&nmp->nm_mtx); 1508 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid, 1509 VQ_NOTRESP, 1); 1510 } else 1511 mtx_unlock(&nmp->nm_mtx); 1512 1513 mtx_lock(&nmp->nm_mtx); 1514 if ((flags & NFSSTA_LOCKTIMEO) && (nmp->nm_state & NFSSTA_LOCKTIMEO)) { 1515 nmp->nm_state &= ~NFSSTA_LOCKTIMEO; 1516 mtx_unlock(&nmp->nm_mtx); 1517 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid, 1518 VQ_NOTRESPLOCK, 1); 1519 } else 1520 mtx_unlock(&nmp->nm_mtx); 1521 } 1522