1 | /*
|
---|
2 | * Copyright (c) 2011 Frantisek Princ
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libext4
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file libext4_filesystem.c
|
---|
35 | * @brief TODO
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <byteorder.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <malloc.h>
|
---|
41 | #include "libext4.h"
|
---|
42 |
|
---|
43 | int ext4_filesystem_init(ext4_filesystem_t *fs, service_id_t service_id)
|
---|
44 | {
|
---|
45 | int rc;
|
---|
46 | ext4_superblock_t *temp_superblock;
|
---|
47 | size_t block_size;
|
---|
48 | uint32_t block_ids_per_block;
|
---|
49 | int i;
|
---|
50 |
|
---|
51 | fs->device = service_id;
|
---|
52 |
|
---|
53 | // TODO what does constant 2048 mean?
|
---|
54 | rc = block_init(EXCHANGE_SERIALIZE, fs->device, 2048);
|
---|
55 | if (rc != EOK) {
|
---|
56 | return rc;
|
---|
57 | }
|
---|
58 |
|
---|
59 | /* Read superblock from device */
|
---|
60 | rc = ext4_superblock_read_direct(fs->device, &temp_superblock);
|
---|
61 | if (rc != EOK) {
|
---|
62 | block_fini(fs->device);
|
---|
63 | return rc;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /* Read block size from superblock and check */
|
---|
67 | block_size = ext4_superblock_get_block_size(temp_superblock);
|
---|
68 | if (block_size > EXT4_MAX_BLOCK_SIZE) {
|
---|
69 | block_fini(fs->device);
|
---|
70 | return ENOTSUP;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /* Initialize block caching */
|
---|
74 | rc = block_cache_init(service_id, block_size, 0, CACHE_MODE_WT);
|
---|
75 | if (rc != EOK) {
|
---|
76 | block_fini(fs->device);
|
---|
77 | return rc;
|
---|
78 | }
|
---|
79 |
|
---|
80 | block_ids_per_block = block_size / sizeof(uint32_t);
|
---|
81 | fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
|
---|
82 | fs->inode_blocks_per_level[0] = 1;
|
---|
83 | for (i = 1; i < 4; i++) {
|
---|
84 | fs->inode_blocks_per_level[i] = fs->inode_blocks_per_level[i-1] *
|
---|
85 | block_ids_per_block;
|
---|
86 | fs->inode_block_limits[i] = fs->inode_block_limits[i-1] +
|
---|
87 | fs->inode_blocks_per_level[i];
|
---|
88 | }
|
---|
89 |
|
---|
90 | /* Return loaded superblock */
|
---|
91 | fs->superblock = temp_superblock;
|
---|
92 |
|
---|
93 | return EOK;
|
---|
94 | }
|
---|
95 |
|
---|
96 | void ext4_filesystem_fini(ext4_filesystem_t *fs)
|
---|
97 | {
|
---|
98 | free(fs->superblock);
|
---|
99 | block_fini(fs->device);
|
---|
100 | }
|
---|
101 |
|
---|
102 | int ext4_filesystem_check_sanity(ext4_filesystem_t *fs)
|
---|
103 | {
|
---|
104 | int rc;
|
---|
105 |
|
---|
106 | rc = ext4_superblock_check_sanity(fs->superblock);
|
---|
107 | if (rc != EOK) {
|
---|
108 | return rc;
|
---|
109 | }
|
---|
110 |
|
---|
111 | return EOK;
|
---|
112 | }
|
---|
113 |
|
---|
114 | int ext4_filesystem_check_features(ext4_filesystem_t *fs, bool *o_read_only)
|
---|
115 | {
|
---|
116 | /* Feature flags are present in rev 1 and later */
|
---|
117 | if (ext4_superblock_get_rev_level(fs->superblock) == 0) {
|
---|
118 | *o_read_only = false;
|
---|
119 | return EOK;
|
---|
120 | }
|
---|
121 |
|
---|
122 | uint32_t incompatible_features;
|
---|
123 | incompatible_features = ext4_superblock_get_features_incompatible(fs->superblock);
|
---|
124 | incompatible_features &= ~EXT4_FEATURE_INCOMPAT_SUPP;
|
---|
125 | if (incompatible_features > 0) {
|
---|
126 | *o_read_only = true;
|
---|
127 | return ENOTSUP;
|
---|
128 | }
|
---|
129 |
|
---|
130 | uint32_t compatible_read_only;
|
---|
131 | compatible_read_only = ext4_superblock_get_features_read_only(fs->superblock);
|
---|
132 | compatible_read_only &= ~EXT4_FEATURE_RO_COMPAT_SUPP;
|
---|
133 | if (compatible_read_only > 0) {
|
---|
134 | *o_read_only = true;
|
---|
135 | }
|
---|
136 |
|
---|
137 | return EOK;
|
---|
138 | }
|
---|
139 |
|
---|
140 | int ext4_filesystem_get_block_group_ref(ext4_filesystem_t *fs, uint32_t bgid,
|
---|
141 | ext4_block_group_ref_t **ref)
|
---|
142 | {
|
---|
143 | int rc;
|
---|
144 | aoff64_t block_id;
|
---|
145 | uint32_t descriptors_per_block;
|
---|
146 | size_t offset;
|
---|
147 | ext4_block_group_ref_t *newref;
|
---|
148 |
|
---|
149 | newref = malloc(sizeof(ext4_block_group_ref_t));
|
---|
150 | if (newref == NULL) {
|
---|
151 | return ENOMEM;
|
---|
152 | }
|
---|
153 |
|
---|
154 | descriptors_per_block = ext4_superblock_get_block_size(fs->superblock)
|
---|
155 | / ext4_superblock_get_desc_size(fs->superblock);
|
---|
156 |
|
---|
157 | /* Block group descriptor table starts at the next block after superblock */
|
---|
158 | block_id = ext4_superblock_get_first_data_block(fs->superblock) + 1;
|
---|
159 |
|
---|
160 | /* Find the block containing the descriptor we are looking for */
|
---|
161 | block_id += bgid / descriptors_per_block;
|
---|
162 | offset = (bgid % descriptors_per_block) * ext4_superblock_get_desc_size(fs->superblock);
|
---|
163 |
|
---|
164 | rc = block_get(&newref->block, fs->device, block_id, 0);
|
---|
165 | if (rc != EOK) {
|
---|
166 | free(newref);
|
---|
167 | return rc;
|
---|
168 | }
|
---|
169 |
|
---|
170 | newref->block_group = newref->block->data + offset;
|
---|
171 | newref->dirty = false;
|
---|
172 |
|
---|
173 | *ref = newref;
|
---|
174 |
|
---|
175 | return EOK;
|
---|
176 | }
|
---|
177 |
|
---|
178 | int ext4_filesystem_put_block_group_ref(ext4_block_group_ref_t *ref)
|
---|
179 | {
|
---|
180 | int rc;
|
---|
181 |
|
---|
182 | if (ref->dirty) {
|
---|
183 | ref->block->dirty = true;
|
---|
184 | }
|
---|
185 |
|
---|
186 | rc = block_put(ref->block);
|
---|
187 | free(ref);
|
---|
188 |
|
---|
189 | return rc;
|
---|
190 | }
|
---|
191 |
|
---|
192 | int ext4_filesystem_get_inode_ref(ext4_filesystem_t *fs, uint32_t index,
|
---|
193 | ext4_inode_ref_t **ref)
|
---|
194 | {
|
---|
195 | int rc;
|
---|
196 | aoff64_t block_id;
|
---|
197 | uint32_t block_group;
|
---|
198 | uint32_t offset_in_group;
|
---|
199 | uint32_t byte_offset_in_group;
|
---|
200 | size_t offset_in_block;
|
---|
201 | uint32_t inodes_per_group;
|
---|
202 | uint32_t inode_table_start;
|
---|
203 | uint16_t inode_size;
|
---|
204 | uint32_t block_size;
|
---|
205 | ext4_block_group_ref_t *bg_ref;
|
---|
206 | ext4_inode_ref_t *newref;
|
---|
207 |
|
---|
208 | newref = malloc(sizeof(ext4_inode_ref_t));
|
---|
209 | if (newref == NULL) {
|
---|
210 | return ENOMEM;
|
---|
211 | }
|
---|
212 |
|
---|
213 | inodes_per_group = ext4_superblock_get_inodes_per_group(fs->superblock);
|
---|
214 |
|
---|
215 | /* inode numbers are 1-based, but it is simpler to work with 0-based
|
---|
216 | * when computing indices
|
---|
217 | */
|
---|
218 | index -= 1;
|
---|
219 | block_group = index / inodes_per_group;
|
---|
220 | offset_in_group = index % inodes_per_group;
|
---|
221 |
|
---|
222 | rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
---|
223 | if (rc != EOK) {
|
---|
224 | free(newref);
|
---|
225 | return rc;
|
---|
226 | }
|
---|
227 |
|
---|
228 | inode_table_start = ext4_block_group_get_inode_table_first_block(
|
---|
229 | bg_ref->block_group);
|
---|
230 |
|
---|
231 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
232 | if (rc != EOK) {
|
---|
233 | free(newref);
|
---|
234 | return rc;
|
---|
235 | }
|
---|
236 |
|
---|
237 | inode_size = ext4_superblock_get_inode_size(fs->superblock);
|
---|
238 | block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
239 |
|
---|
240 | byte_offset_in_group = offset_in_group * inode_size;
|
---|
241 |
|
---|
242 | block_id = inode_table_start + (byte_offset_in_group / block_size);
|
---|
243 | offset_in_block = byte_offset_in_group % block_size;
|
---|
244 |
|
---|
245 | rc = block_get(&newref->block, fs->device, block_id, 0);
|
---|
246 | if (rc != EOK) {
|
---|
247 | free(newref);
|
---|
248 | return rc;
|
---|
249 | }
|
---|
250 |
|
---|
251 | newref->inode = newref->block->data + offset_in_block;
|
---|
252 | /* we decremented index above, but need to store the original value
|
---|
253 | * in the reference
|
---|
254 | */
|
---|
255 | newref->index = index+1;
|
---|
256 | newref->dirty = false;
|
---|
257 |
|
---|
258 | *ref = newref;
|
---|
259 |
|
---|
260 | return EOK;
|
---|
261 | }
|
---|
262 |
|
---|
263 |
|
---|
264 | int ext4_filesystem_put_inode_ref(ext4_inode_ref_t *ref)
|
---|
265 | {
|
---|
266 | int rc;
|
---|
267 |
|
---|
268 | if (ref->dirty) {
|
---|
269 | ref->block->dirty = true;
|
---|
270 | }
|
---|
271 |
|
---|
272 | rc = block_put(ref->block);
|
---|
273 | free(ref);
|
---|
274 |
|
---|
275 | return rc;
|
---|
276 | }
|
---|
277 |
|
---|
278 | int ext4_filesystem_get_inode_data_block_index(ext4_filesystem_t *fs, ext4_inode_t* inode,
|
---|
279 | aoff64_t iblock, uint32_t* fblock)
|
---|
280 | {
|
---|
281 | int rc;
|
---|
282 | uint32_t offset_in_block;
|
---|
283 | uint32_t current_block;
|
---|
284 | aoff64_t block_offset_in_level;
|
---|
285 | int i;
|
---|
286 | int level;
|
---|
287 | block_t *block;
|
---|
288 |
|
---|
289 | /* Handle inode using extents */
|
---|
290 | if (ext4_superblock_has_feature_compatible(fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
|
---|
291 | ext4_inode_has_flag(inode, EXT4_INODE_FLAG_EXTENTS)) {
|
---|
292 | current_block = ext4_inode_get_extent_block(inode, iblock, fs->device);
|
---|
293 | *fblock = current_block;
|
---|
294 | return EOK;
|
---|
295 |
|
---|
296 | }
|
---|
297 |
|
---|
298 | /* Handle simple case when we are dealing with direct reference */
|
---|
299 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
---|
300 | current_block = ext4_inode_get_direct_block(inode, (uint32_t)iblock);
|
---|
301 | *fblock = current_block;
|
---|
302 | return EOK;
|
---|
303 | }
|
---|
304 |
|
---|
305 | /* Determine the indirection level needed to get the desired block */
|
---|
306 | level = -1;
|
---|
307 | for (i = 1; i < 4; i++) {
|
---|
308 | if (iblock < fs->inode_block_limits[i]) {
|
---|
309 | level = i;
|
---|
310 | break;
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | if (level == -1) {
|
---|
315 | return EIO;
|
---|
316 | }
|
---|
317 |
|
---|
318 | /* Compute offsets for the topmost level */
|
---|
319 | block_offset_in_level = iblock - fs->inode_block_limits[level-1];
|
---|
320 | current_block = ext4_inode_get_indirect_block(inode, level-1);
|
---|
321 | offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
322 |
|
---|
323 | if (current_block == 0) {
|
---|
324 | *fblock = 0;
|
---|
325 | return EOK;
|
---|
326 | }
|
---|
327 |
|
---|
328 | /* Navigate through other levels, until we find the block number
|
---|
329 | * or find null reference meaning we are dealing with sparse file
|
---|
330 | */
|
---|
331 | while (level > 0) {
|
---|
332 | rc = block_get(&block, fs->device, current_block, 0);
|
---|
333 | if (rc != EOK) {
|
---|
334 | return rc;
|
---|
335 | }
|
---|
336 |
|
---|
337 | current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
|
---|
338 |
|
---|
339 | rc = block_put(block);
|
---|
340 | if (rc != EOK) {
|
---|
341 | return rc;
|
---|
342 | }
|
---|
343 |
|
---|
344 | if (current_block == 0) {
|
---|
345 | /* This is a sparse file */
|
---|
346 | *fblock = 0;
|
---|
347 | return EOK;
|
---|
348 | }
|
---|
349 |
|
---|
350 | level -= 1;
|
---|
351 |
|
---|
352 | /* If we are on the last level, break here as
|
---|
353 | * there is no next level to visit
|
---|
354 | */
|
---|
355 | if (level == 0) {
|
---|
356 | break;
|
---|
357 | }
|
---|
358 |
|
---|
359 | /* Visit the next level */
|
---|
360 | block_offset_in_level %= fs->inode_blocks_per_level[level];
|
---|
361 | offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
362 | }
|
---|
363 |
|
---|
364 | *fblock = current_block;
|
---|
365 |
|
---|
366 | return EOK;
|
---|
367 | }
|
---|
368 |
|
---|
369 |
|
---|
370 | int ext4_filesystem_set_inode_data_block_index(ext4_filesystem_t *fs,
|
---|
371 | ext4_inode_ref_t *inode_ref, aoff64_t iblock, uint32_t fblock)
|
---|
372 | {
|
---|
373 |
|
---|
374 | int rc;
|
---|
375 | uint32_t offset_in_block;
|
---|
376 | uint32_t current_block, new_block_addr;
|
---|
377 | uint32_t block_size;
|
---|
378 | aoff64_t block_offset_in_level;
|
---|
379 | int i;
|
---|
380 | int level;
|
---|
381 | block_t *block, *new_block;
|
---|
382 |
|
---|
383 | /* Handle inode using extents */
|
---|
384 | if (ext4_superblock_has_feature_compatible(fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
|
---|
385 | ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)) {
|
---|
386 | // TODO
|
---|
387 | return ENOTSUP;
|
---|
388 | }
|
---|
389 |
|
---|
390 | /* Handle simple case when we are dealing with direct reference */
|
---|
391 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
---|
392 | ext4_inode_set_direct_block(inode_ref->inode, (uint32_t)iblock, fblock);
|
---|
393 | inode_ref->dirty = true;
|
---|
394 | return EOK;
|
---|
395 | }
|
---|
396 |
|
---|
397 | /* Determine the indirection level needed to get the desired block */
|
---|
398 | level = -1;
|
---|
399 | for (i = 1; i < 4; i++) {
|
---|
400 | if (iblock < fs->inode_block_limits[i]) {
|
---|
401 | level = i;
|
---|
402 | break;
|
---|
403 | }
|
---|
404 | }
|
---|
405 |
|
---|
406 | if (level == -1) {
|
---|
407 | return EIO;
|
---|
408 | }
|
---|
409 |
|
---|
410 | block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
411 |
|
---|
412 | /* Compute offsets for the topmost level */
|
---|
413 | block_offset_in_level = iblock - fs->inode_block_limits[level-1];
|
---|
414 | current_block = ext4_inode_get_indirect_block(inode_ref->inode, level-1);
|
---|
415 | offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
416 |
|
---|
417 | if (current_block == 0) {
|
---|
418 | rc = ext4_balloc_alloc_block(fs, inode_ref, &new_block_addr);
|
---|
419 | if (rc != EOK) {
|
---|
420 | // TODO error
|
---|
421 | EXT4FS_DBG("error in allocation");
|
---|
422 | }
|
---|
423 | // EXT4FS_DBG("AAA: new addr \%u, level = \%u", new_block_addr, level);
|
---|
424 |
|
---|
425 | ext4_inode_set_indirect_block(inode_ref->inode, level - 1, new_block_addr);
|
---|
426 |
|
---|
427 | inode_ref->dirty = true;
|
---|
428 |
|
---|
429 | rc = block_get(&new_block, fs->device, new_block_addr, BLOCK_FLAGS_NOREAD);
|
---|
430 | if (rc != EOK) {
|
---|
431 | EXT4FS_DBG("block load error");
|
---|
432 | // TODO error
|
---|
433 | }
|
---|
434 |
|
---|
435 | memset(new_block->data, 0, block_size);
|
---|
436 | new_block->dirty = true;
|
---|
437 |
|
---|
438 | rc = block_put(new_block);
|
---|
439 | if (rc != EOK) {
|
---|
440 | EXT4FS_DBG("block put error");
|
---|
441 | }
|
---|
442 |
|
---|
443 | // EXT4FS_DBG("allocated indirect block for level \%u, during setting iblock \%u", level, (uint32_t)iblock);
|
---|
444 |
|
---|
445 | current_block = new_block_addr;
|
---|
446 | }
|
---|
447 |
|
---|
448 | /* Navigate through other levels, until we find the block number
|
---|
449 | * or find null reference meaning we are dealing with sparse file
|
---|
450 | */
|
---|
451 | while (level > 0) {
|
---|
452 |
|
---|
453 | rc = block_get(&block, fs->device, current_block, 0);
|
---|
454 | if (rc != EOK) {
|
---|
455 | return rc;
|
---|
456 | }
|
---|
457 |
|
---|
458 | current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
|
---|
459 |
|
---|
460 | if ((level > 1) && (current_block == 0)) {
|
---|
461 | rc = ext4_balloc_alloc_block(fs, inode_ref, &new_block_addr);
|
---|
462 | if (rc != EOK) {
|
---|
463 | // TODO error
|
---|
464 | EXT4FS_DBG("allocation error");
|
---|
465 | }
|
---|
466 | EXT4FS_DBG("BBB: new addr \%u, offset = \%u, level = \%u", new_block_addr, offset_in_block, level);
|
---|
467 |
|
---|
468 | rc = block_get(&new_block, fs->device, new_block_addr, BLOCK_FLAGS_NOREAD);
|
---|
469 | if (rc != EOK) {
|
---|
470 | // TODO error
|
---|
471 |
|
---|
472 | EXT4FS_DBG("BBB: error block loading");
|
---|
473 |
|
---|
474 | }
|
---|
475 | memset(new_block->data, 0, block_size);
|
---|
476 | new_block->dirty = true;
|
---|
477 |
|
---|
478 | rc = block_put(new_block);
|
---|
479 | if (rc != EOK) {
|
---|
480 | EXT4FS_DBG("BBB: error indirect block saving");
|
---|
481 | }
|
---|
482 |
|
---|
483 | ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(new_block_addr);
|
---|
484 | block->dirty = true;
|
---|
485 | current_block = new_block_addr;
|
---|
486 | }
|
---|
487 |
|
---|
488 | if (level == 1) {
|
---|
489 | ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(fblock);
|
---|
490 | block->dirty = true;
|
---|
491 | }
|
---|
492 |
|
---|
493 | rc = block_put(block);
|
---|
494 | if (rc != EOK) {
|
---|
495 | return rc;
|
---|
496 | }
|
---|
497 |
|
---|
498 | level -= 1;
|
---|
499 |
|
---|
500 | /* If we are on the last level, break here as
|
---|
501 | * there is no next level to visit
|
---|
502 | */
|
---|
503 | if (level == 0) {
|
---|
504 | break;
|
---|
505 | }
|
---|
506 |
|
---|
507 | /* Visit the next level */
|
---|
508 | block_offset_in_level %= fs->inode_blocks_per_level[level];
|
---|
509 | offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
510 | }
|
---|
511 |
|
---|
512 | return EOK;
|
---|
513 | }
|
---|
514 |
|
---|
515 | int ext4_filesystem_release_inode_block(ext4_filesystem_t *fs,
|
---|
516 | ext4_inode_ref_t *inode_ref, uint32_t iblock)
|
---|
517 | {
|
---|
518 | int rc;
|
---|
519 | uint32_t fblock;
|
---|
520 | int i;
|
---|
521 | int level;
|
---|
522 | aoff64_t block_offset_in_level;
|
---|
523 | uint32_t current_block;
|
---|
524 | uint32_t offset_in_block;
|
---|
525 | block_t *block;
|
---|
526 | ext4_inode_t *inode = inode_ref->inode;
|
---|
527 |
|
---|
528 | /* TODO handle extents */
|
---|
529 |
|
---|
530 |
|
---|
531 | /* Handle simple case when we are dealing with direct reference */
|
---|
532 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
---|
533 | fblock = ext4_inode_get_direct_block(inode, iblock);
|
---|
534 | // Sparse file
|
---|
535 | if (fblock == 0) {
|
---|
536 | return EOK;
|
---|
537 | }
|
---|
538 |
|
---|
539 | ext4_inode_set_direct_block(inode, iblock, 0);
|
---|
540 | return ext4_balloc_free_block(fs, inode_ref, fblock);
|
---|
541 | }
|
---|
542 |
|
---|
543 |
|
---|
544 | /* Determine the indirection level needed to get the desired block */
|
---|
545 | level = -1;
|
---|
546 | for (i = 1; i < 4; i++) {
|
---|
547 | if (iblock < fs->inode_block_limits[i]) {
|
---|
548 | level = i;
|
---|
549 | break;
|
---|
550 | }
|
---|
551 | }
|
---|
552 |
|
---|
553 | if (level == -1) {
|
---|
554 | return EIO;
|
---|
555 | }
|
---|
556 |
|
---|
557 | /* Compute offsets for the topmost level */
|
---|
558 | block_offset_in_level = iblock - fs->inode_block_limits[level-1];
|
---|
559 | current_block = ext4_inode_get_indirect_block(inode, level-1);
|
---|
560 | offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
561 |
|
---|
562 | /* Navigate through other levels, until we find the block number
|
---|
563 | * or find null reference meaning we are dealing with sparse file
|
---|
564 | */
|
---|
565 | while (level > 0) {
|
---|
566 | rc = block_get(&block, fs->device, current_block, 0);
|
---|
567 | if (rc != EOK) {
|
---|
568 | return rc;
|
---|
569 | }
|
---|
570 |
|
---|
571 | current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
|
---|
572 |
|
---|
573 | // Set zero
|
---|
574 | if (level == 1) {
|
---|
575 | ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(0);
|
---|
576 | block->dirty = true;
|
---|
577 | }
|
---|
578 |
|
---|
579 | rc = block_put(block);
|
---|
580 | if (rc != EOK) {
|
---|
581 | return rc;
|
---|
582 | }
|
---|
583 |
|
---|
584 | level -= 1;
|
---|
585 |
|
---|
586 | /* If we are on the last level, break here as
|
---|
587 | * there is no next level to visit
|
---|
588 | */
|
---|
589 | if (level == 0) {
|
---|
590 | break;
|
---|
591 | }
|
---|
592 |
|
---|
593 | /* Visit the next level */
|
---|
594 | block_offset_in_level %= fs->inode_blocks_per_level[level];
|
---|
595 | offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
596 | }
|
---|
597 |
|
---|
598 | fblock = current_block;
|
---|
599 |
|
---|
600 | if (fblock == 0) {
|
---|
601 | return EOK;
|
---|
602 | }
|
---|
603 |
|
---|
604 | return ext4_balloc_free_block(fs, inode_ref, fblock);
|
---|
605 |
|
---|
606 | }
|
---|
607 |
|
---|
608 | /**
|
---|
609 | * @}
|
---|
610 | */
|
---|