/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Rick Macklem at The University of Guelph.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)nfs_subs.c 8.8 (Berkeley) 5/22/95
*/
/*
* Copyright 2000 Wasabi Systems, Inc.
* All rights reserved.
*
* Written by Frank van der Linden for Wasabi Systems, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed for the NetBSD Project by
* Wasabi Systems, Inc.
* 4. The name of Wasabi Systems, Inc. may not be used to endorse
* or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* These functions support the macros and help fiddle mbuf chains for
* the nfs op functions. They do things like create the rpc header and
* copy data between mbuf chains and uio lists.
*/
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/mount.h>
#include <sys/vnode.h>
#include <sys/namei.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/filedesc.h>
#include <sys/time.h>
#include <sys/dirent.h>
#include <sys/once.h>
#include <sys/kauth.h>
#include <sys/atomic.h>
#include <sys/cprng.h>
int nuidhash_max = NFS_MAXUIDHASH;
/*
* Data items converted to xdr at startup, since they are constant
* This is kinda hokey, but may save a little time doing byte swaps
*/
u_int32_t nfs_xdrneg1;
u_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
rpc_mismatch, rpc_auth_unix, rpc_msgaccepted,
rpc_auth_kerb;
u_int32_t nfs_prog, nfs_true, nfs_false;
/*
* Maps errno values to nfs error numbers.
* Although it is not obvious whether or not NFS clients really care if
* a returned error value is in the specified list for the procedure, the
* safest thing to do is filter them appropriately. For Version 2, the
* X/Open XNFS document is the only specification that defines error values
* for each RPC (The RFC simply lists all possible error values for all RPCs),
* so I have decided to not do this for Version 2.
* The first entry is the default error return and the rest are the valid
* errors for that RPC in increasing numeric order.
*/
static const short nfsv3err_null[] = {
0,
0,
};
int nfs_webnamei(struct nameidata *, struct vnode *, struct proc *);
/*
* Create the header for an rpc request packet
* The hsiz is the size of the rest of the nfs request header.
* (just used to decide if a cluster is a good idea)
*/
struct mbuf *
nfsm_reqh(struct nfsnode *np, u_long procid, int hsiz, char **bposp)
{
struct mbuf *mb;
char *bpos;
/*
* Build the RPC header and fill in the authorization info.
* The authorization string argument is only used when the credentials
* come from outside of the kernel.
* Returns the head of the mbuf list.
*/
struct mbuf *
nfsm_rpchead(kauth_cred_t cr, int nmflag, int procid,
int auth_type, int auth_len, char *auth_str, int verf_len,
char *verf_str, struct mbuf *mrest, int mrest_len,
struct mbuf **mbp, uint32_t *xidp)
{
struct mbuf *mb;
u_int32_t *tl;
char *bpos;
int i;
struct mbuf *mreq;
int siz, grpsiz, authsiz;
/*
* copies mbuf chain to the uio scatter/gather list
*/
int
nfsm_mbuftouio(struct mbuf **mrep, struct uio *uiop, int siz, char **dpos)
{
char *mbufcp, *uiocp;
int xfer, left, len;
struct mbuf *mp;
long uiosiz, rem;
int error = 0;
mp = *mrep;
mbufcp = *dpos;
len = mtod(mp, char *) + mp->m_len - mbufcp;
rem = nfsm_rndup(siz)-siz;
while (siz > 0) {
if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
return (EFBIG);
left = uiop->uio_iov->iov_len;
uiocp = uiop->uio_iov->iov_base;
if (left > siz)
left = siz;
uiosiz = left;
while (left > 0) {
while (len == 0) {
mp = mp->m_next;
if (mp == NULL)
return (EBADRPC);
mbufcp = mtod(mp, void *);
len = mp->m_len;
}
xfer = (left > len) ? len : left;
error = copyout_vmspace(uiop->uio_vmspace, mbufcp,
uiocp, xfer);
if (error) {
return error;
}
left -= xfer;
len -= xfer;
mbufcp += xfer;
uiocp += xfer;
uiop->uio_offset += xfer;
uiop->uio_resid -= xfer;
}
if (uiop->uio_iov->iov_len <= siz) {
uiop->uio_iovcnt--;
uiop->uio_iov++;
} else {
uiop->uio_iov->iov_base =
(char *)uiop->uio_iov->iov_base + uiosiz;
uiop->uio_iov->iov_len -= uiosiz;
}
siz -= uiosiz;
}
*dpos = mbufcp;
*mrep = mp;
if (rem > 0) {
if (len < rem)
error = nfs_adv(mrep, dpos, rem, len);
else
*dpos += rem;
}
return (error);
}
/*
* copies a uio scatter/gather list to an mbuf chain.
* NOTE: can only handle iovcnt == 1
*/
int
nfsm_uiotombuf(struct uio *uiop, struct mbuf **mq, int siz, char **bpos)
{
char *uiocp;
struct mbuf *mp, *mp2;
int xfer, left, mlen;
int uiosiz, clflg, rem;
char *cp;
int error;
/*
* Get at least "siz" bytes of correctly aligned data.
* When called the mbuf pointers are not necessarily correct,
* dsosp points to what ought to be in m_data and left contains
* what ought to be in m_len.
* This is used by the macros nfsm_dissect and nfsm_dissecton for tough
* cases. (The macros use the vars. dpos and dpos2)
*/
int
nfsm_disct(struct mbuf **mdp, char **dposp, int siz, int left, char **cp2)
{
struct mbuf *m1, *m2;
struct mbuf *havebuf = NULL;
char *src = *dposp;
char *dst;
int len;
#ifdef DEBUG
if (left < 0)
panic("nfsm_disct: left < 0");
#endif
m1 = *mdp;
/*
* Skip through the mbuf chain looking for an mbuf with
* some data. If the first mbuf found has enough data
* and it is correctly aligned return it.
*/
while (left == 0) {
havebuf = m1;
*mdp = m1 = m1->m_next;
if (m1 == NULL)
return (EBADRPC);
src = mtod(m1, void *);
left = m1->m_len;
/*
* If we start a new mbuf and it is big enough
* and correctly aligned just return it, don't
* do any pull up.
*/
if (left >= siz && nfsm_aligned(src)) {
*cp2 = src;
*dposp = src + siz;
return (0);
}
}
if ((m1->m_flags & M_EXT) != 0) {
if (havebuf && M_TRAILINGSPACE(havebuf) >= siz &&
nfsm_aligned(mtod(havebuf, char *) + havebuf->m_len)) {
/*
* If the first mbuf with data has external data
* and there is a previous mbuf with some trailing
* space, use it to move the data into.
*/
m2 = m1;
*mdp = m1 = havebuf;
*cp2 = mtod(m1, char *) + m1->m_len;
} else if (havebuf) {
/*
* If the first mbuf has a external data
* and there is no previous empty mbuf
* allocate a new mbuf and move the external
* data to the new mbuf. Also make the first
* mbuf look empty.
*/
m2 = m1;
*mdp = m1 = m_get(M_WAIT, MT_DATA);
MCLAIM(m1, m2->m_owner);
if ((m2->m_flags & M_PKTHDR) != 0) {
m_move_pkthdr(m1, m2);
}
if (havebuf) {
havebuf->m_next = m1;
}
m1->m_next = m2;
MRESETDATA(m1);
m1->m_len = 0;
m2->m_data = src;
m2->m_len = left;
*cp2 = mtod(m1, char *);
} else {
struct mbuf **nextp = &m1->m_next;
m1->m_len -= left;
do {
m2 = m_get(M_WAIT, MT_DATA);
MCLAIM(m2, m1->m_owner);
if (left >= MINCLSIZE) {
MCLGET(m2, M_WAIT);
}
m2->m_next = *nextp;
*nextp = m2;
nextp = &m2->m_next;
len = (m2->m_flags & M_EXT) != 0 ?
MCLBYTES : MLEN;
if (len > left) {
len = left;
}
memcpy(mtod(m2, char *), src, len);
m2->m_len = len;
src += len;
left -= len;
} while (left > 0);
*mdp = m1 = m1->m_next;
m2 = m1->m_next;
*cp2 = mtod(m1, char *);
}
} else {
/*
* If the first mbuf has no external data
* move the data to the front of the mbuf.
*/
MRESETDATA(m1);
dst = mtod(m1, char *);
if (dst != src) {
memmove(dst, src, left);
}
m1->m_len = left;
m2 = m1->m_next;
*cp2 = m1->m_data;
}
*dposp = *cp2 + siz;
/*
* Loop through mbufs pulling data up into first mbuf until
* the first mbuf is full or there is no more data to
* pullup.
*/
dst = mtod(m1, char *) + m1->m_len;
while ((len = M_TRAILINGSPACE(m1)) != 0 && m2) {
if ((len = uimin(len, m2->m_len)) != 0) {
memcpy(dst, mtod(m2, char *), len);
}
m1->m_len += len;
dst += len;
m2->m_data += len;
m2->m_len -= len;
m2 = m2->m_next;
}
if (m1->m_len < siz)
return (EBADRPC);
return (0);
}
/*
* Advance the position in the mbuf chain.
*/
int
nfs_adv(struct mbuf **mdp, char **dposp, int offs, int left)
{
struct mbuf *m;
int s;
m = *mdp;
s = left;
while (s < offs) {
offs -= s;
m = m->m_next;
if (m == NULL)
return (EBADRPC);
s = m->m_len;
}
*mdp = m;
*dposp = mtod(m, char *) + offs;
return (0);
}
/*
* Copy a string into mbufs for the hard cases...
*/
int
nfsm_strtmbuf(struct mbuf **mb, char **bpos, const char *cp, long siz)
{
struct mbuf *m1 = NULL, *m2;
long left, xfer, len, tlen;
u_int32_t *tl;
int putsize;
/*
* Directory caching routines. They work as follows:
* - a cache is maintained per VDIR nfsnode.
* - for each offset cookie that is exported to userspace, and can
* thus be thrown back at us as an offset to VOP_READDIR, store
* information in the cache.
* - cached are:
* - cookie itself
* - blocknumber (essentially just a search key in the buffer cache)
* - entry number in block.
* - offset cookie of block in which this entry is stored
* - 32 bit cookie if NFSMNT_XLATECOOKIE is used.
* - entries are looked up in a hash table
* - also maintained is an LRU list of entries, used to determine
* which ones to delete if the cache grows too large.
* - if 32 <-> 64 translation mode is requested for a filesystem,
* the cache also functions as a translation table
* - in the translation case, invalidating the cache does not mean
* flushing it, but just marking entries as invalid, except for
* the <64bit cookie, 32bitcookie> pair which is still valid, to
* still be able to use the cache as a translation table.
* - 32 bit cookies are uniquely created by combining the hash table
* entry value, and one generation count per hash table entry,
* incremented each time an entry is appended to the chain.
* - the cache is invalidated each time a direcory is modified
* - sanity checks are also done; if an entry in a block turns
* out not to have a matching cookie, the cache is invalidated
* and a new block starting from the wanted offset is fetched from
* the server.
* - directory entries as read from the server are extended to contain
* the 64bit and, optionally, the 32bit cookies, for sanity checking
* the cache and exporting them to userspace through the cookie
* argument to VOP_READDIR.
*/
u_long
nfs_dirhash(off_t off)
{
int i;
char *cp = (char *)&off;
u_long sum = 0L;
/*
* Zero is always a valid cookie.
*/
if (off == 0)
/* XXXUNCONST */
return (struct nfsdircache *)__UNCONST(&dzero);
if (!np->n_dircache)
return NULL;
/*
* We use a 32bit cookie as search key, directly reconstruct
* the hashentry. Else use the hashfunction.
*/
if (do32) {
ent = (u_int32_t)off >> 24;
if (ent >= NFS_DIRHASHSIZ)
return NULL;
ndhp = &np->n_dircache[ent];
} else {
ndhp = NFSDIRHASH(np, off);
}
if (hashent)
*hashent = (int)(ndhp - np->n_dircache);
NFSDC_LOCK(np);
if (do32) {
LIST_FOREACH(ndp, ndhp, dc_hash) {
if (ndp->dc_cookie32 == (u_int32_t)off) {
/*
* An invalidated entry will become the
* start of a new block fetched from
* the server.
*/
if (ndp->dc_flags & NFSDC_INVALID) {
ndp->dc_blkcookie = ndp->dc_cookie;
ndp->dc_entry = 0;
ndp->dc_flags &= ~NFSDC_INVALID;
}
break;
}
}
} else {
LIST_FOREACH(ndp, ndhp, dc_hash) {
if (ndp->dc_cookie == off)
break;
}
}
if (ndp != NULL)
ndp->dc_refcnt++;
NFSDC_UNLOCK(np);
return ndp;
}
/*
* XXX refuse entries for offset 0. amd(8) erroneously sets
* cookie 0 for the '.' entry, making this necessary. This
* isn't so bad, as 0 is a special case anyway.
*/
if (off == 0)
/* XXXUNCONST */
return (struct nfsdircache *)__UNCONST(&dzero);
if (!np->n_dircache)
/*
* XXX would like to do this in nfs_nget but vtype
* isn't known at that time.
*/
nfs_initdircache(vp);
if ((nmp->nm_flag & NFSMNT_XLATECOOKIE) && !np->n_dirgens)
nfs_initdirxlatecookie(vp);
NFSDC_LOCK(np);
if (ndp && (ndp->dc_flags & NFSDC_INVALID) == 0) {
/*
* Overwriting an old entry. Check if it's the same.
* If so, just return. If not, remove the old entry.
*/
if (ndp->dc_blkcookie == blkoff && ndp->dc_entry == en)
goto done;
nfs_unlinkdircache(np, ndp);
nfs_putdircache_unlocked(np, ndp);
ndp = NULL;
}
ndhp = &np->n_dircache[hashent];
if (!ndp) {
if (newndp == NULL) {
NFSDC_UNLOCK(np);
newndp = kmem_alloc(sizeof(*newndp), KM_SLEEP);
newndp->dc_refcnt = 1;
LIST_NEXT(newndp, dc_hash) = (void *)-1;
goto retry;
}
ndp = newndp;
newndp = NULL;
overwrite = 0;
if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
/*
* We're allocating a new entry, so bump the
* generation number.
*/
KASSERT(np->n_dirgens);
gen = ++np->n_dirgens[hashent];
if (gen == 0) {
np->n_dirgens[hashent]++;
gen++;
}
ndp->dc_cookie32 = (hashent << 24) | (gen & 0xffffff);
}
} else
overwrite = 1;
/*
* If the maximum directory cookie cache size has been reached
* for this node, take one off the front. The idea is that
* directories are typically read front-to-back once, so that
* the oldest entries can be thrown away without much performance
* loss.
*/
if (np->n_dircachesize == NFS_MAXDIRCACHE) {
nfs_unlinkdircache(np, TAILQ_FIRST(&np->n_dirchain));
} else
np->n_dircachesize++;
#define nfs_p() while (atomic_cas_32(&nfs_mutex, 0, 1) == 0) continue;
#define nfs_v() while (atomic_cas_32(&nfs_mutex, 1, 0) == 1) continue;
/*
* This is disgusting, but it must support both modular and monolothic
* configurations, plus the code is shared between server and client.
* For monolithic builds NFSSERVER may not imply NFS. Unfortunately we
* can't use regular mutexes here that would require static initialization
* and we can get initialized from multiple places, so we improvise.
*
* Yuck.
*/
void
nfs_init(void)
{
nfs_p();
if (nfs_refcount++ == 0)
nfs_init0();
nfs_v();
}
/*
* A fiddled version of m_adj() that ensures null fill to a 32-bit
* boundary and only trims off the back end
*
* 1. trim off 'len' bytes as m_adj(mp, -len).
* 2. add zero-padding 'nul' bytes at the end of the mbuf chain.
*/
void
nfs_zeropad(struct mbuf *mp, int len, int nul)
{
struct mbuf *m;
int count;
/*
* Trim from tail. Scan the mbuf chain,
* calculating its length and finding the last mbuf.
* If the adjustment only affects this mbuf, then just
* adjust and return. Otherwise, rescan and truncate
* after the remaining size.
*/
count = 0;
m = mp;
for (;;) {
count += m->m_len;
if (m->m_next == NULL)
break;
m = m->m_next;
}
KDASSERT(count >= len);
if (m->m_len >= len) {
m->m_len -= len;
} else {
count -= len;
/*
* Correct length for chain is "count".
* Find the mbuf with last data, adjust its length,
* and toss data from remaining mbufs on chain.
*/
for (m = mp; m; m = m->m_next) {
if (m->m_len >= count) {
m->m_len = count;
break;
}
count -= m->m_len;
}
KASSERT(m && m->m_next);
m_freem(m->m_next);
m->m_next = NULL;
}
KDASSERT(m->m_next == NULL);
/*
* zero-padding.
*/
if (nul > 0) {
char *cp;
int i;
if (M_READONLY(m) || M_TRAILINGSPACE(m) < nul) {
struct mbuf *n;
/*
* Make these functions instead of macros, so that the kernel text size
* doesn't get too big...
*/
void
nfsm_srvwcc(struct nfsrv_descript *nfsd, int before_ret, struct vattr *before_vap, int after_ret, struct vattr *after_vap, struct mbuf **mbp, char **bposp)
{
struct mbuf *mb = *mbp;
char *bpos = *bposp;
u_int32_t *tl;
/*
* This function compares two net addresses by family and returns true
* if they are the same host.
* If there is any doubt, return false.
* The AF_INET family is handled as a special case so that address mbufs
* don't need to be saved to store "struct in_addr", which is only 4 bytes.
*/
int
netaddr_match(int family, union nethostaddr *haddr, struct mbuf *nam)
{
struct sockaddr_in *inetaddr;
switch (family) {
case AF_INET:
inetaddr = mtod(nam, struct sockaddr_in *);
if (inetaddr->sin_family == AF_INET &&
inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
return (1);
break;
case AF_INET6:
{
struct sockaddr_in6 *sin6_1, *sin6_2;
/* XXXAD mountpoint check looks like nonsense to me */
np = VTONFS(vp);
if (vp->v_type != VREG || vp->v_mount != c->mp || np == NULL)
return false;
return false;
}
/*
* The write verifier has changed (probably due to a server reboot), so all
* PG_NEEDCOMMIT pages will have to be written again. Since they are marked
* as dirty or are being written out just now, all this takes is clearing
* the PG_NEEDCOMMIT flag. Once done the new write verifier can be set for
* the mount point.
*/
void
nfs_clearcommit(struct mount *mp)
{
struct vnode *vp;
struct vnode_iterator *marker;
struct nfsmount *nmp = VFSTONFS(mp);
struct nfs_clearcommit_ctx ctx;
struct nfsnode *np;
struct vm_page *pg;
struct uvm_page_array a;
voff_t off;
if (!(np->n_commitflags & NFS_COMMIT_PUSHED_VALID))
return;
lo = off;
hi = lo + len;
if (lo > np->n_pushedhi || hi < np->n_pushedlo)
return;
if (lo <= np->n_pushedlo)
np->n_pushedlo = hi;
else if (hi >= np->n_pushedhi)
np->n_pushedhi = lo;
else {
/*
* XXX There's only one range. If the deleted range
* is in the middle, pick the largest of the
* contiguous ranges that it leaves.
*/
if ((np->n_pushedlo - lo) > (hi - np->n_pushedhi))
np->n_pushedhi = lo;
else
np->n_pushedlo = hi;
}
#ifdef NFS_DEBUG_COMMIT
printf("del: committed: %u - %u\n", (unsigned)np->n_pushedlo,
(unsigned)np->n_pushedhi);
#endif
}
if (!(np->n_commitflags & NFS_COMMIT_PUSH_VALID))
return;
lo = off;
hi = lo + len;
if (lo > np->n_pushhi || hi < np->n_pushlo)
return;
if (lo <= np->n_pushlo)
np->n_pushlo = hi;
else if (hi >= np->n_pushhi)
np->n_pushhi = lo;
else {
/*
* XXX There's only one range. If the deleted range
* is in the middle, pick the largest of the
* contiguous ranges that it leaves.
*/
if ((np->n_pushlo - lo) > (hi - np->n_pushhi))
np->n_pushhi = lo;
else
np->n_pushlo = hi;
}
#ifdef NFS_DEBUG_COMMIT
printf("del: tobecommitted: %u - %u\n", (unsigned)np->n_pushlo,
(unsigned)np->n_pushhi);
#endif
}
/*
* Map errnos to NFS error numbers. For Version 3 also filter out error
* numbers not specified for the associated procedure.
*/
int
nfsrv_errmap(struct nfsrv_descript *nd, int err)
{
const short *defaulterrp, *errp;
if (nd->nd_flag & ND_NFSV3) {
if (nd->nd_procnum <= NFSPROC_COMMIT) {
errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
while (*++errp) {
if (*errp == err)
return (err);
else if (*errp > err)
break;
}
return ((int)*defaulterrp);
} else
return (err & 0xffff);
}
if (err <= ELAST)
return ((int)nfsrv_v2errmap[err - 1]);
return (NFSERR_IO);
}
u_int32_t
nfs_getxid(void)
{
u_int32_t newxid;
if (__predict_false(nfs_xid == 0)) {
nfs_xid = cprng_fast32();
}
/* get next xid. skip 0 */
do {
newxid = atomic_inc_32_nv(&nfs_xid);
} while (__predict_false(newxid == 0));
return txdr_unsigned(newxid);
}
/*
* assign a new xid for existing request.
* used for NFSERR_JUKEBOX handling.
*/
void
nfs_renewxid(struct nfsreq *req)
{
u_int32_t xid;
int off;
xid = nfs_getxid();
if (req->r_nmp->nm_sotype == SOCK_STREAM)
off = sizeof(u_int32_t); /* RPC record mark */
else
off = 0;