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 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 if ((nd->nd_flag & ND_NFSV41) != 0 && nmp != NULL && 813 nd->nd_procnum != NFSPROC_NULL) { 814 /* 815 * The nfsess_defunct field is protected by 816 * the NFSLOCKMNT()/nm_mtx lock and not the 817 * nfsess_mtx lock to simplify its handling, 818 * for the MDS session. This lock is also 819 * sufficient for nfsess_sessionid, since it 820 * never changes in the structure. 821 */ 822 NFSLOCKCLSTATE(); 823 NFSLOCKMNT(nmp); 824 /* The session must be marked defunct. */ 825 if (dssep == NULL) { 826 /* 827 * This is either an MDS proxy operation or 828 * a client mount with "soft,retrans=N" options. 829 * Mark the MDS session defunct and initiate 830 * recovery, as required. 831 */ 832 NFSCL_DEBUG(1, "Failed soft proxy RPC\n"); 833 sep = NFSMNT_MDSSESSION(nmp); 834 if (bcmp(sep->nfsess_sessionid, nd->nd_sequence, 835 NFSX_V4SESSIONID) == 0) { 836 /* Initiate recovery. */ 837 sep->nfsess_defunct = 1; 838 NFSCL_DEBUG(1, "Marked defunct\n"); 839 if (nmp->nm_clp != NULL) { 840 nmp->nm_clp->nfsc_flags |= 841 NFSCLFLAGS_RECOVER; 842 wakeup(nmp->nm_clp); 843 } 844 } 845 } else { 846 /* 847 * This is a client side DS RPC. Just mark 848 * the session defunct. A subsequent LayoutGet 849 * should get a new session. 850 */ 851 NFSCL_DEBUG(1, "Failed client DS RPC\n"); 852 if (bcmp(dssep->nfsess_sessionid, 853 nd->nd_sequence, NFSX_V4SESSIONID) == 0) { 854 /* Mark it defunct. */ 855 dssep->nfsess_defunct = 1; 856 NFSCL_DEBUG(1, "Marked defunct\n"); 857 } 858 } 859 NFSUNLOCKMNT(nmp); 860 NFSUNLOCKCLSTATE(); 861 } 862 NFSINCRGLOBAL(nfsstatsv1.rpcinvalid); 863 error = ENXIO; 864 } else { 865 NFSINCRGLOBAL(nfsstatsv1.rpcinvalid); 866 error = EACCES; 867 } 868 if (error) { 869 m_freem(nd->nd_mreq); 870 if (usegssname == 0) 871 AUTH_DESTROY(auth); 872 if (rep != NULL) 873 free(rep, M_NFSDREQ); 874 if (set_sigset) 875 newnfs_restore_sigmask(td, &oldset); 876 return (error); 877 } 878 879 KASSERT(nd->nd_mrep != NULL, ("mrep shouldn't be NULL if no error\n")); 880 881 /* 882 * Search for any mbufs that are not a multiple of 4 bytes long 883 * or with m_data not longword aligned. 884 * These could cause pointer alignment problems, so copy them to 885 * well aligned mbufs. 886 */ 887 newnfs_realign(&nd->nd_mrep, M_WAITOK); 888 nd->nd_md = nd->nd_mrep; 889 nd->nd_dpos = NFSMTOD(nd->nd_md, caddr_t); 890 nd->nd_repstat = 0; 891 if (nd->nd_procnum != NFSPROC_NULL && 892 nd->nd_procnum != NFSV4PROC_CBNULL) { 893 /* If sep == NULL, set it to the default in nmp. */ 894 if (sep == NULL && nmp != NULL) 895 sep = nfsmnt_mdssession(nmp); 896 /* 897 * and now the actual NFS xdr. 898 */ 899 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 900 nd->nd_repstat = fxdr_unsigned(u_int32_t, *tl); 901 if (nd->nd_repstat >= 10000) 902 NFSCL_DEBUG(1, "proc=%d reps=%d\n", (int)nd->nd_procnum, 903 (int)nd->nd_repstat); 904 905 /* 906 * Get rid of the tag, return count and SEQUENCE result for 907 * NFSv4. 908 */ 909 if ((nd->nd_flag & ND_NFSV4) != 0) { 910 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 911 i = fxdr_unsigned(int, *tl); 912 error = nfsm_advance(nd, NFSM_RNDUP(i), -1); 913 if (error) 914 goto nfsmout; 915 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 916 opcnt = fxdr_unsigned(int, *tl++); 917 i = fxdr_unsigned(int, *tl++); 918 j = fxdr_unsigned(int, *tl); 919 if (j >= 10000) 920 NFSCL_DEBUG(1, "fop=%d fst=%d\n", i, j); 921 /* 922 * If the first op is Sequence, free up the slot. 923 */ 924 if ((nmp != NULL && i == NFSV4OP_SEQUENCE && j != 0) || 925 (clp != NULL && i == NFSV4OP_CBSEQUENCE && j != 0)) 926 NFSCL_DEBUG(1, "failed seq=%d\n", j); 927 if (((nmp != NULL && i == NFSV4OP_SEQUENCE && j == 0) || 928 (clp != NULL && i == NFSV4OP_CBSEQUENCE && 929 j == 0)) && sep != NULL) { 930 if (i == NFSV4OP_SEQUENCE) 931 NFSM_DISSECT(tl, uint32_t *, 932 NFSX_V4SESSIONID + 933 5 * NFSX_UNSIGNED); 934 else 935 NFSM_DISSECT(tl, uint32_t *, 936 NFSX_V4SESSIONID + 937 4 * NFSX_UNSIGNED); 938 mtx_lock(&sep->nfsess_mtx); 939 if (bcmp(tl, sep->nfsess_sessionid, 940 NFSX_V4SESSIONID) == 0) { 941 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED; 942 retseq = fxdr_unsigned(uint32_t, *tl++); 943 slot = fxdr_unsigned(int, *tl++); 944 freeslot = slot; 945 if (retseq != sep->nfsess_slotseq[slot]) 946 printf("retseq diff 0x%x\n", 947 retseq); 948 retval = fxdr_unsigned(uint32_t, *++tl); 949 if ((retval + 1) < sep->nfsess_foreslots 950 ) 951 sep->nfsess_foreslots = (retval 952 + 1); 953 else if ((retval + 1) > 954 sep->nfsess_foreslots) 955 sep->nfsess_foreslots = (retval 956 < 64) ? (retval + 1) : 64; 957 } 958 mtx_unlock(&sep->nfsess_mtx); 959 960 /* Grab the op and status for the next one. */ 961 if (opcnt > 1) { 962 NFSM_DISSECT(tl, uint32_t *, 963 2 * NFSX_UNSIGNED); 964 i = fxdr_unsigned(int, *tl++); 965 j = fxdr_unsigned(int, *tl); 966 } 967 } 968 } 969 if (nd->nd_repstat != 0) { 970 if (nd->nd_repstat == NFSERR_BADSESSION && 971 nmp != NULL && dssep == NULL && 972 (nd->nd_flag & ND_NFSV41) != 0) { 973 /* 974 * If this is a client side MDS RPC, mark 975 * the MDS session defunct and initiate 976 * recovery, as required. 977 * The nfsess_defunct field is protected by 978 * the NFSLOCKMNT()/nm_mtx lock and not the 979 * nfsess_mtx lock to simplify its handling, 980 * for the MDS session. This lock is also 981 * sufficient for nfsess_sessionid, since it 982 * never changes in the structure. 983 */ 984 NFSCL_DEBUG(1, "Got badsession\n"); 985 NFSLOCKCLSTATE(); 986 NFSLOCKMNT(nmp); 987 sep = NFSMNT_MDSSESSION(nmp); 988 if (bcmp(sep->nfsess_sessionid, nd->nd_sequence, 989 NFSX_V4SESSIONID) == 0) { 990 /* Initiate recovery. */ 991 sep->nfsess_defunct = 1; 992 NFSCL_DEBUG(1, "Marked defunct\n"); 993 if (nmp->nm_clp != NULL) { 994 nmp->nm_clp->nfsc_flags |= 995 NFSCLFLAGS_RECOVER; 996 wakeup(nmp->nm_clp); 997 } 998 } 999 NFSUNLOCKCLSTATE(); 1000 /* 1001 * Sleep for up to 1sec waiting for a new 1002 * session. 1003 */ 1004 mtx_sleep(&nmp->nm_sess, &nmp->nm_mtx, PZERO, 1005 "nfsbadsess", hz); 1006 /* 1007 * Get the session again, in case a new one 1008 * has been created during the sleep. 1009 */ 1010 sep = NFSMNT_MDSSESSION(nmp); 1011 NFSUNLOCKMNT(nmp); 1012 if ((nd->nd_flag & ND_LOOPBADSESS) != 0) { 1013 reterr = nfsv4_sequencelookup(nmp, sep, 1014 &slotpos, &maxslot, &slotseq, 1015 sessionid); 1016 if (reterr == 0) { 1017 /* Fill in new session info. */ 1018 NFSCL_DEBUG(1, 1019 "Filling in new sequence\n"); 1020 tl = nd->nd_sequence; 1021 bcopy(sessionid, tl, 1022 NFSX_V4SESSIONID); 1023 tl += NFSX_V4SESSIONID / 1024 NFSX_UNSIGNED; 1025 *tl++ = txdr_unsigned(slotseq); 1026 *tl++ = txdr_unsigned(slotpos); 1027 *tl = txdr_unsigned(maxslot); 1028 } 1029 if (reterr == NFSERR_BADSESSION || 1030 reterr == 0) { 1031 NFSCL_DEBUG(1, 1032 "Badsession looping\n"); 1033 m_freem(nd->nd_mrep); 1034 nd->nd_mrep = NULL; 1035 goto tryagain; 1036 } 1037 nd->nd_repstat = reterr; 1038 NFSCL_DEBUG(1, "Got err=%d\n", reterr); 1039 } 1040 } 1041 if (((nd->nd_repstat == NFSERR_DELAY || 1042 nd->nd_repstat == NFSERR_GRACE) && 1043 (nd->nd_flag & ND_NFSV4) && 1044 nd->nd_procnum != NFSPROC_DELEGRETURN && 1045 nd->nd_procnum != NFSPROC_SETATTR && 1046 nd->nd_procnum != NFSPROC_READ && 1047 nd->nd_procnum != NFSPROC_READDS && 1048 nd->nd_procnum != NFSPROC_WRITE && 1049 nd->nd_procnum != NFSPROC_WRITEDS && 1050 nd->nd_procnum != NFSPROC_OPEN && 1051 nd->nd_procnum != NFSPROC_CREATE && 1052 nd->nd_procnum != NFSPROC_OPENCONFIRM && 1053 nd->nd_procnum != NFSPROC_OPENDOWNGRADE && 1054 nd->nd_procnum != NFSPROC_CLOSE && 1055 nd->nd_procnum != NFSPROC_LOCK && 1056 nd->nd_procnum != NFSPROC_LOCKU) || 1057 (nd->nd_repstat == NFSERR_DELAY && 1058 (nd->nd_flag & ND_NFSV4) == 0) || 1059 nd->nd_repstat == NFSERR_RESOURCE) { 1060 if (trylater_delay > NFS_TRYLATERDEL) 1061 trylater_delay = NFS_TRYLATERDEL; 1062 waituntil = NFSD_MONOSEC + trylater_delay; 1063 while (NFSD_MONOSEC < waituntil) 1064 (void) nfs_catnap(PZERO, 0, "nfstry"); 1065 trylater_delay *= 2; 1066 if (slot != -1) { 1067 mtx_lock(&sep->nfsess_mtx); 1068 sep->nfsess_slotseq[slot]++; 1069 *nd->nd_slotseq = txdr_unsigned( 1070 sep->nfsess_slotseq[slot]); 1071 mtx_unlock(&sep->nfsess_mtx); 1072 } 1073 m_freem(nd->nd_mrep); 1074 nd->nd_mrep = NULL; 1075 goto tryagain; 1076 } 1077 1078 /* 1079 * If the File Handle was stale, invalidate the 1080 * lookup cache, just in case. 1081 * (vp != NULL implies a client side call) 1082 */ 1083 if (nd->nd_repstat == ESTALE && vp != NULL) { 1084 cache_purge(vp); 1085 if (ncl_call_invalcaches != NULL) 1086 (*ncl_call_invalcaches)(vp); 1087 } 1088 } 1089 if ((nd->nd_flag & ND_NFSV4) != 0) { 1090 /* Free the slot, as required. */ 1091 if (freeslot != -1) 1092 nfsv4_freeslot(sep, freeslot); 1093 /* 1094 * If this op is Putfh, throw its results away. 1095 */ 1096 if (j >= 10000) 1097 NFSCL_DEBUG(1, "nop=%d nst=%d\n", i, j); 1098 if (nmp != NULL && i == NFSV4OP_PUTFH && j == 0) { 1099 NFSM_DISSECT(tl,u_int32_t *,2 * NFSX_UNSIGNED); 1100 i = fxdr_unsigned(int, *tl++); 1101 j = fxdr_unsigned(int, *tl); 1102 if (j >= 10000) 1103 NFSCL_DEBUG(1, "n2op=%d n2st=%d\n", i, 1104 j); 1105 /* 1106 * All Compounds that do an Op that must 1107 * be in sequence consist of NFSV4OP_PUTFH 1108 * followed by one of these. As such, we 1109 * can determine if the seqid# should be 1110 * incremented, here. 1111 */ 1112 if ((i == NFSV4OP_OPEN || 1113 i == NFSV4OP_OPENCONFIRM || 1114 i == NFSV4OP_OPENDOWNGRADE || 1115 i == NFSV4OP_CLOSE || 1116 i == NFSV4OP_LOCK || 1117 i == NFSV4OP_LOCKU) && 1118 (j == 0 || 1119 (j != NFSERR_STALECLIENTID && 1120 j != NFSERR_STALESTATEID && 1121 j != NFSERR_BADSTATEID && 1122 j != NFSERR_BADSEQID && 1123 j != NFSERR_BADXDR && 1124 j != NFSERR_RESOURCE && 1125 j != NFSERR_NOFILEHANDLE))) 1126 nd->nd_flag |= ND_INCRSEQID; 1127 } 1128 /* 1129 * If this op's status is non-zero, mark 1130 * that there is no more data to process. 1131 * The exception is Setattr, which always has xdr 1132 * when it has failed. 1133 */ 1134 if (j != 0 && i != NFSV4OP_SETATTR) 1135 nd->nd_flag |= ND_NOMOREDATA; 1136 1137 /* 1138 * If R_DONTRECOVER is set, replace the stale error 1139 * reply, so that recovery isn't initiated. 1140 */ 1141 if ((nd->nd_repstat == NFSERR_STALECLIENTID || 1142 nd->nd_repstat == NFSERR_BADSESSION || 1143 nd->nd_repstat == NFSERR_STALESTATEID) && 1144 rep != NULL && (rep->r_flags & R_DONTRECOVER)) 1145 nd->nd_repstat = NFSERR_STALEDONTRECOVER; 1146 } 1147 } 1148 1149 #ifdef KDTRACE_HOOKS 1150 if (nmp != NULL && dtrace_nfscl_nfs234_done_probe != NULL) { 1151 uint32_t probe_id; 1152 int probe_procnum; 1153 1154 if (nd->nd_flag & ND_NFSV4) { 1155 probe_id = nfscl_nfs4_done_probes[nd->nd_procnum]; 1156 probe_procnum = nd->nd_procnum; 1157 } else if (nd->nd_flag & ND_NFSV3) { 1158 probe_id = nfscl_nfs3_done_probes[procnum]; 1159 probe_procnum = procnum; 1160 } else { 1161 probe_id = nfscl_nfs2_done_probes[nd->nd_procnum]; 1162 probe_procnum = procnum; 1163 } 1164 if (probe_id != 0) 1165 (dtrace_nfscl_nfs234_done_probe)(probe_id, vp, 1166 nd->nd_mreq, cred, probe_procnum, 0); 1167 } 1168 #endif 1169 1170 m_freem(nd->nd_mreq); 1171 if (usegssname == 0) 1172 AUTH_DESTROY(auth); 1173 if (rep != NULL) 1174 free(rep, M_NFSDREQ); 1175 if (set_sigset) 1176 newnfs_restore_sigmask(td, &oldset); 1177 return (0); 1178 nfsmout: 1179 mbuf_freem(nd->nd_mrep); 1180 mbuf_freem(nd->nd_mreq); 1181 if (usegssname == 0) 1182 AUTH_DESTROY(auth); 1183 if (rep != NULL) 1184 free(rep, M_NFSDREQ); 1185 if (set_sigset) 1186 newnfs_restore_sigmask(td, &oldset); 1187 return (error); 1188 } 1189 1190 /* 1191 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and 1192 * wait for all requests to complete. This is used by forced unmounts 1193 * to terminate any outstanding RPCs. 1194 */ 1195 int 1196 newnfs_nmcancelreqs(struct nfsmount *nmp) 1197 { 1198 struct nfsclds *dsp; 1199 struct __rpc_client *cl; 1200 1201 if (nmp->nm_sockreq.nr_client != NULL) 1202 CLNT_CLOSE(nmp->nm_sockreq.nr_client); 1203 lookformore: 1204 NFSLOCKMNT(nmp); 1205 TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) { 1206 NFSLOCKDS(dsp); 1207 if (dsp != TAILQ_FIRST(&nmp->nm_sess) && 1208 (dsp->nfsclds_flags & NFSCLDS_CLOSED) == 0 && 1209 dsp->nfsclds_sockp != NULL && 1210 dsp->nfsclds_sockp->nr_client != NULL) { 1211 dsp->nfsclds_flags |= NFSCLDS_CLOSED; 1212 cl = dsp->nfsclds_sockp->nr_client; 1213 NFSUNLOCKDS(dsp); 1214 NFSUNLOCKMNT(nmp); 1215 CLNT_CLOSE(cl); 1216 goto lookformore; 1217 } 1218 NFSUNLOCKDS(dsp); 1219 } 1220 NFSUNLOCKMNT(nmp); 1221 return (0); 1222 } 1223 1224 /* 1225 * Any signal that can interrupt an NFS operation in an intr mount 1226 * should be added to this set. SIGSTOP and SIGKILL cannot be masked. 1227 */ 1228 int newnfs_sig_set[] = { 1229 SIGINT, 1230 SIGTERM, 1231 SIGHUP, 1232 SIGKILL, 1233 SIGQUIT 1234 }; 1235 1236 /* 1237 * Check to see if one of the signals in our subset is pending on 1238 * the process (in an intr mount). 1239 */ 1240 static int 1241 nfs_sig_pending(sigset_t set) 1242 { 1243 int i; 1244 1245 for (i = 0 ; i < nitems(newnfs_sig_set); i++) 1246 if (SIGISMEMBER(set, newnfs_sig_set[i])) 1247 return (1); 1248 return (0); 1249 } 1250 1251 /* 1252 * The set/restore sigmask functions are used to (temporarily) overwrite 1253 * the thread td_sigmask during an RPC call (for example). These are also 1254 * used in other places in the NFS client that might tsleep(). 1255 */ 1256 void 1257 newnfs_set_sigmask(struct thread *td, sigset_t *oldset) 1258 { 1259 sigset_t newset; 1260 int i; 1261 struct proc *p; 1262 1263 SIGFILLSET(newset); 1264 if (td == NULL) 1265 td = curthread; /* XXX */ 1266 p = td->td_proc; 1267 /* Remove the NFS set of signals from newset */ 1268 PROC_LOCK(p); 1269 mtx_lock(&p->p_sigacts->ps_mtx); 1270 for (i = 0 ; i < nitems(newnfs_sig_set); i++) { 1271 /* 1272 * But make sure we leave the ones already masked 1273 * by the process, ie. remove the signal from the 1274 * temporary signalmask only if it wasn't already 1275 * in p_sigmask. 1276 */ 1277 if (!SIGISMEMBER(td->td_sigmask, newnfs_sig_set[i]) && 1278 !SIGISMEMBER(p->p_sigacts->ps_sigignore, newnfs_sig_set[i])) 1279 SIGDELSET(newset, newnfs_sig_set[i]); 1280 } 1281 mtx_unlock(&p->p_sigacts->ps_mtx); 1282 kern_sigprocmask(td, SIG_SETMASK, &newset, oldset, 1283 SIGPROCMASK_PROC_LOCKED); 1284 PROC_UNLOCK(p); 1285 } 1286 1287 void 1288 newnfs_restore_sigmask(struct thread *td, sigset_t *set) 1289 { 1290 if (td == NULL) 1291 td = curthread; /* XXX */ 1292 kern_sigprocmask(td, SIG_SETMASK, set, NULL, 0); 1293 } 1294 1295 /* 1296 * NFS wrapper to msleep(), that shoves a new p_sigmask and restores the 1297 * old one after msleep() returns. 1298 */ 1299 int 1300 newnfs_msleep(struct thread *td, void *ident, struct mtx *mtx, int priority, char *wmesg, int timo) 1301 { 1302 sigset_t oldset; 1303 int error; 1304 1305 if ((priority & PCATCH) == 0) 1306 return msleep(ident, mtx, priority, wmesg, timo); 1307 if (td == NULL) 1308 td = curthread; /* XXX */ 1309 newnfs_set_sigmask(td, &oldset); 1310 error = msleep(ident, mtx, priority, wmesg, timo); 1311 newnfs_restore_sigmask(td, &oldset); 1312 return (error); 1313 } 1314 1315 /* 1316 * Test for a termination condition pending on the process. 1317 * This is used for NFSMNT_INT mounts. 1318 */ 1319 int 1320 newnfs_sigintr(struct nfsmount *nmp, struct thread *td) 1321 { 1322 struct proc *p; 1323 sigset_t tmpset; 1324 1325 /* Terminate all requests while attempting a forced unmount. */ 1326 if (NFSCL_FORCEDISM(nmp->nm_mountp)) 1327 return (EIO); 1328 if (!(nmp->nm_flag & NFSMNT_INT)) 1329 return (0); 1330 if (td == NULL) 1331 return (0); 1332 p = td->td_proc; 1333 PROC_LOCK(p); 1334 tmpset = p->p_siglist; 1335 SIGSETOR(tmpset, td->td_siglist); 1336 SIGSETNAND(tmpset, td->td_sigmask); 1337 mtx_lock(&p->p_sigacts->ps_mtx); 1338 SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore); 1339 mtx_unlock(&p->p_sigacts->ps_mtx); 1340 if ((SIGNOTEMPTY(p->p_siglist) || SIGNOTEMPTY(td->td_siglist)) 1341 && nfs_sig_pending(tmpset)) { 1342 PROC_UNLOCK(p); 1343 return (EINTR); 1344 } 1345 PROC_UNLOCK(p); 1346 return (0); 1347 } 1348 1349 static int 1350 nfs_msg(struct thread *td, const char *server, const char *msg, int error) 1351 { 1352 struct proc *p; 1353 1354 p = td ? td->td_proc : NULL; 1355 if (error) { 1356 tprintf(p, LOG_INFO, "nfs server %s: %s, error %d\n", 1357 server, msg, error); 1358 } else { 1359 tprintf(p, LOG_INFO, "nfs server %s: %s\n", server, msg); 1360 } 1361 return (0); 1362 } 1363 1364 static void 1365 nfs_down(struct nfsmount *nmp, struct thread *td, const char *msg, 1366 int error, int flags) 1367 { 1368 if (nmp == NULL) 1369 return; 1370 mtx_lock(&nmp->nm_mtx); 1371 if ((flags & NFSSTA_TIMEO) && !(nmp->nm_state & NFSSTA_TIMEO)) { 1372 nmp->nm_state |= NFSSTA_TIMEO; 1373 mtx_unlock(&nmp->nm_mtx); 1374 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid, 1375 VQ_NOTRESP, 0); 1376 } else 1377 mtx_unlock(&nmp->nm_mtx); 1378 mtx_lock(&nmp->nm_mtx); 1379 if ((flags & NFSSTA_LOCKTIMEO) && !(nmp->nm_state & NFSSTA_LOCKTIMEO)) { 1380 nmp->nm_state |= NFSSTA_LOCKTIMEO; 1381 mtx_unlock(&nmp->nm_mtx); 1382 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid, 1383 VQ_NOTRESPLOCK, 0); 1384 } else 1385 mtx_unlock(&nmp->nm_mtx); 1386 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error); 1387 } 1388 1389 static void 1390 nfs_up(struct nfsmount *nmp, struct thread *td, const char *msg, 1391 int flags, int tprintfmsg) 1392 { 1393 if (nmp == NULL) 1394 return; 1395 if (tprintfmsg) { 1396 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0); 1397 } 1398 1399 mtx_lock(&nmp->nm_mtx); 1400 if ((flags & NFSSTA_TIMEO) && (nmp->nm_state & NFSSTA_TIMEO)) { 1401 nmp->nm_state &= ~NFSSTA_TIMEO; 1402 mtx_unlock(&nmp->nm_mtx); 1403 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid, 1404 VQ_NOTRESP, 1); 1405 } else 1406 mtx_unlock(&nmp->nm_mtx); 1407 1408 mtx_lock(&nmp->nm_mtx); 1409 if ((flags & NFSSTA_LOCKTIMEO) && (nmp->nm_state & NFSSTA_LOCKTIMEO)) { 1410 nmp->nm_state &= ~NFSSTA_LOCKTIMEO; 1411 mtx_unlock(&nmp->nm_mtx); 1412 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid, 1413 VQ_NOTRESPLOCK, 1); 1414 } else 1415 mtx_unlock(&nmp->nm_mtx); 1416 } 1417 1418