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