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