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