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