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 $
38 * $DragonFly: src/sys/vfs/nfs/nfs_serv.c,v 1.48 2008/09/17 21:44:24 dillon Exp $
42 * nfs version 2 and 3 server calls to vnode ops
43 * - these routines generally have 3 phases
44 * 1 - break down and validate rpc request in mbuf list
45 * 2 - do the vnode ops for the request
46 * (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c)
47 * 3 - build the rpc reply in an mbuf list
49 * - do not mix the phases, since the nfsm_?? macros can return failures
50 * on a bad rpc or similar and do not do any vrele() or vput()'s
52 * - the nfsm_reply() macro generates an nfs rpc reply with the nfs
53 * error number iff error != 0 whereas
54 * returning an error from the server function implies a fatal error
55 * such as a badly constructed rpc request that should be dropped without
57 * For Version 3, nfsm_reply() does not return for the error case, since
58 * most version 3 rpcs return more than the status for error cases.
61 * Warning: always pay careful attention to resource cleanup on return
62 * and note that nfsm_*() macros can terminate a procedure on certain
66 #include <sys/param.h>
67 #include <sys/systm.h>
70 #include <sys/nlookup.h>
71 #include <sys/namei.h>
72 #include <sys/unistd.h>
73 #include <sys/vnode.h>
74 #include <sys/mount.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/malloc.h>
79 #include <sys/dirent.h>
81 #include <sys/kernel.h>
82 #include <sys/sysctl.h>
86 #include <vm/vm_extern.h>
87 #include <vm/vm_zone.h>
88 #include <vm/vm_object.h>
92 #include <sys/thread2.h>
98 #include "nfsm_subs.h"
101 #define nfsdbprintf(info) kprintf info
103 #define nfsdbprintf(info)
106 #define MAX_COMMIT_COUNT (1024 * 1024)
108 #define NUM_HEURISTIC 1017
109 #define NHUSE_INIT 64
111 #define NHUSE_MAX 2048
113 static struct nfsheur {
114 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
115 off_t nh_nextr; /* next offset for sequential detection */
116 int nh_use; /* use count for selection */
117 int nh_seqcount; /* heuristic */
118 } nfsheur[NUM_HEURISTIC];
120 nfstype nfsv3_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK,
123 nfstype nfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
126 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
127 int nfsrvw_procrastinate_v3 = 0;
129 static struct timespec nfsver;
131 SYSCTL_DECL(_vfs_nfs);
134 SYSCTL_INT(_vfs_nfs, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0,
135 "Enable unstable and fast writes");
136 static int nfs_commit_blks;
137 static int nfs_commit_miss;
138 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 0,
139 "Number of committed blocks");
140 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 0,
141 "Number of nfs blocks committed from dirty buffers");
143 static int nfsrv_access (struct mount *, struct vnode *, int,
144 struct ucred *, int, struct thread *, int);
145 static void nfsrvw_coalesce (struct nfsrv_descript *,
146 struct nfsrv_descript *);
149 * nfs v3 access service
152 nfsrv3_access(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
153 struct thread *td, struct mbuf **mrq)
155 struct sockaddr *nam = nfsd->nd_nam;
156 struct ucred *cred = &nfsd->nd_cr;
157 struct vnode *vp = NULL;
158 struct mount *mp = NULL;
161 int error = 0, rdonly, getret;
162 struct vattr vattr, *vap = &vattr;
163 u_long testmode, nfsmode;
164 struct nfsm_info info;
167 info.dpos = nfsd->nd_dpos;
168 info.md = nfsd->nd_md;
169 info.mrep = nfsd->nd_mrep;
172 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
173 fhp = &nfh.fh_generic;
174 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
175 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
176 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam, &rdonly,
177 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
179 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
180 nfsm_srvpostop_attr(&info, nfsd, 1, NULL);
184 nfsmode = fxdr_unsigned(u_int32_t, *tl);
185 if ((nfsmode & NFSV3ACCESS_READ) &&
186 nfsrv_access(mp, vp, VREAD, cred, rdonly, td, 0))
187 nfsmode &= ~NFSV3ACCESS_READ;
188 if (vp->v_type == VDIR)
189 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
192 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
193 if ((nfsmode & testmode) &&
194 nfsrv_access(mp, vp, VWRITE, cred, rdonly, td, 0))
195 nfsmode &= ~testmode;
196 if (vp->v_type == VDIR)
197 testmode = NFSV3ACCESS_LOOKUP;
199 testmode = NFSV3ACCESS_EXECUTE;
200 if ((nfsmode & testmode) &&
201 nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 0))
202 nfsmode &= ~testmode;
203 getret = VOP_GETATTR(vp, vap);
206 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
207 NFSX_POSTOPATTR(1) + NFSX_UNSIGNED, &error));
208 nfsm_srvpostop_attr(&info, nfsd, getret, vap);
209 tl = nfsm_build(&info, NFSX_UNSIGNED);
210 *tl = txdr_unsigned(nfsmode);
219 * nfs getattr service
222 nfsrv_getattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
223 struct thread *td, struct mbuf **mrq)
225 struct sockaddr *nam = nfsd->nd_nam;
226 struct ucred *cred = &nfsd->nd_cr;
227 struct nfs_fattr *fp;
229 struct vattr *vap = &va;
230 struct vnode *vp = NULL;
231 struct mount *mp = NULL;
234 int error = 0, rdonly;
235 struct nfsm_info info;
237 info.mrep = nfsd->nd_mrep;
238 info.md = nfsd->nd_md;
239 info.dpos = nfsd->nd_dpos;
242 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
243 fhp = &nfh.fh_generic;
244 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
245 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
246 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
248 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
252 error = VOP_GETATTR(vp, vap);
255 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
256 NFSX_FATTR(nfsd->nd_flag & ND_NFSV3), &error));
261 fp = nfsm_build(&info, NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
262 nfsm_srvfattr(nfsd, vap, fp);
273 * nfs setattr service
276 nfsrv_setattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
277 struct thread *td, struct mbuf **mrq)
279 struct sockaddr *nam = nfsd->nd_nam;
280 struct ucred *cred = &nfsd->nd_cr;
281 struct vattr va, preat;
282 struct vattr *vap = &va;
283 struct nfsv2_sattr *sp;
284 struct nfs_fattr *fp;
285 struct vnode *vp = NULL;
286 struct mount *mp = NULL;
290 int error = 0, rdonly, preat_ret = 1, postat_ret = 1;
292 struct timespec guard;
293 struct nfsm_info info;
295 info.mrep = nfsd->nd_mrep;
297 info.md = nfsd->nd_md;
298 info.dpos = nfsd->nd_dpos;
299 info.v3 = (nfsd->nd_flag & ND_NFSV3);
301 guard.tv_sec = 0; /* fix compiler warning */
304 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
305 fhp = &nfh.fh_generic;
306 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
309 ERROROUT(nfsm_srvsattr(&info, vap));
310 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
311 gcheck = fxdr_unsigned(int, *tl);
313 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
314 fxdr_nfsv3time(tl, &guard);
317 NULLOUT(sp = nfsm_dissect(&info, NFSX_V2SATTR));
319 * Nah nah nah nah na nah
320 * There is a bug in the Sun client that puts 0xffff in the mode
321 * field of sattr when it should put in 0xffffffff. The u_short
322 * doesn't sign extend.
323 * --> check the low order 2 bytes for 0xffff
325 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
326 vap->va_mode = nfstov_mode(sp->sa_mode);
327 if (sp->sa_uid != nfs_xdrneg1)
328 vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
329 if (sp->sa_gid != nfs_xdrneg1)
330 vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
331 if (sp->sa_size != nfs_xdrneg1)
332 vap->va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
333 if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
335 fxdr_nfsv2time(&sp->sa_atime, &vap->va_atime);
337 vap->va_atime.tv_sec =
338 fxdr_unsigned(int32_t, sp->sa_atime.nfsv2_sec);
339 vap->va_atime.tv_nsec = 0;
342 if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
343 fxdr_nfsv2time(&sp->sa_mtime, &vap->va_mtime);
348 * Now that we have all the fields, lets do it.
350 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam, &rdonly,
351 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
353 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
354 2 * NFSX_UNSIGNED, &error));
355 nfsm_srvwcc_data(&info, nfsd, preat_ret, &preat,
362 * vp now an active resource, pay careful attention to cleanup
366 error = preat_ret = VOP_GETATTR(vp, &preat);
367 if (!error && gcheck &&
368 (preat.va_ctime.tv_sec != guard.tv_sec ||
369 preat.va_ctime.tv_nsec != guard.tv_nsec))
370 error = NFSERR_NOT_SYNC;
374 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
375 NFSX_WCCDATA(info.v3), &error));
376 nfsm_srvwcc_data(&info, nfsd, preat_ret, &preat,
384 * If the size is being changed write acces is required, otherwise
385 * just check for a read only file system.
387 if (vap->va_size == ((u_quad_t)((quad_t) -1))) {
388 if (rdonly || (mp->mnt_flag & MNT_RDONLY)) {
393 if (vp->v_type == VDIR) {
396 } else if ((error = nfsrv_access(mp, vp, VWRITE, cred, rdonly,
401 error = VOP_SETATTR(vp, vap, cred);
402 postat_ret = VOP_GETATTR(vp, vap);
408 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
409 NFSX_WCCORFATTR(info.v3), &error));
411 nfsm_srvwcc_data(&info, nfsd, preat_ret, &preat,
416 fp = nfsm_build(&info, NFSX_V2FATTR);
417 nfsm_srvfattr(nfsd, vap, fp);
432 nfsrv_lookup(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
433 struct thread *td, struct mbuf **mrq)
435 struct sockaddr *nam = nfsd->nd_nam;
436 struct ucred *cred = &nfsd->nd_cr;
437 struct nfs_fattr *fp;
438 struct nlookupdata nd;
444 int error = 0, len, dirattr_ret = 1;
446 struct vattr va, dirattr, *vap = &va;
447 struct nfsm_info info;
449 info.mrep = nfsd->nd_mrep;
451 info.md = nfsd->nd_md;
452 info.dpos = nfsd->nd_dpos;
453 info.v3 = (nfsd->nd_flag & ND_NFSV3);
455 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
460 fhp = &nfh.fh_generic;
461 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
462 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
464 pubflag = nfs_ispublicfh(fhp);
466 error = nfs_namei(&nd, cred, 0, NULL, &vp,
467 fhp, len, slp, nam, &info.md, &info.dpos,
468 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), pubflag);
471 * namei failure, only dirp to cleanup. Clear out garbarge from
472 * structure in case macros jump to nfsmout.
478 dirattr_ret = VOP_GETATTR(dirp, &dirattr);
482 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
483 NFSX_POSTOPATTR(info.v3), &error));
484 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
490 * Locate index file for public filehandle
492 * error is 0 on entry and 0 on exit from this block.
496 if (vp->v_type == VDIR && nfs_pub.np_index != NULL) {
498 * Setup call to lookup() to see if we can find
499 * the index file. Arguably, this doesn't belong
500 * in a kernel.. Ugh. If an error occurs, do not
501 * try to install an index file and then clear the
504 * When we replace nd with ind and redirect ndp,
505 * maintenance of ni_startdir and ni_vp shift to
506 * ind and we have to clean them up in the old nd.
507 * However, the cnd resource continues to be maintained
508 * via the original nd. Confused? You aren't alone!
511 cache_copy(&nd.nl_nch, &nch);
513 error = nlookup_init_raw(&nd, nfs_pub.np_index,
514 UIO_SYSSPACE, 0, cred, &nch);
517 error = nlookup(&nd);
521 * Found an index file. Get rid of
522 * the old references. transfer vp and
523 * load up the new vp. Fortunately we do
524 * not have to deal with dvp, that would be
531 error = cache_vget(&nd.nl_nch, nd.nl_cred,
533 KKASSERT(error == 0);
538 * If the public filehandle was used, check that this lookup
539 * didn't result in a filehandle outside the publicly exported
540 * filesystem. We clear the poor vp here to avoid lockups due
544 if (vp->v_mount != nfs_pub.np_mount) {
553 dirattr_ret = VOP_GETATTR(dirp, &dirattr);
559 * Resources at this point:
560 * ndp->ni_vp may not be NULL
565 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
566 NFSX_POSTOPATTR(info.v3), &error));
567 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
573 * Clear out some resources prior to potentially blocking. This
574 * is not as critical as ni_dvp resources in other routines, but
580 * Get underlying attribute, then release remaining resources ( for
581 * the same potential blocking reason ) and reply.
583 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
584 error = VFS_VPTOFH(vp, &fhp->fh_fid);
586 error = VOP_GETATTR(vp, vap);
590 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
591 NFSX_SRVFH(info.v3) +
592 NFSX_POSTOPORFATTR(info.v3) +
593 NFSX_POSTOPATTR(info.v3),
596 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
600 nfsm_srvfhtom(&info, fhp);
602 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
603 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
605 fp = nfsm_build(&info, NFSX_V2FATTR);
606 nfsm_srvfattr(nfsd, vap, fp);
613 nlookup_done(&nd); /* may be called twice */
620 * nfs readlink service
623 nfsrv_readlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
624 struct thread *td, struct mbuf **mrq)
626 struct sockaddr *nam = nfsd->nd_nam;
627 struct ucred *cred = &nfsd->nd_cr;
628 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
629 struct iovec *ivp = iv;
631 int error = 0, rdonly, i, tlen, len, getret;
632 struct mbuf *mp1, *mp2, *mp3;
633 struct vnode *vp = NULL;
634 struct mount *mp = NULL;
638 struct uio io, *uiop = &io;
639 struct nfsm_info info;
641 info.mrep = nfsd->nd_mrep;
643 info.md = nfsd->nd_md;
644 info.dpos = nfsd->nd_dpos;
645 info.v3 = (nfsd->nd_flag & ND_NFSV3);
647 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
652 fhp = &nfh.fh_generic;
653 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
656 while (len < NFS_MAXPATHLEN) {
657 mp1 = m_getcl(MB_WAIT, MT_DATA, 0);
658 mp1->m_len = MCLBYTES;
665 if ((len + mp1->m_len) > NFS_MAXPATHLEN) {
666 mp1->m_len = NFS_MAXPATHLEN-len;
667 len = NFS_MAXPATHLEN;
670 ivp->iov_base = mtod(mp1, caddr_t);
671 ivp->iov_len = mp1->m_len;
676 uiop->uio_iovcnt = i;
677 uiop->uio_offset = 0;
678 uiop->uio_resid = len;
679 uiop->uio_rw = UIO_READ;
680 uiop->uio_segflg = UIO_SYSSPACE;
682 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
683 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
685 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
686 2 * NFSX_UNSIGNED, &error));
687 nfsm_srvpostop_attr(&info, nfsd, 1, NULL);
691 if (vp->v_type != VLNK) {
698 error = VOP_READLINK(vp, uiop, cred);
700 getret = VOP_GETATTR(vp, &attr);
703 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
704 NFSX_POSTOPATTR(info.v3) + NFSX_UNSIGNED,
707 nfsm_srvpostop_attr(&info, nfsd, getret, &attr);
713 if (uiop->uio_resid > 0) {
714 len -= uiop->uio_resid;
715 tlen = nfsm_rndup(len);
716 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
718 tl = nfsm_build(&info, NFSX_UNSIGNED);
719 *tl = txdr_unsigned(len);
720 info.mb->m_next = mp3;
735 nfsrv_read(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
736 struct thread *td, struct mbuf **mrq)
738 struct nfsm_info info;
739 struct sockaddr *nam = nfsd->nd_nam;
740 struct ucred *cred = &nfsd->nd_cr;
744 struct nfs_fattr *fp;
748 int error = 0, rdonly, cnt, len, left, siz, tlen, getret;
750 struct vnode *vp = NULL;
751 struct mount *mp = NULL;
754 struct uio io, *uiop = &io;
755 struct vattr va, *vap = &va;
760 info.mrep = nfsd->nd_mrep;
762 info.md = nfsd->nd_md;
763 info.dpos = nfsd->nd_dpos;
764 info.v3 = (nfsd->nd_flag & ND_NFSV3);
766 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
767 fhp = &nfh.fh_generic;
768 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
770 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
771 off = fxdr_hyper(tl);
773 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
774 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
776 NEGREPLYOUT(reqlen = nfsm_srvstrsiz(&info,
777 NFS_SRVMAXDATA(nfsd), &error));
780 * Reference vp. If an error occurs, vp will be invalid, but we
781 * have to NULL it just in case. The macros might goto nfsmout
785 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
786 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
789 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
790 2 * NFSX_UNSIGNED, &error));
791 nfsm_srvpostop_attr(&info, nfsd, 1, NULL);
796 if (vp->v_type != VREG) {
800 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
803 if ((error = nfsrv_access(mp, vp, VREAD, cred, rdonly, td, 1)) != 0)
804 error = nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 1);
806 getret = VOP_GETATTR(vp, vap);
812 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
813 NFSX_POSTOPATTR(info.v3), &error));
814 nfsm_srvpostop_attr(&info, nfsd, getret, vap);
820 * Calculate byte count to read
823 if (off >= vap->va_size)
825 else if ((off + reqlen) > vap->va_size)
826 cnt = vap->va_size - off;
831 * Calculate seqcount for heuristic
839 * Locate best candidate
842 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
846 if (nfsheur[hi].nh_vp == vp) {
850 if (nfsheur[hi].nh_use > 0)
851 --nfsheur[hi].nh_use;
852 hi = (hi + 1) % NUM_HEURISTIC;
853 if (nfsheur[hi].nh_use < nh->nh_use)
857 if (nh->nh_vp != vp) {
860 nh->nh_use = NHUSE_INIT;
868 * Calculate heuristic
871 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
872 if (++nh->nh_seqcount > IO_SEQMAX)
873 nh->nh_seqcount = IO_SEQMAX;
874 } else if (nh->nh_seqcount > 1) {
879 nh->nh_use += NHUSE_INC;
880 if (nh->nh_use > NHUSE_MAX)
881 nh->nh_use = NHUSE_MAX;
882 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
885 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
886 NFSX_POSTOPORFATTR(info.v3) +
887 3 * NFSX_UNSIGNED + nfsm_rndup(cnt),
890 tl = nfsm_build(&info, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
892 fp = (struct nfs_fattr *)tl;
893 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
895 tl = nfsm_build(&info, NFSX_V2FATTR + NFSX_UNSIGNED);
896 fp = (struct nfs_fattr *)tl;
897 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
899 len = left = nfsm_rndup(cnt);
902 * Generate the mbuf list with the uio_iov ref. to it.
907 siz = min(M_TRAILINGSPACE(m), left);
913 m = m_getcl(MB_WAIT, MT_DATA, 0);
919 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
921 uiop->uio_iov = iv2 = iv;
927 panic("nfsrv_read iov");
928 siz = min(M_TRAILINGSPACE(m), left);
930 iv->iov_base = mtod(m, caddr_t) + m->m_len;
939 uiop->uio_iovcnt = i;
940 uiop->uio_offset = off;
941 uiop->uio_resid = len;
942 uiop->uio_rw = UIO_READ;
943 uiop->uio_segflg = UIO_SYSSPACE;
944 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
945 off = uiop->uio_offset;
947 FREE((caddr_t)iv2, M_TEMP);
948 if (error || (getret = VOP_GETATTR(vp, vap))) {
955 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
956 NFSX_POSTOPATTR(info.v3),
958 nfsm_srvpostop_attr(&info, nfsd, getret, vap);
967 nfsm_srvfattr(nfsd, vap, fp);
968 tlen = len - uiop->uio_resid;
969 cnt = cnt < tlen ? cnt : tlen;
970 tlen = nfsm_rndup(cnt);
971 if (len != tlen || tlen != cnt)
972 nfsm_adj(info.mb, len - tlen, tlen - cnt);
974 *tl++ = txdr_unsigned(cnt);
980 *tl = txdr_unsigned(cnt);
992 nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
993 struct thread *td, struct mbuf **mrq)
995 struct sockaddr *nam = nfsd->nd_nam;
996 struct ucred *cred = &nfsd->nd_cr;
1000 struct nfs_fattr *fp;
1002 struct vattr va, forat;
1003 struct vattr *vap = &va;
1005 int error = 0, rdonly, len, forat_ret = 1;
1006 int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
1007 int stable = NFSV3WRITE_FILESYNC;
1008 struct vnode *vp = NULL;
1009 struct mount *mp = NULL;
1012 struct uio io, *uiop = &io;
1013 struct nfsm_info info;
1016 info.mrep = nfsd->nd_mrep;
1018 info.md = nfsd->nd_md;
1019 info.dpos = nfsd->nd_dpos;
1020 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1022 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1023 if (info.mrep == NULL) {
1027 fhp = &nfh.fh_generic;
1028 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
1030 NULLOUT(tl = nfsm_dissect(&info, 5 * NFSX_UNSIGNED));
1031 off = fxdr_hyper(tl);
1033 stable = fxdr_unsigned(int, *tl++);
1035 NULLOUT(tl = nfsm_dissect(&info, 4 * NFSX_UNSIGNED));
1036 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1039 stable = NFSV3WRITE_UNSTABLE;
1041 retlen = len = fxdr_unsigned(int32_t, *tl);
1045 * For NFS Version 2, it is not obvious what a write of zero length
1046 * should do, but I might as well be consistent with Version 3,
1047 * which is to return ok so long as there are no permission problems.
1053 if (mp1 == info.md) {
1055 adjust = info.dpos - mtod(mp1, caddr_t);
1056 mp1->m_len -= adjust;
1057 if (mp1->m_len > 0 && adjust > 0)
1058 mp1->m_data += adjust;
1062 else if (mp1->m_len > 0) {
1065 mp1->m_len -= (i - len);
1074 if (len > NFS_MAXDATA || len < 0 || i < len) {
1076 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1077 2 * NFSX_UNSIGNED, &error));
1078 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1083 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
1084 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1087 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1088 2 * NFSX_UNSIGNED, &error));
1089 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1095 forat_ret = VOP_GETATTR(vp, &forat);
1096 if (vp->v_type != VREG) {
1100 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1103 error = nfsrv_access(mp, vp, VWRITE, cred, rdonly, td, 1);
1108 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1109 NFSX_WCCDATA(info.v3), &error));
1110 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1117 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
1119 uiop->uio_iov = iv = ivp;
1120 uiop->uio_iovcnt = cnt;
1123 if (mp1->m_len > 0) {
1124 ivp->iov_base = mtod(mp1, caddr_t);
1125 ivp->iov_len = mp1->m_len;
1133 * The IO_METASYNC flag indicates that all metadata (and not just
1134 * enough to ensure data integrity) mus be written to stable storage
1136 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1138 if (stable == NFSV3WRITE_UNSTABLE)
1139 ioflags = IO_NODELOCKED;
1140 else if (stable == NFSV3WRITE_DATASYNC)
1141 ioflags = (IO_SYNC | IO_NODELOCKED);
1143 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1144 uiop->uio_resid = len;
1145 uiop->uio_rw = UIO_WRITE;
1146 uiop->uio_segflg = UIO_SYSSPACE;
1147 uiop->uio_td = NULL;
1148 uiop->uio_offset = off;
1149 error = VOP_WRITE(vp, uiop, ioflags, cred);
1150 nfsstats.srvvop_writes++;
1151 FREE((caddr_t)iv, M_TEMP);
1153 aftat_ret = VOP_GETATTR(vp, vap);
1158 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1159 NFSX_PREOPATTR(info.v3) +
1160 NFSX_POSTOPORFATTR(info.v3) +
1161 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(info.v3),
1164 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1170 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1171 *tl++ = txdr_unsigned(retlen);
1173 * If nfs_async is set, then pretend the write was FILESYNC.
1175 if (stable == NFSV3WRITE_UNSTABLE && !nfs_async)
1176 *tl++ = txdr_unsigned(stable);
1178 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
1180 * Actually, there is no need to txdr these fields,
1181 * but it may make the values more human readable,
1182 * for debugging purposes.
1184 if (nfsver.tv_sec == 0)
1186 *tl++ = txdr_unsigned(nfsver.tv_sec);
1187 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1189 fp = nfsm_build(&info, NFSX_V2FATTR);
1190 nfsm_srvfattr(nfsd, vap, fp);
1200 * NFS write service with write gathering support. Called when
1201 * nfsrvw_procrastinate > 0.
1202 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1203 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1207 nfsrv_writegather(struct nfsrv_descript **ndp, struct nfssvc_sock *slp,
1208 struct thread *td, struct mbuf **mrq)
1211 struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1212 struct nfs_fattr *fp;
1215 struct nfsrvw_delayhash *wpp;
1217 struct vattr va, forat;
1219 int error = 0, rdonly, len, forat_ret = 1;
1220 int ioflags, aftat_ret = 1, adjust, zeroing;
1222 struct vnode *vp = NULL;
1223 struct mount *mp = NULL;
1224 struct uio io, *uiop = &io;
1226 struct nfsm_info info;
1230 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1238 info.mrep = nfsd->nd_mrep;
1240 info.md = nfsd->nd_md;
1241 info.dpos = nfsd->nd_dpos;
1242 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1243 cred = &nfsd->nd_cr;
1244 LIST_INIT(&nfsd->nd_coalesce);
1245 nfsd->nd_mreq = NULL;
1246 nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1247 cur_usec = nfs_curusec();
1248 nfsd->nd_time = cur_usec +
1249 (info.v3 ? nfsrvw_procrastinate_v3 : nfsrvw_procrastinate);
1252 * Now, get the write header..
1254 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, &nfsd->nd_fh, &error));
1256 NULLOUT(tl = nfsm_dissect(&info, 5 * NFSX_UNSIGNED));
1257 nfsd->nd_off = fxdr_hyper(tl);
1259 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1261 NULLOUT(tl = nfsm_dissect(&info, 4 * NFSX_UNSIGNED));
1262 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1265 nfsd->nd_stable = NFSV3WRITE_UNSTABLE;
1267 len = fxdr_unsigned(int32_t, *tl);
1269 nfsd->nd_eoff = nfsd->nd_off + len;
1272 * Trim the header out of the mbuf list and trim off any trailing
1273 * junk so that the mbuf list has only the write data.
1279 if (mp1 == info.md) {
1281 adjust = info.dpos - mtod(mp1, caddr_t);
1282 mp1->m_len -= adjust;
1283 if (mp1->m_len > 0 && adjust > 0)
1284 mp1->m_data += adjust;
1291 mp1->m_len -= (i - len);
1297 if (len > NFS_MAXDATA || len < 0 || i < len) {
1302 nfsm_writereply(&info, nfsd, slp, error, 2 * NFSX_UNSIGNED);
1304 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1307 nfsd->nd_mreq = info.mreq;
1308 nfsd->nd_mrep = NULL;
1313 * Add this entry to the hash and time queues.
1316 wp = slp->ns_tq.lh_first;
1317 while (wp && wp->nd_time < nfsd->nd_time) {
1319 wp = wp->nd_tq.le_next;
1321 NFS_DPF(WG, ("Q%03x", nfsd->nd_retxid & 0xfff));
1323 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1325 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1327 if (nfsd->nd_mrep) {
1328 wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
1332 bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1334 wp = wp->nd_hash.le_next;
1336 while (wp && wp->nd_off < nfsd->nd_off &&
1337 !bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1339 wp = wp->nd_hash.le_next;
1342 LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1345 * Search the hash list for overlapping entries and
1348 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1349 wp = nfsd->nd_hash.le_next;
1350 if (NFSW_SAMECRED(owp, nfsd))
1351 nfsrvw_coalesce(owp, nfsd);
1354 LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1360 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1361 * and generate the associated reply mbuf list(s).
1364 cur_usec = nfs_curusec();
1365 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = owp) {
1366 owp = nfsd->nd_tq.le_next;
1367 if (nfsd->nd_time > cur_usec)
1371 NFS_DPF(WG, ("P%03x", nfsd->nd_retxid & 0xfff));
1372 LIST_REMOVE(nfsd, nd_tq);
1373 LIST_REMOVE(nfsd, nd_hash);
1374 info.mrep = nfsd->nd_mrep;
1376 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1377 nfsd->nd_mrep = NULL;
1378 cred = &nfsd->nd_cr;
1379 forat_ret = aftat_ret = 1;
1380 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &mp, &vp, cred, slp,
1381 nfsd->nd_nam, &rdonly,
1382 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1385 forat_ret = VOP_GETATTR(vp, &forat);
1386 if (vp->v_type != VREG) {
1390 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1396 error = nfsrv_access(mp, vp, VWRITE, cred, rdonly, td, 1);
1399 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1400 ioflags = IO_NODELOCKED;
1401 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1402 ioflags = (IO_SYNC | IO_NODELOCKED);
1404 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1405 uiop->uio_rw = UIO_WRITE;
1406 uiop->uio_segflg = UIO_SYSSPACE;
1407 uiop->uio_td = NULL;
1408 uiop->uio_offset = nfsd->nd_off;
1409 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1410 if (uiop->uio_resid > 0) {
1418 uiop->uio_iovcnt = i;
1419 MALLOC(iov, struct iovec *, i * sizeof (struct iovec),
1421 uiop->uio_iov = ivp = iov;
1424 if (mp1->m_len > 0) {
1425 ivp->iov_base = mtod(mp1, caddr_t);
1426 ivp->iov_len = mp1->m_len;
1432 error = VOP_WRITE(vp, uiop, ioflags, cred);
1433 nfsstats.srvvop_writes++;
1435 FREE((caddr_t)iov, M_TEMP);
1440 aftat_ret = VOP_GETATTR(vp, &va);
1446 * Loop around generating replies for all write rpcs that have
1447 * now been completed.
1451 NFS_DPF(WG, ("R%03x", nfsd->nd_retxid & 0xfff));
1453 nfsm_writereply(&info, nfsd, slp, error,
1454 NFSX_WCCDATA(info.v3));
1456 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1460 nfsm_writereply(&info, nfsd, slp, error,
1461 NFSX_PREOPATTR(info.v3) +
1462 NFSX_POSTOPORFATTR(info.v3) +
1464 NFSX_WRITEVERF(info.v3));
1466 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1468 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1469 *tl++ = txdr_unsigned(nfsd->nd_len);
1470 *tl++ = txdr_unsigned(swp->nd_stable);
1472 * Actually, there is no need to txdr these fields,
1473 * but it may make the values more human readable,
1474 * for debugging purposes.
1476 if (nfsver.tv_sec == 0)
1478 *tl++ = txdr_unsigned(nfsver.tv_sec);
1479 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1481 fp = nfsm_build(&info, NFSX_V2FATTR);
1482 nfsm_srvfattr(nfsd, &va, fp);
1485 nfsd->nd_mreq = info.mreq;
1487 panic("nfsrv_write: nd_mrep not free");
1490 * Done. Put it at the head of the timer queue so that
1491 * the final phase can return the reply.
1495 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1497 nfsd = swp->nd_coalesce.lh_first;
1499 LIST_REMOVE(nfsd, nd_tq);
1503 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1508 * Search for a reply to return.
1510 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = nfsd->nd_tq.le_next) {
1511 if (nfsd->nd_mreq) {
1512 NFS_DPF(WG, ("X%03x", nfsd->nd_retxid & 0xfff));
1513 LIST_REMOVE(nfsd, nd_tq);
1519 *mrq = nfsd->nd_mreq;
1528 * Coalesce the write request nfsd into owp. To do this we must:
1529 * - remove nfsd from the queues
1530 * - merge nfsd->nd_mrep into owp->nd_mrep
1531 * - update the nd_eoff and nd_stable for owp
1532 * - put nfsd on owp's nd_coalesce list
1533 * NB: Must be called at splsoftclock().
1536 nfsrvw_coalesce(struct nfsrv_descript *owp, struct nfsrv_descript *nfsd)
1540 struct nfsrv_descript *p;
1542 NFS_DPF(WG, ("C%03x-%03x",
1543 nfsd->nd_retxid & 0xfff, owp->nd_retxid & 0xfff));
1544 LIST_REMOVE(nfsd, nd_hash);
1545 LIST_REMOVE(nfsd, nd_tq);
1546 if (owp->nd_eoff < nfsd->nd_eoff) {
1547 overlap = owp->nd_eoff - nfsd->nd_off;
1549 panic("nfsrv_coalesce: bad off");
1551 m_adj(nfsd->nd_mrep, overlap);
1555 mp1->m_next = nfsd->nd_mrep;
1556 owp->nd_eoff = nfsd->nd_eoff;
1558 m_freem(nfsd->nd_mrep);
1559 nfsd->nd_mrep = NULL;
1560 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1561 owp->nd_stable = NFSV3WRITE_FILESYNC;
1562 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1563 owp->nd_stable == NFSV3WRITE_UNSTABLE)
1564 owp->nd_stable = NFSV3WRITE_DATASYNC;
1565 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1568 * If nfsd had anything else coalesced into it, transfer them
1569 * to owp, otherwise their replies will never get sent.
1571 for (p = nfsd->nd_coalesce.lh_first; p;
1572 p = nfsd->nd_coalesce.lh_first) {
1573 LIST_REMOVE(p, nd_tq);
1574 LIST_INSERT_HEAD(&owp->nd_coalesce, p, nd_tq);
1579 * nfs create service
1580 * now does a truncate to 0 length via. setattr if it already exists
1583 nfsrv_create(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1584 struct thread *td, struct mbuf **mrq)
1586 struct sockaddr *nam = nfsd->nd_nam;
1587 struct ucred *cred = &nfsd->nd_cr;
1588 struct nfs_fattr *fp;
1589 struct vattr va, dirfor, diraft;
1590 struct vattr *vap = &va;
1591 struct nfsv2_sattr *sp;
1593 struct nlookupdata nd;
1594 int error = 0, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1595 udev_t rdev = NOUDEV;
1597 int how, exclusive_flag = 0;
1605 u_char cverf[NFSX_V3CREATEVERF];
1606 struct nfsm_info info;
1608 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1614 info.mrep = nfsd->nd_mrep;
1616 info.md = nfsd->nd_md;
1617 info.dpos = nfsd->nd_dpos;
1618 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1620 fhp = &nfh.fh_generic;
1621 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
1622 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
1625 * Call namei and do initial cleanup to get a few things
1626 * out of the way. If we get an initial error we cleanup
1627 * and return here to avoid special-casing the invalid nd
1628 * structure through the rest of the case. dirp may be
1629 * set even if an error occurs, but the nd structure will not
1630 * be valid at all if an error occurs so we have to invalidate it
1631 * prior to calling nfsm_reply ( which might goto nfsmout ).
1633 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
1634 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
1635 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1636 mp = vfs_getvfs(&fhp->fh_fsid);
1640 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
1647 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1648 NFSX_WCCDATA(info.v3), &error));
1649 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1650 diraft_ret, &diraft);
1656 * No error. Continue. State:
1659 * vp may be valid or NULL if the target does not
1663 * The error state is set through the code and we may also do some
1664 * opportunistic releasing of vnodes to avoid holding locks through
1665 * NFS I/O. The cleanup at the end is a catch-all
1670 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
1671 how = fxdr_unsigned(int, *tl);
1673 case NFSV3CREATE_GUARDED:
1679 case NFSV3CREATE_UNCHECKED:
1680 ERROROUT(nfsm_srvsattr(&info, vap));
1682 case NFSV3CREATE_EXCLUSIVE:
1683 NULLOUT(cp = nfsm_dissect(&info, NFSX_V3CREATEVERF));
1684 bcopy(cp, cverf, NFSX_V3CREATEVERF);
1688 vap->va_type = VREG;
1690 NULLOUT(sp = nfsm_dissect(&info, NFSX_V2SATTR));
1691 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1692 if (vap->va_type == VNON)
1693 vap->va_type = VREG;
1694 vap->va_mode = nfstov_mode(sp->sa_mode);
1695 switch (vap->va_type) {
1697 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1699 vap->va_size = (u_quad_t)tsize;
1704 rdev = fxdr_unsigned(long, sp->sa_size);
1712 * Iff doesn't exist, create it
1713 * otherwise just truncate to 0 length
1714 * should I set the mode too ?
1716 * The only possible error we can have at this point is EEXIST.
1717 * nd.ni_vp will also be non-NULL in that case.
1720 if (vap->va_mode == (mode_t)VNOVAL)
1722 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1724 error = VOP_NCREATE(&nd.nl_nch, dvp, &vp,
1729 if (exclusive_flag) {
1732 bcopy(cverf, (caddr_t)&vap->va_atime,
1734 error = VOP_SETATTR(vp, vap, cred);
1738 vap->va_type == VCHR ||
1739 vap->va_type == VBLK ||
1740 vap->va_type == VFIFO
1743 * Handle SysV FIFO node special cases. All other
1744 * devices require super user to access.
1746 if (vap->va_type == VCHR && rdev == 0xffffffff)
1747 vap->va_type = VFIFO;
1748 if (vap->va_type != VFIFO &&
1749 (error = priv_check_cred(cred, PRIV_ROOT, 0))) {
1752 vap->va_rmajor = umajor(rdev);
1753 vap->va_rminor = uminor(rdev);
1756 error = VOP_NMKNOD(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1763 * XXX what is this junk supposed to do ?
1770 * release dvp prior to lookup
1778 * Even though LOCKPARENT was cleared, ni_dvp may
1781 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
1782 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT);
1783 nd.ni_cnd.cn_td = td;
1784 nd.ni_cnd.cn_cred = cred;
1786 error = lookup(&nd);
1790 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1792 /* fall through on certain errors */
1794 nfsrv_object_create(nd.ni_vp);
1795 if (nd.ni_cnd.cn_flags & CNP_ISSYMLINK) {
1804 if (vap->va_size != -1) {
1805 error = nfsrv_access(mp, vp, VWRITE, cred,
1806 (nd.nl_flags & NLC_NFS_RDONLY), td, 0);
1808 tempsize = vap->va_size;
1810 vap->va_size = tempsize;
1811 error = VOP_SETATTR(vp, vap, cred);
1817 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
1818 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1820 error = VOP_GETATTR(vp, vap);
1823 if (exclusive_flag && !error &&
1824 bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1826 diraft_ret = VOP_GETATTR(dirp, &diraft);
1830 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1831 NFSX_SRVFH(info.v3) + NFSX_FATTR(info.v3) +
1832 NFSX_WCCDATA(info.v3),
1836 nfsm_srvpostop_fh(&info, fhp);
1837 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
1839 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1840 diraft_ret, &diraft);
1843 nfsm_srvfhtom(&info, fhp);
1844 fp = nfsm_build(&info, NFSX_V2FATTR);
1845 nfsm_srvfattr(nfsd, vap, fp);
1850 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
1871 * nfs v3 mknod service
1874 nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1875 struct thread *td, struct mbuf **mrq)
1877 struct sockaddr *nam = nfsd->nd_nam;
1878 struct ucred *cred = &nfsd->nd_cr;
1879 struct vattr va, dirfor, diraft;
1880 struct vattr *vap = &va;
1882 struct nlookupdata nd;
1883 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
1890 struct nfsm_info info;
1892 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1898 info.mrep = nfsd->nd_mrep;
1900 info.md = nfsd->nd_md;
1901 info.dpos = nfsd->nd_dpos;
1903 fhp = &nfh.fh_generic;
1904 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
1905 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
1908 * Handle nfs_namei() call. If an error occurs, the nd structure
1909 * is not valid. However, nfsm_*() routines may still jump to
1913 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
1914 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
1915 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1917 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
1919 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1920 NFSX_WCCDATA(1), &error));
1921 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1922 diraft_ret, &diraft);
1926 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
1927 vtyp = nfsv3tov_type(*tl);
1928 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1929 error = NFSERR_BADTYPE;
1933 ERROROUT(nfsm_srvsattr(&info, vap));
1934 if (vtyp == VCHR || vtyp == VBLK) {
1935 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
1936 vap->va_rmajor = fxdr_unsigned(u_int32_t, *tl++);
1937 vap->va_rminor = fxdr_unsigned(u_int32_t, *tl);
1941 * Iff doesn't exist, create it.
1947 vap->va_type = vtyp;
1948 if (vap->va_mode == (mode_t)VNOVAL)
1950 if (vtyp == VSOCK) {
1952 error = VOP_NCREATE(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1956 if (vtyp != VFIFO && (error = priv_check_cred(cred, PRIV_ROOT, 0)))
1960 error = VOP_NMKNOD(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1968 * send response, cleanup, return.
1980 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
1981 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1983 error = VOP_GETATTR(vp, vap);
1989 diraft_ret = VOP_GETATTR(dirp, &diraft);
1994 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1995 NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) +
1996 NFSX_WCCDATA(1), &error));
1998 nfsm_srvpostop_fh(&info, fhp);
1999 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
2001 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2002 diraft_ret, &diraft);
2022 * nfs remove service
2025 nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2026 struct thread *td, struct mbuf **mrq)
2028 struct sockaddr *nam = nfsd->nd_nam;
2029 struct ucred *cred = &nfsd->nd_cr;
2030 struct nlookupdata nd;
2031 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2035 struct vattr dirfor, diraft;
2038 struct nfsm_info info;
2040 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2046 info.mrep = nfsd->nd_mrep;
2048 info.md = nfsd->nd_md;
2049 info.dpos = nfsd->nd_dpos;
2050 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2052 fhp = &nfh.fh_generic;
2053 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2054 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2056 error = nfs_namei(&nd, cred, NLC_DELETE, &dvp, &vp,
2057 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2058 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2061 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2064 if (vp->v_type == VDIR) {
2065 error = EPERM; /* POSIX */
2069 * The root of a mounted filesystem cannot be deleted.
2071 if (vp->v_flag & VROOT) {
2083 error = VOP_NREMOVE(&nd.nl_nch, dvp, nd.nl_cred);
2088 if (dirp && info.v3)
2089 diraft_ret = VOP_GETATTR(dirp, &diraft);
2090 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_WCCDATA(info.v3), &error));
2092 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2093 diraft_ret, &diraft);
2113 * nfs rename service
2116 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2117 struct thread *td, struct mbuf **mrq)
2119 struct sockaddr *nam = nfsd->nd_nam;
2120 struct ucred *cred = &nfsd->nd_cr;
2121 int error = 0, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2122 int tdirfor_ret = 1, tdiraft_ret = 1;
2123 struct nlookupdata fromnd, tond;
2124 struct vnode *fvp, *fdirp, *fdvp;
2125 struct vnode *tvp, *tdirp, *tdvp;
2126 struct namecache *ncp;
2127 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2129 fhandle_t *ffhp, *tfhp;
2131 struct nfsm_info info;
2133 info.mrep = nfsd->nd_mrep;
2135 info.md = nfsd->nd_md;
2136 info.dpos = nfsd->nd_dpos;
2137 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2139 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2143 ffhp = &fnfh.fh_generic;
2144 tfhp = &tnfh.fh_generic;
2147 * Clear fields incase goto nfsmout occurs from macro.
2150 nlookup_zero(&fromnd);
2151 nlookup_zero(&tond);
2155 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, ffhp, &error));
2156 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2159 * Remember our original uid so that we can reset cr_uid before
2160 * the second nfs_namei() call, in case it is remapped.
2162 saved_uid = cred->cr_uid;
2163 error = nfs_namei(&fromnd, cred, NLC_RENAME_SRC,
2165 ffhp, len, slp, nam, &info.md, &info.dpos, &fdirp,
2166 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2169 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor);
2172 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2173 2 * NFSX_WCCDATA(info.v3), &error));
2174 nfsm_srvwcc_data(&info, nfsd, fdirfor_ret, &fdirfor,
2175 fdiraft_ret, &fdiraft);
2176 nfsm_srvwcc_data(&info, nfsd, tdirfor_ret, &tdirfor,
2177 tdiraft_ret, &tdiraft);
2183 * We have to unlock the from ncp before we can safely lookup
2186 KKASSERT(fromnd.nl_flags & NLC_NCPISLOCKED);
2187 cache_unlock(&fromnd.nl_nch);
2188 fromnd.nl_flags &= ~NLC_NCPISLOCKED;
2189 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, tfhp, &error));
2190 NEGATIVEOUT(len2 = nfsm_strsiz(&info, NFS_MAXNAMLEN));
2191 cred->cr_uid = saved_uid;
2193 error = nfs_namei(&tond, cred, NLC_RENAME_DST, NULL, NULL,
2194 tfhp, len2, slp, nam, &info.md, &info.dpos, &tdirp,
2195 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2198 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor);
2206 if (cache_lock_nonblock(&fromnd.nl_nch) == 0) {
2207 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2208 } else if (fromnd.nl_nch.ncp > tond.nl_nch.ncp) {
2209 cache_lock(&fromnd.nl_nch);
2210 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2212 cache_unlock(&tond.nl_nch);
2213 cache_lock(&fromnd.nl_nch);
2214 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2215 cache_lock(&tond.nl_nch);
2216 cache_resolve(&tond.nl_nch, tond.nl_cred);
2218 fromnd.nl_flags |= NLC_NCPISLOCKED;
2220 fvp = fromnd.nl_nch.ncp->nc_vp;
2221 tvp = tond.nl_nch.ncp->nc_vp;
2224 * Set fdvp and tdvp. We haven't done all the topology checks
2225 * so these can wind up NULL (e.g. if either fvp or tvp is a mount
2226 * point). If we get through the checks these will be guarenteed
2229 * Holding the children ncp's should be sufficient to prevent
2230 * fdvp and tdvp ripouts.
2232 if (fromnd.nl_nch.ncp->nc_parent)
2233 fdvp = fromnd.nl_nch.ncp->nc_parent->nc_vp;
2236 if (tond.nl_nch.ncp->nc_parent)
2237 tdvp = tond.nl_nch.ncp->nc_parent->nc_vp;
2242 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2248 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2255 if (tvp->v_type == VDIR && (tond.nl_nch.ncp->nc_flag & NCF_ISMOUNTPT)) {
2263 if (fvp->v_type == VDIR && (fromnd.nl_nch.ncp->nc_flag & NCF_ISMOUNTPT)) {
2270 if (fromnd.nl_nch.mount != tond.nl_nch.mount) {
2277 if (fromnd.nl_nch.ncp == tond.nl_nch.ncp->nc_parent) {
2285 * You cannot rename a source into itself or a subdirectory of itself.
2286 * We check this by travsering the target directory upwards looking
2287 * for a match against the source.
2290 for (ncp = tond.nl_nch.ncp; ncp; ncp = ncp->nc_parent) {
2291 if (fromnd.nl_nch.ncp == ncp) {
2299 * If source is the same as the destination (that is the
2300 * same vnode with the same name in the same directory),
2301 * then there is nothing to do.
2303 if (fromnd.nl_nch.ncp == tond.nl_nch.ncp)
2308 * The VOP_NRENAME function releases all vnode references &
2309 * locks prior to returning so we need to clear the pointers
2310 * to bypass cleanup code later on.
2312 error = VOP_NRENAME(&fromnd.nl_nch, &tond.nl_nch,
2313 fdvp, tdvp, tond.nl_cred);
2322 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft);
2324 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft);
2325 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2326 2 * NFSX_WCCDATA(info.v3), &error));
2328 nfsm_srvwcc_data(&info, nfsd, fdirfor_ret, &fdirfor,
2329 fdiraft_ret, &fdiraft);
2330 nfsm_srvwcc_data(&info, nfsd, tdirfor_ret, &tdirfor,
2331 tdiraft_ret, &tdiraft);
2340 nlookup_done(&tond);
2343 nlookup_done(&fromnd);
2351 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2352 struct thread *td, struct mbuf **mrq)
2354 struct sockaddr *nam = nfsd->nd_nam;
2355 struct ucred *cred = &nfsd->nd_cr;
2356 struct nlookupdata nd;
2357 int error = 0, rdonly, len, dirfor_ret = 1, diraft_ret = 1;
2365 struct vattr dirfor, diraft, at;
2367 fhandle_t *fhp, *dfhp;
2368 struct nfsm_info info;
2370 info.mrep = nfsd->nd_mrep;
2372 info.md = nfsd->nd_md;
2373 info.dpos = nfsd->nd_dpos;
2374 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2376 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2378 dirp = dvp = vp = xp = NULL;
2381 fhp = &nfh.fh_generic;
2382 dfhp = &dnfh.fh_generic;
2383 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2384 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, dfhp, &error));
2385 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2387 error = nfsrv_fhtovp(fhp, FALSE, &xmp, &xp, cred, slp, nam,
2388 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2390 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2391 NFSX_POSTOPATTR(info.v3) +
2392 NFSX_WCCDATA(info.v3),
2394 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2395 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2396 diraft_ret, &diraft);
2401 if (xp->v_type == VDIR) {
2402 error = EPERM; /* POSIX */
2406 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2407 dfhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2408 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2411 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2420 if (xp->v_mount != dvp->v_mount)
2425 error = VOP_NLINK(&nd.nl_nch, dvp, xp, nd.nl_cred);
2433 getret = VOP_GETATTR(xp, &at);
2435 diraft_ret = VOP_GETATTR(dirp, &diraft);
2436 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2437 NFSX_POSTOPATTR(info.v3) + NFSX_WCCDATA(info.v3),
2440 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2441 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2442 diraft_ret, &diraft);
2466 * nfs symbolic link service
2469 nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2470 struct thread *td, struct mbuf **mrq)
2472 struct sockaddr *nam = nfsd->nd_nam;
2473 struct ucred *cred = &nfsd->nd_cr;
2474 struct vattr va, dirfor, diraft;
2475 struct nlookupdata nd;
2476 struct vattr *vap = &va;
2477 struct nfsv2_sattr *sp;
2478 char *pathcp = NULL;
2481 int error = 0, len, len2, dirfor_ret = 1, diraft_ret = 1;
2487 struct nfsm_info info;
2489 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2495 info.mrep = nfsd->nd_mrep;
2497 info.md = nfsd->nd_md;
2498 info.dpos = nfsd->nd_dpos;
2499 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2501 fhp = &nfh.fh_generic;
2502 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2503 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2505 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2506 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2507 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2510 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2517 ERROROUT(nfsm_srvsattr(&info, vap));
2519 NEGATIVEOUT(len2 = nfsm_strsiz(&info, NFS_MAXPATHLEN));
2520 MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
2521 iv.iov_base = pathcp;
2523 io.uio_resid = len2;
2527 io.uio_segflg = UIO_SYSSPACE;
2528 io.uio_rw = UIO_READ;
2530 ERROROUT(nfsm_mtouio(&info, &io, len2));
2532 NULLOUT(sp = nfsm_dissect(&info, NFSX_V2SATTR));
2533 vap->va_mode = nfstov_mode(sp->sa_mode);
2535 *(pathcp + len2) = '\0';
2541 if (vap->va_mode == (mode_t)VNOVAL)
2545 error = VOP_NSYMLINK(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap, pathcp);
2549 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
2550 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2552 error = VOP_GETATTR(vp, vap);
2567 FREE(pathcp, M_TEMP);
2571 diraft_ret = VOP_GETATTR(dirp, &diraft);
2575 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2576 NFSX_SRVFH(info.v3) + NFSX_POSTOPATTR(info.v3) +
2577 NFSX_WCCDATA(info.v3),
2581 nfsm_srvpostop_fh(&info, fhp);
2582 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
2584 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2585 diraft_ret, &diraft);
2598 FREE(pathcp, M_TEMP);
2606 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2607 struct thread *td, struct mbuf **mrq)
2609 struct sockaddr *nam = nfsd->nd_nam;
2610 struct ucred *cred = &nfsd->nd_cr;
2611 struct vattr va, dirfor, diraft;
2612 struct vattr *vap = &va;
2613 struct nfs_fattr *fp;
2614 struct nlookupdata nd;
2616 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2622 struct nfsm_info info;
2624 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2630 info.dpos = nfsd->nd_dpos;
2631 info.mrep = nfsd->nd_mrep;
2633 info.md = nfsd->nd_md;
2634 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2636 fhp = &nfh.fh_generic;
2637 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2638 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2640 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2641 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2642 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2645 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2648 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2649 NFSX_WCCDATA(info.v3), &error));
2650 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2651 diraft_ret, &diraft);
2657 ERROROUT(nfsm_srvsattr(&info, vap));
2659 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2660 vap->va_mode = nfstov_mode(*tl++);
2664 * At this point nd.ni_dvp is referenced and exclusively locked and
2665 * nd.ni_vp, if it exists, is referenced but not locked.
2668 vap->va_type = VDIR;
2675 * Issue mkdir op. Since SAVESTART is not set, the pathname
2676 * component is freed by the VOP call. This will fill-in
2677 * nd.ni_vp, reference, and exclusively lock it.
2679 if (vap->va_mode == (mode_t)VNOVAL)
2682 error = VOP_NMKDIR(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
2687 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
2688 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2690 error = VOP_GETATTR(vp, vap);
2694 diraft_ret = VOP_GETATTR(dirp, &diraft);
2695 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2696 NFSX_SRVFH(info.v3) + NFSX_POSTOPATTR(info.v3) +
2697 NFSX_WCCDATA(info.v3),
2701 nfsm_srvpostop_fh(&info, fhp);
2702 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
2704 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2705 diraft_ret, &diraft);
2707 nfsm_srvfhtom(&info, fhp);
2708 fp = nfsm_build(&info, NFSX_V2FATTR);
2709 nfsm_srvfattr(nfsd, vap, fp);
2734 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2735 struct thread *td, struct mbuf **mrq)
2737 struct sockaddr *nam = nfsd->nd_nam;
2738 struct ucred *cred = &nfsd->nd_cr;
2739 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2743 struct vattr dirfor, diraft;
2746 struct nlookupdata nd;
2747 struct nfsm_info info;
2749 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2755 info.mrep = nfsd->nd_mrep;
2757 info.md = nfsd->nd_md;
2758 info.dpos = nfsd->nd_dpos;
2759 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2761 fhp = &nfh.fh_generic;
2762 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2763 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2765 error = nfs_namei(&nd, cred, NLC_DELETE, &dvp, &vp,
2766 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2767 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2770 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2773 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2774 NFSX_WCCDATA(info.v3), &error));
2775 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2776 diraft_ret, &diraft);
2780 if (vp->v_type != VDIR) {
2786 * The root of a mounted filesystem cannot be deleted.
2788 if (vp->v_flag & VROOT)
2792 * Issue or abort op. Since SAVESTART is not set, path name
2793 * component is freed by the VOP after either.
2800 error = VOP_NRMDIR(&nd.nl_nch, dvp, nd.nl_cred);
2807 diraft_ret = VOP_GETATTR(dirp, &diraft);
2808 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_WCCDATA(info.v3), &error));
2810 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2811 diraft_ret, &diraft);
2833 * nfs readdir service
2834 * - mallocs what it thinks is enough to read
2835 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2836 * - calls VOP_READDIR()
2837 * - loops around building the reply
2838 * if the output generated exceeds count break out of loop
2839 * The nfsm_clget macro is used here so that the reply will be packed
2840 * tightly in mbuf clusters.
2841 * - it only knows that it has encountered eof when the VOP_READDIR()
2843 * - as such one readdir rpc will return eof false although you are there
2844 * and then the next will return eof
2845 * - it trims out records with d_fileno == 0
2846 * this doesn't matter for Unix clients, but they might confuse clients
2848 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2849 * than requested, but this may not apply to all filesystems. For
2850 * example, client NFS does not { although it is never remote mounted
2852 * The alternate call nfsrv_readdirplus() does lookups as well.
2853 * PS: The NFS protocol spec. does not clarify what the "count" byte
2854 * argument is a count of.. just name strings and file id's or the
2855 * entire reply rpc or ...
2856 * I tried just file name and id sizes and it confused the Sun client,
2857 * so I am using the full rpc size now. The "paranoia.." comment refers
2858 * to including the status longwords that are not a part of the dir.
2859 * "entry" structures, but are in the rpc.
2863 u_int32_t fl_postopok;
2864 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2866 u_int32_t fl_fhsize;
2867 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2871 nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2872 struct thread *td, struct mbuf **mrq)
2874 struct sockaddr *nam = nfsd->nd_nam;
2875 struct ucred *cred = &nfsd->nd_cr;
2880 struct mbuf *mp1, *mp2;
2881 char *cpos, *cend, *rbuf;
2882 struct vnode *vp = NULL;
2883 struct mount *mp = NULL;
2889 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2890 int siz, cnt, fullsiz, eofflag, rdonly, ncookies;
2891 u_quad_t off, toff, verf;
2892 off_t *cookies = NULL, *cookiep;
2893 struct nfsm_info info;
2895 info.mrep = nfsd->nd_mrep;
2897 info.md = nfsd->nd_md;
2898 info.dpos = nfsd->nd_dpos;
2899 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2901 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2902 fhp = &nfh.fh_generic;
2903 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2905 NULLOUT(tl = nfsm_dissect(&info, 5 * NFSX_UNSIGNED));
2906 toff = fxdr_hyper(tl);
2908 verf = fxdr_hyper(tl);
2911 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2912 toff = fxdr_unsigned(u_quad_t, *tl++);
2913 verf = 0; /* shut up gcc */
2916 cnt = fxdr_unsigned(int, *tl);
2917 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2918 xfer = NFS_SRVMAXDATA(nfsd);
2919 if ((unsigned)cnt > xfer)
2921 if ((unsigned)siz > xfer)
2924 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
2925 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2926 if (!error && vp->v_type != VDIR) {
2932 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
2933 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2939 * Obtain lock on vnode for this section of the code
2943 error = getret = VOP_GETATTR(vp, &at);
2946 * XXX This check may be too strict for Solaris 2.5 clients.
2948 if (!error && toff && verf && verf != at.va_filerev)
2949 error = NFSERR_BAD_COOKIE;
2953 error = nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 0);
2957 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2958 NFSX_POSTOPATTR(info.v3), &error));
2959 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2966 * end section. Allocate rbuf and continue
2968 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
2971 iv.iov_len = fullsiz;
2974 io.uio_offset = (off_t)off;
2975 io.uio_resid = fullsiz;
2976 io.uio_segflg = UIO_SYSSPACE;
2977 io.uio_rw = UIO_READ;
2981 kfree((caddr_t)cookies, M_TEMP);
2984 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
2985 off = (off_t)io.uio_offset;
2986 if (!cookies && !error)
2987 error = NFSERR_PERM;
2989 getret = VOP_GETATTR(vp, &at);
2996 kfree((caddr_t)rbuf, M_TEMP);
2998 kfree((caddr_t)cookies, M_TEMP);
2999 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3000 NFSX_POSTOPATTR(info.v3), &error));
3001 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3006 siz -= io.uio_resid;
3009 * If nothing read, return eof
3015 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3016 NFSX_POSTOPATTR(info.v3) +
3017 NFSX_COOKIEVERF(info.v3) +
3021 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3022 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
3023 txdr_hyper(at.va_filerev, tl);
3026 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
3029 FREE((caddr_t)rbuf, M_TEMP);
3030 FREE((caddr_t)cookies, M_TEMP);
3037 * Check for degenerate cases of nothing useful read.
3038 * If so go try again
3042 dp = (struct dirent *)cpos;
3045 * For some reason FreeBSD's ufs_readdir() chooses to back the
3046 * directory offset up to a block boundary, so it is necessary to
3047 * skip over the records that preceed the requested offset. This
3048 * requires the assumption that file offset cookies monotonically
3051 while (cpos < cend && ncookies > 0 &&
3052 (dp->d_ino == 0 || dp->d_type == DT_WHT ||
3053 ((u_quad_t)(*cookiep)) <= toff)) {
3054 dp = _DIRENT_NEXT(dp);
3059 if (cpos >= cend || ncookies == 0) {
3065 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
3066 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3067 NFSX_POSTOPATTR(info.v3) +
3068 NFSX_COOKIEVERF(info.v3) + siz,
3071 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3072 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
3073 txdr_hyper(at.va_filerev, tl);
3075 mp1 = mp2 = info.mb;
3077 be = bp + M_TRAILINGSPACE(mp1);
3079 /* Loop through the records and build reply */
3080 while (cpos < cend && ncookies > 0) {
3081 if (dp->d_ino != 0 && dp->d_type != DT_WHT) {
3082 nlen = dp->d_namlen;
3083 rem = nfsm_rndup(nlen) - nlen;
3084 len += (4 * NFSX_UNSIGNED + nlen + rem);
3086 len += 2 * NFSX_UNSIGNED;
3092 * Build the directory record xdr from
3095 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3097 bp += NFSX_UNSIGNED;
3099 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3100 *tl = txdr_unsigned(dp->d_ino >> 32);
3101 bp += NFSX_UNSIGNED;
3103 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3104 *tl = txdr_unsigned(dp->d_ino);
3105 bp += NFSX_UNSIGNED;
3106 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3107 *tl = txdr_unsigned(nlen);
3108 bp += NFSX_UNSIGNED;
3110 /* And loop around copying the name */
3114 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3119 bcopy(cp, bp, tsiz);
3125 /* And null pad to a int32_t boundary */
3126 for (i = 0; i < rem; i++)
3128 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3130 /* Finish off the record */
3132 *tl = txdr_unsigned(*cookiep >> 32);
3133 bp += NFSX_UNSIGNED;
3134 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3136 *tl = txdr_unsigned(*cookiep);
3137 bp += NFSX_UNSIGNED;
3139 dp = _DIRENT_NEXT(dp);
3146 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3148 bp += NFSX_UNSIGNED;
3149 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3154 bp += NFSX_UNSIGNED;
3155 if (mp1 != info.mb) {
3157 mp1->m_len = bp - mtod(mp1, caddr_t);
3159 mp1->m_len += bp - info.bpos;
3160 FREE((caddr_t)rbuf, M_TEMP);
3161 FREE((caddr_t)cookies, M_TEMP);
3171 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3172 struct thread *td, struct mbuf **mrq)
3174 struct sockaddr *nam = nfsd->nd_nam;
3175 struct ucred *cred = &nfsd->nd_cr;
3180 struct mbuf *mp1, *mp2;
3181 char *cpos, *cend, *rbuf;
3182 struct vnode *vp = NULL, *nvp;
3183 struct mount *mp = NULL;
3186 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3189 struct vattr va, at, *vap = &va;
3190 struct nfs_fattr *fp;
3191 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3192 int siz, cnt, fullsiz, eofflag, rdonly, dirlen, ncookies;
3193 u_quad_t off, toff, verf;
3194 off_t *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3195 struct nfsm_info info;
3197 info.mrep = nfsd->nd_mrep;
3199 info.md = nfsd->nd_md;
3200 info.dpos = nfsd->nd_dpos;
3201 info.v3 = (nfsd->nd_flag & ND_NFSV3);
3203 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3204 fhp = &nfh.fh_generic;
3205 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3206 NULLOUT(tl = nfsm_dissect(&info, 6 * NFSX_UNSIGNED));
3207 toff = fxdr_hyper(tl);
3209 verf = fxdr_hyper(tl);
3211 siz = fxdr_unsigned(int, *tl++);
3212 cnt = fxdr_unsigned(int, *tl);
3214 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3215 xfer = NFS_SRVMAXDATA(nfsd);
3216 if ((unsigned)cnt > xfer)
3218 if ((unsigned)siz > xfer)
3221 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3222 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3223 if (!error && vp->v_type != VDIR) {
3229 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3230 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3234 error = getret = VOP_GETATTR(vp, &at);
3237 * XXX This check may be too strict for Solaris 2.5 clients.
3239 if (!error && toff && verf && verf != at.va_filerev)
3240 error = NFSERR_BAD_COOKIE;
3243 error = nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 0);
3248 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3249 NFSX_V3POSTOPATTR, &error));
3250 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3255 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3258 iv.iov_len = fullsiz;
3261 io.uio_offset = (off_t)off;
3262 io.uio_resid = fullsiz;
3263 io.uio_segflg = UIO_SYSSPACE;
3264 io.uio_rw = UIO_READ;
3268 kfree((caddr_t)cookies, M_TEMP);
3271 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3272 off = (u_quad_t)io.uio_offset;
3273 getret = VOP_GETATTR(vp, &at);
3274 if (!cookies && !error)
3275 error = NFSERR_PERM;
3282 kfree((caddr_t)cookies, M_TEMP);
3283 kfree((caddr_t)rbuf, M_TEMP);
3284 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3285 NFSX_V3POSTOPATTR, &error));
3286 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3291 siz -= io.uio_resid;
3294 * If nothing read, return eof
3300 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3305 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3306 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
3307 txdr_hyper(at.va_filerev, tl);
3311 FREE((caddr_t)cookies, M_TEMP);
3312 FREE((caddr_t)rbuf, M_TEMP);
3319 * Check for degenerate cases of nothing useful read.
3320 * If so go try again
3324 dp = (struct dirent *)cpos;
3327 * For some reason FreeBSD's ufs_readdir() chooses to back the
3328 * directory offset up to a block boundary, so it is necessary to
3329 * skip over the records that preceed the requested offset. This
3330 * requires the assumption that file offset cookies monotonically
3333 while (cpos < cend && ncookies > 0 &&
3334 (dp->d_ino == 0 || dp->d_type == DT_WHT ||
3335 ((u_quad_t)(*cookiep)) <= toff)) {
3336 dp = _DIRENT_NEXT(dp);
3341 if (cpos >= cend || ncookies == 0) {
3348 * Probe one of the directory entries to see if the filesystem
3351 if (VFS_VGET(vp->v_mount, vp, dp->d_ino, &nvp) == EOPNOTSUPP) {
3352 error = NFSERR_NOTSUPP;
3355 kfree((caddr_t)cookies, M_TEMP);
3356 kfree((caddr_t)rbuf, M_TEMP);
3357 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3358 NFSX_V3POSTOPATTR, &error));
3359 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3368 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3370 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, cnt, &error));
3371 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3372 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
3373 txdr_hyper(at.va_filerev, tl);
3374 mp1 = mp2 = info.mb;
3376 be = bp + M_TRAILINGSPACE(mp1);
3378 /* Loop through the records and build reply */
3379 while (cpos < cend && ncookies > 0) {
3380 if (dp->d_ino != 0 && dp->d_type != DT_WHT) {
3381 nlen = dp->d_namlen;
3382 rem = nfsm_rndup(nlen) - nlen;
3385 * For readdir_and_lookup get the vnode using
3388 if (VFS_VGET(vp->v_mount, vp, dp->d_ino, &nvp))
3390 bzero((caddr_t)nfhp, NFSX_V3FH);
3391 nfhp->fh_fsid = fhp->fh_fsid;
3392 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3397 if (VOP_GETATTR(nvp, vap)) {
3406 * If either the dircount or maxcount will be
3407 * exceeded, get out now. Both of these lengths
3408 * are calculated conservatively, including all
3411 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3413 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3414 if (len > cnt || dirlen > fullsiz) {
3420 * Build the directory record xdr from
3423 fp = (struct nfs_fattr *)&fl.fl_fattr;
3424 nfsm_srvfattr(nfsd, vap, fp);
3425 fl.fl_off.nfsuquad[0] = txdr_unsigned(*cookiep >> 32);
3426 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3427 fl.fl_postopok = nfs_true;
3428 fl.fl_fhok = nfs_true;
3429 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3431 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3433 bp += NFSX_UNSIGNED;
3434 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3435 *tl = txdr_unsigned(dp->d_ino >> 32);
3436 bp += NFSX_UNSIGNED;
3437 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3438 *tl = txdr_unsigned(dp->d_ino);
3439 bp += NFSX_UNSIGNED;
3440 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3441 *tl = txdr_unsigned(nlen);
3442 bp += NFSX_UNSIGNED;
3444 /* And loop around copying the name */
3448 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3449 if ((bp + xfer) > be)
3453 bcopy(cp, bp, tsiz);
3458 /* And null pad to a int32_t boundary */
3459 for (i = 0; i < rem; i++)
3463 * Now copy the flrep structure out.
3465 xfer = sizeof (struct flrep);
3468 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3469 if ((bp + xfer) > be)
3473 bcopy(cp, bp, tsiz);
3480 dp = _DIRENT_NEXT(dp);
3487 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3489 bp += NFSX_UNSIGNED;
3490 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3495 bp += NFSX_UNSIGNED;
3496 if (mp1 != info.mb) {
3498 mp1->m_len = bp - mtod(mp1, caddr_t);
3500 mp1->m_len += bp - info.bpos;
3501 FREE((caddr_t)cookies, M_TEMP);
3502 FREE((caddr_t)rbuf, M_TEMP);
3511 * nfs commit service
3514 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3515 struct thread *td, struct mbuf **mrq)
3517 struct sockaddr *nam = nfsd->nd_nam;
3518 struct ucred *cred = &nfsd->nd_cr;
3519 struct vattr bfor, aft;
3520 struct vnode *vp = NULL;
3521 struct mount *mp = NULL;
3525 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt;
3527 struct nfsm_info info;
3529 info.mrep = nfsd->nd_mrep;
3531 info.md = nfsd->nd_md;
3532 info.dpos = nfsd->nd_dpos;
3534 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3535 fhp = &nfh.fh_generic;
3536 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3537 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
3540 * XXX At this time VOP_FSYNC() does not accept offset and byte
3541 * count parameters, so these arguments are useless (someday maybe).
3543 off = fxdr_hyper(tl);
3545 cnt = fxdr_unsigned(int, *tl);
3546 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3547 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3549 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3550 2 * NFSX_UNSIGNED, &error));
3551 nfsm_srvwcc_data(&info, nfsd, for_ret, &bfor,
3556 for_ret = VOP_GETATTR(vp, &bfor);
3558 if (cnt > MAX_COMMIT_COUNT) {
3560 * Give up and do the whole thing
3563 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3564 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3566 error = VOP_FSYNC(vp, MNT_WAIT, 0);
3569 * Locate and synchronously write any buffers that fall
3570 * into the requested range. Note: we are assuming that
3571 * f_iosize is a power of 2.
3573 int iosize = vp->v_mount->mnt_stat.f_iosize;
3574 int iomask = iosize - 1;
3578 * Align to iosize boundry, super-align to page boundry.
3581 cnt += off & iomask;
3582 off &= ~(u_quad_t)iomask;
3584 if (off & PAGE_MASK) {
3585 cnt += off & PAGE_MASK;
3586 off &= ~(u_quad_t)PAGE_MASK;
3591 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3592 vm_object_page_clean(vp->v_object, off / PAGE_SIZE,
3593 (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3601 * If we have a buffer and it is marked B_DELWRI we
3602 * have to lock and write it. Otherwise the prior
3603 * write is assumed to have already been committed.
3605 * WARNING: FINDBLK_TEST buffers represent stable
3606 * storage but not necessarily stable
3607 * content. It is ok in this case.
3609 if ((bp = findblk(vp, loffset, FINDBLK_TEST)) != NULL) {
3610 if (bp->b_flags & B_DELWRI)
3611 bp = findblk(vp, loffset, 0);
3616 if (bp->b_flags & B_DELWRI) {
3633 aft_ret = VOP_GETATTR(vp, &aft);
3636 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3637 NFSX_V3WCCDATA + NFSX_V3WRITEVERF,
3639 nfsm_srvwcc_data(&info, nfsd, for_ret, &bfor,
3642 tl = nfsm_build(&info, NFSX_V3WRITEVERF);
3643 if (nfsver.tv_sec == 0)
3645 *tl++ = txdr_unsigned(nfsver.tv_sec);
3646 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
3658 * nfs statfs service
3661 nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3662 struct thread *td, struct mbuf **mrq)
3664 struct sockaddr *nam = nfsd->nd_nam;
3665 struct ucred *cred = &nfsd->nd_cr;
3667 struct nfs_statfs *sfp;
3668 int error = 0, rdonly, getret = 1;
3669 struct vnode *vp = NULL;
3670 struct mount *mp = NULL;
3674 struct statfs statfs;
3676 struct nfsm_info info;
3678 info.mrep = nfsd->nd_mrep;
3680 info.md = nfsd->nd_md;
3681 info.dpos = nfsd->nd_dpos;
3682 info.v3 = (nfsd->nd_flag & ND_NFSV3);
3684 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3685 fhp = &nfh.fh_generic;
3686 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3687 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3688 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3690 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3691 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3696 error = VFS_STATFS(vp->v_mount, sf, proc0.p_ucred);
3697 getret = VOP_GETATTR(vp, &at);
3700 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3701 NFSX_POSTOPATTR(info.v3) + NFSX_STATFS(info.v3),
3704 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3709 sfp = nfsm_build(&info, NFSX_STATFS(info.v3));
3711 tval = (u_quad_t)sf->f_blocks;
3712 tval *= (u_quad_t)sf->f_bsize;
3713 txdr_hyper(tval, &sfp->sf_tbytes);
3714 tval = (u_quad_t)sf->f_bfree;
3715 tval *= (u_quad_t)sf->f_bsize;
3716 txdr_hyper(tval, &sfp->sf_fbytes);
3717 tval = (u_quad_t)sf->f_bavail;
3718 tval *= (u_quad_t)sf->f_bsize;
3719 txdr_hyper(tval, &sfp->sf_abytes);
3720 sfp->sf_tfiles.nfsuquad[0] = 0;
3721 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3722 sfp->sf_ffiles.nfsuquad[0] = 0;
3723 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3724 sfp->sf_afiles.nfsuquad[0] = 0;
3725 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3726 sfp->sf_invarsec = 0;
3728 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3729 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3730 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3731 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3732 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3742 * nfs fsinfo service
3745 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3746 struct thread *td, struct mbuf **mrq)
3748 struct sockaddr *nam = nfsd->nd_nam;
3749 struct ucred *cred = &nfsd->nd_cr;
3750 struct nfsv3_fsinfo *sip;
3751 int error = 0, rdonly, getret = 1, pref;
3752 struct vnode *vp = NULL;
3753 struct mount *mp = NULL;
3759 struct nfsm_info info;
3761 info.mrep = nfsd->nd_mrep;
3763 info.md = nfsd->nd_md;
3764 info.dpos = nfsd->nd_dpos;
3766 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3767 fhp = &nfh.fh_generic;
3768 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3769 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3770 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3772 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3773 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3778 /* XXX Try to make a guess on the max file size. */
3779 VFS_STATFS(vp->v_mount, &sb, proc0.p_ucred);
3780 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3782 getret = VOP_GETATTR(vp, &at);
3785 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3786 NFSX_V3POSTOPATTR + NFSX_V3FSINFO, &error));
3787 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3788 sip = nfsm_build(&info, NFSX_V3FSINFO);
3792 * There should be file system VFS OP(s) to get this information.
3793 * For now, assume ufs.
3795 if (slp->ns_so->so_type == SOCK_DGRAM)
3796 pref = NFS_MAXDGRAMDATA;
3799 sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3800 sip->fs_rtpref = txdr_unsigned(pref);
3801 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3802 sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3803 sip->fs_wtpref = txdr_unsigned(pref);
3804 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3805 sip->fs_dtpref = txdr_unsigned(pref);
3806 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3807 sip->fs_timedelta.nfsv3_sec = 0;
3808 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3809 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3810 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3811 NFSV3FSINFO_CANSETTIME);
3820 * nfs pathconf service
3823 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3824 struct thread *td, struct mbuf **mrq)
3826 struct sockaddr *nam = nfsd->nd_nam;
3827 struct ucred *cred = &nfsd->nd_cr;
3828 struct nfsv3_pathconf *pc;
3829 int error = 0, rdonly, getret = 1;
3830 register_t linkmax, namemax, chownres, notrunc;
3831 struct vnode *vp = NULL;
3832 struct mount *mp = NULL;
3836 struct nfsm_info info;
3838 info.mrep = nfsd->nd_mrep;
3840 info.md = nfsd->nd_md;
3841 info.dpos = nfsd->nd_dpos;
3843 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3844 fhp = &nfh.fh_generic;
3845 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3846 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3847 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3849 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3850 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3854 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3856 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3858 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3860 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3861 getret = VOP_GETATTR(vp, &at);
3864 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3865 NFSX_V3POSTOPATTR + NFSX_V3PATHCONF,
3867 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3872 pc = nfsm_build(&info, NFSX_V3PATHCONF);
3874 pc->pc_linkmax = txdr_unsigned(linkmax);
3875 pc->pc_namemax = txdr_unsigned(namemax);
3876 pc->pc_notrunc = txdr_unsigned(notrunc);
3877 pc->pc_chownrestricted = txdr_unsigned(chownres);
3880 * These should probably be supported by VOP_PATHCONF(), but
3881 * until msdosfs is exportable (why would you want to?), the
3882 * Unix defaults should be ok.
3884 pc->pc_caseinsensitive = nfs_false;
3885 pc->pc_casepreserving = nfs_true;
3894 * Null operation, used by clients to ping server
3898 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3899 struct thread *td, struct mbuf **mrq)
3901 struct nfsm_info info;
3902 int error = NFSERR_RETVOID;
3904 info.mrep = nfsd->nd_mrep;
3907 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3908 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
3915 * No operation, used for obsolete procedures
3919 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3920 struct thread *td, struct mbuf **mrq)
3922 struct nfsm_info info;
3925 info.mrep = nfsd->nd_mrep;
3928 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3929 if (nfsd->nd_repstat)
3930 error = nfsd->nd_repstat;
3932 error = EPROCUNAVAIL;
3933 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
3941 * Perform access checking for vnodes obtained from file handles that would
3942 * refer to files already opened by a Unix client. You cannot just use
3943 * vn_writechk() and VOP_ACCESS() for two reasons.
3944 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
3945 * 2 - The owner is to be given access irrespective of mode bits for some
3946 * operations, so that processes that chmod after opening a file don't
3947 * break. I don't like this because it opens a security hole, but since
3948 * the nfs server opens a security hole the size of a barn door anyhow,
3951 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
3952 * will return EPERM instead of EACCESS. EPERM is always an error.
3955 nfsrv_access(struct mount *mp, struct vnode *vp, int flags, struct ucred *cred,
3956 int rdonly, struct thread *td, int override)
3961 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3962 if (flags & VWRITE) {
3963 /* Just vn_writechk() changed to check rdonly */
3965 * Disallow write attempts on read-only file systems;
3966 * unless the file is a socket or a block or character
3967 * device resident on the file system.
3970 ((mp->mnt_flag | vp->v_mount->mnt_flag) & MNT_RDONLY)) {
3971 switch (vp->v_type) {
3981 * If there's shared text associated with
3982 * the inode, we can't allow writing.
3984 if (vp->v_flag & VTEXT)
3987 error = VOP_GETATTR(vp, &vattr);
3990 error = VOP_ACCESS(vp, flags, cred); /* XXX ruid/rgid vs uid/gid */
3992 * Allow certain operations for the owner (reads and writes
3993 * on files that are already open).
3995 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)
3999 #endif /* NFS_NOSERVER */