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