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