[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 |
|
---|
| 53 | void ext4_bitmap_set_bit(uint8_t *bitmap, uint32_t index)
|
---|
| 54 | {
|
---|
| 55 | uint32_t byte_index = index / 8;
|
---|
| 56 | uint32_t bit_index = index % 8;
|
---|
| 57 |
|
---|
| 58 | uint8_t *target = bitmap + byte_index;
|
---|
| 59 |
|
---|
| 60 | *target |= 1 << bit_index;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | bool ext4_bitmap_is_free_bit(uint8_t *bitmap, uint32_t index)
|
---|
| 64 | {
|
---|
| 65 | uint32_t byte_index = index / 8;
|
---|
| 66 | uint32_t bit_index = index % 8;
|
---|
[1e65444] | 67 |
|
---|
[052e82d] | 68 | uint8_t *target = bitmap + byte_index;
|
---|
| 69 |
|
---|
[2674db6] | 70 | if (*target & (1 << bit_index)) {
|
---|
| 71 | return false;
|
---|
| 72 | } else {
|
---|
| 73 | return true;
|
---|
| 74 | }
|
---|
[052e82d] | 75 |
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[2674db6] | 78 | int ext4_bitmap_find_free_byte_and_set_bit(uint8_t *bitmap, uint32_t start, uint32_t *index, uint32_t size)
|
---|
[1c1c736] | 79 | {
|
---|
[2674db6] | 80 | uint8_t *pos = bitmap + (start / 8) + 1;
|
---|
| 81 |
|
---|
| 82 | while (pos < bitmap + size) {
|
---|
| 83 | if (*pos == 0) {
|
---|
| 84 | *pos |= 1;
|
---|
| 85 |
|
---|
| 86 | *index = (pos - bitmap) * 8;
|
---|
| 87 | return EOK;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | ++pos;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | return ENOSPC;
|
---|
| 94 |
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | int ext4_bitmap_find_free_bit_and_set(uint8_t *bitmap, uint32_t start, uint32_t *index, uint32_t size)
|
---|
| 98 | {
|
---|
| 99 | uint8_t *pos = bitmap + (start / 8);
|
---|
[1c1c736] | 100 | int i;
|
---|
[1e65444] | 101 | uint8_t value, new_value;
|
---|
[1c1c736] | 102 |
|
---|
| 103 | while (pos < bitmap + size) {
|
---|
| 104 | if ((*pos & 255) != 255) {
|
---|
| 105 | // free bit found
|
---|
| 106 | break;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | ++pos;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[2674db6] | 112 | // Check the byte containing start
|
---|
| 113 | if (pos == bitmap + (start / 8)) {
|
---|
| 114 | for (i = start % 8; i < 8; ++i) {
|
---|
| 115 | value = *pos;
|
---|
| 116 | if ((value & (1 << i)) == 0) {
|
---|
| 117 | // free bit found
|
---|
| 118 | new_value = value | (1 << i);
|
---|
| 119 | *pos = new_value;
|
---|
| 120 | *index = (pos - bitmap) * 8 + i;
|
---|
| 121 | return EOK;
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
[1c1c736] | 125 |
|
---|
[2674db6] | 126 | if (pos < bitmap + size) {
|
---|
[1e65444] | 127 |
|
---|
[1c1c736] | 128 | for(i = 0; i < 8; ++i) {
|
---|
[1e65444] | 129 | value = *pos;
|
---|
| 130 |
|
---|
| 131 | if ((value & (1 << i)) == 0) {
|
---|
[1c1c736] | 132 | // free bit found
|
---|
[1e65444] | 133 | new_value = value | (1 << i);
|
---|
| 134 | *pos = new_value;
|
---|
[2674db6] | 135 | *index = (pos - bitmap) * 8 + i;
|
---|
[1c1c736] | 136 | return EOK;
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | return ENOSPC;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[052e82d] | 144 | /**
|
---|
| 145 | * @}
|
---|
| 146 | */
|
---|