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