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