Remove VOP_GETVOBJECT, VOP_DESTROYVOBJECT, and VOP_CREATEVOBJECT. Rearrange
[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.31 2006/03/29 18:45:00 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/nlookup.h>
70 #include <sys/namei.h>
71 #include <sys/unistd.h>
72 #include <sys/vnode.h>
73 #include <sys/mount.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/malloc.h>
77 #include <sys/mbuf.h>
78 #include <sys/dirent.h>
79 #include <sys/stat.h>
80 #include <sys/kernel.h>
81 #include <sys/sysctl.h>
82 #include <sys/buf.h>
83
84 #include <vm/vm.h>
85 #include <vm/vm_extern.h>
86 #include <vm/vm_zone.h>
87 #include <vm/vm_object.h>
88
89 #include <sys/buf2.h>
90
91 #include <sys/thread2.h>
92
93 #include "nfsproto.h"
94 #include "rpcv2.h"
95 #include "nfs.h"
96 #include "xdr_subs.h"
97 #include "nfsm_subs.h"
98
99 #ifdef NFSRV_DEBUG
100 #define nfsdbprintf(info)       printf info
101 #else
102 #define nfsdbprintf(info)
103 #endif
104
105 #define MAX_COMMIT_COUNT        (1024 * 1024)
106
107 #define NUM_HEURISTIC           1017
108 #define NHUSE_INIT              64
109 #define NHUSE_INC               16
110 #define NHUSE_MAX               2048
111
112 static struct nfsheur {
113     struct vnode *nh_vp;        /* vp to match (unreferenced pointer) */
114     off_t nh_nextr;             /* next offset for sequential detection */
115     int nh_use;                 /* use count for selection */
116     int nh_seqcount;            /* heuristic */
117 } nfsheur[NUM_HEURISTIC];
118
119 nfstype nfsv3_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK,
120                       NFFIFO, NFNON };
121 #ifndef NFS_NOSERVER 
122 nfstype nfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
123                       NFCHR, NFNON };
124 /* Global vars */
125 extern u_int32_t nfs_xdrneg1;
126 extern u_int32_t nfs_false, nfs_true;
127 extern enum vtype nv3tov_type[8];
128 extern struct nfsstats nfsstats;
129
130 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
131 int nfsrvw_procrastinate_v3 = 0;
132
133 static struct timespec  nfsver;
134
135 SYSCTL_DECL(_vfs_nfs);
136
137 static int nfs_async;
138 SYSCTL_INT(_vfs_nfs, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0, "");
139 static int nfs_commit_blks;
140 static int nfs_commit_miss;
141 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 0, "");
142 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 0, "");
143
144 static int nfsrv_access (struct vnode *,int,struct ucred *,int,
145                 struct thread *, int);
146 static void nfsrvw_coalesce (struct nfsrv_descript *,
147                 struct nfsrv_descript *);
148
149 /*
150  * nfs v3 access service
151  */
152 int
153 nfsrv3_access(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
154               struct thread *td, struct mbuf **mrq)
155 {
156         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
157         struct sockaddr *nam = nfsd->nd_nam;
158         caddr_t dpos = nfsd->nd_dpos;
159         struct ucred *cred = &nfsd->nd_cr;
160         struct vnode *vp = NULL;
161         nfsfh_t nfh;
162         fhandle_t *fhp;
163         u_int32_t *tl;
164         int32_t t1;
165         caddr_t bpos;
166         int error = 0, rdonly, getret;
167         char *cp2;
168         struct mbuf *mb, *mreq, *mb2;
169         struct vattr vattr, *vap = &vattr;
170         u_long testmode, nfsmode;
171
172         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
173         fhp = &nfh.fh_generic;
174         nfsm_srvmtofh(fhp);
175         nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
176         error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
177             (nfsd->nd_flag & ND_KERBAUTH), TRUE);
178         if (error) {
179                 nfsm_reply(NFSX_UNSIGNED);
180                 nfsm_srvpostop_attr(1, (struct vattr *)0);
181                 error = 0;
182                 goto nfsmout;
183         }
184         nfsmode = fxdr_unsigned(u_int32_t, *tl);
185         if ((nfsmode & NFSV3ACCESS_READ) &&
186                 nfsrv_access(vp, VREAD, cred, rdonly, td, 0))
187                 nfsmode &= ~NFSV3ACCESS_READ;
188         if (vp->v_type == VDIR)
189                 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
190                         NFSV3ACCESS_DELETE);
191         else
192                 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
193         if ((nfsmode & testmode) &&
194                 nfsrv_access(vp, VWRITE, cred, rdonly, td, 0))
195                 nfsmode &= ~testmode;
196         if (vp->v_type == VDIR)
197                 testmode = NFSV3ACCESS_LOOKUP;
198         else
199                 testmode = NFSV3ACCESS_EXECUTE;
200         if ((nfsmode & testmode) &&
201                 nfsrv_access(vp, VEXEC, cred, rdonly, td, 0))
202                 nfsmode &= ~testmode;
203         getret = VOP_GETATTR(vp, vap, td);
204         vput(vp);
205         vp = NULL;
206         nfsm_reply(NFSX_POSTOPATTR(1) + NFSX_UNSIGNED);
207         nfsm_srvpostop_attr(getret, vap);
208         nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
209         *tl = txdr_unsigned(nfsmode);
210 nfsmout:
211         if (vp)
212                 vput(vp);
213         return(error);
214 }
215
216 /*
217  * nfs getattr service
218  */
219 int
220 nfsrv_getattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
221               struct thread *td, struct mbuf **mrq)
222 {
223         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
224         struct sockaddr *nam = nfsd->nd_nam;
225         caddr_t dpos = nfsd->nd_dpos;
226         struct ucred *cred = &nfsd->nd_cr;
227         struct nfs_fattr *fp;
228         struct vattr va;
229         struct vattr *vap = &va;
230         struct vnode *vp = NULL;
231         nfsfh_t nfh;
232         fhandle_t *fhp;
233         u_int32_t *tl;
234         int32_t t1;
235         caddr_t bpos;
236         int error = 0, rdonly;
237         char *cp2;
238         struct mbuf *mb, *mb2, *mreq;
239
240         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
241         fhp = &nfh.fh_generic;
242         nfsm_srvmtofh(fhp);
243         error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
244                  &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
245         if (error) {
246                 nfsm_reply(0);
247                 error = 0;
248                 goto nfsmout;
249         }
250         error = VOP_GETATTR(vp, vap, td);
251         vput(vp);
252         vp = NULL;
253         nfsm_reply(NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
254         if (error) {
255                 error = 0;
256                 goto nfsmout;
257         }
258         nfsm_build(fp, struct nfs_fattr *, NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
259         nfsm_srvfillattr(vap, fp);
260         /* fall through */
261
262 nfsmout:
263         if (vp)
264                 vput(vp);
265         return(error);
266 }
267
268 /*
269  * nfs setattr service
270  */
271 int
272 nfsrv_setattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
273               struct thread *td, struct mbuf **mrq)
274 {
275         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
276         struct sockaddr *nam = nfsd->nd_nam;
277         caddr_t dpos = nfsd->nd_dpos;
278         struct ucred *cred = &nfsd->nd_cr;
279         struct vattr va, preat;
280         struct vattr *vap = &va;
281         struct nfsv2_sattr *sp;
282         struct nfs_fattr *fp;
283         struct vnode *vp = NULL;
284         nfsfh_t nfh;
285         fhandle_t *fhp;
286         u_int32_t *tl;
287         int32_t t1;
288         caddr_t bpos;
289         int error = 0, rdonly, preat_ret = 1, postat_ret = 1;
290         int v3 = (nfsd->nd_flag & ND_NFSV3), gcheck = 0;
291         char *cp2;
292         struct mbuf *mb, *mb2, *mreq;
293         struct timespec guard;
294
295         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
296         fhp = &nfh.fh_generic;
297         nfsm_srvmtofh(fhp);
298         VATTR_NULL(vap);
299         if (v3) {
300                 nfsm_srvsattr(vap);
301                 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
302                 gcheck = fxdr_unsigned(int, *tl);
303                 if (gcheck) {
304                         nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
305                         fxdr_nfsv3time(tl, &guard);
306                 }
307         } else {
308                 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
309                 /*
310                  * Nah nah nah nah na nah
311                  * There is a bug in the Sun client that puts 0xffff in the mode
312                  * field of sattr when it should put in 0xffffffff. The u_short
313                  * doesn't sign extend.
314                  * --> check the low order 2 bytes for 0xffff
315                  */
316                 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
317                         vap->va_mode = nfstov_mode(sp->sa_mode);
318                 if (sp->sa_uid != nfs_xdrneg1)
319                         vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
320                 if (sp->sa_gid != nfs_xdrneg1)
321                         vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
322                 if (sp->sa_size != nfs_xdrneg1)
323                         vap->va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
324                 if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
325 #ifdef notyet
326                         fxdr_nfsv2time(&sp->sa_atime, &vap->va_atime);
327 #else
328                         vap->va_atime.tv_sec =
329                                 fxdr_unsigned(int32_t, sp->sa_atime.nfsv2_sec);
330                         vap->va_atime.tv_nsec = 0;
331 #endif
332                 }
333                 if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
334                         fxdr_nfsv2time(&sp->sa_mtime, &vap->va_mtime);
335
336         }
337
338         /*
339          * Now that we have all the fields, lets do it.
340          */
341         error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
342                 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
343         if (error) {
344                 nfsm_reply(2 * NFSX_UNSIGNED);
345                 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
346                 error = 0;
347                 goto nfsmout;
348         }
349
350         /*
351          * vp now an active resource, pay careful attention to cleanup
352          */
353
354         if (v3) {
355                 error = preat_ret = VOP_GETATTR(vp, &preat, td);
356                 if (!error && gcheck &&
357                         (preat.va_ctime.tv_sec != guard.tv_sec ||
358                          preat.va_ctime.tv_nsec != guard.tv_nsec))
359                         error = NFSERR_NOT_SYNC;
360                 if (error) {
361                         vput(vp);
362                         vp = NULL;
363                         nfsm_reply(NFSX_WCCDATA(v3));
364                         nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
365                         error = 0;
366                         goto nfsmout;
367                 }
368         }
369
370         /*
371          * If the size is being changed write acces is required, otherwise
372          * just check for a read only file system.
373          */
374         if (vap->va_size == ((u_quad_t)((quad_t) -1))) {
375                 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
376                         error = EROFS;
377                         goto out;
378                 }
379         } else {
380                 if (vp->v_type == VDIR) {
381                         error = EISDIR;
382                         goto out;
383                 } else if ((error = nfsrv_access(vp, VWRITE, cred, rdonly,
384                             td, 0)) != 0){ 
385                         goto out;
386                 }
387         }
388         error = VOP_SETATTR(vp, vap, cred, td);
389         postat_ret = VOP_GETATTR(vp, vap, td);
390         if (!error)
391                 error = postat_ret;
392 out:
393         vput(vp);
394         vp = NULL;
395         nfsm_reply(NFSX_WCCORFATTR(v3));
396         if (v3) {
397                 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
398                 error = 0;
399                 goto nfsmout;
400         } else {
401                 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
402                 nfsm_srvfillattr(vap, fp);
403         }
404         /* fall through */
405
406 nfsmout:
407         if (vp)
408                 vput(vp);
409         return(error);
410 }
411
412 /*
413  * nfs lookup rpc
414  */
415 int
416 nfsrv_lookup(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
417              struct thread *td, struct mbuf **mrq)
418 {
419         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
420         struct sockaddr *nam = nfsd->nd_nam;
421         caddr_t dpos = nfsd->nd_dpos;
422         struct ucred *cred = &nfsd->nd_cr;
423         struct nfs_fattr *fp;
424         struct nlookupdata nd;
425         struct vnode *vp;
426         struct vnode *dirp;
427         struct namecache *ncp;
428         nfsfh_t nfh;
429         fhandle_t *fhp;
430         caddr_t cp;
431         u_int32_t *tl;
432         int32_t t1;
433         caddr_t bpos;
434         int error = 0, len, dirattr_ret = 1;
435         int v3 = (nfsd->nd_flag & ND_NFSV3), pubflag;
436         char *cp2;
437         struct mbuf *mb, *mb2, *mreq;
438         struct vattr va, dirattr, *vap = &va;
439
440         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
441         nlookup_zero(&nd);
442         dirp = NULL;
443         vp = NULL;
444
445         fhp = &nfh.fh_generic;
446         nfsm_srvmtofh(fhp);
447         nfsm_srvnamesiz(len);
448
449         pubflag = nfs_ispublicfh(fhp);
450
451         error = nfs_namei(&nd, cred, NAMEI_LOOKUP, NULL, &vp,
452                 fhp, len, slp, nam, &md, &dpos,
453                 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), pubflag);
454
455         /*
456          * namei failure, only dirp to cleanup.  Clear out garbarge from
457          * structure in case macros jump to nfsmout.
458          */
459
460         if (error) {
461                 if (dirp) {
462                         if (v3)
463                                 dirattr_ret = VOP_GETATTR(dirp, &dirattr, td);
464                         vrele(dirp);
465                         dirp = NULL;
466                 }
467                 nfsm_reply(NFSX_POSTOPATTR(v3));
468                 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
469                 error = 0;
470                 goto nfsmout;
471         }
472
473         /*
474          * Locate index file for public filehandle
475          *
476          * error is 0 on entry and 0 on exit from this block.
477          */
478
479         if (pubflag) {
480                 if (vp->v_type == VDIR && nfs_pub.np_index != NULL) {
481                         /*
482                          * Setup call to lookup() to see if we can find
483                          * the index file. Arguably, this doesn't belong
484                          * in a kernel.. Ugh.  If an error occurs, do not
485                          * try to install an index file and then clear the
486                          * error.
487                          *
488                          * When we replace nd with ind and redirect ndp,
489                          * maintenance of ni_startdir and ni_vp shift to
490                          * ind and we have to clean them up in the old nd.
491                          * However, the cnd resource continues to be maintained
492                          * via the original nd.  Confused?  You aren't alone!
493                          */
494                         VOP_UNLOCK(vp, 0, td);
495                         ncp = cache_hold(nd.nl_ncp);
496                         nlookup_done(&nd);
497                         error = nlookup_init_raw(&nd, nfs_pub.np_index,
498                                                 UIO_SYSSPACE, 0, cred, ncp);
499                         cache_drop(ncp);
500                         if (error == 0)
501                                 error = nlookup(&nd);
502
503                         if (error == 0) {
504                                 /*
505                                  * Found an index file. Get rid of
506                                  * the old references.  transfer vp and
507                                  * load up the new vp.  Fortunately we do
508                                  * not have to deal with dvp, that would be
509                                  * a huge mess.
510                                  */
511                                 if (dirp)       
512                                         vrele(dirp);
513                                 dirp = vp;
514                                 vp = NULL;
515                                 error = cache_vget(nd.nl_ncp, nd.nl_cred,
516                                                         LK_EXCLUSIVE, &vp);
517                                 KKASSERT(error == 0);
518                         }
519                         error = 0;
520                 }
521                 /*
522                  * If the public filehandle was used, check that this lookup
523                  * didn't result in a filehandle outside the publicly exported
524                  * filesystem.  We clear the poor vp here to avoid lockups due
525                  * to NFS I/O.
526                  */
527
528                 if (vp->v_mount != nfs_pub.np_mount) {
529                         vput(vp);
530                         vp = NULL;
531                         error = EPERM;
532                 }
533         }
534
535         if (dirp) {
536                 if (v3)
537                         dirattr_ret = VOP_GETATTR(dirp, &dirattr, td);
538                 vrele(dirp);
539                 dirp = NULL;
540         }
541
542         /*
543          * Resources at this point:
544          *      ndp->ni_vp      may not be NULL
545          *
546          */
547
548         if (error) {
549                 nfsm_reply(NFSX_POSTOPATTR(v3));
550                 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
551                 error = 0;
552                 goto nfsmout;
553         }
554
555         /*
556          * Clear out some resources prior to potentially blocking.  This
557          * is not as critical as ni_dvp resources in other routines, but
558          * it helps.
559          */
560         nlookup_done(&nd);
561
562         /*
563          * Get underlying attribute, then release remaining resources ( for
564          * the same potential blocking reason ) and reply.
565          */
566         bzero((caddr_t)fhp, sizeof(nfh));
567         fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
568         error = VFS_VPTOFH(vp, &fhp->fh_fid);
569         if (!error)
570                 error = VOP_GETATTR(vp, vap, td);
571
572         vput(vp);
573         vp = NULL;
574         nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPORFATTR(v3) + NFSX_POSTOPATTR(v3));
575         if (error) {
576                 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
577                 error = 0;
578                 goto nfsmout;
579         }
580         nfsm_srvfhtom(fhp, v3);
581         if (v3) {
582                 nfsm_srvpostop_attr(0, vap);
583                 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
584         } else {
585                 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
586                 nfsm_srvfillattr(vap, fp);
587         }
588
589 nfsmout:
590         if (dirp)
591                 vrele(dirp);
592         nlookup_done(&nd);              /* may be called twice */
593         if (vp)
594                 vput(vp);
595         return (error);
596 }
597
598 /*
599  * nfs readlink service
600  */
601 int
602 nfsrv_readlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
603                struct thread *td, struct mbuf **mrq)
604 {
605         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
606         struct sockaddr *nam = nfsd->nd_nam;
607         caddr_t dpos = nfsd->nd_dpos;
608         struct ucred *cred = &nfsd->nd_cr;
609         struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
610         struct iovec *ivp = iv;
611         struct mbuf *mp;
612         u_int32_t *tl;
613         int32_t t1;
614         caddr_t bpos;
615         int error = 0, rdonly, i, tlen, len, getret;
616         int v3 = (nfsd->nd_flag & ND_NFSV3);
617         char *cp2;
618         struct mbuf *mb, *mb2, *mp2, *mp3, *mreq;
619         struct vnode *vp = NULL;
620         struct vattr attr;
621         nfsfh_t nfh;
622         fhandle_t *fhp;
623         struct uio io, *uiop = &io;
624
625         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
626 #ifndef nolint
627         mp2 = (struct mbuf *)0;
628 #endif
629         mp3 = NULL;
630         fhp = &nfh.fh_generic;
631         nfsm_srvmtofh(fhp);
632         len = 0;
633         i = 0;
634         while (len < NFS_MAXPATHLEN) {
635                 mp = m_getcl(MB_WAIT, MT_DATA, 0);
636                 mp->m_len = MCLBYTES;
637                 if (len == 0)
638                         mp3 = mp2 = mp;
639                 else {
640                         mp2->m_next = mp;
641                         mp2 = mp;
642                 }
643                 if ((len+mp->m_len) > NFS_MAXPATHLEN) {
644                         mp->m_len = NFS_MAXPATHLEN-len;
645                         len = NFS_MAXPATHLEN;
646                 } else
647                         len += mp->m_len;
648                 ivp->iov_base = mtod(mp, caddr_t);
649                 ivp->iov_len = mp->m_len;
650                 i++;
651                 ivp++;
652         }
653         uiop->uio_iov = iv;
654         uiop->uio_iovcnt = i;
655         uiop->uio_offset = 0;
656         uiop->uio_resid = len;
657         uiop->uio_rw = UIO_READ;
658         uiop->uio_segflg = UIO_SYSSPACE;
659         uiop->uio_td = NULL;
660         error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
661                  &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
662         if (error) {
663                 nfsm_reply(2 * NFSX_UNSIGNED);
664                 nfsm_srvpostop_attr(1, (struct vattr *)0);
665                 error = 0;
666                 goto nfsmout;
667         }
668         if (vp->v_type != VLNK) {
669                 if (v3)
670                         error = EINVAL;
671                 else
672                         error = ENXIO;
673                 goto out;
674         }
675         error = VOP_READLINK(vp, uiop, cred);
676 out:
677         getret = VOP_GETATTR(vp, &attr, td);
678         vput(vp);
679         vp = NULL;
680         nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_UNSIGNED);
681         if (v3) {
682                 nfsm_srvpostop_attr(getret, &attr);
683                 if (error) {
684                         error = 0;
685                         goto nfsmout;
686                 }
687         }
688         if (uiop->uio_resid > 0) {
689                 len -= uiop->uio_resid;
690                 tlen = nfsm_rndup(len);
691                 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
692         }
693         nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
694         *tl = txdr_unsigned(len);
695         mb->m_next = mp3;
696         mp3 = NULL;
697 nfsmout:
698         if (mp3)
699                 m_freem(mp3);
700         if (vp)
701                 vput(vp);
702         return(error);
703 }
704
705 /*
706  * nfs read service
707  */
708 int
709 nfsrv_read(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
710            struct thread *td, struct mbuf **mrq)
711 {
712         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
713         struct sockaddr *nam = nfsd->nd_nam;
714         caddr_t dpos = nfsd->nd_dpos;
715         struct ucred *cred = &nfsd->nd_cr;
716         struct iovec *iv;
717         struct iovec *iv2;
718         struct mbuf *m;
719         struct nfs_fattr *fp;
720         u_int32_t *tl;
721         int32_t t1;
722         int i;
723         caddr_t bpos;
724         int error = 0, rdonly, cnt, len, left, siz, tlen, getret;
725         int v3 = (nfsd->nd_flag & ND_NFSV3), reqlen;
726         char *cp2;
727         struct mbuf *mb, *mb2, *mreq;
728         struct mbuf *m2;
729         struct vnode *vp = NULL;
730         nfsfh_t nfh;
731         fhandle_t *fhp;
732         struct uio io, *uiop = &io;
733         struct vattr va, *vap = &va;
734         struct nfsheur *nh;
735         off_t off;
736         int ioflag = 0;
737
738         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
739         fhp = &nfh.fh_generic;
740         nfsm_srvmtofh(fhp);
741         if (v3) {
742                 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
743                 off = fxdr_hyper(tl);
744         } else {
745                 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
746                 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
747         }
748         nfsm_srvstrsiz(reqlen, NFS_SRVMAXDATA(nfsd));
749
750         /*
751          * Reference vp.  If an error occurs, vp will be invalid, but we
752          * have to NULL it just in case.  The macros might goto nfsmout
753          * as well.
754          */
755
756         error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
757                  &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
758         if (error) {
759                 vp = NULL;
760                 nfsm_reply(2 * NFSX_UNSIGNED);
761                 nfsm_srvpostop_attr(1, (struct vattr *)0);
762                 error = 0;
763                 goto nfsmout;
764         }
765
766         if (vp->v_type != VREG) {
767                 if (v3)
768                         error = EINVAL;
769                 else
770                         error = (vp->v_type == VDIR) ? EISDIR : EACCES;
771         }
772         if (!error) {
773             if ((error = nfsrv_access(vp, VREAD, cred, rdonly, td, 1)) != 0)
774                 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 1);
775         }
776         getret = VOP_GETATTR(vp, vap, td);
777         if (!error)
778                 error = getret;
779         if (error) {
780                 vput(vp);
781                 vp = NULL;
782                 nfsm_reply(NFSX_POSTOPATTR(v3));
783                 nfsm_srvpostop_attr(getret, vap);
784                 error = 0;
785                 goto nfsmout;
786         }
787
788         /*
789          * Calculate byte count to read
790          */
791
792         if (off >= vap->va_size)
793                 cnt = 0;
794         else if ((off + reqlen) > vap->va_size)
795                 cnt = vap->va_size - off;
796         else
797                 cnt = reqlen;
798
799         /*
800          * Calculate seqcount for heuristic
801          */
802
803         {
804                 int hi;
805                 int try = 32;
806
807                 /*
808                  * Locate best candidate
809                  */
810
811                 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
812                 nh = &nfsheur[hi];
813
814                 while (try--) {
815                         if (nfsheur[hi].nh_vp == vp) {
816                                 nh = &nfsheur[hi];
817                                 break;
818                         }
819                         if (nfsheur[hi].nh_use > 0)
820                                 --nfsheur[hi].nh_use;
821                         hi = (hi + 1) % NUM_HEURISTIC;
822                         if (nfsheur[hi].nh_use < nh->nh_use)
823                                 nh = &nfsheur[hi];
824                 }
825
826                 if (nh->nh_vp != vp) {
827                         nh->nh_vp = vp;
828                         nh->nh_nextr = off;
829                         nh->nh_use = NHUSE_INIT;
830                         if (off == 0)
831                                 nh->nh_seqcount = 4;
832                         else
833                                 nh->nh_seqcount = 1;
834                 }
835
836                 /*
837                  * Calculate heuristic
838                  */
839
840                 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
841                         if (++nh->nh_seqcount > IO_SEQMAX)
842                                 nh->nh_seqcount = IO_SEQMAX;
843                 } else if (nh->nh_seqcount > 1) {
844                         nh->nh_seqcount = 1;
845                 } else {
846                         nh->nh_seqcount = 0;
847                 }
848                 nh->nh_use += NHUSE_INC;
849                 if (nh->nh_use > NHUSE_MAX)
850                         nh->nh_use = NHUSE_MAX;
851                 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
852         }
853
854         nfsm_reply(NFSX_POSTOPORFATTR(v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
855         if (v3) {
856                 nfsm_build(tl, u_int32_t *, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
857                 *tl++ = nfs_true;
858                 fp = (struct nfs_fattr *)tl;
859                 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
860         } else {
861                 nfsm_build(tl, u_int32_t *, NFSX_V2FATTR + NFSX_UNSIGNED);
862                 fp = (struct nfs_fattr *)tl;
863                 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
864         }
865         len = left = nfsm_rndup(cnt);
866         if (cnt > 0) {
867                 /*
868                  * Generate the mbuf list with the uio_iov ref. to it.
869                  */
870                 i = 0;
871                 m = m2 = mb;
872                 while (left > 0) {
873                         siz = min(M_TRAILINGSPACE(m), left);
874                         if (siz > 0) {
875                                 left -= siz;
876                                 i++;
877                         }
878                         if (left > 0) {
879                                 m = m_getcl(MB_WAIT, MT_DATA, 0);
880                                 m->m_len = 0;
881                                 m2->m_next = m;
882                                 m2 = m;
883                         }
884                 }
885                 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
886                        M_TEMP, M_WAITOK);
887                 uiop->uio_iov = iv2 = iv;
888                 m = mb;
889                 left = len;
890                 i = 0;
891                 while (left > 0) {
892                         if (m == NULL)
893                                 panic("nfsrv_read iov");
894                         siz = min(M_TRAILINGSPACE(m), left);
895                         if (siz > 0) {
896                                 iv->iov_base = mtod(m, caddr_t) + m->m_len;
897                                 iv->iov_len = siz;
898                                 m->m_len += siz;
899                                 left -= siz;
900                                 iv++;
901                                 i++;
902                         }
903                         m = m->m_next;
904                 }
905                 uiop->uio_iovcnt = i;
906                 uiop->uio_offset = off;
907                 uiop->uio_resid = len;
908                 uiop->uio_rw = UIO_READ;
909                 uiop->uio_segflg = UIO_SYSSPACE;
910                 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
911                 off = uiop->uio_offset;
912                 nh->nh_nextr = off;
913                 FREE((caddr_t)iv2, M_TEMP);
914                 if (error || (getret = VOP_GETATTR(vp, vap, td))) {
915                         if (!error)
916                                 error = getret;
917                         m_freem(mreq);
918                         vput(vp);
919                         vp = NULL;
920                         nfsm_reply(NFSX_POSTOPATTR(v3));
921                         nfsm_srvpostop_attr(getret, vap);
922                         error = 0;
923                         goto nfsmout;
924                 }
925         } else {
926                 uiop->uio_resid = 0;
927         }
928         vput(vp);
929         vp = NULL;
930         nfsm_srvfillattr(vap, fp);
931         tlen = len - uiop->uio_resid;
932         cnt = cnt < tlen ? cnt : tlen;
933         tlen = nfsm_rndup(cnt);
934         if (len != tlen || tlen != cnt)
935                 nfsm_adj(mb, len - tlen, tlen - cnt);
936         if (v3) {
937                 *tl++ = txdr_unsigned(cnt);
938                 if (len < reqlen)
939                         *tl++ = nfs_true;
940                 else
941                         *tl++ = nfs_false;
942         }
943         *tl = txdr_unsigned(cnt);
944 nfsmout:
945         if (vp)
946                 vput(vp);
947         return(error);
948 }
949
950 /*
951  * nfs write service
952  */
953 int
954 nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
955             struct thread *td, struct mbuf **mrq)
956 {
957         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
958         struct sockaddr *nam = nfsd->nd_nam;
959         caddr_t dpos = nfsd->nd_dpos;
960         struct ucred *cred = &nfsd->nd_cr;
961         struct iovec *ivp;
962         int i, cnt;
963         struct mbuf *mp;
964         struct nfs_fattr *fp;
965         struct iovec *iv;
966         struct vattr va, forat;
967         struct vattr *vap = &va;
968         u_int32_t *tl;
969         int32_t t1;
970         caddr_t bpos;
971         int error = 0, rdonly, len, forat_ret = 1;
972         int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
973         int stable = NFSV3WRITE_FILESYNC;
974         int v3 = (nfsd->nd_flag & ND_NFSV3);
975         char *cp2;
976         struct mbuf *mb, *mb2, *mreq;
977         struct vnode *vp = 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             mp = mrep;
1014             while (mp) {
1015                 if (mp == md) {
1016                         zeroing = 0;
1017                         adjust = dpos - mtod(mp, caddr_t);
1018                         mp->m_len -= adjust;
1019                         if (mp->m_len > 0 && adjust > 0)
1020                                 NFSMADV(mp, adjust);
1021                 }
1022                 if (zeroing)
1023                         mp->m_len = 0;
1024                 else if (mp->m_len > 0) {
1025                         i += mp->m_len;
1026                         if (i > len) {
1027                                 mp->m_len -= (i - len);
1028                                 zeroing = 1;
1029                         }
1030                         if (mp->m_len > 0)
1031                                 cnt++;
1032                 }
1033                 mp = mp->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, &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, td);
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(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             mp = mrep;
1078             while (mp) {
1079                 if (mp->m_len > 0) {
1080                         ivp->iov_base = mtod(mp, caddr_t);
1081                         ivp->iov_len = mp->m_len;
1082                         ivp++;
1083                 }
1084                 mp = mp->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, td);
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 mbuf *mp;
1163         struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1164         struct nfs_fattr *fp;
1165         int i;
1166         struct iovec *iov;
1167         struct nfsrvw_delayhash *wpp;
1168         struct ucred *cred;
1169         struct vattr va, forat;
1170         u_int32_t *tl;
1171         int32_t t1;
1172         caddr_t bpos, dpos;
1173         int error = 0, rdonly, len, forat_ret = 1;
1174         int ioflags, aftat_ret = 1, adjust, v3, zeroing;
1175         char *cp2;
1176         struct mbuf *mb, *mb2, *mreq, *mrep, *md;
1177         struct vnode *vp = 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             mp = mrep;
1229             while (mp) {
1230                 if (mp == md) {
1231                     zeroing = 0;
1232                     adjust = dpos - mtod(mp, caddr_t);
1233                     mp->m_len -= adjust;
1234                     if (mp->m_len > 0 && adjust > 0)
1235                         NFSMADV(mp, adjust);
1236                 }
1237                 if (zeroing)
1238                     mp->m_len = 0;
1239                 else {
1240                     i += mp->m_len;
1241                     if (i > len) {
1242                         mp->m_len -= (i - len);
1243                         zeroing = 1;
1244                     }
1245                 }
1246                 mp = mp->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, &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, td);
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(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                     mp = mrep;
1362                     i = 0;
1363                     while (mp) {
1364                         if (mp->m_len > 0)
1365                             i++;
1366                         mp = mp->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                     mp = mrep;
1373                     while (mp) {
1374                         if (mp->m_len > 0) {
1375                             ivp->iov_base = mtod(mp, caddr_t);
1376                             ivp->iov_len = mp->m_len;
1377                             ivp++;
1378                         }
1379                         mp = mp->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, td);
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 *mp;
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             mp = owp->nd_mrep;
1498             while (mp->m_next)
1499                 mp = mp->m_next;
1500             mp->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, rdev, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1544         int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0;
1545         caddr_t cp;
1546         char *cp2;
1547         struct mbuf *mb, *mb2, *mreq;
1548         struct vnode *dirp;
1549         struct vnode *dvp;
1550         struct vnode *vp;
1551         nfsfh_t nfh;
1552         fhandle_t *fhp;
1553         u_quad_t tempsize;
1554         u_char cverf[NFSX_V3CREATEVERF];
1555
1556         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1557 #ifndef nolint
1558         rdev = 0;
1559 #endif
1560         nlookup_zero(&nd);
1561         dirp = NULL;
1562         dvp = NULL;
1563         vp = NULL;
1564
1565         fhp = &nfh.fh_generic;
1566         nfsm_srvmtofh(fhp);
1567         nfsm_srvnamesiz(len);
1568
1569         /*
1570          * Call namei and do initial cleanup to get a few things
1571          * out of the way.  If we get an initial error we cleanup
1572          * and return here to avoid special-casing the invalid nd
1573          * structure through the rest of the case.  dirp may be
1574          * set even if an error occurs, but the nd structure will not
1575          * be valid at all if an error occurs so we have to invalidate it
1576          * prior to calling nfsm_reply ( which might goto nfsmout ).
1577          */
1578         error = nfs_namei(&nd, cred, NAMEI_CREATE, &dvp, &vp,
1579                           fhp, len, slp, nam, &md, &dpos, &dirp,
1580                           td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1581         if (dirp) {
1582                 if (v3) {
1583                         dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
1584                 } else {
1585                         vrele(dirp);
1586                         dirp = NULL;
1587                 }
1588         }
1589         if (error) {
1590                 nfsm_reply(NFSX_WCCDATA(v3));
1591                 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1592                 error = 0;
1593                 goto nfsmout;
1594         }
1595
1596         /*
1597          * No error.  Continue.  State:
1598          *
1599          *      dirp            may be valid
1600          *      vp              may be valid or NULL if the target does not
1601          *                      exist.
1602          *      dvp             is valid
1603          *
1604          * The error state is set through the code and we may also do some
1605          * opportunistic releasing of vnodes to avoid holding locks through
1606          * NFS I/O.  The cleanup at the end is a catch-all
1607          */
1608
1609         VATTR_NULL(vap);
1610         if (v3) {
1611                 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1612                 how = fxdr_unsigned(int, *tl);
1613                 switch (how) {
1614                 case NFSV3CREATE_GUARDED:
1615                         if (vp) {
1616                                 error = EEXIST;
1617                                 break;
1618                         }
1619                         /* fall through */
1620                 case NFSV3CREATE_UNCHECKED:
1621                         nfsm_srvsattr(vap);
1622                         break;
1623                 case NFSV3CREATE_EXCLUSIVE:
1624                         nfsm_dissect(cp, caddr_t, NFSX_V3CREATEVERF);
1625                         bcopy(cp, cverf, NFSX_V3CREATEVERF);
1626                         exclusive_flag = 1;
1627                         break;
1628                 };
1629                 vap->va_type = VREG;
1630         } else {
1631                 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1632                 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1633                 if (vap->va_type == VNON)
1634                         vap->va_type = VREG;
1635                 vap->va_mode = nfstov_mode(sp->sa_mode);
1636                 switch (vap->va_type) {
1637                 case VREG:
1638                         tsize = fxdr_unsigned(int32_t, sp->sa_size);
1639                         if (tsize != -1)
1640                                 vap->va_size = (u_quad_t)tsize;
1641                         break;
1642                 case VCHR:
1643                 case VBLK:
1644                 case VFIFO:
1645                         rdev = fxdr_unsigned(long, sp->sa_size);
1646                         break;
1647                 default:
1648                         break;
1649                 };
1650         }
1651
1652         /*
1653          * Iff doesn't exist, create it
1654          * otherwise just truncate to 0 length
1655          *   should I set the mode too ?
1656          *
1657          * The only possible error we can have at this point is EEXIST. 
1658          * nd.ni_vp will also be non-NULL in that case.
1659          */
1660         if (vp == NULL) {
1661                 if (vap->va_mode == (mode_t)VNOVAL)
1662                         vap->va_mode = 0;
1663                 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1664                         vput(dvp);
1665                         dvp = NULL;
1666                         error = VOP_NCREATE(nd.nl_ncp, &vp, nd.nl_cred, vap);
1667                         if (error == 0) {
1668                                 if (vap->va_type == VREG)
1669                                         vinitvmio(vp);
1670                                 if (exclusive_flag) {
1671                                         exclusive_flag = 0;
1672                                         VATTR_NULL(vap);
1673                                         bcopy(cverf, (caddr_t)&vap->va_atime,
1674                                                 NFSX_V3CREATEVERF);
1675                                         error = VOP_SETATTR(vp, vap, cred, td);
1676                                 }
1677                         }
1678                 } else if (
1679                         vap->va_type == VCHR || 
1680                         vap->va_type == VBLK ||
1681                         vap->va_type == VFIFO
1682                 ) {
1683                         /*
1684                          * Handle SysV FIFO node special cases.  All other
1685                          * devices require super user to access.
1686                          */
1687                         if (vap->va_type == VCHR && rdev == 0xffffffff)
1688                                 vap->va_type = VFIFO;
1689                         if (vap->va_type != VFIFO &&
1690                             (error = suser_cred(cred, 0))) {
1691                                 goto nfsmreply0;
1692                         }
1693                         vap->va_rdev = rdev;
1694
1695                         vput(dvp);
1696                         dvp = NULL;
1697                         error = VOP_NMKNOD(nd.nl_ncp, &vp, nd.nl_cred, vap);
1698                         if (error)
1699                                 goto nfsmreply0;
1700 #if 0
1701                         /*
1702                          * XXX what is this junk supposed to do ?
1703                          */
1704
1705                         vput(vp);
1706                         vp = NULL;
1707
1708                         /*
1709                          * release dvp prior to lookup
1710                          */
1711                         vput(dvp);
1712                         dvp = NULL;
1713
1714                         /*
1715                          * Setup for lookup. 
1716                          *
1717                          * Even though LOCKPARENT was cleared, ni_dvp may
1718                          * be garbage. 
1719                          */
1720                         nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
1721                         nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT);
1722                         nd.ni_cnd.cn_td = td;
1723                         nd.ni_cnd.cn_cred = cred;
1724
1725                         error = lookup(&nd);
1726                         nd.ni_dvp = NULL;
1727
1728                         if (error != 0) {
1729                                 nfsm_reply(0);
1730                                 /* fall through on certain errors */
1731                         }
1732                         nfsrv_object_create(nd.ni_vp);
1733                         if (nd.ni_cnd.cn_flags & CNP_ISSYMLINK) {
1734                                 error = EINVAL;
1735                                 goto nfsmreply0;
1736                         }
1737 #endif
1738                 } else {
1739                         error = ENXIO;
1740                 }
1741         } else {
1742                 if (vap->va_size != -1) {
1743                         error = nfsrv_access(vp, VWRITE, cred,
1744                             (nd.nl_flags & NLC_NFS_RDONLY), td, 0);
1745                         if (!error) {
1746                                 tempsize = vap->va_size;
1747                                 VATTR_NULL(vap);
1748                                 vap->va_size = tempsize;
1749                                 error = VOP_SETATTR(vp, vap, cred, td);
1750                         }
1751                 }
1752         }
1753
1754         if (!error) {
1755                 bzero((caddr_t)fhp, sizeof(nfh));
1756                 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1757                 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1758                 if (!error)
1759                         error = VOP_GETATTR(vp, vap, td);
1760         }
1761         if (v3) {
1762                 if (exclusive_flag && !error &&
1763                         bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1764                         error = EEXIST;
1765                 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
1766                 vrele(dirp);
1767                 dirp = NULL;
1768         }
1769         nfsm_reply(NFSX_SRVFH(v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1770         if (v3) {
1771                 if (!error) {
1772                         nfsm_srvpostop_fh(fhp);
1773                         nfsm_srvpostop_attr(0, vap);
1774                 }
1775                 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1776                 error = 0;
1777         } else {
1778                 nfsm_srvfhtom(fhp, v3);
1779                 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1780                 nfsm_srvfillattr(vap, fp);
1781         }
1782         goto nfsmout;
1783
1784 nfsmreply0:
1785         nfsm_reply(0);
1786         error = 0;
1787         /* fall through */
1788
1789 nfsmout:
1790         if (dirp)
1791                 vrele(dirp);
1792         nlookup_done(&nd);
1793         if (dvp) {
1794                 if (dvp == vp)
1795                         vrele(dvp);
1796                 else
1797                         vput(dvp);
1798         }
1799         if (vp)
1800                 vput(vp);
1801         return (error);
1802 }
1803
1804 /*
1805  * nfs v3 mknod service
1806  */
1807 int
1808 nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1809             struct thread *td, struct mbuf **mrq)
1810 {
1811         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1812         struct sockaddr *nam = nfsd->nd_nam;
1813         caddr_t dpos = nfsd->nd_dpos;
1814         struct ucred *cred = &nfsd->nd_cr;
1815         struct vattr va, dirfor, diraft;
1816         struct vattr *vap = &va;
1817         u_int32_t *tl;
1818         struct nlookupdata nd;
1819         int32_t t1;
1820         caddr_t bpos;
1821         int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
1822         u_int32_t major, minor;
1823         enum vtype vtyp;
1824         char *cp2;
1825         struct mbuf *mb, *mb2, *mreq;
1826         struct vnode *dirp;
1827         struct vnode *dvp;
1828         struct vnode *vp;
1829         nfsfh_t nfh;
1830         fhandle_t *fhp;
1831
1832         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1833         nlookup_zero(&nd);
1834         dirp = NULL;
1835         dvp = NULL;
1836         vp = NULL;
1837
1838         fhp = &nfh.fh_generic;
1839         nfsm_srvmtofh(fhp);
1840         nfsm_srvnamesiz(len);
1841
1842         /*
1843          * Handle nfs_namei() call.  If an error occurs, the nd structure
1844          * is not valid.  However, nfsm_*() routines may still jump to
1845          * nfsmout.
1846          */
1847
1848         error = nfs_namei(&nd, cred, NAMEI_CREATE, &dvp, &vp,
1849                           fhp, len, slp, nam, &md, &dpos, &dirp,
1850                           td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1851         if (dirp)
1852                 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
1853         if (error) {
1854                 nfsm_reply(NFSX_WCCDATA(1));
1855                 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1856                 error = 0;
1857                 goto nfsmout;
1858         }
1859         nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1860         vtyp = nfsv3tov_type(*tl);
1861         if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1862                 error = NFSERR_BADTYPE;
1863                 goto out;
1864         }
1865         VATTR_NULL(vap);
1866         nfsm_srvsattr(vap);
1867         if (vtyp == VCHR || vtyp == VBLK) {
1868                 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1869                 major = fxdr_unsigned(u_int32_t, *tl++);
1870                 minor = fxdr_unsigned(u_int32_t, *tl);
1871                 vap->va_rdev = makeudev(major, minor);
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                 error = VOP_NCREATE(nd.nl_ncp, &vp, nd.nl_cred, vap);
1886         } else {
1887                 if (vtyp != VFIFO && (error = suser_cred(cred, 0)))
1888                         goto out;
1889
1890                 error = VOP_NMKNOD(nd.nl_ncp, &vp, nd.nl_cred, vap);
1891                 if (error)
1892                         goto out;
1893
1894 #if 0
1895                 vput(vp);
1896                 vp = NULL;
1897
1898                 /*
1899                  * Release dvp prior to lookup
1900                  */
1901                 vput(dvp);
1902                 dvp = NULL;
1903
1904                 /*
1905                  * XXX what is this stuff for?
1906                  */
1907                 KKASSERT(td->td_proc);
1908                 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
1909                 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT);
1910                 nd.ni_cnd.cn_td = td;
1911                 nd.ni_cnd.cn_cred = td->td_proc->p_ucred;
1912
1913                 error = lookup(&nd);
1914                 nd.ni_dvp = NULL;
1915
1916                 if (error)
1917                         goto out;
1918                 if (nd.ni_cnd.cn_flags & CNP_ISSYMLINK)
1919                         error = EINVAL;
1920 #endif
1921         }
1922
1923         /*
1924          * send response, cleanup, return.
1925          */
1926 out:
1927         nlookup_done(&nd);
1928         if (dvp) {
1929                 if (dvp == vp)
1930                         vrele(dvp);
1931                 else
1932                         vput(dvp);
1933                 dvp = NULL;
1934         }
1935         if (!error) {
1936                 bzero((caddr_t)fhp, sizeof(nfh));
1937                 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1938                 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1939                 if (!error)
1940                         error = VOP_GETATTR(vp, vap, td);
1941         }
1942         if (vp) {
1943                 vput(vp);
1944                 vp = NULL;
1945         }
1946         diraft_ret = VOP_GETATTR(dirp, &diraft, td);
1947         if (dirp) {
1948                 vrele(dirp);
1949                 dirp = NULL;
1950         }
1951         nfsm_reply(NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1));
1952         if (!error) {
1953                 nfsm_srvpostop_fh(fhp);
1954                 nfsm_srvpostop_attr(0, vap);
1955         }
1956         nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1957         return (0);
1958 nfsmout:
1959         if (dirp)
1960                 vrele(dirp);
1961         nlookup_done(&nd);
1962         if (dvp) {
1963                 if (dvp == vp)
1964                         vrele(dvp);
1965                 else
1966                         vput(dvp);
1967         }
1968         if (vp)
1969                 vput(vp);
1970         return (error);
1971 }
1972
1973 /*
1974  * nfs remove service
1975  */
1976 int
1977 nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1978              struct thread *td, struct mbuf **mrq)
1979 {
1980         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1981         struct sockaddr *nam = nfsd->nd_nam;
1982         caddr_t dpos = nfsd->nd_dpos;
1983         struct ucred *cred = &nfsd->nd_cr;
1984         struct nlookupdata nd;
1985         u_int32_t *tl;
1986         int32_t t1;
1987         caddr_t bpos;
1988         int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
1989         int v3 = (nfsd->nd_flag & ND_NFSV3);
1990         char *cp2;
1991         struct mbuf *mb, *mreq;
1992         struct vnode *dirp;
1993         struct vnode *dvp;
1994         struct vnode *vp;
1995         struct vattr dirfor, diraft;
1996         nfsfh_t nfh;
1997         fhandle_t *fhp;
1998
1999         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2000         nlookup_zero(&nd);
2001         dirp = NULL;
2002         dvp = NULL;
2003         vp = NULL;
2004
2005         fhp = &nfh.fh_generic;
2006         nfsm_srvmtofh(fhp);
2007         nfsm_srvnamesiz(len);
2008
2009         error = nfs_namei(&nd, cred, NAMEI_DELETE, &dvp, &vp,
2010                           fhp, len, slp, nam, &md, &dpos, &dirp,
2011                           td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2012         if (dirp) {
2013                 if (v3)
2014                         dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2015         }
2016         if (error == 0) {
2017                 if (vp->v_type == VDIR) {
2018                         error = EPERM;          /* POSIX */
2019                         goto out;
2020                 }
2021                 /*
2022                  * The root of a mounted filesystem cannot be deleted.
2023                  */
2024                 if (vp->v_flag & VROOT) {
2025                         error = EBUSY;
2026                         goto out;
2027                 }
2028 out:
2029                 if (!error) {
2030                         if (dvp) {
2031                                 if (dvp == vp)
2032                                         vrele(dvp);
2033                                 else
2034                                         vput(dvp);
2035                                 dvp = NULL;
2036                         }
2037                         if (vp) {
2038                                 vput(vp);
2039                                 vp = NULL;
2040                         }
2041                         error = VOP_NREMOVE(nd.nl_ncp, nd.nl_cred);
2042                 }
2043         }
2044         if (dirp && v3)
2045                 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2046         nfsm_reply(NFSX_WCCDATA(v3));
2047         if (v3) {
2048                 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2049                 error = 0;
2050         }
2051 nfsmout:
2052         nlookup_done(&nd);
2053         if (dirp)
2054                 vrele(dirp);
2055         if (dvp) {
2056                 if (dvp == vp)
2057                         vrele(dvp);
2058                 else
2059                         vput(dvp);
2060         }
2061         if (vp)
2062                 vput(vp);
2063         return(error);
2064 }
2065
2066 /*
2067  * nfs rename service
2068  */
2069 int
2070 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2071              struct thread *td, struct mbuf **mrq)
2072 {
2073         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2074         struct sockaddr *nam = nfsd->nd_nam;
2075         caddr_t dpos = nfsd->nd_dpos;
2076         struct ucred *cred = &nfsd->nd_cr;
2077         u_int32_t *tl;
2078         int32_t t1;
2079         caddr_t bpos;
2080         int error = 0, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2081         int tdirfor_ret = 1, tdiraft_ret = 1;
2082         int v3 = (nfsd->nd_flag & ND_NFSV3);
2083         char *cp2;
2084         struct mbuf *mb, *mreq;
2085         struct nlookupdata fromnd, tond;
2086         struct vnode *fvp, *fdirp;
2087         struct vnode *tvp, *tdirp;
2088         struct namecache *ncp;
2089         struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2090         nfsfh_t fnfh, tnfh;
2091         fhandle_t *ffhp, *tfhp;
2092         uid_t saved_uid;
2093
2094         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2095 #ifndef nolint
2096         fvp = (struct vnode *)0;
2097 #endif
2098         ffhp = &fnfh.fh_generic;
2099         tfhp = &tnfh.fh_generic;
2100
2101         /*
2102          * Clear fields incase goto nfsmout occurs from macro.
2103          */
2104
2105         nlookup_zero(&fromnd);
2106         nlookup_zero(&tond);
2107         fdirp = NULL;
2108         tdirp = NULL;
2109
2110         nfsm_srvmtofh(ffhp);
2111         nfsm_srvnamesiz(len);
2112         /*
2113          * Remember our original uid so that we can reset cr_uid before
2114          * the second nfs_namei() call, in case it is remapped.
2115          */
2116         saved_uid = cred->cr_uid;
2117         error = nfs_namei(&fromnd, cred, NAMEI_DELETE, NULL, NULL,
2118                           ffhp, len, slp, nam, &md, &dpos, &fdirp,
2119                           td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2120         if (fdirp) {
2121                 if (v3)
2122                         fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, td);
2123         }
2124         if (error) {
2125                 nfsm_reply(2 * NFSX_WCCDATA(v3));
2126                 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2127                 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2128                 error = 0;
2129                 goto nfsmout;
2130         }
2131
2132         /*
2133          * We have to unlock the from ncp before we can safely lookup
2134          * the target ncp.
2135          */
2136         KKASSERT(fromnd.nl_flags & NLC_NCPISLOCKED);
2137         cache_unlock(fromnd.nl_ncp);
2138         fromnd.nl_flags &= ~NLC_NCPISLOCKED;
2139         nfsm_srvmtofh(tfhp);
2140         nfsm_strsiz(len2, NFS_MAXNAMLEN);
2141         cred->cr_uid = saved_uid;
2142
2143         error = nfs_namei(&tond, cred, NAMEI_RENAME, NULL, NULL,
2144                           tfhp, len2, slp, nam, &md, &dpos, &tdirp,
2145                           td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2146         if (tdirp) {
2147                 if (v3)
2148                         tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, td);
2149         }
2150         if (error)
2151                 goto out1;
2152
2153         /*
2154          * relock the source
2155          */
2156         if (cache_lock_nonblock(fromnd.nl_ncp) == 0) {
2157                 cache_resolve(fromnd.nl_ncp, fromnd.nl_cred);
2158         } else if (fromnd.nl_ncp > tond.nl_ncp) {
2159                 cache_lock(fromnd.nl_ncp);
2160                 cache_resolve(fromnd.nl_ncp, fromnd.nl_cred);
2161         } else {
2162                 cache_unlock(tond.nl_ncp);
2163                 cache_lock(fromnd.nl_ncp);
2164                 cache_resolve(fromnd.nl_ncp, fromnd.nl_cred);
2165                 cache_lock(tond.nl_ncp);
2166                 cache_resolve(tond.nl_ncp, tond.nl_cred);
2167         }
2168         fromnd.nl_flags |= NLC_NCPISLOCKED;
2169
2170         tvp = tond.nl_ncp->nc_vp;
2171         fvp = fromnd.nl_ncp->nc_vp;
2172
2173         if (tvp != NULL) {
2174                 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2175                         if (v3)
2176                                 error = EEXIST;
2177                         else
2178                                 error = EISDIR;
2179                         goto out;
2180                 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2181                         if (v3)
2182                                 error = EEXIST;
2183                         else
2184                                 error = ENOTDIR;
2185                         goto out;
2186                 }
2187                 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
2188                         if (v3)
2189                                 error = EXDEV;
2190                         else
2191                                 error = ENOTEMPTY;
2192                         goto out;
2193                 }
2194         }
2195         if (fvp->v_type == VDIR && fvp->v_mountedhere) {
2196                 if (v3)
2197                         error = EXDEV;
2198                 else
2199                         error = ENOTEMPTY;
2200                 goto out;
2201         }
2202         if (fromnd.nl_ncp->nc_mount != tond.nl_ncp->nc_mount) {
2203                 if (v3)
2204                         error = EXDEV;
2205                 else
2206                         error = ENOTEMPTY;
2207                 goto out;
2208         }
2209         if (fromnd.nl_ncp == tond.nl_ncp->nc_parent) {
2210                 if (v3)
2211                         error = EINVAL;
2212                 else
2213                         error = ENOTEMPTY;
2214         }
2215
2216         /*
2217          * You cannot rename a source into itself or a subdirectory of itself.
2218          * We check this by travsering the target directory upwards looking
2219          * for a match against the source.
2220          */
2221         if (error == 0) {
2222                 for (ncp = tond.nl_ncp; ncp; ncp = ncp->nc_parent) {
2223                         if (fromnd.nl_ncp == ncp) {
2224                                 error = EINVAL;
2225                                 break;
2226                         }
2227                 }
2228         }
2229
2230         /*
2231          * If source is the same as the destination (that is the
2232          * same vnode with the same name in the same directory),
2233          * then there is nothing to do.
2234          */
2235         if (fromnd.nl_ncp == tond.nl_ncp)
2236                 error = -1;
2237 out:
2238         if (!error) {
2239                 /*
2240                  * The VOP_NRENAME function releases all vnode references &
2241                  * locks prior to returning so we need to clear the pointers
2242                  * to bypass cleanup code later on.
2243                  */
2244                 error = VOP_NRENAME(fromnd.nl_ncp, tond.nl_ncp, tond.nl_cred);
2245         } else {
2246                 if (error == -1)
2247                         error = 0;
2248         }
2249         /* fall through */
2250
2251 out1:
2252         if (fdirp)
2253                 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, td);
2254         if (tdirp)
2255                 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, td);
2256         nfsm_reply(2 * NFSX_WCCDATA(v3));
2257         if (v3) {
2258                 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2259                 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2260         }
2261         error = 0;
2262         /* fall through */
2263
2264 nfsmout:
2265         if (tdirp)
2266                 vrele(tdirp);
2267         nlookup_done(&tond);
2268         if (fdirp)
2269                 vrele(fdirp);
2270         nlookup_done(&fromnd);
2271         return (error);
2272 }
2273
2274 /*
2275  * nfs link service
2276  */
2277 int
2278 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2279            struct thread *td, struct mbuf **mrq)
2280 {
2281         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2282         struct sockaddr *nam = nfsd->nd_nam;
2283         caddr_t dpos = nfsd->nd_dpos;
2284         struct ucred *cred = &nfsd->nd_cr;
2285         struct nlookupdata nd;
2286         u_int32_t *tl;
2287         int32_t t1;
2288         caddr_t bpos;
2289         int error = 0, rdonly, len, dirfor_ret = 1, diraft_ret = 1;
2290         int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
2291         char *cp2;
2292         struct mbuf *mb, *mreq;
2293         struct vnode *dirp;
2294         struct vnode *dvp;
2295         struct vnode *vp;
2296         struct vnode *xp;
2297         struct vattr dirfor, diraft, at;
2298         nfsfh_t nfh, dnfh;
2299         fhandle_t *fhp, *dfhp;
2300
2301         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2302         nlookup_zero(&nd);
2303         dirp = dvp = vp = xp = NULL;
2304
2305         fhp = &nfh.fh_generic;
2306         dfhp = &dnfh.fh_generic;
2307         nfsm_srvmtofh(fhp);
2308         nfsm_srvmtofh(dfhp);
2309         nfsm_srvnamesiz(len);
2310
2311         error = nfsrv_fhtovp(fhp, FALSE, &xp, cred, slp, nam,
2312                  &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2313         if (error) {
2314                 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2315                 nfsm_srvpostop_attr(getret, &at);
2316                 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2317                 xp = NULL;
2318                 error = 0;
2319                 goto nfsmout;
2320         }
2321         if (xp->v_type == VDIR) {
2322                 error = EPERM;          /* POSIX */
2323                 goto out1;
2324         }
2325
2326         error = nfs_namei(&nd, cred, NAMEI_CREATE, &dvp, &vp,
2327                           dfhp, len, slp, nam, &md, &dpos, &dirp,
2328                           td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2329         if (dirp) {
2330                 if (v3)
2331                         dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2332         }
2333         if (error)
2334                 goto out1;
2335
2336         if (vp != NULL) {
2337                 error = EEXIST;
2338                 goto out;
2339         }
2340         if (xp->v_mount != dvp->v_mount)
2341                 error = EXDEV;
2342 out:
2343         if (!error) {
2344                 error = VOP_NLINK(nd.nl_ncp, xp, nd.nl_cred);
2345         }
2346         /* fall through */
2347
2348 out1:
2349         if (v3)
2350                 getret = VOP_GETATTR(xp, &at, td);
2351         if (dirp)
2352                 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
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 = (char *)0, *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         nfsfh_t nfh;
2404         fhandle_t *fhp;
2405
2406         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2407         nlookup_zero(&nd);
2408         dirp = vp = NULL;
2409
2410         fhp = &nfh.fh_generic;
2411         nfsm_srvmtofh(fhp);
2412         nfsm_srvnamesiz(len);
2413
2414         error = nfs_namei(&nd, cred, NAMEI_CREATE, NULL, &vp,
2415                         fhp, len, slp, nam, &md, &dpos, &dirp,
2416                         td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2417         if (dirp) {
2418                 if (v3)
2419                         dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2420         }
2421         if (error)
2422                 goto out;
2423
2424         VATTR_NULL(vap);
2425         if (v3)
2426                 nfsm_srvsattr(vap);
2427         nfsm_strsiz(len2, NFS_MAXPATHLEN);
2428         MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
2429         iv.iov_base = pathcp;
2430         iv.iov_len = len2;
2431         io.uio_resid = len2;
2432         io.uio_offset = 0;
2433         io.uio_iov = &iv;
2434         io.uio_iovcnt = 1;
2435         io.uio_segflg = UIO_SYSSPACE;
2436         io.uio_rw = UIO_READ;
2437         io.uio_td = NULL;
2438         nfsm_mtouio(&io, len2);
2439         if (!v3) {
2440                 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2441                 vap->va_mode = nfstov_mode(sp->sa_mode);
2442         }
2443         *(pathcp + len2) = '\0';
2444         if (vp) {
2445                 error = EEXIST;
2446                 goto out;
2447         }
2448
2449         if (vap->va_mode == (mode_t)VNOVAL)
2450                 vap->va_mode = 0;
2451         error = VOP_NSYMLINK(nd.nl_ncp, &vp, nd.nl_cred, vap, pathcp);
2452         if (error == 0) {
2453                 bzero((caddr_t)fhp, sizeof(nfh));
2454                 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2455                 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2456                 if (!error)
2457                         error = VOP_GETATTR(vp, vap, td);
2458         }
2459
2460 #if 0
2461         /*
2462          * We have a vp in hand from the new API call, we do not have to
2463          * look it up again.
2464          */
2465         if (error == 0) {
2466             if (v3) {
2467                 /*
2468                  * Issue lookup.  Leave SAVESTART set so we can easily free
2469                  * the name buffer later on.
2470                  *
2471                  * since LOCKPARENT is not set, ni_dvp will be garbage on
2472                  * return whether an error occurs or not.
2473                  */
2474                 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
2475                 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT | CNP_FOLLOW);
2476                 nd.ni_cnd.cn_td = td;
2477                 nd.ni_cnd.cn_cred = cred;
2478
2479                 error = lookup(&nd);
2480                 nd.ni_dvp = NULL;
2481
2482                 if (error == 0) {
2483                         bzero((caddr_t)fhp, sizeof(nfh));
2484                         fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2485                         error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2486                         if (!error)
2487                                 error = VOP_GETATTR(nd.ni_vp, vap, td);
2488                         vput(nd.ni_vp);
2489                         nd.ni_vp = NULL;
2490                 }
2491             }
2492         }
2493 #endif
2494 out:
2495         if (vp) {
2496                 vput(vp);
2497                 vp = NULL;
2498         }
2499         if (pathcp) {
2500                 FREE(pathcp, M_TEMP);
2501                 pathcp = NULL;
2502         }
2503         if (dirp) {
2504                 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2505                 vrele(dirp);
2506                 dirp = NULL;
2507         }
2508         nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2509         if (v3) {
2510                 if (!error) {
2511                         nfsm_srvpostop_fh(fhp);
2512                         nfsm_srvpostop_attr(0, vap);
2513                 }
2514                 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2515         }
2516         error = 0;
2517         /* fall through */
2518
2519 nfsmout:
2520         nlookup_done(&nd);
2521         if (vp)
2522                 vput(vp);
2523         if (dirp)
2524                 vrele(dirp);
2525         if (pathcp)
2526                 FREE(pathcp, M_TEMP);
2527         return (error);
2528 }
2529
2530 /*
2531  * nfs mkdir service
2532  */
2533 int
2534 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2535             struct thread *td, struct mbuf **mrq)
2536 {
2537         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2538         struct sockaddr *nam = nfsd->nd_nam;
2539         caddr_t dpos = nfsd->nd_dpos;
2540         struct ucred *cred = &nfsd->nd_cr;
2541         struct vattr va, dirfor, diraft;
2542         struct vattr *vap = &va;
2543         struct nfs_fattr *fp;
2544         struct nlookupdata nd;
2545         caddr_t cp;
2546         u_int32_t *tl;
2547         int32_t t1;
2548         caddr_t bpos;
2549         int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2550         int v3 = (nfsd->nd_flag & ND_NFSV3);
2551         char *cp2;
2552         struct mbuf *mb, *mb2, *mreq;
2553         struct vnode *dirp;
2554         struct vnode *vp;
2555         nfsfh_t nfh;
2556         fhandle_t *fhp;
2557
2558         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2559         nlookup_zero(&nd);
2560         dirp = NULL;
2561         vp = NULL;
2562
2563         fhp = &nfh.fh_generic;
2564         nfsm_srvmtofh(fhp);
2565         nfsm_srvnamesiz(len);
2566
2567         error = nfs_namei(&nd, cred, NAMEI_CREATE, NULL, &vp,
2568                           fhp, len, slp, nam, &md, &dpos, &dirp,
2569                           td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2570         if (dirp) {
2571                 if (v3)
2572                         dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2573         }
2574         if (error) {
2575                 nfsm_reply(NFSX_WCCDATA(v3));
2576                 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2577                 error = 0;
2578                 goto nfsmout;
2579         }
2580         VATTR_NULL(vap);
2581         if (v3) {
2582                 nfsm_srvsattr(vap);
2583         } else {
2584                 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2585                 vap->va_mode = nfstov_mode(*tl++);
2586         }
2587
2588         /*
2589          * At this point nd.ni_dvp is referenced and exclusively locked and
2590          * nd.ni_vp, if it exists, is referenced but not locked.
2591          */
2592
2593         vap->va_type = VDIR;
2594         if (vp != NULL) {
2595                 error = EEXIST;
2596                 goto out;
2597         }
2598
2599         /*
2600          * Issue mkdir op.  Since SAVESTART is not set, the pathname 
2601          * component is freed by the VOP call.  This will fill-in
2602          * nd.ni_vp, reference, and exclusively lock it.
2603          */
2604         if (vap->va_mode == (mode_t)VNOVAL)
2605                 vap->va_mode = 0;
2606         error = VOP_NMKDIR(nd.nl_ncp, &vp, nd.nl_cred, vap);
2607
2608         if (error == 0) {
2609                 bzero((caddr_t)fhp, sizeof(nfh));
2610                 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2611                 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2612                 if (error == 0)
2613                         error = VOP_GETATTR(vp, vap, td);
2614         }
2615 out:
2616         if (dirp)
2617                 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2618         nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2619         if (v3) {
2620                 if (!error) {
2621                         nfsm_srvpostop_fh(fhp);
2622                         nfsm_srvpostop_attr(0, vap);
2623                 }
2624                 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2625         } else {
2626                 nfsm_srvfhtom(fhp, v3);
2627                 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
2628                 nfsm_srvfillattr(vap, fp);
2629         }
2630         error = 0;
2631         /* fall through */
2632
2633 nfsmout:
2634         nlookup_done(&nd);
2635         if (dirp)
2636                 vrele(dirp);
2637         if (vp)
2638                 vput(vp);
2639         return (error);
2640 }
2641
2642 /*
2643  * nfs rmdir service
2644  */
2645 int
2646 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2647             struct thread *td, struct mbuf **mrq)
2648 {
2649         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2650         struct sockaddr *nam = nfsd->nd_nam;
2651         caddr_t dpos = nfsd->nd_dpos;
2652         struct ucred *cred = &nfsd->nd_cr;
2653         u_int32_t *tl;
2654         int32_t t1;
2655         caddr_t bpos;
2656         int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2657         int v3 = (nfsd->nd_flag & ND_NFSV3);
2658         char *cp2;
2659         struct mbuf *mb, *mreq;
2660         struct vnode *dirp;
2661         struct vnode *vp;
2662         struct vattr dirfor, diraft;
2663         nfsfh_t nfh;
2664         fhandle_t *fhp;
2665         struct nlookupdata nd;
2666
2667         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2668         nlookup_zero(&nd);
2669         dirp = NULL;
2670         vp = NULL;
2671
2672         fhp = &nfh.fh_generic;
2673         nfsm_srvmtofh(fhp);
2674         nfsm_srvnamesiz(len);
2675
2676         error = nfs_namei(&nd, cred, NAMEI_DELETE, NULL, &vp,
2677                           fhp, len, slp, nam, &md, &dpos, &dirp,
2678                           td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2679         if (dirp) {
2680                 if (v3)
2681                         dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2682         }
2683         if (error) {
2684                 nfsm_reply(NFSX_WCCDATA(v3));
2685                 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2686                 error = 0;
2687                 goto nfsmout;
2688         }
2689         if (vp->v_type != VDIR) {
2690                 error = ENOTDIR;
2691                 goto out;
2692         }
2693
2694         /*
2695          * The root of a mounted filesystem cannot be deleted.
2696          */
2697         if (vp->v_flag & VROOT)
2698                 error = EBUSY;
2699 out:
2700         /*
2701          * Issue or abort op.  Since SAVESTART is not set, path name
2702          * component is freed by the VOP after either.
2703          */
2704         if (!error) {
2705                 vput(vp);
2706                 vp = NULL;
2707                 error = VOP_NRMDIR(nd.nl_ncp, nd.nl_cred);
2708         }
2709         nlookup_done(&nd);
2710
2711         if (dirp)
2712                 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2713         nfsm_reply(NFSX_WCCDATA(v3));
2714         if (v3) {
2715                 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2716                 error = 0;
2717         }
2718         /* fall through */
2719
2720 nfsmout:
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 mbuf *mp;
2777         struct dirent *dp;
2778         caddr_t cp;
2779         u_int32_t *tl;
2780         int32_t t1;
2781         caddr_t bpos;
2782         struct mbuf *mb, *mb2, *mreq, *mp2;
2783         char *cpos, *cend, *cp2, *rbuf;
2784         struct vnode *vp = 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         u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
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, &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, td);
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(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         VOP_UNLOCK(vp, 0, td);
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         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2875         if (cookies) {
2876                 free((caddr_t)cookies, M_TEMP);
2877                 cookies = NULL;
2878         }
2879         error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
2880         off = (off_t)io.uio_offset;
2881         if (!cookies && !error)
2882                 error = NFSERR_PERM;
2883         if (v3) {
2884                 getret = VOP_GETATTR(vp, &at, td);
2885                 if (!error)
2886                         error = getret;
2887         }
2888         VOP_UNLOCK(vp, 0, td);
2889         if (error) {
2890                 vrele(vp);
2891                 vp = NULL;
2892                 free((caddr_t)rbuf, M_TEMP);
2893                 if (cookies)
2894                         free((caddr_t)cookies, M_TEMP);
2895                 nfsm_reply(NFSX_POSTOPATTR(v3));
2896                 nfsm_srvpostop_attr(getret, &at);
2897                 error = 0;
2898                 goto nfsmout;
2899         }
2900         if (io.uio_resid) {
2901                 siz -= io.uio_resid;
2902
2903                 /*
2904                  * If nothing read, return eof
2905                  * rpc reply
2906                  */
2907                 if (siz == 0) {
2908                         vrele(vp);
2909                         vp = NULL;
2910                         nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
2911                                 2 * NFSX_UNSIGNED);
2912                         if (v3) {
2913                                 nfsm_srvpostop_attr(getret, &at);
2914                                 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2915                                 txdr_hyper(at.va_filerev, tl);
2916                                 tl += 2;
2917                         } else
2918                                 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2919                         *tl++ = nfs_false;
2920                         *tl = nfs_true;
2921                         FREE((caddr_t)rbuf, M_TEMP);
2922                         FREE((caddr_t)cookies, M_TEMP);
2923                         error = 0;
2924                         goto nfsmout;
2925                 }
2926         }
2927
2928         /*
2929          * Check for degenerate cases of nothing useful read.
2930          * If so go try again
2931          */
2932         cpos = rbuf;
2933         cend = rbuf + siz;
2934         dp = (struct dirent *)cpos;
2935         cookiep = cookies;
2936         /*
2937          * For some reason FreeBSD's ufs_readdir() chooses to back the
2938          * directory offset up to a block boundary, so it is necessary to
2939          * skip over the records that preceed the requested offset. This
2940          * requires the assumption that file offset cookies monotonically
2941          * increase.
2942          */
2943         while (cpos < cend && ncookies > 0 &&
2944                 (dp->d_ino == 0 || dp->d_type == DT_WHT ||
2945                  ((u_quad_t)(*cookiep)) <= toff)) {
2946                 dp = _DIRENT_NEXT(dp);
2947                 cpos = (char *)dp;
2948                 cookiep++;
2949                 ncookies--;
2950         }
2951         if (cpos >= cend || ncookies == 0) {
2952                 toff = off;
2953                 siz = fullsiz;
2954                 goto again;
2955         }
2956
2957         len = 3 * NFSX_UNSIGNED;        /* paranoia, probably can be 0 */
2958         nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
2959         if (v3) {
2960                 nfsm_srvpostop_attr(getret, &at);
2961                 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2962                 txdr_hyper(at.va_filerev, tl);
2963         }
2964         mp = mp2 = mb;
2965         bp = bpos;
2966         be = bp + M_TRAILINGSPACE(mp);
2967
2968         /* Loop through the records and build reply */
2969         while (cpos < cend && ncookies > 0) {
2970                 if (dp->d_ino != 0 && dp->d_type != DT_WHT) {
2971                         nlen = dp->d_namlen;
2972                         rem = nfsm_rndup(nlen) - nlen;
2973                         len += (4 * NFSX_UNSIGNED + nlen + rem);
2974                         if (v3)
2975                                 len += 2 * NFSX_UNSIGNED;
2976                         if (len > cnt) {
2977                                 eofflag = 0;
2978                                 break;
2979                         }
2980                         /*
2981                          * Build the directory record xdr from
2982                          * the dirent entry.
2983                          */
2984                         nfsm_clget;
2985                         *tl = nfs_true;
2986                         bp += NFSX_UNSIGNED;
2987                         if (v3) {
2988                                 nfsm_clget;
2989                                 *tl = 0;
2990                                 bp += NFSX_UNSIGNED;
2991                         }
2992                         nfsm_clget;
2993                         *tl = txdr_unsigned(dp->d_ino);
2994                         bp += NFSX_UNSIGNED;
2995                         nfsm_clget;
2996                         *tl = txdr_unsigned(nlen);
2997                         bp += NFSX_UNSIGNED;
2998
2999                         /* And loop around copying the name */
3000                         xfer = nlen;
3001                         cp = dp->d_name;
3002                         while (xfer > 0) {
3003                                 nfsm_clget;
3004                                 if ((bp+xfer) > be)
3005                                         tsiz = be-bp;
3006                                 else
3007                                         tsiz = xfer;
3008                                 bcopy(cp, bp, tsiz);
3009                                 bp += tsiz;
3010                                 xfer -= tsiz;
3011                                 if (xfer > 0)
3012                                         cp += tsiz;
3013                         }
3014                         /* And null pad to a int32_t boundary */
3015                         for (i = 0; i < rem; i++)
3016                                 *bp++ = '\0';
3017                         nfsm_clget;
3018
3019                         /* Finish off the record */
3020                         if (v3) {
3021                                 *tl = 0;
3022                                 bp += NFSX_UNSIGNED;
3023                                 nfsm_clget;
3024                         }
3025                         *tl = txdr_unsigned(*cookiep);
3026                         bp += NFSX_UNSIGNED;
3027                 }
3028                 dp = _DIRENT_NEXT(dp);
3029                 cpos = (char *)dp;
3030                 cookiep++;
3031                 ncookies--;
3032         }
3033         vrele(vp);
3034         vp = NULL;
3035         nfsm_clget;
3036         *tl = nfs_false;
3037         bp += NFSX_UNSIGNED;
3038         nfsm_clget;
3039         if (eofflag)
3040                 *tl = nfs_true;
3041         else
3042                 *tl = nfs_false;
3043         bp += NFSX_UNSIGNED;
3044         if (mp != mb) {
3045                 if (bp < be)
3046                         mp->m_len = bp - mtod(mp, caddr_t);
3047         } else
3048                 mp->m_len += bp - bpos;
3049         FREE((caddr_t)rbuf, M_TEMP);
3050         FREE((caddr_t)cookies, M_TEMP);
3051
3052 nfsmout:
3053         if (vp)
3054                 vrele(vp);
3055         return(error);
3056 }
3057
3058 int
3059 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3060                   struct thread *td, struct mbuf **mrq)
3061 {
3062         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3063         struct sockaddr *nam = nfsd->nd_nam;
3064         caddr_t dpos = nfsd->nd_dpos;
3065         struct ucred *cred = &nfsd->nd_cr;
3066         char *bp, *be;
3067         struct mbuf *mp;
3068         struct dirent *dp;
3069         caddr_t cp;
3070         u_int32_t *tl;
3071         int32_t t1;
3072         caddr_t bpos;
3073         struct mbuf *mb, *mb2, *mreq, *mp2;
3074         char *cpos, *cend, *cp2, *rbuf;
3075         struct vnode *vp = NULL, *nvp;
3076         struct flrep fl;
3077         nfsfh_t nfh;
3078         fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3079         struct uio io;
3080         struct iovec iv;
3081         struct vattr va, at, *vap = &va;
3082         struct nfs_fattr *fp;
3083         int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3084         int siz, cnt, fullsiz, eofflag, rdonly, dirlen, ncookies;
3085         u_quad_t off, toff, verf;
3086         u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3087
3088         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3089         fhp = &nfh.fh_generic;
3090         nfsm_srvmtofh(fhp);
3091         nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3092         toff = fxdr_hyper(tl);
3093         tl += 2;
3094         verf = fxdr_hyper(tl);
3095         tl += 2;
3096         siz = fxdr_unsigned(int, *tl++);
3097         cnt = fxdr_unsigned(int, *tl);
3098         off = toff;
3099         siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3100         xfer = NFS_SRVMAXDATA(nfsd);
3101         if (cnt > xfer)
3102                 cnt = xfer;
3103         if (siz > xfer)
3104                 siz = xfer;
3105         fullsiz = siz;
3106         error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3107                  &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3108         if (!error && vp->v_type != VDIR) {
3109                 error = ENOTDIR;
3110                 vput(vp);
3111                 vp = NULL;
3112         }
3113         if (error) {
3114                 nfsm_reply(NFSX_UNSIGNED);
3115                 nfsm_srvpostop_attr(getret, &at);
3116                 error = 0;
3117                 goto nfsmout;
3118         }
3119         error = getret = VOP_GETATTR(vp, &at, td);
3120 #if 0
3121         /*
3122          * XXX This check may be too strict for Solaris 2.5 clients.
3123          */
3124         if (!error && toff && verf && verf != at.va_filerev)
3125                 error = NFSERR_BAD_COOKIE;
3126 #endif
3127         if (!error) {
3128                 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
3129         }
3130         if (error) {
3131                 vput(vp);
3132                 vp = NULL;
3133                 nfsm_reply(NFSX_V3POSTOPATTR);
3134                 nfsm_srvpostop_attr(getret, &at);
3135                 error = 0;
3136                 goto nfsmout;
3137         }
3138         VOP_UNLOCK(vp, 0, td);
3139         MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3140 again:
3141         iv.iov_base = rbuf;
3142         iv.iov_len = fullsiz;
3143         io.uio_iov = &iv;
3144         io.uio_iovcnt = 1;
3145         io.uio_offset = (off_t)off;
3146         io.uio_resid = fullsiz;
3147         io.uio_segflg = UIO_SYSSPACE;
3148         io.uio_rw = UIO_READ;
3149         io.uio_td = NULL;
3150         eofflag = 0;
3151         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3152         if (cookies) {
3153                 free((caddr_t)cookies, M_TEMP);
3154                 cookies = NULL;
3155         }
3156         error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3157         off = (u_quad_t)io.uio_offset;
3158         getret = VOP_GETATTR(vp, &at, td);
3159         VOP_UNLOCK(vp, 0, td);
3160         if (!cookies && !error)
3161                 error = NFSERR_PERM;
3162         if (!error)
3163                 error = getret;
3164         if (error) {
3165                 vrele(vp);
3166                 vp = NULL;
3167                 if (cookies)
3168                         free((caddr_t)cookies, M_TEMP);
3169                 free((caddr_t)rbuf, M_TEMP);
3170                 nfsm_reply(NFSX_V3POSTOPATTR);
3171                 nfsm_srvpostop_attr(getret, &at);
3172                 error = 0;
3173                 goto nfsmout;
3174         }
3175         if (io.uio_resid) {
3176                 siz -= io.uio_resid;
3177
3178                 /*
3179                  * If nothing read, return eof
3180                  * rpc reply
3181                  */
3182                 if (siz == 0) {
3183                         vrele(vp);
3184                         vp = NULL;
3185                         nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3186                                 2 * NFSX_UNSIGNED);
3187                         nfsm_srvpostop_attr(getret, &at);
3188                         nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3189                         txdr_hyper(at.va_filerev, tl);
3190                         tl += 2;
3191                         *tl++ = nfs_false;
3192                         *tl = nfs_true;
3193                         FREE((caddr_t)cookies, M_TEMP);
3194                         FREE((caddr_t)rbuf, M_TEMP);
3195                         error = 0;
3196                         goto nfsmout;
3197                 }
3198         }
3199
3200         /*
3201          * Check for degenerate cases of nothing useful read.
3202          * If so go try again
3203          */
3204         cpos = rbuf;
3205         cend = rbuf + siz;
3206         dp = (struct dirent *)cpos;
3207         cookiep = cookies;
3208         /*
3209          * For some reason FreeBSD's ufs_readdir() chooses to back the
3210          * directory offset up to a block boundary, so it is necessary to
3211          * skip over the records that preceed the requested offset. This
3212          * requires the assumption that file offset cookies monotonically
3213          * increase.
3214          */
3215         while (cpos < cend && ncookies > 0 &&
3216                 (dp->d_ino == 0 || dp->d_type == DT_WHT ||
3217                  ((u_quad_t)(*cookiep)) <= toff)) {
3218                 dp = _DIRENT_NEXT(dp);
3219                 cpos = (char *)dp;
3220                 cookiep++;
3221                 ncookies--;
3222         }
3223         if (cpos >= cend || ncookies == 0) {
3224                 toff = off;
3225                 siz = fullsiz;
3226                 goto again;
3227         }
3228
3229         /*
3230          * Probe one of the directory entries to see if the filesystem
3231          * supports VGET.
3232          */
3233         if (VFS_VGET(vp->v_mount, dp->d_ino, &nvp) == EOPNOTSUPP) {
3234                 error = NFSERR_NOTSUPP;
3235                 vrele(vp);
3236                 vp = NULL;
3237                 free((caddr_t)cookies, M_TEMP);
3238                 free((caddr_t)rbuf, M_TEMP);
3239                 nfsm_reply(NFSX_V3POSTOPATTR);
3240                 nfsm_srvpostop_attr(getret, &at);
3241                 error = 0;
3242                 goto nfsmout;
3243         }
3244         if (nvp) {
3245                 vput(nvp);
3246                 nvp = NULL;
3247         }
3248             
3249         dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2 * NFSX_UNSIGNED;
3250         nfsm_reply(cnt);
3251         nfsm_srvpostop_attr(getret, &at);
3252         nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3253         txdr_hyper(at.va_filerev, tl);
3254         mp = mp2 = mb;
3255         bp = bpos;
3256         be = bp + M_TRAILINGSPACE(mp);
3257
3258         /* Loop through the records and build reply */
3259         while (cpos < cend && ncookies > 0) {
3260                 if (dp->d_ino != 0 && dp->d_type != DT_WHT) {
3261                         nlen = dp->d_namlen;
3262                         rem = nfsm_rndup(nlen)-nlen;
3263
3264                         /*
3265                          * For readdir_and_lookup get the vnode using
3266                          * the file number.
3267                          */
3268                         if (VFS_VGET(vp->v_mount, dp->d_ino, &nvp))
3269                                 goto invalid;
3270                         bzero((caddr_t)nfhp, NFSX_V3FH);
3271                         nfhp->fh_fsid =
3272                                 nvp->v_mount->mnt_stat.f_fsid;
3273                         if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3274                                 vput(nvp);
3275                                 nvp = NULL;
3276                                 goto invalid;
3277                         }
3278                         if (VOP_GETATTR(nvp, vap, td)) {
3279                                 vput(nvp);
3280                                 nvp = NULL;
3281                                 goto invalid;
3282                         }
3283                         vput(nvp);
3284                         nvp = NULL;
3285
3286                         /*
3287                          * If either the dircount or maxcount will be
3288                          * exceeded, get out now. Both of these lengths
3289                          * are calculated conservatively, including all
3290                          * XDR overheads.
3291                          */
3292                         len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3293                                 NFSX_V3POSTOPATTR);
3294                         dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3295                         if (len > cnt || dirlen > fullsiz) {
3296                                 eofflag = 0;
3297                                 break;
3298                         }
3299
3300                         /*
3301                          * Build the directory record xdr from
3302                          * the dirent entry.
3303                          */
3304                         fp = (struct nfs_fattr *)&fl.fl_fattr;
3305                         nfsm_srvfillattr(vap, fp);
3306                         fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3307                         fl.fl_fhok = nfs_true;
3308                         fl.fl_postopok = nfs_true;
3309                         fl.fl_off.nfsuquad[0] = 0;
3310                         fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3311
3312                         nfsm_clget;
3313                         *tl = nfs_true;
3314                         bp += NFSX_UNSIGNED;
3315                         nfsm_clget;
3316                         *tl = 0;
3317                         bp += NFSX_UNSIGNED;
3318                         nfsm_clget;
3319                         *tl = txdr_unsigned(dp->d_ino);
3320                         bp += NFSX_UNSIGNED;
3321                         nfsm_clget;
3322                         *tl = txdr_unsigned(nlen);
3323                         bp += NFSX_UNSIGNED;
3324
3325                         /* And loop around copying the name */
3326                         xfer = nlen;
3327                         cp = dp->d_name;
3328                         while (xfer > 0) {
3329                                 nfsm_clget;
3330                                 if ((bp + xfer) > be)
3331                                         tsiz = be - bp;
3332                                 else
3333                                         tsiz = xfer;
3334                                 bcopy(cp, bp, tsiz);
3335                                 bp += tsiz;
3336                                 xfer -= tsiz;
3337                                 if (xfer > 0)
3338                                         cp += tsiz;
3339                         }
3340                         /* And null pad to a int32_t boundary */
3341                         for (i = 0; i < rem; i++)
3342                                 *bp++ = '\0';
3343         
3344                         /*
3345                          * Now copy the flrep structure out.
3346                          */
3347                         xfer = sizeof (struct flrep);
3348                         cp = (caddr_t)&fl;
3349                         while (xfer > 0) {
3350                                 nfsm_clget;
3351                                 if ((bp + xfer) > be)
3352                                         tsiz = be - bp;
3353                                 else
3354                                         tsiz = xfer;
3355                                 bcopy(cp, bp, tsiz);
3356                                 bp += tsiz;
3357                                 xfer -= tsiz;
3358                                 if (xfer > 0)
3359                                         cp += tsiz;
3360                         }
3361                 }
3362 invalid:
3363                 dp = _DIRENT_NEXT(dp);
3364                 cpos = (char *)dp;
3365                 cookiep++;
3366                 ncookies--;
3367         }
3368         vrele(vp);
3369         vp = NULL;
3370         nfsm_clget;
3371         *tl = nfs_false;
3372         bp += NFSX_UNSIGNED;
3373         nfsm_clget;
3374         if (eofflag)
3375                 *tl = nfs_true;
3376         else
3377                 *tl = nfs_false;
3378         bp += NFSX_UNSIGNED;
3379         if (mp != mb) {
3380                 if (bp < be)
3381                         mp->m_len = bp - mtod(mp, caddr_t);
3382         } else
3383                 mp->m_len += bp - bpos;
3384         FREE((caddr_t)cookies, M_TEMP);
3385         FREE((caddr_t)rbuf, M_TEMP);
3386 nfsmout:
3387         if (vp)
3388                 vrele(vp);
3389         return(error);
3390 }
3391
3392 /*
3393  * nfs commit service
3394  */
3395 int
3396 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3397              struct thread *td, struct mbuf **mrq)
3398 {
3399         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3400         struct sockaddr *nam = nfsd->nd_nam;
3401         caddr_t dpos = nfsd->nd_dpos;
3402         struct ucred *cred = &nfsd->nd_cr;
3403         struct vattr bfor, aft;
3404         struct vnode *vp = NULL;
3405         nfsfh_t nfh;
3406         fhandle_t *fhp;
3407         u_int32_t *tl;
3408         int32_t t1;
3409         caddr_t bpos;
3410         int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt;
3411         char *cp2;
3412         struct mbuf *mb, *mb2, *mreq;
3413         u_quad_t off;
3414
3415         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3416         fhp = &nfh.fh_generic;
3417         nfsm_srvmtofh(fhp);
3418         nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3419
3420         /*
3421          * XXX At this time VOP_FSYNC() does not accept offset and byte
3422          * count parameters, so these arguments are useless (someday maybe).
3423          */
3424         off = fxdr_hyper(tl);
3425         tl += 2;
3426         cnt = fxdr_unsigned(int, *tl);
3427         error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3428                  &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3429         if (error) {
3430                 nfsm_reply(2 * NFSX_UNSIGNED);
3431                 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3432                 error = 0;
3433                 goto nfsmout;
3434         }
3435         for_ret = VOP_GETATTR(vp, &bfor, td);
3436
3437         if (cnt > MAX_COMMIT_COUNT) {
3438                 /*
3439                  * Give up and do the whole thing
3440                  */
3441                 if (vp->v_object &&
3442                    (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3443                         vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3444                 }
3445                 error = VOP_FSYNC(vp, MNT_WAIT, td);
3446         } else {
3447                 /*
3448                  * Locate and synchronously write any buffers that fall
3449                  * into the requested range.  Note:  we are assuming that
3450                  * f_iosize is a power of 2.
3451                  */
3452                 int iosize = vp->v_mount->mnt_stat.f_iosize;
3453                 int iomask = iosize - 1;
3454                 off_t loffset;
3455
3456                 /*
3457                  * Align to iosize boundry, super-align to page boundry.
3458                  */
3459                 if (off & iomask) {
3460                         cnt += off & iomask;
3461                         off &= ~(u_quad_t)iomask;
3462                 }
3463                 if (off & PAGE_MASK) {
3464                         cnt += off & PAGE_MASK;
3465                         off &= ~(u_quad_t)PAGE_MASK;
3466                 }
3467                 loffset = off;
3468
3469                 if (vp->v_object &&
3470                    (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3471                         vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3472                 }
3473
3474                 crit_enter();
3475                 while (cnt > 0) {
3476                         struct buf *bp;
3477
3478                         /*
3479                          * If we have a buffer and it is marked B_DELWRI we
3480                          * have to lock and write it.  Otherwise the prior
3481                          * write is assumed to have already been committed.
3482                          */
3483                         if ((bp = findblk(vp, loffset)) != NULL && (bp->b_flags & B_DELWRI)) {
3484                                 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
3485                                         if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL) == 0)
3486                                                 BUF_UNLOCK(bp);
3487                                         continue; /* retry */
3488                                 }
3489                                 bremfree(bp);
3490                                 bp->b_flags &= ~B_ASYNC;
3491                                 VOP_BWRITE(bp->b_vp, bp);
3492                                 ++nfs_commit_miss;
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, td);
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 vattr at;
3545         nfsfh_t nfh;
3546         fhandle_t *fhp;
3547         struct statfs statfs;
3548         u_quad_t tval;
3549
3550         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3551         fhp = &nfh.fh_generic;
3552         nfsm_srvmtofh(fhp);
3553         error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3554                  &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3555         if (error) {
3556                 nfsm_reply(NFSX_UNSIGNED);
3557                 nfsm_srvpostop_attr(getret, &at);
3558                 error = 0;
3559                 goto nfsmout;
3560         }
3561         sf = &statfs;
3562         error = VFS_STATFS(vp->v_mount, sf, td);
3563         getret = VOP_GETATTR(vp, &at, td);
3564         vput(vp);
3565         vp = NULL;
3566         nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3567         if (v3)
3568                 nfsm_srvpostop_attr(getret, &at);
3569         if (error) {
3570                 error = 0;
3571                 goto nfsmout;
3572         }
3573         nfsm_build(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
3574         if (v3) {
3575                 tval = (u_quad_t)sf->f_blocks;
3576                 tval *= (u_quad_t)sf->f_bsize;
3577                 txdr_hyper(tval, &sfp->sf_tbytes);
3578                 tval = (u_quad_t)sf->f_bfree;
3579                 tval *= (u_quad_t)sf->f_bsize;
3580                 txdr_hyper(tval, &sfp->sf_fbytes);
3581                 tval = (u_quad_t)sf->f_bavail;
3582                 tval *= (u_quad_t)sf->f_bsize;
3583                 txdr_hyper(tval, &sfp->sf_abytes);
3584                 sfp->sf_tfiles.nfsuquad[0] = 0;
3585                 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3586                 sfp->sf_ffiles.nfsuquad[0] = 0;
3587                 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3588                 sfp->sf_afiles.nfsuquad[0] = 0;
3589                 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3590                 sfp->sf_invarsec = 0;
3591         } else {
3592                 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3593                 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3594                 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3595                 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3596                 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3597         }
3598 nfsmout:
3599         if (vp)
3600                 vput(vp);
3601         return(error);
3602 }
3603
3604 /*
3605  * nfs fsinfo service
3606  */
3607 int
3608 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3609              struct thread *td, struct mbuf **mrq)
3610 {
3611         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3612         struct sockaddr *nam = nfsd->nd_nam;
3613         caddr_t dpos = nfsd->nd_dpos;
3614         struct ucred *cred = &nfsd->nd_cr;
3615         u_int32_t *tl;
3616         struct nfsv3_fsinfo *sip;
3617         int32_t t1;
3618         caddr_t bpos;
3619         int error = 0, rdonly, getret = 1, pref;
3620         char *cp2;
3621         struct mbuf *mb, *mb2, *mreq;
3622         struct vnode *vp = NULL;
3623         struct vattr at;
3624         nfsfh_t nfh;
3625         fhandle_t *fhp;
3626         u_quad_t maxfsize;
3627         struct statfs sb;
3628
3629         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3630         fhp = &nfh.fh_generic;
3631         nfsm_srvmtofh(fhp);
3632         error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3633                  &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3634         if (error) {
3635                 nfsm_reply(NFSX_UNSIGNED);
3636                 nfsm_srvpostop_attr(getret, &at);
3637                 error = 0;
3638                 goto nfsmout;
3639         }
3640
3641         /* XXX Try to make a guess on the max file size. */
3642         VFS_STATFS(vp->v_mount, &sb, td);
3643         maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3644
3645         getret = VOP_GETATTR(vp, &at, td);
3646         vput(vp);
3647         vp = NULL;
3648         nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3649         nfsm_srvpostop_attr(getret, &at);
3650         nfsm_build(sip, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3651
3652         /*
3653          * XXX
3654          * There should be file system VFS OP(s) to get this information.
3655          * For now, assume ufs.
3656          */
3657         if (slp->ns_so->so_type == SOCK_DGRAM)
3658                 pref = NFS_MAXDGRAMDATA;
3659         else
3660                 pref = NFS_MAXDATA;
3661         sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3662         sip->fs_rtpref = txdr_unsigned(pref);
3663         sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3664         sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3665         sip->fs_wtpref = txdr_unsigned(pref);
3666         sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3667         sip->fs_dtpref = txdr_unsigned(pref);
3668         txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3669         sip->fs_timedelta.nfsv3_sec = 0;
3670         sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3671         sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3672                 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3673                 NFSV3FSINFO_CANSETTIME);
3674 nfsmout:
3675         if (vp)
3676                 vput(vp);
3677         return(error);
3678 }
3679
3680 /*
3681  * nfs pathconf service
3682  */
3683 int
3684 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3685                struct thread *td, struct mbuf **mrq)
3686 {
3687         struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3688         struct sockaddr *nam = nfsd->nd_nam;
3689         caddr_t dpos = nfsd->nd_dpos;
3690         struct ucred *cred = &nfsd->nd_cr;
3691         u_int32_t *tl;
3692         struct nfsv3_pathconf *pc;
3693         int32_t t1;
3694         caddr_t bpos;
3695         int error = 0, rdonly, getret = 1;
3696         register_t linkmax, namemax, chownres, notrunc;
3697         char *cp2;
3698         struct mbuf *mb, *mb2, *mreq;
3699         struct vnode *vp = NULL;
3700         struct vattr at;
3701         nfsfh_t nfh;
3702         fhandle_t *fhp;
3703
3704         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3705         fhp = &nfh.fh_generic;
3706         nfsm_srvmtofh(fhp);
3707         error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3708                  &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3709         if (error) {
3710                 nfsm_reply(NFSX_UNSIGNED);
3711                 nfsm_srvpostop_attr(getret, &at);
3712                 error = 0;
3713                 goto nfsmout;
3714         }
3715         error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3716         if (!error)
3717                 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3718         if (!error)
3719                 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3720         if (!error)
3721                 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, &notrunc);
3722         getret = VOP_GETATTR(vp, &at, td);
3723         vput(vp);
3724         vp = NULL;
3725         nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3726         nfsm_srvpostop_attr(getret, &at);
3727         if (error) {
3728                 error = 0;
3729                 goto nfsmout;
3730         }
3731         nfsm_build(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3732
3733         pc->pc_linkmax = txdr_unsigned(linkmax);
3734         pc->pc_namemax = txdr_unsigned(namemax);
3735         pc->pc_notrunc = txdr_unsigned(notrunc);
3736         pc->pc_chownrestricted = txdr_unsigned(chownres);
3737
3738         /*
3739          * These should probably be supported by VOP_PATHCONF(), but
3740          * until msdosfs is exportable (why would you want to?), the
3741          * Unix defaults should be ok.
3742          */
3743         pc->pc_caseinsensitive = nfs_false;
3744         pc->pc_casepreserving = nfs_true;
3745 nfsmout:
3746         if (vp) 
3747                 vput(vp);
3748         return(error);
3749 }
3750
3751 /*
3752  * Null operation, used by clients to ping server
3753  */
3754 /* ARGSUSED */
3755 int
3756 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3757            struct thread *td, struct mbuf **mrq)
3758 {
3759         struct mbuf *mrep = nfsd->nd_mrep;
3760         caddr_t bpos;
3761         int error = NFSERR_RETVOID;
3762         struct mbuf *mb, *mreq;
3763
3764         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3765         nfsm_reply(0);
3766         nfsm_srvdone;
3767 }
3768
3769 /*
3770  * No operation, used for obsolete procedures
3771  */
3772 /* ARGSUSED */
3773 int
3774 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3775            struct thread *td, struct mbuf **mrq)
3776 {
3777         struct mbuf *mrep = nfsd->nd_mrep;
3778         caddr_t bpos;
3779         int error;
3780         struct mbuf *mb, *mreq;
3781
3782         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3783         if (nfsd->nd_repstat)
3784                 error = nfsd->nd_repstat;
3785         else
3786                 error = EPROCUNAVAIL;
3787         nfsm_reply(0);
3788         error = 0;
3789         nfsm_srvdone;
3790 }
3791
3792 /*
3793  * Perform access checking for vnodes obtained from file handles that would
3794  * refer to files already opened by a Unix client. You cannot just use
3795  * vn_writechk() and VOP_ACCESS() for two reasons.
3796  * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
3797  * 2 - The owner is to be given access irrespective of mode bits for some
3798  *     operations, so that processes that chmod after opening a file don't
3799  *     break. I don't like this because it opens a security hole, but since
3800  *     the nfs server opens a security hole the size of a barn door anyhow,
3801  *     what the heck.
3802  *
3803  * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
3804  * will return EPERM instead of EACCESS. EPERM is always an error.
3805  */
3806 static int
3807 nfsrv_access(struct vnode *vp, int flags, struct ucred *cred,
3808              int rdonly, struct thread *td, int override)
3809 {
3810         struct vattr vattr;
3811         int error;
3812
3813         nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3814         if (flags & VWRITE) {
3815                 /* Just vn_writechk() changed to check rdonly */
3816                 /*
3817                  * Disallow write attempts on read-only file systems;
3818                  * unless the file is a socket or a block or character
3819                  * device resident on the file system.
3820                  */
3821                 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3822                         switch (vp->v_type) {
3823                         case VREG:
3824                         case VDIR:
3825                         case VLNK:
3826                                 return (EROFS);
3827                         default:
3828                                 break;
3829                         }
3830                 }
3831                 /*
3832                  * If there's shared text associated with
3833                  * the inode, we can't allow writing.
3834                  */
3835                 if (vp->v_flag & VTEXT)
3836                         return (ETXTBSY);
3837         }
3838         error = VOP_GETATTR(vp, &vattr, td);
3839         if (error)
3840                 return (error);
3841         error = VOP_ACCESS(vp, flags, cred, td);
3842         /*
3843          * Allow certain operations for the owner (reads and writes
3844          * on files that are already open).
3845          */
3846         if (override && error == EACCES && cred->cr_uid == vattr.va_uid)
3847                 error = 0;
3848         return error;
3849 }
3850 #endif /* NFS_NOSERVER */
3851