1 /* 2 * Copyright (c) 1989, 1991, 1993, 1995 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Rick Macklem at The University of Guelph. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)nfs_socket.c 8.5 (Berkeley) 3/30/95 37 * $FreeBSD: src/sys/nfs/nfs_socket.c,v 1.60.2.6 2003/03/26 01:44:46 alfred Exp $ 38 */ 39 40 /* 41 * Socket operations for use by nfs 42 */ 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/proc.h> 47 #include <sys/malloc.h> 48 #include <sys/mount.h> 49 #include <sys/kernel.h> 50 #include <sys/mbuf.h> 51 #include <sys/vnode.h> 52 #include <sys/fcntl.h> 53 #include <sys/protosw.h> 54 #include <sys/resourcevar.h> 55 #include <sys/socket.h> 56 #include <sys/socketvar.h> 57 #include <sys/socketops.h> 58 #include <sys/syslog.h> 59 #include <sys/thread.h> 60 #include <sys/tprintf.h> 61 #include <sys/sysctl.h> 62 #include <sys/signalvar.h> 63 64 #include <sys/signal2.h> 65 #include <sys/mutex2.h> 66 #include <sys/socketvar2.h> 67 68 #include <netinet/in.h> 69 #include <netinet/tcp.h> 70 #include <sys/thread2.h> 71 72 #include "rpcv2.h" 73 #include "nfsproto.h" 74 #include "nfs.h" 75 #include "xdr_subs.h" 76 #include "nfsm_subs.h" 77 #include "nfsmount.h" 78 #include "nfsnode.h" 79 #include "nfsrtt.h" 80 81 #define TRUE 1 82 #define FALSE 0 83 84 /* 85 * RTT calculations are scaled by 256 (8 bits). A proper fractional 86 * RTT will still be calculated even with a slow NFS timer. 87 */ 88 #define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum]] 89 #define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum]] 90 #define NFS_RTT_SCALE_BITS 8 /* bits */ 91 #define NFS_RTT_SCALE 256 /* value */ 92 93 /* 94 * Defines which timer to use for the procnum. 95 * 0 - default 96 * 1 - getattr 97 * 2 - lookup 98 * 3 - read 99 * 4 - write 100 */ 101 static int proct[NFS_NPROCS] = { 102 0, 1, 0, 2, 1, 3, 3, 4, 0, 0, /* 00-09 */ 103 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, /* 10-19 */ 104 0, 5, 0, 0, 0, 0, /* 20-29 */ 105 }; 106 107 static int multt[NFS_NPROCS] = { 108 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-09 */ 109 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10-19 */ 110 1, 2, 1, 1, 1, 1, /* 20-29 */ 111 }; 112 113 static int nfs_backoff[8] = { 2, 3, 5, 8, 13, 21, 34, 55 }; 114 static int nfs_realign_test; 115 static int nfs_realign_count; 116 static int nfs_showrtt; 117 static int nfs_showrexmit; 118 int nfs_maxasyncbio = NFS_MAXASYNCBIO; 119 120 SYSCTL_DECL(_vfs_nfs); 121 122 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_test, CTLFLAG_RW, &nfs_realign_test, 0, 123 "Number of times mbufs have been tested for bad alignment"); 124 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_count, CTLFLAG_RW, &nfs_realign_count, 0, 125 "Number of realignments for badly aligned mbuf data"); 126 SYSCTL_INT(_vfs_nfs, OID_AUTO, showrtt, CTLFLAG_RW, &nfs_showrtt, 0, 127 "Show round trip time output"); 128 SYSCTL_INT(_vfs_nfs, OID_AUTO, showrexmit, CTLFLAG_RW, &nfs_showrexmit, 0, 129 "Show retransmits info"); 130 SYSCTL_INT(_vfs_nfs, OID_AUTO, maxasyncbio, CTLFLAG_RW, &nfs_maxasyncbio, 0, 131 "Max number of asynchronous bio's"); 132 133 static int nfs_request_setup(nfsm_info_t info); 134 static int nfs_request_auth(struct nfsreq *rep); 135 static int nfs_request_try(struct nfsreq *rep); 136 static int nfs_request_waitreply(struct nfsreq *rep); 137 static int nfs_request_processreply(nfsm_info_t info, int); 138 139 int nfsrtton = 0; 140 struct nfsrtt nfsrtt; 141 struct callout nfs_timer_handle; 142 143 static int nfs_msg (struct thread *,char *,char *); 144 static int nfs_rcvlock (struct nfsmount *nmp, struct nfsreq *myreq); 145 static void nfs_rcvunlock (struct nfsmount *nmp); 146 static void nfs_realign (struct mbuf **pm, int hsiz); 147 static int nfs_receive (struct nfsmount *nmp, struct nfsreq *rep, 148 struct sockaddr **aname, struct mbuf **mp); 149 static void nfs_softterm (struct nfsreq *rep, int islocked); 150 static void nfs_hardterm (struct nfsreq *rep, int islocked); 151 static int nfs_reconnect (struct nfsmount *nmp, struct nfsreq *rep); 152 #ifndef NFS_NOSERVER 153 static int nfsrv_getstream (struct nfssvc_sock *, int, int *); 154 static void nfs_timer_req(struct nfsreq *req); 155 static void nfs_checkpkt(struct mbuf *m, int len); 156 157 int (*nfsrv3_procs[NFS_NPROCS]) (struct nfsrv_descript *nd, 158 struct nfssvc_sock *slp, 159 struct thread *td, 160 struct mbuf **mreqp) = { 161 nfsrv_null, 162 nfsrv_getattr, 163 nfsrv_setattr, 164 nfsrv_lookup, 165 nfsrv3_access, 166 nfsrv_readlink, 167 nfsrv_read, 168 nfsrv_write, 169 nfsrv_create, 170 nfsrv_mkdir, 171 nfsrv_symlink, 172 nfsrv_mknod, 173 nfsrv_remove, 174 nfsrv_rmdir, 175 nfsrv_rename, 176 nfsrv_link, 177 nfsrv_readdir, 178 nfsrv_readdirplus, 179 nfsrv_statfs, 180 nfsrv_fsinfo, 181 nfsrv_pathconf, 182 nfsrv_commit, 183 nfsrv_noop, 184 nfsrv_noop, 185 nfsrv_noop, 186 nfsrv_noop 187 }; 188 #endif /* NFS_NOSERVER */ 189 190 /* 191 * Initialize sockets and congestion for a new NFS connection. 192 * We do not free the sockaddr if error. 193 */ 194 int 195 nfs_connect(struct nfsmount *nmp, struct nfsreq *rep) 196 { 197 struct socket *so; 198 int error; 199 struct sockaddr *saddr; 200 struct sockaddr_in *sin; 201 struct thread *td = &thread0; /* only used for socreate and sobind */ 202 203 nmp->nm_so = so = NULL; 204 if (nmp->nm_flag & NFSMNT_FORCE) 205 return (EINVAL); 206 saddr = nmp->nm_nam; 207 error = socreate(saddr->sa_family, &so, nmp->nm_sotype, 208 nmp->nm_soproto, td); 209 if (error) 210 goto bad; 211 nmp->nm_soflags = so->so_proto->pr_flags; 212 213 /* 214 * Some servers require that the client port be a reserved port number. 215 */ 216 if (saddr->sa_family == AF_INET && (nmp->nm_flag & NFSMNT_RESVPORT)) { 217 struct sockopt sopt; 218 int ip; 219 struct sockaddr_in ssin; 220 221 bzero(&sopt, sizeof sopt); 222 ip = IP_PORTRANGE_LOW; 223 sopt.sopt_level = IPPROTO_IP; 224 sopt.sopt_name = IP_PORTRANGE; 225 sopt.sopt_val = (void *)&ip; 226 sopt.sopt_valsize = sizeof(ip); 227 sopt.sopt_td = NULL; 228 error = sosetopt(so, &sopt); 229 if (error) 230 goto bad; 231 bzero(&ssin, sizeof ssin); 232 sin = &ssin; 233 sin->sin_len = sizeof (struct sockaddr_in); 234 sin->sin_family = AF_INET; 235 sin->sin_addr.s_addr = INADDR_ANY; 236 sin->sin_port = htons(0); 237 error = sobind(so, (struct sockaddr *)sin, td); 238 if (error) 239 goto bad; 240 bzero(&sopt, sizeof sopt); 241 ip = IP_PORTRANGE_DEFAULT; 242 sopt.sopt_level = IPPROTO_IP; 243 sopt.sopt_name = IP_PORTRANGE; 244 sopt.sopt_val = (void *)&ip; 245 sopt.sopt_valsize = sizeof(ip); 246 sopt.sopt_td = NULL; 247 error = sosetopt(so, &sopt); 248 if (error) 249 goto bad; 250 } 251 252 /* 253 * Protocols that do not require connections may be optionally left 254 * unconnected for servers that reply from a port other than NFS_PORT. 255 */ 256 if (nmp->nm_flag & NFSMNT_NOCONN) { 257 if (nmp->nm_soflags & PR_CONNREQUIRED) { 258 error = ENOTCONN; 259 goto bad; 260 } 261 } else { 262 error = soconnect(so, nmp->nm_nam, td); 263 if (error) 264 goto bad; 265 266 /* 267 * Wait for the connection to complete. Cribbed from the 268 * connect system call but with the wait timing out so 269 * that interruptible mounts don't hang here for a long time. 270 */ 271 crit_enter(); 272 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) { 273 (void) tsleep((caddr_t)&so->so_timeo, 0, 274 "nfscon", 2 * hz); 275 if ((so->so_state & SS_ISCONNECTING) && 276 so->so_error == 0 && rep && 277 (error = nfs_sigintr(nmp, rep, rep->r_td)) != 0){ 278 soclrstate(so, SS_ISCONNECTING); 279 crit_exit(); 280 goto bad; 281 } 282 } 283 if (so->so_error) { 284 error = so->so_error; 285 so->so_error = 0; 286 crit_exit(); 287 goto bad; 288 } 289 crit_exit(); 290 } 291 so->so_rcv.ssb_timeo = (5 * hz); 292 so->so_snd.ssb_timeo = (5 * hz); 293 294 /* 295 * Get buffer reservation size from sysctl, but impose reasonable 296 * limits. 297 */ 298 if (nmp->nm_sotype == SOCK_STREAM) { 299 if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 300 struct sockopt sopt; 301 int val; 302 303 bzero(&sopt, sizeof sopt); 304 sopt.sopt_level = SOL_SOCKET; 305 sopt.sopt_name = SO_KEEPALIVE; 306 sopt.sopt_val = &val; 307 sopt.sopt_valsize = sizeof val; 308 val = 1; 309 sosetopt(so, &sopt); 310 } 311 if (so->so_proto->pr_protocol == IPPROTO_TCP) { 312 struct sockopt sopt; 313 int val; 314 315 bzero(&sopt, sizeof sopt); 316 sopt.sopt_level = IPPROTO_TCP; 317 sopt.sopt_name = TCP_NODELAY; 318 sopt.sopt_val = &val; 319 sopt.sopt_valsize = sizeof val; 320 val = 1; 321 sosetopt(so, &sopt); 322 323 bzero(&sopt, sizeof sopt); 324 sopt.sopt_level = IPPROTO_TCP; 325 sopt.sopt_name = TCP_FASTKEEP; 326 sopt.sopt_val = &val; 327 sopt.sopt_valsize = sizeof val; 328 val = 1; 329 sosetopt(so, &sopt); 330 } 331 } 332 error = soreserve(so, nfs_soreserve, nfs_soreserve, NULL); 333 if (error) 334 goto bad; 335 atomic_set_int(&so->so_rcv.ssb_flags, SSB_NOINTR); 336 atomic_set_int(&so->so_snd.ssb_flags, SSB_NOINTR); 337 338 /* Initialize other non-zero congestion variables */ 339 nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] = 340 nmp->nm_srtt[3] = (NFS_TIMEO << NFS_RTT_SCALE_BITS); 341 nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] = 342 nmp->nm_sdrtt[3] = 0; 343 nmp->nm_maxasync_scaled = NFS_MINASYNC_SCALED; 344 nmp->nm_timeouts = 0; 345 346 /* 347 * Assign nm_so last. The moment nm_so is assigned the nfs_timer() 348 * can mess with the socket. 349 */ 350 nmp->nm_so = so; 351 return (0); 352 353 bad: 354 if (so) { 355 soshutdown(so, SHUT_RDWR); 356 soclose(so, FNONBLOCK); 357 } 358 return (error); 359 } 360 361 /* 362 * Reconnect routine: 363 * Called when a connection is broken on a reliable protocol. 364 * - clean up the old socket 365 * - nfs_connect() again 366 * - set R_NEEDSXMIT for all outstanding requests on mount point 367 * If this fails the mount point is DEAD! 368 * nb: Must be called with the nfs_sndlock() set on the mount point. 369 */ 370 static int 371 nfs_reconnect(struct nfsmount *nmp, struct nfsreq *rep) 372 { 373 struct nfsreq *req; 374 int error; 375 376 nfs_disconnect(nmp); 377 if (nmp->nm_rxstate >= NFSSVC_STOPPING) 378 return (EINTR); 379 while ((error = nfs_connect(nmp, rep)) != 0) { 380 if (error == EINTR || error == ERESTART) 381 return (EINTR); 382 if (error == EINVAL) 383 return (error); 384 if (nmp->nm_rxstate >= NFSSVC_STOPPING) 385 return (EINTR); 386 (void) tsleep((caddr_t)&lbolt, 0, "nfscon", 0); 387 } 388 389 /* 390 * Loop through outstanding request list and fix up all requests 391 * on old socket. 392 */ 393 crit_enter(); 394 TAILQ_FOREACH(req, &nmp->nm_reqq, r_chain) { 395 KKASSERT(req->r_nmp == nmp); 396 req->r_flags |= R_NEEDSXMIT; 397 } 398 crit_exit(); 399 return (0); 400 } 401 402 /* 403 * NFS disconnect. Clean up and unlink. 404 */ 405 void 406 nfs_disconnect(struct nfsmount *nmp) 407 { 408 struct socket *so; 409 410 if (nmp->nm_so) { 411 so = nmp->nm_so; 412 nmp->nm_so = NULL; 413 soshutdown(so, SHUT_RDWR); 414 soclose(so, FNONBLOCK); 415 } 416 } 417 418 void 419 nfs_safedisconnect(struct nfsmount *nmp) 420 { 421 nfs_rcvlock(nmp, NULL); 422 nfs_disconnect(nmp); 423 nfs_rcvunlock(nmp); 424 } 425 426 /* 427 * This is the nfs send routine. For connection based socket types, it 428 * must be called with an nfs_sndlock() on the socket. 429 * "rep == NULL" indicates that it has been called from a server. 430 * For the client side: 431 * - return EINTR if the RPC is terminated, 0 otherwise 432 * - set R_NEEDSXMIT if the send fails for any reason 433 * - do any cleanup required by recoverable socket errors (?) 434 * For the server side: 435 * - return EINTR or ERESTART if interrupted by a signal 436 * - return EPIPE if a connection is lost for connection based sockets (TCP...) 437 * - do any cleanup required by recoverable socket errors (?) 438 */ 439 int 440 nfs_send(struct socket *so, struct sockaddr *nam, struct mbuf *top, 441 struct nfsreq *rep) 442 { 443 struct sockaddr *sendnam; 444 int error, soflags, flags; 445 446 if (rep) { 447 if (rep->r_flags & R_SOFTTERM) { 448 m_freem(top); 449 return (EINTR); 450 } 451 if ((so = rep->r_nmp->nm_so) == NULL) { 452 rep->r_flags |= R_NEEDSXMIT; 453 m_freem(top); 454 return (0); 455 } 456 rep->r_flags &= ~R_NEEDSXMIT; 457 soflags = rep->r_nmp->nm_soflags; 458 } else { 459 soflags = so->so_proto->pr_flags; 460 } 461 if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED)) 462 sendnam = NULL; 463 else 464 sendnam = nam; 465 if (so->so_type == SOCK_SEQPACKET) 466 flags = MSG_EOR; 467 else 468 flags = 0; 469 470 /* 471 * calls pru_sosend -> sosend -> so_pru_send -> netrpc 472 */ 473 error = so_pru_sosend(so, sendnam, NULL, top, NULL, flags, 474 curthread /*XXX*/); 475 476 /* 477 * ENOBUFS for dgram sockets is transient and non fatal. 478 * No need to log, and no need to break a soft mount. 479 */ 480 if (error == ENOBUFS && so->so_type == SOCK_DGRAM) { 481 error = 0; 482 /* 483 * do backoff retransmit on client 484 */ 485 if (rep) { 486 if ((rep->r_nmp->nm_state & NFSSTA_SENDSPACE) == 0) { 487 rep->r_nmp->nm_state |= NFSSTA_SENDSPACE; 488 kprintf("Warning: NFS: Insufficient sendspace " 489 "(%lu),\n" 490 "\t You must increase vfs.nfs.soreserve" 491 "or decrease vfs.nfs.maxasyncbio\n", 492 so->so_snd.ssb_hiwat); 493 } 494 rep->r_flags |= R_NEEDSXMIT; 495 } 496 } 497 498 if (error) { 499 if (rep) { 500 log(LOG_INFO, "nfs send error %d for server %s\n",error, 501 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname); 502 /* 503 * Deal with errors for the client side. 504 */ 505 if (rep->r_flags & R_SOFTTERM) 506 error = EINTR; 507 else 508 rep->r_flags |= R_NEEDSXMIT; 509 } else { 510 log(LOG_INFO, "nfsd send error %d\n", error); 511 } 512 513 /* 514 * Handle any recoverable (soft) socket errors here. (?) 515 */ 516 if (error != EINTR && error != ERESTART && 517 error != EWOULDBLOCK && error != EPIPE) 518 error = 0; 519 } 520 return (error); 521 } 522 523 /* 524 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all 525 * done by soreceive(), but for SOCK_STREAM we must deal with the Record 526 * Mark and consolidate the data into a new mbuf list. 527 * nb: Sometimes TCP passes the data up to soreceive() in long lists of 528 * small mbufs. 529 * For SOCK_STREAM we must be very careful to read an entire record once 530 * we have read any of it, even if the system call has been interrupted. 531 */ 532 static int 533 nfs_receive(struct nfsmount *nmp, struct nfsreq *rep, 534 struct sockaddr **aname, struct mbuf **mp) 535 { 536 struct socket *so; 537 struct sockbuf sio; 538 struct uio auio; 539 struct iovec aio; 540 struct mbuf *m; 541 struct mbuf *control; 542 u_int32_t len; 543 struct sockaddr **getnam; 544 int error, sotype, rcvflg; 545 struct thread *td = curthread; /* XXX */ 546 547 /* 548 * Set up arguments for soreceive() 549 */ 550 *mp = NULL; 551 *aname = NULL; 552 sotype = nmp->nm_sotype; 553 554 /* 555 * For reliable protocols, lock against other senders/receivers 556 * in case a reconnect is necessary. 557 * For SOCK_STREAM, first get the Record Mark to find out how much 558 * more there is to get. 559 * We must lock the socket against other receivers 560 * until we have an entire rpc request/reply. 561 */ 562 if (sotype != SOCK_DGRAM) { 563 error = nfs_sndlock(nmp, rep); 564 if (error) 565 return (error); 566 tryagain: 567 /* 568 * Check for fatal errors and resending request. 569 */ 570 /* 571 * Ugh: If a reconnect attempt just happened, nm_so 572 * would have changed. NULL indicates a failed 573 * attempt that has essentially shut down this 574 * mount point. 575 */ 576 if (rep && (rep->r_mrep || (rep->r_flags & R_SOFTTERM))) { 577 nfs_sndunlock(nmp); 578 return (EINTR); 579 } 580 so = nmp->nm_so; 581 if (so == NULL) { 582 error = nfs_reconnect(nmp, rep); 583 if (error) { 584 nfs_sndunlock(nmp); 585 return (error); 586 } 587 goto tryagain; 588 } 589 while (rep && (rep->r_flags & R_NEEDSXMIT)) { 590 m = m_copym(rep->r_mreq, 0, M_COPYALL, MB_WAIT); 591 nfsstats.rpcretries++; 592 error = nfs_send(so, rep->r_nmp->nm_nam, m, rep); 593 if (error) { 594 if (error == EINTR || error == ERESTART || 595 (error = nfs_reconnect(nmp, rep)) != 0) { 596 nfs_sndunlock(nmp); 597 return (error); 598 } 599 goto tryagain; 600 } 601 } 602 nfs_sndunlock(nmp); 603 if (sotype == SOCK_STREAM) { 604 /* 605 * Get the length marker from the stream 606 */ 607 aio.iov_base = (caddr_t)&len; 608 aio.iov_len = sizeof(u_int32_t); 609 auio.uio_iov = &aio; 610 auio.uio_iovcnt = 1; 611 auio.uio_segflg = UIO_SYSSPACE; 612 auio.uio_rw = UIO_READ; 613 auio.uio_offset = 0; 614 auio.uio_resid = sizeof(u_int32_t); 615 auio.uio_td = td; 616 do { 617 rcvflg = MSG_WAITALL; 618 error = so_pru_soreceive(so, NULL, &auio, NULL, 619 NULL, &rcvflg); 620 if (error == EWOULDBLOCK && rep) { 621 if (rep->r_flags & R_SOFTTERM) 622 return (EINTR); 623 } 624 } while (error == EWOULDBLOCK); 625 626 if (error == 0 && auio.uio_resid > 0) { 627 /* 628 * Only log short packets if not EOF 629 */ 630 if (auio.uio_resid != sizeof(u_int32_t)) { 631 log(LOG_INFO, 632 "short receive (%d/%d) from nfs server %s\n", 633 (int)(sizeof(u_int32_t) - auio.uio_resid), 634 (int)sizeof(u_int32_t), 635 nmp->nm_mountp->mnt_stat.f_mntfromname); 636 } 637 error = EPIPE; 638 } 639 if (error) 640 goto errout; 641 len = ntohl(len) & ~0x80000000; 642 /* 643 * This is SERIOUS! We are out of sync with the sender 644 * and forcing a disconnect/reconnect is all I can do. 645 */ 646 if (len > NFS_MAXPACKET) { 647 log(LOG_ERR, "%s (%d) from nfs server %s\n", 648 "impossible packet length", 649 len, 650 nmp->nm_mountp->mnt_stat.f_mntfromname); 651 error = EFBIG; 652 goto errout; 653 } 654 655 /* 656 * Get the rest of the packet as an mbuf chain 657 */ 658 sbinit(&sio, len); 659 do { 660 rcvflg = MSG_WAITALL; 661 error = so_pru_soreceive(so, NULL, NULL, &sio, 662 NULL, &rcvflg); 663 } while (error == EWOULDBLOCK || error == EINTR || 664 error == ERESTART); 665 if (error == 0 && sio.sb_cc != len) { 666 if (sio.sb_cc != 0) { 667 log(LOG_INFO, 668 "short receive (%zu/%d) from nfs server %s\n", 669 (size_t)len - auio.uio_resid, len, 670 nmp->nm_mountp->mnt_stat.f_mntfromname); 671 } 672 error = EPIPE; 673 } 674 *mp = sio.sb_mb; 675 } else { 676 /* 677 * Non-stream, so get the whole packet by not 678 * specifying MSG_WAITALL and by specifying a large 679 * length. 680 * 681 * We have no use for control msg., but must grab them 682 * and then throw them away so we know what is going 683 * on. 684 */ 685 sbinit(&sio, 100000000); 686 do { 687 rcvflg = 0; 688 error = so_pru_soreceive(so, NULL, NULL, &sio, 689 &control, &rcvflg); 690 if (control) 691 m_freem(control); 692 if (error == EWOULDBLOCK && rep) { 693 if (rep->r_flags & R_SOFTTERM) { 694 m_freem(sio.sb_mb); 695 return (EINTR); 696 } 697 } 698 } while (error == EWOULDBLOCK || 699 (error == 0 && sio.sb_mb == NULL && control)); 700 if ((rcvflg & MSG_EOR) == 0) 701 kprintf("Egad!!\n"); 702 if (error == 0 && sio.sb_mb == NULL) 703 error = EPIPE; 704 len = sio.sb_cc; 705 *mp = sio.sb_mb; 706 } 707 errout: 708 if (error && error != EINTR && error != ERESTART) { 709 m_freem(*mp); 710 *mp = NULL; 711 if (error != EPIPE) { 712 log(LOG_INFO, 713 "receive error %d from nfs server %s\n", 714 error, 715 nmp->nm_mountp->mnt_stat.f_mntfromname); 716 } 717 error = nfs_sndlock(nmp, rep); 718 if (!error) { 719 error = nfs_reconnect(nmp, rep); 720 if (!error) 721 goto tryagain; 722 else 723 nfs_sndunlock(nmp); 724 } 725 } 726 } else { 727 if ((so = nmp->nm_so) == NULL) 728 return (EACCES); 729 if (so->so_state & SS_ISCONNECTED) 730 getnam = NULL; 731 else 732 getnam = aname; 733 sbinit(&sio, 100000000); 734 do { 735 rcvflg = 0; 736 error = so_pru_soreceive(so, getnam, NULL, &sio, 737 NULL, &rcvflg); 738 if (error == EWOULDBLOCK && rep && 739 (rep->r_flags & R_SOFTTERM)) { 740 m_freem(sio.sb_mb); 741 return (EINTR); 742 } 743 } while (error == EWOULDBLOCK); 744 745 len = sio.sb_cc; 746 *mp = sio.sb_mb; 747 748 /* 749 * A shutdown may result in no error and no mbuf. 750 * Convert to EPIPE. 751 */ 752 if (*mp == NULL && error == 0) 753 error = EPIPE; 754 } 755 if (error) { 756 m_freem(*mp); 757 *mp = NULL; 758 } 759 760 /* 761 * Search for any mbufs that are not a multiple of 4 bytes long 762 * or with m_data not longword aligned. 763 * These could cause pointer alignment problems, so copy them to 764 * well aligned mbufs. 765 */ 766 nfs_realign(mp, 5 * NFSX_UNSIGNED); 767 return (error); 768 } 769 770 /* 771 * Implement receipt of reply on a socket. 772 * 773 * We must search through the list of received datagrams matching them 774 * with outstanding requests using the xid, until ours is found. 775 * 776 * If myrep is NULL we process packets on the socket until 777 * interrupted or until nm_reqrxq is non-empty. 778 */ 779 /* ARGSUSED */ 780 int 781 nfs_reply(struct nfsmount *nmp, struct nfsreq *myrep) 782 { 783 struct nfsreq *rep; 784 struct sockaddr *nam; 785 u_int32_t rxid; 786 u_int32_t *tl; 787 int error; 788 struct nfsm_info info; 789 790 /* 791 * Loop around until we get our own reply 792 */ 793 for (;;) { 794 /* 795 * Lock against other receivers so that I don't get stuck in 796 * sbwait() after someone else has received my reply for me. 797 * Also necessary for connection based protocols to avoid 798 * race conditions during a reconnect. 799 * 800 * If nfs_rcvlock() returns EALREADY, that means that 801 * the reply has already been recieved by another 802 * process and we can return immediately. In this 803 * case, the lock is not taken to avoid races with 804 * other processes. 805 */ 806 info.mrep = NULL; 807 808 error = nfs_rcvlock(nmp, myrep); 809 if (error == EALREADY) 810 return (0); 811 if (error) 812 return (error); 813 814 /* 815 * If myrep is NULL we are the receiver helper thread. 816 * Stop waiting for incoming replies if there are 817 * messages sitting on reqrxq that we need to process, 818 * or if a shutdown request is pending. 819 */ 820 if (myrep == NULL && (TAILQ_FIRST(&nmp->nm_reqrxq) || 821 nmp->nm_rxstate > NFSSVC_PENDING)) { 822 nfs_rcvunlock(nmp); 823 return(EWOULDBLOCK); 824 } 825 826 /* 827 * Get the next Rpc reply off the socket 828 * 829 * We cannot release the receive lock until we've 830 * filled in rep->r_mrep, otherwise a waiting 831 * thread may deadlock in soreceive with no incoming 832 * packets expected. 833 */ 834 error = nfs_receive(nmp, myrep, &nam, &info.mrep); 835 if (error) { 836 /* 837 * Ignore routing errors on connectionless protocols?? 838 */ 839 nfs_rcvunlock(nmp); 840 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) { 841 if (nmp->nm_so == NULL) 842 return (error); 843 nmp->nm_so->so_error = 0; 844 continue; 845 } 846 return (error); 847 } 848 if (nam) 849 kfree(nam, M_SONAME); 850 851 /* 852 * Get the xid and check that it is an rpc reply 853 */ 854 info.md = info.mrep; 855 info.dpos = mtod(info.md, caddr_t); 856 NULLOUT(tl = nfsm_dissect(&info, 2*NFSX_UNSIGNED)); 857 rxid = *tl++; 858 if (*tl != rpc_reply) { 859 nfsstats.rpcinvalid++; 860 m_freem(info.mrep); 861 info.mrep = NULL; 862 nfsmout: 863 nfs_rcvunlock(nmp); 864 continue; 865 } 866 867 /* 868 * Loop through the request list to match up the reply 869 * Iff no match, just drop the datagram. On match, set 870 * r_mrep atomically to prevent the timer from messing 871 * around with the request after we have exited the critical 872 * section. 873 */ 874 crit_enter(); 875 TAILQ_FOREACH(rep, &nmp->nm_reqq, r_chain) { 876 if (rep->r_mrep == NULL && rxid == rep->r_xid) 877 break; 878 } 879 880 /* 881 * Fill in the rest of the reply if we found a match. 882 * 883 * Deal with duplicate responses if there was no match. 884 */ 885 if (rep) { 886 rep->r_md = info.md; 887 rep->r_dpos = info.dpos; 888 if (nfsrtton) { 889 struct rttl *rt; 890 891 rt = &nfsrtt.rttl[nfsrtt.pos]; 892 rt->proc = rep->r_procnum; 893 rt->rto = 0; 894 rt->sent = 0; 895 rt->cwnd = nmp->nm_maxasync_scaled; 896 rt->srtt = nmp->nm_srtt[proct[rep->r_procnum] - 1]; 897 rt->sdrtt = nmp->nm_sdrtt[proct[rep->r_procnum] - 1]; 898 rt->fsid = nmp->nm_mountp->mnt_stat.f_fsid; 899 getmicrotime(&rt->tstamp); 900 if (rep->r_flags & R_TIMING) 901 rt->rtt = rep->r_rtt; 902 else 903 rt->rtt = 1000000; 904 nfsrtt.pos = (nfsrtt.pos + 1) % NFSRTTLOGSIZ; 905 } 906 907 /* 908 * New congestion control is based only on async 909 * requests. 910 */ 911 if (nmp->nm_maxasync_scaled < NFS_MAXASYNC_SCALED) 912 ++nmp->nm_maxasync_scaled; 913 if (rep->r_flags & R_SENT) { 914 rep->r_flags &= ~R_SENT; 915 } 916 /* 917 * Update rtt using a gain of 0.125 on the mean 918 * and a gain of 0.25 on the deviation. 919 * 920 * NOTE SRTT/SDRTT are only good if R_TIMING is set. 921 */ 922 if ((rep->r_flags & R_TIMING) && rep->r_rexmit == 0) { 923 /* 924 * Since the timer resolution of 925 * NFS_HZ is so course, it can often 926 * result in r_rtt == 0. Since 927 * r_rtt == N means that the actual 928 * rtt is between N+dt and N+2-dt ticks, 929 * add 1. 930 */ 931 int n; 932 int d; 933 934 #define NFSRSB NFS_RTT_SCALE_BITS 935 n = ((NFS_SRTT(rep) * 7) + 936 (rep->r_rtt << NFSRSB)) >> 3; 937 d = n - NFS_SRTT(rep); 938 NFS_SRTT(rep) = n; 939 940 /* 941 * Don't let the jitter calculation decay 942 * too quickly, but we want a fast rampup. 943 */ 944 if (d < 0) 945 d = -d; 946 d <<= NFSRSB; 947 if (d < NFS_SDRTT(rep)) 948 n = ((NFS_SDRTT(rep) * 15) + d) >> 4; 949 else 950 n = ((NFS_SDRTT(rep) * 3) + d) >> 2; 951 NFS_SDRTT(rep) = n; 952 #undef NFSRSB 953 } 954 nmp->nm_timeouts = 0; 955 rep->r_mrep = info.mrep; 956 nfs_hardterm(rep, 0); 957 } else { 958 /* 959 * Extract vers, prog, nfsver, procnum. A duplicate 960 * response means we didn't wait long enough so 961 * we increase the SRTT to avoid future spurious 962 * timeouts. 963 */ 964 u_int procnum = nmp->nm_lastreprocnum; 965 int n; 966 967 if (procnum < NFS_NPROCS && proct[procnum]) { 968 if (nfs_showrexmit) 969 kprintf("D"); 970 n = nmp->nm_srtt[proct[procnum]]; 971 n += NFS_ASYSCALE * NFS_HZ; 972 if (n < NFS_ASYSCALE * NFS_HZ * 10) 973 n = NFS_ASYSCALE * NFS_HZ * 10; 974 nmp->nm_srtt[proct[procnum]] = n; 975 } 976 } 977 nfs_rcvunlock(nmp); 978 crit_exit(); 979 980 /* 981 * If not matched to a request, drop it. 982 * If it's mine, get out. 983 */ 984 if (rep == NULL) { 985 nfsstats.rpcunexpected++; 986 m_freem(info.mrep); 987 info.mrep = NULL; 988 } else if (rep == myrep) { 989 if (rep->r_mrep == NULL) 990 panic("nfsreply nil"); 991 return (0); 992 } 993 } 994 } 995 996 /* 997 * Run the request state machine until the target state is reached 998 * or a fatal error occurs. The target state is not run. Specifying 999 * a target of NFSM_STATE_DONE runs the state machine until the rpc 1000 * is complete. 1001 * 1002 * EINPROGRESS is returned for all states other then the DONE state, 1003 * indicating that the rpc is still in progress. 1004 */ 1005 int 1006 nfs_request(struct nfsm_info *info, nfsm_state_t bstate, nfsm_state_t estate) 1007 { 1008 struct nfsreq *req; 1009 1010 while (info->state >= bstate && info->state < estate) { 1011 switch(info->state) { 1012 case NFSM_STATE_SETUP: 1013 /* 1014 * Setup the nfsreq. Any error which occurs during 1015 * this state is fatal. 1016 */ 1017 info->error = nfs_request_setup(info); 1018 if (info->error) { 1019 info->state = NFSM_STATE_DONE; 1020 return (info->error); 1021 } else { 1022 req = info->req; 1023 req->r_mrp = &info->mrep; 1024 req->r_mdp = &info->md; 1025 req->r_dposp = &info->dpos; 1026 info->state = NFSM_STATE_AUTH; 1027 } 1028 break; 1029 case NFSM_STATE_AUTH: 1030 /* 1031 * Authenticate the nfsreq. Any error which occurs 1032 * during this state is fatal. 1033 */ 1034 info->error = nfs_request_auth(info->req); 1035 if (info->error) { 1036 info->state = NFSM_STATE_DONE; 1037 return (info->error); 1038 } else { 1039 info->state = NFSM_STATE_TRY; 1040 } 1041 break; 1042 case NFSM_STATE_TRY: 1043 /* 1044 * Transmit or retransmit attempt. An error in this 1045 * state is ignored and we always move on to the 1046 * next state. 1047 * 1048 * This can trivially race the receiver if the 1049 * request is asynchronous. nfs_request_try() 1050 * will thus set the state for us and we 1051 * must also return immediately if we are 1052 * running an async state machine, because 1053 * info can become invalid due to races after 1054 * try() returns. 1055 */ 1056 if (info->req->r_flags & R_ASYNC) { 1057 nfs_request_try(info->req); 1058 if (estate == NFSM_STATE_WAITREPLY) 1059 return (EINPROGRESS); 1060 } else { 1061 nfs_request_try(info->req); 1062 info->state = NFSM_STATE_WAITREPLY; 1063 } 1064 break; 1065 case NFSM_STATE_WAITREPLY: 1066 /* 1067 * Wait for a reply or timeout and move on to the 1068 * next state. The error returned by this state 1069 * is passed to the processing code in the next 1070 * state. 1071 */ 1072 info->error = nfs_request_waitreply(info->req); 1073 info->state = NFSM_STATE_PROCESSREPLY; 1074 break; 1075 case NFSM_STATE_PROCESSREPLY: 1076 /* 1077 * Process the reply or timeout. Errors which occur 1078 * in this state may cause the state machine to 1079 * go back to an earlier state, and are fatal 1080 * otherwise. 1081 */ 1082 info->error = nfs_request_processreply(info, 1083 info->error); 1084 switch(info->error) { 1085 case ENEEDAUTH: 1086 info->state = NFSM_STATE_AUTH; 1087 break; 1088 case EAGAIN: 1089 info->state = NFSM_STATE_TRY; 1090 break; 1091 default: 1092 /* 1093 * Operation complete, with or without an 1094 * error. We are done. 1095 */ 1096 info->req = NULL; 1097 info->state = NFSM_STATE_DONE; 1098 return (info->error); 1099 } 1100 break; 1101 case NFSM_STATE_DONE: 1102 /* 1103 * Shouldn't be reached 1104 */ 1105 return (info->error); 1106 /* NOT REACHED */ 1107 } 1108 } 1109 1110 /* 1111 * If we are done return the error code (if any). 1112 * Otherwise return EINPROGRESS. 1113 */ 1114 if (info->state == NFSM_STATE_DONE) 1115 return (info->error); 1116 return (EINPROGRESS); 1117 } 1118 1119 /* 1120 * nfs_request - goes something like this 1121 * - fill in request struct 1122 * - links it into list 1123 * - calls nfs_send() for first transmit 1124 * - calls nfs_receive() to get reply 1125 * - break down rpc header and return with nfs reply pointed to 1126 * by mrep or error 1127 * nb: always frees up mreq mbuf list 1128 */ 1129 static int 1130 nfs_request_setup(nfsm_info_t info) 1131 { 1132 struct nfsreq *req; 1133 struct nfsmount *nmp; 1134 struct mbuf *m; 1135 int i; 1136 1137 /* 1138 * Reject requests while attempting a forced unmount. 1139 */ 1140 if (info->vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) { 1141 m_freem(info->mreq); 1142 info->mreq = NULL; 1143 return (EIO); 1144 } 1145 nmp = VFSTONFS(info->vp->v_mount); 1146 req = kmalloc(sizeof(struct nfsreq), M_NFSREQ, M_WAITOK); 1147 req->r_nmp = nmp; 1148 req->r_vp = info->vp; 1149 req->r_td = info->td; 1150 req->r_procnum = info->procnum; 1151 req->r_mreq = NULL; 1152 req->r_cred = info->cred; 1153 1154 i = 0; 1155 m = info->mreq; 1156 while (m) { 1157 i += m->m_len; 1158 m = m->m_next; 1159 } 1160 req->r_mrest = info->mreq; 1161 req->r_mrest_len = i; 1162 1163 /* 1164 * The presence of a non-NULL r_info in req indicates 1165 * async completion via our helper threads. See the receiver 1166 * code. 1167 */ 1168 if (info->bio) { 1169 req->r_info = info; 1170 req->r_flags = R_ASYNC; 1171 } else { 1172 req->r_info = NULL; 1173 req->r_flags = 0; 1174 } 1175 info->req = req; 1176 return(0); 1177 } 1178 1179 static int 1180 nfs_request_auth(struct nfsreq *rep) 1181 { 1182 struct nfsmount *nmp = rep->r_nmp; 1183 struct mbuf *m; 1184 char nickv[RPCX_NICKVERF]; 1185 int error = 0, auth_len, auth_type; 1186 int verf_len; 1187 u_int32_t xid; 1188 char *auth_str, *verf_str; 1189 struct ucred *cred; 1190 1191 cred = rep->r_cred; 1192 rep->r_failed_auth = 0; 1193 1194 /* 1195 * Get the RPC header with authorization. 1196 */ 1197 verf_str = auth_str = NULL; 1198 if (nmp->nm_flag & NFSMNT_KERB) { 1199 verf_str = nickv; 1200 verf_len = sizeof (nickv); 1201 auth_type = RPCAUTH_KERB4; 1202 bzero((caddr_t)rep->r_key, sizeof(rep->r_key)); 1203 if (rep->r_failed_auth || 1204 nfs_getnickauth(nmp, cred, &auth_str, &auth_len, 1205 verf_str, verf_len)) { 1206 error = nfs_getauth(nmp, rep, cred, &auth_str, 1207 &auth_len, verf_str, &verf_len, rep->r_key); 1208 if (error) { 1209 m_freem(rep->r_mrest); 1210 rep->r_mrest = NULL; 1211 kfree((caddr_t)rep, M_NFSREQ); 1212 return (error); 1213 } 1214 } 1215 } else { 1216 auth_type = RPCAUTH_UNIX; 1217 if (cred->cr_ngroups < 1) 1218 panic("nfsreq nogrps"); 1219 auth_len = ((((cred->cr_ngroups - 1) > nmp->nm_numgrps) ? 1220 nmp->nm_numgrps : (cred->cr_ngroups - 1)) << 2) + 1221 5 * NFSX_UNSIGNED; 1222 } 1223 if (rep->r_mrest) 1224 nfs_checkpkt(rep->r_mrest, rep->r_mrest_len); 1225 m = nfsm_rpchead(cred, nmp->nm_flag, rep->r_procnum, auth_type, 1226 auth_len, auth_str, verf_len, verf_str, 1227 rep->r_mrest, rep->r_mrest_len, &rep->r_mheadend, &xid); 1228 rep->r_mrest = NULL; 1229 if (auth_str) 1230 kfree(auth_str, M_TEMP); 1231 1232 /* 1233 * For stream protocols, insert a Sun RPC Record Mark. 1234 */ 1235 if (nmp->nm_sotype == SOCK_STREAM) { 1236 M_PREPEND(m, NFSX_UNSIGNED, MB_WAIT); 1237 if (m == NULL) { 1238 kfree(rep, M_NFSREQ); 1239 return (ENOBUFS); 1240 } 1241 *mtod(m, u_int32_t *) = htonl(0x80000000 | 1242 (m->m_pkthdr.len - NFSX_UNSIGNED)); 1243 } 1244 1245 nfs_checkpkt(m, m->m_pkthdr.len); 1246 1247 rep->r_mreq = m; 1248 rep->r_xid = xid; 1249 return (0); 1250 } 1251 1252 static int 1253 nfs_request_try(struct nfsreq *rep) 1254 { 1255 struct nfsmount *nmp = rep->r_nmp; 1256 struct mbuf *m2; 1257 int error; 1258 1259 /* 1260 * Request is not on any queue, only the owner has access to it 1261 * so it should not be locked by anyone atm. 1262 * 1263 * Interlock to prevent races. While locked the only remote 1264 * action possible is for r_mrep to be set (once we enqueue it). 1265 */ 1266 if (rep->r_flags == 0xdeadc0de) { 1267 print_backtrace(-1); 1268 panic("flags nbad"); 1269 } 1270 KKASSERT((rep->r_flags & (R_LOCKED | R_ONREQQ)) == 0); 1271 if (nmp->nm_flag & NFSMNT_SOFT) 1272 rep->r_retry = nmp->nm_retry; 1273 else 1274 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */ 1275 rep->r_rtt = rep->r_rexmit = 0; 1276 if (proct[rep->r_procnum] > 0) 1277 rep->r_flags |= R_TIMING | R_LOCKED; 1278 else 1279 rep->r_flags |= R_LOCKED; 1280 rep->r_mrep = NULL; 1281 1282 nfsstats.rpcrequests++; 1283 1284 if (nmp->nm_flag & NFSMNT_FORCE) { 1285 rep->r_flags |= R_SOFTTERM; 1286 rep->r_flags &= ~R_LOCKED; 1287 return (0); 1288 } 1289 rep->r_flags |= R_NEEDSXMIT; /* in case send lock races us */ 1290 1291 /* 1292 * Do the client side RPC. 1293 * 1294 * Chain request into list of outstanding requests. Be sure 1295 * to put it LAST so timer finds oldest requests first. Note 1296 * that our control of R_LOCKED prevents the request from 1297 * getting ripped out from under us or transmitted by the 1298 * timer code. 1299 * 1300 * For requests with info structures we must atomically set the 1301 * info's state because the structure could become invalid upon 1302 * return due to races (i.e., if async) 1303 */ 1304 crit_enter(); 1305 mtx_link_init(&rep->r_link); 1306 KKASSERT((rep->r_flags & R_ONREQQ) == 0); 1307 TAILQ_INSERT_TAIL(&nmp->nm_reqq, rep, r_chain); 1308 rep->r_flags |= R_ONREQQ; 1309 ++nmp->nm_reqqlen; 1310 if (rep->r_flags & R_ASYNC) 1311 rep->r_info->state = NFSM_STATE_WAITREPLY; 1312 crit_exit(); 1313 1314 error = 0; 1315 1316 /* 1317 * Send if we can. Congestion control is not handled here any more 1318 * becausing trying to defer the initial send based on the nfs_timer 1319 * requires having a very fast nfs_timer, which is silly. 1320 */ 1321 if (nmp->nm_so) { 1322 if (nmp->nm_soflags & PR_CONNREQUIRED) 1323 error = nfs_sndlock(nmp, rep); 1324 if (error == 0 && (rep->r_flags & R_NEEDSXMIT)) { 1325 m2 = m_copym(rep->r_mreq, 0, M_COPYALL, MB_WAIT); 1326 error = nfs_send(nmp->nm_so, nmp->nm_nam, m2, rep); 1327 rep->r_flags &= ~R_NEEDSXMIT; 1328 if ((rep->r_flags & R_SENT) == 0) { 1329 rep->r_flags |= R_SENT; 1330 } 1331 if (nmp->nm_soflags & PR_CONNREQUIRED) 1332 nfs_sndunlock(nmp); 1333 } 1334 } else { 1335 rep->r_rtt = -1; 1336 } 1337 if (error == EPIPE) 1338 error = 0; 1339 1340 /* 1341 * Release the lock. The only remote action that may have occurred 1342 * would have been the setting of rep->r_mrep. If this occured 1343 * and the request was async we have to move it to the reader 1344 * thread's queue for action. 1345 * 1346 * For async requests also make sure the reader is woken up so 1347 * it gets on the socket to read responses. 1348 */ 1349 crit_enter(); 1350 if (rep->r_flags & R_ASYNC) { 1351 if (rep->r_mrep) 1352 nfs_hardterm(rep, 1); 1353 rep->r_flags &= ~R_LOCKED; 1354 nfssvc_iod_reader_wakeup(nmp); 1355 } else { 1356 rep->r_flags &= ~R_LOCKED; 1357 } 1358 if (rep->r_flags & R_WANTED) { 1359 rep->r_flags &= ~R_WANTED; 1360 wakeup(rep); 1361 } 1362 crit_exit(); 1363 return (error); 1364 } 1365 1366 /* 1367 * This code is only called for synchronous requests. Completed synchronous 1368 * requests are left on reqq and we remove them before moving on to the 1369 * processing state. 1370 */ 1371 static int 1372 nfs_request_waitreply(struct nfsreq *rep) 1373 { 1374 struct nfsmount *nmp = rep->r_nmp; 1375 int error; 1376 1377 KKASSERT((rep->r_flags & R_ASYNC) == 0); 1378 1379 /* 1380 * Wait until the request is finished. 1381 */ 1382 error = nfs_reply(nmp, rep); 1383 1384 /* 1385 * RPC done, unlink the request, but don't rip it out from under 1386 * the callout timer. 1387 * 1388 * Once unlinked no other receiver or the timer will have 1389 * visibility, so we do not have to set R_LOCKED. 1390 */ 1391 crit_enter(); 1392 while (rep->r_flags & R_LOCKED) { 1393 rep->r_flags |= R_WANTED; 1394 tsleep(rep, 0, "nfstrac", 0); 1395 } 1396 KKASSERT(rep->r_flags & R_ONREQQ); 1397 TAILQ_REMOVE(&nmp->nm_reqq, rep, r_chain); 1398 rep->r_flags &= ~R_ONREQQ; 1399 --nmp->nm_reqqlen; 1400 if (TAILQ_FIRST(&nmp->nm_bioq) && 1401 nmp->nm_reqqlen <= nfs_maxasyncbio * 2 / 3) { 1402 nfssvc_iod_writer_wakeup(nmp); 1403 } 1404 crit_exit(); 1405 1406 /* 1407 * Decrement the outstanding request count. 1408 */ 1409 if (rep->r_flags & R_SENT) { 1410 rep->r_flags &= ~R_SENT; 1411 } 1412 return (error); 1413 } 1414 1415 /* 1416 * Process reply with error returned from nfs_requet_waitreply(). 1417 * 1418 * Returns EAGAIN if it wants us to loop up to nfs_request_try() again. 1419 * Returns ENEEDAUTH if it wants us to loop up to nfs_request_auth() again. 1420 */ 1421 static int 1422 nfs_request_processreply(nfsm_info_t info, int error) 1423 { 1424 struct nfsreq *req = info->req; 1425 struct nfsmount *nmp = req->r_nmp; 1426 u_int32_t *tl; 1427 int verf_type; 1428 int i; 1429 1430 /* 1431 * If there was a successful reply and a tprintf msg. 1432 * tprintf a response. 1433 */ 1434 if (error == 0 && (req->r_flags & R_TPRINTFMSG)) { 1435 nfs_msg(req->r_td, nmp->nm_mountp->mnt_stat.f_mntfromname, 1436 "is alive again"); 1437 } 1438 info->mrep = req->r_mrep; 1439 info->md = req->r_md; 1440 info->dpos = req->r_dpos; 1441 if (error) { 1442 m_freem(req->r_mreq); 1443 req->r_mreq = NULL; 1444 kfree(req, M_NFSREQ); 1445 info->req = NULL; 1446 return (error); 1447 } 1448 1449 /* 1450 * break down the rpc header and check if ok 1451 */ 1452 NULLOUT(tl = nfsm_dissect(info, 3 * NFSX_UNSIGNED)); 1453 if (*tl++ == rpc_msgdenied) { 1454 if (*tl == rpc_mismatch) { 1455 error = EOPNOTSUPP; 1456 } else if ((nmp->nm_flag & NFSMNT_KERB) && 1457 *tl++ == rpc_autherr) { 1458 if (req->r_failed_auth == 0) { 1459 req->r_failed_auth++; 1460 req->r_mheadend->m_next = NULL; 1461 m_freem(info->mrep); 1462 info->mrep = NULL; 1463 m_freem(req->r_mreq); 1464 req->r_mreq = NULL; 1465 return (ENEEDAUTH); 1466 } else { 1467 error = EAUTH; 1468 } 1469 } else { 1470 error = EACCES; 1471 } 1472 m_freem(info->mrep); 1473 info->mrep = NULL; 1474 m_freem(req->r_mreq); 1475 req->r_mreq = NULL; 1476 kfree(req, M_NFSREQ); 1477 info->req = NULL; 1478 return (error); 1479 } 1480 1481 /* 1482 * Grab any Kerberos verifier, otherwise just throw it away. 1483 */ 1484 verf_type = fxdr_unsigned(int, *tl++); 1485 i = fxdr_unsigned(int32_t, *tl); 1486 if ((nmp->nm_flag & NFSMNT_KERB) && verf_type == RPCAUTH_KERB4) { 1487 error = nfs_savenickauth(nmp, req->r_cred, i, req->r_key, 1488 &info->md, &info->dpos, info->mrep); 1489 if (error) 1490 goto nfsmout; 1491 } else if (i > 0) { 1492 ERROROUT(nfsm_adv(info, nfsm_rndup(i))); 1493 } 1494 NULLOUT(tl = nfsm_dissect(info, NFSX_UNSIGNED)); 1495 /* 0 == ok */ 1496 if (*tl == 0) { 1497 NULLOUT(tl = nfsm_dissect(info, NFSX_UNSIGNED)); 1498 if (*tl != 0) { 1499 error = fxdr_unsigned(int, *tl); 1500 1501 /* 1502 * Does anyone even implement this? Just impose 1503 * a 1-second delay. 1504 */ 1505 if ((nmp->nm_flag & NFSMNT_NFSV3) && 1506 error == NFSERR_TRYLATER) { 1507 m_freem(info->mrep); 1508 info->mrep = NULL; 1509 error = 0; 1510 1511 tsleep((caddr_t)&lbolt, 0, "nqnfstry", 0); 1512 return (EAGAIN); /* goto tryagain */ 1513 } 1514 1515 /* 1516 * If the File Handle was stale, invalidate the 1517 * lookup cache, just in case. 1518 * 1519 * To avoid namecache<->vnode deadlocks we must 1520 * release the vnode lock if we hold it. 1521 */ 1522 if (error == ESTALE) { 1523 struct vnode *vp = req->r_vp; 1524 int ltype; 1525 1526 ltype = lockstatus(&vp->v_lock, curthread); 1527 if (ltype == LK_EXCLUSIVE || ltype == LK_SHARED) 1528 lockmgr(&vp->v_lock, LK_RELEASE); 1529 cache_inval_vp(vp, CINV_CHILDREN); 1530 if (ltype == LK_EXCLUSIVE || ltype == LK_SHARED) 1531 lockmgr(&vp->v_lock, ltype); 1532 } 1533 if (nmp->nm_flag & NFSMNT_NFSV3) { 1534 KKASSERT(*req->r_mrp == info->mrep); 1535 KKASSERT(*req->r_mdp == info->md); 1536 KKASSERT(*req->r_dposp == info->dpos); 1537 error |= NFSERR_RETERR; 1538 } else { 1539 m_freem(info->mrep); 1540 info->mrep = NULL; 1541 } 1542 m_freem(req->r_mreq); 1543 req->r_mreq = NULL; 1544 kfree(req, M_NFSREQ); 1545 info->req = NULL; 1546 return (error); 1547 } 1548 1549 KKASSERT(*req->r_mrp == info->mrep); 1550 KKASSERT(*req->r_mdp == info->md); 1551 KKASSERT(*req->r_dposp == info->dpos); 1552 m_freem(req->r_mreq); 1553 req->r_mreq = NULL; 1554 kfree(req, M_NFSREQ); 1555 return (0); 1556 } 1557 m_freem(info->mrep); 1558 info->mrep = NULL; 1559 error = EPROTONOSUPPORT; 1560 nfsmout: 1561 m_freem(req->r_mreq); 1562 req->r_mreq = NULL; 1563 kfree(req, M_NFSREQ); 1564 info->req = NULL; 1565 return (error); 1566 } 1567 1568 #ifndef NFS_NOSERVER 1569 /* 1570 * Generate the rpc reply header 1571 * siz arg. is used to decide if adding a cluster is worthwhile 1572 */ 1573 int 1574 nfs_rephead(int siz, struct nfsrv_descript *nd, struct nfssvc_sock *slp, 1575 int err, struct mbuf **mrq, struct mbuf **mbp, caddr_t *bposp) 1576 { 1577 u_int32_t *tl; 1578 struct nfsm_info info; 1579 1580 siz += RPC_REPLYSIZ; 1581 info.mb = m_getl(max_hdr + siz, MB_WAIT, MT_DATA, M_PKTHDR, NULL); 1582 info.mreq = info.mb; 1583 info.mreq->m_pkthdr.len = 0; 1584 /* 1585 * If this is not a cluster, try and leave leading space 1586 * for the lower level headers. 1587 */ 1588 if ((max_hdr + siz) < MINCLSIZE) 1589 info.mreq->m_data += max_hdr; 1590 tl = mtod(info.mreq, u_int32_t *); 1591 info.mreq->m_len = 6 * NFSX_UNSIGNED; 1592 info.bpos = ((caddr_t)tl) + info.mreq->m_len; 1593 *tl++ = txdr_unsigned(nd->nd_retxid); 1594 *tl++ = rpc_reply; 1595 if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) { 1596 *tl++ = rpc_msgdenied; 1597 if (err & NFSERR_AUTHERR) { 1598 *tl++ = rpc_autherr; 1599 *tl = txdr_unsigned(err & ~NFSERR_AUTHERR); 1600 info.mreq->m_len -= NFSX_UNSIGNED; 1601 info.bpos -= NFSX_UNSIGNED; 1602 } else { 1603 *tl++ = rpc_mismatch; 1604 *tl++ = txdr_unsigned(RPC_VER2); 1605 *tl = txdr_unsigned(RPC_VER2); 1606 } 1607 } else { 1608 *tl++ = rpc_msgaccepted; 1609 1610 /* 1611 * For Kerberos authentication, we must send the nickname 1612 * verifier back, otherwise just RPCAUTH_NULL. 1613 */ 1614 if (nd->nd_flag & ND_KERBFULL) { 1615 struct nfsuid *nuidp; 1616 struct timeval ktvin, ktvout; 1617 1618 for (nuidp = NUIDHASH(slp, nd->nd_cr.cr_uid)->lh_first; 1619 nuidp != NULL; nuidp = nuidp->nu_hash.le_next) { 1620 if (nuidp->nu_cr.cr_uid == nd->nd_cr.cr_uid && 1621 (!nd->nd_nam2 || netaddr_match(NU_NETFAM(nuidp), 1622 &nuidp->nu_haddr, nd->nd_nam2))) 1623 break; 1624 } 1625 if (nuidp) { 1626 ktvin.tv_sec = 1627 txdr_unsigned(nuidp->nu_timestamp.tv_sec - 1); 1628 ktvin.tv_usec = 1629 txdr_unsigned(nuidp->nu_timestamp.tv_usec); 1630 1631 /* 1632 * Encrypt the timestamp in ecb mode using the 1633 * session key. 1634 */ 1635 #ifdef NFSKERB 1636 XXX 1637 #else 1638 ktvout.tv_sec = 0; 1639 ktvout.tv_usec = 0; 1640 #endif 1641 1642 *tl++ = rpc_auth_kerb; 1643 *tl++ = txdr_unsigned(3 * NFSX_UNSIGNED); 1644 *tl = ktvout.tv_sec; 1645 tl = nfsm_build(&info, 3 * NFSX_UNSIGNED); 1646 *tl++ = ktvout.tv_usec; 1647 *tl++ = txdr_unsigned(nuidp->nu_cr.cr_uid); 1648 } else { 1649 *tl++ = 0; 1650 *tl++ = 0; 1651 } 1652 } else { 1653 *tl++ = 0; 1654 *tl++ = 0; 1655 } 1656 switch (err) { 1657 case EPROGUNAVAIL: 1658 *tl = txdr_unsigned(RPC_PROGUNAVAIL); 1659 break; 1660 case EPROGMISMATCH: 1661 *tl = txdr_unsigned(RPC_PROGMISMATCH); 1662 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED); 1663 *tl++ = txdr_unsigned(2); 1664 *tl = txdr_unsigned(3); 1665 break; 1666 case EPROCUNAVAIL: 1667 *tl = txdr_unsigned(RPC_PROCUNAVAIL); 1668 break; 1669 case EBADRPC: 1670 *tl = txdr_unsigned(RPC_GARBAGE); 1671 break; 1672 default: 1673 *tl = 0; 1674 if (err != NFSERR_RETVOID) { 1675 tl = nfsm_build(&info, NFSX_UNSIGNED); 1676 if (err) 1677 *tl = txdr_unsigned(nfsrv_errmap(nd, err)); 1678 else 1679 *tl = 0; 1680 } 1681 break; 1682 }; 1683 } 1684 1685 if (mrq != NULL) 1686 *mrq = info.mreq; 1687 *mbp = info.mb; 1688 *bposp = info.bpos; 1689 if (err != 0 && err != NFSERR_RETVOID) 1690 nfsstats.srvrpc_errs++; 1691 return (0); 1692 } 1693 1694 1695 #endif /* NFS_NOSERVER */ 1696 1697 /* 1698 * Nfs timer routine. 1699 * 1700 * Scan the nfsreq list and retranmit any requests that have timed out 1701 * To avoid retransmission attempts on STREAM sockets (in the future) make 1702 * sure to set the r_retry field to 0 (implies nm_retry == 0). 1703 * 1704 * Requests with attached responses, terminated requests, and 1705 * locked requests are ignored. Locked requests will be picked up 1706 * in a later timer call. 1707 */ 1708 void 1709 nfs_timer_callout(void *arg /* never used */) 1710 { 1711 struct nfsmount *nmp; 1712 struct nfsreq *req; 1713 #ifndef NFS_NOSERVER 1714 struct nfssvc_sock *slp; 1715 u_quad_t cur_usec; 1716 #endif /* NFS_NOSERVER */ 1717 1718 lwkt_gettoken(&nfs_token); 1719 TAILQ_FOREACH(nmp, &nfs_mountq, nm_entry) { 1720 lwkt_gettoken(&nmp->nm_token); 1721 TAILQ_FOREACH(req, &nmp->nm_reqq, r_chain) { 1722 KKASSERT(nmp == req->r_nmp); 1723 if (req->r_mrep) 1724 continue; 1725 if (req->r_flags & (R_SOFTTERM | R_LOCKED)) 1726 continue; 1727 1728 /* 1729 * Handle timeout/retry. Be sure to process r_mrep 1730 * for async requests that completed while we had 1731 * the request locked or they will hang in the reqq 1732 * forever. 1733 */ 1734 req->r_flags |= R_LOCKED; 1735 if (nfs_sigintr(nmp, req, req->r_td)) { 1736 nfs_softterm(req, 1); 1737 req->r_flags &= ~R_LOCKED; 1738 } else { 1739 nfs_timer_req(req); 1740 if (req->r_flags & R_ASYNC) { 1741 if (req->r_mrep) 1742 nfs_hardterm(req, 1); 1743 req->r_flags &= ~R_LOCKED; 1744 nfssvc_iod_reader_wakeup(nmp); 1745 } else { 1746 req->r_flags &= ~R_LOCKED; 1747 } 1748 } 1749 if (req->r_flags & R_WANTED) { 1750 req->r_flags &= ~R_WANTED; 1751 wakeup(req); 1752 } 1753 } 1754 lwkt_reltoken(&nmp->nm_token); 1755 } 1756 #ifndef NFS_NOSERVER 1757 1758 /* 1759 * Scan the write gathering queues for writes that need to be 1760 * completed now. 1761 */ 1762 cur_usec = nfs_curusec(); 1763 1764 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) { 1765 /* XXX race against removal */ 1766 if (lwkt_trytoken(&slp->ns_token)) { 1767 if (slp->ns_tq.lh_first && 1768 (slp->ns_tq.lh_first->nd_time <= cur_usec)) { 1769 nfsrv_wakenfsd(slp, 1); 1770 } 1771 lwkt_reltoken(&slp->ns_token); 1772 } 1773 } 1774 #endif /* NFS_NOSERVER */ 1775 1776 callout_reset(&nfs_timer_handle, nfs_ticks, nfs_timer_callout, NULL); 1777 lwkt_reltoken(&nfs_token); 1778 } 1779 1780 static 1781 void 1782 nfs_timer_req(struct nfsreq *req) 1783 { 1784 struct thread *td = &thread0; /* XXX for creds, will break if sleep */ 1785 struct nfsmount *nmp = req->r_nmp; 1786 struct mbuf *m; 1787 struct socket *so; 1788 int timeo; 1789 int error; 1790 1791 /* 1792 * rtt ticks and timeout calculation. Return if the timeout 1793 * has not been reached yet, unless the packet is flagged 1794 * for an immediate send. 1795 * 1796 * The mean rtt doesn't help when we get random I/Os, we have 1797 * to multiply by fairly large numbers. 1798 */ 1799 if (req->r_rtt >= 0) { 1800 /* 1801 * Calculate the timeout to test against. 1802 */ 1803 req->r_rtt++; 1804 if (nmp->nm_flag & NFSMNT_DUMBTIMR) { 1805 timeo = nmp->nm_timeo << NFS_RTT_SCALE_BITS; 1806 } else if (req->r_flags & R_TIMING) { 1807 timeo = NFS_SRTT(req) + NFS_SDRTT(req); 1808 } else { 1809 timeo = nmp->nm_timeo << NFS_RTT_SCALE_BITS; 1810 } 1811 timeo *= multt[req->r_procnum]; 1812 /* timeo is still scaled by SCALE_BITS */ 1813 1814 #define NFSFS (NFS_RTT_SCALE * NFS_HZ) 1815 if (req->r_flags & R_TIMING) { 1816 static long last_time; 1817 if (nfs_showrtt && last_time != time_second) { 1818 kprintf("rpccmd %d NFS SRTT %d SDRTT %d " 1819 "timeo %d.%03d\n", 1820 proct[req->r_procnum], 1821 NFS_SRTT(req), NFS_SDRTT(req), 1822 timeo / NFSFS, 1823 timeo % NFSFS * 1000 / NFSFS); 1824 last_time = time_second; 1825 } 1826 } 1827 #undef NFSFS 1828 1829 /* 1830 * deal with nfs_timer jitter. 1831 */ 1832 timeo = (timeo >> NFS_RTT_SCALE_BITS) + 1; 1833 if (timeo < 2) 1834 timeo = 2; 1835 1836 if (nmp->nm_timeouts > 0) 1837 timeo *= nfs_backoff[nmp->nm_timeouts - 1]; 1838 if (timeo > NFS_MAXTIMEO) 1839 timeo = NFS_MAXTIMEO; 1840 if (req->r_rtt <= timeo) { 1841 if ((req->r_flags & R_NEEDSXMIT) == 0) 1842 return; 1843 } else if (nmp->nm_timeouts < 8) { 1844 nmp->nm_timeouts++; 1845 } 1846 } 1847 1848 /* 1849 * Check for server not responding 1850 */ 1851 if ((req->r_flags & R_TPRINTFMSG) == 0 && 1852 req->r_rexmit > nmp->nm_deadthresh) { 1853 nfs_msg(req->r_td, nmp->nm_mountp->mnt_stat.f_mntfromname, 1854 "not responding"); 1855 req->r_flags |= R_TPRINTFMSG; 1856 } 1857 if (req->r_rexmit >= req->r_retry) { /* too many */ 1858 nfsstats.rpctimeouts++; 1859 nfs_softterm(req, 1); 1860 return; 1861 } 1862 1863 /* 1864 * Generally disable retransmission on reliable sockets, 1865 * unless the request is flagged for immediate send. 1866 */ 1867 if (nmp->nm_sotype != SOCK_DGRAM) { 1868 if (++req->r_rexmit > NFS_MAXREXMIT) 1869 req->r_rexmit = NFS_MAXREXMIT; 1870 if ((req->r_flags & R_NEEDSXMIT) == 0) 1871 return; 1872 } 1873 1874 /* 1875 * Stop here if we do not have a socket! 1876 */ 1877 if ((so = nmp->nm_so) == NULL) 1878 return; 1879 1880 /* 1881 * If there is enough space and the window allows.. resend it. 1882 * 1883 * r_rtt is left intact in case we get an answer after the 1884 * retry that was a reply to the original packet. 1885 * 1886 * NOTE: so_pru_send() 1887 */ 1888 if (ssb_space(&so->so_snd) >= req->r_mreq->m_pkthdr.len && 1889 (req->r_flags & (R_SENT | R_NEEDSXMIT)) && 1890 (m = m_copym(req->r_mreq, 0, M_COPYALL, MB_DONTWAIT))){ 1891 if ((nmp->nm_flag & NFSMNT_NOCONN) == 0) 1892 error = so_pru_send(so, 0, m, NULL, NULL, td); 1893 else 1894 error = so_pru_send(so, 0, m, nmp->nm_nam, NULL, td); 1895 if (error) { 1896 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) 1897 so->so_error = 0; 1898 req->r_flags |= R_NEEDSXMIT; 1899 } else if (req->r_mrep == NULL) { 1900 /* 1901 * Iff first send, start timing 1902 * else turn timing off, backoff timer 1903 * and divide congestion window by 2. 1904 * 1905 * It is possible for the so_pru_send() to 1906 * block and for us to race a reply so we 1907 * only do this if the reply field has not 1908 * been filled in. R_LOCKED will prevent 1909 * the request from being ripped out from under 1910 * us entirely. 1911 * 1912 * Record the last resent procnum to aid us 1913 * in duplicate detection on receive. 1914 */ 1915 if ((req->r_flags & R_NEEDSXMIT) == 0) { 1916 if (nfs_showrexmit) 1917 kprintf("X"); 1918 if (++req->r_rexmit > NFS_MAXREXMIT) 1919 req->r_rexmit = NFS_MAXREXMIT; 1920 nmp->nm_maxasync_scaled >>= 1; 1921 if (nmp->nm_maxasync_scaled < NFS_MINASYNC_SCALED) 1922 nmp->nm_maxasync_scaled = NFS_MINASYNC_SCALED; 1923 nfsstats.rpcretries++; 1924 nmp->nm_lastreprocnum = req->r_procnum; 1925 } else { 1926 req->r_flags |= R_SENT; 1927 req->r_flags &= ~R_NEEDSXMIT; 1928 } 1929 } 1930 } 1931 } 1932 1933 /* 1934 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and 1935 * wait for all requests to complete. This is used by forced unmounts 1936 * to terminate any outstanding RPCs. 1937 * 1938 * Locked requests cannot be canceled but will be marked for 1939 * soft-termination. 1940 */ 1941 int 1942 nfs_nmcancelreqs(struct nfsmount *nmp) 1943 { 1944 struct nfsreq *req; 1945 int i; 1946 1947 crit_enter(); 1948 TAILQ_FOREACH(req, &nmp->nm_reqq, r_chain) { 1949 if (req->r_mrep != NULL || (req->r_flags & R_SOFTTERM)) 1950 continue; 1951 nfs_softterm(req, 0); 1952 } 1953 /* XXX the other two queues as well */ 1954 crit_exit(); 1955 1956 for (i = 0; i < 30; i++) { 1957 crit_enter(); 1958 TAILQ_FOREACH(req, &nmp->nm_reqq, r_chain) { 1959 if (nmp == req->r_nmp) 1960 break; 1961 } 1962 crit_exit(); 1963 if (req == NULL) 1964 return (0); 1965 tsleep(&lbolt, 0, "nfscancel", 0); 1966 } 1967 return (EBUSY); 1968 } 1969 1970 /* 1971 * Soft-terminate a request, effectively marking it as failed. 1972 * 1973 * Must be called from within a critical section. 1974 */ 1975 static void 1976 nfs_softterm(struct nfsreq *rep, int islocked) 1977 { 1978 rep->r_flags |= R_SOFTTERM; 1979 nfs_hardterm(rep, islocked); 1980 } 1981 1982 /* 1983 * Hard-terminate a request, typically after getting a response. 1984 * 1985 * The state machine can still decide to re-issue it later if necessary. 1986 * 1987 * Must be called from within a critical section. 1988 */ 1989 static void 1990 nfs_hardterm(struct nfsreq *rep, int islocked) 1991 { 1992 struct nfsmount *nmp = rep->r_nmp; 1993 1994 /* 1995 * The nm_send count is decremented now to avoid deadlocks 1996 * when the process in soreceive() hasn't yet managed to send 1997 * its own request. 1998 */ 1999 if (rep->r_flags & R_SENT) { 2000 rep->r_flags &= ~R_SENT; 2001 } 2002 2003 /* 2004 * If we locked the request or nobody else has locked the request, 2005 * and the request is async, we can move it to the reader thread's 2006 * queue now and fix up the state. 2007 * 2008 * If we locked the request or nobody else has locked the request, 2009 * we can wake up anyone blocked waiting for a response on the 2010 * request. 2011 */ 2012 if (islocked || (rep->r_flags & R_LOCKED) == 0) { 2013 if ((rep->r_flags & (R_ONREQQ | R_ASYNC)) == 2014 (R_ONREQQ | R_ASYNC)) { 2015 rep->r_flags &= ~R_ONREQQ; 2016 TAILQ_REMOVE(&nmp->nm_reqq, rep, r_chain); 2017 --nmp->nm_reqqlen; 2018 TAILQ_INSERT_TAIL(&nmp->nm_reqrxq, rep, r_chain); 2019 KKASSERT(rep->r_info->state == NFSM_STATE_TRY || 2020 rep->r_info->state == NFSM_STATE_WAITREPLY); 2021 rep->r_info->state = NFSM_STATE_PROCESSREPLY; 2022 nfssvc_iod_reader_wakeup(nmp); 2023 if (TAILQ_FIRST(&nmp->nm_bioq) && 2024 nmp->nm_reqqlen <= nfs_maxasyncbio * 2 / 3) { 2025 nfssvc_iod_writer_wakeup(nmp); 2026 } 2027 } 2028 mtx_abort_ex_link(&nmp->nm_rxlock, &rep->r_link); 2029 } 2030 } 2031 2032 /* 2033 * Test for a termination condition pending on the process. 2034 * This is used for NFSMNT_INT mounts. 2035 */ 2036 int 2037 nfs_sigintr(struct nfsmount *nmp, struct nfsreq *rep, struct thread *td) 2038 { 2039 sigset_t tmpset; 2040 struct proc *p; 2041 struct lwp *lp; 2042 2043 if (rep && (rep->r_flags & R_SOFTTERM)) 2044 return (EINTR); 2045 /* Terminate all requests while attempting a forced unmount. */ 2046 if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF) 2047 return (EINTR); 2048 if (!(nmp->nm_flag & NFSMNT_INT)) 2049 return (0); 2050 /* td might be NULL YYY */ 2051 if (td == NULL || (p = td->td_proc) == NULL) 2052 return (0); 2053 2054 lp = td->td_lwp; 2055 tmpset = lwp_sigpend(lp); 2056 SIGSETNAND(tmpset, lp->lwp_sigmask); 2057 SIGSETNAND(tmpset, p->p_sigignore); 2058 if (SIGNOTEMPTY(tmpset) && NFSINT_SIGMASK(tmpset)) 2059 return (EINTR); 2060 2061 return (0); 2062 } 2063 2064 /* 2065 * Lock a socket against others. 2066 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply 2067 * and also to avoid race conditions between the processes with nfs requests 2068 * in progress when a reconnect is necessary. 2069 */ 2070 int 2071 nfs_sndlock(struct nfsmount *nmp, struct nfsreq *rep) 2072 { 2073 mtx_t mtx = &nmp->nm_txlock; 2074 struct thread *td; 2075 int slptimeo; 2076 int slpflag; 2077 int error; 2078 2079 slpflag = 0; 2080 slptimeo = 0; 2081 td = rep ? rep->r_td : NULL; 2082 if (nmp->nm_flag & NFSMNT_INT) 2083 slpflag = PCATCH; 2084 2085 while ((error = mtx_lock_ex_try(mtx)) != 0) { 2086 if (nfs_sigintr(nmp, rep, td)) { 2087 error = EINTR; 2088 break; 2089 } 2090 error = mtx_lock_ex(mtx, "nfsndlck", slpflag, slptimeo); 2091 if (error == 0) 2092 break; 2093 if (slpflag == PCATCH) { 2094 slpflag = 0; 2095 slptimeo = 2 * hz; 2096 } 2097 } 2098 /* Always fail if our request has been cancelled. */ 2099 if (rep && (rep->r_flags & R_SOFTTERM)) { 2100 if (error == 0) 2101 mtx_unlock(mtx); 2102 error = EINTR; 2103 } 2104 return (error); 2105 } 2106 2107 /* 2108 * Unlock the stream socket for others. 2109 */ 2110 void 2111 nfs_sndunlock(struct nfsmount *nmp) 2112 { 2113 mtx_unlock(&nmp->nm_txlock); 2114 } 2115 2116 /* 2117 * Lock the receiver side of the socket. 2118 * 2119 * rep may be NULL. 2120 */ 2121 static int 2122 nfs_rcvlock(struct nfsmount *nmp, struct nfsreq *rep) 2123 { 2124 mtx_t mtx = &nmp->nm_rxlock; 2125 int slpflag; 2126 int slptimeo; 2127 int error; 2128 2129 /* 2130 * Unconditionally check for completion in case another nfsiod 2131 * get the packet while the caller was blocked, before the caller 2132 * called us. Packet reception is handled by mainline code which 2133 * is protected by the BGL at the moment. 2134 * 2135 * We do not strictly need the second check just before the 2136 * tsleep(), but it's good defensive programming. 2137 */ 2138 if (rep && rep->r_mrep != NULL) 2139 return (EALREADY); 2140 2141 if (nmp->nm_flag & NFSMNT_INT) 2142 slpflag = PCATCH; 2143 else 2144 slpflag = 0; 2145 slptimeo = 0; 2146 2147 while ((error = mtx_lock_ex_try(mtx)) != 0) { 2148 if (nfs_sigintr(nmp, rep, (rep ? rep->r_td : NULL))) { 2149 error = EINTR; 2150 break; 2151 } 2152 if (rep && rep->r_mrep != NULL) { 2153 error = EALREADY; 2154 break; 2155 } 2156 2157 /* 2158 * NOTE: can return ENOLCK, but in that case rep->r_mrep 2159 * will already be set. 2160 */ 2161 if (rep) { 2162 error = mtx_lock_ex_link(mtx, &rep->r_link, 2163 "nfsrcvlk", 2164 slpflag, slptimeo); 2165 } else { 2166 error = mtx_lock_ex(mtx, "nfsrcvlk", slpflag, slptimeo); 2167 } 2168 if (error == 0) 2169 break; 2170 2171 /* 2172 * If our reply was recieved while we were sleeping, 2173 * then just return without taking the lock to avoid a 2174 * situation where a single iod could 'capture' the 2175 * recieve lock. 2176 */ 2177 if (rep && rep->r_mrep != NULL) { 2178 error = EALREADY; 2179 break; 2180 } 2181 if (slpflag == PCATCH) { 2182 slpflag = 0; 2183 slptimeo = 2 * hz; 2184 } 2185 } 2186 if (error == 0) { 2187 if (rep && rep->r_mrep != NULL) { 2188 error = EALREADY; 2189 mtx_unlock(mtx); 2190 } 2191 } 2192 return (error); 2193 } 2194 2195 /* 2196 * Unlock the stream socket for others. 2197 */ 2198 static void 2199 nfs_rcvunlock(struct nfsmount *nmp) 2200 { 2201 mtx_unlock(&nmp->nm_rxlock); 2202 } 2203 2204 /* 2205 * nfs_realign: 2206 * 2207 * Check for badly aligned mbuf data and realign by copying the unaligned 2208 * portion of the data into a new mbuf chain and freeing the portions 2209 * of the old chain that were replaced. 2210 * 2211 * We cannot simply realign the data within the existing mbuf chain 2212 * because the underlying buffers may contain other rpc commands and 2213 * we cannot afford to overwrite them. 2214 * 2215 * We would prefer to avoid this situation entirely. The situation does 2216 * not occur with NFS/UDP and is supposed to only occassionally occur 2217 * with TCP. Use vfs.nfs.realign_count and realign_test to check this. 2218 * 2219 * NOTE! MB_DONTWAIT cannot be used here. The mbufs must be acquired 2220 * because the rpc request OR reply cannot be thrown away. TCP NFS 2221 * mounts do not retry their RPCs unless the TCP connection itself 2222 * is dropped so throwing away a RPC will basically cause the NFS 2223 * operation to lockup indefinitely. 2224 */ 2225 static void 2226 nfs_realign(struct mbuf **pm, int hsiz) 2227 { 2228 struct mbuf *m; 2229 struct mbuf *n = NULL; 2230 2231 /* 2232 * Check for misalignemnt 2233 */ 2234 ++nfs_realign_test; 2235 while ((m = *pm) != NULL) { 2236 if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) 2237 break; 2238 pm = &m->m_next; 2239 } 2240 2241 /* 2242 * If misalignment found make a completely new copy. 2243 */ 2244 if (m) { 2245 ++nfs_realign_count; 2246 n = m_dup_data(m, MB_WAIT); 2247 m_freem(*pm); 2248 *pm = n; 2249 } 2250 } 2251 2252 #ifndef NFS_NOSERVER 2253 2254 /* 2255 * Parse an RPC request 2256 * - verify it 2257 * - fill in the cred struct. 2258 */ 2259 int 2260 nfs_getreq(struct nfsrv_descript *nd, struct nfsd *nfsd, int has_header) 2261 { 2262 int len, i; 2263 u_int32_t *tl; 2264 struct uio uio; 2265 struct iovec iov; 2266 caddr_t cp; 2267 u_int32_t nfsvers, auth_type; 2268 uid_t nickuid; 2269 int error = 0, ticklen; 2270 struct nfsuid *nuidp; 2271 struct timeval tvin, tvout; 2272 struct nfsm_info info; 2273 #if 0 /* until encrypted keys are implemented */ 2274 NFSKERBKEYSCHED_T keys; /* stores key schedule */ 2275 #endif 2276 2277 info.mrep = nd->nd_mrep; 2278 info.md = nd->nd_md; 2279 info.dpos = nd->nd_dpos; 2280 2281 if (has_header) { 2282 NULLOUT(tl = nfsm_dissect(&info, 10 * NFSX_UNSIGNED)); 2283 nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++); 2284 if (*tl++ != rpc_call) { 2285 m_freem(info.mrep); 2286 return (EBADRPC); 2287 } 2288 } else { 2289 NULLOUT(tl = nfsm_dissect(&info, 8 * NFSX_UNSIGNED)); 2290 } 2291 nd->nd_repstat = 0; 2292 nd->nd_flag = 0; 2293 if (*tl++ != rpc_vers) { 2294 nd->nd_repstat = ERPCMISMATCH; 2295 nd->nd_procnum = NFSPROC_NOOP; 2296 return (0); 2297 } 2298 if (*tl != nfs_prog) { 2299 nd->nd_repstat = EPROGUNAVAIL; 2300 nd->nd_procnum = NFSPROC_NOOP; 2301 return (0); 2302 } 2303 tl++; 2304 nfsvers = fxdr_unsigned(u_int32_t, *tl++); 2305 if (nfsvers < NFS_VER2 || nfsvers > NFS_VER3) { 2306 nd->nd_repstat = EPROGMISMATCH; 2307 nd->nd_procnum = NFSPROC_NOOP; 2308 return (0); 2309 } 2310 if (nfsvers == NFS_VER3) 2311 nd->nd_flag = ND_NFSV3; 2312 nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++); 2313 if (nd->nd_procnum == NFSPROC_NULL) 2314 return (0); 2315 if (nd->nd_procnum >= NFS_NPROCS || 2316 (nd->nd_procnum >= NQNFSPROC_GETLEASE) || 2317 (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) { 2318 nd->nd_repstat = EPROCUNAVAIL; 2319 nd->nd_procnum = NFSPROC_NOOP; 2320 return (0); 2321 } 2322 if ((nd->nd_flag & ND_NFSV3) == 0) 2323 nd->nd_procnum = nfsv3_procid[nd->nd_procnum]; 2324 auth_type = *tl++; 2325 len = fxdr_unsigned(int, *tl++); 2326 if (len < 0 || len > RPCAUTH_MAXSIZ) { 2327 m_freem(info.mrep); 2328 return (EBADRPC); 2329 } 2330 2331 nd->nd_flag &= ~ND_KERBAUTH; 2332 /* 2333 * Handle auth_unix or auth_kerb. 2334 */ 2335 if (auth_type == rpc_auth_unix) { 2336 len = fxdr_unsigned(int, *++tl); 2337 if (len < 0 || len > NFS_MAXNAMLEN) { 2338 m_freem(info.mrep); 2339 return (EBADRPC); 2340 } 2341 ERROROUT(nfsm_adv(&info, nfsm_rndup(len))); 2342 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED)); 2343 bzero((caddr_t)&nd->nd_cr, sizeof (struct ucred)); 2344 nd->nd_cr.cr_ref = 1; 2345 nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++); 2346 nd->nd_cr.cr_ruid = nd->nd_cr.cr_svuid = nd->nd_cr.cr_uid; 2347 nd->nd_cr.cr_gid = fxdr_unsigned(gid_t, *tl++); 2348 nd->nd_cr.cr_rgid = nd->nd_cr.cr_svgid = nd->nd_cr.cr_gid; 2349 len = fxdr_unsigned(int, *tl); 2350 if (len < 0 || len > RPCAUTH_UNIXGIDS) { 2351 m_freem(info.mrep); 2352 return (EBADRPC); 2353 } 2354 NULLOUT(tl = nfsm_dissect(&info, (len + 2) * NFSX_UNSIGNED)); 2355 for (i = 1; i <= len; i++) 2356 if (i < NGROUPS) 2357 nd->nd_cr.cr_groups[i] = fxdr_unsigned(gid_t, *tl++); 2358 else 2359 tl++; 2360 nd->nd_cr.cr_ngroups = (len >= NGROUPS) ? NGROUPS : (len + 1); 2361 if (nd->nd_cr.cr_ngroups > 1) 2362 nfsrvw_sort(nd->nd_cr.cr_groups, nd->nd_cr.cr_ngroups); 2363 len = fxdr_unsigned(int, *++tl); 2364 if (len < 0 || len > RPCAUTH_MAXSIZ) { 2365 m_freem(info.mrep); 2366 return (EBADRPC); 2367 } 2368 if (len > 0) { 2369 ERROROUT(nfsm_adv(&info, nfsm_rndup(len))); 2370 } 2371 } else if (auth_type == rpc_auth_kerb) { 2372 switch (fxdr_unsigned(int, *tl++)) { 2373 case RPCAKN_FULLNAME: 2374 ticklen = fxdr_unsigned(int, *tl); 2375 *((u_int32_t *)nfsd->nfsd_authstr) = *tl; 2376 uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED; 2377 nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED; 2378 if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) { 2379 m_freem(info.mrep); 2380 return (EBADRPC); 2381 } 2382 uio.uio_offset = 0; 2383 uio.uio_iov = &iov; 2384 uio.uio_iovcnt = 1; 2385 uio.uio_segflg = UIO_SYSSPACE; 2386 iov.iov_base = (caddr_t)&nfsd->nfsd_authstr[4]; 2387 iov.iov_len = RPCAUTH_MAXSIZ - 4; 2388 ERROROUT(nfsm_mtouio(&info, &uio, uio.uio_resid)); 2389 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED)); 2390 if (*tl++ != rpc_auth_kerb || 2391 fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) { 2392 kprintf("Bad kerb verifier\n"); 2393 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF); 2394 nd->nd_procnum = NFSPROC_NOOP; 2395 return (0); 2396 } 2397 NULLOUT(cp = nfsm_dissect(&info, 4 * NFSX_UNSIGNED)); 2398 tl = (u_int32_t *)cp; 2399 if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) { 2400 kprintf("Not fullname kerb verifier\n"); 2401 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF); 2402 nd->nd_procnum = NFSPROC_NOOP; 2403 return (0); 2404 } 2405 cp += NFSX_UNSIGNED; 2406 bcopy(cp, nfsd->nfsd_verfstr, 3 * NFSX_UNSIGNED); 2407 nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED; 2408 nd->nd_flag |= ND_KERBFULL; 2409 nfsd->nfsd_flag |= NFSD_NEEDAUTH; 2410 break; 2411 case RPCAKN_NICKNAME: 2412 if (len != 2 * NFSX_UNSIGNED) { 2413 kprintf("Kerb nickname short\n"); 2414 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED); 2415 nd->nd_procnum = NFSPROC_NOOP; 2416 return (0); 2417 } 2418 nickuid = fxdr_unsigned(uid_t, *tl); 2419 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED)); 2420 if (*tl++ != rpc_auth_kerb || 2421 fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) { 2422 kprintf("Kerb nick verifier bad\n"); 2423 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF); 2424 nd->nd_procnum = NFSPROC_NOOP; 2425 return (0); 2426 } 2427 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED)); 2428 tvin.tv_sec = *tl++; 2429 tvin.tv_usec = *tl; 2430 2431 for (nuidp = NUIDHASH(nfsd->nfsd_slp,nickuid)->lh_first; 2432 nuidp != NULL; nuidp = nuidp->nu_hash.le_next) { 2433 if (nuidp->nu_cr.cr_uid == nickuid && 2434 (!nd->nd_nam2 || 2435 netaddr_match(NU_NETFAM(nuidp), 2436 &nuidp->nu_haddr, nd->nd_nam2))) 2437 break; 2438 } 2439 if (!nuidp) { 2440 nd->nd_repstat = 2441 (NFSERR_AUTHERR|AUTH_REJECTCRED); 2442 nd->nd_procnum = NFSPROC_NOOP; 2443 return (0); 2444 } 2445 2446 /* 2447 * Now, decrypt the timestamp using the session key 2448 * and validate it. 2449 */ 2450 #ifdef NFSKERB 2451 XXX 2452 #else 2453 tvout.tv_sec = 0; 2454 tvout.tv_usec = 0; 2455 #endif 2456 2457 tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec); 2458 tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec); 2459 if (nuidp->nu_expire < time_second || 2460 nuidp->nu_timestamp.tv_sec > tvout.tv_sec || 2461 (nuidp->nu_timestamp.tv_sec == tvout.tv_sec && 2462 nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) { 2463 nuidp->nu_expire = 0; 2464 nd->nd_repstat = 2465 (NFSERR_AUTHERR|AUTH_REJECTVERF); 2466 nd->nd_procnum = NFSPROC_NOOP; 2467 return (0); 2468 } 2469 nfsrv_setcred(&nuidp->nu_cr, &nd->nd_cr); 2470 nd->nd_flag |= ND_KERBNICK; 2471 }; 2472 } else { 2473 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED); 2474 nd->nd_procnum = NFSPROC_NOOP; 2475 return (0); 2476 } 2477 2478 nd->nd_md = info.md; 2479 nd->nd_dpos = info.dpos; 2480 return (0); 2481 nfsmout: 2482 return (error); 2483 } 2484 2485 #endif 2486 2487 /* 2488 * Send a message to the originating process's terminal. The thread and/or 2489 * process may be NULL. YYY the thread should not be NULL but there may 2490 * still be some uio_td's that are still being passed as NULL through to 2491 * nfsm_request(). 2492 */ 2493 static int 2494 nfs_msg(struct thread *td, char *server, char *msg) 2495 { 2496 tpr_t tpr; 2497 2498 if (td && td->td_proc) 2499 tpr = tprintf_open(td->td_proc); 2500 else 2501 tpr = NULL; 2502 tprintf(tpr, "nfs server %s: %s\n", server, msg); 2503 tprintf_close(tpr); 2504 return (0); 2505 } 2506 2507 #ifndef NFS_NOSERVER 2508 2509 /* 2510 * Socket upcall routine for nfsd sockets. This runs in the protocol 2511 * thread and passes waitflag == MB_DONTWAIT. 2512 */ 2513 void 2514 nfsrv_rcv_upcall(struct socket *so, void *arg, int waitflag) 2515 { 2516 struct nfssvc_sock *slp = (struct nfssvc_sock *)arg; 2517 2518 if (slp->ns_needq_upcall == 0) { 2519 slp->ns_needq_upcall = 1; /* ok to race */ 2520 lwkt_gettoken(&nfs_token); 2521 nfsrv_wakenfsd(slp, 1); 2522 lwkt_reltoken(&nfs_token); 2523 } 2524 #if 0 2525 lwkt_gettoken(&slp->ns_token); 2526 slp->ns_flag |= SLP_NEEDQ; 2527 nfsrv_rcv(so, arg, waitflag); 2528 lwkt_reltoken(&slp->ns_token); 2529 #endif 2530 } 2531 2532 /* 2533 * Process new data on a receive socket. Essentially do as much as we can 2534 * non-blocking, else punt and it will be called with MB_WAIT from an nfsd. 2535 * 2536 * slp->ns_token is held on call 2537 */ 2538 void 2539 nfsrv_rcv(struct socket *so, void *arg, int waitflag) 2540 { 2541 struct nfssvc_sock *slp = (struct nfssvc_sock *)arg; 2542 struct mbuf *m; 2543 struct sockaddr *nam; 2544 struct sockbuf sio; 2545 int flags, error; 2546 int nparallel_wakeup = 0; 2547 2548 ASSERT_LWKT_TOKEN_HELD(&slp->ns_token); 2549 2550 if ((slp->ns_flag & SLP_VALID) == 0) 2551 return; 2552 2553 /* 2554 * Do not allow an infinite number of completed RPC records to build 2555 * up before we stop reading data from the socket. Otherwise we could 2556 * end up holding onto an unreasonable number of mbufs for requests 2557 * waiting for service. 2558 * 2559 * This should give pretty good feedback to the TCP layer and 2560 * prevents a memory crunch for other protocols. 2561 * 2562 * Note that the same service socket can be dispatched to several 2563 * nfs servers simultaniously. The tcp protocol callback calls us 2564 * with MB_DONTWAIT. nfsd calls us with MB_WAIT (typically). 2565 */ 2566 if (NFSRV_RECLIMIT(slp)) 2567 return; 2568 2569 /* 2570 * Handle protocol specifics to parse an RPC request. We always 2571 * pull from the socket using non-blocking I/O. 2572 */ 2573 if (so->so_type == SOCK_STREAM) { 2574 /* 2575 * The data has to be read in an orderly fashion from a TCP 2576 * stream, unlike a UDP socket. It is possible for soreceive 2577 * and/or nfsrv_getstream() to block, so make sure only one 2578 * entity is messing around with the TCP stream at any given 2579 * moment. The receive sockbuf's lock in soreceive is not 2580 * sufficient. 2581 */ 2582 if (slp->ns_flag & SLP_GETSTREAM) 2583 return; 2584 slp->ns_flag |= SLP_GETSTREAM; 2585 2586 /* 2587 * Do soreceive(). Pull out as much data as possible without 2588 * blocking. 2589 */ 2590 sbinit(&sio, 1000000000); 2591 flags = MSG_DONTWAIT; 2592 error = so_pru_soreceive(so, &nam, NULL, &sio, NULL, &flags); 2593 if (error || sio.sb_mb == NULL) { 2594 if (error != EWOULDBLOCK) 2595 slp->ns_flag |= SLP_DISCONN; 2596 slp->ns_flag &= ~(SLP_GETSTREAM | SLP_NEEDQ); 2597 goto done; 2598 } 2599 m = sio.sb_mb; 2600 if (slp->ns_rawend) { 2601 slp->ns_rawend->m_next = m; 2602 slp->ns_cc += sio.sb_cc; 2603 } else { 2604 slp->ns_raw = m; 2605 slp->ns_cc = sio.sb_cc; 2606 } 2607 while (m->m_next) 2608 m = m->m_next; 2609 slp->ns_rawend = m; 2610 2611 /* 2612 * Now try and parse as many record(s) as we can out of the 2613 * raw stream data. This will set SLP_DOREC. 2614 */ 2615 error = nfsrv_getstream(slp, waitflag, &nparallel_wakeup); 2616 if (error && error != EWOULDBLOCK) 2617 slp->ns_flag |= SLP_DISCONN; 2618 slp->ns_flag &= ~SLP_GETSTREAM; 2619 } else { 2620 /* 2621 * For UDP soreceive typically pulls just one packet, loop 2622 * to get the whole batch. 2623 */ 2624 do { 2625 sbinit(&sio, 1000000000); 2626 flags = MSG_DONTWAIT; 2627 error = so_pru_soreceive(so, &nam, NULL, &sio, 2628 NULL, &flags); 2629 if (sio.sb_mb) { 2630 struct nfsrv_rec *rec; 2631 int mf = (waitflag & MB_DONTWAIT) ? 2632 M_NOWAIT : M_WAITOK; 2633 rec = kmalloc(sizeof(struct nfsrv_rec), 2634 M_NFSRVDESC, mf); 2635 if (!rec) { 2636 if (nam) 2637 kfree(nam, M_SONAME); 2638 m_freem(sio.sb_mb); 2639 continue; 2640 } 2641 nfs_realign(&sio.sb_mb, 10 * NFSX_UNSIGNED); 2642 rec->nr_address = nam; 2643 rec->nr_packet = sio.sb_mb; 2644 STAILQ_INSERT_TAIL(&slp->ns_rec, rec, nr_link); 2645 ++slp->ns_numrec; 2646 slp->ns_flag |= SLP_DOREC; 2647 ++nparallel_wakeup; 2648 } else { 2649 slp->ns_flag &= ~SLP_NEEDQ; 2650 } 2651 if (error) { 2652 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) 2653 && error != EWOULDBLOCK) { 2654 slp->ns_flag |= SLP_DISCONN; 2655 break; 2656 } 2657 } 2658 if (NFSRV_RECLIMIT(slp)) 2659 break; 2660 } while (sio.sb_mb); 2661 } 2662 2663 /* 2664 * If we were upcalled from the tcp protocol layer and we have 2665 * fully parsed records ready to go, or there is new data pending, 2666 * or something went wrong, try to wake up a nfsd thread to deal 2667 * with it. 2668 */ 2669 done: 2670 /* XXX this code is currently not executed (nfsrv_rcv_upcall) */ 2671 if (waitflag == MB_DONTWAIT && (slp->ns_flag & SLP_ACTION_MASK)) { 2672 lwkt_gettoken(&nfs_token); 2673 nfsrv_wakenfsd(slp, nparallel_wakeup); 2674 lwkt_reltoken(&nfs_token); 2675 } 2676 } 2677 2678 /* 2679 * Try and extract an RPC request from the mbuf data list received on a 2680 * stream socket. The "waitflag" argument indicates whether or not it 2681 * can sleep. 2682 */ 2683 static int 2684 nfsrv_getstream(struct nfssvc_sock *slp, int waitflag, int *countp) 2685 { 2686 struct mbuf *m, **mpp; 2687 char *cp1, *cp2; 2688 int len; 2689 struct mbuf *om, *m2, *recm; 2690 u_int32_t recmark; 2691 2692 for (;;) { 2693 if (slp->ns_reclen == 0) { 2694 if (slp->ns_cc < NFSX_UNSIGNED) 2695 return (0); 2696 m = slp->ns_raw; 2697 if (m->m_len >= NFSX_UNSIGNED) { 2698 bcopy(mtod(m, caddr_t), (caddr_t)&recmark, NFSX_UNSIGNED); 2699 m->m_data += NFSX_UNSIGNED; 2700 m->m_len -= NFSX_UNSIGNED; 2701 } else { 2702 cp1 = (caddr_t)&recmark; 2703 cp2 = mtod(m, caddr_t); 2704 while (cp1 < ((caddr_t)&recmark) + NFSX_UNSIGNED) { 2705 while (m->m_len == 0) { 2706 m = m->m_next; 2707 cp2 = mtod(m, caddr_t); 2708 } 2709 *cp1++ = *cp2++; 2710 m->m_data++; 2711 m->m_len--; 2712 } 2713 } 2714 slp->ns_cc -= NFSX_UNSIGNED; 2715 recmark = ntohl(recmark); 2716 slp->ns_reclen = recmark & ~0x80000000; 2717 if (recmark & 0x80000000) 2718 slp->ns_flag |= SLP_LASTFRAG; 2719 else 2720 slp->ns_flag &= ~SLP_LASTFRAG; 2721 if (slp->ns_reclen > NFS_MAXPACKET || slp->ns_reclen <= 0) { 2722 log(LOG_ERR, "%s (%d) from nfs client\n", 2723 "impossible packet length", 2724 slp->ns_reclen); 2725 return (EPERM); 2726 } 2727 } 2728 2729 /* 2730 * Now get the record part. 2731 * 2732 * Note that slp->ns_reclen may be 0. Linux sometimes 2733 * generates 0-length RPCs 2734 */ 2735 recm = NULL; 2736 if (slp->ns_cc == slp->ns_reclen) { 2737 recm = slp->ns_raw; 2738 slp->ns_raw = slp->ns_rawend = NULL; 2739 slp->ns_cc = slp->ns_reclen = 0; 2740 } else if (slp->ns_cc > slp->ns_reclen) { 2741 len = 0; 2742 m = slp->ns_raw; 2743 om = NULL; 2744 2745 while (len < slp->ns_reclen) { 2746 if ((len + m->m_len) > slp->ns_reclen) { 2747 m2 = m_copym(m, 0, slp->ns_reclen - len, 2748 waitflag); 2749 if (m2) { 2750 if (om) { 2751 om->m_next = m2; 2752 recm = slp->ns_raw; 2753 } else 2754 recm = m2; 2755 m->m_data += slp->ns_reclen - len; 2756 m->m_len -= slp->ns_reclen - len; 2757 len = slp->ns_reclen; 2758 } else { 2759 return (EWOULDBLOCK); 2760 } 2761 } else if ((len + m->m_len) == slp->ns_reclen) { 2762 om = m; 2763 len += m->m_len; 2764 m = m->m_next; 2765 recm = slp->ns_raw; 2766 om->m_next = NULL; 2767 } else { 2768 om = m; 2769 len += m->m_len; 2770 m = m->m_next; 2771 } 2772 } 2773 slp->ns_raw = m; 2774 slp->ns_cc -= len; 2775 slp->ns_reclen = 0; 2776 } else { 2777 return (0); 2778 } 2779 2780 /* 2781 * Accumulate the fragments into a record. 2782 */ 2783 mpp = &slp->ns_frag; 2784 while (*mpp) 2785 mpp = &((*mpp)->m_next); 2786 *mpp = recm; 2787 if (slp->ns_flag & SLP_LASTFRAG) { 2788 struct nfsrv_rec *rec; 2789 int mf = (waitflag & MB_DONTWAIT) ? M_NOWAIT : M_WAITOK; 2790 rec = kmalloc(sizeof(struct nfsrv_rec), M_NFSRVDESC, mf); 2791 if (!rec) { 2792 m_freem(slp->ns_frag); 2793 } else { 2794 nfs_realign(&slp->ns_frag, 10 * NFSX_UNSIGNED); 2795 rec->nr_address = NULL; 2796 rec->nr_packet = slp->ns_frag; 2797 STAILQ_INSERT_TAIL(&slp->ns_rec, rec, nr_link); 2798 ++slp->ns_numrec; 2799 slp->ns_flag |= SLP_DOREC; 2800 ++*countp; 2801 } 2802 slp->ns_frag = NULL; 2803 } 2804 } 2805 } 2806 2807 #ifdef INVARIANTS 2808 2809 /* 2810 * Sanity check our mbuf chain. 2811 */ 2812 static void 2813 nfs_checkpkt(struct mbuf *m, int len) 2814 { 2815 int xlen = 0; 2816 while (m) { 2817 xlen += m->m_len; 2818 m = m->m_next; 2819 } 2820 if (xlen != len) { 2821 panic("nfs_checkpkt: len mismatch %d/%d mbuf %p", 2822 xlen, len, m); 2823 } 2824 } 2825 2826 #else 2827 2828 static void 2829 nfs_checkpkt(struct mbuf *m __unused, int len __unused) 2830 { 2831 } 2832 2833 #endif 2834 2835 /* 2836 * Parse an RPC header. 2837 * 2838 * If the socket is invalid or no records are pending we return ENOBUFS. 2839 * The caller must deal with NEEDQ races. 2840 */ 2841 int 2842 nfsrv_dorec(struct nfssvc_sock *slp, struct nfsd *nfsd, 2843 struct nfsrv_descript **ndp) 2844 { 2845 struct nfsrv_rec *rec; 2846 struct mbuf *m; 2847 struct sockaddr *nam; 2848 struct nfsrv_descript *nd; 2849 int error; 2850 2851 *ndp = NULL; 2852 if ((slp->ns_flag & SLP_VALID) == 0 || !STAILQ_FIRST(&slp->ns_rec)) 2853 return (ENOBUFS); 2854 rec = STAILQ_FIRST(&slp->ns_rec); 2855 STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link); 2856 KKASSERT(slp->ns_numrec > 0); 2857 if (--slp->ns_numrec == 0) 2858 slp->ns_flag &= ~SLP_DOREC; 2859 nam = rec->nr_address; 2860 m = rec->nr_packet; 2861 kfree(rec, M_NFSRVDESC); 2862 nd = kmalloc(sizeof(struct nfsrv_descript), M_NFSRVDESC, M_WAITOK); 2863 nd->nd_md = nd->nd_mrep = m; 2864 nd->nd_nam2 = nam; 2865 nd->nd_dpos = mtod(m, caddr_t); 2866 error = nfs_getreq(nd, nfsd, TRUE); 2867 if (error) { 2868 if (nam) { 2869 kfree(nam, M_SONAME); 2870 } 2871 kfree((caddr_t)nd, M_NFSRVDESC); 2872 return (error); 2873 } 2874 *ndp = nd; 2875 nfsd->nfsd_nd = nd; 2876 return (0); 2877 } 2878 2879 /* 2880 * Try to assign service sockets to nfsd threads based on the number 2881 * of new rpc requests that have been queued on the service socket. 2882 * 2883 * If no nfsd's are available or additonal requests are pending, set the 2884 * NFSD_CHECKSLP flag so that one of the running nfsds will go look for 2885 * the work in the nfssvc_sock list when it is finished processing its 2886 * current work. This flag is only cleared when an nfsd can not find 2887 * any new work to perform. 2888 */ 2889 void 2890 nfsrv_wakenfsd(struct nfssvc_sock *slp, int nparallel) 2891 { 2892 struct nfsd *nd; 2893 2894 if ((slp->ns_flag & SLP_VALID) == 0) 2895 return; 2896 if (nparallel <= 1) 2897 nparallel = 1; 2898 TAILQ_FOREACH(nd, &nfsd_head, nfsd_chain) { 2899 if (nd->nfsd_flag & NFSD_WAITING) { 2900 nd->nfsd_flag &= ~NFSD_WAITING; 2901 if (nd->nfsd_slp) 2902 panic("nfsd wakeup"); 2903 nfsrv_slpref(slp); 2904 nd->nfsd_slp = slp; 2905 wakeup((caddr_t)nd); 2906 if (--nparallel == 0) 2907 break; 2908 } 2909 } 2910 2911 /* 2912 * If we couldn't assign slp then the NFSDs are all busy and 2913 * we set a flag indicating that there is pending work. 2914 */ 2915 if (nparallel) 2916 nfsd_head_flag |= NFSD_CHECKSLP; 2917 } 2918 #endif /* NFS_NOSERVER */ 2919