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