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