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