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