[052e82d] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 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
|
---|
[2674db6] | 35 | * @brief Ext4 bitmap operations.
|
---|
[052e82d] | 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <errno.h>
|
---|
| 39 | #include <libblock.h>
|
---|
| 40 | #include <sys/types.h>
|
---|
| 41 | #include "libext4.h"
|
---|
| 42 |
|
---|
[2674db6] | 43 | void ext4_bitmap_free_bit(uint8_t *bitmap, uint32_t index)
|
---|
[052e82d] | 44 | {
|
---|
| 45 | uint32_t byte_index = index / 8;
|
---|
| 46 | uint32_t bit_index = index % 8;
|
---|
| 47 |
|
---|
[2674db6] | 48 | uint8_t *target = bitmap + byte_index;
|
---|
| 49 |
|
---|
| 50 | *target &= ~ (1 << bit_index);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[662bd71] | 53 | void ext4_bitmap_free_bits(uint8_t *bitmap, uint32_t index, uint32_t count)
|
---|
| 54 | {
|
---|
| 55 | uint8_t *target;
|
---|
| 56 | uint32_t idx = index;
|
---|
| 57 | uint32_t remaining = count;
|
---|
| 58 | uint32_t byte_index;
|
---|
| 59 |
|
---|
| 60 | while (((idx % 8) != 0) && (remaining > 0)) {
|
---|
| 61 |
|
---|
| 62 | byte_index = idx / 8;
|
---|
| 63 | uint32_t bit_index = idx % 8;
|
---|
| 64 |
|
---|
| 65 | target = bitmap + byte_index;
|
---|
| 66 |
|
---|
| 67 | *target &= ~ (1 << bit_index);
|
---|
| 68 |
|
---|
| 69 | idx++;
|
---|
| 70 | remaining--;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[e7ed26be] | 73 | if (remaining == 0) {
|
---|
| 74 | return;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[662bd71] | 77 | assert((idx % 8) == 0);
|
---|
| 78 |
|
---|
| 79 | byte_index = idx / 8;
|
---|
| 80 | target = bitmap + byte_index;
|
---|
| 81 |
|
---|
| 82 | while (remaining >= 8) {
|
---|
| 83 | *target = 0;
|
---|
| 84 |
|
---|
| 85 | idx += 8;
|
---|
| 86 | remaining -= 8;
|
---|
| 87 | target++;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | assert(remaining < 8);
|
---|
| 91 |
|
---|
| 92 | while (remaining != 0) {
|
---|
| 93 |
|
---|
| 94 | byte_index = idx / 8;
|
---|
| 95 | uint32_t bit_index = idx % 8;
|
---|
| 96 |
|
---|
| 97 | target = bitmap + byte_index;
|
---|
| 98 |
|
---|
| 99 | *target &= ~ (1 << bit_index);
|
---|
| 100 |
|
---|
| 101 | idx++;
|
---|
| 102 | remaining--;
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[2674db6] | 106 | void ext4_bitmap_set_bit(uint8_t *bitmap, uint32_t index)
|
---|
| 107 | {
|
---|
| 108 | uint32_t byte_index = index / 8;
|
---|
| 109 | uint32_t bit_index = index % 8;
|
---|
| 110 |
|
---|
| 111 | uint8_t *target = bitmap + byte_index;
|
---|
| 112 |
|
---|
| 113 | *target |= 1 << bit_index;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | bool ext4_bitmap_is_free_bit(uint8_t *bitmap, uint32_t index)
|
---|
| 117 | {
|
---|
| 118 | uint32_t byte_index = index / 8;
|
---|
| 119 | uint32_t bit_index = index % 8;
|
---|
[1e65444] | 120 |
|
---|
[052e82d] | 121 | uint8_t *target = bitmap + byte_index;
|
---|
| 122 |
|
---|
[2674db6] | 123 | if (*target & (1 << bit_index)) {
|
---|
| 124 | return false;
|
---|
| 125 | } else {
|
---|
| 126 | return true;
|
---|
| 127 | }
|
---|
[052e82d] | 128 |
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[b12ca16] | 131 | int ext4_bitmap_find_free_byte_and_set_bit(uint8_t *bitmap, uint32_t start, uint32_t *index, uint32_t max)
|
---|
[1c1c736] | 132 | {
|
---|
[b12ca16] | 133 | uint32_t idx;
|
---|
| 134 | if (start % 8) {
|
---|
| 135 | idx = start + (8 - (start % 8));
|
---|
| 136 | } else {
|
---|
| 137 | idx = start;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | uint8_t *pos = bitmap + (idx / 8);
|
---|
| 141 |
|
---|
| 142 | while (idx < max) {
|
---|
[2674db6] | 143 |
|
---|
| 144 | if (*pos == 0) {
|
---|
| 145 | *pos |= 1;
|
---|
| 146 |
|
---|
[b12ca16] | 147 | *index = idx;
|
---|
[2674db6] | 148 | return EOK;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[b12ca16] | 151 | idx += 8;
|
---|
[2674db6] | 152 | ++pos;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | return ENOSPC;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[b12ca16] | 158 | int ext4_bitmap_find_free_bit_and_set(uint8_t *bitmap, uint32_t start_idx,
|
---|
| 159 | uint32_t *index, uint32_t max)
|
---|
[2674db6] | 160 | {
|
---|
[b12ca16] | 161 | uint8_t *pos = bitmap + (start_idx / 8);
|
---|
| 162 | uint32_t idx = start_idx;
|
---|
| 163 | bool byte_part = false;
|
---|
[1c1c736] | 164 |
|
---|
[b12ca16] | 165 | // Check the rest of byte
|
---|
| 166 | while ((idx % 8) != 0) {
|
---|
| 167 | byte_part = true;
|
---|
| 168 |
|
---|
[528e5b3] | 169 | if ((*pos & (1 << (idx % 8))) == 0) {
|
---|
[b12ca16] | 170 | *pos |= (1 << (idx % 8));
|
---|
| 171 | *index = idx;
|
---|
| 172 | return EOK;
|
---|
[1c1c736] | 173 | }
|
---|
| 174 |
|
---|
[b12ca16] | 175 | ++idx;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | if (byte_part) {
|
---|
[1c1c736] | 179 | ++pos;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[b12ca16] | 182 | while (idx < max) {
|
---|
| 183 |
|
---|
| 184 | if ((*pos & 255) != 255) {
|
---|
| 185 | // free bit found
|
---|
| 186 | break;
|
---|
[2674db6] | 187 | }
|
---|
[1c1c736] | 188 |
|
---|
[b12ca16] | 189 | idx += 8;
|
---|
| 190 | ++pos;
|
---|
| 191 | }
|
---|
[1e65444] | 192 |
|
---|
| 193 |
|
---|
[b12ca16] | 194 | if (idx < max) {
|
---|
| 195 | for (uint8_t i = 0; i < 8; ++i) {
|
---|
| 196 | if ((*pos & (1 << i)) == 0) {
|
---|
[1c1c736] | 197 | // free bit found
|
---|
[b12ca16] | 198 | *pos |= (1 << i);
|
---|
| 199 | *index = idx;
|
---|
[1c1c736] | 200 | return EOK;
|
---|
| 201 | }
|
---|
[b12ca16] | 202 | idx++;
|
---|
[1c1c736] | 203 | }
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | return ENOSPC;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
[052e82d] | 209 | /**
|
---|
| 210 | * @}
|
---|
| 211 | */
|
---|