source: mainline/uspace/lib/ext4/libext4_bitmap.c@ 6088193

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

bugfixes in indirect block writing

  • Property mode set to 100644
File size: 6.3 KB
Line 
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 Ext4 bitmap (block & inode) operations.
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{
45 // Block numbers are 1-based
46 uint32_t byte_index = index / 8;
47 uint32_t bit_index = index % 8;
48
49// EXT4FS_DBG("freeing block \%u, byte \%u and bit \%u", index, byte_index, bit_index);
50
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
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;
64 uint8_t value, new_value;
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
78// EXT4FS_DBG("byte found \%u", (uint32_t)(pos - bitmap));
79
80 for(i = 0; i < 8; ++i) {
81 value = *pos;
82
83 if ((value & (1 << i)) == 0) {
84 // free bit found
85// EXT4FS_DBG("bit found \%u", i);
86 new_value = value | (1 << i);
87 *pos = new_value;
88 *index = idx + i;
89 return EOK;
90 }
91 }
92 }
93
94 return ENOSPC;
95}
96
97int ext4_bitmap_free_block(ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, 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 uint32_t block_size;
105 ext4_block_group_ref_t *bg_ref;
106 block_t *block;
107
108 block_size = ext4_superblock_get_block_size(fs->superblock);
109
110 blocks_per_group = ext4_superblock_get_blocks_per_group(fs->superblock);
111 block_group = ((block_index - 1) / blocks_per_group);
112 index_in_group = (block_index - 1) % blocks_per_group;
113
114 rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
115 if (rc != EOK) {
116 return rc;
117 }
118
119 bitmap_block = ext4_block_group_get_block_bitmap(bg_ref->block_group);
120
121 rc = block_get(&block, fs->device, bitmap_block, 0);
122 if (rc != EOK) {
123 return rc;
124 }
125
126 ext4_bitmap_free_bit(block->data, index_in_group);
127
128 block->dirty = true;
129 rc = block_put(block);
130 if (rc != EOK) {
131 return rc;
132 }
133
134 uint64_t ino_blocks = ext4_inode_get_blocks_count(fs->superblock, inode_ref->inode);
135 ino_blocks -= block_size / EXT4_INODE_BLOCK_SIZE;
136 ext4_inode_set_blocks_count(fs->superblock, inode_ref->inode, ino_blocks);
137 inode_ref->dirty = true;
138
139 uint32_t free_blocks = ext4_block_group_get_free_blocks_count(bg_ref->block_group);
140 free_blocks++;
141 ext4_block_group_set_free_blocks_count(bg_ref->block_group, free_blocks);
142 bg_ref->dirty = true;
143
144 // TODO change free blocks count in superblock
145
146 rc = ext4_filesystem_put_block_group_ref(bg_ref);
147 if (rc != EOK) {
148 EXT4FS_DBG("error in saving bg_ref \%d", rc);
149 // TODO error
150 return rc;
151 }
152
153// EXT4FS_DBG("block \%u released", block_index);
154
155 return EOK;
156}
157
158int ext4_bitmap_alloc_block(ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, uint32_t *fblock)
159{
160 int rc;
161 uint32_t inodes_per_group, block_group, blocks_per_group;
162 ext4_block_group_ref_t *bg_ref;
163 uint32_t bitmap_block;
164 block_t *block;
165 uint32_t rel_block_idx = 0;
166 uint32_t block_size;
167
168 inodes_per_group = ext4_superblock_get_inodes_per_group(fs->superblock);
169 block_group = (inode_ref->index - 1) / inodes_per_group;
170
171 block_size = ext4_superblock_get_block_size(fs->superblock);
172
173 rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
174 if (rc != EOK) {
175 return rc;
176 }
177
178 bitmap_block = ext4_block_group_get_block_bitmap(bg_ref->block_group);
179
180 rc = block_get(&block, fs->device, bitmap_block, 0);
181 if (rc != EOK) {
182 ext4_filesystem_put_block_group_ref(bg_ref);
183 return rc;
184 }
185
186 rc = ext4_bitmap_find_free_bit_and_set(block->data, &rel_block_idx, block_size);
187 if (rc != EOK) {
188 EXT4FS_DBG("no free block found");
189 // TODO if ENOSPC - try next block groups
190 }
191
192 block->dirty = true;
193
194 rc = block_put(block);
195 if (rc != EOK) {
196 // TODO error
197 EXT4FS_DBG("error in saving bitmap \%d", rc);
198 }
199
200 // TODO decrement superblock free blocks count
201 //uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
202 //sb_free_blocks--;
203 //ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
204
205 uint64_t ino_blocks = ext4_inode_get_blocks_count(fs->superblock, inode_ref->inode);
206 ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
207 ext4_inode_set_blocks_count(fs->superblock, inode_ref->inode, ino_blocks);
208 inode_ref->dirty = true;
209
210 uint32_t bg_free_blocks = ext4_block_group_get_free_blocks_count(bg_ref->block_group);
211 bg_free_blocks--;
212 ext4_block_group_set_free_blocks_count(bg_ref->block_group, bg_free_blocks);
213 bg_ref->dirty = true;
214
215 ext4_filesystem_put_block_group_ref(bg_ref);
216
217 blocks_per_group = ext4_superblock_get_blocks_per_group(fs->superblock);
218
219// EXT4FS_DBG("block \%u allocated", blocks_per_group * block_group + rel_block_idx + 1);
220
221 *fblock = blocks_per_group * block_group + rel_block_idx + 1;
222 return EOK;
223
224}
225
226/**
227 * @}
228 */
Note: See TracBrowser for help on using the repository browser.