source: mainline/uspace/lib/ext4/libext4_filesystem.c@ 6f413125

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

extents: hotfix with writing to an existing empty file

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