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

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

superblock update after block (de)allocation

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