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