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