kernel - Performance tuning
[dragonfly.git] / sys / vfs / nfs / nfs_vnops.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  * 3. 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_vnops.c 8.16 (Berkeley) 5/27/95
33  * $FreeBSD: src/sys/nfs/nfs_vnops.c,v 1.150.2.5 2001/12/20 19:56:28 dillon Exp $
34  */
35
36
37 /*
38  * vnode op calls for Sun NFS version 2 and 3
39  */
40
41 #include "opt_inet.h"
42
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/systm.h>
46 #include <sys/resourcevar.h>
47 #include <sys/proc.h>
48 #include <sys/mount.h>
49 #include <sys/buf.h>
50 #include <sys/malloc.h>
51 #include <sys/mbuf.h>
52 #include <sys/namei.h>
53 #include <sys/nlookup.h>
54 #include <sys/socket.h>
55 #include <sys/vnode.h>
56 #include <sys/dirent.h>
57 #include <sys/fcntl.h>
58 #include <sys/lockf.h>
59 #include <sys/stat.h>
60 #include <sys/sysctl.h>
61 #include <sys/conf.h>
62
63 #include <vm/vm.h>
64 #include <vm/vm_extern.h>
65
66 #include <sys/buf2.h>
67
68 #include <vfs/fifofs/fifo.h>
69 #include <vfs/ufs/dir.h>
70
71 #undef DIRBLKSIZ
72
73 #include "rpcv2.h"
74 #include "nfsproto.h"
75 #include "nfs.h"
76 #include "nfsmount.h"
77 #include "nfsnode.h"
78 #include "xdr_subs.h"
79 #include "nfsm_subs.h"
80
81 #include <net/if.h>
82 #include <netinet/in.h>
83 #include <netinet/in_var.h>
84
85 #include <sys/thread2.h>
86
87 /* Defs */
88 #define TRUE    1
89 #define FALSE   0
90
91 static int      nfsfifo_read (struct vop_read_args *);
92 static int      nfsfifo_write (struct vop_write_args *);
93 static int      nfsfifo_close (struct vop_close_args *);
94 static int      nfs_setattrrpc (struct vnode *,struct vattr *,struct ucred *,struct thread *);
95 static  int     nfs_lookup (struct vop_old_lookup_args *);
96 static  int     nfs_create (struct vop_old_create_args *);
97 static  int     nfs_mknod (struct vop_old_mknod_args *);
98 static  int     nfs_open (struct vop_open_args *);
99 static  int     nfs_close (struct vop_close_args *);
100 static  int     nfs_access (struct vop_access_args *);
101 static  int     nfs_getattr (struct vop_getattr_args *);
102 static  int     nfs_setattr (struct vop_setattr_args *);
103 static  int     nfs_read (struct vop_read_args *);
104 static  int     nfs_mmap (struct vop_mmap_args *);
105 static  int     nfs_fsync (struct vop_fsync_args *);
106 static  int     nfs_remove (struct vop_old_remove_args *);
107 static  int     nfs_link (struct vop_old_link_args *);
108 static  int     nfs_rename (struct vop_old_rename_args *);
109 static  int     nfs_mkdir (struct vop_old_mkdir_args *);
110 static  int     nfs_rmdir (struct vop_old_rmdir_args *);
111 static  int     nfs_symlink (struct vop_old_symlink_args *);
112 static  int     nfs_readdir (struct vop_readdir_args *);
113 static  int     nfs_bmap (struct vop_bmap_args *);
114 static  int     nfs_strategy (struct vop_strategy_args *);
115 static  int     nfs_lookitup (struct vnode *, const char *, int,
116                         struct ucred *, struct thread *, struct nfsnode **);
117 static  int     nfs_sillyrename (struct vnode *,struct vnode *,struct componentname *);
118 static int      nfs_laccess (struct vop_access_args *);
119 static int      nfs_readlink (struct vop_readlink_args *);
120 static int      nfs_print (struct vop_print_args *);
121 static int      nfs_advlock (struct vop_advlock_args *);
122
123 static  int     nfs_nresolve (struct vop_nresolve_args *);
124 /*
125  * Global vfs data structures for nfs
126  */
127 struct vop_ops nfsv2_vnode_vops = {
128         .vop_default =          vop_defaultop,
129         .vop_access =           nfs_access,
130         .vop_advlock =          nfs_advlock,
131         .vop_bmap =             nfs_bmap,
132         .vop_close =            nfs_close,
133         .vop_old_create =       nfs_create,
134         .vop_fsync =            nfs_fsync,
135         .vop_getattr =          nfs_getattr,
136         .vop_getpages =         vop_stdgetpages,
137         .vop_putpages =         vop_stdputpages,
138         .vop_inactive =         nfs_inactive,
139         .vop_old_link =         nfs_link,
140         .vop_old_lookup =       nfs_lookup,
141         .vop_old_mkdir =        nfs_mkdir,
142         .vop_old_mknod =        nfs_mknod,
143         .vop_mmap =             nfs_mmap,
144         .vop_open =             nfs_open,
145         .vop_print =            nfs_print,
146         .vop_read =             nfs_read,
147         .vop_readdir =          nfs_readdir,
148         .vop_readlink =         nfs_readlink,
149         .vop_reclaim =          nfs_reclaim,
150         .vop_old_remove =       nfs_remove,
151         .vop_old_rename =       nfs_rename,
152         .vop_old_rmdir =        nfs_rmdir,
153         .vop_setattr =          nfs_setattr,
154         .vop_strategy =         nfs_strategy,
155         .vop_old_symlink =      nfs_symlink,
156         .vop_write =            nfs_write,
157         .vop_nresolve =         nfs_nresolve
158 };
159
160 /*
161  * Special device vnode ops
162  */
163 struct vop_ops nfsv2_spec_vops = {
164         .vop_default =          vop_defaultop,
165         .vop_access =           nfs_laccess,
166         .vop_close =            nfs_close,
167         .vop_fsync =            nfs_fsync,
168         .vop_getattr =          nfs_getattr,
169         .vop_inactive =         nfs_inactive,
170         .vop_print =            nfs_print,
171         .vop_read =             vop_stdnoread,
172         .vop_reclaim =          nfs_reclaim,
173         .vop_setattr =          nfs_setattr,
174         .vop_write =            vop_stdnowrite
175 };
176
177 struct vop_ops nfsv2_fifo_vops = {
178         .vop_default =          fifo_vnoperate,
179         .vop_access =           nfs_laccess,
180         .vop_close =            nfsfifo_close,
181         .vop_fsync =            nfs_fsync,
182         .vop_getattr =          nfs_getattr,
183         .vop_inactive =         nfs_inactive,
184         .vop_print =            nfs_print,
185         .vop_read =             nfsfifo_read,
186         .vop_reclaim =          nfs_reclaim,
187         .vop_setattr =          nfs_setattr,
188         .vop_write =            nfsfifo_write
189 };
190
191 static int      nfs_mknodrpc (struct vnode *dvp, struct vnode **vpp,
192                                   struct componentname *cnp,
193                                   struct vattr *vap);
194 static int      nfs_removerpc (struct vnode *dvp, const char *name,
195                                    int namelen,
196                                    struct ucred *cred, struct thread *td);
197 static int      nfs_renamerpc (struct vnode *fdvp, const char *fnameptr,
198                                    int fnamelen, struct vnode *tdvp,
199                                    const char *tnameptr, int tnamelen,
200                                    struct ucred *cred, struct thread *td);
201 static int      nfs_renameit (struct vnode *sdvp,
202                                   struct componentname *scnp,
203                                   struct sillyrename *sp);
204
205 SYSCTL_DECL(_vfs_nfs);
206
207 static int nfs_flush_on_rename = 1;
208 SYSCTL_INT(_vfs_nfs, OID_AUTO, flush_on_rename, CTLFLAG_RW, 
209            &nfs_flush_on_rename, 0, "flush fvp prior to rename");
210 static int nfs_flush_on_hlink = 0;
211 SYSCTL_INT(_vfs_nfs, OID_AUTO, flush_on_hlink, CTLFLAG_RW, 
212            &nfs_flush_on_hlink, 0, "flush fvp prior to hard link");
213
214 static int      nfsaccess_cache_timeout = NFS_DEFATTRTIMO;
215 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW, 
216            &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
217
218 static int      nfsneg_cache_timeout = NFS_MINATTRTIMO;
219 SYSCTL_INT(_vfs_nfs, OID_AUTO, neg_cache_timeout, CTLFLAG_RW, 
220            &nfsneg_cache_timeout, 0, "NFS NEGATIVE NAMECACHE timeout");
221
222 static int      nfspos_cache_timeout = NFS_MINATTRTIMO;
223 SYSCTL_INT(_vfs_nfs, OID_AUTO, pos_cache_timeout, CTLFLAG_RW, 
224            &nfspos_cache_timeout, 0, "NFS POSITIVE NAMECACHE timeout");
225
226 static int      nfsv3_commit_on_close = 0;
227 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW, 
228            &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
229 #if 0
230 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD, 
231            &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
232
233 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD, 
234            &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
235 #endif
236
237 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY          \
238                          | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE     \
239                          | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
240
241 /*
242  * Returns whether a name component is a degenerate '.' or '..'.
243  */
244 static __inline
245 int
246 nlcdegenerate(struct nlcomponent *nlc)
247 {
248         if (nlc->nlc_namelen == 1 && nlc->nlc_nameptr[0] == '.')
249                 return(1);
250         if (nlc->nlc_namelen == 2 &&
251             nlc->nlc_nameptr[0] == '.' && nlc->nlc_nameptr[1] == '.')
252                 return(1);
253         return(0);
254 }
255
256 static int
257 nfs3_access_otw(struct vnode *vp, int wmode,
258                 struct thread *td, struct ucred *cred)
259 {
260         struct nfsnode *np = VTONFS(vp);
261         int attrflag;
262         int error = 0;
263         u_int32_t *tl;
264         u_int32_t rmode;
265         struct nfsm_info info;
266
267         info.mrep = NULL;
268         info.v3 = 1;
269
270         nfsstats.rpccnt[NFSPROC_ACCESS]++;
271         nfsm_reqhead(&info, vp, NFSPROC_ACCESS,
272                      NFSX_FH(info.v3) + NFSX_UNSIGNED);
273         ERROROUT(nfsm_fhtom(&info, vp));
274         tl = nfsm_build(&info, NFSX_UNSIGNED);
275         *tl = txdr_unsigned(wmode); 
276         NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_ACCESS, td, cred, &error));
277         ERROROUT(nfsm_postop_attr(&info, vp, &attrflag, NFS_LATTR_NOSHRINK));
278         if (error == 0) {
279                 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
280                 rmode = fxdr_unsigned(u_int32_t, *tl);
281                 np->n_mode = rmode;
282                 np->n_modeuid = cred->cr_uid;
283                 np->n_modestamp = mycpu->gd_time_seconds;
284         }
285         m_freem(info.mrep);
286         info.mrep = NULL;
287 nfsmout:
288         return error;
289 }
290
291 /*
292  * nfs access vnode op.
293  * For nfs version 2, just return ok. File accesses may fail later.
294  * For nfs version 3, use the access rpc to check accessibility. If file modes
295  * are changed on the server, accesses might still fail later.
296  *
297  * nfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
298  */
299 static int
300 nfs_access(struct vop_access_args *ap)
301 {
302         struct ucred *cred;
303         struct vnode *vp = ap->a_vp;
304         thread_t td = curthread;
305         int error = 0;
306         u_int32_t mode, wmode;
307         struct nfsnode *np = VTONFS(vp);
308         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
309         int v3 = NFS_ISV3(vp);
310
311         lwkt_gettoken(&nmp->nm_token);
312
313         /*
314          * Disallow write attempts on filesystems mounted read-only;
315          * unless the file is a socket, fifo, or a block or character
316          * device resident on the filesystem.
317          */
318         if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
319                 switch (vp->v_type) {
320                 case VREG:
321                 case VDIR:
322                 case VLNK:
323                         lwkt_reltoken(&nmp->nm_token);
324                         return (EROFS);
325                 default:
326                         break;
327                 }
328         }
329
330         /*
331          * The NFS protocol passes only the effective uid/gid over the wire but
332          * we need to check access against real ids if AT_EACCESS not set.
333          * Handle this case by cloning the credentials and setting the
334          * effective ids to the real ones.
335          */
336         if (ap->a_flags & AT_EACCESS) {
337                 cred = crhold(ap->a_cred);
338         } else {
339                 cred = crdup(ap->a_cred);
340                 cred->cr_uid = cred->cr_ruid;
341                 cred->cr_gid = cred->cr_rgid;
342         }
343
344         /*
345          * For nfs v3, check to see if we have done this recently, and if
346          * so return our cached result instead of making an ACCESS call.
347          * If not, do an access rpc, otherwise you are stuck emulating
348          * ufs_access() locally using the vattr. This may not be correct,
349          * since the server may apply other access criteria such as
350          * client uid-->server uid mapping that we do not know about.
351          */
352         if (v3) {
353                 if (ap->a_mode & VREAD)
354                         mode = NFSV3ACCESS_READ;
355                 else
356                         mode = 0;
357                 if (vp->v_type != VDIR) {
358                         if (ap->a_mode & VWRITE)
359                                 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
360                         if (ap->a_mode & VEXEC)
361                                 mode |= NFSV3ACCESS_EXECUTE;
362                 } else {
363                         if (ap->a_mode & VWRITE)
364                                 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
365                                          NFSV3ACCESS_DELETE);
366                         if (ap->a_mode & VEXEC)
367                                 mode |= NFSV3ACCESS_LOOKUP;
368                 }
369                 /* XXX safety belt, only make blanket request if caching */
370                 if (nfsaccess_cache_timeout > 0) {
371                         wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY | 
372                                 NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE | 
373                                 NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
374                 } else {
375                         wmode = mode;
376                 }
377
378                 /*
379                  * Does our cached result allow us to give a definite yes to
380                  * this request?
381                  */
382                 if (np->n_modestamp && 
383                    (mycpu->gd_time_seconds < (np->n_modestamp + nfsaccess_cache_timeout)) &&
384                    (cred->cr_uid == np->n_modeuid) &&
385                    ((np->n_mode & mode) == mode)) {
386                         nfsstats.accesscache_hits++;
387                 } else {
388                         /*
389                          * Either a no, or a don't know.  Go to the wire.
390                          */
391                         nfsstats.accesscache_misses++;
392                         error = nfs3_access_otw(vp, wmode, td, cred);
393                         if (!error) {
394                                 if ((np->n_mode & mode) != mode) {
395                                         error = EACCES;
396                                 }
397                         }
398                 }
399         } else {
400                 if ((error = nfs_laccess(ap)) != 0) {
401                         crfree(cred);
402                         lwkt_reltoken(&nmp->nm_token);
403                         return (error);
404                 }
405
406                 /*
407                  * Attempt to prevent a mapped root from accessing a file
408                  * which it shouldn't.  We try to read a byte from the file
409                  * if the user is root and the file is not zero length.
410                  * After calling nfs_laccess, we should have the correct
411                  * file size cached.
412                  */
413                 if (cred->cr_uid == 0 && (ap->a_mode & VREAD)
414                     && VTONFS(vp)->n_size > 0) {
415                         struct iovec aiov;
416                         struct uio auio;
417                         char buf[1];
418
419                         aiov.iov_base = buf;
420                         aiov.iov_len = 1;
421                         auio.uio_iov = &aiov;
422                         auio.uio_iovcnt = 1;
423                         auio.uio_offset = 0;
424                         auio.uio_resid = 1;
425                         auio.uio_segflg = UIO_SYSSPACE;
426                         auio.uio_rw = UIO_READ;
427                         auio.uio_td = td;
428
429                         if (vp->v_type == VREG) {
430                                 error = nfs_readrpc_uio(vp, &auio);
431                         } else if (vp->v_type == VDIR) {
432                                 char* bp;
433                                 bp = kmalloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
434                                 aiov.iov_base = bp;
435                                 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
436                                 error = nfs_readdirrpc_uio(vp, &auio);
437                                 kfree(bp, M_TEMP);
438                         } else if (vp->v_type == VLNK) {
439                                 error = nfs_readlinkrpc_uio(vp, &auio);
440                         } else {
441                                 error = EACCES;
442                         }
443                 }
444         }
445         /*
446          * [re]record creds for reading and/or writing if access
447          * was granted.  Assume the NFS server will grant read access
448          * for execute requests.
449          */
450         if (error == 0) {
451                 if ((ap->a_mode & (VREAD|VEXEC)) && cred != np->n_rucred) {
452                         crhold(cred);
453                         if (np->n_rucred)
454                                 crfree(np->n_rucred);
455                         np->n_rucred = cred;
456                 }
457                 if ((ap->a_mode & VWRITE) && cred != np->n_wucred) {
458                         crhold(cred);
459                         if (np->n_wucred)
460                                 crfree(np->n_wucred);
461                         np->n_wucred = cred;
462                 }
463         }
464         lwkt_reltoken(&nmp->nm_token);
465         crfree(cred);
466         return(error);
467 }
468
469 /*
470  * nfs open vnode op
471  * Check to see if the type is ok
472  * and that deletion is not in progress.
473  * For paged in text files, you will need to flush the page cache
474  * if consistency is lost.
475  *
476  * nfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
477  *          struct file *a_fp)
478  */
479 /* ARGSUSED */
480 static int
481 nfs_open(struct vop_open_args *ap)
482 {
483         struct vnode *vp = ap->a_vp;
484         struct nfsnode *np = VTONFS(vp);
485         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
486         struct vattr vattr;
487         int error;
488
489         lwkt_gettoken(&nmp->nm_token);
490
491         if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
492 #ifdef DIAGNOSTIC
493                 kprintf("open eacces vtyp=%d\n",vp->v_type);
494 #endif
495                 lwkt_reltoken(&nmp->nm_token);
496                 return (EOPNOTSUPP);
497         }
498
499         /*
500          * Save valid creds for reading and writing for later RPCs.
501          */
502         if ((ap->a_mode & FREAD) && ap->a_cred != np->n_rucred) {
503                 crhold(ap->a_cred);
504                 if (np->n_rucred)
505                         crfree(np->n_rucred);
506                 np->n_rucred = ap->a_cred;
507         }
508         if ((ap->a_mode & FWRITE) && ap->a_cred != np->n_wucred) {
509                 crhold(ap->a_cred);
510                 if (np->n_wucred)
511                         crfree(np->n_wucred);
512                 np->n_wucred = ap->a_cred;
513         }
514
515         /*
516          * Clear the attribute cache only if opening with write access.  It
517          * is unclear if we should do this at all here, but we certainly
518          * should not clear the cache unconditionally simply because a file
519          * is being opened.
520          */
521         if (ap->a_mode & FWRITE)
522                 np->n_attrstamp = 0;
523
524         /*
525          * For normal NFS, reconcile changes made locally verses 
526          * changes made remotely.  Note that VOP_GETATTR only goes
527          * to the wire if the cached attribute has timed out or been
528          * cleared.
529          *
530          * If local modifications have been made clear the attribute
531          * cache to force an attribute and modified time check.  If
532          * GETATTR detects that the file has been changed by someone
533          * other then us it will set NRMODIFIED.
534          *
535          * If we are opening a directory and local changes have been
536          * made we have to invalidate the cache in order to ensure
537          * that we get the most up-to-date information from the
538          * server.  XXX
539          */
540         if (np->n_flag & NLMODIFIED) {
541                 np->n_attrstamp = 0;
542                 if (vp->v_type == VDIR) {
543                         error = nfs_vinvalbuf(vp, V_SAVE, 1);
544                         if (error == EINTR)
545                                 return (error);
546                         nfs_invaldir(vp);
547                 }
548         }
549         error = VOP_GETATTR(vp, &vattr);
550         if (error) {
551                 lwkt_reltoken(&nmp->nm_token);
552                 return (error);
553         }
554         if (np->n_flag & NRMODIFIED) {
555                 if (vp->v_type == VDIR)
556                         nfs_invaldir(vp);
557                 error = nfs_vinvalbuf(vp, V_SAVE, 1);
558                 if (error == EINTR) {
559                         lwkt_reltoken(&nmp->nm_token);
560                         return (error);
561                 }
562                 np->n_flag &= ~NRMODIFIED;
563         }
564         error = vop_stdopen(ap);
565         lwkt_reltoken(&nmp->nm_token);
566
567         return error;
568 }
569
570 /*
571  * nfs close vnode op
572  * What an NFS client should do upon close after writing is a debatable issue.
573  * Most NFS clients push delayed writes to the server upon close, basically for
574  * two reasons:
575  * 1 - So that any write errors may be reported back to the client process
576  *     doing the close system call. By far the two most likely errors are
577  *     NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
578  * 2 - To put a worst case upper bound on cache inconsistency between
579  *     multiple clients for the file.
580  * There is also a consistency problem for Version 2 of the protocol w.r.t.
581  * not being able to tell if other clients are writing a file concurrently,
582  * since there is no way of knowing if the changed modify time in the reply
583  * is only due to the write for this client.
584  * (NFS Version 3 provides weak cache consistency data in the reply that
585  *  should be sufficient to detect and handle this case.)
586  *
587  * The current code does the following:
588  * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
589  * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
590  *                     or commit them (this satisfies 1 and 2 except for the
591  *                     case where the server crashes after this close but
592  *                     before the commit RPC, which is felt to be "good
593  *                     enough". Changing the last argument to nfs_flush() to
594  *                     a 1 would force a commit operation, if it is felt a
595  *                     commit is necessary now.
596  * for NQNFS         - do nothing now, since 2 is dealt with via leases and
597  *                     1 should be dealt with via an fsync() system call for
598  *                     cases where write errors are important.
599  *
600  * nfs_close(struct vnode *a_vp, int a_fflag)
601  */
602 /* ARGSUSED */
603 static int
604 nfs_close(struct vop_close_args *ap)
605 {
606         struct vnode *vp = ap->a_vp;
607         struct nfsnode *np = VTONFS(vp);
608         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
609         int error = 0;
610         thread_t td = curthread;
611
612         vn_lock(vp, LK_UPGRADE | LK_RETRY); /* XXX */
613         lwkt_gettoken(&nmp->nm_token);
614
615         if (vp->v_type == VREG) {
616             if (np->n_flag & NLMODIFIED) {
617                 if (NFS_ISV3(vp)) {
618                     /*
619                      * Under NFSv3 we have dirty buffers to dispose of.  We
620                      * must flush them to the NFS server.  We have the option
621                      * of waiting all the way through the commit rpc or just
622                      * waiting for the initial write.  The default is to only
623                      * wait through the initial write so the data is in the
624                      * server's cache, which is roughly similar to the state
625                      * a standard disk subsystem leaves the file in on close().
626                      *
627                      * We cannot clear the NLMODIFIED bit in np->n_flag due to
628                      * potential races with other processes, and certainly
629                      * cannot clear it if we don't commit.
630                      */
631                     int cm = nfsv3_commit_on_close ? 1 : 0;
632                     error = nfs_flush(vp, MNT_WAIT, td, cm);
633                     /* np->n_flag &= ~NLMODIFIED; */
634                 } else {
635                     error = nfs_vinvalbuf(vp, V_SAVE, 1);
636                 }
637                 np->n_attrstamp = 0;
638             }
639             if (np->n_flag & NWRITEERR) {
640                 np->n_flag &= ~NWRITEERR;
641                 error = np->n_error;
642             }
643         }
644         vop_stdclose(ap);
645         lwkt_reltoken(&nmp->nm_token);
646
647         return (error);
648 }
649
650 /*
651  * nfs getattr call from vfs.
652  *
653  * nfs_getattr(struct vnode *a_vp, struct vattr *a_vap)
654  */
655 static int
656 nfs_getattr(struct vop_getattr_args *ap)
657 {
658         struct vnode *vp = ap->a_vp;
659         struct nfsnode *np = VTONFS(vp);
660         struct nfsmount *nmp;
661         int error = 0;
662         thread_t td = curthread;
663         struct nfsm_info info;
664
665         info.mrep = NULL;
666         info.v3 = NFS_ISV3(vp);
667         nmp = VFSTONFS(vp->v_mount);
668
669         lwkt_gettoken(&nmp->nm_token);
670         
671         /*
672          * Update local times for special files.
673          */
674         if (np->n_flag & (NACC | NUPD))
675                 np->n_flag |= NCHG;
676         /*
677          * First look in the cache.
678          */
679         if (nfs_getattrcache(vp, ap->a_vap) == 0)
680                 goto done;
681
682         if (info.v3 && nfsaccess_cache_timeout > 0) {
683                 nfsstats.accesscache_misses++;
684                 nfs3_access_otw(vp, NFSV3ACCESS_ALL, td, nfs_vpcred(vp, ND_CHECK));
685                 if (nfs_getattrcache(vp, ap->a_vap) == 0)
686                         goto done;
687         }
688
689         nfsstats.rpccnt[NFSPROC_GETATTR]++;
690         nfsm_reqhead(&info, vp, NFSPROC_GETATTR, NFSX_FH(info.v3));
691         ERROROUT(nfsm_fhtom(&info, vp));
692         NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_GETATTR, td,
693                                 nfs_vpcred(vp, ND_CHECK), &error));
694         if (error == 0) {
695                 ERROROUT(nfsm_loadattr(&info, vp, ap->a_vap));
696         }
697         m_freem(info.mrep);
698         info.mrep = NULL;
699 done:
700         /*
701          * NFS doesn't support chflags flags.  If the nfs mount was
702          * made -o cache set the UF_CACHE bit for swapcache.
703          */
704         if ((nmp->nm_flag & NFSMNT_CACHE) && (vp->v_flag & VROOT))
705                 ap->a_vap->va_flags |= UF_CACHE;
706 nfsmout:
707         lwkt_reltoken(&nmp->nm_token);
708         return (error);
709 }
710
711 /*
712  * nfs setattr call.
713  *
714  * nfs_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
715  */
716 static int
717 nfs_setattr(struct vop_setattr_args *ap)
718 {
719         struct vnode *vp = ap->a_vp;
720         struct nfsnode *np = VTONFS(vp);
721         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
722         struct vattr *vap = ap->a_vap;
723         int error = 0;
724         off_t tsize;
725         thread_t td = curthread;
726
727 #ifndef nolint
728         tsize = (off_t)0;
729 #endif
730         /*
731          * Setting of flags is not supported.
732          */
733         if (vap->va_flags != VNOVAL)
734                 return (EOPNOTSUPP);
735
736         /*
737          * Disallow write attempts if the filesystem is mounted read-only.
738          */
739         if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
740             vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
741             vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
742             (vp->v_mount->mnt_flag & MNT_RDONLY))
743                 return (EROFS);
744
745         lwkt_gettoken(&nmp->nm_token);
746
747         if (vap->va_size != VNOVAL) {
748                 /*
749                  * truncation requested
750                  */
751                 switch (vp->v_type) {
752                 case VDIR:
753                         lwkt_reltoken(&nmp->nm_token);
754                         return (EISDIR);
755                 case VCHR:
756                 case VBLK:
757                 case VSOCK:
758                 case VFIFO:
759                         if (vap->va_mtime.tv_sec == VNOVAL &&
760                             vap->va_atime.tv_sec == VNOVAL &&
761                             vap->va_mode == (mode_t)VNOVAL &&
762                             vap->va_uid == (uid_t)VNOVAL &&
763                             vap->va_gid == (gid_t)VNOVAL) {
764                                 lwkt_reltoken(&nmp->nm_token);
765                                 return (0);
766                         }
767                         vap->va_size = VNOVAL;
768                         break;
769                 default:
770                         /*
771                          * Disallow write attempts if the filesystem is
772                          * mounted read-only.
773                          */
774                         if (vp->v_mount->mnt_flag & MNT_RDONLY) {
775                                 lwkt_reltoken(&nmp->nm_token);
776                                 return (EROFS);
777                         }
778
779                         tsize = np->n_size;
780 again:
781                         error = nfs_meta_setsize(vp, td, vap->va_size, 0);
782
783 #if 0
784                         if (np->n_flag & NLMODIFIED) {
785                             if (vap->va_size == 0)
786                                 error = nfs_vinvalbuf(vp, 0, 1);
787                             else
788                                 error = nfs_vinvalbuf(vp, V_SAVE, 1);
789                         }
790 #endif
791                         /*
792                          * note: this loop case almost always happens at 
793                          * least once per truncation.
794                          */
795                         if (error == 0 && np->n_size != vap->va_size)
796                                 goto again;
797                         np->n_vattr.va_size = vap->va_size;
798                         break;
799                 }
800         } else if ((np->n_flag & NLMODIFIED) && vp->v_type == VREG) {
801                 /*
802                  * What to do.  If we are modifying the mtime we lose
803                  * mtime detection of changes made by the server or other
804                  * clients.  But programs like rsync/rdist/cpdup are going
805                  * to call utimes a lot.  We don't want to piecemeal sync.
806                  *
807                  * For now sync if any prior remote changes were detected,
808                  * but allow us to lose track of remote changes made during
809                  * the utimes operation.
810                  */
811                 if (np->n_flag & NRMODIFIED)
812                         error = nfs_vinvalbuf(vp, V_SAVE, 1);
813                 if (error == EINTR)
814                         return (error);
815                 if (error == 0) {
816                         if (vap->va_mtime.tv_sec != VNOVAL) {
817                                 np->n_mtime = vap->va_mtime.tv_sec;
818                         }
819                 }
820         }
821         error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
822
823         /*
824          * Sanity check if a truncation was issued.  This should only occur
825          * if multiple processes are racing on the same file.
826          */
827         if (error == 0 && vap->va_size != VNOVAL && 
828             np->n_size != vap->va_size) {
829                 kprintf("NFS ftruncate: server disagrees on the file size: "
830                         "%jd/%jd/%jd\n",
831                         (intmax_t)tsize,
832                         (intmax_t)vap->va_size,
833                         (intmax_t)np->n_size);
834                 goto again;
835         }
836         if (error && vap->va_size != VNOVAL) {
837                 np->n_size = np->n_vattr.va_size = tsize;
838                 nfs_meta_setsize(vp, td, np->n_size, 0);
839         }
840         lwkt_reltoken(&nmp->nm_token);
841
842         return (error);
843 }
844
845 /*
846  * Do an nfs setattr rpc.
847  */
848 static int
849 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
850                struct ucred *cred, struct thread *td)
851 {
852         struct nfsv2_sattr *sp;
853         struct nfsnode *np = VTONFS(vp);
854         u_int32_t *tl;
855         int error = 0, wccflag = NFSV3_WCCRATTR;
856         struct nfsm_info info;
857
858         info.mrep = NULL;
859         info.v3 = NFS_ISV3(vp);
860
861         nfsstats.rpccnt[NFSPROC_SETATTR]++;
862         nfsm_reqhead(&info, vp, NFSPROC_SETATTR,
863                      NFSX_FH(info.v3) + NFSX_SATTR(info.v3));
864         ERROROUT(nfsm_fhtom(&info, vp));
865         if (info.v3) {
866                 nfsm_v3attrbuild(&info, vap, TRUE);
867                 tl = nfsm_build(&info, NFSX_UNSIGNED);
868                 *tl = nfs_false;
869         } else {
870                 sp = nfsm_build(&info, NFSX_V2SATTR);
871                 if (vap->va_mode == (mode_t)VNOVAL)
872                         sp->sa_mode = nfs_xdrneg1;
873                 else
874                         sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
875                 if (vap->va_uid == (uid_t)VNOVAL)
876                         sp->sa_uid = nfs_xdrneg1;
877                 else
878                         sp->sa_uid = txdr_unsigned(vap->va_uid);
879                 if (vap->va_gid == (gid_t)VNOVAL)
880                         sp->sa_gid = nfs_xdrneg1;
881                 else
882                         sp->sa_gid = txdr_unsigned(vap->va_gid);
883                 sp->sa_size = txdr_unsigned(vap->va_size);
884                 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
885                 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
886         }
887         NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_SETATTR, td, cred, &error));
888         if (info.v3) {
889                 np->n_modestamp = 0;
890                 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
891         } else {
892                 ERROROUT(nfsm_loadattr(&info, vp, NULL));
893         }
894         m_freem(info.mrep);
895         info.mrep = NULL;
896 nfsmout:
897         return (error);
898 }
899
900 static
901 void
902 nfs_cache_setvp(struct nchandle *nch, struct vnode *vp, int nctimeout)
903 {
904         if (nctimeout == 0)
905                 nctimeout = 1;
906         else
907                 nctimeout *= hz;
908         cache_setvp(nch, vp);
909         cache_settimeout(nch, nctimeout);
910 }
911
912 /*
913  * NEW API CALL - replaces nfs_lookup().  However, we cannot remove 
914  * nfs_lookup() until all remaining new api calls are implemented.
915  *
916  * Resolve a namecache entry.  This function is passed a locked ncp and
917  * must call nfs_cache_setvp() on it as appropriate to resolve the entry.
918  */
919 static int
920 nfs_nresolve(struct vop_nresolve_args *ap)
921 {
922         struct thread *td = curthread;
923         struct namecache *ncp;
924         struct nfsmount *nmp;
925         struct nfsnode *np;
926         struct vnode *dvp;
927         struct vnode *nvp;
928         nfsfh_t *fhp;
929         int attrflag;
930         int fhsize;
931         int error;
932         int tmp_error;
933         int len;
934         struct nfsm_info info;
935
936         dvp = ap->a_dvp;
937         nmp = VFSTONFS(dvp->v_mount);
938
939         lwkt_gettoken(&nmp->nm_token);
940
941         if ((error = vget(dvp, LK_SHARED)) != 0) {
942                 lwkt_reltoken(&nmp->nm_token);
943                 return (error);
944         }
945
946         info.mrep = NULL;
947         info.v3 = NFS_ISV3(dvp);
948
949         nvp = NULL;
950         nfsstats.lookupcache_misses++;
951         nfsstats.rpccnt[NFSPROC_LOOKUP]++;
952         ncp = ap->a_nch->ncp;
953         len = ncp->nc_nlen;
954         nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
955                      NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
956         ERROROUT(nfsm_fhtom(&info, dvp));
957         ERROROUT(nfsm_strtom(&info, ncp->nc_name, len, NFS_MAXNAMLEN));
958         NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td,
959                                 ap->a_cred, &error));
960         if (error) {
961                 /*
962                  * Cache negatve lookups to reduce NFS traffic, but use
963                  * a fast timeout.  Otherwise use a timeout of 1 tick.
964                  * XXX we should add a namecache flag for no-caching
965                  * to uncache the negative hit as soon as possible, but
966                  * we cannot simply destroy the entry because it is used
967                  * as a placeholder by the caller.
968                  *
969                  * The refactored nfs code will overwrite a non-zero error
970                  * with 0 when we use ERROROUT(), so don't here.
971                  */
972                 if (error == ENOENT)
973                         nfs_cache_setvp(ap->a_nch, NULL, nfsneg_cache_timeout);
974                 tmp_error = nfsm_postop_attr(&info, dvp, &attrflag,
975                                              NFS_LATTR_NOSHRINK);
976                 if (tmp_error) {
977                         error = tmp_error;
978                         goto nfsmout;
979                 }
980                 m_freem(info.mrep);
981                 info.mrep = NULL;
982                 goto nfsmout;
983         }
984
985         /*
986          * Success, get the file handle, do various checks, and load 
987          * post-operation data from the reply packet.  Theoretically
988          * we should never be looking up "." so, theoretically, we
989          * should never get the same file handle as our directory.  But
990          * we check anyway. XXX
991          *
992          * Note that no timeout is set for the positive cache hit.  We
993          * assume, theoretically, that ESTALE returns will be dealt with
994          * properly to handle NFS races and in anycase we cannot depend
995          * on a timeout to deal with NFS open/create/excl issues so instead
996          * of a bad hack here the rest of the NFS client code needs to do
997          * the right thing.
998          */
999         NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
1000
1001         np = VTONFS(dvp);
1002         if (NFS_CMPFH(np, fhp, fhsize)) {
1003                 vref(dvp);
1004                 nvp = dvp;
1005         } else {
1006                 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, NULL);
1007                 if (error) {
1008                         m_freem(info.mrep);
1009                         info.mrep = NULL;
1010                         vput(dvp);
1011                         lwkt_reltoken(&nmp->nm_token);
1012                         return (error);
1013                 }
1014                 nvp = NFSTOV(np);
1015         }
1016         if (info.v3) {
1017                 ERROROUT(nfsm_postop_attr(&info, nvp, &attrflag,
1018                                           NFS_LATTR_NOSHRINK));
1019                 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1020                                           NFS_LATTR_NOSHRINK));
1021         } else {
1022                 ERROROUT(nfsm_loadattr(&info, nvp, NULL));
1023         }
1024         nfs_cache_setvp(ap->a_nch, nvp, nfspos_cache_timeout);
1025         m_freem(info.mrep);
1026         info.mrep = NULL;
1027 nfsmout:
1028         lwkt_reltoken(&nmp->nm_token);
1029         vput(dvp);
1030         if (nvp) {
1031                 if (nvp == dvp)
1032                         vrele(nvp);
1033                 else
1034                         vput(nvp);
1035         }
1036         return (error);
1037 }
1038
1039 /*
1040  * 'cached' nfs directory lookup
1041  *
1042  * NOTE: cannot be removed until NFS implements all the new n*() API calls.
1043  *
1044  * nfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
1045  *            struct componentname *a_cnp)
1046  */
1047 static int
1048 nfs_lookup(struct vop_old_lookup_args *ap)
1049 {
1050         struct componentname *cnp = ap->a_cnp;
1051         struct vnode *dvp = ap->a_dvp;
1052         struct vnode **vpp = ap->a_vpp;
1053         int flags = cnp->cn_flags;
1054         struct vnode *newvp;
1055         struct vnode *notvp;
1056         struct nfsmount *nmp;
1057         long len;
1058         nfsfh_t *fhp;
1059         struct nfsnode *np;
1060         int lockparent, wantparent, attrflag, fhsize;
1061         int error;
1062         int tmp_error;
1063         struct nfsm_info info;
1064
1065         info.mrep = NULL;
1066         info.v3 = NFS_ISV3(dvp);
1067         error = 0;
1068
1069         notvp = (cnp->cn_flags & CNP_NOTVP) ? cnp->cn_notvp : NULL;
1070
1071         /*
1072          * Read-only mount check and directory check.
1073          */
1074         *vpp = NULLVP;
1075         if ((dvp->v_mount->mnt_flag & MNT_RDONLY) &&
1076             (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
1077                 return (EROFS);
1078
1079         if (dvp->v_type != VDIR)
1080                 return (ENOTDIR);
1081
1082         /*
1083          * Look it up in the cache.  Note that ENOENT is only returned if we
1084          * previously entered a negative hit (see later on).  The additional
1085          * nfsneg_cache_timeout check causes previously cached results to
1086          * be instantly ignored if the negative caching is turned off.
1087          */
1088         lockparent = flags & CNP_LOCKPARENT;
1089         wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
1090         nmp = VFSTONFS(dvp->v_mount);
1091         np = VTONFS(dvp);
1092
1093         lwkt_gettoken(&nmp->nm_token);
1094
1095         /*
1096          * Go to the wire.
1097          */
1098         error = 0;
1099         newvp = NULLVP;
1100         nfsstats.lookupcache_misses++;
1101         nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1102         len = cnp->cn_namelen;
1103         nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
1104                      NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
1105         ERROROUT(nfsm_fhtom(&info, dvp));
1106         ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN));
1107         NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, cnp->cn_td,
1108                                 cnp->cn_cred, &error));
1109         if (error) {
1110                 tmp_error = nfsm_postop_attr(&info, dvp, &attrflag,
1111                                              NFS_LATTR_NOSHRINK);
1112                 if (tmp_error) {
1113                         error = tmp_error;
1114                         goto nfsmout;
1115                 }
1116
1117                 m_freem(info.mrep);
1118                 info.mrep = NULL;
1119                 goto nfsmout;
1120         }
1121         NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
1122
1123         /*
1124          * Handle RENAME case...
1125          */
1126         if (cnp->cn_nameiop == NAMEI_RENAME && wantparent) {
1127                 if (NFS_CMPFH(np, fhp, fhsize)) {
1128                         m_freem(info.mrep);
1129                         info.mrep = NULL;
1130                         lwkt_reltoken(&nmp->nm_token);
1131                         return (EISDIR);
1132                 }
1133                 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1134                 if (error) {
1135                         m_freem(info.mrep);
1136                         info.mrep = NULL;
1137                         lwkt_reltoken(&nmp->nm_token);
1138                         return (error);
1139                 }
1140                 newvp = NFSTOV(np);
1141                 if (info.v3) {
1142                         ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
1143                                                   NFS_LATTR_NOSHRINK));
1144                         ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1145                                                   NFS_LATTR_NOSHRINK));
1146                 } else {
1147                         ERROROUT(nfsm_loadattr(&info, newvp, NULL));
1148                 }
1149                 *vpp = newvp;
1150                 m_freem(info.mrep);
1151                 info.mrep = NULL;
1152                 if (!lockparent) {
1153                         vn_unlock(dvp);
1154                         cnp->cn_flags |= CNP_PDIRUNLOCK;
1155                 }
1156                 lwkt_reltoken(&nmp->nm_token);
1157                 return (0);
1158         }
1159
1160         if (flags & CNP_ISDOTDOT) {
1161                 vn_unlock(dvp);
1162                 cnp->cn_flags |= CNP_PDIRUNLOCK;
1163                 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1164                 if (error) {
1165                         vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1166                         cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1167                         lwkt_reltoken(&nmp->nm_token);
1168                         return (error); /* NOTE: return error from nget */
1169                 }
1170                 newvp = NFSTOV(np);
1171                 if (lockparent) {
1172                         error = vn_lock(dvp, LK_EXCLUSIVE);
1173                         if (error) {
1174                                 vput(newvp);
1175                                 lwkt_reltoken(&nmp->nm_token);
1176                                 return (error);
1177                         }
1178                         cnp->cn_flags |= CNP_PDIRUNLOCK;
1179                 }
1180         } else if (NFS_CMPFH(np, fhp, fhsize)) {
1181                 vref(dvp);
1182                 newvp = dvp;
1183         } else {
1184                 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1185                 if (error) {
1186                         m_freem(info.mrep);
1187                         info.mrep = NULL;
1188                         lwkt_reltoken(&nmp->nm_token);
1189                         return (error);
1190                 }
1191                 if (!lockparent) {
1192                         vn_unlock(dvp);
1193                         cnp->cn_flags |= CNP_PDIRUNLOCK;
1194                 }
1195                 newvp = NFSTOV(np);
1196         }
1197         if (info.v3) {
1198                 ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
1199                                           NFS_LATTR_NOSHRINK));
1200                 ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1201                                           NFS_LATTR_NOSHRINK));
1202         } else {
1203                 ERROROUT(nfsm_loadattr(&info, newvp, NULL));
1204         }
1205 #if 0
1206         /* XXX MOVE TO nfs_nremove() */
1207         if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1208             cnp->cn_nameiop != NAMEI_DELETE) {
1209                 np->n_ctime = np->n_vattr.va_ctime.tv_sec; /* XXX */
1210         }
1211 #endif
1212         *vpp = newvp;
1213         m_freem(info.mrep);
1214         info.mrep = NULL;
1215 nfsmout:
1216         if (error) {
1217                 if (newvp != NULLVP) {
1218                         vrele(newvp);
1219                         *vpp = NULLVP;
1220                 }
1221                 if ((cnp->cn_nameiop == NAMEI_CREATE || 
1222                      cnp->cn_nameiop == NAMEI_RENAME) &&
1223                     error == ENOENT) {
1224                         if (!lockparent) {
1225                                 vn_unlock(dvp);
1226                                 cnp->cn_flags |= CNP_PDIRUNLOCK;
1227                         }
1228                         if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1229                                 error = EROFS;
1230                         else
1231                                 error = EJUSTRETURN;
1232                 }
1233         }
1234         lwkt_reltoken(&nmp->nm_token);
1235         return (error);
1236 }
1237
1238 /*
1239  * nfs read call.
1240  * Just call nfs_bioread() to do the work.
1241  *
1242  * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1243  *          struct ucred *a_cred)
1244  */
1245 static int
1246 nfs_read(struct vop_read_args *ap)
1247 {
1248         struct vnode *vp = ap->a_vp;
1249         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1250         int error;
1251
1252         lwkt_gettoken(&nmp->nm_token);
1253         error = nfs_bioread(vp, ap->a_uio, ap->a_ioflag);
1254         lwkt_reltoken(&nmp->nm_token);
1255
1256         return error;
1257 }
1258
1259 /*
1260  * nfs readlink call
1261  *
1262  * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1263  */
1264 static int
1265 nfs_readlink(struct vop_readlink_args *ap)
1266 {
1267         struct vnode *vp = ap->a_vp;
1268         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1269         int error;
1270
1271         if (vp->v_type != VLNK)
1272                 return (EINVAL);
1273
1274         lwkt_gettoken(&nmp->nm_token);
1275         error = nfs_bioread(vp, ap->a_uio, 0);
1276         lwkt_reltoken(&nmp->nm_token);
1277
1278         return error;
1279 }
1280
1281 /*
1282  * Do a readlink rpc.
1283  * Called by nfs_doio() from below the buffer cache.
1284  */
1285 int
1286 nfs_readlinkrpc_uio(struct vnode *vp, struct uio *uiop)
1287 {
1288         int error = 0, len, attrflag;
1289         struct nfsm_info info;
1290
1291         info.mrep = NULL;
1292         info.v3 = NFS_ISV3(vp);
1293
1294         nfsstats.rpccnt[NFSPROC_READLINK]++;
1295         nfsm_reqhead(&info, vp, NFSPROC_READLINK, NFSX_FH(info.v3));
1296         ERROROUT(nfsm_fhtom(&info, vp));
1297         NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READLINK, uiop->uio_td,
1298                                 nfs_vpcred(vp, ND_CHECK), &error));
1299         if (info.v3) {
1300                 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
1301                                           NFS_LATTR_NOSHRINK));
1302         }
1303         if (!error) {
1304                 NEGATIVEOUT(len = nfsm_strsiz(&info, NFS_MAXPATHLEN));
1305                 if (len == NFS_MAXPATHLEN) {
1306                         struct nfsnode *np = VTONFS(vp);
1307                         if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1308                                 len = np->n_size;
1309                 }
1310                 ERROROUT(nfsm_mtouio(&info, uiop, len));
1311         }
1312         m_freem(info.mrep);
1313         info.mrep = NULL;
1314 nfsmout:
1315         return (error);
1316 }
1317
1318 /*
1319  * nfs synchronous read rpc using UIO
1320  */
1321 int
1322 nfs_readrpc_uio(struct vnode *vp, struct uio *uiop)
1323 {
1324         u_int32_t *tl;
1325         struct nfsmount *nmp;
1326         int error = 0, len, retlen, tsiz, eof, attrflag;
1327         struct nfsm_info info;
1328         off_t tmp_off;
1329
1330         info.mrep = NULL;
1331         info.v3 = NFS_ISV3(vp);
1332
1333 #ifndef nolint
1334         eof = 0;
1335 #endif
1336         nmp = VFSTONFS(vp->v_mount);
1337
1338         tsiz = uiop->uio_resid;
1339         tmp_off = uiop->uio_offset + tsiz;
1340         if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset)
1341                 return (EFBIG);
1342         tmp_off = uiop->uio_offset;
1343         while (tsiz > 0) {
1344                 nfsstats.rpccnt[NFSPROC_READ]++;
1345                 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1346                 nfsm_reqhead(&info, vp, NFSPROC_READ,
1347                              NFSX_FH(info.v3) + NFSX_UNSIGNED * 3);
1348                 ERROROUT(nfsm_fhtom(&info, vp));
1349                 tl = nfsm_build(&info, NFSX_UNSIGNED * 3);
1350                 if (info.v3) {
1351                         txdr_hyper(uiop->uio_offset, tl);
1352                         *(tl + 2) = txdr_unsigned(len);
1353                 } else {
1354                         *tl++ = txdr_unsigned(uiop->uio_offset);
1355                         *tl++ = txdr_unsigned(len);
1356                         *tl = 0;
1357                 }
1358                 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READ, uiop->uio_td,
1359                                         nfs_vpcred(vp, ND_READ), &error));
1360                 if (info.v3) {
1361                         ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
1362                                                  NFS_LATTR_NOSHRINK));
1363                         NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
1364                         eof = fxdr_unsigned(int, *(tl + 1));
1365                 } else {
1366                         ERROROUT(nfsm_loadattr(&info, vp, NULL));
1367                 }
1368                 NEGATIVEOUT(retlen = nfsm_strsiz(&info, len));
1369                 ERROROUT(nfsm_mtouio(&info, uiop, retlen));
1370                 m_freem(info.mrep);
1371                 info.mrep = NULL;
1372
1373                 /*
1374                  * Handle short-read from server (NFSv3).  If EOF is not
1375                  * flagged (and no error occurred), but retlen is less
1376                  * then the request size, we must zero-fill the remainder.
1377                  */
1378                 if (retlen < len && info.v3 && eof == 0) {
1379                         ERROROUT(uiomovez(len - retlen, uiop));
1380                         retlen = len;
1381                 }
1382                 tsiz -= retlen;
1383
1384                 /*
1385                  * Terminate loop on EOF or zero-length read.
1386                  *
1387                  * For NFSv2 a short-read indicates EOF, not zero-fill,
1388                  * and also terminates the loop.
1389                  */
1390                 if (info.v3) {
1391                         if (eof || retlen == 0)
1392                                 tsiz = 0;
1393                 } else if (retlen < len) {
1394                         tsiz = 0;
1395                 }
1396         }
1397 nfsmout:
1398         return (error);
1399 }
1400
1401 /*
1402  * nfs write call
1403  */
1404 int
1405 nfs_writerpc_uio(struct vnode *vp, struct uio *uiop,
1406                  int *iomode, int *must_commit)
1407 {
1408         u_int32_t *tl;
1409         int32_t backup;
1410         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1411         int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1412         int  committed = NFSV3WRITE_FILESYNC;
1413         struct nfsm_info info;
1414
1415         info.mrep = NULL;
1416         info.v3 = NFS_ISV3(vp);
1417
1418 #ifndef DIAGNOSTIC
1419         if (uiop->uio_iovcnt != 1)
1420                 panic("nfs: writerpc iovcnt > 1");
1421 #endif
1422         *must_commit = 0;
1423         tsiz = uiop->uio_resid;
1424         if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1425                 return (EFBIG);
1426         while (tsiz > 0) {
1427                 nfsstats.rpccnt[NFSPROC_WRITE]++;
1428                 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1429                 nfsm_reqhead(&info, vp, NFSPROC_WRITE,
1430                              NFSX_FH(info.v3) + 5 * NFSX_UNSIGNED +
1431                              nfsm_rndup(len));
1432                 ERROROUT(nfsm_fhtom(&info, vp));
1433                 if (info.v3) {
1434                         tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
1435                         txdr_hyper(uiop->uio_offset, tl);
1436                         tl += 2;
1437                         *tl++ = txdr_unsigned(len);
1438                         *tl++ = txdr_unsigned(*iomode);
1439                         *tl = txdr_unsigned(len);
1440                 } else {
1441                         u_int32_t x;
1442
1443                         tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1444                         /* Set both "begin" and "current" to non-garbage. */
1445                         x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1446                         *tl++ = x;      /* "begin offset" */
1447                         *tl++ = x;      /* "current offset" */
1448                         x = txdr_unsigned(len);
1449                         *tl++ = x;      /* total to this offset */
1450                         *tl = x;        /* size of this write */
1451                 }
1452                 ERROROUT(nfsm_uiotom(&info, uiop, len));
1453                 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_WRITE, uiop->uio_td,
1454                                         nfs_vpcred(vp, ND_WRITE), &error));
1455                 if (info.v3) {
1456                         /*
1457                          * The write RPC returns a before and after mtime.  The
1458                          * nfsm_wcc_data() macro checks the before n_mtime
1459                          * against the before time and stores the after time
1460                          * in the nfsnode's cached vattr and n_mtime field.
1461                          * The NRMODIFIED bit will be set if the before
1462                          * time did not match the original mtime.
1463                          */
1464                         wccflag = NFSV3_WCCCHK;
1465                         ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
1466                         if (error == 0) {
1467                                 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED + NFSX_V3WRITEVERF));
1468                                 rlen = fxdr_unsigned(int, *tl++);
1469                                 if (rlen == 0) {
1470                                         error = NFSERR_IO;
1471                                         m_freem(info.mrep);
1472                                         info.mrep = NULL;
1473                                         break;
1474                                 } else if (rlen < len) {
1475                                         backup = len - rlen;
1476                                         uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - backup;
1477                                         uiop->uio_iov->iov_len += backup;
1478                                         uiop->uio_offset -= backup;
1479                                         uiop->uio_resid += backup;
1480                                         len = rlen;
1481                                 }
1482                                 commit = fxdr_unsigned(int, *tl++);
1483
1484                                 /*
1485                                  * Return the lowest committment level
1486                                  * obtained by any of the RPCs.
1487                                  */
1488                                 if (committed == NFSV3WRITE_FILESYNC)
1489                                         committed = commit;
1490                                 else if (committed == NFSV3WRITE_DATASYNC &&
1491                                         commit == NFSV3WRITE_UNSTABLE)
1492                                         committed = commit;
1493                                 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1494                                     bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1495                                         NFSX_V3WRITEVERF);
1496                                     nmp->nm_state |= NFSSTA_HASWRITEVERF;
1497                                 } else if (bcmp((caddr_t)tl,
1498                                     (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1499                                     *must_commit = 1;
1500                                     bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1501                                         NFSX_V3WRITEVERF);
1502                                 }
1503                         }
1504                 } else {
1505                         ERROROUT(nfsm_loadattr(&info, vp, NULL));
1506                 }
1507                 m_freem(info.mrep);
1508                 info.mrep = NULL;
1509                 if (error)
1510                         break;
1511                 tsiz -= len;
1512         }
1513 nfsmout:
1514         if (vp->v_mount->mnt_flag & MNT_ASYNC)
1515                 committed = NFSV3WRITE_FILESYNC;
1516         *iomode = committed;
1517         if (error)
1518                 uiop->uio_resid = tsiz;
1519         return (error);
1520 }
1521
1522 /*
1523  * nfs mknod rpc
1524  * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1525  * mode set to specify the file type and the size field for rdev.
1526  */
1527 static int
1528 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1529              struct vattr *vap)
1530 {
1531         struct nfsv2_sattr *sp;
1532         u_int32_t *tl;
1533         struct vnode *newvp = NULL;
1534         struct nfsnode *np = NULL;
1535         struct vattr vattr;
1536         int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1537         int rmajor, rminor;
1538         struct nfsm_info info;
1539
1540         info.mrep = NULL;
1541         info.v3 = NFS_ISV3(dvp);
1542
1543         if (vap->va_type == VCHR || vap->va_type == VBLK) {
1544                 rmajor = txdr_unsigned(vap->va_rmajor);
1545                 rminor = txdr_unsigned(vap->va_rminor);
1546         } else if (vap->va_type == VFIFO || vap->va_type == VSOCK) {
1547                 rmajor = nfs_xdrneg1;
1548                 rminor = nfs_xdrneg1;
1549         } else {
1550                 return (EOPNOTSUPP);
1551         }
1552         if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1553                 return (error);
1554         }
1555         nfsstats.rpccnt[NFSPROC_MKNOD]++;
1556         nfsm_reqhead(&info, dvp, NFSPROC_MKNOD,
1557                      NFSX_FH(info.v3) + 4 * NFSX_UNSIGNED +
1558                      nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(info.v3));
1559         ERROROUT(nfsm_fhtom(&info, dvp));
1560         ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
1561                              NFS_MAXNAMLEN));
1562         if (info.v3) {
1563                 tl = nfsm_build(&info, NFSX_UNSIGNED);
1564                 *tl++ = vtonfsv3_type(vap->va_type);
1565                 nfsm_v3attrbuild(&info, vap, FALSE);
1566                 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1567                         tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
1568                         *tl++ = txdr_unsigned(vap->va_rmajor);
1569                         *tl = txdr_unsigned(vap->va_rminor);
1570                 }
1571         } else {
1572                 sp = nfsm_build(&info, NFSX_V2SATTR);
1573                 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1574                 sp->sa_uid = nfs_xdrneg1;
1575                 sp->sa_gid = nfs_xdrneg1;
1576                 sp->sa_size = makeudev(rmajor, rminor);
1577                 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1578                 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1579         }
1580         NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKNOD, cnp->cn_td,
1581                                 cnp->cn_cred, &error));
1582         if (!error) {
1583                 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
1584                 if (!gotvp) {
1585                         if (newvp) {
1586                                 vput(newvp);
1587                                 newvp = NULL;
1588                         }
1589                         error = nfs_lookitup(dvp, cnp->cn_nameptr,
1590                             cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1591                         if (!error)
1592                                 newvp = NFSTOV(np);
1593                 }
1594         }
1595         if (info.v3) {
1596                 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1597         }
1598         m_freem(info.mrep);
1599         info.mrep = NULL;
1600 nfsmout:
1601         if (error) {
1602                 if (newvp)
1603                         vput(newvp);
1604         } else {
1605                 *vpp = newvp;
1606         }
1607         VTONFS(dvp)->n_flag |= NLMODIFIED;
1608         if (!wccflag)
1609                 VTONFS(dvp)->n_attrstamp = 0;
1610         return (error);
1611 }
1612
1613 /*
1614  * nfs mknod vop
1615  * just call nfs_mknodrpc() to do the work.
1616  *
1617  * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1618  *           struct componentname *a_cnp, struct vattr *a_vap)
1619  */
1620 /* ARGSUSED */
1621 static int
1622 nfs_mknod(struct vop_old_mknod_args *ap)
1623 {
1624         struct nfsmount *nmp = VFSTONFS(ap->a_dvp->v_mount);
1625         int error;
1626
1627         lwkt_gettoken(&nmp->nm_token);
1628         error = nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1629         lwkt_reltoken(&nmp->nm_token);
1630
1631         return error;
1632 }
1633
1634 static u_long create_verf;
1635 /*
1636  * nfs file create call
1637  *
1638  * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1639  *            struct componentname *a_cnp, struct vattr *a_vap)
1640  */
1641 static int
1642 nfs_create(struct vop_old_create_args *ap)
1643 {
1644         struct vnode *dvp = ap->a_dvp;
1645         struct vattr *vap = ap->a_vap;
1646         struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
1647         struct componentname *cnp = ap->a_cnp;
1648         struct nfsv2_sattr *sp;
1649         u_int32_t *tl;
1650         struct nfsnode *np = NULL;
1651         struct vnode *newvp = NULL;
1652         int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1653         struct vattr vattr;
1654         struct nfsm_info info;
1655
1656         info.mrep = NULL;
1657         info.v3 = NFS_ISV3(dvp);
1658         lwkt_gettoken(&nmp->nm_token);
1659
1660         /*
1661          * Oops, not for me..
1662          */
1663         if (vap->va_type == VSOCK) {
1664                 error = nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap);
1665                 lwkt_reltoken(&nmp->nm_token);
1666                 return error;
1667         }
1668
1669         if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1670                 lwkt_reltoken(&nmp->nm_token);
1671                 return (error);
1672         }
1673         if (vap->va_vaflags & VA_EXCLUSIVE)
1674                 fmode |= O_EXCL;
1675 again:
1676         nfsstats.rpccnt[NFSPROC_CREATE]++;
1677         nfsm_reqhead(&info, dvp, NFSPROC_CREATE,
1678                      NFSX_FH(info.v3) + 2 * NFSX_UNSIGNED +
1679                      nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(info.v3));
1680         ERROROUT(nfsm_fhtom(&info, dvp));
1681         ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
1682                              NFS_MAXNAMLEN));
1683         if (info.v3) {
1684                 tl = nfsm_build(&info, NFSX_UNSIGNED);
1685                 if (fmode & O_EXCL) {
1686                         *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1687                         tl = nfsm_build(&info, NFSX_V3CREATEVERF);
1688 #ifdef INET
1689                         if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
1690                                 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia)->sin_addr.s_addr;
1691                         else
1692 #endif
1693                                 *tl++ = create_verf;
1694                         *tl = ++create_verf;
1695                 } else {
1696                         *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1697                         nfsm_v3attrbuild(&info, vap, FALSE);
1698                 }
1699         } else {
1700                 sp = nfsm_build(&info, NFSX_V2SATTR);
1701                 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1702                 sp->sa_uid = nfs_xdrneg1;
1703                 sp->sa_gid = nfs_xdrneg1;
1704                 sp->sa_size = 0;
1705                 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1706                 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1707         }
1708         NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_CREATE, cnp->cn_td,
1709                                 cnp->cn_cred, &error));
1710         if (error == 0) {
1711                 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
1712                 if (!gotvp) {
1713                         if (newvp) {
1714                                 vput(newvp);
1715                                 newvp = NULL;
1716                         }
1717                         error = nfs_lookitup(dvp, cnp->cn_nameptr,
1718                             cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1719                         if (!error)
1720                                 newvp = NFSTOV(np);
1721                 }
1722         }
1723         if (info.v3) {
1724                 if (error == 0)
1725                         error = nfsm_wcc_data(&info, dvp, &wccflag);
1726                 else
1727                         (void)nfsm_wcc_data(&info, dvp, &wccflag);
1728         }
1729         m_freem(info.mrep);
1730         info.mrep = NULL;
1731 nfsmout:
1732         if (error) {
1733                 if (info.v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1734                         KKASSERT(newvp == NULL);
1735                         fmode &= ~O_EXCL;
1736                         goto again;
1737                 }
1738         } else if (info.v3 && (fmode & O_EXCL)) {
1739                 /*
1740                  * We are normally called with only a partially initialized
1741                  * VAP.  Since the NFSv3 spec says that server may use the
1742                  * file attributes to store the verifier, the spec requires
1743                  * us to do a SETATTR RPC. FreeBSD servers store the verifier
1744                  * in atime, but we can't really assume that all servers will
1745                  * so we ensure that our SETATTR sets both atime and mtime.
1746                  */
1747                 if (vap->va_mtime.tv_sec == VNOVAL)
1748                         vfs_timestamp(&vap->va_mtime);
1749                 if (vap->va_atime.tv_sec == VNOVAL)
1750                         vap->va_atime = vap->va_mtime;
1751                 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1752         }
1753         if (error == 0) {
1754                 /*
1755                  * The new np may have enough info for access
1756                  * checks, make sure rucred and wucred are
1757                  * initialized for read and write rpc's.
1758                  */
1759                 np = VTONFS(newvp);
1760                 if (np->n_rucred == NULL)
1761                         np->n_rucred = crhold(cnp->cn_cred);
1762                 if (np->n_wucred == NULL)
1763                         np->n_wucred = crhold(cnp->cn_cred);
1764                 *ap->a_vpp = newvp;
1765         } else if (newvp) {
1766                 vput(newvp);
1767         }
1768         VTONFS(dvp)->n_flag |= NLMODIFIED;
1769         if (!wccflag)
1770                 VTONFS(dvp)->n_attrstamp = 0;
1771         lwkt_reltoken(&nmp->nm_token);
1772         return (error);
1773 }
1774
1775 /*
1776  * nfs file remove call
1777  * To try and make nfs semantics closer to ufs semantics, a file that has
1778  * other processes using the vnode is renamed instead of removed and then
1779  * removed later on the last close.
1780  * - If v_refcnt > 1
1781  *        If a rename is not already in the works
1782  *           call nfs_sillyrename() to set it up
1783  *     else
1784  *        do the remove rpc
1785  *
1786  * nfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
1787  *            struct componentname *a_cnp)
1788  */
1789 static int
1790 nfs_remove(struct vop_old_remove_args *ap)
1791 {
1792         struct vnode *vp = ap->a_vp;
1793         struct vnode *dvp = ap->a_dvp;
1794         struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
1795         struct componentname *cnp = ap->a_cnp;
1796         struct nfsnode *np = VTONFS(vp);
1797         int error = 0;
1798         struct vattr vattr;
1799
1800         lwkt_gettoken(&nmp->nm_token);
1801 #ifndef DIAGNOSTIC
1802         if (VREFCNT(vp) < 1)
1803                 panic("nfs_remove: bad v_refcnt");
1804 #endif
1805         if (vp->v_type == VDIR) {
1806                 error = EPERM;
1807         } else if (VREFCNT(vp) == 1 || (np->n_sillyrename &&
1808                    VOP_GETATTR(vp, &vattr) == 0 && vattr.va_nlink > 1)) {
1809                 /*
1810                  * throw away biocache buffers, mainly to avoid
1811                  * unnecessary delayed writes later.
1812                  */
1813                 error = nfs_vinvalbuf(vp, 0, 1);
1814                 /* Do the rpc */
1815                 if (error != EINTR)
1816                         error = nfs_removerpc(dvp, cnp->cn_nameptr,
1817                                 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
1818                 /*
1819                  * Kludge City: If the first reply to the remove rpc is lost..
1820                  *   the reply to the retransmitted request will be ENOENT
1821                  *   since the file was in fact removed
1822                  *   Therefore, we cheat and return success.
1823                  */
1824                 if (error == ENOENT)
1825                         error = 0;
1826         } else if (!np->n_sillyrename) {
1827                 error = nfs_sillyrename(dvp, vp, cnp);
1828         }
1829         np->n_attrstamp = 0;
1830         lwkt_reltoken(&nmp->nm_token);
1831
1832         return (error);
1833 }
1834
1835 /*
1836  * nfs file remove rpc called from nfs_inactive
1837  */
1838 int
1839 nfs_removeit(struct sillyrename *sp)
1840 {
1841         return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1842                 sp->s_cred, NULL));
1843 }
1844
1845 /*
1846  * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1847  */
1848 static int
1849 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1850               struct ucred *cred, struct thread *td)
1851 {
1852         int error = 0, wccflag = NFSV3_WCCRATTR;
1853         struct nfsm_info info;
1854
1855         info.mrep = NULL;
1856         info.v3 = NFS_ISV3(dvp);
1857
1858         nfsstats.rpccnt[NFSPROC_REMOVE]++;
1859         nfsm_reqhead(&info, dvp, NFSPROC_REMOVE,
1860                      NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1861         ERROROUT(nfsm_fhtom(&info, dvp));
1862         ERROROUT(nfsm_strtom(&info, name, namelen, NFS_MAXNAMLEN));
1863         NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_REMOVE, td, cred, &error));
1864         if (info.v3) {
1865                 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1866         }
1867         m_freem(info.mrep);
1868         info.mrep = NULL;
1869 nfsmout:
1870         VTONFS(dvp)->n_flag |= NLMODIFIED;
1871         if (!wccflag)
1872                 VTONFS(dvp)->n_attrstamp = 0;
1873         return (error);
1874 }
1875
1876 /*
1877  * nfs file rename call
1878  *
1879  * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1880  *            struct componentname *a_fcnp, struct vnode *a_tdvp,
1881  *            struct vnode *a_tvp, struct componentname *a_tcnp)
1882  */
1883 static int
1884 nfs_rename(struct vop_old_rename_args *ap)
1885 {
1886         struct vnode *fvp = ap->a_fvp;
1887         struct vnode *tvp = ap->a_tvp;
1888         struct vnode *fdvp = ap->a_fdvp;
1889         struct vnode *tdvp = ap->a_tdvp;
1890         struct componentname *tcnp = ap->a_tcnp;
1891         struct componentname *fcnp = ap->a_fcnp;
1892         struct nfsmount *nmp = VFSTONFS(fdvp->v_mount);
1893         int error;
1894
1895         lwkt_gettoken(&nmp->nm_token);
1896
1897         /* Check for cross-device rename */
1898         if ((fvp->v_mount != tdvp->v_mount) ||
1899             (tvp && (fvp->v_mount != tvp->v_mount))) {
1900                 error = EXDEV;
1901                 goto out;
1902         }
1903
1904         /*
1905          * We shouldn't have to flush fvp on rename for most server-side
1906          * filesystems as the file handle should not change.  Unfortunately
1907          * the inode for some filesystems (msdosfs) might be tied to the
1908          * file name or directory position so to be completely safe
1909          * vfs.nfs.flush_on_rename is set by default.  Clear to improve
1910          * performance.
1911          *
1912          * We must flush tvp on rename because it might become stale on the
1913          * server after the rename.
1914          */
1915         if (nfs_flush_on_rename)
1916             VOP_FSYNC(fvp, MNT_WAIT, 0);
1917         if (tvp)
1918             VOP_FSYNC(tvp, MNT_WAIT, 0);
1919
1920         /*
1921          * If the tvp exists and is in use, sillyrename it before doing the
1922          * rename of the new file over it.
1923          *
1924          * XXX Can't sillyrename a directory.
1925          *
1926          * We do not attempt to do any namecache purges in this old API
1927          * routine.  The new API compat functions have access to the actual
1928          * namecache structures and will do it for us.
1929          */
1930         if (tvp && VREFCNT(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1931                 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1932                 vput(tvp);
1933                 tvp = NULL;
1934         } else if (tvp) {
1935                 ;
1936         }
1937
1938         error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1939                 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1940                 tcnp->cn_td);
1941
1942 out:
1943         lwkt_reltoken(&nmp->nm_token);
1944         if (tdvp == tvp)
1945                 vrele(tdvp);
1946         else
1947                 vput(tdvp);
1948         if (tvp)
1949                 vput(tvp);
1950         vrele(fdvp);
1951         vrele(fvp);
1952         /*
1953          * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1954          */
1955         if (error == ENOENT)
1956                 error = 0;
1957         return (error);
1958 }
1959
1960 /*
1961  * nfs file rename rpc called from nfs_remove() above
1962  */
1963 static int
1964 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1965              struct sillyrename *sp)
1966 {
1967         return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1968                 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1969 }
1970
1971 /*
1972  * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1973  */
1974 static int
1975 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1976               struct vnode *tdvp, const char *tnameptr, int tnamelen,
1977               struct ucred *cred, struct thread *td)
1978 {
1979         int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1980         struct nfsm_info info;
1981
1982         info.mrep = NULL;
1983         info.v3 = NFS_ISV3(fdvp);
1984
1985         nfsstats.rpccnt[NFSPROC_RENAME]++;
1986         nfsm_reqhead(&info, fdvp, NFSPROC_RENAME,
1987                     (NFSX_FH(info.v3) + NFSX_UNSIGNED)*2 +
1988                     nfsm_rndup(fnamelen) + nfsm_rndup(tnamelen));
1989         ERROROUT(nfsm_fhtom(&info, fdvp));
1990         ERROROUT(nfsm_strtom(&info, fnameptr, fnamelen, NFS_MAXNAMLEN));
1991         ERROROUT(nfsm_fhtom(&info, tdvp));
1992         ERROROUT(nfsm_strtom(&info, tnameptr, tnamelen, NFS_MAXNAMLEN));
1993         NEGKEEPOUT(nfsm_request(&info, fdvp, NFSPROC_RENAME, td, cred, &error));
1994         if (info.v3) {
1995                 ERROROUT(nfsm_wcc_data(&info, fdvp, &fwccflag));
1996                 ERROROUT(nfsm_wcc_data(&info, tdvp, &twccflag));
1997         }
1998         m_freem(info.mrep);
1999         info.mrep = NULL;
2000 nfsmout:
2001         VTONFS(fdvp)->n_flag |= NLMODIFIED;
2002         VTONFS(tdvp)->n_flag |= NLMODIFIED;
2003         if (!fwccflag)
2004                 VTONFS(fdvp)->n_attrstamp = 0;
2005         if (!twccflag)
2006                 VTONFS(tdvp)->n_attrstamp = 0;
2007         return (error);
2008 }
2009
2010 /*
2011  * nfs hard link create call
2012  *
2013  * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
2014  *          struct componentname *a_cnp)
2015  */
2016 static int
2017 nfs_link(struct vop_old_link_args *ap)
2018 {
2019         struct vnode *vp = ap->a_vp;
2020         struct vnode *tdvp = ap->a_tdvp;
2021         struct nfsmount *nmp = VFSTONFS(tdvp->v_mount);
2022         struct componentname *cnp = ap->a_cnp;
2023         int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
2024         struct nfsm_info info;
2025
2026         if (vp->v_mount != tdvp->v_mount) {
2027                 return (EXDEV);
2028         }
2029         lwkt_gettoken(&nmp->nm_token);
2030
2031         /*
2032          * The attribute cache may get out of sync with the server on link.
2033          * Pushing writes to the server before handle was inherited from
2034          * long long ago and it is unclear if we still need to do this.
2035          * Defaults to off.
2036          */
2037         if (nfs_flush_on_hlink)
2038                 VOP_FSYNC(vp, MNT_WAIT, 0);
2039
2040         info.mrep = NULL;
2041         info.v3 = NFS_ISV3(vp);
2042
2043         nfsstats.rpccnt[NFSPROC_LINK]++;
2044         nfsm_reqhead(&info, vp, NFSPROC_LINK,
2045                      NFSX_FH(info.v3) * 2 + NFSX_UNSIGNED +
2046                      nfsm_rndup(cnp->cn_namelen));
2047         ERROROUT(nfsm_fhtom(&info, vp));
2048         ERROROUT(nfsm_fhtom(&info, tdvp));
2049         ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2050                              NFS_MAXNAMLEN));
2051         NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_LINK, cnp->cn_td,
2052                                 cnp->cn_cred, &error));
2053         if (info.v3) {
2054                 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2055                                          NFS_LATTR_NOSHRINK));
2056                 ERROROUT(nfsm_wcc_data(&info, tdvp, &wccflag));
2057         }
2058         m_freem(info.mrep);
2059         info.mrep = NULL;
2060 nfsmout:
2061         VTONFS(tdvp)->n_flag |= NLMODIFIED;
2062         if (!attrflag)
2063                 VTONFS(vp)->n_attrstamp = 0;
2064         if (!wccflag)
2065                 VTONFS(tdvp)->n_attrstamp = 0;
2066         /*
2067          * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2068          */
2069         if (error == EEXIST)
2070                 error = 0;
2071         lwkt_reltoken(&nmp->nm_token);
2072         return (error);
2073 }
2074
2075 /*
2076  * nfs symbolic link create call
2077  *
2078  * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
2079  *              struct componentname *a_cnp, struct vattr *a_vap,
2080  *              char *a_target)
2081  */
2082 static int
2083 nfs_symlink(struct vop_old_symlink_args *ap)
2084 {
2085         struct vnode *dvp = ap->a_dvp;
2086         struct vattr *vap = ap->a_vap;
2087         struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2088         struct componentname *cnp = ap->a_cnp;
2089         struct nfsv2_sattr *sp;
2090         int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
2091         struct vnode *newvp = NULL;
2092         struct nfsm_info info;
2093
2094         info.mrep = NULL;
2095         info.v3 = NFS_ISV3(dvp);
2096         lwkt_gettoken(&nmp->nm_token);
2097
2098         nfsstats.rpccnt[NFSPROC_SYMLINK]++;
2099         slen = strlen(ap->a_target);
2100         nfsm_reqhead(&info, dvp, NFSPROC_SYMLINK,
2101                      NFSX_FH(info.v3) + 2*NFSX_UNSIGNED +
2102                      nfsm_rndup(cnp->cn_namelen) +
2103                      nfsm_rndup(slen) + NFSX_SATTR(info.v3));
2104         ERROROUT(nfsm_fhtom(&info, dvp));
2105         ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2106                              NFS_MAXNAMLEN));
2107         if (info.v3) {
2108                 nfsm_v3attrbuild(&info, vap, FALSE);
2109         }
2110         ERROROUT(nfsm_strtom(&info, ap->a_target, slen, NFS_MAXPATHLEN));
2111         if (info.v3 == 0) {
2112                 sp = nfsm_build(&info, NFSX_V2SATTR);
2113                 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
2114                 sp->sa_uid = nfs_xdrneg1;
2115                 sp->sa_gid = nfs_xdrneg1;
2116                 sp->sa_size = nfs_xdrneg1;
2117                 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2118                 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2119         }
2120
2121         /*
2122          * Issue the NFS request and get the rpc response.
2123          *
2124          * Only NFSv3 responses returning an error of 0 actually return
2125          * a file handle that can be converted into newvp without having
2126          * to do an extra lookup rpc.
2127          */
2128         NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_SYMLINK, cnp->cn_td,
2129                                 cnp->cn_cred, &error));
2130         if (info.v3) {
2131                 if (error == 0) {
2132                        ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2133                 }
2134                 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2135         }
2136
2137         /*
2138          * out code jumps -> here, mrep is also freed.
2139          */
2140
2141         m_freem(info.mrep);
2142         info.mrep = NULL;
2143 nfsmout:
2144
2145         /*
2146          * If we get an EEXIST error, silently convert it to no-error
2147          * in case of an NFS retry.
2148          */
2149         if (error == EEXIST)
2150                 error = 0;
2151
2152         /*
2153          * If we do not have (or no longer have) an error, and we could
2154          * not extract the newvp from the response due to the request being
2155          * NFSv2 or the error being EEXIST.  We have to do a lookup in order
2156          * to obtain a newvp to return.  
2157          */
2158         if (error == 0 && newvp == NULL) {
2159                 struct nfsnode *np = NULL;
2160
2161                 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2162                                      cnp->cn_cred, cnp->cn_td, &np);
2163                 if (!error)
2164                         newvp = NFSTOV(np);
2165         }
2166         if (error) {
2167                 if (newvp)
2168                         vput(newvp);
2169         } else {
2170                 *ap->a_vpp = newvp;
2171         }
2172         VTONFS(dvp)->n_flag |= NLMODIFIED;
2173         if (!wccflag)
2174                 VTONFS(dvp)->n_attrstamp = 0;
2175         lwkt_reltoken(&nmp->nm_token);
2176
2177         return (error);
2178 }
2179
2180 /*
2181  * nfs make dir call
2182  *
2183  * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
2184  *           struct componentname *a_cnp, struct vattr *a_vap)
2185  */
2186 static int
2187 nfs_mkdir(struct vop_old_mkdir_args *ap)
2188 {
2189         struct vnode *dvp = ap->a_dvp;
2190         struct vattr *vap = ap->a_vap;
2191         struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2192         struct componentname *cnp = ap->a_cnp;
2193         struct nfsv2_sattr *sp;
2194         struct nfsnode *np = NULL;
2195         struct vnode *newvp = NULL;
2196         struct vattr vattr;
2197         int error = 0, wccflag = NFSV3_WCCRATTR;
2198         int gotvp = 0;
2199         int len;
2200         struct nfsm_info info;
2201
2202         info.mrep = NULL;
2203         info.v3 = NFS_ISV3(dvp);
2204         lwkt_gettoken(&nmp->nm_token);
2205
2206         if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
2207                 lwkt_reltoken(&nmp->nm_token);
2208                 return (error);
2209         }
2210         len = cnp->cn_namelen;
2211         nfsstats.rpccnt[NFSPROC_MKDIR]++;
2212         nfsm_reqhead(&info, dvp, NFSPROC_MKDIR,
2213                      NFSX_FH(info.v3) + NFSX_UNSIGNED +
2214                      nfsm_rndup(len) + NFSX_SATTR(info.v3));
2215         ERROROUT(nfsm_fhtom(&info, dvp));
2216         ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN));
2217         if (info.v3) {
2218                 nfsm_v3attrbuild(&info, vap, FALSE);
2219         } else {
2220                 sp = nfsm_build(&info, NFSX_V2SATTR);
2221                 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2222                 sp->sa_uid = nfs_xdrneg1;
2223                 sp->sa_gid = nfs_xdrneg1;
2224                 sp->sa_size = nfs_xdrneg1;
2225                 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2226                 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2227         }
2228         NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKDIR, cnp->cn_td,
2229                     cnp->cn_cred, &error));
2230         if (error == 0) {
2231                 ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2232         }
2233         if (info.v3) {
2234                 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2235         }
2236         m_freem(info.mrep);
2237         info.mrep = NULL;
2238 nfsmout:
2239         VTONFS(dvp)->n_flag |= NLMODIFIED;
2240         if (!wccflag)
2241                 VTONFS(dvp)->n_attrstamp = 0;
2242         /*
2243          * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2244          * if we can succeed in looking up the directory.
2245          */
2246         if (error == EEXIST || (!error && !gotvp)) {
2247                 if (newvp) {
2248                         vrele(newvp);
2249                         newvp = NULL;
2250                 }
2251                 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2252                         cnp->cn_td, &np);
2253                 if (!error) {
2254                         newvp = NFSTOV(np);
2255                         if (newvp->v_type != VDIR)
2256                                 error = EEXIST;
2257                 }
2258         }
2259         if (error) {
2260                 if (newvp)
2261                         vrele(newvp);
2262         } else {
2263                 *ap->a_vpp = newvp;
2264         }
2265         lwkt_reltoken(&nmp->nm_token);
2266         return (error);
2267 }
2268
2269 /*
2270  * nfs remove directory call
2271  *
2272  * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2273  *           struct componentname *a_cnp)
2274  */
2275 static int
2276 nfs_rmdir(struct vop_old_rmdir_args *ap)
2277 {
2278         struct vnode *vp = ap->a_vp;
2279         struct vnode *dvp = ap->a_dvp;
2280         struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2281         struct componentname *cnp = ap->a_cnp;
2282         int error = 0, wccflag = NFSV3_WCCRATTR;
2283         struct nfsm_info info;
2284
2285         info.mrep = NULL;
2286         info.v3 = NFS_ISV3(dvp);
2287
2288         if (dvp == vp)
2289                 return (EINVAL);
2290
2291         lwkt_gettoken(&nmp->nm_token);
2292
2293         nfsstats.rpccnt[NFSPROC_RMDIR]++;
2294         nfsm_reqhead(&info, dvp, NFSPROC_RMDIR,
2295                      NFSX_FH(info.v3) + NFSX_UNSIGNED +
2296                      nfsm_rndup(cnp->cn_namelen));
2297         ERROROUT(nfsm_fhtom(&info, dvp));
2298         ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2299                  NFS_MAXNAMLEN));
2300         NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_RMDIR, cnp->cn_td,
2301                                 cnp->cn_cred, &error));
2302         if (info.v3) {
2303                 ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2304         }
2305         m_freem(info.mrep);
2306         info.mrep = NULL;
2307 nfsmout:
2308         VTONFS(dvp)->n_flag |= NLMODIFIED;
2309         if (!wccflag)
2310                 VTONFS(dvp)->n_attrstamp = 0;
2311         /*
2312          * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2313          */
2314         if (error == ENOENT)
2315                 error = 0;
2316         lwkt_reltoken(&nmp->nm_token);
2317
2318         return (error);
2319 }
2320
2321 /*
2322  * nfs readdir call
2323  *
2324  * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2325  */
2326 static int
2327 nfs_readdir(struct vop_readdir_args *ap)
2328 {
2329         struct vnode *vp = ap->a_vp;
2330         struct nfsnode *np = VTONFS(vp);
2331         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2332         struct uio *uio = ap->a_uio;
2333         int tresid, error;
2334         struct vattr vattr;
2335
2336         if (vp->v_type != VDIR)
2337                 return (EPERM);
2338
2339         if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
2340                 return (error);
2341
2342         lwkt_gettoken(&nmp->nm_token);
2343
2344         /*
2345          * If we have a valid EOF offset cache we must call VOP_GETATTR()
2346          * and then check that is still valid, or if this is an NQNFS mount
2347          * we call NQNFS_CKCACHEABLE() instead of VOP_GETATTR().  Note that
2348          * VOP_GETATTR() does not necessarily go to the wire.
2349          */
2350         if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2351             (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0) {
2352                 if (VOP_GETATTR(vp, &vattr) == 0 &&
2353                     (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0
2354                 ) {
2355                         nfsstats.direofcache_hits++;
2356                         goto done;
2357                 }
2358         }
2359
2360         /*
2361          * Call nfs_bioread() to do the real work.  nfs_bioread() does its
2362          * own cache coherency checks so we do not have to.
2363          */
2364         tresid = uio->uio_resid;
2365         error = nfs_bioread(vp, uio, 0);
2366
2367         if (!error && uio->uio_resid == tresid)
2368                 nfsstats.direofcache_misses++;
2369 done:
2370         lwkt_reltoken(&nmp->nm_token);
2371         vn_unlock(vp);
2372
2373         return (error);
2374 }
2375
2376 /*
2377  * Readdir rpc call.  nfs_bioread->nfs_doio->nfs_readdirrpc.
2378  *
2379  * Note that for directories, nfs_bioread maintains the underlying nfs-centric
2380  * offset/block and converts the nfs formatted directory entries for userland
2381  * consumption as well as deals with offsets into the middle of blocks.
2382  * nfs_doio only deals with logical blocks.  In particular, uio_offset will
2383  * be block-bounded.  It must convert to cookies for the actual RPC.
2384  */
2385 int
2386 nfs_readdirrpc_uio(struct vnode *vp, struct uio *uiop)
2387 {
2388         int len, left;
2389         struct nfs_dirent *dp = NULL;
2390         u_int32_t *tl;
2391         nfsuint64 *cookiep;
2392         caddr_t cp;
2393         nfsuint64 cookie;
2394         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2395         struct nfsnode *dnp = VTONFS(vp);
2396         u_quad_t fileno;
2397         int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2398         int attrflag;
2399         struct nfsm_info info;
2400
2401         info.mrep = NULL;
2402         info.v3 = NFS_ISV3(vp);
2403
2404 #ifndef DIAGNOSTIC
2405         if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2406                 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2407                 panic("nfs readdirrpc bad uio");
2408 #endif
2409
2410         /*
2411          * If there is no cookie, assume directory was stale.
2412          */
2413         cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2414         if (cookiep)
2415                 cookie = *cookiep;
2416         else
2417                 return (NFSERR_BAD_COOKIE);
2418         /*
2419          * Loop around doing readdir rpc's of size nm_readdirsize
2420          * truncated to a multiple of DIRBLKSIZ.
2421          * The stopping criteria is EOF or buffer full.
2422          */
2423         while (more_dirs && bigenough) {
2424                 nfsstats.rpccnt[NFSPROC_READDIR]++;
2425                 nfsm_reqhead(&info, vp, NFSPROC_READDIR,
2426                              NFSX_FH(info.v3) + NFSX_READDIR(info.v3));
2427                 ERROROUT(nfsm_fhtom(&info, vp));
2428                 if (info.v3) {
2429                         tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
2430                         *tl++ = cookie.nfsuquad[0];
2431                         *tl++ = cookie.nfsuquad[1];
2432                         *tl++ = dnp->n_cookieverf.nfsuquad[0];
2433                         *tl++ = dnp->n_cookieverf.nfsuquad[1];
2434                 } else {
2435                         /*
2436                          * WARNING!  HAMMER DIRECTORIES WILL NOT WORK WELL
2437                          * WITH NFSv2!!!  There's nothing I can really do
2438                          * about it other than to hope the server supports
2439                          * rdirplus w/NFSv2.
2440                          */
2441                         tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
2442                         *tl++ = cookie.nfsuquad[0];
2443                 }
2444                 *tl = txdr_unsigned(nmp->nm_readdirsize);
2445                 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIR,
2446                                         uiop->uio_td,
2447                                         nfs_vpcred(vp, ND_READ), &error));
2448                 if (info.v3) {
2449                         ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2450                                                   NFS_LATTR_NOSHRINK));
2451                         NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2452                         dnp->n_cookieverf.nfsuquad[0] = *tl++;
2453                         dnp->n_cookieverf.nfsuquad[1] = *tl;
2454                 }
2455                 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2456                 more_dirs = fxdr_unsigned(int, *tl);
2457         
2458                 /* loop thru the dir entries, converting them to std form */
2459                 while (more_dirs && bigenough) {
2460                         if (info.v3) {
2461                                 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2462                                 fileno = fxdr_hyper(tl);
2463                                 len = fxdr_unsigned(int, *(tl + 2));
2464                         } else {
2465                                 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2466                                 fileno = fxdr_unsigned(u_quad_t, *tl++);
2467                                 len = fxdr_unsigned(int, *tl);
2468                         }
2469                         if (len <= 0 || len > NFS_MAXNAMLEN) {
2470                                 error = EBADRPC;
2471                                 m_freem(info.mrep);
2472                                 info.mrep = NULL;
2473                                 goto nfsmout;
2474                         }
2475
2476                         /*
2477                          * len is the number of bytes in the path element
2478                          * name, not including the \0 termination.
2479                          *
2480                          * tlen is the number of bytes w have to reserve for
2481                          * the path element name.
2482                          */
2483                         tlen = nfsm_rndup(len);
2484                         if (tlen == len)
2485                                 tlen += 4;      /* To ensure null termination */
2486
2487                         /*
2488                          * If the entry would cross a DIRBLKSIZ boundary, 
2489                          * extend the previous nfs_dirent to cover the
2490                          * remaining space.
2491                          */
2492                         left = DIRBLKSIZ - blksiz;
2493                         if ((tlen + sizeof(struct nfs_dirent)) > left) {
2494                                 dp->nfs_reclen += left;
2495                                 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2496                                 uiop->uio_iov->iov_len -= left;
2497                                 uiop->uio_offset += left;
2498                                 uiop->uio_resid -= left;
2499                                 blksiz = 0;
2500                         }
2501                         if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2502                                 bigenough = 0;
2503                         if (bigenough) {
2504                                 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2505                                 dp->nfs_ino = fileno;
2506                                 dp->nfs_namlen = len;
2507                                 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2508                                 dp->nfs_type = DT_UNKNOWN;
2509                                 blksiz += dp->nfs_reclen;
2510                                 if (blksiz == DIRBLKSIZ)
2511                                         blksiz = 0;
2512                                 uiop->uio_offset += sizeof(struct nfs_dirent);
2513                                 uiop->uio_resid -= sizeof(struct nfs_dirent);
2514                                 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2515                                 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2516                                 ERROROUT(nfsm_mtouio(&info, uiop, len));
2517
2518                                 /*
2519                                  * The uiop has advanced by nfs_dirent + len
2520                                  * but really needs to advance by
2521                                  * nfs_dirent + tlen
2522                                  */
2523                                 cp = uiop->uio_iov->iov_base;
2524                                 tlen -= len;
2525                                 *cp = '\0';     /* null terminate */
2526                                 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2527                                 uiop->uio_iov->iov_len -= tlen;
2528                                 uiop->uio_offset += tlen;
2529                                 uiop->uio_resid -= tlen;
2530                         } else {
2531                                 /*
2532                                  * NFS strings must be rounded up (nfsm_myouio
2533                                  * handled that in the bigenough case).
2534                                  */
2535                                 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2536                         }
2537                         if (info.v3) {
2538                                 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2539                         } else {
2540                                 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2541                         }
2542
2543                         /*
2544                          * If we were able to accomodate the last entry,
2545                          * get the cookie for the next one.  Otherwise
2546                          * hold-over the cookie for the one we were not
2547                          * able to accomodate.
2548                          */
2549                         if (bigenough) {
2550                                 cookie.nfsuquad[0] = *tl++;
2551                                 if (info.v3)
2552                                         cookie.nfsuquad[1] = *tl++;
2553                         } else if (info.v3) {
2554                                 tl += 2;
2555                         } else {
2556                                 tl++;
2557                         }
2558                         more_dirs = fxdr_unsigned(int, *tl);
2559                 }
2560                 /*
2561                  * If at end of rpc data, get the eof boolean
2562                  */
2563                 if (!more_dirs) {
2564                         NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2565                         more_dirs = (fxdr_unsigned(int, *tl) == 0);
2566                 }
2567                 m_freem(info.mrep);
2568                 info.mrep = NULL;
2569         }
2570         /*
2571          * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2572          * by increasing d_reclen for the last record.
2573          */
2574         if (blksiz > 0) {
2575                 left = DIRBLKSIZ - blksiz;
2576                 dp->nfs_reclen += left;
2577                 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2578                 uiop->uio_iov->iov_len -= left;
2579                 uiop->uio_offset += left;
2580                 uiop->uio_resid -= left;
2581         }
2582
2583         if (bigenough) {
2584                 /*
2585                  * We hit the end of the directory, update direofoffset.
2586                  */
2587                 dnp->n_direofoffset = uiop->uio_offset;
2588         } else {
2589                 /*
2590                  * There is more to go, insert the link cookie so the
2591                  * next block can be read.
2592                  */
2593                 if (uiop->uio_resid > 0)
2594                         kprintf("EEK! readdirrpc resid > 0\n");
2595                 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2596                 *cookiep = cookie;
2597         }
2598 nfsmout:
2599         return (error);
2600 }
2601
2602 /*
2603  * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2604  */
2605 int
2606 nfs_readdirplusrpc_uio(struct vnode *vp, struct uio *uiop)
2607 {
2608         int len, left;
2609         struct nfs_dirent *dp;
2610         u_int32_t *tl;
2611         struct vnode *newvp;
2612         nfsuint64 *cookiep;
2613         caddr_t dpossav1, dpossav2;
2614         caddr_t cp;
2615         struct mbuf *mdsav1, *mdsav2;
2616         nfsuint64 cookie;
2617         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2618         struct nfsnode *dnp = VTONFS(vp), *np;
2619         nfsfh_t *fhp;
2620         u_quad_t fileno;
2621         int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2622         int attrflag, fhsize;
2623         struct nchandle nch;
2624         struct nchandle dnch;
2625         struct nlcomponent nlc;
2626         struct nfsm_info info;
2627
2628         info.mrep = NULL;
2629         info.v3 = 1;
2630
2631 #ifndef nolint
2632         dp = NULL;
2633 #endif
2634 #ifndef DIAGNOSTIC
2635         if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2636                 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2637                 panic("nfs readdirplusrpc bad uio");
2638 #endif
2639         /*
2640          * Obtain the namecache record for the directory so we have something
2641          * to use as a basis for creating the entries.  This function will
2642          * return a held (but not locked) ncp.  The ncp may be disconnected
2643          * from the tree and cannot be used for upward traversals, and the
2644          * ncp may be unnamed.  Note that other unrelated operations may 
2645          * cause the ncp to be named at any time.
2646          *
2647          * We have to lock the ncp to prevent a lock order reversal when
2648          * rdirplus does nlookups of the children, because the vnode is
2649          * locked and has to stay that way.
2650          */
2651         cache_fromdvp(vp, NULL, 0, &dnch);
2652         bzero(&nlc, sizeof(nlc));
2653         newvp = NULLVP;
2654
2655         /*
2656          * If there is no cookie, assume directory was stale.
2657          */
2658         cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2659         if (cookiep) {
2660                 cookie = *cookiep;
2661         } else {
2662                 if (dnch.ncp)
2663                         cache_drop(&dnch);
2664                 return (NFSERR_BAD_COOKIE);
2665         }
2666
2667         /*
2668          * Loop around doing readdir rpc's of size nm_readdirsize
2669          * truncated to a multiple of DIRBLKSIZ.
2670          * The stopping criteria is EOF or buffer full.
2671          */
2672         while (more_dirs && bigenough) {
2673                 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2674                 nfsm_reqhead(&info, vp, NFSPROC_READDIRPLUS,
2675                              NFSX_FH(info.v3) + 6 * NFSX_UNSIGNED);
2676                 ERROROUT(nfsm_fhtom(&info, vp));
2677                 tl = nfsm_build(&info, 6 * NFSX_UNSIGNED);
2678                 *tl++ = cookie.nfsuquad[0];
2679                 *tl++ = cookie.nfsuquad[1];
2680                 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2681                 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2682                 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2683                 *tl = txdr_unsigned(nmp->nm_rsize);
2684                 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIRPLUS,
2685                                         uiop->uio_td,
2686                                         nfs_vpcred(vp, ND_READ), &error));
2687                 ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2688                                           NFS_LATTR_NOSHRINK));
2689                 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2690                 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2691                 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2692                 more_dirs = fxdr_unsigned(int, *tl);
2693
2694                 /* loop thru the dir entries, doctoring them to 4bsd form */
2695                 while (more_dirs && bigenough) {
2696                         NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2697                         fileno = fxdr_hyper(tl);
2698                         len = fxdr_unsigned(int, *(tl + 2));
2699                         if (len <= 0 || len > NFS_MAXNAMLEN) {
2700                                 error = EBADRPC;
2701                                 m_freem(info.mrep);
2702                                 info.mrep = NULL;
2703                                 goto nfsmout;
2704                         }
2705                         tlen = nfsm_rndup(len);
2706                         if (tlen == len)
2707                                 tlen += 4;      /* To ensure null termination*/
2708                         left = DIRBLKSIZ - blksiz;
2709                         if ((tlen + sizeof(struct nfs_dirent)) > left) {
2710                                 dp->nfs_reclen += left;
2711                                 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2712                                 uiop->uio_iov->iov_len -= left;
2713                                 uiop->uio_offset += left;
2714                                 uiop->uio_resid -= left;
2715                                 blksiz = 0;
2716                         }
2717                         if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2718                                 bigenough = 0;
2719                         if (bigenough) {
2720                                 dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2721                                 dp->nfs_ino = fileno;
2722                                 dp->nfs_namlen = len;
2723                                 dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2724                                 dp->nfs_type = DT_UNKNOWN;
2725                                 blksiz += dp->nfs_reclen;
2726                                 if (blksiz == DIRBLKSIZ)
2727                                         blksiz = 0;
2728                                 uiop->uio_offset += sizeof(struct nfs_dirent);
2729                                 uiop->uio_resid -= sizeof(struct nfs_dirent);
2730                                 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2731                                 uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2732                                 nlc.nlc_nameptr = uiop->uio_iov->iov_base;
2733                                 nlc.nlc_namelen = len;
2734                                 ERROROUT(nfsm_mtouio(&info, uiop, len));
2735                                 cp = uiop->uio_iov->iov_base;
2736                                 tlen -= len;
2737                                 *cp = '\0';
2738                                 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2739                                 uiop->uio_iov->iov_len -= tlen;
2740                                 uiop->uio_offset += tlen;
2741                                 uiop->uio_resid -= tlen;
2742                         } else {
2743                                 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2744                         }
2745                         NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2746                         if (bigenough) {
2747                                 cookie.nfsuquad[0] = *tl++;
2748                                 cookie.nfsuquad[1] = *tl++;
2749                         } else {
2750                                 tl += 2;
2751                         }
2752
2753                         /*
2754                          * Since the attributes are before the file handle
2755                          * (sigh), we must skip over the attributes and then
2756                          * come back and get them.
2757                          */
2758                         attrflag = fxdr_unsigned(int, *tl);
2759                         if (attrflag) {
2760                             dpossav1 = info.dpos;
2761                             mdsav1 = info.md;
2762                             ERROROUT(nfsm_adv(&info, NFSX_V3FATTR));
2763                             NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2764                             doit = fxdr_unsigned(int, *tl);
2765                             if (doit) {
2766                                 NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
2767                             }
2768                             if (doit && bigenough && !nlcdegenerate(&nlc) &&
2769                                 !NFS_CMPFH(dnp, fhp, fhsize)
2770                             ) {
2771                                 if (dnch.ncp) {
2772 #if 0
2773                                     kprintf("NFS/READDIRPLUS, ENTER %*.*s\n",
2774                                         nlc.nlc_namelen, nlc.nlc_namelen,
2775                                         nlc.nlc_nameptr);
2776 #endif
2777                                     /*
2778                                      * This is a bit hokey but there isn't
2779                                      * much we can do about it.  We can't
2780                                      * hold the directory vp locked while
2781                                      * doing lookups and gets.
2782                                      */
2783                                     nch = cache_nlookup_nonblock(&dnch, &nlc);
2784                                     if (nch.ncp == NULL)
2785                                         goto rdfail;
2786                                     cache_setunresolved(&nch);
2787                                     error = nfs_nget_nonblock(vp->v_mount, fhp,
2788                                                               fhsize, &np,
2789                                                               NULL);
2790                                     if (error) {
2791                                         cache_put(&nch);
2792                                         goto rdfail;
2793                                     }
2794                                     newvp = NFSTOV(np);
2795                                     dpossav2 = info.dpos;
2796                                     info.dpos = dpossav1;
2797                                     mdsav2 = info.md;
2798                                     info.md = mdsav1;
2799                                     ERROROUT(nfsm_loadattr(&info, newvp, NULL));
2800                                     info.dpos = dpossav2;
2801                                     info.md = mdsav2;
2802                                     dp->nfs_type =
2803                                             IFTODT(VTTOIF(np->n_vattr.va_type));
2804                                     nfs_cache_setvp(&nch, newvp,
2805                                                     nfspos_cache_timeout);
2806                                     vput(newvp);
2807                                     newvp = NULLVP;
2808                                     cache_put(&nch);
2809                                 } else {
2810 rdfail:
2811                                     ;
2812 #if 0
2813                                     kprintf("Warning: NFS/rddirplus, "
2814                                             "UNABLE TO ENTER %*.*s\n",
2815                                         nlc.nlc_namelen, nlc.nlc_namelen,
2816                                         nlc.nlc_nameptr);
2817 #endif
2818                                 }
2819                             }
2820                         } else {
2821                             /* Just skip over the file handle */
2822                             NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2823                             i = fxdr_unsigned(int, *tl);
2824                             ERROROUT(nfsm_adv(&info, nfsm_rndup(i)));
2825                         }
2826                         NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2827                         more_dirs = fxdr_unsigned(int, *tl);
2828                 }
2829                 /*
2830                  * If at end of rpc data, get the eof boolean
2831                  */
2832                 if (!more_dirs) {
2833                         NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2834                         more_dirs = (fxdr_unsigned(int, *tl) == 0);
2835                 }
2836                 m_freem(info.mrep);
2837                 info.mrep = NULL;
2838         }
2839         /*
2840          * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2841          * by increasing d_reclen for the last record.
2842          */
2843         if (blksiz > 0) {
2844                 left = DIRBLKSIZ - blksiz;
2845                 dp->nfs_reclen += left;
2846                 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2847                 uiop->uio_iov->iov_len -= left;
2848                 uiop->uio_offset += left;
2849                 uiop->uio_resid -= left;
2850         }
2851
2852         /*
2853          * We are now either at the end of the directory or have filled the
2854          * block.
2855          */
2856         if (bigenough) {
2857                 dnp->n_direofoffset = uiop->uio_offset;
2858         } else {
2859                 if (uiop->uio_resid > 0)
2860                         kprintf("EEK! readdirplusrpc resid > 0\n");
2861                 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2862                 *cookiep = cookie;
2863         }
2864 nfsmout:
2865         if (newvp != NULLVP) {
2866                 if (newvp == vp)
2867                         vrele(newvp);
2868                 else
2869                         vput(newvp);
2870                 newvp = NULLVP;
2871         }
2872         if (dnch.ncp)
2873                 cache_drop(&dnch);
2874         return (error);
2875 }
2876
2877 /*
2878  * Silly rename. To make the NFS filesystem that is stateless look a little
2879  * more like the "ufs" a remove of an active vnode is translated to a rename
2880  * to a funny looking filename that is removed by nfs_inactive on the
2881  * nfsnode. There is the potential for another process on a different client
2882  * to create the same funny name between the nfs_lookitup() fails and the
2883  * nfs_rename() completes, but...
2884  */
2885 static int
2886 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2887 {
2888         struct sillyrename *sp;
2889         struct nfsnode *np;
2890         int error;
2891
2892         /*
2893          * We previously purged dvp instead of vp.  I don't know why, it
2894          * completely destroys performance.  We can't do it anyway with the
2895          * new VFS API since we would be breaking the namecache topology.
2896          */
2897         cache_purge(vp);        /* XXX */
2898         np = VTONFS(vp);
2899 #ifndef DIAGNOSTIC
2900         if (vp->v_type == VDIR)
2901                 panic("nfs: sillyrename dir");
2902 #endif
2903         sp = kmalloc(sizeof(struct sillyrename), M_NFSREQ, M_WAITOK);
2904         sp->s_cred = crdup(cnp->cn_cred);
2905         sp->s_dvp = dvp;
2906         vref(dvp);
2907
2908         /* Fudge together a funny name */
2909         sp->s_namlen = ksprintf(sp->s_name, ".nfsA%08x4.4",
2910                                 (int)(intptr_t)cnp->cn_td);
2911
2912         /* Try lookitups until we get one that isn't there */
2913         while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2914                 cnp->cn_td, NULL) == 0) {
2915                 sp->s_name[4]++;
2916                 if (sp->s_name[4] > 'z') {
2917                         error = EINVAL;
2918                         goto bad;
2919                 }
2920         }
2921         error = nfs_renameit(dvp, cnp, sp);
2922         if (error)
2923                 goto bad;
2924         error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2925                 cnp->cn_td, &np);
2926         np->n_sillyrename = sp;
2927         return (0);
2928 bad:
2929         vrele(sp->s_dvp);
2930         crfree(sp->s_cred);
2931         kfree((caddr_t)sp, M_NFSREQ);
2932         return (error);
2933 }
2934
2935 /*
2936  * Look up a file name and optionally either update the file handle or
2937  * allocate an nfsnode, depending on the value of npp.
2938  * npp == NULL  --> just do the lookup
2939  * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2940  *                      handled too
2941  * *npp != NULL --> update the file handle in the vnode
2942  */
2943 static int
2944 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2945              struct thread *td, struct nfsnode **npp)
2946 {
2947         struct vnode *newvp = NULL;
2948         struct nfsnode *np, *dnp = VTONFS(dvp);
2949         int error = 0, fhlen, attrflag;
2950         nfsfh_t *nfhp;
2951         struct nfsm_info info;
2952
2953         info.mrep = NULL;
2954         info.v3 = NFS_ISV3(dvp);
2955
2956         nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2957         nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
2958                      NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2959         ERROROUT(nfsm_fhtom(&info, dvp));
2960         ERROROUT(nfsm_strtom(&info, name, len, NFS_MAXNAMLEN));
2961         NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td, cred, &error));
2962         if (npp && !error) {
2963                 NEGATIVEOUT(fhlen = nfsm_getfh(&info, &nfhp));
2964                 if (*npp) {
2965                     np = *npp;
2966                     if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2967                         kfree((caddr_t)np->n_fhp, M_NFSBIGFH);
2968                         np->n_fhp = &np->n_fh;
2969                     } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2970                         np->n_fhp =(nfsfh_t *)kmalloc(fhlen,M_NFSBIGFH,M_WAITOK);
2971                     bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2972                     np->n_fhsize = fhlen;
2973                     newvp = NFSTOV(np);
2974                 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2975                     vref(dvp);
2976                     newvp = dvp;
2977                 } else {
2978                     error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np, NULL);
2979                     if (error) {
2980                         m_freem(info.mrep);
2981                         info.mrep = NULL;
2982                         return (error);
2983                     }
2984                     newvp = NFSTOV(np);
2985                 }
2986                 if (info.v3) {
2987                         ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
2988                                                   NFS_LATTR_NOSHRINK));
2989                         if (!attrflag && *npp == NULL) {
2990                                 m_freem(info.mrep);
2991                                 info.mrep = NULL;
2992                                 if (newvp == dvp)
2993                                         vrele(newvp);
2994                                 else
2995                                         vput(newvp);
2996                                 return (ENOENT);
2997                         }
2998                 } else {
2999                         ERROROUT(nfsm_loadattr(&info, newvp, NULL));
3000                 }
3001         }
3002         m_freem(info.mrep);
3003         info.mrep = NULL;
3004 nfsmout:
3005         if (npp && *npp == NULL) {
3006                 if (error) {
3007                         if (newvp) {
3008                                 if (newvp == dvp)
3009                                         vrele(newvp);
3010                                 else
3011                                         vput(newvp);
3012                         }
3013                 } else
3014                         *npp = np;
3015         }
3016         return (error);
3017 }
3018
3019 /*
3020  * Nfs Version 3 commit rpc
3021  *
3022  * We call it 'uio' to distinguish it from 'bio' but there is no real uio
3023  * involved.
3024  */
3025 int
3026 nfs_commitrpc_uio(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
3027 {
3028         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3029         int error = 0, wccflag = NFSV3_WCCRATTR;
3030         struct nfsm_info info;
3031         u_int32_t *tl;
3032
3033         info.mrep = NULL;
3034         info.v3 = 1;
3035         
3036         if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
3037                 return (0);
3038         nfsstats.rpccnt[NFSPROC_COMMIT]++;
3039         nfsm_reqhead(&info, vp, NFSPROC_COMMIT, NFSX_FH(1));
3040         ERROROUT(nfsm_fhtom(&info, vp));
3041         tl = nfsm_build(&info, 3 * NFSX_UNSIGNED);
3042         txdr_hyper(offset, tl);
3043         tl += 2;
3044         *tl = txdr_unsigned(cnt);
3045         NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_COMMIT, td,
3046                                 nfs_vpcred(vp, ND_WRITE), &error));
3047         ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
3048         if (!error) {
3049                 NULLOUT(tl = nfsm_dissect(&info, NFSX_V3WRITEVERF));
3050                 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
3051                         NFSX_V3WRITEVERF)) {
3052                         bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
3053                                 NFSX_V3WRITEVERF);
3054                         error = NFSERR_STALEWRITEVERF;
3055                 }
3056         }
3057         m_freem(info.mrep);
3058         info.mrep = NULL;
3059 nfsmout:
3060         return (error);
3061 }
3062
3063 /*
3064  * Kludge City..
3065  * - make nfs_bmap() essentially a no-op that does no translation
3066  * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
3067  *   (Maybe I could use the process's page mapping, but I was concerned that
3068  *    Kernel Write might not be enabled and also figured copyout() would do
3069  *    a lot more work than bcopy() and also it currently happens in the
3070  *    context of the swapper process (2).
3071  *
3072  * nfs_bmap(struct vnode *a_vp, off_t a_loffset,
3073  *          off_t *a_doffsetp, int *a_runp, int *a_runb)
3074  */
3075 static int
3076 nfs_bmap(struct vop_bmap_args *ap)
3077 {
3078         /* no token lock required */
3079         if (ap->a_doffsetp != NULL)
3080                 *ap->a_doffsetp = ap->a_loffset;
3081         if (ap->a_runp != NULL)
3082                 *ap->a_runp = 0;
3083         if (ap->a_runb != NULL)
3084                 *ap->a_runb = 0;
3085         return (0);
3086 }
3087
3088 /*
3089  * Strategy routine.
3090  */
3091 static int
3092 nfs_strategy(struct vop_strategy_args *ap)
3093 {
3094         struct bio *bio = ap->a_bio;
3095         struct bio *nbio;
3096         struct buf *bp __debugvar = bio->bio_buf;
3097         struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3098         struct thread *td;
3099         int error;
3100
3101         KASSERT(bp->b_cmd != BUF_CMD_DONE,
3102                 ("nfs_strategy: buffer %p unexpectedly marked done", bp));
3103         KASSERT(BUF_REFCNT(bp) > 0,
3104                 ("nfs_strategy: buffer %p not locked", bp));
3105
3106         if (bio->bio_flags & BIO_SYNC)
3107                 td = curthread; /* XXX */
3108         else
3109                 td = NULL;
3110
3111         lwkt_gettoken(&nmp->nm_token);
3112
3113         /*
3114          * We probably don't need to push an nbio any more since no
3115          * block conversion is required due to the use of 64 bit byte
3116          * offsets, but do it anyway.
3117          *
3118          * NOTE: When NFS callers itself via this strategy routines and
3119          *       sets up a synchronous I/O, it expects the I/O to run
3120          *       synchronously (its bio_done routine just assumes it),
3121          *       so for now we have to honor the bit.
3122          */
3123         nbio = push_bio(bio);
3124         nbio->bio_offset = bio->bio_offset;
3125         nbio->bio_flags = bio->bio_flags & BIO_SYNC;
3126
3127         /*
3128          * If the op is asynchronous and an i/o daemon is waiting
3129          * queue the request, wake it up and wait for completion
3130          * otherwise just do it ourselves.
3131          */
3132         if (bio->bio_flags & BIO_SYNC) {
3133                 error = nfs_doio(ap->a_vp, nbio, td);
3134         } else {
3135                 nfs_asyncio(ap->a_vp, nbio);
3136                 error = 0;
3137         }
3138         lwkt_reltoken(&nmp->nm_token);
3139
3140         return (error);
3141 }
3142
3143 /*
3144  * Mmap a file
3145  *
3146  * NB Currently unsupported.
3147  *
3148  * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred)
3149  */
3150 /* ARGSUSED */
3151 static int
3152 nfs_mmap(struct vop_mmap_args *ap)
3153 {
3154         /* no token lock required */
3155         return (EINVAL);
3156 }
3157
3158 /*
3159  * fsync vnode op. Just call nfs_flush() with commit == 1.
3160  *
3161  * nfs_fsync(struct vnode *a_vp, int a_waitfor)
3162  */
3163 /* ARGSUSED */
3164 static int
3165 nfs_fsync(struct vop_fsync_args *ap)
3166 {
3167         struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3168         int error;
3169
3170         lwkt_gettoken(&nmp->nm_token);
3171
3172         /*
3173          * NOTE: Because attributes are set synchronously we currently
3174          *       do not have to implement vsetisdirty()/vclrisdirty().
3175          */
3176         error = nfs_flush(ap->a_vp, ap->a_waitfor, curthread, 1);
3177
3178         lwkt_reltoken(&nmp->nm_token);
3179
3180         return error;
3181 }
3182
3183 /*
3184  * Flush all the blocks associated with a vnode.   Dirty NFS buffers may be
3185  * in one of two states:  If B_NEEDCOMMIT is clear then the buffer contains
3186  * new NFS data which needs to be written to the server.  If B_NEEDCOMMIT is
3187  * set the buffer contains data that has already been written to the server
3188  * and which now needs a commit RPC.
3189  *
3190  * If commit is 0 we only take one pass and only flush buffers containing new
3191  * dirty data.
3192  *
3193  * If commit is 1 we take two passes, issuing a commit RPC in the second
3194  * pass.
3195  *
3196  * If waitfor is MNT_WAIT and commit is 1, we loop as many times as required
3197  * to completely flush all pending data.
3198  *
3199  * Note that the RB_SCAN code properly handles the case where the
3200  * callback might block and directly or indirectly (another thread) cause
3201  * the RB tree to change.
3202  */
3203
3204 #ifndef NFS_COMMITBVECSIZ
3205 #define NFS_COMMITBVECSIZ       16
3206 #endif
3207
3208 struct nfs_flush_info {
3209         enum { NFI_FLUSHNEW, NFI_COMMIT } mode;
3210         struct thread *td;
3211         struct vnode *vp;
3212         int waitfor;
3213         int slpflag;
3214         int slptimeo;
3215         int loops;
3216         struct buf *bvary[NFS_COMMITBVECSIZ];
3217         int bvsize;
3218         off_t beg_off;
3219         off_t end_off;
3220 };
3221
3222 static int nfs_flush_bp(struct buf *bp, void *data);
3223 static int nfs_flush_docommit(struct nfs_flush_info *info, int error);
3224
3225 int
3226 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
3227 {
3228         struct nfsnode *np = VTONFS(vp);
3229         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3230         struct nfs_flush_info info;
3231         int error;
3232
3233         bzero(&info, sizeof(info));
3234         info.td = td;
3235         info.vp = vp;
3236         info.waitfor = waitfor;
3237         info.slpflag = (nmp->nm_flag & NFSMNT_INT) ? PCATCH : 0;
3238         info.loops = 0;
3239         lwkt_gettoken(&vp->v_token);
3240
3241         do {
3242                 /*
3243                  * Flush mode
3244                  */
3245                 info.mode = NFI_FLUSHNEW;
3246                 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL, 
3247                                 nfs_flush_bp, &info);
3248
3249                 /*
3250                  * Take a second pass if committing and no error occured.  
3251                  * Clean up any left over collection (whether an error 
3252                  * occurs or not).
3253                  */
3254                 if (commit && error == 0) {
3255                         info.mode = NFI_COMMIT;
3256                         error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL, 
3257                                         nfs_flush_bp, &info);
3258                         if (info.bvsize)
3259                                 error = nfs_flush_docommit(&info, error);
3260                 }
3261
3262                 /*
3263                  * Wait for pending I/O to complete before checking whether
3264                  * any further dirty buffers exist.
3265                  */
3266                 while (waitfor == MNT_WAIT &&
3267                        bio_track_active(&vp->v_track_write)) {
3268                         error = bio_track_wait(&vp->v_track_write,
3269                                                info.slpflag, info.slptimeo);
3270                         if (error) {
3271                                 /*
3272                                  * We have to be able to break out if this 
3273                                  * is an 'intr' mount.
3274                                  */
3275                                 if (nfs_sigintr(nmp, NULL, td)) {
3276                                         error = -EINTR;
3277                                         break;
3278                                 }
3279
3280                                 /*
3281                                  * Since we do not process pending signals,
3282                                  * once we get a PCATCH our tsleep() will no
3283                                  * longer sleep, switch to a fixed timeout
3284                                  * instead.
3285                                  */
3286                                 if (info.slpflag == PCATCH) {
3287                                         info.slpflag = 0;
3288                                         info.slptimeo = 2 * hz;
3289                                 }
3290                                 error = 0;
3291                         }
3292                 }
3293                 ++info.loops;
3294                 /*
3295                  * Loop if we are flushing synchronous as well as committing,
3296                  * and dirty buffers are still present.  Otherwise we might livelock.
3297                  */
3298         } while (waitfor == MNT_WAIT && commit && 
3299                  error == 0 && !RB_EMPTY(&vp->v_rbdirty_tree));
3300
3301         /*
3302          * The callbacks have to return a negative error to terminate the
3303          * RB scan.
3304          */
3305         if (error < 0)
3306                 error = -error;
3307
3308         /*
3309          * Deal with any error collection
3310          */
3311         if (np->n_flag & NWRITEERR) {
3312                 error = np->n_error;
3313                 np->n_flag &= ~NWRITEERR;
3314         }
3315         lwkt_reltoken(&vp->v_token);
3316         return (error);
3317 }
3318
3319 static
3320 int
3321 nfs_flush_bp(struct buf *bp, void *data)
3322 {
3323         struct nfs_flush_info *info = data;
3324         int lkflags;
3325         int error;
3326         off_t toff;
3327
3328         error = 0;
3329         switch(info->mode) {
3330         case NFI_FLUSHNEW:
3331                 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3332                 if (error && info->loops && info->waitfor == MNT_WAIT) {
3333                         error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3334                         if (error) {
3335                                 lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
3336                                 if (info->slpflag & PCATCH)
3337                                         lkflags |= LK_PCATCH;
3338                                 error = BUF_TIMELOCK(bp, lkflags, "nfsfsync",
3339                                                      info->slptimeo);
3340                         }
3341                 }
3342
3343                 /*
3344                  * Ignore locking errors
3345                  */
3346                 if (error) {
3347                         error = 0;
3348                         break;
3349                 }
3350
3351                 /*
3352                  * The buffer may have changed out from under us, even if
3353                  * we did not block (MPSAFE).  Check again now that it is
3354                  * locked.
3355                  */
3356                 if (bp->b_vp == info->vp &&
3357                     (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) == B_DELWRI) {
3358                         bremfree(bp);
3359                         bawrite(bp);
3360                 } else {
3361                         BUF_UNLOCK(bp);
3362                 }
3363                 break;
3364         case NFI_COMMIT:
3365                 /*
3366                  * Only process buffers in need of a commit which we can
3367                  * immediately lock.  This may prevent a buffer from being
3368                  * committed, but the normal flush loop will block on the
3369                  * same buffer so we shouldn't get into an endless loop.
3370                  */
3371                 if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) != 
3372                     (B_DELWRI | B_NEEDCOMMIT)) {
3373                         break;
3374                 }
3375                 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
3376                         break;
3377
3378                 /*
3379                  * We must recheck after successfully locking the buffer.
3380                  */
3381                 if (bp->b_vp != info->vp ||
3382                     (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3383                     (B_DELWRI | B_NEEDCOMMIT)) {
3384                         BUF_UNLOCK(bp);
3385                         break;
3386                 }
3387
3388                 /*
3389                  * NOTE: storing the bp in the bvary[] basically sets
3390                  * it up for a commit operation.
3391                  *
3392                  * We must call vfs_busy_pages() now so the commit operation
3393                  * is interlocked with user modifications to memory mapped
3394                  * pages.  The b_dirtyoff/b_dirtyend range is not correct
3395                  * until after the pages have been busied.
3396                  *
3397                  * Note: to avoid loopback deadlocks, we do not
3398                  * assign b_runningbufspace.
3399                  */
3400                 bremfree(bp);
3401                 bp->b_cmd = BUF_CMD_WRITE;
3402                 vfs_busy_pages(bp->b_vp, bp);
3403                 info->bvary[info->bvsize] = bp;
3404                 toff = bp->b_bio2.bio_offset + bp->b_dirtyoff;
3405                 if (info->bvsize == 0 || toff < info->beg_off)
3406                         info->beg_off = toff;
3407                 toff += (off_t)(bp->b_dirtyend - bp->b_dirtyoff);
3408                 if (info->bvsize == 0 || toff > info->end_off)
3409                         info->end_off = toff;
3410                 ++info->bvsize;
3411                 if (info->bvsize == NFS_COMMITBVECSIZ) {
3412                         error = nfs_flush_docommit(info, 0);
3413                         KKASSERT(info->bvsize == 0);
3414                 }
3415         }
3416         return (error);
3417 }
3418
3419 static
3420 int
3421 nfs_flush_docommit(struct nfs_flush_info *info, int error)
3422 {
3423         struct vnode *vp;
3424         struct buf *bp;
3425         off_t bytes;
3426         int retv;
3427         int i;
3428
3429         vp = info->vp;
3430
3431         if (info->bvsize > 0) {
3432                 /*
3433                  * Commit data on the server, as required.  Note that
3434                  * nfs_commit will use the vnode's cred for the commit.
3435                  * The NFSv3 commit RPC is limited to a 32 bit byte count.
3436                  */
3437                 bytes = info->end_off - info->beg_off;
3438                 if (bytes > 0x40000000)
3439                         bytes = 0x40000000;
3440                 if (error) {
3441                         retv = -error;
3442                 } else {
3443                         retv = nfs_commitrpc_uio(vp, info->beg_off,
3444                                                  (int)bytes, info->td);
3445                         if (retv == NFSERR_STALEWRITEVERF)
3446                                 nfs_clearcommit(vp->v_mount);
3447                 }
3448
3449                 /*
3450                  * Now, either mark the blocks I/O done or mark the
3451                  * blocks dirty, depending on whether the commit
3452                  * succeeded.
3453                  */
3454                 for (i = 0; i < info->bvsize; ++i) {
3455                         bp = info->bvary[i];
3456                         if (retv || (bp->b_flags & B_NEEDCOMMIT) == 0) {
3457                                 /*
3458                                  * Either an error or the original
3459                                  * vfs_busy_pages() cleared B_NEEDCOMMIT
3460                                  * due to finding new dirty VM pages in
3461                                  * the buffer.
3462                                  *
3463                                  * Leave B_DELWRI intact.
3464                                  */
3465                                 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3466                                 vfs_unbusy_pages(bp);
3467                                 bp->b_cmd = BUF_CMD_DONE;
3468                                 bqrelse(bp);
3469                         } else {
3470                                 /*
3471                                  * Success, remove B_DELWRI ( bundirty() ).
3472                                  *
3473                                  * b_dirtyoff/b_dirtyend seem to be NFS 
3474                                  * specific.  We should probably move that
3475                                  * into bundirty(). XXX
3476                                  *
3477                                  * We are faking an I/O write, we have to 
3478                                  * start the transaction in order to
3479                                  * immediately biodone() it.
3480                                  */
3481                                 bundirty(bp);
3482                                 bp->b_flags &= ~B_ERROR;
3483                                 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3484                                 bp->b_dirtyoff = bp->b_dirtyend = 0;
3485                                 biodone(&bp->b_bio1);
3486                         }
3487                 }
3488                 info->bvsize = 0;
3489         }
3490         return (error);
3491 }
3492
3493 /*
3494  * NFS advisory byte-level locks.
3495  * Currently unsupported.
3496  *
3497  * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3498  *              int a_flags)
3499  */
3500 static int
3501 nfs_advlock(struct vop_advlock_args *ap)
3502 {
3503         struct nfsnode *np = VTONFS(ap->a_vp);
3504
3505         /* no token lock currently required */
3506         /*
3507          * The following kludge is to allow diskless support to work
3508          * until a real NFS lockd is implemented. Basically, just pretend
3509          * that this is a local lock.
3510          */
3511         return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3512 }
3513
3514 /*
3515  * Print out the contents of an nfsnode.
3516  *
3517  * nfs_print(struct vnode *a_vp)
3518  */
3519 static int
3520 nfs_print(struct vop_print_args *ap)
3521 {
3522         struct vnode *vp = ap->a_vp;
3523         struct nfsnode *np = VTONFS(vp);
3524
3525         kprintf("tag VT_NFS, fileid %lld fsid 0x%x",
3526                 (long long)np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3527         if (vp->v_type == VFIFO)
3528                 fifo_printinfo(vp);
3529         kprintf("\n");
3530         return (0);
3531 }
3532
3533 /*
3534  * nfs special file access vnode op.
3535  *
3536  * nfs_laccess(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
3537  */
3538 static int
3539 nfs_laccess(struct vop_access_args *ap)
3540 {
3541         struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3542         struct vattr vattr;
3543         int error;
3544
3545         lwkt_gettoken(&nmp->nm_token);
3546         error = VOP_GETATTR(ap->a_vp, &vattr);
3547         if (error == 0) {
3548                 error = vop_helper_access(ap, vattr.va_uid, vattr.va_gid, 
3549                                           vattr.va_mode, 0);
3550         }
3551         lwkt_reltoken(&nmp->nm_token);
3552
3553         return (error);
3554 }
3555
3556 /*
3557  * Read wrapper for fifos.
3558  *
3559  * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3560  *              struct ucred *a_cred)
3561  */
3562 static int
3563 nfsfifo_read(struct vop_read_args *ap)
3564 {
3565         struct nfsnode *np = VTONFS(ap->a_vp);
3566
3567         /* no token access required */
3568         /*
3569          * Set access flag.
3570          */
3571         np->n_flag |= NACC;
3572         getnanotime(&np->n_atim);
3573         return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3574 }
3575
3576 /*
3577  * Write wrapper for fifos.
3578  *
3579  * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3580  *               struct ucred *a_cred)
3581  */
3582 static int
3583 nfsfifo_write(struct vop_write_args *ap)
3584 {
3585         struct nfsnode *np = VTONFS(ap->a_vp);
3586
3587         /* no token access required */
3588         /*
3589          * Set update flag.
3590          */
3591         np->n_flag |= NUPD;
3592         getnanotime(&np->n_mtim);
3593         return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3594 }
3595
3596 /*
3597  * Close wrapper for fifos.
3598  *
3599  * Update the times on the nfsnode then do fifo close.
3600  *
3601  * nfsfifo_close(struct vnode *a_vp, int a_fflag)
3602  */
3603 static int
3604 nfsfifo_close(struct vop_close_args *ap)
3605 {
3606         struct vnode *vp = ap->a_vp;
3607         struct nfsnode *np = VTONFS(vp);
3608         struct vattr vattr;
3609         struct timespec ts;
3610
3611         /* no token access required */
3612
3613         vn_lock(vp, LK_UPGRADE | LK_RETRY); /* XXX */
3614         if (np->n_flag & (NACC | NUPD)) {
3615                 getnanotime(&ts);
3616                 if (np->n_flag & NACC)
3617                         np->n_atim = ts;
3618                 if (np->n_flag & NUPD)
3619                         np->n_mtim = ts;
3620                 np->n_flag |= NCHG;
3621                 if (VREFCNT(vp) == 1 &&
3622                     (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3623                         VATTR_NULL(&vattr);
3624                         if (np->n_flag & NACC)
3625                                 vattr.va_atime = np->n_atim;
3626                         if (np->n_flag & NUPD)
3627                                 vattr.va_mtime = np->n_mtim;
3628                         (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE));
3629                 }
3630         }
3631         return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3632 }
3633