source: mainline/uspace/lib/ext4/libext4_bitmap.c@ 1e65444

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1e65444 was 1e65444, checked in by Frantisek Princ <frantisek.princ@…>, 14 years ago

more functional file writing - indirect blocks too

  • Property mode set to 100644
File size: 5.6 KB
RevLine 
[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
[12b4a7f]35 * @brief Ext4 bitmap (block & inode) operations.
[052e82d]36 */
37
38#include <errno.h>
39#include <libblock.h>
40#include <sys/types.h>
41#include "libext4.h"
42
43static void ext4_bitmap_free_bit(uint8_t *bitmap, uint32_t index)
44{
[1e65444]45 // Block numbers are 1-based
[052e82d]46 uint32_t byte_index = index / 8;
47 uint32_t bit_index = index % 8;
48
[1e65444]49// EXT4FS_DBG("freeing block \%u, byte \%u and bit \%u", index, byte_index, bit_index);
50
[052e82d]51 uint8_t *target = bitmap + byte_index;
52 uint8_t old_value = *target;
53
54 uint8_t new_value = old_value & (~ (1 << bit_index));
55
56 *target = new_value;
57}
58
[1c1c736]59static int ext4_bitmap_find_free_bit_and_set(uint8_t *bitmap, uint32_t *index, uint32_t size)
60{
61 uint8_t *pos = bitmap;
62 int i;
63 uint32_t idx = 0;
[1e65444]64 uint8_t value, new_value;
[1c1c736]65
66 while (pos < bitmap + size) {
67 if ((*pos & 255) != 255) {
68 // free bit found
69 break;
70 }
71
72 ++pos;
73 idx += 8;
74 }
75
76 if (pos < bitmap + size) {
77
[1e65444]78// EXT4FS_DBG("byte found \%u", (uint32_t)(pos - bitmap));
79
[1c1c736]80 for(i = 0; i < 8; ++i) {
[1e65444]81 value = *pos;
82
83 if ((value & (1 << i)) == 0) {
[1c1c736]84 // free bit found
[1e65444]85// EXT4FS_DBG("bit found \%u", i);
86 new_value = value | (1 << i);
87 *pos = new_value;
88 *index = idx + i;
[1c1c736]89 return EOK;
90 }
91 }
92 }
93
94 return ENOSPC;
95}
96
[052e82d]97int ext4_bitmap_free_block(ext4_filesystem_t *fs, uint32_t block_index)
98{
99 int rc;
100 uint32_t blocks_per_group;
101 uint32_t block_group;
102 uint32_t index_in_group;
103 uint32_t bitmap_block;
104 ext4_block_group_ref_t *bg_ref;
105 block_t *block;
106
107 blocks_per_group = ext4_superblock_get_blocks_per_group(fs->superblock);
[1e65444]108 block_group = ((block_index - 1) / blocks_per_group);
109 index_in_group = (block_index - 1) % blocks_per_group;
[052e82d]110
111 rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
112 if (rc != EOK) {
113 return rc;
114 }
115
116 bitmap_block = ext4_block_group_get_block_bitmap(bg_ref->block_group);
117
118 rc = block_get(&block, fs->device, bitmap_block, 0);
119 if (rc != EOK) {
120 return rc;
121 }
122
123 ext4_bitmap_free_bit(block->data, index_in_group);
124
125 block->dirty = true;
126 rc = block_put(block);
127 if (rc != EOK) {
128 return rc;
129 }
130
131 uint32_t free_blocks = ext4_block_group_get_free_blocks_count(bg_ref->block_group);
132 free_blocks++;
133 ext4_block_group_set_free_blocks_count(bg_ref->block_group, free_blocks);
[12b4a7f]134 bg_ref->dirty = true;
[052e82d]135
[1c1c736]136 // TODO change free blocks count in superblock
137
[052e82d]138 rc = ext4_filesystem_put_block_group_ref(bg_ref);
139 if (rc != EOK) {
140 // TODO error
141 return rc;
142 }
143
[1e65444]144 EXT4FS_DBG("block \%u released", block_index);
145
[052e82d]146 return EOK;
147}
148
[1c1c736]149int ext4_bitmap_alloc_block(ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, uint32_t *fblock)
150{
151 int rc;
152 uint32_t inodes_per_group, block_group, blocks_per_group;
153 ext4_block_group_ref_t *bg_ref;
154 uint32_t bitmap_block;
155 block_t *block;
156 uint32_t rel_block_idx = 0;
157 uint32_t block_size;
158
159 inodes_per_group = ext4_superblock_get_inodes_per_group(fs->superblock);
[1e65444]160 block_group = (inode_ref->index - 1) / inodes_per_group;
[1c1c736]161
162 block_size = ext4_superblock_get_block_size(fs->superblock);
163
164 rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
165 if (rc != EOK) {
166 return rc;
167 }
168
169 bitmap_block = ext4_block_group_get_block_bitmap(bg_ref->block_group);
170
171 rc = block_get(&block, fs->device, bitmap_block, 0);
172 if (rc != EOK) {
173 ext4_filesystem_put_block_group_ref(bg_ref);
174 return rc;
175 }
176
177 rc = ext4_bitmap_find_free_bit_and_set(block->data, &rel_block_idx, block_size);
[35f48f2]178 if (rc != EOK) {
179 EXT4FS_DBG("no block found");
180 // TODO if ENOSPC - try next block group - try next block groups
181 }
[1c1c736]182
[1e65444]183 block->dirty = true;
184
185 // TODO check retval
186 block_put(block);
187
[35f48f2]188 // TODO decrement superblock free blocks count
189 //uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
190 //sb_free_blocks--;
191 //ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
[1c1c736]192
[35f48f2]193 uint32_t bg_free_blocks = ext4_block_group_get_free_blocks_count(bg_ref->block_group);
[1e65444]194 bg_free_blocks--;
[35f48f2]195 ext4_block_group_set_free_blocks_count(bg_ref->block_group, bg_free_blocks);
196 bg_ref->dirty = true;
[1c1c736]197
[1e65444]198 ext4_filesystem_put_block_group_ref(bg_ref);
199
[1c1c736]200 blocks_per_group = ext4_superblock_get_blocks_per_group(fs->superblock);
[1e65444]201
202 EXT4FS_DBG("block \%u allocated", blocks_per_group * block_group + rel_block_idx + 1);
203
204 *fblock = blocks_per_group * block_group + rel_block_idx + 1;
[1c1c736]205 return EOK;
206
207}
[052e82d]208
209/**
210 * @}
211 */
Note: See TracBrowser for help on using the repository browser.