os/kernelhwsrv/userlibandfileserver/fileserver/shostmassstorage/server/protocol/tblocktransfer.cpp
Update contrib.
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
26 #include "mblocktransferprotocol.h"
27 #include "tblocktransfer.h"
33 Manage a read from a block device.
35 @param aProtocol A reference to object providing protocol read method
36 @param aPosition The position to start reading from
37 @param aLength The length of data to read
38 @param aBuf Buffer to copy the data to
40 void TBlockTransfer::ReadL(MBlockTransferProtocol& aProtocol,
46 __HOSTPRINT1(_L("blocklen = 0%lx"), iBlockLength);
52 TPos headOffset = GetHeadBlockOffset(aPosition);
53 /**** READ10 HEAD ****/
56 TPos headpos = aPosition - headOffset;
58 headlen = ((headOffset + aLength - 1) / iBlockLength) == 0 ? aLength : (iBlockLength - headOffset);
60 __HOSTPRINT2(_L("\tRead head pos = 0%lx length = 0%lx"),
61 headpos, iBlockLength);
62 aProtocol.BlockReadL(headpos, *iHeadbuf, iBlockLength);
63 aBuf.Append(iHeadbuf->Ptr() + headOffset, headlen);
66 /**** READ10 BODY ****/
67 TInt blocksInMain = (aLength - headlen)/iBlockLength;
70 copylen = blocksInMain * iBlockLength;
71 __HOSTPRINT2(_L("\tRead main pos = 0%lx length = 0x%x"),
72 aPosition+headlen, copylen);
73 aProtocol.BlockReadL(aPosition+headlen, (TDes8 &)aBuf, copylen);
78 /**** READ10 TAIL ****/
79 TInt tailLen = aLength - copylen;
82 __HOSTPRINT2(_L("\tRead tail pos = 0%lx length = 0%lx"),
83 aPosition+copylen, iBlockLength);
85 aProtocol.BlockReadL(aPosition+copylen, *iTailbuf, iBlockLength);
86 aBuf.Append(iTailbuf->Ptr(), tailLen);
92 Manage a write to a block device
94 @param aProtocol A reference to object providing protocol read method
95 @param aPosition The position to start reading from
96 @param aLength The length of data to read
97 @param aBuf Buffer containing the data to write
99 void TBlockTransfer::WriteL(MBlockTransferProtocol& aProtocol,
108 TPos headOffset = GetHeadBlockOffset(aPosition);
109 /**** WRITE10 HEAD ****/
112 TPos headpos = aPosition - headOffset;
116 RBuf8& buf = *iTailbuf;
119 headlen = ((headOffset + aLength - 1) / iBlockLength) == 0 ? aLength : (iBlockLength - headOffset);
121 __HOSTPRINT2(_L("\tWrite-Read head pos = 0%lx length = 0%lx"),
122 headpos, iBlockLength);
124 aProtocol.BlockReadL(headpos, *iHeadbuf, iBlockLength);
126 __HOSTPRINT2(_L("\tcopying read data pos = 0%lx offset = 0%lx"),
127 headpos, headOffset);
128 buf.Append(iHeadbuf->Ptr(), headOffset);
131 buf.Append(aBuf.Ptr(), headlen);
133 /* Is it a short write and tail exist? */
134 TInt headEndOffset = headOffset + headlen;
135 if (headEndOffset != iBlockLength)
137 TInt len = iBlockLength - headEndOffset;
139 __HOSTPRINT2(_L("\t(short write) copying read data pos = 0%lx length = %08x"),
140 (headpos + headEndOffset), len);
141 buf.Append(iHeadbuf->Ptr() + headEndOffset, len);
144 __HOSTPRINT2(_L("\tWrite head pos = 0%lx length = %08x"),
147 aProtocol.BlockWriteL(headpos, (TDes8 &)buf, 0, iBlockLength);
150 /**** WRITE10 BODY ****/
151 TPos blocksInMain = (aLength - headlen)/iBlockLength;
154 copylen = blocksInMain * iBlockLength;
156 const TUint64 pos = aPosition + headlen;
157 __HOSTPRINT2(_L("\tWrite main pos = 0%lx length = %08x"),
161 aProtocol.BlockWriteL(pos, (TDes8 &)aBuf, headlen, copylen);
166 /**** WRITE10 TAIL ****/
167 TInt tailLen = aLength - copylen;;
170 RBuf8& buf = *iHeadbuf;
175 const TUint64 pos = aPosition + copylen;
177 __HOSTPRINT2(_L("\tWrite-Read tail pos = 0%lx length = %08x"),
180 aProtocol.BlockReadL(pos, *iTailbuf, iBlockLength);
182 buf.Append(aBuf.Ptr() + copylen, tailLen);
184 buf.Append(iTailbuf->Ptr() + tailLen, iBlockLength - tailLen);
186 aProtocol.BlockWriteL(pos, (TDes8 &)buf, 0, iBlockLength);
191 void TBlockTransfer::SetCapacityL(TUint32 aBlockLength, TLba aLastLba)
194 iBlockLength = static_cast<TInt64>(aBlockLength);
197 __ASSERT_DEBUG(iHeadbuf, User::Invariant());
198 __ASSERT_DEBUG(iTailbuf, User::Invariant());
200 if (iHeadbuf->Length() < iBlockLength)
202 iHeadbuf->ReAllocL(aBlockLength);
203 iTailbuf->ReAllocL(aBlockLength);
208 TPos TBlockTransfer::GetHeadBlockOffset(TPos aPos)
211 return (aPos % iBlockLength);
214 TLba TBlockTransfer::GetHeadBlockLbaL(TPos aPos)
217 TLba lba = I64LOW(aPos / iBlockLength);
219 User::Leave(KErrArgument);
223 TPos TBlockTransfer::GetTailBlockOffset(TPos aPos, TInt aLen)
226 return ((aPos + aLen) % iBlockLength);
229 TLba TBlockTransfer::GetTailBlockLbaL(TPos aPos, TInt aLen)
232 TLba lba = I64LOW((aPos + aLen) / iBlockLength);
234 User::Leave(KErrArgument);