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