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 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 /* 1627 * Encrypt the timestamp in ecb mode using the 1628 * session key. 1629 */ 1630 #ifdef NFSKERB 1631 XXX 1632 #else 1633 ktvout.tv_sec = 0; 1634 ktvout.tv_usec = 0; 1635 #endif 1636 1637 *tl++ = rpc_auth_kerb; 1638 *tl++ = txdr_unsigned(3 * NFSX_UNSIGNED); 1639 *tl = ktvout.tv_sec; 1640 tl = nfsm_build(&info, 3 * NFSX_UNSIGNED); 1641 *tl++ = ktvout.tv_usec; 1642 *tl++ = txdr_unsigned(nuidp->nu_cr.cr_uid); 1643 } else { 1644 *tl++ = 0; 1645 *tl++ = 0; 1646 } 1647 } else { 1648 *tl++ = 0; 1649 *tl++ = 0; 1650 } 1651 switch (err) { 1652 case EPROGUNAVAIL: 1653 *tl = txdr_unsigned(RPC_PROGUNAVAIL); 1654 break; 1655 case EPROGMISMATCH: 1656 *tl = txdr_unsigned(RPC_PROGMISMATCH); 1657 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED); 1658 *tl++ = txdr_unsigned(2); 1659 *tl = txdr_unsigned(3); 1660 break; 1661 case EPROCUNAVAIL: 1662 *tl = txdr_unsigned(RPC_PROCUNAVAIL); 1663 break; 1664 case EBADRPC: 1665 *tl = txdr_unsigned(RPC_GARBAGE); 1666 break; 1667 default: 1668 *tl = 0; 1669 if (err != NFSERR_RETVOID) { 1670 tl = nfsm_build(&info, NFSX_UNSIGNED); 1671 if (err) 1672 *tl = txdr_unsigned(nfsrv_errmap(nd, err)); 1673 else 1674 *tl = 0; 1675 } 1676 break; 1677 }; 1678 } 1679 1680 if (mrq != NULL) 1681 *mrq = info.mreq; 1682 *mbp = info.mb; 1683 *bposp = info.bpos; 1684 if (err != 0 && err != NFSERR_RETVOID) 1685 nfsstats.srvrpc_errs++; 1686 return (0); 1687 } 1688 1689 1690 #endif /* NFS_NOSERVER */ 1691 1692 /* 1693 * Nfs timer routine. 1694 * 1695 * Scan the nfsreq list and retranmit any requests that have timed out 1696 * To avoid retransmission attempts on STREAM sockets (in the future) make 1697 * sure to set the r_retry field to 0 (implies nm_retry == 0). 1698 * 1699 * Requests with attached responses, terminated requests, and 1700 * locked requests are ignored. Locked requests will be picked up 1701 * in a later timer call. 1702 */ 1703 void 1704 nfs_timer_callout(void *arg /* never used */) 1705 { 1706 struct nfsmount *nmp; 1707 struct nfsreq *req; 1708 #ifndef NFS_NOSERVER 1709 struct nfssvc_sock *slp; 1710 u_quad_t cur_usec; 1711 #endif /* NFS_NOSERVER */ 1712 1713 lwkt_gettoken(&nfs_token); 1714 TAILQ_FOREACH(nmp, &nfs_mountq, nm_entry) { 1715 lwkt_gettoken(&nmp->nm_token); 1716 TAILQ_FOREACH(req, &nmp->nm_reqq, r_chain) { 1717 KKASSERT(nmp == req->r_nmp); 1718 if (req->r_mrep) 1719 continue; 1720 if (req->r_flags & (R_SOFTTERM | R_LOCKED)) 1721 continue; 1722 1723 /* 1724 * Handle timeout/retry. Be sure to process r_mrep 1725 * for async requests that completed while we had 1726 * the request locked or they will hang in the reqq 1727 * forever. 1728 */ 1729 req->r_flags |= R_LOCKED; 1730 if (nfs_sigintr(nmp, req, req->r_td)) { 1731 nfs_softterm(req, 1); 1732 req->r_flags &= ~R_LOCKED; 1733 } else { 1734 nfs_timer_req(req); 1735 if (req->r_flags & R_ASYNC) { 1736 if (req->r_mrep) 1737 nfs_hardterm(req, 1); 1738 req->r_flags &= ~R_LOCKED; 1739 nfssvc_iod_reader_wakeup(nmp); 1740 } else { 1741 req->r_flags &= ~R_LOCKED; 1742 } 1743 } 1744 if (req->r_flags & R_WANTED) { 1745 req->r_flags &= ~R_WANTED; 1746 wakeup(req); 1747 } 1748 } 1749 lwkt_reltoken(&nmp->nm_token); 1750 } 1751 #ifndef NFS_NOSERVER 1752 1753 /* 1754 * Scan the write gathering queues for writes that need to be 1755 * completed now. 1756 */ 1757 cur_usec = nfs_curusec(); 1758 1759 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) { 1760 /* XXX race against removal */ 1761 if (lwkt_trytoken(&slp->ns_token)) { 1762 if (slp->ns_tq.lh_first && 1763 (slp->ns_tq.lh_first->nd_time <= cur_usec)) { 1764 nfsrv_wakenfsd(slp, 1); 1765 } 1766 lwkt_reltoken(&slp->ns_token); 1767 } 1768 } 1769 #endif /* NFS_NOSERVER */ 1770 1771 callout_reset(&nfs_timer_handle, nfs_ticks, nfs_timer_callout, NULL); 1772 lwkt_reltoken(&nfs_token); 1773 } 1774 1775 static 1776 void 1777 nfs_timer_req(struct nfsreq *req) 1778 { 1779 struct thread *td = &thread0; /* XXX for creds, will break if sleep */ 1780 struct nfsmount *nmp = req->r_nmp; 1781 struct mbuf *m; 1782 struct socket *so; 1783 int timeo; 1784 int error; 1785 1786 /* 1787 * rtt ticks and timeout calculation. Return if the timeout 1788 * has not been reached yet, unless the packet is flagged 1789 * for an immediate send. 1790 * 1791 * The mean rtt doesn't help when we get random I/Os, we have 1792 * to multiply by fairly large numbers. 1793 */ 1794 if (req->r_rtt >= 0) { 1795 /* 1796 * Calculate the timeout to test against. 1797 */ 1798 req->r_rtt++; 1799 if (nmp->nm_flag & NFSMNT_DUMBTIMR) { 1800 timeo = nmp->nm_timeo << NFS_RTT_SCALE_BITS; 1801 } else if (req->r_flags & R_TIMING) { 1802 timeo = NFS_SRTT(req) + NFS_SDRTT(req); 1803 } else { 1804 timeo = nmp->nm_timeo << NFS_RTT_SCALE_BITS; 1805 } 1806 timeo *= multt[req->r_procnum]; 1807 /* timeo is still scaled by SCALE_BITS */ 1808 1809 #define NFSFS (NFS_RTT_SCALE * NFS_HZ) 1810 if (req->r_flags & R_TIMING) { 1811 static long last_time; 1812 if (nfs_showrtt && last_time != time_second) { 1813 kprintf("rpccmd %d NFS SRTT %d SDRTT %d " 1814 "timeo %d.%03d\n", 1815 proct[req->r_procnum], 1816 NFS_SRTT(req), NFS_SDRTT(req), 1817 timeo / NFSFS, 1818 timeo % NFSFS * 1000 / NFSFS); 1819 last_time = time_second; 1820 } 1821 } 1822 #undef NFSFS 1823 1824 /* 1825 * deal with nfs_timer jitter. 1826 */ 1827 timeo = (timeo >> NFS_RTT_SCALE_BITS) + 1; 1828 if (timeo < 2) 1829 timeo = 2; 1830 1831 if (nmp->nm_timeouts > 0) 1832 timeo *= nfs_backoff[nmp->nm_timeouts - 1]; 1833 if (timeo > NFS_MAXTIMEO) 1834 timeo = NFS_MAXTIMEO; 1835 if (req->r_rtt <= timeo) { 1836 if ((req->r_flags & R_NEEDSXMIT) == 0) 1837 return; 1838 } else if (nmp->nm_timeouts < 8) { 1839 nmp->nm_timeouts++; 1840 } 1841 } 1842 1843 /* 1844 * Check for server not responding 1845 */ 1846 if ((req->r_flags & R_TPRINTFMSG) == 0 && 1847 req->r_rexmit > nmp->nm_deadthresh) { 1848 nfs_msg(req->r_td, nmp->nm_mountp->mnt_stat.f_mntfromname, 1849 "not responding"); 1850 req->r_flags |= R_TPRINTFMSG; 1851 } 1852 if (req->r_rexmit >= req->r_retry) { /* too many */ 1853 nfsstats.rpctimeouts++; 1854 nfs_softterm(req, 1); 1855 return; 1856 } 1857 1858 /* 1859 * Generally disable retransmission on reliable sockets, 1860 * unless the request is flagged for immediate send. 1861 */ 1862 if (nmp->nm_sotype != SOCK_DGRAM) { 1863 if (++req->r_rexmit > NFS_MAXREXMIT) 1864 req->r_rexmit = NFS_MAXREXMIT; 1865 if ((req->r_flags & R_NEEDSXMIT) == 0) 1866 return; 1867 } 1868 1869 /* 1870 * Stop here if we do not have a socket! 1871 */ 1872 if ((so = nmp->nm_so) == NULL) 1873 return; 1874 1875 /* 1876 * If there is enough space and the window allows.. resend it. 1877 * 1878 * r_rtt is left intact in case we get an answer after the 1879 * retry that was a reply to the original packet. 1880 * 1881 * NOTE: so_pru_send() 1882 */ 1883 if (ssb_space(&so->so_snd) >= req->r_mreq->m_pkthdr.len && 1884 (req->r_flags & (R_SENT | R_NEEDSXMIT)) && 1885 (m = m_copym(req->r_mreq, 0, M_COPYALL, MB_DONTWAIT))){ 1886 if ((nmp->nm_flag & NFSMNT_NOCONN) == 0) 1887 error = so_pru_send(so, 0, m, NULL, NULL, td); 1888 else 1889 error = so_pru_send(so, 0, m, nmp->nm_nam, NULL, td); 1890 if (error) { 1891 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) 1892 so->so_error = 0; 1893 req->r_flags |= R_NEEDSXMIT; 1894 } else if (req->r_mrep == NULL) { 1895 /* 1896 * Iff first send, start timing 1897 * else turn timing off, backoff timer 1898 * and divide congestion window by 2. 1899 * 1900 * It is possible for the so_pru_send() to 1901 * block and for us to race a reply so we 1902 * only do this if the reply field has not 1903 * been filled in. R_LOCKED will prevent 1904 * the request from being ripped out from under 1905 * us entirely. 1906 * 1907 * Record the last resent procnum to aid us 1908 * in duplicate detection on receive. 1909 */ 1910 if ((req->r_flags & R_NEEDSXMIT) == 0) { 1911 if (nfs_showrexmit) 1912 kprintf("X"); 1913 if (++req->r_rexmit > NFS_MAXREXMIT) 1914 req->r_rexmit = NFS_MAXREXMIT; 1915 nmp->nm_maxasync_scaled >>= 1; 1916 if (nmp->nm_maxasync_scaled < NFS_MINASYNC_SCALED) 1917 nmp->nm_maxasync_scaled = NFS_MINASYNC_SCALED; 1918 nfsstats.rpcretries++; 1919 nmp->nm_lastreprocnum = req->r_procnum; 1920 } else { 1921 req->r_flags |= R_SENT; 1922 req->r_flags &= ~R_NEEDSXMIT; 1923 } 1924 } 1925 } 1926 } 1927 1928 /* 1929 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and 1930 * wait for all requests to complete. This is used by forced unmounts 1931 * to terminate any outstanding RPCs. 1932 * 1933 * Locked requests cannot be canceled but will be marked for 1934 * soft-termination. 1935 */ 1936 int 1937 nfs_nmcancelreqs(struct nfsmount *nmp) 1938 { 1939 struct nfsreq *req; 1940 int i; 1941 1942 crit_enter(); 1943 TAILQ_FOREACH(req, &nmp->nm_reqq, r_chain) { 1944 if (req->r_mrep != NULL || (req->r_flags & R_SOFTTERM)) 1945 continue; 1946 nfs_softterm(req, 0); 1947 } 1948 /* XXX the other two queues as well */ 1949 crit_exit(); 1950 1951 for (i = 0; i < 30; i++) { 1952 crit_enter(); 1953 TAILQ_FOREACH(req, &nmp->nm_reqq, r_chain) { 1954 if (nmp == req->r_nmp) 1955 break; 1956 } 1957 crit_exit(); 1958 if (req == NULL) 1959 return (0); 1960 tsleep(&lbolt, 0, "nfscancel", 0); 1961 } 1962 return (EBUSY); 1963 } 1964 1965 /* 1966 * Soft-terminate a request, effectively marking it as failed. 1967 * 1968 * Must be called from within a critical section. 1969 */ 1970 static void 1971 nfs_softterm(struct nfsreq *rep, int islocked) 1972 { 1973 rep->r_flags |= R_SOFTTERM; 1974 nfs_hardterm(rep, islocked); 1975 } 1976 1977 /* 1978 * Hard-terminate a request, typically after getting a response. 1979 * 1980 * The state machine can still decide to re-issue it later if necessary. 1981 * 1982 * Must be called from within a critical section. 1983 */ 1984 static void 1985 nfs_hardterm(struct nfsreq *rep, int islocked) 1986 { 1987 struct nfsmount *nmp = rep->r_nmp; 1988 1989 /* 1990 * The nm_send count is decremented now to avoid deadlocks 1991 * when the process in soreceive() hasn't yet managed to send 1992 * its own request. 1993 */ 1994 if (rep->r_flags & R_SENT) { 1995 rep->r_flags &= ~R_SENT; 1996 } 1997 1998 /* 1999 * If we locked the request or nobody else has locked the request, 2000 * and the request is async, we can move it to the reader thread's 2001 * queue now and fix up the state. 2002 * 2003 * If we locked the request or nobody else has locked the request, 2004 * we can wake up anyone blocked waiting for a response on the 2005 * request. 2006 */ 2007 if (islocked || (rep->r_flags & R_LOCKED) == 0) { 2008 if ((rep->r_flags & (R_ONREQQ | R_ASYNC)) == 2009 (R_ONREQQ | R_ASYNC)) { 2010 rep->r_flags &= ~R_ONREQQ; 2011 TAILQ_REMOVE(&nmp->nm_reqq, rep, r_chain); 2012 --nmp->nm_reqqlen; 2013 TAILQ_INSERT_TAIL(&nmp->nm_reqrxq, rep, r_chain); 2014 KKASSERT(rep->r_info->state == NFSM_STATE_TRY || 2015 rep->r_info->state == NFSM_STATE_WAITREPLY); 2016 rep->r_info->state = NFSM_STATE_PROCESSREPLY; 2017 nfssvc_iod_reader_wakeup(nmp); 2018 if (TAILQ_FIRST(&nmp->nm_bioq) && 2019 nmp->nm_reqqlen <= nfs_maxasyncbio * 2 / 3) { 2020 nfssvc_iod_writer_wakeup(nmp); 2021 } 2022 } 2023 mtx_abort_ex_link(&nmp->nm_rxlock, &rep->r_link); 2024 } 2025 } 2026 2027 /* 2028 * Test for a termination condition pending on the process. 2029 * This is used for NFSMNT_INT mounts. 2030 */ 2031 int 2032 nfs_sigintr(struct nfsmount *nmp, struct nfsreq *rep, struct thread *td) 2033 { 2034 sigset_t tmpset; 2035 struct proc *p; 2036 struct lwp *lp; 2037 2038 if (rep && (rep->r_flags & R_SOFTTERM)) 2039 return (EINTR); 2040 /* Terminate all requests while attempting a forced unmount. */ 2041 if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF) 2042 return (EINTR); 2043 if (!(nmp->nm_flag & NFSMNT_INT)) 2044 return (0); 2045 /* td might be NULL YYY */ 2046 if (td == NULL || (p = td->td_proc) == NULL) 2047 return (0); 2048 2049 lp = td->td_lwp; 2050 tmpset = lwp_sigpend(lp); 2051 SIGSETNAND(tmpset, lp->lwp_sigmask); 2052 SIGSETNAND(tmpset, p->p_sigignore); 2053 if (SIGNOTEMPTY(tmpset) && NFSINT_SIGMASK(tmpset)) 2054 return (EINTR); 2055 2056 return (0); 2057 } 2058 2059 /* 2060 * Lock a socket against others. 2061 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply 2062 * and also to avoid race conditions between the processes with nfs requests 2063 * in progress when a reconnect is necessary. 2064 */ 2065 int 2066 nfs_sndlock(struct nfsmount *nmp, struct nfsreq *rep) 2067 { 2068 mtx_t mtx = &nmp->nm_txlock; 2069 struct thread *td; 2070 int slptimeo; 2071 int slpflag; 2072 int error; 2073 2074 slpflag = 0; 2075 slptimeo = 0; 2076 td = rep ? rep->r_td : NULL; 2077 if (nmp->nm_flag & NFSMNT_INT) 2078 slpflag = PCATCH; 2079 2080 while ((error = mtx_lock_ex_try(mtx)) != 0) { 2081 if (nfs_sigintr(nmp, rep, td)) { 2082 error = EINTR; 2083 break; 2084 } 2085 error = mtx_lock_ex(mtx, "nfsndlck", slpflag, slptimeo); 2086 if (error == 0) 2087 break; 2088 if (slpflag == PCATCH) { 2089 slpflag = 0; 2090 slptimeo = 2 * hz; 2091 } 2092 } 2093 /* Always fail if our request has been cancelled. */ 2094 if (rep && (rep->r_flags & R_SOFTTERM)) { 2095 if (error == 0) 2096 mtx_unlock(mtx); 2097 error = EINTR; 2098 } 2099 return (error); 2100 } 2101 2102 /* 2103 * Unlock the stream socket for others. 2104 */ 2105 void 2106 nfs_sndunlock(struct nfsmount *nmp) 2107 { 2108 mtx_unlock(&nmp->nm_txlock); 2109 } 2110 2111 /* 2112 * Lock the receiver side of the socket. 2113 * 2114 * rep may be NULL. 2115 */ 2116 static int 2117 nfs_rcvlock(struct nfsmount *nmp, struct nfsreq *rep) 2118 { 2119 mtx_t mtx = &nmp->nm_rxlock; 2120 int slpflag; 2121 int slptimeo; 2122 int error; 2123 2124 /* 2125 * Unconditionally check for completion in case another nfsiod 2126 * get the packet while the caller was blocked, before the caller 2127 * called us. Packet reception is handled by mainline code which 2128 * is protected by the BGL at the moment. 2129 * 2130 * We do not strictly need the second check just before the 2131 * tsleep(), but it's good defensive programming. 2132 */ 2133 if (rep && rep->r_mrep != NULL) 2134 return (EALREADY); 2135 2136 if (nmp->nm_flag & NFSMNT_INT) 2137 slpflag = PCATCH; 2138 else 2139 slpflag = 0; 2140 slptimeo = 0; 2141 2142 while ((error = mtx_lock_ex_try(mtx)) != 0) { 2143 if (nfs_sigintr(nmp, rep, (rep ? rep->r_td : NULL))) { 2144 error = EINTR; 2145 break; 2146 } 2147 if (rep && rep->r_mrep != NULL) { 2148 error = EALREADY; 2149 break; 2150 } 2151 2152 /* 2153 * NOTE: can return ENOLCK, but in that case rep->r_mrep 2154 * will already be set. 2155 */ 2156 if (rep) { 2157 error = mtx_lock_ex_link(mtx, &rep->r_link, 2158 "nfsrcvlk", 2159 slpflag, slptimeo); 2160 } else { 2161 error = mtx_lock_ex(mtx, "nfsrcvlk", slpflag, slptimeo); 2162 } 2163 if (error == 0) 2164 break; 2165 2166 /* 2167 * If our reply was recieved while we were sleeping, 2168 * then just return without taking the lock to avoid a 2169 * situation where a single iod could 'capture' the 2170 * recieve lock. 2171 */ 2172 if (rep && rep->r_mrep != NULL) { 2173 error = EALREADY; 2174 break; 2175 } 2176 if (slpflag == PCATCH) { 2177 slpflag = 0; 2178 slptimeo = 2 * hz; 2179 } 2180 } 2181 if (error == 0) { 2182 if (rep && rep->r_mrep != NULL) { 2183 error = EALREADY; 2184 mtx_unlock(mtx); 2185 } 2186 } 2187 return (error); 2188 } 2189 2190 /* 2191 * Unlock the stream socket for others. 2192 */ 2193 static void 2194 nfs_rcvunlock(struct nfsmount *nmp) 2195 { 2196 mtx_unlock(&nmp->nm_rxlock); 2197 } 2198 2199 /* 2200 * nfs_realign: 2201 * 2202 * Check for badly aligned mbuf data and realign by copying the unaligned 2203 * portion of the data into a new mbuf chain and freeing the portions 2204 * of the old chain that were replaced. 2205 * 2206 * We cannot simply realign the data within the existing mbuf chain 2207 * because the underlying buffers may contain other rpc commands and 2208 * we cannot afford to overwrite them. 2209 * 2210 * We would prefer to avoid this situation entirely. The situation does 2211 * not occur with NFS/UDP and is supposed to only occassionally occur 2212 * with TCP. Use vfs.nfs.realign_count and realign_test to check this. 2213 * 2214 * NOTE! MB_DONTWAIT cannot be used here. The mbufs must be acquired 2215 * because the rpc request OR reply cannot be thrown away. TCP NFS 2216 * mounts do not retry their RPCs unless the TCP connection itself 2217 * is dropped so throwing away a RPC will basically cause the NFS 2218 * operation to lockup indefinitely. 2219 */ 2220 static void 2221 nfs_realign(struct mbuf **pm, int hsiz) 2222 { 2223 struct mbuf *m; 2224 struct mbuf *n = NULL; 2225 2226 /* 2227 * Check for misalignemnt 2228 */ 2229 ++nfs_realign_test; 2230 while ((m = *pm) != NULL) { 2231 if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) 2232 break; 2233 pm = &m->m_next; 2234 } 2235 2236 /* 2237 * If misalignment found make a completely new copy. 2238 */ 2239 if (m) { 2240 ++nfs_realign_count; 2241 n = m_dup_data(m, MB_WAIT); 2242 m_freem(*pm); 2243 *pm = n; 2244 } 2245 } 2246 2247 #ifndef NFS_NOSERVER 2248 2249 /* 2250 * Parse an RPC request 2251 * - verify it 2252 * - fill in the cred struct. 2253 */ 2254 int 2255 nfs_getreq(struct nfsrv_descript *nd, struct nfsd *nfsd, int has_header) 2256 { 2257 int len, i; 2258 u_int32_t *tl; 2259 struct uio uio; 2260 struct iovec iov; 2261 caddr_t cp; 2262 u_int32_t nfsvers, auth_type; 2263 uid_t nickuid; 2264 int error = 0, ticklen; 2265 struct nfsuid *nuidp; 2266 struct timeval tvin, tvout; 2267 struct nfsm_info info; 2268 #if 0 /* until encrypted keys are implemented */ 2269 NFSKERBKEYSCHED_T keys; /* stores key schedule */ 2270 #endif 2271 2272 info.mrep = nd->nd_mrep; 2273 info.md = nd->nd_md; 2274 info.dpos = nd->nd_dpos; 2275 2276 if (has_header) { 2277 NULLOUT(tl = nfsm_dissect(&info, 10 * NFSX_UNSIGNED)); 2278 nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++); 2279 if (*tl++ != rpc_call) { 2280 m_freem(info.mrep); 2281 return (EBADRPC); 2282 } 2283 } else { 2284 NULLOUT(tl = nfsm_dissect(&info, 8 * NFSX_UNSIGNED)); 2285 } 2286 nd->nd_repstat = 0; 2287 nd->nd_flag = 0; 2288 if (*tl++ != rpc_vers) { 2289 nd->nd_repstat = ERPCMISMATCH; 2290 nd->nd_procnum = NFSPROC_NOOP; 2291 return (0); 2292 } 2293 if (*tl != nfs_prog) { 2294 nd->nd_repstat = EPROGUNAVAIL; 2295 nd->nd_procnum = NFSPROC_NOOP; 2296 return (0); 2297 } 2298 tl++; 2299 nfsvers = fxdr_unsigned(u_int32_t, *tl++); 2300 if (nfsvers < NFS_VER2 || nfsvers > NFS_VER3) { 2301 nd->nd_repstat = EPROGMISMATCH; 2302 nd->nd_procnum = NFSPROC_NOOP; 2303 return (0); 2304 } 2305 if (nfsvers == NFS_VER3) 2306 nd->nd_flag = ND_NFSV3; 2307 nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++); 2308 if (nd->nd_procnum == NFSPROC_NULL) 2309 return (0); 2310 if (nd->nd_procnum >= NFS_NPROCS || 2311 (nd->nd_procnum >= NQNFSPROC_GETLEASE) || 2312 (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) { 2313 nd->nd_repstat = EPROCUNAVAIL; 2314 nd->nd_procnum = NFSPROC_NOOP; 2315 return (0); 2316 } 2317 if ((nd->nd_flag & ND_NFSV3) == 0) 2318 nd->nd_procnum = nfsv3_procid[nd->nd_procnum]; 2319 auth_type = *tl++; 2320 len = fxdr_unsigned(int, *tl++); 2321 if (len < 0 || len > RPCAUTH_MAXSIZ) { 2322 m_freem(info.mrep); 2323 return (EBADRPC); 2324 } 2325 2326 nd->nd_flag &= ~ND_KERBAUTH; 2327 /* 2328 * Handle auth_unix or auth_kerb. 2329 */ 2330 if (auth_type == rpc_auth_unix) { 2331 len = fxdr_unsigned(int, *++tl); 2332 if (len < 0 || len > NFS_MAXNAMLEN) { 2333 m_freem(info.mrep); 2334 return (EBADRPC); 2335 } 2336 ERROROUT(nfsm_adv(&info, nfsm_rndup(len))); 2337 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED)); 2338 bzero((caddr_t)&nd->nd_cr, sizeof (struct ucred)); 2339 nd->nd_cr.cr_ref = 1; 2340 nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++); 2341 nd->nd_cr.cr_ruid = nd->nd_cr.cr_svuid = nd->nd_cr.cr_uid; 2342 nd->nd_cr.cr_gid = fxdr_unsigned(gid_t, *tl++); 2343 nd->nd_cr.cr_rgid = nd->nd_cr.cr_svgid = nd->nd_cr.cr_gid; 2344 len = fxdr_unsigned(int, *tl); 2345 if (len < 0 || len > RPCAUTH_UNIXGIDS) { 2346 m_freem(info.mrep); 2347 return (EBADRPC); 2348 } 2349 NULLOUT(tl = nfsm_dissect(&info, (len + 2) * NFSX_UNSIGNED)); 2350 for (i = 1; i <= len; i++) 2351 if (i < NGROUPS) 2352 nd->nd_cr.cr_groups[i] = fxdr_unsigned(gid_t, *tl++); 2353 else 2354 tl++; 2355 nd->nd_cr.cr_ngroups = (len >= NGROUPS) ? NGROUPS : (len + 1); 2356 if (nd->nd_cr.cr_ngroups > 1) 2357 nfsrvw_sort(nd->nd_cr.cr_groups, nd->nd_cr.cr_ngroups); 2358 len = fxdr_unsigned(int, *++tl); 2359 if (len < 0 || len > RPCAUTH_MAXSIZ) { 2360 m_freem(info.mrep); 2361 return (EBADRPC); 2362 } 2363 if (len > 0) { 2364 ERROROUT(nfsm_adv(&info, nfsm_rndup(len))); 2365 } 2366 } else if (auth_type == rpc_auth_kerb) { 2367 switch (fxdr_unsigned(int, *tl++)) { 2368 case RPCAKN_FULLNAME: 2369 ticklen = fxdr_unsigned(int, *tl); 2370 *((u_int32_t *)nfsd->nfsd_authstr) = *tl; 2371 uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED; 2372 nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED; 2373 if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) { 2374 m_freem(info.mrep); 2375 return (EBADRPC); 2376 } 2377 uio.uio_offset = 0; 2378 uio.uio_iov = &iov; 2379 uio.uio_iovcnt = 1; 2380 uio.uio_segflg = UIO_SYSSPACE; 2381 iov.iov_base = (caddr_t)&nfsd->nfsd_authstr[4]; 2382 iov.iov_len = RPCAUTH_MAXSIZ - 4; 2383 ERROROUT(nfsm_mtouio(&info, &uio, uio.uio_resid)); 2384 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED)); 2385 if (*tl++ != rpc_auth_kerb || 2386 fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) { 2387 kprintf("Bad kerb verifier\n"); 2388 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF); 2389 nd->nd_procnum = NFSPROC_NOOP; 2390 return (0); 2391 } 2392 NULLOUT(cp = nfsm_dissect(&info, 4 * NFSX_UNSIGNED)); 2393 tl = (u_int32_t *)cp; 2394 if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) { 2395 kprintf("Not fullname kerb verifier\n"); 2396 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF); 2397 nd->nd_procnum = NFSPROC_NOOP; 2398 return (0); 2399 } 2400 cp += NFSX_UNSIGNED; 2401 bcopy(cp, nfsd->nfsd_verfstr, 3 * NFSX_UNSIGNED); 2402 nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED; 2403 nd->nd_flag |= ND_KERBFULL; 2404 nfsd->nfsd_flag |= NFSD_NEEDAUTH; 2405 break; 2406 case RPCAKN_NICKNAME: 2407 if (len != 2 * NFSX_UNSIGNED) { 2408 kprintf("Kerb nickname short\n"); 2409 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED); 2410 nd->nd_procnum = NFSPROC_NOOP; 2411 return (0); 2412 } 2413 nickuid = fxdr_unsigned(uid_t, *tl); 2414 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED)); 2415 if (*tl++ != rpc_auth_kerb || 2416 fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) { 2417 kprintf("Kerb nick verifier bad\n"); 2418 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF); 2419 nd->nd_procnum = NFSPROC_NOOP; 2420 return (0); 2421 } 2422 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED)); 2423 tvin.tv_sec = *tl++; 2424 tvin.tv_usec = *tl; 2425 2426 for (nuidp = NUIDHASH(nfsd->nfsd_slp,nickuid)->lh_first; 2427 nuidp != NULL; nuidp = nuidp->nu_hash.le_next) { 2428 if (nuidp->nu_cr.cr_uid == nickuid && 2429 (!nd->nd_nam2 || 2430 netaddr_match(NU_NETFAM(nuidp), 2431 &nuidp->nu_haddr, nd->nd_nam2))) 2432 break; 2433 } 2434 if (!nuidp) { 2435 nd->nd_repstat = 2436 (NFSERR_AUTHERR|AUTH_REJECTCRED); 2437 nd->nd_procnum = NFSPROC_NOOP; 2438 return (0); 2439 } 2440 2441 /* 2442 * Now, decrypt the timestamp using the session key 2443 * and validate it. 2444 */ 2445 #ifdef NFSKERB 2446 XXX 2447 #else 2448 tvout.tv_sec = 0; 2449 tvout.tv_usec = 0; 2450 #endif 2451 2452 tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec); 2453 tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec); 2454 if (nuidp->nu_expire < time_second || 2455 nuidp->nu_timestamp.tv_sec > tvout.tv_sec || 2456 (nuidp->nu_timestamp.tv_sec == tvout.tv_sec && 2457 nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) { 2458 nuidp->nu_expire = 0; 2459 nd->nd_repstat = 2460 (NFSERR_AUTHERR|AUTH_REJECTVERF); 2461 nd->nd_procnum = NFSPROC_NOOP; 2462 return (0); 2463 } 2464 nfsrv_setcred(&nuidp->nu_cr, &nd->nd_cr); 2465 nd->nd_flag |= ND_KERBNICK; 2466 break; 2467 } 2468 } else { 2469 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED); 2470 nd->nd_procnum = NFSPROC_NOOP; 2471 return (0); 2472 } 2473 2474 nd->nd_md = info.md; 2475 nd->nd_dpos = info.dpos; 2476 return (0); 2477 nfsmout: 2478 return (error); 2479 } 2480 2481 #endif 2482 2483 /* 2484 * Send a message to the originating process's terminal. The thread and/or 2485 * process may be NULL. YYY the thread should not be NULL but there may 2486 * still be some uio_td's that are still being passed as NULL through to 2487 * nfsm_request(). 2488 */ 2489 static int 2490 nfs_msg(struct thread *td, char *server, char *msg) 2491 { 2492 tpr_t tpr; 2493 2494 if (td && td->td_proc) 2495 tpr = tprintf_open(td->td_proc); 2496 else 2497 tpr = NULL; 2498 tprintf(tpr, "nfs server %s: %s\n", server, msg); 2499 tprintf_close(tpr); 2500 return (0); 2501 } 2502 2503 #ifndef NFS_NOSERVER 2504 2505 /* 2506 * Socket upcall routine for nfsd sockets. This runs in the protocol 2507 * thread and passes waitflag == MB_DONTWAIT. 2508 */ 2509 void 2510 nfsrv_rcv_upcall(struct socket *so, void *arg, int waitflag) 2511 { 2512 struct nfssvc_sock *slp = (struct nfssvc_sock *)arg; 2513 2514 if (slp->ns_needq_upcall == 0) { 2515 slp->ns_needq_upcall = 1; /* ok to race */ 2516 lwkt_gettoken(&nfs_token); 2517 nfsrv_wakenfsd(slp, 1); 2518 lwkt_reltoken(&nfs_token); 2519 } 2520 #if 0 2521 lwkt_gettoken(&slp->ns_token); 2522 slp->ns_flag |= SLP_NEEDQ; 2523 nfsrv_rcv(so, arg, waitflag); 2524 lwkt_reltoken(&slp->ns_token); 2525 #endif 2526 } 2527 2528 /* 2529 * Process new data on a receive socket. Essentially do as much as we can 2530 * non-blocking, else punt and it will be called with MB_WAIT from an nfsd. 2531 * 2532 * slp->ns_token is held on call 2533 */ 2534 void 2535 nfsrv_rcv(struct socket *so, void *arg, int waitflag) 2536 { 2537 struct nfssvc_sock *slp = (struct nfssvc_sock *)arg; 2538 struct mbuf *m; 2539 struct sockaddr *nam; 2540 struct sockbuf sio; 2541 int flags, error; 2542 int nparallel_wakeup = 0; 2543 2544 ASSERT_LWKT_TOKEN_HELD(&slp->ns_token); 2545 2546 if ((slp->ns_flag & SLP_VALID) == 0) 2547 return; 2548 2549 /* 2550 * Do not allow an infinite number of completed RPC records to build 2551 * up before we stop reading data from the socket. Otherwise we could 2552 * end up holding onto an unreasonable number of mbufs for requests 2553 * waiting for service. 2554 * 2555 * This should give pretty good feedback to the TCP layer and 2556 * prevents a memory crunch for other protocols. 2557 * 2558 * Note that the same service socket can be dispatched to several 2559 * nfs servers simultaniously. The tcp protocol callback calls us 2560 * with MB_DONTWAIT. nfsd calls us with MB_WAIT (typically). 2561 */ 2562 if (NFSRV_RECLIMIT(slp)) 2563 return; 2564 2565 /* 2566 * Handle protocol specifics to parse an RPC request. We always 2567 * pull from the socket using non-blocking I/O. 2568 */ 2569 if (so->so_type == SOCK_STREAM) { 2570 /* 2571 * The data has to be read in an orderly fashion from a TCP 2572 * stream, unlike a UDP socket. It is possible for soreceive 2573 * and/or nfsrv_getstream() to block, so make sure only one 2574 * entity is messing around with the TCP stream at any given 2575 * moment. The receive sockbuf's lock in soreceive is not 2576 * sufficient. 2577 */ 2578 if (slp->ns_flag & SLP_GETSTREAM) 2579 return; 2580 slp->ns_flag |= SLP_GETSTREAM; 2581 2582 /* 2583 * Do soreceive(). Pull out as much data as possible without 2584 * blocking. 2585 */ 2586 sbinit(&sio, 1000000000); 2587 flags = MSG_DONTWAIT; 2588 error = so_pru_soreceive(so, &nam, NULL, &sio, NULL, &flags); 2589 if (error || sio.sb_mb == NULL) { 2590 if (error != EWOULDBLOCK) 2591 slp->ns_flag |= SLP_DISCONN; 2592 slp->ns_flag &= ~(SLP_GETSTREAM | SLP_NEEDQ); 2593 goto done; 2594 } 2595 m = sio.sb_mb; 2596 if (slp->ns_rawend) { 2597 slp->ns_rawend->m_next = m; 2598 slp->ns_cc += sio.sb_cc; 2599 } else { 2600 slp->ns_raw = m; 2601 slp->ns_cc = sio.sb_cc; 2602 } 2603 while (m->m_next) 2604 m = m->m_next; 2605 slp->ns_rawend = m; 2606 2607 /* 2608 * Now try and parse as many record(s) as we can out of the 2609 * raw stream data. This will set SLP_DOREC. 2610 */ 2611 error = nfsrv_getstream(slp, waitflag, &nparallel_wakeup); 2612 if (error && error != EWOULDBLOCK) 2613 slp->ns_flag |= SLP_DISCONN; 2614 slp->ns_flag &= ~SLP_GETSTREAM; 2615 } else { 2616 /* 2617 * For UDP soreceive typically pulls just one packet, loop 2618 * to get the whole batch. 2619 */ 2620 do { 2621 sbinit(&sio, 1000000000); 2622 flags = MSG_DONTWAIT; 2623 error = so_pru_soreceive(so, &nam, NULL, &sio, 2624 NULL, &flags); 2625 if (sio.sb_mb) { 2626 struct nfsrv_rec *rec; 2627 int mf = (waitflag & MB_DONTWAIT) ? 2628 M_NOWAIT : M_WAITOK; 2629 rec = kmalloc(sizeof(struct nfsrv_rec), 2630 M_NFSRVDESC, mf); 2631 if (!rec) { 2632 if (nam) 2633 kfree(nam, M_SONAME); 2634 m_freem(sio.sb_mb); 2635 continue; 2636 } 2637 nfs_realign(&sio.sb_mb, 10 * NFSX_UNSIGNED); 2638 rec->nr_address = nam; 2639 rec->nr_packet = sio.sb_mb; 2640 STAILQ_INSERT_TAIL(&slp->ns_rec, rec, nr_link); 2641 ++slp->ns_numrec; 2642 slp->ns_flag |= SLP_DOREC; 2643 ++nparallel_wakeup; 2644 } else { 2645 slp->ns_flag &= ~SLP_NEEDQ; 2646 } 2647 if (error) { 2648 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) 2649 && error != EWOULDBLOCK) { 2650 slp->ns_flag |= SLP_DISCONN; 2651 break; 2652 } 2653 } 2654 if (NFSRV_RECLIMIT(slp)) 2655 break; 2656 } while (sio.sb_mb); 2657 } 2658 2659 /* 2660 * If we were upcalled from the tcp protocol layer and we have 2661 * fully parsed records ready to go, or there is new data pending, 2662 * or something went wrong, try to wake up a nfsd thread to deal 2663 * with it. 2664 */ 2665 done: 2666 /* XXX this code is currently not executed (nfsrv_rcv_upcall) */ 2667 if (waitflag == MB_DONTWAIT && (slp->ns_flag & SLP_ACTION_MASK)) { 2668 lwkt_gettoken(&nfs_token); 2669 nfsrv_wakenfsd(slp, nparallel_wakeup); 2670 lwkt_reltoken(&nfs_token); 2671 } 2672 } 2673 2674 /* 2675 * Try and extract an RPC request from the mbuf data list received on a 2676 * stream socket. The "waitflag" argument indicates whether or not it 2677 * can sleep. 2678 */ 2679 static int 2680 nfsrv_getstream(struct nfssvc_sock *slp, int waitflag, int *countp) 2681 { 2682 struct mbuf *m, **mpp; 2683 char *cp1, *cp2; 2684 int len; 2685 struct mbuf *om, *m2, *recm; 2686 u_int32_t recmark; 2687 2688 for (;;) { 2689 if (slp->ns_reclen == 0) { 2690 if (slp->ns_cc < NFSX_UNSIGNED) 2691 return (0); 2692 m = slp->ns_raw; 2693 if (m->m_len >= NFSX_UNSIGNED) { 2694 bcopy(mtod(m, caddr_t), (caddr_t)&recmark, NFSX_UNSIGNED); 2695 m->m_data += NFSX_UNSIGNED; 2696 m->m_len -= NFSX_UNSIGNED; 2697 } else { 2698 cp1 = (caddr_t)&recmark; 2699 cp2 = mtod(m, caddr_t); 2700 while (cp1 < ((caddr_t)&recmark) + NFSX_UNSIGNED) { 2701 while (m->m_len == 0) { 2702 m = m->m_next; 2703 cp2 = mtod(m, caddr_t); 2704 } 2705 *cp1++ = *cp2++; 2706 m->m_data++; 2707 m->m_len--; 2708 } 2709 } 2710 slp->ns_cc -= NFSX_UNSIGNED; 2711 recmark = ntohl(recmark); 2712 slp->ns_reclen = recmark & ~0x80000000; 2713 if (recmark & 0x80000000) 2714 slp->ns_flag |= SLP_LASTFRAG; 2715 else 2716 slp->ns_flag &= ~SLP_LASTFRAG; 2717 if (slp->ns_reclen > NFS_MAXPACKET || slp->ns_reclen <= 0) { 2718 log(LOG_ERR, "%s (%d) from nfs client\n", 2719 "impossible packet length", 2720 slp->ns_reclen); 2721 return (EPERM); 2722 } 2723 } 2724 2725 /* 2726 * Now get the record part. 2727 * 2728 * Note that slp->ns_reclen may be 0. Linux sometimes 2729 * generates 0-length RPCs 2730 */ 2731 recm = NULL; 2732 if (slp->ns_cc == slp->ns_reclen) { 2733 recm = slp->ns_raw; 2734 slp->ns_raw = slp->ns_rawend = NULL; 2735 slp->ns_cc = slp->ns_reclen = 0; 2736 } else if (slp->ns_cc > slp->ns_reclen) { 2737 len = 0; 2738 m = slp->ns_raw; 2739 om = NULL; 2740 2741 while (len < slp->ns_reclen) { 2742 if ((len + m->m_len) > slp->ns_reclen) { 2743 m2 = m_copym(m, 0, slp->ns_reclen - len, 2744 waitflag); 2745 if (m2) { 2746 if (om) { 2747 om->m_next = m2; 2748 recm = slp->ns_raw; 2749 } else 2750 recm = m2; 2751 m->m_data += slp->ns_reclen - len; 2752 m->m_len -= slp->ns_reclen - len; 2753 len = slp->ns_reclen; 2754 } else { 2755 return (EWOULDBLOCK); 2756 } 2757 } else if ((len + m->m_len) == slp->ns_reclen) { 2758 om = m; 2759 len += m->m_len; 2760 m = m->m_next; 2761 recm = slp->ns_raw; 2762 om->m_next = NULL; 2763 } else { 2764 om = m; 2765 len += m->m_len; 2766 m = m->m_next; 2767 } 2768 } 2769 slp->ns_raw = m; 2770 slp->ns_cc -= len; 2771 slp->ns_reclen = 0; 2772 } else { 2773 return (0); 2774 } 2775 2776 /* 2777 * Accumulate the fragments into a record. 2778 */ 2779 mpp = &slp->ns_frag; 2780 while (*mpp) 2781 mpp = &((*mpp)->m_next); 2782 *mpp = recm; 2783 if (slp->ns_flag & SLP_LASTFRAG) { 2784 struct nfsrv_rec *rec; 2785 int mf = (waitflag & MB_DONTWAIT) ? M_NOWAIT : M_WAITOK; 2786 rec = kmalloc(sizeof(struct nfsrv_rec), M_NFSRVDESC, mf); 2787 if (!rec) { 2788 m_freem(slp->ns_frag); 2789 } else { 2790 nfs_realign(&slp->ns_frag, 10 * NFSX_UNSIGNED); 2791 rec->nr_address = NULL; 2792 rec->nr_packet = slp->ns_frag; 2793 STAILQ_INSERT_TAIL(&slp->ns_rec, rec, nr_link); 2794 ++slp->ns_numrec; 2795 slp->ns_flag |= SLP_DOREC; 2796 ++*countp; 2797 } 2798 slp->ns_frag = NULL; 2799 } 2800 } 2801 } 2802 2803 #ifdef INVARIANTS 2804 2805 /* 2806 * Sanity check our mbuf chain. 2807 */ 2808 static void 2809 nfs_checkpkt(struct mbuf *m, int len) 2810 { 2811 int xlen = 0; 2812 while (m) { 2813 xlen += m->m_len; 2814 m = m->m_next; 2815 } 2816 if (xlen != len) { 2817 panic("nfs_checkpkt: len mismatch %d/%d mbuf %p", 2818 xlen, len, m); 2819 } 2820 } 2821 2822 #else 2823 2824 static void 2825 nfs_checkpkt(struct mbuf *m __unused, int len __unused) 2826 { 2827 } 2828 2829 #endif 2830 2831 /* 2832 * Parse an RPC header. 2833 * 2834 * If the socket is invalid or no records are pending we return ENOBUFS. 2835 * The caller must deal with NEEDQ races. 2836 */ 2837 int 2838 nfsrv_dorec(struct nfssvc_sock *slp, struct nfsd *nfsd, 2839 struct nfsrv_descript **ndp) 2840 { 2841 struct nfsrv_rec *rec; 2842 struct mbuf *m; 2843 struct sockaddr *nam; 2844 struct nfsrv_descript *nd; 2845 int error; 2846 2847 *ndp = NULL; 2848 if ((slp->ns_flag & SLP_VALID) == 0 || !STAILQ_FIRST(&slp->ns_rec)) 2849 return (ENOBUFS); 2850 rec = STAILQ_FIRST(&slp->ns_rec); 2851 STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link); 2852 KKASSERT(slp->ns_numrec > 0); 2853 if (--slp->ns_numrec == 0) 2854 slp->ns_flag &= ~SLP_DOREC; 2855 nam = rec->nr_address; 2856 m = rec->nr_packet; 2857 kfree(rec, M_NFSRVDESC); 2858 nd = kmalloc(sizeof(struct nfsrv_descript), M_NFSRVDESC, M_WAITOK); 2859 nd->nd_md = nd->nd_mrep = m; 2860 nd->nd_nam2 = nam; 2861 nd->nd_dpos = mtod(m, caddr_t); 2862 error = nfs_getreq(nd, nfsd, TRUE); 2863 if (error) { 2864 if (nam) { 2865 kfree(nam, M_SONAME); 2866 } 2867 kfree((caddr_t)nd, M_NFSRVDESC); 2868 return (error); 2869 } 2870 *ndp = nd; 2871 nfsd->nfsd_nd = nd; 2872 return (0); 2873 } 2874 2875 /* 2876 * Try to assign service sockets to nfsd threads based on the number 2877 * of new rpc requests that have been queued on the service socket. 2878 * 2879 * If no nfsd's are available or additonal requests are pending, set the 2880 * NFSD_CHECKSLP flag so that one of the running nfsds will go look for 2881 * the work in the nfssvc_sock list when it is finished processing its 2882 * current work. This flag is only cleared when an nfsd can not find 2883 * any new work to perform. 2884 */ 2885 void 2886 nfsrv_wakenfsd(struct nfssvc_sock *slp, int nparallel) 2887 { 2888 struct nfsd *nd; 2889 2890 if ((slp->ns_flag & SLP_VALID) == 0) 2891 return; 2892 if (nparallel <= 1) 2893 nparallel = 1; 2894 TAILQ_FOREACH(nd, &nfsd_head, nfsd_chain) { 2895 if (nd->nfsd_flag & NFSD_WAITING) { 2896 nd->nfsd_flag &= ~NFSD_WAITING; 2897 if (nd->nfsd_slp) 2898 panic("nfsd wakeup"); 2899 nfsrv_slpref(slp); 2900 nd->nfsd_slp = slp; 2901 wakeup((caddr_t)nd); 2902 if (--nparallel == 0) 2903 break; 2904 } 2905 } 2906 2907 /* 2908 * If we couldn't assign slp then the NFSDs are all busy and 2909 * we set a flag indicating that there is pending work. 2910 */ 2911 if (nparallel) 2912 nfsd_head_flag |= NFSD_CHECKSLP; 2913 } 2914 #endif /* NFS_NOSERVER */ 2915