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