]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/nfsclient/nfs_subs.c
- Lock access to the buf lists.
[FreeBSD/FreeBSD.git] / sys / nfsclient / nfs_subs.c
1 /*
2  * Copyright (c) 1989, 1993
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_subs.c  8.8 (Berkeley) 5/22/95
37  */
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41
42 /*
43  * These functions support the macros and help fiddle mbuf chains for
44  * the nfs op functions. They do things like create the rpc header and
45  * copy data between mbuf chains and uio lists.
46  */
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/bio.h>
52 #include <sys/buf.h>
53 #include <sys/proc.h>
54 #include <sys/mount.h>
55 #include <sys/vnode.h>
56 #include <sys/namei.h>
57 #include <sys/mbuf.h>
58 #include <sys/socket.h>
59 #include <sys/stat.h>
60 #include <sys/malloc.h>
61 #include <sys/sysent.h>
62 #include <sys/syscall.h>
63 #include <sys/sysproto.h>
64
65 #include <vm/vm.h>
66 #include <vm/vm_object.h>
67 #include <vm/vm_extern.h>
68 #include <vm/uma.h>
69
70 #include <nfs/rpcv2.h>
71 #include <nfs/nfsproto.h>
72 #include <nfsclient/nfs.h>
73 #include <nfsclient/nfsnode.h>
74 #include <nfs/xdr_subs.h>
75 #include <nfsclient/nfsm_subs.h>
76 #include <nfsclient/nfsmount.h>
77
78 #include <netinet/in.h>
79
80 /*
81  * Data items converted to xdr at startup, since they are constant
82  * This is kinda hokey, but may save a little time doing byte swaps
83  */
84 u_int32_t       nfs_xdrneg1;
85 u_int32_t       rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
86                     rpc_mismatch, rpc_auth_unix, rpc_msgaccepted;
87 u_int32_t       nfs_true, nfs_false;
88
89 /* And other global data */
90 static u_int32_t nfs_xid = 0;
91 static enum vtype nv2tov_type[8]= {
92         VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON,  VNON
93 };
94
95 int             nfs_ticks;
96 int             nfs_pbuf_freecnt = -1;  /* start out unlimited */
97
98 struct nfs_reqq nfs_reqq;
99 struct nfs_bufq nfs_bufq;
100
101 static int      nfs_prev_nfsclnt_sy_narg;
102 static sy_call_t *nfs_prev_nfsclnt_sy_call;
103
104 /*
105  * and the reverse mapping from generic to Version 2 procedure numbers
106  */
107 int nfsv2_procid[NFS_NPROCS] = {
108         NFSV2PROC_NULL,
109         NFSV2PROC_GETATTR,
110         NFSV2PROC_SETATTR,
111         NFSV2PROC_LOOKUP,
112         NFSV2PROC_NOOP,
113         NFSV2PROC_READLINK,
114         NFSV2PROC_READ,
115         NFSV2PROC_WRITE,
116         NFSV2PROC_CREATE,
117         NFSV2PROC_MKDIR,
118         NFSV2PROC_SYMLINK,
119         NFSV2PROC_CREATE,
120         NFSV2PROC_REMOVE,
121         NFSV2PROC_RMDIR,
122         NFSV2PROC_RENAME,
123         NFSV2PROC_LINK,
124         NFSV2PROC_READDIR,
125         NFSV2PROC_NOOP,
126         NFSV2PROC_STATFS,
127         NFSV2PROC_NOOP,
128         NFSV2PROC_NOOP,
129         NFSV2PROC_NOOP,
130         NFSV2PROC_NOOP,
131 };
132
133 LIST_HEAD(nfsnodehashhead, nfsnode);
134
135 /*
136  * Create the header for an rpc request packet
137  * The hsiz is the size of the rest of the nfs request header.
138  * (just used to decide if a cluster is a good idea)
139  */
140 struct mbuf *
141 nfsm_reqhead(struct vnode *vp, u_long procid, int hsiz)
142 {
143         struct mbuf *mb;
144
145         MGET(mb, M_TRYWAIT, MT_DATA);
146         if (hsiz >= MINCLSIZE)
147                 MCLGET(mb, M_TRYWAIT);
148         mb->m_len = 0;
149         return (mb);
150 }
151
152 /*
153  * Build the RPC header and fill in the authorization info.
154  * The authorization string argument is only used when the credentials
155  * come from outside of the kernel.
156  * Returns the head of the mbuf list.
157  */
158 struct mbuf *
159 nfsm_rpchead(struct ucred *cr, int nmflag, int procid, int auth_type,
160     int auth_len, struct mbuf *mrest, int mrest_len, struct mbuf **mbp,
161     u_int32_t *xidp)
162 {
163         struct mbuf *mb;
164         u_int32_t *tl;
165         caddr_t bpos;
166         int i;
167         struct mbuf *mreq;
168         int grpsiz, authsiz;
169
170         authsiz = nfsm_rndup(auth_len);
171         MGETHDR(mb, M_TRYWAIT, MT_DATA);
172         if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
173                 MCLGET(mb, M_TRYWAIT);
174         } else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
175                 MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
176         } else {
177                 MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
178         }
179         mb->m_len = 0;
180         mreq = mb;
181         bpos = mtod(mb, caddr_t);
182
183         /*
184          * First the RPC header.
185          */
186         tl = nfsm_build(u_int32_t *, 8 * NFSX_UNSIGNED);
187
188         /* Get a pretty random xid to start with */
189         if (!nfs_xid)
190                 nfs_xid = random();
191         /*
192          * Skip zero xid if it should ever happen.
193          */
194         if (++nfs_xid == 0)
195                 nfs_xid++;
196
197         *tl++ = *xidp = txdr_unsigned(nfs_xid);
198         *tl++ = rpc_call;
199         *tl++ = rpc_vers;
200         *tl++ = txdr_unsigned(NFS_PROG);
201         if (nmflag & NFSMNT_NFSV3) {
202                 *tl++ = txdr_unsigned(NFS_VER3);
203                 *tl++ = txdr_unsigned(procid);
204         } else {
205                 *tl++ = txdr_unsigned(NFS_VER2);
206                 *tl++ = txdr_unsigned(nfsv2_procid[procid]);
207         }
208
209         /*
210          * And then the authorization cred.
211          */
212         *tl++ = txdr_unsigned(auth_type);
213         *tl = txdr_unsigned(authsiz);
214         switch (auth_type) {
215         case RPCAUTH_UNIX:
216                 tl = nfsm_build(u_int32_t *, auth_len);
217                 *tl++ = 0;              /* stamp ?? */
218                 *tl++ = 0;              /* NULL hostname */
219                 *tl++ = txdr_unsigned(cr->cr_uid);
220                 *tl++ = txdr_unsigned(cr->cr_groups[0]);
221                 grpsiz = (auth_len >> 2) - 5;
222                 *tl++ = txdr_unsigned(grpsiz);
223                 for (i = 1; i <= grpsiz; i++)
224                         *tl++ = txdr_unsigned(cr->cr_groups[i]);
225                 break;
226         }
227
228         /*
229          * And the verifier...
230          */
231         tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
232         *tl++ = txdr_unsigned(RPCAUTH_NULL);
233         *tl = 0;
234         mb->m_next = mrest;
235         mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
236         mreq->m_pkthdr.rcvif = NULL;
237         *mbp = mb;
238         return (mreq);
239 }
240
241 /*
242  * copies a uio scatter/gather list to an mbuf chain.
243  * NOTE: can ony handle iovcnt == 1
244  */
245 int
246 nfsm_uiotombuf(struct uio *uiop, struct mbuf **mq, int siz, caddr_t *bpos)
247 {
248         char *uiocp;
249         struct mbuf *mp, *mp2;
250         int xfer, left, mlen;
251         int uiosiz, clflg, rem;
252         char *cp;
253
254 #ifdef DIAGNOSTIC
255         if (uiop->uio_iovcnt != 1)
256                 panic("nfsm_uiotombuf: iovcnt != 1");
257 #endif
258
259         if (siz > MLEN)         /* or should it >= MCLBYTES ?? */
260                 clflg = 1;
261         else
262                 clflg = 0;
263         rem = nfsm_rndup(siz)-siz;
264         mp = mp2 = *mq;
265         while (siz > 0) {
266                 left = uiop->uio_iov->iov_len;
267                 uiocp = uiop->uio_iov->iov_base;
268                 if (left > siz)
269                         left = siz;
270                 uiosiz = left;
271                 while (left > 0) {
272                         mlen = M_TRAILINGSPACE(mp);
273                         if (mlen == 0) {
274                                 MGET(mp, M_TRYWAIT, MT_DATA);
275                                 if (clflg)
276                                         MCLGET(mp, M_TRYWAIT);
277                                 mp->m_len = 0;
278                                 mp2->m_next = mp;
279                                 mp2 = mp;
280                                 mlen = M_TRAILINGSPACE(mp);
281                         }
282                         xfer = (left > mlen) ? mlen : left;
283 #ifdef notdef
284                         /* Not Yet.. */
285                         if (uiop->uio_iov->iov_op != NULL)
286                                 (*(uiop->uio_iov->iov_op))
287                                 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
288                         else
289 #endif
290                         if (uiop->uio_segflg == UIO_SYSSPACE)
291                                 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
292                         else
293                                 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
294                         mp->m_len += xfer;
295                         left -= xfer;
296                         uiocp += xfer;
297                         uiop->uio_offset += xfer;
298                         uiop->uio_resid -= xfer;
299                 }
300                 uiop->uio_iov->iov_base += uiosiz;
301                 uiop->uio_iov->iov_len -= uiosiz;
302                 siz -= uiosiz;
303         }
304         if (rem > 0) {
305                 if (rem > M_TRAILINGSPACE(mp)) {
306                         MGET(mp, M_TRYWAIT, MT_DATA);
307                         mp->m_len = 0;
308                         mp2->m_next = mp;
309                 }
310                 cp = mtod(mp, caddr_t)+mp->m_len;
311                 for (left = 0; left < rem; left++)
312                         *cp++ = '\0';
313                 mp->m_len += rem;
314                 *bpos = cp;
315         } else
316                 *bpos = mtod(mp, caddr_t)+mp->m_len;
317         *mq = mp;
318         return (0);
319 }
320
321 /*
322  * Copy a string into mbufs for the hard cases...
323  */
324 int
325 nfsm_strtmbuf(struct mbuf **mb, char **bpos, const char *cp, long siz)
326 {
327         struct mbuf *m1 = NULL, *m2;
328         long left, xfer, len, tlen;
329         u_int32_t *tl;
330         int putsize;
331
332         putsize = 1;
333         m2 = *mb;
334         left = M_TRAILINGSPACE(m2);
335         if (left > 0) {
336                 tl = ((u_int32_t *)(*bpos));
337                 *tl++ = txdr_unsigned(siz);
338                 putsize = 0;
339                 left -= NFSX_UNSIGNED;
340                 m2->m_len += NFSX_UNSIGNED;
341                 if (left > 0) {
342                         bcopy(cp, (caddr_t) tl, left);
343                         siz -= left;
344                         cp += left;
345                         m2->m_len += left;
346                         left = 0;
347                 }
348         }
349         /* Loop around adding mbufs */
350         while (siz > 0) {
351                 MGET(m1, M_TRYWAIT, MT_DATA);
352                 if (siz > MLEN)
353                         MCLGET(m1, M_TRYWAIT);
354                 m1->m_len = NFSMSIZ(m1);
355                 m2->m_next = m1;
356                 m2 = m1;
357                 tl = mtod(m1, u_int32_t *);
358                 tlen = 0;
359                 if (putsize) {
360                         *tl++ = txdr_unsigned(siz);
361                         m1->m_len -= NFSX_UNSIGNED;
362                         tlen = NFSX_UNSIGNED;
363                         putsize = 0;
364                 }
365                 if (siz < m1->m_len) {
366                         len = nfsm_rndup(siz);
367                         xfer = siz;
368                         if (xfer < len)
369                                 *(tl+(xfer>>2)) = 0;
370                 } else {
371                         xfer = len = m1->m_len;
372                 }
373                 bcopy(cp, (caddr_t) tl, xfer);
374                 m1->m_len = len+tlen;
375                 siz -= xfer;
376                 cp += xfer;
377         }
378         *mb = m1;
379         *bpos = mtod(m1, caddr_t)+m1->m_len;
380         return (0);
381 }
382
383 /*
384  * Called once to initialize data structures...
385  */
386 int
387 nfs_init(struct vfsconf *vfsp)
388 {
389         int i;
390
391         nfsmount_zone = uma_zcreate("NFSMOUNT", sizeof(struct nfsmount),
392             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
393         rpc_vers = txdr_unsigned(RPC_VER2);
394         rpc_call = txdr_unsigned(RPC_CALL);
395         rpc_reply = txdr_unsigned(RPC_REPLY);
396         rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
397         rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
398         rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
399         rpc_autherr = txdr_unsigned(RPC_AUTHERR);
400         rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
401         nfs_true = txdr_unsigned(TRUE);
402         nfs_false = txdr_unsigned(FALSE);
403         nfs_xdrneg1 = txdr_unsigned(-1);
404         nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
405         if (nfs_ticks < 1)
406                 nfs_ticks = 1;
407         /* Ensure async daemons disabled */
408         for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
409                 nfs_iodwant[i] = NULL;
410                 nfs_iodmount[i] = NULL;
411         }
412         nfs_nhinit();                   /* Init the nfsnode table */
413
414         /*
415          * Initialize reply list and start timer
416          */
417         TAILQ_INIT(&nfs_reqq);
418
419         nfs_timer(0);
420
421         nfs_prev_nfsclnt_sy_narg = sysent[SYS_nfsclnt].sy_narg;
422         sysent[SYS_nfsclnt].sy_narg = 2;
423         nfs_prev_nfsclnt_sy_call = sysent[SYS_nfsclnt].sy_call;
424         sysent[SYS_nfsclnt].sy_call = (sy_call_t *)nfsclnt;
425
426         nfs_pbuf_freecnt = nswbuf / 2 + 1;
427
428         return (0);
429 }
430
431 int
432 nfs_uninit(struct vfsconf *vfsp)
433 {
434
435         untimeout(nfs_timer, (void *)NULL, nfs_timer_handle);
436         sysent[SYS_nfsclnt].sy_narg = nfs_prev_nfsclnt_sy_narg;
437         sysent[SYS_nfsclnt].sy_call = nfs_prev_nfsclnt_sy_call;
438         return (0);
439 }
440
441 /*
442  * Attribute cache routines.
443  * nfs_loadattrcache() - loads or updates the cache contents from attributes
444  *      that are on the mbuf list
445  * nfs_getattrcache() - returns valid attributes if found in cache, returns
446  *      error otherwise
447  */
448
449 /*
450  * Load the attribute cache (that lives in the nfsnode entry) with
451  * the values on the mbuf list and
452  * Iff vap not NULL
453  *    copy the attributes to *vaper
454  */
455 int
456 nfs_loadattrcache(struct vnode **vpp, struct mbuf **mdp, caddr_t *dposp,
457     struct vattr *vaper, int dontshrink)
458 {
459         struct vnode *vp = *vpp;
460         struct vattr *vap;
461         struct nfs_fattr *fp;
462         struct nfsnode *np;
463         int32_t t1;
464         caddr_t cp2;
465         int rdev;
466         struct mbuf *md;
467         enum vtype vtyp;
468         u_short vmode;
469         struct timespec mtime;
470         int v3 = NFS_ISV3(vp);
471
472         md = *mdp;
473         t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
474         cp2 = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1);
475         if (cp2 == NULL)
476                 return EBADRPC;
477         fp = (struct nfs_fattr *)cp2;
478         if (v3) {
479                 vtyp = nfsv3tov_type(fp->fa_type);
480                 vmode = fxdr_unsigned(u_short, fp->fa_mode);
481                 rdev = makeudev(fxdr_unsigned(int, fp->fa3_rdev.specdata1),
482                         fxdr_unsigned(int, fp->fa3_rdev.specdata2));
483                 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
484         } else {
485                 vtyp = nfsv2tov_type(fp->fa_type);
486                 vmode = fxdr_unsigned(u_short, fp->fa_mode);
487                 /*
488                  * XXX
489                  *
490                  * The duplicate information returned in fa_type and fa_mode
491                  * is an ambiguity in the NFS version 2 protocol.
492                  *
493                  * VREG should be taken literally as a regular file.  If a
494                  * server intents to return some type information differently
495                  * in the upper bits of the mode field (e.g. for sockets, or
496                  * FIFOs), NFSv2 mandates fa_type to be VNON.  Anyway, we
497                  * leave the examination of the mode bits even in the VREG
498                  * case to avoid breakage for bogus servers, but we make sure
499                  * that there are actually type bits set in the upper part of
500                  * fa_mode (and failing that, trust the va_type field).
501                  *
502                  * NFSv3 cleared the issue, and requires fa_mode to not
503                  * contain any type information (while also introduing sockets
504                  * and FIFOs for fa_type).
505                  */
506                 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0))
507                         vtyp = IFTOVT(vmode);
508                 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
509                 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
510
511                 /*
512                  * Really ugly NFSv2 kludge.
513                  */
514                 if (vtyp == VCHR && rdev == 0xffffffff)
515                         vtyp = VFIFO;
516         }
517
518         /*
519          * If v_type == VNON it is a new node, so fill in the v_type,
520          * n_mtime fields. Check to see if it represents a special
521          * device, and if so, check for a possible alias. Once the
522          * correct vnode has been obtained, fill in the rest of the
523          * information.
524          */
525         np = VTONFS(vp);
526         if (vp->v_type != vtyp) {
527                 vp->v_type = vtyp;
528                 if (vp->v_type == VFIFO) {
529                         vp->v_op = fifo_nfsv2nodeop_p;
530                 }
531                 if (vp->v_type == VCHR || vp->v_type == VBLK) {
532                         vp->v_op = spec_nfsv2nodeop_p;
533                         vp = addaliasu(vp, rdev);
534                         np->n_vnode = vp;
535                 }
536                 np->n_mtime = mtime.tv_sec;
537         }
538         vap = &np->n_vattr;
539         vap->va_type = vtyp;
540         vap->va_mode = (vmode & 07777);
541         vap->va_rdev = rdev;
542         vap->va_mtime = mtime;
543         vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
544         if (v3) {
545                 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
546                 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
547                 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
548                 vap->va_size = fxdr_hyper(&fp->fa3_size);
549                 vap->va_blocksize = NFS_FABLKSIZE;
550                 vap->va_bytes = fxdr_hyper(&fp->fa3_used);
551                 vap->va_fileid = fxdr_unsigned(int32_t,
552                     fp->fa3_fileid.nfsuquad[1]);
553                 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
554                 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
555                 vap->va_flags = 0;
556                 vap->va_filerev = 0;
557         } else {
558                 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
559                 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
560                 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
561                 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
562                 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
563                 vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks)
564                     * NFS_FABLKSIZE;
565                 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
566                 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
567                 vap->va_flags = 0;
568                 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
569                     fp->fa2_ctime.nfsv2_sec);
570                 vap->va_ctime.tv_nsec = 0;
571                 vap->va_gen = fxdr_unsigned(u_int32_t, fp->fa2_ctime.nfsv2_usec);
572                 vap->va_filerev = 0;
573         }
574         np->n_attrstamp = time_second;
575         if (vap->va_size != np->n_size) {
576                 if (vap->va_type == VREG) {
577                         if (dontshrink && vap->va_size < np->n_size) {
578                                 /*
579                                  * We've been told not to shrink the file;
580                                  * zero np->n_attrstamp to indicate that
581                                  * the attributes are stale.
582                                  */
583                                 vap->va_size = np->n_size;
584                                 np->n_attrstamp = 0;
585                         } else if (np->n_flag & NMODIFIED) {
586                                 if (vap->va_size < np->n_size)
587                                         vap->va_size = np->n_size;
588                                 else
589                                         np->n_size = vap->va_size;
590                         } else {
591                                 np->n_size = vap->va_size;
592                         }
593                         vnode_pager_setsize(vp, np->n_size);
594                 } else {
595                         np->n_size = vap->va_size;
596                 }
597         }
598         if (vaper != NULL) {
599                 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
600                 if (np->n_flag & NCHG) {
601                         if (np->n_flag & NACC)
602                                 vaper->va_atime = np->n_atim;
603                         if (np->n_flag & NUPD)
604                                 vaper->va_mtime = np->n_mtim;
605                 }
606         }
607         return (0);
608 }
609
610 #ifdef NFS_ACDEBUG
611 #include <sys/sysctl.h>
612 SYSCTL_DECL(_vfs_nfs);
613 static int nfs_acdebug;
614 SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
615 #endif
616
617 /*
618  * Check the time stamp
619  * If the cache is valid, copy contents to *vap and return 0
620  * otherwise return an error
621  */
622 int
623 nfs_getattrcache(struct vnode *vp, struct vattr *vaper)
624 {
625         struct nfsnode *np;
626         struct vattr *vap;
627         struct nfsmount *nmp;
628         int timeo;
629
630         np = VTONFS(vp);
631         vap = &np->n_vattr;
632         nmp = VFSTONFS(vp->v_mount);
633         /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
634         timeo = (time_second - np->n_mtime) / 10;
635
636 #ifdef NFS_ACDEBUG
637         if (nfs_acdebug>1)
638                 printf("nfs_getattrcache: initial timeo = %d\n", timeo);
639 #endif
640
641         if (vap->va_type == VDIR) {
642                 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
643                         timeo = nmp->nm_acdirmin;
644                 else if (timeo > nmp->nm_acdirmax)
645                         timeo = nmp->nm_acdirmax;
646         } else {
647                 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
648                         timeo = nmp->nm_acregmin;
649                 else if (timeo > nmp->nm_acregmax)
650                         timeo = nmp->nm_acregmax;
651         }
652
653 #ifdef NFS_ACDEBUG
654         if (nfs_acdebug > 2)
655                 printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
656                         nmp->nm_acregmin, nmp->nm_acregmax,
657                         nmp->nm_acdirmin, nmp->nm_acdirmax);
658
659         if (nfs_acdebug)
660                 printf("nfs_getattrcache: age = %d; final timeo = %d\n",
661                         (time_second - np->n_attrstamp), timeo);
662 #endif
663
664         if ((time_second - np->n_attrstamp) >= timeo) {
665                 nfsstats.attrcache_misses++;
666                 return (ENOENT);
667         }
668         nfsstats.attrcache_hits++;
669         if (vap->va_size != np->n_size) {
670                 if (vap->va_type == VREG) {
671                         if (np->n_flag & NMODIFIED) {
672                                 if (vap->va_size < np->n_size)
673                                         vap->va_size = np->n_size;
674                                 else
675                                         np->n_size = vap->va_size;
676                         } else {
677                                 np->n_size = vap->va_size;
678                         }
679                         vnode_pager_setsize(vp, np->n_size);
680                 } else {
681                         np->n_size = vap->va_size;
682                 }
683         }
684         bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
685         if (np->n_flag & NCHG) {
686                 if (np->n_flag & NACC)
687                         vaper->va_atime = np->n_atim;
688                 if (np->n_flag & NUPD)
689                         vaper->va_mtime = np->n_mtim;
690         }
691         return (0);
692 }
693
694 static nfsuint64 nfs_nullcookie = { { 0, 0 } };
695 /*
696  * This function finds the directory cookie that corresponds to the
697  * logical byte offset given.
698  */
699 nfsuint64 *
700 nfs_getcookie(struct nfsnode *np, off_t off, int add)
701 {
702         struct nfsdmap *dp, *dp2;
703         int pos;
704
705         pos = (uoff_t)off / NFS_DIRBLKSIZ;
706         if (pos == 0 || off < 0) {
707 #ifdef DIAGNOSTIC
708                 if (add)
709                         panic("nfs getcookie add at <= 0");
710 #endif
711                 return (&nfs_nullcookie);
712         }
713         pos--;
714         dp = LIST_FIRST(&np->n_cookies);
715         if (!dp) {
716                 if (add) {
717                         MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
718                                 M_NFSDIROFF, M_WAITOK);
719                         dp->ndm_eocookie = 0;
720                         LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
721                 } else
722                         return (NULL);
723         }
724         while (pos >= NFSNUMCOOKIES) {
725                 pos -= NFSNUMCOOKIES;
726                 if (LIST_NEXT(dp, ndm_list)) {
727                         if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
728                                 pos >= dp->ndm_eocookie)
729                                 return (NULL);
730                         dp = LIST_NEXT(dp, ndm_list);
731                 } else if (add) {
732                         MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
733                                 M_NFSDIROFF, M_WAITOK);
734                         dp2->ndm_eocookie = 0;
735                         LIST_INSERT_AFTER(dp, dp2, ndm_list);
736                         dp = dp2;
737                 } else
738                         return (NULL);
739         }
740         if (pos >= dp->ndm_eocookie) {
741                 if (add)
742                         dp->ndm_eocookie = pos + 1;
743                 else
744                         return (NULL);
745         }
746         return (&dp->ndm_cookies[pos]);
747 }
748
749 /*
750  * Invalidate cached directory information, except for the actual directory
751  * blocks (which are invalidated separately).
752  * Done mainly to avoid the use of stale offset cookies.
753  */
754 void
755 nfs_invaldir(struct vnode *vp)
756 {
757         struct nfsnode *np = VTONFS(vp);
758
759 #ifdef DIAGNOSTIC
760         if (vp->v_type != VDIR)
761                 panic("nfs: invaldir not dir");
762 #endif
763         np->n_direofoffset = 0;
764         np->n_cookieverf.nfsuquad[0] = 0;
765         np->n_cookieverf.nfsuquad[1] = 0;
766         if (LIST_FIRST(&np->n_cookies))
767                 LIST_FIRST(&np->n_cookies)->ndm_eocookie = 0;
768 }
769
770 /*
771  * The write verifier has changed (probably due to a server reboot), so all
772  * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
773  * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
774  * and B_CLUSTEROK flags.  Once done the new write verifier can be set for the
775  * mount point.
776  *
777  * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
778  * writes are not clusterable.
779  */
780 void
781 nfs_clearcommit(struct mount *mp)
782 {
783         struct vnode *vp, *nvp;
784         struct buf *bp, *nbp;
785         int s;
786
787         GIANT_REQUIRED;
788
789         s = splbio();
790         mtx_lock(&mntvnode_mtx);
791 loop:
792         for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) {
793                 if (vp->v_mount != mp)  /* Paranoia */
794                         goto loop;
795                 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
796                 VI_LOCK(vp);
797                 mtx_unlock(&mntvnode_mtx);
798                 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
799                         nbp = TAILQ_NEXT(bp, b_vnbufs);
800                         if (BUF_REFCNT(bp) == 0 &&
801                             (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
802                                 == (B_DELWRI | B_NEEDCOMMIT))
803                                 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
804                 }
805                 VI_UNLOCK(vp);
806                 mtx_lock(&mntvnode_mtx);
807         }
808         mtx_unlock(&mntvnode_mtx);
809         splx(s);
810 }
811
812 /*
813  * Helper functions for former macros.  Some of these should be
814  * moved to their callers.
815  */
816
817 int
818 nfsm_mtofh_xx(struct vnode *d, struct vnode **v, int v3, int *f,
819     struct mbuf **md, caddr_t *dpos)
820 {
821         struct nfsnode *ttnp;
822         struct vnode *ttvp;
823         nfsfh_t *ttfhp;
824         u_int32_t *tl;
825         int ttfhsize;
826         int t1;
827
828         if (v3) {
829                 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
830                 if (tl == NULL)
831                         return EBADRPC;
832                 *f = fxdr_unsigned(int, *tl);
833         } else
834                 *f = 1;
835         if (*f) {
836                 t1 = nfsm_getfh_xx(&ttfhp, &ttfhsize, (v3), md, dpos);
837                 if (t1 != 0)
838                         return t1;
839                 t1 = nfs_nget(d->v_mount, ttfhp, ttfhsize, &ttnp);
840                 if (t1 != 0)
841                         return t1;
842                 *v = NFSTOV(ttnp);
843         }
844         if (v3) {
845                 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
846                 if (tl == NULL)
847                         return EBADRPC;
848                 if (*f)
849                         *f = fxdr_unsigned(int, *tl);
850                 else if (fxdr_unsigned(int, *tl))
851                         nfsm_adv_xx(NFSX_V3FATTR, md, dpos);
852         }
853         if (*f) {
854                 ttvp = *v;
855                 t1 = nfs_loadattrcache(&ttvp, md, dpos, NULL, 0);
856                 if (t1)
857                         return t1;
858                 *v = ttvp;
859         }
860         return 0;
861 }
862
863 int
864 nfsm_getfh_xx(nfsfh_t **f, int *s, int v3, struct mbuf **md, caddr_t *dpos)
865 {
866         u_int32_t *tl;
867
868         if (v3) {
869                 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
870                 if (tl == NULL)
871                         return EBADRPC;
872                 *s = fxdr_unsigned(int, *tl);
873                 if (*s <= 0 || *s > NFSX_V3FHMAX)
874                         return EBADRPC;
875         } else
876                 *s = NFSX_V2FH;
877         *f = nfsm_dissect_xx(nfsm_rndup(*s), md, dpos);
878         if (*f == NULL)
879                 return EBADRPC;
880         else
881                 return 0;
882 }
883
884
885 int
886 nfsm_loadattr_xx(struct vnode **v, struct vattr *va, struct mbuf **md,
887     caddr_t *dpos)
888 {
889         int t1;
890
891         struct vnode *ttvp = *v;
892         t1 = nfs_loadattrcache(&ttvp, md, dpos, va, 0);
893         if (t1 != 0)
894                 return t1;
895         *v = ttvp;
896         return 0;
897 }
898
899 int
900 nfsm_postop_attr_xx(struct vnode **v, int *f, struct mbuf **md,
901     caddr_t *dpos)
902 {
903         u_int32_t *tl;
904         int t1;
905
906         struct vnode *ttvp = *v;
907         tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
908         if (tl == NULL)
909                 return EBADRPC;
910         *f = fxdr_unsigned(int, *tl);
911         if (*f != 0) {
912                 t1 = nfs_loadattrcache(&ttvp, md, dpos, NULL, 1);
913                 if (t1 != 0) {
914                         *f = 0;
915                         return t1;
916                 }
917                 *v = ttvp;
918         }
919         return 0;
920 }
921
922 int
923 nfsm_wcc_data_xx(struct vnode **v, int *f, struct mbuf **md, caddr_t *dpos)
924 {
925         u_int32_t *tl;
926         int ttattrf, ttretf = 0;
927         int t1;
928
929         tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
930         if (tl == NULL)
931                 return EBADRPC;
932         if (*tl == nfs_true) {
933                 tl = nfsm_dissect_xx(6 * NFSX_UNSIGNED, md, dpos);
934                 if (tl == NULL)
935                         return EBADRPC;
936                 if (*f)
937                         ttretf = (VTONFS(*v)->n_mtime ==
938                             fxdr_unsigned(u_int32_t, *(tl + 2)));
939         }
940         t1 = nfsm_postop_attr_xx(v, &ttattrf, md, dpos);
941         if (t1)
942                 return t1;
943         if (*f)
944                 *f = ttretf;
945         else
946                 *f = ttattrf;
947         return 0;
948 }
949
950 int
951 nfsm_strtom_xx(const char *a, int s, int m, struct mbuf **mb, caddr_t *bpos)
952 {
953         u_int32_t *tl;
954         int t1;
955
956         if (s > m)
957                 return ENAMETOOLONG;
958         t1 = nfsm_rndup(s) + NFSX_UNSIGNED;
959         if (t1 <= M_TRAILINGSPACE(*mb)) {
960                 tl = nfsm_build_xx(t1, mb, bpos);
961                 *tl++ = txdr_unsigned(s);
962                 *(tl + ((t1 >> 2) - 2)) = 0;
963                 bcopy(a, tl, s);
964         } else {
965                 t1 = nfsm_strtmbuf(mb, bpos, a, s);
966                 if (t1 != 0)
967                         return t1;
968         }
969         return 0;
970 }
971
972 int
973 nfsm_fhtom_xx(struct vnode *v, int v3, struct mbuf **mb, caddr_t *bpos)
974 {
975         u_int32_t *tl;
976         int t1;
977         caddr_t cp;
978
979         if (v3) {
980                 t1 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED;
981                 if (t1 < M_TRAILINGSPACE(*mb)) {
982                         tl = nfsm_build_xx(t1, mb, bpos);
983                         *tl++ = txdr_unsigned(VTONFS(v)->n_fhsize);
984                         *(tl + ((t1 >> 2) - 2)) = 0;
985                         bcopy(VTONFS(v)->n_fhp, tl, VTONFS(v)->n_fhsize);
986                 } else {
987                         t1 = nfsm_strtmbuf(mb, bpos,
988                             (const char *)VTONFS(v)->n_fhp,
989                             VTONFS(v)->n_fhsize);
990                         if (t1 != 0)
991                                 return t1;
992                 }
993         } else {
994                 cp = nfsm_build_xx(NFSX_V2FH, mb, bpos);
995                 bcopy(VTONFS(v)->n_fhp, cp, NFSX_V2FH);
996         }
997         return 0;
998 }
999
1000 void
1001 nfsm_v3attrbuild_xx(struct vattr *va, int full, struct mbuf **mb,
1002     caddr_t *bpos)
1003 {
1004         u_int32_t *tl;
1005
1006         if (va->va_mode != (mode_t)VNOVAL) {
1007                 tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
1008                 *tl++ = nfs_true;
1009                 *tl = txdr_unsigned(va->va_mode);
1010         } else {
1011                 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1012                 *tl = nfs_false;
1013         }
1014         if (full && va->va_uid != (uid_t)VNOVAL) {
1015                 tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
1016                 *tl++ = nfs_true;
1017                 *tl = txdr_unsigned(va->va_uid);
1018         } else {
1019                 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1020                 *tl = nfs_false;
1021         }
1022         if (full && va->va_gid != (gid_t)VNOVAL) {
1023                 tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
1024                 *tl++ = nfs_true;
1025                 *tl = txdr_unsigned(va->va_gid);
1026         } else {
1027                 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1028                 *tl = nfs_false;
1029         }
1030         if (full && va->va_size != VNOVAL) {
1031                 tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
1032                 *tl++ = nfs_true;
1033                 txdr_hyper(va->va_size, tl);
1034         } else {
1035                 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1036                 *tl = nfs_false;
1037         }
1038         if (va->va_atime.tv_sec != VNOVAL) {
1039                 if (va->va_atime.tv_sec != time_second) {
1040                         tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
1041                         *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
1042                         txdr_nfsv3time(&va->va_atime, tl);
1043                 } else {
1044                         tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1045                         *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
1046                 }
1047         } else {
1048                 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1049                 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
1050         }
1051         if (va->va_mtime.tv_sec != VNOVAL) {
1052                 if (va->va_mtime.tv_sec != time_second) {
1053                         tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
1054                         *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
1055                         txdr_nfsv3time(&va->va_mtime, tl);
1056                 } else {
1057                         tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1058                         *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
1059                 }
1060         } else {
1061                 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1062                 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
1063         }
1064 }