Merge from vendor branch GCC:
[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.24 2004/06/04 05:06:40 hmp 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         struct nfsnode *np = VTONFS(vp);
780         caddr_t cp;
781         int32_t t1, t2;
782         caddr_t bpos, dpos, cp2;
783         u_int32_t *tl;
784         int error = 0, wccflag = NFSV3_WCCRATTR;
785         struct mbuf *mreq, *mrep, *md, *mb, *mb2;
786         int v3 = NFS_ISV3(vp);
787
788         nfsstats.rpccnt[NFSPROC_SETATTR]++;
789         nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
790         nfsm_fhtom(vp, v3);
791         if (v3) {
792                 nfsm_v3attrbuild(vap, TRUE);
793                 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
794                 *tl = nfs_false;
795         } else {
796                 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
797                 if (vap->va_mode == (mode_t)VNOVAL)
798                         sp->sa_mode = nfs_xdrneg1;
799                 else
800                         sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
801                 if (vap->va_uid == (uid_t)VNOVAL)
802                         sp->sa_uid = nfs_xdrneg1;
803                 else
804                         sp->sa_uid = txdr_unsigned(vap->va_uid);
805                 if (vap->va_gid == (gid_t)VNOVAL)
806                         sp->sa_gid = nfs_xdrneg1;
807                 else
808                         sp->sa_gid = txdr_unsigned(vap->va_gid);
809                 sp->sa_size = txdr_unsigned(vap->va_size);
810                 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
811                 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
812         }
813         nfsm_request(vp, NFSPROC_SETATTR, td, cred);
814         if (v3) {
815                 np->n_modestamp = 0;
816                 nfsm_wcc_data(vp, wccflag);
817         } else
818                 nfsm_loadattr(vp, (struct vattr *)0);
819         m_freem(mrep);
820 nfsmout:
821         return (error);
822 }
823
824 /*
825  * 'cached' nfs directory lookup
826  *
827  * nfs_lookup(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
828  *            struct vnode **a_vpp, struct componentname *a_cnp)
829  */
830 static int
831 nfs_lookup(struct vop_lookup_args *ap)
832 {
833         struct componentname *cnp = ap->a_cnp;
834         struct vnode *dvp = ap->a_dvp;
835         struct vnode **vpp = ap->a_vpp;
836         int flags = cnp->cn_flags;
837         struct vnode *newvp;
838         u_int32_t *tl;
839         caddr_t cp;
840         int32_t t1, t2;
841         struct nfsmount *nmp;
842         caddr_t bpos, dpos, cp2;
843         struct mbuf *mreq, *mrep, *md, *mb, *mb2;
844         long len;
845         nfsfh_t *fhp;
846         struct nfsnode *np;
847         int lockparent, wantparent, error = 0, attrflag, fhsize;
848         int v3 = NFS_ISV3(dvp);
849         struct thread *td = cnp->cn_td;
850
851         /*
852          * Read-only mount check and directory check.
853          */
854         *vpp = NULLVP;
855         if ((flags & CNP_ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
856             (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
857                 return (EROFS);
858
859         if (dvp->v_type != VDIR)
860                 return (ENOTDIR);
861
862         /*
863          * Look it up in the cache.  Note that ENOENT is only returned if we
864          * previously entered a negative hit (see later on).  The additional
865          * nfsneg_cache_timeout check causes previously cached results to
866          * be instantly ignored if the negative caching is turned off.
867          */
868         lockparent = flags & CNP_LOCKPARENT;
869         wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
870         nmp = VFSTONFS(dvp->v_mount);
871         np = VTONFS(dvp);
872         error = cache_lookup(dvp, NCPNULL, vpp, NCPPNULL, cnp);
873         if (error != 0) {
874                 struct vattr vattr;
875                 int vpid;
876
877                 if (error == ENOENT && nfsneg_cache_timeout) {
878                         *vpp = NULLVP;
879                         return (error);
880                 }
881                 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0) {
882                         *vpp = NULLVP;
883                         return (error);
884                 }
885
886                 newvp = *vpp;
887                 vpid = newvp->v_id;
888                 /*
889                  * See the comment starting `Step through' in ufs/ufs_lookup.c
890                  * for an explanation of the locking protocol
891                  */
892                 if (dvp == newvp) {
893                         vref(newvp);
894                         error = 0;
895                 } else if (flags & CNP_ISDOTDOT) {
896                         VOP_UNLOCK(dvp, NULL, 0, td);
897                         error = vget(newvp, NULL, LK_EXCLUSIVE, td);
898                         if (!error && lockparent && (flags & CNP_ISLASTCN))
899                                 error = vn_lock(dvp, NULL, LK_EXCLUSIVE, td);
900                 } else {
901                         error = vget(newvp, NULL, LK_EXCLUSIVE, td);
902                         if (!lockparent || error || !(flags & CNP_ISLASTCN))
903                                 VOP_UNLOCK(dvp, NULL, 0, td);
904                 }
905                 if (!error) {
906                         if (vpid == newvp->v_id) {
907                            if (!VOP_GETATTR(newvp, &vattr, td)
908                             && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
909                                 nfsstats.lookupcache_hits++;
910                                 if (cnp->cn_nameiop != NAMEI_LOOKUP &&
911                                     (flags & CNP_ISLASTCN))
912                                         cnp->cn_flags |= CNP_SAVENAME;
913                                 return (0);
914                            }
915                            cache_purge(newvp);
916                         }
917                         vput(newvp);
918                         if (lockparent && dvp != newvp && (flags & CNP_ISLASTCN))
919                                 VOP_UNLOCK(dvp, NULL, 0, td);
920                 }
921                 error = vn_lock(dvp, NULL, LK_EXCLUSIVE, td);
922                 *vpp = NULLVP;
923                 if (error)
924                         return (error);
925         }
926
927         /*
928          * Cache miss, go the wire.
929          */
930         error = 0;
931         newvp = NULLVP;
932         nfsstats.lookupcache_misses++;
933         nfsstats.rpccnt[NFSPROC_LOOKUP]++;
934         len = cnp->cn_namelen;
935         nfsm_reqhead(dvp, NFSPROC_LOOKUP,
936                 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
937         nfsm_fhtom(dvp, v3);
938         nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
939         nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_td, cnp->cn_cred);
940         if (error) {
941                 /*
942                  * Cache negatve lookups to reduce NFS traffic, but use
943                  * a fast timeout.
944                  */
945                 if (error == ENOENT &&
946                     (cnp->cn_flags & CNP_MAKEENTRY) && 
947                     cnp->cn_nameiop == NAMEI_LOOKUP &&
948                     nfsneg_cache_timeout) {
949                         int toval = nfsneg_cache_timeout * hz;
950                         if (cnp->cn_flags & CNP_CACHETIMEOUT) {
951                                 if (cnp->cn_timeout > toval)
952                                         cnp->cn_timeout = toval;
953                         } else {
954                                 cnp->cn_flags |= CNP_CACHETIMEOUT;
955                                 cnp->cn_timeout = toval;
956                         }
957                         cache_enter(dvp, NCPNULL, NULL, cnp);
958                 }
959                 nfsm_postop_attr(dvp, attrflag);
960                 m_freem(mrep);
961                 goto nfsmout;
962         }
963         nfsm_getfh(fhp, fhsize, v3);
964
965         /*
966          * Handle RENAME case...
967          */
968         if (cnp->cn_nameiop == NAMEI_RENAME && wantparent && (flags & CNP_ISLASTCN)) {
969                 if (NFS_CMPFH(np, fhp, fhsize)) {
970                         m_freem(mrep);
971                         return (EISDIR);
972                 }
973                 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
974                 if (error) {
975                         m_freem(mrep);
976                         return (error);
977                 }
978                 newvp = NFSTOV(np);
979                 if (v3) {
980                         nfsm_postop_attr(newvp, attrflag);
981                         nfsm_postop_attr(dvp, attrflag);
982                 } else
983                         nfsm_loadattr(newvp, (struct vattr *)0);
984                 *vpp = newvp;
985                 m_freem(mrep);
986                 cnp->cn_flags |= CNP_SAVENAME;
987                 if (!lockparent)
988                         VOP_UNLOCK(dvp, NULL, 0, td);
989                 return (0);
990         }
991
992         if (flags & CNP_ISDOTDOT) {
993                 VOP_UNLOCK(dvp, NULL, 0, td);
994                 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
995                 if (error) {
996                         vn_lock(dvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
997                         return (error);
998                 }
999                 newvp = NFSTOV(np);
1000                 if (lockparent && (flags & CNP_ISLASTCN) &&
1001                     (error = vn_lock(dvp, NULL, LK_EXCLUSIVE, td))) {
1002                         vput(newvp);
1003                         return (error);
1004                 }
1005         } else if (NFS_CMPFH(np, fhp, fhsize)) {
1006                 vref(dvp);
1007                 newvp = dvp;
1008         } else {
1009                 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1010                 if (error) {
1011                         m_freem(mrep);
1012                         return (error);
1013                 }
1014                 if (!lockparent || !(flags & CNP_ISLASTCN))
1015                         VOP_UNLOCK(dvp, NULL, 0, td);
1016                 newvp = NFSTOV(np);
1017         }
1018         if (v3) {
1019                 nfsm_postop_attr(newvp, attrflag);
1020                 nfsm_postop_attr(dvp, attrflag);
1021         } else
1022                 nfsm_loadattr(newvp, (struct vattr *)0);
1023         if (cnp->cn_nameiop != NAMEI_LOOKUP && (flags & CNP_ISLASTCN))
1024                 cnp->cn_flags |= CNP_SAVENAME;
1025         if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1026             (cnp->cn_nameiop != NAMEI_DELETE || !(flags & CNP_ISLASTCN))) {
1027                 np->n_ctime = np->n_vattr.va_ctime.tv_sec;
1028                 cache_enter(dvp, NCPNULL, newvp, cnp);
1029         }
1030         *vpp = newvp;
1031         m_freem(mrep);
1032 nfsmout:
1033         if (error) {
1034                 if (newvp != NULLVP) {
1035                         vrele(newvp);
1036                         *vpp = NULLVP;
1037                 }
1038                 if ((cnp->cn_nameiop == NAMEI_CREATE || cnp->cn_nameiop == NAMEI_RENAME) &&
1039                     (flags & CNP_ISLASTCN) && error == ENOENT) {
1040                         if (!lockparent)
1041                                 VOP_UNLOCK(dvp, NULL, 0, td);
1042                         if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1043                                 error = EROFS;
1044                         else
1045                                 error = EJUSTRETURN;
1046                 }
1047                 if (cnp->cn_nameiop != NAMEI_LOOKUP && (flags & CNP_ISLASTCN))
1048                         cnp->cn_flags |= CNP_SAVENAME;
1049         }
1050         return (error);
1051 }
1052
1053 /*
1054  * nfs read call.
1055  * Just call nfs_bioread() to do the work.
1056  *
1057  * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1058  *          struct ucred *a_cred)
1059  */
1060 static int
1061 nfs_read(struct vop_read_args *ap)
1062 {
1063         struct vnode *vp = ap->a_vp;
1064
1065         return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1066         switch (vp->v_type) {
1067         case VREG:
1068                 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1069         case VDIR:
1070                 return (EISDIR);
1071         default:
1072                 return EOPNOTSUPP;
1073         }
1074 }
1075
1076 /*
1077  * nfs readlink call
1078  *
1079  * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1080  */
1081 static int
1082 nfs_readlink(struct vop_readlink_args *ap)
1083 {
1084         struct vnode *vp = ap->a_vp;
1085
1086         if (vp->v_type != VLNK)
1087                 return (EINVAL);
1088         return (nfs_bioread(vp, ap->a_uio, 0));
1089 }
1090
1091 /*
1092  * Do a readlink rpc.
1093  * Called by nfs_doio() from below the buffer cache.
1094  */
1095 int
1096 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop)
1097 {
1098         u_int32_t *tl;
1099         caddr_t cp;
1100         int32_t t1, t2;
1101         caddr_t bpos, dpos, cp2;
1102         int error = 0, len, attrflag;
1103         struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1104         int v3 = NFS_ISV3(vp);
1105
1106         nfsstats.rpccnt[NFSPROC_READLINK]++;
1107         nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
1108         nfsm_fhtom(vp, v3);
1109         nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, nfs_vpcred(vp, ND_CHECK));
1110         if (v3)
1111                 nfsm_postop_attr(vp, attrflag);
1112         if (!error) {
1113                 nfsm_strsiz(len, NFS_MAXPATHLEN);
1114                 if (len == NFS_MAXPATHLEN) {
1115                         struct nfsnode *np = VTONFS(vp);
1116                         if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1117                                 len = np->n_size;
1118                 }
1119                 nfsm_mtouio(uiop, len);
1120         }
1121         m_freem(mrep);
1122 nfsmout:
1123         return (error);
1124 }
1125
1126 /*
1127  * nfs read rpc call
1128  * Ditto above
1129  */
1130 int
1131 nfs_readrpc(struct vnode *vp, struct uio *uiop)
1132 {
1133         u_int32_t *tl;
1134         caddr_t cp;
1135         int32_t t1, t2;
1136         caddr_t bpos, dpos, cp2;
1137         struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1138         struct nfsmount *nmp;
1139         int error = 0, len, retlen, tsiz, eof, attrflag;
1140         int v3 = NFS_ISV3(vp);
1141
1142 #ifndef nolint
1143         eof = 0;
1144 #endif
1145         nmp = VFSTONFS(vp->v_mount);
1146         tsiz = uiop->uio_resid;
1147         if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1148                 return (EFBIG);
1149         while (tsiz > 0) {
1150                 nfsstats.rpccnt[NFSPROC_READ]++;
1151                 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1152                 nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1153                 nfsm_fhtom(vp, v3);
1154                 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1155                 if (v3) {
1156                         txdr_hyper(uiop->uio_offset, tl);
1157                         *(tl + 2) = txdr_unsigned(len);
1158                 } else {
1159                         *tl++ = txdr_unsigned(uiop->uio_offset);
1160                         *tl++ = txdr_unsigned(len);
1161                         *tl = 0;
1162                 }
1163                 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, nfs_vpcred(vp, ND_READ));
1164                 if (v3) {
1165                         nfsm_postop_attr(vp, attrflag);
1166                         if (error) {
1167                                 m_freem(mrep);
1168                                 goto nfsmout;
1169                         }
1170                         nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1171                         eof = fxdr_unsigned(int, *(tl + 1));
1172                 } else
1173                         nfsm_loadattr(vp, (struct vattr *)0);
1174                 nfsm_strsiz(retlen, nmp->nm_rsize);
1175                 nfsm_mtouio(uiop, retlen);
1176                 m_freem(mrep);
1177                 tsiz -= retlen;
1178                 if (v3) {
1179                         if (eof || retlen == 0) {
1180                                 tsiz = 0;
1181                         }
1182                 } else if (retlen < len) {
1183                         tsiz = 0;
1184                 }
1185         }
1186 nfsmout:
1187         return (error);
1188 }
1189
1190 /*
1191  * nfs write call
1192  */
1193 int
1194 nfs_writerpc(struct vnode *vp, struct uio *uiop, int *iomode, int *must_commit)
1195 {
1196         u_int32_t *tl;
1197         caddr_t cp;
1198         int32_t t1, t2, backup;
1199         caddr_t bpos, dpos, cp2;
1200         struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1201         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1202         int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1203         int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1204
1205 #ifndef DIAGNOSTIC
1206         if (uiop->uio_iovcnt != 1)
1207                 panic("nfs: writerpc iovcnt > 1");
1208 #endif
1209         *must_commit = 0;
1210         tsiz = uiop->uio_resid;
1211         if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1212                 return (EFBIG);
1213         while (tsiz > 0) {
1214                 nfsstats.rpccnt[NFSPROC_WRITE]++;
1215                 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1216                 nfsm_reqhead(vp, NFSPROC_WRITE,
1217                         NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1218                 nfsm_fhtom(vp, v3);
1219                 if (v3) {
1220                         nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1221                         txdr_hyper(uiop->uio_offset, tl);
1222                         tl += 2;
1223                         *tl++ = txdr_unsigned(len);
1224                         *tl++ = txdr_unsigned(*iomode);
1225                         *tl = txdr_unsigned(len);
1226                 } else {
1227                         u_int32_t x;
1228
1229                         nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1230                         /* Set both "begin" and "current" to non-garbage. */
1231                         x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1232                         *tl++ = x;      /* "begin offset" */
1233                         *tl++ = x;      /* "current offset" */
1234                         x = txdr_unsigned(len);
1235                         *tl++ = x;      /* total to this offset */
1236                         *tl = x;        /* size of this write */
1237                 }
1238                 nfsm_uiotom(uiop, len);
1239                 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, nfs_vpcred(vp, ND_WRITE));
1240                 if (v3) {
1241                         wccflag = NFSV3_WCCCHK;
1242                         nfsm_wcc_data(vp, wccflag);
1243                         if (!error) {
1244                                 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1245                                         + NFSX_V3WRITEVERF);
1246                                 rlen = fxdr_unsigned(int, *tl++);
1247                                 if (rlen == 0) {
1248                                         error = NFSERR_IO;
1249                                         m_freem(mrep);
1250                                         break;
1251                                 } else if (rlen < len) {
1252                                         backup = len - rlen;
1253                                         uiop->uio_iov->iov_base -= backup;
1254                                         uiop->uio_iov->iov_len += backup;
1255                                         uiop->uio_offset -= backup;
1256                                         uiop->uio_resid += backup;
1257                                         len = rlen;
1258                                 }
1259                                 commit = fxdr_unsigned(int, *tl++);
1260
1261                                 /*
1262                                  * Return the lowest committment level
1263                                  * obtained by any of the RPCs.
1264                                  */
1265                                 if (committed == NFSV3WRITE_FILESYNC)
1266                                         committed = commit;
1267                                 else if (committed == NFSV3WRITE_DATASYNC &&
1268                                         commit == NFSV3WRITE_UNSTABLE)
1269                                         committed = commit;
1270                                 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1271                                     bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1272                                         NFSX_V3WRITEVERF);
1273                                     nmp->nm_state |= NFSSTA_HASWRITEVERF;
1274                                 } else if (bcmp((caddr_t)tl,
1275                                     (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1276                                     *must_commit = 1;
1277                                     bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1278                                         NFSX_V3WRITEVERF);
1279                                 }
1280                         }
1281                 } else
1282                     nfsm_loadattr(vp, (struct vattr *)0);
1283                 if (wccflag)
1284                     VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime.tv_sec;
1285                 m_freem(mrep);
1286                 if (error)
1287                         break;
1288                 tsiz -= len;
1289         }
1290 nfsmout:
1291         if (vp->v_mount->mnt_flag & MNT_ASYNC)
1292                 committed = NFSV3WRITE_FILESYNC;
1293         *iomode = committed;
1294         if (error)
1295                 uiop->uio_resid = tsiz;
1296         return (error);
1297 }
1298
1299 /*
1300  * nfs mknod rpc
1301  * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1302  * mode set to specify the file type and the size field for rdev.
1303  */
1304 static int
1305 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1306              struct vattr *vap)
1307 {
1308         struct nfsv2_sattr *sp;
1309         u_int32_t *tl;
1310         caddr_t cp;
1311         int32_t t1, t2;
1312         struct vnode *newvp = (struct vnode *)0;
1313         struct nfsnode *np = (struct nfsnode *)0;
1314         struct vattr vattr;
1315         char *cp2;
1316         caddr_t bpos, dpos;
1317         int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1318         struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1319         u_int32_t rdev;
1320         int v3 = NFS_ISV3(dvp);
1321
1322         if (vap->va_type == VCHR || vap->va_type == VBLK)
1323                 rdev = txdr_unsigned(vap->va_rdev);
1324         else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1325                 rdev = nfs_xdrneg1;
1326         else {
1327                 return (EOPNOTSUPP);
1328         }
1329         if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1330                 return (error);
1331         }
1332         nfsstats.rpccnt[NFSPROC_MKNOD]++;
1333         nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1334                 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1335         nfsm_fhtom(dvp, v3);
1336         nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1337         if (v3) {
1338                 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1339                 *tl++ = vtonfsv3_type(vap->va_type);
1340                 nfsm_v3attrbuild(vap, FALSE);
1341                 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1342                         nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1343                         *tl++ = txdr_unsigned(umajor(vap->va_rdev));
1344                         *tl = txdr_unsigned(uminor(vap->va_rdev));
1345                 }
1346         } else {
1347                 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1348                 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1349                 sp->sa_uid = nfs_xdrneg1;
1350                 sp->sa_gid = nfs_xdrneg1;
1351                 sp->sa_size = rdev;
1352                 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1353                 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1354         }
1355         nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_td, cnp->cn_cred);
1356         if (!error) {
1357                 nfsm_mtofh(dvp, newvp, v3, gotvp);
1358                 if (!gotvp) {
1359                         if (newvp) {
1360                                 vput(newvp);
1361                                 newvp = (struct vnode *)0;
1362                         }
1363                         error = nfs_lookitup(dvp, cnp->cn_nameptr,
1364                             cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1365                         if (!error)
1366                                 newvp = NFSTOV(np);
1367                 }
1368         }
1369         if (v3)
1370                 nfsm_wcc_data(dvp, wccflag);
1371         m_freem(mrep);
1372 nfsmout:
1373         if (error) {
1374                 if (newvp)
1375                         vput(newvp);
1376         } else {
1377                 if (cnp->cn_flags & CNP_MAKEENTRY)
1378                         cache_enter(dvp, NCPNULL, newvp, cnp);
1379                 *vpp = newvp;
1380         }
1381         VTONFS(dvp)->n_flag |= NMODIFIED;
1382         if (!wccflag)
1383                 VTONFS(dvp)->n_attrstamp = 0;
1384         return (error);
1385 }
1386
1387 /*
1388  * nfs mknod vop
1389  * just call nfs_mknodrpc() to do the work.
1390  *
1391  * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1392  *           struct componentname *a_cnp, struct vattr *a_vap)
1393  */
1394 /* ARGSUSED */
1395 static int
1396 nfs_mknod(struct vop_mknod_args *ap)
1397 {
1398         return nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1399 }
1400
1401 static u_long create_verf;
1402 /*
1403  * nfs file create call
1404  *
1405  * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1406  *            struct componentname *a_cnp, struct vattr *a_vap)
1407  */
1408 static int
1409 nfs_create(struct vop_create_args *ap)
1410 {
1411         struct vnode *dvp = ap->a_dvp;
1412         struct vattr *vap = ap->a_vap;
1413         struct componentname *cnp = ap->a_cnp;
1414         struct nfsv2_sattr *sp;
1415         u_int32_t *tl;
1416         caddr_t cp;
1417         int32_t t1, t2;
1418         struct nfsnode *np = (struct nfsnode *)0;
1419         struct vnode *newvp = (struct vnode *)0;
1420         caddr_t bpos, dpos, cp2;
1421         int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1422         struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1423         struct vattr vattr;
1424         int v3 = NFS_ISV3(dvp);
1425
1426         /*
1427          * Oops, not for me..
1428          */
1429         if (vap->va_type == VSOCK)
1430                 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1431
1432         if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1433                 return (error);
1434         }
1435         if (vap->va_vaflags & VA_EXCLUSIVE)
1436                 fmode |= O_EXCL;
1437 again:
1438         nfsstats.rpccnt[NFSPROC_CREATE]++;
1439         nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1440                 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1441         nfsm_fhtom(dvp, v3);
1442         nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1443         if (v3) {
1444                 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1445                 if (fmode & O_EXCL) {
1446                         *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1447                         nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1448 #ifdef INET
1449                         if (!TAILQ_EMPTY(&in_ifaddrhead))
1450                                 *tl++ = IA_SIN(in_ifaddrhead.tqh_first)->sin_addr.s_addr;
1451                         else
1452 #endif
1453                                 *tl++ = create_verf;
1454                         *tl = ++create_verf;
1455                 } else {
1456                         *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1457                         nfsm_v3attrbuild(vap, FALSE);
1458                 }
1459         } else {
1460                 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1461                 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1462                 sp->sa_uid = nfs_xdrneg1;
1463                 sp->sa_gid = nfs_xdrneg1;
1464                 sp->sa_size = 0;
1465                 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1466                 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1467         }
1468         nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_td, cnp->cn_cred);
1469         if (!error) {
1470                 nfsm_mtofh(dvp, newvp, v3, gotvp);
1471                 if (!gotvp) {
1472                         if (newvp) {
1473                                 vput(newvp);
1474                                 newvp = (struct vnode *)0;
1475                         }
1476                         error = nfs_lookitup(dvp, cnp->cn_nameptr,
1477                             cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1478                         if (!error)
1479                                 newvp = NFSTOV(np);
1480                 }
1481         }
1482         if (v3)
1483                 nfsm_wcc_data(dvp, wccflag);
1484         m_freem(mrep);
1485 nfsmout:
1486         if (error) {
1487                 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1488                         fmode &= ~O_EXCL;
1489                         goto again;
1490                 }
1491                 if (newvp)
1492                         vput(newvp);
1493         } else if (v3 && (fmode & O_EXCL)) {
1494                 /*
1495                  * We are normally called with only a partially initialized
1496                  * VAP.  Since the NFSv3 spec says that server may use the
1497                  * file attributes to store the verifier, the spec requires
1498                  * us to do a SETATTR RPC. FreeBSD servers store the verifier
1499                  * in atime, but we can't really assume that all servers will
1500                  * so we ensure that our SETATTR sets both atime and mtime.
1501                  */
1502                 if (vap->va_mtime.tv_sec == VNOVAL)
1503                         vfs_timestamp(&vap->va_mtime);
1504                 if (vap->va_atime.tv_sec == VNOVAL)
1505                         vap->va_atime = vap->va_mtime;
1506                 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1507         }
1508         if (!error) {
1509                 if (cnp->cn_flags & CNP_MAKEENTRY)
1510                         cache_enter(dvp, NCPNULL, newvp, cnp);
1511                 /*
1512                  * The new np may have enough info for access
1513                  * checks, make sure rucred and wucred are
1514                  * initialized for read and write rpc's.
1515                  */
1516                 np = VTONFS(newvp);
1517                 if (np->n_rucred == NULL)
1518                         np->n_rucred = crhold(cnp->cn_cred);
1519                 if (np->n_wucred == NULL)
1520                         np->n_wucred = crhold(cnp->cn_cred);
1521                 *ap->a_vpp = newvp;
1522         }
1523         VTONFS(dvp)->n_flag |= NMODIFIED;
1524         if (!wccflag)
1525                 VTONFS(dvp)->n_attrstamp = 0;
1526         return (error);
1527 }
1528
1529 /*
1530  * nfs file remove call
1531  * To try and make nfs semantics closer to ufs semantics, a file that has
1532  * other processes using the vnode is renamed instead of removed and then
1533  * removed later on the last close.
1534  * - If v_usecount > 1
1535  *        If a rename is not already in the works
1536  *           call nfs_sillyrename() to set it up
1537  *     else
1538  *        do the remove rpc
1539  *
1540  * nfs_remove(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
1541  *            struct vnode *a_vp, struct componentname *a_cnp)
1542  */
1543 static int
1544 nfs_remove(struct vop_remove_args *ap)
1545 {
1546         struct vnode *vp = ap->a_vp;
1547         struct vnode *dvp = ap->a_dvp;
1548         struct componentname *cnp = ap->a_cnp;
1549         struct nfsnode *np = VTONFS(vp);
1550         int error = 0;
1551         struct vattr vattr;
1552
1553 #ifndef DIAGNOSTIC
1554         if ((cnp->cn_flags & CNP_HASBUF) == 0)
1555                 panic("nfs_remove: no name");
1556         if (vp->v_usecount < 1)
1557                 panic("nfs_remove: bad v_usecount");
1558 #endif
1559         if (vp->v_type == VDIR)
1560                 error = EPERM;
1561         else if (vp->v_usecount == 1 || (np->n_sillyrename &&
1562             VOP_GETATTR(vp, &vattr, cnp->cn_td) == 0 &&
1563             vattr.va_nlink > 1)) {
1564                 /*
1565                  * Purge the name cache so that the chance of a lookup for
1566                  * the name succeeding while the remove is in progress is
1567                  * minimized. Without node locking it can still happen, such
1568                  * that an I/O op returns ESTALE, but since you get this if
1569                  * another host removes the file..
1570                  */
1571                 cache_purge(vp);
1572                 /*
1573                  * throw away biocache buffers, mainly to avoid
1574                  * unnecessary delayed writes later.
1575                  */
1576                 error = nfs_vinvalbuf(vp, 0, cnp->cn_td, 1);
1577                 /* Do the rpc */
1578                 if (error != EINTR)
1579                         error = nfs_removerpc(dvp, cnp->cn_nameptr,
1580                                 cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
1581                 /*
1582                  * Kludge City: If the first reply to the remove rpc is lost..
1583                  *   the reply to the retransmitted request will be ENOENT
1584                  *   since the file was in fact removed
1585                  *   Therefore, we cheat and return success.
1586                  */
1587                 if (error == ENOENT)
1588                         error = 0;
1589         } else if (!np->n_sillyrename)
1590                 error = nfs_sillyrename(dvp, vp, cnp);
1591         np->n_attrstamp = 0;
1592         return (error);
1593 }
1594
1595 /*
1596  * nfs file remove rpc called from nfs_inactive
1597  */
1598 int
1599 nfs_removeit(struct sillyrename *sp)
1600 {
1601         return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1602                 sp->s_cred, NULL));
1603 }
1604
1605 /*
1606  * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1607  */
1608 static int
1609 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1610               struct ucred *cred, struct thread *td)
1611 {
1612         u_int32_t *tl;
1613         caddr_t cp;
1614         int32_t t1, t2;
1615         caddr_t bpos, dpos, cp2;
1616         int error = 0, wccflag = NFSV3_WCCRATTR;
1617         struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1618         int v3 = NFS_ISV3(dvp);
1619
1620         nfsstats.rpccnt[NFSPROC_REMOVE]++;
1621         nfsm_reqhead(dvp, NFSPROC_REMOVE,
1622                 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1623         nfsm_fhtom(dvp, v3);
1624         nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1625         nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1626         if (v3)
1627                 nfsm_wcc_data(dvp, wccflag);
1628         m_freem(mrep);
1629 nfsmout:
1630         VTONFS(dvp)->n_flag |= NMODIFIED;
1631         if (!wccflag)
1632                 VTONFS(dvp)->n_attrstamp = 0;
1633         return (error);
1634 }
1635
1636 /*
1637  * nfs file rename call
1638  *
1639  * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1640  *            struct componentname *a_fcnp, struct vnode *a_tdvp,
1641  *            struct vnode *a_tvp, struct componentname *a_tcnp)
1642  */
1643 static int
1644 nfs_rename(struct vop_rename_args *ap)
1645 {
1646         struct vnode *fvp = ap->a_fvp;
1647         struct vnode *tvp = ap->a_tvp;
1648         struct vnode *fdvp = ap->a_fdvp;
1649         struct vnode *tdvp = ap->a_tdvp;
1650         struct componentname *tcnp = ap->a_tcnp;
1651         struct componentname *fcnp = ap->a_fcnp;
1652         int error;
1653
1654 #ifndef DIAGNOSTIC
1655         if ((tcnp->cn_flags & CNP_HASBUF) == 0 ||
1656             (fcnp->cn_flags & CNP_HASBUF) == 0)
1657                 panic("nfs_rename: no name");
1658 #endif
1659         /* Check for cross-device rename */
1660         if ((fvp->v_mount != tdvp->v_mount) ||
1661             (tvp && (fvp->v_mount != tvp->v_mount))) {
1662                 error = EXDEV;
1663                 goto out;
1664         }
1665
1666         /*
1667          * We have to flush B_DELWRI data prior to renaming
1668          * the file.  If we don't, the delayed-write buffers
1669          * can be flushed out later after the file has gone stale
1670          * under NFSV3.  NFSV2 does not have this problem because
1671          * ( as far as I can tell ) it flushes dirty buffers more
1672          * often.
1673          */
1674
1675         VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_td);
1676         if (tvp)
1677             VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_td);
1678
1679         /*
1680          * If the tvp exists and is in use, sillyrename it before doing the
1681          * rename of the new file over it.
1682          * XXX Can't sillyrename a directory.
1683          */
1684         if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1685                 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1686                 vput(tvp);
1687                 tvp = NULL;
1688         }
1689
1690         error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1691                 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1692                 tcnp->cn_td);
1693
1694         if (fvp->v_type == VDIR) {
1695                 if (tvp != NULL && tvp->v_type == VDIR)
1696                         cache_purge(tdvp);
1697                 cache_purge(fdvp);
1698         }
1699
1700 out:
1701         if (tdvp == tvp)
1702                 vrele(tdvp);
1703         else
1704                 vput(tdvp);
1705         if (tvp)
1706                 vput(tvp);
1707         vrele(fdvp);
1708         vrele(fvp);
1709         /*
1710          * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1711          */
1712         if (error == ENOENT)
1713                 error = 0;
1714         return (error);
1715 }
1716
1717 /*
1718  * nfs file rename rpc called from nfs_remove() above
1719  */
1720 static int
1721 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1722              struct sillyrename *sp)
1723 {
1724         return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1725                 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1726 }
1727
1728 /*
1729  * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1730  */
1731 static int
1732 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1733               struct vnode *tdvp, const char *tnameptr, int tnamelen,
1734               struct ucred *cred, struct thread *td)
1735 {
1736         u_int32_t *tl;
1737         caddr_t cp;
1738         int32_t t1, t2;
1739         caddr_t bpos, dpos, cp2;
1740         int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1741         struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1742         int v3 = NFS_ISV3(fdvp);
1743
1744         nfsstats.rpccnt[NFSPROC_RENAME]++;
1745         nfsm_reqhead(fdvp, NFSPROC_RENAME,
1746                 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1747                 nfsm_rndup(tnamelen));
1748         nfsm_fhtom(fdvp, v3);
1749         nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1750         nfsm_fhtom(tdvp, v3);
1751         nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1752         nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1753         if (v3) {
1754                 nfsm_wcc_data(fdvp, fwccflag);
1755                 nfsm_wcc_data(tdvp, twccflag);
1756         }
1757         m_freem(mrep);
1758 nfsmout:
1759         VTONFS(fdvp)->n_flag |= NMODIFIED;
1760         VTONFS(tdvp)->n_flag |= NMODIFIED;
1761         if (!fwccflag)
1762                 VTONFS(fdvp)->n_attrstamp = 0;
1763         if (!twccflag)
1764                 VTONFS(tdvp)->n_attrstamp = 0;
1765         return (error);
1766 }
1767
1768 /*
1769  * nfs hard link create call
1770  *
1771  * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
1772  *          struct componentname *a_cnp)
1773  */
1774 static int
1775 nfs_link(struct vop_link_args *ap)
1776 {
1777         struct vnode *vp = ap->a_vp;
1778         struct vnode *tdvp = ap->a_tdvp;
1779         struct componentname *cnp = ap->a_cnp;
1780         u_int32_t *tl;
1781         caddr_t cp;
1782         int32_t t1, t2;
1783         caddr_t bpos, dpos, cp2;
1784         int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1785         struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1786         int v3;
1787
1788         if (vp->v_mount != tdvp->v_mount) {
1789                 return (EXDEV);
1790         }
1791
1792         /*
1793          * Push all writes to the server, so that the attribute cache
1794          * doesn't get "out of sync" with the server.
1795          * XXX There should be a better way!
1796          */
1797         VOP_FSYNC(vp, MNT_WAIT, cnp->cn_td);
1798
1799         v3 = NFS_ISV3(vp);
1800         nfsstats.rpccnt[NFSPROC_LINK]++;
1801         nfsm_reqhead(vp, NFSPROC_LINK,
1802                 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1803         nfsm_fhtom(vp, v3);
1804         nfsm_fhtom(tdvp, v3);
1805         nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1806         nfsm_request(vp, NFSPROC_LINK, cnp->cn_td, cnp->cn_cred);
1807         if (v3) {
1808                 nfsm_postop_attr(vp, attrflag);
1809                 nfsm_wcc_data(tdvp, wccflag);
1810         }
1811         m_freem(mrep);
1812 nfsmout:
1813         VTONFS(tdvp)->n_flag |= NMODIFIED;
1814         if (!attrflag)
1815                 VTONFS(vp)->n_attrstamp = 0;
1816         if (!wccflag)
1817                 VTONFS(tdvp)->n_attrstamp = 0;
1818         /*
1819          * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1820          */
1821         if (error == EEXIST)
1822                 error = 0;
1823         return (error);
1824 }
1825
1826 /*
1827  * nfs symbolic link create call
1828  *
1829  * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1830  *              struct componentname *a_cnp, struct vattr *a_vap,
1831  *              char *a_target)
1832  */
1833 static int
1834 nfs_symlink(struct vop_symlink_args *ap)
1835 {
1836         struct vnode *dvp = ap->a_dvp;
1837         struct vattr *vap = ap->a_vap;
1838         struct componentname *cnp = ap->a_cnp;
1839         struct nfsv2_sattr *sp;
1840         u_int32_t *tl;
1841         caddr_t cp;
1842         int32_t t1, t2;
1843         caddr_t bpos, dpos, cp2;
1844         int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1845         struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1846         struct vnode *newvp = (struct vnode *)0;
1847         int v3 = NFS_ISV3(dvp);
1848
1849         nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1850         slen = strlen(ap->a_target);
1851         nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1852             nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1853         nfsm_fhtom(dvp, v3);
1854         nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1855         if (v3) {
1856                 nfsm_v3attrbuild(vap, FALSE);
1857         }
1858         nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1859         if (!v3) {
1860                 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1861                 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1862                 sp->sa_uid = nfs_xdrneg1;
1863                 sp->sa_gid = nfs_xdrneg1;
1864                 sp->sa_size = nfs_xdrneg1;
1865                 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1866                 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1867         }
1868
1869         /*
1870          * Issue the NFS request and get the rpc response.
1871          *
1872          * Only NFSv3 responses returning an error of 0 actually return
1873          * a file handle that can be converted into newvp without having
1874          * to do an extra lookup rpc.
1875          */
1876         nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_td, cnp->cn_cred);
1877         if (v3) {
1878                 if (error == 0)
1879                         nfsm_mtofh(dvp, newvp, v3, gotvp);
1880                 nfsm_wcc_data(dvp, wccflag);
1881         }
1882
1883         /*
1884          * out code jumps -> here, mrep is also freed.
1885          */
1886
1887         m_freem(mrep);
1888 nfsmout:
1889
1890         /*
1891          * If we get an EEXIST error, silently convert it to no-error
1892          * in case of an NFS retry.
1893          */
1894         if (error == EEXIST)
1895                 error = 0;
1896
1897         /*
1898          * If we do not have (or no longer have) an error, and we could
1899          * not extract the newvp from the response due to the request being
1900          * NFSv2 or the error being EEXIST.  We have to do a lookup in order
1901          * to obtain a newvp to return.  
1902          */
1903         if (error == 0 && newvp == NULL) {
1904                 struct nfsnode *np = NULL;
1905
1906                 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1907                     cnp->cn_cred, cnp->cn_td, &np);
1908                 if (!error)
1909                         newvp = NFSTOV(np);
1910         }
1911         if (error) {
1912                 if (newvp)
1913                         vput(newvp);
1914         } else {
1915                 *ap->a_vpp = newvp;
1916         }
1917         VTONFS(dvp)->n_flag |= NMODIFIED;
1918         if (!wccflag)
1919                 VTONFS(dvp)->n_attrstamp = 0;
1920         return (error);
1921 }
1922
1923 /*
1924  * nfs make dir call
1925  *
1926  * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1927  *           struct componentname *a_cnp, struct vattr *a_vap)
1928  */
1929 static int
1930 nfs_mkdir(struct vop_mkdir_args *ap)
1931 {
1932         struct vnode *dvp = ap->a_dvp;
1933         struct vattr *vap = ap->a_vap;
1934         struct componentname *cnp = ap->a_cnp;
1935         struct nfsv2_sattr *sp;
1936         u_int32_t *tl;
1937         caddr_t cp;
1938         int32_t t1, t2;
1939         int len;
1940         struct nfsnode *np = (struct nfsnode *)0;
1941         struct vnode *newvp = (struct vnode *)0;
1942         caddr_t bpos, dpos, cp2;
1943         int error = 0, wccflag = NFSV3_WCCRATTR;
1944         int gotvp = 0;
1945         struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1946         struct vattr vattr;
1947         int v3 = NFS_ISV3(dvp);
1948
1949         if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1950                 return (error);
1951         }
1952         len = cnp->cn_namelen;
1953         nfsstats.rpccnt[NFSPROC_MKDIR]++;
1954         nfsm_reqhead(dvp, NFSPROC_MKDIR,
1955           NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
1956         nfsm_fhtom(dvp, v3);
1957         nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1958         if (v3) {
1959                 nfsm_v3attrbuild(vap, FALSE);
1960         } else {
1961                 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1962                 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
1963                 sp->sa_uid = nfs_xdrneg1;
1964                 sp->sa_gid = nfs_xdrneg1;
1965                 sp->sa_size = nfs_xdrneg1;
1966                 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1967                 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1968         }
1969         nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_td, cnp->cn_cred);
1970         if (!error)
1971                 nfsm_mtofh(dvp, newvp, v3, gotvp);
1972         if (v3)
1973                 nfsm_wcc_data(dvp, wccflag);
1974         m_freem(mrep);
1975 nfsmout:
1976         VTONFS(dvp)->n_flag |= NMODIFIED;
1977         if (!wccflag)
1978                 VTONFS(dvp)->n_attrstamp = 0;
1979         /*
1980          * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
1981          * if we can succeed in looking up the directory.
1982          */
1983         if (error == EEXIST || (!error && !gotvp)) {
1984                 if (newvp) {
1985                         vrele(newvp);
1986                         newvp = (struct vnode *)0;
1987                 }
1988                 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
1989                         cnp->cn_td, &np);
1990                 if (!error) {
1991                         newvp = NFSTOV(np);
1992                         if (newvp->v_type != VDIR)
1993                                 error = EEXIST;
1994                 }
1995         }
1996         if (error) {
1997                 if (newvp)
1998                         vrele(newvp);
1999         } else
2000                 *ap->a_vpp = newvp;
2001         return (error);
2002 }
2003
2004 /*
2005  * nfs remove directory call
2006  *
2007  * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2008  *           struct componentname *a_cnp)
2009  */
2010 static int
2011 nfs_rmdir(struct vop_rmdir_args *ap)
2012 {
2013         struct vnode *vp = ap->a_vp;
2014         struct vnode *dvp = ap->a_dvp;
2015         struct componentname *cnp = ap->a_cnp;
2016         u_int32_t *tl;
2017         caddr_t cp;
2018         int32_t t1, t2;
2019         caddr_t bpos, dpos, cp2;
2020         int error = 0, wccflag = NFSV3_WCCRATTR;
2021         struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2022         int v3 = NFS_ISV3(dvp);
2023
2024         if (dvp == vp)
2025                 return (EINVAL);
2026         nfsstats.rpccnt[NFSPROC_RMDIR]++;
2027         nfsm_reqhead(dvp, NFSPROC_RMDIR,
2028                 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2029         nfsm_fhtom(dvp, v3);
2030         nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2031         nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_td, cnp->cn_cred);
2032         if (v3)
2033                 nfsm_wcc_data(dvp, wccflag);
2034         m_freem(mrep);
2035 nfsmout:
2036         VTONFS(dvp)->n_flag |= NMODIFIED;
2037         if (!wccflag)
2038                 VTONFS(dvp)->n_attrstamp = 0;
2039         cache_purge(dvp);
2040         cache_purge(vp);
2041         /*
2042          * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2043          */
2044         if (error == ENOENT)
2045                 error = 0;
2046         return (error);
2047 }
2048
2049 /*
2050  * nfs readdir call
2051  *
2052  * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2053  */
2054 static int
2055 nfs_readdir(struct vop_readdir_args *ap)
2056 {
2057         struct vnode *vp = ap->a_vp;
2058         struct nfsnode *np = VTONFS(vp);
2059         struct uio *uio = ap->a_uio;
2060         int tresid, error;
2061         struct vattr vattr;
2062
2063         if (vp->v_type != VDIR)
2064                 return (EPERM);
2065         /*
2066          * First, check for hit on the EOF offset cache
2067          */
2068         if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2069             (np->n_flag & NMODIFIED) == 0) {
2070                 if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) {
2071                         if (NQNFS_CKCACHABLE(vp, ND_READ)) {
2072                                 nfsstats.direofcache_hits++;
2073                                 return (0);
2074                         }
2075                 } else if (VOP_GETATTR(vp, &vattr, uio->uio_td) == 0 &&
2076                         np->n_mtime == vattr.va_mtime.tv_sec) {
2077                         nfsstats.direofcache_hits++;
2078                         return (0);
2079                 }
2080         }
2081
2082         /*
2083          * Call nfs_bioread() to do the real work.
2084          */
2085         tresid = uio->uio_resid;
2086         error = nfs_bioread(vp, uio, 0);
2087
2088         if (!error && uio->uio_resid == tresid)
2089                 nfsstats.direofcache_misses++;
2090         return (error);
2091 }
2092
2093 /*
2094  * Readdir rpc call.
2095  * Called from below the buffer cache by nfs_doio().
2096  */
2097 int
2098 nfs_readdirrpc(struct vnode *vp, struct uio *uiop)
2099 {
2100         int len, left;
2101         struct dirent *dp = NULL;
2102         u_int32_t *tl;
2103         caddr_t cp;
2104         int32_t t1, t2;
2105         nfsuint64 *cookiep;
2106         caddr_t bpos, dpos, cp2;
2107         struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2108         nfsuint64 cookie;
2109         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2110         struct nfsnode *dnp = VTONFS(vp);
2111         u_quad_t fileno;
2112         int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2113         int attrflag;
2114         int v3 = NFS_ISV3(vp);
2115
2116 #ifndef DIAGNOSTIC
2117         if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2118                 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2119                 panic("nfs readdirrpc bad uio");
2120 #endif
2121
2122         /*
2123          * If there is no cookie, assume directory was stale.
2124          */
2125         cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2126         if (cookiep)
2127                 cookie = *cookiep;
2128         else
2129                 return (NFSERR_BAD_COOKIE);
2130         /*
2131          * Loop around doing readdir rpc's of size nm_readdirsize
2132          * truncated to a multiple of DIRBLKSIZ.
2133          * The stopping criteria is EOF or buffer full.
2134          */
2135         while (more_dirs && bigenough) {
2136                 nfsstats.rpccnt[NFSPROC_READDIR]++;
2137                 nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2138                         NFSX_READDIR(v3));
2139                 nfsm_fhtom(vp, v3);
2140                 if (v3) {
2141                         nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2142                         *tl++ = cookie.nfsuquad[0];
2143                         *tl++ = cookie.nfsuquad[1];
2144                         *tl++ = dnp->n_cookieverf.nfsuquad[0];
2145                         *tl++ = dnp->n_cookieverf.nfsuquad[1];
2146                 } else {
2147                         nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2148                         *tl++ = cookie.nfsuquad[0];
2149                 }
2150                 *tl = txdr_unsigned(nmp->nm_readdirsize);
2151                 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2152                 if (v3) {
2153                         nfsm_postop_attr(vp, attrflag);
2154                         if (!error) {
2155                                 nfsm_dissect(tl, u_int32_t *,
2156                                     2 * NFSX_UNSIGNED);
2157                                 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2158                                 dnp->n_cookieverf.nfsuquad[1] = *tl;
2159                         } else {
2160                                 m_freem(mrep);
2161                                 goto nfsmout;
2162                         }
2163                 }
2164                 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2165                 more_dirs = fxdr_unsigned(int, *tl);
2166         
2167                 /* loop thru the dir entries, doctoring them to 4bsd form */
2168                 while (more_dirs && bigenough) {
2169                         if (v3) {
2170                                 nfsm_dissect(tl, u_int32_t *,
2171                                     3 * NFSX_UNSIGNED);
2172                                 fileno = fxdr_hyper(tl);
2173                                 len = fxdr_unsigned(int, *(tl + 2));
2174                         } else {
2175                                 nfsm_dissect(tl, u_int32_t *,
2176                                     2 * NFSX_UNSIGNED);
2177                                 fileno = fxdr_unsigned(u_quad_t, *tl++);
2178                                 len = fxdr_unsigned(int, *tl);
2179                         }
2180                         if (len <= 0 || len > NFS_MAXNAMLEN) {
2181                                 error = EBADRPC;
2182                                 m_freem(mrep);
2183                                 goto nfsmout;
2184                         }
2185                         tlen = nfsm_rndup(len);
2186                         if (tlen == len)
2187                                 tlen += 4;      /* To ensure null termination */
2188                         left = DIRBLKSIZ - blksiz;
2189                         if ((tlen + DIRHDSIZ) > left) {
2190                                 dp->d_reclen += left;
2191                                 uiop->uio_iov->iov_base += left;
2192                                 uiop->uio_iov->iov_len -= left;
2193                                 uiop->uio_offset += left;
2194                                 uiop->uio_resid -= left;
2195                                 blksiz = 0;
2196                         }
2197                         if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2198                                 bigenough = 0;
2199                         if (bigenough) {
2200                                 dp = (struct dirent *)uiop->uio_iov->iov_base;
2201                                 dp->d_fileno = (int)fileno;
2202                                 dp->d_namlen = len;
2203                                 dp->d_reclen = tlen + DIRHDSIZ;
2204                                 dp->d_type = DT_UNKNOWN;
2205                                 blksiz += dp->d_reclen;
2206                                 if (blksiz == DIRBLKSIZ)
2207                                         blksiz = 0;
2208                                 uiop->uio_offset += DIRHDSIZ;
2209                                 uiop->uio_resid -= DIRHDSIZ;
2210                                 uiop->uio_iov->iov_base += DIRHDSIZ;
2211                                 uiop->uio_iov->iov_len -= DIRHDSIZ;
2212                                 nfsm_mtouio(uiop, len);
2213                                 cp = uiop->uio_iov->iov_base;
2214                                 tlen -= len;
2215                                 *cp = '\0';     /* null terminate */
2216                                 uiop->uio_iov->iov_base += tlen;
2217                                 uiop->uio_iov->iov_len -= tlen;
2218                                 uiop->uio_offset += tlen;
2219                                 uiop->uio_resid -= tlen;
2220                         } else
2221                                 nfsm_adv(nfsm_rndup(len));
2222                         if (v3) {
2223                                 nfsm_dissect(tl, u_int32_t *,
2224                                     3 * NFSX_UNSIGNED);
2225                         } else {
2226                                 nfsm_dissect(tl, u_int32_t *,
2227                                     2 * NFSX_UNSIGNED);
2228                         }
2229                         if (bigenough) {
2230                                 cookie.nfsuquad[0] = *tl++;
2231                                 if (v3)
2232                                         cookie.nfsuquad[1] = *tl++;
2233                         } else if (v3)
2234                                 tl += 2;
2235                         else
2236                                 tl++;
2237                         more_dirs = fxdr_unsigned(int, *tl);
2238                 }
2239                 /*
2240                  * If at end of rpc data, get the eof boolean
2241                  */
2242                 if (!more_dirs) {
2243                         nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2244                         more_dirs = (fxdr_unsigned(int, *tl) == 0);
2245                 }
2246                 m_freem(mrep);
2247         }
2248         /*
2249          * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2250          * by increasing d_reclen for the last record.
2251          */
2252         if (blksiz > 0) {
2253                 left = DIRBLKSIZ - blksiz;
2254                 dp->d_reclen += left;
2255                 uiop->uio_iov->iov_base += left;
2256                 uiop->uio_iov->iov_len -= left;
2257                 uiop->uio_offset += left;
2258                 uiop->uio_resid -= left;
2259         }
2260
2261         /*
2262          * We are now either at the end of the directory or have filled the
2263          * block.
2264          */
2265         if (bigenough)
2266                 dnp->n_direofoffset = uiop->uio_offset;
2267         else {
2268                 if (uiop->uio_resid > 0)
2269                         printf("EEK! readdirrpc resid > 0\n");
2270                 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2271                 *cookiep = cookie;
2272         }
2273 nfsmout:
2274         return (error);
2275 }
2276
2277 /*
2278  * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2279  */
2280 int
2281 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop)
2282 {
2283         int len, left;
2284         struct dirent *dp;
2285         u_int32_t *tl;
2286         caddr_t cp;
2287         int32_t t1, t2;
2288         struct vnode *newvp;
2289         nfsuint64 *cookiep;
2290         caddr_t bpos, dpos, cp2, dpossav1, dpossav2;
2291         struct mbuf *mreq, *mrep, *md, *mb, *mb2, *mdsav1, *mdsav2;
2292         struct nameidata nami, *ndp = &nami;
2293         struct componentname *cnp = &ndp->ni_cnd;
2294         nfsuint64 cookie;
2295         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2296         struct nfsnode *dnp = VTONFS(vp), *np;
2297         nfsfh_t *fhp;
2298         u_quad_t fileno;
2299         int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2300         int attrflag, fhsize;
2301
2302 #ifndef nolint
2303         dp = (struct dirent *)0;
2304 #endif
2305 #ifndef DIAGNOSTIC
2306         if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2307                 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2308                 panic("nfs readdirplusrpc bad uio");
2309 #endif
2310         ndp->ni_dvp = vp;
2311         newvp = NULLVP;
2312
2313         /*
2314          * If there is no cookie, assume directory was stale.
2315          */
2316         cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2317         if (cookiep)
2318                 cookie = *cookiep;
2319         else
2320                 return (NFSERR_BAD_COOKIE);
2321         /*
2322          * Loop around doing readdir rpc's of size nm_readdirsize
2323          * truncated to a multiple of DIRBLKSIZ.
2324          * The stopping criteria is EOF or buffer full.
2325          */
2326         while (more_dirs && bigenough) {
2327                 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2328                 nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2329                         NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2330                 nfsm_fhtom(vp, 1);
2331                 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2332                 *tl++ = cookie.nfsuquad[0];
2333                 *tl++ = cookie.nfsuquad[1];
2334                 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2335                 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2336                 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2337                 *tl = txdr_unsigned(nmp->nm_rsize);
2338                 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2339                 nfsm_postop_attr(vp, attrflag);
2340                 if (error) {
2341                         m_freem(mrep);
2342                         goto nfsmout;
2343                 }
2344                 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2345                 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2346                 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2347                 more_dirs = fxdr_unsigned(int, *tl);
2348
2349                 /* loop thru the dir entries, doctoring them to 4bsd form */
2350                 while (more_dirs && bigenough) {
2351                         nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2352                         fileno = fxdr_hyper(tl);
2353                         len = fxdr_unsigned(int, *(tl + 2));
2354                         if (len <= 0 || len > NFS_MAXNAMLEN) {
2355                                 error = EBADRPC;
2356                                 m_freem(mrep);
2357                                 goto nfsmout;
2358                         }
2359                         tlen = nfsm_rndup(len);
2360                         if (tlen == len)
2361                                 tlen += 4;      /* To ensure null termination*/
2362                         left = DIRBLKSIZ - blksiz;
2363                         if ((tlen + DIRHDSIZ) > left) {
2364                                 dp->d_reclen += left;
2365                                 uiop->uio_iov->iov_base += left;
2366                                 uiop->uio_iov->iov_len -= left;
2367                                 uiop->uio_offset += left;
2368                                 uiop->uio_resid -= left;
2369                                 blksiz = 0;
2370                         }
2371                         if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2372                                 bigenough = 0;
2373                         if (bigenough) {
2374                                 dp = (struct dirent *)uiop->uio_iov->iov_base;
2375                                 dp->d_fileno = (int)fileno;
2376                                 dp->d_namlen = len;
2377                                 dp->d_reclen = tlen + DIRHDSIZ;
2378                                 dp->d_type = DT_UNKNOWN;
2379                                 blksiz += dp->d_reclen;
2380                                 if (blksiz == DIRBLKSIZ)
2381                                         blksiz = 0;
2382                                 uiop->uio_offset += DIRHDSIZ;
2383                                 uiop->uio_resid -= DIRHDSIZ;
2384                                 uiop->uio_iov->iov_base += DIRHDSIZ;
2385                                 uiop->uio_iov->iov_len -= DIRHDSIZ;
2386                                 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2387                                 cnp->cn_namelen = len;
2388                                 nfsm_mtouio(uiop, len);
2389                                 cp = uiop->uio_iov->iov_base;
2390                                 tlen -= len;
2391                                 *cp = '\0';
2392                                 uiop->uio_iov->iov_base += tlen;
2393                                 uiop->uio_iov->iov_len -= tlen;
2394                                 uiop->uio_offset += tlen;
2395                                 uiop->uio_resid -= tlen;
2396                         } else
2397                                 nfsm_adv(nfsm_rndup(len));
2398                         nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2399                         if (bigenough) {
2400                                 cookie.nfsuquad[0] = *tl++;
2401                                 cookie.nfsuquad[1] = *tl++;
2402                         } else
2403                                 tl += 2;
2404
2405                         /*
2406                          * Since the attributes are before the file handle
2407                          * (sigh), we must skip over the attributes and then
2408                          * come back and get them.
2409                          */
2410                         attrflag = fxdr_unsigned(int, *tl);
2411                         if (attrflag) {
2412                             dpossav1 = dpos;
2413                             mdsav1 = md;
2414                             nfsm_adv(NFSX_V3FATTR);
2415                             nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2416                             doit = fxdr_unsigned(int, *tl);
2417                             if (doit) {
2418                                 nfsm_getfh(fhp, fhsize, 1);
2419                                 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2420                                     vref(vp);
2421                                     newvp = vp;
2422                                     np = dnp;
2423                                 } else {
2424                                     error = nfs_nget(vp->v_mount, fhp,
2425                                         fhsize, &np);
2426                                     if (error)
2427                                         doit = 0;
2428                                     else
2429                                         newvp = NFSTOV(np);
2430                                 }
2431                             }
2432                             if (doit && bigenough) {
2433                                 dpossav2 = dpos;
2434                                 dpos = dpossav1;
2435                                 mdsav2 = md;
2436                                 md = mdsav1;
2437                                 nfsm_loadattr(newvp, (struct vattr *)0);
2438                                 dpos = dpossav2;
2439                                 md = mdsav2;
2440                                 dp->d_type =
2441                                     IFTODT(VTTOIF(np->n_vattr.va_type));
2442                                 ndp->ni_vp = newvp;
2443                                 cache_enter(ndp->ni_dvp, NCPNULL, ndp->ni_vp, cnp);
2444                             }
2445                         } else {
2446                             /* Just skip over the file handle */
2447                             nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2448                             i = fxdr_unsigned(int, *tl);
2449                             nfsm_adv(nfsm_rndup(i));
2450                         }
2451                         if (newvp != NULLVP) {
2452                             if (newvp == vp)
2453                                 vrele(newvp);
2454                             else
2455                                 vput(newvp);
2456                             newvp = NULLVP;
2457                         }
2458                         nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2459                         more_dirs = fxdr_unsigned(int, *tl);
2460                 }
2461                 /*
2462                  * If at end of rpc data, get the eof boolean
2463                  */
2464                 if (!more_dirs) {
2465                         nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2466                         more_dirs = (fxdr_unsigned(int, *tl) == 0);
2467                 }
2468                 m_freem(mrep);
2469         }
2470         /*
2471          * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2472          * by increasing d_reclen for the last record.
2473          */
2474         if (blksiz > 0) {
2475                 left = DIRBLKSIZ - blksiz;
2476                 dp->d_reclen += left;
2477                 uiop->uio_iov->iov_base += left;
2478                 uiop->uio_iov->iov_len -= left;
2479                 uiop->uio_offset += left;
2480                 uiop->uio_resid -= left;
2481         }
2482
2483         /*
2484          * We are now either at the end of the directory or have filled the
2485          * block.
2486          */
2487         if (bigenough)
2488                 dnp->n_direofoffset = uiop->uio_offset;
2489         else {
2490                 if (uiop->uio_resid > 0)
2491                         printf("EEK! readdirplusrpc resid > 0\n");
2492                 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2493                 *cookiep = cookie;
2494         }
2495 nfsmout:
2496         if (newvp != NULLVP) {
2497                 if (newvp == vp)
2498                         vrele(newvp);
2499                 else
2500                         vput(newvp);
2501                 newvp = NULLVP;
2502         }
2503         return (error);
2504 }
2505
2506 /*
2507  * Silly rename. To make the NFS filesystem that is stateless look a little
2508  * more like the "ufs" a remove of an active vnode is translated to a rename
2509  * to a funny looking filename that is removed by nfs_inactive on the
2510  * nfsnode. There is the potential for another process on a different client
2511  * to create the same funny name between the nfs_lookitup() fails and the
2512  * nfs_rename() completes, but...
2513  */
2514 static int
2515 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2516 {
2517         struct sillyrename *sp;
2518         struct nfsnode *np;
2519         int error;
2520
2521         cache_purge(dvp);
2522         np = VTONFS(vp);
2523 #ifndef DIAGNOSTIC
2524         if (vp->v_type == VDIR)
2525                 panic("nfs: sillyrename dir");
2526 #endif
2527         MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2528                 M_NFSREQ, M_WAITOK);
2529         sp->s_cred = crdup(cnp->cn_cred);
2530         sp->s_dvp = dvp;
2531         vref(dvp);
2532
2533         /* Fudge together a funny name */
2534         sp->s_namlen = sprintf(sp->s_name, ".nfsA%08x4.4", (int)cnp->cn_td);
2535
2536         /* Try lookitups until we get one that isn't there */
2537         while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2538                 cnp->cn_td, (struct nfsnode **)0) == 0) {
2539                 sp->s_name[4]++;
2540                 if (sp->s_name[4] > 'z') {
2541                         error = EINVAL;
2542                         goto bad;
2543                 }
2544         }
2545         error = nfs_renameit(dvp, cnp, sp);
2546         if (error)
2547                 goto bad;
2548         error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2549                 cnp->cn_td, &np);
2550         np->n_sillyrename = sp;
2551         return (0);
2552 bad:
2553         vrele(sp->s_dvp);
2554         crfree(sp->s_cred);
2555         free((caddr_t)sp, M_NFSREQ);
2556         return (error);
2557 }
2558
2559 /*
2560  * Look up a file name and optionally either update the file handle or
2561  * allocate an nfsnode, depending on the value of npp.
2562  * npp == NULL  --> just do the lookup
2563  * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2564  *                      handled too
2565  * *npp != NULL --> update the file handle in the vnode
2566  */
2567 static int
2568 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2569              struct thread *td, struct nfsnode **npp)
2570 {
2571         u_int32_t *tl;
2572         caddr_t cp;
2573         int32_t t1, t2;
2574         struct vnode *newvp = (struct vnode *)0;
2575         struct nfsnode *np, *dnp = VTONFS(dvp);
2576         caddr_t bpos, dpos, cp2;
2577         int error = 0, fhlen, attrflag;
2578         struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2579         nfsfh_t *nfhp;
2580         int v3 = NFS_ISV3(dvp);
2581
2582         nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2583         nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2584                 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2585         nfsm_fhtom(dvp, v3);
2586         nfsm_strtom(name, len, NFS_MAXNAMLEN);
2587         nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2588         if (npp && !error) {
2589                 nfsm_getfh(nfhp, fhlen, v3);
2590                 if (*npp) {
2591                     np = *npp;
2592                     if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2593                         free((caddr_t)np->n_fhp, M_NFSBIGFH);
2594                         np->n_fhp = &np->n_fh;
2595                     } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2596                         np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
2597                     bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2598                     np->n_fhsize = fhlen;
2599                     newvp = NFSTOV(np);
2600                 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2601                     vref(dvp);
2602                     newvp = dvp;
2603                 } else {
2604                     error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2605                     if (error) {
2606                         m_freem(mrep);
2607                         return (error);
2608                     }
2609                     newvp = NFSTOV(np);
2610                 }
2611                 if (v3) {
2612                         nfsm_postop_attr(newvp, attrflag);
2613                         if (!attrflag && *npp == NULL) {
2614                                 m_freem(mrep);
2615                                 if (newvp == dvp)
2616                                         vrele(newvp);
2617                                 else
2618                                         vput(newvp);
2619                                 return (ENOENT);
2620                         }
2621                 } else
2622                         nfsm_loadattr(newvp, (struct vattr *)0);
2623         }
2624         m_freem(mrep);
2625 nfsmout:
2626         if (npp && *npp == NULL) {
2627                 if (error) {
2628                         if (newvp) {
2629                                 if (newvp == dvp)
2630                                         vrele(newvp);
2631                                 else
2632                                         vput(newvp);
2633                         }
2634                 } else
2635                         *npp = np;
2636         }
2637         return (error);
2638 }
2639
2640 /*
2641  * Nfs Version 3 commit rpc
2642  */
2643 int
2644 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
2645 {
2646         caddr_t cp;
2647         u_int32_t *tl;
2648         int32_t t1, t2;
2649         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2650         caddr_t bpos, dpos, cp2;
2651         int error = 0, wccflag = NFSV3_WCCRATTR;
2652         struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2653         
2654         if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2655                 return (0);
2656         nfsstats.rpccnt[NFSPROC_COMMIT]++;
2657         nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2658         nfsm_fhtom(vp, 1);
2659         nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2660         txdr_hyper(offset, tl);
2661         tl += 2;
2662         *tl = txdr_unsigned(cnt);
2663         nfsm_request(vp, NFSPROC_COMMIT, td, nfs_vpcred(vp, ND_WRITE));
2664         nfsm_wcc_data(vp, wccflag);
2665         if (!error) {
2666                 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
2667                 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2668                         NFSX_V3WRITEVERF)) {
2669                         bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2670                                 NFSX_V3WRITEVERF);
2671                         error = NFSERR_STALEWRITEVERF;
2672                 }
2673         }
2674         m_freem(mrep);
2675 nfsmout:
2676         return (error);
2677 }
2678
2679 /*
2680  * Kludge City..
2681  * - make nfs_bmap() essentially a no-op that does no translation
2682  * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2683  *   (Maybe I could use the process's page mapping, but I was concerned that
2684  *    Kernel Write might not be enabled and also figured copyout() would do
2685  *    a lot more work than bcopy() and also it currently happens in the
2686  *    context of the swapper process (2).
2687  *
2688  * nfs_bmap(struct vnode *a_vp, daddr_t a_bn, struct vnode **a_vpp,
2689  *          daddr_t *a_bnp, int *a_runp, int *a_runb)
2690  */
2691 static int
2692 nfs_bmap(struct vop_bmap_args *ap)
2693 {
2694         struct vnode *vp = ap->a_vp;
2695
2696         if (ap->a_vpp != NULL)
2697                 *ap->a_vpp = vp;
2698         if (ap->a_bnp != NULL)
2699                 *ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize);
2700         if (ap->a_runp != NULL)
2701                 *ap->a_runp = 0;
2702         if (ap->a_runb != NULL)
2703                 *ap->a_runb = 0;
2704         return (0);
2705 }
2706
2707 /*
2708  * Strategy routine.
2709  * For async requests when nfsiod(s) are running, queue the request by
2710  * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2711  * request.
2712  */
2713 static int
2714 nfs_strategy(struct vop_strategy_args *ap)
2715 {
2716         struct buf *bp = ap->a_bp;
2717         struct thread *td;
2718         int error = 0;
2719
2720         KASSERT(!(bp->b_flags & B_DONE), ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2721         KASSERT(BUF_REFCNT(bp) > 0, ("nfs_strategy: buffer %p not locked", bp));
2722
2723         if (bp->b_flags & B_PHYS)
2724                 panic("nfs physio");
2725
2726         if (bp->b_flags & B_ASYNC)
2727                 td = NULL;
2728         else
2729                 td = curthread; /* XXX */
2730
2731         /*
2732          * If the op is asynchronous and an i/o daemon is waiting
2733          * queue the request, wake it up and wait for completion
2734          * otherwise just do it ourselves.
2735          */
2736         if ((bp->b_flags & B_ASYNC) == 0 ||
2737                 nfs_asyncio(bp, td))
2738                 error = nfs_doio(bp, td);
2739         return (error);
2740 }
2741
2742 /*
2743  * Mmap a file
2744  *
2745  * NB Currently unsupported.
2746  *
2747  * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred,
2748  *          struct thread *a_td)
2749  */
2750 /* ARGSUSED */
2751 static int
2752 nfs_mmap(struct vop_mmap_args *ap)
2753 {
2754         return (EINVAL);
2755 }
2756
2757 /*
2758  * fsync vnode op. Just call nfs_flush() with commit == 1.
2759  *
2760  * nfs_fsync(struct vnodeop_desc *a_desc, struct vnode *a_vp,
2761  *           struct ucred * a_cred, int a_waitfor, struct thread *a_td)
2762  */
2763 /* ARGSUSED */
2764 static int
2765 nfs_fsync(struct vop_fsync_args *ap)
2766 {
2767         return (nfs_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1));
2768 }
2769
2770 /*
2771  * Flush all the blocks associated with a vnode.
2772  *      Walk through the buffer pool and push any dirty pages
2773  *      associated with the vnode.
2774  */
2775 static int
2776 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
2777 {
2778         struct nfsnode *np = VTONFS(vp);
2779         struct buf *bp;
2780         int i;
2781         struct buf *nbp;
2782         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2783         int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2784         int passone = 1;
2785         u_quad_t off, endoff, toff;
2786         struct buf **bvec = NULL;
2787 #ifndef NFS_COMMITBVECSIZ
2788 #define NFS_COMMITBVECSIZ       20
2789 #endif
2790         struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2791         int bvecsize = 0, bveccount;
2792
2793         if (nmp->nm_flag & NFSMNT_INT)
2794                 slpflag = PCATCH;
2795         if (!commit)
2796                 passone = 0;
2797         /*
2798          * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2799          * server, but nas not been committed to stable storage on the server
2800          * yet. On the first pass, the byte range is worked out and the commit
2801          * rpc is done. On the second pass, nfs_writebp() is called to do the
2802          * job.
2803          */
2804 again:
2805         off = (u_quad_t)-1;
2806         endoff = 0;
2807         bvecpos = 0;
2808         if (NFS_ISV3(vp) && commit) {
2809                 s = splbio();
2810                 /*
2811                  * Count up how many buffers waiting for a commit.
2812                  */
2813                 bveccount = 0;
2814                 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2815                         nbp = TAILQ_NEXT(bp, b_vnbufs);
2816                         if (BUF_REFCNT(bp) == 0 &&
2817                             (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2818                                 == (B_DELWRI | B_NEEDCOMMIT))
2819                                 bveccount++;
2820                 }
2821                 /*
2822                  * Allocate space to remember the list of bufs to commit.  It is
2823                  * important to use M_NOWAIT here to avoid a race with nfs_write.
2824                  * If we can't get memory (for whatever reason), we will end up
2825                  * committing the buffers one-by-one in the loop below.
2826                  */
2827                 if (bvec != NULL && bvec != bvec_on_stack)
2828                         free(bvec, M_TEMP);
2829                 if (bveccount > NFS_COMMITBVECSIZ) {
2830                         bvec = (struct buf **)
2831                                 malloc(bveccount * sizeof(struct buf *),
2832                                        M_TEMP, M_NOWAIT);
2833                         if (bvec == NULL) {
2834                                 bvec = bvec_on_stack;
2835                                 bvecsize = NFS_COMMITBVECSIZ;
2836                         } else
2837                                 bvecsize = bveccount;
2838                 } else {
2839                         bvec = bvec_on_stack;
2840                         bvecsize = NFS_COMMITBVECSIZ;
2841                 }
2842                 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2843                         nbp = TAILQ_NEXT(bp, b_vnbufs);
2844                         if (bvecpos >= bvecsize)
2845                                 break;
2846                         if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2847                             (B_DELWRI | B_NEEDCOMMIT) ||
2848                             BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
2849                                 continue;
2850                         bremfree(bp);
2851                         /*
2852                          * NOTE: we are not clearing B_DONE here, so we have
2853                          * to do it later on in this routine if we intend to 
2854                          * initiate I/O on the bp.
2855                          *
2856                          * Note: to avoid loopback deadlocks, we do not
2857                          * assign b_runningbufspace.
2858                          */
2859                         bp->b_flags |= B_WRITEINPROG;
2860                         vfs_busy_pages(bp, 1);
2861
2862                         /*
2863                          * bp is protected by being locked, but nbp is not
2864                          * and vfs_busy_pages() may sleep.  We have to
2865                          * recalculate nbp.
2866                          */
2867                         nbp = TAILQ_NEXT(bp, b_vnbufs);
2868
2869                         /*
2870                          * A list of these buffers is kept so that the
2871                          * second loop knows which buffers have actually
2872                          * been committed. This is necessary, since there
2873                          * may be a race between the commit rpc and new
2874                          * uncommitted writes on the file.
2875                          */
2876                         bvec[bvecpos++] = bp;
2877                         toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2878                                 bp->b_dirtyoff;
2879                         if (toff < off)
2880                                 off = toff;
2881                         toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2882                         if (toff > endoff)
2883                                 endoff = toff;
2884                 }
2885                 splx(s);
2886         }
2887         if (bvecpos > 0) {
2888                 /*
2889                  * Commit data on the server, as required.  Note that
2890                  * nfs_commit will use the vnode's cred for the commit.
2891                  */
2892                 retv = nfs_commit(vp, off, (int)(endoff - off), td);
2893
2894                 if (retv == NFSERR_STALEWRITEVERF)
2895                         nfs_clearcommit(vp->v_mount);
2896
2897                 /*
2898                  * Now, either mark the blocks I/O done or mark the
2899                  * blocks dirty, depending on whether the commit
2900                  * succeeded.
2901                  */
2902                 for (i = 0; i < bvecpos; i++) {
2903                         bp = bvec[i];
2904                         bp->b_flags &= ~(B_NEEDCOMMIT | B_WRITEINPROG | B_CLUSTEROK);
2905                         if (retv) {
2906                                 /*
2907                                  * Error, leave B_DELWRI intact
2908                                  */
2909                                 vfs_unbusy_pages(bp);
2910                                 brelse(bp);
2911                         } else {
2912                                 /*
2913                                  * Success, remove B_DELWRI ( bundirty() ).
2914                                  *
2915                                  * b_dirtyoff/b_dirtyend seem to be NFS 
2916                                  * specific.  We should probably move that
2917                                  * into bundirty(). XXX
2918                                  */
2919                                 s = splbio();
2920                                 vp->v_numoutput++;
2921                                 bp->b_flags |= B_ASYNC;
2922                                 bundirty(bp);
2923                                 bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
2924                                 bp->b_dirtyoff = bp->b_dirtyend = 0;
2925                                 splx(s);
2926                                 biodone(bp);
2927                         }
2928                 }
2929         }
2930
2931         /*
2932          * Start/do any write(s) that are required.
2933          */
2934 loop:
2935         s = splbio();
2936         for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2937                 nbp = TAILQ_NEXT(bp, b_vnbufs);
2938                 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
2939                         if (waitfor != MNT_WAIT || passone)
2940                                 continue;
2941                         error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL,
2942                             "nfsfsync", slpflag, slptimeo);
2943                         splx(s);
2944                         if (error == 0)
2945                                 panic("nfs_fsync: inconsistent lock");
2946                         if (error == ENOLCK)
2947                                 goto loop;
2948                         if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
2949                                 error = EINTR;
2950                                 goto done;
2951                         }
2952                         if (slpflag == PCATCH) {
2953                                 slpflag = 0;
2954                                 slptimeo = 2 * hz;
2955                         }
2956                         goto loop;
2957                 }
2958                 if ((bp->b_flags & B_DELWRI) == 0)
2959                         panic("nfs_fsync: not dirty");
2960                 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
2961                         BUF_UNLOCK(bp);
2962                         continue;
2963                 }
2964                 bremfree(bp);
2965                 if (passone || !commit)
2966                     bp->b_flags |= B_ASYNC;
2967                 else
2968                     bp->b_flags |= B_ASYNC | B_WRITEINPROG;
2969                 splx(s);
2970                 VOP_BWRITE(bp->b_vp, bp);
2971                 goto loop;
2972         }
2973         splx(s);
2974         if (passone) {
2975                 passone = 0;
2976                 goto again;
2977         }
2978         if (waitfor == MNT_WAIT) {
2979                 while (vp->v_numoutput) {
2980                         vp->v_flag |= VBWAIT;
2981                         error = tsleep((caddr_t)&vp->v_numoutput,
2982                                 slpflag, "nfsfsync", slptimeo);
2983                         if (error) {
2984                             if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
2985                                 error = EINTR;
2986                                 goto done;
2987                             }
2988                             if (slpflag == PCATCH) {
2989                                 slpflag = 0;
2990                                 slptimeo = 2 * hz;
2991                             }
2992                         }
2993                 }
2994                 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) && commit) {
2995                         goto loop;
2996                 }
2997         }
2998         if (np->n_flag & NWRITEERR) {
2999                 error = np->n_error;
3000                 np->n_flag &= ~NWRITEERR;
3001         }
3002 done:
3003         if (bvec != NULL && bvec != bvec_on_stack)
3004                 free(bvec, M_TEMP);
3005         return (error);
3006 }
3007
3008 /*
3009  * NFS advisory byte-level locks.
3010  * Currently unsupported.
3011  *
3012  * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3013  *              int a_flags)
3014  */
3015 static int
3016 nfs_advlock(struct vop_advlock_args *ap)
3017 {
3018         struct nfsnode *np = VTONFS(ap->a_vp);
3019
3020         /*
3021          * The following kludge is to allow diskless support to work
3022          * until a real NFS lockd is implemented. Basically, just pretend
3023          * that this is a local lock.
3024          */
3025         return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3026 }
3027
3028 /*
3029  * Print out the contents of an nfsnode.
3030  *
3031  * nfs_print(struct vnode *a_vp)
3032  */
3033 static int
3034 nfs_print(struct vop_print_args *ap)
3035 {
3036         struct vnode *vp = ap->a_vp;
3037         struct nfsnode *np = VTONFS(vp);
3038
3039         printf("tag VT_NFS, fileid %ld fsid 0x%x",
3040                 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3041         if (vp->v_type == VFIFO)
3042                 fifo_printinfo(vp);
3043         printf("\n");
3044         return (0);
3045 }
3046
3047 /*
3048  * Just call nfs_writebp() with the force argument set to 1.
3049  *
3050  * NOTE: B_DONE may or may not be set in a_bp on call.
3051  *
3052  * nfs_bwrite(struct vnode *a_bp)
3053  */
3054 static int
3055 nfs_bwrite(struct vop_bwrite_args *ap)
3056 {
3057         return (nfs_writebp(ap->a_bp, 1, curthread));
3058 }
3059
3060 /*
3061  * This is a clone of vn_bwrite(), except that B_WRITEINPROG isn't set unless
3062  * the force flag is one and it also handles the B_NEEDCOMMIT flag.  We set
3063  * B_CACHE if this is a VMIO buffer.
3064  */
3065 int
3066 nfs_writebp(struct buf *bp, int force, struct thread *td)
3067 {
3068         int s;
3069         int oldflags = bp->b_flags;
3070 #if 0
3071         int retv = 1;
3072         off_t off;
3073 #endif
3074
3075         if (BUF_REFCNT(bp) == 0)
3076                 panic("bwrite: buffer is not locked???");
3077
3078         if (bp->b_flags & B_INVAL) {
3079                 brelse(bp);
3080                 return(0);
3081         }
3082
3083         bp->b_flags |= B_CACHE;
3084
3085         /*
3086          * Undirty the bp.  We will redirty it later if the I/O fails.
3087          */
3088
3089         s = splbio();
3090         bundirty(bp);
3091         bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
3092
3093         bp->b_vp->v_numoutput++;
3094         splx(s);
3095
3096         /*
3097          * Note: to avoid loopback deadlocks, we do not
3098          * assign b_runningbufspace.
3099          */
3100         vfs_busy_pages(bp, 1);
3101
3102         if (force)
3103                 bp->b_flags |= B_WRITEINPROG;
3104         BUF_KERNPROC(bp);
3105         VOP_STRATEGY(bp->b_vp, bp);
3106
3107         if( (oldflags & B_ASYNC) == 0) {
3108                 int rtval = biowait(bp);
3109
3110                 if (oldflags & B_DELWRI) {
3111                         s = splbio();
3112                         reassignbuf(bp, bp->b_vp);
3113                         splx(s);
3114                 }
3115
3116                 brelse(bp);
3117                 return (rtval);
3118         } 
3119
3120         return (0);
3121 }
3122
3123 /*
3124  * nfs special file access vnode op.
3125  * Essentially just get vattr and then imitate iaccess() since the device is
3126  * local to the client.
3127  *
3128  * nfsspec_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
3129  *                struct thread *a_td)
3130  */
3131 static int
3132 nfsspec_access(struct vop_access_args *ap)
3133 {
3134         struct vattr *vap;
3135         gid_t *gp;
3136         struct ucred *cred = ap->a_cred;
3137         struct vnode *vp = ap->a_vp;
3138         mode_t mode = ap->a_mode;
3139         struct vattr vattr;
3140         int i;
3141         int error;
3142
3143         /*
3144          * Disallow write attempts on filesystems mounted read-only;
3145          * unless the file is a socket, fifo, or a block or character
3146          * device resident on the filesystem.
3147          */
3148         if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3149                 switch (vp->v_type) {
3150                 case VREG:
3151                 case VDIR:
3152                 case VLNK:
3153                         return (EROFS);
3154                 default:
3155                         break;
3156                 }
3157         }
3158         /*
3159          * If you're the super-user,
3160          * you always get access.
3161          */
3162         if (cred->cr_uid == 0)
3163                 return (0);
3164         vap = &vattr;
3165         error = VOP_GETATTR(vp, vap, ap->a_td);
3166         if (error)
3167                 return (error);
3168         /*
3169          * Access check is based on only one of owner, group, public.
3170          * If not owner, then check group. If not a member of the
3171          * group, then check public access.
3172          */
3173         if (cred->cr_uid != vap->va_uid) {
3174                 mode >>= 3;
3175                 gp = cred->cr_groups;
3176                 for (i = 0; i < cred->cr_ngroups; i++, gp++)
3177                         if (vap->va_gid == *gp)
3178                                 goto found;
3179                 mode >>= 3;
3180 found:
3181                 ;
3182         }
3183         error = (vap->va_mode & mode) == mode ? 0 : EACCES;
3184         return (error);
3185 }
3186
3187 /*
3188  * Read wrapper for special devices.
3189  *
3190  * nfsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3191  *              struct ucred *a_cred)
3192  */
3193 static int
3194 nfsspec_read(struct vop_read_args *ap)
3195 {
3196         struct nfsnode *np = VTONFS(ap->a_vp);
3197
3198         /*
3199          * Set access flag.
3200          */
3201         np->n_flag |= NACC;
3202         getnanotime(&np->n_atim);
3203         return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap));
3204 }
3205
3206 /*
3207  * Write wrapper for special devices.
3208  *
3209  * nfsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3210  *               struct ucred *a_cred)
3211  */
3212 static int
3213 nfsspec_write(struct vop_write_args *ap)
3214 {
3215         struct nfsnode *np = VTONFS(ap->a_vp);
3216
3217         /*
3218          * Set update flag.
3219          */
3220         np->n_flag |= NUPD;
3221         getnanotime(&np->n_mtim);
3222         return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap));
3223 }
3224
3225 /*
3226  * Close wrapper for special devices.
3227  *
3228  * Update the times on the nfsnode then do device close.
3229  *
3230  * nfsspec_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
3231  *               struct thread *a_td)
3232  */
3233 static int
3234 nfsspec_close(struct vop_close_args *ap)
3235 {
3236         struct vnode *vp = ap->a_vp;
3237         struct nfsnode *np = VTONFS(vp);
3238         struct vattr vattr;
3239
3240         if (np->n_flag & (NACC | NUPD)) {
3241                 np->n_flag |= NCHG;
3242                 if (vp->v_usecount == 1 &&
3243                     (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3244                         VATTR_NULL(&vattr);
3245                         if (np->n_flag & NACC)
3246                                 vattr.va_atime = np->n_atim;
3247                         if (np->n_flag & NUPD)
3248                                 vattr.va_mtime = np->n_mtim;
3249                         (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
3250                 }
3251         }
3252         return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
3253 }
3254
3255 /*
3256  * Read wrapper for fifos.
3257  *
3258  * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3259  *              struct ucred *a_cred)
3260  */
3261 static int
3262 nfsfifo_read(struct vop_read_args *ap)
3263 {
3264         struct nfsnode *np = VTONFS(ap->a_vp);
3265
3266         /*
3267          * Set access flag.
3268          */
3269         np->n_flag |= NACC;
3270         getnanotime(&np->n_atim);
3271         return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap));
3272 }
3273
3274 /*
3275  * Write wrapper for fifos.
3276  *
3277  * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3278  *               struct ucred *a_cred)
3279  */
3280 static int
3281 nfsfifo_write(struct vop_write_args *ap)
3282 {
3283         struct nfsnode *np = VTONFS(ap->a_vp);
3284
3285         /*
3286          * Set update flag.
3287          */
3288         np->n_flag |= NUPD;
3289         getnanotime(&np->n_mtim);
3290         return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap));
3291 }
3292
3293 /*
3294  * Close wrapper for fifos.
3295  *
3296  * Update the times on the nfsnode then do fifo close.
3297  *
3298  * nfsfifo_close(struct vnode *a_vp, int a_fflag, struct thread *a_td)
3299  */
3300 static int
3301 nfsfifo_close(struct vop_close_args *ap)
3302 {
3303         struct vnode *vp = ap->a_vp;
3304         struct nfsnode *np = VTONFS(vp);
3305         struct vattr vattr;
3306         struct timespec ts;
3307
3308         if (np->n_flag & (NACC | NUPD)) {
3309                 getnanotime(&ts);
3310                 if (np->n_flag & NACC)
3311                         np->n_atim = ts;
3312                 if (np->n_flag & NUPD)
3313                         np->n_mtim = ts;
3314                 np->n_flag |= NCHG;
3315                 if (vp->v_usecount == 1 &&
3316                     (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3317                         VATTR_NULL(&vattr);
3318                         if (np->n_flag & NACC)
3319                                 vattr.va_atime = np->n_atim;
3320                         if (np->n_flag & NUPD)
3321                                 vattr.va_mtime = np->n_mtim;
3322                         (void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
3323                 }
3324         }
3325         return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));
3326 }
3327