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 nfs_prev_nfssvc_sy_narg;
102 static sy_call_t *nfs_prev_nfssvc_sy_call;
107 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
109 const int nfsrv_nfsv3_procid[NFS_NPROCS] = {
136 * and the reverse mapping from generic to Version 2 procedure numbers
138 const int nfsrvv2_procid[NFS_NPROCS] = {
165 * Maps errno values to nfs error numbers.
166 * Use 0 (which gets converted to NFSERR_IO) as the catch all for ones not
167 * specifically defined in RFC 1094.
169 static const u_char nfsrv_v2errmap[ELAST] = {
170 NFSERR_PERM, NFSERR_NOENT, 0, 0, 0,
171 NFSERR_NXIO, 0, 0, 0, 0,
172 0, 0, NFSERR_ACCES, 0, 0,
173 0, NFSERR_EXIST, 0, NFSERR_NODEV, NFSERR_NOTDIR,
174 NFSERR_ISDIR, 0, 0, 0, 0,
175 0, NFSERR_FBIG, NFSERR_NOSPC, 0, NFSERR_ROFS,
182 0, 0, NFSERR_NAMETOL, 0, 0,
183 NFSERR_NOTEMPTY, 0, 0, NFSERR_DQUOT, NFSERR_STALE,
188 * Maps errno values to nfs error numbers.
189 * Although it is not obvious whether or not NFS clients really care if
190 * a returned error value is in the specified list for the procedure, the
191 * safest thing to do is filter them appropriately. For Version 2, the
192 * X/Open XNFS document is the only specification that defines error values
193 * for each RPC (The RFC simply lists all possible error values for all RPCs),
194 * so I have decided to not do this for Version 2.
195 * The first entry is the default error return and the rest are the valid
196 * errors for that RPC in increasing numeric order.
198 static const short nfsv3err_null[] = {
203 static const short nfsv3err_getattr[] = {
212 static const short nfsv3err_setattr[] = {
228 static const short nfsv3err_lookup[] = {
241 static const short nfsv3err_access[] = {
250 static const short nfsv3err_readlink[] = {
262 static const short nfsv3err_read[] = {
274 static const short nfsv3err_write[] = {
289 static const short nfsv3err_create[] = {
306 static const short nfsv3err_mkdir[] = {
323 static const short nfsv3err_symlink[] = {
340 static const short nfsv3err_mknod[] = {
358 static const short nfsv3err_remove[] = {
372 static const short nfsv3err_rmdir[] = {
390 static const short nfsv3err_rename[] = {
413 static const short nfsv3err_link[] = {
433 static const short nfsv3err_readdir[] = {
446 static const short nfsv3err_readdirplus[] = {
460 static const short nfsv3err_fsstat[] = {
469 static const short nfsv3err_fsinfo[] = {
477 static const short nfsv3err_pathconf[] = {
485 static const short nfsv3err_commit[] = {
494 static const short *nfsrv_v3errmap[] = {
512 nfsv3err_readdirplus,
520 * Called once to initialize data structures...
523 nfsrv_modevent(module_t mod, int type, void *data)
530 mtx_init(&nfsd_mtx, "nfsd_mtx", NULL, MTX_DEF);
531 nfsrv_rpc_vers = txdr_unsigned(RPC_VER2);
532 nfsrv_rpc_call = txdr_unsigned(RPC_CALL);
533 nfsrv_rpc_reply = txdr_unsigned(RPC_REPLY);
534 nfsrv_rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
535 nfsrv_rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
536 nfsrv_rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
537 nfsrv_rpc_autherr = txdr_unsigned(RPC_AUTHERR);
538 nfsrv_rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
539 nfsrv_nfs_prog = txdr_unsigned(NFS_PROG);
540 nfsrv_nfs_true = txdr_unsigned(TRUE);
541 nfsrv_nfs_false = txdr_unsigned(FALSE);
542 nfsrv_nfs_xdrneg1 = txdr_unsigned(-1);
543 nfsrv_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
547 nfsrv_initcache(); /* Init the server request cache */
549 nfsrv_init(0); /* Init server data structures */
551 callout_init(&nfsrv_callout, CALLOUT_MPSAFE);
553 callout_init(&nfsrv_callout, 0);
557 nfs_prev_nfssvc_sy_narg = sysent[SYS_nfssvc].sy_narg;
558 sysent[SYS_nfssvc].sy_narg = 2 | SYF_MPSAFE;
559 nfs_prev_nfssvc_sy_call = sysent[SYS_nfssvc].sy_call;
560 sysent[SYS_nfssvc].sy_call = (sy_call_t *)nfssvc;
564 if (nfsrv_numnfsd != 0) {
569 callout_stop(&nfsrv_callout);
570 sysent[SYS_nfssvc].sy_narg = nfs_prev_nfssvc_sy_narg;
571 sysent[SYS_nfssvc].sy_call = nfs_prev_nfssvc_sy_call;
572 mtx_destroy(&nfsd_mtx);
581 static moduledata_t nfsserver_mod = {
586 DECLARE_MODULE(nfsserver, nfsserver_mod, SI_SUB_VFS, SI_ORDER_ANY);
588 /* So that loader and kldload(2) can find us, wherever we are.. */
589 MODULE_VERSION(nfsserver, 1);
592 * Set up nameidata for a lookup() call and do it.
594 * If pubflag is set, this call is done for a lookup operation on the
595 * public filehandle. In that case we allow crossing mountpoints and
596 * absolute pathnames. However, the caller is expected to check that
597 * the lookup result is within the public fs, and deny access if
600 * nfs_namei() clears out garbage fields that namei() might leave garbage.
601 * This is mainly ni_vp and ni_dvp when an error occurs, and ni_dvp when no
602 * error occurs but the parent was not requested.
604 * dirp may be set whether an error is returned or not, and must be
605 * released by the caller.
608 nfs_namei(struct nameidata *ndp, fhandle_t *fhp, int len,
609 struct nfssvc_sock *slp, struct sockaddr *nam, struct mbuf **mdp,
610 caddr_t *dposp, struct vnode **retdirp, int v3, struct vattr *retdirattrp,
611 int *retdirattr_retp, struct thread *td, int pubflag)
615 char *fromcp, *tocp, *cp;
619 int error, rdonly, linklen;
620 struct componentname *cnp = &ndp->ni_cnd;
621 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0;
625 mtx_lock(&Giant); /* VFS */
628 cnp->cn_flags |= NOMACCHECK;
629 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
632 * Copy the name from the mbuf list to ndp->ni_pnbuf
633 * and set the various ndp fields appropriately.
636 tocp = cnp->cn_pnbuf;
638 rem = mtod(md, caddr_t) + md->m_len - fromcp;
639 for (i = 0; i < len; i++) {
646 fromcp = mtod(md, caddr_t);
649 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
659 len = nfsm_rndup(len)-len;
663 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
668 * Extract and set starting directory.
670 mtx_unlock(&Giant); /* VFS */
672 error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
673 nam, &rdonly, pubflag);
675 mtx_lock(&Giant); /* VFS */
678 if (dp->v_type != VDIR) {
685 cnp->cn_flags |= RDONLY;
688 * Set return directory. Reference to dp is implicitly transfered
689 * to the returned pointer
693 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
694 *retdirattr_retp = VOP_GETATTR(dp, retdirattrp,
695 ndp->ni_cnd.cn_cred, td);
696 VOP_UNLOCK(dp, 0, td);
701 * Oh joy. For WebNFS, handle those pesky '%' escapes,
702 * and the 'native path' indicator.
704 cp = uma_zalloc(namei_zone, M_WAITOK);
705 fromcp = cnp->cn_pnbuf;
707 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
708 switch ((unsigned char)*fromcp) {
709 case WEBNFS_NATIVE_CHAR:
711 * 'Native' path for us is the same
712 * as a path according to the NFS spec,
713 * just skip the escape char.
718 * More may be added in the future, range 0x80-0xff
722 uma_zfree(namei_zone, cp);
727 * Translate the '%' escapes, URL-style.
729 while (*fromcp != '\0') {
730 if (*fromcp == WEBNFS_ESC_CHAR) {
731 if (fromcp[1] != '\0' && fromcp[2] != '\0') {
733 *tocp++ = HEXSTRTOI(fromcp);
738 uma_zfree(namei_zone, cp);
745 uma_zfree(namei_zone, cnp->cn_pnbuf);
749 ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
750 ndp->ni_segflg = UIO_SYSSPACE;
753 ndp->ni_rootdir = rootvnode;
755 if (cnp->cn_pnbuf[0] == '/')
758 cnp->cn_flags |= NOCROSSMOUNT;
762 * Initialize for scan, set ni_startdir and bump ref on dp again
763 * because lookup() will dereference ni_startdir.
768 ndp->ni_startdir = dp;
771 cnp->cn_flags |= LOCKLEAF;
773 cnp->cn_nameptr = cnp->cn_pnbuf;
775 * Call lookup() to do the real work. If an error occurs,
776 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
777 * we do not have to dereference anything before returning.
778 * In either case ni_startdir will be dereferenced and NULLed
786 * Check for encountering a symbolic link. Trivial
787 * termination occurs if no symlink encountered.
788 * Note: zfree is safe because error is 0, so we will
789 * not zfree it again when we break.
791 if ((cnp->cn_flags & ISSYMLINK) == 0) {
792 if (cnp->cn_flags & (SAVENAME | SAVESTART))
793 cnp->cn_flags |= HASBUF;
795 uma_zfree(namei_zone, cnp->cn_pnbuf);
796 if (ndp->ni_vp && !lockleaf)
797 VOP_UNLOCK(ndp->ni_vp, 0, td);
804 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
805 VOP_UNLOCK(ndp->ni_dvp, 0, td);
811 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
815 if (ndp->ni_pathlen > 1)
816 cp = uma_zalloc(namei_zone, M_WAITOK);
820 aiov.iov_len = MAXPATHLEN;
821 auio.uio_iov = &aiov;
824 auio.uio_rw = UIO_READ;
825 auio.uio_segflg = UIO_SYSSPACE;
827 auio.uio_resid = MAXPATHLEN;
828 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
831 if (ndp->ni_pathlen > 1)
832 uma_zfree(namei_zone, cp);
838 linklen = MAXPATHLEN - auio.uio_resid;
843 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
844 error = ENAMETOOLONG;
849 * Adjust or replace path
851 if (ndp->ni_pathlen > 1) {
852 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
853 uma_zfree(namei_zone, cnp->cn_pnbuf);
856 cnp->cn_pnbuf[linklen] = '\0';
857 ndp->ni_pathlen += linklen;
860 * Cleanup refs for next loop and check if root directory
861 * should replace current directory. Normally ni_dvp
862 * becomes the new base directory and is cleaned up when
863 * we loop. Explicitly null pointers after invalidation
864 * to clarify operation.
869 if (cnp->cn_pnbuf[0] == '/') {
871 ndp->ni_dvp = ndp->ni_rootdir;
874 ndp->ni_startdir = ndp->ni_dvp;
878 cnp->cn_flags &= ~LOCKLEAF;
881 * nfs_namei() guarentees that fields will not contain garbage
882 * whether an error occurs or not. This allows the caller to track
883 * cleanup state trivially.
887 uma_zfree(namei_zone, cnp->cn_pnbuf);
890 ndp->ni_startdir = NULL;
891 cnp->cn_flags &= ~HASBUF;
892 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
895 mtx_unlock(&Giant); /* VFS */
901 * A fiddled version of m_adj() that ensures null fill to a long
902 * boundary and only trims off the back end
905 nfsm_adj(struct mbuf *mp, int len, int nul)
911 NFSD_LOCK_DONTCARE();
914 * Trim from tail. Scan the mbuf chain,
915 * calculating its length and finding the last mbuf.
916 * If the adjustment only affects this mbuf, then just
917 * adjust and return. Otherwise, rescan and truncate
918 * after the remaining size.
924 if (m->m_next == NULL)
928 if (m->m_len > len) {
931 cp = mtod(m, caddr_t)+m->m_len-nul;
932 for (i = 0; i < nul; i++)
941 * Correct length for chain is "count".
942 * Find the mbuf with last data, adjust its length,
943 * and toss data from remaining mbufs on chain.
945 for (m = mp; m; m = m->m_next) {
946 if (m->m_len >= count) {
949 cp = mtod(m, caddr_t)+m->m_len-nul;
950 for (i = 0; i < nul; i++)
953 if (m->m_next != NULL) {
964 * Make these functions instead of macros, so that the kernel text size
965 * doesn't get too big...
968 nfsm_srvwcc(struct nfsrv_descript *nfsd, int before_ret,
969 struct vattr *before_vap, int after_ret, struct vattr *after_vap,
970 struct mbuf **mbp, char **bposp)
972 struct mbuf *mb = *mbp;
977 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
978 *tl = nfsrv_nfs_false;
980 tl = nfsm_build(u_int32_t *, 7 * NFSX_UNSIGNED);
981 *tl++ = nfsrv_nfs_true;
982 txdr_hyper(before_vap->va_size, tl);
984 txdr_nfsv3time(&(before_vap->va_mtime), tl);
986 txdr_nfsv3time(&(before_vap->va_ctime), tl);
990 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
994 nfsm_srvpostopattr(struct nfsrv_descript *nfsd, int after_ret,
995 struct vattr *after_vap, struct mbuf **mbp, char **bposp)
997 struct mbuf *mb = *mbp;
1000 struct nfs_fattr *fp;
1003 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
1004 *tl = nfsrv_nfs_false;
1006 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
1007 *tl++ = nfsrv_nfs_true;
1008 fp = (struct nfs_fattr *)tl;
1009 nfsm_srvfattr(nfsd, after_vap, fp);
1016 nfsm_srvfattr(struct nfsrv_descript *nfsd, struct vattr *vap,
1017 struct nfs_fattr *fp)
1020 fp->fa_nlink = txdr_unsigned(vap->va_nlink);
1021 fp->fa_uid = txdr_unsigned(vap->va_uid);
1022 fp->fa_gid = txdr_unsigned(vap->va_gid);
1023 if (nfsd->nd_flag & ND_NFSV3) {
1024 fp->fa_type = vtonfsv3_type(vap->va_type);
1025 fp->fa_mode = vtonfsv3_mode(vap->va_mode);
1026 txdr_hyper(vap->va_size, &fp->fa3_size);
1027 txdr_hyper(vap->va_bytes, &fp->fa3_used);
1028 fp->fa3_rdev.specdata1 = txdr_unsigned(umajor(vap->va_rdev));
1029 fp->fa3_rdev.specdata2 = txdr_unsigned(uminor(vap->va_rdev));
1030 fp->fa3_fsid.nfsuquad[0] = 0;
1031 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
1032 fp->fa3_fileid.nfsuquad[0] = 0;
1033 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
1034 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
1035 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
1036 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
1038 fp->fa_type = vtonfsv2_type(vap->va_type);
1039 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1040 fp->fa2_size = txdr_unsigned(vap->va_size);
1041 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
1042 if (vap->va_type == VFIFO)
1043 fp->fa2_rdev = 0xffffffff;
1045 fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
1046 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
1047 fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
1048 fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
1049 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
1050 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
1051 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
1056 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
1057 * - look up fsid in mount list (if not found ret error)
1058 * - get vp and export rights by calling VFS_FHTOVP()
1059 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
1060 * - if not lockflag unlock it with VOP_UNLOCK()
1063 nfsrv_fhtovp(fhandle_t *fhp, int lockflag, struct vnode **vpp,
1064 struct ucred *cred, struct nfssvc_sock *slp, struct sockaddr *nam,
1065 int *rdonlyp, int pubflag)
1067 struct thread *td = curthread; /* XXX */
1070 struct ucred *credanon;
1072 #ifdef MNT_EXNORESPORT /* XXX needs mountd and /etc/exports help yet */
1073 struct sockaddr_int *saddr;
1080 if (nfs_ispublicfh(fhp)) {
1081 if (!pubflag || !nfs_pub.np_valid)
1083 fhp = &nfs_pub.np_handle;
1086 mp = vfs_getvfs(&fhp->fh_fsid);
1090 mtx_lock(&Giant); /* VFS */
1091 error = VFS_CHECKEXP(mp, nam, &exflags, &credanon);
1094 error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp);
1097 #ifdef MNT_EXNORESPORT
1098 if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
1099 saddr = (struct sockaddr_in *)nam;
1100 if ((saddr->sin_family == AF_INET ||
1101 saddr->sin_family == AF_INET6) &&
1102 /* same code for INET and INET6: sin*_port at same offet */
1103 ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
1106 error = NFSERR_AUTHERR | AUTH_TOOWEAK;
1111 * Check/setup credentials.
1113 if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1114 cred->cr_uid = credanon->cr_uid;
1115 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
1116 cred->cr_groups[i] = credanon->cr_groups[i];
1117 cred->cr_ngroups = i;
1119 if (exflags & MNT_EXRDONLY)
1125 VOP_UNLOCK(*vpp, 0, td);
1127 mtx_unlock(&Giant); /* VFS */
1134 * WebNFS: check if a filehandle is a public filehandle. For v3, this
1135 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
1136 * transformed this to all zeroes in both cases, so check for it.
1139 nfs_ispublicfh(fhandle_t *fhp)
1141 char *cp = (char *)fhp;
1144 NFSD_LOCK_DONTCARE();
1146 for (i = 0; i < NFSX_V3FH; i++)
1153 * This function compares two net addresses by family and returns TRUE
1154 * if they are the same host.
1155 * If there is any doubt, return FALSE.
1156 * The AF_INET family is handled as a special case so that address mbufs
1157 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
1160 netaddr_match(int family, union nethostaddr *haddr, struct sockaddr *nam)
1162 struct sockaddr_in *inetaddr;
1164 NFSD_LOCK_DONTCARE();
1168 inetaddr = (struct sockaddr_in *)nam;
1169 if (inetaddr->sin_family == AF_INET &&
1170 inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
1176 register struct sockaddr_in6 *inet6addr1, *inet6addr2;
1178 inet6addr1 = (struct sockaddr_in6 *)nam;
1179 inet6addr2 = (struct sockaddr_in6 *)haddr->had_nam;
1180 /* XXX - should test sin6_scope_id ? */
1181 if (inet6addr1->sin6_family == AF_INET6 &&
1182 IN6_ARE_ADDR_EQUAL(&inet6addr1->sin6_addr,
1183 &inet6addr2->sin6_addr))
1195 * Map errnos to NFS error numbers. For Version 3 also filter out error
1196 * numbers not specified for the associated procedure.
1199 nfsrv_errmap(struct nfsrv_descript *nd, int err)
1201 const short *defaulterrp, *errp;
1204 NFSD_LOCK_DONTCARE();
1206 if (nd->nd_flag & ND_NFSV3) {
1207 if (nd->nd_procnum <= NFSPROC_COMMIT) {
1208 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
1212 else if (*errp > err)
1215 return ((int)*defaulterrp);
1217 return (err & 0xffff);
1221 e = nfsrv_v2errmap[err - 1];
1228 * Sort the group list in increasing numerical order.
1229 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
1230 * that used to be here.)
1233 nfsrvw_sort(gid_t *list, int num)
1238 NFSD_LOCK_DONTCARE();
1240 /* Insertion sort. */
1241 for (i = 1; i < num; i++) {
1243 /* find correct slot for value v, moving others up */
1244 for (j = i; --j >= 0 && v < list[j];)
1245 list[j + 1] = list[j];
1251 * copy credentials making sure that the result can be compared with bcmp().
1254 nfsrv_setcred(struct ucred *incred, struct ucred *outcred)
1258 NFSD_LOCK_DONTCARE();
1260 bzero((caddr_t)outcred, sizeof (struct ucred));
1261 refcount_init(&outcred->cr_ref, 1);
1262 outcred->cr_uid = incred->cr_uid;
1263 outcred->cr_ngroups = incred->cr_ngroups;
1264 for (i = 0; i < incred->cr_ngroups; i++)
1265 outcred->cr_groups[i] = incred->cr_groups[i];
1266 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
1270 * Helper functions for macros.
1274 nfsm_srvfhtom_xx(fhandle_t *f, int v3, struct mbuf **mb, caddr_t *bpos)
1278 NFSD_LOCK_DONTCARE();
1281 tl = nfsm_build_xx(NFSX_UNSIGNED + NFSX_V3FH, mb, bpos);
1282 *tl++ = txdr_unsigned(NFSX_V3FH);
1283 bcopy(f, tl, NFSX_V3FH);
1285 tl = nfsm_build_xx(NFSX_V2FH, mb, bpos);
1286 bcopy(f, tl, NFSX_V2FH);
1291 nfsm_srvpostop_fh_xx(fhandle_t *f, struct mbuf **mb, caddr_t *bpos)
1295 tl = nfsm_build_xx(2 * NFSX_UNSIGNED + NFSX_V3FH, mb, bpos);
1296 *tl++ = nfsrv_nfs_true;
1297 *tl++ = txdr_unsigned(NFSX_V3FH);
1298 bcopy(f, tl, NFSX_V3FH);
1302 nfsm_srvstrsiz_xx(int *s, int m, struct mbuf **md, caddr_t *dpos)
1306 NFSD_LOCK_DONTCARE();
1308 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1311 *s = fxdr_unsigned(int32_t, *tl);
1312 if (*s > m || *s <= 0)
1318 nfsm_srvnamesiz_xx(int *s, int m, struct mbuf **md, caddr_t *dpos)
1322 NFSD_LOCK_DONTCARE();
1324 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1327 *s = fxdr_unsigned(int32_t, *tl);
1329 return NFSERR_NAMETOL;
1336 nfsm_clget_xx(u_int32_t **tl, struct mbuf *mb, struct mbuf **mp,
1337 char **bp, char **be, caddr_t bpos, int droplock)
1341 NFSD_LOCK_DONTCARE();
1346 NFSD_UNLOCK_ASSERT();
1350 (*mp)->m_len += *bp - bpos;
1353 MGET(nmp, M_TRYWAIT, MT_DATA);
1354 MCLGET(nmp, M_TRYWAIT);
1357 nmp->m_len = NFSMSIZ(nmp);
1358 (*mp)->m_next = nmp;
1360 *bp = mtod(*mp, caddr_t);
1361 *be = *bp + (*mp)->m_len;
1363 *tl = (u_int32_t *)*bp;
1367 nfsm_srvmtofh_xx(fhandle_t *f, struct nfsrv_descript *nfsd, struct mbuf **md,
1373 NFSD_LOCK_DONTCARE();
1375 if (nfsd->nd_flag & ND_NFSV3) {
1376 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1379 fhlen = fxdr_unsigned(int, *tl);
1380 if (fhlen != 0 && fhlen != NFSX_V3FH)
1386 tl = nfsm_dissect_xx_nonblock(fhlen, md, dpos);
1389 bcopy((caddr_t)tl, (caddr_t)(f), fhlen);
1391 bzero((caddr_t)(f), NFSX_V3FH);
1397 nfsm_srvsattr_xx(struct vattr *a, struct mbuf **md, caddr_t *dpos)
1401 NFSD_LOCK_DONTCARE();
1403 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1406 if (*tl == nfsrv_nfs_true) {
1407 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1410 (a)->va_mode = nfstov_mode(*tl);
1412 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1415 if (*tl == nfsrv_nfs_true) {
1416 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1419 (a)->va_uid = fxdr_unsigned(uid_t, *tl);
1421 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1424 if (*tl == nfsrv_nfs_true) {
1425 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1428 (a)->va_gid = fxdr_unsigned(gid_t, *tl);
1430 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1433 if (*tl == nfsrv_nfs_true) {
1434 tl = nfsm_dissect_xx_nonblock(2 * NFSX_UNSIGNED, md, dpos);
1437 (a)->va_size = fxdr_hyper(tl);
1439 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1442 switch (fxdr_unsigned(int, *tl)) {
1443 case NFSV3SATTRTIME_TOCLIENT:
1444 tl = nfsm_dissect_xx_nonblock(2 * NFSX_UNSIGNED, md, dpos);
1447 fxdr_nfsv3time(tl, &(a)->va_atime);
1449 case NFSV3SATTRTIME_TOSERVER:
1450 getnanotime(&(a)->va_atime);
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);
1463 case NFSV3SATTRTIME_TOSERVER:
1464 getnanotime(&(a)->va_mtime);