]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/fs/nfsclient/nfs_clvnops.c
MFC: r214048, r214053
[FreeBSD/stable/8.git] / sys / fs / nfsclient / nfs_clvnops.c
1 /*-
2  * Copyright (c) 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Macklem at The University of Guelph.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *      from nfs_vnops.c        8.16 (Berkeley) 5/27/95
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 /*
39  * vnode op calls for Sun NFS version 2, 3 and 4
40  */
41
42 #include "opt_inet.h"
43
44 #include <sys/param.h>
45 #include <sys/kernel.h>
46 #include <sys/systm.h>
47 #include <sys/resourcevar.h>
48 #include <sys/proc.h>
49 #include <sys/mount.h>
50 #include <sys/bio.h>
51 #include <sys/buf.h>
52 #include <sys/jail.h>
53 #include <sys/malloc.h>
54 #include <sys/mbuf.h>
55 #include <sys/namei.h>
56 #include <sys/socket.h>
57 #include <sys/vnode.h>
58 #include <sys/dirent.h>
59 #include <sys/fcntl.h>
60 #include <sys/lockf.h>
61 #include <sys/stat.h>
62 #include <sys/sysctl.h>
63 #include <sys/signalvar.h>
64
65 #include <vm/vm.h>
66 #include <vm/vm_object.h>
67 #include <vm/vm_extern.h>
68 #include <vm/vm_object.h>
69
70 #include <fs/nfs/nfsport.h>
71 #include <fs/nfsclient/nfsnode.h>
72 #include <fs/nfsclient/nfsmount.h>
73 #include <fs/nfsclient/nfs.h>
74
75 #include <net/if.h>
76 #include <netinet/in.h>
77 #include <netinet/in_var.h>
78
79 #include <nfs/nfs_lock.h>
80
81 /* Defs */
82 #define TRUE    1
83 #define FALSE   0
84
85 extern struct nfsstats newnfsstats;
86 MALLOC_DECLARE(M_NEWNFSREQ);
87
88 /*
89  * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these
90  * calls are not in getblk() and brelse() so that they would not be necessary
91  * here.
92  */
93 #ifndef B_VMIO
94 #define vfs_busy_pages(bp, f)
95 #endif
96
97 static vop_read_t       nfsfifo_read;
98 static vop_write_t      nfsfifo_write;
99 static vop_close_t      nfsfifo_close;
100 static int      nfs_setattrrpc(struct vnode *, struct vattr *, struct ucred *,
101                     struct thread *);
102 static vop_lookup_t     nfs_lookup;
103 static vop_create_t     nfs_create;
104 static vop_mknod_t      nfs_mknod;
105 static vop_open_t       nfs_open;
106 static vop_close_t      nfs_close;
107 static vop_access_t     nfs_access;
108 static vop_getattr_t    nfs_getattr;
109 static vop_setattr_t    nfs_setattr;
110 static vop_read_t       nfs_read;
111 static vop_fsync_t      nfs_fsync;
112 static vop_remove_t     nfs_remove;
113 static vop_link_t       nfs_link;
114 static vop_rename_t     nfs_rename;
115 static vop_mkdir_t      nfs_mkdir;
116 static vop_rmdir_t      nfs_rmdir;
117 static vop_symlink_t    nfs_symlink;
118 static vop_readdir_t    nfs_readdir;
119 static vop_strategy_t   nfs_strategy;
120 static vop_lock1_t      nfs_lock1;
121 static  int     nfs_lookitup(struct vnode *, char *, int,
122                     struct ucred *, struct thread *, struct nfsnode **);
123 static  int     nfs_sillyrename(struct vnode *, struct vnode *,
124                     struct componentname *);
125 static vop_access_t     nfsspec_access;
126 static vop_readlink_t   nfs_readlink;
127 static vop_print_t      nfs_print;
128 static vop_advlock_t    nfs_advlock;
129 static vop_advlockasync_t nfs_advlockasync;
130 #ifdef NFS4_ACL_EXTATTR_NAME
131 static vop_getacl_t nfs_getacl;
132 static vop_setacl_t nfs_setacl;
133 #endif
134
135 /*
136  * Global vfs data structures for nfs
137  */
138 struct vop_vector newnfs_vnodeops = {
139         .vop_default =          &default_vnodeops,
140         .vop_access =           nfs_access,
141         .vop_advlock =          nfs_advlock,
142         .vop_advlockasync =     nfs_advlockasync,
143         .vop_close =            nfs_close,
144         .vop_create =           nfs_create,
145         .vop_fsync =            nfs_fsync,
146         .vop_getattr =          nfs_getattr,
147         .vop_getpages =         ncl_getpages,
148         .vop_putpages =         ncl_putpages,
149         .vop_inactive =         ncl_inactive,
150         .vop_link =             nfs_link,
151         .vop_lock1 =            nfs_lock1,
152         .vop_lookup =           nfs_lookup,
153         .vop_mkdir =            nfs_mkdir,
154         .vop_mknod =            nfs_mknod,
155         .vop_open =             nfs_open,
156         .vop_print =            nfs_print,
157         .vop_read =             nfs_read,
158         .vop_readdir =          nfs_readdir,
159         .vop_readlink =         nfs_readlink,
160         .vop_reclaim =          ncl_reclaim,
161         .vop_remove =           nfs_remove,
162         .vop_rename =           nfs_rename,
163         .vop_rmdir =            nfs_rmdir,
164         .vop_setattr =          nfs_setattr,
165         .vop_strategy =         nfs_strategy,
166         .vop_symlink =          nfs_symlink,
167         .vop_write =            ncl_write,
168 #ifdef NFS4_ACL_EXTATTR_NAME
169         .vop_getacl =           nfs_getacl,
170         .vop_setacl =           nfs_setacl,
171 #endif
172 };
173
174 struct vop_vector newnfs_fifoops = {
175         .vop_default =          &fifo_specops,
176         .vop_access =           nfsspec_access,
177         .vop_close =            nfsfifo_close,
178         .vop_fsync =            nfs_fsync,
179         .vop_getattr =          nfs_getattr,
180         .vop_inactive =         ncl_inactive,
181         .vop_print =            nfs_print,
182         .vop_read =             nfsfifo_read,
183         .vop_reclaim =          ncl_reclaim,
184         .vop_setattr =          nfs_setattr,
185         .vop_write =            nfsfifo_write,
186 };
187
188 static int nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp,
189     struct componentname *cnp, struct vattr *vap);
190 static int nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
191     int namelen, struct ucred *cred, struct thread *td);
192 static int nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp,
193     char *fnameptr, int fnamelen, struct vnode *tdvp, struct vnode *tvp,
194     char *tnameptr, int tnamelen, struct ucred *cred, struct thread *td);
195 static int nfs_renameit(struct vnode *sdvp, struct vnode *svp,
196     struct componentname *scnp, struct sillyrename *sp);
197
198 /*
199  * Global variables
200  */
201 #define DIRHDSIZ        (sizeof (struct dirent) - (MAXNAMLEN + 1))
202
203 SYSCTL_DECL(_vfs_newnfs);
204
205 static int      nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
206 SYSCTL_INT(_vfs_newnfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
207            &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
208
209 static int      nfs_prime_access_cache = 0;
210 SYSCTL_INT(_vfs_newnfs, OID_AUTO, prime_access_cache, CTLFLAG_RW,
211            &nfs_prime_access_cache, 0,
212            "Prime NFS ACCESS cache when fetching attributes");
213
214 static int      newnfs_commit_on_close = 0;
215 SYSCTL_INT(_vfs_newnfs, OID_AUTO, commit_on_close, CTLFLAG_RW,
216     &newnfs_commit_on_close, 0, "write+commit on close, else only write");
217
218 static int      nfs_clean_pages_on_close = 1;
219 SYSCTL_INT(_vfs_newnfs, OID_AUTO, clean_pages_on_close, CTLFLAG_RW,
220            &nfs_clean_pages_on_close, 0, "NFS clean dirty pages on close");
221
222 int newnfs_directio_enable = 0;
223 SYSCTL_INT(_vfs_newnfs, OID_AUTO, directio_enable, CTLFLAG_RW,
224            &newnfs_directio_enable, 0, "Enable NFS directio");
225
226 /*
227  * This sysctl allows other processes to mmap a file that has been opened
228  * O_DIRECT by a process.  In general, having processes mmap the file while
229  * Direct IO is in progress can lead to Data Inconsistencies.  But, we allow
230  * this by default to prevent DoS attacks - to prevent a malicious user from
231  * opening up files O_DIRECT preventing other users from mmap'ing these
232  * files.  "Protected" environments where stricter consistency guarantees are
233  * required can disable this knob.  The process that opened the file O_DIRECT
234  * cannot mmap() the file, because mmap'ed IO on an O_DIRECT open() is not
235  * meaningful.
236  */
237 int newnfs_directio_allow_mmap = 1;
238 SYSCTL_INT(_vfs_newnfs, OID_AUTO, directio_allow_mmap, CTLFLAG_RW,
239            &newnfs_directio_allow_mmap, 0, "Enable mmaped IO on file with O_DIRECT opens");
240
241 #if 0
242 SYSCTL_INT(_vfs_newnfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
243            &newnfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
244
245 SYSCTL_INT(_vfs_newnfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
246            &newnfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
247 #endif
248
249 #define NFSACCESS_ALL (NFSACCESS_READ | NFSACCESS_MODIFY                \
250                          | NFSACCESS_EXTEND | NFSACCESS_EXECUTE \
251                          | NFSACCESS_DELETE | NFSACCESS_LOOKUP)
252
253 /*
254  * SMP Locking Note :
255  * The list of locks after the description of the lock is the ordering
256  * of other locks acquired with the lock held.
257  * np->n_mtx : Protects the fields in the nfsnode.
258        VM Object Lock
259        VI_MTX (acquired indirectly)
260  * nmp->nm_mtx : Protects the fields in the nfsmount.
261        rep->r_mtx
262  * ncl_iod_mutex : Global lock, protects shared nfsiod state.
263  * nfs_reqq_mtx : Global lock, protects the nfs_reqq list.
264        nmp->nm_mtx
265        rep->r_mtx
266  * rep->r_mtx : Protects the fields in an nfsreq.
267  */
268
269 static int
270 nfs34_access_otw(struct vnode *vp, int wmode, struct thread *td,
271     struct ucred *cred, u_int32_t *retmode)
272 {
273         int error = 0, attrflag, i, lrupos;
274         u_int32_t rmode;
275         struct nfsnode *np = VTONFS(vp);
276         struct nfsvattr nfsva;
277
278         error = nfsrpc_accessrpc(vp, wmode, cred, td, &nfsva, &attrflag,
279             &rmode, NULL);
280         if (attrflag)
281                 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
282         if (!error) {
283                 lrupos = 0;
284                 mtx_lock(&np->n_mtx);
285                 for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
286                         if (np->n_accesscache[i].uid == cred->cr_uid) {
287                                 np->n_accesscache[i].mode = rmode;
288                                 np->n_accesscache[i].stamp = time_second;
289                                 break;
290                         }
291                         if (i > 0 && np->n_accesscache[i].stamp <
292                             np->n_accesscache[lrupos].stamp)
293                                 lrupos = i;
294                 }
295                 if (i == NFS_ACCESSCACHESIZE) {
296                         np->n_accesscache[lrupos].uid = cred->cr_uid;
297                         np->n_accesscache[lrupos].mode = rmode;
298                         np->n_accesscache[lrupos].stamp = time_second;
299                 }
300                 mtx_unlock(&np->n_mtx);
301                 if (retmode != NULL)
302                         *retmode = rmode;
303         } else if (NFS_ISV4(vp)) {
304                 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
305         }
306         return (error);
307 }
308
309 /*
310  * nfs access vnode op.
311  * For nfs version 2, just return ok. File accesses may fail later.
312  * For nfs version 3, use the access rpc to check accessibility. If file modes
313  * are changed on the server, accesses might still fail later.
314  */
315 static int
316 nfs_access(struct vop_access_args *ap)
317 {
318         struct vnode *vp = ap->a_vp;
319         int error = 0, i, gotahit;
320         u_int32_t mode, wmode, rmode;
321         int v34 = NFS_ISV34(vp);
322         struct nfsnode *np = VTONFS(vp);
323
324         /*
325          * Disallow write attempts on filesystems mounted read-only;
326          * unless the file is a socket, fifo, or a block or character
327          * device resident on the filesystem.
328          */
329         if ((ap->a_accmode & (VWRITE | VAPPEND
330 #ifdef NFS4_ACL_EXTATTR_NAME
331             | VWRITE_NAMED_ATTRS | VDELETE_CHILD | VWRITE_ATTRIBUTES |
332             VDELETE | VWRITE_ACL | VWRITE_OWNER
333 #endif
334             )) != 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
335                 switch (vp->v_type) {
336                 case VREG:
337                 case VDIR:
338                 case VLNK:
339                         return (EROFS);
340                 default:
341                         break;
342                 }
343         }
344         /*
345          * For nfs v3 or v4, 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 (v34) {
353                 if (ap->a_accmode & VREAD)
354                         mode = NFSACCESS_READ;
355                 else
356                         mode = 0;
357                 if (vp->v_type != VDIR) {
358                         if (ap->a_accmode & VWRITE)
359                                 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
360                         if (ap->a_accmode & VAPPEND)
361                                 mode |= NFSACCESS_EXTEND;
362                         if (ap->a_accmode & VEXEC)
363                                 mode |= NFSACCESS_EXECUTE;
364 #ifdef NFS4_ACL_EXTATTR_NAME
365                         if (ap->a_accmode & VDELETE)
366                                 mode |= NFSACCESS_DELETE;
367 #endif
368                 } else {
369                         if (ap->a_accmode & VWRITE)
370                                 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
371                         if (ap->a_accmode & VAPPEND)
372                                 mode |= NFSACCESS_EXTEND;
373                         if (ap->a_accmode & VEXEC)
374                                 mode |= NFSACCESS_LOOKUP;
375 #ifdef NFS4_ACL_EXTATTR_NAME
376                         if (ap->a_accmode & VDELETE)
377                                 mode |= NFSACCESS_DELETE;
378                         if (ap->a_accmode & VDELETE_CHILD)
379                                 mode |= NFSACCESS_MODIFY;
380 #endif
381                 }
382                 /* XXX safety belt, only make blanket request if caching */
383                 if (nfsaccess_cache_timeout > 0) {
384                         wmode = NFSACCESS_READ | NFSACCESS_MODIFY |
385                                 NFSACCESS_EXTEND | NFSACCESS_EXECUTE |
386                                 NFSACCESS_DELETE | NFSACCESS_LOOKUP;
387                 } else {
388                         wmode = mode;
389                 }
390
391                 /*
392                  * Does our cached result allow us to give a definite yes to
393                  * this request?
394                  */
395                 gotahit = 0;
396                 mtx_lock(&np->n_mtx);
397                 for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
398                         if (ap->a_cred->cr_uid == np->n_accesscache[i].uid) {
399                             if (time_second < (np->n_accesscache[i].stamp
400                                 + nfsaccess_cache_timeout) &&
401                                 (np->n_accesscache[i].mode & mode) == mode) {
402                                 NFSINCRGLOBAL(newnfsstats.accesscache_hits);
403                                 gotahit = 1;
404                             }
405                             break;
406                         }
407                 }
408                 mtx_unlock(&np->n_mtx);
409                 if (gotahit == 0) {
410                         /*
411                          * Either a no, or a don't know.  Go to the wire.
412                          */
413                         NFSINCRGLOBAL(newnfsstats.accesscache_misses);
414                         error = nfs34_access_otw(vp, wmode, ap->a_td,
415                             ap->a_cred, &rmode);
416                         if (!error &&
417                             (rmode & mode) != mode)
418                                 error = EACCES;
419                 }
420                 return (error);
421         } else {
422                 if ((error = nfsspec_access(ap)) != 0) {
423                         return (error);
424                 }
425                 /*
426                  * Attempt to prevent a mapped root from accessing a file
427                  * which it shouldn't.  We try to read a byte from the file
428                  * if the user is root and the file is not zero length.
429                  * After calling nfsspec_access, we should have the correct
430                  * file size cached.
431                  */
432                 mtx_lock(&np->n_mtx);
433                 if (ap->a_cred->cr_uid == 0 && (ap->a_accmode & VREAD)
434                     && VTONFS(vp)->n_size > 0) {
435                         struct iovec aiov;
436                         struct uio auio;
437                         char buf[1];
438
439                         mtx_unlock(&np->n_mtx);
440                         aiov.iov_base = buf;
441                         aiov.iov_len = 1;
442                         auio.uio_iov = &aiov;
443                         auio.uio_iovcnt = 1;
444                         auio.uio_offset = 0;
445                         auio.uio_resid = 1;
446                         auio.uio_segflg = UIO_SYSSPACE;
447                         auio.uio_rw = UIO_READ;
448                         auio.uio_td = ap->a_td;
449
450                         if (vp->v_type == VREG)
451                                 error = ncl_readrpc(vp, &auio, ap->a_cred);
452                         else if (vp->v_type == VDIR) {
453                                 char* bp;
454                                 bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
455                                 aiov.iov_base = bp;
456                                 aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
457                                 error = ncl_readdirrpc(vp, &auio, ap->a_cred,
458                                     ap->a_td);
459                                 free(bp, M_TEMP);
460                         } else if (vp->v_type == VLNK)
461                                 error = ncl_readlinkrpc(vp, &auio, ap->a_cred);
462                         else
463                                 error = EACCES;
464                 } else
465                         mtx_unlock(&np->n_mtx);
466                 return (error);
467         }
468 }
469
470
471 /*
472  * nfs open vnode op
473  * Check to see if the type is ok
474  * and that deletion is not in progress.
475  * For paged in text files, you will need to flush the page cache
476  * if consistency is lost.
477  */
478 /* ARGSUSED */
479 static int
480 nfs_open(struct vop_open_args *ap)
481 {
482         struct vnode *vp = ap->a_vp;
483         struct nfsnode *np = VTONFS(vp);
484         struct vattr vattr;
485         int error;
486         int fmode = ap->a_mode;
487
488         if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
489                 return (EOPNOTSUPP);
490
491         /*
492          * For NFSv4, we need to do the Open Op before cache validation,
493          * so that we conform to RFC3530 Sec. 9.3.1.
494          */
495         if (NFS_ISV4(vp)) {
496                 error = nfsrpc_open(vp, fmode, ap->a_cred, ap->a_td);
497                 if (error) {
498                         error = nfscl_maperr(ap->a_td, error, (uid_t)0,
499                             (gid_t)0);
500                         return (error);
501                 }
502         }
503
504         /*
505          * Now, if this Open will be doing reading, re-validate/flush the
506          * cache, so that Close/Open coherency is maintained.
507          */
508         if ((fmode & FREAD) != 0 &&
509             (!NFS_ISV4(vp) || nfscl_mustflush(vp) != 0)) {
510                 mtx_lock(&np->n_mtx);
511                 if (np->n_flag & NMODIFIED) {
512                         mtx_unlock(&np->n_mtx);                 
513                         error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
514                         if (error == EINTR || error == EIO) {
515                                 if (NFS_ISV4(vp))
516                                         (void) nfsrpc_close(vp, 0, ap->a_td);
517                                 return (error);
518                         }
519                         mtx_lock(&np->n_mtx);
520                         np->n_attrstamp = 0;
521                         if (vp->v_type == VDIR)
522                                 np->n_direofoffset = 0;
523                         mtx_unlock(&np->n_mtx);
524                         error = VOP_GETATTR(vp, &vattr, ap->a_cred);
525                         if (error) {
526                                 if (NFS_ISV4(vp))
527                                         (void) nfsrpc_close(vp, 0, ap->a_td);
528                                 return (error);
529                         }
530                         mtx_lock(&np->n_mtx);
531                         np->n_mtime = vattr.va_mtime;
532                         if (NFS_ISV4(vp))
533                                 np->n_change = vattr.va_filerev;
534                         mtx_unlock(&np->n_mtx);
535                 } else {
536                         mtx_unlock(&np->n_mtx);                                         
537                         error = VOP_GETATTR(vp, &vattr, ap->a_cred);
538                         if (error) {
539                                 if (NFS_ISV4(vp))
540                                         (void) nfsrpc_close(vp, 0, ap->a_td);
541                                 return (error);
542                         }
543                         mtx_lock(&np->n_mtx);
544                         if ((NFS_ISV4(vp) && np->n_change != vattr.va_filerev) ||
545                             NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
546                                 if (vp->v_type == VDIR)
547                                         np->n_direofoffset = 0;
548                                 mtx_unlock(&np->n_mtx);
549                                 error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
550                                 if (error == EINTR || error == EIO) {
551                                         if (NFS_ISV4(vp))
552                                                 (void) nfsrpc_close(vp, 0,
553                                                     ap->a_td);
554                                         return (error);
555                                 }
556                                 mtx_lock(&np->n_mtx);
557                                 np->n_mtime = vattr.va_mtime;
558                                 if (NFS_ISV4(vp))
559                                         np->n_change = vattr.va_filerev;
560                         }
561                         mtx_unlock(&np->n_mtx);
562                 }
563         }
564
565         /*
566          * If the object has >= 1 O_DIRECT active opens, we disable caching.
567          */
568         if (newnfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
569                 if (np->n_directio_opens == 0) {
570                         error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
571                         if (error) {
572                                 if (NFS_ISV4(vp))
573                                         (void) nfsrpc_close(vp, 0, ap->a_td);
574                                 return (error);
575                         }
576                         mtx_lock(&np->n_mtx);
577                         np->n_flag |= NNONCACHE;
578                 } else {
579                         mtx_lock(&np->n_mtx);
580                 }
581                 np->n_directio_opens++;
582                 mtx_unlock(&np->n_mtx);
583         }
584         vnode_create_vobject(vp, vattr.va_size, ap->a_td);
585         return (0);
586 }
587
588 /*
589  * nfs close vnode op
590  * What an NFS client should do upon close after writing is a debatable issue.
591  * Most NFS clients push delayed writes to the server upon close, basically for
592  * two reasons:
593  * 1 - So that any write errors may be reported back to the client process
594  *     doing the close system call. By far the two most likely errors are
595  *     NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
596  * 2 - To put a worst case upper bound on cache inconsistency between
597  *     multiple clients for the file.
598  * There is also a consistency problem for Version 2 of the protocol w.r.t.
599  * not being able to tell if other clients are writing a file concurrently,
600  * since there is no way of knowing if the changed modify time in the reply
601  * is only due to the write for this client.
602  * (NFS Version 3 provides weak cache consistency data in the reply that
603  *  should be sufficient to detect and handle this case.)
604  *
605  * The current code does the following:
606  * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
607  * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
608  *                     or commit them (this satisfies 1 and 2 except for the
609  *                     case where the server crashes after this close but
610  *                     before the commit RPC, which is felt to be "good
611  *                     enough". Changing the last argument to ncl_flush() to
612  *                     a 1 would force a commit operation, if it is felt a
613  *                     commit is necessary now.
614  * for NFS Version 4 - flush the dirty buffers and commit them, if
615  *                     nfscl_mustflush() says this is necessary.
616  *                     It is necessary if there is no write delegation held,
617  *                     in order to satisfy open/close coherency.
618  *                     If the file isn't cached on local stable storage,
619  *                     it may be necessary in order to detect "out of space"
620  *                     errors from the server, if the write delegation
621  *                     issued by the server doesn't allow the file to grow.
622  */
623 /* ARGSUSED */
624 static int
625 nfs_close(struct vop_close_args *ap)
626 {
627         struct vnode *vp = ap->a_vp;
628         struct nfsnode *np = VTONFS(vp);
629         struct nfsvattr nfsva;
630         struct ucred *cred;
631         int error = 0, ret, localcred = 0;
632         int fmode = ap->a_fflag;
633
634         if ((vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF))
635                 return (0);
636         /*
637          * During shutdown, a_cred isn't valid, so just use root.
638          */
639         if (ap->a_cred == NOCRED) {
640                 cred = newnfs_getcred();
641                 localcred = 1;
642         } else {
643                 cred = ap->a_cred;
644         }
645         if (vp->v_type == VREG) {
646             /*
647              * Examine and clean dirty pages, regardless of NMODIFIED.
648              * This closes a major hole in close-to-open consistency.
649              * We want to push out all dirty pages (and buffers) on
650              * close, regardless of whether they were dirtied by
651              * mmap'ed writes or via write().
652              */
653             if (nfs_clean_pages_on_close && vp->v_object) {
654                 VM_OBJECT_LOCK(vp->v_object);
655                 vm_object_page_clean(vp->v_object, 0, 0, 0);
656                 VM_OBJECT_UNLOCK(vp->v_object);
657             }
658             mtx_lock(&np->n_mtx);
659             if (np->n_flag & NMODIFIED) {
660                 mtx_unlock(&np->n_mtx);
661                 if (NFS_ISV3(vp)) {
662                     /*
663                      * Under NFSv3 we have dirty buffers to dispose of.  We
664                      * must flush them to the NFS server.  We have the option
665                      * of waiting all the way through the commit rpc or just
666                      * waiting for the initial write.  The default is to only
667                      * wait through the initial write so the data is in the
668                      * server's cache, which is roughly similar to the state
669                      * a standard disk subsystem leaves the file in on close().
670                      *
671                      * We cannot clear the NMODIFIED bit in np->n_flag due to
672                      * potential races with other processes, and certainly
673                      * cannot clear it if we don't commit.
674                      * These races occur when there is no longer the old
675                      * traditional vnode locking implemented for Vnode Ops.
676                      */
677                     int cm = newnfs_commit_on_close ? 1 : 0;
678                     error = ncl_flush(vp, MNT_WAIT, cred, ap->a_td, cm, 0);
679                     /* np->n_flag &= ~NMODIFIED; */
680                 } else if (NFS_ISV4(vp)) { 
681                         if (nfscl_mustflush(vp) != 0) {
682                                 int cm = newnfs_commit_on_close ? 1 : 0;
683                                 error = ncl_flush(vp, MNT_WAIT, cred, ap->a_td,
684                                     cm, 0);
685                                 /*
686                                  * as above w.r.t races when clearing
687                                  * NMODIFIED.
688                                  * np->n_flag &= ~NMODIFIED;
689                                  */
690                         }
691                 } else
692                     error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
693                 mtx_lock(&np->n_mtx);
694             }
695             /* 
696              * Invalidate the attribute cache in all cases.
697              * An open is going to fetch fresh attrs any way, other procs
698              * on this node that have file open will be forced to do an 
699              * otw attr fetch, but this is safe.
700              * --> A user found that their RPC count dropped by 20% when
701              *     this was commented out and I can't see any requirement
702              *     for it, so I've disabled it when negative lookups are
703              *     enabled. (What does this have to do with negative lookup
704              *     caching? Well nothing, except it was reported by the
705              *     same user that needed negative lookup caching and I wanted
706              *     there to be a way to disable it to see if it
707              *     is the cause of some caching/coherency issue that might
708              *     crop up.)
709              */
710             if (VFSTONFS(vp->v_mount)->nm_negnametimeo == 0)
711                     np->n_attrstamp = 0;
712             if (np->n_flag & NWRITEERR) {
713                 np->n_flag &= ~NWRITEERR;
714                 error = np->n_error;
715             }
716             mtx_unlock(&np->n_mtx);
717         }
718
719         if (NFS_ISV4(vp)) {
720                 /*
721                  * Get attributes so "change" is up to date.
722                  */
723                 if (error == 0 && nfscl_mustflush(vp) != 0) {
724                         ret = nfsrpc_getattr(vp, cred, ap->a_td, &nfsva,
725                             NULL);
726                         if (!ret) {
727                                 np->n_change = nfsva.na_filerev;
728                                 (void) nfscl_loadattrcache(&vp, &nfsva, NULL,
729                                     NULL, 0, 0);
730                         }
731                 }
732
733                 /*
734                  * and do the close.
735                  */
736                 ret = nfsrpc_close(vp, 0, ap->a_td);
737                 if (!error && ret)
738                         error = ret;
739                 if (error)
740                         error = nfscl_maperr(ap->a_td, error, (uid_t)0,
741                             (gid_t)0);
742         }
743         if (newnfs_directio_enable)
744                 KASSERT((np->n_directio_asyncwr == 0),
745                         ("nfs_close: dirty unflushed (%d) directio buffers\n",
746                          np->n_directio_asyncwr));
747         if (newnfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
748                 mtx_lock(&np->n_mtx);
749                 KASSERT((np->n_directio_opens > 0), 
750                         ("nfs_close: unexpectedly value (0) of n_directio_opens\n"));
751                 np->n_directio_opens--;
752                 if (np->n_directio_opens == 0)
753                         np->n_flag &= ~NNONCACHE;
754                 mtx_unlock(&np->n_mtx);
755         }
756         if (localcred)
757                 NFSFREECRED(cred);
758         return (error);
759 }
760
761 /*
762  * nfs getattr call from vfs.
763  */
764 static int
765 nfs_getattr(struct vop_getattr_args *ap)
766 {
767         struct vnode *vp = ap->a_vp;
768         struct thread *td = curthread;  /* XXX */
769         struct nfsnode *np = VTONFS(vp);
770         int error = 0;
771         struct nfsvattr nfsva;
772         struct vattr *vap = ap->a_vap;
773         struct vattr vattr;
774
775         /*
776          * Update local times for special files.
777          */
778         mtx_lock(&np->n_mtx);
779         if (np->n_flag & (NACC | NUPD))
780                 np->n_flag |= NCHG;
781         mtx_unlock(&np->n_mtx);
782         /*
783          * First look in the cache.
784          */
785         if (ncl_getattrcache(vp, &vattr) == 0) {
786                 vap->va_type = vattr.va_type;
787                 vap->va_mode = vattr.va_mode;
788                 vap->va_nlink = vattr.va_nlink;
789                 vap->va_uid = vattr.va_uid;
790                 vap->va_gid = vattr.va_gid;
791                 vap->va_fsid = vattr.va_fsid;
792                 vap->va_fileid = vattr.va_fileid;
793                 vap->va_size = vattr.va_size;
794                 vap->va_blocksize = vattr.va_blocksize;
795                 vap->va_atime = vattr.va_atime;
796                 vap->va_mtime = vattr.va_mtime;
797                 vap->va_ctime = vattr.va_ctime;
798                 vap->va_gen = vattr.va_gen;
799                 vap->va_flags = vattr.va_flags;
800                 vap->va_rdev = vattr.va_rdev;
801                 vap->va_bytes = vattr.va_bytes;
802                 vap->va_filerev = vattr.va_filerev;
803                 /*
804                  * Get the local modify time for the case of a write
805                  * delegation.
806                  */
807                 nfscl_deleggetmodtime(vp, &vap->va_mtime);
808                 return (0);
809         }
810
811         if (NFS_ISV34(vp) && nfs_prime_access_cache &&
812             nfsaccess_cache_timeout > 0) {
813                 NFSINCRGLOBAL(newnfsstats.accesscache_misses);
814                 nfs34_access_otw(vp, NFSACCESS_ALL, td, ap->a_cred, NULL);
815                 if (ncl_getattrcache(vp, ap->a_vap) == 0) {
816                         nfscl_deleggetmodtime(vp, &ap->a_vap->va_mtime);
817                         return (0);
818                 }
819         }
820         error = nfsrpc_getattr(vp, ap->a_cred, td, &nfsva, NULL);
821         if (!error)
822                 error = nfscl_loadattrcache(&vp, &nfsva, vap, NULL, 0, 0);
823         if (!error) {
824                 /*
825                  * Get the local modify time for the case of a write
826                  * delegation.
827                  */
828                 nfscl_deleggetmodtime(vp, &vap->va_mtime);
829         } else if (NFS_ISV4(vp)) {
830                 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
831         }
832         return (error);
833 }
834
835 /*
836  * nfs setattr call.
837  */
838 static int
839 nfs_setattr(struct vop_setattr_args *ap)
840 {
841         struct vnode *vp = ap->a_vp;
842         struct nfsnode *np = VTONFS(vp);
843         struct thread *td = curthread;  /* XXX */
844         struct vattr *vap = ap->a_vap;
845         int error = 0;
846         u_quad_t tsize;
847
848 #ifndef nolint
849         tsize = (u_quad_t)0;
850 #endif
851
852         /*
853          * Setting of flags and marking of atimes are not supported.
854          */
855         if (vap->va_flags != VNOVAL)
856                 return (EOPNOTSUPP);
857
858         /*
859          * Disallow write attempts if the filesystem is mounted read-only.
860          */
861         if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
862             vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
863             vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
864             (vp->v_mount->mnt_flag & MNT_RDONLY))
865                 return (EROFS);
866         if (vap->va_size != VNOVAL) {
867                 switch (vp->v_type) {
868                 case VDIR:
869                         return (EISDIR);
870                 case VCHR:
871                 case VBLK:
872                 case VSOCK:
873                 case VFIFO:
874                         if (vap->va_mtime.tv_sec == VNOVAL &&
875                             vap->va_atime.tv_sec == VNOVAL &&
876                             vap->va_mode == (mode_t)VNOVAL &&
877                             vap->va_uid == (uid_t)VNOVAL &&
878                             vap->va_gid == (gid_t)VNOVAL)
879                                 return (0);             
880                         vap->va_size = VNOVAL;
881                         break;
882                 default:
883                         /*
884                          * Disallow write attempts if the filesystem is
885                          * mounted read-only.
886                          */
887                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
888                                 return (EROFS);
889                         /*
890                          *  We run vnode_pager_setsize() early (why?),
891                          * we must set np->n_size now to avoid vinvalbuf
892                          * V_SAVE races that might setsize a lower
893                          * value.
894                          */
895                         mtx_lock(&np->n_mtx);
896                         tsize = np->n_size;
897                         mtx_unlock(&np->n_mtx);
898                         error = ncl_meta_setsize(vp, ap->a_cred, td,
899                             vap->va_size);
900                         mtx_lock(&np->n_mtx);
901                         if (np->n_flag & NMODIFIED) {
902                             tsize = np->n_size;
903                             mtx_unlock(&np->n_mtx);
904                             if (vap->va_size == 0)
905                                 error = ncl_vinvalbuf(vp, 0, td, 1);
906                             else
907                                 error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
908                             if (error) {
909                                 vnode_pager_setsize(vp, tsize);
910                                 return (error);
911                             }
912                             /*
913                              * Call nfscl_delegmodtime() to set the modify time
914                              * locally, as required.
915                              */
916                             nfscl_delegmodtime(vp);
917                         } else
918                             mtx_unlock(&np->n_mtx);
919                         /*
920                          * np->n_size has already been set to vap->va_size
921                          * in ncl_meta_setsize(). We must set it again since
922                          * nfs_loadattrcache() could be called through
923                          * ncl_meta_setsize() and could modify np->n_size.
924                          */
925                         mtx_lock(&np->n_mtx);
926                         np->n_vattr.na_size = np->n_size = vap->va_size;
927                         mtx_unlock(&np->n_mtx);
928                 };
929         } else {
930                 mtx_lock(&np->n_mtx);
931                 if ((vap->va_mtime.tv_sec != VNOVAL || vap->va_atime.tv_sec != VNOVAL) && 
932                     (np->n_flag & NMODIFIED) && vp->v_type == VREG) {
933                         mtx_unlock(&np->n_mtx);
934                         if ((error = ncl_vinvalbuf(vp, V_SAVE, td, 1)) != 0 &&
935                             (error == EINTR || error == EIO))
936                                 return (error);
937                 } else
938                         mtx_unlock(&np->n_mtx);
939         }
940         error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
941         if (error && vap->va_size != VNOVAL) {
942                 mtx_lock(&np->n_mtx);
943                 np->n_size = np->n_vattr.na_size = tsize;
944                 vnode_pager_setsize(vp, tsize);
945                 mtx_unlock(&np->n_mtx);
946         }
947         return (error);
948 }
949
950 /*
951  * Do an nfs setattr rpc.
952  */
953 static int
954 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
955     struct thread *td)
956 {
957         struct nfsnode *np = VTONFS(vp);
958         int error, ret, attrflag, i;
959         struct nfsvattr nfsva;
960
961         if (NFS_ISV34(vp)) {
962                 mtx_lock(&np->n_mtx);
963                 for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
964                         np->n_accesscache[i].stamp = 0;
965                 np->n_flag |= NDELEGMOD;
966                 mtx_unlock(&np->n_mtx);
967         }
968         error = nfsrpc_setattr(vp, vap, NULL, cred, td, &nfsva, &attrflag,
969             NULL);
970         if (attrflag) {
971                 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
972                 if (ret && !error)
973                         error = ret;
974         }
975         if (error && NFS_ISV4(vp))
976                 error = nfscl_maperr(td, error, vap->va_uid, vap->va_gid);
977         return (error);
978 }
979
980 /*
981  * nfs lookup call, one step at a time...
982  * First look in cache
983  * If not found, unlock the directory nfsnode and do the rpc
984  */
985 static int
986 nfs_lookup(struct vop_lookup_args *ap)
987 {
988         struct componentname *cnp = ap->a_cnp;
989         struct vnode *dvp = ap->a_dvp;
990         struct vnode **vpp = ap->a_vpp;
991         struct mount *mp = dvp->v_mount;
992         int flags = cnp->cn_flags;
993         struct vnode *newvp;
994         struct nfsmount *nmp;
995         struct nfsnode *np, *newnp;
996         int error = 0, attrflag, dattrflag, ltype;
997         struct thread *td = cnp->cn_thread;
998         struct nfsfh *nfhp;
999         struct nfsvattr dnfsva, nfsva;
1000         struct vattr vattr;
1001         struct timespec dmtime;
1002         
1003         *vpp = NULLVP;
1004         if ((flags & ISLASTCN) && (mp->mnt_flag & MNT_RDONLY) &&
1005             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1006                 return (EROFS);
1007         if (dvp->v_type != VDIR)
1008                 return (ENOTDIR);
1009         nmp = VFSTONFS(mp);
1010         np = VTONFS(dvp);
1011
1012         /* For NFSv4, wait until any remove is done. */
1013         mtx_lock(&np->n_mtx);
1014         while (NFSHASNFSV4(nmp) && (np->n_flag & NREMOVEINPROG)) {
1015                 np->n_flag |= NREMOVEWANT;
1016                 (void) msleep((caddr_t)np, &np->n_mtx, PZERO, "nfslkup", 0);
1017         }
1018         mtx_unlock(&np->n_mtx);
1019
1020         if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0)
1021                 return (error);
1022         error = cache_lookup(dvp, vpp, cnp);
1023         if (error > 0 && error != ENOENT)
1024                 return (error);
1025         if (error == -1) {
1026                 /*
1027                  * We only accept a positive hit in the cache if the
1028                  * change time of the file matches our cached copy.
1029                  * Otherwise, we discard the cache entry and fallback
1030                  * to doing a lookup RPC.
1031                  *
1032                  * To better handle stale file handles and attributes,
1033                  * clear the attribute cache of this node if it is a
1034                  * leaf component, part of an open() call, and not
1035                  * locally modified before fetching the attributes.
1036                  * This should allow stale file handles to be detected
1037                  * here where we can fall back to a LOOKUP RPC to
1038                  * recover rather than having nfs_open() detect the
1039                  * stale file handle and failing open(2) with ESTALE.
1040                  */
1041                 newvp = *vpp;
1042                 newnp = VTONFS(newvp);
1043                 if ((flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1044                     !(newnp->n_flag & NMODIFIED)) {
1045                         mtx_lock(&newnp->n_mtx);
1046                         newnp->n_attrstamp = 0;
1047                         mtx_unlock(&newnp->n_mtx);
1048                 }
1049                 if (nfscl_nodeleg(newvp, 0) == 0 ||
1050                     (VOP_GETATTR(newvp, &vattr, cnp->cn_cred) == 0 &&
1051                     timespeccmp(&vattr.va_ctime, &newnp->n_ctime, ==))) {
1052                         NFSINCRGLOBAL(newnfsstats.lookupcache_hits);
1053                         if (cnp->cn_nameiop != LOOKUP &&
1054                             (flags & ISLASTCN))
1055                                 cnp->cn_flags |= SAVENAME;
1056                         return (0);
1057                 }
1058                 cache_purge(newvp);
1059                 if (dvp != newvp)
1060                         vput(newvp);
1061                 else 
1062                         vrele(newvp);
1063                 *vpp = NULLVP;
1064         } else if (error == ENOENT) {
1065                 if (dvp->v_iflag & VI_DOOMED)
1066                         return (ENOENT);
1067                 /*
1068                  * We only accept a negative hit in the cache if the
1069                  * modification time of the parent directory matches
1070                  * our cached copy.  Otherwise, we discard all of the
1071                  * negative cache entries for this directory. We also
1072                  * only trust -ve cache entries for less than
1073                  * nm_negative_namecache_timeout seconds.
1074                  */
1075                 if ((u_int)(ticks - np->n_dmtime_ticks) <
1076                     (nmp->nm_negnametimeo * hz) &&
1077                     VOP_GETATTR(dvp, &vattr, cnp->cn_cred) == 0 &&
1078                     timespeccmp(&vattr.va_mtime, &np->n_dmtime, ==)) {
1079                         NFSINCRGLOBAL(newnfsstats.lookupcache_hits);
1080                         return (ENOENT);
1081                 }
1082                 cache_purge_negative(dvp);
1083                 mtx_lock(&np->n_mtx);
1084                 timespecclear(&np->n_dmtime);
1085                 mtx_unlock(&np->n_mtx);
1086         }
1087
1088         /*
1089          * Cache the modification time of the parent directory in case
1090          * the lookup fails and results in adding the first negative
1091          * name cache entry for the directory.  Since this is reading
1092          * a single time_t, don't bother with locking.  The
1093          * modification time may be a bit stale, but it must be read
1094          * before performing the lookup RPC to prevent a race where
1095          * another lookup updates the timestamp on the directory after
1096          * the lookup RPC has been performed on the server but before
1097          * n_dmtime is set at the end of this function.
1098          */
1099         dmtime = np->n_vattr.na_mtime;
1100         error = 0;
1101         newvp = NULLVP;
1102         NFSINCRGLOBAL(newnfsstats.lookupcache_misses);
1103         error = nfsrpc_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1104             cnp->cn_cred, td, &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1105             NULL);
1106         if (dattrflag)
1107                 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1108         if (error) {
1109                 if (newvp != NULLVP) {
1110                         vput(newvp);
1111                         *vpp = NULLVP;
1112                 }
1113
1114                 if (error != ENOENT) {
1115                         if (NFS_ISV4(dvp))
1116                                 error = nfscl_maperr(td, error, (uid_t)0,
1117                                     (gid_t)0);
1118                         return (error);
1119                 }
1120
1121                 /* The requested file was not found. */
1122                 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1123                     (flags & ISLASTCN)) {
1124                         /*
1125                          * XXX: UFS does a full VOP_ACCESS(dvp,
1126                          * VWRITE) here instead of just checking
1127                          * MNT_RDONLY.
1128                          */
1129                         if (mp->mnt_flag & MNT_RDONLY)
1130                                 return (EROFS);
1131                         cnp->cn_flags |= SAVENAME;
1132                         return (EJUSTRETURN);
1133                 }
1134
1135                 if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE) {
1136                         /*
1137                          * Maintain n_dmtime as the modification time
1138                          * of the parent directory when the oldest -ve
1139                          * name cache entry for this directory was
1140                          * added.  If a -ve cache entry has already
1141                          * been added with a newer modification time
1142                          * by a concurrent lookup, then don't bother
1143                          * adding a cache entry.  The modification
1144                          * time of the directory might have changed
1145                          * due to the file this lookup failed to find
1146                          * being created.  In that case a subsequent
1147                          * lookup would incorrectly use the entry
1148                          * added here instead of doing an extra
1149                          * lookup.
1150                          */
1151                         mtx_lock(&np->n_mtx);
1152                         if (timespeccmp(&np->n_dmtime, &dmtime, <=)) {
1153                                 if (!timespecisset(&np->n_dmtime)) {
1154                                         np->n_dmtime = dmtime;
1155                                         np->n_dmtime_ticks = ticks;
1156                                 }
1157                                 mtx_unlock(&np->n_mtx);
1158                                 cache_enter(dvp, NULL, cnp);
1159                         } else
1160                                 mtx_unlock(&np->n_mtx);
1161                 }
1162                 return (ENOENT);
1163         }
1164
1165         /*
1166          * Handle RENAME case...
1167          */
1168         if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
1169                 if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1170                         FREE((caddr_t)nfhp, M_NFSFH);
1171                         return (EISDIR);
1172                 }
1173                 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL);
1174                 if (error)
1175                         return (error);
1176                 newvp = NFSTOV(np);
1177                 if (attrflag)
1178                         (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1179                             0, 1);
1180                 *vpp = newvp;
1181                 cnp->cn_flags |= SAVENAME;
1182                 return (0);
1183         }
1184
1185         if (flags & ISDOTDOT) {
1186                 ltype = VOP_ISLOCKED(dvp);
1187                 error = vfs_busy(mp, MBF_NOWAIT);
1188                 if (error != 0) {
1189                         vfs_ref(mp);
1190                         VOP_UNLOCK(dvp, 0);
1191                         error = vfs_busy(mp, 0);
1192                         vn_lock(dvp, ltype | LK_RETRY);
1193                         vfs_rel(mp);
1194                         if (error == 0 && (dvp->v_iflag & VI_DOOMED)) {
1195                                 vfs_unbusy(mp);
1196                                 error = ENOENT;
1197                         }
1198                         if (error != 0)
1199                                 return (error);
1200                 }
1201                 VOP_UNLOCK(dvp, 0);
1202                 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL);
1203                 if (error == 0)
1204                         newvp = NFSTOV(np);
1205                 vfs_unbusy(mp);
1206                 if (newvp != dvp)
1207                         vn_lock(dvp, ltype | LK_RETRY);
1208                 if (dvp->v_iflag & VI_DOOMED) {
1209                         if (error == 0) {
1210                                 if (newvp == dvp)
1211                                         vrele(newvp);
1212                                 else
1213                                         vput(newvp);
1214                         }
1215                         error = ENOENT;
1216                 }
1217                 if (error != 0)
1218                         return (error);
1219                 if (attrflag)
1220                         (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1221                             0, 1);
1222         } else if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1223                 FREE((caddr_t)nfhp, M_NFSFH);
1224                 VREF(dvp);
1225                 newvp = dvp;
1226                 if (attrflag)
1227                         (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1228                             0, 1);
1229         } else {
1230                 error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL);
1231                 if (error)
1232                         return (error);
1233                 newvp = NFSTOV(np);
1234                 if (attrflag)
1235                         (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1236                             0, 1);
1237                 else if ((flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1238                     !(np->n_flag & NMODIFIED)) {                        
1239                         /*
1240                          * Flush the attribute cache when opening a
1241                          * leaf node to ensure that fresh attributes
1242                          * are fetched in nfs_open() since we did not
1243                          * fetch attributes from the LOOKUP reply.
1244                          */
1245                         mtx_lock(&np->n_mtx);
1246                         np->n_attrstamp = 0;
1247                         mtx_unlock(&np->n_mtx);
1248                 }
1249         }
1250         if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1251                 cnp->cn_flags |= SAVENAME;
1252         if ((cnp->cn_flags & MAKEENTRY) &&
1253             (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
1254                 np->n_ctime = np->n_vattr.na_vattr.va_ctime;
1255                 cache_enter(dvp, newvp, cnp);
1256         }
1257         *vpp = newvp;
1258         return (0);
1259 }
1260
1261 /*
1262  * nfs read call.
1263  * Just call ncl_bioread() to do the work.
1264  */
1265 static int
1266 nfs_read(struct vop_read_args *ap)
1267 {
1268         struct vnode *vp = ap->a_vp;
1269
1270         switch (vp->v_type) {
1271         case VREG:
1272                 return (ncl_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1273         case VDIR:
1274                 return (EISDIR);
1275         default:
1276                 return (EOPNOTSUPP);
1277         }
1278 }
1279
1280 /*
1281  * nfs readlink call
1282  */
1283 static int
1284 nfs_readlink(struct vop_readlink_args *ap)
1285 {
1286         struct vnode *vp = ap->a_vp;
1287
1288         if (vp->v_type != VLNK)
1289                 return (EINVAL);
1290         return (ncl_bioread(vp, ap->a_uio, 0, ap->a_cred));
1291 }
1292
1293 /*
1294  * Do a readlink rpc.
1295  * Called by ncl_doio() from below the buffer cache.
1296  */
1297 int
1298 ncl_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1299 {
1300         int error, ret, attrflag;
1301         struct nfsvattr nfsva;
1302
1303         error = nfsrpc_readlink(vp, uiop, cred, uiop->uio_td, &nfsva,
1304             &attrflag, NULL);
1305         if (attrflag) {
1306                 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1307                 if (ret && !error)
1308                         error = ret;
1309         }
1310         if (error && NFS_ISV4(vp))
1311                 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1312         return (error);
1313 }
1314
1315 /*
1316  * nfs read rpc call
1317  * Ditto above
1318  */
1319 int
1320 ncl_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1321 {
1322         int error, ret, attrflag;
1323         struct nfsvattr nfsva;
1324
1325         error = nfsrpc_read(vp, uiop, cred, uiop->uio_td, &nfsva, &attrflag,
1326             NULL);
1327         if (attrflag) {
1328                 ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1329                 if (ret && !error)
1330                         error = ret;
1331         }
1332         if (error && NFS_ISV4(vp))
1333                 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1334         return (error);
1335 }
1336
1337 /*
1338  * nfs write call
1339  */
1340 int
1341 ncl_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1342     int *iomode, int *must_commit, int called_from_strategy)
1343 {
1344         struct nfsvattr nfsva;
1345         int error = 0, attrflag, ret;
1346         u_char verf[NFSX_VERF];
1347         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1348
1349         *must_commit = 0;
1350         error = nfsrpc_write(vp, uiop, iomode, verf, cred,
1351             uiop->uio_td, &nfsva, &attrflag, NULL, called_from_strategy);
1352         NFSLOCKMNT(nmp);
1353         if (!error && NFSHASWRITEVERF(nmp) &&
1354             NFSBCMP(verf, nmp->nm_verf, NFSX_VERF)) {
1355                 *must_commit = 1;
1356                 NFSBCOPY(verf, nmp->nm_verf, NFSX_VERF);
1357         }
1358         NFSUNLOCKMNT(nmp);
1359         if (attrflag) {
1360                 if (VTONFS(vp)->n_flag & ND_NFSV4)
1361                         ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 1,
1362                             1);
1363                 else
1364                         ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
1365                             1);
1366                 if (ret && !error)
1367                         error = ret;
1368         }
1369         if (vp->v_mount->mnt_kern_flag & MNTK_ASYNC)
1370                 *iomode = NFSWRITE_FILESYNC;
1371         if (error && NFS_ISV4(vp))
1372                 error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1373         return (error);
1374 }
1375
1376 /*
1377  * nfs mknod rpc
1378  * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1379  * mode set to specify the file type and the size field for rdev.
1380  */
1381 static int
1382 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1383     struct vattr *vap)
1384 {
1385         struct nfsvattr nfsva, dnfsva;
1386         struct vnode *newvp = NULL;
1387         struct nfsnode *np = NULL, *dnp;
1388         struct nfsfh *nfhp;
1389         struct vattr vattr;
1390         int error = 0, attrflag, dattrflag;
1391         u_int32_t rdev;
1392
1393         if (vap->va_type == VCHR || vap->va_type == VBLK)
1394                 rdev = vap->va_rdev;
1395         else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1396                 rdev = 0xffffffff;
1397         else
1398                 return (EOPNOTSUPP);
1399         if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1400                 return (error);
1401         error = nfsrpc_mknod(dvp, cnp->cn_nameptr, cnp->cn_namelen, vap,
1402             rdev, vap->va_type, cnp->cn_cred, cnp->cn_thread, &dnfsva,
1403             &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
1404         if (!error) {
1405                 if (!nfhp)
1406                         (void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1407                             cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
1408                             &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1409                             NULL);
1410                 if (nfhp)
1411                         error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1412                             cnp->cn_thread, &np, NULL);
1413         }
1414         if (dattrflag)
1415                 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1416         if (!error) {
1417                 newvp = NFSTOV(np);
1418                 if (attrflag)
1419                         error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1420                             0, 1);
1421         }
1422         if (!error) {
1423                 if ((cnp->cn_flags & MAKEENTRY))
1424                         cache_enter(dvp, newvp, cnp);
1425                 *vpp = newvp;
1426         } else if (NFS_ISV4(dvp)) {
1427                 error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
1428                     vap->va_gid);
1429         }
1430         dnp = VTONFS(dvp);
1431         mtx_lock(&dnp->n_mtx);
1432         dnp->n_flag |= NMODIFIED;
1433         if (!dattrflag)
1434                 dnp->n_attrstamp = 0;
1435         mtx_unlock(&dnp->n_mtx);
1436         return (error);
1437 }
1438
1439 /*
1440  * nfs mknod vop
1441  * just call nfs_mknodrpc() to do the work.
1442  */
1443 /* ARGSUSED */
1444 static int
1445 nfs_mknod(struct vop_mknod_args *ap)
1446 {
1447         return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1448 }
1449
1450 static u_long create_verf;
1451 /*
1452  * nfs file create call
1453  */
1454 static int
1455 nfs_create(struct vop_create_args *ap)
1456 {
1457         struct vnode *dvp = ap->a_dvp;
1458         struct vattr *vap = ap->a_vap;
1459         struct componentname *cnp = ap->a_cnp;
1460         struct nfsnode *np = NULL, *dnp;
1461         struct vnode *newvp = NULL;
1462         struct nfsmount *nmp;
1463         struct nfsvattr dnfsva, nfsva;
1464         struct nfsfh *nfhp;
1465         nfsquad_t cverf;
1466         int error = 0, attrflag, dattrflag, fmode = 0;
1467         struct vattr vattr;
1468
1469         /*
1470          * Oops, not for me..
1471          */
1472         if (vap->va_type == VSOCK)
1473                 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1474
1475         if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1476                 return (error);
1477         if (vap->va_vaflags & VA_EXCLUSIVE)
1478                 fmode |= O_EXCL;
1479         dnp = VTONFS(dvp);
1480         nmp = VFSTONFS(vnode_mount(dvp));
1481 again:
1482         /* For NFSv4, wait until any remove is done. */
1483         mtx_lock(&dnp->n_mtx);
1484         while (NFSHASNFSV4(nmp) && (dnp->n_flag & NREMOVEINPROG)) {
1485                 dnp->n_flag |= NREMOVEWANT;
1486                 (void) msleep((caddr_t)dnp, &dnp->n_mtx, PZERO, "nfscrt", 0);
1487         }
1488         mtx_unlock(&dnp->n_mtx);
1489
1490 #ifdef INET
1491         CURVNET_SET(CRED_TO_VNET(cnp->cn_cred));
1492         IN_IFADDR_RLOCK();
1493         if (!TAILQ_EMPTY(&V_in_ifaddrhead))
1494                 cverf.lval[0] = IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr.s_addr;
1495         else
1496 #endif
1497                 cverf.lval[0] = create_verf;
1498 #ifdef INET
1499         IN_IFADDR_RUNLOCK();
1500         CURVNET_RESTORE();
1501 #endif
1502         cverf.lval[1] = ++create_verf;
1503         error = nfsrpc_create(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1504             vap, cverf, fmode, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva,
1505             &nfhp, &attrflag, &dattrflag, NULL);
1506         if (!error) {
1507                 if (nfhp == NULL)
1508                         (void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1509                             cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
1510                             &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1511                             NULL);
1512                 if (nfhp != NULL)
1513                         error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1514                             cnp->cn_thread, &np, NULL);
1515         }
1516         if (dattrflag)
1517                 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1518         if (!error) {
1519                 newvp = NFSTOV(np);
1520                 if (attrflag)
1521                         error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1522                             0, 1);
1523         }
1524         if (error) {
1525                 if (newvp != NULL) {
1526                         vrele(newvp);
1527                         newvp = NULL;
1528                 }
1529                 if (NFS_ISV34(dvp) && (fmode & O_EXCL) &&
1530                     error == NFSERR_NOTSUPP) {
1531                         fmode &= ~O_EXCL;
1532                         goto again;
1533                 }
1534         } else if (NFS_ISV34(dvp) && (fmode & O_EXCL)) {
1535                 if (nfscl_checksattr(vap, &nfsva)) {
1536                         error = nfsrpc_setattr(newvp, vap, NULL, cnp->cn_cred,
1537                             cnp->cn_thread, &nfsva, &attrflag, NULL);
1538                         if (error && (vap->va_uid != (uid_t)VNOVAL ||
1539                             vap->va_gid != (gid_t)VNOVAL)) {
1540                                 /* try again without setting uid/gid */
1541                                 vap->va_uid = (uid_t)VNOVAL;
1542                                 vap->va_gid = (uid_t)VNOVAL;
1543                                 error = nfsrpc_setattr(newvp, vap, NULL, 
1544                                     cnp->cn_cred, cnp->cn_thread, &nfsva,
1545                                     &attrflag, NULL);
1546                         }
1547                         if (attrflag)
1548                                 (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1549                                     NULL, 0, 1);
1550                 }
1551         }
1552         if (!error) {
1553                 if (cnp->cn_flags & MAKEENTRY)
1554                         cache_enter(dvp, newvp, cnp);
1555                 *ap->a_vpp = newvp;
1556         } else if (NFS_ISV4(dvp)) {
1557                 error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
1558                     vap->va_gid);
1559         }
1560         mtx_lock(&dnp->n_mtx);
1561         dnp->n_flag |= NMODIFIED;
1562         if (!dattrflag)
1563                 dnp->n_attrstamp = 0;
1564         mtx_unlock(&dnp->n_mtx);
1565         return (error);
1566 }
1567
1568 /*
1569  * nfs file remove call
1570  * To try and make nfs semantics closer to ufs semantics, a file that has
1571  * other processes using the vnode is renamed instead of removed and then
1572  * removed later on the last close.
1573  * - If v_usecount > 1
1574  *        If a rename is not already in the works
1575  *           call nfs_sillyrename() to set it up
1576  *     else
1577  *        do the remove rpc
1578  */
1579 static int
1580 nfs_remove(struct vop_remove_args *ap)
1581 {
1582         struct vnode *vp = ap->a_vp;
1583         struct vnode *dvp = ap->a_dvp;
1584         struct componentname *cnp = ap->a_cnp;
1585         struct nfsnode *np = VTONFS(vp);
1586         int error = 0;
1587         struct vattr vattr;
1588
1589         KASSERT((cnp->cn_flags & HASBUF) != 0, ("nfs_remove: no name"));
1590         KASSERT(vrefcnt(vp) > 0, ("nfs_remove: bad v_usecount"));
1591         if (vp->v_type == VDIR)
1592                 error = EPERM;
1593         else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1594             VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 &&
1595             vattr.va_nlink > 1)) {
1596                 /*
1597                  * Purge the name cache so that the chance of a lookup for
1598                  * the name succeeding while the remove is in progress is
1599                  * minimized. Without node locking it can still happen, such
1600                  * that an I/O op returns ESTALE, but since you get this if
1601                  * another host removes the file..
1602                  */
1603                 cache_purge(vp);
1604                 /*
1605                  * throw away biocache buffers, mainly to avoid
1606                  * unnecessary delayed writes later.
1607                  */
1608                 error = ncl_vinvalbuf(vp, 0, cnp->cn_thread, 1);
1609                 /* Do the rpc */
1610                 if (error != EINTR && error != EIO)
1611                         error = nfs_removerpc(dvp, vp, cnp->cn_nameptr,
1612                             cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
1613                 /*
1614                  * Kludge City: If the first reply to the remove rpc is lost..
1615                  *   the reply to the retransmitted request will be ENOENT
1616                  *   since the file was in fact removed
1617                  *   Therefore, we cheat and return success.
1618                  */
1619                 if (error == ENOENT)
1620                         error = 0;
1621         } else if (!np->n_sillyrename)
1622                 error = nfs_sillyrename(dvp, vp, cnp);
1623         np->n_attrstamp = 0;
1624         return (error);
1625 }
1626
1627 /*
1628  * nfs file remove rpc called from nfs_inactive
1629  */
1630 int
1631 ncl_removeit(struct sillyrename *sp, struct vnode *vp)
1632 {
1633         /*
1634          * Make sure that the directory vnode is still valid.
1635          * XXX we should lock sp->s_dvp here.
1636          */
1637         if (sp->s_dvp->v_type == VBAD)
1638                 return (0);
1639         return (nfs_removerpc(sp->s_dvp, vp, sp->s_name, sp->s_namlen,
1640             sp->s_cred, NULL));
1641 }
1642
1643 /*
1644  * Nfs remove rpc, called from nfs_remove() and ncl_removeit().
1645  */
1646 static int
1647 nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
1648     int namelen, struct ucred *cred, struct thread *td)
1649 {
1650         struct nfsvattr dnfsva;
1651         struct nfsnode *dnp = VTONFS(dvp);
1652         int error = 0, dattrflag;
1653
1654         mtx_lock(&dnp->n_mtx);
1655         dnp->n_flag |= NREMOVEINPROG;
1656         mtx_unlock(&dnp->n_mtx);
1657         error = nfsrpc_remove(dvp, name, namelen, vp, cred, td, &dnfsva,
1658             &dattrflag, NULL);
1659         mtx_lock(&dnp->n_mtx);
1660         if ((dnp->n_flag & NREMOVEWANT)) {
1661                 dnp->n_flag &= ~(NREMOVEWANT | NREMOVEINPROG);
1662                 mtx_unlock(&dnp->n_mtx);
1663                 wakeup((caddr_t)dnp);
1664         } else {
1665                 dnp->n_flag &= ~NREMOVEINPROG;
1666                 mtx_unlock(&dnp->n_mtx);
1667         }
1668         if (dattrflag)
1669                 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1670         mtx_lock(&dnp->n_mtx);
1671         dnp->n_flag |= NMODIFIED;
1672         if (!dattrflag)
1673                 dnp->n_attrstamp = 0;
1674         mtx_unlock(&dnp->n_mtx);
1675         if (error && NFS_ISV4(dvp))
1676                 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1677         return (error);
1678 }
1679
1680 /*
1681  * nfs file rename call
1682  */
1683 static int
1684 nfs_rename(struct vop_rename_args *ap)
1685 {
1686         struct vnode *fvp = ap->a_fvp;
1687         struct vnode *tvp = ap->a_tvp;
1688         struct vnode *fdvp = ap->a_fdvp;
1689         struct vnode *tdvp = ap->a_tdvp;
1690         struct componentname *tcnp = ap->a_tcnp;
1691         struct componentname *fcnp = ap->a_fcnp;
1692         struct nfsnode *fnp = VTONFS(ap->a_fvp);
1693         struct nfsnode *tdnp = VTONFS(ap->a_tdvp);
1694         struct nfsv4node *newv4 = NULL;
1695         int error;
1696
1697         KASSERT((tcnp->cn_flags & HASBUF) != 0 &&
1698             (fcnp->cn_flags & HASBUF) != 0, ("nfs_rename: no name"));
1699         /* Check for cross-device rename */
1700         if ((fvp->v_mount != tdvp->v_mount) ||
1701             (tvp && (fvp->v_mount != tvp->v_mount))) {
1702                 error = EXDEV;
1703                 goto out;
1704         }
1705
1706         if (fvp == tvp) {
1707                 ncl_printf("nfs_rename: fvp == tvp (can't happen)\n");
1708                 error = 0;
1709                 goto out;
1710         }
1711         if ((error = vn_lock(fvp, LK_EXCLUSIVE)))
1712                 goto out;
1713
1714         /*
1715          * We have to flush B_DELWRI data prior to renaming
1716          * the file.  If we don't, the delayed-write buffers
1717          * can be flushed out later after the file has gone stale
1718          * under NFSV3.  NFSV2 does not have this problem because
1719          * ( as far as I can tell ) it flushes dirty buffers more
1720          * often.
1721          * 
1722          * Skip the rename operation if the fsync fails, this can happen
1723          * due to the server's volume being full, when we pushed out data
1724          * that was written back to our cache earlier. Not checking for
1725          * this condition can result in potential (silent) data loss.
1726          */
1727         error = VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_thread);
1728         VOP_UNLOCK(fvp, 0);
1729         if (!error && tvp)
1730                 error = VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_thread);
1731         if (error)
1732                 goto out;
1733
1734         /*
1735          * If the tvp exists and is in use, sillyrename it before doing the
1736          * rename of the new file over it.
1737          * XXX Can't sillyrename a directory.
1738          */
1739         if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1740                 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1741                 vput(tvp);
1742                 tvp = NULL;
1743         }
1744
1745         error = nfs_renamerpc(fdvp, fvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1746             tdvp, tvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1747             tcnp->cn_thread);
1748
1749         if (!error) {
1750                 /*
1751                  * For NFSv4, check to see if it is the same name and
1752                  * replace the name, if it is different.
1753                  */
1754                 MALLOC(newv4, struct nfsv4node *,
1755                     sizeof (struct nfsv4node) +
1756                     tdnp->n_fhp->nfh_len + tcnp->cn_namelen - 1,
1757                     M_NFSV4NODE, M_WAITOK);
1758                 mtx_lock(&tdnp->n_mtx);
1759                 mtx_lock(&fnp->n_mtx);
1760                 if (fnp->n_v4 != NULL && fvp->v_type == VREG &&
1761                     (fnp->n_v4->n4_namelen != tcnp->cn_namelen ||
1762                       NFSBCMP(tcnp->cn_nameptr, NFS4NODENAME(fnp->n_v4),
1763                       tcnp->cn_namelen) ||
1764                       tdnp->n_fhp->nfh_len != fnp->n_v4->n4_fhlen ||
1765                       NFSBCMP(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
1766                         tdnp->n_fhp->nfh_len))) {
1767 #ifdef notdef
1768 { char nnn[100]; int nnnl;
1769 nnnl = (tcnp->cn_namelen < 100) ? tcnp->cn_namelen : 99;
1770 bcopy(tcnp->cn_nameptr, nnn, nnnl);
1771 nnn[nnnl] = '\0';
1772 printf("ren replace=%s\n",nnn);
1773 }
1774 #endif
1775                         FREE((caddr_t)fnp->n_v4, M_NFSV4NODE);
1776                         fnp->n_v4 = newv4;
1777                         newv4 = NULL;
1778                         fnp->n_v4->n4_fhlen = tdnp->n_fhp->nfh_len;
1779                         fnp->n_v4->n4_namelen = tcnp->cn_namelen;
1780                         NFSBCOPY(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
1781                             tdnp->n_fhp->nfh_len);
1782                         NFSBCOPY(tcnp->cn_nameptr,
1783                             NFS4NODENAME(fnp->n_v4), tcnp->cn_namelen);
1784                 }
1785                 mtx_unlock(&tdnp->n_mtx);
1786                 mtx_unlock(&fnp->n_mtx);
1787                 if (newv4 != NULL)
1788                         FREE((caddr_t)newv4, M_NFSV4NODE);
1789         }
1790
1791         if (fvp->v_type == VDIR) {
1792                 if (tvp != NULL && tvp->v_type == VDIR)
1793                         cache_purge(tdvp);
1794                 cache_purge(fdvp);
1795         }
1796
1797 out:
1798         if (tdvp == tvp)
1799                 vrele(tdvp);
1800         else
1801                 vput(tdvp);
1802         if (tvp)
1803                 vput(tvp);
1804         vrele(fdvp);
1805         vrele(fvp);
1806         /*
1807          * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1808          */
1809         if (error == ENOENT)
1810                 error = 0;
1811         return (error);
1812 }
1813
1814 /*
1815  * nfs file rename rpc called from nfs_remove() above
1816  */
1817 static int
1818 nfs_renameit(struct vnode *sdvp, struct vnode *svp, struct componentname *scnp,
1819     struct sillyrename *sp)
1820 {
1821
1822         return (nfs_renamerpc(sdvp, svp, scnp->cn_nameptr, scnp->cn_namelen,
1823             sdvp, NULL, sp->s_name, sp->s_namlen, scnp->cn_cred,
1824             scnp->cn_thread));
1825 }
1826
1827 /*
1828  * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1829  */
1830 static int
1831 nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp, char *fnameptr,
1832     int fnamelen, struct vnode *tdvp, struct vnode *tvp, char *tnameptr,
1833     int tnamelen, struct ucred *cred, struct thread *td)
1834 {
1835         struct nfsvattr fnfsva, tnfsva;
1836         struct nfsnode *fdnp = VTONFS(fdvp);
1837         struct nfsnode *tdnp = VTONFS(tdvp);
1838         int error = 0, fattrflag, tattrflag;
1839
1840         error = nfsrpc_rename(fdvp, fvp, fnameptr, fnamelen, tdvp, tvp,
1841             tnameptr, tnamelen, cred, td, &fnfsva, &tnfsva, &fattrflag,
1842             &tattrflag, NULL, NULL);
1843         mtx_lock(&fdnp->n_mtx);
1844         fdnp->n_flag |= NMODIFIED;
1845         mtx_unlock(&fdnp->n_mtx);
1846         mtx_lock(&tdnp->n_mtx);
1847         tdnp->n_flag |= NMODIFIED;
1848         mtx_unlock(&tdnp->n_mtx);
1849         if (fattrflag)
1850                 (void) nfscl_loadattrcache(&fdvp, &fnfsva, NULL, NULL, 0, 1);
1851         else
1852                 fdnp->n_attrstamp = 0;
1853         if (tattrflag)
1854                 (void) nfscl_loadattrcache(&tdvp, &tnfsva, NULL, NULL, 0, 1);
1855         else
1856                 tdnp->n_attrstamp = 0;
1857         if (error && NFS_ISV4(fdvp))
1858                 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1859         return (error);
1860 }
1861
1862 /*
1863  * nfs hard link create call
1864  */
1865 static int
1866 nfs_link(struct vop_link_args *ap)
1867 {
1868         struct vnode *vp = ap->a_vp;
1869         struct vnode *tdvp = ap->a_tdvp;
1870         struct componentname *cnp = ap->a_cnp;
1871         struct nfsnode *tdnp;
1872         struct nfsvattr nfsva, dnfsva;
1873         int error = 0, attrflag, dattrflag;
1874
1875         if (vp->v_mount != tdvp->v_mount) {
1876                 return (EXDEV);
1877         }
1878
1879         /*
1880          * Push all writes to the server, so that the attribute cache
1881          * doesn't get "out of sync" with the server.
1882          * XXX There should be a better way!
1883          */
1884         VOP_FSYNC(vp, MNT_WAIT, cnp->cn_thread);
1885
1886         error = nfsrpc_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
1887             cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &attrflag,
1888             &dattrflag, NULL);
1889         tdnp = VTONFS(tdvp);
1890         mtx_lock(&tdnp->n_mtx);
1891         tdnp->n_flag |= NMODIFIED;
1892         mtx_unlock(&tdnp->n_mtx);
1893         if (attrflag)
1894                 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1895         else
1896                 VTONFS(vp)->n_attrstamp = 0;
1897         if (dattrflag)
1898                 (void) nfscl_loadattrcache(&tdvp, &dnfsva, NULL, NULL, 0, 1);
1899         else
1900                 tdnp->n_attrstamp = 0;
1901         /*
1902          * If negative lookup caching is enabled, I might as well
1903          * add an entry for this node. Not necessary for correctness,
1904          * but if negative caching is enabled, then the system
1905          * must care about lookup caching hit rate, so...
1906          */
1907         if (VFSTONFS(vp->v_mount)->nm_negnametimeo != 0 &&
1908             (cnp->cn_flags & MAKEENTRY))
1909                 cache_enter(tdvp, vp, cnp);
1910         if (error && NFS_ISV4(vp))
1911                 error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
1912                     (gid_t)0);
1913         return (error);
1914 }
1915
1916 /*
1917  * nfs symbolic link create call
1918  */
1919 static int
1920 nfs_symlink(struct vop_symlink_args *ap)
1921 {
1922         struct vnode *dvp = ap->a_dvp;
1923         struct vattr *vap = ap->a_vap;
1924         struct componentname *cnp = ap->a_cnp;
1925         struct nfsvattr nfsva, dnfsva;
1926         struct nfsfh *nfhp;
1927         struct nfsnode *np = NULL, *dnp;
1928         struct vnode *newvp = NULL;
1929         int error = 0, attrflag, dattrflag, ret;
1930
1931         vap->va_type = VLNK;
1932         error = nfsrpc_symlink(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1933             ap->a_target, vap, cnp->cn_cred, cnp->cn_thread, &dnfsva,
1934             &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
1935         if (nfhp) {
1936                 ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
1937                     &np, NULL);
1938                 if (!ret)
1939                         newvp = NFSTOV(np);
1940                 else if (!error)
1941                         error = ret;
1942         }
1943         if (newvp != NULL) {
1944                 if (attrflag)
1945                         (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1946                             0, 1);
1947         } else if (!error) {
1948                 /*
1949                  * If we do not have an error and we could not extract the
1950                  * newvp from the response due to the request being NFSv2, we
1951                  * have to do a lookup in order to obtain a newvp to return.
1952                  */
1953                 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1954                     cnp->cn_cred, cnp->cn_thread, &np);
1955                 if (!error)
1956                         newvp = NFSTOV(np);
1957         }
1958         if (error) {
1959                 if (newvp)
1960                         vput(newvp);
1961                 if (NFS_ISV4(dvp))
1962                         error = nfscl_maperr(cnp->cn_thread, error,
1963                             vap->va_uid, vap->va_gid);
1964         } else {
1965                 /*
1966                  * If negative lookup caching is enabled, I might as well
1967                  * add an entry for this node. Not necessary for correctness,
1968                  * but if negative caching is enabled, then the system
1969                  * must care about lookup caching hit rate, so...
1970                  */
1971                 if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
1972                     (cnp->cn_flags & MAKEENTRY))
1973                         cache_enter(dvp, newvp, cnp);
1974                 *ap->a_vpp = newvp;
1975         }
1976
1977         dnp = VTONFS(dvp);
1978         mtx_lock(&dnp->n_mtx);
1979         dnp->n_flag |= NMODIFIED;
1980         mtx_unlock(&dnp->n_mtx);
1981         if (dattrflag)
1982                 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1983         else
1984                 dnp->n_attrstamp = 0;
1985         return (error);
1986 }
1987
1988 /*
1989  * nfs make dir call
1990  */
1991 static int
1992 nfs_mkdir(struct vop_mkdir_args *ap)
1993 {
1994         struct vnode *dvp = ap->a_dvp;
1995         struct vattr *vap = ap->a_vap;
1996         struct componentname *cnp = ap->a_cnp;
1997         struct nfsnode *np = NULL, *dnp;
1998         struct vnode *newvp = NULL;
1999         struct vattr vattr;
2000         struct nfsfh *nfhp;
2001         struct nfsvattr nfsva, dnfsva;
2002         int error = 0, attrflag, dattrflag, ret;
2003
2004         if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
2005                 return (error);
2006         vap->va_type = VDIR;
2007         error = nfsrpc_mkdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2008             vap, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &nfhp,
2009             &attrflag, &dattrflag, NULL);
2010         dnp = VTONFS(dvp);
2011         mtx_lock(&dnp->n_mtx);
2012         dnp->n_flag |= NMODIFIED;
2013         mtx_unlock(&dnp->n_mtx);
2014         if (dattrflag)
2015                 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2016         else
2017                 dnp->n_attrstamp = 0;
2018         if (nfhp) {
2019                 ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
2020                     &np, NULL);
2021                 if (!ret) {
2022                         newvp = NFSTOV(np);
2023                         if (attrflag)
2024                            (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
2025                                 NULL, 0, 1);
2026                 } else if (!error)
2027                         error = ret;
2028         }
2029         if (!error && newvp == NULL) {
2030                 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2031                     cnp->cn_cred, cnp->cn_thread, &np);
2032                 if (!error) {
2033                         newvp = NFSTOV(np);
2034                         if (newvp->v_type != VDIR)
2035                                 error = EEXIST;
2036                 }
2037         }
2038         if (error) {
2039                 if (newvp)
2040                         vput(newvp);
2041                 if (NFS_ISV4(dvp))
2042                         error = nfscl_maperr(cnp->cn_thread, error,
2043                             vap->va_uid, vap->va_gid);
2044         } else {
2045                 /*
2046                  * If negative lookup caching is enabled, I might as well
2047                  * add an entry for this node. Not necessary for correctness,
2048                  * but if negative caching is enabled, then the system
2049                  * must care about lookup caching hit rate, so...
2050                  */
2051                 if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2052                     (cnp->cn_flags & MAKEENTRY))
2053                         cache_enter(dvp, newvp, cnp);
2054                 *ap->a_vpp = newvp;
2055         }
2056         return (error);
2057 }
2058
2059 /*
2060  * nfs remove directory call
2061  */
2062 static int
2063 nfs_rmdir(struct vop_rmdir_args *ap)
2064 {
2065         struct vnode *vp = ap->a_vp;
2066         struct vnode *dvp = ap->a_dvp;
2067         struct componentname *cnp = ap->a_cnp;
2068         struct nfsnode *dnp;
2069         struct nfsvattr dnfsva;
2070         int error, dattrflag;
2071
2072         if (dvp == vp)
2073                 return (EINVAL);
2074         error = nfsrpc_rmdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2075             cnp->cn_cred, cnp->cn_thread, &dnfsva, &dattrflag, NULL);
2076         dnp = VTONFS(dvp);
2077         mtx_lock(&dnp->n_mtx);
2078         dnp->n_flag |= NMODIFIED;
2079         mtx_unlock(&dnp->n_mtx);
2080         if (dattrflag)
2081                 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2082         else
2083                 dnp->n_attrstamp = 0;
2084
2085         cache_purge(dvp);
2086         cache_purge(vp);
2087         if (error && NFS_ISV4(dvp))
2088                 error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
2089                     (gid_t)0);
2090         /*
2091          * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2092          */
2093         if (error == ENOENT)
2094                 error = 0;
2095         return (error);
2096 }
2097
2098 /*
2099  * nfs readdir call
2100  */
2101 static int
2102 nfs_readdir(struct vop_readdir_args *ap)
2103 {
2104         struct vnode *vp = ap->a_vp;
2105         struct nfsnode *np = VTONFS(vp);
2106         struct uio *uio = ap->a_uio;
2107         int tresid, error = 0;
2108         struct vattr vattr;
2109         
2110         if (vp->v_type != VDIR) 
2111                 return(EPERM);
2112
2113         /*
2114          * First, check for hit on the EOF offset cache
2115          */
2116         if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2117             (np->n_flag & NMODIFIED) == 0) {
2118                 if (VOP_GETATTR(vp, &vattr, ap->a_cred) == 0) {
2119                         mtx_lock(&np->n_mtx);
2120                         if ((NFS_ISV4(vp) && np->n_change == vattr.va_filerev) ||
2121                             !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
2122                                 mtx_unlock(&np->n_mtx);
2123                                 NFSINCRGLOBAL(newnfsstats.direofcache_hits);
2124                                 return (0);
2125                         } else
2126                                 mtx_unlock(&np->n_mtx);
2127                 }
2128         }
2129
2130         /*
2131          * Call ncl_bioread() to do the real work.
2132          */
2133         tresid = uio->uio_resid;
2134         error = ncl_bioread(vp, uio, 0, ap->a_cred);
2135
2136         if (!error && uio->uio_resid == tresid)
2137                 NFSINCRGLOBAL(newnfsstats.direofcache_misses);
2138         return (error);
2139 }
2140
2141 /*
2142  * Readdir rpc call.
2143  * Called from below the buffer cache by ncl_doio().
2144  */
2145 int
2146 ncl_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2147     struct thread *td)
2148 {
2149         struct nfsvattr nfsva;
2150         nfsuint64 *cookiep, cookie;
2151         struct nfsnode *dnp = VTONFS(vp);
2152         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2153         int error = 0, eof, attrflag;
2154
2155         KASSERT(uiop->uio_iovcnt == 1 &&
2156             (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2157             (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2158             ("nfs readdirrpc bad uio"));
2159
2160         /*
2161          * If there is no cookie, assume directory was stale.
2162          */
2163         ncl_dircookie_lock(dnp);
2164         cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2165         if (cookiep) {
2166                 cookie = *cookiep;
2167                 ncl_dircookie_unlock(dnp);
2168         } else {
2169                 ncl_dircookie_unlock(dnp);              
2170                 return (NFSERR_BAD_COOKIE);
2171         }
2172
2173         if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2174                 (void)ncl_fsinfo(nmp, vp, cred, td);
2175
2176         error = nfsrpc_readdir(vp, uiop, &cookie, cred, td, &nfsva,
2177             &attrflag, &eof, NULL);
2178         if (attrflag)
2179                 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2180
2181         if (!error) {
2182                 /*
2183                  * We are now either at the end of the directory or have filled
2184                  * the block.
2185                  */
2186                 if (eof)
2187                         dnp->n_direofoffset = uiop->uio_offset;
2188                 else {
2189                         if (uiop->uio_resid > 0)
2190                                 ncl_printf("EEK! readdirrpc resid > 0\n");
2191                         ncl_dircookie_lock(dnp);
2192                         cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2193                         *cookiep = cookie;
2194                         ncl_dircookie_unlock(dnp);
2195                 }
2196         } else if (NFS_ISV4(vp)) {
2197                 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2198         }
2199         return (error);
2200 }
2201
2202 /*
2203  * NFS V3 readdir plus RPC. Used in place of ncl_readdirrpc().
2204  */
2205 int
2206 ncl_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2207     struct thread *td)
2208 {
2209         struct nfsvattr nfsva;
2210         nfsuint64 *cookiep, cookie;
2211         struct nfsnode *dnp = VTONFS(vp);
2212         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2213         int error = 0, attrflag, eof;
2214
2215         KASSERT(uiop->uio_iovcnt == 1 &&
2216             (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2217             (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2218             ("nfs readdirplusrpc bad uio"));
2219
2220         /*
2221          * If there is no cookie, assume directory was stale.
2222          */
2223         ncl_dircookie_lock(dnp);
2224         cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2225         if (cookiep) {
2226                 cookie = *cookiep;
2227                 ncl_dircookie_unlock(dnp);
2228         } else {
2229                 ncl_dircookie_unlock(dnp);
2230                 return (NFSERR_BAD_COOKIE);
2231         }
2232
2233         if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2234                 (void)ncl_fsinfo(nmp, vp, cred, td);
2235         error = nfsrpc_readdirplus(vp, uiop, &cookie, cred, td, &nfsva,
2236             &attrflag, &eof, NULL);
2237         if (attrflag)
2238                 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2239
2240         if (!error) {
2241                 /*
2242                  * We are now either at end of the directory or have filled the
2243                  * the block.
2244                  */
2245                 if (eof)
2246                         dnp->n_direofoffset = uiop->uio_offset;
2247                 else {
2248                         if (uiop->uio_resid > 0)
2249                                 ncl_printf("EEK! readdirplusrpc resid > 0\n");
2250                         ncl_dircookie_lock(dnp);
2251                         cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2252                         *cookiep = cookie;
2253                         ncl_dircookie_unlock(dnp);
2254                 }
2255         } else if (NFS_ISV4(vp)) {
2256                 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2257         }
2258         return (error);
2259 }
2260
2261 /*
2262  * Silly rename. To make the NFS filesystem that is stateless look a little
2263  * more like the "ufs" a remove of an active vnode is translated to a rename
2264  * to a funny looking filename that is removed by nfs_inactive on the
2265  * nfsnode. There is the potential for another process on a different client
2266  * to create the same funny name between the nfs_lookitup() fails and the
2267  * nfs_rename() completes, but...
2268  */
2269 static int
2270 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2271 {
2272         struct sillyrename *sp;
2273         struct nfsnode *np;
2274         int error;
2275         short pid;
2276         unsigned int lticks;
2277
2278         cache_purge(dvp);
2279         np = VTONFS(vp);
2280         KASSERT(vp->v_type != VDIR, ("nfs: sillyrename dir"));
2281         MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2282             M_NEWNFSREQ, M_WAITOK);
2283         sp->s_cred = crhold(cnp->cn_cred);
2284         sp->s_dvp = dvp;
2285         VREF(dvp);
2286
2287         /* 
2288          * Fudge together a funny name.
2289          * Changing the format of the funny name to accomodate more 
2290          * sillynames per directory.
2291          * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is 
2292          * CPU ticks since boot.
2293          */
2294         pid = cnp->cn_thread->td_proc->p_pid;
2295         lticks = (unsigned int)ticks;
2296         for ( ; ; ) {
2297                 sp->s_namlen = sprintf(sp->s_name, 
2298                                        ".nfs.%08x.%04x4.4", lticks, 
2299                                        pid);
2300                 if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2301                                  cnp->cn_thread, NULL))
2302                         break;
2303                 lticks++;
2304         }
2305         error = nfs_renameit(dvp, vp, cnp, sp);
2306         if (error)
2307                 goto bad;
2308         error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2309                 cnp->cn_thread, &np);
2310         np->n_sillyrename = sp;
2311         return (0);
2312 bad:
2313         vrele(sp->s_dvp);
2314         crfree(sp->s_cred);
2315         free((caddr_t)sp, M_NEWNFSREQ);
2316         return (error);
2317 }
2318
2319 /*
2320  * Look up a file name and optionally either update the file handle or
2321  * allocate an nfsnode, depending on the value of npp.
2322  * npp == NULL  --> just do the lookup
2323  * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2324  *                      handled too
2325  * *npp != NULL --> update the file handle in the vnode
2326  */
2327 static int
2328 nfs_lookitup(struct vnode *dvp, char *name, int len, struct ucred *cred,
2329     struct thread *td, struct nfsnode **npp)
2330 {
2331         struct vnode *newvp = NULL, *vp;
2332         struct nfsnode *np, *dnp = VTONFS(dvp);
2333         struct nfsfh *nfhp, *onfhp;
2334         struct nfsvattr nfsva, dnfsva;
2335         struct componentname cn;
2336         int error = 0, attrflag, dattrflag;
2337         u_int hash;
2338
2339         error = nfsrpc_lookup(dvp, name, len, cred, td, &dnfsva, &nfsva,
2340             &nfhp, &attrflag, &dattrflag, NULL);
2341         if (dattrflag)
2342                 (void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2343         if (npp && !error) {
2344                 if (*npp != NULL) {
2345                     np = *npp;
2346                     vp = NFSTOV(np);
2347                     /*
2348                      * For NFSv4, check to see if it is the same name and
2349                      * replace the name, if it is different.
2350                      */
2351                     if (np->n_v4 != NULL && nfsva.na_type == VREG &&
2352                         (np->n_v4->n4_namelen != len ||
2353                          NFSBCMP(name, NFS4NODENAME(np->n_v4), len) ||
2354                          dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
2355                          NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2356                          dnp->n_fhp->nfh_len))) {
2357 #ifdef notdef
2358 { char nnn[100]; int nnnl;
2359 nnnl = (len < 100) ? len : 99;
2360 bcopy(name, nnn, nnnl);
2361 nnn[nnnl] = '\0';
2362 printf("replace=%s\n",nnn);
2363 }
2364 #endif
2365                             FREE((caddr_t)np->n_v4, M_NFSV4NODE);
2366                             MALLOC(np->n_v4, struct nfsv4node *,
2367                                 sizeof (struct nfsv4node) +
2368                                 dnp->n_fhp->nfh_len + len - 1,
2369                                 M_NFSV4NODE, M_WAITOK);
2370                             np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
2371                             np->n_v4->n4_namelen = len;
2372                             NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2373                                 dnp->n_fhp->nfh_len);
2374                             NFSBCOPY(name, NFS4NODENAME(np->n_v4), len);
2375                     }
2376                     hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len,
2377                         FNV1_32_INIT);
2378                     onfhp = np->n_fhp;
2379                     /*
2380                      * Rehash node for new file handle.
2381                      */
2382                     vfs_hash_rehash(vp, hash);
2383                     np->n_fhp = nfhp;
2384                     if (onfhp != NULL)
2385                         FREE((caddr_t)onfhp, M_NFSFH);
2386                     newvp = NFSTOV(np);
2387                 } else if (NFS_CMPFH(dnp, nfhp->nfh_fh, nfhp->nfh_len)) {
2388                     FREE((caddr_t)nfhp, M_NFSFH);
2389                     VREF(dvp);
2390                     newvp = dvp;
2391                 } else {
2392                     cn.cn_nameptr = name;
2393                     cn.cn_namelen = len;
2394                     error = nfscl_nget(dvp->v_mount, dvp, nfhp, &cn, td,
2395                         &np, NULL);
2396                     if (error)
2397                         return (error);
2398                     newvp = NFSTOV(np);
2399                 }
2400                 if (!attrflag && *npp == NULL) {
2401                         vrele(newvp);
2402                         return (ENOENT);
2403                 }
2404                 if (attrflag)
2405                         (void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2406                             0, 1);
2407         }
2408         if (npp && *npp == NULL) {
2409                 if (error) {
2410                         if (newvp) {
2411                                 if (newvp == dvp)
2412                                         vrele(newvp);
2413                                 else
2414                                         vput(newvp);
2415                         }
2416                 } else
2417                         *npp = np;
2418         }
2419         if (error && NFS_ISV4(dvp))
2420                 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2421         return (error);
2422 }
2423
2424 /*
2425  * Nfs Version 3 and 4 commit rpc
2426  */
2427 int
2428 ncl_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2429    struct thread *td)
2430 {
2431         struct nfsvattr nfsva;
2432         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2433         int error, attrflag;
2434         u_char verf[NFSX_VERF];
2435
2436         mtx_lock(&nmp->nm_mtx);
2437         if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
2438                 mtx_unlock(&nmp->nm_mtx);
2439                 return (0);
2440         }
2441         mtx_unlock(&nmp->nm_mtx);
2442         error = nfsrpc_commit(vp, offset, cnt, cred, td, verf, &nfsva,
2443             &attrflag, NULL);
2444         if (!error) {
2445                 if (NFSBCMP((caddr_t)nmp->nm_verf, verf, NFSX_VERF)) {
2446                         NFSBCOPY(verf, (caddr_t)nmp->nm_verf, NFSX_VERF);
2447                         error = NFSERR_STALEWRITEVERF;
2448                 }
2449                 if (!error && attrflag)
2450                         (void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL,
2451                             0, 1);
2452         } else if (NFS_ISV4(vp)) {
2453                 error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2454         }
2455         return (error);
2456 }
2457
2458 /*
2459  * Strategy routine.
2460  * For async requests when nfsiod(s) are running, queue the request by
2461  * calling ncl_asyncio(), otherwise just all ncl_doio() to do the
2462  * request.
2463  */
2464 static int
2465 nfs_strategy(struct vop_strategy_args *ap)
2466 {
2467         struct buf *bp = ap->a_bp;
2468         struct ucred *cr;
2469
2470         KASSERT(!(bp->b_flags & B_DONE),
2471             ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2472         BUF_ASSERT_HELD(bp);
2473
2474         if (bp->b_iocmd == BIO_READ)
2475                 cr = bp->b_rcred;
2476         else
2477                 cr = bp->b_wcred;
2478
2479         /*
2480          * If the op is asynchronous and an i/o daemon is waiting
2481          * queue the request, wake it up and wait for completion
2482          * otherwise just do it ourselves.
2483          */
2484         if ((bp->b_flags & B_ASYNC) == 0 ||
2485             ncl_asyncio(VFSTONFS(ap->a_vp->v_mount), bp, NOCRED, curthread))
2486                 (void) ncl_doio(ap->a_vp, bp, cr, curthread, 1);
2487         return (0);
2488 }
2489
2490 /*
2491  * fsync vnode op. Just call ncl_flush() with commit == 1.
2492  */
2493 /* ARGSUSED */
2494 static int
2495 nfs_fsync(struct vop_fsync_args *ap)
2496 {
2497         return (ncl_flush(ap->a_vp, ap->a_waitfor, NULL, ap->a_td, 1, 0));
2498 }
2499
2500 /*
2501  * Flush all the blocks associated with a vnode.
2502  *      Walk through the buffer pool and push any dirty pages
2503  *      associated with the vnode.
2504  * If the called_from_renewthread argument is TRUE, it has been called
2505  * from the NFSv4 renew thread and, as such, cannot block indefinitely
2506  * waiting for a buffer write to complete.
2507  */
2508 int
2509 ncl_flush(struct vnode *vp, int waitfor, struct ucred *cred, struct thread *td,
2510     int commit, int called_from_renewthread)
2511 {
2512         struct nfsnode *np = VTONFS(vp);
2513         struct buf *bp;
2514         int i;
2515         struct buf *nbp;
2516         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2517         int error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2518         int passone = 1, trycnt = 0;
2519         u_quad_t off, endoff, toff;
2520         struct ucred* wcred = NULL;
2521         struct buf **bvec = NULL;
2522         struct bufobj *bo;
2523 #ifndef NFS_COMMITBVECSIZ
2524 #define NFS_COMMITBVECSIZ       20
2525 #endif
2526         struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2527         int bvecsize = 0, bveccount;
2528
2529         if (called_from_renewthread != 0)
2530                 slptimeo = hz;
2531         if (nmp->nm_flag & NFSMNT_INT)
2532                 slpflag = NFS_PCATCH;
2533         if (!commit)
2534                 passone = 0;
2535         bo = &vp->v_bufobj;
2536         /*
2537          * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2538          * server, but has not been committed to stable storage on the server
2539          * yet. On the first pass, the byte range is worked out and the commit
2540          * rpc is done. On the second pass, ncl_writebp() is called to do the
2541          * job.
2542          */
2543 again:
2544         off = (u_quad_t)-1;
2545         endoff = 0;
2546         bvecpos = 0;
2547         if (NFS_ISV34(vp) && commit) {
2548                 if (bvec != NULL && bvec != bvec_on_stack)
2549                         free(bvec, M_TEMP);
2550                 /*
2551                  * Count up how many buffers waiting for a commit.
2552                  */
2553                 bveccount = 0;
2554                 BO_LOCK(bo);
2555                 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2556                         if (!BUF_ISLOCKED(bp) &&
2557                             (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2558                                 == (B_DELWRI | B_NEEDCOMMIT))
2559                                 bveccount++;
2560                 }
2561                 /*
2562                  * Allocate space to remember the list of bufs to commit.  It is
2563                  * important to use M_NOWAIT here to avoid a race with nfs_write.
2564                  * If we can't get memory (for whatever reason), we will end up
2565                  * committing the buffers one-by-one in the loop below.
2566                  */
2567                 if (bveccount > NFS_COMMITBVECSIZ) {
2568                         /*
2569                          * Release the vnode interlock to avoid a lock
2570                          * order reversal.
2571                          */
2572                         BO_UNLOCK(bo);
2573                         bvec = (struct buf **)
2574                                 malloc(bveccount * sizeof(struct buf *),
2575                                        M_TEMP, M_NOWAIT);
2576                         BO_LOCK(bo);
2577                         if (bvec == NULL) {
2578                                 bvec = bvec_on_stack;
2579                                 bvecsize = NFS_COMMITBVECSIZ;
2580                         } else
2581                                 bvecsize = bveccount;
2582                 } else {
2583                         bvec = bvec_on_stack;
2584                         bvecsize = NFS_COMMITBVECSIZ;
2585                 }
2586                 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2587                         if (bvecpos >= bvecsize)
2588                                 break;
2589                         if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2590                                 nbp = TAILQ_NEXT(bp, b_bobufs);
2591                                 continue;
2592                         }
2593                         if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2594                             (B_DELWRI | B_NEEDCOMMIT)) {
2595                                 BUF_UNLOCK(bp);
2596                                 nbp = TAILQ_NEXT(bp, b_bobufs);
2597                                 continue;
2598                         }
2599                         BO_UNLOCK(bo);
2600                         bremfree(bp);
2601                         /*
2602                          * Work out if all buffers are using the same cred
2603                          * so we can deal with them all with one commit.
2604                          *
2605                          * NOTE: we are not clearing B_DONE here, so we have
2606                          * to do it later on in this routine if we intend to
2607                          * initiate I/O on the bp.
2608                          *
2609                          * Note: to avoid loopback deadlocks, we do not
2610                          * assign b_runningbufspace.
2611                          */
2612                         if (wcred == NULL)
2613                                 wcred = bp->b_wcred;
2614                         else if (wcred != bp->b_wcred)
2615                                 wcred = NOCRED;
2616                         vfs_busy_pages(bp, 1);
2617
2618                         BO_LOCK(bo);
2619                         /*
2620                          * bp is protected by being locked, but nbp is not
2621                          * and vfs_busy_pages() may sleep.  We have to
2622                          * recalculate nbp.
2623                          */
2624                         nbp = TAILQ_NEXT(bp, b_bobufs);
2625
2626                         /*
2627                          * A list of these buffers is kept so that the
2628                          * second loop knows which buffers have actually
2629                          * been committed. This is necessary, since there
2630                          * may be a race between the commit rpc and new
2631                          * uncommitted writes on the file.
2632                          */
2633                         bvec[bvecpos++] = bp;
2634                         toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2635                                 bp->b_dirtyoff;
2636                         if (toff < off)
2637                                 off = toff;
2638                         toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2639                         if (toff > endoff)
2640                                 endoff = toff;
2641                 }
2642                 BO_UNLOCK(bo);
2643         }
2644         if (bvecpos > 0) {
2645                 /*
2646                  * Commit data on the server, as required.
2647                  * If all bufs are using the same wcred, then use that with
2648                  * one call for all of them, otherwise commit each one
2649                  * separately.
2650                  */
2651                 if (wcred != NOCRED)
2652                         retv = ncl_commit(vp, off, (int)(endoff - off),
2653                                           wcred, td);
2654                 else {
2655                         retv = 0;
2656                         for (i = 0; i < bvecpos; i++) {
2657                                 off_t off, size;
2658                                 bp = bvec[i];
2659                                 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2660                                         bp->b_dirtyoff;
2661                                 size = (u_quad_t)(bp->b_dirtyend
2662                                                   - bp->b_dirtyoff);
2663                                 retv = ncl_commit(vp, off, (int)size,
2664                                                   bp->b_wcred, td);
2665                                 if (retv) break;
2666                         }
2667                 }
2668
2669                 if (retv == NFSERR_STALEWRITEVERF)
2670                         ncl_clearcommit(vp->v_mount);
2671
2672                 /*
2673                  * Now, either mark the blocks I/O done or mark the
2674                  * blocks dirty, depending on whether the commit
2675                  * succeeded.
2676                  */
2677                 for (i = 0; i < bvecpos; i++) {
2678                         bp = bvec[i];
2679                         bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2680                         if (retv) {
2681                                 /*
2682                                  * Error, leave B_DELWRI intact
2683                                  */
2684                                 vfs_unbusy_pages(bp);
2685                                 brelse(bp);
2686                         } else {
2687                                 /*
2688                                  * Success, remove B_DELWRI ( bundirty() ).
2689                                  *
2690                                  * b_dirtyoff/b_dirtyend seem to be NFS
2691                                  * specific.  We should probably move that
2692                                  * into bundirty(). XXX
2693                                  */
2694                                 bufobj_wref(bo);
2695                                 bp->b_flags |= B_ASYNC;
2696                                 bundirty(bp);
2697                                 bp->b_flags &= ~B_DONE;
2698                                 bp->b_ioflags &= ~BIO_ERROR;
2699                                 bp->b_dirtyoff = bp->b_dirtyend = 0;
2700                                 bufdone(bp);
2701                         }
2702                 }
2703         }
2704
2705         /*
2706          * Start/do any write(s) that are required.
2707          */
2708 loop:
2709         BO_LOCK(bo);
2710         TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2711                 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2712                         if (waitfor != MNT_WAIT || passone)
2713                                 continue;
2714
2715                         error = BUF_TIMELOCK(bp,
2716                             LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2717                             BO_MTX(bo), "nfsfsync", slpflag, slptimeo);
2718                         if (error == 0) {
2719                                 BUF_UNLOCK(bp);
2720                                 goto loop;
2721                         }
2722                         if (error == ENOLCK) {
2723                                 error = 0;
2724                                 goto loop;
2725                         }
2726                         if (called_from_renewthread != 0) {
2727                                 /*
2728                                  * Return EIO so the flush will be retried
2729                                  * later.
2730                                  */
2731                                 error = EIO;
2732                                 goto done;
2733                         }
2734                         if (newnfs_sigintr(nmp, td)) {
2735                                 error = EINTR;
2736                                 goto done;
2737                         }
2738                         if (slpflag & PCATCH) {
2739                                 slpflag = 0;
2740                                 slptimeo = 2 * hz;
2741                         }
2742                         goto loop;
2743                 }
2744                 if ((bp->b_flags & B_DELWRI) == 0)
2745                         panic("nfs_fsync: not dirty");
2746                 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
2747                         BUF_UNLOCK(bp);
2748                         continue;
2749                 }
2750                 BO_UNLOCK(bo);
2751                 bremfree(bp);
2752                 if (passone || !commit)
2753                     bp->b_flags |= B_ASYNC;
2754                 else
2755                     bp->b_flags |= B_ASYNC;
2756                 bwrite(bp);
2757                 if (newnfs_sigintr(nmp, td)) {
2758                         error = EINTR;
2759                         goto done;
2760                 }
2761                 goto loop;
2762         }
2763         if (passone) {
2764                 passone = 0;
2765                 BO_UNLOCK(bo);
2766                 goto again;
2767         }
2768         if (waitfor == MNT_WAIT) {
2769                 while (bo->bo_numoutput) {
2770                         error = bufobj_wwait(bo, slpflag, slptimeo);
2771                         if (error) {
2772                             BO_UNLOCK(bo);
2773                             if (called_from_renewthread != 0) {
2774                                 /*
2775                                  * Return EIO so that the flush will be
2776                                  * retried later.
2777                                  */
2778                                 error = EIO;
2779                                 goto done;
2780                             }
2781                             error = newnfs_sigintr(nmp, td);
2782                             if (error)
2783                                 goto done;
2784                             if (slpflag & PCATCH) {
2785                                 slpflag = 0;
2786                                 slptimeo = 2 * hz;
2787                             }
2788                             BO_LOCK(bo);
2789                         }
2790                 }
2791                 if (bo->bo_dirty.bv_cnt != 0 && commit) {
2792                         BO_UNLOCK(bo);
2793                         goto loop;
2794                 }
2795                 /*
2796                  * Wait for all the async IO requests to drain
2797                  */
2798                 BO_UNLOCK(bo);
2799                 mtx_lock(&np->n_mtx);
2800                 while (np->n_directio_asyncwr > 0) {
2801                         np->n_flag |= NFSYNCWAIT;
2802                         error = newnfs_msleep(td, &np->n_directio_asyncwr,
2803                             &np->n_mtx, slpflag | (PRIBIO + 1), 
2804                             "nfsfsync", 0);
2805                         if (error) {
2806                                 if (newnfs_sigintr(nmp, td)) {
2807                                         mtx_unlock(&np->n_mtx);
2808                                         error = EINTR;  
2809                                         goto done;
2810                                 }
2811                         }
2812                 }
2813                 mtx_unlock(&np->n_mtx);
2814         } else
2815                 BO_UNLOCK(bo);
2816         mtx_lock(&np->n_mtx);
2817         if (np->n_flag & NWRITEERR) {
2818                 error = np->n_error;
2819                 np->n_flag &= ~NWRITEERR;
2820         }
2821         if (commit && bo->bo_dirty.bv_cnt == 0 &&
2822             bo->bo_numoutput == 0 && np->n_directio_asyncwr == 0)
2823                 np->n_flag &= ~NMODIFIED;
2824         mtx_unlock(&np->n_mtx);
2825 done:
2826         if (bvec != NULL && bvec != bvec_on_stack)
2827                 free(bvec, M_TEMP);
2828         if (error == 0 && commit != 0 && waitfor == MNT_WAIT &&
2829             (bo->bo_dirty.bv_cnt != 0 || bo->bo_numoutput != 0 ||
2830              np->n_directio_asyncwr != 0) && trycnt++ < 5) {
2831                 /* try, try again... */
2832                 passone = 1;
2833                 wcred = NULL;
2834                 bvec = NULL;
2835                 bvecsize = 0;
2836 printf("try%d\n", trycnt);
2837                 goto again;
2838         }
2839         return (error);
2840 }
2841
2842 /*
2843  * NFS advisory byte-level locks.
2844  */
2845 static int
2846 nfs_advlock(struct vop_advlock_args *ap)
2847 {
2848         struct vnode *vp = ap->a_vp;
2849         struct ucred *cred;
2850         struct nfsnode *np = VTONFS(ap->a_vp);
2851         struct proc *p = (struct proc *)ap->a_id;
2852         struct thread *td = curthread;  /* XXX */
2853         struct vattr va;
2854         int ret, error = EOPNOTSUPP;
2855         u_quad_t size;
2856         
2857         if (NFS_ISV4(vp) && (ap->a_flags & F_POSIX)) {
2858                 cred = p->p_ucred;
2859                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2860                 if (vp->v_iflag & VI_DOOMED) {
2861                         VOP_UNLOCK(vp, 0);
2862                         return (EBADF);
2863                 }
2864
2865                 /*
2866                  * If this is unlocking a write locked region, flush and
2867                  * commit them before unlocking. This is required by
2868                  * RFC3530 Sec. 9.3.2.
2869                  */
2870                 if (ap->a_op == F_UNLCK &&
2871                     nfscl_checkwritelocked(vp, ap->a_fl, cred, td))
2872                         (void) ncl_flush(vp, MNT_WAIT, cred, td, 1, 0);
2873
2874                 /*
2875                  * Loop around doing the lock op, while a blocking lock
2876                  * must wait for the lock op to succeed.
2877                  */
2878                 do {
2879                         ret = nfsrpc_advlock(vp, np->n_size, ap->a_op,
2880                             ap->a_fl, 0, cred, td);
2881                         if (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
2882                             ap->a_op == F_SETLK) {
2883                                 VOP_UNLOCK(vp, 0);
2884                                 error = nfs_catnap(PZERO | PCATCH, ret,
2885                                     "ncladvl");
2886                                 if (error)
2887                                         return (EINTR);
2888                                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2889                                 if (vp->v_iflag & VI_DOOMED) {
2890                                         VOP_UNLOCK(vp, 0);
2891                                         return (EBADF);
2892                                 }
2893                         }
2894                 } while (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
2895                      ap->a_op == F_SETLK);
2896                 if (ret == NFSERR_DENIED) {
2897                         VOP_UNLOCK(vp, 0);
2898                         return (EAGAIN);
2899                 } else if (ret == EINVAL || ret == EBADF || ret == EINTR) {
2900                         VOP_UNLOCK(vp, 0);
2901                         return (ret);
2902                 } else if (ret != 0) {
2903                         VOP_UNLOCK(vp, 0);
2904                         return (EACCES);
2905                 }
2906
2907                 /*
2908                  * Now, if we just got a lock, invalidate data in the buffer
2909                  * cache, as required, so that the coherency conforms with
2910                  * RFC3530 Sec. 9.3.2.
2911                  */
2912                 if (ap->a_op == F_SETLK) {
2913                         if ((np->n_flag & NMODIFIED) == 0) {
2914                                 np->n_attrstamp = 0;
2915                                 ret = VOP_GETATTR(vp, &va, cred);
2916                         }
2917                         if ((np->n_flag & NMODIFIED) || ret ||
2918                             np->n_change != va.va_filerev) {
2919                                 (void) ncl_vinvalbuf(vp, V_SAVE, td, 1);
2920                                 np->n_attrstamp = 0;
2921                                 ret = VOP_GETATTR(vp, &va, cred);
2922                                 if (!ret) {
2923                                         np->n_mtime = va.va_mtime;
2924                                         np->n_change = va.va_filerev;
2925                                 }
2926                         }
2927                 }
2928                 VOP_UNLOCK(vp, 0);
2929                 return (0);
2930         } else if (!NFS_ISV4(vp)) {
2931                 error = vn_lock(vp, LK_SHARED);
2932                 if (error)
2933                         return (error);
2934                 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
2935                         size = VTONFS(vp)->n_size;
2936                         VOP_UNLOCK(vp, 0);
2937                         error = lf_advlock(ap, &(vp->v_lockf), size);
2938                 } else {
2939                         if (nfs_advlock_p != NULL)
2940                                 error = nfs_advlock_p(ap);
2941                         else {
2942                                 VOP_UNLOCK(vp, 0);
2943                                 error = ENOLCK;
2944                         }
2945                 }
2946         }
2947         return (error);
2948 }
2949
2950 /*
2951  * NFS advisory byte-level locks.
2952  */
2953 static int
2954 nfs_advlockasync(struct vop_advlockasync_args *ap)
2955 {
2956         struct vnode *vp = ap->a_vp;
2957         u_quad_t size;
2958         int error;
2959         
2960         if (NFS_ISV4(vp))
2961                 return (EOPNOTSUPP);
2962         error = vn_lock(vp, LK_SHARED);
2963         if (error)
2964                 return (error);
2965         if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
2966                 size = VTONFS(vp)->n_size;
2967                 VOP_UNLOCK(vp, 0);
2968                 error = lf_advlockasync(ap, &(vp->v_lockf), size);
2969         } else {
2970                 VOP_UNLOCK(vp, 0);
2971                 error = EOPNOTSUPP;
2972         }
2973         return (error);
2974 }
2975
2976 /*
2977  * Print out the contents of an nfsnode.
2978  */
2979 static int
2980 nfs_print(struct vop_print_args *ap)
2981 {
2982         struct vnode *vp = ap->a_vp;
2983         struct nfsnode *np = VTONFS(vp);
2984
2985         ncl_printf("\tfileid %ld fsid 0x%x",
2986            np->n_vattr.na_fileid, np->n_vattr.na_fsid);
2987         if (vp->v_type == VFIFO)
2988                 fifo_printinfo(vp);
2989         printf("\n");
2990         return (0);
2991 }
2992
2993 /*
2994  * This is the "real" nfs::bwrite(struct buf*).
2995  * We set B_CACHE if this is a VMIO buffer.
2996  */
2997 int
2998 ncl_writebp(struct buf *bp, int force __unused, struct thread *td)
2999 {
3000         int s;
3001         int oldflags = bp->b_flags;
3002 #if 0
3003         int retv = 1;
3004         off_t off;
3005 #endif
3006
3007         BUF_ASSERT_HELD(bp);
3008
3009         if (bp->b_flags & B_INVAL) {
3010                 brelse(bp);
3011                 return(0);
3012         }
3013
3014         bp->b_flags |= B_CACHE;
3015
3016         /*
3017          * Undirty the bp.  We will redirty it later if the I/O fails.
3018          */
3019
3020         s = splbio();
3021         bundirty(bp);
3022         bp->b_flags &= ~B_DONE;
3023         bp->b_ioflags &= ~BIO_ERROR;
3024         bp->b_iocmd = BIO_WRITE;
3025
3026         bufobj_wref(bp->b_bufobj);
3027         curthread->td_ru.ru_oublock++;
3028         splx(s);
3029
3030         /*
3031          * Note: to avoid loopback deadlocks, we do not
3032          * assign b_runningbufspace.
3033          */
3034         vfs_busy_pages(bp, 1);
3035
3036         BUF_KERNPROC(bp);
3037         bp->b_iooffset = dbtob(bp->b_blkno);
3038         bstrategy(bp);
3039
3040         if( (oldflags & B_ASYNC) == 0) {
3041                 int rtval = bufwait(bp);
3042
3043                 if (oldflags & B_DELWRI) {
3044                         s = splbio();
3045                         reassignbuf(bp);
3046                         splx(s);
3047                 }
3048                 brelse(bp);
3049                 return (rtval);
3050         }
3051
3052         return (0);
3053 }
3054
3055 /*
3056  * nfs special file access vnode op.
3057  * Essentially just get vattr and then imitate iaccess() since the device is
3058  * local to the client.
3059  */
3060 static int
3061 nfsspec_access(struct vop_access_args *ap)
3062 {
3063         struct vattr *vap;
3064         struct ucred *cred = ap->a_cred;
3065         struct vnode *vp = ap->a_vp;
3066         accmode_t accmode = ap->a_accmode;
3067         struct vattr vattr;
3068         int error;
3069
3070         /*
3071          * Disallow write attempts on filesystems mounted read-only;
3072          * unless the file is a socket, fifo, or a block or character
3073          * device resident on the filesystem.
3074          */
3075         if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3076                 switch (vp->v_type) {
3077                 case VREG:
3078                 case VDIR:
3079                 case VLNK:
3080                         return (EROFS);
3081                 default:
3082                         break;
3083                 }
3084         }
3085         vap = &vattr;
3086         error = VOP_GETATTR(vp, vap, cred);
3087         if (error)
3088                 goto out;
3089         error  = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3090             accmode, cred, NULL);
3091 out:
3092         return error;
3093 }
3094
3095 /*
3096  * Read wrapper for fifos.
3097  */
3098 static int
3099 nfsfifo_read(struct vop_read_args *ap)
3100 {
3101         struct nfsnode *np = VTONFS(ap->a_vp);
3102         int error;
3103
3104         /*
3105          * Set access flag.
3106          */
3107         mtx_lock(&np->n_mtx);
3108         np->n_flag |= NACC;
3109         getnanotime(&np->n_atim);
3110         mtx_unlock(&np->n_mtx);
3111         error = fifo_specops.vop_read(ap);
3112         return error;   
3113 }
3114
3115 /*
3116  * Write wrapper for fifos.
3117  */
3118 static int
3119 nfsfifo_write(struct vop_write_args *ap)
3120 {
3121         struct nfsnode *np = VTONFS(ap->a_vp);
3122
3123         /*
3124          * Set update flag.
3125          */
3126         mtx_lock(&np->n_mtx);
3127         np->n_flag |= NUPD;
3128         getnanotime(&np->n_mtim);
3129         mtx_unlock(&np->n_mtx);
3130         return(fifo_specops.vop_write(ap));
3131 }
3132
3133 /*
3134  * Close wrapper for fifos.
3135  *
3136  * Update the times on the nfsnode then do fifo close.
3137  */
3138 static int
3139 nfsfifo_close(struct vop_close_args *ap)
3140 {
3141         struct vnode *vp = ap->a_vp;
3142         struct nfsnode *np = VTONFS(vp);
3143         struct vattr vattr;
3144         struct timespec ts;
3145
3146         mtx_lock(&np->n_mtx);
3147         if (np->n_flag & (NACC | NUPD)) {
3148                 getnanotime(&ts);
3149                 if (np->n_flag & NACC)
3150                         np->n_atim = ts;
3151                 if (np->n_flag & NUPD)
3152                         np->n_mtim = ts;
3153                 np->n_flag |= NCHG;
3154                 if (vrefcnt(vp) == 1 &&
3155                     (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3156                         VATTR_NULL(&vattr);
3157                         if (np->n_flag & NACC)
3158                                 vattr.va_atime = np->n_atim;
3159                         if (np->n_flag & NUPD)
3160                                 vattr.va_mtime = np->n_mtim;
3161                         mtx_unlock(&np->n_mtx);
3162                         (void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3163                         goto out;
3164                 }
3165         }
3166         mtx_unlock(&np->n_mtx);
3167 out:
3168         return (fifo_specops.vop_close(ap));
3169 }
3170
3171 /*
3172  * Just call ncl_writebp() with the force argument set to 1.
3173  *
3174  * NOTE: B_DONE may or may not be set in a_bp on call.
3175  */
3176 static int
3177 nfs_bwrite(struct buf *bp)
3178 {
3179
3180         return (ncl_writebp(bp, 1, curthread));
3181 }
3182
3183 struct buf_ops buf_ops_newnfs = {
3184         .bop_name       =       "buf_ops_nfs",
3185         .bop_write      =       nfs_bwrite,
3186         .bop_strategy   =       bufstrategy,
3187         .bop_sync       =       bufsync,
3188         .bop_bdflush    =       bufbdflush,
3189 };
3190
3191 /*
3192  * Cloned from vop_stdlock(), and then the ugly hack added.
3193  */
3194 static int
3195 nfs_lock1(struct vop_lock1_args *ap)
3196 {
3197         struct vnode *vp = ap->a_vp;
3198         int error = 0;
3199
3200         /*
3201          * Since vfs_hash_get() calls vget() and it will no longer work
3202          * for FreeBSD8 with flags == 0, I can only think of this horrible
3203          * hack to work around it. I call vfs_hash_get() with LK_EXCLOTHER
3204          * and then handle it here. All I want for this case is a v_usecount
3205          * on the vnode to use for recovery, while another thread might
3206          * hold a lock on the vnode. I have the other threads blocked, so
3207          * there isn't any race problem.
3208          */
3209         if ((ap->a_flags & LK_TYPE_MASK) == LK_EXCLOTHER) {
3210                 if ((ap->a_flags & LK_INTERLOCK) == 0)
3211                         panic("ncllock1");
3212                 if ((vp->v_iflag & VI_DOOMED))
3213                         error = ENOENT;
3214                 VI_UNLOCK(vp);
3215                 return (error);
3216         }
3217         return (_lockmgr_args(vp->v_vnlock, ap->a_flags, VI_MTX(vp),
3218             LK_WMESG_DEFAULT, LK_PRIO_DEFAULT, LK_TIMO_DEFAULT, ap->a_file,
3219             ap->a_line));
3220 }
3221
3222 #ifdef NFS4_ACL_EXTATTR_NAME
3223 static int
3224 nfs_getacl(struct vop_getacl_args *ap)
3225 {
3226         int error;
3227
3228         if (ap->a_type != ACL_TYPE_NFS4)
3229                 return (EOPNOTSUPP);
3230         error = nfsrpc_getacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3231             NULL);
3232         if (error > NFSERR_STALE) {
3233                 (void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3234                 error = EPERM;
3235         }
3236         return (error);
3237 }
3238
3239 static int
3240 nfs_setacl(struct vop_setacl_args *ap)
3241 {
3242         int error;
3243
3244         if (ap->a_type != ACL_TYPE_NFS4)
3245                 return (EOPNOTSUPP);
3246         error = nfsrpc_setacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3247             NULL);
3248         if (error > NFSERR_STALE) {
3249                 (void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3250                 error = EPERM;
3251         }
3252         return (error);
3253 }
3254
3255 #endif  /* NFS4_ACL_EXTATTR_NAME */