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