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