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

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

some small improvements of splitting directory index data blocks

  • 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,
245 ext4_inode_ref_t *inode_ref, uint32_t *fblock)
246{
247 int rc;
248 uint32_t allocated_block = 0;
249
250 uint32_t bitmap_block_addr;
251 block_t *bitmap_block;
252 uint32_t rel_block_idx = 0;
253
254 // Find GOAL
255 uint32_t goal = ext4_balloc_find_goal(fs, inode_ref);
256 if (goal == 0) {
257 // TODO
258 EXT4FS_DBG("ERRORR (goal == 0)");
259 return ENOSPC;
260 }
261
262 // Load block group number for goal and relative index
263 uint32_t block_group = ext4_balloc_get_bgid_of_block(fs->superblock, goal);
264 uint32_t index_in_group = ext4_balloc_blockaddr2_index_in_group(fs->superblock, goal);
265
266
267 ext4_block_group_ref_t *bg_ref;
268 rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
269 if (rc != EOK) {
270 EXT4FS_DBG("initial BG ref not loaded");
271 return rc;
272 }
273
274 uint32_t first_in_group =
275 ext4_balloc_get_first_data_block_in_group(fs->superblock,
276 bg_ref->block_group, block_group);
277
278 uint32_t first_in_group_index = ext4_balloc_blockaddr2_index_in_group(
279 fs->superblock, first_in_group);
280
281 if (index_in_group < first_in_group_index) {
282 index_in_group = first_in_group_index;
283 }
284
285 // Load bitmap
286 bitmap_block_addr = ext4_block_group_get_block_bitmap(bg_ref->block_group,
287 fs->superblock);
288
289 rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
290 if (rc != EOK) {
291 ext4_filesystem_put_block_group_ref(bg_ref);
292 EXT4FS_DBG("initial bitmap not loaded");
293 return rc;
294 }
295
296 // Check if goal is free
297 if (ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group)) {
298 ext4_bitmap_set_bit(bitmap_block->data, index_in_group);
299 bitmap_block->dirty = true;
300 rc = block_put(bitmap_block);
301 if (rc != EOK) {
302 // TODO error
303 EXT4FS_DBG("goal check: error in saving initial bitmap \%d", rc);
304 ext4_filesystem_put_block_group_ref(bg_ref);
305 return rc;
306 }
307
308 allocated_block = goal;
309 goto success;
310
311 }
312
313 uint32_t blocks_in_group = ext4_superblock_get_blocks_in_group(fs->superblock, block_group);
314
315 uint32_t end_idx = (index_in_group + 63) & ~63;
316 if (end_idx > blocks_in_group) {
317 end_idx = blocks_in_group;
318 }
319
320 // Try to find free block near to goal
321 for (uint32_t tmp_idx = index_in_group + 1; tmp_idx < end_idx; ++tmp_idx) {
322 if (ext4_bitmap_is_free_bit(bitmap_block->data, tmp_idx)) {
323
324 ext4_bitmap_set_bit(bitmap_block->data, tmp_idx);
325 bitmap_block->dirty = true;
326 rc = block_put(bitmap_block);
327 if (rc != EOK) {
328 // TODO error
329 EXT4FS_DBG("near blocks: error in saving initial bitmap \%d", rc);
330 }
331
332 allocated_block = ext4_balloc_index_in_group2blockaddr(
333 fs->superblock, tmp_idx, block_group);
334
335 goto success;
336 }
337
338 }
339
340 // Find free BYTE in bitmap
341 rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
342 if (rc == EOK) {
343 bitmap_block->dirty = true;
344 rc = block_put(bitmap_block);
345 if (rc != EOK) {
346 // TODO error
347 EXT4FS_DBG("free byte: error in saving initial bitmap \%d", rc);
348 }
349
350 allocated_block = ext4_balloc_index_in_group2blockaddr(
351 fs->superblock, rel_block_idx, block_group);
352
353 goto success;
354 }
355
356 // Find free bit in bitmap
357 rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
358 if (rc == EOK) {
359 bitmap_block->dirty = true;
360 rc = block_put(bitmap_block);
361 if (rc != EOK) {
362 // TODO error
363 EXT4FS_DBG("free bit: error in saving initial bitmap \%d", rc);
364 }
365
366 allocated_block = ext4_balloc_index_in_group2blockaddr(
367 fs->superblock, rel_block_idx, block_group);
368
369 goto success;
370 }
371
372 block_put(bitmap_block);
373 ext4_filesystem_put_block_group_ref(bg_ref);
374
375 // Try other block groups
376 uint32_t block_group_count = ext4_superblock_get_block_group_count(fs->superblock);
377
378 uint32_t bgid = (block_group + 1) % block_group_count;
379 uint32_t count = block_group_count;
380
381 while (count > 0) {
382 rc = ext4_filesystem_get_block_group_ref(fs, bgid, &bg_ref);
383 if (rc != EOK) {
384 EXT4FS_DBG("errrrrrrrrrrr");
385 return rc;
386 }
387
388 // Load bitmap
389 bitmap_block_addr = ext4_block_group_get_block_bitmap(
390 bg_ref->block_group, fs->superblock);
391
392 rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
393 if (rc != EOK) {
394 ext4_filesystem_put_block_group_ref(bg_ref);
395 EXT4FS_DBG("errrrrrrrrrr");
396 return rc;
397 }
398
399 first_in_group = ext4_balloc_get_first_data_block_in_group(
400 fs->superblock, bg_ref->block_group, bgid);
401 index_in_group = ext4_balloc_blockaddr2_index_in_group(fs->superblock,
402 first_in_group);
403 blocks_in_group = ext4_superblock_get_blocks_in_group(fs->superblock, bgid);
404
405 first_in_group_index = ext4_balloc_blockaddr2_index_in_group(
406 fs->superblock, first_in_group);
407
408 if (index_in_group < first_in_group_index) {
409 index_in_group = first_in_group_index;
410 }
411
412
413 rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
414 if (rc == EOK) {
415 bitmap_block->dirty = true;
416 rc = block_put(bitmap_block);
417 if (rc != EOK) {
418 // TODO error
419 EXT4FS_DBG("error in saving bitmap \%d", rc);
420 }
421
422 allocated_block = ext4_balloc_index_in_group2blockaddr(
423 fs->superblock, rel_block_idx, bgid);
424
425 goto success;
426 }
427
428 // Find free bit in bitmap
429 rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data, index_in_group, &rel_block_idx, blocks_in_group);
430 if (rc == EOK) {
431 bitmap_block->dirty = true;
432 rc = block_put(bitmap_block);
433 if (rc != EOK) {
434 // TODO error
435 EXT4FS_DBG("error in saving bitmap \%d", rc);
436 }
437
438 allocated_block = ext4_balloc_index_in_group2blockaddr(
439 fs->superblock, rel_block_idx, bgid);
440
441 goto success;
442 }
443
444
445 // Next group
446 block_put(bitmap_block);
447 ext4_filesystem_put_block_group_ref(bg_ref);
448 bgid = (bgid + 1) % block_group_count;
449 count--;
450 }
451
452 return ENOSPC;
453
454success:
455 ; // Empty command - because of syntax
456
457 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
458
459 // Update superblock free blocks count
460 uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(fs->superblock);
461 sb_free_blocks--;
462 ext4_superblock_set_free_blocks_count(fs->superblock, sb_free_blocks);
463
464 // Update inode blocks (different block size!) count
465
466 uint64_t ino_blocks = ext4_inode_get_blocks_count(fs->superblock, inode_ref->inode);
467 ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
468 ext4_inode_set_blocks_count(fs->superblock, inode_ref->inode, ino_blocks);
469 inode_ref->dirty = true;
470
471 // Update block group free blocks count
472 uint32_t bg_free_blocks = ext4_block_group_get_free_blocks_count(
473 bg_ref->block_group, fs->superblock);
474 bg_free_blocks--;
475 ext4_block_group_set_free_blocks_count(bg_ref->block_group,
476 fs->superblock, bg_free_blocks);
477 bg_ref->dirty = true;
478
479 ext4_filesystem_put_block_group_ref(bg_ref);
480
481 *fblock = allocated_block;
482 return EOK;
483}
484
485
486/**
487 * @}
488 */
Note: See TracBrowser for help on using the repository browser.