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