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