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