source: mainline/uspace/lib/ext4/libext4_filesystem.c@ 2966122

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2966122 was 6088193, checked in by Frantisek Princ <frantisek.princ@…>, 14 years ago

bugfixes in indirect block writing

  • Property mode set to 100644
File size: 14.6 KB
Line 
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
43int 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
96void ext4_filesystem_fini(ext4_filesystem_t *fs)
97{
98 free(fs->superblock);
99 block_fini(fs->device);
100}
101
102int 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
114int 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
140int 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
178int 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
192int 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
264int 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
278int 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
370int 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
391 /* Handle simple case when we are dealing with direct reference */
392 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
393 ext4_inode_set_direct_block(inode_ref->inode, (uint32_t)iblock, fblock);
394 inode_ref->dirty = true;
395 return EOK;
396 }
397
398 /* Determine the indirection level needed to get the desired block */
399 level = -1;
400 for (i = 1; i < 4; i++) {
401 if (iblock < fs->inode_block_limits[i]) {
402 level = i;
403 break;
404 }
405 }
406
407 if (level == -1) {
408 return EIO;
409 }
410
411 block_size = ext4_superblock_get_block_size(fs->superblock);
412
413 /* Compute offsets for the topmost level */
414 block_offset_in_level = iblock - fs->inode_block_limits[level-1];
415 current_block = ext4_inode_get_indirect_block(inode_ref->inode, level-1);
416 offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
417
418 if (current_block == 0) {
419 rc = ext4_bitmap_alloc_block(fs, inode_ref, &new_block_addr);
420 if (rc != EOK) {
421 // TODO error
422 EXT4FS_DBG("error in allocation");
423 }
424// EXT4FS_DBG("AAA: new addr \%u, level = \%u", new_block_addr, level);
425
426 ext4_inode_set_indirect_block(inode_ref->inode, level - 1, new_block_addr);
427
428 inode_ref->dirty = true;
429
430 rc = block_get(&new_block, fs->device, new_block_addr, BLOCK_FLAGS_NOREAD);
431 if (rc != EOK) {
432 EXT4FS_DBG("block load error");
433 // TODO error
434 }
435
436 memset(new_block->data, 0, block_size);
437 new_block->dirty = true;
438
439 rc = block_put(new_block);
440 if (rc != EOK) {
441 EXT4FS_DBG("block put error");
442 }
443
444// EXT4FS_DBG("allocated indirect block for level \%u, during setting iblock \%u", level, (uint32_t)iblock);
445
446 current_block = new_block_addr;
447 }
448
449 /* Navigate through other levels, until we find the block number
450 * or find null reference meaning we are dealing with sparse file
451 */
452 while (level > 0) {
453
454 rc = block_get(&block, fs->device, current_block, 0);
455 if (rc != EOK) {
456 return rc;
457 }
458
459 current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
460
461 if (current_block == 0) {
462 if (level > 1) {
463
464 rc = ext4_bitmap_alloc_block(fs, inode_ref, &new_block_addr);
465 if (rc != EOK) {
466 // TODO error
467 }
468
469 rc = block_get(&new_block, fs->device, new_block_addr, BLOCK_FLAGS_NOREAD);
470 if (rc != EOK) {
471 // TODO error
472
473 EXT4FS_DBG("BBB: error block loading");
474
475 }
476 memset(new_block->data, 0, block_size);
477 new_block->dirty = true;
478
479 block_put(new_block);
480
481 ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(new_block_addr);
482 block->dirty = true;
483 current_block = new_block_addr;
484 } else {
485 ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(fblock);
486 block->dirty = true;
487 }
488 }
489
490 rc = block_put(block);
491 if (rc != EOK) {
492 return rc;
493 }
494
495 level -= 1;
496
497 /* If we are on the last level, break here as
498 * there is no next level to visit
499 */
500 if (level == 0) {
501 break;
502 }
503
504 /* Visit the next level */
505 block_offset_in_level %= fs->inode_blocks_per_level[level];
506 offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
507 }
508
509 return EOK;
510}
511
512int ext4_filesystem_release_inode_block(ext4_filesystem_t *fs,
513 ext4_inode_ref_t *inode_ref, uint32_t iblock)
514{
515 int rc;
516 uint32_t fblock;
517 int i;
518 int level;
519 aoff64_t block_offset_in_level;
520 uint32_t current_block;
521 uint32_t offset_in_block;
522 block_t *block;
523 ext4_inode_t *inode = inode_ref->inode;
524
525 /* TODO handle extents */
526
527
528 /* Handle simple case when we are dealing with direct reference */
529 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
530 fblock = ext4_inode_get_direct_block(inode, iblock);
531 // Sparse file
532 if (fblock == 0) {
533 return EOK;
534 }
535
536 ext4_inode_set_direct_block(inode, iblock, 0);
537 return ext4_bitmap_free_block(fs, inode_ref, fblock);
538 }
539
540
541 /* Determine the indirection level needed to get the desired block */
542 level = -1;
543 for (i = 1; i < 4; i++) {
544 if (iblock < fs->inode_block_limits[i]) {
545 level = i;
546 break;
547 }
548 }
549
550 if (level == -1) {
551 return EIO;
552 }
553
554 /* Compute offsets for the topmost level */
555 block_offset_in_level = iblock - fs->inode_block_limits[level-1];
556 current_block = ext4_inode_get_indirect_block(inode, level-1);
557 offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
558
559 /* Navigate through other levels, until we find the block number
560 * or find null reference meaning we are dealing with sparse file
561 */
562 while (level > 0) {
563 rc = block_get(&block, fs->device, current_block, 0);
564 if (rc != EOK) {
565 return rc;
566 }
567
568 current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
569
570 // Set zero
571 if (level == 1) {
572 ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(0);
573 block->dirty = true;
574 }
575
576 rc = block_put(block);
577 if (rc != EOK) {
578 return rc;
579 }
580
581 level -= 1;
582
583 /* If we are on the last level, break here as
584 * there is no next level to visit
585 */
586 if (level == 0) {
587 break;
588 }
589
590 /* Visit the next level */
591 block_offset_in_level %= fs->inode_blocks_per_level[level];
592 offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
593 }
594
595 fblock = current_block;
596
597 if (fblock == 0) {
598 return EOK;
599 }
600
601 return ext4_bitmap_free_block(fs, inode_ref, fblock);
602
603}
604
605
606/**
607 * @}
608 */
Note: See TracBrowser for help on using the repository browser.