NFS - Cleanup pass -factor out strict temporaries from nfsm_info
[dragonfly.git] / sys / vfs / nfs / nfsm_subs.c
1 /*
2  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 /*
35  * Copyright (c) 1989, 1993
36  *      The Regents of the University of California.  All rights reserved.
37  *
38  * This code is derived from software contributed to Berkeley by
39  * Rick Macklem at The University of Guelph.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. All advertising materials mentioning features or use of this software
50  *    must display the following acknowledgement:
51  *      This product includes software developed by the University of
52  *      California, Berkeley and its contributors.
53  * 4. Neither the name of the University nor the names of its contributors
54  *    may be used to endorse or promote products derived from this software
55  *    without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67  * SUCH DAMAGE.
68  */
69
70 /*
71  * These functions support the macros and help fiddle mbuf chains for
72  * the nfs op functions. They do things like create the rpc header and
73  * copy data between mbuf chains and uio lists.
74  */
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/kernel.h>
78 #include <sys/buf.h>
79 #include <sys/proc.h>
80 #include <sys/mount.h>
81 #include <sys/vnode.h>
82 #include <sys/nlookup.h>
83 #include <sys/namei.h>
84 #include <sys/mbuf.h>
85 #include <sys/socket.h>
86 #include <sys/stat.h>
87 #include <sys/malloc.h>
88 #include <sys/sysent.h>
89 #include <sys/syscall.h>
90 #include <sys/conf.h>
91 #include <sys/objcache.h>
92
93 #include <vm/vm.h>
94 #include <vm/vm_object.h>
95 #include <vm/vm_extern.h>
96 #include <vm/vm_zone.h>
97
98 #include <sys/buf2.h>
99
100 #include "rpcv2.h"
101 #include "nfsproto.h"
102 #include "nfs.h"
103 #include "nfsmount.h"
104 #include "nfsnode.h"
105 #include "xdr_subs.h"
106 #include "nfsm_subs.h"
107 #include "nfsrtt.h"
108
109 #include <netinet/in.h>
110
111 static u_int32_t nfs_xid = 0;
112
113 /*
114  * Create the header for an rpc request packet
115  * The hsiz is the size of the rest of the nfs request header.
116  * (just used to decide if a cluster is a good idea)
117  */
118 void
119 nfsm_reqhead(nfsm_info_t info, struct vnode *vp, u_long procid, int hsiz)
120 {
121         info->mb = m_getl(hsiz, MB_WAIT, MT_DATA, 0, NULL);
122         info->mb->m_len = 0;
123         info->mreq = info->mb;
124         info->bpos = mtod(info->mb, caddr_t);
125 }
126
127 /*
128  * Build the RPC header and fill in the authorization info.
129  * The authorization string argument is only used when the credentials
130  * come from outside of the kernel.
131  * Returns the head of the mbuf list.
132  */
133 struct mbuf *
134 nfsm_rpchead(struct ucred *cr, int nmflag, int procid, int auth_type,
135              int auth_len, char *auth_str, int verf_len, char *verf_str,
136              struct mbuf *mrest, int mrest_len, struct mbuf **mbp,
137              u_int32_t *xidp)
138 {
139         struct nfsm_info info;
140         struct mbuf *mb2;
141         u_int32_t *tl;
142         int siz, grpsiz, authsiz, dsiz;
143         int i;
144
145         authsiz = nfsm_rndup(auth_len);
146         dsiz = authsiz + 10 * NFSX_UNSIGNED;
147         info.mb = m_getl(dsiz, MB_WAIT, MT_DATA, M_PKTHDR, NULL);
148         if (dsiz < MINCLSIZE) {
149                 if (dsiz < MHLEN)
150                         MH_ALIGN(info.mb, dsiz);
151                 else
152                         MH_ALIGN(info.mb, 8 * NFSX_UNSIGNED);
153         }
154         info.mb->m_len = info.mb->m_pkthdr.len = 0;
155         info.mreq = info.mb;
156         info.bpos = mtod(info.mb, caddr_t);
157
158         /*
159          * First the RPC header.
160          */
161         tl = nfsm_build(&info, 8 * NFSX_UNSIGNED);
162
163         /* Get a pretty random xid to start with */
164         if (!nfs_xid)
165                 nfs_xid = krandom();
166         /*
167          * Skip zero xid if it should ever happen.
168          */
169         if (++nfs_xid == 0)
170                 nfs_xid++;
171
172         *tl++ = *xidp = txdr_unsigned(nfs_xid);
173         *tl++ = rpc_call;
174         *tl++ = rpc_vers;
175         *tl++ = txdr_unsigned(NFS_PROG);
176         if (nmflag & NFSMNT_NFSV3)
177                 *tl++ = txdr_unsigned(NFS_VER3);
178         else
179                 *tl++ = txdr_unsigned(NFS_VER2);
180         if (nmflag & NFSMNT_NFSV3)
181                 *tl++ = txdr_unsigned(procid);
182         else
183                 *tl++ = txdr_unsigned(nfsv2_procid[procid]);
184
185         /*
186          * And then the authorization cred.
187          */
188         *tl++ = txdr_unsigned(auth_type);
189         *tl = txdr_unsigned(authsiz);
190         switch (auth_type) {
191         case RPCAUTH_UNIX:
192                 tl = nfsm_build(&info, auth_len);
193                 *tl++ = 0;              /* stamp ?? */
194                 *tl++ = 0;              /* NULL hostname */
195                 *tl++ = txdr_unsigned(cr->cr_uid);
196                 *tl++ = txdr_unsigned(cr->cr_groups[0]);
197                 grpsiz = (auth_len >> 2) - 5;
198                 *tl++ = txdr_unsigned(grpsiz);
199                 for (i = 1; i <= grpsiz; i++)
200                         *tl++ = txdr_unsigned(cr->cr_groups[i]);
201                 break;
202         case RPCAUTH_KERB4:
203                 siz = auth_len;
204                 while (siz > 0) {
205                         if (M_TRAILINGSPACE(info.mb) == 0) {
206                                 mb2 = m_getl(siz, MB_WAIT, MT_DATA, 0, NULL);
207                                 mb2->m_len = 0;
208                                 info.mb->m_next = mb2;
209                                 info.mb = mb2;
210                                 info.bpos = mtod(info.mb, caddr_t);
211                         }
212                         i = min(siz, M_TRAILINGSPACE(info.mb));
213                         bcopy(auth_str, info.bpos, i);
214                         info.mb->m_len += i;
215                         auth_str += i;
216                         info.bpos += i;
217                         siz -= i;
218                 }
219                 if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
220                         for (i = 0; i < siz; i++)
221                                 *info.bpos++ = '\0';
222                         info.mb->m_len += siz;
223                 }
224                 break;
225         };
226
227         /*
228          * And the verifier...
229          */
230         tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
231         if (verf_str) {
232                 *tl++ = txdr_unsigned(RPCAUTH_KERB4);
233                 *tl = txdr_unsigned(verf_len);
234                 siz = verf_len;
235                 while (siz > 0) {
236                         if (M_TRAILINGSPACE(info.mb) == 0) {
237                                 mb2 = m_getl(siz, MB_WAIT, MT_DATA,
238                                                   0, NULL);
239                                 mb2->m_len = 0;
240                                 info.mb->m_next = mb2;
241                                 info.mb = mb2;
242                                 info.bpos = mtod(info.mb, caddr_t);
243                         }
244                         i = min(siz, M_TRAILINGSPACE(info.mb));
245                         bcopy(verf_str, info.bpos, i);
246                         info.mb->m_len += i;
247                         verf_str += i;
248                         info.bpos += i;
249                         siz -= i;
250                 }
251                 if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
252                         for (i = 0; i < siz; i++)
253                                 *info.bpos++ = '\0';
254                         info.mb->m_len += siz;
255                 }
256         } else {
257                 *tl++ = txdr_unsigned(RPCAUTH_NULL);
258                 *tl = 0;
259         }
260         info.mb->m_next = mrest;
261         info.mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
262         info.mreq->m_pkthdr.rcvif = NULL;
263         *mbp = info.mb;
264         return (info.mreq);
265 }
266
267 void *
268 nfsm_build(nfsm_info_t info, int bytes)
269 {
270         struct mbuf *mb2;
271         void *ptr;
272
273         if (bytes > M_TRAILINGSPACE(info->mb)) {
274                 MGET(mb2, MB_WAIT, MT_DATA);
275                 if (bytes > MLEN)
276                         panic("build > MLEN");
277                 info->mb->m_next = mb2;
278                 info->mb = mb2;
279                 info->mb->m_len = 0;
280                 info->bpos = mtod(info->mb, caddr_t);
281         }
282         ptr = info->bpos;
283         info->mb->m_len += bytes;
284         info->bpos += bytes;
285         return (ptr);
286 }
287
288 /*
289  *
290  * If NULL returned caller is expected to abort with an EBADRPC error.
291  * Caller will usually use the NULLOUT macro.
292  */
293 void *
294 nfsm_dissect(nfsm_info_t info, int bytes)
295 {
296         caddr_t cp2;
297         void *ptr;
298         int error;
299         int n;
300
301         n = mtod(info->md, caddr_t) + info->md->m_len - info->dpos;
302         if (bytes <= n) {
303                 ptr = info->dpos;
304                 info->dpos += bytes;
305         } else {
306                 error = nfsm_disct(&info->md, &info->dpos, bytes, n, &cp2);
307                 if (error) {
308                         m_freem(info->mrep);
309                         info->mrep = NULL;
310                         ptr = NULL;
311                 } else {
312                         ptr = cp2;
313                 }
314         }
315         return (ptr);
316 }
317
318 /*
319  *
320  * Caller is expected to abort if non-zero error is returned.
321  */
322 int
323 nfsm_fhtom(nfsm_info_t info, struct vnode *vp)
324 {
325         u_int32_t *tl;
326         caddr_t cp;
327         int error;
328         int n;
329
330         if (info->v3) {
331                 n = nfsm_rndup(VTONFS(vp)->n_fhsize) + NFSX_UNSIGNED;
332                 if (n <= M_TRAILINGSPACE(info->mb)) {
333                         tl = nfsm_build(info, n);
334                         *tl++ = txdr_unsigned(VTONFS(vp)->n_fhsize);
335                         *(tl + ((n >> 2) - 2)) = 0;
336                         bcopy((caddr_t)VTONFS(vp)->n_fhp,(caddr_t)tl,
337                                 VTONFS(vp)->n_fhsize);
338                         error = 0;
339                 } else if ((error = nfsm_strtmbuf(&info->mb, &info->bpos,
340                                                 (caddr_t)VTONFS(vp)->n_fhp,
341                                                 VTONFS(vp)->n_fhsize)) != 0) {
342                         m_freem(info->mreq);
343                         info->mreq = NULL;
344                 }
345         } else {
346                 cp = nfsm_build(info, NFSX_V2FH);
347                 bcopy(VTONFS(vp)->n_fhp, cp, NFSX_V2FH);
348                 error = 0;
349         }
350         return (error);
351 }
352
353 void
354 nfsm_srvfhtom(nfsm_info_t info, fhandle_t *fhp)
355 {
356         u_int32_t *tl;
357
358         if (info->v3) {
359                 tl = nfsm_build(info, NFSX_UNSIGNED + NFSX_V3FH);
360                 *tl++ = txdr_unsigned(NFSX_V3FH);
361                 bcopy(fhp, tl, NFSX_V3FH);
362         } else {
363                 tl = nfsm_build(info, NFSX_V2FH);
364                 bcopy(fhp, tl, NFSX_V2FH);
365         }
366 }
367
368 void
369 nfsm_srvpostop_fh(nfsm_info_t info, fhandle_t *fhp)
370 {
371         u_int32_t *tl;
372
373         tl = nfsm_build(info, 2 * NFSX_UNSIGNED + NFSX_V3FH);
374         *tl++ = nfs_true;
375         *tl++ = txdr_unsigned(NFSX_V3FH);
376         bcopy(fhp, tl, NFSX_V3FH);
377 }
378
379 /*
380  * Caller is expected to abort if non-zero error is returned.
381  *
382  * NOTE: (*vpp) may be loaded with a valid vnode even if (*gotvpp)
383  *       winds up 0.  The caller is responsible for dealing with (*vpp).
384  */
385 int
386 nfsm_mtofh(nfsm_info_t info, struct vnode *dvp, struct vnode **vpp, int *gotvpp)
387 {
388         struct nfsnode *ttnp;
389         nfsfh_t *ttfhp;
390         u_int32_t *tl;
391         int ttfhsize;
392         int error = 0;
393
394         if (info->v3) {
395                 tl = nfsm_dissect(info, NFSX_UNSIGNED);
396                 if (tl == NULL)
397                         return(EBADRPC);
398                 *gotvpp = fxdr_unsigned(int, *tl);
399         } else {
400                 *gotvpp = 1;
401         }
402         if (*gotvpp) {
403                 NEGATIVEOUT(ttfhsize = nfsm_getfh(info, &ttfhp));
404                 error = nfs_nget(dvp->v_mount, ttfhp, ttfhsize, &ttnp);
405                 if (error) {
406                         m_freem(info->mrep);
407                         info->mrep = NULL;
408                         return (error);
409                 }
410                 *vpp = NFSTOV(ttnp);
411         }
412         if (info->v3) {
413                 tl = nfsm_dissect(info, NFSX_UNSIGNED);
414                 if (tl == NULL)
415                         return (EBADRPC);
416                 if (*gotvpp) {
417                         *gotvpp = fxdr_unsigned(int, *tl);
418                 } else if (fxdr_unsigned(int, *tl)) {
419                         error = nfsm_adv(info, NFSX_V3FATTR);
420                         if (error)
421                                 return (error);
422                 }
423         }
424         if (*gotvpp)
425                 error = nfsm_loadattr(info, *vpp, NULL);
426 nfsmout:
427         return (error);
428 }
429
430 /*
431  *
432  * Caller is expected to abort with EBADRPC if a negative length is returned.
433  */
434 int
435 nfsm_getfh(nfsm_info_t info, nfsfh_t **fhpp)
436 {
437         u_int32_t *tl;
438         int n;
439
440         *fhpp = NULL;
441         if (info->v3) {
442                 tl = nfsm_dissect(info, NFSX_UNSIGNED);
443                 if (tl == NULL)
444                         return(-1);
445                 if ((n = fxdr_unsigned(int, *tl)) <= 0 || n > NFSX_V3FHMAX) {
446                         m_freem(info->mrep);
447                         info->mrep = NULL;
448                         return(-1);
449                 }
450         } else {
451                 n = NFSX_V2FH;
452         }
453         *fhpp = nfsm_dissect(info, nfsm_rndup(n));
454         if (*fhpp == NULL)
455                 return(-1);
456         return(n);
457 }
458
459 /*
460  * Caller is expected to abort if a non-zero error is returned.
461  */
462 int
463 nfsm_loadattr(nfsm_info_t info, struct vnode *vp, struct vattr *vap)
464 {
465         int error;
466
467         error = nfs_loadattrcache(vp, &info->md, &info->dpos, vap, 0);
468         if (error) {
469                 m_freem(info->mrep);
470                 info->mrep = NULL;
471                 return (error);
472         }
473         return (0);
474 }
475
476 /*
477  * Caller is expected to abort if a non-zero error is returned.
478  */
479 int
480 nfsm_postop_attr(nfsm_info_t info, struct vnode *vp, int *attrp, int lflags)
481 {
482         u_int32_t *tl;
483         int error;
484
485         tl = nfsm_dissect(info, NFSX_UNSIGNED);
486         if (tl == NULL)
487                 return(EBADRPC);
488         *attrp = fxdr_unsigned(int, *tl);
489         if (*attrp) {
490                 error = nfs_loadattrcache(vp, &info->md, &info->dpos,
491                                           NULL, lflags);
492                 if (error) {
493                         *attrp = 0;
494                         m_freem(info->mrep);
495                         info->mrep = NULL;
496                         return (error);
497                 }
498         }
499         return (0);
500 }
501
502 /*
503  * Caller is expected to abort if a non-zero error is returned.
504  */
505 int
506 nfsm_wcc_data(nfsm_info_t info, struct vnode *vp, int *attrp)
507 {
508         u_int32_t *tl;
509         int error;
510         int ttattrf;
511         int ttretf = 0;
512
513         tl = nfsm_dissect(info, NFSX_UNSIGNED);
514         if (tl == NULL)
515                 return (EBADRPC);
516         if (*tl == nfs_true) {
517                 tl = nfsm_dissect(info, 6 * NFSX_UNSIGNED);
518                 if (tl == NULL)
519                         return (EBADRPC);
520                 if (*attrp) {
521                         ttretf = (VTONFS(vp)->n_mtime ==
522                                 fxdr_unsigned(u_int32_t, *(tl + 2)));
523                         if (ttretf == 0)
524                                 VTONFS(vp)->n_flag |= NRMODIFIED;
525                 }
526                 error = nfsm_postop_attr(info, vp, &ttattrf,
527                                  NFS_LATTR_NOSHRINK|NFS_LATTR_NOMTIMECHECK);
528                 if (error)
529                         return(error);
530         } else {
531                 error = nfsm_postop_attr(info, vp, &ttattrf,
532                                          NFS_LATTR_NOSHRINK);
533                 if (error)
534                         return(error);
535         }
536         if (*attrp)
537                 *attrp = ttretf;
538         else
539                 *attrp = ttattrf;
540         return(0);
541 }
542
543 /*
544  * This function updates the attribute cache based on data returned in the
545  * NFS reply for NFS RPCs that modify the target file.  If the RPC succeeds
546  * a 'before' and 'after' mtime is returned that allows us to determine if
547  * the new mtime attribute represents our modification or someone else's
548  * modification.
549  *
550  * The flag argument returns non-0 if the original times matched, zero if
551  * they did not match.  NRMODIFIED is automatically set if the before time
552  * does not match the original n_mtime, and n_mtime is automatically updated
553  * to the new after time (by nfsm_postop_attr()).
554  *
555  * If full is true, set all fields, otherwise just set mode and time fields
556  */
557 void
558 nfsm_v3attrbuild(nfsm_info_t info, struct vattr *vap, int full)
559 {
560         u_int32_t *tl;
561
562         if (vap->va_mode != (mode_t)VNOVAL) {
563                 tl = nfsm_build(info, 2 * NFSX_UNSIGNED);
564                 *tl++ = nfs_true;
565                 *tl = txdr_unsigned(vap->va_mode);
566         } else {
567                 tl = nfsm_build(info, NFSX_UNSIGNED);
568                 *tl = nfs_false;
569         }
570         if (full && vap->va_uid != (uid_t)VNOVAL) {
571                 tl = nfsm_build(info, 2 * NFSX_UNSIGNED);
572                 *tl++ = nfs_true;
573                 *tl = txdr_unsigned(vap->va_uid);
574         } else {
575                 tl = nfsm_build(info, NFSX_UNSIGNED);
576                 *tl = nfs_false;
577         }
578         if (full && vap->va_gid != (gid_t)VNOVAL) {
579                 tl = nfsm_build(info, 2 * NFSX_UNSIGNED);
580                 *tl++ = nfs_true;
581                 *tl = txdr_unsigned(vap->va_gid);
582         } else {
583                 tl = nfsm_build(info, NFSX_UNSIGNED);
584                 *tl = nfs_false;
585         }
586         if (full && vap->va_size != VNOVAL) {
587                 tl = nfsm_build(info, 3 * NFSX_UNSIGNED);
588                 *tl++ = nfs_true;
589                 txdr_hyper(vap->va_size, tl);
590         } else {
591                 tl = nfsm_build(info, NFSX_UNSIGNED);
592                 *tl = nfs_false;
593         }
594         if (vap->va_atime.tv_sec != VNOVAL) {
595                 if (vap->va_atime.tv_sec != time_second) {
596                         tl = nfsm_build(info, 3 * NFSX_UNSIGNED);
597                         *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
598                         txdr_nfsv3time(&vap->va_atime, tl);
599                 } else {
600                         tl = nfsm_build(info, NFSX_UNSIGNED);
601                         *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
602                 }
603         } else {
604                 tl = nfsm_build(info, NFSX_UNSIGNED);
605                 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
606         }
607         if (vap->va_mtime.tv_sec != VNOVAL) {
608                 if (vap->va_mtime.tv_sec != time_second) {
609                         tl = nfsm_build(info, 3 * NFSX_UNSIGNED);
610                         *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
611                         txdr_nfsv3time(&vap->va_mtime, tl);
612                 } else {
613                         tl = nfsm_build(info, NFSX_UNSIGNED);
614                         *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
615                 }
616         } else {
617                 tl = nfsm_build(info, NFSX_UNSIGNED);
618                 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
619         }
620 }
621
622 /*
623  * Caller is expected to abort with EBADRPC if a negative length is returned.
624  */
625 int
626 nfsm_strsiz(nfsm_info_t info, int maxlen)
627 {
628         u_int32_t *tl;
629         int len;
630
631         tl = nfsm_dissect(info, NFSX_UNSIGNED);
632         if (tl == NULL)
633                 return(-1);
634         len = fxdr_unsigned(int32_t, *tl);
635         if (len < 0 || len > maxlen)
636                 return(-1);
637         return (len);
638 }
639
640 /*
641  * Caller is expected to abort if a negative length is returned, but also
642  * call nfsm_reply(0) if -2 is returned.
643  *
644  * This function sets *errorp.  Caller should not modify the error code.
645  */
646 int
647 nfsm_srvstrsiz(nfsm_info_t info, int maxlen, int *errorp)
648 {
649         u_int32_t *tl;
650         int len;
651
652         tl = nfsm_dissect(info, NFSX_UNSIGNED);
653         if (tl == NULL) {
654                 *errorp = EBADRPC;
655                 return(-1);
656         }
657         len = fxdr_unsigned(int32_t,*tl);
658         if (len > maxlen || len <= 0) {
659                 *errorp = EBADRPC;
660                 return(-2);
661         }
662         return(len);
663 }
664
665 /*
666  * Caller is expected to abort if a negative length is returned, but also
667  * call nfsm_reply(0) if -2 is returned.
668  *
669  * This function sets *errorp.  Caller should not modify the error code.
670  */
671 int
672 nfsm_srvnamesiz(nfsm_info_t info, int *errorp)
673 {
674         u_int32_t *tl;
675         int len;
676
677         tl = nfsm_dissect(info, NFSX_UNSIGNED);
678         if (tl == NULL) {
679                 *errorp = EBADRPC;
680                 return(-1);
681         }
682
683         /*
684          * In this case if *errorp is not EBADRPC and we are NFSv3,
685          * nfsm_reply() will not return a negative number.  But all
686          * call cases assume len is valid so we really do want
687          * to return -1.
688          */
689         len = fxdr_unsigned(int32_t,*tl);
690         if (len > NFS_MAXNAMLEN)
691                 *errorp = NFSERR_NAMETOL;
692         if (len <= 0)
693                 *errorp = EBADRPC;
694         if (*errorp)
695                 return(-2);
696         return (len);
697 }
698
699 /*
700  * Caller is expected to abort if a non-zero error is returned.
701  */
702 int
703 nfsm_mtouio(nfsm_info_t info, struct uio *uiop, int len)
704 {
705         int error;
706
707         if (len > 0 &&
708            (error = nfsm_mbuftouio(&info->md, uiop, len, &info->dpos)) != 0) {
709                 m_freem(info->mrep);
710                 info->mrep = NULL;
711                 return(error);
712         }
713         return(0);
714 }
715
716 /*
717  * Caller is expected to abort if a non-zero error is returned.
718  */
719 int
720 nfsm_uiotom(nfsm_info_t info, struct uio *uiop, int len)
721 {
722         int error;
723
724         if ((error = nfsm_uiotombuf(uiop, &info->mb, len, &info->bpos)) != 0) {
725                 m_freem(info->mreq);
726                 info->mreq = NULL;
727                 return (error);
728         }
729         return(0);
730 }
731
732 /*
733  * Caller is expected to abort if a negative value is returned.  This
734  * function sets *errorp.  Caller should not modify the error code.
735  */
736 int
737 nfsm_request(nfsm_info_t info, struct vnode *vp, int procnum,
738              thread_t td, struct ucred *cred, int *errorp)
739 {
740         *errorp = nfs_request(vp, info->mreq, procnum, td, cred,
741                               &info->mrep, &info->md, &info->dpos);
742         if (*errorp) {
743                 if ((*errorp & NFSERR_RETERR) == 0)
744                         return(-1);
745                 *errorp &= ~NFSERR_RETERR;
746         }
747         return(0);
748 }
749
750 /*
751  * Caller is expected to abort if a non-zero error is returned.
752  */
753 int
754 nfsm_strtom(nfsm_info_t info, const void *data, int len, int maxlen)
755 {
756         u_int32_t *tl;
757         int error;
758         int n;
759
760         if (len > maxlen) {
761                 m_freem(info->mreq);
762                 info->mreq = NULL;
763                 return(ENAMETOOLONG);
764         }
765         n = nfsm_rndup(len) + NFSX_UNSIGNED;
766         if (n <= M_TRAILINGSPACE(info->mb)) {
767                 tl = nfsm_build(info, n);
768                 *tl++ = txdr_unsigned(len);
769                 *(tl + ((n >> 2) - 2)) = 0;
770                 bcopy(data, tl, len);
771                 error = 0;
772         } else {
773                 error = nfsm_strtmbuf(&info->mb, &info->bpos, data, len);
774                 if (error) {
775                         m_freem(info->mreq);
776                         info->mreq = NULL;
777                 }
778         }
779         return (error);
780 }
781
782 /*
783  * Caller is expected to abort if a negative value is returned.  This
784  * function sets *errorp.  Caller should not modify the error code.
785  */
786 int
787 nfsm_reply(nfsm_info_t info,
788            struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
789            int siz, int *errorp)
790 {
791         nfsd->nd_repstat = *errorp;
792         if (*errorp && !(nfsd->nd_flag & ND_NFSV3))
793                 siz = 0;
794         nfs_rephead(siz, nfsd, slp, *errorp, &info->mreq,
795                     &info->mb, &info->bpos);
796         if (info->mrep != NULL) {
797                 m_freem(info->mrep);
798                 info->mrep = NULL;
799         }
800         if (*errorp && (!(nfsd->nd_flag & ND_NFSV3) || *errorp == EBADRPC)) {
801                 *errorp = 0;
802                 return(-1);
803         }
804         return(0);
805 }
806
807 void
808 nfsm_writereply(nfsm_info_t info,
809                 struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
810                 int error, int siz)
811 {
812         nfsd->nd_repstat = error;
813         if (error && !(info->v3))
814                 siz = 0;
815         nfs_rephead(siz, nfsd, slp, error, &info->mreq, &info->mb, &info->bpos);
816 }
817
818 /*
819  * Caller is expected to abort if a non-zero error is returned.
820  */
821 int
822 nfsm_adv(nfsm_info_t info, int len)
823 {
824         int error;
825         int n;
826
827         n = mtod(info->md, caddr_t) + info->md->m_len - info->dpos;
828         if (n >= len) {
829                 info->dpos += len;
830                 error = 0;
831         } else if ((error = nfs_adv(&info->md, &info->dpos, len, n)) != 0) {
832                 m_freem(info->mrep);
833                 info->mrep = NULL;
834         }
835         return (error);
836 }
837
838 /*
839  * Caller is expected to abort if a negative length is returned, but also
840  * call nfsm_reply(0) if -2 is returned.
841  *
842  * This function sets *errorp.  Caller should not modify the error code.
843  */
844 int
845 nfsm_srvmtofh(nfsm_info_t info, struct nfsrv_descript *nfsd,
846               fhandle_t *fhp, int *errorp)
847 {
848         u_int32_t *tl;
849         int fhlen;
850
851         if (nfsd->nd_flag & ND_NFSV3) {
852                 tl = nfsm_dissect(info, NFSX_UNSIGNED);
853                 if (tl == NULL) {
854                         *errorp = EBADRPC;
855                         return(-1);
856                 }
857                 fhlen = fxdr_unsigned(int, *tl);
858                 if (fhlen != 0 && fhlen != NFSX_V3FH) {
859                         *errorp = EBADRPC;
860                         return(-2);
861                 }
862         } else {
863                 fhlen = NFSX_V2FH;
864         }
865         if (fhlen != 0) {
866                 tl = nfsm_dissect(info, fhlen);
867                 if (tl == NULL) {
868                         *errorp = EBADRPC;
869                         return(-1);
870                 }
871                 bcopy(tl, fhp, fhlen);
872         } else {
873                 bzero(fhp, NFSX_V3FH);
874         }
875         return(0);
876 }
877
878 void *
879 _nfsm_clget(nfsm_info_t info, struct mbuf *mp1, struct mbuf *mp2,
880            char *bp, char *be)
881 {
882         u_int32_t *tl;
883
884         if (bp >= be) {
885                 if (mp1 == info->mb)
886                         mp1->m_len += bp - info->bpos;
887                 mp1 = m_getcl(MB_WAIT, MT_DATA, 0);
888                 mp1->m_len = MCLBYTES;
889                 mp2->m_next = mp1;
890                 mp2 = mp1;
891                 bp = mtod(mp1, caddr_t);
892                 be = bp + mp1->m_len;
893         }
894         tl = (u_int32_t *)bp;
895         return (tl);
896 }
897
898 int
899 nfsm_srvsattr(nfsm_info_t info, struct vattr *vap)
900 {
901         u_int32_t *tl;
902         int error = 0;
903
904         NULLOUT(tl = nfsm_dissect(info, NFSX_UNSIGNED));
905         if (*tl == nfs_true) {
906                 NULLOUT(tl = nfsm_dissect(info, NFSX_UNSIGNED));
907                 vap->va_mode = nfstov_mode(*tl);
908         }
909         NULLOUT(tl = nfsm_dissect(info, NFSX_UNSIGNED));
910         if (*tl == nfs_true) {
911                 NULLOUT(tl = nfsm_dissect(info, NFSX_UNSIGNED));
912                 vap->va_uid = fxdr_unsigned(uid_t, *tl);
913         }
914         NULLOUT(tl = nfsm_dissect(info, NFSX_UNSIGNED));
915         if (*tl == nfs_true) {
916                 NULLOUT(tl = nfsm_dissect(info, NFSX_UNSIGNED));
917                 vap->va_gid = fxdr_unsigned(gid_t, *tl);
918         }
919         NULLOUT(tl = nfsm_dissect(info, NFSX_UNSIGNED));
920         if (*tl == nfs_true) {
921                 NULLOUT(tl = nfsm_dissect(info, 2 * NFSX_UNSIGNED));
922                 vap->va_size = fxdr_hyper(tl);
923         }
924         NULLOUT(tl = nfsm_dissect(info, NFSX_UNSIGNED));
925         switch (fxdr_unsigned(int, *tl)) {
926         case NFSV3SATTRTIME_TOCLIENT:
927                 NULLOUT(tl = nfsm_dissect(info, 2 * NFSX_UNSIGNED));
928                 fxdr_nfsv3time(tl, &vap->va_atime);
929                 break;
930         case NFSV3SATTRTIME_TOSERVER:
931                 getnanotime(&vap->va_atime);
932                 break;
933         };
934         NULLOUT(tl = nfsm_dissect(info, NFSX_UNSIGNED));
935         switch (fxdr_unsigned(int, *tl)) {
936         case NFSV3SATTRTIME_TOCLIENT:
937                 NULLOUT(tl = nfsm_dissect(info, 2 * NFSX_UNSIGNED));
938                 fxdr_nfsv3time(tl, &vap->va_mtime);
939                 break;
940         case NFSV3SATTRTIME_TOSERVER:
941                 getnanotime(&vap->va_mtime);
942                 break;
943         }
944 nfsmout:
945         return (error);
946 }
947
948 /*
949  * copies mbuf chain to the uio scatter/gather list
950  */
951 int
952 nfsm_mbuftouio(struct mbuf **mrep, struct uio *uiop, int siz, caddr_t *dpos)
953 {
954         char *mbufcp, *uiocp;
955         int xfer, left, len;
956         struct mbuf *mp;
957         long uiosiz, rem;
958         int error = 0;
959
960         mp = *mrep;
961         mbufcp = *dpos;
962         len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
963         rem = nfsm_rndup(siz)-siz;
964         while (siz > 0) {
965                 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
966                         return (EFBIG);
967                 left = uiop->uio_iov->iov_len;
968                 uiocp = uiop->uio_iov->iov_base;
969                 if (left > siz)
970                         left = siz;
971                 uiosiz = left;
972                 while (left > 0) {
973                         while (len == 0) {
974                                 mp = mp->m_next;
975                                 if (mp == NULL)
976                                         return (EBADRPC);
977                                 mbufcp = mtod(mp, caddr_t);
978                                 len = mp->m_len;
979                         }
980                         xfer = (left > len) ? len : left;
981 #ifdef notdef
982                         /* Not Yet.. */
983                         if (uiop->uio_iov->iov_op != NULL)
984                                 (*(uiop->uio_iov->iov_op))
985                                 (mbufcp, uiocp, xfer);
986                         else
987 #endif
988                         if (uiop->uio_segflg == UIO_SYSSPACE)
989                                 bcopy(mbufcp, uiocp, xfer);
990                         else
991                                 copyout(mbufcp, uiocp, xfer);
992                         left -= xfer;
993                         len -= xfer;
994                         mbufcp += xfer;
995                         uiocp += xfer;
996                         uiop->uio_offset += xfer;
997                         uiop->uio_resid -= xfer;
998                 }
999                 if (uiop->uio_iov->iov_len <= siz) {
1000                         uiop->uio_iovcnt--;
1001                         uiop->uio_iov++;
1002                 } else {
1003                         uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + uiosiz;
1004                         uiop->uio_iov->iov_len -= uiosiz;
1005                 }
1006                 siz -= uiosiz;
1007         }
1008         *dpos = mbufcp;
1009         *mrep = mp;
1010         if (rem > 0) {
1011                 if (len < rem)
1012                         error = nfs_adv(mrep, dpos, rem, len);
1013                 else
1014                         *dpos += rem;
1015         }
1016         return (error);
1017 }
1018
1019 /*
1020  * copies a uio scatter/gather list to an mbuf chain.
1021  * NOTE: can ony handle iovcnt == 1
1022  */
1023 int
1024 nfsm_uiotombuf(struct uio *uiop, struct mbuf **mq, int siz, caddr_t *bpos)
1025 {
1026         char *uiocp;
1027         struct mbuf *mp, *mp2;
1028         int xfer, left, mlen;
1029         int uiosiz, rem;
1030         boolean_t getcluster;
1031         char *cp;
1032
1033 #ifdef DIAGNOSTIC
1034         if (uiop->uio_iovcnt != 1)
1035                 panic("nfsm_uiotombuf: iovcnt != 1");
1036 #endif
1037
1038         if (siz >= MINCLSIZE)
1039                 getcluster = TRUE;
1040         else
1041                 getcluster = FALSE;
1042         rem = nfsm_rndup(siz) - siz;
1043         mp = mp2 = *mq;
1044         while (siz > 0) {
1045                 left = uiop->uio_iov->iov_len;
1046                 uiocp = uiop->uio_iov->iov_base;
1047                 if (left > siz)
1048                         left = siz;
1049                 uiosiz = left;
1050                 while (left > 0) {
1051                         mlen = M_TRAILINGSPACE(mp);
1052                         if (mlen == 0) {
1053                                 if (getcluster)
1054                                         mp = m_getcl(MB_WAIT, MT_DATA, 0);
1055                                 else
1056                                         mp = m_get(MB_WAIT, MT_DATA);
1057                                 mp->m_len = 0;
1058                                 mp2->m_next = mp;
1059                                 mp2 = mp;
1060                                 mlen = M_TRAILINGSPACE(mp);
1061                         }
1062                         xfer = (left > mlen) ? mlen : left;
1063 #ifdef notdef
1064                         /* Not Yet.. */
1065                         if (uiop->uio_iov->iov_op != NULL)
1066                                 (*(uiop->uio_iov->iov_op))
1067                                 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
1068                         else
1069 #endif
1070                         if (uiop->uio_segflg == UIO_SYSSPACE)
1071                                 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
1072                         else
1073                                 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
1074                         mp->m_len += xfer;
1075                         left -= xfer;
1076                         uiocp += xfer;
1077                         uiop->uio_offset += xfer;
1078                         uiop->uio_resid -= xfer;
1079                 }
1080                 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + uiosiz;
1081                 uiop->uio_iov->iov_len -= uiosiz;
1082                 siz -= uiosiz;
1083         }
1084         if (rem > 0) {
1085                 if (rem > M_TRAILINGSPACE(mp)) {
1086                         MGET(mp, MB_WAIT, MT_DATA);
1087                         mp->m_len = 0;
1088                         mp2->m_next = mp;
1089                 }
1090                 cp = mtod(mp, caddr_t)+mp->m_len;
1091                 for (left = 0; left < rem; left++)
1092                         *cp++ = '\0';
1093                 mp->m_len += rem;
1094                 *bpos = cp;
1095         } else
1096                 *bpos = mtod(mp, caddr_t)+mp->m_len;
1097         *mq = mp;
1098         return (0);
1099 }
1100
1101 /*
1102  * Help break down an mbuf chain by setting the first siz bytes contiguous
1103  * pointed to by returned val.
1104  * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
1105  * cases. (The macros use the vars. dpos and dpos2)
1106  */
1107 int
1108 nfsm_disct(struct mbuf **mdp, caddr_t *dposp, int siz, int left, caddr_t *cp2)
1109 {
1110         struct mbuf *mp, *mp2;
1111         int siz2, xfer;
1112         caddr_t p;
1113
1114         mp = *mdp;
1115         while (left == 0) {
1116                 *mdp = mp = mp->m_next;
1117                 if (mp == NULL)
1118                         return (EBADRPC);
1119                 left = mp->m_len;
1120                 *dposp = mtod(mp, caddr_t);
1121         }
1122         if (left >= siz) {
1123                 *cp2 = *dposp;
1124                 *dposp += siz;
1125         } else if (mp->m_next == NULL) {
1126                 return (EBADRPC);
1127         } else if (siz > MHLEN) {
1128                 panic("nfs S too big");
1129         } else {
1130                 MGET(mp2, MB_WAIT, MT_DATA);
1131                 mp2->m_next = mp->m_next;
1132                 mp->m_next = mp2;
1133                 mp->m_len -= left;
1134                 mp = mp2;
1135                 *cp2 = p = mtod(mp, caddr_t);
1136                 bcopy(*dposp, p, left);         /* Copy what was left */
1137                 siz2 = siz-left;
1138                 p += left;
1139                 mp2 = mp->m_next;
1140                 /* Loop around copying up the siz2 bytes */
1141                 while (siz2 > 0) {
1142                         if (mp2 == NULL)
1143                                 return (EBADRPC);
1144                         xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
1145                         if (xfer > 0) {
1146                                 bcopy(mtod(mp2, caddr_t), p, xfer);
1147                                 mp2->m_len -= xfer;
1148                                 mp2->m_data += xfer;
1149                                 p += xfer;
1150                                 siz2 -= xfer;
1151                         }
1152                         if (siz2 > 0)
1153                                 mp2 = mp2->m_next;
1154                 }
1155                 mp->m_len = siz;
1156                 *mdp = mp2;
1157                 *dposp = mtod(mp2, caddr_t);
1158         }
1159         return (0);
1160 }
1161
1162 /*
1163  * Advance the position in the mbuf chain.
1164  */
1165 int
1166 nfs_adv(struct mbuf **mdp, caddr_t *dposp, int offs, int left)
1167 {
1168         struct mbuf *m;
1169         int s;
1170
1171         m = *mdp;
1172         s = left;
1173         while (s < offs) {
1174                 offs -= s;
1175                 m = m->m_next;
1176                 if (m == NULL)
1177                         return (EBADRPC);
1178                 s = m->m_len;
1179         }
1180         *mdp = m;
1181         *dposp = mtod(m, caddr_t)+offs;
1182         return (0);
1183 }
1184
1185 /*
1186  * Copy a string into mbufs for the hard cases...
1187  */
1188 int
1189 nfsm_strtmbuf(struct mbuf **mb, char **bpos, const char *cp, long siz)
1190 {
1191         struct mbuf *m1 = NULL, *m2;
1192         long left, xfer, len, tlen;
1193         u_int32_t *tl;
1194         int putsize;
1195
1196         putsize = 1;
1197         m2 = *mb;
1198         left = M_TRAILINGSPACE(m2);
1199         if (left > 0) {
1200                 tl = ((u_int32_t *)(*bpos));
1201                 *tl++ = txdr_unsigned(siz);
1202                 putsize = 0;
1203                 left -= NFSX_UNSIGNED;
1204                 m2->m_len += NFSX_UNSIGNED;
1205                 if (left > 0) {
1206                         bcopy(cp, (caddr_t) tl, left);
1207                         siz -= left;
1208                         cp += left;
1209                         m2->m_len += left;
1210                         left = 0;
1211                 }
1212         }
1213         /* Loop around adding mbufs */
1214         while (siz > 0) {
1215                 int msize;
1216
1217                 m1 = m_getl(siz, MB_WAIT, MT_DATA, 0, &msize);
1218                 m1->m_len = msize;
1219                 m2->m_next = m1;
1220                 m2 = m1;
1221                 tl = mtod(m1, u_int32_t *);
1222                 tlen = 0;
1223                 if (putsize) {
1224                         *tl++ = txdr_unsigned(siz);
1225                         m1->m_len -= NFSX_UNSIGNED;
1226                         tlen = NFSX_UNSIGNED;
1227                         putsize = 0;
1228                 }
1229                 if (siz < m1->m_len) {
1230                         len = nfsm_rndup(siz);
1231                         xfer = siz;
1232                         if (xfer < len)
1233                                 *(tl+(xfer>>2)) = 0;
1234                 } else {
1235                         xfer = len = m1->m_len;
1236                 }
1237                 bcopy(cp, (caddr_t) tl, xfer);
1238                 m1->m_len = len+tlen;
1239                 siz -= xfer;
1240                 cp += xfer;
1241         }
1242         *mb = m1;
1243         *bpos = mtod(m1, caddr_t)+m1->m_len;
1244         return (0);
1245 }
1246
1247 /*
1248  * A fiddled version of m_adj() that ensures null fill to a long
1249  * boundary and only trims off the back end
1250  */
1251 void
1252 nfsm_adj(struct mbuf *mp, int len, int nul)
1253 {
1254         struct mbuf *m;
1255         int count, i;
1256         char *cp;
1257
1258         /*
1259          * Trim from tail.  Scan the mbuf chain,
1260          * calculating its length and finding the last mbuf.
1261          * If the adjustment only affects this mbuf, then just
1262          * adjust and return.  Otherwise, rescan and truncate
1263          * after the remaining size.
1264          */
1265         count = 0;
1266         m = mp;
1267         for (;;) {
1268                 count += m->m_len;
1269                 if (m->m_next == NULL)
1270                         break;
1271                 m = m->m_next;
1272         }
1273         if (m->m_len > len) {
1274                 m->m_len -= len;
1275                 if (nul > 0) {
1276                         cp = mtod(m, caddr_t)+m->m_len-nul;
1277                         for (i = 0; i < nul; i++)
1278                                 *cp++ = '\0';
1279                 }
1280                 return;
1281         }
1282         count -= len;
1283         if (count < 0)
1284                 count = 0;
1285         /*
1286          * Correct length for chain is "count".
1287          * Find the mbuf with last data, adjust its length,
1288          * and toss data from remaining mbufs on chain.
1289          */
1290         for (m = mp; m; m = m->m_next) {
1291                 if (m->m_len >= count) {
1292                         m->m_len = count;
1293                         if (nul > 0) {
1294                                 cp = mtod(m, caddr_t)+m->m_len-nul;
1295                                 for (i = 0; i < nul; i++)
1296                                         *cp++ = '\0';
1297                         }
1298                         break;
1299                 }
1300                 count -= m->m_len;
1301         }
1302         for (m = m->m_next;m;m = m->m_next)
1303                 m->m_len = 0;
1304 }
1305
1306 /*
1307  * Make these functions instead of macros, so that the kernel text size
1308  * doesn't get too big...
1309  */
1310 void
1311 nfsm_srvwcc_data(nfsm_info_t info, struct nfsrv_descript *nfsd,
1312                  int before_ret, struct vattr *before_vap,
1313                  int after_ret, struct vattr *after_vap)
1314 {
1315         u_int32_t *tl;
1316
1317         /*
1318          * before_ret is 0 if before_vap is valid, non-zero if it isn't.
1319          */
1320         if (before_ret) {
1321                 tl = nfsm_build(info, NFSX_UNSIGNED);
1322                 *tl = nfs_false;
1323         } else {
1324                 tl = nfsm_build(info, 7 * NFSX_UNSIGNED);
1325                 *tl++ = nfs_true;
1326                 txdr_hyper(before_vap->va_size, tl);
1327                 tl += 2;
1328                 txdr_nfsv3time(&(before_vap->va_mtime), tl);
1329                 tl += 2;
1330                 txdr_nfsv3time(&(before_vap->va_ctime), tl);
1331         }
1332         nfsm_srvpostop_attr(info, nfsd, after_ret, after_vap);
1333 }
1334
1335 void
1336 nfsm_srvpostop_attr(nfsm_info_t info, struct nfsrv_descript *nfsd,
1337                    int after_ret, struct vattr *after_vap)
1338 {
1339         struct nfs_fattr *fp;
1340         u_int32_t *tl;
1341
1342         if (after_ret) {
1343                 tl = nfsm_build(info, NFSX_UNSIGNED);
1344                 *tl = nfs_false;
1345         } else {
1346                 tl = nfsm_build(info, NFSX_UNSIGNED + NFSX_V3FATTR);
1347                 *tl++ = nfs_true;
1348                 fp = (struct nfs_fattr *)tl;
1349                 nfsm_srvfattr(nfsd, after_vap, fp);
1350         }
1351 }
1352
1353 void
1354 nfsm_srvfattr(struct nfsrv_descript *nfsd, struct vattr *vap,
1355               struct nfs_fattr *fp)
1356 {
1357         /*
1358          * NFS seems to truncate nlink to 16 bits, don't let it overflow.
1359          */
1360         if (vap->va_nlink > 65535)
1361                 fp->fa_nlink = 65535;
1362         else
1363                 fp->fa_nlink = txdr_unsigned(vap->va_nlink);
1364         fp->fa_uid = txdr_unsigned(vap->va_uid);
1365         fp->fa_gid = txdr_unsigned(vap->va_gid);
1366         if (nfsd->nd_flag & ND_NFSV3) {
1367                 fp->fa_type = vtonfsv3_type(vap->va_type);
1368                 fp->fa_mode = vtonfsv3_mode(vap->va_mode);
1369                 txdr_hyper(vap->va_size, &fp->fa3_size);
1370                 txdr_hyper(vap->va_bytes, &fp->fa3_used);
1371                 fp->fa3_rdev.specdata1 = txdr_unsigned(vap->va_rmajor);
1372                 fp->fa3_rdev.specdata2 = txdr_unsigned(vap->va_rminor);
1373                 fp->fa3_fsid.nfsuquad[0] = 0;
1374                 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
1375                 txdr_hyper(vap->va_fileid, &fp->fa3_fileid);
1376                 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
1377                 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
1378                 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
1379         } else {
1380                 fp->fa_type = vtonfsv2_type(vap->va_type);
1381                 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1382                 fp->fa2_size = txdr_unsigned(vap->va_size);
1383                 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
1384                 if (vap->va_type == VFIFO)
1385                         fp->fa2_rdev = 0xffffffff;
1386                 else
1387                         fp->fa2_rdev = txdr_unsigned(makeudev(vap->va_rmajor, vap->va_rminor));
1388                 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
1389                 fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
1390                 fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
1391                 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
1392                 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
1393                 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
1394         }
1395 }