]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/nfsserver/nfs_srvsock.c
Merge OpenBSM 1.1 alpha 2 from the OpenBSM vendor branch to head, both
[FreeBSD/FreeBSD.git] / sys / nfsserver / nfs_srvsock.c
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  * 4. 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  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 /*
39  * Socket operations for use by nfs
40  */
41
42 #include "opt_mac.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/lock.h>
48 #include <sys/malloc.h>
49 #include <sys/mbuf.h>
50 #include <sys/mount.h>
51 #include <sys/mutex.h>
52 #include <sys/proc.h>
53 #include <sys/protosw.h>
54 #include <sys/refcount.h>
55 #include <sys/signalvar.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/sysctl.h>
59 #include <sys/syslog.h>
60 #include <sys/vnode.h>
61
62 #include <netinet/in.h>
63 #include <netinet/tcp.h>
64
65 #include <nfs/rpcv2.h>
66 #include <nfs/nfsproto.h>
67 #include <nfsserver/nfs.h>
68 #include <nfs/xdr_subs.h>
69 #include <nfsserver/nfsm_subs.h>
70
71 #include <security/mac/mac_framework.h>
72
73 #ifdef NFS_LEGACYRPC
74
75 #define TRUE    1
76 #define FALSE   0
77
78 static int nfs_realign_test;
79 static int nfs_realign_count;
80
81 SYSCTL_DECL(_vfs_nfsrv);
82
83 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, realign_test, CTLFLAG_RW, &nfs_realign_test, 0, "");
84 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, realign_count, CTLFLAG_RW, &nfs_realign_count, 0, "");
85
86
87 /*
88  * There is a congestion window for outstanding rpcs maintained per mount
89  * point. The cwnd size is adjusted in roughly the way that:
90  * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
91  * SIGCOMM '88". ACM, August 1988.
92  * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
93  * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
94  * of rpcs is in progress.
95  * (The sent count and cwnd are scaled for integer arith.)
96  * Variants of "slow start" were tried and were found to be too much of a
97  * performance hit (ave. rtt 3 times larger),
98  * I suspect due to the large rtt that nfs rpcs have.
99  */
100 #define NFS_CWNDSCALE   256
101 #define NFS_MAXCWND     (NFS_CWNDSCALE * 32)
102 struct callout  nfsrv_callout;
103
104 static void     nfs_realign(struct mbuf **pm, int hsiz);        /* XXX SHARED */
105 static int      nfsrv_getstream(struct nfssvc_sock *, int);
106
107 int32_t (*nfsrv3_procs[NFS_NPROCS])(struct nfsrv_descript *nd,
108                                 struct nfssvc_sock *slp,
109                                 struct mbuf **mreqp) = {
110         nfsrv_null,
111         nfsrv_getattr,
112         nfsrv_setattr,
113         nfsrv_lookup,
114         nfsrv3_access,
115         nfsrv_readlink,
116         nfsrv_read,
117         nfsrv_write,
118         nfsrv_create,
119         nfsrv_mkdir,
120         nfsrv_symlink,
121         nfsrv_mknod,
122         nfsrv_remove,
123         nfsrv_rmdir,
124         nfsrv_rename,
125         nfsrv_link,
126         nfsrv_readdir,
127         nfsrv_readdirplus,
128         nfsrv_statfs,
129         nfsrv_fsinfo,
130         nfsrv_pathconf,
131         nfsrv_commit,
132         nfsrv_noop
133 };
134
135
136 /*
137  * Generate the rpc reply header
138  * siz arg. is used to decide if adding a cluster is worthwhile
139  */
140 struct mbuf *
141 nfs_rephead(int siz, struct nfsrv_descript *nd, int err,
142     struct mbuf **mbp, caddr_t *bposp)
143 {
144         u_int32_t *tl;
145         struct mbuf *mreq;
146         caddr_t bpos;
147         struct mbuf *mb;
148
149         nd->nd_repstat = err;
150         if (err && (nd->nd_flag & ND_NFSV3) == 0)       /* XXX recheck */
151                 siz = 0;
152         MGETHDR(mreq, M_WAIT, MT_DATA);
153         mb = mreq;
154         /*
155          * If this is a big reply, use a cluster else
156          * try and leave leading space for the lower level headers.
157          */
158         mreq->m_len = 6 * NFSX_UNSIGNED;
159         siz += RPC_REPLYSIZ;
160         if ((max_hdr + siz) >= MINCLSIZE) {
161                 MCLGET(mreq, M_WAIT);
162         } else
163                 mreq->m_data += min(max_hdr, M_TRAILINGSPACE(mreq));
164         tl = mtod(mreq, u_int32_t *);
165         bpos = ((caddr_t)tl) + mreq->m_len;
166         *tl++ = txdr_unsigned(nd->nd_retxid);
167         *tl++ = nfsrv_rpc_reply;
168         if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) {
169                 *tl++ = nfsrv_rpc_msgdenied;
170                 if (err & NFSERR_AUTHERR) {
171                         *tl++ = nfsrv_rpc_autherr;
172                         *tl = txdr_unsigned(err & ~NFSERR_AUTHERR);
173                         mreq->m_len -= NFSX_UNSIGNED;
174                         bpos -= NFSX_UNSIGNED;
175                 } else {
176                         *tl++ = nfsrv_rpc_mismatch;
177                         *tl++ = txdr_unsigned(RPC_VER2);
178                         *tl = txdr_unsigned(RPC_VER2);
179                 }
180         } else {
181                 *tl++ = nfsrv_rpc_msgaccepted;
182                 /*
183                  * Send a RPCAUTH_NULL verifier - no Kerberos.
184                  */
185                 *tl++ = 0;
186                 *tl++ = 0;
187                 switch (err) {
188                 case EPROGUNAVAIL:
189                         *tl = txdr_unsigned(RPC_PROGUNAVAIL);
190                         break;
191                 case EPROGMISMATCH:
192                         *tl = txdr_unsigned(RPC_PROGMISMATCH);
193                         tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
194                         *tl++ = txdr_unsigned(2);
195                         *tl = txdr_unsigned(3);
196                         break;
197                 case EPROCUNAVAIL:
198                         *tl = txdr_unsigned(RPC_PROCUNAVAIL);
199                         break;
200                 case EBADRPC:
201                         *tl = txdr_unsigned(RPC_GARBAGE);
202                         break;
203                 default:
204                         *tl = 0;
205                         if (err != NFSERR_RETVOID) {
206                                 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
207                                 if (err)
208                                     *tl = txdr_unsigned(nfsrv_errmap(nd, err));
209                                 else
210                                     *tl = 0;
211                         }
212                         break;
213                 }
214         }
215         *mbp = mb;
216         *bposp = bpos;
217         if (err != 0 && err != NFSERR_RETVOID)
218                 nfsrvstats.srvrpc_errs++;
219         return mreq;
220 }
221
222
223 /*
224  *      nfs_realign:
225  *
226  *      Check for badly aligned mbuf data and realign by copying the unaligned
227  *      portion of the data into a new mbuf chain and freeing the portions
228  *      of the old chain that were replaced.
229  *
230  *      We cannot simply realign the data within the existing mbuf chain
231  *      because the underlying buffers may contain other rpc commands and
232  *      we cannot afford to overwrite them.
233  *
234  *      We would prefer to avoid this situation entirely.  The situation does
235  *      not occur with NFS/UDP and is supposed to only occassionally occur
236  *      with TCP.  Use vfs.nfs.realign_count and realign_test to check this.
237  */
238 static void
239 nfs_realign(struct mbuf **pm, int hsiz) /* XXX COMMON */
240 {
241         struct mbuf *m;
242         struct mbuf *n = NULL;
243         int off = 0;
244
245         ++nfs_realign_test;
246         while ((m = *pm) != NULL) {
247                 if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) {
248                         MGET(n, M_WAIT, MT_DATA);
249                         if (m->m_len >= MINCLSIZE) {
250                                 MCLGET(n, M_WAIT);
251                         }
252                         n->m_len = 0;
253                         break;
254                 }
255                 pm = &m->m_next;
256         }
257
258         /*
259          * If n is non-NULL, loop on m copying data, then replace the
260          * portion of the chain that had to be realigned.
261          */
262         if (n != NULL) {
263                 ++nfs_realign_count;
264                 while (m) {
265                         m_copyback(n, off, m->m_len, mtod(m, caddr_t));
266                         off += m->m_len;
267                         m = m->m_next;
268                 }
269                 m_freem(*pm);
270                 *pm = n;
271         }
272 }
273
274
275 /*
276  * Parse an RPC request
277  * - verify it
278  * - fill in the cred struct.
279  */
280 int
281 nfs_getreq(struct nfsrv_descript *nd, struct nfsd *nfsd, int has_header)
282 {
283         int len, i;
284         u_int32_t *tl;
285         caddr_t dpos;
286         u_int32_t nfsvers, auth_type;
287         int error = 0;
288         struct mbuf *mrep, *md;
289
290         NFSD_LOCK_ASSERT();
291
292         mrep = nd->nd_mrep;
293         md = nd->nd_md;
294         dpos = nd->nd_dpos;
295         if (has_header) {
296                 tl = nfsm_dissect_nonblock(u_int32_t *, 10 * NFSX_UNSIGNED);
297                 nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++);
298                 if (*tl++ != nfsrv_rpc_call) {
299                         m_freem(mrep);
300                         return (EBADRPC);
301                 }
302         } else
303                 tl = nfsm_dissect_nonblock(u_int32_t *, 8 * NFSX_UNSIGNED);
304         nd->nd_repstat = 0;
305         nd->nd_flag = 0;
306         if (*tl++ != nfsrv_rpc_vers) {
307                 nd->nd_repstat = ERPCMISMATCH;
308                 nd->nd_procnum = NFSPROC_NOOP;
309                 return (0);
310         }
311         if (*tl != nfsrv_nfs_prog) {
312                 nd->nd_repstat = EPROGUNAVAIL;
313                 nd->nd_procnum = NFSPROC_NOOP;
314                 return (0);
315         }
316         tl++;
317         nfsvers = fxdr_unsigned(u_int32_t, *tl++);
318         if (nfsvers < NFS_VER2 || nfsvers > NFS_VER3) {
319                 nd->nd_repstat = EPROGMISMATCH;
320                 nd->nd_procnum = NFSPROC_NOOP;
321                 return (0);
322         }
323         nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++);
324         if (nd->nd_procnum == NFSPROC_NULL)
325                 return (0);
326         if (nfsvers == NFS_VER3) {
327                 nd->nd_flag = ND_NFSV3;
328                 if (nd->nd_procnum >= NFS_NPROCS) {
329                         nd->nd_repstat = EPROCUNAVAIL;
330                         nd->nd_procnum = NFSPROC_NOOP;
331                         return (0);
332                 }
333         } else {
334                 if (nd->nd_procnum > NFSV2PROC_STATFS) {
335                         nd->nd_repstat = EPROCUNAVAIL;
336                         nd->nd_procnum = NFSPROC_NOOP;
337                         return (0);
338                 }
339                 /* Map the v2 procedure numbers into v3 ones */
340                 nd->nd_procnum = nfsrv_nfsv3_procid[nd->nd_procnum];
341         }
342         auth_type = *tl++;
343         len = fxdr_unsigned(int, *tl++);
344         if (len < 0 || len > RPCAUTH_MAXSIZ) {
345                 m_freem(mrep);
346                 return (EBADRPC);
347         }
348
349         /*
350          * Handle auth_unix;
351          */
352         if (auth_type == nfsrv_rpc_auth_unix) {
353                 len = fxdr_unsigned(int, *++tl);
354                 if (len < 0 || len > NFS_MAXNAMLEN) {
355                         m_freem(mrep);
356                         return (EBADRPC);
357                 }
358                 nfsm_adv(nfsm_rndup(len));
359                 tl = nfsm_dissect_nonblock(u_int32_t *, 3 * NFSX_UNSIGNED);
360                 nd->nd_cr->cr_uid = nd->nd_cr->cr_ruid =
361                     nd->nd_cr->cr_svuid = fxdr_unsigned(uid_t, *tl++);
362                 nd->nd_cr->cr_groups[0] = nd->nd_cr->cr_rgid =
363                     nd->nd_cr->cr_svgid = fxdr_unsigned(gid_t, *tl++);
364 #ifdef MAC
365                 mac_cred_associate_nfsd(nd->nd_cr);
366 #endif
367                 len = fxdr_unsigned(int, *tl);
368                 if (len < 0 || len > RPCAUTH_UNIXGIDS) {
369                         m_freem(mrep);
370                         return (EBADRPC);
371                 }
372                 tl = nfsm_dissect_nonblock(u_int32_t *, (len + 2) * NFSX_UNSIGNED);
373                 for (i = 1; i <= len; i++)
374                     if (i < NGROUPS)
375                         nd->nd_cr->cr_groups[i] = fxdr_unsigned(gid_t, *tl++);
376                     else
377                         tl++;
378                 nd->nd_cr->cr_ngroups = (len >= NGROUPS) ? NGROUPS : (len + 1);
379                 if (nd->nd_cr->cr_ngroups > 1)
380                     nfsrvw_sort(nd->nd_cr->cr_groups, nd->nd_cr->cr_ngroups);
381                 len = fxdr_unsigned(int, *++tl);
382                 if (len < 0 || len > RPCAUTH_MAXSIZ) {
383                         m_freem(mrep);
384                         return (EBADRPC);
385                 }
386                 if (len > 0)
387                         nfsm_adv(nfsm_rndup(len));
388                 nd->nd_credflavor = RPCAUTH_UNIX;
389         } else {
390                 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
391                 nd->nd_procnum = NFSPROC_NOOP;
392                 return (0);
393         }
394
395         nd->nd_md = md;
396         nd->nd_dpos = dpos;
397         return (0);
398 nfsmout:
399         return (error);
400 }
401
402 /*
403  * Socket upcall routine for the nfsd sockets.
404  * The caddr_t arg is a pointer to the "struct nfssvc_sock".
405  * Essentially do as much as possible non-blocking, else punt and it will
406  * be called with M_WAIT from an nfsd.
407  */
408 void
409 nfsrv_rcv(struct socket *so, void *arg, int waitflag)
410 {
411         struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
412         struct mbuf *m;
413         struct mbuf *mp;
414         struct sockaddr *nam;
415         struct uio auio;
416         int flags, error;
417
418         NFSD_UNLOCK_ASSERT();
419
420         /* XXXRW: Unlocked read. */
421         if ((slp->ns_flag & SLP_VALID) == 0)
422                 return;
423
424         /*
425          * We can't do this in the context of a socket callback
426          * because we're called with locks held.
427          * XXX: SMP
428          */
429         if (waitflag == M_DONTWAIT) {
430                 NFSD_LOCK();
431                 slp->ns_flag |= SLP_NEEDQ;
432                 goto dorecs;
433         }
434
435
436         NFSD_LOCK();
437         auio.uio_td = NULL;
438         if (so->so_type == SOCK_STREAM) {
439                 /*
440                  * If there are already records on the queue, defer soreceive()
441                  * to an nfsd so that there is feedback to the TCP layer that
442                  * the nfs servers are heavily loaded.
443                  */
444                 if (STAILQ_FIRST(&slp->ns_rec) != NULL &&
445                     waitflag == M_DONTWAIT) {
446                         slp->ns_flag |= SLP_NEEDQ;
447                         goto dorecs;
448                 }
449
450                 /*
451                  * Do soreceive().
452                  */
453                 auio.uio_resid = 1000000000;
454                 flags = MSG_DONTWAIT;
455                 NFSD_UNLOCK();
456                 error = soreceive(so, &nam, &auio, &mp, NULL, &flags);
457                 NFSD_LOCK();
458                 if (error || mp == NULL) {
459                         if (error == EWOULDBLOCK)
460                                 slp->ns_flag |= SLP_NEEDQ;
461                         else
462                                 slp->ns_flag |= SLP_DISCONN;
463                         goto dorecs;
464                 }
465                 m = mp;
466                 if (slp->ns_rawend) {
467                         slp->ns_rawend->m_next = m;
468                         slp->ns_cc += 1000000000 - auio.uio_resid;
469                 } else {
470                         slp->ns_raw = m;
471                         slp->ns_cc = 1000000000 - auio.uio_resid;
472                 }
473                 while (m->m_next)
474                         m = m->m_next;
475                 slp->ns_rawend = m;
476
477                 /*
478                  * Now try and parse record(s) out of the raw stream data.
479                  */
480                 error = nfsrv_getstream(slp, waitflag);
481                 if (error) {
482                         if (error == EPERM)
483                                 slp->ns_flag |= SLP_DISCONN;
484                         else
485                                 slp->ns_flag |= SLP_NEEDQ;
486                 }
487         } else {
488                 do {
489                         auio.uio_resid = 1000000000;
490                         flags = MSG_DONTWAIT;
491                         NFSD_UNLOCK();
492                         error = soreceive(so, &nam, &auio, &mp, NULL, &flags);
493                         if (mp) {
494                                 struct nfsrv_rec *rec;
495                                 rec = malloc(sizeof(struct nfsrv_rec),
496                                     M_NFSRVDESC, 
497                                     waitflag == M_DONTWAIT ? M_NOWAIT : M_WAITOK);
498                                 if (!rec) {
499                                         if (nam)
500                                                 free(nam, M_SONAME);
501                                         m_freem(mp);
502                                         NFSD_LOCK();
503                                         continue;
504                                 }
505                                 nfs_realign(&mp, 10 * NFSX_UNSIGNED);
506                                 NFSD_LOCK();
507                                 rec->nr_address = nam;
508                                 rec->nr_packet = mp;
509                                 STAILQ_INSERT_TAIL(&slp->ns_rec, rec, nr_link);
510                         } else
511                                 NFSD_LOCK();
512                         if (error) {
513                                 if ((so->so_proto->pr_flags & PR_CONNREQUIRED)
514                                         && error != EWOULDBLOCK) {
515                                         slp->ns_flag |= SLP_DISCONN;
516                                         goto dorecs;
517                                 }
518                         }
519                 } while (mp);
520         }
521
522         /*
523          * Now try and process the request records, non-blocking.
524          */
525 dorecs:
526         if (waitflag == M_DONTWAIT &&
527                 (STAILQ_FIRST(&slp->ns_rec) != NULL ||
528                  (slp->ns_flag & (SLP_NEEDQ | SLP_DISCONN))))
529                 nfsrv_wakenfsd(slp);
530         NFSD_UNLOCK();
531 }
532
533 /*
534  * Try and extract an RPC request from the mbuf data list received on a
535  * stream socket. The "waitflag" argument indicates whether or not it
536  * can sleep.
537  */
538 static int
539 nfsrv_getstream(struct nfssvc_sock *slp, int waitflag)
540 {
541         struct mbuf *m, **mpp;
542         char *cp1, *cp2;
543         int len;
544         struct mbuf *om, *m2, *recm;
545         u_int32_t recmark;
546
547         NFSD_LOCK_ASSERT();
548
549         if (slp->ns_flag & SLP_GETSTREAM)
550                 panic("nfs getstream");
551         slp->ns_flag |= SLP_GETSTREAM;
552         for (;;) {
553             if (slp->ns_reclen == 0) {
554                 if (slp->ns_cc < NFSX_UNSIGNED) {
555                         slp->ns_flag &= ~SLP_GETSTREAM;
556                         return (0);
557                 }
558                 m = slp->ns_raw;
559                 if (m->m_len >= NFSX_UNSIGNED) {
560                         bcopy(mtod(m, caddr_t), (caddr_t)&recmark, NFSX_UNSIGNED);
561                         m->m_data += NFSX_UNSIGNED;
562                         m->m_len -= NFSX_UNSIGNED;
563                 } else {
564                         cp1 = (caddr_t)&recmark;
565                         cp2 = mtod(m, caddr_t);
566                         while (cp1 < ((caddr_t)&recmark) + NFSX_UNSIGNED) {
567                                 while (m->m_len == 0) {
568                                         m = m->m_next;
569                                         cp2 = mtod(m, caddr_t);
570                                 }
571                                 *cp1++ = *cp2++;
572                                 m->m_data++;
573                                 m->m_len--;
574                         }
575                 }
576                 slp->ns_cc -= NFSX_UNSIGNED;
577                 recmark = ntohl(recmark);
578                 slp->ns_reclen = recmark & ~0x80000000;
579                 if (recmark & 0x80000000)
580                         slp->ns_flag |= SLP_LASTFRAG;
581                 else
582                         slp->ns_flag &= ~SLP_LASTFRAG;
583                 if (slp->ns_reclen > NFS_MAXPACKET || slp->ns_reclen <= 0) {
584                         slp->ns_flag &= ~SLP_GETSTREAM;
585                         return (EPERM);
586                 }
587             }
588
589             /*
590              * Now get the record part.
591              *
592              * Note that slp->ns_reclen may be 0.  Linux sometimes
593              * generates 0-length RPCs.
594              */
595             recm = NULL;
596             if (slp->ns_cc == slp->ns_reclen) {
597                 recm = slp->ns_raw;
598                 slp->ns_raw = slp->ns_rawend = NULL;
599                 slp->ns_cc = slp->ns_reclen = 0;
600             } else if (slp->ns_cc > slp->ns_reclen) {
601                 len = 0;
602                 m = slp->ns_raw;
603                 om = NULL;
604
605                 while (len < slp->ns_reclen) {
606                         if ((len + m->m_len) > slp->ns_reclen) {
607                                 NFSD_UNLOCK();
608                                 m2 = m_copym(m, 0, slp->ns_reclen - len,
609                                         waitflag);
610                                 NFSD_LOCK();
611                                 if (m2) {
612                                         if (om) {
613                                                 om->m_next = m2;
614                                                 recm = slp->ns_raw;
615                                         } else
616                                                 recm = m2;
617                                         m->m_data += slp->ns_reclen - len;
618                                         m->m_len -= slp->ns_reclen - len;
619                                         len = slp->ns_reclen;
620                                 } else {
621                                         slp->ns_flag &= ~SLP_GETSTREAM;
622                                         return (EWOULDBLOCK);
623                                 }
624                         } else if ((len + m->m_len) == slp->ns_reclen) {
625                                 om = m;
626                                 len += m->m_len;
627                                 m = m->m_next;
628                                 recm = slp->ns_raw;
629                                 om->m_next = NULL;
630                         } else {
631                                 om = m;
632                                 len += m->m_len;
633                                 m = m->m_next;
634                         }
635                 }
636                 slp->ns_raw = m;
637                 slp->ns_cc -= len;
638                 slp->ns_reclen = 0;
639             } else {
640                 slp->ns_flag &= ~SLP_GETSTREAM;
641                 return (0);
642             }
643
644             /*
645              * Accumulate the fragments into a record.
646              */
647             mpp = &slp->ns_frag;
648             while (*mpp)
649                 mpp = &((*mpp)->m_next);
650             *mpp = recm;
651             if (slp->ns_flag & SLP_LASTFRAG) {
652                 struct nfsrv_rec *rec;
653                 NFSD_UNLOCK();
654                 rec = malloc(sizeof(struct nfsrv_rec), M_NFSRVDESC,
655                     waitflag == M_DONTWAIT ? M_NOWAIT : M_WAITOK);
656                 if (rec) {
657                     nfs_realign(&slp->ns_frag, 10 * NFSX_UNSIGNED);
658                     rec->nr_address = NULL;
659                     rec->nr_packet = slp->ns_frag;
660                     NFSD_LOCK();
661                     STAILQ_INSERT_TAIL(&slp->ns_rec, rec, nr_link);
662                 } else {
663                     NFSD_LOCK();
664                 }
665                 if (!rec) {
666                     m_freem(slp->ns_frag);
667                 }
668                 slp->ns_frag = NULL;
669             }
670         }
671 }
672
673 /*
674  * Parse an RPC header.
675  */
676 int
677 nfsrv_dorec(struct nfssvc_sock *slp, struct nfsd *nfsd,
678     struct nfsrv_descript **ndp)
679 {
680         struct nfsrv_rec *rec;
681         struct mbuf *m;
682         struct sockaddr *nam;
683         struct nfsrv_descript *nd;
684         int error;
685
686         NFSD_LOCK_ASSERT();
687
688         *ndp = NULL;
689         if ((slp->ns_flag & SLP_VALID) == 0 ||
690             STAILQ_FIRST(&slp->ns_rec) == NULL)
691                 return (ENOBUFS);
692         rec = STAILQ_FIRST(&slp->ns_rec);
693         KASSERT(rec->nr_packet != NULL, ("nfsrv_dorec: missing mbuf"));
694         STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
695         nam = rec->nr_address;
696         m = rec->nr_packet;
697         free(rec, M_NFSRVDESC);
698         NFSD_UNLOCK();
699         nd = malloc(sizeof (struct nfsrv_descript),
700                 M_NFSRVDESC, M_WAITOK);
701         nd->nd_cr = crget();
702         NFSD_LOCK();
703         nd->nd_md = nd->nd_mrep = m;
704         nd->nd_nam2 = nam;
705         nd->nd_dpos = mtod(m, caddr_t);
706         error = nfs_getreq(nd, nfsd, TRUE);
707         if (error) {
708                 if (nam) {
709                         free(nam, M_SONAME);
710                 }
711                 if (nd->nd_cr != NULL)
712                         crfree(nd->nd_cr);
713                 free((caddr_t)nd, M_NFSRVDESC);
714                 return (error);
715         }
716         *ndp = nd;
717         nfsd->nfsd_nd = nd;
718         return (0);
719 }
720
721 /*
722  * Search for a sleeping nfsd and wake it up.
723  * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
724  * running nfsds will go look for the work in the nfssvc_sock list.
725  */
726 void
727 nfsrv_wakenfsd(struct nfssvc_sock *slp)
728 {
729         struct nfsd *nd;
730
731         NFSD_LOCK_ASSERT();
732
733         if ((slp->ns_flag & SLP_VALID) == 0)
734                 return;
735         TAILQ_FOREACH(nd, &nfsd_head, nfsd_chain) {
736                 if (nd->nfsd_flag & NFSD_WAITING) {
737                         nd->nfsd_flag &= ~NFSD_WAITING;
738                         if (nd->nfsd_slp)
739                                 panic("nfsd wakeup");
740                         slp->ns_sref++;
741                         nd->nfsd_slp = slp;
742                         wakeup(nd);
743                         return;
744                 }
745         }
746         slp->ns_flag |= SLP_DOREC;
747         nfsd_head_flag |= NFSD_CHECKSLP;
748 }
749
750 /*
751  * This is the nfs send routine.
752  * For the server side:
753  * - return EINTR or ERESTART if interrupted by a signal
754  * - return EPIPE if a connection is lost for connection based sockets (TCP...)
755  * - do any cleanup required by recoverable socket errors (?)
756  */
757 int
758 nfsrv_send(struct socket *so, struct sockaddr *nam, struct mbuf *top)
759 {
760         struct sockaddr *sendnam;
761         int error, soflags, flags;
762
763         NFSD_UNLOCK_ASSERT();
764
765         soflags = so->so_proto->pr_flags;
766         if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
767                 sendnam = NULL;
768         else
769                 sendnam = nam;
770         if (so->so_type == SOCK_SEQPACKET)
771                 flags = MSG_EOR;
772         else
773                 flags = 0;
774
775         error = sosend(so, sendnam, 0, top, 0, flags, curthread/*XXX*/);
776         if (error == ENOBUFS && so->so_type == SOCK_DGRAM)
777                 error = 0;
778
779         if (error) {
780                 log(LOG_INFO, "nfsd send error %d\n", error);
781
782                 /*
783                  * Handle any recoverable (soft) socket errors here. (?)
784                  */
785                 if (error != EINTR && error != ERESTART &&
786                     error != EWOULDBLOCK && error != EPIPE)
787                         error = 0;
788         }
789         return (error);
790 }
791
792 /*
793  * NFS server timer routine.
794  */
795 void
796 nfsrv_timer(void *arg)
797 {
798         struct nfssvc_sock *slp;
799         u_quad_t cur_usec;
800
801         NFSD_LOCK();
802         /*
803          * Scan the write gathering queues for writes that need to be
804          * completed now.
805          */
806         cur_usec = nfs_curusec();
807         TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
808                 if (LIST_FIRST(&slp->ns_tq) &&
809                     LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec)
810                         nfsrv_wakenfsd(slp);
811         }
812         NFSD_UNLOCK();
813         callout_reset(&nfsrv_callout, nfsrv_ticks, nfsrv_timer, NULL);
814 }
815
816 #endif /* NFS_LEGACYRPC */