]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/nfsserver/nfs_serv.c
This commit was generated by cvs2svn to compensate for changes in r89232,
[FreeBSD/FreeBSD.git] / sys / nfsserver / nfs_serv.c
1 /*
2  * Copyright (c) 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Macklem at The University of Guelph.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *      @(#)nfs_serv.c  8.8 (Berkeley) 7/31/95
37  */
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41
42 /*
43  * nfs version 2 and 3 server calls to vnode ops
44  * - these routines generally have 3 phases
45  *   1 - break down and validate rpc request in mbuf list
46  *   2 - do the vnode ops for the request
47  *       (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c)
48  *   3 - build the rpc reply in an mbuf list
49  *   nb:
50  *      - do not mix the phases, since the nfsm_?? macros can return failures
51  *        on a bad rpc or similar and do not do any vrele() or vput()'s
52  *
53  *      - the nfsm_reply() macro generates an nfs rpc reply with the nfs
54  *      error number iff error != 0 whereas
55  *      returning an error from the server function implies a fatal error
56  *      such as a badly constructed rpc request that should be dropped without
57  *      a reply.
58  *      For Version 3, nfsm_reply() does not return for the error case, since
59  *      most version 3 rpcs return more than the status for error cases.
60  *
61  * Other notes:
62  *      Warning: always pay careful attention to resource cleanup on return
63  *      and note that nfsm_*() macros can terminate a procedure on certain
64  *      errors.
65  *
66  *      lookup() and namei()
67  *      may return garbage in various structural fields/return elements
68  *      if an error is returned, and may garbage up nd.ni_dvp even if no
69  *      error is returned and you did not request LOCKPARENT or WANTPARENT.
70  *
71  *      We use the ni_cnd.cn_flags 'HASBUF' flag to track whether the name
72  *      buffer has been freed or not.
73  */
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/proc.h>
78 #include <sys/namei.h>
79 #include <sys/unistd.h>
80 #include <sys/vnode.h>
81 #include <sys/mount.h>
82 #include <sys/socket.h>
83 #include <sys/socketvar.h>
84 #include <sys/malloc.h>
85 #include <sys/mbuf.h>
86 #include <sys/dirent.h>
87 #include <sys/stat.h>
88 #include <sys/kernel.h>
89 #include <sys/sysctl.h>
90 #include <sys/bio.h>
91 #include <sys/buf.h>
92
93 #include <vm/vm.h>
94 #include <vm/vm_extern.h>
95 #include <vm/vm_object.h>
96
97 #include <nfs/nfsproto.h>
98 #include <nfs/rpcv2.h>
99 #include <nfsserver/nfs.h>
100 #include <nfs/xdr_subs.h>
101 #include <nfsserver/nfsm_subs.h>
102
103 #ifdef NFSRV_DEBUG
104 #define nfsdbprintf(info)       printf info
105 #else
106 #define nfsdbprintf(info)
107 #endif
108
109 #define MAX_COMMIT_COUNT        (1024 * 1024)
110
111 #define NUM_HEURISTIC           64
112 #define NHUSE_INIT              64
113 #define NHUSE_INC               16
114 #define NHUSE_MAX               2048
115
116 static struct nfsheur {
117         struct vnode *nh_vp;    /* vp to match (unreferenced pointer) */
118         off_t nh_nextr;         /* next offset for sequential detection */
119         int nh_use;             /* use count for selection */
120         int nh_seqcount;        /* heuristic */
121 } nfsheur[NUM_HEURISTIC];
122
123 /* Global vars */
124
125 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
126 int nfsrvw_procrastinate_v3 = 0;
127
128 static struct timeval   nfsver = { 0 };
129
130 SYSCTL_NODE(_vfs, OID_AUTO, nfsrv, CTLFLAG_RW, 0, "NFS server");
131
132 static int nfs_async;
133 static int nfs_commit_blks;
134 static int nfs_commit_miss;
135 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0, "");
136 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 0, "");
137 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 0, "");
138
139 struct nfsrvstats nfsrvstats;
140 SYSCTL_STRUCT(_vfs_nfsrv, NFS_NFSRVSTATS, nfsrvstats, CTLFLAG_RD,
141         &nfsrvstats, nfsrvstats, "S,nfsrvstats");
142
143 static int      nfsrv_access(struct vnode *, int, struct ucred *, int,
144                     struct thread *, int);
145 static void     nfsrvw_coalesce(struct nfsrv_descript *,
146                     struct nfsrv_descript *);
147
148 /*
149  * Clear nameidata fields that are tested in nsfmout cleanup code prior
150  * to using first nfsm macro (that might jump to the cleanup code).
151  */
152
153 static __inline void
154 ndclear(struct nameidata *nd)
155 {
156
157         nd->ni_cnd.cn_flags = 0;
158         nd->ni_vp = NULL;
159         nd->ni_dvp = NULL;
160         nd->ni_startdir = NULL;
161 }
162
163 /*
164  * nfs v3 access service
165  */
166 int
167 nfsrv3_access(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
168     struct thread *td, struct mbuf **mrq)
169 {
170         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
171         struct sockaddr *nam = nfsd->nd_nam;
172         caddr_t dpos = nfsd->nd_dpos;
173         struct ucred *cred = &nfsd->nd_cr;
174         struct vnode *vp = NULL;
175         nfsfh_t nfh;
176         fhandle_t *fhp;
177         u_int32_t *tl;
178         caddr_t bpos;
179         int error = 0, rdonly, getret;
180         struct mbuf *mb, *mreq;
181         struct vattr vattr, *vap = &vattr;
182         u_long testmode, nfsmode;
183         int v3 = (nfsd->nd_flag & ND_NFSV3);
184
185         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
186         if (!v3)
187                 panic("nfsrv3_access: v3 proc called on a v2 connection");
188         fhp = &nfh.fh_generic;
189         nfsm_srvmtofh(fhp);
190         tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
191         error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
192         if (error) {
193                 nfsm_reply(NFSX_UNSIGNED);
194                 nfsm_srvpostop_attr(1, (struct vattr *)0);
195                 error = 0;
196                 goto nfsmout;
197         }
198         nfsmode = fxdr_unsigned(u_int32_t, *tl);
199         if ((nfsmode & NFSV3ACCESS_READ) &&
200                 nfsrv_access(vp, VREAD, cred, rdonly, td, 0))
201                 nfsmode &= ~NFSV3ACCESS_READ;
202         if (vp->v_type == VDIR)
203                 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
204                         NFSV3ACCESS_DELETE);
205         else
206                 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
207         if ((nfsmode & testmode) &&
208                 nfsrv_access(vp, VWRITE, cred, rdonly, td, 0))
209                 nfsmode &= ~testmode;
210         if (vp->v_type == VDIR)
211                 testmode = NFSV3ACCESS_LOOKUP;
212         else
213                 testmode = NFSV3ACCESS_EXECUTE;
214         if ((nfsmode & testmode) &&
215                 nfsrv_access(vp, VEXEC, cred, rdonly, td, 0))
216                 nfsmode &= ~testmode;
217         getret = VOP_GETATTR(vp, vap, cred, td);
218         vput(vp);
219         vp = NULL;
220         nfsm_reply(NFSX_POSTOPATTR(1) + NFSX_UNSIGNED);
221         nfsm_srvpostop_attr(getret, vap);
222         tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
223         *tl = txdr_unsigned(nfsmode);
224 nfsmout:
225         if (vp)
226                 vput(vp);
227         return(error);
228 }
229
230 /*
231  * nfs getattr service
232  */
233 int
234 nfsrv_getattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
235     struct thread *td, struct mbuf **mrq)
236 {
237         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
238         struct sockaddr *nam = nfsd->nd_nam;
239         caddr_t dpos = nfsd->nd_dpos;
240         struct ucred *cred = &nfsd->nd_cr;
241         struct nfs_fattr *fp;
242         struct vattr va;
243         struct vattr *vap = &va;
244         struct vnode *vp = NULL;
245         nfsfh_t nfh;
246         fhandle_t *fhp;
247         caddr_t bpos;
248         int error = 0, rdonly;
249         struct mbuf *mb, *mreq;
250
251         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
252         fhp = &nfh.fh_generic;
253         nfsm_srvmtofh(fhp);
254         error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
255         if (error) {
256                 nfsm_reply(0);
257                 error = 0;
258                 goto nfsmout;
259         }
260         error = VOP_GETATTR(vp, vap, cred, td);
261         vput(vp);
262         vp = NULL;
263         nfsm_reply(NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
264         if (error) {
265                 error = 0;
266                 goto nfsmout;
267         }
268         fp = nfsm_build(struct nfs_fattr *,
269             NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
270         nfsm_srvfillattr(vap, fp);
271         /* fall through */
272
273 nfsmout:
274         if (vp)
275                 vput(vp);
276         return(error);
277 }
278
279 /*
280  * nfs setattr service
281  */
282 int
283 nfsrv_setattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
284     struct thread *td, struct mbuf **mrq)
285 {
286         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
287         struct sockaddr *nam = nfsd->nd_nam;
288         caddr_t dpos = nfsd->nd_dpos;
289         struct ucred *cred = &nfsd->nd_cr;
290         struct vattr va, preat;
291         struct vattr *vap = &va;
292         struct nfsv2_sattr *sp;
293         struct nfs_fattr *fp;
294         struct vnode *vp = NULL;
295         nfsfh_t nfh;
296         fhandle_t *fhp;
297         u_int32_t *tl;
298         caddr_t bpos;
299         int error = 0, rdonly, preat_ret = 1, postat_ret = 1;
300         int v3 = (nfsd->nd_flag & ND_NFSV3), gcheck = 0;
301         struct mbuf *mb, *mreq;
302         struct timespec guard;
303         struct mount *mp = NULL;
304
305         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
306         fhp = &nfh.fh_generic;
307         nfsm_srvmtofh(fhp);
308         if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
309                 error = ESTALE;
310                 goto out;
311         }
312         if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
313                 mp = NULL;
314                 goto out;
315         }
316         (void) vn_start_write(vp, &mp, V_WAIT);
317         vput(vp);
318         vp = NULL;
319         VATTR_NULL(vap);
320         if (v3) {
321                 nfsm_srvsattr(vap);
322                 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
323                 gcheck = fxdr_unsigned(int, *tl);
324                 if (gcheck) {
325                         tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
326                         fxdr_nfsv3time(tl, &guard);
327                 }
328         } else {
329                 sp = nfsm_dissect(struct nfsv2_sattr *, NFSX_V2SATTR);
330                 /*
331                  * Nah nah nah nah na nah
332                  * There is a bug in the Sun client that puts 0xffff in the mode
333                  * field of sattr when it should put in 0xffffffff. The u_short
334                  * doesn't sign extend.
335                  * --> check the low order 2 bytes for 0xffff
336                  */
337                 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
338                         vap->va_mode = nfstov_mode(sp->sa_mode);
339                 if (sp->sa_uid != nfsrv_nfs_xdrneg1)
340                         vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
341                 if (sp->sa_gid != nfsrv_nfs_xdrneg1)
342                         vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
343                 if (sp->sa_size != nfsrv_nfs_xdrneg1)
344                         vap->va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
345                 if (sp->sa_atime.nfsv2_sec != nfsrv_nfs_xdrneg1) {
346 #ifdef notyet
347                         fxdr_nfsv2time(&sp->sa_atime, &vap->va_atime);
348 #else
349                         vap->va_atime.tv_sec =
350                                 fxdr_unsigned(int32_t, sp->sa_atime.nfsv2_sec);
351                         vap->va_atime.tv_nsec = 0;
352 #endif
353                 }
354                 if (sp->sa_mtime.nfsv2_sec != nfsrv_nfs_xdrneg1)
355                         fxdr_nfsv2time(&sp->sa_mtime, &vap->va_mtime);
356
357         }
358
359         /*
360          * Now that we have all the fields, lets do it.
361          */
362         error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
363         if (error) {
364                 nfsm_reply(2 * NFSX_UNSIGNED);
365                 if (v3)
366                         nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
367                 error = 0;
368                 goto nfsmout;
369         }
370
371         /*
372          * vp now an active resource, pay careful attention to cleanup
373          */
374         if (v3) {
375                 error = preat_ret = VOP_GETATTR(vp, &preat, cred, td);
376                 if (!error && gcheck &&
377                         (preat.va_ctime.tv_sec != guard.tv_sec ||
378                          preat.va_ctime.tv_nsec != guard.tv_nsec))
379                         error = NFSERR_NOT_SYNC;
380                 if (error) {
381                         vput(vp);
382                         vp = NULL;
383                         nfsm_reply(NFSX_WCCDATA(v3));
384                         if (v3)
385                                 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
386                         error = 0;
387                         goto nfsmout;
388                 }
389         }
390
391         /*
392          * If the size is being changed write acces is required, otherwise
393          * just check for a read only file system.
394          */
395         if (vap->va_size == ((u_quad_t)((quad_t) -1))) {
396                 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
397                         error = EROFS;
398                         goto out;
399                 }
400         } else {
401                 if (vp->v_type == VDIR) {
402                         error = EISDIR;
403                         goto out;
404                 } else if ((error = nfsrv_access(vp, VWRITE, cred, rdonly,
405                         td, 0)) != 0)
406                         goto out;
407         }
408         error = VOP_SETATTR(vp, vap, cred, td);
409         postat_ret = VOP_GETATTR(vp, vap, cred, td);
410         if (!error)
411                 error = postat_ret;
412 out:
413         if (vp != NULL)
414                 vput(vp);
415         vp = NULL;
416         nfsm_reply(NFSX_WCCORFATTR(v3));
417         if (v3) {
418                 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
419                 error = 0;
420                 goto nfsmout;
421         } else {
422                 /* v2 non-error case (see nfsm_reply). */
423                 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
424                 nfsm_srvfillattr(vap, fp);
425         }
426         /* fall through */
427
428 nfsmout:
429         if (vp)
430                 vput(vp);
431         vn_finished_write(mp);
432         return(error);
433 }
434
435 /*
436  * nfs lookup rpc
437  */
438 int
439 nfsrv_lookup(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
440     struct thread *td, struct mbuf **mrq)
441 {
442         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
443         struct sockaddr *nam = nfsd->nd_nam;
444         caddr_t dpos = nfsd->nd_dpos;
445         struct ucred *cred = &nfsd->nd_cr;
446         struct nfs_fattr *fp;
447         struct nameidata nd, ind, *ndp = &nd;
448         struct vnode *vp, *dirp = NULL;
449         nfsfh_t nfh;
450         fhandle_t *fhp;
451         caddr_t bpos;
452         int error = 0, len, dirattr_ret = 1;
453         int v3 = (nfsd->nd_flag & ND_NFSV3), pubflag;
454         struct mbuf *mb, *mreq;
455         struct vattr va, dirattr, *vap = &va;
456
457         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
458         ndclear(&nd);
459
460         fhp = &nfh.fh_generic;
461         nfsm_srvmtofh(fhp);
462         nfsm_srvnamesiz(len);
463
464         pubflag = nfs_ispublicfh(fhp);
465
466         nd.ni_cnd.cn_cred = cred;
467         nd.ni_cnd.cn_nameiop = LOOKUP;
468         nd.ni_cnd.cn_flags = LOCKLEAF | SAVESTART;
469         error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
470                 &dirp, td, pubflag);
471
472         /*
473          * namei failure, only dirp to cleanup.  Clear out garbarge from
474          * structure in case macros jump to nfsmout.
475          */
476
477         if (error) {
478                 if (dirp) {
479                         if (v3)
480                                 dirattr_ret = VOP_GETATTR(dirp, &dirattr, cred,
481                                         td);
482                         vrele(dirp);
483                         dirp = NULL;
484                 }
485                 nfsm_reply(NFSX_POSTOPATTR(v3));
486                 if (v3)
487                         nfsm_srvpostop_attr(dirattr_ret, &dirattr);
488                 error = 0;
489                 goto nfsmout;
490         }
491
492         /*
493          * Locate index file for public filehandle
494          *
495          * error is 0 on entry and 0 on exit from this block.
496          */
497
498         if (pubflag) {
499                 if (nd.ni_vp->v_type == VDIR && nfs_pub.np_index != NULL) {
500                         /*
501                          * Setup call to lookup() to see if we can find
502                          * the index file. Arguably, this doesn't belong
503                          * in a kernel.. Ugh.  If an error occurs, do not
504                          * try to install an index file and then clear the
505                          * error.
506                          *
507                          * When we replace nd with ind and redirect ndp,
508                          * maintenance of ni_startdir and ni_vp shift to
509                          * ind and we have to clean them up in the old nd.
510                          * However, the cnd resource continues to be maintained
511                          * via the original nd.  Confused?  You aren't alone!
512                          */
513                         ind = nd;
514                         VOP_UNLOCK(nd.ni_vp, 0, td);
515                         ind.ni_pathlen = strlen(nfs_pub.np_index);
516                         ind.ni_cnd.cn_nameptr = ind.ni_cnd.cn_pnbuf =
517                             nfs_pub.np_index;
518                         ind.ni_startdir = nd.ni_vp;
519                         VREF(ind.ni_startdir);
520
521                         error = lookup(&ind);
522                         ind.ni_dvp = NULL;
523
524                         if (error == 0) {
525                                 /*
526                                  * Found an index file. Get rid of
527                                  * the old references.  transfer nd.ni_vp'
528                                  */
529                                 if (dirp)
530                                         vrele(dirp);
531                                 dirp = nd.ni_vp;
532                                 nd.ni_vp = NULL;
533                                 vrele(nd.ni_startdir);
534                                 nd.ni_startdir = NULL;
535                                 ndp = &ind;
536                         }
537                         error = 0;
538                 }
539                 /*
540                  * If the public filehandle was used, check that this lookup
541                  * didn't result in a filehandle outside the publicly exported
542                  * filesystem.  We clear the poor vp here to avoid lockups due
543                  * to NFS I/O.
544                  */
545
546                 if (ndp->ni_vp->v_mount != nfs_pub.np_mount) {
547                         vput(nd.ni_vp);
548                         nd.ni_vp = NULL;
549                         error = EPERM;
550                 }
551         }
552
553         if (dirp) {
554                 if (v3)
555                         dirattr_ret = VOP_GETATTR(dirp, &dirattr, cred,
556                                 td);
557                 vrele(dirp);
558                 dirp = NULL;
559         }
560
561         /*
562          * Resources at this point:
563          *      ndp->ni_vp      may not be NULL
564          *
565          */
566
567         if (error) {
568                 nfsm_reply(NFSX_POSTOPATTR(v3));
569                 if (v3)
570                         nfsm_srvpostop_attr(dirattr_ret, &dirattr);
571                 error = 0;
572                 goto nfsmout;
573         }
574
575         /*
576          * Clear out some resources prior to potentially blocking.  This
577          * is not as critical as ni_dvp resources in other routines, but
578          * it helps.
579          */
580         vrele(ndp->ni_startdir);
581         ndp->ni_startdir = NULL;
582         NDFREE(&nd, NDF_ONLY_PNBUF);
583
584         /*
585          * Get underlying attribute, then release remaining resources ( for
586          * the same potential blocking reason ) and reply.
587          */
588         vp = ndp->ni_vp;
589         bzero((caddr_t)fhp, sizeof(nfh));
590         fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
591         error = VFS_VPTOFH(vp, &fhp->fh_fid);
592         if (!error)
593                 error = VOP_GETATTR(vp, vap, cred, td);
594
595         vput(vp);
596         ndp->ni_vp = NULL;
597         nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPORFATTR(v3) + NFSX_POSTOPATTR(v3));
598         if (error) {
599                 if (v3)
600                         nfsm_srvpostop_attr(dirattr_ret, &dirattr);
601                 error = 0;
602                 goto nfsmout;
603         }
604         nfsm_srvfhtom(fhp, v3);
605         if (v3) {
606                 nfsm_srvpostop_attr(0, vap);
607                 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
608         } else {
609                 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
610                 nfsm_srvfillattr(vap, fp);
611         }
612
613 nfsmout:
614         if (dirp)
615                 vrele(dirp);
616         NDFREE(&nd, NDF_ONLY_PNBUF);
617         if (ndp->ni_startdir)
618                 vrele(ndp->ni_startdir);
619         if (ndp->ni_vp)
620                 vput(ndp->ni_vp);
621         return (error);
622 }
623
624 /*
625  * nfs readlink service
626  */
627 int
628 nfsrv_readlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
629     struct thread *td, struct mbuf **mrq)
630 {
631         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
632         struct sockaddr *nam = nfsd->nd_nam;
633         caddr_t dpos = nfsd->nd_dpos;
634         struct ucred *cred = &nfsd->nd_cr;
635         struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
636         struct iovec *ivp = iv;
637         struct mbuf *mp;
638         u_int32_t *tl;
639         caddr_t bpos;
640         int error = 0, rdonly, i, tlen, len, getret;
641         int v3 = (nfsd->nd_flag & ND_NFSV3);
642         struct mbuf *mb, *mp3, *nmp, *mreq;
643         struct vnode *vp = NULL;
644         struct vattr attr;
645         nfsfh_t nfh;
646         fhandle_t *fhp;
647         struct uio io, *uiop = &io;
648
649         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
650 #ifndef nolint
651         mp = (struct mbuf *)0;
652 #endif
653         mp3 = NULL;
654         fhp = &nfh.fh_generic;
655         nfsm_srvmtofh(fhp);
656         len = 0;
657         i = 0;
658         while (len < NFS_MAXPATHLEN) {
659                 MGET(nmp, M_TRYWAIT, MT_DATA);
660                 MCLGET(nmp, M_TRYWAIT);
661                 nmp->m_len = NFSMSIZ(nmp);
662                 if (len == 0)
663                         mp3 = mp = nmp;
664                 else {
665                         mp->m_next = nmp;
666                         mp = nmp;
667                 }
668                 if ((len + mp->m_len) > NFS_MAXPATHLEN) {
669                         mp->m_len = NFS_MAXPATHLEN - len;
670                         len = NFS_MAXPATHLEN;
671                 } else
672                         len += mp->m_len;
673                 ivp->iov_base = mtod(mp, caddr_t);
674                 ivp->iov_len = mp->m_len;
675                 i++;
676                 ivp++;
677         }
678         uiop->uio_iov = iv;
679         uiop->uio_iovcnt = i;
680         uiop->uio_offset = 0;
681         uiop->uio_resid = len;
682         uiop->uio_rw = UIO_READ;
683         uiop->uio_segflg = UIO_SYSSPACE;
684         uiop->uio_td = (struct thread *)0;
685         error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
686         if (error) {
687                 nfsm_reply(2 * NFSX_UNSIGNED);
688                 if (v3)
689                         nfsm_srvpostop_attr(1, (struct vattr *)0);
690                 error = 0;
691                 goto nfsmout;
692         }
693         if (vp->v_type != VLNK) {
694                 if (v3)
695                         error = EINVAL;
696                 else
697                         error = ENXIO;
698                 goto out;
699         }
700         error = VOP_READLINK(vp, uiop, cred);
701 out:
702         getret = VOP_GETATTR(vp, &attr, cred, td);
703         vput(vp);
704         vp = NULL;
705         nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_UNSIGNED);
706         if (v3) {
707                 nfsm_srvpostop_attr(getret, &attr);
708                 if (error) {
709                         error = 0;
710                         goto nfsmout;
711                 }
712         }
713         if (uiop->uio_resid > 0) {
714                 len -= uiop->uio_resid;
715                 tlen = nfsm_rndup(len);
716                 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
717         }
718         tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
719         *tl = txdr_unsigned(len);
720         mb->m_next = mp3;
721         mp3 = NULL;
722 nfsmout:
723         if (mp3)
724                 m_freem(mp3);
725         if (vp)
726                 vput(vp);
727         return(error);
728 }
729
730 /*
731  * nfs read service
732  */
733 int
734 nfsrv_read(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
735     struct thread *td, struct mbuf **mrq)
736 {
737         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
738         struct sockaddr *nam = nfsd->nd_nam;
739         caddr_t dpos = nfsd->nd_dpos;
740         struct ucred *cred = &nfsd->nd_cr;
741         struct iovec *iv;
742         struct iovec *iv2;
743         struct mbuf *m;
744         struct nfs_fattr *fp;
745         u_int32_t *tl;
746         int i;
747         caddr_t bpos;
748         int error = 0, rdonly, cnt, len, left, siz, tlen, getret;
749         int v3 = (nfsd->nd_flag & ND_NFSV3), reqlen;
750         struct mbuf *mb, *mreq;
751         struct mbuf *m2;
752         struct vnode *vp = NULL;
753         nfsfh_t nfh;
754         fhandle_t *fhp;
755         struct uio io, *uiop = &io;
756         struct vattr va, *vap = &va;
757         struct nfsheur *nh;
758         off_t off;
759         int ioflag = 0;
760
761         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
762         fhp = &nfh.fh_generic;
763         nfsm_srvmtofh(fhp);
764         if (v3) {
765                 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
766                 off = fxdr_hyper(tl);
767         } else {
768                 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
769                 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
770         }
771         nfsm_srvstrsiz(reqlen, NFS_SRVMAXDATA(nfsd));
772
773         /*
774          * Reference vp.  If an error occurs, vp will be invalid, but we
775          * have to NULL it just in case.  The macros might goto nfsmout
776          * as well.
777          */
778
779         error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
780         if (error) {
781                 vp = NULL;
782                 nfsm_reply(2 * NFSX_UNSIGNED);
783                 if (v3)
784                         nfsm_srvpostop_attr(1, (struct vattr *)0);
785                 error = 0;
786                 goto nfsmout;
787         }
788
789         if (vp->v_type != VREG) {
790                 if (v3)
791                         error = EINVAL;
792                 else
793                         error = (vp->v_type == VDIR) ? EISDIR : EACCES;
794         }
795         if (!error) {
796                 if ((error = nfsrv_access(vp, VREAD, cred, rdonly, td, 1)) != 0)
797                         error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 1);
798         }
799         getret = VOP_GETATTR(vp, vap, cred, td);
800         if (!error)
801                 error = getret;
802         if (error) {
803                 vput(vp);
804                 vp = NULL;
805                 nfsm_reply(NFSX_POSTOPATTR(v3));
806                 if (v3)
807                         nfsm_srvpostop_attr(getret, vap);
808                 error = 0;
809                 goto nfsmout;
810         }
811
812         /*
813          * Calculate byte count to read
814          */
815
816         if (off >= vap->va_size)
817                 cnt = 0;
818         else if ((off + reqlen) > vap->va_size)
819                 cnt = vap->va_size - off;
820         else
821                 cnt = reqlen;
822
823         /*
824          * Calculate seqcount for heuristic
825          */
826
827         {
828                 int hi;
829                 int try = 4;
830
831                 /*
832                  * Locate best candidate
833                  */
834
835                 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) & (NUM_HEURISTIC - 1);
836                 nh = &nfsheur[hi];
837
838                 while (try--) {
839                         if (nfsheur[hi].nh_vp == vp) {
840                                 nh = &nfsheur[hi];
841                                 break;
842                         }
843                         if (nfsheur[hi].nh_use > 0)
844                                 --nfsheur[hi].nh_use;
845                         hi = (hi + 1) & (NUM_HEURISTIC - 1);
846                         if (nfsheur[hi].nh_use < nh->nh_use)
847                                 nh = &nfsheur[hi];
848                 }
849
850                 if (nh->nh_vp != vp) {
851                         nh->nh_vp = vp;
852                         nh->nh_nextr = off;
853                         nh->nh_use = NHUSE_INIT;
854                         if (off == 0)
855                                 nh->nh_seqcount = 4;
856                         else
857                                 nh->nh_seqcount = 1;
858                 }
859
860                 /*
861                  * Calculate heuristic
862                  */
863
864                 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
865                         if (++nh->nh_seqcount > 127)
866                                 nh->nh_seqcount = 127;
867                 } else if (nh->nh_seqcount > 1) {
868                         nh->nh_seqcount = 1;
869                 } else {
870                         nh->nh_seqcount = 0;
871                 }
872                 nh->nh_use += NHUSE_INC;
873                 if (nh->nh_use > NHUSE_MAX)
874                         nh->nh_use = NHUSE_MAX;
875                 ioflag |= nh->nh_seqcount << 16;
876         }
877
878         nfsm_reply(NFSX_POSTOPORFATTR(v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
879         if (v3) {
880                 tl = nfsm_build(u_int32_t *, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
881                 *tl++ = nfsrv_nfs_true;
882                 fp = (struct nfs_fattr *)tl;
883                 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
884         } else {
885                 tl = nfsm_build(u_int32_t *, NFSX_V2FATTR + NFSX_UNSIGNED);
886                 fp = (struct nfs_fattr *)tl;
887                 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
888         }
889         len = left = nfsm_rndup(cnt);
890         if (cnt > 0) {
891                 /*
892                  * Generate the mbuf list with the uio_iov ref. to it.
893                  */
894                 i = 0;
895                 m = m2 = mb;
896                 while (left > 0) {
897                         siz = min(M_TRAILINGSPACE(m), left);
898                         if (siz > 0) {
899                                 left -= siz;
900                                 i++;
901                         }
902                         if (left > 0) {
903                                 MGET(m, M_TRYWAIT, MT_DATA);
904                                 MCLGET(m, M_TRYWAIT);
905                                 m->m_len = 0;
906                                 m2->m_next = m;
907                                 m2 = m;
908                         }
909                 }
910                 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
911                        M_TEMP, M_WAITOK);
912                 uiop->uio_iov = iv2 = iv;
913                 m = mb;
914                 left = len;
915                 i = 0;
916                 while (left > 0) {
917                         if (m == NULL)
918                                 panic("nfsrv_read iov");
919                         siz = min(M_TRAILINGSPACE(m), left);
920                         if (siz > 0) {
921                                 iv->iov_base = mtod(m, caddr_t) + m->m_len;
922                                 iv->iov_len = siz;
923                                 m->m_len += siz;
924                                 left -= siz;
925                                 iv++;
926                                 i++;
927                         }
928                         m = m->m_next;
929                 }
930                 uiop->uio_iovcnt = i;
931                 uiop->uio_offset = off;
932                 uiop->uio_resid = len;
933                 uiop->uio_rw = UIO_READ;
934                 uiop->uio_segflg = UIO_SYSSPACE;
935                 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
936                 off = uiop->uio_offset;
937                 nh->nh_nextr = off;
938                 FREE((caddr_t)iv2, M_TEMP);
939                 if (error || (getret = VOP_GETATTR(vp, vap, cred, td))) {
940                         if (!error)
941                                 error = getret;
942                         m_freem(mreq);
943                         vput(vp);
944                         vp = NULL;
945                         nfsm_reply(NFSX_POSTOPATTR(v3));
946                         if (v3)
947                                 nfsm_srvpostop_attr(getret, vap);
948                         error = 0;
949                         goto nfsmout;
950                 }
951         } else {
952                 uiop->uio_resid = 0;
953         }
954         vput(vp);
955         vp = NULL;
956         nfsm_srvfillattr(vap, fp);
957         tlen = len - uiop->uio_resid;
958         cnt = cnt < tlen ? cnt : tlen;
959         tlen = nfsm_rndup(cnt);
960         if (len != tlen || tlen != cnt)
961                 nfsm_adj(mb, len - tlen, tlen - cnt);
962         if (v3) {
963                 *tl++ = txdr_unsigned(cnt);
964                 if (len < reqlen)
965                         *tl++ = nfsrv_nfs_true;
966                 else
967                         *tl++ = nfsrv_nfs_false;
968         }
969         *tl = txdr_unsigned(cnt);
970 nfsmout:
971         if (vp)
972                 vput(vp);
973         return(error);
974 }
975
976 /*
977  * nfs write service
978  */
979 int
980 nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
981     struct thread *td, struct mbuf **mrq)
982 {
983         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
984         struct sockaddr *nam = nfsd->nd_nam;
985         caddr_t dpos = nfsd->nd_dpos;
986         struct ucred *cred = &nfsd->nd_cr;
987         struct iovec *ivp;
988         int i, cnt;
989         struct mbuf *mp;
990         struct nfs_fattr *fp;
991         struct iovec *iv;
992         struct vattr va, forat;
993         struct vattr *vap = &va;
994         u_int32_t *tl;
995         caddr_t bpos;
996         int error = 0, rdonly, len, forat_ret = 1;
997         int ioflags, aftat_ret = 1, retlen = 0, zeroing, adjust;
998         int stable = NFSV3WRITE_FILESYNC;
999         int v3 = (nfsd->nd_flag & ND_NFSV3);
1000         struct mbuf *mb, *mreq;
1001         struct vnode *vp = NULL;
1002         nfsfh_t nfh;
1003         fhandle_t *fhp;
1004         struct uio io, *uiop = &io;
1005         off_t off;
1006         struct mount *mntp = NULL;
1007
1008         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1009         if (mrep == NULL) {
1010                 *mrq = NULL;
1011                 error = 0;
1012                 goto nfsmout;
1013         }
1014         fhp = &nfh.fh_generic;
1015         nfsm_srvmtofh(fhp);
1016         if ((mntp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
1017                 error = ESTALE;
1018                 goto ereply;
1019         }
1020         if ((error = VFS_FHTOVP(mntp, &fhp->fh_fid, &vp)) != 0) {
1021                 mntp = NULL;
1022                 goto ereply;
1023         }
1024         (void) vn_start_write(vp, &mntp, V_WAIT);
1025         vput(vp);
1026         vp = NULL;
1027         if (v3) {
1028                 tl = nfsm_dissect(u_int32_t *, 5 * NFSX_UNSIGNED);
1029                 off = fxdr_hyper(tl);
1030                 tl += 3;
1031                 stable = fxdr_unsigned(int, *tl++);
1032         } else {
1033                 tl = nfsm_dissect(u_int32_t *, 4 * NFSX_UNSIGNED);
1034                 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1035                 tl += 2;
1036                 if (nfs_async)
1037                         stable = NFSV3WRITE_UNSTABLE;
1038         }
1039         retlen = len = fxdr_unsigned(int32_t, *tl);
1040         cnt = i = 0;
1041
1042         /*
1043          * For NFS Version 2, it is not obvious what a write of zero length
1044          * should do, but I might as well be consistent with Version 3,
1045          * which is to return ok so long as there are no permission problems.
1046          */
1047         if (len > 0) {
1048             zeroing = 1;
1049             mp = mrep;
1050             while (mp) {
1051                 if (mp == md) {
1052                         zeroing = 0;
1053                         adjust = dpos - mtod(mp, caddr_t);
1054                         mp->m_len -= adjust;
1055                         if (mp->m_len > 0 && adjust > 0)
1056                                 mp->m_data += adjust;
1057                 }
1058                 if (zeroing)
1059                         mp->m_len = 0;
1060                 else if (mp->m_len > 0) {
1061                         i += mp->m_len;
1062                         if (i > len) {
1063                                 mp->m_len -= (i - len);
1064                                 zeroing = 1;
1065                         }
1066                         if (mp->m_len > 0)
1067                                 cnt++;
1068                 }
1069                 mp = mp->m_next;
1070             }
1071         }
1072         if (len > NFS_MAXDATA || len < 0 || i < len) {
1073                 error = EIO;
1074                 nfsm_reply(2 * NFSX_UNSIGNED);
1075                 if (v3)
1076                         nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1077                 error = 0;
1078                 goto nfsmout;
1079         }
1080         error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
1081         if (error) {
1082                 vp = NULL;
1083                 nfsm_reply(2 * NFSX_UNSIGNED);
1084                 if (v3)
1085                         nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1086                 error = 0;
1087                 goto nfsmout;
1088         }
1089         if (v3)
1090                 forat_ret = VOP_GETATTR(vp, &forat, cred, td);
1091         if (vp->v_type != VREG) {
1092                 if (v3)
1093                         error = EINVAL;
1094                 else
1095                         error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1096         }
1097         if (!error)
1098                 error = nfsrv_access(vp, VWRITE, cred, rdonly, td, 1);
1099         if (error) {
1100                 vput(vp);
1101                 vp = NULL;
1102                 nfsm_reply(NFSX_WCCDATA(v3));
1103                 if (v3)
1104                         nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1105                 error = 0;
1106                 goto nfsmout;
1107         }
1108
1109         if (len > 0) {
1110             MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
1111                 M_WAITOK);
1112             uiop->uio_iov = iv = ivp;
1113             uiop->uio_iovcnt = cnt;
1114             mp = mrep;
1115             while (mp) {
1116                 if (mp->m_len > 0) {
1117                         ivp->iov_base = mtod(mp, caddr_t);
1118                         ivp->iov_len = mp->m_len;
1119                         ivp++;
1120                 }
1121                 mp = mp->m_next;
1122             }
1123
1124             /*
1125              * XXX
1126              * The IO_METASYNC flag indicates that all metadata (and not just
1127              * enough to ensure data integrity) mus be written to stable storage
1128              * synchronously.
1129              * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1130              */
1131             if (stable == NFSV3WRITE_UNSTABLE)
1132                 ioflags = IO_NODELOCKED;
1133             else if (stable == NFSV3WRITE_DATASYNC)
1134                 ioflags = (IO_SYNC | IO_NODELOCKED);
1135             else
1136                 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1137             uiop->uio_resid = len;
1138             uiop->uio_rw = UIO_WRITE;
1139             uiop->uio_segflg = UIO_SYSSPACE;
1140             uiop->uio_td = (struct thread *)0;
1141             uiop->uio_offset = off;
1142             error = VOP_WRITE(vp, uiop, ioflags, cred);
1143             nfsrvstats.srvvop_writes++;
1144             FREE((caddr_t)iv, M_TEMP);
1145         }
1146         aftat_ret = VOP_GETATTR(vp, vap, cred, td);
1147         vput(vp);
1148         vp = NULL;
1149         if (!error)
1150                 error = aftat_ret;
1151 ereply:
1152         nfsm_reply(NFSX_PREOPATTR(v3) + NFSX_POSTOPORFATTR(v3) +
1153                 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(v3));
1154         if (v3) {
1155                 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1156                 if (error) {
1157                         error = 0;
1158                         goto nfsmout;
1159                 }
1160                 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
1161                 *tl++ = txdr_unsigned(retlen);
1162                 /*
1163                  * If nfs_async is set, then pretend the write was FILESYNC.
1164                  */
1165                 if (stable == NFSV3WRITE_UNSTABLE && !nfs_async)
1166                         *tl++ = txdr_unsigned(stable);
1167                 else
1168                         *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
1169                 /*
1170                  * Actually, there is no need to txdr these fields,
1171                  * but it may make the values more human readable,
1172                  * for debugging purposes.
1173                  */
1174                 if (nfsver.tv_sec == 0)
1175                         nfsver = boottime;
1176                 *tl++ = txdr_unsigned(nfsver.tv_sec);
1177                 *tl = txdr_unsigned(nfsver.tv_usec);
1178         } else {
1179                 /* v2, non-error case (see nfsm_reply). */
1180                 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
1181                 nfsm_srvfillattr(vap, fp);
1182         }
1183 nfsmout:
1184         if (vp)
1185                 vput(vp);
1186         vn_finished_write(mntp);
1187         return(error);
1188 }
1189
1190 /*
1191  * NFS write service with write gathering support. Called when
1192  * nfsrvw_procrastinate > 0.
1193  * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1194  * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1195  * Jan. 1994.
1196  */
1197 int
1198 nfsrv_writegather(struct nfsrv_descript **ndp, struct nfssvc_sock *slp,
1199     struct thread *td, struct mbuf **mrq)
1200 {
1201         struct iovec *ivp;
1202         struct mbuf *mp;
1203         struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1204         struct nfs_fattr *fp;
1205         int i;
1206         struct iovec *iov;
1207         struct nfsrvw_delayhash *wpp;
1208         struct ucred *cred;
1209         struct vattr va, forat;
1210         u_int32_t *tl;
1211         caddr_t bpos, dpos;
1212         int error = 0, rdonly, len, forat_ret = 1;
1213         int ioflags, aftat_ret = 1, s, adjust, v3, zeroing;
1214         struct mbuf *mb, *mreq, *mrep, *md;
1215         struct vnode *vp = NULL;
1216         struct uio io, *uiop = &io;
1217         u_quad_t cur_usec;
1218         struct mount *mntp = NULL;
1219
1220         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1221 #ifndef nolint
1222         i = 0;
1223         len = 0;
1224 #endif
1225         *mrq = NULL;
1226         if (*ndp) {
1227             nfsd = *ndp;
1228             *ndp = NULL;
1229             mrep = nfsd->nd_mrep;
1230             md = nfsd->nd_md;
1231             dpos = nfsd->nd_dpos;
1232             cred = &nfsd->nd_cr;
1233             v3 = (nfsd->nd_flag & ND_NFSV3);
1234             LIST_INIT(&nfsd->nd_coalesce);
1235             nfsd->nd_mreq = NULL;
1236             nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1237             cur_usec = nfs_curusec();
1238             nfsd->nd_time = cur_usec +
1239                 (v3 ? nfsrvw_procrastinate_v3 : nfsrvw_procrastinate);
1240
1241             /*
1242              * Now, get the write header..
1243              */
1244             nfsm_srvmtofh(&nfsd->nd_fh);
1245             if (v3) {
1246                 tl = nfsm_dissect(u_int32_t *, 5 * NFSX_UNSIGNED);
1247                 nfsd->nd_off = fxdr_hyper(tl);
1248                 tl += 3;
1249                 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1250             } else {
1251                 tl = nfsm_dissect(u_int32_t *, 4 * NFSX_UNSIGNED);
1252                 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1253                 tl += 2;
1254                 if (nfs_async)
1255                         nfsd->nd_stable = NFSV3WRITE_UNSTABLE;
1256             }
1257             len = fxdr_unsigned(int32_t, *tl);
1258             nfsd->nd_len = len;
1259             nfsd->nd_eoff = nfsd->nd_off + len;
1260
1261             /*
1262              * Trim the header out of the mbuf list and trim off any trailing
1263              * junk so that the mbuf list has only the write data.
1264              */
1265             zeroing = 1;
1266             i = 0;
1267             mp = mrep;
1268             while (mp) {
1269                 if (mp == md) {
1270                     zeroing = 0;
1271                     adjust = dpos - mtod(mp, caddr_t);
1272                     mp->m_len -= adjust;
1273                     if (mp->m_len > 0 && adjust > 0)
1274                         mp->m_data += adjust;
1275                 }
1276                 if (zeroing)
1277                     mp->m_len = 0;
1278                 else {
1279                     i += mp->m_len;
1280                     if (i > len) {
1281                         mp->m_len -= (i - len);
1282                         zeroing = 1;
1283                     }
1284                 }
1285                 mp = mp->m_next;
1286             }
1287             if (len > NFS_MAXDATA || len < 0  || i < len) {
1288 nfsmout:
1289                 m_freem(mrep);
1290                 error = EIO;
1291                 nfsm_writereply(2 * NFSX_UNSIGNED);
1292                 if (v3)
1293                     nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1294                 nfsd->nd_mreq = mreq;
1295                 nfsd->nd_mrep = NULL;
1296                 nfsd->nd_time = 0;
1297             }
1298
1299             /*
1300              * Add this entry to the hash and time queues.
1301              */
1302             s = splsoftclock();
1303             owp = NULL;
1304             wp = LIST_FIRST(&slp->ns_tq);
1305             while (wp && wp->nd_time < nfsd->nd_time) {
1306                 owp = wp;
1307                 wp = LIST_NEXT(wp, nd_tq);
1308             }
1309             NFS_DPF(WG, ("Q%03x", nfsd->nd_retxid & 0xfff));
1310             if (owp) {
1311                 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1312             } else {
1313                 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1314             }
1315             if (nfsd->nd_mrep) {
1316                 wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
1317                 owp = NULL;
1318                 wp = LIST_FIRST(wpp);
1319                 while (wp &&
1320                     bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh, NFSX_V3FH)){
1321                     owp = wp;
1322                     wp = LIST_NEXT(wp, nd_hash);
1323                 }
1324                 while (wp && wp->nd_off < nfsd->nd_off &&
1325                     !bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh, NFSX_V3FH)) {
1326                     owp = wp;
1327                     wp = LIST_NEXT(wp, nd_hash);
1328                 }
1329                 if (owp) {
1330                     LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1331
1332                     /*
1333                      * Search the hash list for overlapping entries and
1334                      * coalesce.
1335                      */
1336                     for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1337                         wp = LIST_NEXT(nfsd, nd_hash);
1338                         if (NFSW_SAMECRED(owp, nfsd))
1339                             nfsrvw_coalesce(owp, nfsd);
1340                     }
1341                 } else {
1342                     LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1343                 }
1344             }
1345             splx(s);
1346         }
1347
1348         /*
1349          * Now, do VOP_WRITE()s for any one(s) that need to be done now
1350          * and generate the associated reply mbuf list(s).
1351          */
1352 loop1:
1353         cur_usec = nfs_curusec();
1354         s = splsoftclock();
1355         for (nfsd = LIST_FIRST(&slp->ns_tq); nfsd; nfsd = owp) {
1356                 owp = LIST_NEXT(nfsd, nd_tq);
1357                 if (nfsd->nd_time > cur_usec)
1358                     break;
1359                 if (nfsd->nd_mreq)
1360                     continue;
1361                 NFS_DPF(WG, ("P%03x", nfsd->nd_retxid & 0xfff));
1362                 LIST_REMOVE(nfsd, nd_tq);
1363                 LIST_REMOVE(nfsd, nd_hash);
1364                 splx(s);
1365                 mrep = nfsd->nd_mrep;
1366                 nfsd->nd_mrep = NULL;
1367                 cred = &nfsd->nd_cr;
1368                 v3 = (nfsd->nd_flag & ND_NFSV3);
1369                 forat_ret = aftat_ret = 1;
1370                 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &vp, cred, slp,
1371                     nfsd->nd_nam, &rdonly, TRUE);
1372                 if (!error) {
1373                     if (v3)
1374                         forat_ret = VOP_GETATTR(vp, &forat, cred, td);
1375                     if (vp->v_type != VREG) {
1376                         if (v3)
1377                             error = EINVAL;
1378                         else
1379                             error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1380                     }
1381                 } else {
1382                     vp = NULL;
1383                 }
1384                 if (!error)
1385                     error = nfsrv_access(vp, VWRITE, cred, rdonly, td, 1);
1386                 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1387                     ioflags = IO_NODELOCKED;
1388                 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1389                     ioflags = (IO_SYNC | IO_NODELOCKED);
1390                 else
1391                     ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1392                 uiop->uio_rw = UIO_WRITE;
1393                 uiop->uio_segflg = UIO_SYSSPACE;
1394                 uiop->uio_td = (struct thread *)0;
1395                 uiop->uio_offset = nfsd->nd_off;
1396                 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1397                 if (uiop->uio_resid > 0) {
1398                     mp = mrep;
1399                     i = 0;
1400                     while (mp) {
1401                         if (mp->m_len > 0)
1402                             i++;
1403                         mp = mp->m_next;
1404                     }
1405                     uiop->uio_iovcnt = i;
1406                     MALLOC(iov, struct iovec *, i * sizeof (struct iovec),
1407                         M_TEMP, M_WAITOK);
1408                     uiop->uio_iov = ivp = iov;
1409                     mp = mrep;
1410                     while (mp) {
1411                         if (mp->m_len > 0) {
1412                             ivp->iov_base = mtod(mp, caddr_t);
1413                             ivp->iov_len = mp->m_len;
1414                             ivp++;
1415                         }
1416                         mp = mp->m_next;
1417                     }
1418                     if (!error) {
1419                         if (vn_start_write(vp, &mntp, V_NOWAIT) != 0) {
1420                             VOP_UNLOCK(vp, 0, td);
1421                             error = vn_start_write(NULL, &mntp, V_WAIT);
1422                             vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1423                         }
1424                     }
1425                     if (!error) {
1426                         error = VOP_WRITE(vp, uiop, ioflags, cred);
1427                         nfsrvstats.srvvop_writes++;
1428                         vn_finished_write(mntp);
1429                     }
1430                     FREE((caddr_t)iov, M_TEMP);
1431                 }
1432                 m_freem(mrep);
1433                 if (vp) {
1434                     aftat_ret = VOP_GETATTR(vp, &va, cred, td);
1435                     vput(vp);
1436                     vp = NULL;
1437                 }
1438
1439                 /*
1440                  * Loop around generating replies for all write rpcs that have
1441                  * now been completed.
1442                  */
1443                 swp = nfsd;
1444                 do {
1445                     NFS_DPF(WG, ("R%03x", nfsd->nd_retxid & 0xfff));
1446                     if (error) {
1447                         nfsm_writereply(NFSX_WCCDATA(v3));
1448                         if (v3) {
1449                             nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1450                         }
1451                     } else {
1452                         nfsm_writereply(NFSX_PREOPATTR(v3) +
1453                             NFSX_POSTOPORFATTR(v3) + 2 * NFSX_UNSIGNED +
1454                             NFSX_WRITEVERF(v3));
1455                         if (v3) {
1456                             nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1457                             tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
1458                             *tl++ = txdr_unsigned(nfsd->nd_len);
1459                             *tl++ = txdr_unsigned(swp->nd_stable);
1460                             /*
1461                              * Actually, there is no need to txdr these fields,
1462                              * but it may make the values more human readable,
1463                              * for debugging purposes.
1464                              */
1465                             if (nfsver.tv_sec == 0)
1466                                     nfsver = boottime;
1467                             *tl++ = txdr_unsigned(nfsver.tv_sec);
1468                             *tl = txdr_unsigned(nfsver.tv_usec);
1469                         } else {
1470                             fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
1471                             nfsm_srvfillattr(&va, fp);
1472                         }
1473                     }
1474                     nfsd->nd_mreq = mreq;
1475                     if (nfsd->nd_mrep)
1476                         panic("nfsrv_write: nd_mrep not free");
1477
1478                     /*
1479                      * Done. Put it at the head of the timer queue so that
1480                      * the final phase can return the reply.
1481                      */
1482                     s = splsoftclock();
1483                     if (nfsd != swp) {
1484                         nfsd->nd_time = 0;
1485                         LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1486                     }
1487                     nfsd = LIST_FIRST(&swp->nd_coalesce);
1488                     if (nfsd) {
1489                         LIST_REMOVE(nfsd, nd_tq);
1490                     }
1491                     splx(s);
1492                 } while (nfsd);
1493                 s = splsoftclock();
1494                 swp->nd_time = 0;
1495                 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1496                 splx(s);
1497                 goto loop1;
1498         }
1499         splx(s);
1500
1501         /*
1502          * Search for a reply to return.
1503          */
1504         s = splsoftclock();
1505         LIST_FOREACH(nfsd, &slp->ns_tq, nd_tq)
1506                 if (nfsd->nd_mreq) {
1507                     NFS_DPF(WG, ("X%03x", nfsd->nd_retxid & 0xfff));
1508                     LIST_REMOVE(nfsd, nd_tq);
1509                     *mrq = nfsd->nd_mreq;
1510                     *ndp = nfsd;
1511                     break;
1512                 }
1513         splx(s);
1514         return (0);
1515 }
1516
1517 /*
1518  * Coalesce the write request nfsd into owp. To do this we must:
1519  * - remove nfsd from the queues
1520  * - merge nfsd->nd_mrep into owp->nd_mrep
1521  * - update the nd_eoff and nd_stable for owp
1522  * - put nfsd on owp's nd_coalesce list
1523  * NB: Must be called at splsoftclock().
1524  */
1525 static void
1526 nfsrvw_coalesce(struct nfsrv_descript *owp, struct nfsrv_descript *nfsd)
1527 {
1528         int overlap;
1529         struct mbuf *mp;
1530         struct nfsrv_descript *p;
1531
1532         NFS_DPF(WG, ("C%03x-%03x",
1533                      nfsd->nd_retxid & 0xfff, owp->nd_retxid & 0xfff));
1534         LIST_REMOVE(nfsd, nd_hash);
1535         LIST_REMOVE(nfsd, nd_tq);
1536         if (owp->nd_eoff < nfsd->nd_eoff) {
1537             overlap = owp->nd_eoff - nfsd->nd_off;
1538             if (overlap < 0)
1539                 panic("nfsrv_coalesce: bad off");
1540             if (overlap > 0)
1541                 m_adj(nfsd->nd_mrep, overlap);
1542             mp = owp->nd_mrep;
1543             while (mp->m_next)
1544                 mp = mp->m_next;
1545             mp->m_next = nfsd->nd_mrep;
1546             owp->nd_eoff = nfsd->nd_eoff;
1547         } else
1548             m_freem(nfsd->nd_mrep);
1549         nfsd->nd_mrep = NULL;
1550         if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1551             owp->nd_stable = NFSV3WRITE_FILESYNC;
1552         else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1553             owp->nd_stable == NFSV3WRITE_UNSTABLE)
1554             owp->nd_stable = NFSV3WRITE_DATASYNC;
1555         LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1556
1557         /*
1558          * If nfsd had anything else coalesced into it, transfer them
1559          * to owp, otherwise their replies will never get sent.
1560          */
1561         for (p = LIST_FIRST(&nfsd->nd_coalesce); p;
1562              p = LIST_FIRST(&nfsd->nd_coalesce)) {
1563             LIST_REMOVE(p, nd_tq);
1564             LIST_INSERT_HEAD(&owp->nd_coalesce, p, nd_tq);
1565         }
1566 }
1567
1568 /*
1569  * nfs create service
1570  * now does a truncate to 0 length via. setattr if it already exists
1571  */
1572 int
1573 nfsrv_create(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1574     struct thread *td, struct mbuf **mrq)
1575 {
1576         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1577         struct sockaddr *nam = nfsd->nd_nam;
1578         caddr_t dpos = nfsd->nd_dpos;
1579         struct ucred *cred = &nfsd->nd_cr;
1580         struct nfs_fattr *fp;
1581         struct vattr va, dirfor, diraft;
1582         struct vattr *vap = &va;
1583         struct nfsv2_sattr *sp;
1584         u_int32_t *tl;
1585         struct nameidata nd;
1586         caddr_t bpos;
1587         int error = 0, rdev, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1588         int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0;
1589         caddr_t cp;
1590         struct mbuf *mb, *mreq;
1591         struct vnode *dirp = (struct vnode *)0;
1592         nfsfh_t nfh;
1593         fhandle_t *fhp;
1594         u_quad_t tempsize;
1595         u_char cverf[NFSX_V3CREATEVERF];
1596         struct mount *mp = NULL;
1597         struct vnode *vp;
1598
1599         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1600 #ifndef nolint
1601         rdev = 0;
1602 #endif
1603         ndclear(&nd);
1604
1605         fhp = &nfh.fh_generic;
1606         nfsm_srvmtofh(fhp);
1607         if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
1608                 error = ESTALE;
1609                 goto ereply;
1610         }
1611         if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
1612                 mp = NULL;
1613                 goto ereply;
1614         }
1615         (void) vn_start_write(vp, &mp, V_WAIT);
1616         vput(vp);
1617         nfsm_srvnamesiz(len);
1618
1619         nd.ni_cnd.cn_cred = cred;
1620         nd.ni_cnd.cn_nameiop = CREATE;
1621         nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
1622
1623         /*
1624          * Call namei and do initial cleanup to get a few things
1625          * out of the way.  If we get an initial error we cleanup
1626          * and return here to avoid special-casing the invalid nd
1627          * structure through the rest of the case.  dirp may be
1628          * set even if an error occurs, but the nd structure will not
1629          * be valid at all if an error occurs so we have to invalidate it
1630          * prior to calling nfsm_reply ( which might goto nfsmout ).
1631          */
1632         error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1633                 &dirp, td, FALSE);
1634         if (dirp) {
1635                 if (v3) {
1636                         dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
1637                                 td);
1638                 } else {
1639                         vrele(dirp);
1640                         dirp = NULL;
1641                 }
1642         }
1643         if (error) {
1644                 nfsm_reply(NFSX_WCCDATA(v3));
1645                 if (v3)
1646                         nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1647                 error = 0;
1648                 goto nfsmout;
1649         }
1650
1651         /*
1652          * No error.  Continue.  State:
1653          *
1654          *      startdir        is valid ( we release this immediately )
1655          *      dirp            may be valid
1656          *      nd.ni_vp        may be valid
1657          *      nd.ni_dvp       is valid
1658          *
1659          * The error state is set through the code and we may also do some
1660          * opportunistic releasing of vnodes to avoid holding locks through
1661          * NFS I/O.  The cleanup at the end is a catch-all
1662          */
1663
1664         VATTR_NULL(vap);
1665         if (v3) {
1666                 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1667                 how = fxdr_unsigned(int, *tl);
1668                 switch (how) {
1669                 case NFSV3CREATE_GUARDED:
1670                         if (nd.ni_vp) {
1671                                 error = EEXIST;
1672                                 break;
1673                         }
1674                         /* fall through */
1675                 case NFSV3CREATE_UNCHECKED:
1676                         nfsm_srvsattr(vap);
1677                         break;
1678                 case NFSV3CREATE_EXCLUSIVE:
1679                         cp = nfsm_dissect(caddr_t, NFSX_V3CREATEVERF);
1680                         bcopy(cp, cverf, NFSX_V3CREATEVERF);
1681                         exclusive_flag = 1;
1682                         if (nd.ni_vp == NULL)
1683                                 vap->va_mode = 0;
1684                         break;
1685                 };
1686                 vap->va_type = VREG;
1687         } else {
1688                 sp = nfsm_dissect(struct nfsv2_sattr *, NFSX_V2SATTR);
1689                 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1690                 if (vap->va_type == VNON)
1691                         vap->va_type = VREG;
1692                 vap->va_mode = nfstov_mode(sp->sa_mode);
1693                 switch (vap->va_type) {
1694                 case VREG:
1695                         tsize = fxdr_unsigned(int32_t, sp->sa_size);
1696                         if (tsize != -1)
1697                                 vap->va_size = (u_quad_t)tsize;
1698                         break;
1699                 case VCHR:
1700                 case VBLK:
1701                 case VFIFO:
1702                         rdev = fxdr_unsigned(long, sp->sa_size);
1703                         break;
1704                 default:
1705                         break;
1706                 };
1707         }
1708
1709         /*
1710          * Iff doesn't exist, create it
1711          * otherwise just truncate to 0 length
1712          *   should I set the mode too ?
1713          *
1714          * The only possible error we can have at this point is EEXIST.
1715          * nd.ni_vp will also be non-NULL in that case.
1716          */
1717         if (nd.ni_vp == NULL) {
1718                 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1719                         error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1720                         if (error)
1721                                 NDFREE(&nd, NDF_ONLY_PNBUF);
1722                         else {
1723                                 nfsrv_object_create(nd.ni_vp);
1724                                 if (exclusive_flag) {
1725                                         exclusive_flag = 0;
1726                                         VATTR_NULL(vap);
1727                                         bcopy(cverf, (caddr_t)&vap->va_atime,
1728                                                 NFSX_V3CREATEVERF);
1729                                         error = VOP_SETATTR(nd.ni_vp, vap, cred,
1730                                                 td);
1731                                 }
1732                         }
1733                 } else if (
1734                         vap->va_type == VCHR ||
1735                         vap->va_type == VBLK ||
1736                         vap->va_type == VFIFO
1737                 ) {
1738                         /*
1739                          * Handle SysV FIFO node special cases.  All other
1740                          * devices require super user to access.
1741                          */
1742                         if (vap->va_type == VCHR && rdev == 0xffffffff)
1743                                 vap->va_type = VFIFO;
1744                         if (vap->va_type != VFIFO &&
1745                             (error = suser_xxx(cred, 0, 0))) {
1746                                 goto nfsmreply0;
1747                         }
1748                         vap->va_rdev = rdev;
1749                         error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1750                         if (error) {
1751                                 NDFREE(&nd, NDF_ONLY_PNBUF);
1752                                 goto nfsmreply0;
1753                         }
1754                         vput(nd.ni_vp);
1755                         nd.ni_vp = NULL;
1756
1757                         /*
1758                          * release dvp prior to lookup
1759                          */
1760                         vput(nd.ni_dvp);
1761                         nd.ni_dvp = NULL;
1762
1763                         /*
1764                          * Setup for lookup.
1765                          *
1766                          * Even though LOCKPARENT was cleared, ni_dvp may
1767                          * be garbage.
1768                          */
1769                         nd.ni_cnd.cn_nameiop = LOOKUP;
1770                         nd.ni_cnd.cn_flags &= ~(LOCKPARENT);
1771                         nd.ni_cnd.cn_thread = td;
1772                         nd.ni_cnd.cn_cred = cred;
1773
1774                         error = lookup(&nd);
1775                         nd.ni_dvp = NULL;
1776
1777                         if (error != 0) {
1778                                 nfsm_reply(0);
1779                                 /* fall through on certain errors */
1780                         }
1781                         nfsrv_object_create(nd.ni_vp);
1782                         if (nd.ni_cnd.cn_flags & ISSYMLINK) {
1783                                 error = EINVAL;
1784                                 goto nfsmreply0;
1785                         }
1786                 } else {
1787                         error = ENXIO;
1788                 }
1789         } else {
1790                 if (vap->va_size != -1) {
1791                         error = nfsrv_access(nd.ni_vp, VWRITE, cred,
1792                             (nd.ni_cnd.cn_flags & RDONLY), td, 0);
1793                         if (!error) {
1794                                 tempsize = vap->va_size;
1795                                 VATTR_NULL(vap);
1796                                 vap->va_size = tempsize;
1797                                 error = VOP_SETATTR(nd.ni_vp, vap, cred,
1798                                          td);
1799                         }
1800                 }
1801         }
1802
1803         if (!error) {
1804                 bzero((caddr_t)fhp, sizeof(nfh));
1805                 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
1806                 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
1807                 if (!error)
1808                         error = VOP_GETATTR(nd.ni_vp, vap, cred, td);
1809         }
1810         if (v3) {
1811                 if (exclusive_flag && !error &&
1812                         bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1813                         error = EEXIST;
1814                 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
1815                 vrele(dirp);
1816                 dirp = NULL;
1817         }
1818 ereply:
1819         nfsm_reply(NFSX_SRVFH(v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1820         if (v3) {
1821                 if (!error) {
1822                         nfsm_srvpostop_fh(fhp);
1823                         nfsm_srvpostop_attr(0, vap);
1824                 }
1825                 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1826                 error = 0;
1827         } else {
1828                 /* v2 non-error case (see nfsm_reply). */
1829                 nfsm_srvfhtom(fhp, v3);
1830                 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
1831                 nfsm_srvfillattr(vap, fp);
1832         }
1833         goto nfsmout;
1834
1835 nfsmreply0:
1836         nfsm_reply(0);
1837 nfsmout:
1838         if (nd.ni_startdir) {
1839                 vrele(nd.ni_startdir);
1840                 nd.ni_startdir = NULL;
1841         }
1842         if (dirp)
1843                 vrele(dirp);
1844         NDFREE(&nd, NDF_ONLY_PNBUF);
1845         if (nd.ni_dvp) {
1846                 if (nd.ni_dvp == nd.ni_vp)
1847                         vrele(nd.ni_dvp);
1848                 else
1849                         vput(nd.ni_dvp);
1850         }
1851         if (nd.ni_vp)
1852                 vput(nd.ni_vp);
1853         vn_finished_write(mp);
1854         return (error);
1855 }
1856
1857 /*
1858  * nfs v3 mknod service
1859  */
1860 int
1861 nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1862     struct thread *td, struct mbuf **mrq)
1863 {
1864         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1865         struct sockaddr *nam = nfsd->nd_nam;
1866         caddr_t dpos = nfsd->nd_dpos;
1867         struct ucred *cred = &nfsd->nd_cr;
1868         struct vattr va, dirfor, diraft;
1869         struct vattr *vap = &va;
1870         u_int32_t *tl;
1871         struct nameidata nd;
1872         caddr_t bpos;
1873         int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
1874         u_int32_t major, minor;
1875         enum vtype vtyp;
1876         struct mbuf *mb, *mreq;
1877         struct vnode *vp, *dirp = (struct vnode *)0;
1878         nfsfh_t nfh;
1879         fhandle_t *fhp;
1880         struct mount *mp = NULL;
1881         int v3 = (nfsd->nd_flag & ND_NFSV3);
1882
1883         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1884         if (!v3)
1885                 panic("nfsrv_mknod: v3 proc called on a v2 connection");
1886         ndclear(&nd);
1887
1888         fhp = &nfh.fh_generic;
1889         nfsm_srvmtofh(fhp);
1890         if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
1891                 error = ESTALE;
1892                 goto ereply;
1893         }
1894         if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
1895                 mp = NULL;
1896                 goto ereply;
1897         }
1898         (void) vn_start_write(vp, &mp, V_WAIT);
1899         vput(vp);
1900         vp = NULL;
1901         nfsm_srvnamesiz(len);
1902
1903         nd.ni_cnd.cn_cred = cred;
1904         nd.ni_cnd.cn_nameiop = CREATE;
1905         nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
1906
1907         /*
1908          * Handle nfs_namei() call.  If an error occurs, the nd structure
1909          * is not valid.  However, nfsm_*() routines may still jump to
1910          * nfsmout.
1911          */
1912
1913         error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1914                 &dirp, td, FALSE);
1915         if (dirp)
1916                 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, td);
1917         if (error) {
1918                 nfsm_reply(NFSX_WCCDATA(1));
1919                 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1920                 error = 0;
1921                 goto nfsmout;
1922         }
1923         tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1924         vtyp = nfsv3tov_type(*tl);
1925         if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1926                 error = NFSERR_BADTYPE;
1927                 goto out;
1928         }
1929         VATTR_NULL(vap);
1930         nfsm_srvsattr(vap);
1931         if (vtyp == VCHR || vtyp == VBLK) {
1932                 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
1933                 major = fxdr_unsigned(u_int32_t, *tl++);
1934                 minor = fxdr_unsigned(u_int32_t, *tl);
1935                 vap->va_rdev = makeudev(major, minor);
1936         }
1937
1938         /*
1939          * Iff doesn't exist, create it.
1940          */
1941         if (nd.ni_vp) {
1942                 error = EEXIST;
1943                 goto out;
1944         }
1945         vap->va_type = vtyp;
1946         if (vtyp == VSOCK) {
1947                 vrele(nd.ni_startdir);
1948                 nd.ni_startdir = NULL;
1949                 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1950                 if (error)
1951                         NDFREE(&nd, NDF_ONLY_PNBUF);
1952         } else {
1953                 if (vtyp != VFIFO && (error = suser_xxx(cred, 0, 0)))
1954                         goto out;
1955                 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1956                 if (error) {
1957                         NDFREE(&nd, NDF_ONLY_PNBUF);
1958                         goto out;
1959                 }
1960                 vput(nd.ni_vp);
1961                 nd.ni_vp = NULL;
1962
1963                 /*
1964                  * Release dvp prior to lookup
1965                  */
1966                 vput(nd.ni_dvp);
1967                 nd.ni_dvp = NULL;
1968
1969                 nd.ni_cnd.cn_nameiop = LOOKUP;
1970                 nd.ni_cnd.cn_flags &= ~(LOCKPARENT);
1971                 nd.ni_cnd.cn_thread = td;
1972                 nd.ni_cnd.cn_cred = td->td_proc->p_ucred;
1973
1974                 error = lookup(&nd);
1975                 nd.ni_dvp = NULL;
1976
1977                 if (error)
1978                         goto out;
1979                 if (nd.ni_cnd.cn_flags & ISSYMLINK)
1980                         error = EINVAL;
1981         }
1982
1983         /*
1984          * send response, cleanup, return.
1985          */
1986 out:
1987         if (nd.ni_startdir) {
1988                 vrele(nd.ni_startdir);
1989                 nd.ni_startdir = NULL;
1990         }
1991         NDFREE(&nd, NDF_ONLY_PNBUF);
1992         if (nd.ni_dvp) {
1993                 if (nd.ni_dvp == nd.ni_vp)
1994                         vrele(nd.ni_dvp);
1995                 else
1996                         vput(nd.ni_dvp);
1997                 nd.ni_dvp = NULL;
1998         }
1999         vp = nd.ni_vp;
2000         if (!error) {
2001                 bzero((caddr_t)fhp, sizeof(nfh));
2002                 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2003                 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2004                 if (!error)
2005                         error = VOP_GETATTR(vp, vap, cred, td);
2006                 vput(vp);
2007                 vp = NULL;
2008                 nd.ni_vp = NULL;
2009         }
2010         diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2011         if (dirp) {
2012                 vrele(dirp);
2013                 dirp = NULL;
2014         }
2015 ereply:
2016         nfsm_reply(NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1));
2017         if (v3) {
2018                 if (!error) {
2019                         nfsm_srvpostop_fh(fhp);
2020                         nfsm_srvpostop_attr(0, vap);
2021                 }
2022                 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2023         }
2024         vn_finished_write(mp);
2025         return (0);
2026 nfsmout:
2027         if (dirp)
2028                 vrele(dirp);
2029         if (nd.ni_startdir)
2030                 vrele(nd.ni_startdir);
2031         NDFREE(&nd, NDF_ONLY_PNBUF);
2032         if (nd.ni_dvp) {
2033                 if (nd.ni_dvp == nd.ni_vp)
2034                         vrele(nd.ni_dvp);
2035                 else
2036                         vput(nd.ni_dvp);
2037         }
2038         if (nd.ni_vp)
2039                 vput(nd.ni_vp);
2040         vn_finished_write(mp);
2041         return (error);
2042 }
2043
2044 /*
2045  * nfs remove service
2046  */
2047 int
2048 nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2049     struct thread *td, struct mbuf **mrq)
2050 {
2051         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2052         struct sockaddr *nam = nfsd->nd_nam;
2053         caddr_t dpos = nfsd->nd_dpos;
2054         struct ucred *cred = &nfsd->nd_cr;
2055         struct nameidata nd;
2056         caddr_t bpos;
2057         int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2058         int v3 = (nfsd->nd_flag & ND_NFSV3);
2059         struct mbuf *mb, *mreq;
2060         struct vnode *dirp;
2061         struct vattr dirfor, diraft;
2062         nfsfh_t nfh;
2063         fhandle_t *fhp;
2064         struct mount *mp = NULL;
2065         struct vnode *vp;
2066
2067         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2068         ndclear(&nd);
2069
2070         fhp = &nfh.fh_generic;
2071         nfsm_srvmtofh(fhp);
2072         if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2073                 error = ESTALE;
2074                 goto ereply;
2075         }
2076         if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
2077                 mp = NULL;
2078                 goto ereply;
2079         }
2080         (void) vn_start_write(vp, &mp, V_WAIT);
2081         vput(vp);
2082         vp = NULL;
2083         nfsm_srvnamesiz(len);
2084
2085         nd.ni_cnd.cn_cred = cred;
2086         nd.ni_cnd.cn_nameiop = DELETE;
2087         nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
2088         error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2089                 &dirp, td, FALSE);
2090         if (dirp) {
2091                 if (v3) {
2092                         dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2093                                 td);
2094                 } else {
2095                         vrele(dirp);
2096                         dirp = NULL;
2097                 }
2098         }
2099         if (error == 0) {
2100                 if (nd.ni_vp->v_type == VDIR) {
2101                         error = EPERM;          /* POSIX */
2102                         goto out;
2103                 }
2104                 /*
2105                  * The root of a mounted filesystem cannot be deleted.
2106                  */
2107                 if (nd.ni_vp->v_flag & VROOT) {
2108                         error = EBUSY;
2109                         goto out;
2110                 }
2111 out:
2112                 if (!error) {
2113                         error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2114                         NDFREE(&nd, NDF_ONLY_PNBUF);
2115                 }
2116         }
2117         if (dirp && v3) {
2118                 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2119                 vrele(dirp);
2120                 dirp = NULL;
2121         }
2122 ereply:
2123         nfsm_reply(NFSX_WCCDATA(v3));
2124         if (v3) {
2125                 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2126                 error = 0;
2127         }
2128 nfsmout:
2129         NDFREE(&nd, NDF_ONLY_PNBUF);
2130         if (nd.ni_dvp) {
2131                 if (nd.ni_dvp == nd.ni_vp)
2132                         vrele(nd.ni_dvp);
2133                 else
2134                         vput(nd.ni_dvp);
2135         }
2136         if (nd.ni_vp)
2137                 vput(nd.ni_vp);
2138         vn_finished_write(mp);
2139         return(error);
2140 }
2141
2142 /*
2143  * nfs rename service
2144  */
2145 int
2146 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2147     struct thread *td, struct mbuf **mrq)
2148 {
2149         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2150         struct sockaddr *nam = nfsd->nd_nam;
2151         caddr_t dpos = nfsd->nd_dpos;
2152         struct ucred *cred = &nfsd->nd_cr;
2153         caddr_t bpos;
2154         int error = 0, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2155         int tdirfor_ret = 1, tdiraft_ret = 1;
2156         int v3 = (nfsd->nd_flag & ND_NFSV3);
2157         struct mbuf *mb, *mreq;
2158         struct nameidata fromnd, tond;
2159         struct vnode *fvp, *tvp, *tdvp, *fdirp = (struct vnode *)0;
2160         struct vnode *tdirp = (struct vnode *)0;
2161         struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2162         nfsfh_t fnfh, tnfh;
2163         fhandle_t *ffhp, *tfhp;
2164         uid_t saved_uid;
2165         struct mount *mp = NULL;
2166         struct vnode *vp;
2167
2168         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2169 #ifndef nolint
2170         fvp = (struct vnode *)0;
2171 #endif
2172         ffhp = &fnfh.fh_generic;
2173         tfhp = &tnfh.fh_generic;
2174
2175         /*
2176          * Clear fields incase goto nfsmout occurs from macro.
2177          */
2178
2179         ndclear(&fromnd);
2180         ndclear(&tond);
2181
2182         nfsm_srvmtofh(ffhp);
2183         if ((mp = vfs_getvfs(&ffhp->fh_fsid)) == NULL) {
2184                 error = ESTALE;
2185                 goto out1;
2186         }
2187         if ((error = VFS_FHTOVP(mp, &ffhp->fh_fid, &vp)) != 0) {
2188                 mp = NULL;
2189                 goto out1;
2190         }
2191         (void) vn_start_write(vp, &mp, V_WAIT);
2192         vput(vp);
2193         vp = NULL;
2194         nfsm_srvnamesiz(len);
2195         /*
2196          * Remember our original uid so that we can reset cr_uid before
2197          * the second nfs_namei() call, in case it is remapped.
2198          */
2199         saved_uid = cred->cr_uid;
2200         fromnd.ni_cnd.cn_cred = cred;
2201         fromnd.ni_cnd.cn_nameiop = DELETE;
2202         fromnd.ni_cnd.cn_flags = WANTPARENT | SAVESTART;
2203         error = nfs_namei(&fromnd, ffhp, len, slp, nam, &md,
2204                 &dpos, &fdirp, td, FALSE);
2205         if (fdirp) {
2206                 if (v3) {
2207                         fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, cred,
2208                                 td);
2209                 } else {
2210                         vrele(fdirp);
2211                         fdirp = NULL;
2212                 }
2213         }
2214         if (error) {
2215                 nfsm_reply(2 * NFSX_WCCDATA(v3));
2216                 if (v3) {
2217                         nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2218                         nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2219                 }
2220                 error = 0;
2221                 goto nfsmout;
2222         }
2223         fvp = fromnd.ni_vp;
2224         nfsm_srvmtofh(tfhp);
2225         nfsm_strsiz(len2, NFS_MAXNAMLEN);
2226         cred->cr_uid = saved_uid;
2227         tond.ni_cnd.cn_cred = cred;
2228         tond.ni_cnd.cn_nameiop = RENAME;
2229         tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART;
2230         error = nfs_namei(&tond, tfhp, len2, slp, nam, &md,
2231                 &dpos, &tdirp, td, FALSE);
2232         if (tdirp) {
2233                 if (v3) {
2234                         tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, cred,
2235                                 td);
2236                 } else {
2237                         vrele(tdirp);
2238                         tdirp = NULL;
2239                 }
2240         }
2241         if (error)
2242                 goto out1;
2243
2244         tdvp = tond.ni_dvp;
2245         tvp = tond.ni_vp;
2246         if (tvp != NULL) {
2247                 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2248                         if (v3)
2249                                 error = EEXIST;
2250                         else
2251                                 error = EISDIR;
2252                         goto out;
2253                 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2254                         if (v3)
2255                                 error = EEXIST;
2256                         else
2257                                 error = ENOTDIR;
2258                         goto out;
2259                 }
2260                 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
2261                         if (v3)
2262                                 error = EXDEV;
2263                         else
2264                                 error = ENOTEMPTY;
2265                         goto out;
2266                 }
2267         }
2268         if (fvp->v_type == VDIR && fvp->v_mountedhere) {
2269                 if (v3)
2270                         error = EXDEV;
2271                 else
2272                         error = ENOTEMPTY;
2273                 goto out;
2274         }
2275         if (fvp->v_mount != tdvp->v_mount) {
2276                 if (v3)
2277                         error = EXDEV;
2278                 else
2279                         error = ENOTEMPTY;
2280                 goto out;
2281         }
2282         if (fvp == tdvp) {
2283                 if (v3)
2284                         error = EINVAL;
2285                 else
2286                         error = ENOTEMPTY;
2287         }
2288         /*
2289          * If source is the same as the destination (that is the
2290          * same vnode with the same name in the same directory),
2291          * then there is nothing to do.
2292          */
2293         if (fvp == tvp && fromnd.ni_dvp == tdvp &&
2294             fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
2295             !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
2296               fromnd.ni_cnd.cn_namelen))
2297                 error = -1;
2298 out:
2299         if (!error) {
2300                 /*
2301                  * The VOP_RENAME function releases all vnode references &
2302                  * locks prior to returning so we need to clear the pointers
2303                  * to bypass cleanup code later on.
2304                  */
2305                 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2306                                    tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2307                 fromnd.ni_dvp = NULL;
2308                 fromnd.ni_vp = NULL;
2309                 tond.ni_dvp = NULL;
2310                 tond.ni_vp = NULL;
2311                 if (error) {
2312                         fromnd.ni_cnd.cn_flags &= ~HASBUF;
2313                         tond.ni_cnd.cn_flags &= ~HASBUF;
2314                 }
2315         } else {
2316                 if (error == -1)
2317                         error = 0;
2318         }
2319         /* fall through */
2320
2321 out1:
2322         if (fdirp)
2323                 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, cred, td);
2324         if (tdirp)
2325                 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, cred, td);
2326         nfsm_reply(2 * NFSX_WCCDATA(v3));
2327         if (v3) {
2328                 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2329                 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2330         }
2331         error = 0;
2332         /* fall through */
2333
2334 nfsmout:
2335         /*
2336          * Clear out tond related fields
2337          */
2338         if (tdirp)
2339                 vrele(tdirp);
2340         if (tond.ni_startdir)
2341                 vrele(tond.ni_startdir);
2342         NDFREE(&tond, NDF_ONLY_PNBUF);
2343         if (tond.ni_dvp) {
2344                 if (tond.ni_dvp == tond.ni_vp)
2345                         vrele(tond.ni_dvp);
2346                 else
2347                         vput(tond.ni_dvp);
2348         }
2349         if (tond.ni_vp)
2350                 vput(tond.ni_vp);
2351
2352         /*
2353          * Clear out fromnd related fields
2354          */
2355         if (fdirp)
2356                 vrele(fdirp);
2357         if (fromnd.ni_startdir)
2358                 vrele(fromnd.ni_startdir);
2359         NDFREE(&fromnd, NDF_ONLY_PNBUF);
2360         if (fromnd.ni_dvp)
2361                 vrele(fromnd.ni_dvp);
2362         if (fromnd.ni_vp)
2363                 vrele(fromnd.ni_vp);
2364
2365         vn_finished_write(mp);
2366         return (error);
2367 }
2368
2369 /*
2370  * nfs link service
2371  */
2372 int
2373 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2374     struct thread *td, struct mbuf **mrq)
2375 {
2376         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2377         struct sockaddr *nam = nfsd->nd_nam;
2378         caddr_t dpos = nfsd->nd_dpos;
2379         struct ucred *cred = &nfsd->nd_cr;
2380         struct nameidata nd;
2381         caddr_t bpos;
2382         int error = 0, rdonly, len, dirfor_ret = 1, diraft_ret = 1;
2383         int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
2384         struct mbuf *mb, *mreq;
2385         struct vnode *vp = NULL, *xp, *dirp = (struct vnode *)0;
2386         struct vattr dirfor, diraft, at;
2387         nfsfh_t nfh, dnfh;
2388         fhandle_t *fhp, *dfhp;
2389         struct mount *mp = NULL;
2390
2391         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2392         ndclear(&nd);
2393
2394         fhp = &nfh.fh_generic;
2395         dfhp = &dnfh.fh_generic;
2396         nfsm_srvmtofh(fhp);
2397         if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2398                 error = ESTALE;
2399                 goto ereply;
2400         }
2401         if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
2402                 mp = NULL;
2403                 goto ereply;
2404         }
2405         (void) vn_start_write(vp, &mp, V_WAIT);
2406         vput(vp);
2407         vp = NULL;
2408         nfsm_srvmtofh(dfhp);
2409         nfsm_srvnamesiz(len);
2410
2411         error = nfsrv_fhtovp(fhp, FALSE, &vp, cred, slp, nam, &rdonly, TRUE);
2412         if (error) {
2413                 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2414                 if (v3) {
2415                         nfsm_srvpostop_attr(getret, &at);
2416                         nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2417                 }
2418                 vp = NULL;
2419                 error = 0;
2420                 goto nfsmout;
2421         }
2422         if (vp->v_type == VDIR) {
2423                 error = EPERM;          /* POSIX */
2424                 goto out1;
2425         }
2426         nd.ni_cnd.cn_cred = cred;
2427         nd.ni_cnd.cn_nameiop = CREATE;
2428         nd.ni_cnd.cn_flags = LOCKPARENT;
2429         error = nfs_namei(&nd, dfhp, len, slp, nam, &md, &dpos,
2430                 &dirp, td, FALSE);
2431         if (dirp) {
2432                 if (v3) {
2433                         dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2434                                 td);
2435                 } else {
2436                         vrele(dirp);
2437                         dirp = NULL;
2438                 }
2439         }
2440         if (error)
2441                 goto out1;
2442
2443         xp = nd.ni_vp;
2444         if (xp != NULL) {
2445                 error = EEXIST;
2446                 goto out;
2447         }
2448         xp = nd.ni_dvp;
2449         if (vp->v_mount != xp->v_mount)
2450                 error = EXDEV;
2451 out:
2452         if (!error) {
2453                 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
2454                 NDFREE(&nd, NDF_ONLY_PNBUF);
2455         }
2456         /* fall through */
2457
2458 out1:
2459         if (v3)
2460                 getret = VOP_GETATTR(vp, &at, cred, td);
2461         if (dirp)
2462                 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2463 ereply:
2464         nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2465         if (v3) {
2466                 nfsm_srvpostop_attr(getret, &at);
2467                 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2468                 error = 0;
2469         }
2470         /* fall through */
2471
2472 nfsmout:
2473         NDFREE(&nd, NDF_ONLY_PNBUF);
2474         if (dirp)
2475                 vrele(dirp);
2476         if (vp)
2477                 vrele(vp);
2478         if (nd.ni_dvp) {
2479                 if (nd.ni_dvp == nd.ni_vp)
2480                         vrele(nd.ni_dvp);
2481                 else
2482                         vput(nd.ni_dvp);
2483         }
2484         if (nd.ni_vp)
2485                 vrele(nd.ni_vp);
2486         vn_finished_write(mp);
2487         return(error);
2488 }
2489
2490 /*
2491  * nfs symbolic link service
2492  */
2493 int
2494 nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2495     struct thread *td, struct mbuf **mrq)
2496 {
2497         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2498         struct sockaddr *nam = nfsd->nd_nam;
2499         caddr_t dpos = nfsd->nd_dpos;
2500         struct ucred *cred = &nfsd->nd_cr;
2501         struct vattr va, dirfor, diraft;
2502         struct nameidata nd;
2503         struct vattr *vap = &va;
2504         struct nfsv2_sattr *sp;
2505         char *bpos, *pathcp = (char *)0;
2506         struct uio io;
2507         struct iovec iv;
2508         int error = 0, len, len2, dirfor_ret = 1, diraft_ret = 1;
2509         int v3 = (nfsd->nd_flag & ND_NFSV3);
2510         struct mbuf *mb, *mreq;
2511         struct vnode *dirp = (struct vnode *)0;
2512         nfsfh_t nfh;
2513         fhandle_t *fhp;
2514         struct mount *mp = NULL;
2515         struct vnode *vp;
2516
2517         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2518         ndclear(&nd);
2519
2520         fhp = &nfh.fh_generic;
2521         nfsm_srvmtofh(fhp);
2522         if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2523                 error = ESTALE;
2524                 goto out;
2525         }
2526         if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
2527                 mp = NULL;
2528                 goto out;
2529         }
2530         (void) vn_start_write(vp, &mp, V_WAIT);
2531         vput(vp);
2532         vp = NULL;
2533         nfsm_srvnamesiz(len);
2534         nd.ni_cnd.cn_cred = cred;
2535         nd.ni_cnd.cn_nameiop = CREATE;
2536         nd.ni_cnd.cn_flags = LOCKPARENT | SAVESTART;
2537         error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2538                 &dirp, td, FALSE);
2539         if (dirp) {
2540                 if (v3) {
2541                         dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2542                                 td);
2543                 } else {
2544                         vrele(dirp);
2545                         dirp = NULL;
2546                 }
2547         }
2548         if (error)
2549                 goto out;
2550
2551         VATTR_NULL(vap);
2552         if (v3)
2553                 nfsm_srvsattr(vap);
2554         nfsm_strsiz(len2, NFS_MAXPATHLEN);
2555         MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
2556         iv.iov_base = pathcp;
2557         iv.iov_len = len2;
2558         io.uio_resid = len2;
2559         io.uio_offset = 0;
2560         io.uio_iov = &iv;
2561         io.uio_iovcnt = 1;
2562         io.uio_segflg = UIO_SYSSPACE;
2563         io.uio_rw = UIO_READ;
2564         io.uio_td = (struct thread *)0;
2565         nfsm_mtouio(&io, len2);
2566         if (!v3) {
2567                 sp = nfsm_dissect(struct nfsv2_sattr *, NFSX_V2SATTR);
2568                 vap->va_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
2569         }
2570         *(pathcp + len2) = '\0';
2571         if (nd.ni_vp) {
2572                 error = EEXIST;
2573                 goto out;
2574         }
2575
2576         /*
2577          * issue symlink op.  SAVESTART is set so the underlying path component
2578          * is only freed by the VOP if an error occurs.
2579          */
2580         error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap, pathcp);
2581         if (error)
2582                 NDFREE(&nd, NDF_ONLY_PNBUF);
2583         else
2584                 vput(nd.ni_vp);
2585         nd.ni_vp = NULL;
2586         /*
2587          * releases directory prior to potential lookup op.
2588          */
2589         vput(nd.ni_dvp);
2590         nd.ni_dvp = NULL;
2591
2592         if (error == 0) {
2593             if (v3) {
2594                 /*
2595                  * Issue lookup.  Leave SAVESTART set so we can easily free
2596                  * the name buffer later on.
2597                  *
2598                  * since LOCKPARENT is not set, ni_dvp will be garbage on
2599                  * return whether an error occurs or not.
2600                  */
2601                 nd.ni_cnd.cn_nameiop = LOOKUP;
2602                 nd.ni_cnd.cn_flags &= ~(LOCKPARENT | FOLLOW);
2603                 nd.ni_cnd.cn_flags |= (NOFOLLOW | LOCKLEAF);
2604                 nd.ni_cnd.cn_thread = td;
2605                 nd.ni_cnd.cn_cred = cred;
2606
2607                 error = lookup(&nd);
2608                 nd.ni_dvp = NULL;
2609
2610                 if (error == 0) {
2611                         bzero((caddr_t)fhp, sizeof(nfh));
2612                         fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2613                         error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2614                         if (!error)
2615                                 error = VOP_GETATTR(nd.ni_vp, vap, cred,
2616                                         td);
2617                         vput(nd.ni_vp);
2618                         nd.ni_vp = NULL;
2619                 }
2620             }
2621         }
2622 out:
2623         /*
2624          * These releases aren't strictly required, does even doing them
2625          * make any sense? XXX can nfsm_reply() block?
2626          */
2627         if (pathcp) {
2628                 FREE(pathcp, M_TEMP);
2629                 pathcp = NULL;
2630         }
2631         if (dirp) {
2632                 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2633                 vrele(dirp);
2634                 dirp = NULL;
2635         }
2636         if (nd.ni_startdir) {
2637                 vrele(nd.ni_startdir);
2638                 nd.ni_startdir = NULL;
2639         }
2640         nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2641         if (v3) {
2642                 if (!error) {
2643                         nfsm_srvpostop_fh(fhp);
2644                         nfsm_srvpostop_attr(0, vap);
2645                 }
2646                 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2647         }
2648         error = 0;
2649         /* fall through */
2650
2651 nfsmout:
2652         NDFREE(&nd, NDF_ONLY_PNBUF);
2653         if (nd.ni_dvp) {
2654                 if (nd.ni_dvp == nd.ni_vp)
2655                         vrele(nd.ni_dvp);
2656                 else
2657                         vput(nd.ni_dvp);
2658         }
2659         if (nd.ni_vp)
2660                 vrele(nd.ni_vp);
2661         if (nd.ni_startdir)
2662                 vrele(nd.ni_startdir);
2663         if (dirp)
2664                 vrele(dirp);
2665         if (pathcp)
2666                 FREE(pathcp, M_TEMP);
2667
2668         vn_finished_write(mp);
2669         return (error);
2670 }
2671
2672 /*
2673  * nfs mkdir service
2674  */
2675 int
2676 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2677     struct thread *td, struct mbuf **mrq)
2678 {
2679         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2680         struct sockaddr *nam = nfsd->nd_nam;
2681         caddr_t dpos = nfsd->nd_dpos;
2682         struct ucred *cred = &nfsd->nd_cr;
2683         struct vattr va, dirfor, diraft;
2684         struct vattr *vap = &va;
2685         struct nfs_fattr *fp;
2686         struct nameidata nd;
2687         u_int32_t *tl;
2688         caddr_t bpos;
2689         int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2690         int v3 = (nfsd->nd_flag & ND_NFSV3);
2691         struct mbuf *mb, *mreq;
2692         struct vnode *dirp = NULL;
2693         int vpexcl = 0;
2694         nfsfh_t nfh;
2695         fhandle_t *fhp;
2696         struct mount *mp = NULL;
2697         struct vnode *vp;
2698
2699         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2700         ndclear(&nd);
2701
2702         fhp = &nfh.fh_generic;
2703         nfsm_srvmtofh(fhp);
2704         if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2705                 error = ESTALE;
2706                 goto out;
2707         }
2708         if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
2709                 mp = NULL;
2710                 goto out;
2711         }
2712         (void) vn_start_write(vp, &mp, V_WAIT);
2713         vput(vp);
2714         vp = NULL;
2715         nfsm_srvnamesiz(len);
2716         nd.ni_cnd.cn_cred = cred;
2717         nd.ni_cnd.cn_nameiop = CREATE;
2718         nd.ni_cnd.cn_flags = LOCKPARENT;
2719
2720         error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2721                 &dirp, td, FALSE);
2722         if (dirp) {
2723                 if (v3) {
2724                         dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2725                                 td);
2726                 } else {
2727                         vrele(dirp);
2728                         dirp = NULL;
2729                 }
2730         }
2731         if (error) {
2732                 nfsm_reply(NFSX_WCCDATA(v3));
2733                 if (v3)
2734                         nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2735                 error = 0;
2736                 goto nfsmout;
2737         }
2738         VATTR_NULL(vap);
2739         if (v3) {
2740                 nfsm_srvsattr(vap);
2741         } else {
2742                 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
2743                 vap->va_mode = nfstov_mode(*tl++);
2744         }
2745
2746         /*
2747          * At this point nd.ni_dvp is referenced and exclusively locked and
2748          * nd.ni_vp, if it exists, is referenced but not locked.
2749          */
2750
2751         vap->va_type = VDIR;
2752         if (nd.ni_vp != NULL) {
2753                 NDFREE(&nd, NDF_ONLY_PNBUF);
2754                 error = EEXIST;
2755                 goto out;
2756         }
2757
2758         /*
2759          * Issue mkdir op.  Since SAVESTART is not set, the pathname
2760          * component is freed by the VOP call.  This will fill-in
2761          * nd.ni_vp, reference, and exclusively lock it.
2762          */
2763         error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
2764         NDFREE(&nd, NDF_ONLY_PNBUF);
2765         vpexcl = 1;
2766
2767         vput(nd.ni_dvp);
2768         nd.ni_dvp = NULL;
2769
2770         if (!error) {
2771                 bzero((caddr_t)fhp, sizeof(nfh));
2772                 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2773                 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2774                 if (!error)
2775                         error = VOP_GETATTR(nd.ni_vp, vap, cred, td);
2776         }
2777 out:
2778         if (dirp)
2779                 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2780         nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2781         if (v3) {
2782                 if (!error) {
2783                         nfsm_srvpostop_fh(fhp);
2784                         nfsm_srvpostop_attr(0, vap);
2785                 }
2786                 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2787         } else {
2788                 /* v2, non-error case (see nfsm_reply) */
2789                 nfsm_srvfhtom(fhp, v3);
2790                 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
2791                 nfsm_srvfillattr(vap, fp);
2792         }
2793         error = 0;
2794         /* fall through */
2795
2796 nfsmout:
2797         if (dirp)
2798                 vrele(dirp);
2799         if (nd.ni_dvp) {
2800                 NDFREE(&nd, NDF_ONLY_PNBUF);
2801                 if (nd.ni_dvp == nd.ni_vp && vpexcl)
2802                         vrele(nd.ni_dvp);
2803                 else
2804                         vput(nd.ni_dvp);
2805         }
2806         if (nd.ni_vp) {
2807                 if (vpexcl)
2808                         vput(nd.ni_vp);
2809                 else
2810                         vrele(nd.ni_vp);
2811         }
2812         vn_finished_write(mp);
2813         return (error);
2814 }
2815
2816 /*
2817  * nfs rmdir service
2818  */
2819 int
2820 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2821     struct thread *td, struct mbuf **mrq)
2822 {
2823         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2824         struct sockaddr *nam = nfsd->nd_nam;
2825         caddr_t dpos = nfsd->nd_dpos;
2826         struct ucred *cred = &nfsd->nd_cr;
2827         caddr_t bpos;
2828         int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2829         int v3 = (nfsd->nd_flag & ND_NFSV3);
2830         struct mbuf *mb, *mreq;
2831         struct vnode *vp, *dirp = (struct vnode *)0;
2832         struct vattr dirfor, diraft;
2833         nfsfh_t nfh;
2834         fhandle_t *fhp;
2835         struct nameidata nd;
2836         struct mount *mp = NULL;
2837
2838         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2839         ndclear(&nd);
2840
2841         fhp = &nfh.fh_generic;
2842         nfsm_srvmtofh(fhp);
2843         if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2844                 error = ESTALE;
2845                 goto out;
2846         }
2847         if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
2848                 mp = NULL;
2849                 goto out;
2850         }
2851         (void) vn_start_write(vp, &mp, V_WAIT);
2852         vput(vp);
2853         vp = NULL;
2854         nfsm_srvnamesiz(len);
2855         nd.ni_cnd.cn_cred = cred;
2856         nd.ni_cnd.cn_nameiop = DELETE;
2857         nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
2858         error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2859                 &dirp, td, FALSE);
2860         if (dirp) {
2861                 if (v3) {
2862                         dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2863                                 td);
2864                 } else {
2865                         vrele(dirp);
2866                         dirp = NULL;
2867                 }
2868         }
2869         if (error) {
2870                 nfsm_reply(NFSX_WCCDATA(v3));
2871                 if (v3)
2872                         nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2873                 error = 0;
2874                 goto nfsmout;
2875         }
2876         vp = nd.ni_vp;
2877         if (vp->v_type != VDIR) {
2878                 error = ENOTDIR;
2879                 goto out;
2880         }
2881         /*
2882          * No rmdir "." please.
2883          */
2884         if (nd.ni_dvp == vp) {
2885                 error = EINVAL;
2886                 goto out;
2887         }
2888         /*
2889          * The root of a mounted filesystem cannot be deleted.
2890          */
2891         if (vp->v_flag & VROOT)
2892                 error = EBUSY;
2893 out:
2894         /*
2895          * Issue or abort op.  Since SAVESTART is not set, path name
2896          * component is freed by the VOP after either.
2897          */
2898         if (!error)
2899                 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2900         NDFREE(&nd, NDF_ONLY_PNBUF);
2901
2902         if (dirp)
2903                 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2904         nfsm_reply(NFSX_WCCDATA(v3));
2905         if (v3) {
2906                 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2907                 error = 0;
2908         }
2909         /* fall through */
2910
2911 nfsmout:
2912         NDFREE(&nd, NDF_ONLY_PNBUF);
2913         if (dirp)
2914                 vrele(dirp);
2915         if (nd.ni_dvp) {
2916                 if (nd.ni_dvp == nd.ni_vp)
2917                         vrele(nd.ni_dvp);
2918                 else
2919                         vput(nd.ni_dvp);
2920         }
2921         if (nd.ni_vp)
2922                 vput(nd.ni_vp);
2923
2924         vn_finished_write(mp);
2925         return(error);
2926 }
2927
2928 /*
2929  * nfs readdir service
2930  * - mallocs what it thinks is enough to read
2931  *      count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2932  * - calls VOP_READDIR()
2933  * - loops around building the reply
2934  *      if the output generated exceeds count break out of loop
2935  *      The nfsm_clget macro is used here so that the reply will be packed
2936  *      tightly in mbuf clusters.
2937  * - it only knows that it has encountered eof when the VOP_READDIR()
2938  *      reads nothing
2939  * - as such one readdir rpc will return eof false although you are there
2940  *      and then the next will return eof
2941  * - it trims out records with d_fileno == 0
2942  *      this doesn't matter for Unix clients, but they might confuse clients
2943  *      for other os'.
2944  * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2945  *      than requested, but this may not apply to all filesystems. For
2946  *      example, client NFS does not { although it is never remote mounted
2947  *      anyhow }
2948  *     The alternate call nfsrv_readdirplus() does lookups as well.
2949  * PS: The NFS protocol spec. does not clarify what the "count" byte
2950  *      argument is a count of.. just name strings and file id's or the
2951  *      entire reply rpc or ...
2952  *      I tried just file name and id sizes and it confused the Sun client,
2953  *      so I am using the full rpc size now. The "paranoia.." comment refers
2954  *      to including the status longwords that are not a part of the dir.
2955  *      "entry" structures, but are in the rpc.
2956  */
2957 struct flrep {
2958         nfsuint64       fl_off;
2959         u_int32_t       fl_postopok;
2960         u_int32_t       fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2961         u_int32_t       fl_fhok;
2962         u_int32_t       fl_fhsize;
2963         u_int32_t       fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2964 };
2965
2966 int
2967 nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2968     struct thread *td, struct mbuf **mrq)
2969 {
2970         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2971         struct sockaddr *nam = nfsd->nd_nam;
2972         caddr_t dpos = nfsd->nd_dpos;
2973         struct ucred *cred = &nfsd->nd_cr;
2974         char *bp, *be;
2975         struct mbuf *mp;
2976         struct dirent *dp;
2977         caddr_t cp;
2978         u_int32_t *tl;
2979         caddr_t bpos;
2980         struct mbuf *mb, *mreq;
2981         char *cpos, *cend, *rbuf;
2982         struct vnode *vp = NULL;
2983         struct vattr at;
2984         nfsfh_t nfh;
2985         fhandle_t *fhp;
2986         struct uio io;
2987         struct iovec iv;
2988         int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2989         int siz, cnt, fullsiz, eofflag, rdonly, ncookies;
2990         int v3 = (nfsd->nd_flag & ND_NFSV3);
2991         u_quad_t off, toff, verf;
2992         u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
2993
2994         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2995         fhp = &nfh.fh_generic;
2996         nfsm_srvmtofh(fhp);
2997         if (v3) {
2998                 tl = nfsm_dissect(u_int32_t *, 5 * NFSX_UNSIGNED);
2999                 toff = fxdr_hyper(tl);
3000                 tl += 2;
3001                 verf = fxdr_hyper(tl);
3002                 tl += 2;
3003         } else {
3004                 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
3005                 toff = fxdr_unsigned(u_quad_t, *tl++);
3006                 verf = 0;       /* shut up gcc */
3007         }
3008         off = toff;
3009         cnt = fxdr_unsigned(int, *tl);
3010         siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3011         xfer = NFS_SRVMAXDATA(nfsd);
3012         if (siz > xfer)
3013                 siz = xfer;
3014         fullsiz = siz;
3015         error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
3016         if (!error && vp->v_type != VDIR) {
3017                 error = ENOTDIR;
3018                 vput(vp);
3019                 vp = NULL;
3020         }
3021         if (error) {
3022                 nfsm_reply(NFSX_UNSIGNED);
3023                 if (v3)
3024                         nfsm_srvpostop_attr(getret, &at);
3025                 error = 0;
3026                 goto nfsmout;
3027         }
3028
3029         /*
3030          * Obtain lock on vnode for this section of the code
3031          */
3032         if (v3) {
3033                 error = getret = VOP_GETATTR(vp, &at, cred, td);
3034 #if 0
3035                 /*
3036                  * XXX This check may be too strict for Solaris 2.5 clients.
3037                  */
3038                 if (!error && toff && verf && verf != at.va_filerev)
3039                         error = NFSERR_BAD_COOKIE;
3040 #endif
3041         }
3042         if (!error)
3043                 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
3044         if (error) {
3045                 vput(vp);
3046                 vp = NULL;
3047                 nfsm_reply(NFSX_POSTOPATTR(v3));
3048                 if (v3)
3049                         nfsm_srvpostop_attr(getret, &at);
3050                 error = 0;
3051                 goto nfsmout;
3052         }
3053         VOP_UNLOCK(vp, 0, td);
3054
3055         /*
3056          * end section.  Allocate rbuf and continue
3057          */
3058         MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3059 again:
3060         iv.iov_base = rbuf;
3061         iv.iov_len = fullsiz;
3062         io.uio_iov = &iv;
3063         io.uio_iovcnt = 1;
3064         io.uio_offset = (off_t)off;
3065         io.uio_resid = fullsiz;
3066         io.uio_segflg = UIO_SYSSPACE;
3067         io.uio_rw = UIO_READ;
3068         io.uio_td = (struct thread *)0;
3069         eofflag = 0;
3070         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3071         if (cookies) {
3072                 free((caddr_t)cookies, M_TEMP);
3073                 cookies = NULL;
3074         }
3075         error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3076         off = (off_t)io.uio_offset;
3077         if (!cookies && !error)
3078                 error = NFSERR_PERM;
3079         if (v3) {
3080                 getret = VOP_GETATTR(vp, &at, cred, td);
3081                 if (!error)
3082                         error = getret;
3083         }
3084         VOP_UNLOCK(vp, 0, td);
3085         if (error) {
3086                 vrele(vp);
3087                 vp = NULL;
3088                 free((caddr_t)rbuf, M_TEMP);
3089                 if (cookies)
3090                         free((caddr_t)cookies, M_TEMP);
3091                 nfsm_reply(NFSX_POSTOPATTR(v3));
3092                 if (v3)
3093                         nfsm_srvpostop_attr(getret, &at);
3094                 error = 0;
3095                 goto nfsmout;
3096         }
3097         if (io.uio_resid) {
3098                 siz -= io.uio_resid;
3099
3100                 /*
3101                  * If nothing read, return eof
3102                  * rpc reply
3103                  */
3104                 if (siz == 0) {
3105                         vrele(vp);
3106                         vp = NULL;
3107                         nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
3108                                 2 * NFSX_UNSIGNED);
3109                         if (v3) {
3110                                 nfsm_srvpostop_attr(getret, &at);
3111                                 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
3112                                 txdr_hyper(at.va_filerev, tl);
3113                                 tl += 2;
3114                         } else
3115                                 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
3116                         *tl++ = nfsrv_nfs_false;
3117                         *tl = nfsrv_nfs_true;
3118                         FREE((caddr_t)rbuf, M_TEMP);
3119                         FREE((caddr_t)cookies, M_TEMP);
3120                         error = 0;
3121                         goto nfsmout;
3122                 }
3123         }
3124
3125         /*
3126          * Check for degenerate cases of nothing useful read.
3127          * If so go try again
3128          */
3129         cpos = rbuf;
3130         cend = rbuf + siz;
3131         dp = (struct dirent *)cpos;
3132         cookiep = cookies;
3133         /*
3134          * For some reason FreeBSD's ufs_readdir() chooses to back the
3135          * directory offset up to a block boundary, so it is necessary to
3136          * skip over the records that precede the requested offset. This
3137          * requires the assumption that file offset cookies monotonically
3138          * increase.
3139          */
3140         while (cpos < cend && ncookies > 0 &&
3141                 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3142                  ((u_quad_t)(*cookiep)) <= toff)) {
3143                 cpos += dp->d_reclen;
3144                 dp = (struct dirent *)cpos;
3145                 cookiep++;
3146                 ncookies--;
3147         }
3148         if (cpos >= cend || ncookies == 0) {
3149                 toff = off;
3150                 siz = fullsiz;
3151                 goto again;
3152         }
3153
3154         len = 3 * NFSX_UNSIGNED;        /* paranoia, probably can be 0 */
3155         nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
3156         if (v3) {
3157                 nfsm_srvpostop_attr(getret, &at);
3158                 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
3159                 txdr_hyper(at.va_filerev, tl);
3160         }
3161         mp = mb;
3162         bp = bpos;
3163         be = bp + M_TRAILINGSPACE(mp);
3164
3165         /* Loop through the records and build reply */
3166         while (cpos < cend && ncookies > 0) {
3167                 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3168                         nlen = dp->d_namlen;
3169                         rem = nfsm_rndup(nlen) - nlen;
3170                         len += (4 * NFSX_UNSIGNED + nlen + rem);
3171                         if (v3)
3172                                 len += 2 * NFSX_UNSIGNED;
3173                         if (len > cnt) {
3174                                 eofflag = 0;
3175                                 break;
3176                         }
3177                         /*
3178                          * Build the directory record xdr from
3179                          * the dirent entry.
3180                          */
3181                         nfsm_clget;
3182                         *tl = nfsrv_nfs_true;
3183                         bp += NFSX_UNSIGNED;
3184                         if (v3) {
3185                                 nfsm_clget;
3186                                 *tl = 0;
3187                                 bp += NFSX_UNSIGNED;
3188                         }
3189                         nfsm_clget;
3190                         *tl = txdr_unsigned(dp->d_fileno);
3191                         bp += NFSX_UNSIGNED;
3192                         nfsm_clget;
3193                         *tl = txdr_unsigned(nlen);
3194                         bp += NFSX_UNSIGNED;
3195
3196                         /* And loop around copying the name */
3197                         xfer = nlen;
3198                         cp = dp->d_name;
3199                         while (xfer > 0) {
3200                                 nfsm_clget;
3201                                 if ((bp+xfer) > be)
3202                                         tsiz = be-bp;
3203                                 else
3204                                         tsiz = xfer;
3205                                 bcopy(cp, bp, tsiz);
3206                                 bp += tsiz;
3207                                 xfer -= tsiz;
3208                                 if (xfer > 0)
3209                                         cp += tsiz;
3210                         }
3211                         /* And null pad to a int32_t boundary */
3212                         for (i = 0; i < rem; i++)
3213                                 *bp++ = '\0';
3214                         nfsm_clget;
3215
3216                         /* Finish off the record */
3217                         if (v3) {
3218                                 *tl = 0;
3219                                 bp += NFSX_UNSIGNED;
3220                                 nfsm_clget;
3221                         }
3222                         *tl = txdr_unsigned(*cookiep);
3223                         bp += NFSX_UNSIGNED;
3224                 }
3225                 cpos += dp->d_reclen;
3226                 dp = (struct dirent *)cpos;
3227                 cookiep++;
3228                 ncookies--;
3229         }
3230         vrele(vp);
3231         vp = NULL;
3232         nfsm_clget;
3233         *tl = nfsrv_nfs_false;
3234         bp += NFSX_UNSIGNED;
3235         nfsm_clget;
3236         if (eofflag)
3237                 *tl = nfsrv_nfs_true;
3238         else
3239                 *tl = nfsrv_nfs_false;
3240         bp += NFSX_UNSIGNED;
3241         if (mp != mb) {
3242                 if (bp < be)
3243                         mp->m_len = bp - mtod(mp, caddr_t);
3244         } else
3245                 mp->m_len += bp - bpos;
3246         FREE((caddr_t)rbuf, M_TEMP);
3247         FREE((caddr_t)cookies, M_TEMP);
3248
3249 nfsmout:
3250         if (vp)
3251                 vrele(vp);
3252         return(error);
3253 }
3254
3255 int
3256 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3257     struct thread *td, struct mbuf **mrq)
3258 {
3259         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3260         struct sockaddr *nam = nfsd->nd_nam;
3261         caddr_t dpos = nfsd->nd_dpos;
3262         struct ucred *cred = &nfsd->nd_cr;
3263         char *bp, *be;
3264         struct mbuf *mp;
3265         struct dirent *dp;
3266         caddr_t cp;
3267         u_int32_t *tl;
3268         caddr_t bpos;
3269         struct mbuf *mb, *mreq;
3270         char *cpos, *cend, *rbuf;
3271         struct vnode *vp = NULL, *nvp;
3272         struct flrep fl;
3273         nfsfh_t nfh;
3274         fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3275         struct uio io;
3276         struct iovec iv;
3277         struct vattr va, at, *vap = &va;
3278         struct nfs_fattr *fp;
3279         int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3280         int siz, cnt, fullsiz, eofflag, rdonly, dirlen, ncookies;
3281         u_quad_t off, toff, verf;
3282         u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3283         int v3 = (nfsd->nd_flag & ND_NFSV3);
3284
3285         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3286         if (!v3)
3287                 panic("nfsrv_readdirplus: v3 proc called on a v2 connection");
3288         fhp = &nfh.fh_generic;
3289         nfsm_srvmtofh(fhp);
3290         tl = nfsm_dissect(u_int32_t *, 6 * NFSX_UNSIGNED);
3291         toff = fxdr_hyper(tl);
3292         tl += 2;
3293         verf = fxdr_hyper(tl);
3294         tl += 2;
3295         siz = fxdr_unsigned(int, *tl++);
3296         cnt = fxdr_unsigned(int, *tl);
3297         off = toff;
3298         siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3299         xfer = NFS_SRVMAXDATA(nfsd);
3300         if (siz > xfer)
3301                 siz = xfer;
3302         fullsiz = siz;
3303         error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
3304         if (!error && vp->v_type != VDIR) {
3305                 error = ENOTDIR;
3306                 vput(vp);
3307                 vp = NULL;
3308         }
3309         if (error) {
3310                 nfsm_reply(NFSX_UNSIGNED);
3311                 nfsm_srvpostop_attr(getret, &at);
3312                 error = 0;
3313                 goto nfsmout;
3314         }
3315         error = getret = VOP_GETATTR(vp, &at, cred, td);
3316 #if 0
3317         /*
3318          * XXX This check may be too strict for Solaris 2.5 clients.
3319          */
3320         if (!error && toff && verf && verf != at.va_filerev)
3321                 error = NFSERR_BAD_COOKIE;
3322 #endif
3323         if (!error)
3324                 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
3325         if (error) {
3326                 vput(vp);
3327                 vp = NULL;
3328                 nfsm_reply(NFSX_V3POSTOPATTR);
3329                 nfsm_srvpostop_attr(getret, &at);
3330                 error = 0;
3331                 goto nfsmout;
3332         }
3333         VOP_UNLOCK(vp, 0, td);
3334         MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3335 again:
3336         iv.iov_base = rbuf;
3337         iv.iov_len = fullsiz;
3338         io.uio_iov = &iv;
3339         io.uio_iovcnt = 1;
3340         io.uio_offset = (off_t)off;
3341         io.uio_resid = fullsiz;
3342         io.uio_segflg = UIO_SYSSPACE;
3343         io.uio_rw = UIO_READ;
3344         io.uio_td = (struct thread *)0;
3345         eofflag = 0;
3346         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3347         if (cookies) {
3348                 free((caddr_t)cookies, M_TEMP);
3349                 cookies = NULL;
3350         }
3351         error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3352         off = (u_quad_t)io.uio_offset;
3353         getret = VOP_GETATTR(vp, &at, cred, td);
3354         VOP_UNLOCK(vp, 0, td);
3355         if (!cookies && !error)
3356                 error = NFSERR_PERM;
3357         if (!error)
3358                 error = getret;
3359         if (error) {
3360                 vrele(vp);
3361                 vp = NULL;
3362                 if (cookies)
3363                         free((caddr_t)cookies, M_TEMP);
3364                 free((caddr_t)rbuf, M_TEMP);
3365                 nfsm_reply(NFSX_V3POSTOPATTR);
3366                 nfsm_srvpostop_attr(getret, &at);
3367                 error = 0;
3368                 goto nfsmout;
3369         }
3370         if (io.uio_resid) {
3371                 siz -= io.uio_resid;
3372
3373                 /*
3374                  * If nothing read, return eof
3375                  * rpc reply
3376                  */
3377                 if (siz == 0) {
3378                         vrele(vp);
3379                         vp = NULL;
3380                         nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3381                                 2 * NFSX_UNSIGNED);
3382                         nfsm_srvpostop_attr(getret, &at);
3383                         tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
3384                         txdr_hyper(at.va_filerev, tl);
3385                         tl += 2;
3386                         *tl++ = nfsrv_nfs_false;
3387                         *tl = nfsrv_nfs_true;
3388                         FREE((caddr_t)cookies, M_TEMP);
3389                         FREE((caddr_t)rbuf, M_TEMP);
3390                         error = 0;
3391                         goto nfsmout;
3392                 }
3393         }
3394
3395         /*
3396          * Check for degenerate cases of nothing useful read.
3397          * If so go try again
3398          */
3399         cpos = rbuf;
3400         cend = rbuf + siz;
3401         dp = (struct dirent *)cpos;
3402         cookiep = cookies;
3403         /*
3404          * For some reason FreeBSD's ufs_readdir() chooses to back the
3405          * directory offset up to a block boundary, so it is necessary to
3406          * skip over the records that precede the requested offset. This
3407          * requires the assumption that file offset cookies monotonically
3408          * increase.
3409          */
3410         while (cpos < cend && ncookies > 0 &&
3411                 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3412                  ((u_quad_t)(*cookiep)) <= toff)) {
3413                 cpos += dp->d_reclen;
3414                 dp = (struct dirent *)cpos;
3415                 cookiep++;
3416                 ncookies--;
3417         }
3418         if (cpos >= cend || ncookies == 0) {
3419                 toff = off;
3420                 siz = fullsiz;
3421                 goto again;
3422         }
3423
3424         /*
3425          * Probe one of the directory entries to see if the filesystem
3426          * supports VGET.
3427          */
3428         if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp) == EOPNOTSUPP) {
3429                 error = NFSERR_NOTSUPP;
3430                 vrele(vp);
3431                 vp = NULL;
3432                 free((caddr_t)cookies, M_TEMP);
3433                 free((caddr_t)rbuf, M_TEMP);
3434                 nfsm_reply(NFSX_V3POSTOPATTR);
3435                 nfsm_srvpostop_attr(getret, &at);
3436                 error = 0;
3437                 goto nfsmout;
3438         }
3439         vput(nvp);
3440         nvp = NULL;
3441
3442         dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3443             2 * NFSX_UNSIGNED;
3444         nfsm_reply(cnt);
3445         nfsm_srvpostop_attr(getret, &at);
3446         tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
3447         txdr_hyper(at.va_filerev, tl);
3448         mp = mb;
3449         bp = bpos;
3450         be = bp + M_TRAILINGSPACE(mp);
3451
3452         /* Loop through the records and build reply */
3453         while (cpos < cend && ncookies > 0) {
3454                 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3455                         nlen = dp->d_namlen;
3456                         rem = nfsm_rndup(nlen)-nlen;
3457
3458                         /*
3459                          * For readdir_and_lookup get the vnode using
3460                          * the file number.
3461                          */
3462                         if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
3463                                 goto invalid;
3464                         bzero((caddr_t)nfhp, NFSX_V3FH);
3465                         nfhp->fh_fsid =
3466                                 nvp->v_mount->mnt_stat.f_fsid;
3467                         if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3468                                 vput(nvp);
3469                                 nvp = NULL;
3470                                 goto invalid;
3471                         }
3472                         if (VOP_GETATTR(nvp, vap, cred, td)) {
3473                                 vput(nvp);
3474                                 nvp = NULL;
3475                                 goto invalid;
3476                         }
3477                         vput(nvp);
3478                         nvp = NULL;
3479
3480                         /*
3481                          * If either the dircount or maxcount will be
3482                          * exceeded, get out now. Both of these lengths
3483                          * are calculated conservatively, including all
3484                          * XDR overheads.
3485                          */
3486                         len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3487                                 NFSX_V3POSTOPATTR);
3488                         dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3489                         if (len > cnt || dirlen > fullsiz) {
3490                                 eofflag = 0;
3491                                 break;
3492                         }
3493
3494                         /*
3495                          * Build the directory record xdr from
3496                          * the dirent entry.
3497                          */
3498                         fp = (struct nfs_fattr *)&fl.fl_fattr;
3499                         nfsm_srvfillattr(vap, fp);
3500                         fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3501                         fl.fl_fhok = nfsrv_nfs_true;
3502                         fl.fl_postopok = nfsrv_nfs_true;
3503                         fl.fl_off.nfsuquad[0] = 0;
3504                         fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3505
3506                         nfsm_clget;
3507                         *tl = nfsrv_nfs_true;
3508                         bp += NFSX_UNSIGNED;
3509                         nfsm_clget;
3510                         *tl = 0;
3511                         bp += NFSX_UNSIGNED;
3512                         nfsm_clget;
3513                         *tl = txdr_unsigned(dp->d_fileno);
3514                         bp += NFSX_UNSIGNED;
3515                         nfsm_clget;
3516                         *tl = txdr_unsigned(nlen);
3517                         bp += NFSX_UNSIGNED;
3518
3519                         /* And loop around copying the name */
3520                         xfer = nlen;
3521                         cp = dp->d_name;
3522                         while (xfer > 0) {
3523                                 nfsm_clget;
3524                                 if ((bp + xfer) > be)
3525                                         tsiz = be - bp;
3526                                 else
3527                                         tsiz = xfer;
3528                                 bcopy(cp, bp, tsiz);
3529                                 bp += tsiz;
3530                                 xfer -= tsiz;
3531                                 if (xfer > 0)
3532                                         cp += tsiz;
3533                         }
3534                         /* And null pad to a int32_t boundary */
3535                         for (i = 0; i < rem; i++)
3536                                 *bp++ = '\0';
3537
3538                         /*
3539                          * Now copy the flrep structure out.
3540                          */
3541                         xfer = sizeof (struct flrep);
3542                         cp = (caddr_t)&fl;
3543                         while (xfer > 0) {
3544                                 nfsm_clget;
3545                                 if ((bp + xfer) > be)
3546                                         tsiz = be - bp;
3547                                 else
3548                                         tsiz = xfer;
3549                                 bcopy(cp, bp, tsiz);
3550                                 bp += tsiz;
3551                                 xfer -= tsiz;
3552                                 if (xfer > 0)
3553                                         cp += tsiz;
3554                         }
3555                 }
3556 invalid:
3557                 cpos += dp->d_reclen;
3558                 dp = (struct dirent *)cpos;
3559                 cookiep++;
3560                 ncookies--;
3561         }
3562         vrele(vp);
3563         vp = NULL;
3564         nfsm_clget;
3565         *tl = nfsrv_nfs_false;
3566         bp += NFSX_UNSIGNED;
3567         nfsm_clget;
3568         if (eofflag)
3569                 *tl = nfsrv_nfs_true;
3570         else
3571                 *tl = nfsrv_nfs_false;
3572         bp += NFSX_UNSIGNED;
3573         if (mp != mb) {
3574                 if (bp < be)
3575                         mp->m_len = bp - mtod(mp, caddr_t);
3576         } else
3577                 mp->m_len += bp - bpos;
3578         FREE((caddr_t)cookies, M_TEMP);
3579         FREE((caddr_t)rbuf, M_TEMP);
3580 nfsmout:
3581         if (vp)
3582                 vrele(vp);
3583         return(error);
3584 }
3585
3586 /*
3587  * nfs commit service
3588  */
3589 int
3590 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3591     struct thread *td, struct mbuf **mrq)
3592 {
3593         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3594         struct sockaddr *nam = nfsd->nd_nam;
3595         caddr_t dpos = nfsd->nd_dpos;
3596         struct ucred *cred = &nfsd->nd_cr;
3597         struct vattr bfor, aft;
3598         struct vnode *vp = NULL;
3599         nfsfh_t nfh;
3600         fhandle_t *fhp;
3601         u_int32_t *tl;
3602         caddr_t bpos;
3603         int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt;
3604         struct mbuf *mb, *mreq;
3605         u_quad_t off;
3606         struct mount *mp = NULL;
3607         int v3 = (nfsd->nd_flag & ND_NFSV3);
3608
3609         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3610         if (!v3)
3611                 panic("nfsrv_commit: v3 proc called on a v2 connection");
3612         fhp = &nfh.fh_generic;
3613         nfsm_srvmtofh(fhp);
3614         if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
3615                 error = ESTALE;
3616                 goto ereply;
3617         }
3618         if ((error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp)) != 0) {
3619                 mp = NULL;
3620                 goto ereply;
3621         }
3622         (void) vn_start_write(vp, &mp, V_WAIT);
3623         vput(vp);
3624         vp = NULL;
3625         tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
3626
3627         /*
3628          * XXX At this time VOP_FSYNC() does not accept offset and byte
3629          * count parameters, so these arguments are useless (someday maybe).
3630          */
3631         off = fxdr_hyper(tl);
3632         tl += 2;
3633         cnt = fxdr_unsigned(int, *tl);
3634         error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
3635         if (error) {
3636                 nfsm_reply(2 * NFSX_UNSIGNED);
3637                 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3638                 error = 0;
3639                 goto nfsmout;
3640         }
3641         for_ret = VOP_GETATTR(vp, &bfor, cred, td);
3642
3643         if (cnt > MAX_COMMIT_COUNT) {
3644                 /*
3645                  * Give up and do the whole thing
3646                  */
3647                 if (vp->v_object &&
3648                    (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3649                         vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3650                 }
3651                 error = VOP_FSYNC(vp, cred, MNT_WAIT, td);
3652         } else {
3653                 /*
3654                  * Locate and synchronously write any buffers that fall
3655                  * into the requested range.  Note:  we are assuming that
3656                  * f_iosize is a power of 2.
3657                  */
3658                 int iosize = vp->v_mount->mnt_stat.f_iosize;
3659                 int iomask = iosize - 1;
3660                 int s;
3661                 daddr_t lblkno;
3662
3663                 /*
3664                  * Align to iosize boundry, super-align to page boundry.
3665                  */
3666                 if (off & iomask) {
3667                         cnt += off & iomask;
3668                         off &= ~(u_quad_t)iomask;
3669                 }
3670                 if (off & PAGE_MASK) {
3671                         cnt += off & PAGE_MASK;
3672                         off &= ~(u_quad_t)PAGE_MASK;
3673                 }
3674                 lblkno = off / iosize;
3675
3676                 if (vp->v_object &&
3677                    (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3678                         vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3679                 }
3680
3681                 s = splbio();
3682                 while (cnt > 0) {
3683                         struct buf *bp;
3684
3685                         /*
3686                          * If we have a buffer and it is marked B_DELWRI we
3687                          * have to lock and write it.  Otherwise the prior
3688                          * write is assumed to have already been committed.
3689                          */
3690                         if ((bp = gbincore(vp, lblkno)) != NULL && (bp->b_flags & B_DELWRI)) {
3691                                 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
3692                                         BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL);
3693                                         continue; /* retry */
3694                                 }
3695                                 bremfree(bp);
3696                                 bp->b_flags &= ~B_ASYNC;
3697                                 BUF_WRITE(bp);
3698                                 ++nfs_commit_miss;
3699                         }
3700                         ++nfs_commit_blks;
3701                         if (cnt < iosize)
3702                                 break;
3703                         cnt -= iosize;
3704                         ++lblkno;
3705                 }
3706                 splx(s);
3707         }
3708
3709         aft_ret = VOP_GETATTR(vp, &aft, cred, td);
3710         vput(vp);
3711         vp = NULL;
3712 ereply:
3713         nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3714         nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3715         if (!error) {
3716                 tl = nfsm_build(u_int32_t *, NFSX_V3WRITEVERF);
3717                 if (nfsver.tv_sec == 0)
3718                         nfsver = boottime;
3719                 *tl++ = txdr_unsigned(nfsver.tv_sec);
3720                 *tl = txdr_unsigned(nfsver.tv_usec);
3721         } else {
3722                 error = 0;
3723         }
3724 nfsmout:
3725         if (vp)
3726                 vput(vp);
3727         vn_finished_write(mp);
3728         return(error);
3729 }
3730
3731 /*
3732  * nfs statfs service
3733  */
3734 int
3735 nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3736     struct thread *td, struct mbuf **mrq)
3737 {
3738         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3739         struct sockaddr *nam = nfsd->nd_nam;
3740         caddr_t dpos = nfsd->nd_dpos;
3741         struct ucred *cred = &nfsd->nd_cr;
3742         struct statfs *sf;
3743         struct nfs_statfs *sfp;
3744         caddr_t bpos;
3745         int error = 0, rdonly, getret = 1;
3746         int v3 = (nfsd->nd_flag & ND_NFSV3);
3747         struct mbuf *mb, *mreq;
3748         struct vnode *vp = NULL;
3749         struct vattr at;
3750         nfsfh_t nfh;
3751         fhandle_t *fhp;
3752         struct statfs statfs;
3753         u_quad_t tval;
3754
3755         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3756         fhp = &nfh.fh_generic;
3757         nfsm_srvmtofh(fhp);
3758         error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
3759         if (error) {
3760                 nfsm_reply(NFSX_UNSIGNED);
3761                 if (v3)
3762                         nfsm_srvpostop_attr(getret, &at);
3763                 error = 0;
3764                 goto nfsmout;
3765         }
3766         sf = &statfs;
3767         error = VFS_STATFS(vp->v_mount, sf, td);
3768         getret = VOP_GETATTR(vp, &at, cred, td);
3769         vput(vp);
3770         vp = NULL;
3771         nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3772         if (v3)
3773                 nfsm_srvpostop_attr(getret, &at);
3774         if (error) {
3775                 error = 0;
3776                 goto nfsmout;
3777         }
3778         sfp = nfsm_build(struct nfs_statfs *, NFSX_STATFS(v3));
3779         if (v3) {
3780                 tval = (u_quad_t)sf->f_blocks;
3781                 tval *= (u_quad_t)sf->f_bsize;
3782                 txdr_hyper(tval, &sfp->sf_tbytes);
3783                 tval = (u_quad_t)sf->f_bfree;
3784                 tval *= (u_quad_t)sf->f_bsize;
3785                 txdr_hyper(tval, &sfp->sf_fbytes);
3786                 tval = (u_quad_t)sf->f_bavail;
3787                 tval *= (u_quad_t)sf->f_bsize;
3788                 txdr_hyper(tval, &sfp->sf_abytes);
3789                 sfp->sf_tfiles.nfsuquad[0] = 0;
3790                 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3791                 sfp->sf_ffiles.nfsuquad[0] = 0;
3792                 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3793                 sfp->sf_afiles.nfsuquad[0] = 0;
3794                 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3795                 sfp->sf_invarsec = 0;
3796         } else {
3797                 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3798                 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3799                 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3800                 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3801                 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3802         }
3803 nfsmout:
3804         if (vp)
3805                 vput(vp);
3806         return(error);
3807 }
3808
3809 /*
3810  * nfs fsinfo service
3811  */
3812 int
3813 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3814     struct thread *td, struct mbuf **mrq)
3815 {
3816         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3817         struct sockaddr *nam = nfsd->nd_nam;
3818         caddr_t dpos = nfsd->nd_dpos;
3819         struct ucred *cred = &nfsd->nd_cr;
3820         struct nfsv3_fsinfo *sip;
3821         caddr_t bpos;
3822         int error = 0, rdonly, getret = 1, pref;
3823         struct mbuf *mb, *mreq;
3824         struct vnode *vp = NULL;
3825         struct vattr at;
3826         nfsfh_t nfh;
3827         fhandle_t *fhp;
3828         u_quad_t maxfsize;
3829         struct statfs sb;
3830         int v3 = (nfsd->nd_flag & ND_NFSV3);
3831
3832         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3833         if (!v3)
3834                 panic("nfsrv_fsinfo: v3 proc called on a v2 connection");
3835         fhp = &nfh.fh_generic;
3836         nfsm_srvmtofh(fhp);
3837         error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
3838         if (error) {
3839                 nfsm_reply(NFSX_UNSIGNED);
3840                 nfsm_srvpostop_attr(getret, &at);
3841                 error = 0;
3842                 goto nfsmout;
3843         }
3844
3845         /* XXX Try to make a guess on the max file size. */
3846         VFS_STATFS(vp->v_mount, &sb, td);
3847         maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3848
3849         getret = VOP_GETATTR(vp, &at, cred, td);
3850         vput(vp);
3851         vp = NULL;
3852         nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3853         nfsm_srvpostop_attr(getret, &at);
3854         sip = nfsm_build(struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3855
3856         /*
3857          * XXX
3858          * There should be file system VFS OP(s) to get this information.
3859          * For now, assume ufs.
3860          */
3861         if (slp->ns_so->so_type == SOCK_DGRAM)
3862                 pref = NFS_MAXDGRAMDATA;
3863         else
3864                 pref = NFS_MAXDATA;
3865         sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3866         sip->fs_rtpref = txdr_unsigned(pref);
3867         sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3868         sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3869         sip->fs_wtpref = txdr_unsigned(pref);
3870         sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3871         sip->fs_dtpref = txdr_unsigned(pref);
3872         txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3873         sip->fs_timedelta.nfsv3_sec = 0;
3874         sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3875         sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3876                 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3877                 NFSV3FSINFO_CANSETTIME);
3878 nfsmout:
3879         if (vp)
3880                 vput(vp);
3881         return(error);
3882 }
3883
3884 /*
3885  * nfs pathconf service
3886  */
3887 int
3888 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3889     struct thread *td, struct mbuf **mrq)
3890 {
3891         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3892         struct sockaddr *nam = nfsd->nd_nam;
3893         caddr_t dpos = nfsd->nd_dpos;
3894         struct ucred *cred = &nfsd->nd_cr;
3895         struct nfsv3_pathconf *pc;
3896         caddr_t bpos;
3897         int error = 0, rdonly, getret = 1;
3898         register_t linkmax, namemax, chownres, notrunc;
3899         struct mbuf *mb, *mreq;
3900         struct vnode *vp = NULL;
3901         struct vattr at;
3902         nfsfh_t nfh;
3903         fhandle_t *fhp;
3904         int v3 = (nfsd->nd_flag & ND_NFSV3);
3905
3906         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3907         if (!v3)
3908                 panic("nfsrv_pathconf: v3 proc called on a v2 connection");
3909         fhp = &nfh.fh_generic;
3910         nfsm_srvmtofh(fhp);
3911         error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly, TRUE);
3912         if (error) {
3913                 nfsm_reply(NFSX_UNSIGNED);
3914                 nfsm_srvpostop_attr(getret, &at);
3915                 error = 0;
3916                 goto nfsmout;
3917         }
3918         error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3919         if (!error)
3920                 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3921         if (!error)
3922                 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3923         if (!error)
3924                 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, &notrunc);
3925         getret = VOP_GETATTR(vp, &at, cred, td);
3926         vput(vp);
3927         vp = NULL;
3928         nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3929         nfsm_srvpostop_attr(getret, &at);
3930         if (error) {
3931                 error = 0;
3932                 goto nfsmout;
3933         }
3934         pc = nfsm_build(struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3935
3936         pc->pc_linkmax = txdr_unsigned(linkmax);
3937         pc->pc_namemax = txdr_unsigned(namemax);
3938         pc->pc_notrunc = txdr_unsigned(notrunc);
3939         pc->pc_chownrestricted = txdr_unsigned(chownres);
3940
3941         /*
3942          * These should probably be supported by VOP_PATHCONF(), but
3943          * until msdosfs is exportable (why would you want to?), the
3944          * Unix defaults should be ok.
3945          */
3946         pc->pc_caseinsensitive = nfsrv_nfs_false;
3947         pc->pc_casepreserving = nfsrv_nfs_true;
3948 nfsmout:
3949         if (vp)
3950                 vput(vp);
3951         return(error);
3952 }
3953
3954 /*
3955  * Null operation, used by clients to ping server
3956  */
3957 /* ARGSUSED */
3958 int
3959 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3960     struct thread *td, struct mbuf **mrq)
3961 {
3962         struct mbuf *mrep = nfsd->nd_mrep;
3963         caddr_t bpos;
3964         int error = NFSERR_RETVOID;
3965         struct mbuf *mb, *mreq;
3966
3967         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3968         nfsm_reply(0);
3969 nfsmout:
3970         return (error);
3971 }
3972
3973 /*
3974  * No operation, used for obsolete procedures
3975  */
3976 /* ARGSUSED */
3977 int
3978 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3979     struct thread *td, struct mbuf **mrq)
3980 {
3981         struct mbuf *mrep = nfsd->nd_mrep;
3982         caddr_t bpos;
3983         int error;
3984         struct mbuf *mb, *mreq;
3985
3986         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3987         if (nfsd->nd_repstat)
3988                 error = nfsd->nd_repstat;
3989         else
3990                 error = EPROCUNAVAIL;
3991         nfsm_reply(0);
3992         error = 0;
3993 nfsmout:
3994         return (error);
3995 }
3996
3997 /*
3998  * Perform access checking for vnodes obtained from file handles that would
3999  * refer to files already opened by a Unix client. You cannot just use
4000  * vn_writechk() and VOP_ACCESS() for two reasons.
4001  * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
4002  * 2 - The owner is to be given access irrespective of mode bits for some
4003  *     operations, so that processes that chmod after opening a file don't
4004  *     break. I don't like this because it opens a security hole, but since
4005  *     the nfs server opens a security hole the size of a barn door anyhow,
4006  *     what the heck.
4007  *
4008  * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
4009  * will return EPERM instead of EACCESS. EPERM is always an error.
4010  */
4011 static int
4012 nfsrv_access(struct vnode *vp, int flags, struct ucred *cred, int rdonly,
4013     struct thread *td, int override)
4014 {
4015         struct vattr vattr;
4016         int error;
4017
4018         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
4019         if (flags & VWRITE) {
4020                 /* Just vn_writechk() changed to check rdonly */
4021                 /*
4022                  * Disallow write attempts on read-only file systems;
4023                  * unless the file is a socket or a block or character
4024                  * device resident on the file system.
4025                  */
4026                 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
4027                         switch (vp->v_type) {
4028                         case VREG:
4029                         case VDIR:
4030                         case VLNK:
4031                                 return (EROFS);
4032                         default:
4033                                 break;
4034                         }
4035                 }
4036                 /*
4037                  * If there's shared text associated with
4038                  * the inode, we can't allow writing.
4039                  */
4040                 if (vp->v_flag & VTEXT)
4041                         return (ETXTBSY);
4042         }
4043         error = VOP_GETATTR(vp, &vattr, cred, td);
4044         if (error)
4045                 return (error);
4046         error = VOP_ACCESS(vp, flags, cred, td);
4047         /*
4048          * Allow certain operations for the owner (reads and writes
4049          * on files that are already open).
4050          */
4051         if (override && error == EACCES && cred->cr_uid == vattr.va_uid)
4052                 error = 0;
4053         return error;
4054 }