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