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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)nfs_subs.c 8.8 (Berkeley) 5/22/95
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 * These functions support the macros and help fiddle mbuf chains for
40 * the nfs op functions. They do things like create the rpc header and
41 * copy data between mbuf chains and uio lists.
44 #include "opt_inet6.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
52 #include <sys/mount.h>
53 #include <sys/vnode.h>
54 #include <sys/namei.h>
56 #include <sys/refcount.h>
57 #include <sys/socket.h>
59 #include <sys/malloc.h>
60 #include <sys/module.h>
61 #include <sys/sysent.h>
62 #include <sys/syscall.h>
63 #include <sys/sysproto.h>
66 #include <vm/vm_object.h>
67 #include <vm/vm_extern.h>
70 #include <nfs/rpcv2.h>
71 #include <nfs/nfsproto.h>
72 #include <nfsserver/nfs.h>
73 #include <nfs/xdr_subs.h>
74 #include <nfsserver/nfsm_subs.h>
76 #include <netinet/in.h>
79 * Data items converted to xdr at startup, since they are constant
80 * This is kinda hokey, but may save a little time doing byte swaps
82 u_int32_t nfsrv_nfs_xdrneg1;
83 u_int32_t nfsrv_rpc_call, nfsrv_rpc_vers, nfsrv_rpc_reply,
84 nfsrv_rpc_msgdenied, nfsrv_rpc_autherr,
85 nfsrv_rpc_mismatch, nfsrv_rpc_auth_unix, nfsrv_rpc_msgaccepted;
86 u_int32_t nfsrv_nfs_prog, nfsrv_nfs_true, nfsrv_nfs_false;
88 /* And other global data */
89 static const nfstype nfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR,
90 NFLNK, NFNON, NFCHR, NFNON };
91 #define vtonfsv2_type(a) txdr_unsigned(nfsv2_type[((int32_t)(a))])
92 #define vtonfsv3_mode(m) txdr_unsigned((m) & ALLPERMS)
96 struct nfssvc_sockhead nfssvc_sockhead;
97 int nfssvc_sockhead_flag;
98 struct nfsd_head nfsd_head;
101 static int nfssvc_offset = SYS_nfssvc;
102 static struct sysent nfssvc_prev_sysent;
108 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
110 const int nfsrv_nfsv3_procid[NFS_NPROCS] = {
137 * and the reverse mapping from generic to Version 2 procedure numbers
139 const int nfsrvv2_procid[NFS_NPROCS] = {
166 * Maps errno values to nfs error numbers.
167 * Use 0 (which gets converted to NFSERR_IO) as the catch all for ones not
168 * specifically defined in RFC 1094.
170 static const u_char nfsrv_v2errmap[ELAST] = {
171 NFSERR_PERM, NFSERR_NOENT, 0, 0, 0,
172 NFSERR_NXIO, 0, 0, 0, 0,
173 0, 0, NFSERR_ACCES, 0, 0,
174 0, NFSERR_EXIST, 0, NFSERR_NODEV, NFSERR_NOTDIR,
175 NFSERR_ISDIR, 0, 0, 0, 0,
176 0, NFSERR_FBIG, NFSERR_NOSPC, 0, NFSERR_ROFS,
183 0, 0, NFSERR_NAMETOL, 0, 0,
184 NFSERR_NOTEMPTY, 0, 0, NFSERR_DQUOT, NFSERR_STALE,
189 * Maps errno values to nfs error numbers.
190 * Although it is not obvious whether or not NFS clients really care if
191 * a returned error value is in the specified list for the procedure, the
192 * safest thing to do is filter them appropriately. For Version 2, the
193 * X/Open XNFS document is the only specification that defines error values
194 * for each RPC (The RFC simply lists all possible error values for all RPCs),
195 * so I have decided to not do this for Version 2.
196 * The first entry is the default error return and the rest are the valid
197 * errors for that RPC in increasing numeric order.
199 static const short nfsv3err_null[] = {
204 static const short nfsv3err_getattr[] = {
213 static const short nfsv3err_setattr[] = {
229 static const short nfsv3err_lookup[] = {
242 static const short nfsv3err_access[] = {
251 static const short nfsv3err_readlink[] = {
263 static const short nfsv3err_read[] = {
275 static const short nfsv3err_write[] = {
290 static const short nfsv3err_create[] = {
307 static const short nfsv3err_mkdir[] = {
324 static const short nfsv3err_symlink[] = {
341 static const short nfsv3err_mknod[] = {
359 static const short nfsv3err_remove[] = {
373 static const short nfsv3err_rmdir[] = {
391 static const short nfsv3err_rename[] = {
414 static const short nfsv3err_link[] = {
434 static const short nfsv3err_readdir[] = {
447 static const short nfsv3err_readdirplus[] = {
461 static const short nfsv3err_fsstat[] = {
470 static const short nfsv3err_fsinfo[] = {
478 static const short nfsv3err_pathconf[] = {
486 static const short nfsv3err_commit[] = {
495 static const short *nfsrv_v3errmap[] = {
513 nfsv3err_readdirplus,
521 * Called once to initialize data structures...
524 nfsrv_modevent(module_t mod, int type, void *data)
526 static int registered;
531 mtx_init(&nfsd_mtx, "nfsd_mtx", NULL, MTX_DEF);
532 nfsrv_rpc_vers = txdr_unsigned(RPC_VER2);
533 nfsrv_rpc_call = txdr_unsigned(RPC_CALL);
534 nfsrv_rpc_reply = txdr_unsigned(RPC_REPLY);
535 nfsrv_rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
536 nfsrv_rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
537 nfsrv_rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
538 nfsrv_rpc_autherr = txdr_unsigned(RPC_AUTHERR);
539 nfsrv_rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
540 nfsrv_nfs_prog = txdr_unsigned(NFS_PROG);
541 nfsrv_nfs_true = txdr_unsigned(TRUE);
542 nfsrv_nfs_false = txdr_unsigned(FALSE);
543 nfsrv_nfs_xdrneg1 = txdr_unsigned(-1);
544 nfsrv_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
548 nfsrv_initcache(); /* Init the server request cache */
550 nfsrv_init(0); /* Init server data structures */
551 callout_init(&nfsrv_callout, CALLOUT_MPSAFE);
555 error = syscall_register(&nfssvc_offset, &nfssvc_sysent,
556 &nfssvc_prev_sysent);
563 if (nfsrv_numnfsd != 0) {
569 syscall_deregister(&nfssvc_offset, &nfssvc_prev_sysent);
570 callout_drain(&nfsrv_callout);
571 nfsrv_destroycache(); /* Free the server request cache */
572 mtx_destroy(&nfsd_mtx);
580 static moduledata_t nfsserver_mod = {
585 DECLARE_MODULE(nfsserver, nfsserver_mod, SI_SUB_VFS, SI_ORDER_ANY);
587 /* So that loader and kldload(2) can find us, wherever we are.. */
588 MODULE_VERSION(nfsserver, 1);
591 * Set up nameidata for a lookup() call and do it.
593 * If pubflag is set, this call is done for a lookup operation on the
594 * public filehandle. In that case we allow crossing mountpoints and
595 * absolute pathnames. However, the caller is expected to check that
596 * the lookup result is within the public fs, and deny access if
599 * nfs_namei() clears out garbage fields that namei() might leave garbage.
600 * This is mainly ni_vp and ni_dvp when an error occurs, and ni_dvp when no
601 * error occurs but the parent was not requested.
603 * dirp may be set whether an error is returned or not, and must be
604 * released by the caller.
607 nfs_namei(struct nameidata *ndp, fhandle_t *fhp, int len,
608 struct nfssvc_sock *slp, struct sockaddr *nam, struct mbuf **mdp,
609 caddr_t *dposp, struct vnode **retdirp, int v3, struct vattr *retdirattrp,
610 int *retdirattr_retp, struct thread *td, int pubflag)
614 char *fromcp, *tocp, *cp;
618 int error, rdonly, linklen;
619 struct componentname *cnp = &ndp->ni_cnd;
620 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0;
627 cnp->cn_flags |= NOMACCHECK;
628 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
631 * Copy the name from the mbuf list to ndp->ni_pnbuf
632 * and set the various ndp fields appropriately.
635 tocp = cnp->cn_pnbuf;
637 rem = mtod(md, caddr_t) + md->m_len - fromcp;
638 for (i = 0; i < len; i++) {
645 fromcp = mtod(md, caddr_t);
648 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
658 len = nfsm_rndup(len)-len;
662 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
667 * Extract and set starting directory.
669 error = nfsrv_fhtovp(fhp, FALSE, &dp, &dvfslocked,
670 ndp->ni_cnd.cn_cred, slp, nam, &rdonly, pubflag);
673 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
674 if (dp->v_type != VDIR) {
681 cnp->cn_flags |= RDONLY;
684 * Set return directory. Reference to dp is implicitly transfered
685 * to the returned pointer
689 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
690 *retdirattr_retp = VOP_GETATTR(dp, retdirattrp,
691 ndp->ni_cnd.cn_cred, td);
692 VOP_UNLOCK(dp, 0, td);
697 * Oh joy. For WebNFS, handle those pesky '%' escapes,
698 * and the 'native path' indicator.
700 cp = uma_zalloc(namei_zone, M_WAITOK);
701 fromcp = cnp->cn_pnbuf;
703 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
704 switch ((unsigned char)*fromcp) {
705 case WEBNFS_NATIVE_CHAR:
707 * 'Native' path for us is the same
708 * as a path according to the NFS spec,
709 * just skip the escape char.
714 * More may be added in the future, range 0x80-0xff
718 uma_zfree(namei_zone, cp);
723 * Translate the '%' escapes, URL-style.
725 while (*fromcp != '\0') {
726 if (*fromcp == WEBNFS_ESC_CHAR) {
727 if (fromcp[1] != '\0' && fromcp[2] != '\0') {
729 *tocp++ = HEXSTRTOI(fromcp);
734 uma_zfree(namei_zone, cp);
741 uma_zfree(namei_zone, cnp->cn_pnbuf);
745 ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
746 ndp->ni_segflg = UIO_SYSSPACE;
749 ndp->ni_rootdir = rootvnode;
751 if (cnp->cn_pnbuf[0] == '/') {
754 tvfslocked = VFS_LOCK_GIANT(rootvnode->v_mount);
755 VFS_UNLOCK_GIANT(vfslocked);
757 vfslocked = tvfslocked;
760 cnp->cn_flags |= NOCROSSMOUNT;
764 * Initialize for scan, set ni_startdir and bump ref on dp again
765 * because lookup() will dereference ni_startdir.
770 ndp->ni_startdir = dp;
773 cnp->cn_flags |= LOCKLEAF;
775 cnp->cn_nameptr = cnp->cn_pnbuf;
777 * Call lookup() to do the real work. If an error occurs,
778 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
779 * we do not have to dereference anything before returning.
780 * In either case ni_startdir will be dereferenced and NULLed
784 ndp->ni_cnd.cn_flags |= GIANTHELD;
786 vfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
787 ndp->ni_cnd.cn_flags &= ~GIANTHELD;
792 * Check for encountering a symbolic link. Trivial
793 * termination occurs if no symlink encountered.
794 * Note: zfree is safe because error is 0, so we will
795 * not zfree it again when we break.
797 if ((cnp->cn_flags & ISSYMLINK) == 0) {
798 if (cnp->cn_flags & (SAVENAME | SAVESTART))
799 cnp->cn_flags |= HASBUF;
801 uma_zfree(namei_zone, cnp->cn_pnbuf);
802 if (ndp->ni_vp && !lockleaf)
803 VOP_UNLOCK(ndp->ni_vp, 0, td);
810 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
811 VOP_UNLOCK(ndp->ni_dvp, 0, td);
817 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
821 if (ndp->ni_pathlen > 1)
822 cp = uma_zalloc(namei_zone, M_WAITOK);
826 aiov.iov_len = MAXPATHLEN;
827 auio.uio_iov = &aiov;
830 auio.uio_rw = UIO_READ;
831 auio.uio_segflg = UIO_SYSSPACE;
833 auio.uio_resid = MAXPATHLEN;
834 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
837 if (ndp->ni_pathlen > 1)
838 uma_zfree(namei_zone, cp);
844 linklen = MAXPATHLEN - auio.uio_resid;
849 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
850 error = ENAMETOOLONG;
855 * Adjust or replace path
857 if (ndp->ni_pathlen > 1) {
858 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
859 uma_zfree(namei_zone, cnp->cn_pnbuf);
862 cnp->cn_pnbuf[linklen] = '\0';
863 ndp->ni_pathlen += linklen;
866 * Cleanup refs for next loop and check if root directory
867 * should replace current directory. Normally ni_dvp
868 * becomes the new base directory and is cleaned up when
869 * we loop. Explicitly null pointers after invalidation
870 * to clarify operation.
875 if (cnp->cn_pnbuf[0] == '/') {
877 ndp->ni_dvp = ndp->ni_rootdir;
880 ndp->ni_startdir = ndp->ni_dvp;
884 cnp->cn_flags &= ~LOCKLEAF;
885 if (cnp->cn_flags & GIANTHELD) {
887 cnp->cn_flags &= ~GIANTHELD;
891 * nfs_namei() guarentees that fields will not contain garbage
892 * whether an error occurs or not. This allows the caller to track
893 * cleanup state trivially.
897 uma_zfree(namei_zone, cnp->cn_pnbuf);
900 ndp->ni_startdir = NULL;
901 cnp->cn_flags &= ~HASBUF;
902 VFS_UNLOCK_GIANT(vfslocked);
904 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
908 * This differs from normal namei() in that even on failure we may
909 * return with Giant held due to the dirp return. Make sure we only
910 * have not recursed however. The calling code only expects to drop
913 if (vfslocked || dvfslocked)
914 ndp->ni_cnd.cn_flags |= GIANTHELD;
915 if (vfslocked && dvfslocked)
916 VFS_UNLOCK_GIANT(vfslocked);
921 * A fiddled version of m_adj() that ensures null fill to a long
922 * boundary and only trims off the back end
925 nfsm_adj(struct mbuf *mp, int len, int nul)
932 * Trim from tail. Scan the mbuf chain,
933 * calculating its length and finding the last mbuf.
934 * If the adjustment only affects this mbuf, then just
935 * adjust and return. Otherwise, rescan and truncate
936 * after the remaining size.
942 if (m->m_next == NULL)
946 if (m->m_len > len) {
949 cp = mtod(m, caddr_t)+m->m_len-nul;
950 for (i = 0; i < nul; i++)
959 * Correct length for chain is "count".
960 * Find the mbuf with last data, adjust its length,
961 * and toss data from remaining mbufs on chain.
963 for (m = mp; m; m = m->m_next) {
964 if (m->m_len >= count) {
967 cp = mtod(m, caddr_t)+m->m_len-nul;
968 for (i = 0; i < nul; i++)
971 if (m->m_next != NULL) {
982 * Make these functions instead of macros, so that the kernel text size
983 * doesn't get too big...
986 nfsm_srvwcc(struct nfsrv_descript *nfsd, int before_ret,
987 struct vattr *before_vap, int after_ret, struct vattr *after_vap,
988 struct mbuf **mbp, char **bposp)
990 struct mbuf *mb = *mbp;
995 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
996 *tl = nfsrv_nfs_false;
998 tl = nfsm_build(u_int32_t *, 7 * NFSX_UNSIGNED);
999 *tl++ = nfsrv_nfs_true;
1000 txdr_hyper(before_vap->va_size, tl);
1002 txdr_nfsv3time(&(before_vap->va_mtime), tl);
1004 txdr_nfsv3time(&(before_vap->va_ctime), tl);
1008 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
1012 nfsm_srvpostopattr(struct nfsrv_descript *nfsd, int after_ret,
1013 struct vattr *after_vap, struct mbuf **mbp, char **bposp)
1015 struct mbuf *mb = *mbp;
1016 char *bpos = *bposp;
1018 struct nfs_fattr *fp;
1021 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
1022 *tl = nfsrv_nfs_false;
1024 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
1025 *tl++ = nfsrv_nfs_true;
1026 fp = (struct nfs_fattr *)tl;
1027 nfsm_srvfattr(nfsd, after_vap, fp);
1034 nfsm_srvfattr(struct nfsrv_descript *nfsd, struct vattr *vap,
1035 struct nfs_fattr *fp)
1038 fp->fa_nlink = txdr_unsigned(vap->va_nlink);
1039 fp->fa_uid = txdr_unsigned(vap->va_uid);
1040 fp->fa_gid = txdr_unsigned(vap->va_gid);
1041 if (nfsd->nd_flag & ND_NFSV3) {
1042 fp->fa_type = vtonfsv3_type(vap->va_type);
1043 fp->fa_mode = vtonfsv3_mode(vap->va_mode);
1044 txdr_hyper(vap->va_size, &fp->fa3_size);
1045 txdr_hyper(vap->va_bytes, &fp->fa3_used);
1046 fp->fa3_rdev.specdata1 = txdr_unsigned(umajor(vap->va_rdev));
1047 fp->fa3_rdev.specdata2 = txdr_unsigned(uminor(vap->va_rdev));
1048 fp->fa3_fsid.nfsuquad[0] = 0;
1049 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
1050 fp->fa3_fileid.nfsuquad[0] = 0;
1051 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
1052 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
1053 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
1054 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
1056 fp->fa_type = vtonfsv2_type(vap->va_type);
1057 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1058 fp->fa2_size = txdr_unsigned(vap->va_size);
1059 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
1060 if (vap->va_type == VFIFO)
1061 fp->fa2_rdev = 0xffffffff;
1063 fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
1064 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
1065 fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
1066 fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
1067 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
1068 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
1069 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
1074 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
1075 * - look up fsid in mount list (if not found ret error)
1076 * - get vp and export rights by calling VFS_FHTOVP()
1077 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
1078 * - if not lockflag unlock it with VOP_UNLOCK()
1081 nfsrv_fhtovp(fhandle_t *fhp, int lockflag, struct vnode **vpp, int *vfslockedp,
1082 struct ucred *cred, struct nfssvc_sock *slp, struct sockaddr *nam,
1083 int *rdonlyp, int pubflag)
1085 struct thread *td = curthread; /* XXX */
1088 struct ucred *credanon;
1090 #ifdef MNT_EXNORESPORT /* XXX needs mountd and /etc/exports help yet */
1091 struct sockaddr_int *saddr;
1098 if (nfs_ispublicfh(fhp)) {
1099 if (!pubflag || !nfs_pub.np_valid)
1101 fhp = &nfs_pub.np_handle;
1104 mp = vfs_getvfs(&fhp->fh_fsid);
1107 vfslocked = VFS_LOCK_GIANT(mp);
1108 error = VFS_CHECKEXP(mp, nam, &exflags, &credanon);
1111 error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp);
1114 #ifdef MNT_EXNORESPORT
1115 if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
1116 saddr = (struct sockaddr_in *)nam;
1117 if ((saddr->sin_family == AF_INET ||
1118 saddr->sin_family == AF_INET6) &&
1119 /* same code for INET and INET6: sin*_port at same offet */
1120 ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
1123 error = NFSERR_AUTHERR | AUTH_TOOWEAK;
1128 * Check/setup credentials.
1130 if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1131 cred->cr_uid = credanon->cr_uid;
1132 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
1133 cred->cr_groups[i] = credanon->cr_groups[i];
1134 cred->cr_ngroups = i;
1136 if (exflags & MNT_EXRDONLY)
1142 VOP_UNLOCK(*vpp, 0, td);
1146 VFS_UNLOCK_GIANT(vfslocked);
1148 *vfslockedp = vfslocked;
1154 * WebNFS: check if a filehandle is a public filehandle. For v3, this
1155 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
1156 * transformed this to all zeroes in both cases, so check for it.
1159 nfs_ispublicfh(fhandle_t *fhp)
1161 char *cp = (char *)fhp;
1164 NFSD_LOCK_DONTCARE();
1166 for (i = 0; i < NFSX_V3FH; i++)
1173 * This function compares two net addresses by family and returns TRUE
1174 * if they are the same host.
1175 * If there is any doubt, return FALSE.
1176 * The AF_INET family is handled as a special case so that address mbufs
1177 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
1180 netaddr_match(int family, union nethostaddr *haddr, struct sockaddr *nam)
1182 struct sockaddr_in *inetaddr;
1184 NFSD_LOCK_DONTCARE();
1188 inetaddr = (struct sockaddr_in *)nam;
1189 if (inetaddr->sin_family == AF_INET &&
1190 inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
1196 register struct sockaddr_in6 *inet6addr1, *inet6addr2;
1198 inet6addr1 = (struct sockaddr_in6 *)nam;
1199 inet6addr2 = (struct sockaddr_in6 *)haddr->had_nam;
1200 /* XXX - should test sin6_scope_id ? */
1201 if (inet6addr1->sin6_family == AF_INET6 &&
1202 IN6_ARE_ADDR_EQUAL(&inet6addr1->sin6_addr,
1203 &inet6addr2->sin6_addr))
1215 * Map errnos to NFS error numbers. For Version 3 also filter out error
1216 * numbers not specified for the associated procedure.
1219 nfsrv_errmap(struct nfsrv_descript *nd, int err)
1221 const short *defaulterrp, *errp;
1225 if (nd->nd_flag & ND_NFSV3) {
1226 if (nd->nd_procnum <= NFSPROC_COMMIT) {
1227 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
1231 else if (*errp > err)
1234 return ((int)*defaulterrp);
1236 return (err & 0xffff);
1240 e = nfsrv_v2errmap[err - 1];
1247 * Sort the group list in increasing numerical order.
1248 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
1249 * that used to be here.)
1252 nfsrvw_sort(gid_t *list, int num)
1257 /* Insertion sort. */
1258 for (i = 1; i < num; i++) {
1260 /* find correct slot for value v, moving others up */
1261 for (j = i; --j >= 0 && v < list[j];)
1262 list[j + 1] = list[j];
1268 * Helper functions for macros.
1272 nfsm_srvfhtom_xx(fhandle_t *f, int v3, struct mbuf **mb, caddr_t *bpos)
1277 tl = nfsm_build_xx(NFSX_UNSIGNED + NFSX_V3FH, mb, bpos);
1278 *tl++ = txdr_unsigned(NFSX_V3FH);
1279 bcopy(f, tl, NFSX_V3FH);
1281 tl = nfsm_build_xx(NFSX_V2FH, mb, bpos);
1282 bcopy(f, tl, NFSX_V2FH);
1287 nfsm_srvpostop_fh_xx(fhandle_t *f, struct mbuf **mb, caddr_t *bpos)
1291 tl = nfsm_build_xx(2 * NFSX_UNSIGNED + NFSX_V3FH, mb, bpos);
1292 *tl++ = nfsrv_nfs_true;
1293 *tl++ = txdr_unsigned(NFSX_V3FH);
1294 bcopy(f, tl, NFSX_V3FH);
1298 nfsm_srvstrsiz_xx(int *s, int m, struct mbuf **md, caddr_t *dpos)
1302 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1305 *s = fxdr_unsigned(int32_t, *tl);
1306 if (*s > m || *s <= 0)
1312 nfsm_srvnamesiz_xx(int *s, int m, struct mbuf **md, caddr_t *dpos)
1316 NFSD_LOCK_DONTCARE();
1318 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1321 *s = fxdr_unsigned(int32_t, *tl);
1323 return NFSERR_NAMETOL;
1330 nfsm_srvnamesiz0_xx(int *s, int m, struct mbuf **md, caddr_t *dpos)
1334 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1337 *s = fxdr_unsigned(int32_t, *tl);
1339 return NFSERR_NAMETOL;
1346 nfsm_clget_xx(u_int32_t **tl, struct mbuf *mb, struct mbuf **mp,
1347 char **bp, char **be, caddr_t bpos)
1351 NFSD_UNLOCK_ASSERT();
1355 (*mp)->m_len += *bp - bpos;
1356 MGET(nmp, M_TRYWAIT, MT_DATA);
1357 MCLGET(nmp, M_TRYWAIT);
1358 nmp->m_len = NFSMSIZ(nmp);
1359 (*mp)->m_next = nmp;
1361 *bp = mtod(*mp, caddr_t);
1362 *be = *bp + (*mp)->m_len;
1364 *tl = (u_int32_t *)*bp;
1368 nfsm_srvmtofh_xx(fhandle_t *f, struct nfsrv_descript *nfsd, struct mbuf **md,
1374 if (nfsd->nd_flag & ND_NFSV3) {
1375 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1378 fhlen = fxdr_unsigned(int, *tl);
1379 if (fhlen != 0 && fhlen != NFSX_V3FH)
1385 tl = nfsm_dissect_xx_nonblock(fhlen, md, dpos);
1388 bcopy((caddr_t)tl, (caddr_t)(f), fhlen);
1390 bzero((caddr_t)(f), NFSX_V3FH);
1396 nfsm_srvsattr_xx(struct vattr *a, struct mbuf **md, caddr_t *dpos)
1401 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1404 if (*tl == nfsrv_nfs_true) {
1405 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1408 (a)->va_mode = nfstov_mode(*tl);
1410 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1413 if (*tl == nfsrv_nfs_true) {
1414 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1417 (a)->va_uid = fxdr_unsigned(uid_t, *tl);
1419 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1422 if (*tl == nfsrv_nfs_true) {
1423 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1426 (a)->va_gid = fxdr_unsigned(gid_t, *tl);
1428 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1431 if (*tl == nfsrv_nfs_true) {
1432 tl = nfsm_dissect_xx_nonblock(2 * NFSX_UNSIGNED, md, dpos);
1435 (a)->va_size = fxdr_hyper(tl);
1437 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1440 switch (fxdr_unsigned(int, *tl)) {
1441 case NFSV3SATTRTIME_TOCLIENT:
1442 tl = nfsm_dissect_xx_nonblock(2 * NFSX_UNSIGNED, md, dpos);
1445 fxdr_nfsv3time(tl, &(a)->va_atime);
1448 case NFSV3SATTRTIME_TOSERVER:
1449 getnanotime(&(a)->va_atime);
1450 a->va_vaflags |= VA_UTIMES_NULL;
1453 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1456 switch (fxdr_unsigned(int, *tl)) {
1457 case NFSV3SATTRTIME_TOCLIENT:
1458 tl = nfsm_dissect_xx_nonblock(2 * NFSX_UNSIGNED, md, dpos);
1461 fxdr_nfsv3time(tl, &(a)->va_mtime);
1462 a->va_vaflags &= ~VA_UTIMES_NULL;
1464 case NFSV3SATTRTIME_TOSERVER:
1465 getnanotime(&(a)->va_mtime);
1467 a->va_vaflags |= VA_UTIMES_NULL;