1 /* ----> DO NOT REMOVE THE FOLLOWING NOTICE <----
\r
3 Copyright (c) 2014-2015 Datalight, Inc.
\r
4 All Rights Reserved Worldwide.
\r
6 This program is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; use version 2 of the License.
\r
10 This program is distributed in the hope that it will be useful,
\r
11 but "AS-IS," WITHOUT ANY WARRANTY; without even the implied warranty
\r
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 GNU General Public License for more details.
\r
15 You should have received a copy of the GNU General Public License along
\r
16 with this program; if not, write to the Free Software Foundation, Inc.,
\r
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
\r
19 /* Businesses and individuals that for commercial or other reasons cannot
\r
20 comply with the terms of the GPLv2 license may obtain a commercial license
\r
21 before incorporating Reliance Edge into proprietary software for
\r
22 distribution in any form. Visit http://www.datalight.com/reliance-edge for
\r
26 @brief Implements routines for the external imap.
\r
28 The external imap is used on volumes that are too big for the imap bitmap
\r
29 to be stored entirely in the metaroot, so instead the bitmap is stored in
\r
30 imap nodes on disk, and the metaroot bitmap is used to toggle between imap
\r
35 #if REDCONF_IMAP_EXTERNAL == 1
\r
37 #include <redcore.h>
\r
40 #if REDCONF_READ_ONLY == 0
\r
41 static REDSTATUS ImapNodeBranch(uint32_t ulImapNode, IMAPNODE **ppImap);
\r
42 static bool ImapNodeIsBranched(uint32_t ulImapNode);
\r
46 /** @brief Get the allocation bit of a block from the imap as it exists in
\r
49 @param bMR The metaroot index: either 0 or 1.
\r
50 @param ulBlock The block number to query.
\r
51 @param pfAllocated On successful exit, populated with the allocation bit
\r
54 @return A negated ::REDSTATUS code indicating the operation result.
\r
56 @retval 0 Operation was successful.
\r
57 @retval -RED_EINVAL @p bMR is out of range; or @p ulBlock is out of range;
\r
58 or @p pfAllocated is `NULL`.
\r
59 @retval -RED_EIO A disk I/O error occurred.
\r
61 REDSTATUS RedImapEBlockGet(
\r
68 if( gpRedCoreVol->fImapInline
\r
70 || (ulBlock < gpRedCoreVol->ulInodeTableStartBN)
\r
71 || (ulBlock >= gpRedVolume->ulBlockCount)
\r
72 || (pfAllocated == NULL))
\r
79 uint32_t ulOffset = ulBlock - gpRedCoreVol->ulInodeTableStartBN;
\r
80 uint32_t ulImapNode = ulOffset / IMAPNODE_ENTRIES;
\r
81 uint8_t bMRToRead = bMR;
\r
84 #if REDCONF_READ_ONLY == 0
\r
85 /* If the imap node is not branched, then both copies of the imap are
\r
86 identical. If the old metaroot copy is requested, use the current
\r
87 copy instead, since it is more likely to be buffered.
\r
89 if(bMR == (1U - gpRedCoreVol->bCurMR))
\r
91 if(!ImapNodeIsBranched(ulImapNode))
\r
93 bMRToRead = 1U - bMR;
\r
98 ret = RedBufferGet(RedImapNodeBlock(bMRToRead, ulImapNode), BFLAG_META_IMAP, CAST_VOID_PTR_PTR(&pImap));
\r
102 *pfAllocated = RedBitGet(pImap->abEntries, ulOffset % IMAPNODE_ENTRIES);
\r
104 RedBufferPut(pImap);
\r
112 #if REDCONF_READ_ONLY == 0
\r
113 /** @brief Set the allocation bit of a block in the working-state imap.
\r
115 @param ulBlock The block number to allocate or free.
\r
116 @param fAllocated Whether to allocate the block (true) or free it (false).
\r
118 @return A negated ::REDSTATUS code indicating the operation result.
\r
120 @retval 0 Operation was successful.
\r
121 @retval -RED_EINVAL @p ulBlock is out of range.
\r
122 @retval -RED_EIO A disk I/O error occurred.
\r
124 REDSTATUS RedImapEBlockSet(
\r
130 if( gpRedCoreVol->fImapInline
\r
131 || (ulBlock < gpRedCoreVol->ulInodeTableStartBN)
\r
132 || (ulBlock >= gpRedVolume->ulBlockCount))
\r
139 uint32_t ulOffset = ulBlock - gpRedCoreVol->ulInodeTableStartBN;
\r
140 uint32_t ulImapNode = ulOffset / IMAPNODE_ENTRIES;
\r
143 ret = ImapNodeBranch(ulImapNode, &pImap);
\r
147 uint32_t ulImapEntry = ulOffset % IMAPNODE_ENTRIES;
\r
149 if(RedBitGet(pImap->abEntries, ulImapEntry) == fAllocated)
\r
151 /* The driver shouldn't ever set a bit in the imap to its
\r
152 current value. That shouldn't ever be needed, and it
\r
153 indicates that the driver is doing unnecessary I/O, or
\r
154 that the imap is corrupt.
\r
159 else if(fAllocated)
\r
161 RedBitSet(pImap->abEntries, ulImapEntry);
\r
165 RedBitClear(pImap->abEntries, ulImapEntry);
\r
168 RedBufferPut(pImap);
\r
176 /** @brief Branch an imap node and get a buffer for it.
\r
178 If the imap node is already branched, it can be overwritten in its current
\r
179 location, and this function just gets it buffered dirty. If the node is not
\r
180 already branched, the metaroot must be updated to toggle the imap node to
\r
181 its alternate location, thereby preserving the committed state copy of the
\r
184 @param ulImapNode The imap node to branch and buffer.
\r
185 @param ppImap On successful return, populated with the imap node
\r
186 buffer, which will be marked dirty.
\r
188 @return A negated ::REDSTATUS code indicating the operation result.
\r
190 @retval 0 Operation was successful.
\r
191 @retval -RED_EINVAL @p ulImapNode is out of range; or @p ppImap is `NULL`.
\r
192 @retval -RED_EIO A disk I/O error occurred.
\r
194 static REDSTATUS ImapNodeBranch(
\r
195 uint32_t ulImapNode,
\r
200 if((ulImapNode >= gpRedCoreVol->ulImapNodeCount) || (ppImap == NULL))
\r
205 else if(ImapNodeIsBranched(ulImapNode))
\r
207 /* Imap node is already branched, so just get it buffered dirty.
\r
209 ret = RedBufferGet(RedImapNodeBlock(gpRedCoreVol->bCurMR, ulImapNode), BFLAG_META_IMAP | BFLAG_DIRTY, CAST_VOID_PTR_PTR(ppImap));
\r
213 uint32_t ulBlockCurrent;
\r
214 uint32_t ulBlockOld;
\r
216 /* The metaroot currently points to the committed state imap node.
\r
217 Toggle the metaroot to point at the alternate, writeable location.
\r
219 if(RedBitGet(gpRedMR->abEntries, ulImapNode))
\r
221 RedBitClear(gpRedMR->abEntries, ulImapNode);
\r
225 RedBitSet(gpRedMR->abEntries, ulImapNode);
\r
228 ulBlockCurrent = RedImapNodeBlock(gpRedCoreVol->bCurMR, ulImapNode);
\r
229 ulBlockOld = RedImapNodeBlock(1U - gpRedCoreVol->bCurMR, ulImapNode);
\r
231 ret = RedBufferDiscardRange(ulBlockCurrent, 1U);
\r
233 /* Buffer the committed copy then reassign the block number to the
\r
234 writeable location. This also dirties the buffer.
\r
238 ret = RedBufferGet(ulBlockOld, BFLAG_META_IMAP, CAST_VOID_PTR_PTR(ppImap));
\r
242 RedBufferBranch(*ppImap, ulBlockCurrent);
\r
251 /** @brief Determine whether an imap node is branched.
\r
253 If the imap node is branched, it can be overwritten in its current location.
\r
255 @param ulImapNode The imap node to examine.
\r
257 @return Whether the imap node is branched.
\r
259 static bool ImapNodeIsBranched(
\r
260 uint32_t ulImapNode)
\r
262 bool fNodeBitSetInMetaroot0 = RedBitGet(gpRedCoreVol->aMR[0U].abEntries, ulImapNode);
\r
263 bool fNodeBitSetInMetaroot1 = RedBitGet(gpRedCoreVol->aMR[1U].abEntries, ulImapNode);
\r
265 /* If the imap node is not branched, both metaroots will point to the same
\r
268 return fNodeBitSetInMetaroot0 != fNodeBitSetInMetaroot1;
\r
270 #endif /* REDCONF_READ_ONLY == 0 */
\r
273 /** @brief Calculate the block number of the imap node location indicated by the
\r
276 An imap node has two locations on disk. A bit in the metaroot bitmap
\r
277 indicates which location is the valid one, according to that metaroot. This
\r
278 function returns the block number of the imap node which is valid in the
\r
281 @param bMR Which metaroot to examine.
\r
282 @param ulImapNode The imap node for which to calculate the block number.
\r
284 @return Block number of the imap node, as indicated by the given metaroot.
\r
286 uint32_t RedImapNodeBlock(
\r
288 uint32_t ulImapNode)
\r
292 REDASSERT(ulImapNode < gpRedCoreVol->ulImapNodeCount);
\r
294 ulBlock = gpRedCoreVol->ulImapStartBN + (ulImapNode * 2U);
\r
300 else if(RedBitGet(gpRedCoreVol->aMR[bMR].abEntries, ulImapNode))
\r
302 /* Bit is set, so point ulBlock at the second copy of the node.
\r
308 /* ulBlock already points at the first copy of the node.
\r
315 #endif /* REDCONF_IMAP_EXTERNAL == 1 */
\r