#include <sys/param.h>
#include <sys/proc.h>
#include <sys/kauth.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/mount_internal.h>
#include <sys/vnode_internal.h>
#include <sys/kpi_mbuf.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/malloc.h>
#include <sys/syscall.h>
#include <sys/ubc_internal.h>
#include <sys/fcntl.h>
#include <sys/uio_internal.h>
#include <sys/domain.h>
#include <libkern/OSAtomic.h>
#include <kern/thread_call.h>
#include <sys/vm.h>
#include <sys/vmparam.h>
#include <sys/time.h>
#include <kern/clock.h>
#include <nfs/rpcv2.h>
#include <nfs/nfsproto.h>
#include <nfs/nfs.h>
#include <nfs/nfsnode.h>
#include <nfs/xdr_subs.h>
#include <nfs/nfsm_subs.h>
#include <nfs/nfs_gss.h>
#include <nfs/nfsmount.h>
#include <nfs/nfs_lock.h>
#include <miscfs/specfs/specdev.h>
#include <netinet/in.h>
#include <net/kpi_interface.h>
struct nfsstats nfsstats;
size_t nfs_mbuf_mhlen = 0, nfs_mbuf_minclsize = 0;
nfstype
vtonfs_type(enum vtype vtype, int nfsvers)
{
switch (vtype) {
case VNON:
return NFNON;
case VREG:
return NFREG;
case VDIR:
return NFDIR;
case VBLK:
return NFBLK;
case VCHR:
return NFCHR;
case VLNK:
return NFLNK;
case VSOCK:
if (nfsvers > NFS_VER2)
return NFSOCK;
case VFIFO:
if (nfsvers > NFS_VER2)
return NFFIFO;
case VBAD:
case VSTR:
case VCPLX:
default:
return NFNON;
}
}
enum vtype
nfstov_type(nfstype nvtype, int nfsvers)
{
switch (nvtype) {
case NFNON:
return VNON;
case NFREG:
return VREG;
case NFDIR:
return VDIR;
case NFBLK:
return VBLK;
case NFCHR:
return VCHR;
case NFLNK:
return VLNK;
case NFSOCK:
if (nfsvers > NFS_VER2)
return VSOCK;
case NFFIFO:
if (nfsvers > NFS_VER2)
return VFIFO;
case NFATTRDIR:
if (nfsvers > NFS_VER3)
return VDIR;
case NFNAMEDATTR:
if (nfsvers > NFS_VER3)
return VREG;
default:
return VNON;
}
}
int
vtonfsv2_mode(enum vtype vtype, mode_t m)
{
if (vtype == VFIFO)
return vnode_makeimode(VCHR, m);
return vnode_makeimode(vtype, m);
}
#if NFSSERVER
int nfsv3_procid[NFS_NPROCS] = {
NFSPROC_NULL,
NFSPROC_GETATTR,
NFSPROC_SETATTR,
NFSPROC_NOOP,
NFSPROC_LOOKUP,
NFSPROC_READLINK,
NFSPROC_READ,
NFSPROC_NOOP,
NFSPROC_WRITE,
NFSPROC_CREATE,
NFSPROC_REMOVE,
NFSPROC_RENAME,
NFSPROC_LINK,
NFSPROC_SYMLINK,
NFSPROC_MKDIR,
NFSPROC_RMDIR,
NFSPROC_READDIR,
NFSPROC_FSSTAT,
NFSPROC_NOOP,
NFSPROC_NOOP,
NFSPROC_NOOP,
NFSPROC_NOOP,
NFSPROC_NOOP
};
#endif
int nfsv2_procid[NFS_NPROCS] = {
NFSV2PROC_NULL,
NFSV2PROC_GETATTR,
NFSV2PROC_SETATTR,
NFSV2PROC_LOOKUP,
NFSV2PROC_NOOP,
NFSV2PROC_READLINK,
NFSV2PROC_READ,
NFSV2PROC_WRITE,
NFSV2PROC_CREATE,
NFSV2PROC_MKDIR,
NFSV2PROC_SYMLINK,
NFSV2PROC_CREATE,
NFSV2PROC_REMOVE,
NFSV2PROC_RMDIR,
NFSV2PROC_RENAME,
NFSV2PROC_LINK,
NFSV2PROC_READDIR,
NFSV2PROC_NOOP,
NFSV2PROC_STATFS,
NFSV2PROC_NOOP,
NFSV2PROC_NOOP,
NFSV2PROC_NOOP,
NFSV2PROC_NOOP
};
void
nfs_mbuf_init(void)
{
struct mbuf_stat ms;
mbuf_stats(&ms);
nfs_mbuf_mhlen = ms.mhlen;
nfs_mbuf_minclsize = ms.minclsize;
}
#if NFSSERVER
int
nfsm_mbuf_get_list(size_t size, mbuf_t *mp, int *mbcnt)
{
int error, cnt;
mbuf_t mhead, mlast, m;
size_t len, mlen;
error = cnt = 0;
mhead = mlast = NULL;
len = 0;
while (len < size) {
nfsm_mbuf_get(error, &m, (size - len));
if (error)
break;
if (!mhead)
mhead = m;
if (mlast && ((error = mbuf_setnext(mlast, m)))) {
mbuf_free(m);
break;
}
mlen = mbuf_maxlen(m);
if ((len + mlen) > size)
mlen = size - len;
mbuf_setlen(m, mlen);
len += mlen;
cnt++;
mlast = m;
}
if (!error) {
*mp = mhead;
*mbcnt = cnt;
}
return (error);
}
#endif
int
nfsm_chain_new_mbuf(struct nfsm_chain *nmc, size_t sizehint)
{
mbuf_t mb;
int error = 0;
if (nmc->nmc_flags & NFSM_CHAIN_FLAG_ADD_CLUSTERS)
sizehint = nfs_mbuf_minclsize;
nfsm_mbuf_get(error, &mb, sizehint);
if (error)
return (error);
if (mb == NULL)
panic("got NULL mbuf?");
if (nmc->nmc_mcur) {
mbuf_setlen(nmc->nmc_mcur, nmc->nmc_ptr - (caddr_t)mbuf_data(nmc->nmc_mcur));
error = mbuf_setnext(nmc->nmc_mcur, mb);
if (error) {
mbuf_free(mb);
return (error);
}
}
nmc->nmc_mcur = mb;
nmc->nmc_ptr = mbuf_data(mb);
nmc->nmc_left = mbuf_trailingspace(mb);
return (0);
}
int
nfsm_chain_add_opaque_f(struct nfsm_chain *nmc, const u_char *buf, uint32_t len)
{
uint32_t paddedlen, tlen;
int error;
paddedlen = nfsm_rndup(len);
while (paddedlen) {
if (!nmc->nmc_left) {
error = nfsm_chain_new_mbuf(nmc, paddedlen);
if (error)
return (error);
}
tlen = MIN(nmc->nmc_left, paddedlen);
if (tlen) {
if (len) {
if (tlen > len)
tlen = len;
bcopy(buf, nmc->nmc_ptr, tlen);
} else {
bzero(nmc->nmc_ptr, tlen);
}
nmc->nmc_ptr += tlen;
nmc->nmc_left -= tlen;
paddedlen -= tlen;
if (len) {
buf += tlen;
len -= tlen;
}
}
}
return (0);
}
int
nfsm_chain_add_opaque_nopad_f(struct nfsm_chain *nmc, const u_char *buf, uint32_t len)
{
uint32_t tlen;
int error;
while (len > 0) {
if (nmc->nmc_left <= 0) {
error = nfsm_chain_new_mbuf(nmc, len);
if (error)
return (error);
}
tlen = MIN(nmc->nmc_left, len);
bcopy(buf, nmc->nmc_ptr, tlen);
nmc->nmc_ptr += tlen;
nmc->nmc_left -= tlen;
len -= tlen;
buf += tlen;
}
return (0);
}
int
nfsm_chain_add_uio(struct nfsm_chain *nmc, struct uio *uiop, uint32_t len)
{
uint32_t paddedlen, tlen;
int error;
paddedlen = nfsm_rndup(len);
while (paddedlen) {
if (!nmc->nmc_left) {
error = nfsm_chain_new_mbuf(nmc, paddedlen);
if (error)
return (error);
}
tlen = MIN(nmc->nmc_left, paddedlen);
if (tlen) {
if (len) {
if (tlen > len)
tlen = len;
uiomove(nmc->nmc_ptr, tlen, uiop);
} else {
bzero(nmc->nmc_ptr, tlen);
}
nmc->nmc_ptr += tlen;
nmc->nmc_left -= tlen;
paddedlen -= tlen;
if (len)
len -= tlen;
}
}
return (0);
}
int
nfsm_chain_offset(struct nfsm_chain *nmc)
{
mbuf_t mb;
int len = 0;
for (mb = nmc->nmc_mhead; mb; mb = mbuf_next(mb)) {
if (mb == nmc->nmc_mcur)
return (len + (nmc->nmc_ptr - (caddr_t) mbuf_data(mb)));
len += mbuf_len(mb);
}
return (len);
}
int
nfsm_chain_advance(struct nfsm_chain *nmc, uint32_t len)
{
mbuf_t mb;
while (len) {
if (nmc->nmc_left >= len) {
nmc->nmc_left -= len;
nmc->nmc_ptr += len;
return (0);
}
len -= nmc->nmc_left;
nmc->nmc_mcur = mb = mbuf_next(nmc->nmc_mcur);
if (!mb)
return (EBADRPC);
nmc->nmc_ptr = mbuf_data(mb);
nmc->nmc_left = mbuf_len(mb);
}
return (0);
}
int
nfsm_chain_reverse(struct nfsm_chain *nmc, uint32_t len)
{
uint32_t mlen, new_offset;
int error = 0;
mlen = nmc->nmc_ptr - (caddr_t) mbuf_data(nmc->nmc_mcur);
if (len <= mlen) {
nmc->nmc_ptr -= len;
nmc->nmc_left += len;
return (0);
}
new_offset = nfsm_chain_offset(nmc) - len;
nfsm_chain_dissect_init(error, nmc, nmc->nmc_mhead);
if (error)
return (error);
return (nfsm_chain_advance(nmc, new_offset));
}
int
nfsm_chain_get_opaque_pointer_f(struct nfsm_chain *nmc, uint32_t len, u_char **pptr)
{
mbuf_t mbcur, mb;
uint32_t left, need, mblen, cplen, padlen;
u_char *ptr;
int error = 0;
while (nmc->nmc_mcur && (nmc->nmc_left == 0)) {
mb = mbuf_next(nmc->nmc_mcur);
nmc->nmc_mcur = mb;
if (!mb)
break;
nmc->nmc_ptr = mbuf_data(mb);
nmc->nmc_left = mbuf_len(mb);
}
if (!nmc->nmc_mcur)
return (EBADRPC);
if (nmc->nmc_left >= len) {
*pptr = (u_char*)nmc->nmc_ptr;
error = nfsm_chain_advance(nmc, nfsm_rndup(len));
return (error);
}
padlen = nfsm_rndup(len) - len;
mbcur = nmc->nmc_mcur;
left = nmc->nmc_left;
need = len - left;
if (need > mbuf_trailingspace(mbcur)) {
nfsm_mbuf_get(error, &mb, len);
if (error)
return (error);
if (mbuf_maxlen(mb) < len) {
mbuf_free(mb);
return (EOVERFLOW);
}
*pptr = ptr = mbuf_data(mb);
bcopy(nmc->nmc_ptr, ptr, left);
ptr += left;
mbuf_setlen(mb, left);
error = mbuf_setnext(mb, mbuf_next(mbcur));
if (!error)
error = mbuf_setnext(mbcur, mb);
if (error) {
mbuf_free(mb);
return (error);
}
mbuf_setlen(mbcur, mbuf_len(mbcur) - left);
nmc->nmc_mcur = mbcur = mb;
nmc->nmc_left = 0;
nmc->nmc_ptr = (caddr_t)ptr;
} else {
*pptr = (u_char*)nmc->nmc_ptr;
nmc->nmc_ptr += left;
nmc->nmc_left = 0;
}
mbuf_setlen(mbcur, mbuf_len(mbcur) + need);
mb = mbuf_next(mbcur);
while (need && mb) {
ptr = mbuf_data(mb);
mblen = mbuf_len(mb);
cplen = MIN(mblen, need);
if (cplen) {
bcopy(ptr, nmc->nmc_ptr, cplen);
error = mbuf_setdata(mb, ptr + cplen, mblen - cplen);
if (error) {
mbuf_setlen(mbcur, mbuf_len(mbcur) - need);
return (error);
}
nmc->nmc_ptr += cplen;
need -= cplen;
}
if (need)
mb = mbuf_next(mb);
}
if (need) {
mbuf_setlen(mbcur, mbuf_len(mbcur) - need);
return (EBADRPC);
}
nmc->nmc_mcur = mb;
nmc->nmc_ptr = mbuf_data(mb);
nmc->nmc_left = mbuf_len(mb);
if (padlen)
error = nfsm_chain_advance(nmc, padlen);
return (error);
}
int
nfsm_chain_get_opaque_f(struct nfsm_chain *nmc, uint32_t len, u_char *buf)
{
uint32_t cplen, padlen;
int error = 0;
padlen = nfsm_rndup(len) - len;
while (len && nmc->nmc_mcur) {
cplen = MIN(nmc->nmc_left, len);
if (cplen) {
bcopy(nmc->nmc_ptr, buf, cplen);
nmc->nmc_ptr += cplen;
nmc->nmc_left -= cplen;
buf += cplen;
len -= cplen;
}
if (len) {
mbuf_t mb = mbuf_next(nmc->nmc_mcur);
nmc->nmc_mcur = mb;
nmc->nmc_ptr = mb ? mbuf_data(mb) : NULL;
nmc->nmc_left = mb ? mbuf_len(mb) : 0;
}
}
if (len)
return (EBADRPC);
if (padlen)
nfsm_chain_adv(error, nmc, padlen);
return (error);
}
int
nfsm_chain_get_uio(struct nfsm_chain *nmc, uint32_t len, struct uio *uiop)
{
uint32_t cplen, padlen;
int error = 0;
padlen = nfsm_rndup(len) - len;
while (len && nmc->nmc_mcur) {
cplen = MIN(nmc->nmc_left, len);
if (cplen) {
error = uiomove(nmc->nmc_ptr, cplen, uiop);
if (error)
return (error);
nmc->nmc_ptr += cplen;
nmc->nmc_left -= cplen;
len -= cplen;
}
if (len) {
mbuf_t mb = mbuf_next(nmc->nmc_mcur);
nmc->nmc_mcur = mb;
nmc->nmc_ptr = mb ? mbuf_data(mb) : NULL;
nmc->nmc_left = mb ? mbuf_len(mb) : 0;
}
}
if (len)
return (EBADRPC);
if (padlen)
nfsm_chain_adv(error, nmc, padlen);
return (error);
}
#if NFSCLIENT
int
nfsm_chain_add_v2sattr_f(struct nfsm_chain *nmc, struct vnode_attr *vap, uint32_t szrdev)
{
int error = 0;
nfsm_chain_add_32(error, nmc, vtonfsv2_mode(vap->va_type,
(VATTR_IS_ACTIVE(vap, va_mode) ? vap->va_mode : 0600)));
nfsm_chain_add_32(error, nmc,
VATTR_IS_ACTIVE(vap, va_uid) ? vap->va_uid : (uint32_t)-1);
nfsm_chain_add_32(error, nmc,
VATTR_IS_ACTIVE(vap, va_gid) ? vap->va_gid : (uint32_t)-1);
nfsm_chain_add_32(error, nmc, szrdev);
nfsm_chain_add_v2time(error, nmc,
VATTR_IS_ACTIVE(vap, va_access_time) ?
&vap->va_access_time : NULL);
nfsm_chain_add_v2time(error, nmc,
VATTR_IS_ACTIVE(vap, va_modify_time) ?
&vap->va_modify_time : NULL);
return (error);
}
int
nfsm_chain_add_v3sattr_f(struct nfsm_chain *nmc, struct vnode_attr *vap)
{
int error = 0;
if (VATTR_IS_ACTIVE(vap, va_mode)) {
nfsm_chain_add_32(error, nmc, TRUE);
nfsm_chain_add_32(error, nmc, vap->va_mode);
} else {
nfsm_chain_add_32(error, nmc, FALSE);
}
if (VATTR_IS_ACTIVE(vap, va_uid)) {
nfsm_chain_add_32(error, nmc, TRUE);
nfsm_chain_add_32(error, nmc, vap->va_uid);
} else {
nfsm_chain_add_32(error, nmc, FALSE);
}
if (VATTR_IS_ACTIVE(vap, va_gid)) {
nfsm_chain_add_32(error, nmc, TRUE);
nfsm_chain_add_32(error, nmc, vap->va_gid);
} else {
nfsm_chain_add_32(error, nmc, FALSE);
}
if (VATTR_IS_ACTIVE(vap, va_data_size)) {
nfsm_chain_add_32(error, nmc, TRUE);
nfsm_chain_add_64(error, nmc, vap->va_data_size);
} else {
nfsm_chain_add_32(error, nmc, FALSE);
}
if (vap->va_vaflags & VA_UTIMES_NULL) {
nfsm_chain_add_32(error, nmc, NFS_TIME_SET_TO_SERVER);
nfsm_chain_add_32(error, nmc, NFS_TIME_SET_TO_SERVER);
} else {
if (VATTR_IS_ACTIVE(vap, va_access_time)) {
nfsm_chain_add_32(error, nmc, NFS_TIME_SET_TO_CLIENT);
nfsm_chain_add_32(error, nmc, vap->va_access_time.tv_sec);
nfsm_chain_add_32(error, nmc, vap->va_access_time.tv_nsec);
} else {
nfsm_chain_add_32(error, nmc, NFS_TIME_DONT_CHANGE);
}
if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
nfsm_chain_add_32(error, nmc, NFS_TIME_SET_TO_CLIENT);
nfsm_chain_add_32(error, nmc, vap->va_modify_time.tv_sec);
nfsm_chain_add_32(error, nmc, vap->va_modify_time.tv_nsec);
} else {
nfsm_chain_add_32(error, nmc, NFS_TIME_DONT_CHANGE);
}
}
return (error);
}
int
nfsm_chain_get_fh_attr(
struct nfsm_chain *nmc,
nfsnode_t dnp,
vfs_context_t ctx,
int nfsvers,
uint64_t *xidp,
fhandle_t *fhp,
struct nfs_vattr *nvap)
{
int error = 0, gotfh, gotattr;
gotfh = gotattr = 1;
if (nfsvers == NFS_VER3)
nfsm_chain_get_32(error, nmc, gotfh);
if (!error && gotfh)
nfsm_chain_get_fh(error, nmc, nfsvers, fhp);
else
fhp->fh_len = 0;
if (nfsvers == NFS_VER3)
nfsm_chain_get_32(error, nmc, gotattr);
nfsmout_if(error);
if (gotattr) {
if (!gotfh)
nfsm_chain_adv(error, nmc, NFSX_V3FATTR);
else
error = nfs_parsefattr(nmc, nfsvers, nvap);
} else if (gotfh) {
if (nfs3_getattr_rpc(NULL, NFSTOMP(dnp), fhp->fh_data, fhp->fh_len, ctx, nvap, xidp)) {
gotattr = 0;
fhp->fh_len = 0;
}
}
nfsmout:
return (error);
}
int
nfsm_chain_get_wcc_data_f(
struct nfsm_chain *nmc,
nfsnode_t np,
struct timespec *premtime,
int *newpostattr,
u_int64_t *xidp)
{
int error = 0;
uint32_t flag = 0;
nfsm_chain_get_32(error, nmc, flag);
if (!error && flag) {
nfsm_chain_adv(error, nmc, 2 * NFSX_UNSIGNED);
nfsm_chain_get_32(error, nmc, premtime->tv_sec);
nfsm_chain_get_32(error, nmc, premtime->tv_nsec);
nfsm_chain_adv(error, nmc, 2 * NFSX_UNSIGNED);
} else {
premtime->tv_sec = 0;
premtime->tv_nsec = 0;
}
nfsm_chain_postop_attr_update_flag(error, nmc, np, *newpostattr, xidp);
return (error);
}
int
nfsm_rpchead(
struct nfsreq *req,
int auth_len,
mbuf_t mrest,
u_int64_t *xidp,
mbuf_t *mreqp)
{
struct nfsmount *nmp = req->r_nmp;
int nfsvers = nmp->nm_vers;
int proc = ((nfsvers == NFS_VER2) ? nfsv2_procid[req->r_procnum] : (int)req->r_procnum);
int auth_type = (!auth_len && !req->r_cred) ? RPCAUTH_NULL : nmp->nm_auth;
return nfsm_rpchead2(nmp->nm_sotype, NFS_PROG, nfsvers, proc,
auth_type, auth_len, req->r_cred, req, mrest, xidp, mreqp);
}
int
nfsm_rpchead2(int sotype, int prog, int vers, int proc, int auth_type, int auth_len,
kauth_cred_t cred, struct nfsreq *req, mbuf_t mrest, u_int64_t *xidp, mbuf_t *mreqp)
{
mbuf_t mreq, mb;
int error, i, grpsiz, authsiz, reqlen;
size_t headlen;
struct timeval tv;
struct nfsm_chain nmreq;
authsiz = nfsm_rndup(auth_len);
headlen = authsiz + 10 * NFSX_UNSIGNED;
if (sotype == SOCK_STREAM)
headlen += NFSX_UNSIGNED;
if (headlen >= nfs_mbuf_minclsize) {
error = mbuf_getpacket(MBUF_WAITOK, &mreq);
} else {
error = mbuf_gethdr(MBUF_WAITOK, MBUF_TYPE_DATA, &mreq);
if (!error) {
if (headlen < nfs_mbuf_mhlen)
mbuf_align_32(mreq, headlen);
else
mbuf_align_32(mreq, 8 * NFSX_UNSIGNED);
}
}
if (error) {
return (error);
}
if (*xidp == 0) {
lck_mtx_lock(nfs_request_mutex);
if (!nfs_xid) {
microtime(&tv);
nfs_xid = tv.tv_sec << 12;
}
if (++nfs_xid == 0) {
nfs_xidwrap++;
nfs_xid++;
}
*xidp = nfs_xid + ((u_int64_t)nfs_xidwrap << 32);
lck_mtx_unlock(nfs_request_mutex);
}
nmreq.nmc_mcur = nmreq.nmc_mhead = mreq;
nmreq.nmc_ptr = mbuf_data(nmreq.nmc_mcur);
nmreq.nmc_left = mbuf_trailingspace(nmreq.nmc_mcur);
if (sotype == SOCK_STREAM)
nfsm_chain_add_32(error, &nmreq, 0);
nfsm_chain_add_32(error, &nmreq, (*xidp & 0xffffffff));
nfsm_chain_add_32(error, &nmreq, RPC_CALL);
nfsm_chain_add_32(error, &nmreq, RPC_VER2);
nfsm_chain_add_32(error, &nmreq, prog);
nfsm_chain_add_32(error, &nmreq, vers);
nfsm_chain_add_32(error, &nmreq, proc);
add_cred:
switch (auth_type) {
case RPCAUTH_NULL:
nfsm_chain_add_32(error, &nmreq, RPCAUTH_NULL);
nfsm_chain_add_32(error, &nmreq, 0);
nfsm_chain_add_32(error, &nmreq, RPCAUTH_NULL);
nfsm_chain_add_32(error, &nmreq, 0);
nfsm_chain_build_done(error, &nmreq);
break;
case RPCAUTH_UNIX:
nfsm_chain_add_32(error, &nmreq, RPCAUTH_UNIX);
nfsm_chain_add_32(error, &nmreq, authsiz);
nfsm_chain_add_32(error, &nmreq, 0);
nfsm_chain_add_32(error, &nmreq, 0);
nfsm_chain_add_32(error, &nmreq, kauth_cred_getuid(cred));
nfsm_chain_add_32(error, &nmreq, cred->cr_groups[0]);
grpsiz = (auth_len >> 2) - 5;
nfsm_chain_add_32(error, &nmreq, grpsiz);
for (i = 1; i <= grpsiz; i++)
nfsm_chain_add_32(error, &nmreq, cred->cr_groups[i]);
nfsm_chain_add_32(error, &nmreq, RPCAUTH_NULL);
nfsm_chain_add_32(error, &nmreq, 0);
nfsm_chain_build_done(error, &nmreq);
if (!error)
error = mbuf_setnext(nmreq.nmc_mcur, mrest);
break;
case RPCAUTH_KRB5:
case RPCAUTH_KRB5I:
case RPCAUTH_KRB5P:
error = nfs_gss_clnt_cred_put(req, &nmreq, mrest);
if (error == ENEEDAUTH) {
error = 0;
auth_type = RPCAUTH_UNIX;
goto add_cred;
}
break;
};
if (!error)
error = mbuf_pkthdr_setrcvif(mreq, 0);
if (error) {
mbuf_freem(mreq);
return (error);
}
reqlen = 0;
for (mb = nmreq.nmc_mhead; mb; mb = mbuf_next(mb))
reqlen += mbuf_len(mb);
mbuf_pkthdr_setlen(mreq, reqlen);
if (sotype == SOCK_STREAM)
nfsm_chain_set_recmark(error, &nmreq,
(reqlen - NFSX_UNSIGNED) | 0x80000000);
*mreqp = mreq;
return (0);
}
int
nfs_parsefattr(struct nfsm_chain *nmc, int nfsvers, struct nfs_vattr *nvap)
{
int error = 0;
enum vtype vtype;
u_short vmode;
uint32_t val, val2;
dev_t rdev;
val = val2 = 0;
nfsm_chain_get_32(error, nmc, vtype);
nfsm_chain_get_32(error, nmc, vmode);
nfsmout_if(error);
if (nfsvers == NFS_VER3) {
nvap->nva_type = nfstov_type(vtype, nfsvers);
} else {
vtype = nfstov_type(vtype, nfsvers);
if ((vtype == VNON) || ((vtype == VREG) && ((vmode & S_IFMT) != 0)))
vtype = IFTOVT(vmode);
nvap->nva_type = vtype;
}
nvap->nva_mode = (vmode & 07777);
nfsm_chain_get_32(error, nmc, nvap->nva_nlink);
nfsm_chain_get_32(error, nmc, nvap->nva_uid);
nfsm_chain_get_32(error, nmc, nvap->nva_gid);
if (nfsvers == NFS_VER3) {
nfsm_chain_get_64(error, nmc, nvap->nva_size);
nfsm_chain_get_64(error, nmc, nvap->nva_bytes);
nfsm_chain_get_32(error, nmc, nvap->nva_rawdev.specdata1);
nfsm_chain_get_32(error, nmc, nvap->nva_rawdev.specdata2);
nfsmout_if(error);
nfsm_chain_get_64(error, nmc, nvap->nva_fsid.major);
nvap->nva_fsid.minor = 0;
nfsm_chain_get_64(error, nmc, nvap->nva_fileid);
} else {
nfsm_chain_get_32(error, nmc, nvap->nva_size);
nfsm_chain_adv(error, nmc, NFSX_UNSIGNED);
nfsm_chain_get_32(error, nmc, rdev);
nfsmout_if(error);
nvap->nva_rawdev.specdata1 = major(rdev);
nvap->nva_rawdev.specdata2 = minor(rdev);
nfsm_chain_get_32(error, nmc, val);
nfsmout_if(error);
nvap->nva_bytes = val * NFS_FABLKSIZE;
nfsm_chain_get_32(error, nmc, val);
nfsmout_if(error);
nvap->nva_fsid.major = (uint64_t)val;
nvap->nva_fsid.minor = 0;
nfsm_chain_get_32(error, nmc, val);
nfsmout_if(error);
nvap->nva_fileid = (uint64_t)val;
if ((vtype == VCHR) && (rdev == (dev_t)0xffffffff))
nvap->nva_type = VFIFO;
}
nfsm_chain_get_time(error, nmc, nfsvers,
nvap->nva_timesec[NFSTIME_ACCESS],
nvap->nva_timensec[NFSTIME_ACCESS]);
nfsm_chain_get_time(error, nmc, nfsvers,
nvap->nva_timesec[NFSTIME_MODIFY],
nvap->nva_timensec[NFSTIME_MODIFY]);
nfsm_chain_get_time(error, nmc, nfsvers,
nvap->nva_timesec[NFSTIME_CHANGE],
nvap->nva_timensec[NFSTIME_CHANGE]);
nfsmout:
return (error);
}
int
nfs_loadattrcache(
nfsnode_t np,
struct nfs_vattr *nvap,
u_int64_t *xidp,
int dontshrink)
{
mount_t mp;
vnode_t vp;
struct timeval now;
struct nfs_vattr *npnvap;
if (np->n_hflag & NHINIT) {
vp = NULL;
mp = np->n_mount;
} else {
vp = NFSTOV(np);
mp = vnode_mount(vp);
}
FSDBG_TOP(527, np, vp, *xidp >> 32, *xidp);
if (!VFSTONFS(mp)) {
FSDBG_BOT(527, ENXIO, 1, 0, *xidp);
return (ENXIO);
}
if (*xidp < np->n_xid) {
NATTRINVALIDATE(np);
FSDBG_BOT(527, 0, np, np->n_xid, *xidp);
*xidp = 0;
return (0);
}
if (vp && (nvap->nva_type != vnode_vtype(vp))) {
printf("nfs loadattrcache vnode changed type, was %d now %d\n",
vnode_vtype(vp), nvap->nva_type);
FSDBG_BOT(527, ESTALE, 3, 0, *xidp);
return (ESTALE);
}
microuptime(&now);
np->n_attrstamp = now.tv_sec;
np->n_xid = *xidp;
npnvap = &np->n_vattr;
bcopy((caddr_t)nvap, (caddr_t)npnvap, sizeof(*nvap));
if (nvap->nva_size != np->n_size) {
np->n_newsize = nvap->nva_size;
FSDBG(527, np, nvap->nva_size, np->n_size, (nvap->nva_type == VREG) | (np->n_flag & NMODIFIED ? 6 : 4));
SET(np->n_flag, NUPDATESIZE);
if (vp && (nvap->nva_type == VREG)) {
if (!UBCINFOEXISTS(vp) || (dontshrink && (np->n_newsize < np->n_size))) {
FSDBG(527, np, np->n_size, np->n_vattr.nva_size, 0xf00d0001);
nvap->nva_size = np->n_size;
CLR(np->n_flag, NUPDATESIZE);
NATTRINVALIDATE(np);
} else if ((np->n_flag & NMODIFIED) && (nvap->nva_size < np->n_size)) {
FSDBG(527, np, np->n_size, np->n_vattr.nva_size, 0xf00d0002);
nvap->nva_size = np->n_size;
CLR(np->n_flag, NUPDATESIZE);
}
}
}
if (np->n_flag & NCHG) {
if (np->n_flag & NACC) {
nvap->nva_timesec[NFSTIME_ACCESS] = np->n_atim.tv_sec;
nvap->nva_timensec[NFSTIME_ACCESS] = np->n_atim.tv_nsec;
}
if (np->n_flag & NUPD) {
nvap->nva_timesec[NFSTIME_MODIFY] = np->n_mtim.tv_sec;
nvap->nva_timensec[NFSTIME_MODIFY] = np->n_mtim.tv_nsec;
}
}
FSDBG_BOT(527, 0, np, np->n_size, *xidp);
return (0);
}
int
nfs_attrcachetimeout(nfsnode_t np)
{
struct nfsmount *nmp;
struct timeval now;
int isdir, timeo;
if (!(nmp = NFSTONMP(np)))
return (0);
isdir = vnode_isdir(NFSTOV(np));
if ((np)->n_flag & NMODIFIED)
timeo = isdir ? nmp->nm_acdirmin : nmp->nm_acregmin;
else {
microtime(&now);
timeo = (now.tv_sec - (np)->n_mtime.tv_sec) / 10;
if (isdir) {
if (timeo < nmp->nm_acdirmin)
timeo = nmp->nm_acdirmin;
else if (timeo > nmp->nm_acdirmax)
timeo = nmp->nm_acdirmax;
} else {
if (timeo < nmp->nm_acregmin)
timeo = nmp->nm_acregmin;
else if (timeo > nmp->nm_acregmax)
timeo = nmp->nm_acregmax;
}
}
return (timeo);
}
int
nfs_getattrcache(nfsnode_t np, struct nfs_vattr *nvaper, int alreadylocked)
{
struct nfs_vattr *nvap;
struct timeval nowup;
int32_t timeo;
if (!alreadylocked && nfs_lock(np, NFS_NODE_LOCK_SHARED)) {
FSDBG(528, np, 0, 0xffffff00, ENOENT);
OSAddAtomic(1, (SInt32*)&nfsstats.attrcache_misses);
return (ENOENT);
}
if (!NATTRVALID(np)) {
if (!alreadylocked)
nfs_unlock(np);
FSDBG(528, np, 0, 0xffffff01, ENOENT);
OSAddAtomic(1, (SInt32*)&nfsstats.attrcache_misses);
return (ENOENT);
}
timeo = nfs_attrcachetimeout(np);
microuptime(&nowup);
if ((nowup.tv_sec - np->n_attrstamp) >= timeo) {
if (!alreadylocked)
nfs_unlock(np);
FSDBG(528, np, 0, 0xffffff02, ENOENT);
OSAddAtomic(1, (SInt32*)&nfsstats.attrcache_misses);
return (ENOENT);
}
nvap = &np->n_vattr;
FSDBG(528, np, nvap->nva_size, np->n_size, 0xcace);
OSAddAtomic(1, (SInt32*)&nfsstats.attrcache_hits);
if (nvap->nva_size != np->n_size) {
if (!alreadylocked) {
if (lck_rw_lock_shared_to_exclusive(&np->n_lock) == FALSE)
lck_rw_lock_exclusive(&np->n_lock);
}
np->n_newsize = nvap->nva_size;
FSDBG(528, np, nvap->nva_size, np->n_size, (nvap->nva_type == VREG) | (np->n_flag & NMODIFIED ? 6 : 4));
SET(np->n_flag, NUPDATESIZE);
if ((nvap->nva_type == VREG) && (np->n_flag & NMODIFIED) &&
(nvap->nva_size < np->n_size)) {
nvap->nva_size = np->n_size;
CLR(np->n_flag, NUPDATESIZE);
}
}
bcopy((caddr_t)nvap, (caddr_t)nvaper, sizeof(struct nfs_vattr));
if (np->n_flag & NCHG) {
if (np->n_flag & NACC) {
nvaper->nva_timesec[NFSTIME_ACCESS] = np->n_atim.tv_sec;
nvaper->nva_timensec[NFSTIME_ACCESS] = np->n_atim.tv_nsec;
}
if (np->n_flag & NUPD) {
nvaper->nva_timesec[NFSTIME_MODIFY] = np->n_mtim.tv_sec;
nvaper->nva_timensec[NFSTIME_MODIFY] = np->n_mtim.tv_nsec;
}
}
if (!alreadylocked)
nfs_unlock(np);
return (0);
}
static nfsuint64 nfs_nullcookie = { { 0, 0 } };
nfsuint64 *
nfs_getcookie(nfsnode_t dnp, off_t off, int add)
{
struct nfsdmap *dp, *dp2;
int pos;
pos = off / NFS_DIRBLKSIZ;
if (pos == 0)
return (&nfs_nullcookie);
pos--;
dp = dnp->n_cookies.lh_first;
if (!dp) {
if (add) {
MALLOC_ZONE(dp, struct nfsdmap *, sizeof(struct nfsdmap),
M_NFSDIROFF, M_WAITOK);
if (!dp)
return ((nfsuint64 *)0);
dp->ndm_eocookie = 0;
LIST_INSERT_HEAD(&dnp->n_cookies, dp, ndm_list);
} else
return ((nfsuint64 *)0);
}
while (pos >= NFSNUMCOOKIES) {
pos -= NFSNUMCOOKIES;
if (dp->ndm_list.le_next) {
if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
pos >= dp->ndm_eocookie)
return ((nfsuint64 *)0);
dp = dp->ndm_list.le_next;
} else if (add) {
MALLOC_ZONE(dp2, struct nfsdmap *, sizeof(struct nfsdmap),
M_NFSDIROFF, M_WAITOK);
if (!dp2)
return ((nfsuint64 *)0);
dp2->ndm_eocookie = 0;
LIST_INSERT_AFTER(dp, dp2, ndm_list);
dp = dp2;
} else
return ((nfsuint64 *)0);
}
if (pos >= dp->ndm_eocookie) {
if (add)
dp->ndm_eocookie = pos + 1;
else
return ((nfsuint64 *)0);
}
return (&dp->ndm_cookies[pos]);
}
void
nfs_invaldir(nfsnode_t dnp)
{
if (vnode_vtype(NFSTOV(dnp)) != VDIR) {
printf("nfs: invaldir not dir\n");
return;
}
dnp->n_direofoffset = 0;
dnp->n_cookieverf.nfsuquad[0] = 0;
dnp->n_cookieverf.nfsuquad[1] = 0;
if (dnp->n_cookies.lh_first)
dnp->n_cookies.lh_first->ndm_eocookie = 0;
}
#endif
void
nfs_interval_timer_start(thread_call_t call, int interval)
{
uint64_t deadline;
clock_interval_to_deadline(interval, 1000 * 1000, &deadline);
thread_call_enter_delayed(call, deadline);
}
#if NFSSERVER
static void nfsrv_init_user_list(struct nfs_active_user_list *);
static void nfsrv_free_user_list(struct nfs_active_user_list *);
int
nfsm_chain_add_wcc_data_f(
struct nfsrv_descript *nd,
struct nfsm_chain *nmc,
int preattrerr,
struct vnode_attr *prevap,
int postattrerr,
struct vnode_attr *postvap)
{
int error = 0;
if (preattrerr) {
nfsm_chain_add_32(error, nmc, FALSE);
} else {
nfsm_chain_add_32(error, nmc, TRUE);
nfsm_chain_add_64(error, nmc, prevap->va_data_size);
nfsm_chain_add_time(error, nmc, NFS_VER3, &prevap->va_modify_time);
nfsm_chain_add_time(error, nmc, NFS_VER3, &prevap->va_change_time);
}
nfsm_chain_add_postop_attr(error, nd, nmc, postattrerr, postvap);
return (error);
}
int
nfsm_chain_get_path_namei(
struct nfsm_chain *nmc,
uint32_t len,
struct nameidata *nip)
{
struct componentname *cnp = &nip->ni_cnd;
int error = 0;
char *cp;
if (len > (MAXPATHLEN - 1))
return (ENAMETOOLONG);
MALLOC_ZONE(cnp->cn_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
if (!cnp->cn_pnbuf)
return (ENOMEM);
cnp->cn_pnlen = MAXPATHLEN;
cnp->cn_flags |= HASBUF;
cp = cnp->cn_pnbuf;
nfsm_chain_get_opaque(error, nmc, len, cp);
if (error)
goto out;
cnp->cn_pnbuf[len] = '\0';
if ((strlen(cp) != len) || strchr(cp, '/'))
error = EACCES;
out:
if (error) {
if (cnp->cn_pnbuf)
FREE_ZONE(cnp->cn_pnbuf, MAXPATHLEN, M_NAMEI);
cnp->cn_flags &= ~HASBUF;
} else {
nip->ni_pathlen = len;
}
return (error);
}
int
nfsrv_namei(
struct nfsrv_descript *nd,
vfs_context_t ctx,
struct nameidata *nip,
struct nfs_filehandle *nfhp,
vnode_t *retdirp,
struct nfs_export **nxp,
struct nfs_export_options **nxop)
{
vnode_t dp;
int error;
struct componentname *cnp = &nip->ni_cnd;
char *tmppn;
*retdirp = NULL;
error = nfsrv_fhtovp(nfhp, nd, &dp, nxp, nxop);
if (error)
goto out;
error = nfsrv_credcheck(nd, ctx, *nxp, *nxop);
if (error || (vnode_vtype(dp) != VDIR)) {
vnode_put(dp);
error = ENOTDIR;
goto out;
}
*retdirp = dp;
nip->ni_cnd.cn_context = ctx;
if (*nxop && ((*nxop)->nxo_flags & NX_READONLY))
cnp->cn_flags |= RDONLY;
cnp->cn_flags |= NOCROSSMOUNT;
cnp->cn_nameptr = cnp->cn_pnbuf;
nip->ni_usedvp = nip->ni_startdir = dp;
error = lookup(nip);
if (error)
goto out;
if (cnp->cn_flags & ISSYMLINK) {
if ((cnp->cn_flags & FSNODELOCKHELD)) {
cnp->cn_flags &= ~FSNODELOCKHELD;
unlock_fsnode(nip->ni_dvp, NULL);
}
if (cnp->cn_flags & (LOCKPARENT | WANTPARENT))
vnode_put(nip->ni_dvp);
if (nip->ni_vp) {
vnode_put(nip->ni_vp);
nip->ni_vp = NULL;
}
error = EINVAL;
}
out:
if (error) {
tmppn = cnp->cn_pnbuf;
cnp->cn_pnbuf = NULL;
cnp->cn_flags &= ~HASBUF;
FREE_ZONE(tmppn, cnp->cn_pnlen, M_NAMEI);
}
return (error);
}
void
nfsm_adj(mbuf_t mp, int len, int nul)
{
mbuf_t m, mnext;
int count, i, mlen;
char *cp;
count = 0;
m = mp;
for (;;) {
mlen = mbuf_len(m);
count += mlen;
mnext = mbuf_next(m);
if (mnext == NULL)
break;
m = mnext;
}
if (mlen > len) {
mlen -= len;
mbuf_setlen(m, mlen);
if (nul > 0) {
cp = (caddr_t)mbuf_data(m) + mlen - nul;
for (i = 0; i < nul; i++)
*cp++ = '\0';
}
return;
}
count -= len;
if (count < 0)
count = 0;
for (m = mp; m; m = mbuf_next(m)) {
mlen = mbuf_len(m);
if (mlen >= count) {
mlen = count;
mbuf_setlen(m, count);
if (nul > 0) {
cp = (caddr_t)mbuf_data(m) + mlen - nul;
for (i = 0; i < nul; i++)
*cp++ = '\0';
}
break;
}
count -= mlen;
}
for (m = mbuf_next(m); m; m = mbuf_next(m))
mbuf_setlen(m, 0);
}
int
nfsm_chain_trim_data(struct nfsm_chain *nmc, int len, int *mlen)
{
int cnt = 0, dlen, adjust;
caddr_t data;
mbuf_t m;
if (mlen)
*mlen = 0;
for (m = nmc->nmc_mhead; m && (m != nmc->nmc_mcur); m = mbuf_next(m))
mbuf_setlen(m, 0);
if (!m)
return (EIO);
data = mbuf_data(m);
dlen = mbuf_len(m);
adjust = nmc->nmc_ptr - data;
dlen -= adjust;
if ((dlen > 0) && (adjust > 0)) {
if (mbuf_setdata(m, nmc->nmc_ptr, dlen))
return(EIO);
} else
mbuf_setlen(m, dlen);
for (; m && (cnt < len); m = mbuf_next(m)) {
dlen = mbuf_len(m);
cnt += dlen;
if (cnt > len) {
mbuf_setlen(m, dlen - (cnt - len));
if (m == nmc->nmc_mcur)
nmc->nmc_left -= (cnt - len);
cnt = len;
}
}
if (mlen)
*mlen = cnt;
if (m == nmc->nmc_mcur)
nmc->nmc_left = 0;
for (; m; m = mbuf_next(m))
mbuf_setlen(m, 0);
return (0);
}
int
nfsm_chain_add_fattr(
struct nfsrv_descript *nd,
struct nfsm_chain *nmc,
struct vnode_attr *vap)
{
int error = 0;
nfsm_chain_add_32(error, nmc, vtonfs_type(vap->va_type, nd->nd_vers));
if (nd->nd_vers == NFS_VER3) {
nfsm_chain_add_32(error, nmc, vap->va_mode & 07777);
} else {
nfsm_chain_add_32(error, nmc, vtonfsv2_mode(vap->va_type, vap->va_mode));
}
nfsm_chain_add_32(error, nmc, vap->va_nlink);
nfsm_chain_add_32(error, nmc, vap->va_uid);
nfsm_chain_add_32(error, nmc, vap->va_gid);
if (nd->nd_vers == NFS_VER3) {
nfsm_chain_add_64(error, nmc, vap->va_data_size);
nfsm_chain_add_64(error, nmc, vap->va_data_alloc);
nfsm_chain_add_32(error, nmc, major(vap->va_rdev));
nfsm_chain_add_32(error, nmc, minor(vap->va_rdev));
nfsm_chain_add_64(error, nmc, vap->va_fsid);
nfsm_chain_add_64(error, nmc, vap->va_fileid);
} else {
nfsm_chain_add_32(error, nmc, vap->va_data_size);
nfsm_chain_add_32(error, nmc, NFS_FABLKSIZE);
if (vap->va_type == VFIFO)
nfsm_chain_add_32(error, nmc, 0xffffffff);
else
nfsm_chain_add_32(error, nmc, vap->va_rdev);
nfsm_chain_add_32(error, nmc, vap->va_data_alloc / NFS_FABLKSIZE);
nfsm_chain_add_32(error, nmc, vap->va_fsid);
nfsm_chain_add_32(error, nmc, vap->va_fileid);
}
nfsm_chain_add_time(error, nmc, nd->nd_vers, &vap->va_access_time);
nfsm_chain_add_time(error, nmc, nd->nd_vers, &vap->va_modify_time);
nfsm_chain_add_time(error, nmc, nd->nd_vers, &vap->va_change_time);
return (error);
}
int
nfsm_chain_get_sattr(
struct nfsrv_descript *nd,
struct nfsm_chain *nmc,
struct vnode_attr *vap)
{
int error = 0, nullflag = 0;
uint32_t val = 0;
uint64_t val64;
struct timespec now;
if (nd->nd_vers == NFS_VER2) {
nfsm_chain_get_32(error, nmc, val);
if ((val & 0xffff) != 0xffff) {
VATTR_SET(vap, va_mode, val & 07777);
VATTR_SET(vap, va_type, IFTOVT(val));
VATTR_CLEAR_ACTIVE(vap, va_type);
}
nfsm_chain_get_32(error, nmc, val);
if (val != (uint32_t)-1)
VATTR_SET(vap, va_uid, val);
nfsm_chain_get_32(error, nmc, val);
if (val != (uint32_t)-1)
VATTR_SET(vap, va_gid, val);
nfsm_chain_get_32(error, nmc, val);
VATTR_SET(vap, va_data_size, val);
if (val == (uint32_t)-1)
VATTR_CLEAR_ACTIVE(vap, va_data_size);
nfsm_chain_get_time(error, nmc, NFS_VER2,
vap->va_access_time.tv_sec,
vap->va_access_time.tv_nsec);
if (vap->va_access_time.tv_sec != -1)
VATTR_SET_ACTIVE(vap, va_access_time);
nfsm_chain_get_time(error, nmc, NFS_VER2,
vap->va_modify_time.tv_sec,
vap->va_modify_time.tv_nsec);
if (vap->va_modify_time.tv_sec != -1)
VATTR_SET_ACTIVE(vap, va_modify_time);
return (error);
}
nfsm_chain_get_32(error, nmc, val);
if (val) {
nfsm_chain_get_32(error, nmc, val);
VATTR_SET(vap, va_mode, val & 07777);
}
nfsm_chain_get_32(error, nmc, val);
if (val) {
nfsm_chain_get_32(error, nmc, val);
VATTR_SET(vap, va_uid, val);
}
nfsm_chain_get_32(error, nmc, val);
if (val) {
nfsm_chain_get_32(error, nmc, val);
VATTR_SET(vap, va_gid, val);
}
nfsm_chain_get_32(error, nmc, val);
if (val) {
nfsm_chain_get_64(error, nmc, val64);
VATTR_SET(vap, va_data_size, val64);
}
nanotime(&now);
nfsm_chain_get_32(error, nmc, val);
switch (val) {
case NFS_TIME_SET_TO_CLIENT:
nfsm_chain_get_time(error, nmc, nd->nd_vers,
vap->va_access_time.tv_sec,
vap->va_access_time.tv_nsec);
VATTR_SET_ACTIVE(vap, va_access_time);
break;
case NFS_TIME_SET_TO_SERVER:
VATTR_SET(vap, va_access_time, now);
nullflag = VA_UTIMES_NULL;
break;
}
nfsm_chain_get_32(error, nmc, val);
switch (val) {
case NFS_TIME_SET_TO_CLIENT:
nfsm_chain_get_time(error, nmc, nd->nd_vers,
vap->va_modify_time.tv_sec,
vap->va_modify_time.tv_nsec);
VATTR_SET_ACTIVE(vap, va_modify_time);
break;
case NFS_TIME_SET_TO_SERVER:
VATTR_SET(vap, va_modify_time, now);
vap->va_vaflags |= nullflag;
break;
}
return (error);
}
static int
nfsrv_cmp_secflavs(struct nfs_sec *sf1, struct nfs_sec *sf2)
{
int i;
if (sf1->count != sf2->count)
return 1;
for (i = 0; i < sf1->count; i++)
if (sf1->flavors[i] != sf2->flavors[i])
return 1;
return 0;
}
static int
nfsrv_hang_addrlist(struct nfs_export *nx, struct user_nfs_export_args *unxa)
{
struct nfs_export_net_args nxna;
struct nfs_netopt *no, *rn_no;
struct radix_node_head *rnh;
struct radix_node *rn;
struct sockaddr *saddr, *smask;
struct domain *dom;
int i, error;
unsigned int net;
user_addr_t uaddr;
kauth_cred_t cred;
struct ucred temp_cred;
uaddr = unxa->nxa_nets;
for (net = 0; net < unxa->nxa_netcount; net++, uaddr += sizeof(nxna)) {
error = copyin(uaddr, &nxna, sizeof(nxna));
if (error)
return (error);
if (nxna.nxna_flags & (NX_MAPROOT|NX_MAPALL)) {
bzero(&temp_cred, sizeof(temp_cred));
temp_cred.cr_uid = nxna.nxna_cred.cr_uid;
temp_cred.cr_ngroups = nxna.nxna_cred.cr_ngroups;
for (i=0; i < nxna.nxna_cred.cr_ngroups && i < NGROUPS; i++)
temp_cred.cr_groups[i] = nxna.nxna_cred.cr_groups[i];
cred = kauth_cred_create(&temp_cred);
if (!IS_VALID_CRED(cred))
return (ENOMEM);
} else {
cred = NOCRED;
}
if (nxna.nxna_addr.ss_len == 0) {
if (nx->nx_flags & NX_DEFAULTEXPORT) {
if (IS_VALID_CRED(cred))
kauth_cred_unref(&cred);
return (EEXIST);
}
nx->nx_flags |= NX_DEFAULTEXPORT;
nx->nx_defopt.nxo_flags = nxna.nxna_flags;
nx->nx_defopt.nxo_cred = cred;
bcopy(&nxna.nxna_sec, &nx->nx_defopt.nxo_sec, sizeof(struct nfs_sec));
nx->nx_expcnt++;
continue;
}
i = sizeof(struct nfs_netopt);
i += nxna.nxna_addr.ss_len + nxna.nxna_mask.ss_len;
MALLOC(no, struct nfs_netopt *, i, M_NETADDR, M_WAITOK);
if (!no) {
if (IS_VALID_CRED(cred))
kauth_cred_unref(&cred);
return (ENOMEM);
}
bzero(no, sizeof(struct nfs_netopt));
no->no_opt.nxo_flags = nxna.nxna_flags;
no->no_opt.nxo_cred = cred;
bcopy(&nxna.nxna_sec, &no->no_opt.nxo_sec, sizeof(struct nfs_sec));
saddr = (struct sockaddr *)(no + 1);
bcopy(&nxna.nxna_addr, saddr, nxna.nxna_addr.ss_len);
if (nxna.nxna_mask.ss_len) {
smask = (struct sockaddr *)((caddr_t)saddr + nxna.nxna_addr.ss_len);
bcopy(&nxna.nxna_mask, smask, nxna.nxna_mask.ss_len);
} else {
smask = NULL;
}
i = saddr->sa_family;
if ((rnh = nx->nx_rtable[i]) == 0) {
for (dom = domains; dom; dom = dom->dom_next)
if (dom->dom_family == i && dom->dom_rtattach) {
dom->dom_rtattach((void **)&nx->nx_rtable[i],
dom->dom_rtoffset);
break;
}
if ((rnh = nx->nx_rtable[i]) == 0) {
if (IS_VALID_CRED(cred))
kauth_cred_unref(&cred);
_FREE(no, M_NETADDR);
return (ENOBUFS);
}
}
rn = (*rnh->rnh_addaddr)((caddr_t)saddr, (caddr_t)smask, rnh, no->no_rnodes);
if (rn == 0) {
int matched = 0;
rn = (*rnh->rnh_matchaddr)((caddr_t)saddr, rnh);
rn_no = (struct nfs_netopt *)rn;
if (rn != 0 && (rn->rn_flags & RNF_ROOT) == 0 &&
(rn_no->no_opt.nxo_flags == nxna.nxna_flags) &&
(!nfsrv_cmp_secflavs(&rn_no->no_opt.nxo_sec, &nxna.nxna_sec))) {
kauth_cred_t cred2 = rn_no->no_opt.nxo_cred;
if (cred == cred2) {
matched = 1;
} else if (cred && cred2 && (cred->cr_uid == cred2->cr_uid) &&
(cred->cr_ngroups == cred2->cr_ngroups)) {
for (i=0; i < cred2->cr_ngroups && i < NGROUPS; i++)
if (cred->cr_groups[i] != cred2->cr_groups[i])
break;
if (i >= cred2->cr_ngroups || i >= NGROUPS)
matched = 1;
}
}
if (IS_VALID_CRED(cred))
kauth_cred_unref(&cred);
_FREE(no, M_NETADDR);
if (matched)
continue;
return (EPERM);
}
nx->nx_expcnt++;
}
return (0);
}
struct nfsrv_free_netopt_arg {
uint32_t *cnt;
struct radix_node_head *rnh;
};
static int
nfsrv_free_netopt(struct radix_node *rn, void *w)
{
struct nfsrv_free_netopt_arg *fna = (struct nfsrv_free_netopt_arg *)w;
struct radix_node_head *rnh = fna->rnh;
uint32_t *cnt = fna->cnt;
struct nfs_netopt *nno = (struct nfs_netopt *)rn;
(*rnh->rnh_deladdr)(rn->rn_key, rn->rn_mask, rnh);
if (IS_VALID_CRED(nno->no_opt.nxo_cred))
kauth_cred_unref(&nno->no_opt.nxo_cred);
_FREE((caddr_t)rn, M_NETADDR);
*cnt -= 1;
return (0);
}
static int
nfsrv_free_addrlist(struct nfs_export *nx, struct user_nfs_export_args *unxa)
{
struct nfs_export_net_args nxna;
struct radix_node_head *rnh;
struct radix_node *rn;
struct nfsrv_free_netopt_arg fna;
struct nfs_netopt *nno;
user_addr_t uaddr;
unsigned int net;
int i, error;
if (!unxa || !unxa->nxa_netcount) {
for (i = 0; i <= AF_MAX; i++)
if ( (rnh = nx->nx_rtable[i]) ) {
fna.rnh = rnh;
fna.cnt = &nx->nx_expcnt;
(*rnh->rnh_walktree)(rnh, nfsrv_free_netopt, (caddr_t)&fna);
_FREE((caddr_t)rnh, M_RTABLE);
nx->nx_rtable[i] = 0;
}
return (0);
}
uaddr = unxa->nxa_nets;
for (net = 0; net < unxa->nxa_netcount; net++, uaddr += sizeof(nxna)) {
error = copyin(uaddr, &nxna, sizeof(nxna));
if (error)
return (error);
if (nxna.nxna_addr.ss_len == 0) {
if (nx->nx_flags & NX_DEFAULTEXPORT) {
nx->nx_flags &= ~NX_DEFAULTEXPORT;
if (IS_VALID_CRED(nx->nx_defopt.nxo_cred)) {
kauth_cred_unref(&nx->nx_defopt.nxo_cred);
}
nx->nx_expcnt--;
}
continue;
}
if ((rnh = nx->nx_rtable[nxna.nxna_addr.ss_family]) == 0) {
if (!(unxa->nxa_flags & NXA_ADD))
printf("nfsrv_free_addrlist: address not found (0)\n");
continue;
}
rn = (*rnh->rnh_lookup)(&nxna.nxna_addr,
nxna.nxna_mask.ss_len ? &nxna.nxna_mask : NULL, rnh);
if (!rn || (rn->rn_flags & RNF_ROOT)) {
if (!(unxa->nxa_flags & NXA_ADD))
printf("nfsrv_free_addrlist: address not found (1)\n");
continue;
}
(*rnh->rnh_deladdr)(rn->rn_key, rn->rn_mask, rnh);
nno = (struct nfs_netopt *)rn;
if (IS_VALID_CRED(nno->no_opt.nxo_cred))
kauth_cred_unref(&nno->no_opt.nxo_cred);
_FREE((caddr_t)rn, M_NETADDR);
nx->nx_expcnt--;
if (nx->nx_expcnt == ((nx->nx_flags & NX_DEFAULTEXPORT) ? 1 : 0)) {
_FREE((caddr_t)rnh, M_RTABLE);
nx->nx_rtable[nxna.nxna_addr.ss_family] = 0;
}
}
return (0);
}
void enablequotas(struct mount *mp, vfs_context_t ctx);
int
nfsrv_export(struct user_nfs_export_args *unxa, vfs_context_t ctx)
{
int error = 0, pathlen;
struct nfs_exportfs *nxfs, *nxfs2, *nxfs3;
struct nfs_export *nx, *nx2, *nx3;
struct nfs_filehandle nfh;
struct nameidata mnd, xnd;
vnode_t mvp = NULL, xvp = NULL;
mount_t mp = NULL;
char path[MAXPATHLEN];
int expisroot;
if (unxa->nxa_flags & NXA_DELETE_ALL) {
lck_rw_lock_exclusive(&nfsrv_export_rwlock);
while ((nxfs = LIST_FIRST(&nfsrv_exports))) {
mp = vfs_getvfs_by_mntonname(nxfs->nxfs_path);
if (mp)
vfs_clearflags(mp, MNT_EXPORTED);
while ((nx = LIST_FIRST(&nxfs->nxfs_exports))) {
LIST_REMOVE(nx, nx_next);
LIST_REMOVE(nx, nx_hash);
nfsrv_free_addrlist(nx, NULL);
nx->nx_flags &= ~NX_DEFAULTEXPORT;
if (IS_VALID_CRED(nx->nx_defopt.nxo_cred)) {
kauth_cred_unref(&nx->nx_defopt.nxo_cred);
}
nfsrv_free_user_list(&nx->nx_user_list);
FREE(nx->nx_path, M_TEMP);
FREE(nx, M_TEMP);
}
LIST_REMOVE(nxfs, nxfs_next);
FREE(nxfs->nxfs_path, M_TEMP);
FREE(nxfs, M_TEMP);
}
lck_rw_done(&nfsrv_export_rwlock);
return (0);
}
error = copyinstr(unxa->nxa_fspath, path, MAXPATHLEN, (size_t *)&pathlen);
if (error)
return (error);
lck_rw_lock_exclusive(&nfsrv_export_rwlock);
LIST_FOREACH(nxfs, &nfsrv_exports, nxfs_next) {
if (nxfs->nxfs_id == unxa->nxa_fsid)
break;
}
if (nxfs) {
if (strncmp(path, nxfs->nxfs_path, MAXPATHLEN)) {
error = EEXIST;
goto unlock_out;
}
if ((unxa->nxa_flags & (NXA_ADD|NXA_OFFLINE)) == NXA_ADD) {
mp = vfs_getvfs_by_mntonname(nxfs->nxfs_path);
NDINIT(&mnd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNPATH1,
UIO_SYSSPACE, CAST_USER_ADDR_T(nxfs->nxfs_path), ctx);
error = namei(&mnd);
if (error)
goto unlock_out;
mvp = mnd.ni_vp;
if (!vnode_isvroot(mvp)) {
error = EINVAL;
goto out;
}
if (mp != vnode_mount(mvp)) {
error = EINVAL;
goto out;
}
}
} else {
if (!(unxa->nxa_flags & NXA_ADD)) {
error = ENOENT;
goto unlock_out;
}
NDINIT(&mnd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNPATH1,
UIO_SYSSPACE, CAST_USER_ADDR_T(path), ctx);
error = namei(&mnd);
if (error) {
if (!(unxa->nxa_flags & NXA_OFFLINE))
goto unlock_out;
} else {
mvp = mnd.ni_vp;
if (!vnode_isvroot(mvp)) {
if (!(unxa->nxa_flags & NXA_OFFLINE)) {
error = EINVAL;
goto out;
}
vnode_put(mvp);
nameidone(&mnd);
mvp = NULL;
} else {
mp = vnode_mount(mvp);
nfh.nfh_len = NFSV3_MAX_FID_SIZE;
error = VFS_VPTOFH(mvp, (int*)&nfh.nfh_len, &nfh.nfh_fid[0], NULL);
if (!error && (nfh.nfh_len > (int)NFSV3_MAX_FID_SIZE))
error = EIO;
if (error)
goto out;
}
}
MALLOC(nxfs, struct nfs_exportfs *, sizeof(struct nfs_exportfs), M_TEMP, M_WAITOK);
if (!nxfs) {
error = ENOMEM;
goto out;
}
bzero(nxfs, sizeof(struct nfs_exportfs));
nxfs->nxfs_id = unxa->nxa_fsid;
MALLOC(nxfs->nxfs_path, char*, pathlen, M_TEMP, M_WAITOK);
if (!nxfs->nxfs_path) {
FREE(nxfs, M_TEMP);
error = ENOMEM;
goto out;
}
bcopy(path, nxfs->nxfs_path, pathlen);
nxfs3 = NULL;
LIST_FOREACH(nxfs2, &nfsrv_exports, nxfs_next) {
if (strncmp(nxfs->nxfs_path, nxfs2->nxfs_path, MAXPATHLEN) > 0)
break;
nxfs3 = nxfs2;
}
if (nxfs2)
LIST_INSERT_BEFORE(nxfs2, nxfs, nxfs_next);
else if (nxfs3)
LIST_INSERT_AFTER(nxfs3, nxfs, nxfs_next);
else
LIST_INSERT_HEAD(&nfsrv_exports, nxfs, nxfs_next);
if (mp)
enablequotas(mp, ctx);
}
if (unxa->nxa_exppath) {
error = copyinstr(unxa->nxa_exppath, path, MAXPATHLEN, (size_t *)&pathlen);
if (error)
goto out;
LIST_FOREACH(nx, &nxfs->nxfs_exports, nx_next) {
if (nx->nx_id == unxa->nxa_expid)
break;
}
if (nx) {
if (strncmp(path, nx->nx_path, MAXPATHLEN)) {
error = EEXIST;
goto out;
}
} else {
if (!(unxa->nxa_flags & NXA_ADD)) {
error = ENOENT;
goto out;
}
MALLOC(nx, struct nfs_export *, sizeof(struct nfs_export), M_TEMP, M_WAITOK);
if (!nx) {
error = ENOMEM;
goto out1;
}
bzero(nx, sizeof(struct nfs_export));
nx->nx_id = unxa->nxa_expid;
nx->nx_fs = nxfs;
microtime(&nx->nx_exptime);
MALLOC(nx->nx_path, char*, pathlen, M_TEMP, M_WAITOK);
if (!nx->nx_path) {
error = ENOMEM;
FREE(nx, M_TEMP);
nx = NULL;
goto out1;
}
bcopy(path, nx->nx_path, pathlen);
nfsrv_init_user_list(&nx->nx_user_list);
nx3 = NULL;
LIST_FOREACH(nx2, &nxfs->nxfs_exports, nx_next) {
if (strncmp(nx->nx_path, nx2->nx_path, MAXPATHLEN) > 0)
break;
nx3 = nx2;
}
if (nx2)
LIST_INSERT_BEFORE(nx2, nx, nx_next);
else if (nx3)
LIST_INSERT_AFTER(nx3, nx, nx_next);
else
LIST_INSERT_HEAD(&nxfs->nxfs_exports, nx, nx_next);
LIST_INSERT_HEAD(NFSRVEXPHASH(nxfs->nxfs_id, nx->nx_id), nx, nx_hash);
error = 0;
if ((nx3 && !strncmp(nx3->nx_path, nx->nx_path, pathlen - 1) &&
(nx3->nx_path[pathlen-1] == '/')) ||
(nx2 && !strncmp(nx2->nx_path, nx->nx_path, strlen(nx2->nx_path)) &&
(nx->nx_path[strlen(nx2->nx_path)] == '/')))
error = EINVAL;
if (!error) {
expisroot = !nx->nx_path[0] ||
((nx->nx_path[0] == '.') && !nx->nx_path[1]);
LIST_FOREACH(nx2, &nxfs->nxfs_exports, nx_next) {
if (expisroot) {
if (nx2 != nx)
break;
} else if (!nx2->nx_path[0])
break;
else if ((nx2->nx_path[0] == '.') && !nx2->nx_path[1])
break;
}
if (nx2)
error = EINVAL;
}
if (error) {
printf("nfsrv_export: warning: nested exports: %s/%s\n",
nxfs->nxfs_path, nx->nx_path);
error = 0;
}
nx->nx_fh.nfh_xh.nxh_flags = NXHF_INVALIDFH;
}
if ((nx->nx_fh.nfh_xh.nxh_version != htonl(NFS_FH_VERSION)) ||
(nx->nx_fh.nfh_xh.nxh_flags & NXHF_INVALIDFH)) {
nx->nx_fh.nfh_xh.nxh_version = htonl(NFS_FH_VERSION);
nx->nx_fh.nfh_xh.nxh_fsid = htonl(nx->nx_fs->nxfs_id);
nx->nx_fh.nfh_xh.nxh_expid = htonl(nx->nx_id);
nx->nx_fh.nfh_xh.nxh_flags = 0;
nx->nx_fh.nfh_xh.nxh_reserved = 0;
nx->nx_fh.nfh_fhp = (u_char*)&nx->nx_fh.nfh_xh;
bzero(&nx->nx_fh.nfh_fid[0], NFSV2_MAX_FID_SIZE);
if (mvp) {
if (!nx->nx_path[0] || ((nx->nx_path[0] == '.') && !nx->nx_path[1])) {
xvp = mvp;
vnode_get(xvp);
} else {
xnd.ni_cnd.cn_nameiop = LOOKUP;
xnd.ni_cnd.cn_flags = LOCKLEAF;
xnd.ni_pathlen = pathlen - 1;
xnd.ni_cnd.cn_nameptr = xnd.ni_cnd.cn_pnbuf = path;
xnd.ni_startdir = mvp;
xnd.ni_usedvp = mvp;
xnd.ni_cnd.cn_context = ctx;
error = lookup(&xnd);
if (error)
goto out1;
xvp = xnd.ni_vp;
}
if (vnode_vtype(xvp) != VDIR) {
error = EINVAL;
vnode_put(xvp);
goto out1;
}
nx->nx_fh.nfh_len = NFSV3_MAX_FID_SIZE;
error = VFS_VPTOFH(xvp, (int*)&nx->nx_fh.nfh_len, &nx->nx_fh.nfh_fid[0], NULL);
if (!error && (nx->nx_fh.nfh_len > (int)NFSV3_MAX_FID_SIZE)) {
error = EIO;
} else {
nx->nx_fh.nfh_xh.nxh_fidlen = nx->nx_fh.nfh_len;
nx->nx_fh.nfh_len += sizeof(nx->nx_fh.nfh_xh);
}
vnode_put(xvp);
if (error)
goto out1;
} else {
nx->nx_fh.nfh_xh.nxh_flags = NXHF_INVALIDFH;
nx->nx_fh.nfh_xh.nxh_fidlen = 0;
nx->nx_fh.nfh_len = sizeof(nx->nx_fh.nfh_xh);
}
}
} else {
nx = NULL;
}
if (unxa->nxa_flags & NXA_DELETE) {
if (!nx) {
while ((nx = LIST_FIRST(&nxfs->nxfs_exports))) {
LIST_REMOVE(nx, nx_next);
LIST_REMOVE(nx, nx_hash);
nfsrv_free_addrlist(nx, NULL);
nx->nx_flags &= ~NX_DEFAULTEXPORT;
if (IS_VALID_CRED(nx->nx_defopt.nxo_cred)) {
kauth_cred_unref(&nx->nx_defopt.nxo_cred);
}
nfsrv_free_user_list(&nx->nx_user_list);
FREE(nx->nx_path, M_TEMP);
FREE(nx, M_TEMP);
}
goto out1;
} else if (!unxa->nxa_netcount) {
nfsrv_free_addrlist(nx, NULL);
nx->nx_flags &= ~NX_DEFAULTEXPORT;
if (IS_VALID_CRED(nx->nx_defopt.nxo_cred)) {
kauth_cred_unref(&nx->nx_defopt.nxo_cred);
}
} else {
error = nfsrv_free_addrlist(nx, unxa);
if (error)
goto out1;
}
}
if (unxa->nxa_flags & NXA_ADD) {
if (unxa->nxa_flags & NXA_OFFLINE)
microtime(&nx->nx_exptime);
error = nfsrv_hang_addrlist(nx, unxa);
if (!error && mp)
vfs_setflags(mp, MNT_EXPORTED);
}
out1:
if (nx && !nx->nx_expcnt) {
LIST_REMOVE(nx, nx_next);
LIST_REMOVE(nx, nx_hash);
nfsrv_free_user_list(&nx->nx_user_list);
FREE(nx->nx_path, M_TEMP);
FREE(nx, M_TEMP);
}
if (LIST_EMPTY(&nxfs->nxfs_exports)) {
LIST_REMOVE(nxfs, nxfs_next);
FREE(nxfs->nxfs_path, M_TEMP);
FREE(nxfs, M_TEMP);
if (mp)
vfs_clearflags(mp, MNT_EXPORTED);
}
out:
if (mvp) {
vnode_put(mvp);
nameidone(&mnd);
}
unlock_out:
lck_rw_done(&nfsrv_export_rwlock);
return (error);
}
static struct nfs_export_options *
nfsrv_export_lookup(struct nfs_export *nx, mbuf_t nam)
{
struct nfs_export_options *nxo = NULL;
struct nfs_netopt *no = NULL;
struct radix_node_head *rnh;
struct sockaddr *saddr;
if (nam != NULL) {
saddr = mbuf_data(nam);
rnh = nx->nx_rtable[saddr->sa_family];
if (rnh != NULL) {
no = (struct nfs_netopt *)
(*rnh->rnh_matchaddr)((caddr_t)saddr, rnh);
if (no && no->no_rnodes->rn_flags & RNF_ROOT)
no = NULL;
if (no)
nxo = &no->no_opt;
}
}
if ((nxo == NULL) && (nx->nx_flags & NX_DEFAULTEXPORT))
nxo = &nx->nx_defopt;
return (nxo);
}
static struct nfs_export *
nfsrv_fhtoexport(struct nfs_filehandle *nfhp)
{
struct nfs_exphandle *nxh = (struct nfs_exphandle*)nfhp->nfh_fhp;
struct nfs_export *nx;
uint32_t fsid, expid;
fsid = ntohl(nxh->nxh_fsid);
expid = ntohl(nxh->nxh_expid);
nx = NFSRVEXPHASH(fsid, expid)->lh_first;
for (; nx; nx = LIST_NEXT(nx, nx_hash)) {
if (nx->nx_fs->nxfs_id != fsid)
continue;
if (nx->nx_id != expid)
continue;
break;
}
return nx;
}
int
nfsrv_fhtovp(
struct nfs_filehandle *nfhp,
struct nfsrv_descript *nd,
vnode_t *vpp,
struct nfs_export **nxp,
struct nfs_export_options **nxop)
{
struct nfs_exphandle *nxh = (struct nfs_exphandle*)nfhp->nfh_fhp;
struct nfs_export_options *nxo;
u_char *fidp;
int error;
struct mount *mp;
mbuf_t nam = NULL;
uint32_t v;
int i, valid;
*vpp = NULL;
*nxp = NULL;
*nxop = NULL;
if (nd != NULL)
nam = nd->nd_nam;
v = ntohl(nxh->nxh_version);
if (v != NFS_FH_VERSION) {
return (ESTALE);
}
if (nfhp->nfh_len > NFSV3_MAX_FH_SIZE)
return (EBADRPC);
if (nfhp->nfh_len < (int)sizeof(struct nfs_exphandle))
return (ESTALE);
v = ntohs(nxh->nxh_flags);
if (v & NXHF_INVALIDFH)
return (ESTALE);
*nxp = nfsrv_fhtoexport(nfhp);
if (!*nxp)
return (ESTALE);
*nxop = nxo = nfsrv_export_lookup(*nxp, nam);
if (nam && (*nxop == NULL))
return (EACCES);
if (nd != NULL) {
for (i = 0, valid = 0; i < nxo->nxo_sec.count; i++) {
if (nd->nd_sec == nxo->nxo_sec.flavors[i]) {
valid = 1;
break;
}
}
if (!valid) {
if (nd->nd_vers == NFS_VER2)
if (nd->nd_procnum == NFSV2PROC_GETATTR ||
nd->nd_procnum == NFSV2PROC_STATFS)
valid = 1;
if (nd->nd_vers == NFS_VER3)
if (nd->nd_procnum == NFSPROC_FSINFO)
valid = 1;
if (!valid)
return (NFSERR_AUTHERR | AUTH_REJECTCRED);
}
}
if (nxo && (nxo->nxo_flags & NX_OFFLINE))
return ((nd->nd_vers == NFS_VER2) ? ESTALE : NFSERR_TRYLATER);
mp = vfs_getvfs_by_mntonname((*nxp)->nx_fs->nxfs_path);
if (!mp) {
return ((nd->nd_vers == NFS_VER2) ? ESTALE : NFSERR_TRYLATER);
}
fidp = nfhp->nfh_fhp + sizeof(*nxh);
error = VFS_FHTOVP(mp, nxh->nxh_fidlen, fidp, vpp, NULL);
if (error)
return (error);
if (*vpp == NULL)
return (ESTALE);
return (0);
}
int
nfsrv_credcheck(
struct nfsrv_descript *nd,
vfs_context_t ctx,
__unused struct nfs_export *nx,
struct nfs_export_options *nxo)
{
if (nxo && nxo->nxo_cred) {
if ((nxo->nxo_flags & NX_MAPALL) ||
((nxo->nxo_flags & NX_MAPROOT) && !suser(nd->nd_cr, NULL))) {
kauth_cred_ref(nxo->nxo_cred);
kauth_cred_unref(&nd->nd_cr);
nd->nd_cr = nxo->nxo_cred;
}
}
ctx->vc_ucred = nd->nd_cr;
return (0);
}
int
nfsrv_vptofh(
struct nfs_export *nx,
int nfsvers,
struct nfs_filehandle *dnfhp,
vnode_t vp,
vfs_context_t ctx,
struct nfs_filehandle *nfhp)
{
int error;
uint32_t maxfidsize;
nfhp->nfh_fhp = (u_char*)&nfhp->nfh_xh;
nfhp->nfh_xh.nxh_version = htonl(NFS_FH_VERSION);
nfhp->nfh_xh.nxh_fsid = htonl(nx->nx_fs->nxfs_id);
nfhp->nfh_xh.nxh_expid = htonl(nx->nx_id);
nfhp->nfh_xh.nxh_flags = 0;
nfhp->nfh_xh.nxh_reserved = 0;
if (nfsvers == NFS_VER2)
bzero(&nfhp->nfh_fid[0], NFSV2_MAX_FID_SIZE);
if (dnfhp && nfsrv_fhmatch(dnfhp, &nx->nx_fh)) {
if (nfsvers == NFS_VER2)
nfhp->nfh_len = NFSX_V2FH;
else
nfhp->nfh_len = sizeof(nfhp->nfh_xh);
nfhp->nfh_xh.nxh_fidlen = 0;
nfhp->nfh_xh.nxh_flags = htons(NXHF_INVALIDFH);
return (0);
}
if (nfsvers == NFS_VER2)
maxfidsize = NFSV2_MAX_FID_SIZE;
else
maxfidsize = NFSV3_MAX_FID_SIZE;
nfhp->nfh_len = maxfidsize;
error = VFS_VPTOFH(vp, (int*)&nfhp->nfh_len, &nfhp->nfh_fid[0], ctx);
if (error)
return (error);
if (nfhp->nfh_len > maxfidsize)
return (EOVERFLOW);
nfhp->nfh_xh.nxh_fidlen = nfhp->nfh_len;
nfhp->nfh_len += sizeof(nfhp->nfh_xh);
if ((nfsvers == NFS_VER2) && (nfhp->nfh_len < NFSX_V2FH))
nfhp->nfh_len = NFSX_V2FH;
return (0);
}
int
nfsrv_fhmatch(struct nfs_filehandle *fh1, struct nfs_filehandle *fh2)
{
struct nfs_exphandle *nxh1, *nxh2;
int len1, len2;
nxh1 = (struct nfs_exphandle *)fh1->nfh_fhp;
nxh2 = (struct nfs_exphandle *)fh2->nfh_fhp;
len1 = sizeof(fh1->nfh_xh) + nxh1->nxh_fidlen;
len2 = sizeof(fh2->nfh_xh) + nxh2->nxh_fidlen;
if (len1 != len2)
return (0);
if (bcmp(nxh1, nxh2, len1))
return (0);
return (1);
}
static int
nfsrv_cmp_sockaddr(struct sockaddr_storage *sock1, struct sockaddr_storage *sock2)
{
struct sockaddr_in *ipv4_sock1, *ipv4_sock2;
struct sockaddr_in6 *ipv6_sock1, *ipv6_sock2;
if (sock1 == NULL || sock2 == NULL)
return 1;
if (sock1->ss_len != sock2->ss_len)
return 1;
if (sock1->ss_family != sock2->ss_family)
return 1;
if (sock1->ss_family == AF_INET) {
ipv4_sock1 = (struct sockaddr_in *)sock1;
ipv4_sock2 = (struct sockaddr_in *)sock2;
if (!bcmp(&ipv4_sock1->sin_addr, &ipv4_sock2->sin_addr, sizeof(struct in_addr)))
return 0;
} else {
ipv6_sock1 = (struct sockaddr_in6 *)sock1;
ipv6_sock2 = (struct sockaddr_in6 *)sock2;
if (!bcmp(&ipv6_sock1->sin6_addr, &ipv6_sock2->sin6_addr, sizeof(struct in6_addr)))
return 0;
}
return 1;
}
static struct nfs_user_stat_node *
nfsrv_get_user_stat_node(struct nfs_active_user_list *list, struct sockaddr_storage *sock, uid_t uid)
{
struct nfs_user_stat_node *unode;
struct timeval now;
struct nfs_user_stat_hashtbl_head *head;
head = NFS_USER_STAT_HASH(list->user_hashtbl, uid);
LIST_FOREACH(unode, head, hash_link) {
if (uid == unode->uid && nfsrv_cmp_sockaddr(sock, &unode->sock) == 0) {
break;
}
}
if (unode) {
TAILQ_REMOVE(&list->user_lru, unode, lru_link);
TAILQ_INSERT_TAIL(&list->user_lru, unode, lru_link);
microtime(&now);
unode->tm_last = (uint32_t)now.tv_sec;
return unode;
}
if (list->node_count < nfsrv_user_stat_max_nodes) {
MALLOC(unode, struct nfs_user_stat_node *, sizeof(struct nfs_user_stat_node),
M_TEMP, M_WAITOK | M_ZERO);
if (!unode)
return NULL;
OSAddAtomic(1, (SInt32*)&nfsrv_user_stat_node_count);
list->node_count++;
} else {
unode = TAILQ_FIRST(&list->user_lru);
if (!unode)
return NULL;
TAILQ_REMOVE(&list->user_lru, unode, lru_link);
LIST_REMOVE(unode, hash_link);
}
unode->uid = uid;
bcopy(sock, &unode->sock, sock->ss_len);
microtime(&now);
unode->ops = 0;
unode->bytes_read = 0;
unode->bytes_written = 0;
unode->tm_start = (uint32_t)now.tv_sec;
unode->tm_last = (uint32_t)now.tv_sec;
TAILQ_INSERT_TAIL(&list->user_lru, unode, lru_link);
LIST_INSERT_HEAD(head, unode, hash_link);
return unode;
}
void
nfsrv_update_user_stat(struct nfs_export *nx, struct nfsrv_descript *nd, uid_t uid, u_int ops, u_int rd_bytes, u_int wr_bytes)
{
struct nfs_user_stat_node *unode;
struct nfs_active_user_list *ulist;
struct sockaddr_storage *sock_stor;
if ((!nfsrv_user_stat_enabled) || (!nx) || (!nd) || (!nd->nd_nam))
return;
sock_stor = (struct sockaddr_storage *)mbuf_data(nd->nd_nam);
if ((sock_stor->ss_family != AF_INET) && (sock_stor->ss_family != AF_INET6))
return;
ulist = &nx->nx_user_list;
lck_mtx_lock(&ulist->user_mutex);
unode = nfsrv_get_user_stat_node(ulist, sock_stor, uid);
if (!unode) {
lck_mtx_unlock(&ulist->user_mutex);
return;
}
unode->ops += ops;
unode->bytes_read += rd_bytes;
unode->bytes_written += wr_bytes;
lck_mtx_unlock(&ulist->user_mutex);
}
static void
nfsrv_init_user_list(struct nfs_active_user_list *ulist)
{
uint i;
TAILQ_INIT(&ulist->user_lru);
for(i = 0; i < NFS_USER_STAT_HASH_SIZE; i++)
LIST_INIT(&ulist->user_hashtbl[i]);
ulist->node_count = 0;
lck_mtx_init(&ulist->user_mutex, nfsrv_active_user_mutex_group, LCK_ATTR_NULL);
}
static void
nfsrv_free_user_list(struct nfs_active_user_list *ulist)
{
struct nfs_user_stat_node *unode;
if (!ulist)
return;
while ((unode = TAILQ_FIRST(&ulist->user_lru))) {
TAILQ_REMOVE(&ulist->user_lru, unode, lru_link);
LIST_REMOVE(unode, hash_link);
FREE(unode, M_TEMP);
OSAddAtomic(-1, (SInt32*)&nfsrv_user_stat_node_count);
}
ulist->node_count = 0;
lck_mtx_destroy(&ulist->user_mutex, nfsrv_active_user_mutex_group);
}
void
nfsrv_active_user_list_reclaim(void)
{
struct nfs_exportfs *nxfs;
struct nfs_export *nx;
struct nfs_active_user_list *ulist;
struct nfs_user_stat_hashtbl_head oldlist;
struct nfs_user_stat_node *unode, *unode_next;
struct timeval now;
uint32_t tstale;
LIST_INIT(&oldlist);
lck_rw_lock_shared(&nfsrv_export_rwlock);
microtime(&now);
tstale = now.tv_sec - nfsrv_user_stat_max_idle_sec;
LIST_FOREACH(nxfs, &nfsrv_exports, nxfs_next) {
LIST_FOREACH(nx, &nxfs->nxfs_exports, nx_next) {
ulist = &nx->nx_user_list;
lck_mtx_lock(&ulist->user_mutex);
for (unode = TAILQ_FIRST(&ulist->user_lru); unode; unode = unode_next) {
unode_next = TAILQ_NEXT(unode, lru_link);
if (unode->tm_last >= tstale)
break;
TAILQ_REMOVE(&ulist->user_lru, unode, lru_link);
LIST_REMOVE(unode, hash_link);
LIST_INSERT_HEAD(&oldlist, unode, hash_link);
OSAddAtomic(-1, (SInt32*)&nfsrv_user_stat_node_count);
ulist->node_count--;
}
lck_mtx_unlock(&ulist->user_mutex);
}
}
lck_rw_done(&nfsrv_export_rwlock);
while ((unode = LIST_FIRST(&oldlist))) {
LIST_REMOVE(unode, hash_link);
FREE(unode, M_TEMP);
}
}
static u_char nfsrv_v2errmap[] = {
NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO,
NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO,
NFSERR_IO, NFSERR_EXIST, NFSERR_IO, NFSERR_NODEV, NFSERR_NOTDIR,
NFSERR_ISDIR, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS,
NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO,
NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE,
};
static short nfsv3err_null[] = {
0,
0,
};
static short nfsv3err_getattr[] = {
NFSERR_IO,
NFSERR_IO,
NFSERR_STALE,
NFSERR_BADHANDLE,
NFSERR_SERVERFAULT,
NFSERR_TRYLATER,
0,
};
static short nfsv3err_setattr[] = {
NFSERR_IO,
NFSERR_PERM,
NFSERR_IO,
NFSERR_ACCES,
NFSERR_INVAL,
NFSERR_NOSPC,
NFSERR_ROFS,
NFSERR_DQUOT,
NFSERR_STALE,
NFSERR_BADHANDLE,
NFSERR_NOT_SYNC,
NFSERR_SERVERFAULT,
NFSERR_TRYLATER,
0,
};
static short nfsv3err_lookup[] = {
NFSERR_IO,
NFSERR_NOENT,
NFSERR_IO,
NFSERR_ACCES,
NFSERR_NOTDIR,
NFSERR_NAMETOL,
NFSERR_STALE,
NFSERR_BADHANDLE,
NFSERR_SERVERFAULT,
NFSERR_TRYLATER,
0,
};
static short nfsv3err_access[] = {
NFSERR_IO,
NFSERR_IO,
NFSERR_STALE,
NFSERR_BADHANDLE,
NFSERR_SERVERFAULT,
NFSERR_TRYLATER,
0,
};
static short nfsv3err_readlink[] = {
NFSERR_IO,
NFSERR_IO,
NFSERR_ACCES,
NFSERR_INVAL,
NFSERR_STALE,
NFSERR_BADHANDLE,
NFSERR_NOTSUPP,
NFSERR_SERVERFAULT,
NFSERR_TRYLATER,
0,
};
static short nfsv3err_read[] = {
NFSERR_IO,
NFSERR_IO,
NFSERR_NXIO,
NFSERR_ACCES,
NFSERR_INVAL,
NFSERR_STALE,
NFSERR_BADHANDLE,
NFSERR_SERVERFAULT,
NFSERR_TRYLATER,
0,
};
static short nfsv3err_write[] = {
NFSERR_IO,
NFSERR_IO,
NFSERR_ACCES,
NFSERR_INVAL,
NFSERR_FBIG,
NFSERR_NOSPC,
NFSERR_ROFS,
NFSERR_DQUOT,
NFSERR_STALE,
NFSERR_BADHANDLE,
NFSERR_SERVERFAULT,
NFSERR_TRYLATER,
0,
};
static short nfsv3err_create[] = {
NFSERR_IO,
NFSERR_IO,
NFSERR_ACCES,
NFSERR_EXIST,
NFSERR_NOTDIR,
NFSERR_NOSPC,
NFSERR_ROFS,
NFSERR_NAMETOL,
NFSERR_DQUOT,
NFSERR_STALE,
NFSERR_BADHANDLE,
NFSERR_NOTSUPP,
NFSERR_SERVERFAULT,
NFSERR_TRYLATER,
0,
};
static short nfsv3err_mkdir[] = {
NFSERR_IO,
NFSERR_IO,
NFSERR_ACCES,
NFSERR_EXIST,
NFSERR_NOTDIR,
NFSERR_NOSPC,
NFSERR_ROFS,
NFSERR_NAMETOL,
NFSERR_DQUOT,
NFSERR_STALE,
NFSERR_BADHANDLE,
NFSERR_NOTSUPP,
NFSERR_SERVERFAULT,
NFSERR_TRYLATER,
0,
};
static short nfsv3err_symlink[] = {
NFSERR_IO,
NFSERR_IO,
NFSERR_ACCES,
NFSERR_EXIST,
NFSERR_NOTDIR,
NFSERR_NOSPC,
NFSERR_ROFS,
NFSERR_NAMETOL,
NFSERR_DQUOT,
NFSERR_STALE,
NFSERR_BADHANDLE,
NFSERR_NOTSUPP,
NFSERR_SERVERFAULT,
NFSERR_TRYLATER,
0,
};
static short nfsv3err_mknod[] = {
NFSERR_IO,
NFSERR_IO,
NFSERR_ACCES,
NFSERR_EXIST,
NFSERR_NOTDIR,
NFSERR_NOSPC,
NFSERR_ROFS,
NFSERR_NAMETOL,
NFSERR_DQUOT,
NFSERR_STALE,
NFSERR_BADHANDLE,
NFSERR_NOTSUPP,
NFSERR_SERVERFAULT,
NFSERR_BADTYPE,
NFSERR_TRYLATER,
0,
};
static short nfsv3err_remove[] = {
NFSERR_IO,
NFSERR_NOENT,
NFSERR_IO,
NFSERR_ACCES,
NFSERR_NOTDIR,
NFSERR_ROFS,
NFSERR_NAMETOL,
NFSERR_STALE,
NFSERR_BADHANDLE,
NFSERR_SERVERFAULT,
NFSERR_TRYLATER,
0,
};
static short nfsv3err_rmdir[] = {
NFSERR_IO,
NFSERR_NOENT,
NFSERR_IO,
NFSERR_ACCES,
NFSERR_EXIST,
NFSERR_NOTDIR,
NFSERR_INVAL,
NFSERR_ROFS,
NFSERR_NAMETOL,
NFSERR_NOTEMPTY,
NFSERR_STALE,
NFSERR_BADHANDLE,
NFSERR_NOTSUPP,
NFSERR_SERVERFAULT,
NFSERR_TRYLATER,
0,
};
static short nfsv3err_rename[] = {
NFSERR_IO,
NFSERR_NOENT,
NFSERR_IO,
NFSERR_ACCES,
NFSERR_EXIST,
NFSERR_XDEV,
NFSERR_NOTDIR,
NFSERR_ISDIR,
NFSERR_INVAL,
NFSERR_NOSPC,
NFSERR_ROFS,
NFSERR_MLINK,
NFSERR_NAMETOL,
NFSERR_NOTEMPTY,
NFSERR_DQUOT,
NFSERR_STALE,
NFSERR_BADHANDLE,
NFSERR_NOTSUPP,
NFSERR_SERVERFAULT,
NFSERR_TRYLATER,
0,
};
static short nfsv3err_link[] = {
NFSERR_IO,
NFSERR_IO,
NFSERR_ACCES,
NFSERR_EXIST,
NFSERR_XDEV,
NFSERR_NOTDIR,
NFSERR_INVAL,
NFSERR_NOSPC,
NFSERR_ROFS,
NFSERR_MLINK,
NFSERR_NAMETOL,
NFSERR_DQUOT,
NFSERR_STALE,
NFSERR_BADHANDLE,
NFSERR_NOTSUPP,
NFSERR_SERVERFAULT,
NFSERR_TRYLATER,
0,
};
static short nfsv3err_readdir[] = {
NFSERR_IO,
NFSERR_IO,
NFSERR_ACCES,
NFSERR_NOTDIR,
NFSERR_STALE,
NFSERR_BADHANDLE,
NFSERR_BAD_COOKIE,
NFSERR_TOOSMALL,
NFSERR_SERVERFAULT,
NFSERR_TRYLATER,
0,
};
static short nfsv3err_readdirplus[] = {
NFSERR_IO,
NFSERR_IO,
NFSERR_ACCES,
NFSERR_NOTDIR,
NFSERR_STALE,
NFSERR_BADHANDLE,
NFSERR_BAD_COOKIE,
NFSERR_NOTSUPP,
NFSERR_TOOSMALL,
NFSERR_SERVERFAULT,
NFSERR_TRYLATER,
0,
};
static short nfsv3err_fsstat[] = {
NFSERR_IO,
NFSERR_IO,
NFSERR_STALE,
NFSERR_BADHANDLE,
NFSERR_SERVERFAULT,
NFSERR_TRYLATER,
0,
};
static short nfsv3err_fsinfo[] = {
NFSERR_STALE,
NFSERR_STALE,
NFSERR_BADHANDLE,
NFSERR_SERVERFAULT,
NFSERR_TRYLATER,
0,
};
static short nfsv3err_pathconf[] = {
NFSERR_STALE,
NFSERR_STALE,
NFSERR_BADHANDLE,
NFSERR_SERVERFAULT,
NFSERR_TRYLATER,
0,
};
static short nfsv3err_commit[] = {
NFSERR_IO,
NFSERR_IO,
NFSERR_STALE,
NFSERR_BADHANDLE,
NFSERR_SERVERFAULT,
NFSERR_TRYLATER,
0,
};
static short *nfsrv_v3errmap[] = {
nfsv3err_null,
nfsv3err_getattr,
nfsv3err_setattr,
nfsv3err_lookup,
nfsv3err_access,
nfsv3err_readlink,
nfsv3err_read,
nfsv3err_write,
nfsv3err_create,
nfsv3err_mkdir,
nfsv3err_symlink,
nfsv3err_mknod,
nfsv3err_remove,
nfsv3err_rmdir,
nfsv3err_rename,
nfsv3err_link,
nfsv3err_readdir,
nfsv3err_readdirplus,
nfsv3err_fsstat,
nfsv3err_fsinfo,
nfsv3err_pathconf,
nfsv3err_commit,
};
int
nfsrv_errmap(struct nfsrv_descript *nd, int err)
{
short *defaulterrp, *errp;
if (nd->nd_vers == NFS_VER2) {
if (err <= (int)sizeof(nfsrv_v2errmap))
return ((int)nfsrv_v2errmap[err - 1]);
return (NFSERR_IO);
}
if (nd->nd_procnum > NFSPROC_COMMIT)
return (err & 0xffff);
errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
while (*++errp) {
if (*errp == err)
return (err);
else if (*errp > err)
break;
}
return ((int)*defaulterrp);
}
#endif