From d08c284cec641c1aa88bc61985852989a6069a3f Mon Sep 17 00:00:00 2001 From: Pawel Dziepak Date: Tue, 29 May 2012 23:39:56 +0200 Subject: [PATCH] nfs4: Add NFS4 request builder Currently supports only the following operations: ACCESS, GETATTR, GETFH LOOKUP and PUTROOTFH. --- src/add-ons/kernel/file_systems/nfs4/Jamfile | 1 + .../file_systems/nfs4/RequestBuilder.cpp | 143 ++++++++++++++++++ .../kernel/file_systems/nfs4/RequestBuilder.h | 43 ++++++ 3 files changed, 187 insertions(+) create mode 100644 src/add-ons/kernel/file_systems/nfs4/RequestBuilder.cpp create mode 100644 src/add-ons/kernel/file_systems/nfs4/RequestBuilder.h diff --git a/src/add-ons/kernel/file_systems/nfs4/Jamfile b/src/add-ons/kernel/file_systems/nfs4/Jamfile index db1386b800..8c82e50edd 100644 --- a/src/add-ons/kernel/file_systems/nfs4/Jamfile +++ b/src/add-ons/kernel/file_systems/nfs4/Jamfile @@ -5,6 +5,7 @@ UsePrivateHeaders kernel ; KernelAddon nfs4 : Connection.cpp kernel_interface.cpp + RequestBuilder.cpp RPCAuth.cpp RPCCall.cpp RPCReply.cpp diff --git a/src/add-ons/kernel/file_systems/nfs4/RequestBuilder.cpp b/src/add-ons/kernel/file_systems/nfs4/RequestBuilder.cpp new file mode 100644 index 0000000000..3b53a4090c --- /dev/null +++ b/src/add-ons/kernel/file_systems/nfs4/RequestBuilder.cpp @@ -0,0 +1,143 @@ +/* + * Copyright 2012 Haiku, Inc. All rights reserved. + * Distributed under the terms of the MIT License. + * + * Authors: + * Paweł Dziepak, pdziepak@quarnos.org + */ + + +#include "RequestBuilder.h" + +#include + + +RequestBuilder::RequestBuilder(Procedure proc) + : + fOpCount(0), + fProcedure(proc), + fRequest(RPC::Call::Create(proc, RPC::Auth::CreateSys(), + RPC::Auth::CreateNone())) +{ + if (fProcedure == ProcCompound) { + fRequest->Stream().AddOpaque(NULL, 0); + fRequest->Stream().AddUInt(0); + + fOpCountPosition = fRequest->Stream().Current(); + fRequest->Stream().AddUInt(0); + } +} + + +RequestBuilder::~RequestBuilder() +{ + delete fRequest; +} + + +status_t +RequestBuilder::Access() +{ + if (fProcedure != ProcCompound) + return B_BAD_VALUE; + if (fRequest == NULL) + return B_NO_MEMORY; + + fRequest->Stream().AddUInt(OpAccess); + fRequest->Stream().AddUInt(ACCESS4_READ | ACCESS4_LOOKUP | ACCESS4_MODIFY + | ACCESS4_EXTEND | ACCESS4_DELETE + | ACCESS4_EXECUTE); + fOpCount++; + + return B_OK; +} + + +status_t +RequestBuilder::GetAttr(Attribute* attrs, uint32 count) +{ + if (fProcedure != ProcCompound) + return B_BAD_VALUE; + if (fRequest == NULL) + return B_NO_MEMORY; + + fRequest->Stream().AddUInt(OpGetAttr); + + // 2 is safe in NFS4, not in NFS4.1 though + uint32 bitmap[2]; + memset(bitmap, 0, sizeof(bitmap)); + for (uint32 i = 0; i < count; i++) { + bitmap[(int)attrs[i] / 32] |= 1 << (int)attrs[i] % 32; + } + + uint32 bcount = bitmap[1] != 0 ? 2 : 1; + fRequest->Stream().AddUInt(bcount); + for (uint32 i = 0; i < bcount; i++) + fRequest->Stream().AddUInt(bitmap[i]); + + fOpCount++; + + return B_OK; +} + + +status_t +RequestBuilder::GetFH() +{ + if (fProcedure != ProcCompound) + return B_BAD_VALUE; + if (fRequest == NULL) + return B_NO_MEMORY; + + fRequest->Stream().AddUInt(OpGetFH); + fOpCount++; + + return B_OK; +} + + +status_t +RequestBuilder::LookUp(const char* name) +{ + if (fProcedure != ProcCompound) + return B_BAD_VALUE; + if (fRequest == NULL) + return B_NO_MEMORY; + if (name == NULL) + return B_BAD_VALUE; + + fRequest->Stream().AddUInt(OpLookUp); + fRequest->Stream().AddString(name, strlen(name)); + fOpCount++; + + return B_OK; +} + + +status_t +RequestBuilder::PutRootFH() +{ + if (fProcedure != ProcCompound) + return B_BAD_VALUE; + if (fRequest == NULL) + return B_NO_MEMORY; + + fRequest->Stream().AddUInt(OpPutRootFH); + fOpCount++; + + return B_OK; +} + + +RPC::Call* +RequestBuilder::Request() +{ + if (fProcedure == ProcCompound) + fRequest->Stream().InsertUInt(fOpCountPosition, fOpCount); + + if (fRequest == NULL || fRequest->Stream().Error() == B_OK) + return fRequest; + else + return NULL; +} + diff --git a/src/add-ons/kernel/file_systems/nfs4/RequestBuilder.h b/src/add-ons/kernel/file_systems/nfs4/RequestBuilder.h new file mode 100644 index 0000000000..377519f15c --- /dev/null +++ b/src/add-ons/kernel/file_systems/nfs4/RequestBuilder.h @@ -0,0 +1,43 @@ +/* + * Copyright 2012 Haiku, Inc. All rights reserved. + * Distributed under the terms of the MIT License. + * + * Authors: + * Paweł Dziepak, pdziepak@quarnos.org + */ +#ifndef REQUESTBUILDER_H +#define REQUESTBUILDER_H + + +#include + +#include "NFS4Defs.h" +#include "RPCCall.h" +#include "XDR.h" + + +class RequestBuilder { +public: + RequestBuilder(Procedure proc); + ~RequestBuilder(); + + status_t Access(); + status_t GetAttr(Attribute* attrs, uint32 count); + status_t GetFH(); + status_t LookUp(const char* name); + status_t PutRootFH(); + + RPC::Call* Request(); + +private: + uint32 fOpCount; + XDR::Stream::Position fOpCountPosition; + + Procedure fProcedure; + + RPC::Call* fRequest; +}; + + +#endif // REQUESTBUILDER_H +