source: mainline/uspace/lib/ext4/libext4_filesystem.c@ 1e65444

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

more functional file writing - indirect blocks too

  • Property mode set to 100644
File size: 14.4 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 /* Navigate through other levels, until we find the block number
324 * or find null reference meaning we are dealing with sparse file
325 */
326 while (level > 0) {
327 rc = block_get(&block, fs->device, current_block, 0);
328 if (rc != EOK) {
329 return rc;
330 }
331
332 current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
333
334 rc = block_put(block);
335 if (rc != EOK) {
336 return rc;
337 }
338
339 if (current_block == 0) {
340 /* This is a sparse file */
341 *fblock = 0;
342 return EOK;
343 }
344
345 level -= 1;
346
347 /* If we are on the last level, break here as
348 * there is no next level to visit
349 */
350 if (level == 0) {
351 break;
352 }
353
354 /* Visit the next level */
355 block_offset_in_level %= fs->inode_blocks_per_level[level];
356 offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
357 }
358
359 *fblock = current_block;
360
361 return EOK;
362}
363
364
365int ext4_filesystem_set_inode_data_block_index(ext4_filesystem_t *fs,
366 ext4_inode_ref_t *inode_ref, aoff64_t iblock, uint32_t fblock)
367{
368
369 int rc;
370 uint32_t offset_in_block;
371 uint32_t current_block, new_block_addr;
372 uint32_t block_size;
373 aoff64_t block_offset_in_level;
374 int i;
375 int level;
376 block_t *block, *new_block;
377
378 /* Handle inode using extents */
379 if (ext4_superblock_has_feature_compatible(fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
380 ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)) {
381 // TODO
382 return ENOTSUP;
383
384 }
385
386 /* Handle simple case when we are dealing with direct reference */
387 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
388 ext4_inode_set_direct_block(inode_ref->inode, (uint32_t)iblock, fblock);
389 inode_ref->dirty = true;
390 return EOK;
391 }
392
393 /* Determine the indirection level needed to get the desired block */
394 level = -1;
395 for (i = 1; i < 4; i++) {
396 if (iblock < fs->inode_block_limits[i]) {
397 level = i;
398 break;
399 }
400 }
401
402 if (level == -1) {
403 return EIO;
404 }
405
406 block_size = ext4_superblock_get_block_size(fs->superblock);
407
408 /* Compute offsets for the topmost level */
409 block_offset_in_level = iblock - fs->inode_block_limits[level-1];
410 current_block = ext4_inode_get_indirect_block(inode_ref->inode, level-1);
411 offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
412
413 if (current_block == 0) {
414 rc = ext4_bitmap_alloc_block(fs, inode_ref, &new_block_addr);
415 if (rc != EOK) {
416 // TODO error
417 }
418 EXT4FS_DBG("AAA: new addr \%u, level = \%u", new_block_addr, level);
419
420 ext4_inode_set_indirect_block(inode_ref->inode, level - 1, new_block_addr);
421
422 inode_ref->dirty = true;
423
424 rc = block_get(&new_block, fs->device, new_block_addr, BLOCK_FLAGS_NOREAD);
425 if (rc != EOK) {
426 EXT4FS_DBG("block load error");
427 // TODO error
428 }
429
430 memset(new_block->data, 0, block_size);
431 new_block->dirty = true;
432
433 rc = block_put(new_block);
434 if (rc != EOK) {
435 EXT4FS_DBG("block put error");
436 }
437
438 current_block = new_block_addr;
439 }
440
441 /* Navigate through other levels, until we find the block number
442 * or find null reference meaning we are dealing with sparse file
443 */
444 while (level > 0) {
445
446 rc = block_get(&block, fs->device, current_block, 0);
447 if (rc != EOK) {
448 return rc;
449 }
450
451 current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
452
453 if (current_block == 0) {
454 if (level > 1) {
455
456 rc = ext4_bitmap_alloc_block(fs, inode_ref, &new_block_addr);
457 if (rc != EOK) {
458 // TODO error
459 }
460
461 rc = block_get(&new_block, fs->device, new_block_addr, BLOCK_FLAGS_NOREAD);
462 if (rc != EOK) {
463 // TODO error
464 }
465 memset(new_block->data, 0, block_size);
466 new_block->dirty = true;
467
468 block_put(new_block);
469
470 ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(new_block_addr);
471 block->dirty = true;
472 current_block = new_block_addr;
473 } else {
474 ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(fblock);
475 block->dirty = true;
476 }
477 }
478
479 rc = block_put(block);
480 if (rc != EOK) {
481 return rc;
482 }
483
484 level -= 1;
485
486 /* If we are on the last level, break here as
487 * there is no next level to visit
488 */
489 if (level == 0) {
490 break;
491 }
492
493 /* Visit the next level */
494 block_offset_in_level %= fs->inode_blocks_per_level[level];
495 offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
496 }
497
498 return EOK;
499}
500
501int ext4_filesystem_release_inode_block(ext4_filesystem_t *fs,
502 ext4_inode_ref_t *inode_ref, uint32_t iblock)
503{
504 int rc;
505 uint32_t fblock;
506 int i;
507 int level;
508 aoff64_t block_offset_in_level;
509 uint32_t current_block;
510 uint32_t offset_in_block;
511 block_t *block;
512 ext4_inode_t *inode = inode_ref->inode;
513
514 /* TODO handle extents */
515
516
517 /* Handle simple case when we are dealing with direct reference */
518 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
519 fblock = ext4_inode_get_direct_block(inode, iblock);
520 // Sparse file
521 if (fblock == 0) {
522 return EOK;
523 }
524
525 ext4_inode_set_direct_block(inode, iblock, 0);
526 return ext4_bitmap_free_block(fs, fblock);
527 }
528
529
530 /* Determine the indirection level needed to get the desired block */
531 level = -1;
532 for (i = 1; i < 4; i++) {
533 if (iblock < fs->inode_block_limits[i]) {
534 level = i;
535 break;
536 }
537 }
538
539 if (level == -1) {
540 return EIO;
541 }
542
543 /* Compute offsets for the topmost level */
544 block_offset_in_level = iblock - fs->inode_block_limits[level-1];
545 current_block = ext4_inode_get_indirect_block(inode, level-1);
546 offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
547
548 /* Navigate through other levels, until we find the block number
549 * or find null reference meaning we are dealing with sparse file
550 */
551 while (level > 0) {
552 rc = block_get(&block, fs->device, current_block, 0);
553 if (rc != EOK) {
554 return rc;
555 }
556
557 current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
558
559 // Set zero
560 if (level == 1) {
561 ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(0);
562 block->dirty = true;
563 }
564
565 rc = block_put(block);
566 if (rc != EOK) {
567 return rc;
568 }
569
570 level -= 1;
571
572 /* If we are on the last level, break here as
573 * there is no next level to visit
574 */
575 if (level == 0) {
576 break;
577 }
578
579 /* Visit the next level */
580 block_offset_in_level %= fs->inode_blocks_per_level[level];
581 offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
582 }
583
584 fblock = current_block;
585
586 if (fblock == 0) {
587 return EOK;
588 }
589
590 return ext4_bitmap_free_block(fs, fblock);
591
592}
593
594
595/**
596 * @}
597 */
Note: See TracBrowser for help on using the repository browser.