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