]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/nfs4client/nfs4_vnops.c
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[FreeBSD/FreeBSD.git] / sys / nfs4client / nfs4_vnops.c
1 /* $Id: nfs_vnops.c,v 1.45 2003/11/05 14:59:02 rees Exp $ */
2
3 /*-
4  * copyright (c) 2003
5  * the regents of the university of michigan
6  * all rights reserved
7  * 
8  * permission is granted to use, copy, create derivative works and redistribute
9  * this software and such derivative works for any purpose, so long as the name
10  * of the university of michigan is not used in any advertising or publicity
11  * pertaining to the use or distribution of this software without specific,
12  * written prior authorization.  if the above copyright notice or any other
13  * identification of the university of michigan is included in any copy of any
14  * portion of this software, then the disclaimer below must also be included.
15  * 
16  * this software is provided as is, without representation from the university
17  * of michigan as to its fitness for any purpose, and without warranty by the
18  * university of michigan of any kind, either express or implied, including
19  * without limitation the implied warranties of merchantability and fitness for
20  * a particular purpose. the regents of the university of michigan shall not be
21  * liable for any damages, including special, indirect, incidental, or
22  * consequential damages, with respect to any claim arising out of or in
23  * connection with the use of the software, even if it has been or is hereafter
24  * advised of the possibility of such damages.
25  */
26
27 /*
28  * Copyright (c) 1989, 1993
29  *      The Regents of the University of California.  All rights reserved.
30  *
31  * This code is derived from software contributed to Berkeley by
32  * Rick Macklem at The University of Guelph.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions
36  * are met:
37  * 1. Redistributions of source code must retain the above copyright
38  *    notice, this list of conditions and the following disclaimer.
39  * 2. Redistributions in binary form must reproduce the above copyright
40  *    notice, this list of conditions and the following disclaimer in the
41  *    documentation and/or other materials provided with the distribution.
42  * 4. Neither the name of the University nor the names of its contributors
43  *    may be used to endorse or promote products derived from this software
44  *    without specific prior written permission.
45  *
46  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56  * SUCH DAMAGE.
57  *
58  *      @(#)nfs_vnops.c 8.16 (Berkeley) 5/27/95
59  */
60
61 #include <sys/cdefs.h>
62 __FBSDID("$FreeBSD$");
63
64 /*
65  * vnode op calls for Sun NFS version 2 and 3
66  */
67
68 #include "opt_inet.h"
69
70 #include <sys/param.h>
71 #include <sys/kernel.h>
72 #include <sys/systm.h>
73 #include <sys/resourcevar.h>
74 #include <sys/proc.h>
75 #include <sys/mount.h>
76 #include <sys/bio.h>
77 #include <sys/buf.h>
78 #include <sys/malloc.h>
79 #include <sys/mbuf.h>
80 #include <sys/namei.h>
81 #include <sys/socket.h>
82 #include <sys/vnode.h>
83 #include <sys/dirent.h>
84 #include <sys/fcntl.h>
85 #include <sys/lockf.h>
86 #include <sys/stat.h>
87 #include <sys/sysctl.h>
88 #include <sys/lockmgr.h>
89 #include <sys/signalvar.h>
90
91 #include <vm/vm.h>
92 #include <vm/vm_extern.h>
93
94 #include <fs/fifofs/fifo.h>
95
96 #include <rpc/rpcclnt.h>
97
98 #include <nfs/rpcv2.h>
99 #include <nfs/nfsproto.h>
100 #include <nfsclient/nfs.h>
101 #include <nfs4client/nfs4.h>
102 #include <nfsclient/nfsnode.h>
103 #include <nfsclient/nfsmount.h>
104 #include <nfsclient/nfs_lock.h>
105 #include <nfs/xdr_subs.h>
106 #include <nfsclient/nfsm_subs.h>
107
108 #include <net/if.h>
109 #include <netinet/in.h>
110 #include <netinet/in_var.h>
111
112 /* NFSv4 */
113 #include <nfs4client/nfs4m_subs.h>
114 #include <nfs4client/nfs4_vn.h>
115
116 /* Defs */
117 #define TRUE    1
118 #define FALSE   0
119
120 /*
121  * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these
122  * calls are not in getblk() and brelse() so that they would not be necessary
123  * here.
124  */
125 #ifndef B_VMIO
126 #define vfs_busy_pages(bp, f)
127 #endif
128
129 static int      nfs4_flush(struct vnode *, int, struct thread *,
130                     int);
131 static int      nfs4_setattrrpc(struct vnode *, struct vattr *, struct ucred *);
132 static int      nfs4_closerpc(struct vnode *, struct ucred *, int);
133
134 static vop_lookup_t     nfs4_lookup;
135 static vop_create_t     nfs4_create;
136 static vop_mknod_t      nfs4_mknod;
137 static vop_open_t       nfs4_open;
138 static vop_close_t      nfs4_close;
139 static vop_access_t     nfs4_access;
140 static vop_getattr_t    nfs4_getattr;
141 static vop_setattr_t    nfs4_setattr;
142 static vop_read_t       nfs4_read;
143 static vop_fsync_t      nfs4_fsync;
144 static vop_remove_t     nfs4_remove;
145 static vop_link_t       nfs4_link;
146 static vop_rename_t     nfs4_rename;
147 static vop_mkdir_t      nfs4_mkdir;
148 static vop_rmdir_t      nfs4_rmdir;
149 static vop_symlink_t    nfs4_symlink;
150 static vop_readdir_t    nfs4_readdir;
151 static vop_strategy_t   nfs4_strategy;
152 static  int     nfs4_lookitup(struct vnode *, const char *, int,
153                     struct ucred *, struct thread *, struct nfsnode **);
154 static  int     nfs4_sillyrename(struct vnode *, struct vnode *,
155                     struct componentname *);
156 static vop_readlink_t   nfs4_readlink;
157 static vop_print_t      nfs4_print;
158 static vop_advlock_t    nfs4_advlock;
159 static vop_advlockasync_t nfs4_advlockasync;
160
161 /*
162  * Global vfs data structures for nfs
163  */
164 struct vop_vector nfs4_vnodeops = {
165         .vop_default =          &default_vnodeops,
166         .vop_access =           nfs4_access,
167         .vop_advlock =          nfs4_advlock,
168         .vop_advlockasync =     nfs4_advlockasync,
169         .vop_close =            nfs4_close,
170         .vop_create =           nfs4_create,
171         .vop_fsync =            nfs4_fsync,
172         .vop_getattr =          nfs4_getattr,
173         .vop_getpages =         nfs_getpages,
174         .vop_putpages =         nfs_putpages,
175         .vop_inactive =         nfs_inactive,
176         .vop_lease =            VOP_NULL,
177         .vop_link =             nfs4_link,
178         .vop_lookup =           nfs4_lookup,
179         .vop_mkdir =            nfs4_mkdir,
180         .vop_mknod =            nfs4_mknod,
181         .vop_open =             nfs4_open,
182         .vop_print =            nfs4_print,
183         .vop_read =             nfs4_read,
184         .vop_readdir =          nfs4_readdir,
185         .vop_readlink =         nfs4_readlink,
186         .vop_reclaim =          nfs_reclaim,
187         .vop_remove =           nfs4_remove,
188         .vop_rename =           nfs4_rename,
189         .vop_rmdir =            nfs4_rmdir,
190         .vop_setattr =          nfs4_setattr,
191         .vop_strategy =         nfs4_strategy,
192         .vop_symlink =          nfs4_symlink,
193         .vop_write =            nfs_write,
194 };
195
196 static int      nfs4_removerpc(struct vnode *dvp, const char *name, int namelen,
197                               struct ucred *cred, struct thread *td);
198 static int      nfs4_renamerpc(struct vnode *fdvp, const char *fnameptr,
199                               int fnamelen, struct vnode *tdvp,
200                               const char *tnameptr, int tnamelen,
201                               struct ucred *cred, struct thread *td);
202 static int      nfs4_renameit(struct vnode *sdvp, struct componentname *scnp,
203                              struct sillyrename *sp);
204 static int      nfs4_openrpc(struct vnode *, struct vnode **,
205                             struct componentname *, int, struct vattr *);
206 static int      nfs4_open_confirm(struct vnode *vp, struct nfs4_compound *cpp,
207                                  struct nfs4_oparg_open *openap,
208                                  struct nfs4_oparg_getfh *gfh,
209                                  struct ucred *cred, struct thread *td);
210 static int      nfs4_createrpc(struct vnode *, struct vnode **,
211                               struct componentname *, nfstype,
212                               struct vattr *, char *);
213
214 /*
215  * Global variables
216  */
217 struct nfs4_lowner nfs4_masterlowner;
218
219 #define DIRHDSIZ        (sizeof (struct dirent) - (MAXNAMLEN + 1))
220
221 SYSCTL_DECL(_vfs_nfs4);
222
223 static int      nfs4_access_cache_timeout = NFS_MAXATTRTIMO;
224 SYSCTL_INT(_vfs_nfs4, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
225            &nfs4_access_cache_timeout, 0, "NFS ACCESS cache timeout");
226
227 #if 0
228 static int      nfsv3_commit_on_close = 0;
229 SYSCTL_INT(_vfs_nfs4, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
230            &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
231
232 SYSCTL_INT(_vfs_nfs4, OID_AUTO, access_cache_hits, CTLFLAG_RD,
233            &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
234
235 SYSCTL_INT(_vfs_nfs4, OID_AUTO, access_cache_misses, CTLFLAG_RD,
236            &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
237 #endif
238
239 #define NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY          \
240                          | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE     \
241                          | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
242 static int
243 nfs4_v3_access_otw(struct vnode *vp, int wmode, struct thread *td,
244     struct ucred *cred)
245 {
246         const int v3 = 1;
247         u_int32_t *tl;
248         int error = 0, attrflag;
249
250         return (0);
251
252         struct mbuf *mreq, *mrep = NULL, *md, *mb;
253         caddr_t bpos, dpos;
254         u_int32_t rmode;
255         struct nfsnode *np = VTONFS(vp);
256
257         nfsstats.rpccnt[NFSPROC_ACCESS]++;
258         mreq = nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
259         mb = mreq;
260         bpos = mtod(mb, caddr_t);
261         nfsm_fhtom(vp, v3);
262         tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
263         *tl = txdr_unsigned(wmode);
264         nfsm_request(vp, NFSPROC_ACCESS, td, cred);
265         nfsm_postop_attr(vp, attrflag);
266         if (!error) {
267                 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
268                 rmode = fxdr_unsigned(u_int32_t, *tl);
269                 np->n_mode = rmode;
270                 np->n_modeuid = cred->cr_uid;
271                 np->n_modestamp = time_second;
272         }
273         m_freem(mrep);
274 nfsmout:
275         return error;
276 }
277
278 /*
279  * nfs access vnode op.
280  * For nfs version 2, just return ok. File accesses may fail later.
281  * For nfs version 3, use the access rpc to check accessibility. If file modes
282  * are changed on the server, accesses might still fail later.
283  */
284 static int
285 nfs4_access(struct vop_access_args *ap)
286 {
287         struct vnode *vp = ap->a_vp;
288         int error = 0;
289         u_int32_t mode, wmode;
290         int v3 = NFS_ISV3(vp);  /* v3 \in v4 */
291         struct nfsnode *np = VTONFS(vp);
292         caddr_t bpos, dpos;
293         struct mbuf *mreq, *mrep = NULL, *md, *mb;
294         struct nfs4_compound cp;
295         struct nfs4_oparg_access acc;
296         struct thread *td = ap->a_td;
297         struct ucred *cred = ap->a_cred;
298
299         /*
300          * Disallow write attempts on filesystems mounted read-only;
301          * unless the file is a socket, fifo, or a block or character
302          * device resident on the filesystem.
303          */
304         if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
305                 switch (vp->v_type) {
306                 case VREG:
307                 case VDIR:
308                 case VLNK:
309                         return (EROFS);
310                 default:
311                         break;
312                 }
313         }
314         /*
315          * For nfs v3, check to see if we have done this recently, and if
316          * so return our cached result instead of making an ACCESS call.
317          * If not, do an access rpc, otherwise you are stuck emulating
318          * ufs_access() locally using the vattr. This may not be correct,
319          * since the server may apply other access criteria such as
320          * client uid-->server uid mapping that we do not know about.
321          */
322         /* XXX Disable this for now; needs fixing of _access_otw() */
323         if (0 && v3) {
324                 if (ap->a_mode & VREAD)
325                         mode = NFSV3ACCESS_READ;
326                 else
327                         mode = 0;
328                 if (vp->v_type != VDIR) {
329                         if (ap->a_mode & VWRITE)
330                                 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
331                         if (ap->a_mode & VEXEC)
332                                 mode |= NFSV3ACCESS_EXECUTE;
333                 } else {
334                         if (ap->a_mode & VWRITE)
335                                 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
336                                     NFSV3ACCESS_DELETE);
337                         if (ap->a_mode & VEXEC)
338                                 mode |= NFSV3ACCESS_LOOKUP;
339                 }
340                 /* XXX safety belt, only make blanket request if caching */
341                 if (nfs4_access_cache_timeout > 0) {
342                         wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
343                             NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
344                             NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
345                 } else {
346                         wmode = mode;
347                 }
348
349                 /*
350                  * Does our cached result allow us to give a definite yes to
351                  * this request?
352                  */
353                 if (time_second < np->n_modestamp + nfs4_access_cache_timeout &&
354                     ap->a_cred->cr_uid == np->n_modeuid &&
355                     (np->n_mode & mode) == mode) {
356                         nfsstats.accesscache_hits++;
357                 } else {
358                         /*
359                          * Either a no, or a don't know.  Go to the wire.
360                          */
361                         nfsstats.accesscache_misses++;
362                         error = nfs4_v3_access_otw(vp, wmode, ap->a_td,
363                             ap->a_cred);
364                         if (error == 0) {
365                                 if ((np->n_mode & mode) != mode)
366                                         error = EACCES;
367                         }
368                 }
369                 return (error);
370         }
371
372         /* XXX use generic access code here? */
373         mode = ap->a_mode & VREAD ? NFSV4ACCESS_READ : 0;
374         if (vp->v_type == VDIR) {
375                 if (ap->a_mode & VWRITE)
376                         mode |= NFSV4ACCESS_MODIFY | NFSV4ACCESS_EXTEND | NFSV4ACCESS_DELETE;
377                 if (ap->a_mode & VEXEC)
378                         mode |= NFSV4ACCESS_LOOKUP;
379         } else {
380                 if (ap->a_mode & VWRITE)
381                         mode |= NFSV4ACCESS_MODIFY | NFSV4ACCESS_EXTEND;
382                 if (ap->a_mode & VEXEC)
383                         mode |= NFSV4ACCESS_EXECUTE;
384         }
385
386         nfs_v4initcompound(&cp);
387         acc.mode = mode;
388
389         mreq = nfsm_reqhead(vp, NFSV4PROC_COMPOUND, 0);
390         mb = mreq;
391         bpos = mtod(mb, caddr_t);
392
393         nfsm_v4build_compound(&cp, "nfs4_access()");
394         nfsm_v4build_putfh(&cp, vp);
395         nfsm_v4build_access(&cp, &acc);
396         nfsm_v4build_finalize(&cp);
397
398         nfsm_request(vp, NFSV4PROC_COMPOUND, td, cred);
399         if (error != 0)
400                 goto nfsmout;
401
402         nfsm_v4dissect_compound(&cp);
403         nfsm_v4dissect_putfh(&cp);
404         nfsm_v4dissect_access(&cp, &acc);
405
406         if ((acc.rmode & mode) != mode)
407                 error = EACCES;
408
409  nfsmout:
410         error = nfs_v4postop(&cp, error);
411
412         if (mrep != NULL)
413                 m_freem(mrep);
414
415         return (error);
416 }
417
418 static int
419 nfs4_openrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
420     int flags, struct vattr *vap)
421 {
422         struct vnode *vp = *vpp;
423         struct nfs4_oparg_getattr getattr;
424         struct nfs4_oparg_getfh getfh;
425         struct nfs4_oparg_open opena;
426         struct nfs4_compound cp;
427         caddr_t bpos, dpos;
428         int error = 0;
429         struct mbuf *mreq, *mrep = NULL, *md, *mb;
430         struct ucred *cred = cnp->cn_cred;
431         struct thread *td = cnp->cn_thread;
432         struct nfs4_fctx xfc, *fcp;
433         struct nfsnode *np;
434
435         if (vp == NULL) {
436                 /* Create a new file */
437                 np = NULL;
438                 fcp = &xfc;
439                 bzero(fcp, sizeof(*fcp));
440         } else {
441                 np = VTONFS(vp);
442                 fcp = flags & FWRITE ? &np->n_wfc : &np->n_rfc;
443         }
444
445         /*
446          * Since we are currently only one lockowner; we only open the
447          * file once each for reading and writing.
448          */
449         if (fcp->refcnt++ != 0) {
450                 *vpp = vp;
451                 /*printf("not opening %s\n", np->n_name != NULL ? np->n_name : "");*/
452                 return (0);
453         }
454
455         fcp->lop = &nfs4_masterlowner;
456         fcp->np = np;
457
458         nfs_v4initcompound(&cp);
459         cp.nmp = VFSTONFS(dvp->v_mount);
460
461         opena.ctype = NCLNULL;
462         opena.flags = flags;
463         opena.vap = vap;
464         opena.fcp = fcp;                /* For lockowner */
465         opena.cnp = cnp;
466
467         getattr.bm = &nfsv4_getattrbm;
468
469         mreq = nfsm_reqhead(vp, NFSV4PROC_COMPOUND, 0);
470         mb = mreq;
471         bpos = mtod(mb, caddr_t);
472
473         nfsm_v4build_compound(&cp, "nfs4_openrpc()");
474         nfsm_v4build_putfh(&cp, dvp);
475         nfsm_v4build_open(&cp, &opena);
476         nfsm_v4build_getattr(&cp, &getattr);
477         nfsm_v4build_getfh(&cp, &getfh);
478         nfsm_v4build_finalize(&cp);
479
480         nfsm_request(vp != NULL ? vp : dvp, NFSV4PROC_COMPOUND, td, cred);
481         if (error != 0)
482                 goto nfsmout;
483
484         nfsm_v4dissect_compound(&cp);
485         nfsm_v4dissect_putfh(&cp);
486         nfsm_v4dissect_open(&cp, &opena);
487         nfsm_v4dissect_getattr(&cp, &getattr);
488         nfsm_v4dissect_getfh(&cp, &getfh);
489
490         error = nfs_v4postop(&cp, error);
491
492         if (opena.rflags & NFSV4OPENRES_CONFIRM) {
493                 error = nfs4_open_confirm(vp ? vp : dvp, &cp, &opena, &getfh, cred, td);
494                 if (error != 0)
495                         goto nfsmout;
496         }
497
498         if (vp == NULL) {
499                 /* New file */
500                 error = nfs_nget(dvp->v_mount, &getfh.fh_val,
501                                  getfh.fh_len, &np, LK_EXCLUSIVE);
502                 if (error != 0)
503                         goto nfsmout;
504
505                 vp = NFSTOV(np);
506                 np->n_dvp = dvp;
507                 np->n_namelen = cnp->cn_namelen; /* XXX memory leaks on these; track! */
508                 if (np->n_name != NULL)
509                         FREE(np->n_name, M_NFSREQ);
510                 MALLOC(np->n_name, u_char *, np->n_namelen + 1, M_NFSREQ, M_WAITOK);
511                 bcopy(cnp->cn_nameptr, np->n_name, np->n_namelen);
512                 np->n_name[np->n_namelen] = '\0';
513                 if (flags & FWRITE)
514                         np->n_wfc = *fcp;
515                 else
516                         np->n_rfc = *fcp;
517
518                 /*printf("opened new file %s\n", np->n_name);*/
519
520                 nfs4_vnop_loadattrcache(vp, &getattr.fa, NULL);
521                 *vpp = vp;
522         } else {
523                 /*printf("openend \"old\" %s\n", np->n_name != NULL ? np->n_name : "");*/
524
525                 if (flags & O_TRUNC && np->n_size != 0) {
526                         struct vattr va;
527
528                         VATTR_NULL(&va);
529                         va.va_size = 0;
530                         error = nfs4_setattrrpc(vp, &va, cnp->cn_cred);
531                 }
532                 np->n_attrstamp = 0;
533         }
534
535  nfsmout:
536         if (mrep != NULL)
537                 m_freem(mrep);
538
539         return (error);
540 }
541
542 static int
543 nfs4_open_confirm(struct vnode *vp, struct nfs4_compound *cpp,
544     struct nfs4_oparg_open *openap, struct nfs4_oparg_getfh *gfh,
545     struct ucred *cred, struct thread *td)
546 {
547         caddr_t bpos, dpos;
548         int error = 0;
549         struct mbuf *mreq, *mrep = NULL, *md, *mb;
550
551         nfs_v4initcompound(cpp);
552         cpp->nmp = VFSTONFS(vp->v_mount);
553
554         mreq = nfsm_reqhead(vp, NFSV4PROC_COMPOUND, 0);
555         mb = mreq;
556         bpos = mtod(mb, caddr_t);
557
558         nfsm_v4build_compound(cpp, "nfs4_open_confirm()");
559         nfsm_v4build_putfh_nv(cpp, gfh);
560         nfsm_v4build_open_confirm(cpp, openap);
561         nfsm_v4build_finalize(cpp);
562
563         nfsm_request(vp, NFSV4PROC_COMPOUND, td, cred);
564         if (error != 0)
565                 goto nfsmout;
566
567         nfsm_v4dissect_compound(cpp);
568         nfsm_v4dissect_putfh(cpp);
569         nfsm_v4dissect_open_confirm(cpp, openap);
570
571  nfsmout:
572         error = nfs_v4postop(cpp, error);
573
574         if (mrep != NULL)
575                 m_freem(mrep);
576
577         return (error);
578 }
579
580
581 /*
582  * nfs open vnode op
583  * Check to see if the type is ok
584  * and that deletion is not in progress.
585  * For paged in text files, you will need to flush the page cache
586  * if consistency is lost.
587  */
588 /* ARGSUSED */
589 static int
590 nfs4_open(struct vop_open_args *ap)
591 {
592         struct vnode *vp = ap->a_vp;
593         struct nfsnode *np = VTONFS(vp);
594         enum vtype vtype = vp->v_type;
595         int mode = ap->a_mode;
596         struct componentname cn;
597
598         if (vtype != VREG) {
599                 if (vtype != VDIR && vtype != VLNK) {
600 #ifdef DIAGNOSTIC
601                         printf("open eacces vtyp=%d\n", vp->v_type);
602 #endif
603                         return (EACCES);
604                 } else
605                         return (0);
606         }
607
608         if (np->n_flag & NCREATED) {
609                 np->n_flag &= ~NCREATED;
610                 return (0);
611         }
612
613         cn.cn_nameptr = np->n_name;
614         cn.cn_namelen = np->n_namelen;
615         cn.cn_cred = ap->a_cred;
616         cn.cn_thread = ap->a_td;
617
618         return (nfs4_openrpc(np->n_dvp, &vp, &cn, mode, NULL));
619 }
620
621 static int
622 nfs4_closerpc(struct vnode *vp, struct ucred *cred, int flags)
623 {
624         caddr_t bpos, dpos;
625         int error = 0;
626         struct mbuf *mreq, *mrep = NULL, *md, *mb;
627         struct thread *td;
628         struct nfs4_fctx *fcp;
629         struct nfs4_compound cp;
630         struct nfsnode *np = VTONFS(vp);
631
632         td = curthread;
633         fcp = flags & FWRITE ? &np->n_wfc : &np->n_rfc;
634
635         nfs_v4initcompound(&cp);
636
637         if (--fcp->refcnt != 0)
638                 return (0);
639
640         /*printf("closing %s\n", np->n_name != NULL ? np->n_name : "");*/
641
642         cp.fcp = fcp;
643
644         mreq = nfsm_reqhead(vp, NFSV4PROC_COMPOUND, 0);
645         mb = mreq;
646         bpos = mtod(mb, caddr_t);
647
648         nfsm_v4build_compound(&cp, "nfs4_closerpc()");
649         nfsm_v4build_putfh(&cp, vp);
650         nfsm_v4build_close(&cp, fcp);
651         nfsm_v4build_finalize(&cp);
652
653         nfsm_request(vp, NFSV4PROC_COMPOUND, td, cred);
654         if (error != 0)
655                 goto nfsmout;
656
657         nfsm_v4dissect_compound(&cp);
658         nfsm_v4dissect_putfh(&cp);
659         nfsm_v4dissect_close(&cp, fcp);
660
661  nfsmout:
662         error = nfs_v4postop(&cp, error);
663
664         if (mrep != NULL)
665                 m_freem(mrep);
666
667         return (error);
668 }
669
670 /*
671  * nfs close vnode op
672  * play it safe for now (see comments in v2/v3 nfs_close regarding dirty buffers)
673  */
674 /* ARGSUSED */
675 static int
676 nfs4_close(struct vop_close_args *ap)
677 {
678         struct vnode *vp = ap->a_vp;
679         struct nfsnode *np = VTONFS(vp);
680         int error = 0;
681
682         if (vp->v_type != VREG)
683                 return (0);
684
685         if (np->n_flag & NMODIFIED) {
686                 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
687                 np->n_attrstamp = 0;
688         }
689
690         error = nfs4_closerpc(vp, ap->a_cred, ap->a_fflag);
691
692         if (!error && np->n_flag & NWRITEERR) {
693                 np->n_flag &= ~NWRITEERR;
694                 error = np->n_error;
695         }
696         return (error);
697 }
698
699 /*
700  * nfs getattr call from vfs.
701  */
702 static int
703 nfs4_getattr(struct vop_getattr_args *ap)
704 {
705         struct vnode *vp = ap->a_vp;
706         struct nfsnode *np = VTONFS(vp);
707         caddr_t bpos, dpos;
708         int error = 0;
709         struct mbuf *mreq, *mrep = NULL, *md, *mb;
710         struct nfs4_oparg_getattr ga;
711         struct nfs4_compound cp;
712
713         /*
714          * Update local times for special files.
715          */
716         if (np->n_flag & (NACC | NUPD))
717                 np->n_flag |= NCHG;
718         /*
719          * First look in the cache.
720          */
721         if (nfs_getattrcache(vp, ap->a_vap) == 0)
722                 return (0);
723
724         nfsstats.rpccnt[NFSPROC_GETATTR]++;
725
726         mreq = nfsm_reqhead(vp, NFSV4PROC_COMPOUND, NFSX_FH(1));
727         mb = mreq;
728         bpos = mtod(mb, caddr_t);
729
730         ga.bm = &nfsv4_getattrbm;
731         nfs_v4initcompound(&cp);
732
733         nfsm_v4build_compound(&cp, "nfs4_getattr()");
734         nfsm_v4build_putfh(&cp, vp);
735         nfsm_v4build_getattr(&cp, &ga);
736         nfsm_v4build_finalize(&cp);
737
738         nfsm_request(vp, NFSV4PROC_COMPOUND, curthread, ap->a_cred);
739         if (error != 0)
740                 goto nfsmout;
741
742         nfsm_v4dissect_compound(&cp);
743         nfsm_v4dissect_putfh(&cp);
744         nfsm_v4dissect_getattr(&cp, &ga);
745
746         nfs4_vnop_loadattrcache(vp, &ga.fa, ap->a_vap);
747
748 nfsmout:
749         error = nfs_v4postop(&cp, error);
750
751         if (mrep != NULL)
752                 m_freem(mrep);
753         return (error);
754 }
755
756 /*
757  * nfs setattr call.
758  */
759 static int
760 nfs4_setattr(struct vop_setattr_args *ap)
761 {
762         struct vnode *vp = ap->a_vp;
763         struct nfsnode *np = VTONFS(vp);
764         struct vattr *vap = ap->a_vap;
765         struct thread *td = curthread;
766         int error = 0;
767         u_quad_t tsize;
768
769 #ifndef nolint
770         tsize = (u_quad_t)0;
771 #endif
772
773         /*
774          * Setting of flags is not supported.
775          */
776         if (vap->va_flags != VNOVAL)
777                 return (EOPNOTSUPP);
778
779         /*
780          * Disallow write attempts if the filesystem is mounted read-only.
781          */
782         if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
783             vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
784             vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
785             (vp->v_mount->mnt_flag & MNT_RDONLY))
786                 return (EROFS);
787         if (vap->va_size != VNOVAL) {
788                 switch (vp->v_type) {
789                 case VDIR:
790                         return (EISDIR);
791                 case VCHR:
792                 case VBLK:
793                 case VSOCK:
794                 case VFIFO:
795                         if (vap->va_mtime.tv_sec == VNOVAL &&
796                             vap->va_atime.tv_sec == VNOVAL &&
797                             vap->va_mode == (mode_t)VNOVAL &&
798                             vap->va_uid == (uid_t)VNOVAL &&
799                             vap->va_gid == (gid_t)VNOVAL)
800                                 return (0);
801                         vap->va_size = VNOVAL;
802                         break;
803                 default:
804                         /*
805                          * Disallow write attempts if the filesystem is
806                          * mounted read-only.
807                          */
808                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
809                                 return (EROFS);
810
811                         /*
812                          *  We run vnode_pager_setsize() early (why?),
813                          * we must set np->n_size now to avoid vinvalbuf
814                          * V_SAVE races that might setsize a lower
815                          * value.
816                          */
817
818                         tsize = np->n_size;
819                         error = nfs_meta_setsize(vp, ap->a_cred, td,
820                             vap->va_size);
821
822                         if (np->n_flag & NMODIFIED) {
823                             if (vap->va_size == 0)
824                                 error = nfs_vinvalbuf(vp, 0, td, 1);
825                             else
826                                 error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
827                             if (error) {
828                                 vnode_pager_setsize(vp, np->n_size);
829                                 return (error);
830                             }
831                         }
832                         /*
833                          * np->n_size has already been set to vap->va_size
834                          * in nfs_meta_setsize(). We must set it again since
835                          * nfs_loadattrcache() could be called through
836                          * nfs_meta_setsize() and could modify np->n_size.
837                          */
838                         np->n_vattr.va_size = np->n_size = vap->va_size;
839                 };
840         } else if ((vap->va_mtime.tv_sec != VNOVAL ||
841                 vap->va_atime.tv_sec != VNOVAL) && (np->n_flag & NMODIFIED) &&
842                 vp->v_type == VREG &&
843                 (error = nfs_vinvalbuf(vp, V_SAVE, td, 1)) == EINTR)
844                 return (error);
845
846         if (vap->va_size != VNOVAL && np->n_wfc.refcnt == 0) {
847                 /* Have to open the file before we can truncate it */
848                 struct componentname cn;
849
850                 cn.cn_nameptr = np->n_name;
851                 cn.cn_namelen = np->n_namelen;
852                 cn.cn_cred = ap->a_cred;
853                 cn.cn_thread = td;
854                 error = nfs4_openrpc(np->n_dvp, &vp, &cn, FWRITE, NULL);
855                 if (error)
856                         return error;
857                 np->n_flag |= NTRUNCATE;
858         }
859
860         error = nfs4_setattrrpc(vp, vap, ap->a_cred);
861         if (error && vap->va_size != VNOVAL) {
862                 np->n_size = np->n_vattr.va_size = tsize;
863                 vnode_pager_setsize(vp, np->n_size);
864         }
865         return (error);
866 }
867
868 /*
869  * Do an nfs setattr rpc.
870  */
871 static int
872 nfs4_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred)
873 {
874         caddr_t bpos, dpos;
875         int error = 0;
876         struct mbuf *mreq, *mrep = NULL, *md, *mb;
877         struct thread *td;
878         struct nfs4_compound cp;
879         struct nfs4_oparg_getattr ga;
880         struct nfsnode *np = VTONFS(vp);
881         struct nfs4_fctx *fcp;
882
883         td = curthread;
884         nfsstats.rpccnt[NFSPROC_SETATTR]++;
885         mreq = nfsm_reqhead(vp, NFSV4PROC_COMPOUND, 0);
886         mb = mreq;
887         bpos = mtod(mb, caddr_t);
888
889         ga.bm = &nfsv4_getattrbm;
890         fcp = (vap->va_size != VNOVAL) ? &np->n_wfc : NULL;
891         nfs_v4initcompound(&cp);
892
893         nfsm_v4build_compound(&cp, "nfs4_setattrrpc");
894         nfsm_v4build_putfh(&cp, vp);
895         nfsm_v4build_setattr(&cp, vap, fcp);
896         nfsm_v4build_getattr(&cp, &ga);
897         nfsm_v4build_finalize(&cp);
898
899         nfsm_request(vp, NFSV4PROC_COMPOUND, td, cred);
900         if (error != 0)
901                 goto nfsmout;
902
903         nfsm_v4dissect_compound(&cp);
904         nfsm_v4dissect_putfh(&cp);
905         nfsm_v4dissect_setattr(&cp);
906         nfsm_v4dissect_getattr(&cp, &ga);
907
908         nfs4_vnop_loadattrcache(vp, &ga.fa, NULL);
909
910         /* TODO: do the settatr and close in a single compound rpc */
911         if (np->n_flag & NTRUNCATE) {
912                 error = nfs4_closerpc(vp, cred, FWRITE);
913                 np->n_flag &= ~NTRUNCATE;
914         }
915
916 nfsmout:
917         error = nfs_v4postop(&cp, error);
918
919         if (mrep != NULL)
920                 m_freem(mrep);
921         
922         return (error);
923 }
924
925 /*
926  * nfs lookup call, one step at a time...
927  * First look in cache
928  * If not found, unlock the directory nfsnode and do the rpc
929  */
930 static int
931 nfs4_lookup(struct vop_lookup_args *ap)
932 {
933         struct componentname *cnp = ap->a_cnp;
934         struct vnode *dvp = ap->a_dvp;
935         struct vnode **vpp = ap->a_vpp;
936         int isdot, flags = cnp->cn_flags;
937         struct vnode *newvp;
938         struct nfsmount *nmp;
939         caddr_t bpos, dpos;
940         struct mbuf *mreq, *mrep = NULL, *md, *mb;
941         long len;
942         nfsfh_t *fhp;
943         struct nfsnode *np;
944         int error = 0, fhsize;
945         struct thread *td = cnp->cn_thread;
946         struct nfs4_compound cp;
947         struct nfs4_oparg_getattr ga, dga;
948         struct nfs4_oparg_lookup l;
949         struct nfs4_oparg_getfh gfh;
950
951         *vpp = NULLVP;
952         if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
953             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
954                 return (EROFS);
955         if (dvp->v_type != VDIR)
956                 return (ENOTDIR);
957         nmp = VFSTONFS(dvp->v_mount);
958         np = VTONFS(dvp);
959
960         isdot = cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.';
961
962         if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0) {
963                 *vpp = NULLVP;
964                 return (error);
965         }
966         if ((error = cache_lookup(dvp, vpp, cnp)) && error != ENOENT) {
967                 struct vattr vattr;
968
969                 newvp = *vpp;
970                 if (!VOP_GETATTR(newvp, &vattr, cnp->cn_cred)
971                  && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
972                      nfsstats.lookupcache_hits++;
973                      if (cnp->cn_nameiop != LOOKUP &&
974                          (flags & ISLASTCN))
975                              cnp->cn_flags |= SAVENAME;
976                      return (0);
977                 }
978                 cache_purge(newvp);
979                 if (newvp != dvp)
980                         vput(newvp);
981                 else
982                         vrele(newvp);
983         }
984
985         error = 0;
986         newvp = NULLVP;
987         nfsstats.lookupcache_misses++;
988         nfsstats.rpccnt[NFSPROC_LOOKUP]++;
989
990         len = cnp->cn_namelen;
991         mreq = nfsm_reqhead(NULL, NFSV4PROC_COMPOUND, 0);
992         mb = mreq;
993         bpos = mtod(mb, caddr_t);
994
995         ga.bm = &nfsv4_getattrbm;
996         dga.bm = &nfsv4_getattrbm;
997         nfs_v4initcompound(&cp);
998
999         nfsm_v4build_compound(&cp, "nfs4_lookup()");
1000         nfsm_v4build_putfh(&cp, dvp);
1001         nfsm_v4build_getattr(&cp, &dga);
1002         if (flags & ISDOTDOT)
1003                 nfsm_v4build_lookupp(&cp);
1004         else if (!isdot) {
1005                 l.name = cnp->cn_nameptr;
1006                 l.namelen = len;
1007                 nfsm_v4build_lookup(&cp, &l);
1008         }
1009         nfsm_v4build_getattr(&cp, &ga);
1010         nfsm_v4build_getfh(&cp, &gfh);
1011         nfsm_v4build_finalize(&cp);
1012
1013         nfsm_request(dvp, NFSV4PROC_COMPOUND, cnp->cn_thread, cnp->cn_cred);
1014         if (error != 0)
1015                 goto nfsmout;
1016
1017         nfsm_v4dissect_compound(&cp);
1018         nfsm_v4dissect_putfh(&cp);
1019         nfsm_v4dissect_getattr(&cp, &dga);
1020         if (flags & ISDOTDOT)
1021                 nfsm_v4dissect_lookupp(&cp);
1022         else if (!isdot)
1023                 nfsm_v4dissect_lookup(&cp);
1024         nfsm_v4dissect_getattr(&cp, &ga);
1025         nfsm_v4dissect_getfh(&cp, &gfh);
1026
1027         nfs4_vnop_loadattrcache(dvp, &dga.fa, NULL);
1028         fhp = &gfh.fh_val;
1029         fhsize = gfh.fh_len;
1030
1031         /*
1032          * Handle RENAME case...
1033          */
1034         if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
1035                 if (NFS_CMPFH(np, fhp, fhsize))
1036                         return (EISDIR);
1037
1038                 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, LK_EXCLUSIVE);
1039                 if (error)
1040                         return (error);
1041
1042                 newvp = NFSTOV(np);
1043
1044                 nfs4_vnop_loadattrcache(newvp, &ga.fa, NULL);
1045
1046                 *vpp = newvp;
1047                 cnp->cn_flags |= SAVENAME;
1048                 return (0);
1049         }
1050
1051         if (flags & ISDOTDOT) {
1052                 VOP_UNLOCK(dvp, 0);
1053
1054                 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, LK_EXCLUSIVE);
1055                 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1056                 if (error)
1057                         return (error);
1058                 newvp = NFSTOV(np);
1059
1060                 nfs4_vnop_loadattrcache(newvp, &ga.fa, NULL);
1061         } else if (NFS_CMPFH(np, fhp, fhsize)) {
1062                 VREF(dvp);
1063                 newvp = dvp;
1064         } else {
1065                 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, LK_EXCLUSIVE);
1066                 if (error)
1067                         return (error);
1068                 newvp = NFSTOV(np);
1069
1070                 /* Fill in np used by open. */
1071                 np->n_dvp = dvp;
1072                 np->n_namelen = cnp->cn_namelen;
1073                 if (np->n_name != NULL)
1074                         FREE(np->n_name, M_NFSREQ);
1075                 MALLOC(np->n_name, u_char *, np->n_namelen + 1, M_NFSREQ, M_WAITOK);
1076                 bcopy(cnp->cn_nameptr, np->n_name, np->n_namelen);
1077                 np->n_name[np->n_namelen] = '\0';
1078
1079                 nfs4_vnop_loadattrcache(newvp, &ga.fa, NULL);
1080         }
1081
1082         if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1083                 cnp->cn_flags |= SAVENAME;
1084         if ((cnp->cn_flags & MAKEENTRY) &&
1085             (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) {
1086                 np->n_ctime = np->n_vattr.va_ctime.tv_sec;
1087                 cache_enter(dvp, newvp, cnp);
1088         }
1089         *vpp = newvp;
1090         m_freem(mrep);
1091 nfsmout:
1092         error = nfs_v4postop(&cp, error);
1093
1094         if (error) {
1095                 if (newvp != NULLVP) {
1096                         vrele(newvp);
1097                         *vpp = NULLVP;
1098                 }
1099                 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1100                     (flags & ISLASTCN) && error == ENOENT) {
1101                         if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1102                                 error = EROFS;
1103                         else
1104                                 error = EJUSTRETURN;
1105                 }
1106                 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1107                         cnp->cn_flags |= SAVENAME;
1108         }
1109
1110         return (error);
1111 }
1112
1113 /*
1114  * nfs read call.
1115  * Just call nfs_bioread() to do the work.
1116  */
1117 static int
1118 nfs4_read(struct vop_read_args *ap)
1119 {
1120         struct vnode *vp = ap->a_vp;
1121
1122         switch (vp->v_type) {
1123         case VREG:
1124                 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1125         case VDIR:
1126                 return (EISDIR);
1127         default:
1128                 return (EOPNOTSUPP);
1129         }
1130 }
1131
1132 /*
1133  * nfs readlink call
1134  */
1135 static int
1136 nfs4_readlink(struct vop_readlink_args *ap)
1137 {
1138         struct vnode *vp = ap->a_vp;
1139
1140         if (vp->v_type != VLNK)
1141                 return (EINVAL);
1142         return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred));
1143 }
1144
1145 /*
1146  * Do a readlink rpc.
1147  * Called by nfs_doio() from below the buffer cache.
1148  */
1149 int
1150 nfs4_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1151 {
1152         caddr_t bpos, dpos;
1153         int error = 0;
1154         struct mbuf *mreq, *mrep = NULL, *md, *mb;
1155         struct nfs4_compound cp;
1156
1157         nfsstats.rpccnt[NFSPROC_READLINK]++;
1158
1159         mreq = nfsm_reqhead(vp, NFSV4PROC_COMPOUND, 0);
1160         mb = mreq;
1161         bpos = mtod(mb, caddr_t);
1162
1163         nfs_v4initcompound(&cp);
1164
1165         nfsm_v4build_compound(&cp, "nfs4_readlinkrpc()");
1166         nfsm_v4build_putfh(&cp, vp);
1167         nfsm_v4build_readlink(&cp);
1168         nfsm_v4build_finalize(&cp);
1169
1170         nfsm_request(vp, NFSV4PROC_COMPOUND, uiop->uio_td, cred);
1171         if (error != 0)
1172                 goto nfsmout;
1173
1174         nfsm_v4dissect_compound(&cp);
1175         nfsm_v4dissect_putfh(&cp);
1176         nfsm_v4dissect_readlink(&cp, uiop);
1177
1178 nfsmout:
1179         error = nfs_v4postop(&cp, error);
1180
1181         if (mrep != NULL)
1182                 m_freem(mrep);
1183         return (error);
1184 }
1185
1186 /*
1187  * nfs read rpc call
1188  * Ditto above
1189  */
1190 int
1191 nfs4_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1192 {
1193         caddr_t bpos, dpos;
1194         struct mbuf *mreq, *mrep = NULL, *md, *mb;
1195         struct nfsmount *nmp;
1196         int error = 0, len, tsiz;
1197         struct nfs4_compound cp;
1198         struct nfs4_oparg_read read;
1199         struct nfsnode *np = VTONFS(vp);
1200
1201         nmp = VFSTONFS(vp->v_mount);
1202         tsiz = uiop->uio_resid;
1203         if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1204                 return (EFBIG);
1205
1206         if (tsiz == 0)
1207                 return (0);
1208
1209         read.uiop = uiop;
1210         read.fcp = np->n_rfc.refcnt > 0 ? &np->n_rfc : &np->n_wfc;
1211
1212         while (tsiz > 0) {
1213                 nfsstats.rpccnt[NFSPROC_READ]++;
1214                 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1215
1216                 read.off = uiop->uio_offset;
1217                 read.maxcnt = len;
1218                 nfs_v4initcompound(&cp);
1219
1220                 mreq = nfsm_reqhead(vp, NFSV4PROC_COMPOUND, 0);
1221                 mb = mreq;
1222                 bpos = mtod(mb, caddr_t);
1223
1224                 nfsm_v4build_compound(&cp, "nfs4_readrpc()");
1225                 nfsm_v4build_putfh(&cp, vp);
1226                 nfsm_v4build_read(&cp, &read);
1227                 nfsm_v4build_finalize(&cp);
1228
1229                 nfsm_request(vp, NFSV4PROC_COMPOUND, uiop->uio_td, cred);
1230                 if (error != 0) {
1231                         error = nfs_v4postop(&cp, error);
1232                         goto nfsmout;
1233                 }
1234
1235                 nfsm_v4dissect_compound(&cp);
1236                 nfsm_v4dissect_putfh(&cp);
1237                 nfsm_v4dissect_read(&cp, &read);
1238
1239                 if (read.eof || read.retlen == 0)
1240                         tsiz = 0;
1241                 else
1242                         tsiz -= read.retlen;
1243
1244                 error = nfs_v4postop(&cp, error);
1245
1246                 m_freem(mrep);
1247                 mrep = NULL;
1248         }
1249 nfsmout:
1250         if (mrep != NULL)
1251                 m_freem(mrep);
1252         
1253         return (error);
1254 }
1255
1256 /*
1257  * nfs write call
1258  */
1259 int
1260 nfs4_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1261     int *iomode, int *must_commit)
1262 {
1263         int32_t backup;
1264         caddr_t bpos, dpos;
1265         struct mbuf *mreq, *mrep = NULL, *md, *mb;
1266         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1267         int error = 0, len, tsiz, wccflag = 1, rlen;
1268         struct nfs4_compound cp;
1269         struct nfs4_oparg_write write;
1270         nfsv4stablehow commit, committed = NSHFILESYNC;
1271         caddr_t verf;
1272         struct nfsnode *np = VTONFS(vp);
1273
1274 #ifndef DIAGNOSTIC
1275         if (uiop->uio_iovcnt != 1)
1276                 panic("nfs: writerpc iovcnt > 1");
1277 #endif
1278         *must_commit = 0;
1279         tsiz = uiop->uio_resid;
1280         if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1281                 return (EFBIG);
1282
1283         if (tsiz == 0)
1284                 return (0);
1285
1286         write.stable = (nfsv4stablehow)*iomode;
1287         write.uiop = uiop;
1288         write.fcp = &np->n_wfc;
1289
1290         while (tsiz > 0) {
1291                 nfsstats.rpccnt[NFSPROC_WRITE]++;
1292                 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1293
1294                 write.off = uiop->uio_offset;
1295                 write.cnt = len;
1296                 nfs_v4initcompound(&cp);
1297
1298                 mreq = nfsm_reqhead(vp, NFSV4PROC_COMPOUND, 0);
1299                 mb = mreq;
1300                 bpos = mtod(mb, caddr_t);
1301
1302                 nfsm_v4build_compound(&cp, "nfs4_writerpc()");
1303                 nfsm_v4build_putfh(&cp, vp);
1304                 nfsm_v4build_write(&cp, &write);
1305                 nfsm_v4build_finalize(&cp);
1306
1307                 nfsm_request(vp, NFSV4PROC_COMPOUND, uiop->uio_td, cred);
1308                 if (error != 0) {
1309                         error = nfs_v4postop(&cp, error);
1310                         goto nfsmout;
1311                 }
1312
1313                 nfsm_v4dissect_compound(&cp);
1314                 nfsm_v4dissect_putfh(&cp);
1315                 nfsm_v4dissect_write(&cp, &write);
1316
1317                 rlen = write.retlen;
1318                 if (rlen == 0) {
1319                         error = NFSERR_IO;
1320                         break;
1321                 } else if (rlen < len) {
1322                         backup = len - rlen;
1323                         uiop->uio_iov->iov_base =
1324                             (char *)uiop->uio_iov->iov_base -  backup;
1325                         uiop->uio_iov->iov_len += backup;
1326                         uiop->uio_offset -= backup;
1327                         uiop->uio_resid += backup;
1328                         len = rlen;
1329                 }
1330
1331                 commit = write.committed;
1332
1333                 if (committed == NSHFILESYNC ||
1334                     (committed = NSHDATASYNC && commit == NSHUNSTABLE))
1335                         committed = commit;
1336
1337                 verf = (caddr_t)write.wverf;
1338
1339                 if ((nmp->nm_flag & NFSSTA_HASWRITEVERF) == 0) {
1340                         bcopy(verf, nmp->nm_verf, NFSX_V4VERF);
1341                         nmp->nm_flag |= NFSMNT_HASWRITEVERF;
1342                 } else if (bcmp(verf, nmp->nm_verf, NFSX_V4VERF)) {
1343                         *must_commit = 1;
1344                         bcopy(verf, nmp->nm_verf, NFSX_V4VERF);
1345                 }
1346
1347                 /* XXX wccflag */
1348                 if (wccflag)
1349                         VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime;
1350
1351                 error = nfs_v4postop(&cp, error);
1352
1353                 m_freem(mrep);
1354                 mrep = NULL;
1355                 if (error)
1356                         break;
1357                 tsiz -= len;
1358         }
1359 nfsmout:
1360         if (mrep != NULL)
1361                 m_freem(mrep);
1362         *iomode = committed;
1363         if (error)
1364                 uiop->uio_resid = tsiz;
1365         return (error);
1366 }
1367
1368 /* ARGSUSED */
1369 static int
1370 nfs4_mknod(struct vop_mknod_args *ap)
1371 {
1372         struct vattr *vap = ap->a_vap;
1373         struct vnode *newvp = NULL;
1374         int error;
1375
1376         error = nfs4_createrpc(ap->a_dvp, &newvp,
1377             ap->a_cnp, (nfstype)vap->va_type, vap, NULL);
1378
1379         /* XXX - is this actually referenced here? */
1380         if (error == 0) {
1381                 *ap->a_vpp = newvp;
1382                 vrele(newvp);
1383         }
1384
1385         return (error);
1386 }
1387
1388 static int
1389 nfs4_createrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1390     nfstype ftype, struct vattr *vap, char *linktarget)
1391 {
1392         struct nfsnode *dnp = VTONFS(dvp);
1393         struct nfsnode *np = NULL;
1394         struct vnode *newvp = NULL;
1395         struct nfs4_compound cp;
1396         struct nfs4_oparg_create c;
1397         struct nfs4_oparg_getattr ga;
1398         struct nfs4_oparg_getfh gfh;
1399         caddr_t bpos, dpos;
1400         struct mbuf *mreq, *mrep = NULL, *md, *mb;
1401         int error = 0;
1402
1403         nfsstats.rpccnt[NFSPROC_CREATE]++;
1404
1405         mreq = nfsm_reqhead(dvp, NFSV4PROC_COMPOUND, 0);
1406         mb = mreq;
1407         bpos = mtod(mb, caddr_t);
1408
1409         bzero(&c, sizeof(c));
1410         bzero(&ga, sizeof(ga));
1411
1412         c.type = ftype;
1413         c.vap = vap;
1414         c.linktext = linktarget;
1415         c.name = cnp->cn_nameptr;
1416         c.namelen = cnp->cn_namelen;
1417
1418         ga.bm = &nfsv4_getattrbm;
1419         nfs_v4initcompound(&cp);
1420
1421         nfsm_v4build_compound(&cp, "nfs4_createrpc()");
1422         nfsm_v4build_putfh(&cp, dvp);
1423         nfsm_v4build_create(&cp, &c);
1424         nfsm_v4build_getattr(&cp, &ga);
1425         nfsm_v4build_getfh(&cp, &gfh);  
1426         nfsm_v4build_finalize(&cp); 
1427
1428         nfsm_request(dvp, NFSV4PROC_COMPOUND, cnp->cn_thread, cnp->cn_cred);
1429         if (error != 0)
1430                 goto nfsmout;
1431
1432         nfsm_v4dissect_compound(&cp);
1433         nfsm_v4dissect_putfh(&cp);
1434         nfsm_v4dissect_create(&cp, &c);
1435         nfsm_v4dissect_getattr(&cp, &ga);
1436         nfsm_v4dissect_getfh(&cp, &gfh);        
1437         
1438         error = nfs_nget(dvp->v_mount, &gfh.fh_val, gfh.fh_len, &np, LK_EXCLUSIVE);
1439         if (error != 0)
1440                 goto nfsmout;
1441
1442         newvp = NFSTOV(np);
1443         nfs4_vnop_loadattrcache(newvp, &ga.fa, NULL);
1444
1445         if (cnp->cn_flags & MAKEENTRY)
1446                 cache_enter(dvp, newvp, cnp);
1447
1448         dnp->n_flag |= NMODIFIED;
1449         dnp->n_attrstamp = 0;
1450
1451  nfsmout:
1452         error = nfs_v4postop(&cp, error);
1453
1454         if (mrep != NULL)
1455                 m_freem(mrep);
1456
1457         /* XXX */
1458         /*FREE(cnp->cn_pnbuf, M_NAMEI);*/
1459         if (error != 0 && newvp != NULL)
1460                 vput(newvp);
1461         else if (error == 0)
1462                 *vpp = newvp;
1463
1464         return (error);
1465 }
1466
1467 static int
1468 nfs4_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1469     struct vnode *tdvp, const char *tnameptr, int tnamelen,
1470     struct ucred *cred, struct thread *td)
1471 {
1472
1473         struct nfsnode *fnp = VTONFS(fdvp), *tnp = VTONFS(tdvp);
1474         caddr_t bpos, dpos;
1475         struct mbuf *mreq, *mrep = NULL, *md, *mb;
1476         struct nfs4_compound cp;
1477         struct nfs4_oparg_rename r;
1478         int error = 0;
1479
1480         nfsstats.rpccnt[NFSPROC_RENAME]++;
1481
1482         r.fname = fnameptr;
1483         r.fnamelen = fnamelen;
1484         r.tname = tnameptr;
1485         r.tnamelen = tnamelen;
1486         nfs_v4initcompound(&cp);
1487
1488         mreq = nfsm_reqhead(fdvp, NFSV4PROC_COMPOUND, 0);
1489         mb = mreq;
1490         bpos = mtod(mb, caddr_t);
1491
1492         nfsm_v4build_compound(&cp, "nfs4_renamerpc()");
1493         nfsm_v4build_putfh(&cp, fdvp);
1494         nfsm_v4build_savefh(&cp);
1495         nfsm_v4build_putfh(&cp, tdvp);
1496         nfsm_v4build_rename(&cp, &r);
1497         nfsm_v4build_finalize(&cp);
1498
1499         nfsm_request(fdvp, NFSV4PROC_COMPOUND, td, cred);
1500         if (error != 0)
1501                 goto nfsmout;
1502
1503         nfsm_v4dissect_compound(&cp);
1504         nfsm_v4dissect_putfh(&cp);
1505         nfsm_v4dissect_savefh(&cp);
1506         nfsm_v4dissect_putfh(&cp);
1507         nfsm_v4dissect_rename(&cp);
1508
1509         /* XXX should this always be performed?  */
1510         fnp->n_flag |= NMODIFIED;
1511         tnp->n_flag |= NMODIFIED;
1512         fnp->n_attrstamp = tnp->n_attrstamp = 0;
1513
1514  nfsmout:
1515         error = nfs_v4postop(&cp, error);
1516
1517         if (mrep != NULL)
1518                 m_freem(mrep);
1519
1520         return (error);
1521 }
1522
1523 /*
1524  * nfs file create call
1525  */
1526 static int
1527 nfs4_create(struct vop_create_args *ap)
1528 {
1529         struct vnode *dvp = ap->a_dvp;
1530         struct vattr *vap = ap->a_vap;
1531         struct nfsnode *dnp = VTONFS(dvp);
1532         struct componentname *cnp = ap->a_cnp;
1533         struct vnode *newvp = NULL;
1534         int error = 0, fmode = (O_CREAT | FREAD | FWRITE);
1535         struct vattr vattr;
1536
1537         if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0)
1538                 return (error);
1539
1540         if (vap->va_vaflags & VA_EXCLUSIVE)
1541                 fmode |= O_EXCL;
1542
1543         error = nfs4_openrpc(dvp, &newvp, cnp, fmode, vap);
1544         if (error != 0)
1545                 goto out;
1546
1547         VTONFS(newvp)->n_flag |= NCREATED;
1548
1549         if (cnp->cn_flags & MAKEENTRY)
1550                 cache_enter(dvp, newvp, cnp);
1551
1552         *ap->a_vpp = newvp;
1553
1554         dnp->n_flag |= NMODIFIED;
1555         dnp->n_attrstamp = 0;   /* XXX; wccflag */
1556
1557  out:
1558         return (error);
1559 }
1560
1561 /*
1562  * nfs file remove call
1563  * To try and make nfs semantics closer to ufs semantics, a file that has
1564  * other processes using the vnode is renamed instead of removed and then
1565  * removed later on the last close.
1566  * - If v_usecount > 1
1567  *        If a rename is not already in the works
1568  *           call nfs4_sillyrename() to set it up
1569  *     else
1570  *        do the remove rpc
1571  */
1572 static int
1573 nfs4_remove(struct vop_remove_args *ap)
1574 {
1575         struct vnode *vp = ap->a_vp;
1576         struct vnode *dvp = ap->a_dvp;
1577         struct componentname *cnp = ap->a_cnp;
1578         struct nfsnode *np = VTONFS(vp);
1579         int error = 0;
1580         struct vattr vattr;
1581
1582 #ifndef DIAGNOSTIC
1583         if ((cnp->cn_flags & HASBUF) == 0)
1584                 panic("nfs4_remove: no name");
1585         if (vrefcnt(vp) < 1)
1586                 panic("nfs4_remove: bad v_usecount");
1587 #endif
1588         if (vp->v_type == VDIR)
1589                 error = EPERM;
1590         else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1591             !VOP_GETATTR(vp, &vattr, cnp->cn_cred) && vattr.va_nlink > 1)) {
1592                 /*
1593                  * Purge the name cache so that the chance of a lookup for
1594                  * the name succeeding while the remove is in progress is
1595                  * minimized. Without node locking it can still happen, such
1596                  * that an I/O op returns ESTALE, but since you get this if
1597                  * another host removes the file..
1598                  */
1599                 cache_purge(vp);
1600                 /*
1601                  * throw away biocache buffers, mainly to avoid
1602                  * unnecessary delayed writes later.
1603                  */
1604                 error = nfs_vinvalbuf(vp, 0, cnp->cn_thread, 1);
1605                 /* Do the rpc */
1606                 if (error != EINTR)
1607                         error = nfs4_removerpc(dvp, cnp->cn_nameptr,
1608                                 cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
1609                 /*
1610                  * Kludge City: If the first reply to the remove rpc is lost..
1611                  *   the reply to the retransmitted request will be ENOENT
1612                  *   since the file was in fact removed
1613                  *   Therefore, we cheat and return success.
1614                  */
1615                 if (error == ENOENT)
1616                         error = 0;
1617         } else if (!np->n_sillyrename)
1618                 error = nfs4_sillyrename(dvp, vp, cnp);
1619         np->n_attrstamp = 0;
1620         return (error);
1621 }
1622
1623 /*
1624  * nfs file remove rpc called from nfs_inactive
1625  */
1626 int
1627 nfs4_removeit(struct sillyrename *sp)
1628 {
1629         /*
1630          * Make sure that the directory vnode is still valid.
1631          * XXX we should lock sp->s_dvp here.
1632          */
1633         if (sp->s_dvp->v_type == VBAD)
1634                 return (0);
1635         return (nfs4_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred,
1636                 NULL));
1637 }
1638
1639 /*
1640  * Nfs remove rpc, called from nfs4_remove() and nfs4_removeit().
1641  */
1642 static int
1643 nfs4_removerpc(struct vnode *dvp, const char *name, int namelen,
1644     struct ucred *cred, struct thread *td)
1645 {
1646         caddr_t bpos, dpos;
1647         int error = 0;
1648         struct mbuf *mreq, *mrep = NULL, *md, *mb;
1649         struct nfs4_compound cp;
1650
1651         nfsstats.rpccnt[NFSPROC_REMOVE]++;
1652
1653         mreq = nfsm_reqhead(dvp, NFSV4PROC_COMPOUND, 0);
1654         mb = mreq;
1655         bpos = mtod(mb, caddr_t);
1656
1657         nfs_v4initcompound(&cp);
1658
1659         nfsm_v4build_compound(&cp, "nfs4_removerpc()");
1660         nfsm_v4build_putfh(&cp, dvp);
1661         nfsm_v4build_remove(&cp, name, namelen);
1662         nfsm_v4build_finalize(&cp);
1663
1664         nfsm_request(dvp, NFSV4PROC_COMPOUND, td, cred);
1665         if (error != 0)
1666                 goto nfsmout;
1667
1668         nfsm_v4dissect_compound(&cp);
1669         nfsm_v4dissect_putfh(&cp);
1670         nfsm_v4dissect_remove(&cp);
1671
1672  nfsmout:
1673         error = nfs_v4postop(&cp, error);
1674
1675         if (mrep != NULL)
1676                 m_freem(mrep);
1677
1678         VTONFS(dvp)->n_flag |= NMODIFIED;
1679         VTONFS(dvp)->n_attrstamp = 0; /* XXX wccflag */
1680
1681         return (error);
1682 }
1683
1684 /*
1685  * nfs file rename call
1686  */
1687 static int
1688 nfs4_rename(struct vop_rename_args *ap)
1689 {
1690         struct vnode *fvp = ap->a_fvp;
1691         struct vnode *tvp = ap->a_tvp;
1692         struct vnode *fdvp = ap->a_fdvp;
1693         struct vnode *tdvp = ap->a_tdvp;
1694         struct componentname *tcnp = ap->a_tcnp;
1695         struct componentname *fcnp = ap->a_fcnp;
1696         int error;
1697
1698  #ifndef DIAGNOSTIC
1699         if ((tcnp->cn_flags & HASBUF) == 0 ||
1700             (fcnp->cn_flags & HASBUF) == 0)
1701                 panic("nfs4_rename: no name");
1702 #endif
1703         /* Check for cross-device rename */
1704         if ((fvp->v_mount != tdvp->v_mount) ||
1705             (tvp && (fvp->v_mount != tvp->v_mount))) {
1706                 error = EXDEV;
1707                 goto out;
1708         }
1709
1710         if (fvp == tvp) {
1711                 printf("nfs4_rename: fvp == tvp (can't happen)\n");
1712                 error = 0;
1713                 goto out;
1714         }
1715         if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
1716                 goto out;
1717
1718         /*
1719          * We have to flush B_DELWRI data prior to renaming
1720          * the file.  If we don't, the delayed-write buffers
1721          * can be flushed out later after the file has gone stale
1722          * under NFSV3.  NFSV2 does not have this problem because
1723          * ( as far as I can tell ) it flushes dirty buffers more
1724          * often.
1725          */
1726         VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_thread);
1727         VOP_UNLOCK(fvp, 0);
1728         if (tvp)
1729             VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_thread);
1730
1731         /*
1732          * If the tvp exists and is in use, sillyrename it before doing the
1733          * rename of the new file over it.
1734          * XXX Can't sillyrename a directory.
1735          */
1736         if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1737                 tvp->v_type != VDIR && !nfs4_sillyrename(tdvp, tvp, tcnp)) {
1738                 vput(tvp);
1739                 tvp = NULL;
1740         }
1741
1742         error = nfs4_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1743                 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1744                 tcnp->cn_thread);
1745
1746         if (fvp->v_type == VDIR) {
1747                 if (tvp != NULL && tvp->v_type == VDIR)
1748                         cache_purge(tdvp);
1749                 cache_purge(fdvp);
1750         }
1751
1752 out:
1753         if (tdvp == tvp)
1754                 vrele(tdvp);
1755         else
1756                 vput(tdvp);
1757         if (tvp)
1758                 vput(tvp);
1759         vrele(fdvp);
1760         vrele(fvp);
1761         /*
1762          * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1763          */
1764         if (error == ENOENT)
1765                 error = 0;
1766         return (error);
1767 }
1768
1769 /*
1770  * nfs file rename rpc called from nfs4_remove() above
1771  */
1772 static int
1773 nfs4_renameit(struct vnode *sdvp, struct componentname *scnp,
1774     struct sillyrename *sp)
1775 {
1776         return (nfs4_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen, sdvp,
1777             sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_thread));
1778 }
1779
1780 /*
1781  * nfs hard link create call
1782  */
1783 static int
1784 nfs4_link(struct vop_link_args *ap)
1785 {
1786         struct vnode *vp = ap->a_vp;
1787         struct vnode *tdvp = ap->a_tdvp;
1788         struct componentname *cnp = ap->a_cnp;
1789         caddr_t bpos, dpos;
1790         int error = 0;
1791         struct mbuf *mreq, *mrep = NULL, *md, *mb;
1792         struct nfs4_compound cp;
1793         struct nfs4_oparg_link l;
1794
1795         if (vp->v_mount != tdvp->v_mount) {
1796                 return (EXDEV);
1797         }
1798
1799         /*
1800          * Push all writes to the server, so that the attribute cache
1801          * doesn't get "out of sync" with the server.
1802          * XXX There should be a better way!
1803          */
1804         VOP_FSYNC(vp, MNT_WAIT, cnp->cn_thread);
1805
1806         nfsstats.rpccnt[NFSPROC_LINK]++;
1807
1808         l.name = cnp->cn_nameptr;
1809         l.namelen = cnp->cn_namelen;
1810         nfs_v4initcompound(&cp);
1811
1812         mreq = nfsm_reqhead(vp, NFSV4PROC_COMPOUND, 0);
1813         mb = mreq;
1814         bpos = mtod(mb, caddr_t);
1815
1816         nfsm_v4build_compound(&cp, "nfs4_link()");
1817         nfsm_v4build_putfh(&cp, vp);
1818         nfsm_v4build_savefh(&cp);
1819         nfsm_v4build_putfh(&cp, tdvp);
1820         nfsm_v4build_link(&cp, &l);
1821         nfsm_v4build_finalize(&cp);
1822
1823         nfsm_request(vp, NFSV4PROC_COMPOUND, cnp->cn_thread, cnp->cn_cred);
1824         if (error != 0)
1825                 goto nfsmout;
1826
1827         nfsm_v4dissect_compound(&cp);
1828         nfsm_v4dissect_putfh(&cp);
1829         nfsm_v4dissect_savefh(&cp);
1830         nfsm_v4dissect_putfh(&cp);
1831         nfsm_v4dissect_link(&cp);
1832
1833         VTONFS(tdvp)->n_flag |= NMODIFIED;
1834         VTONFS(vp)->n_attrstamp = 0;
1835         VTONFS(tdvp)->n_attrstamp = 0;
1836
1837 nfsmout:
1838         error = nfs_v4postop(&cp, error);
1839
1840         if (mrep != NULL)
1841                 m_freem(mrep);
1842
1843         return (error);
1844 }
1845
1846 /*
1847  * nfs symbolic link create call
1848  */
1849 static int
1850 nfs4_symlink(struct vop_symlink_args *ap)
1851 {
1852         struct vnode *dvp = ap->a_dvp;
1853         int error = 0;
1854         struct vnode *newvp = NULL;
1855
1856         nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1857
1858         error = nfs4_createrpc(ap->a_dvp, &newvp, ap->a_cnp, NFLNK,
1859             ap->a_vap, ap->a_target);
1860
1861         if (error != 0 && newvp != NULL)
1862                 vput(newvp);
1863         else if (error == 0)
1864                  *ap->a_vpp = newvp;
1865
1866         VTONFS(dvp)->n_flag |= NMODIFIED;
1867         VTONFS(dvp)->n_attrstamp = 0; /* XXX wccflags */
1868
1869         return (error);
1870 }
1871
1872 /*
1873  * nfs make dir call
1874  */
1875 static int
1876 nfs4_mkdir(struct vop_mkdir_args *ap)
1877 {
1878         return (nfs4_createrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, NFDIR,
1879                     ap->a_vap, NULL));
1880 }
1881
1882 /*
1883  * nfs remove directory call
1884  */
1885 static int
1886 nfs4_rmdir(struct vop_rmdir_args *ap)
1887 {
1888         struct vnode *vp = ap->a_vp;
1889         struct vnode *dvp = ap->a_dvp;
1890         struct nfsnode *dnp = VTONFS(dvp);
1891         struct componentname *cnp = ap->a_cnp;
1892         int error = 0;
1893
1894         if (dvp == vp)
1895                 return (EINVAL);
1896
1897         error = (nfs4_removerpc(dvp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_cred,
1898                                NULL));
1899         if (error)
1900                 return (error);
1901
1902         dnp->n_flag |= NMODIFIED;
1903         dnp->n_attrstamp = 0;
1904         cache_purge(dvp);
1905         cache_purge(vp);
1906
1907         return (error);
1908 }
1909
1910 /*
1911  * nfs readdir call
1912  */
1913 static int
1914 nfs4_readdir(struct vop_readdir_args *ap)
1915 {
1916         struct vnode *vp = ap->a_vp;
1917         struct nfsnode *np = VTONFS(vp);
1918         struct uio *uio = ap->a_uio;
1919         int tresid, error;
1920         struct vattr vattr;
1921
1922         if (vp->v_type != VDIR)
1923                 return (EPERM);
1924         /*
1925          * First, check for hit on the EOF offset cache
1926          */
1927         if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
1928             (np->n_flag & NMODIFIED) == 0) {
1929                 if (!VOP_GETATTR(vp, &vattr, ap->a_cred) &&
1930                         !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
1931                         nfsstats.direofcache_hits++;
1932                         return (0);
1933                 }
1934         }
1935
1936         /*
1937          * Call nfs_bioread() to do the real work.
1938          */
1939         tresid = uio->uio_resid;
1940         error = nfs_bioread(vp, uio, 0, ap->a_cred);
1941
1942         if (!error && uio->uio_resid == tresid)
1943                 nfsstats.direofcache_misses++;
1944         return (error);
1945 }
1946
1947 static u_char fty_to_dty[] = {
1948         DT_UNKNOWN,             /* NFNON */
1949         DT_REG,                 /* NFREG */
1950         DT_DIR,                 /* NFDIR */
1951         DT_BLK,                 /* NFBLK */
1952         DT_CHR,                 /* NFCHR */
1953         DT_LNK,                 /* NFLNK */
1954         DT_SOCK,                /* NFSOCK */
1955         DT_FIFO,                /* NFFIFO */
1956         DT_UNKNOWN,             /* NFATTRDIT */
1957         DT_UNKNOWN,             /* NFNAMEDATTR */
1958         DT_UNKNOWN,             /* NFBAD */
1959 };
1960
1961 /*
1962  * Readdir rpc call.
1963  * Called from below the buffer cache by nfs_doio().
1964  */
1965 int
1966 nfs4_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1967 {
1968         int len, left;
1969         struct dirent *dp = NULL;
1970         u_int32_t *tl;
1971         caddr_t p;
1972         uint64_t *cookiep;
1973         caddr_t bpos, dpos;
1974         struct mbuf *mreq, *mrep = NULL, *md, *mb;
1975         uint64_t cookie;
1976         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1977         struct nfsnode *dnp = VTONFS(vp);
1978         int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
1979         struct nfs4_compound cp;
1980         struct nfs4_oparg_readdir readdir;
1981         struct nfsv4_fattr fattr;
1982         u_int fty;
1983
1984 #ifndef DIAGNOSTIC
1985         if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
1986                 (uiop->uio_resid & (DIRBLKSIZ - 1)))
1987                 panic("nfs readdirrpc bad uio");
1988 #endif
1989
1990         /*
1991          * If there is no cookie, assume directory was stale.
1992          */
1993         cookiep = nfs4_getcookie(dnp, uiop->uio_offset, 0);
1994         if (cookiep)
1995                 cookie = *cookiep;
1996         else
1997                 return (NFSERR_BAD_COOKIE);
1998
1999         /* Generate fake entries for "." and ".." */
2000         while (cookie < 2 && bigenough) {
2001                 cookie++;
2002                 len = 4 + DIRHDSIZ;
2003
2004                 if (len > uiop->uio_resid) {
2005                         bigenough = 0;
2006                         break;
2007                 }
2008                 dp = (struct dirent *)uiop->uio_iov->iov_base;
2009
2010                 dp->d_namlen = cookie;
2011                 dp->d_reclen = len;
2012                 dp->d_type = DT_DIR;
2013                 if (cookie == 1)
2014                         dp->d_fileno = dnp->n_vattr.va_fileid; /* XXX has problems with pynfs virtualhandles */
2015                 else
2016                         dp->d_fileno = dnp->n_dvp != NULL ?
2017                             VTONFS(dnp->n_dvp)->n_vattr.va_fileid : cookie;
2018
2019                 p = dp->d_name;
2020                 *p++ = '.';
2021                 if (cookie == 2)
2022                         *p++ = '.';
2023                 *p = '\0';
2024
2025                 blksiz += len;
2026                 if (blksiz == DIRBLKSIZ)
2027                         blksiz = 0;
2028                 uiop->uio_offset += len;
2029                 uiop->uio_resid -= len;
2030                 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + len;
2031                 uiop->uio_iov->iov_len -= len;
2032         }
2033
2034         if (cookie == 2)
2035                 cookie = 0;
2036
2037         /* This is sort of ugly, to prevent v4postop() from acting weird */
2038         bzero(&cp, sizeof(cp));
2039
2040         /*
2041          * Loop around doing readdir rpc's of size nm_readdirsize
2042          * truncated to a multiple of DIRBLKSIZ.
2043          * The stopping criteria is EOF or buffer full.
2044          */
2045         /*
2046          * XXX this is sort of ugly for nfsv4; we don't maintain the
2047          * strict abstraction, but do the decoding inline.  that's ok.
2048          */
2049         while (more_dirs && bigenough) {
2050                 nfsstats.rpccnt[NFSPROC_READDIR]++;
2051
2052                 mreq = nfsm_reqhead(vp, NFSV4PROC_COMPOUND, 0);
2053                 mb = mreq;
2054                 bpos = mtod(mb, caddr_t);
2055
2056                 readdir.cnt = nmp->nm_readdirsize;
2057                 readdir.cookie = cookie;
2058                 readdir.bm = &nfsv4_readdirbm;
2059                 if (cookie == 0)
2060                         bzero(&readdir.verf, sizeof(readdir.verf));
2061                 else
2062                         bcopy(&dnp->n_cookieverf, &readdir.verf,
2063                             sizeof(readdir.verf));
2064
2065                 nfs_v4initcompound(&cp);
2066
2067                 nfsm_v4build_compound(&cp, "nfs4_readdirrpc()");
2068                 nfsm_v4build_putfh(&cp, vp);
2069                 nfsm_v4build_readdir(&cp, &readdir);
2070                 nfsm_v4build_finalize(&cp);
2071
2072                 nfsm_request(vp, NFSV4PROC_COMPOUND, uiop->uio_td, cred);
2073                 if (error != 0)
2074                         goto nfsmout;
2075
2076                 nfsm_v4dissect_compound(&cp);
2077                 nfsm_v4dissect_putfh(&cp);
2078
2079                 /*
2080                  * XXX - Readdir gets handled inline like in
2081                  * NFSv{2,3}.  This is a nasty inconsistency and
2082                  * should be fixed.
2083                  */
2084
2085                 tl = nfsm_dissect(uint32_t *, 5 * NFSX_UNSIGNED);
2086                 if (fxdr_unsigned(uint32_t, *tl++) != NFSV4OP_READDIR) {
2087                         error = EBADRPC;
2088                         goto nfsmout;
2089                 }
2090                 if (fxdr_unsigned(uint32_t, *tl++) != 0) {
2091                         error = EBADRPC;
2092                         goto nfsmout;
2093                 }
2094
2095                 bcopy(tl, &dnp->n_cookieverf, NFSX_V4VERF);
2096                 tl += 2;
2097                 more_dirs = fxdr_unsigned(int, *tl++);
2098
2099                 /* loop thru the dir entries, doctoring them to 4bsd form */
2100                 while (more_dirs && bigenough) {
2101                         tl = nfsm_dissect(uint32_t *, 3 * NFSX_UNSIGNED);
2102                         cookie = fxdr_hyper(tl);
2103                         tl += 2;
2104                         /* XXX cookie sanity check */
2105                         len = fxdr_unsigned(int, *tl++);
2106                         if (len <= 0 || len > NFS_MAXNAMLEN) {
2107                                 error = EBADRPC;
2108                                 goto nfsmout;
2109                         }
2110                         tlen = nfsm_rndup(len);
2111                         if (tlen == len)
2112                                 tlen += 4;      /* To ensure null termination */
2113                         left = DIRBLKSIZ - blksiz;
2114                         if ((tlen + DIRHDSIZ) > left) {
2115                                 dp->d_reclen += left;
2116                                 uiop->uio_iov->iov_base =
2117                                     (char *)uiop->uio_iov->iov_base + left;
2118                                 uiop->uio_iov->iov_len -= left;
2119                                 uiop->uio_offset += left;
2120                                 uiop->uio_resid -= left;
2121                                 blksiz = 0;
2122                         }
2123                         if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2124                                 bigenough = 0;
2125                         if (bigenough) {
2126                                 dp = (struct dirent *)uiop->uio_iov->iov_base;
2127
2128                                 dp->d_namlen = len;
2129                                 dp->d_reclen = tlen + DIRHDSIZ;
2130
2131                                 blksiz += dp->d_reclen;
2132                                 if (blksiz == DIRBLKSIZ)
2133                                         blksiz = 0;
2134                                 uiop->uio_offset += DIRHDSIZ;
2135                                 uiop->uio_resid -= DIRHDSIZ;
2136                                 uiop->uio_iov->iov_base =
2137                                     (char *)uiop->uio_iov->iov_base + DIRHDSIZ;
2138                                 uiop->uio_iov->iov_len -= DIRHDSIZ;
2139
2140                                 /* Copy name */
2141                                 nfsm_mtouio(uiop, len);
2142                                 p = uiop->uio_iov->iov_base;
2143                                 tlen -= len;
2144                                 *p = '\0';      /* null terminate */
2145                                 /* printf("nfs4_readdirrpc: name: \"%s\" cookie %d\n",
2146                                    p - len, (int) cookie);*/
2147                                 uiop->uio_iov->iov_base =
2148                                     (char *)uiop->uio_iov->iov_base + tlen;
2149                                 uiop->uio_iov->iov_len -= tlen;
2150                                 uiop->uio_offset += tlen;
2151                                 uiop->uio_resid -= tlen;
2152
2153                                 /* Copy attributes */
2154                                 nfsm_v4dissect_attrs(&fattr);
2155
2156                                 dp->d_fileno = nfs_v4fileid4_to_fileid(
2157                                         fattr.fa4_valid & FA4V_FILEID &&
2158                                             fattr.fa4_fileid ?
2159                                             fattr.fa4_fileid : cookie);
2160
2161                                 fty = (u_int)fattr.fa4_type;
2162                                 dp->d_type = fattr.fa4_valid & FA4V_TYPE &&
2163                                     (fty < sizeof(fty_to_dty)) ?
2164                                     fty_to_dty[fty] : DT_UNKNOWN;
2165                         } else
2166                                 nfsm_adv(nfsm_rndup(len));
2167
2168                         tl = nfsm_dissect(uint32_t *, NFSX_UNSIGNED);
2169                         more_dirs = fxdr_unsigned(int, *tl++);
2170                 }
2171                 /*
2172                  * If at end of rpc data, get the eof boolean
2173                  */
2174                 if (!more_dirs) {
2175                         tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2176                         more_dirs = (fxdr_unsigned(int, *tl) == 0);
2177                 }
2178
2179                 error = nfs_v4postop(&cp, error);
2180
2181                 m_freem(mrep);
2182                 mrep = NULL;
2183         }
2184         /*
2185          * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2186          * by increasing d_reclen for the last record.
2187          */
2188         if (blksiz > 0) {
2189                 left = DIRBLKSIZ - blksiz;
2190                 dp->d_reclen += left;
2191                 uiop->uio_iov->iov_base =
2192                     (char *)uiop->uio_iov->iov_base + left;
2193                 uiop->uio_iov->iov_len -= left;
2194                 uiop->uio_offset += left;
2195                 uiop->uio_resid -= left;
2196         }
2197
2198         /*
2199          * We are now either at the end of the directory or have filled the
2200          * block.
2201          */
2202         if (bigenough)
2203                 dnp->n_direofoffset = uiop->uio_offset;
2204         else {
2205                 if (uiop->uio_resid > 0)
2206                         printf("EEK! readdirrpc resid > 0\n");
2207                 cookiep = nfs4_getcookie(dnp, uiop->uio_offset, 1);
2208                 *cookiep = cookie;
2209         }
2210 nfsmout:
2211         if (mrep != NULL)
2212                 m_freem(mrep);
2213         return (error);
2214 }
2215
2216 /*
2217  * Silly rename. To make the NFS filesystem that is stateless look a little
2218  * more like the "ufs" a remove of an active vnode is translated to a rename
2219  * to a funny looking filename that is removed by nfs_inactive on the
2220  * nfsnode. There is the potential for another process on a different client
2221  * to create the same funny name between the nfs_lookitup() fails and the
2222  * nfs_rename() completes, but...
2223  */
2224 static int
2225 nfs4_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2226 {
2227         struct sillyrename *sp;
2228         struct nfsnode *np;
2229         int error;
2230         short pid;
2231
2232         cache_purge(dvp);
2233         np = VTONFS(vp);
2234 #ifndef DIAGNOSTIC
2235         if (vp->v_type == VDIR)
2236                 panic("nfs: sillyrename dir");
2237 #endif
2238         MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2239                 M_NFSREQ, M_WAITOK);
2240         sp->s_cred = crhold(cnp->cn_cred);
2241         sp->s_dvp = dvp;
2242         sp->s_removeit = nfs4_removeit;
2243         VREF(dvp);
2244
2245         /* Fudge together a funny name */
2246         pid = cnp->cn_thread->td_proc->p_pid;
2247         sp->s_namlen = sprintf(sp->s_name, ".nfsA%04x4.4", pid);
2248
2249         /* Try lookitups until we get one that isn't there */
2250         while (nfs4_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2251                 cnp->cn_thread, NULL) == 0) {
2252                 sp->s_name[4]++;
2253                 if (sp->s_name[4] > 'z') {
2254                         error = EINVAL;
2255                         goto bad;
2256                 }
2257         }
2258         error = nfs4_renameit(dvp, cnp, sp);
2259         if (error)
2260                 goto bad;
2261         error = nfs4_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2262                 cnp->cn_thread, &np);
2263         np->n_sillyrename = sp;
2264         return (0);
2265 bad:
2266         vrele(sp->s_dvp);
2267         crfree(sp->s_cred);
2268         free((caddr_t)sp, M_NFSREQ);
2269         return (error);
2270 }
2271
2272 /*
2273  * Look up a file name and optionally either update the file handle or
2274  * allocate an nfsnode, depending on the value of npp.
2275  * npp == NULL  --> just do the lookup
2276  * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2277  *                      handled too
2278  * *npp != NULL --> update the file handle in the vnode
2279  */
2280 static int
2281 nfs4_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2282     struct thread *td, struct nfsnode **npp)
2283 {
2284         struct vnode *newvp = NULL;
2285         struct nfsnode *np, *dnp = VTONFS(dvp);
2286         caddr_t bpos, dpos;
2287         int error = 0, fhlen;
2288         struct mbuf *mreq, *mrep = NULL, *md, *mb;
2289         nfsfh_t *nfhp;
2290         struct nfs4_compound cp;
2291         struct nfs4_oparg_lookup l;
2292         struct nfs4_oparg_getfh gfh;
2293         struct nfs4_oparg_getattr ga;
2294
2295         nfsstats.rpccnt[NFSPROC_RENAME]++;
2296
2297         mreq = nfsm_reqhead(dvp, NFSV4PROC_COMPOUND, 0);
2298         mb = mreq;
2299         bpos = mtod(mb, caddr_t);
2300
2301         l.name = name;
2302         l.namelen = len;
2303
2304         nfs_v4initcompound(&cp);
2305
2306         ga.bm = &nfsv4_getattrbm;
2307
2308         nfsm_v4build_compound(&cp, "nfs4_renamerpc()");
2309         nfsm_v4build_putfh(&cp, dvp);
2310         nfsm_v4build_lookup(&cp, &l);
2311         nfsm_v4build_getfh(&cp, &gfh);
2312         nfsm_v4build_getattr(&cp, &ga);
2313
2314         nfsm_request(dvp, NFSV4PROC_COMPOUND, td, cred);
2315         if (error != 0)
2316                 goto nfsmout;
2317
2318         nfsm_v4dissect_compound(&cp);
2319         nfsm_v4dissect_putfh(&cp);
2320         nfsm_v4dissect_lookup(&cp);
2321         nfsm_v4dissect_getfh(&cp, &gfh);
2322         nfsm_v4dissect_getattr(&cp, &ga);
2323
2324         if (npp != NULL && error == 0) {
2325                 nfhp = &gfh.fh_val;
2326                 fhlen = gfh.fh_len;
2327
2328                 if (*npp != NULL) {
2329                         np = *npp;
2330                         if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2331                                 free((caddr_t)np->n_fhp, M_NFSBIGFH);
2332                                 np->n_fhp = &np->n_fh;
2333                         } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2334                                 np->n_fhp =(nfsfh_t *)malloc(fhlen, M_NFSBIGFH, M_WAITOK);
2335                         bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2336                         np->n_fhsize = fhlen;
2337                         newvp = NFSTOV(np);
2338                 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2339                         VREF(dvp);
2340                         newvp = dvp;
2341                 } else {
2342                         error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np, LK_EXCLUSIVE);
2343                         if (error) {
2344                                 m_freem(mrep);
2345                                 return (error);
2346                         }
2347                         newvp = NFSTOV(np);
2348                 }
2349
2350                 if (newvp != dvp) {
2351                         np->n_dvp = dvp;
2352                         np->n_namelen = len;
2353                         if (np->n_name != NULL)
2354                                 FREE(np->n_name, M_NFSREQ);
2355                         MALLOC(np->n_name, u_char *,
2356                             np->n_namelen + 1, M_NFSREQ, M_WAITOK);
2357                         memcpy(np->n_name, name, len);
2358                         np->n_name[len] = '\0';
2359                 }
2360                 nfs4_vnop_loadattrcache(newvp, &ga.fa, NULL);
2361         }
2362
2363 nfsmout:
2364         error = nfs_v4postop(&cp, error);
2365
2366         if (mrep != NULL)
2367                 m_freem(mrep);
2368         if (npp && *npp == NULL) {
2369                 if (error) {
2370                         if (newvp) {
2371                                 if (newvp == dvp)
2372                                         vrele(newvp);
2373                                 else
2374                                         vput(newvp);
2375                         }
2376                 } else
2377                         *npp = np;
2378         }
2379
2380
2381         return (error);
2382 }
2383
2384 /*
2385  * Nfs Version 3 commit rpc
2386  */
2387 int
2388 nfs4_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2389     struct thread *td)
2390 {
2391         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2392         caddr_t bpos, dpos;
2393         int error = 0;
2394         struct mbuf *mreq, *mrep = NULL, *md, *mb;
2395         struct nfs4_compound cp;
2396         struct nfs4_oparg_commit commit;
2397
2398         if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2399                 return (0);
2400         nfsstats.rpccnt[NFSPROC_COMMIT]++;
2401
2402         mreq = nfsm_reqhead(vp, NFSV4PROC_COMPOUND, 0);
2403         mb = mreq;
2404         bpos = mtod(mb, caddr_t);
2405
2406         commit.start = offset;
2407         commit.len = cnt;
2408
2409         nfs_v4initcompound(&cp);
2410
2411         nfsm_v4build_compound(&cp, "nfs4_commit()");
2412         nfsm_v4build_putfh(&cp, vp);
2413         nfsm_v4build_commit(&cp, &commit);
2414         nfsm_v4build_finalize(&cp);
2415
2416         nfsm_request(vp, NFSV4PROC_COMPOUND, td, cred);
2417         if (error != 0)
2418                 goto nfsmout;
2419
2420         nfsm_v4dissect_compound(&cp);
2421         nfsm_v4dissect_putfh(&cp);
2422         nfsm_v4dissect_commit(&cp, &commit);
2423         
2424         /* XXX */
2425         /* nfsm_wcc_data(vp, wccflag);*/
2426         if (bcmp(nmp->nm_verf, commit.verf, NFSX_V4VERF)) {
2427                 bcopy(commit.verf, nmp->nm_verf, NFSX_V4VERF);
2428                 error = NFSERR_STALEWRITEVERF;
2429         }
2430
2431 nfsmout:
2432         error = nfs_v4postop(&cp, error);
2433
2434         if (mrep == NULL)
2435                 m_freem(mrep);
2436         return (error);
2437 }
2438
2439 /*
2440  * Strategy routine.
2441  * For async requests when nfsiod(s) are running, queue the request by
2442  * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2443  * request.
2444  */
2445 static int
2446 nfs4_strategy(struct vop_strategy_args *ap)
2447 {
2448         struct buf *bp = ap->a_bp;
2449         struct ucred *cr;
2450         int error = 0;
2451
2452         KASSERT(!(bp->b_flags & B_DONE),
2453             ("nfs4_strategy: buffer %p unexpectedly marked B_DONE", bp));
2454         BUF_ASSERT_HELD(bp);
2455
2456         if (bp->b_iocmd == BIO_READ)
2457                 cr = bp->b_rcred;
2458         else
2459                 cr = bp->b_wcred;
2460
2461         /*
2462          * If the op is asynchronous and an i/o daemon is waiting
2463          * queue the request, wake it up and wait for completion
2464          * otherwise just do it ourselves.
2465          */
2466         if ((bp->b_flags & B_ASYNC) == 0 ||
2467                 nfs_asyncio(VFSTONFS(ap->a_vp->v_mount), bp, NOCRED, curthread))
2468                 error = nfs_doio(ap->a_vp, bp, cr, curthread);
2469         return (error);
2470 }
2471
2472 /*
2473  * fsync vnode op. Just call nfs4_flush() with commit == 1.
2474  */
2475 /* ARGSUSED */
2476 static int
2477 nfs4_fsync(struct vop_fsync_args *ap)
2478 {
2479         return (nfs4_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1));
2480 }
2481
2482 /*
2483  * Flush all the blocks associated with a vnode.
2484  *      Walk through the buffer pool and push any dirty pages
2485  *      associated with the vnode.
2486  */
2487 static int
2488 nfs4_flush(struct vnode *vp, int waitfor, struct thread *td,
2489     int commit)
2490 {
2491         struct nfsnode *np = VTONFS(vp);
2492         struct bufobj *bo;
2493         struct buf *bp;
2494         int i;
2495         struct buf *nbp;
2496         struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2497         int error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2498         int passone = 1;
2499         u_quad_t off, endoff, toff;
2500         struct ucred* wcred = NULL;
2501         struct buf **bvec = NULL;
2502 #ifndef NFS_COMMITBVECSIZ
2503 #define NFS_COMMITBVECSIZ       20
2504 #endif
2505         struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2506         int bvecsize = 0, bveccount;
2507         bo = &vp->v_bufobj;
2508
2509         if (nmp->nm_flag & NFSMNT_INT)
2510                 slpflag = PCATCH;
2511         if (!commit)
2512                 passone = 0;
2513         /*
2514          * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2515          * server, but nas not been committed to stable storage on the server
2516          * yet. On the first pass, the byte range is worked out and the commit
2517          * rpc is done. On the second pass, nfs_writebp() is called to do the
2518          * job.
2519          */
2520 again:
2521         off = (u_quad_t)-1;
2522         endoff = 0;
2523         bvecpos = 0;
2524         if (NFS_ISV3(vp) && commit) {
2525                 if (bvec != NULL && bvec != bvec_on_stack)
2526                         free(bvec, M_TEMP);
2527                 /*
2528                  * Count up how many buffers waiting for a commit.
2529                  */
2530                 bveccount = 0;
2531                 BO_LOCK(bo);
2532                 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2533                         if (!BUF_ISLOCKED(bp) &&
2534                             (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2535                                 == (B_DELWRI | B_NEEDCOMMIT))
2536                                 bveccount++;
2537                 }
2538                 /*
2539                  * Allocate space to remember the list of bufs to commit.  It is
2540                  * important to use M_NOWAIT here to avoid a race with nfs4_write.
2541                  * If we can't get memory (for whatever reason), we will end up
2542                  * committing the buffers one-by-one in the loop below.
2543                  */
2544                 if (bveccount > NFS_COMMITBVECSIZ) {
2545                         /*
2546                          * Release the vnode interlock to avoid a lock
2547                          * order reversal.
2548                          */
2549                         BO_UNLOCK(bo);
2550                         bvec = (struct buf **)
2551                                 malloc(bveccount * sizeof(struct buf *),
2552                                        M_TEMP, M_NOWAIT);
2553                         BO_LOCK(bo);
2554                         if (bvec == NULL) {
2555                                 bvec = bvec_on_stack;
2556                                 bvecsize = NFS_COMMITBVECSIZ;
2557                         } else
2558                                 bvecsize = bveccount;
2559                 } else {
2560                         bvec = bvec_on_stack;
2561                         bvecsize = NFS_COMMITBVECSIZ;
2562                 }
2563                 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2564                         if (bvecpos >= bvecsize)
2565                                 break;
2566                         if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2567                                 nbp = TAILQ_NEXT(bp, b_bobufs);
2568                                 continue;
2569                         }
2570                         if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2571                             (B_DELWRI | B_NEEDCOMMIT)) {
2572                                 BUF_UNLOCK(bp);
2573                                 nbp = TAILQ_NEXT(bp, b_bobufs);
2574                                 continue;
2575                         }
2576                         BO_UNLOCK(bo);
2577                         bremfree(bp);
2578                         /*
2579                          * Work out if all buffers are using the same cred
2580                          * so we can deal with them all with one commit.
2581                          *
2582                          * NOTE: we are not clearing B_DONE here, so we have
2583                          * to do it later on in this routine if we intend to
2584                          * initiate I/O on the bp.
2585                          *
2586                          * Note: to avoid loopback deadlocks, we do not
2587                          * assign b_runningbufspace.
2588                          */
2589                         if (wcred == NULL)
2590                                 wcred = bp->b_wcred;
2591                         else if (wcred != bp->b_wcred)
2592                                 wcred = NOCRED;
2593                         vfs_busy_pages(bp, 1);
2594
2595                         BO_LOCK(bo);
2596                         /*
2597                          * bp is protected by being locked, but nbp is not
2598                          * and vfs_busy_pages() may sleep.  We have to
2599                          * recalculate nbp.
2600                          */
2601                         nbp = TAILQ_NEXT(bp, b_bobufs);
2602
2603                         /*
2604                          * A list of these buffers is kept so that the
2605                          * second loop knows which buffers have actually
2606                          * been committed. This is necessary, since there
2607                          * may be a race between the commit rpc and new
2608                          * uncommitted writes on the file.
2609                          */
2610                         bvec[bvecpos++] = bp;
2611                         toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2612                                 bp->b_dirtyoff;
2613                         if (toff < off)
2614                                 off = toff;
2615                         toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2616                         if (toff > endoff)
2617                                 endoff = toff;
2618                 }
2619                 BO_UNLOCK(bo);
2620         }
2621         if (bvecpos > 0) {
2622                 /*
2623                  * Commit data on the server, as required.
2624                  * If all bufs are using the same wcred, then use that with
2625                  * one call for all of them, otherwise commit each one
2626                  * separately.
2627                  */
2628                 if (wcred != NOCRED)
2629                         retv = nfs4_commit(vp, off, (int)(endoff - off),
2630                                           wcred, td);
2631                 else {
2632                         retv = 0;
2633                         for (i = 0; i < bvecpos; i++) {
2634                                 off_t off, size;
2635                                 bp = bvec[i];
2636                                 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2637                                         bp->b_dirtyoff;
2638                                 size = (u_quad_t)(bp->b_dirtyend
2639                                                   - bp->b_dirtyoff);
2640                                 retv = nfs4_commit(vp, off, (int)size,
2641                                                   bp->b_wcred, td);
2642                                 if (retv) break;
2643                         }
2644                 }
2645
2646                 if (retv == NFSERR_STALEWRITEVERF)
2647                         nfs_clearcommit(vp->v_mount);
2648
2649                 /*
2650                  * Now, either mark the blocks I/O done or mark the
2651                  * blocks dirty, depending on whether the commit
2652                  * succeeded.
2653                  */
2654                 for (i = 0; i < bvecpos; i++) {
2655                         bp = bvec[i];
2656                         bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2657                         if (retv) {
2658                                 /*
2659                                  * Error, leave B_DELWRI intact
2660                                  */
2661                                 vfs_unbusy_pages(bp);
2662                                 brelse(bp);
2663                         } else {
2664                                 /*
2665                                  * Success, remove B_DELWRI ( bundirty() ).
2666                                  *
2667                                  * b_dirtyoff/b_dirtyend seem to be NFS
2668                                  * specific.  We should probably move that
2669                                  * into bundirty(). XXX
2670                                  */
2671                                 bufobj_wref(bo);
2672                                 bp->b_flags |= B_ASYNC;
2673                                 bundirty(bp);
2674                                 bp->b_flags &= ~B_DONE;
2675                                 bp->b_ioflags &= ~BIO_ERROR;
2676                                 bp->b_dirtyoff = bp->b_dirtyend = 0;
2677                                 bufdone(bp);
2678                         }
2679                 }
2680         }
2681
2682         /*
2683          * Start/do any write(s) that are required.
2684          */
2685 loop:
2686         BO_LOCK(bo);
2687         TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2688                 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
2689                         if (waitfor != MNT_WAIT || passone)
2690                                 continue;
2691
2692                         error = BUF_TIMELOCK(bp,
2693                             LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2694                             BO_MTX(bo), "nfsfsync", slpflag, slptimeo);
2695                         if (error == 0)
2696                                 panic("nfs4_fsync: inconsistent lock");
2697                         if (error == ENOLCK) {
2698                                 error = 0;
2699                                 goto loop;
2700                         }
2701                         if (nfs4_sigintr(nmp, NULL, td)) {
2702                                 error = EINTR;
2703                                 goto done;
2704                         }
2705                         if (slpflag == PCATCH) {
2706                                 slpflag = 0;
2707                                 slptimeo = 2 * hz;
2708                         }
2709                         goto loop;
2710                 }
2711                 if ((bp->b_flags & B_DELWRI) == 0)
2712                         panic("nfs4_fsync: not dirty");
2713                 if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
2714                         BUF_UNLOCK(bp);
2715                         continue;
2716                 }
2717                 BO_LOCK(bo);
2718                 bremfree(bp);
2719                 if (passone || !commit)
2720                     bp->b_flags |= B_ASYNC;
2721                 else
2722                     bp->b_flags |= B_ASYNC;
2723                 bwrite(bp);
2724                 goto loop;
2725         }
2726         if (passone) {
2727                 passone = 0;
2728                 BO_UNLOCK(bo);
2729                 goto again;
2730         }
2731         if (waitfor == MNT_WAIT) {
2732                 while (bo->bo_numoutput) {
2733                         error = bufobj_wwait(bo, slpflag, slptimeo);
2734                         if (error) {
2735                             BO_UNLOCK(bo);
2736                             if (nfs4_sigintr(nmp, NULL, td)) {
2737                                 error = EINTR;
2738                                 goto done;
2739                             }
2740                             if (slpflag == PCATCH) {
2741                                 slpflag = 0;
2742                                 slptimeo = 2 * hz;
2743                             }
2744                             BO_LOCK(bo);
2745                         }
2746                 }
2747                 if (vp->v_bufobj.bo_dirty.bv_cnt > 0 && commit) {
2748                         BO_UNLOCK(bo);
2749                         goto loop;
2750                 }
2751         }
2752         BO_UNLOCK(bo);
2753         if (np->n_flag & NWRITEERR) {
2754                 error = np->n_error;
2755                 np->n_flag &= ~NWRITEERR;
2756         }
2757 done:
2758         if (bvec != NULL && bvec != bvec_on_stack)
2759                 free(bvec, M_TEMP);
2760         return (error);
2761 }
2762
2763 /*
2764  * NFS advisory byte-level locks.
2765  */
2766 static int
2767 nfs4_advlock(struct vop_advlock_args *ap)
2768 {
2769         struct vnode *vp = ap->a_vp;
2770         u_quad_t size;
2771         int error;
2772
2773         return (EPERM);
2774
2775         error = vn_lock(vp, LK_SHARED);
2776         if (error)
2777                 return (error);
2778         if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
2779                 size = VTONFS(vp)->n_size;
2780                 VOP_UNLOCK(vp, 0);
2781                 error = lf_advlock(ap, &(vp->v_lockf), size);
2782         } else
2783                 error = nfs_dolock(ap);
2784         return (error);
2785 }
2786
2787 /*
2788  * NFS advisory byte-level locks.
2789  */
2790 static int
2791 nfs4_advlockasync(struct vop_advlockasync_args *ap)
2792 {
2793         struct vnode *vp = ap->a_vp;
2794         u_quad_t size;
2795         int error;
2796
2797         return (EPERM);
2798
2799         error = vn_lock(vp, LK_SHARED);
2800         if (error)
2801                 return (error);
2802         if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
2803                 size = VTONFS(vp)->n_size;
2804                 VOP_UNLOCK(vp, 0);
2805                 error = lf_advlockasync(ap, &(vp->v_lockf), size);
2806         } else {
2807                 VOP_UNLOCK(vp, 0);
2808                 error = EOPNOTSUPP;
2809         }
2810         return (error);
2811 }
2812
2813 /*
2814  * Print out the contents of an nfsnode.
2815  */
2816 static int
2817 nfs4_print(struct vop_print_args *ap)
2818 {
2819         struct vnode *vp = ap->a_vp;
2820         struct nfsnode *np = VTONFS(vp);
2821
2822         printf("\tfileid %ld fsid 0x%x",
2823            np->n_vattr.va_fileid, np->n_vattr.va_fsid);
2824         if (vp->v_type == VFIFO)
2825                 fifo_printinfo(vp);
2826         printf("\n");
2827         return (0);
2828 }
2829
2830 /*
2831  * This is the "real" nfs::bwrite(struct buf*).
2832  * We set B_CACHE if this is a VMIO buffer.
2833  */
2834 int
2835 nfs4_writebp(struct buf *bp, int force __unused, struct thread *td)
2836 {
2837         int s;
2838         int oldflags = bp->b_flags;
2839 #if 0
2840         int retv = 1;
2841         off_t off;
2842 #endif
2843
2844         BUF_ASSERT_HELD(bp);
2845
2846         if (bp->b_flags & B_INVAL) {
2847                 brelse(bp);
2848                 return(0);
2849         }
2850
2851         bp->b_flags |= B_CACHE;
2852
2853         /*
2854          * Undirty the bp.  We will redirty it later if the I/O fails.
2855          */
2856
2857         s = splbio();
2858         bundirty(bp);
2859         bp->b_flags &= ~B_DONE;
2860         bp->b_ioflags &= ~BIO_ERROR;
2861         bp->b_iocmd = BIO_WRITE;
2862
2863         bufobj_wref(bp->b_bufobj);
2864         curthread->td_ru.ru_oublock++;
2865         splx(s);
2866
2867         /*
2868          * Note: to avoid loopback deadlocks, we do not
2869          * assign b_runningbufspace.
2870          */
2871         vfs_busy_pages(bp, 1);
2872
2873         BUF_KERNPROC(bp);
2874         bp->b_iooffset = dbtob(bp->b_blkno);
2875         bstrategy(bp);
2876
2877         if( (oldflags & B_ASYNC) == 0) {
2878                 int rtval = bufwait(bp);
2879
2880                 if (oldflags & B_DELWRI) {
2881                         s = splbio();
2882                         reassignbuf(bp);
2883                         splx(s);
2884                 }
2885
2886                 brelse(bp);
2887                 return (rtval);
2888         }
2889
2890         return (0);
2891 }
2892
2893 /*
2894  * Just call nfs_writebp() with the force argument set to 1.
2895  *
2896  * NOTE: B_DONE may or may not be set in a_bp on call.
2897  */
2898 static int
2899 nfs4_bwrite(struct buf *bp)
2900 {
2901
2902         return (nfs4_writebp(bp, 1, curthread));
2903 }
2904
2905 struct buf_ops buf_ops_nfs4 = {
2906         .bop_name       =       "buf_ops_nfs4",
2907         .bop_write      =       nfs4_bwrite,
2908         .bop_strategy   =       bufstrategy,
2909         .bop_sync       =       bufsync,
2910         .bop_bdflush    =       bufbdflush,
2911 };