1 | /*
|
---|
2 | * Copyright (c) 2012 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 | * @file balloc.c
|
---|
34 | * @brief Physical block allocator.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <errno.h>
|
---|
38 | #include <stdint.h>
|
---|
39 | #include "ext4/balloc.h"
|
---|
40 | #include "ext4/bitmap.h"
|
---|
41 | #include "ext4/block_group.h"
|
---|
42 | #include "ext4/filesystem.h"
|
---|
43 | #include "ext4/inode.h"
|
---|
44 | #include "ext4/superblock.h"
|
---|
45 | #include "ext4/types.h"
|
---|
46 |
|
---|
47 | /** Free block.
|
---|
48 | *
|
---|
49 | * @param inode_ref Inode, where the block is allocated
|
---|
50 | * @param block_addr Absolute block address to free
|
---|
51 | *
|
---|
52 | * @return Error code
|
---|
53 | *
|
---|
54 | */
|
---|
55 | errno_t ext4_balloc_free_block(ext4_inode_ref_t *inode_ref, uint32_t block_addr)
|
---|
56 | {
|
---|
57 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
58 | ext4_superblock_t *sb = fs->superblock;
|
---|
59 |
|
---|
60 | /* Compute indexes */
|
---|
61 | uint32_t block_group = ext4_filesystem_blockaddr2group(sb, block_addr);
|
---|
62 | uint32_t index_in_group =
|
---|
63 | ext4_filesystem_blockaddr2_index_in_group(sb, block_addr);
|
---|
64 |
|
---|
65 | /* Load block group reference */
|
---|
66 | ext4_block_group_ref_t *bg_ref;
|
---|
67 | errno_t rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
---|
68 | if (rc != EOK)
|
---|
69 | return rc;
|
---|
70 |
|
---|
71 | /* Load block with bitmap */
|
---|
72 | uint32_t bitmap_block_addr =
|
---|
73 | ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
|
---|
74 | block_t *bitmap_block;
|
---|
75 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
|
---|
76 | if (rc != EOK) {
|
---|
77 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
78 | return rc;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /* Modify bitmap */
|
---|
82 | ext4_bitmap_free_bit(bitmap_block->data, index_in_group);
|
---|
83 | bitmap_block->dirty = true;
|
---|
84 |
|
---|
85 | /* Release block with bitmap */
|
---|
86 | rc = block_put(bitmap_block);
|
---|
87 | if (rc != EOK) {
|
---|
88 | /* Error in saving bitmap */
|
---|
89 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
90 | return rc;
|
---|
91 | }
|
---|
92 |
|
---|
93 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
94 |
|
---|
95 | /* Update superblock free blocks count */
|
---|
96 | uint32_t sb_free_blocks =
|
---|
97 | ext4_superblock_get_free_blocks_count(sb);
|
---|
98 | sb_free_blocks++;
|
---|
99 | ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
|
---|
100 |
|
---|
101 | /* Update inode blocks count */
|
---|
102 | uint64_t ino_blocks =
|
---|
103 | ext4_inode_get_blocks_count(sb, inode_ref->inode);
|
---|
104 | ino_blocks -= block_size / EXT4_INODE_BLOCK_SIZE;
|
---|
105 | ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
|
---|
106 | inode_ref->dirty = true;
|
---|
107 |
|
---|
108 | /* Update block group free blocks count */
|
---|
109 | uint32_t free_blocks =
|
---|
110 | ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
|
---|
111 | free_blocks++;
|
---|
112 | ext4_block_group_set_free_blocks_count(bg_ref->block_group,
|
---|
113 | sb, free_blocks);
|
---|
114 | bg_ref->dirty = true;
|
---|
115 |
|
---|
116 | /* Release block group reference */
|
---|
117 | return ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
118 | }
|
---|
119 |
|
---|
120 | static errno_t ext4_balloc_free_blocks_internal(ext4_inode_ref_t *inode_ref,
|
---|
121 | uint32_t first, uint32_t count)
|
---|
122 | {
|
---|
123 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
124 | ext4_superblock_t *sb = fs->superblock;
|
---|
125 |
|
---|
126 | /* Compute indexes */
|
---|
127 | uint32_t block_group_first = ext4_filesystem_blockaddr2group(sb,
|
---|
128 | first);
|
---|
129 | uint32_t block_group_last = ext4_filesystem_blockaddr2group(sb,
|
---|
130 | first + count - 1);
|
---|
131 |
|
---|
132 | assert(block_group_first == block_group_last);
|
---|
133 |
|
---|
134 | /* Load block group reference */
|
---|
135 | ext4_block_group_ref_t *bg_ref;
|
---|
136 | errno_t rc = ext4_filesystem_get_block_group_ref(fs, block_group_first, &bg_ref);
|
---|
137 | if (rc != EOK)
|
---|
138 | return rc;
|
---|
139 |
|
---|
140 | uint32_t index_in_group_first =
|
---|
141 | ext4_filesystem_blockaddr2_index_in_group(sb, first);
|
---|
142 |
|
---|
143 | /* Load block with bitmap */
|
---|
144 | uint32_t bitmap_block_addr =
|
---|
145 | ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
|
---|
146 |
|
---|
147 | block_t *bitmap_block;
|
---|
148 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
|
---|
149 | if (rc != EOK) {
|
---|
150 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
151 | return rc;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /* Modify bitmap */
|
---|
155 | ext4_bitmap_free_bits(bitmap_block->data, index_in_group_first, count);
|
---|
156 | bitmap_block->dirty = true;
|
---|
157 |
|
---|
158 | /* Release block with bitmap */
|
---|
159 | rc = block_put(bitmap_block);
|
---|
160 | if (rc != EOK) {
|
---|
161 | /* Error in saving bitmap */
|
---|
162 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
163 | return rc;
|
---|
164 | }
|
---|
165 |
|
---|
166 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
167 |
|
---|
168 | /* Update superblock free blocks count */
|
---|
169 | uint32_t sb_free_blocks =
|
---|
170 | ext4_superblock_get_free_blocks_count(sb);
|
---|
171 | sb_free_blocks += count;
|
---|
172 | ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
|
---|
173 |
|
---|
174 | /* Update inode blocks count */
|
---|
175 | uint64_t ino_blocks =
|
---|
176 | ext4_inode_get_blocks_count(sb, inode_ref->inode);
|
---|
177 | ino_blocks -= count * (block_size / EXT4_INODE_BLOCK_SIZE);
|
---|
178 | ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
|
---|
179 | inode_ref->dirty = true;
|
---|
180 |
|
---|
181 | /* Update block group free blocks count */
|
---|
182 | uint32_t free_blocks =
|
---|
183 | ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
|
---|
184 | free_blocks += count;
|
---|
185 | ext4_block_group_set_free_blocks_count(bg_ref->block_group,
|
---|
186 | sb, free_blocks);
|
---|
187 | bg_ref->dirty = true;
|
---|
188 |
|
---|
189 | /* Release block group reference */
|
---|
190 | return ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
191 | }
|
---|
192 |
|
---|
193 | /** Free continuous set of blocks.
|
---|
194 | *
|
---|
195 | * @param inode_ref Inode, where the blocks are allocated
|
---|
196 | * @param first First block to release
|
---|
197 | * @param count Number of blocks to release
|
---|
198 | *
|
---|
199 | */
|
---|
200 | errno_t ext4_balloc_free_blocks(ext4_inode_ref_t *inode_ref,
|
---|
201 | uint32_t first, uint32_t count)
|
---|
202 | {
|
---|
203 | errno_t r;
|
---|
204 | uint32_t gid;
|
---|
205 | uint64_t limit;
|
---|
206 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
207 | ext4_superblock_t *sb = fs->superblock;
|
---|
208 |
|
---|
209 | while (count) {
|
---|
210 | gid = ext4_filesystem_blockaddr2group(sb, first);
|
---|
211 | limit = ext4_filesystem_index_in_group2blockaddr(sb, 0,
|
---|
212 | gid + 1);
|
---|
213 |
|
---|
214 | if ((first + count) >= limit) {
|
---|
215 | /* This extent spans over 2 or more block groups,
|
---|
216 | * we'll break it into smaller parts.
|
---|
217 | */
|
---|
218 | uint32_t s = limit - first;
|
---|
219 |
|
---|
220 | r = ext4_balloc_free_blocks_internal(inode_ref,
|
---|
221 | first, s);
|
---|
222 | if (r != EOK)
|
---|
223 | return r;
|
---|
224 |
|
---|
225 | first = limit;
|
---|
226 | count -= s;
|
---|
227 | } else {
|
---|
228 | return ext4_balloc_free_blocks_internal(inode_ref,
|
---|
229 | first, count);
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | return EOK;
|
---|
234 | }
|
---|
235 |
|
---|
236 | /** Compute first block for data in block group.
|
---|
237 | *
|
---|
238 | * @param sb Pointer to superblock
|
---|
239 | * @param bg Pointer to block group
|
---|
240 | * @param bgid Index of block group
|
---|
241 | *
|
---|
242 | * @return Absolute block index of first block
|
---|
243 | *
|
---|
244 | */
|
---|
245 | uint32_t ext4_balloc_get_first_data_block_in_group(ext4_superblock_t *sb,
|
---|
246 | ext4_block_group_ref_t *bg_ref)
|
---|
247 | {
|
---|
248 | uint32_t r;
|
---|
249 | uint64_t itable = ext4_block_group_get_inode_table_first_block(
|
---|
250 | bg_ref->block_group, sb);
|
---|
251 | uint32_t itable_sz = ext4_filesystem_bg_get_itable_size(sb, bg_ref);
|
---|
252 |
|
---|
253 | if (!ext4_superblock_has_feature_incompatible(sb,
|
---|
254 | EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
|
---|
255 | /* If we are not using FLEX_BG, the first data block
|
---|
256 | * is always after the inode table.
|
---|
257 | */
|
---|
258 | r = itable + itable_sz;
|
---|
259 | return ext4_filesystem_blockaddr2_index_in_group(sb, r);
|
---|
260 | }
|
---|
261 |
|
---|
262 | uint64_t bbmap = ext4_block_group_get_block_bitmap(bg_ref->block_group,
|
---|
263 | sb);
|
---|
264 | uint64_t ibmap = ext4_block_group_get_inode_bitmap(bg_ref->block_group,
|
---|
265 | sb);
|
---|
266 |
|
---|
267 | r = ext4_filesystem_index_in_group2blockaddr(sb, 0, bg_ref->index);
|
---|
268 | r += ext4_filesystem_bg_get_backup_blocks(bg_ref);
|
---|
269 |
|
---|
270 | if (ext4_filesystem_blockaddr2group(sb, bbmap) != bg_ref->index)
|
---|
271 | bbmap = -1; /* Invalid */
|
---|
272 |
|
---|
273 | if (ext4_filesystem_blockaddr2group(sb, ibmap) != bg_ref->index)
|
---|
274 | ibmap = -1;
|
---|
275 |
|
---|
276 | while (1) {
|
---|
277 | if (r == bbmap || r == ibmap)
|
---|
278 | r++;
|
---|
279 | else if (r >= itable && r < (itable + itable_sz))
|
---|
280 | r = itable + itable_sz;
|
---|
281 | else
|
---|
282 | break;
|
---|
283 | }
|
---|
284 |
|
---|
285 | return r;
|
---|
286 | }
|
---|
287 |
|
---|
288 | /** Compute 'goal' for allocation algorithm.
|
---|
289 | *
|
---|
290 | * @param inode_ref Reference to inode, to allocate block for
|
---|
291 | *
|
---|
292 | * @return Goal block number
|
---|
293 | *
|
---|
294 | */
|
---|
295 | static errno_t ext4_balloc_find_goal(ext4_inode_ref_t *inode_ref, uint32_t *goal)
|
---|
296 | {
|
---|
297 | *goal = 0;
|
---|
298 | ext4_superblock_t *sb = inode_ref->fs->superblock;
|
---|
299 |
|
---|
300 | uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
|
---|
301 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
302 | uint32_t inode_block_count = inode_size / block_size;
|
---|
303 |
|
---|
304 | if (inode_size % block_size != 0)
|
---|
305 | inode_block_count++;
|
---|
306 |
|
---|
307 | /* If inode has some blocks, get last block address + 1 */
|
---|
308 | if (inode_block_count > 0) {
|
---|
309 | errno_t rc = ext4_filesystem_get_inode_data_block_index(inode_ref,
|
---|
310 | inode_block_count - 1, goal);
|
---|
311 | if (rc != EOK)
|
---|
312 | return rc;
|
---|
313 |
|
---|
314 | if (goal != 0) {
|
---|
315 | (*goal)++;
|
---|
316 | return EOK;
|
---|
317 | }
|
---|
318 | /* If goal == 0, sparse file -> continue */
|
---|
319 | }
|
---|
320 |
|
---|
321 | /* Identify block group of inode */
|
---|
322 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
---|
323 | uint32_t block_group = (inode_ref->index - 1) / inodes_per_group;
|
---|
324 |
|
---|
325 | /* Load block group reference */
|
---|
326 | ext4_block_group_ref_t *bg_ref;
|
---|
327 | errno_t rc = ext4_filesystem_get_block_group_ref(inode_ref->fs,
|
---|
328 | block_group, &bg_ref);
|
---|
329 | if (rc != EOK)
|
---|
330 | return rc;
|
---|
331 |
|
---|
332 | *goal = ext4_balloc_get_first_data_block_in_group(sb, bg_ref);
|
---|
333 |
|
---|
334 | return ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
335 | }
|
---|
336 |
|
---|
337 | /** Data block allocation algorithm.
|
---|
338 | *
|
---|
339 | * @param inode_ref Inode to allocate block for
|
---|
340 | * @param fblock Allocated block address
|
---|
341 | *
|
---|
342 | * @return Error code
|
---|
343 | *
|
---|
344 | */
|
---|
345 | errno_t ext4_balloc_alloc_block(ext4_inode_ref_t *inode_ref, uint32_t *fblock)
|
---|
346 | {
|
---|
347 | uint32_t allocated_block = 0;
|
---|
348 |
|
---|
349 | uint32_t bitmap_block_addr;
|
---|
350 | block_t *bitmap_block;
|
---|
351 | uint32_t rel_block_idx = 0;
|
---|
352 | uint32_t free_blocks;
|
---|
353 | uint32_t goal;
|
---|
354 | uint32_t block_size;
|
---|
355 |
|
---|
356 | /* Find GOAL */
|
---|
357 | errno_t rc = ext4_balloc_find_goal(inode_ref, &goal);
|
---|
358 | if (rc != EOK)
|
---|
359 | return rc;
|
---|
360 |
|
---|
361 | ext4_superblock_t *sb = inode_ref->fs->superblock;
|
---|
362 |
|
---|
363 | /* Load block group number for goal and relative index */
|
---|
364 | uint32_t block_group = ext4_filesystem_blockaddr2group(sb, goal);
|
---|
365 | uint32_t index_in_group =
|
---|
366 | ext4_filesystem_blockaddr2_index_in_group(sb, goal);
|
---|
367 |
|
---|
368 | /* Load block group reference */
|
---|
369 | ext4_block_group_ref_t *bg_ref;
|
---|
370 | rc = ext4_filesystem_get_block_group_ref(inode_ref->fs,
|
---|
371 | block_group, &bg_ref);
|
---|
372 | if (rc != EOK)
|
---|
373 | return rc;
|
---|
374 |
|
---|
375 | free_blocks =
|
---|
376 | ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
|
---|
377 | if (free_blocks == 0) {
|
---|
378 | /* This group has no free blocks */
|
---|
379 | goto goal_failed;
|
---|
380 | }
|
---|
381 |
|
---|
382 | /* Compute indexes */
|
---|
383 | uint32_t first_in_group =
|
---|
384 | ext4_balloc_get_first_data_block_in_group(sb, bg_ref);
|
---|
385 |
|
---|
386 | uint32_t first_in_group_index =
|
---|
387 | ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
|
---|
388 |
|
---|
389 | if (index_in_group < first_in_group_index)
|
---|
390 | index_in_group = first_in_group_index;
|
---|
391 |
|
---|
392 | /* Load block with bitmap */
|
---|
393 | bitmap_block_addr =
|
---|
394 | ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
|
---|
395 |
|
---|
396 | rc = block_get(&bitmap_block, inode_ref->fs->device,
|
---|
397 | bitmap_block_addr, BLOCK_FLAGS_NONE);
|
---|
398 | if (rc != EOK) {
|
---|
399 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
400 | return rc;
|
---|
401 | }
|
---|
402 |
|
---|
403 | /* Check if goal is free */
|
---|
404 | if (ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group)) {
|
---|
405 | ext4_bitmap_set_bit(bitmap_block->data, index_in_group);
|
---|
406 | bitmap_block->dirty = true;
|
---|
407 | rc = block_put(bitmap_block);
|
---|
408 | if (rc != EOK) {
|
---|
409 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
410 | return rc;
|
---|
411 | }
|
---|
412 |
|
---|
413 | allocated_block =
|
---|
414 | ext4_filesystem_index_in_group2blockaddr(sb, index_in_group,
|
---|
415 | block_group);
|
---|
416 |
|
---|
417 | goto success;
|
---|
418 | }
|
---|
419 |
|
---|
420 | uint32_t blocks_in_group =
|
---|
421 | ext4_superblock_get_blocks_in_group(sb, block_group);
|
---|
422 |
|
---|
423 | uint32_t end_idx = (index_in_group + 63) & ~63;
|
---|
424 | if (end_idx > blocks_in_group)
|
---|
425 | end_idx = blocks_in_group;
|
---|
426 |
|
---|
427 | /* Try to find free block near to goal */
|
---|
428 | for (uint32_t tmp_idx = index_in_group + 1; tmp_idx < end_idx;
|
---|
429 | ++tmp_idx) {
|
---|
430 | if (ext4_bitmap_is_free_bit(bitmap_block->data, tmp_idx)) {
|
---|
431 | ext4_bitmap_set_bit(bitmap_block->data, tmp_idx);
|
---|
432 | bitmap_block->dirty = true;
|
---|
433 | rc = block_put(bitmap_block);
|
---|
434 | if (rc != EOK)
|
---|
435 | return rc;
|
---|
436 |
|
---|
437 | allocated_block =
|
---|
438 | ext4_filesystem_index_in_group2blockaddr(sb, tmp_idx,
|
---|
439 | block_group);
|
---|
440 |
|
---|
441 | goto success;
|
---|
442 | }
|
---|
443 | }
|
---|
444 |
|
---|
445 | /* Find free BYTE in bitmap */
|
---|
446 | rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data,
|
---|
447 | index_in_group, &rel_block_idx, blocks_in_group);
|
---|
448 | if (rc == EOK) {
|
---|
449 | bitmap_block->dirty = true;
|
---|
450 | rc = block_put(bitmap_block);
|
---|
451 | if (rc != EOK)
|
---|
452 | return rc;
|
---|
453 |
|
---|
454 | allocated_block =
|
---|
455 | ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
|
---|
456 | block_group);
|
---|
457 |
|
---|
458 | goto success;
|
---|
459 | }
|
---|
460 |
|
---|
461 | /* Find free bit in bitmap */
|
---|
462 | rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
|
---|
463 | index_in_group, &rel_block_idx, blocks_in_group);
|
---|
464 | if (rc == EOK) {
|
---|
465 | bitmap_block->dirty = true;
|
---|
466 | rc = block_put(bitmap_block);
|
---|
467 | if (rc != EOK)
|
---|
468 | return rc;
|
---|
469 |
|
---|
470 | allocated_block =
|
---|
471 | ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
|
---|
472 | block_group);
|
---|
473 |
|
---|
474 | goto success;
|
---|
475 | }
|
---|
476 |
|
---|
477 | /* No free block found yet */
|
---|
478 | rc = block_put(bitmap_block);
|
---|
479 | if (rc != EOK) {
|
---|
480 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
481 | return rc;
|
---|
482 | }
|
---|
483 |
|
---|
484 | goal_failed:
|
---|
485 |
|
---|
486 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
487 | if (rc != EOK)
|
---|
488 | return rc;
|
---|
489 |
|
---|
490 | /* Try other block groups */
|
---|
491 | uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
|
---|
492 |
|
---|
493 | uint32_t bgid = (block_group + 1) % block_group_count;
|
---|
494 | uint32_t count = block_group_count;
|
---|
495 |
|
---|
496 | while (count > 0) {
|
---|
497 | rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, bgid,
|
---|
498 | &bg_ref);
|
---|
499 | if (rc != EOK)
|
---|
500 | return rc;
|
---|
501 |
|
---|
502 | free_blocks =
|
---|
503 | ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
|
---|
504 | if (free_blocks == 0) {
|
---|
505 | /* This group has no free blocks */
|
---|
506 | goto next_group;
|
---|
507 | }
|
---|
508 |
|
---|
509 | /* Load block with bitmap */
|
---|
510 | bitmap_block_addr =
|
---|
511 | ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
|
---|
512 |
|
---|
513 | rc = block_get(&bitmap_block, inode_ref->fs->device,
|
---|
514 | bitmap_block_addr, 0);
|
---|
515 | if (rc != EOK) {
|
---|
516 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
517 | return rc;
|
---|
518 | }
|
---|
519 |
|
---|
520 | /* Compute indexes */
|
---|
521 | first_in_group =
|
---|
522 | ext4_balloc_get_first_data_block_in_group(sb, bg_ref);
|
---|
523 | index_in_group =
|
---|
524 | ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
|
---|
525 | blocks_in_group = ext4_superblock_get_blocks_in_group(sb, bgid);
|
---|
526 |
|
---|
527 | first_in_group_index =
|
---|
528 | ext4_filesystem_blockaddr2_index_in_group(sb, first_in_group);
|
---|
529 |
|
---|
530 | if (index_in_group < first_in_group_index)
|
---|
531 | index_in_group = first_in_group_index;
|
---|
532 |
|
---|
533 | /* Try to find free byte in bitmap */
|
---|
534 | rc = ext4_bitmap_find_free_byte_and_set_bit(bitmap_block->data,
|
---|
535 | index_in_group, &rel_block_idx, blocks_in_group);
|
---|
536 | if (rc == EOK) {
|
---|
537 | bitmap_block->dirty = true;
|
---|
538 | rc = block_put(bitmap_block);
|
---|
539 | if (rc != EOK) {
|
---|
540 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
541 | return rc;
|
---|
542 | }
|
---|
543 |
|
---|
544 | allocated_block =
|
---|
545 | ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
|
---|
546 | bgid);
|
---|
547 |
|
---|
548 | goto success;
|
---|
549 | }
|
---|
550 |
|
---|
551 | /* Try to find free bit in bitmap */
|
---|
552 | rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
|
---|
553 | index_in_group, &rel_block_idx, blocks_in_group);
|
---|
554 | if (rc == EOK) {
|
---|
555 | bitmap_block->dirty = true;
|
---|
556 | rc = block_put(bitmap_block);
|
---|
557 | if (rc != EOK) {
|
---|
558 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
559 | return rc;
|
---|
560 | }
|
---|
561 |
|
---|
562 | allocated_block =
|
---|
563 | ext4_filesystem_index_in_group2blockaddr(sb, rel_block_idx,
|
---|
564 | bgid);
|
---|
565 |
|
---|
566 | goto success;
|
---|
567 | }
|
---|
568 |
|
---|
569 | rc = block_put(bitmap_block);
|
---|
570 | if (rc != EOK) {
|
---|
571 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
572 | return rc;
|
---|
573 | }
|
---|
574 |
|
---|
575 | next_group:
|
---|
576 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
577 | if (rc != EOK)
|
---|
578 | return rc;
|
---|
579 |
|
---|
580 | /* Goto next group */
|
---|
581 | bgid = (bgid + 1) % block_group_count;
|
---|
582 | count--;
|
---|
583 | }
|
---|
584 |
|
---|
585 | return ENOSPC;
|
---|
586 |
|
---|
587 | success:
|
---|
588 | block_size = ext4_superblock_get_block_size(sb);
|
---|
589 |
|
---|
590 | /* Update superblock free blocks count */
|
---|
591 | uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
|
---|
592 | sb_free_blocks--;
|
---|
593 | ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
|
---|
594 |
|
---|
595 | /* Update inode blocks (different block size!) count */
|
---|
596 | uint64_t ino_blocks =
|
---|
597 | ext4_inode_get_blocks_count(sb, inode_ref->inode);
|
---|
598 | ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
|
---|
599 | ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
|
---|
600 | inode_ref->dirty = true;
|
---|
601 |
|
---|
602 | /* Update block group free blocks count */
|
---|
603 | uint32_t bg_free_blocks =
|
---|
604 | ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
|
---|
605 | bg_free_blocks--;
|
---|
606 | ext4_block_group_set_free_blocks_count(bg_ref->block_group, sb,
|
---|
607 | bg_free_blocks);
|
---|
608 | bg_ref->dirty = true;
|
---|
609 |
|
---|
610 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
611 |
|
---|
612 | *fblock = allocated_block;
|
---|
613 | return rc;
|
---|
614 | }
|
---|
615 |
|
---|
616 | /** Try to allocate concrete block.
|
---|
617 | *
|
---|
618 | * @param inode_ref Inode to allocate block for
|
---|
619 | * @param fblock Block address to allocate
|
---|
620 | * @param free Output value - if target block is free
|
---|
621 | *
|
---|
622 | * @return Error code
|
---|
623 | *
|
---|
624 | */
|
---|
625 | errno_t ext4_balloc_try_alloc_block(ext4_inode_ref_t *inode_ref, uint32_t fblock,
|
---|
626 | bool *free)
|
---|
627 | {
|
---|
628 | errno_t rc;
|
---|
629 |
|
---|
630 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
631 | ext4_superblock_t *sb = fs->superblock;
|
---|
632 |
|
---|
633 | /* Compute indexes */
|
---|
634 | uint32_t block_group = ext4_filesystem_blockaddr2group(sb, fblock);
|
---|
635 | uint32_t index_in_group =
|
---|
636 | ext4_filesystem_blockaddr2_index_in_group(sb, fblock);
|
---|
637 |
|
---|
638 | /* Load block group reference */
|
---|
639 | ext4_block_group_ref_t *bg_ref;
|
---|
640 | rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
---|
641 | if (rc != EOK)
|
---|
642 | return rc;
|
---|
643 |
|
---|
644 | /* Load block with bitmap */
|
---|
645 | uint32_t bitmap_block_addr =
|
---|
646 | ext4_block_group_get_block_bitmap(bg_ref->block_group, sb);
|
---|
647 | block_t *bitmap_block;
|
---|
648 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
|
---|
649 | if (rc != EOK) {
|
---|
650 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
651 | return rc;
|
---|
652 | }
|
---|
653 |
|
---|
654 | /* Check if block is free */
|
---|
655 | *free = ext4_bitmap_is_free_bit(bitmap_block->data, index_in_group);
|
---|
656 |
|
---|
657 | /* Allocate block if possible */
|
---|
658 | if (*free) {
|
---|
659 | ext4_bitmap_set_bit(bitmap_block->data, index_in_group);
|
---|
660 | bitmap_block->dirty = true;
|
---|
661 | }
|
---|
662 |
|
---|
663 | /* Release block with bitmap */
|
---|
664 | rc = block_put(bitmap_block);
|
---|
665 | if (rc != EOK) {
|
---|
666 | /* Error in saving bitmap */
|
---|
667 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
668 | return rc;
|
---|
669 | }
|
---|
670 |
|
---|
671 | /* If block is not free, return */
|
---|
672 | if (!(*free))
|
---|
673 | goto terminate;
|
---|
674 |
|
---|
675 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
676 |
|
---|
677 | /* Update superblock free blocks count */
|
---|
678 | uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
|
---|
679 | sb_free_blocks--;
|
---|
680 | ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
|
---|
681 |
|
---|
682 | /* Update inode blocks count */
|
---|
683 | uint64_t ino_blocks =
|
---|
684 | ext4_inode_get_blocks_count(sb, inode_ref->inode);
|
---|
685 | ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
|
---|
686 | ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
|
---|
687 | inode_ref->dirty = true;
|
---|
688 |
|
---|
689 | /* Update block group free blocks count */
|
---|
690 | uint32_t free_blocks =
|
---|
691 | ext4_block_group_get_free_blocks_count(bg_ref->block_group, sb);
|
---|
692 | free_blocks--;
|
---|
693 | ext4_block_group_set_free_blocks_count(bg_ref->block_group,
|
---|
694 | sb, free_blocks);
|
---|
695 | bg_ref->dirty = true;
|
---|
696 |
|
---|
697 | terminate:
|
---|
698 | return ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
699 | }
|
---|
700 |
|
---|
701 | /**
|
---|
702 | * @}
|
---|
703 | */
|
---|