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