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