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