]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/nfsclient/nfs_subs.c
Import driver source from hyperv-20130502 vendor branch.
[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 "opt_kdtrace.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/bio.h>
50 #include <sys/buf.h>
51 #include <sys/proc.h>
52 #include <sys/mount.h>
53 #include <sys/vnode.h>
54 #include <sys/namei.h>
55 #include <sys/mbuf.h>
56 #include <sys/socket.h>
57 #include <sys/stat.h>
58 #include <sys/malloc.h>
59 #include <sys/rwlock.h>
60 #include <sys/sysent.h>
61 #include <sys/syscall.h>
62 #include <sys/sysproto.h>
63 #include <sys/taskqueue.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/nfsproto.h>
71 #include <nfsclient/nfs.h>
72 #include <nfsclient/nfsnode.h>
73 #include <nfs/nfs_kdtrace.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  * Note that stdarg.h and the ANSI style va_start macro is used for both
82  * ANSI and traditional C compilers.
83  */
84 #include <machine/stdarg.h>
85
86 #ifdef KDTRACE_HOOKS
87 dtrace_nfsclient_attrcache_flush_probe_func_t
88     dtrace_nfsclient_attrcache_flush_done_probe;
89 uint32_t nfsclient_attrcache_flush_done_id;
90
91 dtrace_nfsclient_attrcache_get_hit_probe_func_t
92     dtrace_nfsclient_attrcache_get_hit_probe;
93 uint32_t nfsclient_attrcache_get_hit_id;
94
95 dtrace_nfsclient_attrcache_get_miss_probe_func_t
96     dtrace_nfsclient_attrcache_get_miss_probe;
97 uint32_t nfsclient_attrcache_get_miss_id;
98
99 dtrace_nfsclient_attrcache_load_probe_func_t
100     dtrace_nfsclient_attrcache_load_done_probe;
101 uint32_t nfsclient_attrcache_load_done_id;
102 #endif /* !KDTRACE_HOOKS */
103
104 /*
105  * Data items converted to xdr at startup, since they are constant
106  * This is kinda hokey, but may save a little time doing byte swaps
107  */
108 u_int32_t       nfs_xdrneg1;
109 u_int32_t       nfs_true, nfs_false;
110
111 /* And other global data */
112 static u_int32_t nfs_xid = 0;
113 static enum vtype nv2tov_type[8]= {
114         VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON,  VNON
115 };
116
117 int             nfs_ticks;
118 int             nfs_pbuf_freecnt = -1;  /* start out unlimited */
119
120 struct nfs_bufq nfs_bufq;
121 static struct mtx nfs_xid_mtx;
122 struct task     nfs_nfsiodnew_task;
123
124 /*
125  * and the reverse mapping from generic to Version 2 procedure numbers
126  */
127 int nfsv2_procid[NFS_NPROCS] = {
128         NFSV2PROC_NULL,
129         NFSV2PROC_GETATTR,
130         NFSV2PROC_SETATTR,
131         NFSV2PROC_LOOKUP,
132         NFSV2PROC_NOOP,
133         NFSV2PROC_READLINK,
134         NFSV2PROC_READ,
135         NFSV2PROC_WRITE,
136         NFSV2PROC_CREATE,
137         NFSV2PROC_MKDIR,
138         NFSV2PROC_SYMLINK,
139         NFSV2PROC_CREATE,
140         NFSV2PROC_REMOVE,
141         NFSV2PROC_RMDIR,
142         NFSV2PROC_RENAME,
143         NFSV2PROC_LINK,
144         NFSV2PROC_READDIR,
145         NFSV2PROC_NOOP,
146         NFSV2PROC_STATFS,
147         NFSV2PROC_NOOP,
148         NFSV2PROC_NOOP,
149         NFSV2PROC_NOOP,
150         NFSV2PROC_NOOP,
151 };
152
153 LIST_HEAD(nfsnodehashhead, nfsnode);
154
155 u_int32_t
156 nfs_xid_gen(void)
157 {
158         uint32_t xid;
159
160         mtx_lock(&nfs_xid_mtx);
161
162         /* Get a pretty random xid to start with */
163         if (!nfs_xid)
164                 nfs_xid = random();
165         /*
166          * Skip zero xid if it should ever happen.
167          */
168         if (++nfs_xid == 0)
169                 nfs_xid++;
170         xid = nfs_xid;
171         mtx_unlock(&nfs_xid_mtx);
172         return xid;
173 }
174
175 /*
176  * copies a uio scatter/gather list to an mbuf chain.
177  * NOTE: can ony handle iovcnt == 1
178  */
179 int
180 nfsm_uiotombuf(struct uio *uiop, struct mbuf **mq, int siz, caddr_t *bpos)
181 {
182         char *uiocp;
183         struct mbuf *mp, *mp2;
184         int xfer, left, mlen;
185         int uiosiz, clflg, rem;
186         char *cp;
187
188         KASSERT(uiop->uio_iovcnt == 1, ("nfsm_uiotombuf: iovcnt != 1"));
189
190         if (siz > MLEN)         /* or should it >= MCLBYTES ?? */
191                 clflg = 1;
192         else
193                 clflg = 0;
194         rem = nfsm_rndup(siz)-siz;
195         mp = mp2 = *mq;
196         while (siz > 0) {
197                 left = uiop->uio_iov->iov_len;
198                 uiocp = uiop->uio_iov->iov_base;
199                 if (left > siz)
200                         left = siz;
201                 uiosiz = left;
202                 while (left > 0) {
203                         mlen = M_TRAILINGSPACE(mp);
204                         if (mlen == 0) {
205                                 if (clflg)
206                                         mp = m_getcl(M_WAITOK, MT_DATA, 0);
207                                 else
208                                         mp = m_get(M_WAITOK, MT_DATA);
209                                 mp2->m_next = mp;
210                                 mp2 = mp;
211                                 mlen = M_TRAILINGSPACE(mp);
212                         }
213                         xfer = (left > mlen) ? mlen : left;
214 #ifdef notdef
215                         /* Not Yet.. */
216                         if (uiop->uio_iov->iov_op != NULL)
217                                 (*(uiop->uio_iov->iov_op))
218                                 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
219                         else
220 #endif
221                         if (uiop->uio_segflg == UIO_SYSSPACE)
222                                 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
223                         else
224                                 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
225                         mp->m_len += xfer;
226                         left -= xfer;
227                         uiocp += xfer;
228                         uiop->uio_offset += xfer;
229                         uiop->uio_resid -= xfer;
230                 }
231                 uiop->uio_iov->iov_base =
232                     (char *)uiop->uio_iov->iov_base + uiosiz;
233                 uiop->uio_iov->iov_len -= uiosiz;
234                 siz -= uiosiz;
235         }
236         if (rem > 0) {
237                 if (rem > M_TRAILINGSPACE(mp)) {
238                         mp = m_get(M_WAITOK, MT_DATA);
239                         mp2->m_next = mp;
240                 }
241                 cp = mtod(mp, caddr_t)+mp->m_len;
242                 for (left = 0; left < rem; left++)
243                         *cp++ = '\0';
244                 mp->m_len += rem;
245                 *bpos = cp;
246         } else
247                 *bpos = mtod(mp, caddr_t)+mp->m_len;
248         *mq = mp;
249         return (0);
250 }
251
252 /*
253  * Copy a string into mbufs for the hard cases...
254  */
255 int
256 nfsm_strtmbuf(struct mbuf **mb, char **bpos, const char *cp, long siz)
257 {
258         struct mbuf *m1 = NULL, *m2;
259         long left, xfer, len, tlen;
260         u_int32_t *tl;
261         int putsize;
262
263         putsize = 1;
264         m2 = *mb;
265         left = M_TRAILINGSPACE(m2);
266         if (left > 0) {
267                 tl = ((u_int32_t *)(*bpos));
268                 *tl++ = txdr_unsigned(siz);
269                 putsize = 0;
270                 left -= NFSX_UNSIGNED;
271                 m2->m_len += NFSX_UNSIGNED;
272                 if (left > 0) {
273                         bcopy(cp, (caddr_t) tl, left);
274                         siz -= left;
275                         cp += left;
276                         m2->m_len += left;
277                         left = 0;
278                 }
279         }
280         /* Loop around adding mbufs */
281         while (siz > 0) {
282                 if (siz > MLEN) {
283                         m1 = m_getcl(M_WAITOK, MT_DATA, 0);
284                         m1->m_len = MCLBYTES;
285                 } else {
286                         m1 = m_get(M_WAITOK, MT_DATA);
287                         m1->m_len = MLEN;
288                 }
289                 m2->m_next = m1;
290                 m2 = m1;
291                 tl = mtod(m1, u_int32_t *);
292                 tlen = 0;
293                 if (putsize) {
294                         *tl++ = txdr_unsigned(siz);
295                         m1->m_len -= NFSX_UNSIGNED;
296                         tlen = NFSX_UNSIGNED;
297                         putsize = 0;
298                 }
299                 if (siz < m1->m_len) {
300                         len = nfsm_rndup(siz);
301                         xfer = siz;
302                         if (xfer < len)
303                                 *(tl+(xfer>>2)) = 0;
304                 } else {
305                         xfer = len = m1->m_len;
306                 }
307                 bcopy(cp, (caddr_t) tl, xfer);
308                 m1->m_len = len+tlen;
309                 siz -= xfer;
310                 cp += xfer;
311         }
312         *mb = m1;
313         *bpos = mtod(m1, caddr_t)+m1->m_len;
314         return (0);
315 }
316
317 /*
318  * Called once to initialize data structures...
319  */
320 int
321 nfs_init(struct vfsconf *vfsp)
322 {
323         int i;
324
325         nfsmount_zone = uma_zcreate("NFSMOUNT", sizeof(struct nfsmount),
326             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
327         nfs_true = txdr_unsigned(TRUE);
328         nfs_false = txdr_unsigned(FALSE);
329         nfs_xdrneg1 = txdr_unsigned(-1);
330         nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
331         if (nfs_ticks < 1)
332                 nfs_ticks = 1;
333         /* Ensure async daemons disabled */
334         for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
335                 nfs_iodwant[i] = NFSIOD_NOT_AVAILABLE;
336                 nfs_iodmount[i] = NULL;
337         }
338         nfs_nhinit();                   /* Init the nfsnode table */
339
340         /*
341          * Initialize reply list and start timer
342          */
343         mtx_init(&nfs_iod_mtx, "NFS iod lock", NULL, MTX_DEF);
344         mtx_init(&nfs_xid_mtx, "NFS xid lock", NULL, MTX_DEF);
345         TASK_INIT(&nfs_nfsiodnew_task, 0, nfs_nfsiodnew_tq, NULL);
346
347         nfs_pbuf_freecnt = nswbuf / 2 + 1;
348
349         return (0);
350 }
351
352 int
353 nfs_uninit(struct vfsconf *vfsp)
354 {
355         int i;
356
357         /*
358          * Tell all nfsiod processes to exit. Clear nfs_iodmax, and wakeup
359          * any sleeping nfsiods so they check nfs_iodmax and exit.
360          * Drain nfsiodnew task before we wait for them to finish.
361          */
362         mtx_lock(&nfs_iod_mtx);
363         nfs_iodmax = 0;
364         mtx_unlock(&nfs_iod_mtx);
365         taskqueue_drain(taskqueue_thread, &nfs_nfsiodnew_task);
366         mtx_lock(&nfs_iod_mtx);
367         for (i = 0; i < nfs_numasync; i++)
368                 if (nfs_iodwant[i] == NFSIOD_AVAILABLE)
369                         wakeup(&nfs_iodwant[i]);
370         /* The last nfsiod to exit will wake us up when nfs_numasync hits 0 */
371         while (nfs_numasync)
372                 msleep(&nfs_numasync, &nfs_iod_mtx, PWAIT, "ioddie", 0);
373         mtx_unlock(&nfs_iod_mtx);
374         nfs_nhuninit();
375         uma_zdestroy(nfsmount_zone);
376         return (0);
377 }
378
379 void 
380 nfs_dircookie_lock(struct nfsnode *np)
381 {
382         mtx_lock(&np->n_mtx);
383         while (np->n_flag & NDIRCOOKIELK)
384                 (void) msleep(&np->n_flag, &np->n_mtx, PZERO, "nfsdirlk", 0);
385         np->n_flag |= NDIRCOOKIELK;
386         mtx_unlock(&np->n_mtx);
387 }
388
389 void 
390 nfs_dircookie_unlock(struct nfsnode *np)
391 {
392         mtx_lock(&np->n_mtx);
393         np->n_flag &= ~NDIRCOOKIELK;
394         wakeup(&np->n_flag);
395         mtx_unlock(&np->n_mtx);
396 }
397
398 int
399 nfs_upgrade_vnlock(struct vnode *vp)
400 {
401         int old_lock;
402
403         ASSERT_VOP_LOCKED(vp, "nfs_upgrade_vnlock");
404         old_lock = VOP_ISLOCKED(vp);
405         if (old_lock != LK_EXCLUSIVE) {
406                 KASSERT(old_lock == LK_SHARED,
407                     ("nfs_upgrade_vnlock: wrong old_lock %d", old_lock));
408                 /* Upgrade to exclusive lock, this might block */
409                 vn_lock(vp, LK_UPGRADE | LK_RETRY);
410         }
411         return (old_lock);
412 }
413
414 void
415 nfs_downgrade_vnlock(struct vnode *vp, int old_lock)
416 {
417         if (old_lock != LK_EXCLUSIVE) {
418                 KASSERT(old_lock == LK_SHARED, ("wrong old_lock %d", old_lock));
419                 /* Downgrade from exclusive lock. */
420                 vn_lock(vp, LK_DOWNGRADE | LK_RETRY);
421         }
422 }
423
424 void
425 nfs_printf(const char *fmt, ...)
426 {
427         va_list ap;
428
429         mtx_lock(&Giant);
430         va_start(ap, fmt);
431         vprintf(fmt, ap);
432         va_end(ap);
433         mtx_unlock(&Giant);
434 }
435
436 /*
437  * Attribute cache routines.
438  * nfs_loadattrcache() - loads or updates the cache contents from attributes
439  *      that are on the mbuf list
440  * nfs_getattrcache() - returns valid attributes if found in cache, returns
441  *      error otherwise
442  */
443
444 /*
445  * Load the attribute cache (that lives in the nfsnode entry) with
446  * the values on the mbuf list and
447  * Iff vap not NULL
448  *    copy the attributes to *vaper
449  */
450 int
451 nfs_loadattrcache(struct vnode **vpp, struct mbuf **mdp, caddr_t *dposp,
452                   struct vattr *vaper, int dontshrink)
453 {
454         struct vnode *vp = *vpp;
455         struct vattr *vap;
456         struct nfs_fattr *fp;
457         struct nfsnode *np = NULL;
458         int32_t t1;
459         caddr_t cp2;
460         int rdev;
461         struct mbuf *md;
462         enum vtype vtyp;
463         u_short vmode;
464         struct timespec mtime, mtime_save;
465         int v3 = NFS_ISV3(vp);
466         int error = 0;
467         u_quad_t nsize;
468         int setnsize;
469
470         md = *mdp;
471         t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
472         cp2 = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, M_WAITOK);
473         if (cp2 == NULL) {
474                 error = EBADRPC;
475                 goto out;
476         }
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 = makedev(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         mtx_lock(&np->n_mtx);
527         if (vp->v_type != vtyp) {
528                 vp->v_type = vtyp;
529                 if (vp->v_type == VFIFO)
530                         vp->v_op = &nfs_fifoops;
531                 np->n_mtime = mtime;
532         }
533         vap = &np->n_vattr;
534         vap->va_type = vtyp;
535         vap->va_mode = (vmode & 07777);
536         vap->va_rdev = rdev;
537         mtime_save = vap->va_mtime;
538         vap->va_mtime = mtime;
539         vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
540         if (v3) {
541                 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
542                 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
543                 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
544                 vap->va_size = fxdr_hyper(&fp->fa3_size);
545                 vap->va_blocksize = NFS_FABLKSIZE;
546                 vap->va_bytes = fxdr_hyper(&fp->fa3_used);
547                 vap->va_fileid = fxdr_unsigned(int32_t,
548                     fp->fa3_fileid.nfsuquad[1]);
549                 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
550                 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
551                 vap->va_flags = 0;
552                 vap->va_filerev = 0;
553         } else {
554                 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
555                 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
556                 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
557                 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
558                 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
559                 vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks)
560                     * NFS_FABLKSIZE;
561                 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
562                 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
563                 vap->va_flags = 0;
564                 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
565                     fp->fa2_ctime.nfsv2_sec);
566                 vap->va_ctime.tv_nsec = 0;
567                 vap->va_gen = fxdr_unsigned(u_int32_t, fp->fa2_ctime.nfsv2_usec);
568                 vap->va_filerev = 0;
569         }
570         np->n_attrstamp = time_second;
571         setnsize = 0;
572         nsize = 0;
573         if (vap->va_size != np->n_size) {
574                 if (vap->va_type == VREG) {
575                         if (dontshrink && vap->va_size < np->n_size) {
576                                 /*
577                                  * We've been told not to shrink the file;
578                                  * zero np->n_attrstamp to indicate that
579                                  * the attributes are stale.
580                                  */
581                                 vap->va_size = np->n_size;
582                                 np->n_attrstamp = 0;
583                                 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
584                         } else if (np->n_flag & NMODIFIED) {
585                                 /*
586                                  * We've modified the file: Use the larger
587                                  * of our size, and the server's size.
588                                  */
589                                 if (vap->va_size < np->n_size) {
590                                         vap->va_size = np->n_size;
591                                 } else {
592                                         np->n_size = vap->va_size;
593                                         np->n_flag |= NSIZECHANGED;
594                                 }
595                         } else {
596                                 np->n_size = vap->va_size;
597                                 np->n_flag |= NSIZECHANGED;
598                         }
599                         setnsize = 1;
600                         nsize = vap->va_size;
601                 } else {
602                         np->n_size = vap->va_size;
603                 }
604         }
605         /*
606          * The following checks are added to prevent a race between (say)
607          * a READDIR+ and a WRITE. 
608          * READDIR+, WRITE requests sent out.
609          * READDIR+ resp, WRITE resp received on client.
610          * However, the WRITE resp was handled before the READDIR+ resp
611          * causing the post op attrs from the write to be loaded first
612          * and the attrs from the READDIR+ to be loaded later. If this 
613          * happens, we have stale attrs loaded into the attrcache.
614          * We detect this by for the mtime moving back. We invalidate the 
615          * attrcache when this happens.
616          */
617         if (timespeccmp(&mtime_save, &vap->va_mtime, >)) {
618                 /* Size changed or mtime went backwards */
619                 np->n_attrstamp = 0;
620                 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
621         }
622         if (vaper != NULL) {
623                 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
624                 if (np->n_flag & NCHG) {
625                         if (np->n_flag & NACC)
626                                 vaper->va_atime = np->n_atim;
627                         if (np->n_flag & NUPD)
628                                 vaper->va_mtime = np->n_mtim;
629                 }
630         }
631
632 #ifdef KDTRACE_HOOKS
633         if (np->n_attrstamp != 0)
634                 KDTRACE_NFS_ATTRCACHE_LOAD_DONE(vp, &np->n_vattr, 0);
635 #endif
636         mtx_unlock(&np->n_mtx);
637         if (setnsize)
638                 vnode_pager_setsize(vp, nsize);
639 out:
640 #ifdef KDTRACE_HOOKS
641         if (error)
642                 KDTRACE_NFS_ATTRCACHE_LOAD_DONE(vp, NULL, error);
643 #endif
644         return (error);
645 }
646
647 #ifdef NFS_ACDEBUG
648 #include <sys/sysctl.h>
649 SYSCTL_DECL(_vfs_oldnfs);
650 static int nfs_acdebug;
651 SYSCTL_INT(_vfs_oldnfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0,
652     "Toggle acdebug (attribute cache debug) flag");
653 #endif
654
655 /*
656  * Check the time stamp
657  * If the cache is valid, copy contents to *vap and return 0
658  * otherwise return an error
659  */
660 int
661 nfs_getattrcache(struct vnode *vp, struct vattr *vaper)
662 {
663         struct nfsnode *np;
664         struct vattr *vap;
665         struct nfsmount *nmp;
666         int timeo;
667         
668         np = VTONFS(vp);
669         vap = &np->n_vattr;
670         nmp = VFSTONFS(vp->v_mount);
671 #ifdef NFS_ACDEBUG
672         mtx_lock(&Giant);       /* nfs_printf() */
673 #endif
674         mtx_lock(&np->n_mtx);
675         /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
676         timeo = (time_second - np->n_mtime.tv_sec) / 10;
677
678 #ifdef NFS_ACDEBUG
679         if (nfs_acdebug>1)
680                 nfs_printf("nfs_getattrcache: initial timeo = %d\n", timeo);
681 #endif
682
683         if (vap->va_type == VDIR) {
684                 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
685                         timeo = nmp->nm_acdirmin;
686                 else if (timeo > nmp->nm_acdirmax)
687                         timeo = nmp->nm_acdirmax;
688         } else {
689                 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
690                         timeo = nmp->nm_acregmin;
691                 else if (timeo > nmp->nm_acregmax)
692                         timeo = nmp->nm_acregmax;
693         }
694
695 #ifdef NFS_ACDEBUG
696         if (nfs_acdebug > 2)
697                 nfs_printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
698                            nmp->nm_acregmin, nmp->nm_acregmax,
699                            nmp->nm_acdirmin, nmp->nm_acdirmax);
700
701         if (nfs_acdebug)
702                 nfs_printf("nfs_getattrcache: age = %d; final timeo = %d\n",
703                            (time_second - np->n_attrstamp), timeo);
704 #endif
705
706         if ((time_second - np->n_attrstamp) >= timeo) {
707                 nfsstats.attrcache_misses++;
708                 mtx_unlock(&np->n_mtx);
709 #ifdef NFS_ACDEBUG
710                 mtx_unlock(&Giant);     /* nfs_printf() */
711 #endif
712                 KDTRACE_NFS_ATTRCACHE_GET_MISS(vp);
713                 return (ENOENT);
714         }
715         nfsstats.attrcache_hits++;
716         if (vap->va_size != np->n_size) {
717                 if (vap->va_type == VREG) {
718                         if (np->n_flag & NMODIFIED) {
719                                 if (vap->va_size < np->n_size)
720                                         vap->va_size = np->n_size;
721                                 else
722                                         np->n_size = vap->va_size;
723                         } else {
724                                 np->n_size = vap->va_size;
725                         }
726                         vnode_pager_setsize(vp, np->n_size);
727                 } else {
728                         np->n_size = vap->va_size;
729                 }
730         }
731         bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
732         if (np->n_flag & NCHG) {
733                 if (np->n_flag & NACC)
734                         vaper->va_atime = np->n_atim;
735                 if (np->n_flag & NUPD)
736                         vaper->va_mtime = np->n_mtim;
737         }
738         mtx_unlock(&np->n_mtx);
739 #ifdef NFS_ACDEBUG
740         mtx_unlock(&Giant);     /* nfs_printf() */
741 #endif
742         KDTRACE_NFS_ATTRCACHE_GET_HIT(vp, vap);
743         return (0);
744 }
745
746 /*
747  * Purge all cached information about an NFS vnode including name
748  * cache entries, the attribute cache, and the access cache.  This is
749  * called when an NFS request for a node fails with a stale
750  * filehandle.
751  */
752 void
753 nfs_purgecache(struct vnode *vp)
754 {
755         struct nfsnode *np;
756         int i;
757
758         np = VTONFS(vp);
759         cache_purge(vp);
760         mtx_lock(&np->n_mtx);
761         np->n_attrstamp = 0;
762         KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
763         for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
764                 np->n_accesscache[i].stamp = 0;
765         KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
766         mtx_unlock(&np->n_mtx);
767 }
768
769 static nfsuint64 nfs_nullcookie = { { 0, 0 } };
770 /*
771  * This function finds the directory cookie that corresponds to the
772  * logical byte offset given.
773  */
774 nfsuint64 *
775 nfs_getcookie(struct nfsnode *np, off_t off, int add)
776 {
777         struct nfsdmap *dp, *dp2;
778         int pos;
779         nfsuint64 *retval = NULL;
780         
781         pos = (uoff_t)off / NFS_DIRBLKSIZ;
782         if (pos == 0 || off < 0) {
783                 KASSERT(!add, ("nfs getcookie add at <= 0"));
784                 return (&nfs_nullcookie);
785         }
786         pos--;
787         dp = LIST_FIRST(&np->n_cookies);
788         if (!dp) {
789                 if (add) {
790                         dp = malloc(sizeof (struct nfsdmap),
791                                 M_NFSDIROFF, M_WAITOK);
792                         dp->ndm_eocookie = 0;
793                         LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
794                 } else
795                         goto out;
796         }
797         while (pos >= NFSNUMCOOKIES) {
798                 pos -= NFSNUMCOOKIES;
799                 if (LIST_NEXT(dp, ndm_list)) {
800                         if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
801                             pos >= dp->ndm_eocookie)
802                                 goto out;
803                         dp = LIST_NEXT(dp, ndm_list);
804                 } else if (add) {
805                         dp2 = malloc(sizeof (struct nfsdmap),
806                                 M_NFSDIROFF, M_WAITOK);
807                         dp2->ndm_eocookie = 0;
808                         LIST_INSERT_AFTER(dp, dp2, ndm_list);
809                         dp = dp2;
810                 } else
811                         goto out;
812         }
813         if (pos >= dp->ndm_eocookie) {
814                 if (add)
815                         dp->ndm_eocookie = pos + 1;
816                 else
817                         goto out;
818         }
819         retval = &dp->ndm_cookies[pos];
820 out:
821         return (retval);
822 }
823
824 /*
825  * Invalidate cached directory information, except for the actual directory
826  * blocks (which are invalidated separately).
827  * Done mainly to avoid the use of stale offset cookies.
828  */
829 void
830 nfs_invaldir(struct vnode *vp)
831 {
832         struct nfsnode *np = VTONFS(vp);
833
834         KASSERT(vp->v_type == VDIR, ("nfs: invaldir not dir"));
835         nfs_dircookie_lock(np);
836         np->n_direofoffset = 0;
837         np->n_cookieverf.nfsuquad[0] = 0;
838         np->n_cookieverf.nfsuquad[1] = 0;
839         if (LIST_FIRST(&np->n_cookies))
840                 LIST_FIRST(&np->n_cookies)->ndm_eocookie = 0;
841         nfs_dircookie_unlock(np);
842 }
843
844 /*
845  * The write verifier has changed (probably due to a server reboot), so all
846  * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
847  * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
848  * and B_CLUSTEROK flags.  Once done the new write verifier can be set for the
849  * mount point.
850  *
851  * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
852  * writes are not clusterable.
853  */
854 void
855 nfs_clearcommit(struct mount *mp)
856 {
857         struct vnode *vp, *nvp;
858         struct buf *bp, *nbp;
859         struct bufobj *bo;
860
861         MNT_VNODE_FOREACH_ALL(vp, mp, nvp) {
862                 bo = &vp->v_bufobj;
863                 vholdl(vp);
864                 VI_UNLOCK(vp);
865                 BO_LOCK(bo);
866                 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
867                         if (!BUF_ISLOCKED(bp) &&
868                             (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
869                                 == (B_DELWRI | B_NEEDCOMMIT))
870                                 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
871                 }
872                 BO_UNLOCK(bo);
873                 vdrop(vp);
874         }
875 }
876
877 /*
878  * Helper functions for former macros.  Some of these should be
879  * moved to their callers.
880  */
881
882 int
883 nfsm_mtofh_xx(struct vnode *d, struct vnode **v, int v3, int *f,
884     struct mbuf **md, caddr_t *dpos)
885 {
886         struct nfsnode *ttnp;
887         struct vnode *ttvp;
888         nfsfh_t *ttfhp;
889         u_int32_t *tl;
890         int ttfhsize;
891         int t1;
892
893         if (v3) {
894                 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
895                 if (tl == NULL)
896                         return EBADRPC;
897                 *f = fxdr_unsigned(int, *tl);
898         } else
899                 *f = 1;
900         if (*f) {
901                 t1 = nfsm_getfh_xx(&ttfhp, &ttfhsize, (v3), md, dpos);
902                 if (t1 != 0)
903                         return t1;
904                 t1 = nfs_nget(d->v_mount, ttfhp, ttfhsize, &ttnp, LK_EXCLUSIVE);
905                 if (t1 != 0)
906                         return t1;
907                 *v = NFSTOV(ttnp);
908         }
909         if (v3) {
910                 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
911                 if (tl == NULL)
912                         return EBADRPC;
913                 if (*f)
914                         *f = fxdr_unsigned(int, *tl);
915                 else if (fxdr_unsigned(int, *tl))
916                         nfsm_adv_xx(NFSX_V3FATTR, md, dpos);
917         }
918         if (*f) {
919                 ttvp = *v;
920                 t1 = nfs_loadattrcache(&ttvp, md, dpos, NULL, 0);
921                 if (t1)
922                         return t1;
923                 *v = ttvp;
924         }
925         return 0;
926 }
927
928 int
929 nfsm_getfh_xx(nfsfh_t **f, int *s, int v3, struct mbuf **md, caddr_t *dpos)
930 {
931         u_int32_t *tl;
932
933         if (v3) {
934                 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
935                 if (tl == NULL)
936                         return EBADRPC;
937                 *s = fxdr_unsigned(int, *tl);
938                 if (*s <= 0 || *s > NFSX_V3FHMAX)
939                         return EBADRPC;
940         } else
941                 *s = NFSX_V2FH;
942         *f = nfsm_dissect_xx(nfsm_rndup(*s), md, dpos);
943         if (*f == NULL)
944                 return EBADRPC;
945         else
946                 return 0;
947 }
948
949
950 int
951 nfsm_loadattr_xx(struct vnode **v, struct vattr *va, struct mbuf **md,
952                  caddr_t *dpos)
953 {
954         int t1;
955
956         struct vnode *ttvp = *v;
957         t1 = nfs_loadattrcache(&ttvp, md, dpos, va, 0);
958         if (t1 != 0)
959                 return t1;
960         *v = ttvp;
961         return 0;
962 }
963
964 int
965 nfsm_postop_attr_xx(struct vnode **v, int *f, struct vattr *va,
966                     struct mbuf **md, caddr_t *dpos)
967 {
968         u_int32_t *tl;
969         int t1;
970
971         struct vnode *ttvp = *v;
972         tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
973         if (tl == NULL)
974                 return EBADRPC;
975         *f = fxdr_unsigned(int, *tl);
976         if (*f != 0) {
977                 t1 = nfs_loadattrcache(&ttvp, md, dpos, va, 1);
978                 if (t1 != 0) {
979                         *f = 0;
980                         return t1;
981                 }
982                 *v = ttvp;
983         }
984         return 0;
985 }
986
987 int
988 nfsm_wcc_data_xx(struct vnode **v, int *f, struct mbuf **md, caddr_t *dpos)
989 {
990         u_int32_t *tl;
991         int ttattrf, ttretf = 0;
992         int t1;
993
994         tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
995         if (tl == NULL)
996                 return EBADRPC;
997         if (*tl == nfs_true) {
998                 tl = nfsm_dissect_xx(6 * NFSX_UNSIGNED, md, dpos);
999                 if (tl == NULL)
1000                         return EBADRPC;
1001                 mtx_lock(&(VTONFS(*v))->n_mtx);
1002                 if (*f)
1003                         ttretf = (VTONFS(*v)->n_mtime.tv_sec == fxdr_unsigned(u_int32_t, *(tl + 2)) && 
1004                                   VTONFS(*v)->n_mtime.tv_nsec == fxdr_unsigned(u_int32_t, *(tl + 3))); 
1005                 mtx_unlock(&(VTONFS(*v))->n_mtx);
1006         }
1007         t1 = nfsm_postop_attr_xx(v, &ttattrf, NULL, md, dpos);
1008         if (t1)
1009                 return t1;
1010         if (*f)
1011                 *f = ttretf;
1012         else
1013                 *f = ttattrf;
1014         return 0;
1015 }
1016
1017 int
1018 nfsm_strtom_xx(const char *a, int s, int m, struct mbuf **mb, caddr_t *bpos)
1019 {
1020         u_int32_t *tl;
1021         int t1;
1022
1023         if (s > m)
1024                 return ENAMETOOLONG;
1025         t1 = nfsm_rndup(s) + NFSX_UNSIGNED;
1026         if (t1 <= M_TRAILINGSPACE(*mb)) {
1027                 tl = nfsm_build_xx(t1, mb, bpos);
1028                 *tl++ = txdr_unsigned(s);
1029                 *(tl + ((t1 >> 2) - 2)) = 0;
1030                 bcopy(a, tl, s);
1031         } else {
1032                 t1 = nfsm_strtmbuf(mb, bpos, a, s);
1033                 if (t1 != 0)
1034                         return t1;
1035         }
1036         return 0;
1037 }
1038
1039 int
1040 nfsm_fhtom_xx(struct vnode *v, int v3, struct mbuf **mb, caddr_t *bpos)
1041 {
1042         u_int32_t *tl;
1043         int t1;
1044         caddr_t cp;
1045
1046         if (v3) {
1047                 t1 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED;
1048                 if (t1 < M_TRAILINGSPACE(*mb)) {
1049                         tl = nfsm_build_xx(t1, mb, bpos);
1050                         *tl++ = txdr_unsigned(VTONFS(v)->n_fhsize);
1051                         *(tl + ((t1 >> 2) - 2)) = 0;
1052                         bcopy(VTONFS(v)->n_fhp, tl, VTONFS(v)->n_fhsize);
1053                 } else {
1054                         t1 = nfsm_strtmbuf(mb, bpos,
1055                             (const char *)VTONFS(v)->n_fhp,
1056                             VTONFS(v)->n_fhsize);
1057                         if (t1 != 0)
1058                                 return t1;
1059                 }
1060         } else {
1061                 cp = nfsm_build_xx(NFSX_V2FH, mb, bpos);
1062                 bcopy(VTONFS(v)->n_fhp, cp, NFSX_V2FH);
1063         }
1064         return 0;
1065 }
1066
1067 void
1068 nfsm_v3attrbuild_xx(struct vattr *va, int full, struct mbuf **mb,
1069     caddr_t *bpos)
1070 {
1071         u_int32_t *tl;
1072
1073         if (va->va_mode != (mode_t)VNOVAL) {
1074                 tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
1075                 *tl++ = nfs_true;
1076                 *tl = txdr_unsigned(va->va_mode);
1077         } else {
1078                 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1079                 *tl = nfs_false;
1080         }
1081         if (full && va->va_uid != (uid_t)VNOVAL) {
1082                 tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
1083                 *tl++ = nfs_true;
1084                 *tl = txdr_unsigned(va->va_uid);
1085         } else {
1086                 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1087                 *tl = nfs_false;
1088         }
1089         if (full && va->va_gid != (gid_t)VNOVAL) {
1090                 tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
1091                 *tl++ = nfs_true;
1092                 *tl = txdr_unsigned(va->va_gid);
1093         } else {
1094                 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1095                 *tl = nfs_false;
1096         }
1097         if (full && va->va_size != VNOVAL) {
1098                 tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
1099                 *tl++ = nfs_true;
1100                 txdr_hyper(va->va_size, tl);
1101         } else {
1102                 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1103                 *tl = nfs_false;
1104         }
1105         if (va->va_atime.tv_sec != VNOVAL) {
1106                 if ((va->va_vaflags & VA_UTIMES_NULL) == 0) {
1107                         tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
1108                         *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
1109                         txdr_nfsv3time(&va->va_atime, tl);
1110                 } else {
1111                         tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1112                         *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
1113                 }
1114         } else {
1115                 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1116                 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
1117         }
1118         if (va->va_mtime.tv_sec != VNOVAL) {
1119                 if ((va->va_vaflags & VA_UTIMES_NULL) == 0) {
1120                         tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
1121                         *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
1122                         txdr_nfsv3time(&va->va_mtime, tl);
1123                 } else {
1124                         tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1125                         *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
1126                 }
1127         } else {
1128                 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1129                 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
1130         }
1131 }