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