source: mainline/uspace/lib/ext4/libext4_filesystem.c@ 2add9ec

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

added TODOs for comments and first part of superblock comments

  • Property mode set to 100644
File size: 25.3 KB
RevLine 
[6c501f8]1/*
[f22d5ef0]2 * Copyright (c) 2012 Frantisek Princ
[6c501f8]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
[e63ce679]35 * @brief More complex filesystem operations.
[6c501f8]36 */
37
[9b9d37bb]38#include <byteorder.h>
[6c501f8]39#include <errno.h>
[01ab41b]40#include <malloc.h>
[3711e7e]41#include "libext4.h"
[6c501f8]42
[9fc72fb3]43/** TODO comment
44 *
45 */
[6c501f8]46int ext4_filesystem_init(ext4_filesystem_t *fs, service_id_t service_id)
47{
[01ab41b]48 int rc;
49
50 fs->device = service_id;
51
[fffb061]52 rc = block_init(EXCHANGE_SERIALIZE, fs->device, 4096);
[01ab41b]53 if (rc != EOK) {
54 return rc;
55 }
56
[9c0c0e1]57 /* Read superblock from device */
[d9bbe45]58 ext4_superblock_t *temp_superblock;
[01ab41b]59 rc = ext4_superblock_read_direct(fs->device, &temp_superblock);
60 if (rc != EOK) {
61 block_fini(fs->device);
62 return rc;
63 }
64
[9c0c0e1]65 /* Read block size from superblock and check */
[d9bbe45]66 uint32_t block_size = ext4_superblock_get_block_size(temp_superblock);
[01ab41b]67 if (block_size > EXT4_MAX_BLOCK_SIZE) {
68 block_fini(fs->device);
69 return ENOTSUP;
70 }
71
[9c0c0e1]72 /* Initialize block caching */
[b12ca16]73 rc = block_cache_init(service_id, block_size, 0, CACHE_MODE_WT);
[01ab41b]74 if (rc != EOK) {
75 block_fini(fs->device);
76 return rc;
77 }
78
[d9bbe45]79 uint32_t block_ids_per_block = block_size / sizeof(uint32_t);
[a9a0982]80 fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
81 fs->inode_blocks_per_level[0] = 1;
[d9bbe45]82 for (int i = 1; i < 4; i++) {
[6088193]83 fs->inode_blocks_per_level[i] = fs->inode_blocks_per_level[i-1] *
[a9a0982]84 block_ids_per_block;
85 fs->inode_block_limits[i] = fs->inode_block_limits[i-1] +
86 fs->inode_blocks_per_level[i];
87 }
88
[9c0c0e1]89 /* Return loaded superblock */
[01ab41b]90 fs->superblock = temp_superblock;
91
[6c501f8]92 return EOK;
93}
94
[9fc72fb3]95/** TODO comment
96 *
97 */
[ae3d4f8]98int ext4_filesystem_fini(ext4_filesystem_t *fs, bool write_sb)
[3711e7e]99{
[ae3d4f8]100 int rc = EOK;
[d9bbe45]101
[ae3d4f8]102 if (write_sb) {
103 rc = ext4_superblock_write_direct(fs->device, fs->superblock);
104 }
105
[3711e7e]106 free(fs->superblock);
107 block_fini(fs->device);
[ae3d4f8]108
109 return rc;
[3711e7e]110}
111
[9fc72fb3]112/** TODO comment
113 *
114 */
[6c501f8]115int ext4_filesystem_check_sanity(ext4_filesystem_t *fs)
116{
[01ab41b]117 int rc;
118
119 rc = ext4_superblock_check_sanity(fs->superblock);
120 if (rc != EOK) {
121 return rc;
122 }
123
[6c501f8]124 return EOK;
125}
126
[9fc72fb3]127/** TODO comment
128 *
129 */
[9c0c0e1]130int ext4_filesystem_check_features(ext4_filesystem_t *fs, bool *o_read_only)
[6c501f8]131{
[9c0c0e1]132 /* Feature flags are present in rev 1 and later */
133 if (ext4_superblock_get_rev_level(fs->superblock) == 0) {
134 *o_read_only = false;
135 return EOK;
136 }
137
138 uint32_t incompatible_features;
139 incompatible_features = ext4_superblock_get_features_incompatible(fs->superblock);
140 incompatible_features &= ~EXT4_FEATURE_INCOMPAT_SUPP;
141 if (incompatible_features > 0) {
142 *o_read_only = true;
143 return ENOTSUP;
144 }
145
146 uint32_t compatible_read_only;
147 compatible_read_only = ext4_superblock_get_features_read_only(fs->superblock);
148 compatible_read_only &= ~EXT4_FEATURE_RO_COMPAT_SUPP;
149 if (compatible_read_only > 0) {
150 *o_read_only = true;
151 }
152
[6c501f8]153 return EOK;
154}
155
[9fc72fb3]156/** TODO comment
157 *
158 */
[3711e7e]159int ext4_filesystem_get_block_group_ref(ext4_filesystem_t *fs, uint32_t bgid,
160 ext4_block_group_ref_t **ref)
[6c501f8]161{
[3711e7e]162 int rc;
163
[d9bbe45]164 ext4_block_group_ref_t *newref = malloc(sizeof(ext4_block_group_ref_t));
[3711e7e]165 if (newref == NULL) {
166 return ENOMEM;
167 }
168
[d9bbe45]169 uint32_t descriptors_per_block = ext4_superblock_get_block_size(fs->superblock)
[c25e39b]170 / ext4_superblock_get_desc_size(fs->superblock);
[3711e7e]171
172 /* Block group descriptor table starts at the next block after superblock */
[d9bbe45]173 aoff64_t block_id = ext4_superblock_get_first_data_block(fs->superblock) + 1;
[3711e7e]174
175 /* Find the block containing the descriptor we are looking for */
176 block_id += bgid / descriptors_per_block;
[d9bbe45]177 uint32_t offset = (bgid % descriptors_per_block) * ext4_superblock_get_desc_size(fs->superblock);
[3711e7e]178
179 rc = block_get(&newref->block, fs->device, block_id, 0);
180 if (rc != EOK) {
181 free(newref);
182 return rc;
183 }
184
185 newref->block_group = newref->block->data + offset;
[1ac1ab4]186 newref->fs = fs;
187 newref->index = bgid;
[12b4a7f]188 newref->dirty = false;
[3711e7e]189
190 *ref = newref;
191
192 return EOK;
[6c501f8]193}
194
[9fc72fb3]195/** TODO comment
196 *
197 */
[1ac1ab4]198static uint16_t ext4_filesystem_bg_checksum(ext4_superblock_t *sb, uint32_t bgid,
199 ext4_block_group_t *bg)
200{
201 uint16_t crc = 0;
202
203 if (ext4_superblock_has_feature_read_only(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
204
205 void *base = bg;
206 void *checksum = &bg->checksum;
207
208 uint32_t offset = (uint32_t)(checksum - base);
209
210 uint32_t le_group = host2uint32_t_le(bgid);
211
212 crc = crc16(~0, sb->uuid, sizeof(sb->uuid));
213 crc = crc16(crc, (uint8_t *)&le_group, sizeof(le_group));
214 crc = crc16(crc, (uint8_t *)bg, offset);
215
216 offset += sizeof(bg->checksum); /* skip checksum */
217
218 /* for checksum of struct ext4_group_desc do the rest...*/
219 if ((ext4_superblock_has_feature_incompatible(sb, EXT4_FEATURE_INCOMPAT_64BIT)) &&
220 offset < ext4_superblock_get_desc_size(sb)) {
221
222 crc = crc16(crc, ((uint8_t *)bg) + offset, ext4_superblock_get_desc_size(sb) - offset);
223 }
224 }
225
226 return crc;
227
228}
229
[9fc72fb3]230/** TODO comment
231 *
232 */
[829d238]233int ext4_filesystem_put_block_group_ref(ext4_block_group_ref_t *ref)
234{
235 int rc;
236
[12b4a7f]237 if (ref->dirty) {
[5b0a3946]238 uint16_t checksum = ext4_filesystem_bg_checksum(
[1ac1ab4]239 ref->fs->superblock, ref->index, ref->block_group);
240
[5b0a3946]241 ext4_block_group_set_checksum(ref->block_group, checksum);
[1ac1ab4]242
[12b4a7f]243 ref->block->dirty = true;
244 }
245
[829d238]246 rc = block_put(ref->block);
247 free(ref);
248
249 return rc;
250}
251
[9fc72fb3]252/** TODO comment
253 *
254 */
[3711e7e]255int ext4_filesystem_get_inode_ref(ext4_filesystem_t *fs, uint32_t index,
256 ext4_inode_ref_t **ref)
257{
258 int rc;
259
[d9bbe45]260 ext4_inode_ref_t *newref = malloc(sizeof(ext4_inode_ref_t));
[3711e7e]261 if (newref == NULL) {
262 return ENOMEM;
263 }
264
[d9bbe45]265 uint32_t inodes_per_group =
266 ext4_superblock_get_inodes_per_group(fs->superblock);
[3711e7e]267
268 /* inode numbers are 1-based, but it is simpler to work with 0-based
269 * when computing indices
270 */
271 index -= 1;
[d9bbe45]272 uint32_t block_group = index / inodes_per_group;
273 uint32_t offset_in_group = index % inodes_per_group;
[3711e7e]274
[d9bbe45]275 ext4_block_group_ref_t *bg_ref;
[3711e7e]276 rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
277 if (rc != EOK) {
278 free(newref);
279 return rc;
280 }
281
[d9bbe45]282 uint32_t inode_table_start = ext4_block_group_get_inode_table_first_block(
[fe27eb4]283 bg_ref->block_group, fs->superblock);
[3711e7e]284
[829d238]285 rc = ext4_filesystem_put_block_group_ref(bg_ref);
286 if (rc != EOK) {
287 free(newref);
288 return rc;
289 }
290
[d9bbe45]291 uint16_t inode_size = ext4_superblock_get_inode_size(fs->superblock);
292 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
293 uint32_t byte_offset_in_group = offset_in_group * inode_size;
[3711e7e]294
[d9bbe45]295 aoff64_t block_id = inode_table_start + (byte_offset_in_group / block_size);
[3711e7e]296 rc = block_get(&newref->block, fs->device, block_id, 0);
297 if (rc != EOK) {
298 free(newref);
299 return rc;
300 }
301
[d9bbe45]302 uint32_t offset_in_block = byte_offset_in_group % block_size;
[3711e7e]303 newref->inode = newref->block->data + offset_in_block;
304 /* we decremented index above, but need to store the original value
305 * in the reference
306 */
[1ac1ab4]307 newref->index = index + 1;
308 newref->fs = fs;
[052e82d]309 newref->dirty = false;
[3711e7e]310
311 *ref = newref;
312
[9b9d37bb]313 return EOK;
314}
315
[9fc72fb3]316/** TODO comment
317 *
318 */
[9b9d37bb]319int ext4_filesystem_put_inode_ref(ext4_inode_ref_t *ref)
320{
321 int rc;
322
[052e82d]323 if (ref->dirty) {
324 ref->block->dirty = true;
325 }
326
[9b9d37bb]327 rc = block_put(ref->block);
328 free(ref);
329
330 return rc;
331}
332
[9fc72fb3]333/** TODO comment
334 *
335 */
[304faab]336int ext4_filesystem_alloc_inode(ext4_filesystem_t *fs,
337 ext4_inode_ref_t **inode_ref, int flags)
[2d2c6ce]338{
[304faab]339 int rc;
[2d2c6ce]340
[304faab]341 bool is_dir = false;
[2d2c6ce]342 if (flags & L_DIRECTORY) {
[304faab]343 is_dir = true;
344 }
345
346 // allocate inode
347 uint32_t index;
348 rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
349 if (rc != EOK) {
350 return rc;
351 }
352
353 rc = ext4_filesystem_get_inode_ref(fs, index, inode_ref);
354 if (rc != EOK) {
355 ext4_ialloc_free_inode(fs, index, is_dir);
356 return rc;
357 }
358
359 // init inode
360 ext4_inode_t *inode = (*inode_ref)->inode;
361
362 if (is_dir) {
[2d2c6ce]363 ext4_inode_set_mode(fs->superblock, inode, EXT4_INODE_MODE_DIRECTORY);
364 ext4_inode_set_links_count(inode, 1); // '.' entry
365 } else {
366 ext4_inode_set_mode(fs->superblock, inode, EXT4_INODE_MODE_FILE);
367 ext4_inode_set_links_count(inode, 0);
368 }
369
370 ext4_inode_set_uid(inode, 0);
371 ext4_inode_set_gid(inode, 0);
372 ext4_inode_set_size(inode, 0);
373 ext4_inode_set_access_time(inode, 0);
374 ext4_inode_set_change_inode_time(inode, 0);
375 ext4_inode_set_modification_time(inode, 0);
376 ext4_inode_set_deletion_time(inode, 0);
377 ext4_inode_set_blocks_count(fs->superblock, inode, 0);
378 ext4_inode_set_flags(inode, 0);
379 ext4_inode_set_generation(inode, 0);
380
[936132f]381 // Reset blocks array
[2d2c6ce]382 for (uint32_t i = 0; i < EXT4_INODE_BLOCKS; i++) {
383 inode->blocks[i] = 0;
384 }
385
[936132f]386 if (ext4_superblock_has_feature_incompatible(
387 fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
388
389 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
390
391 // Initialize extent root header
392 ext4_extent_header_t *header = ext4_inode_get_extent_header(inode);
393 ext4_extent_header_set_depth(header, 0);
394 ext4_extent_header_set_entries_count(header, 0);
395 ext4_extent_header_set_generation(header, 0);
396 ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
397
398 uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof (uint32_t) - sizeof(ext4_extent_header_t))
399 / sizeof(ext4_extent_t);
400
401 ext4_extent_header_set_max_entries_count(header, max_entries);
402 }
403
[304faab]404 (*inode_ref)->dirty = true;
405
[2d2c6ce]406 return EOK;
407}
408
[9fc72fb3]409/** TODO comment
410 *
411 */
[1ac1ab4]412int ext4_filesystem_free_inode(ext4_inode_ref_t *inode_ref)
[3d4fd2c]413{
414 int rc;
415
[3e2952b]416 ext4_filesystem_t *fs = inode_ref->fs;
417
418 if (ext4_superblock_has_feature_incompatible(
419 fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
420 ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)) {
421
422 // Data structures are released during truncate operation...
423 goto finish;
424 }
425
[ca3d77a]426 // release all indirect (no data) blocks
[3d4fd2c]427
428 // 1) Single indirect
[d9bbe45]429 uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
[3d4fd2c]430 if (fblock != 0) {
[1ac1ab4]431 rc = ext4_balloc_free_block(inode_ref, fblock);
[3d4fd2c]432 if (rc != EOK) {
[ca3d77a]433 return rc;
[3d4fd2c]434 }
435
436 ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
437 }
438
439 block_t *block;
440 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
441 uint32_t count = block_size / sizeof(uint32_t);
442
443 // 2) Double indirect
444 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
445 if (fblock != 0) {
446 rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
447 if (rc != EOK) {
[e63ce679]448 return rc;
[3d4fd2c]449 }
450
451 uint32_t ind_block;
452 for (uint32_t offset = 0; offset < count; ++offset) {
453 ind_block = uint32_t_le2host(((uint32_t*)block->data)[offset]);
454
455 if (ind_block != 0) {
[1ac1ab4]456 rc = ext4_balloc_free_block(inode_ref, ind_block);
[3d4fd2c]457 if (rc != EOK) {
[e63ce679]458 block_put(block);
459 return rc;
[3d4fd2c]460 }
461 }
462 }
463
464 block_put(block);
[1ac1ab4]465 rc = ext4_balloc_free_block(inode_ref, fblock);
[3d4fd2c]466 if (rc != EOK) {
[e63ce679]467 return rc;
[3d4fd2c]468 }
469
470 ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
471 }
472
473
474 // 3) Tripple indirect
475 block_t *subblock;
476 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
477 if (fblock != 0) {
478 rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
479 if (rc != EOK) {
[e63ce679]480 return rc;
[3d4fd2c]481 }
482
483 uint32_t ind_block;
484 for (uint32_t offset = 0; offset < count; ++offset) {
485 ind_block = uint32_t_le2host(((uint32_t*)block->data)[offset]);
486
487 if (ind_block != 0) {
488 rc = block_get(&subblock, fs->device, ind_block, BLOCK_FLAGS_NONE);
489 if (rc != EOK) {
[e63ce679]490 block_put(block);
491 return rc;
[3d4fd2c]492 }
493
494 uint32_t ind_subblock;
495 for (uint32_t suboffset = 0; suboffset < count; ++suboffset) {
496 ind_subblock = uint32_t_le2host(((uint32_t*)subblock->data)[suboffset]);
497
498 if (ind_subblock != 0) {
[1ac1ab4]499 rc = ext4_balloc_free_block(inode_ref, ind_subblock);
[3d4fd2c]500 if (rc != EOK) {
[e63ce679]501 block_put(subblock);
502 block_put(block);
503 return rc;
[3d4fd2c]504 }
505 }
506
507 }
508 block_put(subblock);
509
510 }
511
[1ac1ab4]512 rc = ext4_balloc_free_block(inode_ref, ind_block);
[3d4fd2c]513 if (rc != EOK) {
[e63ce679]514 block_put(block);
515 return rc;
[3d4fd2c]516 }
517
518
519 }
520
521 block_put(block);
[1ac1ab4]522 rc = ext4_balloc_free_block(inode_ref, fblock);
[3d4fd2c]523 if (rc != EOK) {
[e63ce679]524 return rc;
[3d4fd2c]525 }
526
527 ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
528 }
529
[3e2952b]530finish:
[e5f8762]531 inode_ref->dirty = true;
532
[3d4fd2c]533 // Free inode
[304faab]534 if (ext4_inode_is_type(fs->superblock, inode_ref->inode,
535 EXT4_INODE_MODE_DIRECTORY)) {
536 rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
537 } else {
538 rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
539 }
[3d4fd2c]540 if (rc != EOK) {
541 return rc;
542 }
543
544 return EOK;
545}
546
[9fc72fb3]547/** TODO comment
548 *
549 */
[1ac1ab4]550int ext4_filesystem_truncate_inode(
[3d4fd2c]551 ext4_inode_ref_t *inode_ref, aoff64_t new_size)
552{
[ca3d77a]553 int rc;
554
[1ac1ab4]555 ext4_superblock_t *sb = inode_ref->fs->superblock;
556
557 if (! ext4_inode_can_truncate(sb, inode_ref->inode)) {
[3d4fd2c]558 // Unable to truncate
559 return EINVAL;
560 }
561
[1ac1ab4]562 aoff64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
[3d4fd2c]563 if (old_size == new_size) {
564 // Nothing to do
565 return EOK;
566 }
567
[ca3d77a]568 // It's not suppported to make the larger file
[3d4fd2c]569 if (old_size < new_size) {
570 return EINVAL;
571 }
572
[d9bbe45]573 aoff64_t size_diff = old_size - new_size;
[1ac1ab4]574 uint32_t block_size = ext4_superblock_get_block_size(sb);
[ca3d77a]575 uint32_t diff_blocks_count = size_diff / block_size;
[3d4fd2c]576 if (size_diff % block_size != 0) {
[ca3d77a]577 diff_blocks_count++;
[3d4fd2c]578 }
579
[ca3d77a]580 uint32_t old_blocks_count = old_size / block_size;
[3d4fd2c]581 if (old_size % block_size != 0) {
[ca3d77a]582 old_blocks_count++;
[3d4fd2c]583 }
584
[5b0a3946]585 if (ext4_superblock_has_feature_incompatible(
586 inode_ref->fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
587 ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)) {
588
[1196df6]589 rc = ext4_extent_release_blocks_from(inode_ref,
590 old_blocks_count - diff_blocks_count);
[ca3d77a]591 if (rc != EOK) {
592 return rc;
593 }
[5b0a3946]594 } else {
595 // starting from 1 because of logical blocks are numbered from 0
596 for (uint32_t i = 1; i <= diff_blocks_count; ++i) {
597 rc = ext4_filesystem_release_inode_block(inode_ref, old_blocks_count - i);
598 if (rc != EOK) {
599 return rc;
600 }
601 }
[3d4fd2c]602 }
603
604 ext4_inode_set_size(inode_ref->inode, new_size);
605
606 inode_ref->dirty = true;
607
608 return EOK;
609}
610
[9fc72fb3]611/** TODO comment
612 *
613 */
[1ac1ab4]614int ext4_filesystem_get_inode_data_block_index(ext4_inode_ref_t *inode_ref,
[b73530a]615 aoff64_t iblock, uint32_t *fblock)
[9b9d37bb]616{
617 int rc;
[d9bbe45]618
[1ac1ab4]619 ext4_filesystem_t *fs = inode_ref->fs;
[d9bbe45]620
[b73530a]621 if (ext4_inode_get_size(fs->superblock, inode_ref->inode) == 0) {
622 *fblock = 0;
623 return EOK;
624 }
625
[9b9d37bb]626 uint32_t current_block;
627
[8958a26]628 /* Handle inode using extents */
[a872fc09]629 if (ext4_superblock_has_feature_incompatible(fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
[9104bb5]630 ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)) {
[1ac1ab4]631 rc = ext4_extent_find_block(inode_ref, iblock, &current_block);
[9104bb5]632
633 if (rc != EOK) {
634 return rc;
635 }
636
[acd869e]637 *fblock = current_block;
638 return EOK;
639
640 }
641
[9104bb5]642 ext4_inode_t *inode = inode_ref->inode;
643
[9b9d37bb]644 /* Handle simple case when we are dealing with direct reference */
[e68c834]645 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
[9b9d37bb]646 current_block = ext4_inode_get_direct_block(inode, (uint32_t)iblock);
647 *fblock = current_block;
648 return EOK;
649 }
650
651 /* Determine the indirection level needed to get the desired block */
[d9bbe45]652 int level = -1;
653 for (int i = 1; i < 4; i++) {
[a9a0982]654 if (iblock < fs->inode_block_limits[i]) {
[9b9d37bb]655 level = i;
656 break;
657 }
658 }
659
660 if (level == -1) {
661 return EIO;
662 }
663
664 /* Compute offsets for the topmost level */
[d9bbe45]665 aoff64_t block_offset_in_level = iblock - fs->inode_block_limits[level-1];
[9b9d37bb]666 current_block = ext4_inode_get_indirect_block(inode, level-1);
[d9bbe45]667 uint32_t offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
[9b9d37bb]668
[6088193]669 if (current_block == 0) {
670 *fblock = 0;
671 return EOK;
672 }
673
[d9bbe45]674 block_t *block;
675
[9b9d37bb]676 /* Navigate through other levels, until we find the block number
677 * or find null reference meaning we are dealing with sparse file
678 */
679 while (level > 0) {
680 rc = block_get(&block, fs->device, current_block, 0);
681 if (rc != EOK) {
682 return rc;
683 }
684
685 current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
686
687 rc = block_put(block);
688 if (rc != EOK) {
689 return rc;
690 }
691
692 if (current_block == 0) {
693 /* This is a sparse file */
694 *fblock = 0;
695 return EOK;
696 }
697
698 level -= 1;
699
700 /* If we are on the last level, break here as
701 * there is no next level to visit
702 */
703 if (level == 0) {
704 break;
705 }
706
707 /* Visit the next level */
[a9a0982]708 block_offset_in_level %= fs->inode_blocks_per_level[level];
709 offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
[9b9d37bb]710 }
711
712 *fblock = current_block;
713
[3711e7e]714 return EOK;
715}
[6c501f8]716
[9fc72fb3]717/** TODO comment
718 *
719 */
[1ac1ab4]720int ext4_filesystem_set_inode_data_block_index(ext4_inode_ref_t *inode_ref,
721 aoff64_t iblock, uint32_t fblock)
[35f48f2]722{
[1e65444]723 int rc;
[d9bbe45]724
[1ac1ab4]725 ext4_filesystem_t *fs = inode_ref->fs;
[35f48f2]726
727 /* Handle inode using extents */
728 if (ext4_superblock_has_feature_compatible(fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
[1e65444]729 ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)) {
[ce6de59]730 // not reachable !!!
[35f48f2]731 return ENOTSUP;
732 }
733
734 /* Handle simple case when we are dealing with direct reference */
735 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
[1e65444]736 ext4_inode_set_direct_block(inode_ref->inode, (uint32_t)iblock, fblock);
737 inode_ref->dirty = true;
[35f48f2]738 return EOK;
739 }
740
[1e65444]741 /* Determine the indirection level needed to get the desired block */
[d9bbe45]742 int level = -1;
743 for (int i = 1; i < 4; i++) {
[1e65444]744 if (iblock < fs->inode_block_limits[i]) {
745 level = i;
746 break;
747 }
748 }
749
750 if (level == -1) {
751 return EIO;
752 }
753
[d9bbe45]754 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
[1e65444]755
756 /* Compute offsets for the topmost level */
[d9bbe45]757 aoff64_t block_offset_in_level = iblock - fs->inode_block_limits[level-1];
758 uint32_t current_block = ext4_inode_get_indirect_block(inode_ref->inode, level-1);
759 uint32_t offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
760
761 uint32_t new_block_addr;
762 block_t *block, *new_block;
[1e65444]763
764 if (current_block == 0) {
[1ac1ab4]765 rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
[1e65444]766 if (rc != EOK) {
[e63ce679]767 return rc;
[1e65444]768 }
769
770 ext4_inode_set_indirect_block(inode_ref->inode, level - 1, new_block_addr);
771
772 inode_ref->dirty = true;
773
774 rc = block_get(&new_block, fs->device, new_block_addr, BLOCK_FLAGS_NOREAD);
775 if (rc != EOK) {
[1ac1ab4]776 ext4_balloc_free_block(inode_ref, new_block_addr);
[e63ce679]777 return rc;
[1e65444]778 }
779
780 memset(new_block->data, 0, block_size);
781 new_block->dirty = true;
782
783 rc = block_put(new_block);
784 if (rc != EOK) {
[e63ce679]785 return rc;
[1e65444]786 }
787
788 current_block = new_block_addr;
789 }
790
791 /* Navigate through other levels, until we find the block number
792 * or find null reference meaning we are dealing with sparse file
793 */
794 while (level > 0) {
795
796 rc = block_get(&block, fs->device, current_block, 0);
797 if (rc != EOK) {
798 return rc;
799 }
800
801 current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
802
[b12ca16]803 if ((level > 1) && (current_block == 0)) {
[1ac1ab4]804 rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
[b12ca16]805 if (rc != EOK) {
[e63ce679]806 block_put(block);
807 return rc;
[b12ca16]808 }
[1e65444]809
[b12ca16]810 rc = block_get(&new_block, fs->device, new_block_addr, BLOCK_FLAGS_NOREAD);
811 if (rc != EOK) {
[e63ce679]812 block_put(block);
813 return rc;
[b12ca16]814 }
[e63ce679]815
[b12ca16]816 memset(new_block->data, 0, block_size);
817 new_block->dirty = true;
[6088193]818
[b12ca16]819 rc = block_put(new_block);
820 if (rc != EOK) {
[e63ce679]821 block_put(block);
822 return rc;
[b12ca16]823 }
[1e65444]824
[b12ca16]825 ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(new_block_addr);
826 block->dirty = true;
827 current_block = new_block_addr;
828 }
[1e65444]829
[b12ca16]830 if (level == 1) {
831 ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(fblock);
832 block->dirty = true;
[1e65444]833 }
834
835 rc = block_put(block);
836 if (rc != EOK) {
837 return rc;
838 }
839
840 level -= 1;
841
842 /* If we are on the last level, break here as
843 * there is no next level to visit
844 */
845 if (level == 0) {
846 break;
847 }
848
849 /* Visit the next level */
850 block_offset_in_level %= fs->inode_blocks_per_level[level];
851 offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
852 }
[35f48f2]853
854 return EOK;
855}
856
[9fc72fb3]857/** TODO comment
858 *
859 */
[1ac1ab4]860int ext4_filesystem_release_inode_block(
[052e82d]861 ext4_inode_ref_t *inode_ref, uint32_t iblock)
[d5a78e28]862{
863 int rc;
[d9bbe45]864
[fffb061]865 uint32_t fblock;
866
[1ac1ab4]867 ext4_filesystem_t *fs = inode_ref->fs;
868
[ce6de59]869 // EXTENTS are handled otherwise
[5b0a3946]870 assert(! (ext4_superblock_has_feature_incompatible(fs->superblock,
871 EXT4_FEATURE_INCOMPAT_EXTENTS) &&
872 ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)));
[12b4a7f]873
[d9bbe45]874 ext4_inode_t *inode = inode_ref->inode;
875
[052e82d]876 /* Handle simple case when we are dealing with direct reference */
877 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
[12b4a7f]878 fblock = ext4_inode_get_direct_block(inode, iblock);
[052e82d]879 // Sparse file
880 if (fblock == 0) {
881 return EOK;
882 }
[d5a78e28]883
[052e82d]884 ext4_inode_set_direct_block(inode, iblock, 0);
[1ac1ab4]885 return ext4_balloc_free_block(inode_ref, fblock);
[12b4a7f]886 }
887
888
889 /* Determine the indirection level needed to get the desired block */
[d9bbe45]890 int level = -1;
891 for (int i = 1; i < 4; i++) {
[12b4a7f]892 if (iblock < fs->inode_block_limits[i]) {
893 level = i;
894 break;
[052e82d]895 }
[12b4a7f]896 }
897
898 if (level == -1) {
899 return EIO;
900 }
901
902 /* Compute offsets for the topmost level */
[d9bbe45]903 aoff64_t block_offset_in_level = iblock - fs->inode_block_limits[level-1];
904 uint32_t current_block = ext4_inode_get_indirect_block(inode, level-1);
905 uint32_t offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
[d5a78e28]906
[12b4a7f]907 /* Navigate through other levels, until we find the block number
908 * or find null reference meaning we are dealing with sparse file
909 */
[d9bbe45]910 block_t *block;
[12b4a7f]911 while (level > 0) {
912 rc = block_get(&block, fs->device, current_block, 0);
913 if (rc != EOK) {
914 return rc;
[052e82d]915 }
[d5a78e28]916
[12b4a7f]917 current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
[d5a78e28]918
[12b4a7f]919 // Set zero
920 if (level == 1) {
921 ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(0);
922 block->dirty = true;
[052e82d]923 }
[d5a78e28]924
[12b4a7f]925 rc = block_put(block);
926 if (rc != EOK) {
927 return rc;
928 }
[d5a78e28]929
[12b4a7f]930 level -= 1;
931
932 /* If we are on the last level, break here as
933 * there is no next level to visit
934 */
935 if (level == 0) {
936 break;
[052e82d]937 }
[12b4a7f]938
939 /* Visit the next level */
940 block_offset_in_level %= fs->inode_blocks_per_level[level];
941 offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
942 }
943
944 fblock = current_block;
945
946 if (fblock == 0) {
947 return EOK;
[052e82d]948 }
[d5a78e28]949
[1ac1ab4]950 return ext4_balloc_free_block(inode_ref, fblock);
[d5a78e28]951
952}
953
[9fc72fb3]954/** TODO comment
955 *
956 */
[5b16912]957int ext4_filesystem_append_inode_block(ext4_inode_ref_t *inode_ref,
958 uint32_t *fblock, uint32_t *iblock)
959{
[ce6de59]960 int rc;
961
[7eb033ce]962 EXT4FS_DBG("");
963
[ce6de59]964 // Handle extents separately
965 if (ext4_superblock_has_feature_incompatible(
966 inode_ref->fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
967 ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)) {
[5b16912]968
[ce6de59]969 return ext4_extent_append_block(inode_ref, iblock, fblock);
[5b16912]970
[ce6de59]971 }
[5b16912]972
973 ext4_superblock_t *sb = inode_ref->fs->superblock;
974
975 // Compute next block index and allocate data block
976 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
977 uint32_t block_size = ext4_superblock_get_block_size(sb);
978
[7eb033ce]979 // TODO zarovnat inode size a ne assert!!!
[5b16912]980 assert(inode_size % block_size == 0);
981
982 // Logical blocks are numbered from 0
983 uint32_t new_block_idx = inode_size / block_size;
984
985 uint32_t phys_block;
986 rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
987 if (rc != EOK) {
988 return rc;
989 }
990
991 rc = ext4_filesystem_set_inode_data_block_index(inode_ref, new_block_idx, phys_block);
992 if (rc != EOK) {
993 ext4_balloc_free_block(inode_ref, phys_block);
994 return rc;
995 }
996
997 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
998
999 inode_ref->dirty = true;
1000
1001 *fblock = phys_block;
1002 *iblock = new_block_idx;
1003 return EOK;
1004}
1005
[9fc72fb3]1006/** TODO comment
1007 *
1008 */
[1ac1ab4]1009int ext4_filesystem_add_orphan(ext4_inode_ref_t *inode_ref)
[ebcaff4]1010{
[1ac1ab4]1011 uint32_t next_orphan = ext4_superblock_get_last_orphan(
1012 inode_ref->fs->superblock);
[ebcaff4]1013 ext4_inode_set_deletion_time(inode_ref->inode, next_orphan);
[1ac1ab4]1014 ext4_superblock_set_last_orphan(
1015 inode_ref->fs->superblock, inode_ref->index);
[ebcaff4]1016 inode_ref->dirty = true;
1017
1018 return EOK;
1019}
1020
[9fc72fb3]1021/** TODO comment
1022 *
1023 */
[1ac1ab4]1024int ext4_filesystem_delete_orphan(ext4_inode_ref_t *inode_ref)
[ebcaff4]1025{
1026 int rc;
1027
[1ac1ab4]1028 uint32_t last_orphan = ext4_superblock_get_last_orphan(
1029 inode_ref->fs->superblock);
[ebcaff4]1030 assert(last_orphan > 0);
1031
1032 uint32_t next_orphan = ext4_inode_get_deletion_time(inode_ref->inode);
1033
1034 if (last_orphan == inode_ref->index) {
[1ac1ab4]1035 ext4_superblock_set_last_orphan(inode_ref->fs->superblock, next_orphan);
[ebcaff4]1036 ext4_inode_set_deletion_time(inode_ref->inode, 0);
1037 inode_ref->dirty = true;
1038 return EOK;
1039 }
1040
1041 ext4_inode_ref_t *current;
[1ac1ab4]1042 rc = ext4_filesystem_get_inode_ref(inode_ref->fs, last_orphan, &current);
[ebcaff4]1043 if (rc != EOK) {
1044 return rc;
1045 }
1046 next_orphan = ext4_inode_get_deletion_time(current->inode);
1047
1048 bool found;
1049
1050 while (next_orphan != 0) {
1051 if (next_orphan == inode_ref->index) {
1052 next_orphan = ext4_inode_get_deletion_time(inode_ref->inode);
1053 ext4_inode_set_deletion_time(current->inode, next_orphan);
1054 current->dirty = true;
1055 found = true;
1056 break;
1057 }
1058
1059 ext4_filesystem_put_inode_ref(current);
1060
[1ac1ab4]1061 rc = ext4_filesystem_get_inode_ref(inode_ref->fs, next_orphan, &current);
[ebcaff4]1062 if (rc != EOK) {
1063 return rc;
1064 }
1065 next_orphan = ext4_inode_get_deletion_time(current->inode);
1066
1067 }
1068
1069 ext4_inode_set_deletion_time(inode_ref->inode, 0);
1070
1071 rc = ext4_filesystem_put_inode_ref(current);
1072 if (rc != EOK) {
1073 return rc;
1074 }
1075
1076 if (!found) {
1077 return ENOENT;
1078 }
1079
1080 return EOK;
1081}
1082
[6c501f8]1083/**
1084 * @}
1085 */
Note: See TracBrowser for help on using the repository browser.