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