source: mainline/uspace/lib/ext4/libext4_balloc.c@ ea75ceb

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

added many getters and setters

  • Property mode set to 100644
File size: 14.2 KB
RevLine 
[2674db6]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
[b12ca16]33/**
34 * @file libext4_balloc.c
35 * @brief Block allocator.
36 */
37
[2674db6]38#include <errno.h>
39#include <sys/types.h>
40#include "libext4.h"
41
[b12ca16]42static uint32_t ext4_balloc_blockaddr2_index_in_group(ext4_superblock_t *sb,
43 uint32_t block_addr)
[2674db6]44{
[b12ca16]45 uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
46 uint32_t first_block = ext4_superblock_get_first_data_block(sb);
[2674db6]47
[b12ca16]48 // First block == 0 or 1
49 if (first_block == 0) {
50 return block_addr % blocks_per_group;
51 } else {
52 return (block_addr - 1) % blocks_per_group;
53 }
54}
55
56static uint32_t ext4_balloc_index_in_group2blockaddr(ext4_superblock_t *sb,
57 uint32_t index, uint32_t bgid)
58{
59 uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
60
61 if (ext4_superblock_get_first_data_block(sb) == 0) {
62 return bgid * blocks_per_group + index;
63 } else {
64 return bgid * blocks_per_group + index + 1;
65 }
66
67}
68
69static uint32_t ext4_balloc_get_bgid_of_block(ext4_superblock_t *sb,
70 uint32_t block_addr)
71{
72 uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
73 uint32_t first_block = ext4_superblock_get_first_data_block(sb);
[2674db6]74
75 // First block == 0 or 1
76 if (first_block == 0) {
[b12ca16]77 return block_addr / blocks_per_group;
[2674db6]78 } else {
[b12ca16]79 return (block_addr - 1) / blocks_per_group;
[2674db6]80 }
[b12ca16]81}
82
83
84int ext4_balloc_free_block(ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, uint32_t block_addr)
85{
86 int rc;
[2674db6]87
[b12ca16]88 uint32_t block_group = ext4_balloc_get_bgid_of_block(fs->superblock, block_addr);
89 uint32_t index_in_group = ext4_balloc_blockaddr2_index_in_group(fs->superblock, block_addr);
90
91 ext4_block_group_ref_t *bg_ref;
[2674db6]92 rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
93 if (rc != EOK) {
[b12ca16]94 EXT4FS_DBG("error in loading bg_ref \%d", rc);
[2674db6]95 return rc;
96 }
97
[fe27eb4]98 uint32_t bitmap_block_addr = ext4_block_group_get_block_bitmap(
99 bg_ref->block_group, fs->superblock);
[b12ca16]100 block_t *bitmap_block;
101 rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
[2674db6]102 if (rc != EOK) {
[b12ca16]103 EXT4FS_DBG("error in loading bitmap \%d", rc);
[2674db6]104 return rc;
105 }
106
[b12ca16]107 ext4_bitmap_free_bit(bitmap_block->data, index_in_group);
108 bitmap_block->dirty = true;
[2674db6]109
[b12ca16]110 rc = block_put(bitmap_block);
[2674db6]111 if (rc != EOK) {
[b12ca16]112 // Error in saving bitmap
113 ext4_filesystem_put_block_group_ref(bg_ref);
114 EXT4FS_DBG("error in saving bitmap \%d", rc);
[2674db6]115 return rc;
116 }
117
[b12ca16]118 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
119
[ae3d4f8]120 // Update superblock free blocks count
121 uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(fs->superblock);
122 sb_free_blocks--;
123 ext4_superblock_set_free_blocks_count(fs->superblock, sb_free_blocks);
124
125 // Update inode blocks count
[2674db6]126 uint64_t ino_blocks = ext4_inode_get_blocks_count(fs->superblock, inode_ref->inode);
127 ino_blocks -= block_size / EXT4_INODE_BLOCK_SIZE;
128 ext4_inode_set_blocks_count(fs->superblock, inode_ref->inode, ino_blocks);
129 inode_ref->dirty = true;
130
[ae3d4f8]131 // Update block group free blocks count
[fe27eb4]132 uint32_t free_blocks = ext4_block_group_get_free_blocks_count(
133 bg_ref->block_group, fs->superblock);
[2674db6]134 free_blocks++;
[fe27eb4]135 ext4_block_group_set_free_blocks_count(bg_ref->block_group,
136 fs->superblock, free_blocks);
[2674db6]137 bg_ref->dirty = true;
138
139 rc = ext4_filesystem_put_block_group_ref(bg_ref);
140 if (rc != EOK) {
141 EXT4FS_DBG("error in saving bg_ref \%d", rc);
142 // TODO error
143 return rc;
144 }
145
146 return EOK;
147}
148
[b12ca16]149static uint32_t ext4_balloc_get_first_data_block_in_group(
150 ext4_superblock_t *sb, ext4_block_group_t *bg, uint32_t bgid)
151{
152 uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
[fe27eb4]153 uint32_t inode_table_first_block = ext4_block_group_get_inode_table_first_block(
154 bg, sb);
[b12ca16]155 uint16_t inode_table_item_size = ext4_superblock_get_inode_size(sb);
156 uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
157 uint32_t block_size = ext4_superblock_get_block_size(sb);
158 uint32_t inode_table_bytes;
159
160 if (bgid < block_group_count - 1) {
161 inode_table_bytes = inodes_per_group * inode_table_item_size;
162 } else {
163 // last block group could be smaller
164 uint32_t inodes_count_total = ext4_superblock_get_inodes_count(sb);
165 inode_table_bytes =
166 (inodes_count_total - ((block_group_count - 1) * inodes_per_group))
167 * inode_table_item_size;
168 }
169
170 uint32_t inode_table_blocks = inode_table_bytes / block_size;
171
172 if (inode_table_bytes % block_size) {
173 inode_table_blocks++;
174 }
175
176 return inode_table_first_block + inode_table_blocks;
177}
178
[2674db6]179
[b12ca16]180static uint32_t ext4_balloc_find_goal(ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref)
[2674db6]181{
[b12ca16]182 int rc;
[2674db6]183 uint32_t goal = 0;
184
185 uint64_t inode_size = ext4_inode_get_size(fs->superblock, inode_ref->inode);
186 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
187 uint32_t inode_block_count = inode_size / block_size;
188
189 if (inode_size % block_size != 0) {
190 inode_block_count++;
191 }
192
193 if (inode_block_count > 0) {
194 // TODO check retval
195 ext4_filesystem_get_inode_data_block_index(fs, inode_ref->inode, inode_block_count - 1, &goal);
196
197 // TODO
198 // If goal == 0 -> SPARSE file !!!
[b12ca16]199
[2674db6]200 goal++;
201 return goal;
202 }
203
204 // Identify block group of inode
205 uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(fs->superblock);
206 uint32_t block_group = (inode_ref->index - 1) / inodes_per_group;
207 block_size = ext4_superblock_get_block_size(fs->superblock);
208
209 ext4_block_group_ref_t *bg_ref;
210 rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
211 if (rc != EOK) {
212 return 0;
213 }
214
[b12ca16]215 uint32_t block_group_count = ext4_superblock_get_block_group_count(fs->superblock);
[fe27eb4]216 uint32_t inode_table_first_block = ext4_block_group_get_inode_table_first_block(
217 bg_ref->block_group, fs->superblock);
[2674db6]218 uint16_t inode_table_item_size = ext4_superblock_get_inode_size(fs->superblock);
[b12ca16]219 uint32_t inode_table_bytes;
220
221 if (block_group < block_group_count - 1) {
222 inode_table_bytes = inodes_per_group * inode_table_item_size;
223 } else {
224 // last block group could be smaller
225 uint32_t inodes_count_total = ext4_superblock_get_inodes_count(fs->superblock);
226 inode_table_bytes =
227 (inodes_count_total - ((block_group_count - 1) * inodes_per_group))
228 * inode_table_item_size;
229 }
230
[2674db6]231 uint32_t inode_table_blocks = inode_table_bytes / block_size;
232
233 if (inode_table_bytes % block_size) {
234 inode_table_blocks++;
235 }
236
237 goal = inode_table_first_block + inode_table_blocks;
238
239 ext4_filesystem_put_block_group_ref(bg_ref);
240
241 return goal;
242}
243
[b12ca16]244int ext4_balloc_alloc_block(ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, uint32_t *fblock)
[2674db6]245{
246 int rc;
[b12ca16]247 uint32_t allocated_block = 0;
248
[528e5b3]249 uint32_t bitmap_block_addr;
250 block_t *bitmap_block;
[2674db6]251 uint32_t rel_block_idx = 0;
252
[b12ca16]253 // Find GOAL
254 uint32_t goal = ext4_balloc_find_goal(fs, inode_ref);
[2674db6]255 if (goal == 0) {
256 // TODO
257 EXT4FS_DBG("ERRORR (goal == 0)");
[b12ca16]258 return ENOSPC;
[2674db6]259 }
260
[b12ca16]261 // Load block group number for goal and relative index
262 uint32_t block_group = ext4_balloc_get_bgid_of_block(fs->superblock, goal);
263 uint32_t index_in_group = ext4_balloc_blockaddr2_index_in_group(fs->superblock, goal);
[2674db6]264
265
[b12ca16]266 ext4_block_group_ref_t *bg_ref;
[2674db6]267 rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
268 if (rc != EOK) {
[b12ca16]269 EXT4FS_DBG("initial BG ref not loaded");
[2674db6]270 return rc;
271 }
272
[b12ca16]273 uint32_t first_in_group =
274 ext4_balloc_get_first_data_block_in_group(fs->superblock,
275 bg_ref->block_group, block_group);
276
[528e5b3]277 uint32_t first_in_group_index = ext4_balloc_blockaddr2_index_in_group(
278 fs->superblock, first_in_group);
279
280 if (index_in_group < first_in_group_index) {
281 index_in_group = first_in_group_index;
[b12ca16]282 }
283
[2674db6]284 // Load bitmap
[fe27eb4]285 bitmap_block_addr = ext4_block_group_get_block_bitmap(bg_ref->block_group,
286 fs->superblock);
[2674db6]287
[528e5b3]288 rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
[2674db6]289 if (rc != EOK) {
290 ext4_filesystem_put_block_group_ref(bg_ref);
[b12ca16]291 EXT4FS_DBG("initial bitmap not loaded");
[2674db6]292 return rc;
293 }
294
[b12ca16]295 // Check if goal is free
[528e5b3]296 if (ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group)) {
297 ext4_bitmap_set_bit(bitmap_block->data, index_in_group);
298 bitmap_block->dirty = true;
299 rc = block_put(bitmap_block);
[2674db6]300 if (rc != EOK) {
301 // TODO error
[b12ca16]302 EXT4FS_DBG("goal check: error in saving initial bitmap \%d", rc);
303 ext4_filesystem_put_block_group_ref(bg_ref);
304 return rc;
[2674db6]305 }
306
307 allocated_block = goal;
[b12ca16]308 goto success;
[2674db6]309
310 }
311
[b12ca16]312 uint32_t blocks_in_group = ext4_superblock_get_blocks_in_group(fs->superblock, block_group);
[2674db6]313
[b12ca16]314 uint32_t end_idx = (index_in_group + 63) & ~63;
315 if (end_idx > blocks_in_group) {
316 end_idx = blocks_in_group;
317 }
[2674db6]318
[b12ca16]319 // Try to find free block near to goal
320 for (uint32_t tmp_idx = index_in_group + 1; tmp_idx < end_idx; ++tmp_idx) {
[528e5b3]321 if (ext4_bitmap_is_free_bit(bitmap_block->data, tmp_idx)) {
[2674db6]322
[528e5b3]323 ext4_bitmap_set_bit(bitmap_block->data, tmp_idx);
324 bitmap_block->dirty = true;
325 rc = block_put(bitmap_block);
[2674db6]326 if (rc != EOK) {
327 // TODO error
[b12ca16]328 EXT4FS_DBG("near blocks: error in saving initial bitmap \%d", rc);
[2674db6]329 }
330
[b12ca16]331 allocated_block = ext4_balloc_index_in_group2blockaddr(
332 fs->superblock, tmp_idx, block_group);
333
334 goto success;
[2674db6]335 }
336
[b12ca16]337 }
[2674db6]338
339 // Find free BYTE in bitmap
[528e5b3]340 rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
[2674db6]341 if (rc == EOK) {
[528e5b3]342 bitmap_block->dirty = true;
343 rc = block_put(bitmap_block);
[2674db6]344 if (rc != EOK) {
345 // TODO error
[b12ca16]346 EXT4FS_DBG("free byte: error in saving initial bitmap \%d", rc);
[2674db6]347 }
348
[b12ca16]349 allocated_block = ext4_balloc_index_in_group2blockaddr(
350 fs->superblock, rel_block_idx, block_group);
[2674db6]351
[b12ca16]352 goto success;
353 }
[2674db6]354
355 // Find free bit in bitmap
[528e5b3]356 rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
[2674db6]357 if (rc == EOK) {
[528e5b3]358 bitmap_block->dirty = true;
359 rc = block_put(bitmap_block);
[2674db6]360 if (rc != EOK) {
361 // TODO error
[b12ca16]362 EXT4FS_DBG("free bit: error in saving initial bitmap \%d", rc);
[2674db6]363 }
364
[b12ca16]365 allocated_block = ext4_balloc_index_in_group2blockaddr(
366 fs->superblock, rel_block_idx, block_group);
[2674db6]367
[b12ca16]368 goto success;
[2674db6]369 }
370
[528e5b3]371 block_put(bitmap_block);
[b12ca16]372 ext4_filesystem_put_block_group_ref(bg_ref);
373
374 // Try other block groups
375 uint32_t block_group_count = ext4_superblock_get_block_group_count(fs->superblock);
376
377 uint32_t bgid = (block_group + 1) % block_group_count;
378 uint32_t count = block_group_count;
379
380 while (count > 0) {
381 rc = ext4_filesystem_get_block_group_ref(fs, bgid, &bg_ref);
382 if (rc != EOK) {
383 EXT4FS_DBG("errrrrrrrrrrr");
384 return rc;
385 }
386
387 // Load bitmap
[fe27eb4]388 bitmap_block_addr = ext4_block_group_get_block_bitmap(
389 bg_ref->block_group, fs->superblock);
[b12ca16]390
[528e5b3]391 rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
[b12ca16]392 if (rc != EOK) {
393 ext4_filesystem_put_block_group_ref(bg_ref);
394 EXT4FS_DBG("errrrrrrrrrr");
395 return rc;
396 }
397
398 first_in_group = ext4_balloc_get_first_data_block_in_group(
399 fs->superblock, bg_ref->block_group, bgid);
400 index_in_group = ext4_balloc_blockaddr2_index_in_group(fs->superblock,
401 first_in_group);
402 blocks_in_group = ext4_superblock_get_blocks_in_group(fs->superblock, bgid);
403
[528e5b3]404 first_in_group_index = ext4_balloc_blockaddr2_index_in_group(
405 fs->superblock, first_in_group);
406
407 if (index_in_group < first_in_group_index) {
408 index_in_group = first_in_group_index;
[b12ca16]409 }
410
[528e5b3]411
412 rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
[b12ca16]413 if (rc == EOK) {
[528e5b3]414 bitmap_block->dirty = true;
415 rc = block_put(bitmap_block);
[b12ca16]416 if (rc != EOK) {
417 // TODO error
418 EXT4FS_DBG("error in saving bitmap \%d", rc);
419 }
420
421 allocated_block = ext4_balloc_index_in_group2blockaddr(
422 fs->superblock, rel_block_idx, bgid);
423
424 goto success;
425 }
426
427 // Find free bit in bitmap
[528e5b3]428 rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
[b12ca16]429 if (rc == EOK) {
[528e5b3]430 bitmap_block->dirty = true;
431 rc = block_put(bitmap_block);
[b12ca16]432 if (rc != EOK) {
433 // TODO error
434 EXT4FS_DBG("error in saving bitmap \%d", rc);
435 }
436
437 allocated_block = ext4_balloc_index_in_group2blockaddr(
438 fs->superblock, rel_block_idx, bgid);
439
440 goto success;
441 }
442
[2674db6]443
[b12ca16]444 // Next group
[528e5b3]445 block_put(bitmap_block);
[b12ca16]446 ext4_filesystem_put_block_group_ref(bg_ref);
447 bgid = (bgid + 1) % block_group_count;
448 count--;
449 }
450
[2674db6]451 return ENOSPC;
452
[b12ca16]453success:
[528e5b3]454 ; // Empty command - because of syntax
455
[b12ca16]456 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
457
[ae3d4f8]458 // Update superblock free blocks count
459 uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(fs->superblock);
460 sb_free_blocks--;
461 ext4_superblock_set_free_blocks_count(fs->superblock, sb_free_blocks);
[2674db6]462
[ae3d4f8]463 // Update inode blocks (different block size!) count
[2674db6]464 uint64_t ino_blocks = ext4_inode_get_blocks_count(fs->superblock, inode_ref->inode);
465 ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
466 ext4_inode_set_blocks_count(fs->superblock, inode_ref->inode, ino_blocks);
467 inode_ref->dirty = true;
468
[ae3d4f8]469 // Update block group free blocks count
[fe27eb4]470 uint32_t bg_free_blocks = ext4_block_group_get_free_blocks_count(
471 bg_ref->block_group, fs->superblock);
[2674db6]472 bg_free_blocks--;
[fe27eb4]473 ext4_block_group_set_free_blocks_count(bg_ref->block_group,
474 fs->superblock, bg_free_blocks);
[2674db6]475 bg_ref->dirty = true;
476
477 ext4_filesystem_put_block_group_ref(bg_ref);
478
479 *fblock = allocated_block;
480 return EOK;
481}
482
483
484/**
485 * @}
486 */
Note: See TracBrowser for help on using the repository browser.