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