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