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