2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)nfs_serv.c 8.8 (Berkeley) 7/31/95
37 * $FreeBSD: src/sys/nfs/nfs_serv.c,v 1.93.2.6 2002/12/29 18:19:53 dillon Exp $
41 * nfs version 2 and 3 server calls to vnode ops
42 * - these routines generally have 3 phases
43 * 1 - break down and validate rpc request in mbuf list
44 * 2 - do the vnode ops for the request
45 * (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c)
46 * 3 - build the rpc reply in an mbuf list
48 * - do not mix the phases, since the nfsm_?? macros can return failures
49 * on a bad rpc or similar and do not do any vrele() or vput()'s
51 * - the nfsm_reply() macro generates an nfs rpc reply with the nfs
52 * error number iff error != 0 whereas
53 * returning an error from the server function implies a fatal error
54 * such as a badly constructed rpc request that should be dropped without
56 * For Version 3, nfsm_reply() does not return for the error case, since
57 * most version 3 rpcs return more than the status for error cases.
60 * Warning: always pay careful attention to resource cleanup on return
61 * and note that nfsm_*() macros can terminate a procedure on certain
65 #include <sys/param.h>
66 #include <sys/systm.h>
69 #include <sys/nlookup.h>
70 #include <sys/namei.h>
71 #include <sys/unistd.h>
72 #include <sys/vnode.h>
73 #include <sys/mount.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/malloc.h>
78 #include <sys/dirent.h>
80 #include <sys/kernel.h>
81 #include <sys/sysctl.h>
85 #include <vm/vm_extern.h>
86 #include <vm/vm_object.h>
90 #include <sys/thread2.h>
96 #include "nfsm_subs.h"
99 #define nfsdbprintf(info) kprintf info
101 #define nfsdbprintf(info)
104 #define MAX_REORDERED_RPC (16)
105 #define MAX_COMMIT_COUNT (1024 * 1024)
107 #define NUM_HEURISTIC 1031
108 #define NHUSE_INIT 64
110 #define NHUSE_MAX 2048
112 static struct nfsheur {
113 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
114 off_t nh_nextoff; /* next offset for sequential detection */
115 int nh_use; /* use count for selection */
116 int nh_seqcount; /* heuristic */
117 } nfsheur[NUM_HEURISTIC];
119 nfstype nfsv3_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK,
122 nfstype nfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
125 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
126 int nfsrvw_procrastinate_v3 = 0;
128 static struct timespec nfsver;
130 SYSCTL_DECL(_vfs_nfs);
133 SYSCTL_INT(_vfs_nfs, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0,
134 "Enable unstable and fast writes");
135 static int nfs_commit_blks;
136 static int nfs_commit_miss;
137 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 0,
138 "Number of committed blocks");
139 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 0,
140 "Number of nfs blocks committed from dirty buffers");
142 static int nfsrv_access (struct mount *, struct vnode *, int,
143 struct ucred *, int, struct thread *, int);
144 static void nfsrvw_coalesce (struct nfsrv_descript *,
145 struct nfsrv_descript *);
148 * Heuristic to detect sequential operation.
150 static struct nfsheur *
151 nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp, int writeop)
156 /* Locate best candidate */
158 hi = ((int)(vm_offset_t) vp / sizeof(struct vnode)) % NUM_HEURISTIC;
162 if (nfsheur[hi].nh_vp == vp) {
166 if (nfsheur[hi].nh_use > 0)
167 --nfsheur[hi].nh_use;
168 hi = (hi + 1) % NUM_HEURISTIC;
169 if (nfsheur[hi].nh_use < nh->nh_use)
173 /* Initialize hint if this is a new file */
174 if (nh->nh_vp != vp) {
176 nh->nh_nextoff = uio->uio_offset;
177 nh->nh_use = NHUSE_INIT;
178 if (uio->uio_offset == 0)
185 * Calculate heuristic
187 * See vfs_vnops.c:sequential_heuristic().
189 if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) ||
190 uio->uio_offset == nh->nh_nextoff) {
191 nh->nh_seqcount += howmany(uio->uio_resid, 16384);
192 if (nh->nh_seqcount > IO_SEQMAX)
193 nh->nh_seqcount = IO_SEQMAX;
194 } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC *
195 imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) {
196 /* Probably a reordered RPC, leave seqcount alone. */
197 } else if (nh->nh_seqcount > 1) {
198 nh->nh_seqcount /= 2;
202 nh->nh_use += NHUSE_INC;
203 if (nh->nh_use > NHUSE_MAX)
204 nh->nh_use = NHUSE_MAX;
209 * nfs v3 access service
212 nfsrv3_access(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
213 struct thread *td, struct mbuf **mrq)
215 struct sockaddr *nam = nfsd->nd_nam;
216 struct ucred *cred = &nfsd->nd_cr;
217 struct vnode *vp = NULL;
218 struct mount *mp = NULL;
221 int error = 0, rdonly, getret;
222 struct vattr vattr, *vap = &vattr;
223 u_long testmode, nfsmode;
224 struct nfsm_info info;
227 info.dpos = nfsd->nd_dpos;
228 info.md = nfsd->nd_md;
229 info.mrep = nfsd->nd_mrep;
232 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
233 fhp = &nfh.fh_generic;
234 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
235 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
236 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam, &rdonly,
237 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
239 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
240 nfsm_srvpostop_attr(&info, nfsd, 1, NULL);
244 nfsmode = fxdr_unsigned(u_int32_t, *tl);
245 if ((nfsmode & NFSV3ACCESS_READ) &&
246 nfsrv_access(mp, vp, VREAD, cred, rdonly, td, 0))
247 nfsmode &= ~NFSV3ACCESS_READ;
248 if (vp->v_type == VDIR)
249 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
252 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
253 if ((nfsmode & testmode) &&
254 nfsrv_access(mp, vp, VWRITE, cred, rdonly, td, 0))
255 nfsmode &= ~testmode;
256 if (vp->v_type == VDIR)
257 testmode = NFSV3ACCESS_LOOKUP;
259 testmode = NFSV3ACCESS_EXECUTE;
260 if ((nfsmode & testmode) &&
261 nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 0))
262 nfsmode &= ~testmode;
263 getret = VOP_GETATTR(vp, vap);
266 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
267 NFSX_POSTOPATTR(1) + NFSX_UNSIGNED, &error));
268 nfsm_srvpostop_attr(&info, nfsd, getret, vap);
269 tl = nfsm_build(&info, NFSX_UNSIGNED);
270 *tl = txdr_unsigned(nfsmode);
279 * nfs getattr service
282 nfsrv_getattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
283 struct thread *td, struct mbuf **mrq)
285 struct sockaddr *nam = nfsd->nd_nam;
286 struct ucred *cred = &nfsd->nd_cr;
287 struct nfs_fattr *fp;
289 struct vattr *vap = &va;
290 struct vnode *vp = NULL;
291 struct mount *mp = NULL;
294 int error = 0, rdonly;
295 struct nfsm_info info;
297 info.mrep = nfsd->nd_mrep;
298 info.md = nfsd->nd_md;
299 info.dpos = nfsd->nd_dpos;
302 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
303 fhp = &nfh.fh_generic;
304 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
305 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
306 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
308 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
312 error = VOP_GETATTR(vp, vap);
315 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
316 NFSX_FATTR(nfsd->nd_flag & ND_NFSV3), &error));
321 fp = nfsm_build(&info, NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
322 nfsm_srvfattr(nfsd, vap, fp);
333 * nfs setattr service
336 nfsrv_setattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
337 struct thread *td, struct mbuf **mrq)
339 struct sockaddr *nam = nfsd->nd_nam;
340 struct ucred *cred = &nfsd->nd_cr;
341 struct vattr va, preat;
342 struct vattr *vap = &va;
343 struct nfsv2_sattr *sp;
344 struct nfs_fattr *fp;
345 struct vnode *vp = NULL;
346 struct mount *mp = NULL;
350 int error = 0, rdonly, preat_ret = 1, postat_ret = 1;
352 struct timespec guard;
353 struct nfsm_info info;
355 info.mrep = nfsd->nd_mrep;
357 info.md = nfsd->nd_md;
358 info.dpos = nfsd->nd_dpos;
359 info.v3 = (nfsd->nd_flag & ND_NFSV3);
361 guard.tv_sec = 0; /* fix compiler warning */
364 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
365 fhp = &nfh.fh_generic;
366 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
369 ERROROUT(nfsm_srvsattr(&info, vap));
370 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
371 gcheck = fxdr_unsigned(int, *tl);
373 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
374 fxdr_nfsv3time(tl, &guard);
377 NULLOUT(sp = nfsm_dissect(&info, NFSX_V2SATTR));
379 * Nah nah nah nah na nah
380 * There is a bug in the Sun client that puts 0xffff in the mode
381 * field of sattr when it should put in 0xffffffff. The u_short
382 * doesn't sign extend.
383 * --> check the low order 2 bytes for 0xffff
385 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
386 vap->va_mode = nfstov_mode(sp->sa_mode);
387 if (sp->sa_uid != nfs_xdrneg1)
388 vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
389 if (sp->sa_gid != nfs_xdrneg1)
390 vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
391 if (sp->sa_size != nfs_xdrneg1)
392 vap->va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
393 if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
395 fxdr_nfsv2time(&sp->sa_atime, &vap->va_atime);
397 vap->va_atime.tv_sec =
398 fxdr_unsigned(int32_t, sp->sa_atime.nfsv2_sec);
399 vap->va_atime.tv_nsec = 0;
402 if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
403 fxdr_nfsv2time(&sp->sa_mtime, &vap->va_mtime);
408 * Now that we have all the fields, lets do it.
410 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam, &rdonly,
411 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
413 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
414 2 * NFSX_UNSIGNED, &error));
415 nfsm_srvwcc_data(&info, nfsd, preat_ret, &preat,
422 * vp now an active resource, pay careful attention to cleanup
426 error = preat_ret = VOP_GETATTR(vp, &preat);
427 if (!error && gcheck &&
428 (preat.va_ctime.tv_sec != guard.tv_sec ||
429 preat.va_ctime.tv_nsec != guard.tv_nsec))
430 error = NFSERR_NOT_SYNC;
434 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
435 NFSX_WCCDATA(info.v3), &error));
436 nfsm_srvwcc_data(&info, nfsd, preat_ret, &preat,
444 * If the size is being changed write acces is required, otherwise
445 * just check for a read only file system.
447 if (vap->va_size == ((u_quad_t)((quad_t) -1))) {
448 if (rdonly || (mp->mnt_flag & MNT_RDONLY)) {
453 if (vp->v_type == VDIR) {
456 } else if ((error = nfsrv_access(mp, vp, VWRITE, cred, rdonly,
461 error = VOP_SETATTR(vp, vap, cred);
462 postat_ret = VOP_GETATTR(vp, vap);
468 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
469 NFSX_WCCORFATTR(info.v3), &error));
471 nfsm_srvwcc_data(&info, nfsd, preat_ret, &preat,
476 fp = nfsm_build(&info, NFSX_V2FATTR);
477 nfsm_srvfattr(nfsd, vap, fp);
492 nfsrv_lookup(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
493 struct thread *td, struct mbuf **mrq)
495 struct sockaddr *nam = nfsd->nd_nam;
496 struct ucred *cred = &nfsd->nd_cr;
497 struct nfs_fattr *fp;
498 struct nlookupdata nd;
504 int error = 0, len, dirattr_ret = 1;
506 struct vattr va, dirattr, *vap = &va;
507 struct nfsm_info info;
509 info.mrep = nfsd->nd_mrep;
511 info.md = nfsd->nd_md;
512 info.dpos = nfsd->nd_dpos;
513 info.v3 = (nfsd->nd_flag & ND_NFSV3);
515 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
520 fhp = &nfh.fh_generic;
521 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
522 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
524 pubflag = nfs_ispublicfh(fhp);
526 error = nfs_namei(&nd, cred, 0, NULL, &vp,
527 fhp, len, slp, nam, &info.md, &info.dpos,
528 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), pubflag);
531 * namei failure, only dirp to cleanup. Clear out garbarge from
532 * structure in case macros jump to nfsmout.
538 dirattr_ret = VOP_GETATTR(dirp, &dirattr);
542 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
543 NFSX_POSTOPATTR(info.v3), &error));
544 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
550 * Locate index file for public filehandle
552 * error is 0 on entry and 0 on exit from this block.
556 if (vp->v_type == VDIR && nfs_pub.np_index != NULL) {
558 * Setup call to lookup() to see if we can find
559 * the index file. Arguably, this doesn't belong
560 * in a kernel.. Ugh. If an error occurs, do not
561 * try to install an index file and then clear the
564 * When we replace nd with ind and redirect ndp,
565 * maintenance of ni_startdir and ni_vp shift to
566 * ind and we have to clean them up in the old nd.
567 * However, the cnd resource continues to be maintained
568 * via the original nd. Confused? You aren't alone!
571 cache_copy(&nd.nl_nch, &nch);
573 error = nlookup_init_raw(&nd, nfs_pub.np_index,
574 UIO_SYSSPACE, 0, cred, &nch);
577 error = nlookup(&nd);
581 * Found an index file. Get rid of
582 * the old references. transfer vp and
583 * load up the new vp. Fortunately we do
584 * not have to deal with dvp, that would be
591 error = cache_vget(&nd.nl_nch, nd.nl_cred,
593 KKASSERT(error == 0);
598 * If the public filehandle was used, check that this lookup
599 * didn't result in a filehandle outside the publicly exported
600 * filesystem. We clear the poor vp here to avoid lockups due
604 if (vp->v_mount != nfs_pub.np_mount) {
613 dirattr_ret = VOP_GETATTR(dirp, &dirattr);
619 * Resources at this point:
620 * ndp->ni_vp may not be NULL
625 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
626 NFSX_POSTOPATTR(info.v3), &error));
627 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
633 * Clear out some resources prior to potentially blocking. This
634 * is not as critical as ni_dvp resources in other routines, but
640 * Get underlying attribute, then release remaining resources ( for
641 * the same potential blocking reason ) and reply.
643 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
644 error = VFS_VPTOFH(vp, &fhp->fh_fid);
646 error = VOP_GETATTR(vp, vap);
650 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
651 NFSX_SRVFH(info.v3) +
652 NFSX_POSTOPORFATTR(info.v3) +
653 NFSX_POSTOPATTR(info.v3),
656 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
660 nfsm_srvfhtom(&info, fhp);
662 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
663 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
665 fp = nfsm_build(&info, NFSX_V2FATTR);
666 nfsm_srvfattr(nfsd, vap, fp);
673 nlookup_done(&nd); /* may be called twice */
680 * nfs readlink service
683 nfsrv_readlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
684 struct thread *td, struct mbuf **mrq)
686 struct sockaddr *nam = nfsd->nd_nam;
687 struct ucred *cred = &nfsd->nd_cr;
688 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
689 struct iovec *ivp = iv;
691 int error = 0, rdonly, i, tlen, len, getret;
692 struct mbuf *mp1, *mp2, *mp3;
693 struct vnode *vp = NULL;
694 struct mount *mp = NULL;
698 struct uio io, *uiop = &io;
699 struct nfsm_info info;
701 info.mrep = nfsd->nd_mrep;
703 info.md = nfsd->nd_md;
704 info.dpos = nfsd->nd_dpos;
705 info.v3 = (nfsd->nd_flag & ND_NFSV3);
707 bzero(&io, sizeof(struct uio));
709 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
714 fhp = &nfh.fh_generic;
715 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
718 while (len < NFS_MAXPATHLEN) {
719 mp1 = m_getcl(MB_WAIT, MT_DATA, 0);
720 mp1->m_len = MCLBYTES;
727 if ((len + mp1->m_len) > NFS_MAXPATHLEN) {
728 mp1->m_len = NFS_MAXPATHLEN-len;
729 len = NFS_MAXPATHLEN;
732 ivp->iov_base = mtod(mp1, caddr_t);
733 ivp->iov_len = mp1->m_len;
738 uiop->uio_iovcnt = i;
739 uiop->uio_offset = 0;
740 uiop->uio_resid = len;
741 uiop->uio_rw = UIO_READ;
742 uiop->uio_segflg = UIO_SYSSPACE;
744 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
745 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
747 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
748 2 * NFSX_UNSIGNED, &error));
749 nfsm_srvpostop_attr(&info, nfsd, 1, NULL);
753 if (vp->v_type != VLNK) {
760 error = VOP_READLINK(vp, uiop, cred);
762 getret = VOP_GETATTR(vp, &attr);
765 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
766 NFSX_POSTOPATTR(info.v3) + NFSX_UNSIGNED,
769 nfsm_srvpostop_attr(&info, nfsd, getret, &attr);
775 if (uiop->uio_resid > 0) {
776 len -= uiop->uio_resid;
777 tlen = nfsm_rndup(len);
778 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
780 tl = nfsm_build(&info, NFSX_UNSIGNED);
781 *tl = txdr_unsigned(len);
782 info.mb->m_next = mp3;
797 nfsrv_read(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
798 struct thread *td, struct mbuf **mrq)
800 struct nfsm_info info;
801 struct sockaddr *nam = nfsd->nd_nam;
802 struct ucred *cred = &nfsd->nd_cr;
806 struct nfs_fattr *fp;
810 int error = 0, rdonly, cnt, len, left, siz, tlen, getret;
812 struct vnode *vp = NULL;
813 struct mount *mp = NULL;
816 struct uio io, *uiop = &io;
817 struct vattr va, *vap = &va;
822 info.mrep = nfsd->nd_mrep;
824 info.md = nfsd->nd_md;
825 info.dpos = nfsd->nd_dpos;
826 info.v3 = (nfsd->nd_flag & ND_NFSV3);
828 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
829 fhp = &nfh.fh_generic;
830 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
832 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
833 off = fxdr_hyper(tl);
835 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
836 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
838 NEGREPLYOUT(reqlen = nfsm_srvstrsiz(&info,
839 NFS_SRVMAXDATA(nfsd), &error));
842 * Reference vp. If an error occurs, vp will be invalid, but we
843 * have to NULL it just in case. The macros might goto nfsmout
847 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
848 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
851 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
852 2 * NFSX_UNSIGNED, &error));
853 nfsm_srvpostop_attr(&info, nfsd, 1, NULL);
858 if (vp->v_type != VREG) {
862 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
865 if ((error = nfsrv_access(mp, vp, VREAD, cred, rdonly, td, 1)) != 0)
866 error = nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 1);
868 getret = VOP_GETATTR(vp, vap);
874 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
875 NFSX_POSTOPATTR(info.v3), &error));
876 nfsm_srvpostop_attr(&info, nfsd, getret, vap);
882 * Calculate byte count to read
885 if (off >= vap->va_size)
887 else if ((off + reqlen) > vap->va_size)
888 cnt = vap->va_size - off;
892 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
893 NFSX_POSTOPORFATTR(info.v3) +
894 3 * NFSX_UNSIGNED + nfsm_rndup(cnt),
897 tl = nfsm_build(&info, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
899 fp = (struct nfs_fattr *)tl;
900 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
902 tl = nfsm_build(&info, NFSX_V2FATTR + NFSX_UNSIGNED);
903 fp = (struct nfs_fattr *)tl;
904 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
906 len = left = nfsm_rndup(cnt);
909 * Generate the mbuf list with the uio_iov ref. to it.
914 siz = min(M_TRAILINGSPACE(m), left);
920 m = m_getcl(MB_WAIT, MT_DATA, 0);
926 iv = kmalloc(i * sizeof(struct iovec), M_TEMP, M_WAITOK);
927 uiop->uio_iov = iv2 = iv;
933 panic("nfsrv_read iov");
934 siz = min(M_TRAILINGSPACE(m), left);
936 iv->iov_base = mtod(m, caddr_t) + m->m_len;
945 uiop->uio_iovcnt = i;
946 uiop->uio_offset = off;
947 uiop->uio_resid = len;
948 uiop->uio_rw = UIO_READ;
949 uiop->uio_segflg = UIO_SYSSPACE;
950 nh = nfsrv_sequential_heuristic(uiop, vp, 0);
951 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
952 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
954 off = uiop->uio_offset;
955 nh->nh_nextoff = off;
957 kfree((caddr_t)iv2, M_TEMP);
958 if (error || (getret = VOP_GETATTR(vp, vap))) {
965 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
966 NFSX_POSTOPATTR(info.v3),
968 nfsm_srvpostop_attr(&info, nfsd, getret, vap);
977 nfsm_srvfattr(nfsd, vap, fp);
978 tlen = len - uiop->uio_resid;
979 cnt = cnt < tlen ? cnt : tlen;
980 tlen = nfsm_rndup(cnt);
981 if (len != tlen || tlen != cnt)
982 nfsm_adj(info.mb, len - tlen, tlen - cnt);
984 *tl++ = txdr_unsigned(cnt);
990 *tl = txdr_unsigned(cnt);
1002 nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1003 struct thread *td, struct mbuf **mrq)
1005 struct sockaddr *nam = nfsd->nd_nam;
1006 struct ucred *cred = &nfsd->nd_cr;
1010 struct nfs_fattr *fp;
1012 struct vattr va, forat;
1013 struct vattr *vap = &va;
1015 int error = 0, rdonly, len, forat_ret = 1;
1016 int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
1017 int stable = NFSV3WRITE_FILESYNC;
1018 struct vnode *vp = NULL;
1019 struct mount *mp = NULL;
1023 struct uio io, *uiop = &io;
1024 struct nfsm_info info;
1027 info.mrep = nfsd->nd_mrep;
1029 info.md = nfsd->nd_md;
1030 info.dpos = nfsd->nd_dpos;
1031 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1033 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1034 if (info.mrep == NULL) {
1038 fhp = &nfh.fh_generic;
1039 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
1041 NULLOUT(tl = nfsm_dissect(&info, 5 * NFSX_UNSIGNED));
1042 off = fxdr_hyper(tl);
1044 stable = fxdr_unsigned(int, *tl++);
1046 NULLOUT(tl = nfsm_dissect(&info, 4 * NFSX_UNSIGNED));
1047 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1050 stable = NFSV3WRITE_UNSTABLE;
1052 retlen = len = fxdr_unsigned(int32_t, *tl);
1056 * For NFS Version 2, it is not obvious what a write of zero length
1057 * should do, but I might as well be consistent with Version 3,
1058 * which is to return ok so long as there are no permission problems.
1064 if (mp1 == info.md) {
1066 adjust = info.dpos - mtod(mp1, caddr_t);
1067 mp1->m_len -= adjust;
1068 if (mp1->m_len > 0 && adjust > 0)
1069 mp1->m_data += adjust;
1073 else if (mp1->m_len > 0) {
1076 mp1->m_len -= (i - len);
1085 if (len > NFS_MAXDATA || len < 0 || i < len) {
1087 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1088 2 * NFSX_UNSIGNED, &error));
1089 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1094 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
1095 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1098 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1099 2 * NFSX_UNSIGNED, &error));
1100 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1106 forat_ret = VOP_GETATTR(vp, &forat);
1107 if (vp->v_type != VREG) {
1111 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1114 error = nfsrv_access(mp, vp, VWRITE, cred, rdonly, td, 1);
1119 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1120 NFSX_WCCDATA(info.v3), &error));
1121 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1128 ivp = kmalloc(cnt * sizeof(struct iovec), M_TEMP, M_WAITOK);
1129 uiop->uio_iov = iv = ivp;
1130 uiop->uio_iovcnt = cnt;
1133 if (mp1->m_len > 0) {
1134 ivp->iov_base = mtod(mp1, caddr_t);
1135 ivp->iov_len = mp1->m_len;
1143 * The IO_METASYNC flag indicates that all metadata (and not just
1144 * enough to ensure data integrity) mus be written to stable storage
1146 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1148 if (stable == NFSV3WRITE_UNSTABLE)
1149 ioflags = IO_NODELOCKED;
1150 else if (stable == NFSV3WRITE_DATASYNC)
1151 ioflags = (IO_SYNC | IO_NODELOCKED);
1153 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1154 uiop->uio_resid = len;
1155 uiop->uio_rw = UIO_WRITE;
1156 uiop->uio_segflg = UIO_SYSSPACE;
1157 uiop->uio_td = NULL;
1158 uiop->uio_offset = off;
1159 nh = nfsrv_sequential_heuristic(uiop, vp, 1);
1160 ioflags |= nh->nh_seqcount << IO_SEQSHIFT;
1161 error = VOP_WRITE(vp, uiop, ioflags, cred);
1163 nh->nh_nextoff = uiop->uio_offset;
1164 nfsstats.srvvop_writes++;
1165 kfree((caddr_t)iv, M_TEMP);
1167 aftat_ret = VOP_GETATTR(vp, vap);
1172 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1173 NFSX_PREOPATTR(info.v3) +
1174 NFSX_POSTOPORFATTR(info.v3) +
1175 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(info.v3),
1178 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1184 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1185 *tl++ = txdr_unsigned(retlen);
1187 * If nfs_async is set, then pretend the write was FILESYNC.
1189 if (stable == NFSV3WRITE_UNSTABLE && !nfs_async)
1190 *tl++ = txdr_unsigned(stable);
1192 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
1194 * Actually, there is no need to txdr these fields,
1195 * but it may make the values more human readable,
1196 * for debugging purposes.
1198 if (nfsver.tv_sec == 0)
1200 *tl++ = txdr_unsigned(nfsver.tv_sec);
1201 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1203 fp = nfsm_build(&info, NFSX_V2FATTR);
1204 nfsm_srvfattr(nfsd, vap, fp);
1214 * NFS write service with write gathering support. Called when
1215 * nfsrvw_procrastinate > 0.
1216 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1217 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1221 nfsrv_writegather(struct nfsrv_descript **ndp, struct nfssvc_sock *slp,
1222 struct thread *td, struct mbuf **mrq)
1225 struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1226 struct nfs_fattr *fp;
1229 struct nfsrvw_delayhash *wpp;
1231 struct vattr va, forat;
1233 int error = 0, rdonly, len, forat_ret = 1;
1234 int ioflags, aftat_ret = 1, adjust, zeroing;
1236 struct vnode *vp = NULL;
1237 struct mount *mp = NULL;
1238 struct uio io, *uiop = &io;
1240 struct nfsm_info info;
1244 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1252 info.mrep = nfsd->nd_mrep;
1254 info.md = nfsd->nd_md;
1255 info.dpos = nfsd->nd_dpos;
1256 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1257 cred = &nfsd->nd_cr;
1258 LIST_INIT(&nfsd->nd_coalesce);
1259 nfsd->nd_mreq = NULL;
1260 nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1261 cur_usec = nfs_curusec();
1262 nfsd->nd_time = cur_usec +
1263 (info.v3 ? nfsrvw_procrastinate_v3 : nfsrvw_procrastinate);
1266 * Now, get the write header..
1268 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, &nfsd->nd_fh, &error));
1270 NULLOUT(tl = nfsm_dissect(&info, 5 * NFSX_UNSIGNED));
1271 nfsd->nd_off = fxdr_hyper(tl);
1273 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1275 NULLOUT(tl = nfsm_dissect(&info, 4 * NFSX_UNSIGNED));
1276 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1279 nfsd->nd_stable = NFSV3WRITE_UNSTABLE;
1281 len = fxdr_unsigned(int32_t, *tl);
1283 nfsd->nd_eoff = nfsd->nd_off + len;
1286 * Trim the header out of the mbuf list and trim off any trailing
1287 * junk so that the mbuf list has only the write data.
1293 if (mp1 == info.md) {
1295 adjust = info.dpos - mtod(mp1, caddr_t);
1296 mp1->m_len -= adjust;
1297 if (mp1->m_len > 0 && adjust > 0)
1298 mp1->m_data += adjust;
1305 mp1->m_len -= (i - len);
1311 if (len > NFS_MAXDATA || len < 0 || i < len) {
1316 nfsm_writereply(&info, nfsd, slp, error, 2 * NFSX_UNSIGNED);
1318 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1321 nfsd->nd_mreq = info.mreq;
1322 nfsd->nd_mrep = NULL;
1327 * Add this entry to the hash and time queues.
1330 wp = slp->ns_tq.lh_first;
1331 while (wp && wp->nd_time < nfsd->nd_time) {
1333 wp = wp->nd_tq.le_next;
1335 NFS_DPF(WG, ("Q%03x", nfsd->nd_retxid & 0xfff));
1337 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1339 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1341 if (nfsd->nd_mrep) {
1342 wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
1346 bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1348 wp = wp->nd_hash.le_next;
1350 while (wp && wp->nd_off < nfsd->nd_off &&
1351 !bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1353 wp = wp->nd_hash.le_next;
1356 LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1359 * Search the hash list for overlapping entries and
1362 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1363 wp = nfsd->nd_hash.le_next;
1364 if (NFSW_SAMECRED(owp, nfsd))
1365 nfsrvw_coalesce(owp, nfsd);
1368 LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1374 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1375 * and generate the associated reply mbuf list(s).
1378 cur_usec = nfs_curusec();
1379 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = owp) {
1380 owp = nfsd->nd_tq.le_next;
1381 if (nfsd->nd_time > cur_usec)
1385 NFS_DPF(WG, ("P%03x", nfsd->nd_retxid & 0xfff));
1386 LIST_REMOVE(nfsd, nd_tq);
1387 LIST_REMOVE(nfsd, nd_hash);
1388 info.mrep = nfsd->nd_mrep;
1390 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1391 nfsd->nd_mrep = NULL;
1392 cred = &nfsd->nd_cr;
1393 forat_ret = aftat_ret = 1;
1394 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &mp, &vp, cred, slp,
1395 nfsd->nd_nam, &rdonly,
1396 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1399 forat_ret = VOP_GETATTR(vp, &forat);
1400 if (vp->v_type != VREG) {
1404 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1410 error = nfsrv_access(mp, vp, VWRITE, cred, rdonly, td, 1);
1413 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1414 ioflags = IO_NODELOCKED;
1415 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1416 ioflags = (IO_SYNC | IO_NODELOCKED);
1418 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1419 uiop->uio_rw = UIO_WRITE;
1420 uiop->uio_segflg = UIO_SYSSPACE;
1421 uiop->uio_td = NULL;
1422 uiop->uio_offset = nfsd->nd_off;
1423 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1424 if (uiop->uio_resid > 0) {
1432 uiop->uio_iovcnt = i;
1433 iov = kmalloc(i * sizeof(struct iovec), M_TEMP, M_WAITOK);
1434 uiop->uio_iov = ivp = iov;
1437 if (mp1->m_len > 0) {
1438 ivp->iov_base = mtod(mp1, caddr_t);
1439 ivp->iov_len = mp1->m_len;
1445 error = VOP_WRITE(vp, uiop, ioflags, cred);
1446 nfsstats.srvvop_writes++;
1448 kfree((caddr_t)iov, M_TEMP);
1453 aftat_ret = VOP_GETATTR(vp, &va);
1459 * Loop around generating replies for all write rpcs that have
1460 * now been completed.
1464 NFS_DPF(WG, ("R%03x", nfsd->nd_retxid & 0xfff));
1466 nfsm_writereply(&info, nfsd, slp, error,
1467 NFSX_WCCDATA(info.v3));
1469 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1473 nfsm_writereply(&info, nfsd, slp, error,
1474 NFSX_PREOPATTR(info.v3) +
1475 NFSX_POSTOPORFATTR(info.v3) +
1477 NFSX_WRITEVERF(info.v3));
1479 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1481 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1482 *tl++ = txdr_unsigned(nfsd->nd_len);
1483 *tl++ = txdr_unsigned(swp->nd_stable);
1485 * Actually, there is no need to txdr these fields,
1486 * but it may make the values more human readable,
1487 * for debugging purposes.
1489 if (nfsver.tv_sec == 0)
1491 *tl++ = txdr_unsigned(nfsver.tv_sec);
1492 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1494 fp = nfsm_build(&info, NFSX_V2FATTR);
1495 nfsm_srvfattr(nfsd, &va, fp);
1498 nfsd->nd_mreq = info.mreq;
1500 panic("nfsrv_write: nd_mrep not free");
1503 * Done. Put it at the head of the timer queue so that
1504 * the final phase can return the reply.
1508 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1510 nfsd = swp->nd_coalesce.lh_first;
1512 LIST_REMOVE(nfsd, nd_tq);
1516 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1521 * Search for a reply to return.
1523 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = nfsd->nd_tq.le_next) {
1524 if (nfsd->nd_mreq) {
1525 NFS_DPF(WG, ("X%03x", nfsd->nd_retxid & 0xfff));
1526 LIST_REMOVE(nfsd, nd_tq);
1532 *mrq = nfsd->nd_mreq;
1541 * Coalesce the write request nfsd into owp. To do this we must:
1542 * - remove nfsd from the queues
1543 * - merge nfsd->nd_mrep into owp->nd_mrep
1544 * - update the nd_eoff and nd_stable for owp
1545 * - put nfsd on owp's nd_coalesce list
1546 * NB: Must be called at splsoftclock().
1549 nfsrvw_coalesce(struct nfsrv_descript *owp, struct nfsrv_descript *nfsd)
1553 struct nfsrv_descript *p;
1555 NFS_DPF(WG, ("C%03x-%03x",
1556 nfsd->nd_retxid & 0xfff, owp->nd_retxid & 0xfff));
1557 LIST_REMOVE(nfsd, nd_hash);
1558 LIST_REMOVE(nfsd, nd_tq);
1559 if (owp->nd_eoff < nfsd->nd_eoff) {
1560 overlap = owp->nd_eoff - nfsd->nd_off;
1562 panic("nfsrv_coalesce: bad off");
1564 m_adj(nfsd->nd_mrep, overlap);
1568 mp1->m_next = nfsd->nd_mrep;
1569 owp->nd_eoff = nfsd->nd_eoff;
1571 m_freem(nfsd->nd_mrep);
1572 nfsd->nd_mrep = NULL;
1573 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1574 owp->nd_stable = NFSV3WRITE_FILESYNC;
1575 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1576 owp->nd_stable == NFSV3WRITE_UNSTABLE)
1577 owp->nd_stable = NFSV3WRITE_DATASYNC;
1578 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1581 * If nfsd had anything else coalesced into it, transfer them
1582 * to owp, otherwise their replies will never get sent.
1584 for (p = nfsd->nd_coalesce.lh_first; p;
1585 p = nfsd->nd_coalesce.lh_first) {
1586 LIST_REMOVE(p, nd_tq);
1587 LIST_INSERT_HEAD(&owp->nd_coalesce, p, nd_tq);
1592 * nfs create service
1593 * now does a truncate to 0 length via. setattr if it already exists
1596 nfsrv_create(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1597 struct thread *td, struct mbuf **mrq)
1599 struct sockaddr *nam = nfsd->nd_nam;
1600 struct ucred *cred = &nfsd->nd_cr;
1601 struct nfs_fattr *fp;
1602 struct vattr va, dirfor, diraft;
1603 struct vattr *vap = &va;
1604 struct nfsv2_sattr *sp;
1606 struct nlookupdata nd;
1607 int error = 0, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1608 udev_t rdev = NOUDEV;
1610 int how, exclusive_flag = 0;
1618 u_char cverf[NFSX_V3CREATEVERF];
1619 struct nfsm_info info;
1621 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1627 info.mrep = nfsd->nd_mrep;
1629 info.md = nfsd->nd_md;
1630 info.dpos = nfsd->nd_dpos;
1631 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1633 fhp = &nfh.fh_generic;
1634 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
1635 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
1638 * Call namei and do initial cleanup to get a few things
1639 * out of the way. If we get an initial error we cleanup
1640 * and return here to avoid special-casing the invalid nd
1641 * structure through the rest of the case. dirp may be
1642 * set even if an error occurs, but the nd structure will not
1643 * be valid at all if an error occurs so we have to invalidate it
1644 * prior to calling nfsm_reply ( which might goto nfsmout ).
1646 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
1647 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
1648 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1649 mp = vfs_getvfs(&fhp->fh_fsid);
1653 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
1660 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1661 NFSX_WCCDATA(info.v3), &error));
1662 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1663 diraft_ret, &diraft);
1669 * No error. Continue. State:
1672 * vp may be valid or NULL if the target does not
1676 * The error state is set through the code and we may also do some
1677 * opportunistic releasing of vnodes to avoid holding locks through
1678 * NFS I/O. The cleanup at the end is a catch-all
1683 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
1684 how = fxdr_unsigned(int, *tl);
1686 case NFSV3CREATE_GUARDED:
1692 case NFSV3CREATE_UNCHECKED:
1693 ERROROUT(nfsm_srvsattr(&info, vap));
1695 case NFSV3CREATE_EXCLUSIVE:
1696 NULLOUT(cp = nfsm_dissect(&info, NFSX_V3CREATEVERF));
1697 bcopy(cp, cverf, NFSX_V3CREATEVERF);
1701 vap->va_type = VREG;
1703 NULLOUT(sp = nfsm_dissect(&info, NFSX_V2SATTR));
1704 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1705 if (vap->va_type == VNON)
1706 vap->va_type = VREG;
1707 vap->va_mode = nfstov_mode(sp->sa_mode);
1708 switch (vap->va_type) {
1710 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1712 vap->va_size = (u_quad_t)tsize;
1717 rdev = fxdr_unsigned(long, sp->sa_size);
1725 * Iff doesn't exist, create it
1726 * otherwise just truncate to 0 length
1727 * should I set the mode too ?
1729 * The only possible error we can have at this point is EEXIST.
1730 * nd.ni_vp will also be non-NULL in that case.
1733 if (vap->va_mode == (mode_t)VNOVAL)
1735 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1737 error = VOP_NCREATE(&nd.nl_nch, dvp, &vp,
1742 if (exclusive_flag) {
1745 bcopy(cverf, (caddr_t)&vap->va_atime,
1747 error = VOP_SETATTR(vp, vap, cred);
1751 vap->va_type == VCHR ||
1752 vap->va_type == VBLK ||
1753 vap->va_type == VFIFO
1756 * Handle SysV FIFO node special cases. All other
1757 * devices require super user to access.
1759 if (vap->va_type == VCHR && rdev == 0xffffffff)
1760 vap->va_type = VFIFO;
1761 if (vap->va_type != VFIFO &&
1762 (error = priv_check_cred(cred, PRIV_ROOT, 0))) {
1765 vap->va_rmajor = umajor(rdev);
1766 vap->va_rminor = uminor(rdev);
1769 error = VOP_NMKNOD(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1776 * XXX what is this junk supposed to do ?
1783 * release dvp prior to lookup
1791 * Even though LOCKPARENT was cleared, ni_dvp may
1794 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
1795 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT);
1796 nd.ni_cnd.cn_td = td;
1797 nd.ni_cnd.cn_cred = cred;
1799 error = lookup(&nd);
1803 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1805 /* fall through on certain errors */
1807 nfsrv_object_create(nd.ni_vp);
1808 if (nd.ni_cnd.cn_flags & CNP_ISSYMLINK) {
1817 if (vap->va_size != -1) {
1818 error = nfsrv_access(mp, vp, VWRITE, cred,
1819 (nd.nl_flags & NLC_NFS_RDONLY), td, 0);
1821 tempsize = vap->va_size;
1823 vap->va_size = tempsize;
1824 error = VOP_SETATTR(vp, vap, cred);
1830 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
1831 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1833 error = VOP_GETATTR(vp, vap);
1836 if (exclusive_flag && !error &&
1837 bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1839 diraft_ret = VOP_GETATTR(dirp, &diraft);
1843 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1844 NFSX_SRVFH(info.v3) + NFSX_FATTR(info.v3) +
1845 NFSX_WCCDATA(info.v3),
1849 nfsm_srvpostop_fh(&info, fhp);
1850 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
1852 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1853 diraft_ret, &diraft);
1856 nfsm_srvfhtom(&info, fhp);
1857 fp = nfsm_build(&info, NFSX_V2FATTR);
1858 nfsm_srvfattr(nfsd, vap, fp);
1863 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
1884 * nfs v3 mknod service
1887 nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1888 struct thread *td, struct mbuf **mrq)
1890 struct sockaddr *nam = nfsd->nd_nam;
1891 struct ucred *cred = &nfsd->nd_cr;
1892 struct vattr va, dirfor, diraft;
1893 struct vattr *vap = &va;
1895 struct nlookupdata nd;
1896 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
1903 struct nfsm_info info;
1905 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1911 info.mrep = nfsd->nd_mrep;
1913 info.md = nfsd->nd_md;
1914 info.dpos = nfsd->nd_dpos;
1916 fhp = &nfh.fh_generic;
1917 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
1918 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
1921 * Handle nfs_namei() call. If an error occurs, the nd structure
1922 * is not valid. However, nfsm_*() routines may still jump to
1926 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
1927 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
1928 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1930 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
1932 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1933 NFSX_WCCDATA(1), &error));
1934 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1935 diraft_ret, &diraft);
1939 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
1940 vtyp = nfsv3tov_type(*tl);
1941 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1942 error = NFSERR_BADTYPE;
1946 ERROROUT(nfsm_srvsattr(&info, vap));
1947 if (vtyp == VCHR || vtyp == VBLK) {
1948 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
1949 vap->va_rmajor = fxdr_unsigned(u_int32_t, *tl++);
1950 vap->va_rminor = fxdr_unsigned(u_int32_t, *tl);
1954 * Iff doesn't exist, create it.
1960 vap->va_type = vtyp;
1961 if (vap->va_mode == (mode_t)VNOVAL)
1963 if (vtyp == VSOCK) {
1965 error = VOP_NCREATE(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1969 if (vtyp != VFIFO && (error = priv_check_cred(cred, PRIV_ROOT, 0)))
1973 error = VOP_NMKNOD(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1981 * send response, cleanup, return.
1993 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
1994 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1996 error = VOP_GETATTR(vp, vap);
2002 diraft_ret = VOP_GETATTR(dirp, &diraft);
2007 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2008 NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) +
2009 NFSX_WCCDATA(1), &error));
2011 nfsm_srvpostop_fh(&info, fhp);
2012 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
2014 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2015 diraft_ret, &diraft);
2035 * nfs remove service
2038 nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2039 struct thread *td, struct mbuf **mrq)
2041 struct sockaddr *nam = nfsd->nd_nam;
2042 struct ucred *cred = &nfsd->nd_cr;
2043 struct nlookupdata nd;
2044 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2048 struct vattr dirfor, diraft;
2051 struct nfsm_info info;
2053 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2059 info.mrep = nfsd->nd_mrep;
2061 info.md = nfsd->nd_md;
2062 info.dpos = nfsd->nd_dpos;
2063 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2065 fhp = &nfh.fh_generic;
2066 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2067 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2069 error = nfs_namei(&nd, cred, NLC_DELETE, &dvp, &vp,
2070 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2071 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2074 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2077 if (vp->v_type == VDIR) {
2078 error = EPERM; /* POSIX */
2082 * The root of a mounted filesystem cannot be deleted.
2084 if (vp->v_flag & VROOT) {
2096 error = VOP_NREMOVE(&nd.nl_nch, dvp, nd.nl_cred);
2101 if (dirp && info.v3)
2102 diraft_ret = VOP_GETATTR(dirp, &diraft);
2103 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_WCCDATA(info.v3), &error));
2105 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2106 diraft_ret, &diraft);
2126 * nfs rename service
2129 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2130 struct thread *td, struct mbuf **mrq)
2132 struct sockaddr *nam = nfsd->nd_nam;
2133 struct ucred *cred = &nfsd->nd_cr;
2134 int error = 0, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2135 int tdirfor_ret = 1, tdiraft_ret = 1;
2136 struct nlookupdata fromnd, tond;
2137 struct vnode *fvp, *fdirp, *fdvp;
2138 struct vnode *tvp, *tdirp, *tdvp;
2139 struct namecache *ncp;
2140 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2142 fhandle_t *ffhp, *tfhp;
2144 struct nfsm_info info;
2146 info.mrep = nfsd->nd_mrep;
2148 info.md = nfsd->nd_md;
2149 info.dpos = nfsd->nd_dpos;
2150 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2152 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2156 ffhp = &fnfh.fh_generic;
2157 tfhp = &tnfh.fh_generic;
2160 * Clear fields incase goto nfsmout occurs from macro.
2163 nlookup_zero(&fromnd);
2164 nlookup_zero(&tond);
2168 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, ffhp, &error));
2169 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2172 * Remember our original uid so that we can reset cr_uid before
2173 * the second nfs_namei() call, in case it is remapped.
2175 saved_uid = cred->cr_uid;
2176 error = nfs_namei(&fromnd, cred, NLC_RENAME_SRC,
2178 ffhp, len, slp, nam, &info.md, &info.dpos, &fdirp,
2179 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2182 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor);
2185 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2186 2 * NFSX_WCCDATA(info.v3), &error));
2187 nfsm_srvwcc_data(&info, nfsd, fdirfor_ret, &fdirfor,
2188 fdiraft_ret, &fdiraft);
2189 nfsm_srvwcc_data(&info, nfsd, tdirfor_ret, &tdirfor,
2190 tdiraft_ret, &tdiraft);
2196 * We have to unlock the from ncp before we can safely lookup
2199 KKASSERT(fromnd.nl_flags & NLC_NCPISLOCKED);
2200 cache_unlock(&fromnd.nl_nch);
2201 fromnd.nl_flags &= ~NLC_NCPISLOCKED;
2202 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, tfhp, &error));
2203 NEGATIVEOUT(len2 = nfsm_strsiz(&info, NFS_MAXNAMLEN));
2204 cred->cr_uid = saved_uid;
2206 error = nfs_namei(&tond, cred, NLC_RENAME_DST, NULL, NULL,
2207 tfhp, len2, slp, nam, &info.md, &info.dpos, &tdirp,
2208 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2211 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor);
2219 if (cache_lock_nonblock(&fromnd.nl_nch) == 0) {
2220 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2221 } else if (fromnd.nl_nch.ncp > tond.nl_nch.ncp) {
2222 cache_lock(&fromnd.nl_nch);
2223 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2225 cache_unlock(&tond.nl_nch);
2226 cache_lock(&fromnd.nl_nch);
2227 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2228 cache_lock(&tond.nl_nch);
2229 cache_resolve(&tond.nl_nch, tond.nl_cred);
2231 fromnd.nl_flags |= NLC_NCPISLOCKED;
2233 fvp = fromnd.nl_nch.ncp->nc_vp;
2234 tvp = tond.nl_nch.ncp->nc_vp;
2237 * Set fdvp and tdvp. We haven't done all the topology checks
2238 * so these can wind up NULL (e.g. if either fvp or tvp is a mount
2239 * point). If we get through the checks these will be guarenteed
2242 * Holding the children ncp's should be sufficient to prevent
2243 * fdvp and tdvp ripouts.
2245 if (fromnd.nl_nch.ncp->nc_parent)
2246 fdvp = fromnd.nl_nch.ncp->nc_parent->nc_vp;
2249 if (tond.nl_nch.ncp->nc_parent)
2250 tdvp = tond.nl_nch.ncp->nc_parent->nc_vp;
2255 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2261 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2268 if (tvp->v_type == VDIR && (tond.nl_nch.ncp->nc_flag & NCF_ISMOUNTPT)) {
2276 if (fvp->v_type == VDIR && (fromnd.nl_nch.ncp->nc_flag & NCF_ISMOUNTPT)) {
2283 if (fromnd.nl_nch.mount != tond.nl_nch.mount) {
2290 if (fromnd.nl_nch.ncp == tond.nl_nch.ncp->nc_parent) {
2298 * You cannot rename a source into itself or a subdirectory of itself.
2299 * We check this by travsering the target directory upwards looking
2300 * for a match against the source.
2303 for (ncp = tond.nl_nch.ncp; ncp; ncp = ncp->nc_parent) {
2304 if (fromnd.nl_nch.ncp == ncp) {
2312 * If source is the same as the destination (that is the
2313 * same vnode with the same name in the same directory),
2314 * then there is nothing to do.
2316 if (fromnd.nl_nch.ncp == tond.nl_nch.ncp)
2321 * The VOP_NRENAME function releases all vnode references &
2322 * locks prior to returning so we need to clear the pointers
2323 * to bypass cleanup code later on.
2325 error = VOP_NRENAME(&fromnd.nl_nch, &tond.nl_nch,
2326 fdvp, tdvp, tond.nl_cred);
2335 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft);
2337 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft);
2338 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2339 2 * NFSX_WCCDATA(info.v3), &error));
2341 nfsm_srvwcc_data(&info, nfsd, fdirfor_ret, &fdirfor,
2342 fdiraft_ret, &fdiraft);
2343 nfsm_srvwcc_data(&info, nfsd, tdirfor_ret, &tdirfor,
2344 tdiraft_ret, &tdiraft);
2353 nlookup_done(&tond);
2356 nlookup_done(&fromnd);
2364 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2365 struct thread *td, struct mbuf **mrq)
2367 struct sockaddr *nam = nfsd->nd_nam;
2368 struct ucred *cred = &nfsd->nd_cr;
2369 struct nlookupdata nd;
2370 int error = 0, rdonly, len, dirfor_ret = 1, diraft_ret = 1;
2378 struct vattr dirfor, diraft, at;
2380 fhandle_t *fhp, *dfhp;
2381 struct nfsm_info info;
2383 info.mrep = nfsd->nd_mrep;
2385 info.md = nfsd->nd_md;
2386 info.dpos = nfsd->nd_dpos;
2387 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2389 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2391 dirp = dvp = vp = xp = NULL;
2394 fhp = &nfh.fh_generic;
2395 dfhp = &dnfh.fh_generic;
2396 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2397 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, dfhp, &error));
2398 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2400 error = nfsrv_fhtovp(fhp, FALSE, &xmp, &xp, cred, slp, nam,
2401 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2403 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2404 NFSX_POSTOPATTR(info.v3) +
2405 NFSX_WCCDATA(info.v3),
2407 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2408 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2409 diraft_ret, &diraft);
2414 if (xp->v_type == VDIR) {
2415 error = EPERM; /* POSIX */
2419 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2420 dfhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2421 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2424 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2433 if (xp->v_mount != dvp->v_mount)
2438 error = VOP_NLINK(&nd.nl_nch, dvp, xp, nd.nl_cred);
2446 getret = VOP_GETATTR(xp, &at);
2448 diraft_ret = VOP_GETATTR(dirp, &diraft);
2449 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2450 NFSX_POSTOPATTR(info.v3) + NFSX_WCCDATA(info.v3),
2453 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2454 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2455 diraft_ret, &diraft);
2479 * nfs symbolic link service
2482 nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2483 struct thread *td, struct mbuf **mrq)
2485 struct sockaddr *nam = nfsd->nd_nam;
2486 struct ucred *cred = &nfsd->nd_cr;
2487 struct vattr va, dirfor, diraft;
2488 struct nlookupdata nd;
2489 struct vattr *vap = &va;
2490 struct nfsv2_sattr *sp;
2491 char *pathcp = NULL;
2494 int error = 0, len, len2, dirfor_ret = 1, diraft_ret = 1;
2500 struct nfsm_info info;
2502 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2508 info.mrep = nfsd->nd_mrep;
2510 info.md = nfsd->nd_md;
2511 info.dpos = nfsd->nd_dpos;
2512 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2514 fhp = &nfh.fh_generic;
2515 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2516 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2518 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2519 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2520 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2523 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2530 ERROROUT(nfsm_srvsattr(&info, vap));
2532 NEGATIVEOUT(len2 = nfsm_strsiz(&info, NFS_MAXPATHLEN));
2533 pathcp = kmalloc(len2 + 1, M_TEMP, M_WAITOK);
2534 iv.iov_base = pathcp;
2536 io.uio_resid = len2;
2540 io.uio_segflg = UIO_SYSSPACE;
2541 io.uio_rw = UIO_READ;
2543 ERROROUT(nfsm_mtouio(&info, &io, len2));
2545 NULLOUT(sp = nfsm_dissect(&info, NFSX_V2SATTR));
2546 vap->va_mode = nfstov_mode(sp->sa_mode);
2548 *(pathcp + len2) = '\0';
2554 if (vap->va_mode == (mode_t)VNOVAL)
2558 error = VOP_NSYMLINK(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap, pathcp);
2562 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
2563 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2565 error = VOP_GETATTR(vp, vap);
2580 kfree(pathcp, M_TEMP);
2584 diraft_ret = VOP_GETATTR(dirp, &diraft);
2588 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2589 NFSX_SRVFH(info.v3) + NFSX_POSTOPATTR(info.v3) +
2590 NFSX_WCCDATA(info.v3),
2594 nfsm_srvpostop_fh(&info, fhp);
2595 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
2597 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2598 diraft_ret, &diraft);
2611 kfree(pathcp, M_TEMP);
2619 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2620 struct thread *td, struct mbuf **mrq)
2622 struct sockaddr *nam = nfsd->nd_nam;
2623 struct ucred *cred = &nfsd->nd_cr;
2624 struct vattr va, dirfor, diraft;
2625 struct vattr *vap = &va;
2626 struct nfs_fattr *fp;
2627 struct nlookupdata nd;
2629 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2635 struct nfsm_info info;
2637 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2643 info.dpos = nfsd->nd_dpos;
2644 info.mrep = nfsd->nd_mrep;
2646 info.md = nfsd->nd_md;
2647 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2649 fhp = &nfh.fh_generic;
2650 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2651 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2653 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2654 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2655 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2658 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2661 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2662 NFSX_WCCDATA(info.v3), &error));
2663 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2664 diraft_ret, &diraft);
2670 ERROROUT(nfsm_srvsattr(&info, vap));
2672 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2673 vap->va_mode = nfstov_mode(*tl++);
2677 * At this point nd.ni_dvp is referenced and exclusively locked and
2678 * nd.ni_vp, if it exists, is referenced but not locked.
2681 vap->va_type = VDIR;
2688 * Issue mkdir op. Since SAVESTART is not set, the pathname
2689 * component is freed by the VOP call. This will fill-in
2690 * nd.ni_vp, reference, and exclusively lock it.
2692 if (vap->va_mode == (mode_t)VNOVAL)
2695 error = VOP_NMKDIR(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
2700 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
2701 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2703 error = VOP_GETATTR(vp, vap);
2707 diraft_ret = VOP_GETATTR(dirp, &diraft);
2708 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2709 NFSX_SRVFH(info.v3) + NFSX_POSTOPATTR(info.v3) +
2710 NFSX_WCCDATA(info.v3),
2714 nfsm_srvpostop_fh(&info, fhp);
2715 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
2717 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2718 diraft_ret, &diraft);
2720 nfsm_srvfhtom(&info, fhp);
2721 fp = nfsm_build(&info, NFSX_V2FATTR);
2722 nfsm_srvfattr(nfsd, vap, fp);
2747 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2748 struct thread *td, struct mbuf **mrq)
2750 struct sockaddr *nam = nfsd->nd_nam;
2751 struct ucred *cred = &nfsd->nd_cr;
2752 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2756 struct vattr dirfor, diraft;
2759 struct nlookupdata nd;
2760 struct nfsm_info info;
2762 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2768 info.mrep = nfsd->nd_mrep;
2770 info.md = nfsd->nd_md;
2771 info.dpos = nfsd->nd_dpos;
2772 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2774 fhp = &nfh.fh_generic;
2775 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2776 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2778 error = nfs_namei(&nd, cred, NLC_DELETE, &dvp, &vp,
2779 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2780 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2783 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2786 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2787 NFSX_WCCDATA(info.v3), &error));
2788 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2789 diraft_ret, &diraft);
2793 if (vp->v_type != VDIR) {
2799 * The root of a mounted filesystem cannot be deleted.
2801 if (vp->v_flag & VROOT)
2805 * Issue or abort op. Since SAVESTART is not set, path name
2806 * component is freed by the VOP after either.
2813 error = VOP_NRMDIR(&nd.nl_nch, dvp, nd.nl_cred);
2820 diraft_ret = VOP_GETATTR(dirp, &diraft);
2821 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_WCCDATA(info.v3), &error));
2823 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2824 diraft_ret, &diraft);
2846 * nfs readdir service
2847 * - mallocs what it thinks is enough to read
2848 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2849 * - calls VOP_READDIR()
2850 * - loops around building the reply
2851 * if the output generated exceeds count break out of loop
2852 * The nfsm_clget macro is used here so that the reply will be packed
2853 * tightly in mbuf clusters.
2854 * - it only knows that it has encountered eof when the VOP_READDIR()
2856 * - as such one readdir rpc will return eof false although you are there
2857 * and then the next will return eof
2858 * - it trims out records with d_fileno == 0
2859 * this doesn't matter for Unix clients, but they might confuse clients
2861 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2862 * than requested, but this may not apply to all filesystems. For
2863 * example, client NFS does not { although it is never remote mounted
2865 * The alternate call nfsrv_readdirplus() does lookups as well.
2866 * PS: The NFS protocol spec. does not clarify what the "count" byte
2867 * argument is a count of.. just name strings and file id's or the
2868 * entire reply rpc or ...
2869 * I tried just file name and id sizes and it confused the Sun client,
2870 * so I am using the full rpc size now. The "paranoia.." comment refers
2871 * to including the status longwords that are not a part of the dir.
2872 * "entry" structures, but are in the rpc.
2876 u_int32_t fl_postopok;
2877 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2879 u_int32_t fl_fhsize;
2880 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2884 nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2885 struct thread *td, struct mbuf **mrq)
2887 struct sockaddr *nam = nfsd->nd_nam;
2888 struct ucred *cred = &nfsd->nd_cr;
2893 struct mbuf *mp1, *mp2;
2894 char *cpos, *cend, *rbuf;
2895 struct vnode *vp = NULL;
2896 struct mount *mp = NULL;
2902 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2903 int siz, cnt, fullsiz, eofflag, rdonly, ncookies;
2904 u_quad_t off, toff, verf;
2905 off_t *cookies = NULL, *cookiep;
2906 struct nfsm_info info;
2908 info.mrep = nfsd->nd_mrep;
2910 info.md = nfsd->nd_md;
2911 info.dpos = nfsd->nd_dpos;
2912 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2914 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2915 fhp = &nfh.fh_generic;
2916 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2918 NULLOUT(tl = nfsm_dissect(&info, 5 * NFSX_UNSIGNED));
2919 toff = fxdr_hyper(tl);
2921 verf = fxdr_hyper(tl);
2924 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2925 toff = fxdr_unsigned(u_quad_t, *tl++);
2926 verf = 0; /* shut up gcc */
2929 cnt = fxdr_unsigned(int, *tl);
2930 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2931 xfer = NFS_SRVMAXDATA(nfsd);
2932 if ((unsigned)cnt > xfer)
2934 if ((unsigned)siz > xfer)
2937 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
2938 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2939 if (!error && vp->v_type != VDIR) {
2945 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
2946 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2952 * Obtain lock on vnode for this section of the code
2956 error = getret = VOP_GETATTR(vp, &at);
2959 * XXX This check may be too strict for Solaris 2.5 clients.
2961 if (!error && toff && verf && verf != at.va_filerev)
2962 error = NFSERR_BAD_COOKIE;
2966 error = nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 0);
2970 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2971 NFSX_POSTOPATTR(info.v3), &error));
2972 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2979 * end section. Allocate rbuf and continue
2981 rbuf = kmalloc(siz, M_TEMP, M_WAITOK);
2984 iv.iov_len = fullsiz;
2987 io.uio_offset = (off_t)off;
2988 io.uio_resid = fullsiz;
2989 io.uio_segflg = UIO_SYSSPACE;
2990 io.uio_rw = UIO_READ;
2994 kfree((caddr_t)cookies, M_TEMP);
2997 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
2998 off = (off_t)io.uio_offset;
2999 if (!cookies && !error)
3000 error = NFSERR_PERM;
3002 getret = VOP_GETATTR(vp, &at);
3009 kfree((caddr_t)rbuf, M_TEMP);
3011 kfree((caddr_t)cookies, M_TEMP);
3012 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3013 NFSX_POSTOPATTR(info.v3), &error));
3014 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3019 siz -= io.uio_resid;
3022 * If nothing read, return eof
3028 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3029 NFSX_POSTOPATTR(info.v3) +
3030 NFSX_COOKIEVERF(info.v3) +
3034 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3035 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
3036 txdr_hyper(at.va_filerev, tl);
3039 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
3042 kfree((caddr_t)rbuf, M_TEMP);
3043 kfree((caddr_t)cookies, M_TEMP);
3050 * Check for degenerate cases of nothing useful read.
3051 * If so go try again
3055 dp = (struct dirent *)cpos;
3058 * For some reason FreeBSD's ufs_readdir() chooses to back the
3059 * directory offset up to a block boundary, so it is necessary to
3060 * skip over the records that preceed the requested offset. This
3061 * requires the assumption that file offset cookies monotonically
3064 while (cpos < cend && ncookies > 0 &&
3065 (dp->d_ino == 0 || dp->d_type == DT_WHT ||
3066 ((u_quad_t)(*cookiep)) <= toff)) {
3067 dp = _DIRENT_NEXT(dp);
3072 if (cpos >= cend || ncookies == 0) {
3078 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
3079 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3080 NFSX_POSTOPATTR(info.v3) +
3081 NFSX_COOKIEVERF(info.v3) + siz,
3084 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3085 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
3086 txdr_hyper(at.va_filerev, tl);
3088 mp1 = mp2 = info.mb;
3090 be = bp + M_TRAILINGSPACE(mp1);
3092 /* Loop through the records and build reply */
3093 while (cpos < cend && ncookies > 0) {
3094 if (dp->d_ino != 0 && dp->d_type != DT_WHT) {
3095 nlen = dp->d_namlen;
3096 rem = nfsm_rndup(nlen) - nlen;
3097 len += (4 * NFSX_UNSIGNED + nlen + rem);
3099 len += 2 * NFSX_UNSIGNED;
3105 * Build the directory record xdr from
3108 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3110 bp += NFSX_UNSIGNED;
3112 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3113 *tl = txdr_unsigned(dp->d_ino >> 32);
3114 bp += NFSX_UNSIGNED;
3116 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3117 *tl = txdr_unsigned(dp->d_ino);
3118 bp += NFSX_UNSIGNED;
3119 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3120 *tl = txdr_unsigned(nlen);
3121 bp += NFSX_UNSIGNED;
3123 /* And loop around copying the name */
3127 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3132 bcopy(cp, bp, tsiz);
3138 /* And null pad to a int32_t boundary */
3139 for (i = 0; i < rem; i++)
3141 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3143 /* Finish off the record */
3145 *tl = txdr_unsigned(*cookiep >> 32);
3146 bp += NFSX_UNSIGNED;
3147 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3149 *tl = txdr_unsigned(*cookiep);
3150 bp += NFSX_UNSIGNED;
3152 dp = _DIRENT_NEXT(dp);
3159 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3161 bp += NFSX_UNSIGNED;
3162 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3167 bp += NFSX_UNSIGNED;
3168 if (mp1 != info.mb) {
3170 mp1->m_len = bp - mtod(mp1, caddr_t);
3172 mp1->m_len += bp - info.bpos;
3173 kfree((caddr_t)rbuf, M_TEMP);
3174 kfree((caddr_t)cookies, M_TEMP);
3184 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3185 struct thread *td, struct mbuf **mrq)
3187 struct sockaddr *nam = nfsd->nd_nam;
3188 struct ucred *cred = &nfsd->nd_cr;
3193 struct mbuf *mp1, *mp2;
3194 char *cpos, *cend, *rbuf;
3195 struct vnode *vp = NULL, *nvp;
3196 struct mount *mp = NULL;
3199 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3202 struct vattr va, at, *vap = &va;
3203 struct nfs_fattr *fp;
3204 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3205 int siz, cnt, fullsiz, eofflag, rdonly, dirlen, ncookies;
3206 u_quad_t off, toff, verf;
3207 off_t *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3208 struct nfsm_info info;
3210 info.mrep = nfsd->nd_mrep;
3212 info.md = nfsd->nd_md;
3213 info.dpos = nfsd->nd_dpos;
3214 info.v3 = (nfsd->nd_flag & ND_NFSV3);
3216 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3217 fhp = &nfh.fh_generic;
3218 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3219 NULLOUT(tl = nfsm_dissect(&info, 6 * NFSX_UNSIGNED));
3220 toff = fxdr_hyper(tl);
3222 verf = fxdr_hyper(tl);
3224 siz = fxdr_unsigned(int, *tl++);
3225 cnt = fxdr_unsigned(int, *tl);
3227 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3228 xfer = NFS_SRVMAXDATA(nfsd);
3229 if ((unsigned)cnt > xfer)
3231 if ((unsigned)siz > xfer)
3234 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3235 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3236 if (!error && vp->v_type != VDIR) {
3242 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3243 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3247 error = getret = VOP_GETATTR(vp, &at);
3250 * XXX This check may be too strict for Solaris 2.5 clients.
3252 if (!error && toff && verf && verf != at.va_filerev)
3253 error = NFSERR_BAD_COOKIE;
3256 error = nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 0);
3261 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3262 NFSX_V3POSTOPATTR, &error));
3263 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3268 rbuf = kmalloc(siz, M_TEMP, M_WAITOK);
3271 iv.iov_len = fullsiz;
3274 io.uio_offset = (off_t)off;
3275 io.uio_resid = fullsiz;
3276 io.uio_segflg = UIO_SYSSPACE;
3277 io.uio_rw = UIO_READ;
3281 kfree((caddr_t)cookies, M_TEMP);
3284 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3285 off = (u_quad_t)io.uio_offset;
3286 getret = VOP_GETATTR(vp, &at);
3287 if (!cookies && !error)
3288 error = NFSERR_PERM;
3295 kfree((caddr_t)cookies, M_TEMP);
3296 kfree((caddr_t)rbuf, M_TEMP);
3297 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3298 NFSX_V3POSTOPATTR, &error));
3299 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3304 siz -= io.uio_resid;
3307 * If nothing read, return eof
3313 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3318 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3319 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
3320 txdr_hyper(at.va_filerev, tl);
3324 kfree((caddr_t)cookies, M_TEMP);
3325 kfree((caddr_t)rbuf, M_TEMP);
3332 * Check for degenerate cases of nothing useful read.
3333 * If so go try again
3337 dp = (struct dirent *)cpos;
3340 * For some reason FreeBSD's ufs_readdir() chooses to back the
3341 * directory offset up to a block boundary, so it is necessary to
3342 * skip over the records that preceed the requested offset. This
3343 * requires the assumption that file offset cookies monotonically
3346 while (cpos < cend && ncookies > 0 &&
3347 (dp->d_ino == 0 || dp->d_type == DT_WHT ||
3348 ((u_quad_t)(*cookiep)) <= toff)) {
3349 dp = _DIRENT_NEXT(dp);
3354 if (cpos >= cend || ncookies == 0) {
3361 * Probe one of the directory entries to see if the filesystem
3364 if (VFS_VGET(vp->v_mount, vp, dp->d_ino, &nvp) == EOPNOTSUPP) {
3365 error = NFSERR_NOTSUPP;
3368 kfree((caddr_t)cookies, M_TEMP);
3369 kfree((caddr_t)rbuf, M_TEMP);
3370 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3371 NFSX_V3POSTOPATTR, &error));
3372 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3381 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3383 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, cnt, &error));
3384 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3385 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
3386 txdr_hyper(at.va_filerev, tl);
3387 mp1 = mp2 = info.mb;
3389 be = bp + M_TRAILINGSPACE(mp1);
3391 /* Loop through the records and build reply */
3392 while (cpos < cend && ncookies > 0) {
3393 if (dp->d_ino != 0 && dp->d_type != DT_WHT) {
3394 nlen = dp->d_namlen;
3395 rem = nfsm_rndup(nlen) - nlen;
3398 * For readdir_and_lookup get the vnode using
3401 if (VFS_VGET(vp->v_mount, vp, dp->d_ino, &nvp))
3403 bzero((caddr_t)nfhp, NFSX_V3FH);
3404 nfhp->fh_fsid = fhp->fh_fsid;
3405 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3410 if (VOP_GETATTR(nvp, vap)) {
3419 * If either the dircount or maxcount will be
3420 * exceeded, get out now. Both of these lengths
3421 * are calculated conservatively, including all
3424 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3426 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3427 if (len > cnt || dirlen > fullsiz) {
3433 * Build the directory record xdr from
3436 fp = (struct nfs_fattr *)&fl.fl_fattr;
3437 nfsm_srvfattr(nfsd, vap, fp);
3438 fl.fl_off.nfsuquad[0] = txdr_unsigned(*cookiep >> 32);
3439 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3440 fl.fl_postopok = nfs_true;
3441 fl.fl_fhok = nfs_true;
3442 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3444 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3446 bp += NFSX_UNSIGNED;
3447 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3448 *tl = txdr_unsigned(dp->d_ino >> 32);
3449 bp += NFSX_UNSIGNED;
3450 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3451 *tl = txdr_unsigned(dp->d_ino);
3452 bp += NFSX_UNSIGNED;
3453 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3454 *tl = txdr_unsigned(nlen);
3455 bp += NFSX_UNSIGNED;
3457 /* And loop around copying the name */
3461 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3462 if ((bp + xfer) > be)
3466 bcopy(cp, bp, tsiz);
3471 /* And null pad to a int32_t boundary */
3472 for (i = 0; i < rem; i++)
3476 * Now copy the flrep structure out.
3478 xfer = sizeof (struct flrep);
3481 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3482 if ((bp + xfer) > be)
3486 bcopy(cp, bp, tsiz);
3493 dp = _DIRENT_NEXT(dp);
3500 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3502 bp += NFSX_UNSIGNED;
3503 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3508 bp += NFSX_UNSIGNED;
3509 if (mp1 != info.mb) {
3511 mp1->m_len = bp - mtod(mp1, caddr_t);
3513 mp1->m_len += bp - info.bpos;
3514 kfree((caddr_t)cookies, M_TEMP);
3515 kfree((caddr_t)rbuf, M_TEMP);
3524 * nfs commit service
3527 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3528 struct thread *td, struct mbuf **mrq)
3530 struct sockaddr *nam = nfsd->nd_nam;
3531 struct ucred *cred = &nfsd->nd_cr;
3532 struct vattr bfor, aft;
3533 struct vnode *vp = NULL;
3534 struct mount *mp = NULL;
3538 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt;
3540 struct nfsm_info info;
3542 info.mrep = nfsd->nd_mrep;
3544 info.md = nfsd->nd_md;
3545 info.dpos = nfsd->nd_dpos;
3547 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3548 fhp = &nfh.fh_generic;
3549 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3550 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
3553 * XXX At this time VOP_FSYNC() does not accept offset and byte
3554 * count parameters, so these arguments are useless (someday maybe).
3556 off = fxdr_hyper(tl);
3558 cnt = fxdr_unsigned(int, *tl);
3559 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3560 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3562 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3563 2 * NFSX_UNSIGNED, &error));
3564 nfsm_srvwcc_data(&info, nfsd, for_ret, &bfor,
3569 for_ret = VOP_GETATTR(vp, &bfor);
3572 * RFC 1813 3.3.21: If count is 0, a flush from offset to the end of
3573 * file is done. At this time VOP_FSYNC does not accept offset and
3574 * byte count parameters, so call VOP_FSYNC the whole file for now.
3576 if (cnt == 0 || cnt > MAX_COMMIT_COUNT) {
3578 * Give up and do the whole thing
3581 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3582 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3584 error = VOP_FSYNC(vp, MNT_WAIT, 0);
3587 * Locate and synchronously write any buffers that fall
3588 * into the requested range. Note: we are assuming that
3589 * f_iosize is a power of 2.
3591 int iosize = vp->v_mount->mnt_stat.f_iosize;
3592 int iomask = iosize - 1;
3596 * Align to iosize boundry, super-align to page boundry.
3599 cnt += off & iomask;
3600 off &= ~(u_quad_t)iomask;
3602 if (off & PAGE_MASK) {
3603 cnt += off & PAGE_MASK;
3604 off &= ~(u_quad_t)PAGE_MASK;
3609 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3610 vm_object_page_clean(vp->v_object, off / PAGE_SIZE,
3611 (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3615 while (error == 0 || cnt > 0) {
3619 * If we have a buffer and it is marked B_DELWRI we
3620 * have to lock and write it. Otherwise the prior
3621 * write is assumed to have already been committed.
3623 * WARNING: FINDBLK_TEST buffers represent stable
3624 * storage but not necessarily stable
3625 * content. It is ok in this case.
3627 if ((bp = findblk(vp, loffset, FINDBLK_TEST)) != NULL) {
3628 if (bp->b_flags & B_DELWRI)
3629 bp = findblk(vp, loffset, 0);
3634 if (bp->b_flags & B_DELWRI) {
3651 aft_ret = VOP_GETATTR(vp, &aft);
3654 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3655 NFSX_V3WCCDATA + NFSX_V3WRITEVERF,
3657 nfsm_srvwcc_data(&info, nfsd, for_ret, &bfor,
3660 tl = nfsm_build(&info, NFSX_V3WRITEVERF);
3661 if (nfsver.tv_sec == 0)
3663 *tl++ = txdr_unsigned(nfsver.tv_sec);
3664 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
3676 * nfs statfs service
3679 nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3680 struct thread *td, struct mbuf **mrq)
3682 struct sockaddr *nam = nfsd->nd_nam;
3683 struct ucred *cred = &nfsd->nd_cr;
3685 struct nfs_statfs *sfp;
3686 int error = 0, rdonly, getret = 1;
3687 struct vnode *vp = NULL;
3688 struct mount *mp = NULL;
3692 struct statfs statfs;
3694 struct nfsm_info info;
3696 info.mrep = nfsd->nd_mrep;
3698 info.md = nfsd->nd_md;
3699 info.dpos = nfsd->nd_dpos;
3700 info.v3 = (nfsd->nd_flag & ND_NFSV3);
3702 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3703 fhp = &nfh.fh_generic;
3704 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3705 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3706 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3708 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3709 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3714 error = VFS_STATFS(vp->v_mount, sf, proc0.p_ucred);
3715 getret = VOP_GETATTR(vp, &at);
3718 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3719 NFSX_POSTOPATTR(info.v3) + NFSX_STATFS(info.v3),
3722 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3727 sfp = nfsm_build(&info, NFSX_STATFS(info.v3));
3729 tval = (u_quad_t)sf->f_blocks;
3730 tval *= (u_quad_t)sf->f_bsize;
3731 txdr_hyper(tval, &sfp->sf_tbytes);
3732 tval = (u_quad_t)sf->f_bfree;
3733 tval *= (u_quad_t)sf->f_bsize;
3734 txdr_hyper(tval, &sfp->sf_fbytes);
3735 tval = (u_quad_t)sf->f_bavail;
3736 tval *= (u_quad_t)sf->f_bsize;
3737 txdr_hyper(tval, &sfp->sf_abytes);
3738 sfp->sf_tfiles.nfsuquad[0] = 0;
3739 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3740 sfp->sf_ffiles.nfsuquad[0] = 0;
3741 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3742 sfp->sf_afiles.nfsuquad[0] = 0;
3743 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3744 sfp->sf_invarsec = 0;
3746 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3747 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3748 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3749 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3750 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3760 * nfs fsinfo service
3763 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3764 struct thread *td, struct mbuf **mrq)
3766 struct sockaddr *nam = nfsd->nd_nam;
3767 struct ucred *cred = &nfsd->nd_cr;
3768 struct nfsv3_fsinfo *sip;
3769 int error = 0, rdonly, getret = 1, pref;
3770 struct vnode *vp = NULL;
3771 struct mount *mp = NULL;
3777 struct nfsm_info info;
3779 info.mrep = nfsd->nd_mrep;
3781 info.md = nfsd->nd_md;
3782 info.dpos = nfsd->nd_dpos;
3784 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3785 fhp = &nfh.fh_generic;
3786 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3787 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3788 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3790 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3791 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3796 /* XXX Try to make a guess on the max file size. */
3797 VFS_STATFS(vp->v_mount, &sb, proc0.p_ucred);
3798 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3800 getret = VOP_GETATTR(vp, &at);
3803 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3804 NFSX_V3POSTOPATTR + NFSX_V3FSINFO, &error));
3805 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3806 sip = nfsm_build(&info, NFSX_V3FSINFO);
3810 * There should be file system VFS OP(s) to get this information.
3811 * For now, assume ufs.
3813 if (slp->ns_so->so_type == SOCK_DGRAM)
3814 pref = NFS_MAXDGRAMDATA;
3817 sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3818 sip->fs_rtpref = txdr_unsigned(pref);
3819 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3820 sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3821 sip->fs_wtpref = txdr_unsigned(pref);
3822 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3823 sip->fs_dtpref = txdr_unsigned(pref);
3824 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3825 sip->fs_timedelta.nfsv3_sec = 0;
3826 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3827 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3828 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3829 NFSV3FSINFO_CANSETTIME);
3838 * nfs pathconf service
3841 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3842 struct thread *td, struct mbuf **mrq)
3844 struct sockaddr *nam = nfsd->nd_nam;
3845 struct ucred *cred = &nfsd->nd_cr;
3846 struct nfsv3_pathconf *pc;
3847 int error = 0, rdonly, getret = 1;
3848 register_t linkmax, namemax, chownres, notrunc;
3849 struct vnode *vp = NULL;
3850 struct mount *mp = NULL;
3854 struct nfsm_info info;
3856 info.mrep = nfsd->nd_mrep;
3858 info.md = nfsd->nd_md;
3859 info.dpos = nfsd->nd_dpos;
3861 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3862 fhp = &nfh.fh_generic;
3863 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3864 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3865 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3867 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3868 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3872 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3874 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3876 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3878 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3879 getret = VOP_GETATTR(vp, &at);
3882 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3883 NFSX_V3POSTOPATTR + NFSX_V3PATHCONF,
3885 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3890 pc = nfsm_build(&info, NFSX_V3PATHCONF);
3892 pc->pc_linkmax = txdr_unsigned(linkmax);
3893 pc->pc_namemax = txdr_unsigned(namemax);
3894 pc->pc_notrunc = txdr_unsigned(notrunc);
3895 pc->pc_chownrestricted = txdr_unsigned(chownres);
3898 * These should probably be supported by VOP_PATHCONF(), but
3899 * until msdosfs is exportable (why would you want to?), the
3900 * Unix defaults should be ok.
3902 pc->pc_caseinsensitive = nfs_false;
3903 pc->pc_casepreserving = nfs_true;
3912 * Null operation, used by clients to ping server
3916 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3917 struct thread *td, struct mbuf **mrq)
3919 struct nfsm_info info;
3920 int error = NFSERR_RETVOID;
3922 info.mrep = nfsd->nd_mrep;
3925 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3926 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
3933 * No operation, used for obsolete procedures
3937 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3938 struct thread *td, struct mbuf **mrq)
3940 struct nfsm_info info;
3943 info.mrep = nfsd->nd_mrep;
3946 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3947 if (nfsd->nd_repstat)
3948 error = nfsd->nd_repstat;
3950 error = EPROCUNAVAIL;
3951 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
3959 * Perform access checking for vnodes obtained from file handles that would
3960 * refer to files already opened by a Unix client. You cannot just use
3961 * vn_writechk() and VOP_ACCESS() for two reasons.
3962 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
3963 * 2 - The owner is to be given access irrespective of mode bits for some
3964 * operations, so that processes that chmod after opening a file don't
3965 * break. I don't like this because it opens a security hole, but since
3966 * the nfs server opens a security hole the size of a barn door anyhow,
3969 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
3970 * will return EPERM instead of EACCESS. EPERM is always an error.
3973 nfsrv_access(struct mount *mp, struct vnode *vp, int flags, struct ucred *cred,
3974 int rdonly, struct thread *td, int override)
3979 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3980 if (flags & VWRITE) {
3981 /* Just vn_writechk() changed to check rdonly */
3983 * Disallow write attempts on read-only file systems;
3984 * unless the file is a socket or a block or character
3985 * device resident on the file system.
3988 ((mp->mnt_flag | vp->v_mount->mnt_flag) & MNT_RDONLY)) {
3989 switch (vp->v_type) {
3999 * If there's shared text associated with
4000 * the inode, we can't allow writing.
4002 if (vp->v_flag & VTEXT)
4005 error = VOP_GETATTR(vp, &vattr);
4008 error = VOP_ACCESS(vp, flags, cred); /* XXX ruid/rgid vs uid/gid */
4010 * Allow certain operations for the owner (reads and writes
4011 * on files that are already open).
4013 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)
4017 #endif /* NFS_NOSERVER */