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