]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/nfsclient/nfs_vnops.c
This commit was generated by cvs2svn to compensate for changes in r99679,
[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                 vhold(*vpp);
780                 newvp = *vpp;
781                 vpid = newvp->v_id;
782                 /*
783                  * See the comment starting `Step through' in ufs/ufs_lookup.c
784                  * for an explanation of the locking protocol
785                  */
786                 if (dvp == newvp) {
787                         VREF(newvp);
788                         error = 0;
789                 } else if (flags & ISDOTDOT) {
790                         VOP_UNLOCK(dvp, 0, td);
791                         cnp->cn_flags |= PDIRUNLOCK;
792                         error = vget(newvp, LK_EXCLUSIVE, td);
793                         if (!error && lockparent && (flags & ISLASTCN)) {
794                                 error = vn_lock(dvp, LK_EXCLUSIVE, td);
795                                 if (error == 0)
796                                         cnp->cn_flags &= ~PDIRUNLOCK;
797                         }
798                 } else {
799                         error = vget(newvp, LK_EXCLUSIVE, td);
800                         if (!lockparent || error || !(flags & ISLASTCN)) {
801                                 VOP_UNLOCK(dvp, 0, td);
802                                 cnp->cn_flags |= PDIRUNLOCK;
803                         }
804                 }
805                 if (!error) {
806                         if (vpid == newvp->v_id) {
807                            if (!VOP_GETATTR(newvp, &vattr, cnp->cn_cred, td)
808                             && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
809                                 nfsstats.lookupcache_hits++;
810                                 if (cnp->cn_nameiop != LOOKUP &&
811                                     (flags & ISLASTCN))
812                                         cnp->cn_flags |= SAVENAME;
813                                 vdrop(newvp);
814                                 return (0);
815                            }
816                            cache_purge(newvp);
817                         }
818                         vput(newvp);
819                         if (lockparent && dvp != newvp && (flags & ISLASTCN))
820                                 VOP_UNLOCK(dvp, 0, td);
821                 }
822                 vdrop(newvp);
823                 error = vn_lock(dvp, LK_EXCLUSIVE, td);
824                 *vpp = NULLVP;
825                 if (error) {
826                         cnp->cn_flags |= PDIRUNLOCK;
827                         return (error);
828                 }
829                 cnp->cn_flags &= ~PDIRUNLOCK;
830         }
831         error = 0;
832         newvp = NULLVP;
833         nfsstats.lookupcache_misses++;
834         nfsstats.rpccnt[NFSPROC_LOOKUP]++;
835         len = cnp->cn_namelen;
836         mreq = nfsm_reqhead(dvp, NFSPROC_LOOKUP,
837                 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
838         mb = mreq;
839         bpos = mtod(mb, caddr_t);
840         nfsm_fhtom(dvp, v3);
841         nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
842         nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_thread, cnp->cn_cred);
843         if (error) {
844                 nfsm_postop_attr(dvp, attrflag);
845                 m_freem(mrep);
846                 goto nfsmout;
847         }
848         nfsm_getfh(fhp, fhsize, v3);
849
850         /*
851          * Handle RENAME case...
852          */
853         if (cnp->cn_nameiop == RENAME && wantparent && (flags & ISLASTCN)) {
854                 if (NFS_CMPFH(np, fhp, fhsize)) {
855                         m_freem(mrep);
856                         return (EISDIR);
857                 }
858                 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
859                 if (error) {
860                         m_freem(mrep);
861                         return (error);
862                 }
863                 newvp = NFSTOV(np);
864                 if (v3) {
865                         nfsm_postop_attr(newvp, attrflag);
866                         nfsm_postop_attr(dvp, attrflag);
867                 } else
868                         nfsm_loadattr(newvp, (struct vattr *)0);
869                 *vpp = newvp;
870                 m_freem(mrep);
871                 cnp->cn_flags |= SAVENAME;
872                 if (!lockparent) {
873                         VOP_UNLOCK(dvp, 0, td);
874                         cnp->cn_flags |= PDIRUNLOCK;
875                 }
876                 return (0);
877         }
878
879         if (flags & ISDOTDOT) {
880                 VOP_UNLOCK(dvp, 0, td);
881                 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
882                 if (error) {
883                         vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
884                         return (error);
885                 }
886                 newvp = NFSTOV(np);
887                 if (lockparent && (flags & ISLASTCN)) {
888                         error = vn_lock(dvp, LK_EXCLUSIVE, td);
889                         if (error) {
890                                 cnp->cn_flags |= PDIRUNLOCK;
891                                 vput(newvp);
892                                 return (error);
893                         }
894                 } else
895                         cnp->cn_flags |= PDIRUNLOCK;
896         } else if (NFS_CMPFH(np, fhp, fhsize)) {
897                 VREF(dvp);
898                 newvp = dvp;
899         } else {
900                 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
901                 if (error) {
902                         m_freem(mrep);
903                         return (error);
904                 }
905                 if (!lockparent || !(flags & ISLASTCN)) {
906                         cnp->cn_flags |= PDIRUNLOCK;
907                         VOP_UNLOCK(dvp, 0, td);
908                 }
909                 newvp = NFSTOV(np);
910         }
911         if (v3) {
912                 nfsm_postop_attr(newvp, attrflag);
913                 nfsm_postop_attr(dvp, attrflag);
914         } else
915                 nfsm_loadattr(newvp, (struct vattr *)0);
916         if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
917                 cnp->cn_flags |= SAVENAME;
918         if ((cnp->cn_flags & MAKEENTRY) &&
919             (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
920                 np->n_ctime = np->n_vattr.va_ctime.tv_sec;
921                 cache_enter(dvp, newvp, cnp);
922         }
923         *vpp = newvp;
924         m_freem(mrep);
925 nfsmout:
926         if (error) {
927                 if (newvp != NULLVP) {
928                         vrele(newvp);
929                         *vpp = NULLVP;
930                 }
931                 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
932                     (flags & ISLASTCN) && error == ENOENT) {
933                         if (!lockparent) {
934                                 VOP_UNLOCK(dvp, 0, td);
935                                 cnp->cn_flags |= PDIRUNLOCK;
936                         }
937                         if (dvp->v_mount->mnt_flag & MNT_RDONLY)
938                                 error = EROFS;
939                         else
940                                 error = EJUSTRETURN;
941                 }
942                 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
943                         cnp->cn_flags |= SAVENAME;
944         }
945         return (error);
946 }
947
948 /*
949  * nfs read call.
950  * Just call nfs_bioread() to do the work.
951  */
952 static int
953 nfs_read(struct vop_read_args *ap)
954 {
955         struct vnode *vp = ap->a_vp;
956
957         if (vp->v_type != VREG)
958                 return (EPERM);
959         return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
960 }
961
962 /*
963  * nfs readlink call
964  */
965 static int
966 nfs_readlink(struct vop_readlink_args *ap)
967 {
968         struct vnode *vp = ap->a_vp;
969
970         if (vp->v_type != VLNK)
971                 return (EINVAL);
972         return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred));
973 }
974
975 /*
976  * Do a readlink rpc.
977  * Called by nfs_doio() from below the buffer cache.
978  */
979 int
980 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
981 {
982         caddr_t bpos, dpos;
983         int error = 0, len, attrflag;
984         struct mbuf *mreq, *mrep, *md, *mb;
985         int v3 = NFS_ISV3(vp);
986
987         nfsstats.rpccnt[NFSPROC_READLINK]++;
988         mreq = nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
989         mb = mreq;
990         bpos = mtod(mb, caddr_t);
991         nfsm_fhtom(vp, v3);
992         nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, cred);
993         if (v3)
994                 nfsm_postop_attr(vp, attrflag);
995         if (!error) {
996                 nfsm_strsiz(len, NFS_MAXPATHLEN);
997                 if (len == NFS_MAXPATHLEN) {
998                         struct nfsnode *np = VTONFS(vp);
999                         if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1000                                 len = np->n_size;
1001                 }
1002                 nfsm_mtouio(uiop, len);
1003         }
1004         m_freem(mrep);
1005 nfsmout:
1006         return (error);
1007 }
1008
1009 /*
1010  * nfs read rpc call
1011  * Ditto above
1012  */
1013 int
1014 nfs_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1015 {
1016         u_int32_t *tl;
1017         caddr_t bpos, dpos;
1018         struct mbuf *mreq, *mrep, *md, *mb;
1019         struct nfsmount *nmp;
1020         int error = 0, len, retlen, tsiz, eof, attrflag;
1021         int v3 = NFS_ISV3(vp);
1022
1023 #ifndef nolint
1024         eof = 0;
1025 #endif
1026         nmp = VFSTONFS(vp->v_mount);
1027         tsiz = uiop->uio_resid;
1028         if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1029                 return (EFBIG);
1030         while (tsiz > 0) {
1031                 nfsstats.rpccnt[NFSPROC_READ]++;
1032                 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1033                 mreq = nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1034                 mb = mreq;
1035                 bpos = mtod(mb, caddr_t);
1036                 nfsm_fhtom(vp, v3);
1037                 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED * 3);
1038                 if (v3) {
1039                         txdr_hyper(uiop->uio_offset, tl);
1040                         *(tl + 2) = txdr_unsigned(len);
1041                 } else {
1042                         *tl++ = txdr_unsigned(uiop->uio_offset);
1043                         *tl++ = txdr_unsigned(len);
1044                         *tl = 0;
1045                 }
1046                 nfsm_request(vp, NFSPROC_READ, uiop->uio_td, cred);
1047                 if (v3) {
1048                         nfsm_postop_attr(vp, attrflag);
1049                         if (error) {
1050                                 m_freem(mrep);
1051                                 goto nfsmout;
1052                         }
1053                         tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
1054                         eof = fxdr_unsigned(int, *(tl + 1));
1055                 } else
1056                         nfsm_loadattr(vp, (struct vattr *)0);
1057                 nfsm_strsiz(retlen, nmp->nm_rsize);
1058                 nfsm_mtouio(uiop, retlen);
1059                 m_freem(mrep);
1060                 tsiz -= retlen;
1061                 if (v3) {
1062                         if (eof || retlen == 0) {
1063                                 tsiz = 0;
1064                         }
1065                 } else if (retlen < len) {
1066                         tsiz = 0;
1067                 }
1068         }
1069 nfsmout:
1070         return (error);
1071 }
1072
1073 /*
1074  * nfs write call
1075  */
1076 int
1077 nfs_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1078     int *iomode, int *must_commit)
1079 {
1080         u_int32_t *tl;
1081         int32_t backup;
1082         caddr_t bpos, dpos;
1083         struct mbuf *mreq, *mrep, *md, *mb;
1084         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1085         int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1086         int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1087
1088 #ifndef DIAGNOSTIC
1089         if (uiop->uio_iovcnt != 1)
1090                 panic("nfs: writerpc iovcnt > 1");
1091 #endif
1092         *must_commit = 0;
1093         tsiz = uiop->uio_resid;
1094         if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1095                 return (EFBIG);
1096         while (tsiz > 0) {
1097                 nfsstats.rpccnt[NFSPROC_WRITE]++;
1098                 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1099                 mreq = nfsm_reqhead(vp, NFSPROC_WRITE,
1100                         NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1101                 mb = mreq;
1102                 bpos = mtod(mb, caddr_t);
1103                 nfsm_fhtom(vp, v3);
1104                 if (v3) {
1105                         tl = nfsm_build(u_int32_t *, 5 * NFSX_UNSIGNED);
1106                         txdr_hyper(uiop->uio_offset, tl);
1107                         tl += 2;
1108                         *tl++ = txdr_unsigned(len);
1109                         *tl++ = txdr_unsigned(*iomode);
1110                         *tl = txdr_unsigned(len);
1111                 } else {
1112                         u_int32_t x;
1113
1114                         tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
1115                         /* Set both "begin" and "current" to non-garbage. */
1116                         x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1117                         *tl++ = x;      /* "begin offset" */
1118                         *tl++ = x;      /* "current offset" */
1119                         x = txdr_unsigned(len);
1120                         *tl++ = x;      /* total to this offset */
1121                         *tl = x;        /* size of this write */
1122                 }
1123                 nfsm_uiotom(uiop, len);
1124                 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, cred);
1125                 if (v3) {
1126                         wccflag = NFSV3_WCCCHK;
1127                         nfsm_wcc_data(vp, wccflag);
1128                         if (!error) {
1129                                 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED
1130                                         + NFSX_V3WRITEVERF);
1131                                 rlen = fxdr_unsigned(int, *tl++);
1132                                 if (rlen == 0) {
1133                                         error = NFSERR_IO;
1134                                         m_freem(mrep);
1135                                         break;
1136                                 } else if (rlen < len) {
1137                                         backup = len - rlen;
1138                                         uiop->uio_iov->iov_base -= backup;
1139                                         uiop->uio_iov->iov_len += backup;
1140                                         uiop->uio_offset -= backup;
1141                                         uiop->uio_resid += backup;
1142                                         len = rlen;
1143                                 }
1144                                 commit = fxdr_unsigned(int, *tl++);
1145
1146                                 /*
1147                                  * Return the lowest committment level
1148                                  * obtained by any of the RPCs.
1149                                  */
1150                                 if (committed == NFSV3WRITE_FILESYNC)
1151                                         committed = commit;
1152                                 else if (committed == NFSV3WRITE_DATASYNC &&
1153                                         commit == NFSV3WRITE_UNSTABLE)
1154                                         committed = commit;
1155                                 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1156                                     bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1157                                         NFSX_V3WRITEVERF);
1158                                     nmp->nm_state |= NFSSTA_HASWRITEVERF;
1159                                 } else if (bcmp((caddr_t)tl,
1160                                     (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1161                                     *must_commit = 1;
1162                                     bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1163                                         NFSX_V3WRITEVERF);
1164                                 }
1165                         }
1166                 } else
1167                     nfsm_loadattr(vp, (struct vattr *)0);
1168                 if (wccflag)
1169                     VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime.tv_sec;
1170                 m_freem(mrep);
1171                 if (error)
1172                         break;
1173                 tsiz -= len;
1174         }
1175 nfsmout:
1176         if (vp->v_mount->mnt_flag & MNT_ASYNC)
1177                 committed = NFSV3WRITE_FILESYNC;
1178         *iomode = committed;
1179         if (error)
1180                 uiop->uio_resid = tsiz;
1181         return (error);
1182 }
1183
1184 /*
1185  * nfs mknod rpc
1186  * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1187  * mode set to specify the file type and the size field for rdev.
1188  */
1189 static int
1190 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1191     struct vattr *vap)
1192 {
1193         struct nfsv2_sattr *sp;
1194         u_int32_t *tl;
1195         struct vnode *newvp = (struct vnode *)0;
1196         struct nfsnode *np = (struct nfsnode *)0;
1197         struct vattr vattr;
1198         caddr_t bpos, dpos;
1199         int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1200         struct mbuf *mreq, *mrep, *md, *mb;
1201         u_int32_t rdev;
1202         int v3 = NFS_ISV3(dvp);
1203
1204         if (vap->va_type == VCHR || vap->va_type == VBLK)
1205                 rdev = txdr_unsigned(vap->va_rdev);
1206         else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1207                 rdev = nfs_xdrneg1;
1208         else {
1209                 return (EOPNOTSUPP);
1210         }
1211         if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
1212                 return (error);
1213         }
1214         nfsstats.rpccnt[NFSPROC_MKNOD]++;
1215         mreq = nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1216                 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1217         mb = mreq;
1218         bpos = mtod(mb, caddr_t);
1219         nfsm_fhtom(dvp, v3);
1220         nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1221         if (v3) {
1222                 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
1223                 *tl++ = vtonfsv3_type(vap->va_type);
1224                 nfsm_v3attrbuild(vap, FALSE);
1225                 if (vap->va_type == VCHR || vap->va_type == VBLK) {
1226                         tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
1227                         *tl++ = txdr_unsigned(umajor(vap->va_rdev));
1228                         *tl = txdr_unsigned(uminor(vap->va_rdev));
1229                 }
1230         } else {
1231                 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1232                 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1233                 sp->sa_uid = nfs_xdrneg1;
1234                 sp->sa_gid = nfs_xdrneg1;
1235                 sp->sa_size = rdev;
1236                 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1237                 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1238         }
1239         nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_thread, cnp->cn_cred);
1240         if (!error) {
1241                 nfsm_mtofh(dvp, newvp, v3, gotvp);
1242                 if (!gotvp) {
1243                         if (newvp) {
1244                                 vput(newvp);
1245                                 newvp = (struct vnode *)0;
1246                         }
1247                         error = nfs_lookitup(dvp, cnp->cn_nameptr,
1248                             cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread, &np);
1249                         if (!error)
1250                                 newvp = NFSTOV(np);
1251                 }
1252         }
1253         if (v3)
1254                 nfsm_wcc_data(dvp, wccflag);
1255         m_freem(mrep);
1256 nfsmout:
1257         if (error) {
1258                 if (newvp)
1259                         vput(newvp);
1260         } else {
1261                 if (cnp->cn_flags & MAKEENTRY)
1262                         cache_enter(dvp, newvp, cnp);
1263                 *vpp = newvp;
1264         }
1265         VTONFS(dvp)->n_flag |= NMODIFIED;
1266         if (!wccflag)
1267                 VTONFS(dvp)->n_attrstamp = 0;
1268         return (error);
1269 }
1270
1271 /*
1272  * nfs mknod vop
1273  * just call nfs_mknodrpc() to do the work.
1274  */
1275 /* ARGSUSED */
1276 static int
1277 nfs_mknod(struct vop_mknod_args *ap)
1278 {
1279
1280         return nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1281 }
1282
1283 static u_long create_verf;
1284 /*
1285  * nfs file create call
1286  */
1287 static int
1288 nfs_create(struct vop_create_args *ap)
1289 {
1290         struct vnode *dvp = ap->a_dvp;
1291         struct vattr *vap = ap->a_vap;
1292         struct componentname *cnp = ap->a_cnp;
1293         struct nfsv2_sattr *sp;
1294         u_int32_t *tl;
1295         struct nfsnode *np = (struct nfsnode *)0;
1296         struct vnode *newvp = (struct vnode *)0;
1297         caddr_t bpos, dpos;
1298         int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1299         struct mbuf *mreq, *mrep, *md, *mb;
1300         struct vattr vattr;
1301         int v3 = NFS_ISV3(dvp);
1302
1303         /*
1304          * Oops, not for me..
1305          */
1306         if (vap->va_type == VSOCK)
1307                 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1308
1309         if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
1310                 return (error);
1311         }
1312         if (vap->va_vaflags & VA_EXCLUSIVE)
1313                 fmode |= O_EXCL;
1314 again:
1315         nfsstats.rpccnt[NFSPROC_CREATE]++;
1316         mreq = nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1317                 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1318         mb = mreq;
1319         bpos = mtod(mb, caddr_t);
1320         nfsm_fhtom(dvp, v3);
1321         nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1322         if (v3) {
1323                 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
1324                 if (fmode & O_EXCL) {
1325                         *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1326                         tl = nfsm_build(u_int32_t *, NFSX_V3CREATEVERF);
1327 #ifdef INET
1328                         if (!TAILQ_EMPTY(&in_ifaddrhead))
1329                                 *tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr.s_addr;
1330                         else
1331 #endif
1332                                 *tl++ = create_verf;
1333                         *tl = ++create_verf;
1334                 } else {
1335                         *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1336                         nfsm_v3attrbuild(vap, FALSE);
1337                 }
1338         } else {
1339                 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1340                 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1341                 sp->sa_uid = nfs_xdrneg1;
1342                 sp->sa_gid = nfs_xdrneg1;
1343                 sp->sa_size = 0;
1344                 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1345                 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1346         }
1347         nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_thread, cnp->cn_cred);
1348         if (!error) {
1349                 nfsm_mtofh(dvp, newvp, v3, gotvp);
1350                 if (!gotvp) {
1351                         if (newvp) {
1352                                 vput(newvp);
1353                                 newvp = (struct vnode *)0;
1354                         }
1355                         error = nfs_lookitup(dvp, cnp->cn_nameptr,
1356                             cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread, &np);
1357                         if (!error)
1358                                 newvp = NFSTOV(np);
1359                 }
1360         }
1361         if (v3)
1362                 nfsm_wcc_data(dvp, wccflag);
1363         m_freem(mrep);
1364 nfsmout:
1365         if (error) {
1366                 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1367                         fmode &= ~O_EXCL;
1368                         goto again;
1369                 }
1370                 if (newvp)
1371                         vput(newvp);
1372         } else if (v3 && (fmode & O_EXCL)) {
1373                 /*
1374                  * We are normally called with only a partially initialized
1375                  * VAP.  Since the NFSv3 spec says that server may use the
1376                  * file attributes to store the verifier, the spec requires
1377                  * us to do a SETATTR RPC. FreeBSD servers store the verifier
1378                  * in atime, but we can't really assume that all servers will
1379                  * so we ensure that our SETATTR sets both atime and mtime.
1380                  */
1381                 if (vap->va_mtime.tv_sec == VNOVAL)
1382                         vfs_timestamp(&vap->va_mtime);
1383                 if (vap->va_atime.tv_sec == VNOVAL)
1384                         vap->va_atime = vap->va_mtime;
1385                 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_thread);
1386         }
1387         if (!error) {
1388                 if (cnp->cn_flags & MAKEENTRY)
1389                         cache_enter(dvp, newvp, cnp);
1390                 *ap->a_vpp = newvp;
1391         }
1392         VTONFS(dvp)->n_flag |= NMODIFIED;
1393         if (!wccflag)
1394                 VTONFS(dvp)->n_attrstamp = 0;
1395         return (error);
1396 }
1397
1398 /*
1399  * nfs file remove call
1400  * To try and make nfs semantics closer to ufs semantics, a file that has
1401  * other processes using the vnode is renamed instead of removed and then
1402  * removed later on the last close.
1403  * - If v_usecount > 1
1404  *        If a rename is not already in the works
1405  *           call nfs_sillyrename() to set it up
1406  *     else
1407  *        do the remove rpc
1408  */
1409 static int
1410 nfs_remove(struct vop_remove_args *ap)
1411 {
1412         struct vnode *vp = ap->a_vp;
1413         struct vnode *dvp = ap->a_dvp;
1414         struct componentname *cnp = ap->a_cnp;
1415         struct nfsnode *np = VTONFS(vp);
1416         int error = 0;
1417         struct vattr vattr;
1418
1419 #ifndef DIAGNOSTIC
1420         if ((cnp->cn_flags & HASBUF) == 0)
1421                 panic("nfs_remove: no name");
1422         if (vp->v_usecount < 1)
1423                 panic("nfs_remove: bad v_usecount");
1424 #endif
1425         if (vp->v_type == VDIR)
1426                 error = EPERM;
1427         else if (vp->v_usecount == 1 || (np->n_sillyrename &&
1428             VOP_GETATTR(vp, &vattr, cnp->cn_cred, cnp->cn_thread) == 0 &&
1429             vattr.va_nlink > 1)) {
1430                 /*
1431                  * Purge the name cache so that the chance of a lookup for
1432                  * the name succeeding while the remove is in progress is
1433                  * minimized. Without node locking it can still happen, such
1434                  * that an I/O op returns ESTALE, but since you get this if
1435                  * another host removes the file..
1436                  */
1437                 cache_purge(vp);
1438                 /*
1439                  * throw away biocache buffers, mainly to avoid
1440                  * unnecessary delayed writes later.
1441                  */
1442                 error = nfs_vinvalbuf(vp, 0, cnp->cn_cred, cnp->cn_thread, 1);
1443                 /* Do the rpc */
1444                 if (error != EINTR)
1445                         error = nfs_removerpc(dvp, cnp->cn_nameptr,
1446                                 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
1447                 /*
1448                  * Kludge City: If the first reply to the remove rpc is lost..
1449                  *   the reply to the retransmitted request will be ENOENT
1450                  *   since the file was in fact removed
1451                  *   Therefore, we cheat and return success.
1452                  */
1453                 if (error == ENOENT)
1454                         error = 0;
1455         } else if (!np->n_sillyrename)
1456                 error = nfs_sillyrename(dvp, vp, cnp);
1457         np->n_attrstamp = 0;
1458         return (error);
1459 }
1460
1461 /*
1462  * nfs file remove rpc called from nfs_inactive
1463  */
1464 int
1465 nfs_removeit(struct sillyrename *sp)
1466 {
1467
1468         return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred,
1469                 (struct thread *)0));
1470 }
1471
1472 /*
1473  * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1474  */
1475 static int
1476 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1477     struct ucred *cred, struct thread *td)
1478 {
1479         caddr_t bpos, dpos;
1480         int error = 0, wccflag = NFSV3_WCCRATTR;
1481         struct mbuf *mreq, *mrep, *md, *mb;
1482         int v3 = NFS_ISV3(dvp);
1483
1484         nfsstats.rpccnt[NFSPROC_REMOVE]++;
1485         mreq = nfsm_reqhead(dvp, NFSPROC_REMOVE,
1486                 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1487         mb = mreq;
1488         bpos = mtod(mb, caddr_t);
1489         nfsm_fhtom(dvp, v3);
1490         nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1491         nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1492         if (v3)
1493                 nfsm_wcc_data(dvp, wccflag);
1494         m_freem(mrep);
1495 nfsmout:
1496         VTONFS(dvp)->n_flag |= NMODIFIED;
1497         if (!wccflag)
1498                 VTONFS(dvp)->n_attrstamp = 0;
1499         return (error);
1500 }
1501
1502 /*
1503  * nfs file rename call
1504  */
1505 static int
1506 nfs_rename(struct vop_rename_args *ap)
1507 {
1508         struct vnode *fvp = ap->a_fvp;
1509         struct vnode *tvp = ap->a_tvp;
1510         struct vnode *fdvp = ap->a_fdvp;
1511         struct vnode *tdvp = ap->a_tdvp;
1512         struct componentname *tcnp = ap->a_tcnp;
1513         struct componentname *fcnp = ap->a_fcnp;
1514         int error;
1515
1516 #ifndef DIAGNOSTIC
1517         if ((tcnp->cn_flags & HASBUF) == 0 ||
1518             (fcnp->cn_flags & HASBUF) == 0)
1519                 panic("nfs_rename: no name");
1520 #endif
1521         /* Check for cross-device rename */
1522         if ((fvp->v_mount != tdvp->v_mount) ||
1523             (tvp && (fvp->v_mount != tvp->v_mount))) {
1524                 error = EXDEV;
1525                 goto out;
1526         }
1527
1528         /*
1529          * We have to flush B_DELWRI data prior to renaming
1530          * the file.  If we don't, the delayed-write buffers
1531          * can be flushed out later after the file has gone stale
1532          * under NFSV3.  NFSV2 does not have this problem because
1533          * ( as far as I can tell ) it flushes dirty buffers more
1534          * often.
1535          */
1536
1537         VOP_FSYNC(fvp, fcnp->cn_cred, MNT_WAIT, fcnp->cn_thread);
1538         if (tvp)
1539             VOP_FSYNC(tvp, tcnp->cn_cred, MNT_WAIT, tcnp->cn_thread);
1540
1541         /*
1542          * If the tvp exists and is in use, sillyrename it before doing the
1543          * rename of the new file over it.
1544          * XXX Can't sillyrename a directory.
1545          */
1546         if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1547                 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1548                 vput(tvp);
1549                 tvp = NULL;
1550         }
1551
1552         error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1553                 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1554                 tcnp->cn_thread);
1555
1556         if (fvp->v_type == VDIR) {
1557                 if (tvp != NULL && tvp->v_type == VDIR)
1558                         cache_purge(tdvp);
1559                 cache_purge(fdvp);
1560         }
1561
1562 out:
1563         if (tdvp == tvp)
1564                 vrele(tdvp);
1565         else
1566                 vput(tdvp);
1567         if (tvp)
1568                 vput(tvp);
1569         vrele(fdvp);
1570         vrele(fvp);
1571         /*
1572          * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1573          */
1574         if (error == ENOENT)
1575                 error = 0;
1576         return (error);
1577 }
1578
1579 /*
1580  * nfs file rename rpc called from nfs_remove() above
1581  */
1582 static int
1583 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1584     struct sillyrename *sp)
1585 {
1586
1587         return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen, sdvp,
1588             sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_thread));
1589 }
1590
1591 /*
1592  * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1593  */
1594 static int
1595 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1596     struct vnode *tdvp, const char *tnameptr, int tnamelen, struct ucred *cred,
1597     struct thread *td)
1598 {
1599         caddr_t bpos, dpos;
1600         int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1601         struct mbuf *mreq, *mrep, *md, *mb;
1602         int v3 = NFS_ISV3(fdvp);
1603
1604         nfsstats.rpccnt[NFSPROC_RENAME]++;
1605         mreq = nfsm_reqhead(fdvp, NFSPROC_RENAME,
1606                 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1607                 nfsm_rndup(tnamelen));
1608         mb = mreq;
1609         bpos = mtod(mb, caddr_t);
1610         nfsm_fhtom(fdvp, v3);
1611         nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1612         nfsm_fhtom(tdvp, v3);
1613         nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1614         nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1615         if (v3) {
1616                 nfsm_wcc_data(fdvp, fwccflag);
1617                 nfsm_wcc_data(tdvp, twccflag);
1618         }
1619         m_freem(mrep);
1620 nfsmout:
1621         VTONFS(fdvp)->n_flag |= NMODIFIED;
1622         VTONFS(tdvp)->n_flag |= NMODIFIED;
1623         if (!fwccflag)
1624                 VTONFS(fdvp)->n_attrstamp = 0;
1625         if (!twccflag)
1626                 VTONFS(tdvp)->n_attrstamp = 0;
1627         return (error);
1628 }
1629
1630 /*
1631  * nfs hard link create call
1632  */
1633 static int
1634 nfs_link(struct vop_link_args *ap)
1635 {
1636         struct vnode *vp = ap->a_vp;
1637         struct vnode *tdvp = ap->a_tdvp;
1638         struct componentname *cnp = ap->a_cnp;
1639         caddr_t bpos, dpos;
1640         int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1641         struct mbuf *mreq, *mrep, *md, *mb;
1642         int v3;
1643
1644         if (vp->v_mount != tdvp->v_mount) {
1645                 return (EXDEV);
1646         }
1647
1648         /*
1649          * Push all writes to the server, so that the attribute cache
1650          * doesn't get "out of sync" with the server.
1651          * XXX There should be a better way!
1652          */
1653         VOP_FSYNC(vp, cnp->cn_cred, MNT_WAIT, cnp->cn_thread);
1654
1655         v3 = NFS_ISV3(vp);
1656         nfsstats.rpccnt[NFSPROC_LINK]++;
1657         mreq = nfsm_reqhead(vp, NFSPROC_LINK,
1658                 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1659         mb = mreq;
1660         bpos = mtod(mb, caddr_t);
1661         nfsm_fhtom(vp, v3);
1662         nfsm_fhtom(tdvp, v3);
1663         nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1664         nfsm_request(vp, NFSPROC_LINK, cnp->cn_thread, cnp->cn_cred);
1665         if (v3) {
1666                 nfsm_postop_attr(vp, attrflag);
1667                 nfsm_wcc_data(tdvp, wccflag);
1668         }
1669         m_freem(mrep);
1670 nfsmout:
1671         VTONFS(tdvp)->n_flag |= NMODIFIED;
1672         if (!attrflag)
1673                 VTONFS(vp)->n_attrstamp = 0;
1674         if (!wccflag)
1675                 VTONFS(tdvp)->n_attrstamp = 0;
1676         /*
1677          * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1678          */
1679         if (error == EEXIST)
1680                 error = 0;
1681         return (error);
1682 }
1683
1684 /*
1685  * nfs symbolic link create call
1686  */
1687 static int
1688 nfs_symlink(struct vop_symlink_args *ap)
1689 {
1690         struct vnode *dvp = ap->a_dvp;
1691         struct vattr *vap = ap->a_vap;
1692         struct componentname *cnp = ap->a_cnp;
1693         struct nfsv2_sattr *sp;
1694         caddr_t bpos, dpos;
1695         int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1696         struct mbuf *mreq, *mrep, *md, *mb;
1697         struct vnode *newvp = (struct vnode *)0;
1698         int v3 = NFS_ISV3(dvp);
1699
1700         nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1701         slen = strlen(ap->a_target);
1702         mreq = nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1703             nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1704         mb = mreq;
1705         bpos = mtod(mb, caddr_t);
1706         nfsm_fhtom(dvp, v3);
1707         nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1708         if (v3) {
1709                 nfsm_v3attrbuild(vap, FALSE);
1710         }
1711         nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1712         if (!v3) {
1713                 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1714                 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1715                 sp->sa_uid = nfs_xdrneg1;
1716                 sp->sa_gid = nfs_xdrneg1;
1717                 sp->sa_size = nfs_xdrneg1;
1718                 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1719                 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1720         }
1721
1722         /*
1723          * Issue the NFS request and get the rpc response.
1724          *
1725          * Only NFSv3 responses returning an error of 0 actually return
1726          * a file handle that can be converted into newvp without having
1727          * to do an extra lookup rpc.
1728          */
1729         nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_thread, cnp->cn_cred);
1730         if (v3) {
1731                 if (error == 0)
1732                         nfsm_mtofh(dvp, newvp, v3, gotvp);
1733                 nfsm_wcc_data(dvp, wccflag);
1734         }
1735
1736         /*
1737          * out code jumps -> here, mrep is also freed.
1738          */
1739
1740         m_freem(mrep);
1741 nfsmout:
1742
1743         /*
1744          * If we get an EEXIST error, silently convert it to no-error
1745          * in case of an NFS retry.
1746          */
1747         if (error == EEXIST)
1748                 error = 0;
1749
1750         /*
1751          * If we do not have (or no longer have) an error, and we could
1752          * not extract the newvp from the response due to the request being
1753          * NFSv2 or the error being EEXIST.  We have to do a lookup in order
1754          * to obtain a newvp to return.
1755          */
1756         if (error == 0 && newvp == NULL) {
1757                 struct nfsnode *np = NULL;
1758
1759                 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1760                     cnp->cn_cred, cnp->cn_thread, &np);
1761                 if (!error)
1762                         newvp = NFSTOV(np);
1763         }
1764         if (error) {
1765                 if (newvp)
1766                         vput(newvp);
1767         } else {
1768                 *ap->a_vpp = newvp;
1769         }
1770         VTONFS(dvp)->n_flag |= NMODIFIED;
1771         if (!wccflag)
1772                 VTONFS(dvp)->n_attrstamp = 0;
1773         return (error);
1774 }
1775
1776 /*
1777  * nfs make dir call
1778  */
1779 static int
1780 nfs_mkdir(struct vop_mkdir_args *ap)
1781 {
1782         struct vnode *dvp = ap->a_dvp;
1783         struct vattr *vap = ap->a_vap;
1784         struct componentname *cnp = ap->a_cnp;
1785         struct nfsv2_sattr *sp;
1786         int len;
1787         struct nfsnode *np = (struct nfsnode *)0;
1788         struct vnode *newvp = (struct vnode *)0;
1789         caddr_t bpos, dpos;
1790         int error = 0, wccflag = NFSV3_WCCRATTR;
1791         int gotvp = 0;
1792         struct mbuf *mreq, *mrep, *md, *mb;
1793         struct vattr vattr;
1794         int v3 = NFS_ISV3(dvp);
1795
1796         if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_thread)) != 0) {
1797                 return (error);
1798         }
1799         len = cnp->cn_namelen;
1800         nfsstats.rpccnt[NFSPROC_MKDIR]++;
1801         mreq = nfsm_reqhead(dvp, NFSPROC_MKDIR,
1802           NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
1803         mb = mreq;
1804         bpos = mtod(mb, caddr_t);
1805         nfsm_fhtom(dvp, v3);
1806         nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1807         if (v3) {
1808                 nfsm_v3attrbuild(vap, FALSE);
1809         } else {
1810                 sp = nfsm_build(struct nfsv2_sattr *, NFSX_V2SATTR);
1811                 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
1812                 sp->sa_uid = nfs_xdrneg1;
1813                 sp->sa_gid = nfs_xdrneg1;
1814                 sp->sa_size = nfs_xdrneg1;
1815                 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1816                 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1817         }
1818         nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_thread, cnp->cn_cred);
1819         if (!error)
1820                 nfsm_mtofh(dvp, newvp, v3, gotvp);
1821         if (v3)
1822                 nfsm_wcc_data(dvp, wccflag);
1823         m_freem(mrep);
1824 nfsmout:
1825         VTONFS(dvp)->n_flag |= NMODIFIED;
1826         if (!wccflag)
1827                 VTONFS(dvp)->n_attrstamp = 0;
1828         /*
1829          * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
1830          * if we can succeed in looking up the directory.
1831          */
1832         if (error == EEXIST || (!error && !gotvp)) {
1833                 if (newvp) {
1834                         vrele(newvp);
1835                         newvp = (struct vnode *)0;
1836                 }
1837                 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
1838                         cnp->cn_thread, &np);
1839                 if (!error) {
1840                         newvp = NFSTOV(np);
1841                         if (newvp->v_type != VDIR)
1842                                 error = EEXIST;
1843                 }
1844         }
1845         if (error) {
1846                 if (newvp)
1847                         vrele(newvp);
1848         } else
1849                 *ap->a_vpp = newvp;
1850         return (error);
1851 }
1852
1853 /*
1854  * nfs remove directory call
1855  */
1856 static int
1857 nfs_rmdir(struct vop_rmdir_args *ap)
1858 {
1859         struct vnode *vp = ap->a_vp;
1860         struct vnode *dvp = ap->a_dvp;
1861         struct componentname *cnp = ap->a_cnp;
1862         caddr_t bpos, dpos;
1863         int error = 0, wccflag = NFSV3_WCCRATTR;
1864         struct mbuf *mreq, *mrep, *md, *mb;
1865         int v3 = NFS_ISV3(dvp);
1866
1867         if (dvp == vp)
1868                 return (EINVAL);
1869         nfsstats.rpccnt[NFSPROC_RMDIR]++;
1870         mreq = nfsm_reqhead(dvp, NFSPROC_RMDIR,
1871                 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1872         mb = mreq;
1873         bpos = mtod(mb, caddr_t);
1874         nfsm_fhtom(dvp, v3);
1875         nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1876         nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_thread, cnp->cn_cred);
1877         if (v3)
1878                 nfsm_wcc_data(dvp, wccflag);
1879         m_freem(mrep);
1880 nfsmout:
1881         VTONFS(dvp)->n_flag |= NMODIFIED;
1882         if (!wccflag)
1883                 VTONFS(dvp)->n_attrstamp = 0;
1884         cache_purge(dvp);
1885         cache_purge(vp);
1886         /*
1887          * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
1888          */
1889         if (error == ENOENT)
1890                 error = 0;
1891         return (error);
1892 }
1893
1894 /*
1895  * nfs readdir call
1896  */
1897 static int
1898 nfs_readdir(struct vop_readdir_args *ap)
1899 {
1900         struct vnode *vp = ap->a_vp;
1901         struct nfsnode *np = VTONFS(vp);
1902         struct uio *uio = ap->a_uio;
1903         int tresid, error;
1904         struct vattr vattr;
1905
1906         if (vp->v_type != VDIR)
1907                 return (EPERM);
1908         /*
1909          * First, check for hit on the EOF offset cache
1910          */
1911         if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
1912             (np->n_flag & NMODIFIED) == 0) {
1913                 if (VOP_GETATTR(vp, &vattr, ap->a_cred, uio->uio_td) == 0 &&
1914                         np->n_mtime == vattr.va_mtime.tv_sec) {
1915                         nfsstats.direofcache_hits++;
1916                         return (0);
1917                 }
1918         }
1919
1920         /*
1921          * Call nfs_bioread() to do the real work.
1922          */
1923         tresid = uio->uio_resid;
1924         error = nfs_bioread(vp, uio, 0, ap->a_cred);
1925
1926         if (!error && uio->uio_resid == tresid)
1927                 nfsstats.direofcache_misses++;
1928         return (error);
1929 }
1930
1931 /*
1932  * Readdir rpc call.
1933  * Called from below the buffer cache by nfs_doio().
1934  */
1935 int
1936 nfs_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1937 {
1938         int len, left;
1939         struct dirent *dp = NULL;
1940         u_int32_t *tl;
1941         caddr_t cp;
1942         nfsuint64 *cookiep;
1943         caddr_t bpos, dpos;
1944         struct mbuf *mreq, *mrep, *md, *mb;
1945         nfsuint64 cookie;
1946         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1947         struct nfsnode *dnp = VTONFS(vp);
1948         u_quad_t fileno;
1949         int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
1950         int attrflag;
1951         int v3 = NFS_ISV3(vp);
1952
1953 #ifndef DIAGNOSTIC
1954         if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
1955                 (uiop->uio_resid & (DIRBLKSIZ - 1)))
1956                 panic("nfs readdirrpc bad uio");
1957 #endif
1958
1959         /*
1960          * If there is no cookie, assume directory was stale.
1961          */
1962         cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
1963         if (cookiep)
1964                 cookie = *cookiep;
1965         else
1966                 return (NFSERR_BAD_COOKIE);
1967         /*
1968          * Loop around doing readdir rpc's of size nm_readdirsize
1969          * truncated to a multiple of DIRBLKSIZ.
1970          * The stopping criteria is EOF or buffer full.
1971          */
1972         while (more_dirs && bigenough) {
1973                 nfsstats.rpccnt[NFSPROC_READDIR]++;
1974                 mreq = nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
1975                         NFSX_READDIR(v3));
1976                 mb = mreq;
1977                 bpos = mtod(mb, caddr_t);
1978                 nfsm_fhtom(vp, v3);
1979                 if (v3) {
1980                         tl = nfsm_build(u_int32_t *, 5 * NFSX_UNSIGNED);
1981                         *tl++ = cookie.nfsuquad[0];
1982                         *tl++ = cookie.nfsuquad[1];
1983                         *tl++ = dnp->n_cookieverf.nfsuquad[0];
1984                         *tl++ = dnp->n_cookieverf.nfsuquad[1];
1985                 } else {
1986                         tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
1987                         *tl++ = cookie.nfsuquad[0];
1988                 }
1989                 *tl = txdr_unsigned(nmp->nm_readdirsize);
1990                 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, cred);
1991                 if (v3) {
1992                         nfsm_postop_attr(vp, attrflag);
1993                         if (!error) {
1994                                 tl = nfsm_dissect(u_int32_t *,
1995                                     2 * NFSX_UNSIGNED);
1996                                 dnp->n_cookieverf.nfsuquad[0] = *tl++;
1997                                 dnp->n_cookieverf.nfsuquad[1] = *tl;
1998                         } else {
1999                                 m_freem(mrep);
2000                                 goto nfsmout;
2001                         }
2002                 }
2003                 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2004                 more_dirs = fxdr_unsigned(int, *tl);
2005
2006                 /* loop thru the dir entries, doctoring them to 4bsd form */
2007                 while (more_dirs && bigenough) {
2008                         if (v3) {
2009                                 tl = nfsm_dissect(u_int32_t *,
2010                                     3 * NFSX_UNSIGNED);
2011                                 fileno = fxdr_hyper(tl);
2012                                 len = fxdr_unsigned(int, *(tl + 2));
2013                         } else {
2014                                 tl = nfsm_dissect(u_int32_t *,
2015                                     2 * NFSX_UNSIGNED);
2016                                 fileno = fxdr_unsigned(u_quad_t, *tl++);
2017                                 len = fxdr_unsigned(int, *tl);
2018                         }
2019                         if (len <= 0 || len > NFS_MAXNAMLEN) {
2020                                 error = EBADRPC;
2021                                 m_freem(mrep);
2022                                 goto nfsmout;
2023                         }
2024                         tlen = nfsm_rndup(len);
2025                         if (tlen == len)
2026                                 tlen += 4;      /* To ensure null termination */
2027                         left = DIRBLKSIZ - blksiz;
2028                         if ((tlen + DIRHDSIZ) > left) {
2029                                 dp->d_reclen += left;
2030                                 uiop->uio_iov->iov_base += left;
2031                                 uiop->uio_iov->iov_len -= left;
2032                                 uiop->uio_offset += left;
2033                                 uiop->uio_resid -= left;
2034                                 blksiz = 0;
2035                         }
2036                         if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2037                                 bigenough = 0;
2038                         if (bigenough) {
2039                                 dp = (struct dirent *)uiop->uio_iov->iov_base;
2040                                 dp->d_fileno = (int)fileno;
2041                                 dp->d_namlen = len;
2042                                 dp->d_reclen = tlen + DIRHDSIZ;
2043                                 dp->d_type = DT_UNKNOWN;
2044                                 blksiz += dp->d_reclen;
2045                                 if (blksiz == DIRBLKSIZ)
2046                                         blksiz = 0;
2047                                 uiop->uio_offset += DIRHDSIZ;
2048                                 uiop->uio_resid -= DIRHDSIZ;
2049                                 uiop->uio_iov->iov_base += DIRHDSIZ;
2050                                 uiop->uio_iov->iov_len -= DIRHDSIZ;
2051                                 nfsm_mtouio(uiop, len);
2052                                 cp = uiop->uio_iov->iov_base;
2053                                 tlen -= len;
2054                                 *cp = '\0';     /* null terminate */
2055                                 uiop->uio_iov->iov_base += tlen;
2056                                 uiop->uio_iov->iov_len -= tlen;
2057                                 uiop->uio_offset += tlen;
2058                                 uiop->uio_resid -= tlen;
2059                         } else
2060                                 nfsm_adv(nfsm_rndup(len));
2061                         if (v3) {
2062                                 tl = nfsm_dissect(u_int32_t *,
2063                                     3 * NFSX_UNSIGNED);
2064                         } else {
2065                                 tl = nfsm_dissect(u_int32_t *,
2066                                     2 * NFSX_UNSIGNED);
2067                         }
2068                         if (bigenough) {
2069                                 cookie.nfsuquad[0] = *tl++;
2070                                 if (v3)
2071                                         cookie.nfsuquad[1] = *tl++;
2072                         } else if (v3)
2073                                 tl += 2;
2074                         else
2075                                 tl++;
2076                         more_dirs = fxdr_unsigned(int, *tl);
2077                 }
2078                 /*
2079                  * If at end of rpc data, get the eof boolean
2080                  */
2081                 if (!more_dirs) {
2082                         tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2083                         more_dirs = (fxdr_unsigned(int, *tl) == 0);
2084                 }
2085                 m_freem(mrep);
2086         }
2087         /*
2088          * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2089          * by increasing d_reclen for the last record.
2090          */
2091         if (blksiz > 0) {
2092                 left = DIRBLKSIZ - blksiz;
2093                 dp->d_reclen += left;
2094                 uiop->uio_iov->iov_base += left;
2095                 uiop->uio_iov->iov_len -= left;
2096                 uiop->uio_offset += left;
2097                 uiop->uio_resid -= left;
2098         }
2099
2100         /*
2101          * We are now either at the end of the directory or have filled the
2102          * block.
2103          */
2104         if (bigenough)
2105                 dnp->n_direofoffset = uiop->uio_offset;
2106         else {
2107                 if (uiop->uio_resid > 0)
2108                         printf("EEK! readdirrpc resid > 0\n");
2109                 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2110                 *cookiep = cookie;
2111         }
2112 nfsmout:
2113         return (error);
2114 }
2115
2116 /*
2117  * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2118  */
2119 int
2120 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
2121 {
2122         int len, left;
2123         struct dirent *dp;
2124         u_int32_t *tl;
2125         caddr_t cp;
2126         struct vnode *newvp;
2127         nfsuint64 *cookiep;
2128         caddr_t bpos, dpos, dpossav1, dpossav2;
2129         struct mbuf *mreq, *mrep, *md, *mb, *mdsav1, *mdsav2;
2130         struct nameidata nami, *ndp = &nami;
2131         struct componentname *cnp = &ndp->ni_cnd;
2132         nfsuint64 cookie;
2133         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2134         struct nfsnode *dnp = VTONFS(vp), *np;
2135         nfsfh_t *fhp;
2136         u_quad_t fileno;
2137         int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2138         int attrflag, fhsize;
2139
2140 #ifndef nolint
2141         dp = (struct dirent *)0;
2142 #endif
2143 #ifndef DIAGNOSTIC
2144         if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2145                 (uiop->uio_resid & (DIRBLKSIZ - 1)))
2146                 panic("nfs readdirplusrpc bad uio");
2147 #endif
2148         ndp->ni_dvp = vp;
2149         newvp = NULLVP;
2150
2151         /*
2152          * If there is no cookie, assume directory was stale.
2153          */
2154         cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2155         if (cookiep)
2156                 cookie = *cookiep;
2157         else
2158                 return (NFSERR_BAD_COOKIE);
2159         /*
2160          * Loop around doing readdir rpc's of size nm_readdirsize
2161          * truncated to a multiple of DIRBLKSIZ.
2162          * The stopping criteria is EOF or buffer full.
2163          */
2164         while (more_dirs && bigenough) {
2165                 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2166                 mreq = nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2167                         NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2168                 mb = mreq;
2169                 bpos = mtod(mb, caddr_t);
2170                 nfsm_fhtom(vp, 1);
2171                 tl = nfsm_build(u_int32_t *, 6 * NFSX_UNSIGNED);
2172                 *tl++ = cookie.nfsuquad[0];
2173                 *tl++ = cookie.nfsuquad[1];
2174                 *tl++ = dnp->n_cookieverf.nfsuquad[0];
2175                 *tl++ = dnp->n_cookieverf.nfsuquad[1];
2176                 *tl++ = txdr_unsigned(nmp->nm_readdirsize);
2177                 *tl = txdr_unsigned(nmp->nm_rsize);
2178                 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, cred);
2179                 nfsm_postop_attr(vp, attrflag);
2180                 if (error) {
2181                         m_freem(mrep);
2182                         goto nfsmout;
2183                 }
2184                 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2185                 dnp->n_cookieverf.nfsuquad[0] = *tl++;
2186                 dnp->n_cookieverf.nfsuquad[1] = *tl++;
2187                 more_dirs = fxdr_unsigned(int, *tl);
2188
2189                 /* loop thru the dir entries, doctoring them to 4bsd form */
2190                 while (more_dirs && bigenough) {
2191                         tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2192                         fileno = fxdr_hyper(tl);
2193                         len = fxdr_unsigned(int, *(tl + 2));
2194                         if (len <= 0 || len > NFS_MAXNAMLEN) {
2195                                 error = EBADRPC;
2196                                 m_freem(mrep);
2197                                 goto nfsmout;
2198                         }
2199                         tlen = nfsm_rndup(len);
2200                         if (tlen == len)
2201                                 tlen += 4;      /* To ensure null termination*/
2202                         left = DIRBLKSIZ - blksiz;
2203                         if ((tlen + DIRHDSIZ) > left) {
2204                                 dp->d_reclen += left;
2205                                 uiop->uio_iov->iov_base += left;
2206                                 uiop->uio_iov->iov_len -= left;
2207                                 uiop->uio_offset += left;
2208                                 uiop->uio_resid -= left;
2209                                 blksiz = 0;
2210                         }
2211                         if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2212                                 bigenough = 0;
2213                         if (bigenough) {
2214                                 dp = (struct dirent *)uiop->uio_iov->iov_base;
2215                                 dp->d_fileno = (int)fileno;
2216                                 dp->d_namlen = len;
2217                                 dp->d_reclen = tlen + DIRHDSIZ;
2218                                 dp->d_type = DT_UNKNOWN;
2219                                 blksiz += dp->d_reclen;
2220                                 if (blksiz == DIRBLKSIZ)
2221                                         blksiz = 0;
2222                                 uiop->uio_offset += DIRHDSIZ;
2223                                 uiop->uio_resid -= DIRHDSIZ;
2224                                 uiop->uio_iov->iov_base += DIRHDSIZ;
2225                                 uiop->uio_iov->iov_len -= DIRHDSIZ;
2226                                 cnp->cn_nameptr = uiop->uio_iov->iov_base;
2227                                 cnp->cn_namelen = len;
2228                                 nfsm_mtouio(uiop, len);
2229                                 cp = uiop->uio_iov->iov_base;
2230                                 tlen -= len;
2231                                 *cp = '\0';
2232                                 uiop->uio_iov->iov_base += tlen;
2233                                 uiop->uio_iov->iov_len -= tlen;
2234                                 uiop->uio_offset += tlen;
2235                                 uiop->uio_resid -= tlen;
2236                         } else
2237                                 nfsm_adv(nfsm_rndup(len));
2238                         tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
2239                         if (bigenough) {
2240                                 cookie.nfsuquad[0] = *tl++;
2241                                 cookie.nfsuquad[1] = *tl++;
2242                         } else
2243                                 tl += 2;
2244
2245                         /*
2246                          * Since the attributes are before the file handle
2247                          * (sigh), we must skip over the attributes and then
2248                          * come back and get them.
2249                          */
2250                         attrflag = fxdr_unsigned(int, *tl);
2251                         if (attrflag) {
2252                             dpossav1 = dpos;
2253                             mdsav1 = md;
2254                             nfsm_adv(NFSX_V3FATTR);
2255                             tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2256                             doit = fxdr_unsigned(int, *tl);
2257                             if (doit) {
2258                                 nfsm_getfh(fhp, fhsize, 1);
2259                                 if (NFS_CMPFH(dnp, fhp, fhsize)) {
2260                                     VREF(vp);
2261                                     newvp = vp;
2262                                     np = dnp;
2263                                 } else {
2264                                     error = nfs_nget(vp->v_mount, fhp,
2265                                         fhsize, &np);
2266                                     if (error)
2267                                         doit = 0;
2268                                     else
2269                                         newvp = NFSTOV(np);
2270                                 }
2271                             }
2272                             if (doit && bigenough) {
2273                                 dpossav2 = dpos;
2274                                 dpos = dpossav1;
2275                                 mdsav2 = md;
2276                                 md = mdsav1;
2277                                 nfsm_loadattr(newvp, (struct vattr *)0);
2278                                 dpos = dpossav2;
2279                                 md = mdsav2;
2280                                 dp->d_type =
2281                                     IFTODT(VTTOIF(np->n_vattr.va_type));
2282                                 ndp->ni_vp = newvp;
2283                                 cache_enter(ndp->ni_dvp, ndp->ni_vp, cnp);
2284                             }
2285                         } else {
2286                             /* Just skip over the file handle */
2287                             tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2288                             i = fxdr_unsigned(int, *tl);
2289                             nfsm_adv(nfsm_rndup(i));
2290                         }
2291                         if (newvp != NULLVP) {
2292                             if (newvp == vp)
2293                                 vrele(newvp);
2294                             else
2295                                 vput(newvp);
2296                             newvp = NULLVP;
2297                         }
2298                         tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2299                         more_dirs = fxdr_unsigned(int, *tl);
2300                 }
2301                 /*
2302                  * If at end of rpc data, get the eof boolean
2303                  */
2304                 if (!more_dirs) {
2305                         tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2306                         more_dirs = (fxdr_unsigned(int, *tl) == 0);
2307                 }
2308                 m_freem(mrep);
2309         }
2310         /*
2311          * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2312          * by increasing d_reclen for the last record.
2313          */
2314         if (blksiz > 0) {
2315                 left = DIRBLKSIZ - blksiz;
2316                 dp->d_reclen += left;
2317                 uiop->uio_iov->iov_base += left;
2318                 uiop->uio_iov->iov_len -= left;
2319                 uiop->uio_offset += left;
2320                 uiop->uio_resid -= left;
2321         }
2322
2323         /*
2324          * We are now either at the end of the directory or have filled the
2325          * block.
2326          */
2327         if (bigenough)
2328                 dnp->n_direofoffset = uiop->uio_offset;
2329         else {
2330                 if (uiop->uio_resid > 0)
2331                         printf("EEK! readdirplusrpc resid > 0\n");
2332                 cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2333                 *cookiep = cookie;
2334         }
2335 nfsmout:
2336         if (newvp != NULLVP) {
2337                 if (newvp == vp)
2338                         vrele(newvp);
2339                 else
2340                         vput(newvp);
2341                 newvp = NULLVP;
2342         }
2343         return (error);
2344 }
2345
2346 /*
2347  * Silly rename. To make the NFS filesystem that is stateless look a little
2348  * more like the "ufs" a remove of an active vnode is translated to a rename
2349  * to a funny looking filename that is removed by nfs_inactive on the
2350  * nfsnode. There is the potential for another process on a different client
2351  * to create the same funny name between the nfs_lookitup() fails and the
2352  * nfs_rename() completes, but...
2353  */
2354 static int
2355 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2356 {
2357         struct sillyrename *sp;
2358         struct nfsnode *np;
2359         int error;
2360         short pid;
2361
2362         cache_purge(dvp);
2363         np = VTONFS(vp);
2364 #ifndef DIAGNOSTIC
2365         if (vp->v_type == VDIR)
2366                 panic("nfs: sillyrename dir");
2367 #endif
2368         MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2369                 M_NFSREQ, M_WAITOK);
2370         sp->s_cred = crhold(cnp->cn_cred);
2371         sp->s_dvp = dvp;
2372         VREF(dvp);
2373
2374         /* Fudge together a funny name */
2375         pid = cnp->cn_thread->td_proc->p_pid;
2376         sp->s_namlen = sprintf(sp->s_name, ".nfsA%04x4.4", pid);
2377
2378         /* Try lookitups until we get one that isn't there */
2379         while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2380                 cnp->cn_thread, (struct nfsnode **)0) == 0) {
2381                 sp->s_name[4]++;
2382                 if (sp->s_name[4] > 'z') {
2383                         error = EINVAL;
2384                         goto bad;
2385                 }
2386         }
2387         error = nfs_renameit(dvp, cnp, sp);
2388         if (error)
2389                 goto bad;
2390         error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2391                 cnp->cn_thread, &np);
2392         np->n_sillyrename = sp;
2393         return (0);
2394 bad:
2395         vrele(sp->s_dvp);
2396         crfree(sp->s_cred);
2397         free((caddr_t)sp, M_NFSREQ);
2398         return (error);
2399 }
2400
2401 /*
2402  * Look up a file name and optionally either update the file handle or
2403  * allocate an nfsnode, depending on the value of npp.
2404  * npp == NULL  --> just do the lookup
2405  * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2406  *                      handled too
2407  * *npp != NULL --> update the file handle in the vnode
2408  */
2409 static int
2410 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2411     struct thread *td, struct nfsnode **npp)
2412 {
2413         struct vnode *newvp = (struct vnode *)0;
2414         struct nfsnode *np, *dnp = VTONFS(dvp);
2415         caddr_t bpos, dpos;
2416         int error = 0, fhlen, attrflag;
2417         struct mbuf *mreq, *mrep, *md, *mb;
2418         nfsfh_t *nfhp;
2419         int v3 = NFS_ISV3(dvp);
2420
2421         nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2422         mreq = nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2423                 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2424         mb = mreq;
2425         bpos = mtod(mb, caddr_t);
2426         nfsm_fhtom(dvp, v3);
2427         nfsm_strtom(name, len, NFS_MAXNAMLEN);
2428         nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2429         if (npp && !error) {
2430                 nfsm_getfh(nfhp, fhlen, v3);
2431                 if (*npp) {
2432                     np = *npp;
2433                     if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2434                         free((caddr_t)np->n_fhp, M_NFSBIGFH);
2435                         np->n_fhp = &np->n_fh;
2436                     } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2437                         np->n_fhp =(nfsfh_t *)malloc(fhlen, M_NFSBIGFH, M_WAITOK);
2438                     bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2439                     np->n_fhsize = fhlen;
2440                     newvp = NFSTOV(np);
2441                 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2442                     VREF(dvp);
2443                     newvp = dvp;
2444                 } else {
2445                     error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2446                     if (error) {
2447                         m_freem(mrep);
2448                         return (error);
2449                     }
2450                     newvp = NFSTOV(np);
2451                 }
2452                 if (v3) {
2453                         nfsm_postop_attr(newvp, attrflag);
2454                         if (!attrflag && *npp == NULL) {
2455                                 m_freem(mrep);
2456                                 if (newvp == dvp)
2457                                         vrele(newvp);
2458                                 else
2459                                         vput(newvp);
2460                                 return (ENOENT);
2461                         }
2462                 } else
2463                         nfsm_loadattr(newvp, (struct vattr *)0);
2464         }
2465         m_freem(mrep);
2466 nfsmout:
2467         if (npp && *npp == NULL) {
2468                 if (error) {
2469                         if (newvp) {
2470                                 if (newvp == dvp)
2471                                         vrele(newvp);
2472                                 else
2473                                         vput(newvp);
2474                         }
2475                 } else
2476                         *npp = np;
2477         }
2478         return (error);
2479 }
2480
2481 /*
2482  * Nfs Version 3 commit rpc
2483  */
2484 int
2485 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2486     struct thread *td)
2487 {
2488         u_int32_t *tl;
2489         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2490         caddr_t bpos, dpos;
2491         int error = 0, wccflag = NFSV3_WCCRATTR;
2492         struct mbuf *mreq, *mrep, *md, *mb;
2493
2494         if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2495                 return (0);
2496         nfsstats.rpccnt[NFSPROC_COMMIT]++;
2497         mreq = nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2498         mb = mreq;
2499         bpos = mtod(mb, caddr_t);
2500         nfsm_fhtom(vp, 1);
2501         tl = nfsm_build(u_int32_t *, 3 * NFSX_UNSIGNED);
2502         txdr_hyper(offset, tl);
2503         tl += 2;
2504         *tl = txdr_unsigned(cnt);
2505         nfsm_request(vp, NFSPROC_COMMIT, td, cred);
2506         nfsm_wcc_data(vp, wccflag);
2507         if (!error) {
2508                 tl = nfsm_dissect(u_int32_t *, NFSX_V3WRITEVERF);
2509                 if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2510                         NFSX_V3WRITEVERF)) {
2511                         bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2512                                 NFSX_V3WRITEVERF);
2513                         error = NFSERR_STALEWRITEVERF;
2514                 }
2515         }
2516         m_freem(mrep);
2517 nfsmout:
2518         return (error);
2519 }
2520
2521 /*
2522  * Strategy routine.
2523  * For async requests when nfsiod(s) are running, queue the request by
2524  * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2525  * request.
2526  */
2527 static int
2528 nfs_strategy(struct vop_strategy_args *ap)
2529 {
2530         struct buf *bp = ap->a_bp;
2531         struct ucred *cr;
2532         struct thread *td;
2533         int error = 0;
2534
2535         KASSERT(!(bp->b_flags & B_DONE), ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2536         KASSERT(BUF_REFCNT(bp) > 0, ("nfs_strategy: buffer %p not locked", bp));
2537
2538         if (bp->b_flags & B_PHYS)
2539                 panic("nfs physio");
2540
2541         if (bp->b_flags & B_ASYNC)
2542                 td = (struct thread *)0;
2543         else
2544                 td = curthread; /* XXX */
2545
2546         if (bp->b_iocmd == BIO_READ)
2547                 cr = bp->b_rcred;
2548         else
2549                 cr = bp->b_wcred;
2550
2551         /*
2552          * If the op is asynchronous and an i/o daemon is waiting
2553          * queue the request, wake it up and wait for completion
2554          * otherwise just do it ourselves.
2555          */
2556         if ((bp->b_flags & B_ASYNC) == 0 ||
2557                 nfs_asyncio(bp, NOCRED, td))
2558                 error = nfs_doio(bp, cr, td);
2559         return (error);
2560 }
2561
2562 /*
2563  * fsync vnode op. Just call nfs_flush() with commit == 1.
2564  */
2565 /* ARGSUSED */
2566 static int
2567 nfs_fsync(struct vop_fsync_args *ap)
2568 {
2569
2570         return (nfs_flush(ap->a_vp, ap->a_cred, ap->a_waitfor, ap->a_td, 1));
2571 }
2572
2573 /*
2574  * Flush all the blocks associated with a vnode.
2575  *      Walk through the buffer pool and push any dirty pages
2576  *      associated with the vnode.
2577  */
2578 static int
2579 nfs_flush(struct vnode *vp, struct ucred *cred, int waitfor, struct thread *td,
2580     int commit)
2581 {
2582         struct nfsnode *np = VTONFS(vp);
2583         struct buf *bp;
2584         int i;
2585         struct buf *nbp;
2586         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2587         int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2588         int passone = 1;
2589         u_quad_t off, endoff, toff;
2590         struct ucred* wcred = NULL;
2591         struct buf **bvec = NULL;
2592 #ifndef NFS_COMMITBVECSIZ
2593 #define NFS_COMMITBVECSIZ       20
2594 #endif
2595         struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2596         int bvecsize = 0, bveccount;
2597
2598         if (nmp->nm_flag & NFSMNT_INT)
2599                 slpflag = PCATCH;
2600         if (!commit)
2601                 passone = 0;
2602         /*
2603          * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2604          * server, but nas not been committed to stable storage on the server
2605          * yet. On the first pass, the byte range is worked out and the commit
2606          * rpc is done. On the second pass, nfs_writebp() is called to do the
2607          * job.
2608          */
2609 again:
2610         off = (u_quad_t)-1;
2611         endoff = 0;
2612         bvecpos = 0;
2613         if (NFS_ISV3(vp) && commit) {
2614                 s = splbio();
2615                 /*
2616                  * Count up how many buffers waiting for a commit.
2617                  */
2618                 bveccount = 0;
2619                 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2620                         nbp = TAILQ_NEXT(bp, b_vnbufs);
2621                         if (BUF_REFCNT(bp) == 0 &&
2622                             (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2623                                 == (B_DELWRI | B_NEEDCOMMIT))
2624                                 bveccount++;
2625                 }
2626                 /*
2627                  * Allocate space to remember the list of bufs to commit.  It is
2628                  * important to use M_NOWAIT here to avoid a race with nfs_write.
2629                  * If we can't get memory (for whatever reason), we will end up
2630                  * committing the buffers one-by-one in the loop below.
2631                  */
2632                 if (bvec != NULL && bvec != bvec_on_stack)
2633                         free(bvec, M_TEMP);
2634                 if (bveccount > NFS_COMMITBVECSIZ) {
2635                         bvec = (struct buf **)
2636                                 malloc(bveccount * sizeof(struct buf *),
2637                                        M_TEMP, M_NOWAIT);
2638                         if (bvec == NULL) {
2639                                 bvec = bvec_on_stack;
2640                                 bvecsize = NFS_COMMITBVECSIZ;
2641                         } else
2642                                 bvecsize = bveccount;
2643                 } else {
2644                         bvec = bvec_on_stack;
2645                         bvecsize = NFS_COMMITBVECSIZ;
2646                 }
2647                 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2648                         nbp = TAILQ_NEXT(bp, b_vnbufs);
2649                         if (bvecpos >= bvecsize)
2650                                 break;
2651                         if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2652                             (B_DELWRI | B_NEEDCOMMIT) ||
2653                             BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
2654                                 continue;
2655                         bremfree(bp);
2656                         /*
2657                          * Work out if all buffers are using the same cred
2658                          * so we can deal with them all with one commit.
2659                          *
2660                          * NOTE: we are not clearing B_DONE here, so we have
2661                          * to do it later on in this routine if we intend to
2662                          * initiate I/O on the bp.
2663                          *
2664                          * Note: to avoid loopback deadlocks, we do not
2665                          * assign b_runningbufspace.
2666                          */
2667                         if (wcred == NULL)
2668                                 wcred = bp->b_wcred;
2669                         else if (wcred != bp->b_wcred)
2670                                 wcred = NOCRED;
2671                         bp->b_flags |= B_WRITEINPROG;
2672                         vfs_busy_pages(bp, 1);
2673
2674                         /*
2675                          * bp is protected by being locked, but nbp is not
2676                          * and vfs_busy_pages() may sleep.  We have to
2677                          * recalculate nbp.
2678                          */
2679                         nbp = TAILQ_NEXT(bp, b_vnbufs);
2680
2681                         /*
2682                          * A list of these buffers is kept so that the
2683                          * second loop knows which buffers have actually
2684                          * been committed. This is necessary, since there
2685                          * may be a race between the commit rpc and new
2686                          * uncommitted writes on the file.
2687                          */
2688                         bvec[bvecpos++] = bp;
2689                         toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2690                                 bp->b_dirtyoff;
2691                         if (toff < off)
2692                                 off = toff;
2693                         toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2694                         if (toff > endoff)
2695                                 endoff = toff;
2696                 }
2697                 splx(s);
2698         }
2699         if (bvecpos > 0) {
2700                 /*
2701                  * Commit data on the server, as required.
2702                  * If all bufs are using the same wcred, then use that with
2703                  * one call for all of them, otherwise commit each one
2704                  * separately.
2705                  */
2706                 if (wcred != NOCRED)
2707                         retv = nfs_commit(vp, off, (int)(endoff - off),
2708                                           wcred, td);
2709                 else {
2710                         retv = 0;
2711                         for (i = 0; i < bvecpos; i++) {
2712                                 off_t off, size;
2713                                 bp = bvec[i];
2714                                 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2715                                         bp->b_dirtyoff;
2716                                 size = (u_quad_t)(bp->b_dirtyend
2717                                                   - bp->b_dirtyoff);
2718                                 retv = nfs_commit(vp, off, (int)size,
2719                                                   bp->b_wcred, td);
2720                                 if (retv) break;
2721                         }
2722                 }
2723
2724                 if (retv == NFSERR_STALEWRITEVERF)
2725                         nfs_clearcommit(vp->v_mount);
2726
2727                 /*
2728                  * Now, either mark the blocks I/O done or mark the
2729                  * blocks dirty, depending on whether the commit
2730                  * succeeded.
2731                  */
2732                 for (i = 0; i < bvecpos; i++) {
2733                         bp = bvec[i];
2734                         bp->b_flags &= ~(B_NEEDCOMMIT | B_WRITEINPROG | B_CLUSTEROK);
2735                         if (retv) {
2736                                 /*
2737                                  * Error, leave B_DELWRI intact
2738                                  */
2739                                 vfs_unbusy_pages(bp);
2740                                 brelse(bp);
2741                         } else {
2742                                 /*
2743                                  * Success, remove B_DELWRI ( bundirty() ).
2744                                  *
2745                                  * b_dirtyoff/b_dirtyend seem to be NFS
2746                                  * specific.  We should probably move that
2747                                  * into bundirty(). XXX
2748                                  */
2749                                 s = splbio();
2750                                 vp->v_numoutput++;
2751                                 bp->b_flags |= B_ASYNC;
2752                                 bundirty(bp);
2753                                 bp->b_flags &= ~B_DONE;
2754                                 bp->b_ioflags &= ~BIO_ERROR;
2755                                 bp->b_dirtyoff = bp->b_dirtyend = 0;
2756                                 splx(s);
2757                                 bufdone(bp);
2758                         }
2759                 }
2760         }
2761
2762         /*
2763          * Start/do any write(s) that are required.
2764          */
2765 loop:
2766         s = splbio();
2767         for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2768                 nbp = TAILQ_NEXT(bp, b_vnbufs);
2769                 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
2770                         if (waitfor != MNT_WAIT || passone)
2771                                 continue;
2772                         error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL,
2773                             "nfsfsync", slpflag, slptimeo);
2774                         splx(s);
2775                         if (error == 0)
2776                                 panic("nfs_fsync: inconsistent lock");
2777                         if (error == ENOLCK)
2778                                 goto loop;
2779                         if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
2780                                 error = EINTR;
2781                                 goto done;
2782                         }
2783                         if (slpflag == PCATCH) {
2784                                 slpflag = 0;
2785                                 slptimeo = 2 * hz;
2786                         }
2787                         goto loop;
2788                 }
2789                 if ((bp->b_flags & B_DELWRI) == 0)
2790                         panic("nfs_fsync: not dirty");
2791                 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
2792                         BUF_UNLOCK(bp);
2793                         continue;
2794                 }
2795                 bremfree(bp);
2796                 if (passone || !commit)
2797                     bp->b_flags |= B_ASYNC;
2798                 else
2799                     bp->b_flags |= B_ASYNC | B_WRITEINPROG;
2800                 splx(s);
2801                 BUF_WRITE(bp);
2802                 goto loop;
2803         }
2804         splx(s);
2805         if (passone) {
2806                 passone = 0;
2807                 goto again;
2808         }
2809         if (waitfor == MNT_WAIT) {
2810                 while (vp->v_numoutput) {
2811                         vp->v_flag |= VBWAIT;
2812                         error = tsleep((caddr_t)&vp->v_numoutput,
2813                                 slpflag | (PRIBIO + 1), "nfsfsync", slptimeo);
2814                         if (error) {
2815                             if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
2816                                 error = EINTR;
2817                                 goto done;
2818                             }
2819                             if (slpflag == PCATCH) {
2820                                 slpflag = 0;
2821                                 slptimeo = 2 * hz;
2822                             }
2823                         }
2824                 }
2825                 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) && commit) {
2826                         goto loop;
2827                 }
2828         }
2829         if (np->n_flag & NWRITEERR) {
2830                 error = np->n_error;
2831                 np->n_flag &= ~NWRITEERR;
2832         }
2833 done:
2834         if (bvec != NULL && bvec != bvec_on_stack)
2835                 free(bvec, M_TEMP);
2836         return (error);
2837 }
2838
2839 /*
2840  * NFS advisory byte-level locks.
2841  */
2842 static int
2843 nfs_advlock(struct vop_advlock_args *ap)
2844 {
2845
2846         if ((VFSTONFS(ap->a_vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
2847                 struct nfsnode *np = VTONFS(ap->a_vp);
2848
2849                 return (lf_advlock(ap, &(np->n_lockf), np->n_size));
2850         }
2851         return (nfs_dolock(ap));
2852 }
2853
2854 /*
2855  * Print out the contents of an nfsnode.
2856  */
2857 static int
2858 nfs_print(struct vop_print_args *ap)
2859 {
2860         struct vnode *vp = ap->a_vp;
2861         struct nfsnode *np = VTONFS(vp);
2862
2863         printf("tag VT_NFS, fileid %ld fsid 0x%x",
2864                 np->n_vattr.va_fileid, np->n_vattr.va_fsid);
2865         if (vp->v_type == VFIFO)
2866                 fifo_printinfo(vp);
2867         printf("\n");
2868         return (0);
2869 }
2870
2871 /*
2872  * This is the "real" nfs::bwrite(struct buf*).
2873  * B_WRITEINPROG isn't set unless the force flag is one and it
2874  * handles the B_NEEDCOMMIT flag.
2875  * We set B_CACHE if this is a VMIO buffer.
2876  */
2877 int
2878 nfs_writebp(struct buf *bp, int force, struct thread *td)
2879 {
2880         int s;
2881         int oldflags = bp->b_flags;
2882 #if 0
2883         int retv = 1;
2884         off_t off;
2885 #endif
2886
2887         if (BUF_REFCNT(bp) == 0)
2888                 panic("bwrite: buffer is not locked???");
2889
2890         if (bp->b_flags & B_INVAL) {
2891                 brelse(bp);
2892                 return(0);
2893         }
2894
2895         bp->b_flags |= B_CACHE;
2896
2897         /*
2898          * Undirty the bp.  We will redirty it later if the I/O fails.
2899          */
2900
2901         s = splbio();
2902         bundirty(bp);
2903         bp->b_flags &= ~B_DONE;
2904         bp->b_ioflags &= ~BIO_ERROR;
2905         bp->b_iocmd = BIO_WRITE;
2906
2907         bp->b_vp->v_numoutput++;
2908         curthread->td_proc->p_stats->p_ru.ru_oublock++;
2909         splx(s);
2910
2911         /*
2912          * Note: to avoid loopback deadlocks, we do not
2913          * assign b_runningbufspace.
2914          */
2915         vfs_busy_pages(bp, 1);
2916
2917         if (force)
2918                 bp->b_flags |= B_WRITEINPROG;
2919         BUF_KERNPROC(bp);
2920         BUF_STRATEGY(bp);
2921
2922         if( (oldflags & B_ASYNC) == 0) {
2923                 int rtval = bufwait(bp);
2924
2925                 if (oldflags & B_DELWRI) {
2926                         s = splbio();
2927                         reassignbuf(bp, bp->b_vp);
2928                         splx(s);
2929                 }
2930
2931                 brelse(bp);
2932                 return (rtval);
2933         }
2934
2935         return (0);
2936 }
2937
2938 /*
2939  * nfs special file access vnode op.
2940  * Essentially just get vattr and then imitate iaccess() since the device is
2941  * local to the client.
2942  */
2943 static int
2944 nfsspec_access(struct vop_access_args *ap)
2945 {
2946         struct vattr *vap;
2947         gid_t *gp;
2948         struct ucred *cred = ap->a_cred;
2949         struct vnode *vp = ap->a_vp;
2950         mode_t mode = ap->a_mode;
2951         struct vattr vattr;
2952         int i;
2953         int error;
2954
2955         /*
2956          * Disallow write attempts on filesystems mounted read-only;
2957          * unless the file is a socket, fifo, or a block or character
2958          * device resident on the filesystem.
2959          */
2960         if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
2961                 switch (vp->v_type) {
2962                 case VREG:
2963                 case VDIR:
2964                 case VLNK:
2965                         return (EROFS);
2966                 default:
2967                         break;
2968                 }
2969         }
2970         /*
2971          * If you're the super-user,
2972          * you always get access.
2973          */
2974         if (cred->cr_uid == 0)
2975                 return (0);
2976         vap = &vattr;
2977         error = VOP_GETATTR(vp, vap, cred, ap->a_td);
2978         if (error)
2979                 return (error);
2980         /*
2981          * Access check is based on only one of owner, group, public.
2982          * If not owner, then check group. If not a member of the
2983          * group, then check public access.
2984          */
2985         if (cred->cr_uid != vap->va_uid) {
2986                 mode >>= 3;
2987                 gp = cred->cr_groups;
2988                 for (i = 0; i < cred->cr_ngroups; i++, gp++)
2989                         if (vap->va_gid == *gp)
2990                                 goto found;
2991                 mode >>= 3;
2992 found:
2993                 ;
2994         }
2995         error = (vap->va_mode & mode) == mode ? 0 : EACCES;
2996         return (error);
2997 }
2998
2999 /*
3000  * Read wrapper for special devices.
3001  */
3002 static int
3003 nfsspec_read(struct vop_read_args *ap)
3004 {
3005         struct nfsnode *np = VTONFS(ap->a_vp);
3006
3007         /*
3008          * Set access flag.
3009          */
3010         np->n_flag |= NACC;
3011         getnanotime(&np->n_atim);
3012         return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap));
3013 }
3014
3015 /*
3016  * Write wrapper for special devices.
3017  */
3018 static int
3019 nfsspec_write(struct vop_write_args *ap)
3020 {
3021         struct nfsnode *np = VTONFS(ap->a_vp);
3022
3023         /*
3024          * Set update flag.
3025          */
3026         np->n_flag |= NUPD;
3027         getnanotime(&np->n_mtim);
3028         return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap));
3029 }
3030
3031 /*
3032  * Close wrapper for special devices.
3033  *
3034  * Update the times on the nfsnode then do device close.
3035  */
3036 static int
3037 nfsspec_close(struct vop_close_args *ap)
3038 {
3039         struct vnode *vp = ap->a_vp;
3040         struct nfsnode *np = VTONFS(vp);
3041         struct vattr vattr;
3042
3043         if (np->n_flag & (NACC | NUPD)) {
3044                 np->n_flag |= NCHG;
3045                 if (vp->v_usecount == 1 &&
3046                     (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3047                         VATTR_NULL(&vattr);
3048                         if (np->n_flag & NACC)
3049                                 vattr.va_atime = np->n_atim;
3050                         if (np->n_flag & NUPD)
3051                                 vattr.va_mtime = np->n_mtim;
3052                         (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_td);
3053                 }
3054         }
3055         return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
3056 }
3057
3058 /*
3059  * Read wrapper for fifos.
3060  */
3061 static int
3062 nfsfifo_read(struct vop_read_args *ap)
3063 {
3064         struct nfsnode *np = VTONFS(ap->a_vp);
3065
3066         /*
3067          * Set access flag.
3068          */
3069         np->n_flag |= NACC;
3070         getnanotime(&np->n_atim);
3071         return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap));
3072 }
3073
3074 /*
3075  * Write wrapper for fifos.
3076  */
3077 static int
3078 nfsfifo_write(struct vop_write_args *ap)
3079 {
3080         struct nfsnode *np = VTONFS(ap->a_vp);
3081
3082         /*
3083          * Set update flag.
3084          */
3085         np->n_flag |= NUPD;
3086         getnanotime(&np->n_mtim);
3087         return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap));
3088 }
3089
3090 /*
3091  * Close wrapper for fifos.
3092  *
3093  * Update the times on the nfsnode then do fifo close.
3094  */
3095 static int
3096 nfsfifo_close(struct vop_close_args *ap)
3097 {
3098         struct vnode *vp = ap->a_vp;
3099         struct nfsnode *np = VTONFS(vp);
3100         struct vattr vattr;
3101         struct timespec ts;
3102
3103         if (np->n_flag & (NACC | NUPD)) {
3104                 getnanotime(&ts);
3105                 if (np->n_flag & NACC)
3106                         np->n_atim = ts;
3107                 if (np->n_flag & NUPD)
3108                         np->n_mtim = ts;
3109                 np->n_flag |= NCHG;
3110                 if (vp->v_usecount == 1 &&
3111                     (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3112                         VATTR_NULL(&vattr);
3113                         if (np->n_flag & NACC)
3114                                 vattr.va_atime = np->n_atim;
3115                         if (np->n_flag & NUPD)
3116                                 vattr.va_mtime = np->n_mtim;
3117                         (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_td);
3118                 }
3119         }
3120         return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));
3121 }
3122