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