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