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