1 | /*
|
---|
2 | * Copyright (c) 2012 Frantisek Princ
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libext4
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file libext4_bitmap.c
|
---|
35 | * @brief Ext4 bitmap operations.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <errno.h>
|
---|
39 | #include <libblock.h>
|
---|
40 | #include <sys/types.h>
|
---|
41 | #include "libext4.h"
|
---|
42 |
|
---|
43 | /** Set bit in bitmap to 0 (free).
|
---|
44 | *
|
---|
45 | * Index must be checked by caller, if it's not out of bounds.
|
---|
46 | *
|
---|
47 | * @param bitmap pointer to bitmap
|
---|
48 | * @param index index of bit in bitmap
|
---|
49 | */
|
---|
50 | void ext4_bitmap_free_bit(uint8_t *bitmap, uint32_t index)
|
---|
51 | {
|
---|
52 | uint32_t byte_index = index / 8;
|
---|
53 | uint32_t bit_index = index % 8;
|
---|
54 |
|
---|
55 | uint8_t *target = bitmap + byte_index;
|
---|
56 |
|
---|
57 | *target &= ~ (1 << bit_index);
|
---|
58 | }
|
---|
59 |
|
---|
60 | /** Free continous set of bits (set to 0).
|
---|
61 | *
|
---|
62 | * Index and count must be checked by caller, if they aren't out of bounds.
|
---|
63 | *
|
---|
64 | * @param bitmap pointer to bitmap
|
---|
65 | * @param index index of first bit to zeroed
|
---|
66 | * @param count number of bits to be zeroed
|
---|
67 | */
|
---|
68 | void ext4_bitmap_free_bits(uint8_t *bitmap, uint32_t index, uint32_t count)
|
---|
69 | {
|
---|
70 | uint8_t *target;
|
---|
71 | uint32_t idx = index;
|
---|
72 | uint32_t remaining = count;
|
---|
73 | uint32_t byte_index;
|
---|
74 |
|
---|
75 | // Align index to multiple of 8
|
---|
76 | while (((idx % 8) != 0) && (remaining > 0)) {
|
---|
77 |
|
---|
78 | byte_index = idx / 8;
|
---|
79 | uint32_t bit_index = idx % 8;
|
---|
80 |
|
---|
81 | target = bitmap + byte_index;
|
---|
82 |
|
---|
83 | *target &= ~ (1 << bit_index);
|
---|
84 |
|
---|
85 | idx++;
|
---|
86 | remaining--;
|
---|
87 | }
|
---|
88 |
|
---|
89 | // For < 8 bits this check necessary
|
---|
90 | if (remaining == 0) {
|
---|
91 | return;
|
---|
92 | }
|
---|
93 |
|
---|
94 | assert((idx % 8) == 0);
|
---|
95 |
|
---|
96 | byte_index = idx / 8;
|
---|
97 | target = bitmap + byte_index;
|
---|
98 |
|
---|
99 | // Zero the whole bytes
|
---|
100 | while (remaining >= 8) {
|
---|
101 | *target = 0;
|
---|
102 |
|
---|
103 | idx += 8;
|
---|
104 | remaining -= 8;
|
---|
105 | target++;
|
---|
106 | }
|
---|
107 |
|
---|
108 | assert(remaining < 8);
|
---|
109 |
|
---|
110 | // Zero remaining bytes
|
---|
111 | while (remaining != 0) {
|
---|
112 |
|
---|
113 | byte_index = idx / 8;
|
---|
114 | uint32_t bit_index = idx % 8;
|
---|
115 |
|
---|
116 | target = bitmap + byte_index;
|
---|
117 |
|
---|
118 | *target &= ~ (1 << bit_index);
|
---|
119 |
|
---|
120 | idx++;
|
---|
121 | remaining--;
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | /** Set bit in bitmap to 1 (used).
|
---|
126 | *
|
---|
127 | * @param bitmap pointer to bitmap
|
---|
128 | * @param index index of bit to set
|
---|
129 | */
|
---|
130 | void ext4_bitmap_set_bit(uint8_t *bitmap, uint32_t index)
|
---|
131 | {
|
---|
132 | uint32_t byte_index = index / 8;
|
---|
133 | uint32_t bit_index = index % 8;
|
---|
134 |
|
---|
135 | uint8_t *target = bitmap + byte_index;
|
---|
136 |
|
---|
137 | *target |= 1 << bit_index;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /** Check if requested bit is free.
|
---|
141 | *
|
---|
142 | * @param bitmap pointer to bitmap
|
---|
143 | * @param index index of bit to be checked
|
---|
144 | * @return true if bit is free, else false
|
---|
145 | */
|
---|
146 | bool ext4_bitmap_is_free_bit(uint8_t *bitmap, uint32_t index)
|
---|
147 | {
|
---|
148 | uint32_t byte_index = index / 8;
|
---|
149 | uint32_t bit_index = index % 8;
|
---|
150 |
|
---|
151 | uint8_t *target = bitmap + byte_index;
|
---|
152 |
|
---|
153 | if (*target & (1 << bit_index)) {
|
---|
154 | return false;
|
---|
155 | } else {
|
---|
156 | return true;
|
---|
157 | }
|
---|
158 |
|
---|
159 | }
|
---|
160 |
|
---|
161 | /** Try to find free byte and set the first bit as used.
|
---|
162 | *
|
---|
163 | * Walk through bitmap and try to find free byte ( == 0).
|
---|
164 | * If byte found, set the first bit as used.
|
---|
165 | *
|
---|
166 | * @param bitmap pointer to bitmap
|
---|
167 | * @param start index of bit, where the algorithm will begin
|
---|
168 | * @param index output value - index of bit (if found free byte)
|
---|
169 | * @param max maximum index of bit in bitmap
|
---|
170 | * @return error code
|
---|
171 | */
|
---|
172 | int ext4_bitmap_find_free_byte_and_set_bit(uint8_t *bitmap, uint32_t start, uint32_t *index, uint32_t max)
|
---|
173 | {
|
---|
174 | uint32_t idx;
|
---|
175 |
|
---|
176 | // Align idx
|
---|
177 | if (start % 8) {
|
---|
178 | idx = start + (8 - (start % 8));
|
---|
179 | } else {
|
---|
180 | idx = start;
|
---|
181 | }
|
---|
182 |
|
---|
183 | uint8_t *pos = bitmap + (idx / 8);
|
---|
184 |
|
---|
185 | // Try to find free byte
|
---|
186 | while (idx < max) {
|
---|
187 |
|
---|
188 | if (*pos == 0) {
|
---|
189 | *pos |= 1;
|
---|
190 |
|
---|
191 | *index = idx;
|
---|
192 | return EOK;
|
---|
193 | }
|
---|
194 |
|
---|
195 | idx += 8;
|
---|
196 | ++pos;
|
---|
197 | }
|
---|
198 |
|
---|
199 | // Free byte not found
|
---|
200 | return ENOSPC;
|
---|
201 | }
|
---|
202 |
|
---|
203 | /** Try to find free bit and set it as used (1).
|
---|
204 | *
|
---|
205 | * Walk through bitmap and try to find any free bit.
|
---|
206 | *
|
---|
207 | * @param bitmap pointer to bitmap
|
---|
208 | * @param start_idx index of bit, where algorithm will begin
|
---|
209 | * @param index output value - index of set bit (if found)
|
---|
210 | * @param max maximum index of bit in bitmap
|
---|
211 | * @return error code
|
---|
212 | */
|
---|
213 | int ext4_bitmap_find_free_bit_and_set(uint8_t *bitmap, uint32_t start_idx,
|
---|
214 | uint32_t *index, uint32_t max)
|
---|
215 | {
|
---|
216 | uint8_t *pos = bitmap + (start_idx / 8);
|
---|
217 | uint32_t idx = start_idx;
|
---|
218 | bool byte_part = false;
|
---|
219 |
|
---|
220 | // Check the rest of first byte
|
---|
221 | while ((idx % 8) != 0) {
|
---|
222 | byte_part = true;
|
---|
223 |
|
---|
224 | if ((*pos & (1 << (idx % 8))) == 0) {
|
---|
225 | *pos |= (1 << (idx % 8));
|
---|
226 | *index = idx;
|
---|
227 | return EOK;
|
---|
228 | }
|
---|
229 |
|
---|
230 | ++idx;
|
---|
231 | }
|
---|
232 |
|
---|
233 | if (byte_part) {
|
---|
234 | ++pos;
|
---|
235 | }
|
---|
236 |
|
---|
237 | // Check the whole bytes (255 = 11111111 binary)
|
---|
238 | while (idx < max) {
|
---|
239 |
|
---|
240 | if ((*pos & 255) != 255) {
|
---|
241 | // free bit found
|
---|
242 | break;
|
---|
243 | }
|
---|
244 |
|
---|
245 | idx += 8;
|
---|
246 | ++pos;
|
---|
247 | }
|
---|
248 |
|
---|
249 | // If idx < max, some free bit found
|
---|
250 | if (idx < max) {
|
---|
251 |
|
---|
252 | // Check which bit from byte is free
|
---|
253 | for (uint8_t i = 0; i < 8; ++i) {
|
---|
254 | if ((*pos & (1 << i)) == 0) {
|
---|
255 | // free bit found
|
---|
256 | *pos |= (1 << i);
|
---|
257 | *index = idx;
|
---|
258 | return EOK;
|
---|
259 | }
|
---|
260 | idx++;
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | // Free bit not found
|
---|
265 | return ENOSPC;
|
---|
266 | }
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * @}
|
---|
270 | */
|
---|