[052e82d] | 1 | /*
|
---|
[f22d5ef0] | 2 | * Copyright (c) 2012 Frantisek Princ
|
---|
[052e82d] | 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 | * @{
|
---|
[38542dc] | 31 | */
|
---|
[052e82d] | 32 | /**
|
---|
[38542dc] | 33 | * @file libext4_bitmap.c
|
---|
| 34 | * @brief Ext4 bitmap operations.
|
---|
[052e82d] | 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <errno.h>
|
---|
[f73b291] | 38 | #include <block.h>
|
---|
[052e82d] | 39 | #include <sys/types.h>
|
---|
[fcb0d76] | 40 | #include "ext4/bitmap.h"
|
---|
[052e82d] | 41 |
|
---|
[7b16549] | 42 | /** Set bit in bitmap to 0 (free).
|
---|
| 43 | *
|
---|
| 44 | * Index must be checked by caller, if it's not out of bounds.
|
---|
| 45 | *
|
---|
[38542dc] | 46 | * @param bitmap Pointer to bitmap
|
---|
| 47 | * @param index Index of bit in bitmap
|
---|
| 48 | *
|
---|
[7b16549] | 49 | */
|
---|
[2674db6] | 50 | void ext4_bitmap_free_bit(uint8_t *bitmap, uint32_t index)
|
---|
[052e82d] | 51 | {
|
---|
| 52 | uint32_t byte_index = index / 8;
|
---|
| 53 | uint32_t bit_index = index % 8;
|
---|
[38542dc] | 54 |
|
---|
[2674db6] | 55 | uint8_t *target = bitmap + byte_index;
|
---|
[38542dc] | 56 |
|
---|
[2674db6] | 57 | *target &= ~ (1 << bit_index);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[7b16549] | 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 | *
|
---|
[38542dc] | 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 | *
|
---|
[7b16549] | 68 | */
|
---|
[662bd71] | 69 | void ext4_bitmap_free_bits(uint8_t *bitmap, uint32_t index, uint32_t count)
|
---|
| 70 | {
|
---|
| 71 | uint8_t *target;
|
---|
| 72 | uint32_t idx = index;
|
---|
| 73 | uint32_t remaining = count;
|
---|
| 74 | uint32_t byte_index;
|
---|
[38542dc] | 75 |
|
---|
[06d85e5] | 76 | /* Align index to multiple of 8 */
|
---|
[662bd71] | 77 | while (((idx % 8) != 0) && (remaining > 0)) {
|
---|
| 78 | byte_index = idx / 8;
|
---|
| 79 | uint32_t bit_index = idx % 8;
|
---|
[38542dc] | 80 |
|
---|
[662bd71] | 81 | target = bitmap + byte_index;
|
---|
| 82 | *target &= ~ (1 << bit_index);
|
---|
[38542dc] | 83 |
|
---|
[662bd71] | 84 | idx++;
|
---|
| 85 | remaining--;
|
---|
| 86 | }
|
---|
[38542dc] | 87 |
|
---|
[06d85e5] | 88 | /* For < 8 bits this check necessary */
|
---|
[38542dc] | 89 | if (remaining == 0)
|
---|
[e7ed26be] | 90 | return;
|
---|
[38542dc] | 91 |
|
---|
[662bd71] | 92 | assert((idx % 8) == 0);
|
---|
[38542dc] | 93 |
|
---|
[662bd71] | 94 | byte_index = idx / 8;
|
---|
| 95 | target = bitmap + byte_index;
|
---|
[38542dc] | 96 |
|
---|
[06d85e5] | 97 | /* Zero the whole bytes */
|
---|
[662bd71] | 98 | while (remaining >= 8) {
|
---|
| 99 | *target = 0;
|
---|
[38542dc] | 100 |
|
---|
[662bd71] | 101 | idx += 8;
|
---|
| 102 | remaining -= 8;
|
---|
| 103 | target++;
|
---|
| 104 | }
|
---|
[38542dc] | 105 |
|
---|
[662bd71] | 106 | assert(remaining < 8);
|
---|
[38542dc] | 107 |
|
---|
[06d85e5] | 108 | /* Zero remaining bytes */
|
---|
[662bd71] | 109 | while (remaining != 0) {
|
---|
| 110 | byte_index = idx / 8;
|
---|
| 111 | uint32_t bit_index = idx % 8;
|
---|
[38542dc] | 112 |
|
---|
[662bd71] | 113 | target = bitmap + byte_index;
|
---|
| 114 | *target &= ~ (1 << bit_index);
|
---|
[38542dc] | 115 |
|
---|
[662bd71] | 116 | idx++;
|
---|
| 117 | remaining--;
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[7b16549] | 121 | /** Set bit in bitmap to 1 (used).
|
---|
| 122 | *
|
---|
[38542dc] | 123 | * @param bitmap Pointer to bitmap
|
---|
| 124 | * @param index Index of bit to set
|
---|
| 125 | *
|
---|
[7b16549] | 126 | */
|
---|
[2674db6] | 127 | void ext4_bitmap_set_bit(uint8_t *bitmap, uint32_t index)
|
---|
| 128 | {
|
---|
| 129 | uint32_t byte_index = index / 8;
|
---|
| 130 | uint32_t bit_index = index % 8;
|
---|
[38542dc] | 131 |
|
---|
[2674db6] | 132 | uint8_t *target = bitmap + byte_index;
|
---|
[38542dc] | 133 |
|
---|
[2674db6] | 134 | *target |= 1 << bit_index;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[7b16549] | 137 | /** Check if requested bit is free.
|
---|
| 138 | *
|
---|
[38542dc] | 139 | * @param bitmap Pointer to bitmap
|
---|
| 140 | * @param index Index of bit to be checked
|
---|
| 141 | *
|
---|
| 142 | * @return True if bit is free, else false
|
---|
| 143 | *
|
---|
[7b16549] | 144 | */
|
---|
[2674db6] | 145 | bool ext4_bitmap_is_free_bit(uint8_t *bitmap, uint32_t index)
|
---|
| 146 | {
|
---|
| 147 | uint32_t byte_index = index / 8;
|
---|
| 148 | uint32_t bit_index = index % 8;
|
---|
[38542dc] | 149 |
|
---|
[052e82d] | 150 | uint8_t *target = bitmap + byte_index;
|
---|
[38542dc] | 151 |
|
---|
| 152 | if (*target & (1 << bit_index))
|
---|
[2674db6] | 153 | return false;
|
---|
[38542dc] | 154 | else
|
---|
[2674db6] | 155 | return true;
|
---|
[052e82d] | 156 | }
|
---|
| 157 |
|
---|
[38542dc] | 158 | /** Try to find free byte and set the first bit as used.
|
---|
[7b16549] | 159 | *
|
---|
[38542dc] | 160 | * Walk through bitmap and try to find free byte (equal to 0).
|
---|
[7b16549] | 161 | * If byte found, set the first bit as used.
|
---|
| 162 | *
|
---|
[38542dc] | 163 | * @param bitmap Pointer to bitmap
|
---|
| 164 | * @param start Index of bit, where the algorithm will begin
|
---|
| 165 | * @param index Output value - index of bit (if found free byte)
|
---|
| 166 | * @param max Maximum index of bit in bitmap
|
---|
| 167 | *
|
---|
| 168 | * @return Error code
|
---|
| 169 | *
|
---|
[7b16549] | 170 | */
|
---|
[38542dc] | 171 | int ext4_bitmap_find_free_byte_and_set_bit(uint8_t *bitmap, uint32_t start,
|
---|
| 172 | uint32_t *index, uint32_t max)
|
---|
[1c1c736] | 173 | {
|
---|
[b12ca16] | 174 | uint32_t idx;
|
---|
[38542dc] | 175 |
|
---|
[06d85e5] | 176 | /* Align idx */
|
---|
[38542dc] | 177 | if (start % 8)
|
---|
[b12ca16] | 178 | idx = start + (8 - (start % 8));
|
---|
[38542dc] | 179 | else
|
---|
[b12ca16] | 180 | idx = start;
|
---|
[38542dc] | 181 |
|
---|
[b12ca16] | 182 | uint8_t *pos = bitmap + (idx / 8);
|
---|
[38542dc] | 183 |
|
---|
[06d85e5] | 184 | /* Try to find free byte */
|
---|
[b12ca16] | 185 | while (idx < max) {
|
---|
[2674db6] | 186 | if (*pos == 0) {
|
---|
| 187 | *pos |= 1;
|
---|
[38542dc] | 188 |
|
---|
[b12ca16] | 189 | *index = idx;
|
---|
[2674db6] | 190 | return EOK;
|
---|
| 191 | }
|
---|
[38542dc] | 192 |
|
---|
[b12ca16] | 193 | idx += 8;
|
---|
[2674db6] | 194 | ++pos;
|
---|
| 195 | }
|
---|
[38542dc] | 196 |
|
---|
[06d85e5] | 197 | /* Free byte not found */
|
---|
[2674db6] | 198 | return ENOSPC;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[7b16549] | 201 | /** Try to find free bit and set it as used (1).
|
---|
| 202 | *
|
---|
| 203 | * Walk through bitmap and try to find any free bit.
|
---|
| 204 | *
|
---|
[38542dc] | 205 | * @param bitmap Pointer to bitmap
|
---|
| 206 | * @param start_idx Index of bit, where algorithm will begin
|
---|
| 207 | * @param index Output value - index of set bit (if found)
|
---|
| 208 | * @param max Maximum index of bit in bitmap
|
---|
| 209 | *
|
---|
| 210 | * @return Error code
|
---|
| 211 | *
|
---|
[7b16549] | 212 | */
|
---|
[b12ca16] | 213 | int ext4_bitmap_find_free_bit_and_set(uint8_t *bitmap, uint32_t start_idx,
|
---|
[38542dc] | 214 | uint32_t *index, uint32_t max)
|
---|
[2674db6] | 215 | {
|
---|
[b12ca16] | 216 | uint8_t *pos = bitmap + (start_idx / 8);
|
---|
| 217 | uint32_t idx = start_idx;
|
---|
| 218 | bool byte_part = false;
|
---|
[38542dc] | 219 |
|
---|
[06d85e5] | 220 | /* Check the rest of first byte */
|
---|
[b12ca16] | 221 | while ((idx % 8) != 0) {
|
---|
| 222 | byte_part = true;
|
---|
[38542dc] | 223 |
|
---|
[528e5b3] | 224 | if ((*pos & (1 << (idx % 8))) == 0) {
|
---|
[b12ca16] | 225 | *pos |= (1 << (idx % 8));
|
---|
| 226 | *index = idx;
|
---|
| 227 | return EOK;
|
---|
[1c1c736] | 228 | }
|
---|
[38542dc] | 229 |
|
---|
[b12ca16] | 230 | ++idx;
|
---|
| 231 | }
|
---|
[38542dc] | 232 |
|
---|
| 233 | if (byte_part)
|
---|
[1c1c736] | 234 | ++pos;
|
---|
[38542dc] | 235 |
|
---|
[06d85e5] | 236 | /* Check the whole bytes (255 = 11111111 binary) */
|
---|
[b12ca16] | 237 | while (idx < max) {
|
---|
| 238 | if ((*pos & 255) != 255) {
|
---|
[38542dc] | 239 | /* Free bit found */
|
---|
[b12ca16] | 240 | break;
|
---|
[2674db6] | 241 | }
|
---|
[38542dc] | 242 |
|
---|
[b12ca16] | 243 | idx += 8;
|
---|
| 244 | ++pos;
|
---|
| 245 | }
|
---|
[38542dc] | 246 |
|
---|
[06d85e5] | 247 | /* If idx < max, some free bit found */
|
---|
[b12ca16] | 248 | if (idx < max) {
|
---|
[06d85e5] | 249 | /* Check which bit from byte is free */
|
---|
[b12ca16] | 250 | for (uint8_t i = 0; i < 8; ++i) {
|
---|
| 251 | if ((*pos & (1 << i)) == 0) {
|
---|
[38542dc] | 252 | /* Free bit found */
|
---|
| 253 | *pos |= (1 << i);
|
---|
| 254 |
|
---|
[b12ca16] | 255 | *index = idx;
|
---|
[1c1c736] | 256 | return EOK;
|
---|
| 257 | }
|
---|
[38542dc] | 258 |
|
---|
[b12ca16] | 259 | idx++;
|
---|
[1c1c736] | 260 | }
|
---|
| 261 | }
|
---|
[38542dc] | 262 |
|
---|
[06d85e5] | 263 | /* Free bit not found */
|
---|
[1c1c736] | 264 | return ENOSPC;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
[052e82d] | 267 | /**
|
---|
| 268 | * @}
|
---|
[38542dc] | 269 | */
|
---|