/* $NetBSD: nfs_clcomsubs.c,v 1.4 2022/02/09 21:50:24 andvar Exp $ */
/*-
* 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.
* 4. 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.
*
*/
/*
* 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.
*/
#ifndef APPLEKEXT
#include <fs/nfs/common/nfsport.h>
/*
* Start building a request. Mostly just put the first file handle in
* place.
*/
APPLESTATIC void
nfscl_reqstart(struct nfsrv_descript *nd, int procnum, struct nfsmount *nmp,
u_int8_t *nfhp, int fhlen, u_int32_t **opcntpp, struct nfsclsession *sep)
{
struct mbuf *mb;
u_int32_t *tl;
int opcnt;
nfsattrbit_t attrbits;
/*
* First, fill in some of the fields of nd.
*/
nd->nd_slotseq = NULL;
if (NFSHASNFSV4(nmp)) {
nd->nd_flag = ND_NFSV4 | ND_NFSCL;
if (NFSHASNFSV4N(nmp))
nd->nd_flag |= ND_NFSV41;
} else if (NFSHASNFSV3(nmp))
nd->nd_flag = ND_NFSV3 | ND_NFSCL;
else
nd->nd_flag = ND_NFSV2 | ND_NFSCL;
nd->nd_procnum = procnum;
nd->nd_repstat = 0;
/*
* Get the first mbuf for the request.
*/
if (nfs_bigrequest[procnum])
NFSMCLGET(mb, M_WAITOK);
else
NFSMGET(mb);
mbuf_setlen(mb, 0);
nd->nd_mreq = nd->nd_mb = mb;
nd->nd_bpos = NFSMTOD(mb, caddr_t);
/*
* And fill the first file handle into the request.
*/
if (nd->nd_flag & ND_NFSV4) {
opcnt = nfsv4_opmap[procnum].opcnt +
nfsv4_opflag[nfsv4_opmap[procnum].op].needscfh;
if ((nd->nd_flag & ND_NFSV41) != 0) {
opcnt += nfsv4_opflag[nfsv4_opmap[procnum].op].needsseq;
if (procnum == NFSPROC_RENEW)
/*
* For the special case of Renew, just do a
* Sequence Op.
*/
opcnt = 1;
else if (procnum == NFSPROC_WRITEDS ||
procnum == NFSPROC_COMMITDS)
/*
* For the special case of a Writeor Commit to
* a DS, the opcnt == 3, for Sequence, PutFH,
* Write/Commit.
*/
opcnt = 3;
}
/*
* What should the tag really be?
*/
(void) nfsm_strtom(nd, nfsv4_opmap[procnum].tag,
nfsv4_opmap[procnum].taglen);
NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
if ((nd->nd_flag & ND_NFSV41) != 0)
*tl++ = txdr_unsigned(NFSV41_MINORVERSION);
else
*tl++ = txdr_unsigned(NFSV4_MINORVERSION);
if (opcntpp != NULL)
*opcntpp = tl;
*tl = txdr_unsigned(opcnt);
if ((nd->nd_flag & ND_NFSV41) != 0 &&
nfsv4_opflag[nfsv4_opmap[procnum].op].needsseq > 0) {
NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
*tl = txdr_unsigned(NFSV4OP_SEQUENCE);
if (sep == NULL)
nfsv4_setsequence(nmp, nd,
NFSMNT_MDSSESSION(nmp),
nfs_bigreply[procnum]);
else
nfsv4_setsequence(nmp, nd, sep,
nfs_bigreply[procnum]);
}
if (nfsv4_opflag[nfsv4_opmap[procnum].op].needscfh > 0) {
NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
*tl = txdr_unsigned(NFSV4OP_PUTFH);
(void) nfsm_fhtom(nd, nfhp, fhlen, 0);
if (nfsv4_opflag[nfsv4_opmap[procnum].op].needscfh
== 2 && procnum != NFSPROC_WRITEDS &&
procnum != NFSPROC_COMMITDS) {
NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
*tl = txdr_unsigned(NFSV4OP_GETATTR);
/*
* For Lookup Ops, we want all the directory
* attributes, so we can load the name cache.
*/
if (procnum == NFSPROC_LOOKUP ||
procnum == NFSPROC_LOOKUPP)
NFSGETATTR_ATTRBIT(&attrbits);
else {
NFSWCCATTR_ATTRBIT(&attrbits);
nd->nd_flag |= ND_V4WCCATTR;
}
(void) nfsrv_putattrbit(nd, &attrbits);
}
}
if (procnum != NFSPROC_RENEW ||
(nd->nd_flag & ND_NFSV41) == 0) {
NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
*tl = txdr_unsigned(nfsv4_opmap[procnum].op);
}
} else {
(void) nfsm_fhtom(nd, nfhp, fhlen, 0);
}
if (procnum < NFSV41_NPROCS)
NFSINCRGLOBAL(nfsstatsv1.rpccnt[procnum]);
}
#ifndef APPLE
/*
* copies a uio scatter/gather list to an mbuf chain.
* NOTE: can only handle iovcnt == 1
*/
APPLESTATIC void
nfsm_uiombuf(struct nfsrv_descript *nd, struct uio *uiop, int siz)
{
char *uiocp;
struct mbuf *mp, *mp2;
int xfer, left, mlen;
int uiosiz, clflg, rem;
char *cp, *tcp;
/*
* This function finds the directory cookie that corresponds to the
* logical byte offset given.
*/
APPLESTATIC nfsuint64 *
nfscl_getcookie(struct nfsnode *np, off_t off, int add)
{
struct nfsdmap *dp, *dp2;
int pos;
/*
* Gets a file handle out of an nfs reply sent to the client and returns
* the file handle and the file's attributes.
* For V4, it assumes that Getfh and Getattr Op's results are here.
*/
APPLESTATIC int
nfscl_mtofh(struct nfsrv_descript *nd, struct nfsfh **nfhpp,
struct nfsvattr *nap, int *attrflagp)
{
u_int32_t *tl;
int error = 0, flag = 1;
*nfhpp = NULL;
*attrflagp = 0;
/*
* First get the file handle and vnode.
*/
if (nd->nd_flag & ND_NFSV3) {
NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
flag = fxdr_unsigned(int, *tl);
} else if (nd->nd_flag & ND_NFSV4) {
NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
}
if (flag) {
error = nfsm_getfh(nd, nfhpp);
if (error)
return (error);
}
/*
* Now, get the attributes.
*/
if (nd->nd_flag & ND_NFSV4) {
NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
} else if (nd->nd_flag & ND_NFSV3) {
NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
if (flag) {
flag = fxdr_unsigned(int, *tl);
} else if (fxdr_unsigned(int, *tl)) {
error = nfsm_advance(nd, NFSX_V3FATTR, -1);
if (error)
return (error);
}
}
if (flag) {
error = nfsm_loadattr(nd, nap);
if (!error)
*attrflagp = 1;
}
nfsmout:
return (error);
}
/*
* Put a state Id in the mbuf list.
*/
APPLESTATIC void
nfsm_stateidtom(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp, int flag)
{
nfsv4stateid_t *st;
/*
* Get an exclusive lock. (Not needed for OpenBSD4, since there is only one
* thread for each posix process in the kernel.)
*/
APPLESTATIC void
nfscl_lockexcl(struct nfsv4lock *lckp, void *mutex)
{
int igotlock;
do {
igotlock = nfsv4_lock(lckp, 1, NULL, mutex, NULL);
} while (!igotlock);
}