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