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