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;
532 mtx_init(&nfsd_mtx, "nfsd_mtx", NULL, MTX_DEF);
533 nfsrv_rpc_vers = txdr_unsigned(RPC_VER2);
534 nfsrv_rpc_call = txdr_unsigned(RPC_CALL);
535 nfsrv_rpc_reply = txdr_unsigned(RPC_REPLY);
536 nfsrv_rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
537 nfsrv_rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
538 nfsrv_rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
539 nfsrv_rpc_autherr = txdr_unsigned(RPC_AUTHERR);
540 nfsrv_rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
541 nfsrv_nfs_prog = txdr_unsigned(NFS_PROG);
542 nfsrv_nfs_true = txdr_unsigned(TRUE);
543 nfsrv_nfs_false = txdr_unsigned(FALSE);
544 nfsrv_nfs_xdrneg1 = txdr_unsigned(-1);
545 nfsrv_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
549 nfsrv_initcache(); /* Init the server request cache */
551 nfsrv_init(0); /* Init server data structures */
553 callout_init(&nfsrv_callout, CALLOUT_MPSAFE);
555 callout_init(&nfsrv_callout, 0);
559 error = syscall_register(&nfssvc_offset, &nfssvc_sysent,
560 &nfssvc_prev_sysent);
567 if (nfsrv_numnfsd != 0) {
573 syscall_deregister(&nfssvc_offset, &nfssvc_prev_sysent);
574 callout_drain(&nfsrv_callout);
575 nfsrv_destroycache(); /* Free the server request cache */
576 nfsrv_destroycache(); /* Free the server request cache */
577 mtx_destroy(&nfsd_mtx);
586 static moduledata_t nfsserver_mod = {
591 DECLARE_MODULE(nfsserver, nfsserver_mod, SI_SUB_VFS, SI_ORDER_ANY);
593 /* So that loader and kldload(2) can find us, wherever we are.. */
594 MODULE_VERSION(nfsserver, 1);
597 * Set up nameidata for a lookup() call and do it.
599 * If pubflag is set, this call is done for a lookup operation on the
600 * public filehandle. In that case we allow crossing mountpoints and
601 * absolute pathnames. However, the caller is expected to check that
602 * the lookup result is within the public fs, and deny access if
605 * nfs_namei() clears out garbage fields that namei() might leave garbage.
606 * This is mainly ni_vp and ni_dvp when an error occurs, and ni_dvp when no
607 * error occurs but the parent was not requested.
609 * dirp may be set whether an error is returned or not, and must be
610 * released by the caller.
613 nfs_namei(struct nameidata *ndp, fhandle_t *fhp, int len,
614 struct nfssvc_sock *slp, struct sockaddr *nam, struct mbuf **mdp,
615 caddr_t *dposp, struct vnode **retdirp, int v3, struct vattr *retdirattrp,
616 int *retdirattr_retp, struct thread *td, int pubflag)
620 char *fromcp, *tocp, *cp;
624 int error, rdonly, linklen;
625 struct componentname *cnp = &ndp->ni_cnd;
626 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0;
633 cnp->cn_flags |= NOMACCHECK;
634 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
637 * Copy the name from the mbuf list to ndp->ni_pnbuf
638 * and set the various ndp fields appropriately.
641 tocp = cnp->cn_pnbuf;
643 rem = mtod(md, caddr_t) + md->m_len - fromcp;
644 for (i = 0; i < len; i++) {
651 fromcp = mtod(md, caddr_t);
654 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
664 len = nfsm_rndup(len)-len;
668 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
673 * Extract and set starting directory.
675 error = nfsrv_fhtovp(fhp, FALSE, &dp, &dvfslocked,
676 ndp->ni_cnd.cn_cred, slp, nam, &rdonly, pubflag);
679 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
680 if (dp->v_type != VDIR) {
687 cnp->cn_flags |= RDONLY;
690 * Set return directory. Reference to dp is implicitly transfered
691 * to the returned pointer
695 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
696 *retdirattr_retp = VOP_GETATTR(dp, retdirattrp,
697 ndp->ni_cnd.cn_cred, td);
698 VOP_UNLOCK(dp, 0, td);
703 * Oh joy. For WebNFS, handle those pesky '%' escapes,
704 * and the 'native path' indicator.
706 cp = uma_zalloc(namei_zone, M_WAITOK);
707 fromcp = cnp->cn_pnbuf;
709 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
710 switch ((unsigned char)*fromcp) {
711 case WEBNFS_NATIVE_CHAR:
713 * 'Native' path for us is the same
714 * as a path according to the NFS spec,
715 * just skip the escape char.
720 * More may be added in the future, range 0x80-0xff
724 uma_zfree(namei_zone, cp);
729 * Translate the '%' escapes, URL-style.
731 while (*fromcp != '\0') {
732 if (*fromcp == WEBNFS_ESC_CHAR) {
733 if (fromcp[1] != '\0' && fromcp[2] != '\0') {
735 *tocp++ = HEXSTRTOI(fromcp);
740 uma_zfree(namei_zone, cp);
747 uma_zfree(namei_zone, cnp->cn_pnbuf);
751 ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
752 ndp->ni_segflg = UIO_SYSSPACE;
755 ndp->ni_rootdir = rootvnode;
757 if (cnp->cn_pnbuf[0] == '/') {
760 tvfslocked = VFS_LOCK_GIANT(rootvnode->v_mount);
761 VFS_UNLOCK_GIANT(vfslocked);
763 vfslocked = tvfslocked;
766 cnp->cn_flags |= NOCROSSMOUNT;
770 * Initialize for scan, set ni_startdir and bump ref on dp again
771 * because lookup() will dereference ni_startdir.
776 ndp->ni_startdir = dp;
779 cnp->cn_flags |= LOCKLEAF;
781 cnp->cn_nameptr = cnp->cn_pnbuf;
783 * Call lookup() to do the real work. If an error occurs,
784 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
785 * we do not have to dereference anything before returning.
786 * In either case ni_startdir will be dereferenced and NULLed
790 ndp->ni_cnd.cn_flags |= GIANTHELD;
792 vfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
793 ndp->ni_cnd.cn_flags &= ~GIANTHELD;
798 * Check for encountering a symbolic link. Trivial
799 * termination occurs if no symlink encountered.
800 * Note: zfree is safe because error is 0, so we will
801 * not zfree it again when we break.
803 if ((cnp->cn_flags & ISSYMLINK) == 0) {
804 if (cnp->cn_flags & (SAVENAME | SAVESTART))
805 cnp->cn_flags |= HASBUF;
807 uma_zfree(namei_zone, cnp->cn_pnbuf);
808 if (ndp->ni_vp && !lockleaf)
809 VOP_UNLOCK(ndp->ni_vp, 0, td);
816 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
817 VOP_UNLOCK(ndp->ni_dvp, 0, td);
823 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
827 if (ndp->ni_pathlen > 1)
828 cp = uma_zalloc(namei_zone, M_WAITOK);
832 aiov.iov_len = MAXPATHLEN;
833 auio.uio_iov = &aiov;
836 auio.uio_rw = UIO_READ;
837 auio.uio_segflg = UIO_SYSSPACE;
839 auio.uio_resid = MAXPATHLEN;
840 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
843 if (ndp->ni_pathlen > 1)
844 uma_zfree(namei_zone, cp);
850 linklen = MAXPATHLEN - auio.uio_resid;
855 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
856 error = ENAMETOOLONG;
861 * Adjust or replace path
863 if (ndp->ni_pathlen > 1) {
864 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
865 uma_zfree(namei_zone, cnp->cn_pnbuf);
868 cnp->cn_pnbuf[linklen] = '\0';
869 ndp->ni_pathlen += linklen;
872 * Cleanup refs for next loop and check if root directory
873 * should replace current directory. Normally ni_dvp
874 * becomes the new base directory and is cleaned up when
875 * we loop. Explicitly null pointers after invalidation
876 * to clarify operation.
881 if (cnp->cn_pnbuf[0] == '/') {
883 ndp->ni_dvp = ndp->ni_rootdir;
886 ndp->ni_startdir = ndp->ni_dvp;
890 cnp->cn_flags &= ~LOCKLEAF;
891 if (cnp->cn_flags & GIANTHELD) {
893 cnp->cn_flags &= ~GIANTHELD;
897 * nfs_namei() guarentees that fields will not contain garbage
898 * whether an error occurs or not. This allows the caller to track
899 * cleanup state trivially.
903 uma_zfree(namei_zone, cnp->cn_pnbuf);
906 ndp->ni_startdir = NULL;
907 cnp->cn_flags &= ~HASBUF;
908 VFS_UNLOCK_GIANT(vfslocked);
910 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
914 * This differs from normal namei() in that even on failure we may
915 * return with Giant held due to the dirp return. Make sure we only
916 * have not recursed however. The calling code only expects to drop
919 if (vfslocked || dvfslocked)
920 ndp->ni_cnd.cn_flags |= GIANTHELD;
921 if (vfslocked && dvfslocked)
922 VFS_UNLOCK_GIANT(vfslocked);
927 * A fiddled version of m_adj() that ensures null fill to a long
928 * boundary and only trims off the back end
931 nfsm_adj(struct mbuf *mp, int len, int nul)
938 * Trim from tail. Scan the mbuf chain,
939 * calculating its length and finding the last mbuf.
940 * If the adjustment only affects this mbuf, then just
941 * adjust and return. Otherwise, rescan and truncate
942 * after the remaining size.
948 if (m->m_next == NULL)
952 if (m->m_len > len) {
955 cp = mtod(m, caddr_t)+m->m_len-nul;
956 for (i = 0; i < nul; i++)
965 * Correct length for chain is "count".
966 * Find the mbuf with last data, adjust its length,
967 * and toss data from remaining mbufs on chain.
969 for (m = mp; m; m = m->m_next) {
970 if (m->m_len >= count) {
973 cp = mtod(m, caddr_t)+m->m_len-nul;
974 for (i = 0; i < nul; i++)
977 if (m->m_next != NULL) {
988 * Make these functions instead of macros, so that the kernel text size
989 * doesn't get too big...
992 nfsm_srvwcc(struct nfsrv_descript *nfsd, int before_ret,
993 struct vattr *before_vap, int after_ret, struct vattr *after_vap,
994 struct mbuf **mbp, char **bposp)
996 struct mbuf *mb = *mbp;
1001 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
1002 *tl = nfsrv_nfs_false;
1004 tl = nfsm_build(u_int32_t *, 7 * NFSX_UNSIGNED);
1005 *tl++ = nfsrv_nfs_true;
1006 txdr_hyper(before_vap->va_size, tl);
1008 txdr_nfsv3time(&(before_vap->va_mtime), tl);
1010 txdr_nfsv3time(&(before_vap->va_ctime), tl);
1014 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
1018 nfsm_srvpostopattr(struct nfsrv_descript *nfsd, int after_ret,
1019 struct vattr *after_vap, struct mbuf **mbp, char **bposp)
1021 struct mbuf *mb = *mbp;
1022 char *bpos = *bposp;
1024 struct nfs_fattr *fp;
1027 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
1028 *tl = nfsrv_nfs_false;
1030 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
1031 *tl++ = nfsrv_nfs_true;
1032 fp = (struct nfs_fattr *)tl;
1033 nfsm_srvfattr(nfsd, after_vap, fp);
1040 nfsm_srvfattr(struct nfsrv_descript *nfsd, struct vattr *vap,
1041 struct nfs_fattr *fp)
1044 fp->fa_nlink = txdr_unsigned(vap->va_nlink);
1045 fp->fa_uid = txdr_unsigned(vap->va_uid);
1046 fp->fa_gid = txdr_unsigned(vap->va_gid);
1047 if (nfsd->nd_flag & ND_NFSV3) {
1048 fp->fa_type = vtonfsv3_type(vap->va_type);
1049 fp->fa_mode = vtonfsv3_mode(vap->va_mode);
1050 txdr_hyper(vap->va_size, &fp->fa3_size);
1051 txdr_hyper(vap->va_bytes, &fp->fa3_used);
1052 fp->fa3_rdev.specdata1 = txdr_unsigned(umajor(vap->va_rdev));
1053 fp->fa3_rdev.specdata2 = txdr_unsigned(uminor(vap->va_rdev));
1054 fp->fa3_fsid.nfsuquad[0] = 0;
1055 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
1056 fp->fa3_fileid.nfsuquad[0] = 0;
1057 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
1058 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
1059 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
1060 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
1062 fp->fa_type = vtonfsv2_type(vap->va_type);
1063 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1064 fp->fa2_size = txdr_unsigned(vap->va_size);
1065 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
1066 if (vap->va_type == VFIFO)
1067 fp->fa2_rdev = 0xffffffff;
1069 fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
1070 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
1071 fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
1072 fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
1073 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
1074 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
1075 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
1080 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
1081 * - look up fsid in mount list (if not found ret error)
1082 * - get vp and export rights by calling VFS_FHTOVP()
1083 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
1084 * - if not lockflag unlock it with VOP_UNLOCK()
1087 nfsrv_fhtovp(fhandle_t *fhp, int lockflag, struct vnode **vpp, int *vfslockedp,
1088 struct ucred *cred, struct nfssvc_sock *slp, struct sockaddr *nam,
1089 int *rdonlyp, int pubflag)
1091 struct thread *td = curthread; /* XXX */
1094 struct ucred *credanon;
1096 #ifdef MNT_EXNORESPORT /* XXX needs mountd and /etc/exports help yet */
1097 struct sockaddr_int *saddr;
1104 if (nfs_ispublicfh(fhp)) {
1105 if (!pubflag || !nfs_pub.np_valid)
1107 fhp = &nfs_pub.np_handle;
1110 mp = vfs_getvfs(&fhp->fh_fsid);
1113 vfslocked = VFS_LOCK_GIANT(mp);
1114 error = VFS_CHECKEXP(mp, nam, &exflags, &credanon);
1117 error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp);
1120 #ifdef MNT_EXNORESPORT
1121 if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
1122 saddr = (struct sockaddr_in *)nam;
1123 if ((saddr->sin_family == AF_INET ||
1124 saddr->sin_family == AF_INET6) &&
1125 /* same code for INET and INET6: sin*_port at same offet */
1126 ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
1129 error = NFSERR_AUTHERR | AUTH_TOOWEAK;
1134 * Check/setup credentials.
1136 if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1137 cred->cr_uid = credanon->cr_uid;
1138 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
1139 cred->cr_groups[i] = credanon->cr_groups[i];
1140 cred->cr_ngroups = i;
1142 if (exflags & MNT_EXRDONLY)
1148 VOP_UNLOCK(*vpp, 0, td);
1152 VFS_UNLOCK_GIANT(vfslocked);
1154 *vfslockedp = vfslocked;
1160 * WebNFS: check if a filehandle is a public filehandle. For v3, this
1161 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
1162 * transformed this to all zeroes in both cases, so check for it.
1165 nfs_ispublicfh(fhandle_t *fhp)
1167 char *cp = (char *)fhp;
1170 NFSD_LOCK_DONTCARE();
1172 for (i = 0; i < NFSX_V3FH; i++)
1179 * This function compares two net addresses by family and returns TRUE
1180 * if they are the same host.
1181 * If there is any doubt, return FALSE.
1182 * The AF_INET family is handled as a special case so that address mbufs
1183 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
1186 netaddr_match(int family, union nethostaddr *haddr, struct sockaddr *nam)
1188 struct sockaddr_in *inetaddr;
1190 NFSD_LOCK_DONTCARE();
1194 inetaddr = (struct sockaddr_in *)nam;
1195 if (inetaddr->sin_family == AF_INET &&
1196 inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
1202 register struct sockaddr_in6 *inet6addr1, *inet6addr2;
1204 inet6addr1 = (struct sockaddr_in6 *)nam;
1205 inet6addr2 = (struct sockaddr_in6 *)haddr->had_nam;
1206 /* XXX - should test sin6_scope_id ? */
1207 if (inet6addr1->sin6_family == AF_INET6 &&
1208 IN6_ARE_ADDR_EQUAL(&inet6addr1->sin6_addr,
1209 &inet6addr2->sin6_addr))
1221 * Map errnos to NFS error numbers. For Version 3 also filter out error
1222 * numbers not specified for the associated procedure.
1225 nfsrv_errmap(struct nfsrv_descript *nd, int err)
1227 const short *defaulterrp, *errp;
1231 if (nd->nd_flag & ND_NFSV3) {
1232 if (nd->nd_procnum <= NFSPROC_COMMIT) {
1233 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
1237 else if (*errp > err)
1240 return ((int)*defaulterrp);
1242 return (err & 0xffff);
1246 e = nfsrv_v2errmap[err - 1];
1253 * Sort the group list in increasing numerical order.
1254 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
1255 * that used to be here.)
1258 nfsrvw_sort(gid_t *list, int num)
1263 /* Insertion sort. */
1264 for (i = 1; i < num; i++) {
1266 /* find correct slot for value v, moving others up */
1267 for (j = i; --j >= 0 && v < list[j];)
1268 list[j + 1] = list[j];
1274 * copy credentials making sure that the result can be compared with bcmp().
1277 nfsrv_setcred(struct ucred *incred, struct ucred *outcred)
1281 bzero((caddr_t)outcred, sizeof (struct ucred));
1282 refcount_init(&outcred->cr_ref, 1);
1283 outcred->cr_uid = incred->cr_uid;
1284 outcred->cr_ngroups = incred->cr_ngroups;
1285 for (i = 0; i < incred->cr_ngroups; i++)
1286 outcred->cr_groups[i] = incred->cr_groups[i];
1287 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
1291 * Helper functions for macros.
1295 nfsm_srvfhtom_xx(fhandle_t *f, int v3, struct mbuf **mb, caddr_t *bpos)
1300 tl = nfsm_build_xx(NFSX_UNSIGNED + NFSX_V3FH, mb, bpos);
1301 *tl++ = txdr_unsigned(NFSX_V3FH);
1302 bcopy(f, tl, NFSX_V3FH);
1304 tl = nfsm_build_xx(NFSX_V2FH, mb, bpos);
1305 bcopy(f, tl, NFSX_V2FH);
1310 nfsm_srvpostop_fh_xx(fhandle_t *f, struct mbuf **mb, caddr_t *bpos)
1314 tl = nfsm_build_xx(2 * NFSX_UNSIGNED + NFSX_V3FH, mb, bpos);
1315 *tl++ = nfsrv_nfs_true;
1316 *tl++ = txdr_unsigned(NFSX_V3FH);
1317 bcopy(f, tl, NFSX_V3FH);
1321 nfsm_srvstrsiz_xx(int *s, int m, struct mbuf **md, caddr_t *dpos)
1325 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1328 *s = fxdr_unsigned(int32_t, *tl);
1329 if (*s > m || *s <= 0)
1335 nfsm_srvnamesiz_xx(int *s, int m, struct mbuf **md, caddr_t *dpos)
1339 NFSD_LOCK_DONTCARE();
1341 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1344 *s = fxdr_unsigned(int32_t, *tl);
1346 return NFSERR_NAMETOL;
1353 nfsm_srvnamesiz0_xx(int *s, int m, struct mbuf **md, caddr_t *dpos)
1357 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1360 *s = fxdr_unsigned(int32_t, *tl);
1362 return NFSERR_NAMETOL;
1369 nfsm_clget_xx(u_int32_t **tl, struct mbuf *mb, struct mbuf **mp,
1370 char **bp, char **be, caddr_t bpos)
1374 NFSD_UNLOCK_ASSERT();
1378 (*mp)->m_len += *bp - bpos;
1379 MGET(nmp, M_TRYWAIT, MT_DATA);
1380 MCLGET(nmp, M_TRYWAIT);
1381 nmp->m_len = NFSMSIZ(nmp);
1382 (*mp)->m_next = nmp;
1384 *bp = mtod(*mp, caddr_t);
1385 *be = *bp + (*mp)->m_len;
1387 *tl = (u_int32_t *)*bp;
1391 nfsm_srvmtofh_xx(fhandle_t *f, struct nfsrv_descript *nfsd, struct mbuf **md,
1397 if (nfsd->nd_flag & ND_NFSV3) {
1398 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1401 fhlen = fxdr_unsigned(int, *tl);
1402 if (fhlen != 0 && fhlen != NFSX_V3FH)
1408 tl = nfsm_dissect_xx_nonblock(fhlen, md, dpos);
1411 bcopy((caddr_t)tl, (caddr_t)(f), fhlen);
1413 bzero((caddr_t)(f), NFSX_V3FH);
1419 nfsm_srvsattr_xx(struct vattr *a, struct mbuf **md, caddr_t *dpos)
1424 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1427 if (*tl == nfsrv_nfs_true) {
1428 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1431 (a)->va_mode = nfstov_mode(*tl);
1433 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1436 if (*tl == nfsrv_nfs_true) {
1437 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1440 (a)->va_uid = fxdr_unsigned(uid_t, *tl);
1442 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1445 if (*tl == nfsrv_nfs_true) {
1446 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1449 (a)->va_gid = fxdr_unsigned(gid_t, *tl);
1451 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1454 if (*tl == nfsrv_nfs_true) {
1455 tl = nfsm_dissect_xx_nonblock(2 * NFSX_UNSIGNED, md, dpos);
1458 (a)->va_size = fxdr_hyper(tl);
1460 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1463 switch (fxdr_unsigned(int, *tl)) {
1464 case NFSV3SATTRTIME_TOCLIENT:
1465 tl = nfsm_dissect_xx_nonblock(2 * NFSX_UNSIGNED, md, dpos);
1468 fxdr_nfsv3time(tl, &(a)->va_atime);
1471 case NFSV3SATTRTIME_TOSERVER:
1472 getnanotime(&(a)->va_atime);
1473 a->va_vaflags |= VA_UTIMES_NULL;
1476 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
1479 switch (fxdr_unsigned(int, *tl)) {
1480 case NFSV3SATTRTIME_TOCLIENT:
1481 tl = nfsm_dissect_xx_nonblock(2 * NFSX_UNSIGNED, md, dpos);
1484 fxdr_nfsv3time(tl, &(a)->va_mtime);
1485 a->va_vaflags &= ~VA_UTIMES_NULL;
1487 case NFSV3SATTRTIME_TOSERVER:
1488 getnanotime(&(a)->va_mtime);
1490 a->va_vaflags |= VA_UTIMES_NULL;