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