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