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