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