[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
|
---|
| 35 | * @brief TODO
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <errno.h>
|
---|
| 39 | #include <libblock.h>
|
---|
| 40 | #include <sys/types.h>
|
---|
| 41 | #include "libext4.h"
|
---|
| 42 |
|
---|
| 43 | static void ext4_bitmap_free_bit(uint8_t *bitmap, uint32_t index)
|
---|
| 44 | {
|
---|
| 45 | uint32_t byte_index = index / 8;
|
---|
| 46 | uint32_t bit_index = index % 8;
|
---|
| 47 |
|
---|
| 48 | uint8_t *target = bitmap + byte_index;
|
---|
| 49 | uint8_t old_value = *target;
|
---|
| 50 |
|
---|
| 51 | uint8_t new_value = old_value & (~ (1 << bit_index));
|
---|
| 52 |
|
---|
| 53 | *target = new_value;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | int ext4_bitmap_free_block(ext4_filesystem_t *fs, uint32_t block_index)
|
---|
| 57 | {
|
---|
| 58 | int rc;
|
---|
| 59 | uint32_t blocks_per_group;
|
---|
| 60 | uint32_t block_group;
|
---|
| 61 | uint32_t index_in_group;
|
---|
| 62 | uint32_t bitmap_block;
|
---|
| 63 | ext4_block_group_ref_t *bg_ref;
|
---|
| 64 | block_t *block;
|
---|
| 65 |
|
---|
| 66 | blocks_per_group = ext4_superblock_get_blocks_per_group(fs->superblock);
|
---|
| 67 | block_group = block_index / blocks_per_group;
|
---|
| 68 | index_in_group = block_index % blocks_per_group;
|
---|
| 69 |
|
---|
| 70 | rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
---|
| 71 | if (rc != EOK) {
|
---|
| 72 | return rc;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | bitmap_block = ext4_block_group_get_block_bitmap(bg_ref->block_group);
|
---|
| 76 |
|
---|
| 77 | rc = block_get(&block, fs->device, bitmap_block, 0);
|
---|
| 78 | if (rc != EOK) {
|
---|
| 79 | return rc;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | ext4_bitmap_free_bit(block->data, index_in_group);
|
---|
| 83 |
|
---|
| 84 | block->dirty = true;
|
---|
| 85 | rc = block_put(block);
|
---|
| 86 | if (rc != EOK) {
|
---|
| 87 | return rc;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | uint32_t free_blocks = ext4_block_group_get_free_blocks_count(bg_ref->block_group);
|
---|
| 91 | free_blocks++;
|
---|
| 92 | ext4_block_group_set_free_blocks_count(bg_ref->block_group, free_blocks);
|
---|
| 93 |
|
---|
| 94 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
| 95 | if (rc != EOK) {
|
---|
| 96 | // TODO error
|
---|
| 97 | return rc;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | return EOK;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 |
|
---|
| 104 | /**
|
---|
| 105 | * @}
|
---|
| 106 | */
|
---|