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