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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ebcaff4 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
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_balloc.c
35 * @brief Block allocator.
36 */
37
38#include <errno.h>
39#include <sys/types.h>
40#include "libext4.h"
41
42static uint32_t ext4_balloc_blockaddr2_index_in_group(ext4_superblock_t *sb,
43 uint32_t block_addr)
44{
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);
47
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);
74
75 // First block == 0 or 1
76 if (first_block == 0) {
77 return block_addr / blocks_per_group;
78 } else {
79 return (block_addr - 1) / blocks_per_group;
80 }
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;
87
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;
92 rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
93 if (rc != EOK) {
94 EXT4FS_DBG("error in loading bg_ref \%d", rc);
95 return rc;
96 }
97
98 uint32_t bitmap_block_addr = ext4_block_group_get_block_bitmap(
99 bg_ref->block_group, fs->superblock);
100 block_t *bitmap_block;
101 rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
102 if (rc != EOK) {
103 EXT4FS_DBG("error in loading bitmap \%d", rc);
104 return rc;
105 }
106
107 ext4_bitmap_free_bit(bitmap_block->data, index_in_group);
108 bitmap_block->dirty = true;
109
110 rc = block_put(bitmap_block);
111 if (rc != EOK) {
112 // Error in saving bitmap
113 ext4_filesystem_put_block_group_ref(bg_ref);
114 EXT4FS_DBG("error in saving bitmap \%d", rc);
115 return rc;
116 }
117
118 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
119
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
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
131 // Update block group free blocks count
132 uint32_t free_blocks = ext4_block_group_get_free_blocks_count(
133 bg_ref->block_group, fs->superblock);
134 free_blocks++;
135 ext4_block_group_set_free_blocks_count(bg_ref->block_group,
136 fs->superblock, free_blocks);
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
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);
153 uint32_t inode_table_first_block = ext4_block_group_get_inode_table_first_block(
154 bg, sb);
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
179
180static uint32_t ext4_balloc_find_goal(ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref)
181{
182 int rc;
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 !!!
199
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
215 uint32_t block_group_count = ext4_superblock_get_block_group_count(fs->superblock);
216 uint32_t inode_table_first_block = ext4_block_group_get_inode_table_first_block(
217 bg_ref->block_group, fs->superblock);
218 uint16_t inode_table_item_size = ext4_superblock_get_inode_size(fs->superblock);
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
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
244int ext4_balloc_alloc_block(ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, uint32_t *fblock)
245{
246 int rc;
247 uint32_t allocated_block = 0;
248
249 uint32_t bitmap_block_addr;
250 block_t *bitmap_block;
251 uint32_t rel_block_idx = 0;
252
253 // Find GOAL
254 uint32_t goal = ext4_balloc_find_goal(fs, inode_ref);
255 if (goal == 0) {
256 // TODO
257 EXT4FS_DBG("ERRORR (goal == 0)");
258 return ENOSPC;
259 }
260
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);
264
265
266 ext4_block_group_ref_t *bg_ref;
267 rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
268 if (rc != EOK) {
269 EXT4FS_DBG("initial BG ref not loaded");
270 return rc;
271 }
272
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
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;
282 }
283
284 // Load bitmap
285 bitmap_block_addr = ext4_block_group_get_block_bitmap(bg_ref->block_group,
286 fs->superblock);
287
288 rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
289 if (rc != EOK) {
290 ext4_filesystem_put_block_group_ref(bg_ref);
291 EXT4FS_DBG("initial bitmap not loaded");
292 return rc;
293 }
294
295 // Check if goal is free
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);
300 if (rc != EOK) {
301 // TODO error
302 EXT4FS_DBG("goal check: error in saving initial bitmap \%d", rc);
303 ext4_filesystem_put_block_group_ref(bg_ref);
304 return rc;
305 }
306
307 allocated_block = goal;
308 goto success;
309
310 }
311
312 uint32_t blocks_in_group = ext4_superblock_get_blocks_in_group(fs->superblock, block_group);
313
314 uint32_t end_idx = (index_in_group + 63) & ~63;
315 if (end_idx > blocks_in_group) {
316 end_idx = blocks_in_group;
317 }
318
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) {
321 if (ext4_bitmap_is_free_bit(bitmap_block->data, tmp_idx)) {
322
323 ext4_bitmap_set_bit(bitmap_block->data, tmp_idx);
324 bitmap_block->dirty = true;
325 rc = block_put(bitmap_block);
326 if (rc != EOK) {
327 // TODO error
328 EXT4FS_DBG("near blocks: error in saving initial bitmap \%d", rc);
329 }
330
331 allocated_block = ext4_balloc_index_in_group2blockaddr(
332 fs->superblock, tmp_idx, block_group);
333
334 goto success;
335 }
336
337 }
338
339 // Find free BYTE in bitmap
340 rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
341 if (rc == EOK) {
342 bitmap_block->dirty = true;
343 rc = block_put(bitmap_block);
344 if (rc != EOK) {
345 // TODO error
346 EXT4FS_DBG("free byte: error in saving initial bitmap \%d", rc);
347 }
348
349 allocated_block = ext4_balloc_index_in_group2blockaddr(
350 fs->superblock, rel_block_idx, block_group);
351
352 goto success;
353 }
354
355 // Find free bit in bitmap
356 rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
357 if (rc == EOK) {
358 bitmap_block->dirty = true;
359 rc = block_put(bitmap_block);
360 if (rc != EOK) {
361 // TODO error
362 EXT4FS_DBG("free bit: error in saving initial bitmap \%d", rc);
363 }
364
365 allocated_block = ext4_balloc_index_in_group2blockaddr(
366 fs->superblock, rel_block_idx, block_group);
367
368 goto success;
369 }
370
371 block_put(bitmap_block);
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
388 bitmap_block_addr = ext4_block_group_get_block_bitmap(
389 bg_ref->block_group, fs->superblock);
390
391 rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
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
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;
409 }
410
411
412 rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
413 if (rc == EOK) {
414 bitmap_block->dirty = true;
415 rc = block_put(bitmap_block);
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
428 rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
429 if (rc == EOK) {
430 bitmap_block->dirty = true;
431 rc = block_put(bitmap_block);
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
443
444 // Next group
445 block_put(bitmap_block);
446 ext4_filesystem_put_block_group_ref(bg_ref);
447 bgid = (bgid + 1) % block_group_count;
448 count--;
449 }
450
451 return ENOSPC;
452
453success:
454 ; // Empty command - because of syntax
455
456 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
457
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);
462
463 // Update inode blocks (different block size!) count
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
469 // Update block group free blocks count
470 uint32_t bg_free_blocks = ext4_block_group_get_free_blocks_count(
471 bg_ref->block_group, fs->superblock);
472 bg_free_blocks--;
473 ext4_block_group_set_free_blocks_count(bg_ref->block_group,
474 fs->superblock, bg_free_blocks);
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.