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_COMMIT_COUNT (1024 * 1024)
106 #define NUM_HEURISTIC 1017
107 #define NHUSE_INIT 64
109 #define NHUSE_MAX 2048
111 static struct nfsheur {
112 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
113 off_t nh_nextr; /* next offset for sequential detection */
114 int nh_use; /* use count for selection */
115 int nh_seqcount; /* heuristic */
116 } nfsheur[NUM_HEURISTIC];
118 nfstype nfsv3_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK,
121 nfstype nfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
124 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
125 int nfsrvw_procrastinate_v3 = 0;
127 static struct timespec nfsver;
129 SYSCTL_DECL(_vfs_nfs);
132 SYSCTL_INT(_vfs_nfs, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0,
133 "Enable unstable and fast writes");
134 static int nfs_commit_blks;
135 static int nfs_commit_miss;
136 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 0,
137 "Number of committed blocks");
138 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 0,
139 "Number of nfs blocks committed from dirty buffers");
141 static int nfsrv_access (struct mount *, struct vnode *, int,
142 struct ucred *, int, struct thread *, int);
143 static void nfsrvw_coalesce (struct nfsrv_descript *,
144 struct nfsrv_descript *);
147 * nfs v3 access service
150 nfsrv3_access(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
151 struct thread *td, struct mbuf **mrq)
153 struct sockaddr *nam = nfsd->nd_nam;
154 struct ucred *cred = &nfsd->nd_cr;
155 struct vnode *vp = NULL;
156 struct mount *mp = NULL;
159 int error = 0, rdonly, getret;
160 struct vattr vattr, *vap = &vattr;
161 u_long testmode, nfsmode;
162 struct nfsm_info info;
165 info.dpos = nfsd->nd_dpos;
166 info.md = nfsd->nd_md;
167 info.mrep = nfsd->nd_mrep;
170 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
171 fhp = &nfh.fh_generic;
172 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
173 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
174 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam, &rdonly,
175 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
177 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
178 nfsm_srvpostop_attr(&info, nfsd, 1, NULL);
182 nfsmode = fxdr_unsigned(u_int32_t, *tl);
183 if ((nfsmode & NFSV3ACCESS_READ) &&
184 nfsrv_access(mp, vp, VREAD, cred, rdonly, td, 0))
185 nfsmode &= ~NFSV3ACCESS_READ;
186 if (vp->v_type == VDIR)
187 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
190 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
191 if ((nfsmode & testmode) &&
192 nfsrv_access(mp, vp, VWRITE, cred, rdonly, td, 0))
193 nfsmode &= ~testmode;
194 if (vp->v_type == VDIR)
195 testmode = NFSV3ACCESS_LOOKUP;
197 testmode = NFSV3ACCESS_EXECUTE;
198 if ((nfsmode & testmode) &&
199 nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 0))
200 nfsmode &= ~testmode;
201 getret = VOP_GETATTR(vp, vap);
204 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
205 NFSX_POSTOPATTR(1) + NFSX_UNSIGNED, &error));
206 nfsm_srvpostop_attr(&info, nfsd, getret, vap);
207 tl = nfsm_build(&info, NFSX_UNSIGNED);
208 *tl = txdr_unsigned(nfsmode);
217 * nfs getattr service
220 nfsrv_getattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
221 struct thread *td, struct mbuf **mrq)
223 struct sockaddr *nam = nfsd->nd_nam;
224 struct ucred *cred = &nfsd->nd_cr;
225 struct nfs_fattr *fp;
227 struct vattr *vap = &va;
228 struct vnode *vp = NULL;
229 struct mount *mp = NULL;
232 int error = 0, rdonly;
233 struct nfsm_info info;
235 info.mrep = nfsd->nd_mrep;
236 info.md = nfsd->nd_md;
237 info.dpos = nfsd->nd_dpos;
240 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
241 fhp = &nfh.fh_generic;
242 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
243 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
244 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
246 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
250 error = VOP_GETATTR(vp, vap);
253 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
254 NFSX_FATTR(nfsd->nd_flag & ND_NFSV3), &error));
259 fp = nfsm_build(&info, NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
260 nfsm_srvfattr(nfsd, vap, fp);
271 * nfs setattr service
274 nfsrv_setattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
275 struct thread *td, struct mbuf **mrq)
277 struct sockaddr *nam = nfsd->nd_nam;
278 struct ucred *cred = &nfsd->nd_cr;
279 struct vattr va, preat;
280 struct vattr *vap = &va;
281 struct nfsv2_sattr *sp;
282 struct nfs_fattr *fp;
283 struct vnode *vp = NULL;
284 struct mount *mp = NULL;
288 int error = 0, rdonly, preat_ret = 1, postat_ret = 1;
290 struct timespec guard;
291 struct nfsm_info info;
293 info.mrep = nfsd->nd_mrep;
295 info.md = nfsd->nd_md;
296 info.dpos = nfsd->nd_dpos;
297 info.v3 = (nfsd->nd_flag & ND_NFSV3);
299 guard.tv_sec = 0; /* fix compiler warning */
302 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
303 fhp = &nfh.fh_generic;
304 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
307 ERROROUT(nfsm_srvsattr(&info, vap));
308 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
309 gcheck = fxdr_unsigned(int, *tl);
311 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
312 fxdr_nfsv3time(tl, &guard);
315 NULLOUT(sp = nfsm_dissect(&info, NFSX_V2SATTR));
317 * Nah nah nah nah na nah
318 * There is a bug in the Sun client that puts 0xffff in the mode
319 * field of sattr when it should put in 0xffffffff. The u_short
320 * doesn't sign extend.
321 * --> check the low order 2 bytes for 0xffff
323 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
324 vap->va_mode = nfstov_mode(sp->sa_mode);
325 if (sp->sa_uid != nfs_xdrneg1)
326 vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
327 if (sp->sa_gid != nfs_xdrneg1)
328 vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
329 if (sp->sa_size != nfs_xdrneg1)
330 vap->va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
331 if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
333 fxdr_nfsv2time(&sp->sa_atime, &vap->va_atime);
335 vap->va_atime.tv_sec =
336 fxdr_unsigned(int32_t, sp->sa_atime.nfsv2_sec);
337 vap->va_atime.tv_nsec = 0;
340 if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
341 fxdr_nfsv2time(&sp->sa_mtime, &vap->va_mtime);
346 * Now that we have all the fields, lets do it.
348 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam, &rdonly,
349 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
351 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
352 2 * NFSX_UNSIGNED, &error));
353 nfsm_srvwcc_data(&info, nfsd, preat_ret, &preat,
360 * vp now an active resource, pay careful attention to cleanup
364 error = preat_ret = VOP_GETATTR(vp, &preat);
365 if (!error && gcheck &&
366 (preat.va_ctime.tv_sec != guard.tv_sec ||
367 preat.va_ctime.tv_nsec != guard.tv_nsec))
368 error = NFSERR_NOT_SYNC;
372 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
373 NFSX_WCCDATA(info.v3), &error));
374 nfsm_srvwcc_data(&info, nfsd, preat_ret, &preat,
382 * If the size is being changed write acces is required, otherwise
383 * just check for a read only file system.
385 if (vap->va_size == ((u_quad_t)((quad_t) -1))) {
386 if (rdonly || (mp->mnt_flag & MNT_RDONLY)) {
391 if (vp->v_type == VDIR) {
394 } else if ((error = nfsrv_access(mp, vp, VWRITE, cred, rdonly,
399 error = VOP_SETATTR(vp, vap, cred);
400 postat_ret = VOP_GETATTR(vp, vap);
406 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
407 NFSX_WCCORFATTR(info.v3), &error));
409 nfsm_srvwcc_data(&info, nfsd, preat_ret, &preat,
414 fp = nfsm_build(&info, NFSX_V2FATTR);
415 nfsm_srvfattr(nfsd, vap, fp);
430 nfsrv_lookup(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
431 struct thread *td, struct mbuf **mrq)
433 struct sockaddr *nam = nfsd->nd_nam;
434 struct ucred *cred = &nfsd->nd_cr;
435 struct nfs_fattr *fp;
436 struct nlookupdata nd;
442 int error = 0, len, dirattr_ret = 1;
444 struct vattr va, dirattr, *vap = &va;
445 struct nfsm_info info;
447 info.mrep = nfsd->nd_mrep;
449 info.md = nfsd->nd_md;
450 info.dpos = nfsd->nd_dpos;
451 info.v3 = (nfsd->nd_flag & ND_NFSV3);
453 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
458 fhp = &nfh.fh_generic;
459 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
460 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
462 pubflag = nfs_ispublicfh(fhp);
464 error = nfs_namei(&nd, cred, 0, NULL, &vp,
465 fhp, len, slp, nam, &info.md, &info.dpos,
466 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), pubflag);
469 * namei failure, only dirp to cleanup. Clear out garbarge from
470 * structure in case macros jump to nfsmout.
476 dirattr_ret = VOP_GETATTR(dirp, &dirattr);
480 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
481 NFSX_POSTOPATTR(info.v3), &error));
482 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
488 * Locate index file for public filehandle
490 * error is 0 on entry and 0 on exit from this block.
494 if (vp->v_type == VDIR && nfs_pub.np_index != NULL) {
496 * Setup call to lookup() to see if we can find
497 * the index file. Arguably, this doesn't belong
498 * in a kernel.. Ugh. If an error occurs, do not
499 * try to install an index file and then clear the
502 * When we replace nd with ind and redirect ndp,
503 * maintenance of ni_startdir and ni_vp shift to
504 * ind and we have to clean them up in the old nd.
505 * However, the cnd resource continues to be maintained
506 * via the original nd. Confused? You aren't alone!
509 cache_copy(&nd.nl_nch, &nch);
511 error = nlookup_init_raw(&nd, nfs_pub.np_index,
512 UIO_SYSSPACE, 0, cred, &nch);
515 error = nlookup(&nd);
519 * Found an index file. Get rid of
520 * the old references. transfer vp and
521 * load up the new vp. Fortunately we do
522 * not have to deal with dvp, that would be
529 error = cache_vget(&nd.nl_nch, nd.nl_cred,
531 KKASSERT(error == 0);
536 * If the public filehandle was used, check that this lookup
537 * didn't result in a filehandle outside the publicly exported
538 * filesystem. We clear the poor vp here to avoid lockups due
542 if (vp->v_mount != nfs_pub.np_mount) {
551 dirattr_ret = VOP_GETATTR(dirp, &dirattr);
557 * Resources at this point:
558 * ndp->ni_vp may not be NULL
563 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
564 NFSX_POSTOPATTR(info.v3), &error));
565 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
571 * Clear out some resources prior to potentially blocking. This
572 * is not as critical as ni_dvp resources in other routines, but
578 * Get underlying attribute, then release remaining resources ( for
579 * the same potential blocking reason ) and reply.
581 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
582 error = VFS_VPTOFH(vp, &fhp->fh_fid);
584 error = VOP_GETATTR(vp, vap);
588 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
589 NFSX_SRVFH(info.v3) +
590 NFSX_POSTOPORFATTR(info.v3) +
591 NFSX_POSTOPATTR(info.v3),
594 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
598 nfsm_srvfhtom(&info, fhp);
600 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
601 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
603 fp = nfsm_build(&info, NFSX_V2FATTR);
604 nfsm_srvfattr(nfsd, vap, fp);
611 nlookup_done(&nd); /* may be called twice */
618 * nfs readlink service
621 nfsrv_readlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
622 struct thread *td, struct mbuf **mrq)
624 struct sockaddr *nam = nfsd->nd_nam;
625 struct ucred *cred = &nfsd->nd_cr;
626 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
627 struct iovec *ivp = iv;
629 int error = 0, rdonly, i, tlen, len, getret;
630 struct mbuf *mp1, *mp2, *mp3;
631 struct vnode *vp = NULL;
632 struct mount *mp = NULL;
636 struct uio io, *uiop = &io;
637 struct nfsm_info info;
639 info.mrep = nfsd->nd_mrep;
641 info.md = nfsd->nd_md;
642 info.dpos = nfsd->nd_dpos;
643 info.v3 = (nfsd->nd_flag & ND_NFSV3);
645 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
650 fhp = &nfh.fh_generic;
651 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
654 while (len < NFS_MAXPATHLEN) {
655 mp1 = m_getcl(MB_WAIT, MT_DATA, 0);
656 mp1->m_len = MCLBYTES;
663 if ((len + mp1->m_len) > NFS_MAXPATHLEN) {
664 mp1->m_len = NFS_MAXPATHLEN-len;
665 len = NFS_MAXPATHLEN;
668 ivp->iov_base = mtod(mp1, caddr_t);
669 ivp->iov_len = mp1->m_len;
674 uiop->uio_iovcnt = i;
675 uiop->uio_offset = 0;
676 uiop->uio_resid = len;
677 uiop->uio_rw = UIO_READ;
678 uiop->uio_segflg = UIO_SYSSPACE;
680 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
681 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
683 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
684 2 * NFSX_UNSIGNED, &error));
685 nfsm_srvpostop_attr(&info, nfsd, 1, NULL);
689 if (vp->v_type != VLNK) {
696 error = VOP_READLINK(vp, uiop, cred);
698 getret = VOP_GETATTR(vp, &attr);
701 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
702 NFSX_POSTOPATTR(info.v3) + NFSX_UNSIGNED,
705 nfsm_srvpostop_attr(&info, nfsd, getret, &attr);
711 if (uiop->uio_resid > 0) {
712 len -= uiop->uio_resid;
713 tlen = nfsm_rndup(len);
714 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
716 tl = nfsm_build(&info, NFSX_UNSIGNED);
717 *tl = txdr_unsigned(len);
718 info.mb->m_next = mp3;
733 nfsrv_read(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
734 struct thread *td, struct mbuf **mrq)
736 struct nfsm_info info;
737 struct sockaddr *nam = nfsd->nd_nam;
738 struct ucred *cred = &nfsd->nd_cr;
742 struct nfs_fattr *fp;
746 int error = 0, rdonly, cnt, len, left, siz, tlen, getret;
748 struct vnode *vp = NULL;
749 struct mount *mp = NULL;
752 struct uio io, *uiop = &io;
753 struct vattr va, *vap = &va;
758 info.mrep = nfsd->nd_mrep;
760 info.md = nfsd->nd_md;
761 info.dpos = nfsd->nd_dpos;
762 info.v3 = (nfsd->nd_flag & ND_NFSV3);
764 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
765 fhp = &nfh.fh_generic;
766 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
768 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
769 off = fxdr_hyper(tl);
771 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
772 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
774 NEGREPLYOUT(reqlen = nfsm_srvstrsiz(&info,
775 NFS_SRVMAXDATA(nfsd), &error));
778 * Reference vp. If an error occurs, vp will be invalid, but we
779 * have to NULL it just in case. The macros might goto nfsmout
783 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
784 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
787 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
788 2 * NFSX_UNSIGNED, &error));
789 nfsm_srvpostop_attr(&info, nfsd, 1, NULL);
794 if (vp->v_type != VREG) {
798 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
801 if ((error = nfsrv_access(mp, vp, VREAD, cred, rdonly, td, 1)) != 0)
802 error = nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 1);
804 getret = VOP_GETATTR(vp, vap);
810 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
811 NFSX_POSTOPATTR(info.v3), &error));
812 nfsm_srvpostop_attr(&info, nfsd, getret, vap);
818 * Calculate byte count to read
821 if (off >= vap->va_size)
823 else if ((off + reqlen) > vap->va_size)
824 cnt = vap->va_size - off;
829 * Calculate seqcount for heuristic
837 * Locate best candidate
840 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
844 if (nfsheur[hi].nh_vp == vp) {
848 if (nfsheur[hi].nh_use > 0)
849 --nfsheur[hi].nh_use;
850 hi = (hi + 1) % NUM_HEURISTIC;
851 if (nfsheur[hi].nh_use < nh->nh_use)
855 if (nh->nh_vp != vp) {
858 nh->nh_use = NHUSE_INIT;
866 * Calculate heuristic
869 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
870 if (++nh->nh_seqcount > IO_SEQMAX)
871 nh->nh_seqcount = IO_SEQMAX;
872 } else if (nh->nh_seqcount > 1) {
877 nh->nh_use += NHUSE_INC;
878 if (nh->nh_use > NHUSE_MAX)
879 nh->nh_use = NHUSE_MAX;
880 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
883 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
884 NFSX_POSTOPORFATTR(info.v3) +
885 3 * NFSX_UNSIGNED + nfsm_rndup(cnt),
888 tl = nfsm_build(&info, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
890 fp = (struct nfs_fattr *)tl;
891 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
893 tl = nfsm_build(&info, NFSX_V2FATTR + NFSX_UNSIGNED);
894 fp = (struct nfs_fattr *)tl;
895 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
897 len = left = nfsm_rndup(cnt);
900 * Generate the mbuf list with the uio_iov ref. to it.
905 siz = min(M_TRAILINGSPACE(m), left);
911 m = m_getcl(MB_WAIT, MT_DATA, 0);
917 iv = kmalloc(i * sizeof(struct iovec), M_TEMP, M_WAITOK);
918 uiop->uio_iov = iv2 = iv;
924 panic("nfsrv_read iov");
925 siz = min(M_TRAILINGSPACE(m), left);
927 iv->iov_base = mtod(m, caddr_t) + m->m_len;
936 uiop->uio_iovcnt = i;
937 uiop->uio_offset = off;
938 uiop->uio_resid = len;
939 uiop->uio_rw = UIO_READ;
940 uiop->uio_segflg = UIO_SYSSPACE;
941 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
942 off = uiop->uio_offset;
944 kfree((caddr_t)iv2, M_TEMP);
945 if (error || (getret = VOP_GETATTR(vp, vap))) {
952 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
953 NFSX_POSTOPATTR(info.v3),
955 nfsm_srvpostop_attr(&info, nfsd, getret, vap);
964 nfsm_srvfattr(nfsd, vap, fp);
965 tlen = len - uiop->uio_resid;
966 cnt = cnt < tlen ? cnt : tlen;
967 tlen = nfsm_rndup(cnt);
968 if (len != tlen || tlen != cnt)
969 nfsm_adj(info.mb, len - tlen, tlen - cnt);
971 *tl++ = txdr_unsigned(cnt);
977 *tl = txdr_unsigned(cnt);
989 nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
990 struct thread *td, struct mbuf **mrq)
992 struct sockaddr *nam = nfsd->nd_nam;
993 struct ucred *cred = &nfsd->nd_cr;
997 struct nfs_fattr *fp;
999 struct vattr va, forat;
1000 struct vattr *vap = &va;
1002 int error = 0, rdonly, len, forat_ret = 1;
1003 int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
1004 int stable = NFSV3WRITE_FILESYNC;
1005 struct vnode *vp = NULL;
1006 struct mount *mp = NULL;
1009 struct uio io, *uiop = &io;
1010 struct nfsm_info info;
1013 info.mrep = nfsd->nd_mrep;
1015 info.md = nfsd->nd_md;
1016 info.dpos = nfsd->nd_dpos;
1017 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1019 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1020 if (info.mrep == NULL) {
1024 fhp = &nfh.fh_generic;
1025 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
1027 NULLOUT(tl = nfsm_dissect(&info, 5 * NFSX_UNSIGNED));
1028 off = fxdr_hyper(tl);
1030 stable = fxdr_unsigned(int, *tl++);
1032 NULLOUT(tl = nfsm_dissect(&info, 4 * NFSX_UNSIGNED));
1033 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1036 stable = NFSV3WRITE_UNSTABLE;
1038 retlen = len = fxdr_unsigned(int32_t, *tl);
1042 * For NFS Version 2, it is not obvious what a write of zero length
1043 * should do, but I might as well be consistent with Version 3,
1044 * which is to return ok so long as there are no permission problems.
1050 if (mp1 == info.md) {
1052 adjust = info.dpos - mtod(mp1, caddr_t);
1053 mp1->m_len -= adjust;
1054 if (mp1->m_len > 0 && adjust > 0)
1055 mp1->m_data += adjust;
1059 else if (mp1->m_len > 0) {
1062 mp1->m_len -= (i - len);
1071 if (len > NFS_MAXDATA || len < 0 || i < len) {
1073 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1074 2 * NFSX_UNSIGNED, &error));
1075 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1080 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
1081 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1084 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1085 2 * NFSX_UNSIGNED, &error));
1086 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1092 forat_ret = VOP_GETATTR(vp, &forat);
1093 if (vp->v_type != VREG) {
1097 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1100 error = nfsrv_access(mp, vp, VWRITE, cred, rdonly, td, 1);
1105 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1106 NFSX_WCCDATA(info.v3), &error));
1107 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1114 ivp = kmalloc(cnt * sizeof(struct iovec), M_TEMP, M_WAITOK);
1115 uiop->uio_iov = iv = ivp;
1116 uiop->uio_iovcnt = cnt;
1119 if (mp1->m_len > 0) {
1120 ivp->iov_base = mtod(mp1, caddr_t);
1121 ivp->iov_len = mp1->m_len;
1129 * The IO_METASYNC flag indicates that all metadata (and not just
1130 * enough to ensure data integrity) mus be written to stable storage
1132 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1134 if (stable == NFSV3WRITE_UNSTABLE)
1135 ioflags = IO_NODELOCKED;
1136 else if (stable == NFSV3WRITE_DATASYNC)
1137 ioflags = (IO_SYNC | IO_NODELOCKED);
1139 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1140 uiop->uio_resid = len;
1141 uiop->uio_rw = UIO_WRITE;
1142 uiop->uio_segflg = UIO_SYSSPACE;
1143 uiop->uio_td = NULL;
1144 uiop->uio_offset = off;
1145 error = VOP_WRITE(vp, uiop, ioflags, cred);
1146 nfsstats.srvvop_writes++;
1147 kfree((caddr_t)iv, M_TEMP);
1149 aftat_ret = VOP_GETATTR(vp, vap);
1154 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1155 NFSX_PREOPATTR(info.v3) +
1156 NFSX_POSTOPORFATTR(info.v3) +
1157 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(info.v3),
1160 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1166 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1167 *tl++ = txdr_unsigned(retlen);
1169 * If nfs_async is set, then pretend the write was FILESYNC.
1171 if (stable == NFSV3WRITE_UNSTABLE && !nfs_async)
1172 *tl++ = txdr_unsigned(stable);
1174 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
1176 * Actually, there is no need to txdr these fields,
1177 * but it may make the values more human readable,
1178 * for debugging purposes.
1180 if (nfsver.tv_sec == 0)
1182 *tl++ = txdr_unsigned(nfsver.tv_sec);
1183 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1185 fp = nfsm_build(&info, NFSX_V2FATTR);
1186 nfsm_srvfattr(nfsd, vap, fp);
1196 * NFS write service with write gathering support. Called when
1197 * nfsrvw_procrastinate > 0.
1198 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1199 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1203 nfsrv_writegather(struct nfsrv_descript **ndp, struct nfssvc_sock *slp,
1204 struct thread *td, struct mbuf **mrq)
1207 struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1208 struct nfs_fattr *fp;
1211 struct nfsrvw_delayhash *wpp;
1213 struct vattr va, forat;
1215 int error = 0, rdonly, len, forat_ret = 1;
1216 int ioflags, aftat_ret = 1, adjust, zeroing;
1218 struct vnode *vp = NULL;
1219 struct mount *mp = NULL;
1220 struct uio io, *uiop = &io;
1222 struct nfsm_info info;
1226 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1234 info.mrep = nfsd->nd_mrep;
1236 info.md = nfsd->nd_md;
1237 info.dpos = nfsd->nd_dpos;
1238 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1239 cred = &nfsd->nd_cr;
1240 LIST_INIT(&nfsd->nd_coalesce);
1241 nfsd->nd_mreq = NULL;
1242 nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1243 cur_usec = nfs_curusec();
1244 nfsd->nd_time = cur_usec +
1245 (info.v3 ? nfsrvw_procrastinate_v3 : nfsrvw_procrastinate);
1248 * Now, get the write header..
1250 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, &nfsd->nd_fh, &error));
1252 NULLOUT(tl = nfsm_dissect(&info, 5 * NFSX_UNSIGNED));
1253 nfsd->nd_off = fxdr_hyper(tl);
1255 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1257 NULLOUT(tl = nfsm_dissect(&info, 4 * NFSX_UNSIGNED));
1258 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1261 nfsd->nd_stable = NFSV3WRITE_UNSTABLE;
1263 len = fxdr_unsigned(int32_t, *tl);
1265 nfsd->nd_eoff = nfsd->nd_off + len;
1268 * Trim the header out of the mbuf list and trim off any trailing
1269 * junk so that the mbuf list has only the write data.
1275 if (mp1 == info.md) {
1277 adjust = info.dpos - mtod(mp1, caddr_t);
1278 mp1->m_len -= adjust;
1279 if (mp1->m_len > 0 && adjust > 0)
1280 mp1->m_data += adjust;
1287 mp1->m_len -= (i - len);
1293 if (len > NFS_MAXDATA || len < 0 || i < len) {
1298 nfsm_writereply(&info, nfsd, slp, error, 2 * NFSX_UNSIGNED);
1300 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1303 nfsd->nd_mreq = info.mreq;
1304 nfsd->nd_mrep = NULL;
1309 * Add this entry to the hash and time queues.
1312 wp = slp->ns_tq.lh_first;
1313 while (wp && wp->nd_time < nfsd->nd_time) {
1315 wp = wp->nd_tq.le_next;
1317 NFS_DPF(WG, ("Q%03x", nfsd->nd_retxid & 0xfff));
1319 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1321 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1323 if (nfsd->nd_mrep) {
1324 wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
1328 bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1330 wp = wp->nd_hash.le_next;
1332 while (wp && wp->nd_off < nfsd->nd_off &&
1333 !bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1335 wp = wp->nd_hash.le_next;
1338 LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1341 * Search the hash list for overlapping entries and
1344 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1345 wp = nfsd->nd_hash.le_next;
1346 if (NFSW_SAMECRED(owp, nfsd))
1347 nfsrvw_coalesce(owp, nfsd);
1350 LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1356 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1357 * and generate the associated reply mbuf list(s).
1360 cur_usec = nfs_curusec();
1361 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = owp) {
1362 owp = nfsd->nd_tq.le_next;
1363 if (nfsd->nd_time > cur_usec)
1367 NFS_DPF(WG, ("P%03x", nfsd->nd_retxid & 0xfff));
1368 LIST_REMOVE(nfsd, nd_tq);
1369 LIST_REMOVE(nfsd, nd_hash);
1370 info.mrep = nfsd->nd_mrep;
1372 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1373 nfsd->nd_mrep = NULL;
1374 cred = &nfsd->nd_cr;
1375 forat_ret = aftat_ret = 1;
1376 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &mp, &vp, cred, slp,
1377 nfsd->nd_nam, &rdonly,
1378 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1381 forat_ret = VOP_GETATTR(vp, &forat);
1382 if (vp->v_type != VREG) {
1386 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1392 error = nfsrv_access(mp, vp, VWRITE, cred, rdonly, td, 1);
1395 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1396 ioflags = IO_NODELOCKED;
1397 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1398 ioflags = (IO_SYNC | IO_NODELOCKED);
1400 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1401 uiop->uio_rw = UIO_WRITE;
1402 uiop->uio_segflg = UIO_SYSSPACE;
1403 uiop->uio_td = NULL;
1404 uiop->uio_offset = nfsd->nd_off;
1405 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1406 if (uiop->uio_resid > 0) {
1414 uiop->uio_iovcnt = i;
1415 iov = kmalloc(i * sizeof(struct iovec), M_TEMP, M_WAITOK);
1416 uiop->uio_iov = ivp = iov;
1419 if (mp1->m_len > 0) {
1420 ivp->iov_base = mtod(mp1, caddr_t);
1421 ivp->iov_len = mp1->m_len;
1427 error = VOP_WRITE(vp, uiop, ioflags, cred);
1428 nfsstats.srvvop_writes++;
1430 kfree((caddr_t)iov, M_TEMP);
1435 aftat_ret = VOP_GETATTR(vp, &va);
1441 * Loop around generating replies for all write rpcs that have
1442 * now been completed.
1446 NFS_DPF(WG, ("R%03x", nfsd->nd_retxid & 0xfff));
1448 nfsm_writereply(&info, nfsd, slp, error,
1449 NFSX_WCCDATA(info.v3));
1451 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1455 nfsm_writereply(&info, nfsd, slp, error,
1456 NFSX_PREOPATTR(info.v3) +
1457 NFSX_POSTOPORFATTR(info.v3) +
1459 NFSX_WRITEVERF(info.v3));
1461 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1463 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1464 *tl++ = txdr_unsigned(nfsd->nd_len);
1465 *tl++ = txdr_unsigned(swp->nd_stable);
1467 * Actually, there is no need to txdr these fields,
1468 * but it may make the values more human readable,
1469 * for debugging purposes.
1471 if (nfsver.tv_sec == 0)
1473 *tl++ = txdr_unsigned(nfsver.tv_sec);
1474 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1476 fp = nfsm_build(&info, NFSX_V2FATTR);
1477 nfsm_srvfattr(nfsd, &va, fp);
1480 nfsd->nd_mreq = info.mreq;
1482 panic("nfsrv_write: nd_mrep not free");
1485 * Done. Put it at the head of the timer queue so that
1486 * the final phase can return the reply.
1490 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1492 nfsd = swp->nd_coalesce.lh_first;
1494 LIST_REMOVE(nfsd, nd_tq);
1498 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1503 * Search for a reply to return.
1505 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = nfsd->nd_tq.le_next) {
1506 if (nfsd->nd_mreq) {
1507 NFS_DPF(WG, ("X%03x", nfsd->nd_retxid & 0xfff));
1508 LIST_REMOVE(nfsd, nd_tq);
1514 *mrq = nfsd->nd_mreq;
1523 * Coalesce the write request nfsd into owp. To do this we must:
1524 * - remove nfsd from the queues
1525 * - merge nfsd->nd_mrep into owp->nd_mrep
1526 * - update the nd_eoff and nd_stable for owp
1527 * - put nfsd on owp's nd_coalesce list
1528 * NB: Must be called at splsoftclock().
1531 nfsrvw_coalesce(struct nfsrv_descript *owp, struct nfsrv_descript *nfsd)
1535 struct nfsrv_descript *p;
1537 NFS_DPF(WG, ("C%03x-%03x",
1538 nfsd->nd_retxid & 0xfff, owp->nd_retxid & 0xfff));
1539 LIST_REMOVE(nfsd, nd_hash);
1540 LIST_REMOVE(nfsd, nd_tq);
1541 if (owp->nd_eoff < nfsd->nd_eoff) {
1542 overlap = owp->nd_eoff - nfsd->nd_off;
1544 panic("nfsrv_coalesce: bad off");
1546 m_adj(nfsd->nd_mrep, overlap);
1550 mp1->m_next = nfsd->nd_mrep;
1551 owp->nd_eoff = nfsd->nd_eoff;
1553 m_freem(nfsd->nd_mrep);
1554 nfsd->nd_mrep = NULL;
1555 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1556 owp->nd_stable = NFSV3WRITE_FILESYNC;
1557 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1558 owp->nd_stable == NFSV3WRITE_UNSTABLE)
1559 owp->nd_stable = NFSV3WRITE_DATASYNC;
1560 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1563 * If nfsd had anything else coalesced into it, transfer them
1564 * to owp, otherwise their replies will never get sent.
1566 for (p = nfsd->nd_coalesce.lh_first; p;
1567 p = nfsd->nd_coalesce.lh_first) {
1568 LIST_REMOVE(p, nd_tq);
1569 LIST_INSERT_HEAD(&owp->nd_coalesce, p, nd_tq);
1574 * nfs create service
1575 * now does a truncate to 0 length via. setattr if it already exists
1578 nfsrv_create(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1579 struct thread *td, struct mbuf **mrq)
1581 struct sockaddr *nam = nfsd->nd_nam;
1582 struct ucred *cred = &nfsd->nd_cr;
1583 struct nfs_fattr *fp;
1584 struct vattr va, dirfor, diraft;
1585 struct vattr *vap = &va;
1586 struct nfsv2_sattr *sp;
1588 struct nlookupdata nd;
1589 int error = 0, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1590 udev_t rdev = NOUDEV;
1592 int how, exclusive_flag = 0;
1600 u_char cverf[NFSX_V3CREATEVERF];
1601 struct nfsm_info info;
1603 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1609 info.mrep = nfsd->nd_mrep;
1611 info.md = nfsd->nd_md;
1612 info.dpos = nfsd->nd_dpos;
1613 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1615 fhp = &nfh.fh_generic;
1616 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
1617 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
1620 * Call namei and do initial cleanup to get a few things
1621 * out of the way. If we get an initial error we cleanup
1622 * and return here to avoid special-casing the invalid nd
1623 * structure through the rest of the case. dirp may be
1624 * set even if an error occurs, but the nd structure will not
1625 * be valid at all if an error occurs so we have to invalidate it
1626 * prior to calling nfsm_reply ( which might goto nfsmout ).
1628 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
1629 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
1630 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1631 mp = vfs_getvfs(&fhp->fh_fsid);
1635 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
1642 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1643 NFSX_WCCDATA(info.v3), &error));
1644 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1645 diraft_ret, &diraft);
1651 * No error. Continue. State:
1654 * vp may be valid or NULL if the target does not
1658 * The error state is set through the code and we may also do some
1659 * opportunistic releasing of vnodes to avoid holding locks through
1660 * NFS I/O. The cleanup at the end is a catch-all
1665 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
1666 how = fxdr_unsigned(int, *tl);
1668 case NFSV3CREATE_GUARDED:
1674 case NFSV3CREATE_UNCHECKED:
1675 ERROROUT(nfsm_srvsattr(&info, vap));
1677 case NFSV3CREATE_EXCLUSIVE:
1678 NULLOUT(cp = nfsm_dissect(&info, NFSX_V3CREATEVERF));
1679 bcopy(cp, cverf, NFSX_V3CREATEVERF);
1683 vap->va_type = VREG;
1685 NULLOUT(sp = nfsm_dissect(&info, NFSX_V2SATTR));
1686 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1687 if (vap->va_type == VNON)
1688 vap->va_type = VREG;
1689 vap->va_mode = nfstov_mode(sp->sa_mode);
1690 switch (vap->va_type) {
1692 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1694 vap->va_size = (u_quad_t)tsize;
1699 rdev = fxdr_unsigned(long, sp->sa_size);
1707 * Iff doesn't exist, create it
1708 * otherwise just truncate to 0 length
1709 * should I set the mode too ?
1711 * The only possible error we can have at this point is EEXIST.
1712 * nd.ni_vp will also be non-NULL in that case.
1715 if (vap->va_mode == (mode_t)VNOVAL)
1717 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1719 error = VOP_NCREATE(&nd.nl_nch, dvp, &vp,
1724 if (exclusive_flag) {
1727 bcopy(cverf, (caddr_t)&vap->va_atime,
1729 error = VOP_SETATTR(vp, vap, cred);
1733 vap->va_type == VCHR ||
1734 vap->va_type == VBLK ||
1735 vap->va_type == VFIFO
1738 * Handle SysV FIFO node special cases. All other
1739 * devices require super user to access.
1741 if (vap->va_type == VCHR && rdev == 0xffffffff)
1742 vap->va_type = VFIFO;
1743 if (vap->va_type != VFIFO &&
1744 (error = priv_check_cred(cred, PRIV_ROOT, 0))) {
1747 vap->va_rmajor = umajor(rdev);
1748 vap->va_rminor = uminor(rdev);
1751 error = VOP_NMKNOD(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1758 * XXX what is this junk supposed to do ?
1765 * release dvp prior to lookup
1773 * Even though LOCKPARENT was cleared, ni_dvp may
1776 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
1777 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT);
1778 nd.ni_cnd.cn_td = td;
1779 nd.ni_cnd.cn_cred = cred;
1781 error = lookup(&nd);
1785 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1787 /* fall through on certain errors */
1789 nfsrv_object_create(nd.ni_vp);
1790 if (nd.ni_cnd.cn_flags & CNP_ISSYMLINK) {
1799 if (vap->va_size != -1) {
1800 error = nfsrv_access(mp, vp, VWRITE, cred,
1801 (nd.nl_flags & NLC_NFS_RDONLY), td, 0);
1803 tempsize = vap->va_size;
1805 vap->va_size = tempsize;
1806 error = VOP_SETATTR(vp, vap, cred);
1812 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
1813 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1815 error = VOP_GETATTR(vp, vap);
1818 if (exclusive_flag && !error &&
1819 bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1821 diraft_ret = VOP_GETATTR(dirp, &diraft);
1825 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1826 NFSX_SRVFH(info.v3) + NFSX_FATTR(info.v3) +
1827 NFSX_WCCDATA(info.v3),
1831 nfsm_srvpostop_fh(&info, fhp);
1832 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
1834 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1835 diraft_ret, &diraft);
1838 nfsm_srvfhtom(&info, fhp);
1839 fp = nfsm_build(&info, NFSX_V2FATTR);
1840 nfsm_srvfattr(nfsd, vap, fp);
1845 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
1866 * nfs v3 mknod service
1869 nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1870 struct thread *td, struct mbuf **mrq)
1872 struct sockaddr *nam = nfsd->nd_nam;
1873 struct ucred *cred = &nfsd->nd_cr;
1874 struct vattr va, dirfor, diraft;
1875 struct vattr *vap = &va;
1877 struct nlookupdata nd;
1878 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
1885 struct nfsm_info info;
1887 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1893 info.mrep = nfsd->nd_mrep;
1895 info.md = nfsd->nd_md;
1896 info.dpos = nfsd->nd_dpos;
1898 fhp = &nfh.fh_generic;
1899 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
1900 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
1903 * Handle nfs_namei() call. If an error occurs, the nd structure
1904 * is not valid. However, nfsm_*() routines may still jump to
1908 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
1909 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
1910 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1912 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
1914 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1915 NFSX_WCCDATA(1), &error));
1916 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1917 diraft_ret, &diraft);
1921 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
1922 vtyp = nfsv3tov_type(*tl);
1923 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1924 error = NFSERR_BADTYPE;
1928 ERROROUT(nfsm_srvsattr(&info, vap));
1929 if (vtyp == VCHR || vtyp == VBLK) {
1930 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
1931 vap->va_rmajor = fxdr_unsigned(u_int32_t, *tl++);
1932 vap->va_rminor = fxdr_unsigned(u_int32_t, *tl);
1936 * Iff doesn't exist, create it.
1942 vap->va_type = vtyp;
1943 if (vap->va_mode == (mode_t)VNOVAL)
1945 if (vtyp == VSOCK) {
1947 error = VOP_NCREATE(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1951 if (vtyp != VFIFO && (error = priv_check_cred(cred, PRIV_ROOT, 0)))
1955 error = VOP_NMKNOD(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1963 * send response, cleanup, return.
1975 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
1976 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1978 error = VOP_GETATTR(vp, vap);
1984 diraft_ret = VOP_GETATTR(dirp, &diraft);
1989 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1990 NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) +
1991 NFSX_WCCDATA(1), &error));
1993 nfsm_srvpostop_fh(&info, fhp);
1994 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
1996 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1997 diraft_ret, &diraft);
2017 * nfs remove service
2020 nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2021 struct thread *td, struct mbuf **mrq)
2023 struct sockaddr *nam = nfsd->nd_nam;
2024 struct ucred *cred = &nfsd->nd_cr;
2025 struct nlookupdata nd;
2026 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2030 struct vattr dirfor, diraft;
2033 struct nfsm_info info;
2035 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2041 info.mrep = nfsd->nd_mrep;
2043 info.md = nfsd->nd_md;
2044 info.dpos = nfsd->nd_dpos;
2045 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2047 fhp = &nfh.fh_generic;
2048 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2049 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2051 error = nfs_namei(&nd, cred, NLC_DELETE, &dvp, &vp,
2052 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2053 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2056 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2059 if (vp->v_type == VDIR) {
2060 error = EPERM; /* POSIX */
2064 * The root of a mounted filesystem cannot be deleted.
2066 if (vp->v_flag & VROOT) {
2078 error = VOP_NREMOVE(&nd.nl_nch, dvp, nd.nl_cred);
2083 if (dirp && info.v3)
2084 diraft_ret = VOP_GETATTR(dirp, &diraft);
2085 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_WCCDATA(info.v3), &error));
2087 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2088 diraft_ret, &diraft);
2108 * nfs rename service
2111 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2112 struct thread *td, struct mbuf **mrq)
2114 struct sockaddr *nam = nfsd->nd_nam;
2115 struct ucred *cred = &nfsd->nd_cr;
2116 int error = 0, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2117 int tdirfor_ret = 1, tdiraft_ret = 1;
2118 struct nlookupdata fromnd, tond;
2119 struct vnode *fvp, *fdirp, *fdvp;
2120 struct vnode *tvp, *tdirp, *tdvp;
2121 struct namecache *ncp;
2122 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2124 fhandle_t *ffhp, *tfhp;
2126 struct nfsm_info info;
2128 info.mrep = nfsd->nd_mrep;
2130 info.md = nfsd->nd_md;
2131 info.dpos = nfsd->nd_dpos;
2132 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2134 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2138 ffhp = &fnfh.fh_generic;
2139 tfhp = &tnfh.fh_generic;
2142 * Clear fields incase goto nfsmout occurs from macro.
2145 nlookup_zero(&fromnd);
2146 nlookup_zero(&tond);
2150 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, ffhp, &error));
2151 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2154 * Remember our original uid so that we can reset cr_uid before
2155 * the second nfs_namei() call, in case it is remapped.
2157 saved_uid = cred->cr_uid;
2158 error = nfs_namei(&fromnd, cred, NLC_RENAME_SRC,
2160 ffhp, len, slp, nam, &info.md, &info.dpos, &fdirp,
2161 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2164 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor);
2167 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2168 2 * NFSX_WCCDATA(info.v3), &error));
2169 nfsm_srvwcc_data(&info, nfsd, fdirfor_ret, &fdirfor,
2170 fdiraft_ret, &fdiraft);
2171 nfsm_srvwcc_data(&info, nfsd, tdirfor_ret, &tdirfor,
2172 tdiraft_ret, &tdiraft);
2178 * We have to unlock the from ncp before we can safely lookup
2181 KKASSERT(fromnd.nl_flags & NLC_NCPISLOCKED);
2182 cache_unlock(&fromnd.nl_nch);
2183 fromnd.nl_flags &= ~NLC_NCPISLOCKED;
2184 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, tfhp, &error));
2185 NEGATIVEOUT(len2 = nfsm_strsiz(&info, NFS_MAXNAMLEN));
2186 cred->cr_uid = saved_uid;
2188 error = nfs_namei(&tond, cred, NLC_RENAME_DST, NULL, NULL,
2189 tfhp, len2, slp, nam, &info.md, &info.dpos, &tdirp,
2190 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2193 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor);
2201 if (cache_lock_nonblock(&fromnd.nl_nch) == 0) {
2202 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2203 } else if (fromnd.nl_nch.ncp > tond.nl_nch.ncp) {
2204 cache_lock(&fromnd.nl_nch);
2205 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2207 cache_unlock(&tond.nl_nch);
2208 cache_lock(&fromnd.nl_nch);
2209 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2210 cache_lock(&tond.nl_nch);
2211 cache_resolve(&tond.nl_nch, tond.nl_cred);
2213 fromnd.nl_flags |= NLC_NCPISLOCKED;
2215 fvp = fromnd.nl_nch.ncp->nc_vp;
2216 tvp = tond.nl_nch.ncp->nc_vp;
2219 * Set fdvp and tdvp. We haven't done all the topology checks
2220 * so these can wind up NULL (e.g. if either fvp or tvp is a mount
2221 * point). If we get through the checks these will be guarenteed
2224 * Holding the children ncp's should be sufficient to prevent
2225 * fdvp and tdvp ripouts.
2227 if (fromnd.nl_nch.ncp->nc_parent)
2228 fdvp = fromnd.nl_nch.ncp->nc_parent->nc_vp;
2231 if (tond.nl_nch.ncp->nc_parent)
2232 tdvp = tond.nl_nch.ncp->nc_parent->nc_vp;
2237 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2243 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2250 if (tvp->v_type == VDIR && (tond.nl_nch.ncp->nc_flag & NCF_ISMOUNTPT)) {
2258 if (fvp->v_type == VDIR && (fromnd.nl_nch.ncp->nc_flag & NCF_ISMOUNTPT)) {
2265 if (fromnd.nl_nch.mount != tond.nl_nch.mount) {
2272 if (fromnd.nl_nch.ncp == tond.nl_nch.ncp->nc_parent) {
2280 * You cannot rename a source into itself or a subdirectory of itself.
2281 * We check this by travsering the target directory upwards looking
2282 * for a match against the source.
2285 for (ncp = tond.nl_nch.ncp; ncp; ncp = ncp->nc_parent) {
2286 if (fromnd.nl_nch.ncp == ncp) {
2294 * If source is the same as the destination (that is the
2295 * same vnode with the same name in the same directory),
2296 * then there is nothing to do.
2298 if (fromnd.nl_nch.ncp == tond.nl_nch.ncp)
2303 * The VOP_NRENAME function releases all vnode references &
2304 * locks prior to returning so we need to clear the pointers
2305 * to bypass cleanup code later on.
2307 error = VOP_NRENAME(&fromnd.nl_nch, &tond.nl_nch,
2308 fdvp, tdvp, tond.nl_cred);
2317 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft);
2319 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft);
2320 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2321 2 * NFSX_WCCDATA(info.v3), &error));
2323 nfsm_srvwcc_data(&info, nfsd, fdirfor_ret, &fdirfor,
2324 fdiraft_ret, &fdiraft);
2325 nfsm_srvwcc_data(&info, nfsd, tdirfor_ret, &tdirfor,
2326 tdiraft_ret, &tdiraft);
2335 nlookup_done(&tond);
2338 nlookup_done(&fromnd);
2346 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2347 struct thread *td, struct mbuf **mrq)
2349 struct sockaddr *nam = nfsd->nd_nam;
2350 struct ucred *cred = &nfsd->nd_cr;
2351 struct nlookupdata nd;
2352 int error = 0, rdonly, len, dirfor_ret = 1, diraft_ret = 1;
2360 struct vattr dirfor, diraft, at;
2362 fhandle_t *fhp, *dfhp;
2363 struct nfsm_info info;
2365 info.mrep = nfsd->nd_mrep;
2367 info.md = nfsd->nd_md;
2368 info.dpos = nfsd->nd_dpos;
2369 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2371 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2373 dirp = dvp = vp = xp = NULL;
2376 fhp = &nfh.fh_generic;
2377 dfhp = &dnfh.fh_generic;
2378 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2379 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, dfhp, &error));
2380 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2382 error = nfsrv_fhtovp(fhp, FALSE, &xmp, &xp, cred, slp, nam,
2383 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2385 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2386 NFSX_POSTOPATTR(info.v3) +
2387 NFSX_WCCDATA(info.v3),
2389 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2390 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2391 diraft_ret, &diraft);
2396 if (xp->v_type == VDIR) {
2397 error = EPERM; /* POSIX */
2401 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2402 dfhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2403 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2406 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2415 if (xp->v_mount != dvp->v_mount)
2420 error = VOP_NLINK(&nd.nl_nch, dvp, xp, nd.nl_cred);
2428 getret = VOP_GETATTR(xp, &at);
2430 diraft_ret = VOP_GETATTR(dirp, &diraft);
2431 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2432 NFSX_POSTOPATTR(info.v3) + NFSX_WCCDATA(info.v3),
2435 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2436 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2437 diraft_ret, &diraft);
2461 * nfs symbolic link service
2464 nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2465 struct thread *td, struct mbuf **mrq)
2467 struct sockaddr *nam = nfsd->nd_nam;
2468 struct ucred *cred = &nfsd->nd_cr;
2469 struct vattr va, dirfor, diraft;
2470 struct nlookupdata nd;
2471 struct vattr *vap = &va;
2472 struct nfsv2_sattr *sp;
2473 char *pathcp = NULL;
2476 int error = 0, len, len2, dirfor_ret = 1, diraft_ret = 1;
2482 struct nfsm_info info;
2484 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2490 info.mrep = nfsd->nd_mrep;
2492 info.md = nfsd->nd_md;
2493 info.dpos = nfsd->nd_dpos;
2494 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2496 fhp = &nfh.fh_generic;
2497 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2498 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2500 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2501 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2502 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2505 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2512 ERROROUT(nfsm_srvsattr(&info, vap));
2514 NEGATIVEOUT(len2 = nfsm_strsiz(&info, NFS_MAXPATHLEN));
2515 pathcp = kmalloc(len2 + 1, M_TEMP, M_WAITOK);
2516 iv.iov_base = pathcp;
2518 io.uio_resid = len2;
2522 io.uio_segflg = UIO_SYSSPACE;
2523 io.uio_rw = UIO_READ;
2525 ERROROUT(nfsm_mtouio(&info, &io, len2));
2527 NULLOUT(sp = nfsm_dissect(&info, NFSX_V2SATTR));
2528 vap->va_mode = nfstov_mode(sp->sa_mode);
2530 *(pathcp + len2) = '\0';
2536 if (vap->va_mode == (mode_t)VNOVAL)
2540 error = VOP_NSYMLINK(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap, pathcp);
2544 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
2545 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2547 error = VOP_GETATTR(vp, vap);
2562 kfree(pathcp, M_TEMP);
2566 diraft_ret = VOP_GETATTR(dirp, &diraft);
2570 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2571 NFSX_SRVFH(info.v3) + NFSX_POSTOPATTR(info.v3) +
2572 NFSX_WCCDATA(info.v3),
2576 nfsm_srvpostop_fh(&info, fhp);
2577 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
2579 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2580 diraft_ret, &diraft);
2593 kfree(pathcp, M_TEMP);
2601 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2602 struct thread *td, struct mbuf **mrq)
2604 struct sockaddr *nam = nfsd->nd_nam;
2605 struct ucred *cred = &nfsd->nd_cr;
2606 struct vattr va, dirfor, diraft;
2607 struct vattr *vap = &va;
2608 struct nfs_fattr *fp;
2609 struct nlookupdata nd;
2611 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2617 struct nfsm_info info;
2619 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2625 info.dpos = nfsd->nd_dpos;
2626 info.mrep = nfsd->nd_mrep;
2628 info.md = nfsd->nd_md;
2629 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2631 fhp = &nfh.fh_generic;
2632 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2633 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2635 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2636 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2637 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2640 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2643 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2644 NFSX_WCCDATA(info.v3), &error));
2645 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2646 diraft_ret, &diraft);
2652 ERROROUT(nfsm_srvsattr(&info, vap));
2654 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2655 vap->va_mode = nfstov_mode(*tl++);
2659 * At this point nd.ni_dvp is referenced and exclusively locked and
2660 * nd.ni_vp, if it exists, is referenced but not locked.
2663 vap->va_type = VDIR;
2670 * Issue mkdir op. Since SAVESTART is not set, the pathname
2671 * component is freed by the VOP call. This will fill-in
2672 * nd.ni_vp, reference, and exclusively lock it.
2674 if (vap->va_mode == (mode_t)VNOVAL)
2677 error = VOP_NMKDIR(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
2682 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
2683 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2685 error = VOP_GETATTR(vp, vap);
2689 diraft_ret = VOP_GETATTR(dirp, &diraft);
2690 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2691 NFSX_SRVFH(info.v3) + NFSX_POSTOPATTR(info.v3) +
2692 NFSX_WCCDATA(info.v3),
2696 nfsm_srvpostop_fh(&info, fhp);
2697 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
2699 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2700 diraft_ret, &diraft);
2702 nfsm_srvfhtom(&info, fhp);
2703 fp = nfsm_build(&info, NFSX_V2FATTR);
2704 nfsm_srvfattr(nfsd, vap, fp);
2729 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2730 struct thread *td, struct mbuf **mrq)
2732 struct sockaddr *nam = nfsd->nd_nam;
2733 struct ucred *cred = &nfsd->nd_cr;
2734 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2738 struct vattr dirfor, diraft;
2741 struct nlookupdata nd;
2742 struct nfsm_info info;
2744 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2750 info.mrep = nfsd->nd_mrep;
2752 info.md = nfsd->nd_md;
2753 info.dpos = nfsd->nd_dpos;
2754 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2756 fhp = &nfh.fh_generic;
2757 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2758 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2760 error = nfs_namei(&nd, cred, NLC_DELETE, &dvp, &vp,
2761 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2762 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2765 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2768 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2769 NFSX_WCCDATA(info.v3), &error));
2770 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2771 diraft_ret, &diraft);
2775 if (vp->v_type != VDIR) {
2781 * The root of a mounted filesystem cannot be deleted.
2783 if (vp->v_flag & VROOT)
2787 * Issue or abort op. Since SAVESTART is not set, path name
2788 * component is freed by the VOP after either.
2795 error = VOP_NRMDIR(&nd.nl_nch, dvp, nd.nl_cred);
2802 diraft_ret = VOP_GETATTR(dirp, &diraft);
2803 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_WCCDATA(info.v3), &error));
2805 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2806 diraft_ret, &diraft);
2828 * nfs readdir service
2829 * - mallocs what it thinks is enough to read
2830 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2831 * - calls VOP_READDIR()
2832 * - loops around building the reply
2833 * if the output generated exceeds count break out of loop
2834 * The nfsm_clget macro is used here so that the reply will be packed
2835 * tightly in mbuf clusters.
2836 * - it only knows that it has encountered eof when the VOP_READDIR()
2838 * - as such one readdir rpc will return eof false although you are there
2839 * and then the next will return eof
2840 * - it trims out records with d_fileno == 0
2841 * this doesn't matter for Unix clients, but they might confuse clients
2843 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2844 * than requested, but this may not apply to all filesystems. For
2845 * example, client NFS does not { although it is never remote mounted
2847 * The alternate call nfsrv_readdirplus() does lookups as well.
2848 * PS: The NFS protocol spec. does not clarify what the "count" byte
2849 * argument is a count of.. just name strings and file id's or the
2850 * entire reply rpc or ...
2851 * I tried just file name and id sizes and it confused the Sun client,
2852 * so I am using the full rpc size now. The "paranoia.." comment refers
2853 * to including the status longwords that are not a part of the dir.
2854 * "entry" structures, but are in the rpc.
2858 u_int32_t fl_postopok;
2859 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2861 u_int32_t fl_fhsize;
2862 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2866 nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2867 struct thread *td, struct mbuf **mrq)
2869 struct sockaddr *nam = nfsd->nd_nam;
2870 struct ucred *cred = &nfsd->nd_cr;
2875 struct mbuf *mp1, *mp2;
2876 char *cpos, *cend, *rbuf;
2877 struct vnode *vp = NULL;
2878 struct mount *mp = NULL;
2884 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2885 int siz, cnt, fullsiz, eofflag, rdonly, ncookies;
2886 u_quad_t off, toff, verf;
2887 off_t *cookies = NULL, *cookiep;
2888 struct nfsm_info info;
2890 info.mrep = nfsd->nd_mrep;
2892 info.md = nfsd->nd_md;
2893 info.dpos = nfsd->nd_dpos;
2894 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2896 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2897 fhp = &nfh.fh_generic;
2898 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2900 NULLOUT(tl = nfsm_dissect(&info, 5 * NFSX_UNSIGNED));
2901 toff = fxdr_hyper(tl);
2903 verf = fxdr_hyper(tl);
2906 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2907 toff = fxdr_unsigned(u_quad_t, *tl++);
2908 verf = 0; /* shut up gcc */
2911 cnt = fxdr_unsigned(int, *tl);
2912 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2913 xfer = NFS_SRVMAXDATA(nfsd);
2914 if ((unsigned)cnt > xfer)
2916 if ((unsigned)siz > xfer)
2919 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
2920 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2921 if (!error && vp->v_type != VDIR) {
2927 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
2928 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2934 * Obtain lock on vnode for this section of the code
2938 error = getret = VOP_GETATTR(vp, &at);
2941 * XXX This check may be too strict for Solaris 2.5 clients.
2943 if (!error && toff && verf && verf != at.va_filerev)
2944 error = NFSERR_BAD_COOKIE;
2948 error = nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 0);
2952 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2953 NFSX_POSTOPATTR(info.v3), &error));
2954 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2961 * end section. Allocate rbuf and continue
2963 rbuf = kmalloc(siz, M_TEMP, M_WAITOK);
2966 iv.iov_len = fullsiz;
2969 io.uio_offset = (off_t)off;
2970 io.uio_resid = fullsiz;
2971 io.uio_segflg = UIO_SYSSPACE;
2972 io.uio_rw = UIO_READ;
2976 kfree((caddr_t)cookies, M_TEMP);
2979 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
2980 off = (off_t)io.uio_offset;
2981 if (!cookies && !error)
2982 error = NFSERR_PERM;
2984 getret = VOP_GETATTR(vp, &at);
2991 kfree((caddr_t)rbuf, M_TEMP);
2993 kfree((caddr_t)cookies, M_TEMP);
2994 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2995 NFSX_POSTOPATTR(info.v3), &error));
2996 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3001 siz -= io.uio_resid;
3004 * If nothing read, return eof
3010 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3011 NFSX_POSTOPATTR(info.v3) +
3012 NFSX_COOKIEVERF(info.v3) +
3016 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3017 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
3018 txdr_hyper(at.va_filerev, tl);
3021 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
3024 kfree((caddr_t)rbuf, M_TEMP);
3025 kfree((caddr_t)cookies, M_TEMP);
3032 * Check for degenerate cases of nothing useful read.
3033 * If so go try again
3037 dp = (struct dirent *)cpos;
3040 * For some reason FreeBSD's ufs_readdir() chooses to back the
3041 * directory offset up to a block boundary, so it is necessary to
3042 * skip over the records that preceed the requested offset. This
3043 * requires the assumption that file offset cookies monotonically
3046 while (cpos < cend && ncookies > 0 &&
3047 (dp->d_ino == 0 || dp->d_type == DT_WHT ||
3048 ((u_quad_t)(*cookiep)) <= toff)) {
3049 dp = _DIRENT_NEXT(dp);
3054 if (cpos >= cend || ncookies == 0) {
3060 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
3061 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3062 NFSX_POSTOPATTR(info.v3) +
3063 NFSX_COOKIEVERF(info.v3) + siz,
3066 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3067 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
3068 txdr_hyper(at.va_filerev, tl);
3070 mp1 = mp2 = info.mb;
3072 be = bp + M_TRAILINGSPACE(mp1);
3074 /* Loop through the records and build reply */
3075 while (cpos < cend && ncookies > 0) {
3076 if (dp->d_ino != 0 && dp->d_type != DT_WHT) {
3077 nlen = dp->d_namlen;
3078 rem = nfsm_rndup(nlen) - nlen;
3079 len += (4 * NFSX_UNSIGNED + nlen + rem);
3081 len += 2 * NFSX_UNSIGNED;
3087 * Build the directory record xdr from
3090 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3092 bp += NFSX_UNSIGNED;
3094 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3095 *tl = txdr_unsigned(dp->d_ino >> 32);
3096 bp += NFSX_UNSIGNED;
3098 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3099 *tl = txdr_unsigned(dp->d_ino);
3100 bp += NFSX_UNSIGNED;
3101 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3102 *tl = txdr_unsigned(nlen);
3103 bp += NFSX_UNSIGNED;
3105 /* And loop around copying the name */
3109 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3114 bcopy(cp, bp, tsiz);
3120 /* And null pad to a int32_t boundary */
3121 for (i = 0; i < rem; i++)
3123 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3125 /* Finish off the record */
3127 *tl = txdr_unsigned(*cookiep >> 32);
3128 bp += NFSX_UNSIGNED;
3129 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3131 *tl = txdr_unsigned(*cookiep);
3132 bp += NFSX_UNSIGNED;
3134 dp = _DIRENT_NEXT(dp);
3141 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3143 bp += NFSX_UNSIGNED;
3144 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3149 bp += NFSX_UNSIGNED;
3150 if (mp1 != info.mb) {
3152 mp1->m_len = bp - mtod(mp1, caddr_t);
3154 mp1->m_len += bp - info.bpos;
3155 kfree((caddr_t)rbuf, M_TEMP);
3156 kfree((caddr_t)cookies, M_TEMP);
3166 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3167 struct thread *td, struct mbuf **mrq)
3169 struct sockaddr *nam = nfsd->nd_nam;
3170 struct ucred *cred = &nfsd->nd_cr;
3175 struct mbuf *mp1, *mp2;
3176 char *cpos, *cend, *rbuf;
3177 struct vnode *vp = NULL, *nvp;
3178 struct mount *mp = NULL;
3181 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3184 struct vattr va, at, *vap = &va;
3185 struct nfs_fattr *fp;
3186 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3187 int siz, cnt, fullsiz, eofflag, rdonly, dirlen, ncookies;
3188 u_quad_t off, toff, verf;
3189 off_t *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3190 struct nfsm_info info;
3192 info.mrep = nfsd->nd_mrep;
3194 info.md = nfsd->nd_md;
3195 info.dpos = nfsd->nd_dpos;
3196 info.v3 = (nfsd->nd_flag & ND_NFSV3);
3198 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3199 fhp = &nfh.fh_generic;
3200 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3201 NULLOUT(tl = nfsm_dissect(&info, 6 * NFSX_UNSIGNED));
3202 toff = fxdr_hyper(tl);
3204 verf = fxdr_hyper(tl);
3206 siz = fxdr_unsigned(int, *tl++);
3207 cnt = fxdr_unsigned(int, *tl);
3209 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3210 xfer = NFS_SRVMAXDATA(nfsd);
3211 if ((unsigned)cnt > xfer)
3213 if ((unsigned)siz > xfer)
3216 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3217 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3218 if (!error && vp->v_type != VDIR) {
3224 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3225 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3229 error = getret = VOP_GETATTR(vp, &at);
3232 * XXX This check may be too strict for Solaris 2.5 clients.
3234 if (!error && toff && verf && verf != at.va_filerev)
3235 error = NFSERR_BAD_COOKIE;
3238 error = nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 0);
3243 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3244 NFSX_V3POSTOPATTR, &error));
3245 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3250 rbuf = kmalloc(siz, M_TEMP, M_WAITOK);
3253 iv.iov_len = fullsiz;
3256 io.uio_offset = (off_t)off;
3257 io.uio_resid = fullsiz;
3258 io.uio_segflg = UIO_SYSSPACE;
3259 io.uio_rw = UIO_READ;
3263 kfree((caddr_t)cookies, M_TEMP);
3266 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3267 off = (u_quad_t)io.uio_offset;
3268 getret = VOP_GETATTR(vp, &at);
3269 if (!cookies && !error)
3270 error = NFSERR_PERM;
3277 kfree((caddr_t)cookies, M_TEMP);
3278 kfree((caddr_t)rbuf, M_TEMP);
3279 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3280 NFSX_V3POSTOPATTR, &error));
3281 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3286 siz -= io.uio_resid;
3289 * If nothing read, return eof
3295 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3300 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3301 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
3302 txdr_hyper(at.va_filerev, tl);
3306 kfree((caddr_t)cookies, M_TEMP);
3307 kfree((caddr_t)rbuf, M_TEMP);
3314 * Check for degenerate cases of nothing useful read.
3315 * If so go try again
3319 dp = (struct dirent *)cpos;
3322 * For some reason FreeBSD's ufs_readdir() chooses to back the
3323 * directory offset up to a block boundary, so it is necessary to
3324 * skip over the records that preceed the requested offset. This
3325 * requires the assumption that file offset cookies monotonically
3328 while (cpos < cend && ncookies > 0 &&
3329 (dp->d_ino == 0 || dp->d_type == DT_WHT ||
3330 ((u_quad_t)(*cookiep)) <= toff)) {
3331 dp = _DIRENT_NEXT(dp);
3336 if (cpos >= cend || ncookies == 0) {
3343 * Probe one of the directory entries to see if the filesystem
3346 if (VFS_VGET(vp->v_mount, vp, dp->d_ino, &nvp) == EOPNOTSUPP) {
3347 error = NFSERR_NOTSUPP;
3350 kfree((caddr_t)cookies, M_TEMP);
3351 kfree((caddr_t)rbuf, M_TEMP);
3352 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3353 NFSX_V3POSTOPATTR, &error));
3354 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3363 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3365 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, cnt, &error));
3366 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3367 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
3368 txdr_hyper(at.va_filerev, tl);
3369 mp1 = mp2 = info.mb;
3371 be = bp + M_TRAILINGSPACE(mp1);
3373 /* Loop through the records and build reply */
3374 while (cpos < cend && ncookies > 0) {
3375 if (dp->d_ino != 0 && dp->d_type != DT_WHT) {
3376 nlen = dp->d_namlen;
3377 rem = nfsm_rndup(nlen) - nlen;
3380 * For readdir_and_lookup get the vnode using
3383 if (VFS_VGET(vp->v_mount, vp, dp->d_ino, &nvp))
3385 bzero((caddr_t)nfhp, NFSX_V3FH);
3386 nfhp->fh_fsid = fhp->fh_fsid;
3387 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3392 if (VOP_GETATTR(nvp, vap)) {
3401 * If either the dircount or maxcount will be
3402 * exceeded, get out now. Both of these lengths
3403 * are calculated conservatively, including all
3406 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3408 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3409 if (len > cnt || dirlen > fullsiz) {
3415 * Build the directory record xdr from
3418 fp = (struct nfs_fattr *)&fl.fl_fattr;
3419 nfsm_srvfattr(nfsd, vap, fp);
3420 fl.fl_off.nfsuquad[0] = txdr_unsigned(*cookiep >> 32);
3421 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3422 fl.fl_postopok = nfs_true;
3423 fl.fl_fhok = nfs_true;
3424 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3426 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3428 bp += NFSX_UNSIGNED;
3429 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3430 *tl = txdr_unsigned(dp->d_ino >> 32);
3431 bp += NFSX_UNSIGNED;
3432 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3433 *tl = txdr_unsigned(dp->d_ino);
3434 bp += NFSX_UNSIGNED;
3435 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3436 *tl = txdr_unsigned(nlen);
3437 bp += NFSX_UNSIGNED;
3439 /* And loop around copying the name */
3443 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3444 if ((bp + xfer) > be)
3448 bcopy(cp, bp, tsiz);
3453 /* And null pad to a int32_t boundary */
3454 for (i = 0; i < rem; i++)
3458 * Now copy the flrep structure out.
3460 xfer = sizeof (struct flrep);
3463 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3464 if ((bp + xfer) > be)
3468 bcopy(cp, bp, tsiz);
3475 dp = _DIRENT_NEXT(dp);
3482 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3484 bp += NFSX_UNSIGNED;
3485 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3490 bp += NFSX_UNSIGNED;
3491 if (mp1 != info.mb) {
3493 mp1->m_len = bp - mtod(mp1, caddr_t);
3495 mp1->m_len += bp - info.bpos;
3496 kfree((caddr_t)cookies, M_TEMP);
3497 kfree((caddr_t)rbuf, M_TEMP);
3506 * nfs commit service
3509 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3510 struct thread *td, struct mbuf **mrq)
3512 struct sockaddr *nam = nfsd->nd_nam;
3513 struct ucred *cred = &nfsd->nd_cr;
3514 struct vattr bfor, aft;
3515 struct vnode *vp = NULL;
3516 struct mount *mp = NULL;
3520 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt;
3522 struct nfsm_info info;
3524 info.mrep = nfsd->nd_mrep;
3526 info.md = nfsd->nd_md;
3527 info.dpos = nfsd->nd_dpos;
3529 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3530 fhp = &nfh.fh_generic;
3531 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3532 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
3535 * XXX At this time VOP_FSYNC() does not accept offset and byte
3536 * count parameters, so these arguments are useless (someday maybe).
3538 off = fxdr_hyper(tl);
3540 cnt = fxdr_unsigned(int, *tl);
3541 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3542 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3544 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3545 2 * NFSX_UNSIGNED, &error));
3546 nfsm_srvwcc_data(&info, nfsd, for_ret, &bfor,
3551 for_ret = VOP_GETATTR(vp, &bfor);
3553 if (cnt > MAX_COMMIT_COUNT) {
3555 * Give up and do the whole thing
3558 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3559 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3561 error = VOP_FSYNC(vp, MNT_WAIT, 0);
3564 * Locate and synchronously write any buffers that fall
3565 * into the requested range. Note: we are assuming that
3566 * f_iosize is a power of 2.
3568 int iosize = vp->v_mount->mnt_stat.f_iosize;
3569 int iomask = iosize - 1;
3573 * Align to iosize boundry, super-align to page boundry.
3576 cnt += off & iomask;
3577 off &= ~(u_quad_t)iomask;
3579 if (off & PAGE_MASK) {
3580 cnt += off & PAGE_MASK;
3581 off &= ~(u_quad_t)PAGE_MASK;
3586 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3587 vm_object_page_clean(vp->v_object, off / PAGE_SIZE,
3588 (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3596 * If we have a buffer and it is marked B_DELWRI we
3597 * have to lock and write it. Otherwise the prior
3598 * write is assumed to have already been committed.
3600 * WARNING: FINDBLK_TEST buffers represent stable
3601 * storage but not necessarily stable
3602 * content. It is ok in this case.
3604 if ((bp = findblk(vp, loffset, FINDBLK_TEST)) != NULL) {
3605 if (bp->b_flags & B_DELWRI)
3606 bp = findblk(vp, loffset, 0);
3611 if (bp->b_flags & B_DELWRI) {
3628 aft_ret = VOP_GETATTR(vp, &aft);
3631 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3632 NFSX_V3WCCDATA + NFSX_V3WRITEVERF,
3634 nfsm_srvwcc_data(&info, nfsd, for_ret, &bfor,
3637 tl = nfsm_build(&info, NFSX_V3WRITEVERF);
3638 if (nfsver.tv_sec == 0)
3640 *tl++ = txdr_unsigned(nfsver.tv_sec);
3641 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
3653 * nfs statfs service
3656 nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3657 struct thread *td, struct mbuf **mrq)
3659 struct sockaddr *nam = nfsd->nd_nam;
3660 struct ucred *cred = &nfsd->nd_cr;
3662 struct nfs_statfs *sfp;
3663 int error = 0, rdonly, getret = 1;
3664 struct vnode *vp = NULL;
3665 struct mount *mp = NULL;
3669 struct statfs statfs;
3671 struct nfsm_info info;
3673 info.mrep = nfsd->nd_mrep;
3675 info.md = nfsd->nd_md;
3676 info.dpos = nfsd->nd_dpos;
3677 info.v3 = (nfsd->nd_flag & ND_NFSV3);
3679 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3680 fhp = &nfh.fh_generic;
3681 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3682 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3683 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3685 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3686 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3691 error = VFS_STATFS(vp->v_mount, sf, proc0.p_ucred);
3692 getret = VOP_GETATTR(vp, &at);
3695 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3696 NFSX_POSTOPATTR(info.v3) + NFSX_STATFS(info.v3),
3699 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3704 sfp = nfsm_build(&info, NFSX_STATFS(info.v3));
3706 tval = (u_quad_t)sf->f_blocks;
3707 tval *= (u_quad_t)sf->f_bsize;
3708 txdr_hyper(tval, &sfp->sf_tbytes);
3709 tval = (u_quad_t)sf->f_bfree;
3710 tval *= (u_quad_t)sf->f_bsize;
3711 txdr_hyper(tval, &sfp->sf_fbytes);
3712 tval = (u_quad_t)sf->f_bavail;
3713 tval *= (u_quad_t)sf->f_bsize;
3714 txdr_hyper(tval, &sfp->sf_abytes);
3715 sfp->sf_tfiles.nfsuquad[0] = 0;
3716 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3717 sfp->sf_ffiles.nfsuquad[0] = 0;
3718 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3719 sfp->sf_afiles.nfsuquad[0] = 0;
3720 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3721 sfp->sf_invarsec = 0;
3723 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3724 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3725 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3726 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3727 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3737 * nfs fsinfo service
3740 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3741 struct thread *td, struct mbuf **mrq)
3743 struct sockaddr *nam = nfsd->nd_nam;
3744 struct ucred *cred = &nfsd->nd_cr;
3745 struct nfsv3_fsinfo *sip;
3746 int error = 0, rdonly, getret = 1, pref;
3747 struct vnode *vp = NULL;
3748 struct mount *mp = NULL;
3754 struct nfsm_info info;
3756 info.mrep = nfsd->nd_mrep;
3758 info.md = nfsd->nd_md;
3759 info.dpos = nfsd->nd_dpos;
3761 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3762 fhp = &nfh.fh_generic;
3763 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3764 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3765 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3767 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3768 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3773 /* XXX Try to make a guess on the max file size. */
3774 VFS_STATFS(vp->v_mount, &sb, proc0.p_ucred);
3775 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3777 getret = VOP_GETATTR(vp, &at);
3780 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3781 NFSX_V3POSTOPATTR + NFSX_V3FSINFO, &error));
3782 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3783 sip = nfsm_build(&info, NFSX_V3FSINFO);
3787 * There should be file system VFS OP(s) to get this information.
3788 * For now, assume ufs.
3790 if (slp->ns_so->so_type == SOCK_DGRAM)
3791 pref = NFS_MAXDGRAMDATA;
3794 sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3795 sip->fs_rtpref = txdr_unsigned(pref);
3796 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3797 sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3798 sip->fs_wtpref = txdr_unsigned(pref);
3799 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3800 sip->fs_dtpref = txdr_unsigned(pref);
3801 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3802 sip->fs_timedelta.nfsv3_sec = 0;
3803 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3804 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3805 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3806 NFSV3FSINFO_CANSETTIME);
3815 * nfs pathconf service
3818 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3819 struct thread *td, struct mbuf **mrq)
3821 struct sockaddr *nam = nfsd->nd_nam;
3822 struct ucred *cred = &nfsd->nd_cr;
3823 struct nfsv3_pathconf *pc;
3824 int error = 0, rdonly, getret = 1;
3825 register_t linkmax, namemax, chownres, notrunc;
3826 struct vnode *vp = NULL;
3827 struct mount *mp = NULL;
3831 struct nfsm_info info;
3833 info.mrep = nfsd->nd_mrep;
3835 info.md = nfsd->nd_md;
3836 info.dpos = nfsd->nd_dpos;
3838 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3839 fhp = &nfh.fh_generic;
3840 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3841 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3842 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3844 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3845 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3849 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3851 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3853 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3855 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3856 getret = VOP_GETATTR(vp, &at);
3859 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3860 NFSX_V3POSTOPATTR + NFSX_V3PATHCONF,
3862 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3867 pc = nfsm_build(&info, NFSX_V3PATHCONF);
3869 pc->pc_linkmax = txdr_unsigned(linkmax);
3870 pc->pc_namemax = txdr_unsigned(namemax);
3871 pc->pc_notrunc = txdr_unsigned(notrunc);
3872 pc->pc_chownrestricted = txdr_unsigned(chownres);
3875 * These should probably be supported by VOP_PATHCONF(), but
3876 * until msdosfs is exportable (why would you want to?), the
3877 * Unix defaults should be ok.
3879 pc->pc_caseinsensitive = nfs_false;
3880 pc->pc_casepreserving = nfs_true;
3889 * Null operation, used by clients to ping server
3893 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3894 struct thread *td, struct mbuf **mrq)
3896 struct nfsm_info info;
3897 int error = NFSERR_RETVOID;
3899 info.mrep = nfsd->nd_mrep;
3902 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3903 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
3910 * No operation, used for obsolete procedures
3914 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3915 struct thread *td, struct mbuf **mrq)
3917 struct nfsm_info info;
3920 info.mrep = nfsd->nd_mrep;
3923 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3924 if (nfsd->nd_repstat)
3925 error = nfsd->nd_repstat;
3927 error = EPROCUNAVAIL;
3928 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
3936 * Perform access checking for vnodes obtained from file handles that would
3937 * refer to files already opened by a Unix client. You cannot just use
3938 * vn_writechk() and VOP_ACCESS() for two reasons.
3939 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
3940 * 2 - The owner is to be given access irrespective of mode bits for some
3941 * operations, so that processes that chmod after opening a file don't
3942 * break. I don't like this because it opens a security hole, but since
3943 * the nfs server opens a security hole the size of a barn door anyhow,
3946 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
3947 * will return EPERM instead of EACCESS. EPERM is always an error.
3950 nfsrv_access(struct mount *mp, struct vnode *vp, int flags, struct ucred *cred,
3951 int rdonly, struct thread *td, int override)
3956 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3957 if (flags & VWRITE) {
3958 /* Just vn_writechk() changed to check rdonly */
3960 * Disallow write attempts on read-only file systems;
3961 * unless the file is a socket or a block or character
3962 * device resident on the file system.
3965 ((mp->mnt_flag | vp->v_mount->mnt_flag) & MNT_RDONLY)) {
3966 switch (vp->v_type) {
3976 * If there's shared text associated with
3977 * the inode, we can't allow writing.
3979 if (vp->v_flag & VTEXT)
3982 error = VOP_GETATTR(vp, &vattr);
3985 error = VOP_ACCESS(vp, flags, cred); /* XXX ruid/rgid vs uid/gid */
3987 * Allow certain operations for the owner (reads and writes
3988 * on files that are already open).
3990 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)
3994 #endif /* NFS_NOSERVER */