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