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