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