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