source: mainline/uspace/lib/ext4/src/filesystem.c@ a49c39c8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a49c39c8 was 7bdedb5, checked in by Maurizio Lombardi <mlombard@…>, 7 years ago

libext4: fix double free in case ext4_filesystem_open() fails.

I hit an assert in free() when opening an invalid ext4 filesystem,
this was due to a bug in ext4_filesystem_open().
In case of error, it called ext4_filesystem_fini() against an
uninitialized filesystem instance.

  • Property mode set to 100644
File size: 52.4 KB
Line 
1/*
2 * Copyright (c) 2018 Jiri Svoboda
3 * Copyright (c) 2011 Martin Sucha
4 * Copyright (c) 2012 Frantisek Princ
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * - The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/** @addtogroup libext4
32 * @{
33 */
34/**
35 * @file filesystem.c
36 * @brief More complex filesystem operations.
37 */
38
39#include <byteorder.h>
40#include <errno.h>
41#include <mem.h>
42#include <align.h>
43#include <crypto.h>
44#include <ipc/vfs.h>
45#include <libfs.h>
46#include <stdlib.h>
47#include "ext4/balloc.h"
48#include "ext4/bitmap.h"
49#include "ext4/block_group.h"
50#include "ext4/cfg.h"
51#include "ext4/directory.h"
52#include "ext4/extent.h"
53#include "ext4/filesystem.h"
54#include "ext4/ialloc.h"
55#include "ext4/inode.h"
56#include "ext4/ops.h"
57#include "ext4/superblock.h"
58
59static errno_t ext4_filesystem_check_features(ext4_filesystem_t *, bool *);
60static errno_t ext4_filesystem_init_block_groups(ext4_filesystem_t *);
61static errno_t ext4_filesystem_alloc_this_inode(ext4_filesystem_t *,
62 uint32_t, ext4_inode_ref_t **, int);
63static uint32_t ext4_filesystem_inodes_per_block(ext4_superblock_t *);
64
65/** Initialize filesystem for opening.
66 *
67 * But do not mark mounted just yet.
68 *
69 * @param fs Filesystem instance to be initialized
70 * @param service_id Block device to open
71 * @param cmode Cache mode
72 *
73 * @return Error code
74 *
75 */
76static errno_t ext4_filesystem_init(ext4_filesystem_t *fs, service_id_t service_id,
77 enum cache_mode cmode)
78{
79 errno_t rc;
80 ext4_superblock_t *temp_superblock = NULL;
81
82 fs->device = service_id;
83
84 /* Initialize block library (4096 is size of communication channel) */
85 rc = block_init(fs->device, 4096);
86 if (rc != EOK)
87 goto err;
88
89 /* Read superblock from device to memory */
90 rc = ext4_superblock_read_direct(fs->device, &temp_superblock);
91 if (rc != EOK)
92 goto err_1;
93
94 /* Read block size from superblock and check */
95 uint32_t block_size = ext4_superblock_get_block_size(temp_superblock);
96 if (block_size > EXT4_MAX_BLOCK_SIZE) {
97 rc = ENOTSUP;
98 goto err_1;
99 }
100
101 /* Initialize block caching by libblock */
102 rc = block_cache_init(service_id, block_size, 0, cmode);
103 if (rc != EOK)
104 goto err_1;
105
106 /* Compute limits for indirect block levels */
107 uint32_t block_ids_per_block = block_size / sizeof(uint32_t);
108 fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
109 fs->inode_blocks_per_level[0] = 1;
110 for (unsigned int i = 1; i < 4; i++) {
111 fs->inode_blocks_per_level[i] = fs->inode_blocks_per_level[i - 1] *
112 block_ids_per_block;
113 fs->inode_block_limits[i] = fs->inode_block_limits[i - 1] +
114 fs->inode_blocks_per_level[i];
115 }
116
117 /* Return loaded superblock */
118 fs->superblock = temp_superblock;
119
120 uint16_t state = ext4_superblock_get_state(fs->superblock);
121
122 if (((state & EXT4_SUPERBLOCK_STATE_VALID_FS) !=
123 EXT4_SUPERBLOCK_STATE_VALID_FS) ||
124 ((state & EXT4_SUPERBLOCK_STATE_ERROR_FS) ==
125 EXT4_SUPERBLOCK_STATE_ERROR_FS)) {
126 rc = ENOTSUP;
127 goto err_2;
128 }
129
130 rc = ext4_superblock_check_sanity(fs->superblock);
131 if (rc != EOK)
132 goto err_2;
133
134 /* Check flags */
135 bool read_only;
136 rc = ext4_filesystem_check_features(fs, &read_only);
137 if (rc != EOK)
138 goto err_2;
139
140 return EOK;
141err_2:
142 block_cache_fini(fs->device);
143err_1:
144 block_fini(fs->device);
145err:
146 if (temp_superblock)
147 ext4_superblock_release(temp_superblock);
148 return rc;
149}
150
151/** Finalize filesystem.
152 *
153 * @param fs Filesystem to be finalized
154 *
155 */
156static void ext4_filesystem_fini(ext4_filesystem_t *fs)
157{
158 /* Release memory space for superblock */
159 free(fs->superblock);
160
161 /* Finish work with block library */
162 block_cache_fini(fs->device);
163 block_fini(fs->device);
164}
165
166/** Create lost+found directory.
167 *
168 * @param fs Filesystem
169 * @return EOK on success or error code
170 */
171static errno_t ext4_filesystem_create_lost_found(ext4_filesystem_t *fs,
172 ext4_inode_ref_t *root_dir_ref)
173{
174 errno_t rc;
175 ext4_inode_ref_t *inode_ref;
176
177 rc = ext4_filesystem_alloc_inode(fs, &inode_ref, L_DIRECTORY);
178 if (rc != EOK)
179 goto error;
180
181 rc = ext4_directory_add_entry(inode_ref, ".", inode_ref);
182 if (rc != EOK)
183 goto error;
184
185 rc = ext4_directory_add_entry(inode_ref, "..", root_dir_ref);
186 if (rc != EOK)
187 goto error;
188
189 rc = ext4_directory_add_entry(root_dir_ref, "lost+found", inode_ref);
190 if (rc != EOK)
191 goto error;
192
193 inode_ref->dirty = true;
194
195 uint16_t nlinks = ext4_inode_get_links_count(inode_ref->inode);
196 ext4_inode_set_links_count(inode_ref->inode, nlinks + 1);
197
198 rc = ext4_filesystem_put_inode_ref(inode_ref);
199 if (rc != EOK)
200 goto error;
201
202error:
203 return rc;
204}
205
206/** Create root directory.
207 *
208 * @param fs Filesystem
209 * @return EOK on success or error code
210 */
211static errno_t ext4_filesystem_create_root_dir(ext4_filesystem_t *fs)
212{
213 errno_t rc;
214 ext4_inode_ref_t *inode_ref;
215
216 rc = ext4_filesystem_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX,
217 &inode_ref);
218 if (rc != EOK)
219 goto error;
220
221 inode_ref->dirty = true;
222
223 rc = ext4_directory_add_entry(inode_ref, ".", inode_ref);
224 if (rc != EOK)
225 goto error;
226
227 rc = ext4_directory_add_entry(inode_ref, "..", inode_ref);
228 if (rc != EOK)
229 goto error;
230
231 uint16_t nlinks = ext4_inode_get_links_count(inode_ref->inode);
232 ext4_inode_set_links_count(inode_ref->inode, nlinks + 1);
233
234 rc = ext4_filesystem_create_lost_found(fs, inode_ref);
235 if (rc != EOK)
236 goto error;
237
238 nlinks = ext4_inode_get_links_count(inode_ref->inode);
239 ext4_inode_set_links_count(inode_ref->inode, nlinks + 1);
240
241 rc = ext4_filesystem_put_inode_ref(inode_ref);
242 if (rc != EOK)
243 goto error;
244
245error:
246 return rc;
247}
248
249/** Create new filesystem.
250 *
251 * @param cfg Configuration of new file system
252 * @param service_id Block device where to create new file system
253 */
254errno_t ext4_filesystem_create(ext4_cfg_t *cfg, service_id_t service_id)
255{
256 errno_t rc;
257 ext4_superblock_t *superblock = NULL;
258 ext4_filesystem_t *fs = NULL;
259 size_t dev_bsize;
260 aoff64_t dev_nblocks;
261 ext4_inode_ref_t *inode_ref = NULL;
262 bool block_inited = false;
263 bool fs_inited = false;
264 uint32_t idx;
265
266 /* Initialize block library (4096 is size of communication channel) */
267 rc = block_init(service_id, 4096);
268 if (rc != EOK)
269 goto err;
270
271 block_inited = true;
272
273 /* Get device block size */
274 rc = block_get_bsize(service_id, &dev_bsize);
275 if (rc != EOK)
276 goto err;
277
278 /* Get device number of blocks */
279 rc = block_get_nblocks(service_id, &dev_nblocks);
280 if (rc != EOK)
281 goto err;
282
283 /* Create superblock */
284 rc = ext4_superblock_create(dev_bsize, dev_nblocks, cfg, &superblock);
285 if (rc != EOK)
286 goto err;
287
288 /* Write superblock to device */
289 rc = ext4_superblock_write_direct(service_id, superblock);
290 if (rc != EOK)
291 goto err;
292
293 block_fini(service_id);
294 block_inited = false;
295 ext4_superblock_release(superblock);
296 superblock = NULL;
297
298 fs = calloc(1, sizeof(ext4_filesystem_t));
299 if (fs == NULL)
300 goto err;
301
302 /* Open file system */
303 rc = ext4_filesystem_init(fs, service_id, CACHE_MODE_WT);
304 if (rc != EOK)
305 goto err;
306
307 fs_inited = true;
308
309 /* Init block groups */
310 rc = ext4_filesystem_init_block_groups(fs);
311 if (rc != EOK)
312 goto err;
313
314 /* Reserved i-nodes */
315 for (idx = 1; idx < EXT4_REV0_FIRST_INO; idx++) {
316 if (idx == EXT4_INODE_ROOT_INDEX) {
317 rc = ext4_filesystem_alloc_this_inode(fs, idx,
318 &inode_ref, L_DIRECTORY);
319 if (rc != EOK)
320 goto error;
321
322 rc = ext4_filesystem_put_inode_ref(inode_ref);
323 if (rc != EOK)
324 goto error;
325 } else {
326 /* Allocate inode by allocation algorithm */
327 errno_t rc = ext4_ialloc_alloc_this_inode(fs, idx,
328 false);
329 if (rc != EOK)
330 return rc;
331
332 rc = ext4_filesystem_get_inode_ref(fs, idx,
333 &inode_ref);
334 if (rc != EOK)
335 goto error;
336
337 memset(inode_ref->inode, 0, ext4_superblock_get_inode_size(fs->superblock));
338 inode_ref->dirty = true;
339
340 rc = ext4_filesystem_put_inode_ref(inode_ref);
341 if (rc != EOK)
342 goto error;
343 }
344 }
345
346 /* Create root directory */
347 rc = ext4_filesystem_create_root_dir(fs);
348 if (rc != EOK)
349 goto err;
350
351 /* Write superblock to device */
352 rc = ext4_superblock_write_direct(service_id, fs->superblock);
353 if (rc != EOK)
354 goto err;
355
356 ext4_filesystem_fini(fs);
357 free(fs);
358 return EOK;
359err:
360 if (fs_inited)
361 ext4_filesystem_fini(fs);
362 if (fs != NULL)
363 free(fs);
364 if (superblock != NULL)
365 ext4_superblock_release(superblock);
366 if (block_inited)
367 block_fini(service_id);
368 return rc;
369error:
370 return rc;
371}
372
373/** Probe filesystem.
374 *
375 * @param service_id Block device to probe
376 * @param info Place to store probe information
377 *
378 * @return EOK or an error code.
379 *
380 */
381errno_t ext4_filesystem_probe(service_id_t service_id,
382 ext4_fs_probe_info_t *info)
383{
384 ext4_filesystem_t *fs = NULL;
385 errno_t rc;
386
387 fs = calloc(1, sizeof(ext4_filesystem_t));
388 if (fs == NULL)
389 return ENOMEM;
390
391 /* Initialize the file system for opening */
392 rc = ext4_filesystem_init(fs, service_id, CACHE_MODE_WT);
393 if (rc != EOK) {
394 free(fs);
395 return rc;
396 }
397
398 rc = ext4_superblock_get_volume_name(fs->superblock, info->vol_name,
399 sizeof(info->vol_name));
400 if (rc != EOK) {
401 ext4_filesystem_fini(fs);
402 return rc;
403 }
404
405 ext4_filesystem_fini(fs);
406 return EOK;
407}
408
409/** Open filesystem and read all needed data.
410 *
411 * @param fs Filesystem to be initialized
412 * @param inst Instance
413 * @param service_id Identifier if device with the filesystem
414 * @param cmode Cache mode
415 * @param size Output value - size of root node
416 *
417 * @return Error code
418 *
419 */
420errno_t ext4_filesystem_open(ext4_instance_t *inst, service_id_t service_id,
421 enum cache_mode cmode, aoff64_t *size, ext4_filesystem_t **rfs)
422{
423 int fs_inited = 0;
424 ext4_filesystem_t *fs = NULL;
425 fs_node_t *root_node = NULL;
426 errno_t rc;
427
428 fs = calloc(1, sizeof(ext4_filesystem_t));
429 if (fs == NULL) {
430 rc = ENOMEM;
431 goto error;
432 }
433
434 inst->filesystem = fs;
435
436 /* Initialize the file system for opening */
437 rc = ext4_filesystem_init(fs, service_id, cmode);
438 if (rc != EOK)
439 goto error;
440
441 fs_inited = 1;
442
443 /* Read root node */
444 rc = ext4_node_get_core(&root_node, inst, EXT4_INODE_ROOT_INDEX);
445 if (rc != EOK)
446 goto error;
447
448 /* Mark system as mounted */
449 ext4_superblock_set_state(fs->superblock, EXT4_SUPERBLOCK_STATE_ERROR_FS);
450 rc = ext4_superblock_write_direct(fs->device, fs->superblock);
451 if (rc != EOK)
452 goto error;
453
454 uint16_t mnt_count = ext4_superblock_get_mount_count(fs->superblock);
455 ext4_superblock_set_mount_count(fs->superblock, mnt_count + 1);
456
457 ext4_node_t *enode = EXT4_NODE(root_node);
458
459 *size = ext4_inode_get_size(fs->superblock, enode->inode_ref->inode);
460
461 ext4_node_put(root_node);
462 *rfs = fs;
463 return EOK;
464error:
465 if (root_node != NULL)
466 ext4_node_put(root_node);
467
468 if (fs_inited)
469 ext4_filesystem_fini(fs);
470 free(fs);
471 return rc;
472}
473
474/** Close filesystem.
475 *
476 * @param fs Filesystem to be destroyed
477 *
478 * @return EOK or an error code. On error the state of the file
479 * system is unchanged.
480 *
481 */
482errno_t ext4_filesystem_close(ext4_filesystem_t *fs)
483{
484 /* Write the superblock to the device */
485 ext4_superblock_set_state(fs->superblock, EXT4_SUPERBLOCK_STATE_VALID_FS);
486 errno_t rc = ext4_superblock_write_direct(fs->device, fs->superblock);
487 if (rc != EOK)
488 return rc;
489
490 ext4_filesystem_fini(fs);
491 return EOK;
492}
493
494/** Check filesystem's features, if supported by this driver
495 *
496 * Function can return EOK and set read_only flag. It mean's that
497 * there are some not-supported features, that can cause problems
498 * during some write operations.
499 *
500 * @param fs Filesystem to be checked
501 * @param read_only Place to write flag saying whether filesystem
502 * should be mounted only for reading
503 *
504 * @return Error code
505 *
506 */
507static errno_t ext4_filesystem_check_features(ext4_filesystem_t *fs,
508 bool *read_only)
509{
510 /* Feature flags are present only in higher revisions */
511 if (ext4_superblock_get_rev_level(fs->superblock) == 0) {
512 *read_only = false;
513 return EOK;
514 }
515
516 /*
517 * Check incompatible features - if filesystem has some,
518 * volume can't be mounted
519 */
520 uint32_t incompatible_features;
521 incompatible_features =
522 ext4_superblock_get_features_incompatible(fs->superblock);
523 incompatible_features &= ~EXT4_FEATURE_INCOMPAT_SUPP;
524 if (incompatible_features > 0)
525 return ENOTSUP;
526
527 /*
528 * Check read-only features, if filesystem has some,
529 * volume can be mount only in read-only mode
530 */
531 uint32_t compatible_read_only;
532 compatible_read_only =
533 ext4_superblock_get_features_read_only(fs->superblock);
534 compatible_read_only &= ~EXT4_FEATURE_RO_COMPAT_SUPP;
535 if (compatible_read_only > 0) {
536 *read_only = true;
537 return EOK;
538 }
539
540 return EOK;
541}
542
543/** Convert block address to relative index in block group.
544 *
545 * @param sb Superblock pointer
546 * @param block_addr Block number to convert
547 *
548 * @return Relative number of block
549 *
550 */
551uint32_t ext4_filesystem_blockaddr2_index_in_group(ext4_superblock_t *sb,
552 uint32_t block_addr)
553{
554 uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
555 uint32_t first_block = ext4_superblock_get_first_data_block(sb);
556
557 /* First block == 0 or 1 */
558 if (first_block == 0)
559 return block_addr % blocks_per_group;
560 else
561 return (block_addr - 1) % blocks_per_group;
562}
563
564/** Convert relative block address in group to absolute address.
565 *
566 * @param sb Superblock pointer
567 *
568 * @return Absolute block address
569 *
570 */
571uint32_t ext4_filesystem_index_in_group2blockaddr(ext4_superblock_t *sb,
572 uint32_t index, uint32_t bgid)
573{
574 uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
575
576 if (ext4_superblock_get_first_data_block(sb) == 0)
577 return bgid * blocks_per_group + index;
578 else
579 return bgid * blocks_per_group + index + 1;
580}
581
582/** Convert the absolute block number to group number
583 *
584 * @param sb Pointer to the superblock
585 * @param b Absolute block number
586 *
587 * @return Group number
588 */
589uint32_t ext4_filesystem_blockaddr2group(ext4_superblock_t *sb, uint64_t b)
590{
591 uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
592 uint32_t first_block = ext4_superblock_get_first_data_block(sb);
593
594 return (b - first_block) / blocks_per_group;
595}
596
597/** Initialize block group structures
598 */
599static errno_t ext4_filesystem_init_block_groups(ext4_filesystem_t *fs)
600{
601 errno_t rc;
602 block_t *block;
603 aoff64_t b;
604 ext4_block_group_t *bg;
605 ext4_superblock_t *sb = fs->superblock;
606 ext4_block_group_ref_t *bg_ref;
607
608 uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
609 uint32_t block_size = ext4_superblock_get_block_size(sb);
610 uint32_t desc_size = ext4_superblock_get_desc_size(fs->superblock);
611 /* Number of descriptors per block */
612 uint32_t descriptors_per_block =
613 ext4_superblock_get_block_size(fs->superblock) / desc_size;
614 /* Block where block group descriptor (and first block group) starts */
615 aoff64_t block_id =
616 ext4_superblock_get_first_data_block(fs->superblock) + 1;
617 /* Number of blocks containing descriptor table */
618 uint32_t dtable_blocks =
619 (block_group_count + descriptors_per_block - 1) /
620 descriptors_per_block;
621
622 uint32_t bg_index;
623 aoff64_t bg_block0;
624 uint32_t dcnt;
625 uint32_t i;
626 uint32_t now;
627
628 aoff64_t block_bitmap;
629 aoff64_t inode_bitmap;
630 aoff64_t inode_table;
631 uint32_t free_blocks;
632 uint32_t free_inodes;
633 uint32_t used_dirs;
634 uint32_t reserved;
635 uint32_t inode_table_blocks;
636
637 dcnt = block_group_count;
638
639 /* Fill in block descriptors */
640 b = block_id;
641 bg_index = 0;
642 bg_block0 = block_id;
643 while (dcnt > 0) {
644 rc = block_get(&block, fs->device, b, BLOCK_FLAGS_NOREAD);
645 if (rc != EOK)
646 return rc;
647
648 if (dcnt > descriptors_per_block)
649 now = descriptors_per_block;
650 else
651 now = dcnt;
652
653 memset(block->data, 0, block_size);
654
655 for (i = 0; i < now; i++) {
656 bg = (ext4_block_group_t *) (block->data + i *
657 desc_size);
658
659 block_bitmap = bg_block0 + dtable_blocks;
660 inode_bitmap = block_bitmap + 1;
661 inode_table = inode_bitmap + 1;
662
663 free_blocks = ext4_superblock_get_blocks_in_group(sb,
664 bg_index);
665
666 free_inodes =
667 ext4_filesystem_bg_get_itable_size(sb, bg_index) *
668 ext4_filesystem_inodes_per_block(sb);
669 used_dirs = 0;
670
671 ext4_block_group_set_block_bitmap(bg, sb, block_bitmap);
672 ext4_block_group_set_inode_bitmap(bg, sb, inode_bitmap);
673 ext4_block_group_set_inode_table_first_block(bg, sb,
674 inode_table);
675 ext4_block_group_set_free_blocks_count(bg, sb,
676 free_blocks);
677 ext4_block_group_set_free_inodes_count(bg, sb,
678 free_inodes);
679 ext4_block_group_set_used_dirs_count(bg, sb,
680 used_dirs);
681
682 /// XX Lazy
683 ext4_block_group_set_flag(bg,
684 EXT4_BLOCK_GROUP_BLOCK_UNINIT);
685 ext4_block_group_set_flag(bg,
686 EXT4_BLOCK_GROUP_INODE_UNINIT);
687
688 bg_index++;
689 bg_block0 += ext4_superblock_get_blocks_per_group(sb);
690 }
691
692 block->dirty = true;
693
694 rc = block_put(block);
695 if (rc != EOK)
696 return rc;
697
698 ++b;
699 dcnt -= now;
700 }
701
702 /* This initializes the bitmaps and inode table */
703 for (bg_index = 0; bg_index < block_group_count; bg_index++) {
704 rc = ext4_filesystem_get_block_group_ref(fs, bg_index, &bg_ref);
705 if (rc != EOK)
706 return rc;
707
708 /*
709 * Adjust number of free blocks
710 */
711 free_blocks = ext4_superblock_get_blocks_in_group(sb, bg_index);
712 reserved = ext4_filesystem_bg_get_backup_blocks(bg_ref);
713 inode_table_blocks = ext4_filesystem_bg_get_itable_size(sb,
714 bg_ref->index);
715 /* One for block bitmap one for inode bitmap */
716 free_blocks = free_blocks - reserved - 2 - inode_table_blocks;
717
718 ext4_block_group_set_free_blocks_count(bg_ref->block_group,
719 sb, free_blocks);
720 bg_ref->dirty = true;
721
722 rc = ext4_filesystem_put_block_group_ref(bg_ref);
723 if (rc != EOK)
724 return rc;
725 }
726
727 return EOK;
728}
729
730/** Initialize block bitmap in block group.
731 *
732 * @param bg_ref Reference to block group
733 *
734 * @return Error code
735 *
736 */
737static errno_t ext4_filesystem_init_block_bitmap(ext4_block_group_ref_t *bg_ref)
738{
739 uint64_t itb;
740 uint32_t sz;
741 uint32_t i;
742
743 /* Load bitmap */
744 ext4_superblock_t *sb = bg_ref->fs->superblock;
745 uint64_t bitmap_block_addr = ext4_block_group_get_block_bitmap(
746 bg_ref->block_group, bg_ref->fs->superblock);
747 uint64_t bitmap_inode_addr = ext4_block_group_get_inode_bitmap(
748 bg_ref->block_group, bg_ref->fs->superblock);
749 uint32_t blocks_group = ext4_superblock_get_blocks_per_group(sb);
750 uint32_t bg_blocks = ext4_superblock_get_blocks_in_group(sb,
751 bg_ref->index);
752
753 block_t *bitmap_block;
754 errno_t rc = block_get(&bitmap_block, bg_ref->fs->device,
755 bitmap_block_addr, BLOCK_FLAGS_NOREAD);
756 if (rc != EOK)
757 return rc;
758
759 uint8_t *bitmap = bitmap_block->data;
760
761 /* Initialize all bitmap bits to zero */
762 uint32_t block_size = ext4_superblock_get_block_size(sb);
763 memset(bitmap, 0, block_size);
764
765 /* Determine the number of reserved blocks in the group */
766 uint32_t reserved_cnt = ext4_filesystem_bg_get_backup_blocks(bg_ref);
767
768 /* Set bits from to first block to first data block - 1 to one (allocated) */
769 for (uint32_t block = 0; block < reserved_cnt; ++block)
770 ext4_bitmap_set_bit(bitmap, block);
771
772 uint32_t bitmap_block_gid = ext4_filesystem_blockaddr2group(sb,
773 bitmap_block_addr);
774 if (bitmap_block_gid == bg_ref->index) {
775 ext4_bitmap_set_bit(bitmap,
776 ext4_filesystem_blockaddr2_index_in_group(sb, bitmap_block_addr));
777 }
778
779 uint32_t bitmap_inode_gid = ext4_filesystem_blockaddr2group(sb,
780 bitmap_inode_addr);
781 if (bitmap_inode_gid == bg_ref->index) {
782 ext4_bitmap_set_bit(bitmap,
783 ext4_filesystem_blockaddr2_index_in_group(sb, bitmap_inode_addr));
784 }
785
786 itb = ext4_block_group_get_inode_table_first_block(bg_ref->block_group,
787 sb);
788 sz = ext4_filesystem_bg_get_itable_size(sb, bg_ref->index);
789
790 for (i = 0; i < sz; ++i, ++itb) {
791 uint32_t gid = ext4_filesystem_blockaddr2group(sb, itb);
792 if (gid == bg_ref->index) {
793 ext4_bitmap_set_bit(bitmap,
794 ext4_filesystem_blockaddr2_index_in_group(sb, itb));
795 }
796 }
797
798 /* For last group need to mark blocks which are outside of the FS */
799 for (uint32_t block = bg_blocks; block < blocks_group; block++) {
800 ext4_bitmap_set_bit(bitmap, block);
801 }
802
803 bitmap_block->dirty = true;
804
805 /* Save bitmap */
806 return block_put(bitmap_block);
807}
808
809/** Initialize i-node bitmap in block group.
810 *
811 * @param bg_ref Reference to block group
812 *
813 * @return Error code
814 *
815 */
816static errno_t ext4_filesystem_init_inode_bitmap(ext4_block_group_ref_t *bg_ref)
817{
818 /* Load bitmap */
819 uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
820 bg_ref->block_group, bg_ref->fs->superblock);
821 block_t *bitmap_block;
822
823 errno_t rc = block_get(&bitmap_block, bg_ref->fs->device,
824 bitmap_block_addr, BLOCK_FLAGS_NOREAD);
825 if (rc != EOK)
826 return rc;
827
828 uint8_t *bitmap = bitmap_block->data;
829
830 /* Initialize all bitmap bits to zero */
831 uint32_t block_size = ext4_superblock_get_block_size(bg_ref->fs->superblock);
832 uint32_t inodes_per_group =
833 ext4_superblock_get_inodes_per_group(bg_ref->fs->superblock);
834 memset(bitmap, 0, (inodes_per_group + 7) / 8);
835
836 uint32_t start_bit = inodes_per_group;
837 uint32_t end_bit = block_size * 8;
838
839 uint32_t i;
840 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
841 ext4_bitmap_set_bit(bitmap, i);
842
843 if (i < end_bit)
844 memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
845
846 bitmap_block->dirty = true;
847
848 /* Save bitmap */
849 return block_put(bitmap_block);
850}
851
852/** Initialize i-node table in block group.
853 *
854 * @param bg_ref Reference to block group
855 *
856 * @return Error code
857 *
858 */
859static errno_t ext4_filesystem_init_inode_table(ext4_block_group_ref_t *bg_ref)
860{
861 ext4_superblock_t *sb = bg_ref->fs->superblock;
862
863 uint32_t block_size = ext4_superblock_get_block_size(sb);
864 uint32_t inodes_per_block = ext4_filesystem_inodes_per_block(sb);
865
866 uint32_t inodes_in_group =
867 ext4_superblock_get_inodes_in_group(sb, bg_ref->index);
868
869 uint32_t table_blocks = inodes_in_group / inodes_per_block;
870
871 if (inodes_in_group % inodes_per_block)
872 table_blocks++;
873
874 /* Compute initialization bounds */
875 uint32_t first_block = ext4_block_group_get_inode_table_first_block(
876 bg_ref->block_group, sb);
877
878 uint32_t last_block = first_block + table_blocks - 1;
879
880 /* Initialization of all itable blocks */
881 for (uint32_t fblock = first_block; fblock <= last_block; ++fblock) {
882 block_t *block;
883 errno_t rc = block_get(&block, bg_ref->fs->device, fblock,
884 BLOCK_FLAGS_NOREAD);
885 if (rc != EOK)
886 return rc;
887
888 memset(block->data, 0, block_size);
889 block->dirty = true;
890
891 rc = block_put(block);
892 if (rc != EOK)
893 return rc;
894 }
895
896 return EOK;
897}
898
899/** Get reference to block group specified by index.
900 *
901 * @param fs Filesystem to find block group on
902 * @param bgid Index of block group to load
903 * @param ref Output pointer for reference
904 *
905 * @return Error code
906 *
907 */
908errno_t ext4_filesystem_get_block_group_ref(ext4_filesystem_t *fs, uint32_t bgid,
909 ext4_block_group_ref_t **ref)
910{
911 /* Allocate memory for new structure */
912 ext4_block_group_ref_t *newref =
913 malloc(sizeof(ext4_block_group_ref_t));
914 if (newref == NULL)
915 return ENOMEM;
916
917 /* Compute number of descriptors, that fits in one data block */
918 uint32_t descriptors_per_block =
919 ext4_superblock_get_block_size(fs->superblock) /
920 ext4_superblock_get_desc_size(fs->superblock);
921
922 /* Block group descriptor table starts at the next block after superblock */
923 aoff64_t block_id =
924 ext4_superblock_get_first_data_block(fs->superblock) + 1;
925
926 /* Find the block containing the descriptor we are looking for */
927 block_id += bgid / descriptors_per_block;
928 uint32_t offset = (bgid % descriptors_per_block) *
929 ext4_superblock_get_desc_size(fs->superblock);
930
931 /* Load block with descriptors */
932 errno_t rc = block_get(&newref->block, fs->device, block_id, 0);
933 if (rc != EOK) {
934 free(newref);
935 return rc;
936 }
937
938 /* Initialize in-memory representation */
939 newref->block_group = newref->block->data + offset;
940 newref->fs = fs;
941 newref->index = bgid;
942 newref->dirty = false;
943
944 *ref = newref;
945
946 if (ext4_block_group_has_flag(newref->block_group,
947 EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
948 rc = ext4_filesystem_init_block_bitmap(newref);
949 if (rc != EOK) {
950 block_put(newref->block);
951 free(newref);
952 return rc;
953 }
954
955 ext4_block_group_clear_flag(newref->block_group,
956 EXT4_BLOCK_GROUP_BLOCK_UNINIT);
957
958 newref->dirty = true;
959 }
960
961 if (ext4_block_group_has_flag(newref->block_group,
962 EXT4_BLOCK_GROUP_INODE_UNINIT)) {
963 rc = ext4_filesystem_init_inode_bitmap(newref);
964 if (rc != EOK) {
965 block_put(newref->block);
966 free(newref);
967 return rc;
968 }
969
970 ext4_block_group_clear_flag(newref->block_group,
971 EXT4_BLOCK_GROUP_INODE_UNINIT);
972
973 if (!ext4_block_group_has_flag(newref->block_group,
974 EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
975 rc = ext4_filesystem_init_inode_table(newref);
976 if (rc != EOK) {
977 block_put(newref->block);
978 free(newref);
979 return rc;
980 }
981
982 ext4_block_group_set_flag(newref->block_group,
983 EXT4_BLOCK_GROUP_ITABLE_ZEROED);
984 }
985
986 newref->dirty = true;
987 }
988
989 return EOK;
990}
991
992/** Compute checksum of block group descriptor.
993 *
994 * @param sb Superblock
995 * @param bgid Index of block group in the filesystem
996 * @param bg Block group to compute checksum for
997 *
998 * @return Checksum value
999 *
1000 */
1001static uint16_t ext4_filesystem_bg_checksum(ext4_superblock_t *sb, uint32_t bgid,
1002 ext4_block_group_t *bg)
1003{
1004 /* If checksum not supported, 0 will be returned */
1005 uint16_t crc = 0;
1006
1007 /* Compute the checksum only if the filesystem supports it */
1008 if (ext4_superblock_has_feature_read_only(sb,
1009 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1010 void *base = bg;
1011 void *checksum = &bg->checksum;
1012
1013 uint32_t offset = (uint32_t) (checksum - base);
1014
1015 /* Convert block group index to little endian */
1016 uint32_t le_group = host2uint32_t_le(bgid);
1017
1018 /* Initialization */
1019 crc = crc16_ibm(~0, sb->uuid, sizeof(sb->uuid));
1020
1021 /* Include index of block group */
1022 crc = crc16_ibm(crc, (uint8_t *) &le_group, sizeof(le_group));
1023
1024 /* Compute crc from the first part (stop before checksum field) */
1025 crc = crc16_ibm(crc, (uint8_t *) bg, offset);
1026
1027 /* Skip checksum */
1028 offset += sizeof(bg->checksum);
1029
1030 /* Checksum of the rest of block group descriptor */
1031 if ((ext4_superblock_has_feature_incompatible(sb,
1032 EXT4_FEATURE_INCOMPAT_64BIT)) &&
1033 (offset < ext4_superblock_get_desc_size(sb)))
1034 crc = crc16_ibm(crc, ((uint8_t *) bg) + offset,
1035 ext4_superblock_get_desc_size(sb) - offset);
1036 }
1037
1038 return crc;
1039}
1040
1041/** Get the size of the block group's inode table
1042 *
1043 * @param sb Pointer to the superblock
1044 * @param bg_index Block group index
1045 *
1046 * @return Size of the inode table in blocks.
1047 */
1048uint32_t ext4_filesystem_bg_get_itable_size(ext4_superblock_t *sb,
1049 uint32_t bg_index)
1050{
1051 uint32_t itable_size;
1052 uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
1053 uint16_t inode_table_item_size = ext4_superblock_get_inode_size(sb);
1054 uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
1055 uint32_t block_size = ext4_superblock_get_block_size(sb);
1056
1057 if (bg_index < block_group_count - 1) {
1058 itable_size = inodes_per_group * inode_table_item_size;
1059 } else {
1060 /* Last block group could be smaller */
1061 uint32_t inodes_count_total = ext4_superblock_get_inodes_count(sb);
1062 itable_size =
1063 (inodes_count_total - ((block_group_count - 1) * inodes_per_group)) *
1064 inode_table_item_size;
1065 }
1066
1067 return ROUND_UP(itable_size, block_size) / block_size;
1068}
1069
1070/** Get the number of blocks used by superblock + gdt + reserved gdt backups
1071 *
1072 * @param bg Pointer to block group
1073 *
1074 * @return Number of blocks
1075 */
1076uint32_t ext4_filesystem_bg_get_backup_blocks(ext4_block_group_ref_t *bg)
1077{
1078 return ext4_superblock_get_group_backup_blocks(bg->fs->superblock,
1079 bg->index);
1080}
1081
1082/** Put reference to block group.
1083 *
1084 * @param ref Pointer for reference to be put back
1085 *
1086 * @return Error code
1087 *
1088 */
1089errno_t ext4_filesystem_put_block_group_ref(ext4_block_group_ref_t *ref)
1090{
1091 /* Check if reference modified */
1092 if (ref->dirty) {
1093 /* Compute new checksum of block group */
1094 uint16_t checksum =
1095 ext4_filesystem_bg_checksum(ref->fs->superblock, ref->index,
1096 ref->block_group);
1097 ext4_block_group_set_checksum(ref->block_group, checksum);
1098
1099 /* Mark block dirty for writing changes to physical device */
1100 ref->block->dirty = true;
1101 }
1102
1103 /* Put back block, that contains block group descriptor */
1104 errno_t rc = block_put(ref->block);
1105 free(ref);
1106
1107 return rc;
1108}
1109
1110/** Get reference to i-node specified by index.
1111 *
1112 * @param fs Filesystem to find i-node on
1113 * @param index Index of i-node to load
1114 * @oaram ref Output pointer for reference
1115 *
1116 * @return Error code
1117 *
1118 */
1119errno_t ext4_filesystem_get_inode_ref(ext4_filesystem_t *fs, uint32_t index,
1120 ext4_inode_ref_t **ref)
1121{
1122 /* Allocate memory for new structure */
1123 ext4_inode_ref_t *newref =
1124 malloc(sizeof(ext4_inode_ref_t));
1125 if (newref == NULL)
1126 return ENOMEM;
1127
1128 /* Compute number of i-nodes, that fits in one data block */
1129 uint32_t inodes_per_group =
1130 ext4_superblock_get_inodes_per_group(fs->superblock);
1131
1132 /*
1133 * Inode numbers are 1-based, but it is simpler to work with 0-based
1134 * when computing indices
1135 */
1136 index -= 1;
1137 uint32_t block_group = index / inodes_per_group;
1138 uint32_t offset_in_group = index % inodes_per_group;
1139
1140 /* Load block group, where i-node is located */
1141 ext4_block_group_ref_t *bg_ref;
1142 errno_t rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
1143 if (rc != EOK) {
1144 free(newref);
1145 return rc;
1146 }
1147
1148 /* Load block address, where i-node table is located */
1149 uint32_t inode_table_start =
1150 ext4_block_group_get_inode_table_first_block(bg_ref->block_group,
1151 fs->superblock);
1152
1153 /* Put back block group reference (not needed more) */
1154 rc = ext4_filesystem_put_block_group_ref(bg_ref);
1155 if (rc != EOK) {
1156 free(newref);
1157 return rc;
1158 }
1159
1160 /* Compute position of i-node in the block group */
1161 uint16_t inode_size = ext4_superblock_get_inode_size(fs->superblock);
1162 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
1163 uint32_t byte_offset_in_group = offset_in_group * inode_size;
1164
1165 /* Compute block address */
1166 aoff64_t block_id = inode_table_start + (byte_offset_in_group / block_size);
1167 rc = block_get(&newref->block, fs->device, block_id, 0);
1168 if (rc != EOK) {
1169 free(newref);
1170 return rc;
1171 }
1172
1173 /* Compute position of i-node in the data block */
1174 uint32_t offset_in_block = byte_offset_in_group % block_size;
1175 newref->inode = newref->block->data + offset_in_block;
1176
1177 /* We need to store the original value of index in the reference */
1178 newref->index = index + 1;
1179 newref->fs = fs;
1180 newref->dirty = false;
1181
1182 *ref = newref;
1183
1184 return EOK;
1185}
1186
1187/** Put reference to i-node.
1188 *
1189 * @param ref Pointer for reference to be put back
1190 *
1191 * @return Error code
1192 *
1193 */
1194errno_t ext4_filesystem_put_inode_ref(ext4_inode_ref_t *ref)
1195{
1196 /* Check if reference modified */
1197 if (ref->dirty) {
1198 /* Mark block dirty for writing changes to physical device */
1199 ref->block->dirty = true;
1200 }
1201
1202 /* Put back block, that contains i-node */
1203 errno_t rc = block_put(ref->block);
1204 free(ref);
1205
1206 return rc;
1207}
1208
1209/** Initialize newly allocated i-node in the filesystem.
1210 *
1211 * @param fs Filesystem to initialize i-node on
1212 * @param index I-node index
1213 * @param inode_ref Output pointer to return reference to allocated i-node
1214 * @param flags Flags to be set for newly created i-node
1215 *
1216 * @return Error code
1217 *
1218 */
1219static errno_t ext4_filesystem_init_inode(ext4_filesystem_t *fs, uint32_t index,
1220 ext4_inode_ref_t **inode_ref, int flags)
1221{
1222 /* Check if newly allocated i-node will be a directory */
1223 bool is_dir = false;
1224 if (flags & L_DIRECTORY)
1225 is_dir = true;
1226
1227 /* Load i-node from on-disk i-node table */
1228 errno_t rc = ext4_filesystem_get_inode_ref(fs, index, inode_ref);
1229 if (rc != EOK) {
1230 ext4_ialloc_free_inode(fs, index, is_dir);
1231 return rc;
1232 }
1233
1234 /* Initialize i-node */
1235 ext4_inode_t *inode = (*inode_ref)->inode;
1236
1237 uint16_t mode;
1238 if (is_dir) {
1239 /*
1240 * Default directory permissions to be compatible with other systems
1241 * 0777 (octal) == rwxrwxrwx
1242 */
1243
1244 mode = 0777;
1245 mode |= EXT4_INODE_MODE_DIRECTORY;
1246 ext4_inode_set_mode(fs->superblock, inode, mode);
1247 ext4_inode_set_links_count(inode, 1); /* '.' entry */
1248 } else {
1249 /*
1250 * Default file permissions to be compatible with other systems
1251 * 0666 (octal) == rw-rw-rw-
1252 */
1253
1254 mode = 0666;
1255 mode |= EXT4_INODE_MODE_FILE;
1256 ext4_inode_set_mode(fs->superblock, inode, mode);
1257 ext4_inode_set_links_count(inode, 0);
1258 }
1259
1260 ext4_inode_set_uid(inode, 0);
1261 ext4_inode_set_gid(inode, 0);
1262 ext4_inode_set_size(inode, 0);
1263 ext4_inode_set_access_time(inode, 0);
1264 ext4_inode_set_change_inode_time(inode, 0);
1265 ext4_inode_set_modification_time(inode, 0);
1266 ext4_inode_set_deletion_time(inode, 0);
1267 ext4_inode_set_blocks_count(fs->superblock, inode, 0);
1268 ext4_inode_set_flags(inode, 0);
1269 ext4_inode_set_generation(inode, 0);
1270
1271 /* Reset blocks array */
1272 for (uint32_t i = 0; i < EXT4_INODE_BLOCKS; i++)
1273 inode->blocks[i] = 0;
1274
1275 /* Initialize extents if needed */
1276 if (ext4_superblock_has_feature_incompatible(
1277 fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
1278 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
1279
1280 /* Initialize extent root header */
1281 ext4_extent_header_t *header = ext4_inode_get_extent_header(inode);
1282 ext4_extent_header_set_depth(header, 0);
1283 ext4_extent_header_set_entries_count(header, 0);
1284 ext4_extent_header_set_generation(header, 0);
1285 ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
1286
1287 uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) -
1288 sizeof(ext4_extent_header_t)) / sizeof(ext4_extent_t);
1289
1290 ext4_extent_header_set_max_entries_count(header, max_entries);
1291 }
1292
1293 (*inode_ref)->dirty = true;
1294
1295 return EOK;
1296}
1297
1298/** Allocate new i-node in the filesystem.
1299 *
1300 * @param fs Filesystem to allocated i-node on
1301 * @param inode_ref Output pointer to return reference to allocated i-node
1302 * @param flags Flags to be set for newly created i-node
1303 *
1304 * @return Error code
1305 *
1306 */
1307errno_t ext4_filesystem_alloc_inode(ext4_filesystem_t *fs,
1308 ext4_inode_ref_t **inode_ref, int flags)
1309{
1310 /* Check if newly allocated i-node will be a directory */
1311 bool is_dir = false;
1312 if (flags & L_DIRECTORY)
1313 is_dir = true;
1314
1315 /* Allocate inode by allocation algorithm */
1316 uint32_t index;
1317 errno_t rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
1318 if (rc != EOK)
1319 return rc;
1320
1321 rc = ext4_filesystem_init_inode(fs, index, inode_ref, flags);
1322 if (rc != EOK) {
1323 ext4_ialloc_free_inode(fs, index, is_dir);
1324 return rc;
1325 }
1326
1327 return EOK;
1328}
1329
1330/** Allocate specific i-node in the filesystem.
1331 *
1332 * @param fs Filesystem to allocated i-node on
1333 * @param index Index of i-node to allocate
1334 * @param inode_ref Output pointer to return reference to allocated i-node
1335 * @param flags Flags to be set for newly created i-node
1336 *
1337 * @return Error code
1338 *
1339 */
1340static errno_t ext4_filesystem_alloc_this_inode(ext4_filesystem_t *fs,
1341 uint32_t index, ext4_inode_ref_t **inode_ref, int flags)
1342{
1343 /* Check if newly allocated i-node will be a directory */
1344 bool is_dir = false;
1345 if (flags & L_DIRECTORY)
1346 is_dir = true;
1347
1348 /* Allocate inode by allocation algorithm */
1349 errno_t rc = ext4_ialloc_alloc_this_inode(fs, index, is_dir);
1350 if (rc != EOK)
1351 return rc;
1352
1353 rc = ext4_filesystem_init_inode(fs, index, inode_ref, flags);
1354 if (rc != EOK) {
1355 ext4_ialloc_free_inode(fs, index, is_dir);
1356 return rc;
1357 }
1358
1359 return EOK;
1360}
1361
1362/** Release i-node and mark it as free.
1363 *
1364 * @param inode_ref I-node to be released
1365 *
1366 * @return Error code
1367 *
1368 */
1369errno_t ext4_filesystem_free_inode(ext4_inode_ref_t *inode_ref)
1370{
1371 ext4_filesystem_t *fs = inode_ref->fs;
1372
1373 /* For extents must be data block destroyed by other way */
1374 if ((ext4_superblock_has_feature_incompatible(fs->superblock,
1375 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1376 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1377 /* Data structures are released during truncate operation... */
1378 goto finish;
1379 }
1380
1381 /* Release all indirect (no data) blocks */
1382
1383 /* 1) Single indirect */
1384 uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
1385 if (fblock != 0) {
1386 errno_t rc = ext4_balloc_free_block(inode_ref, fblock);
1387 if (rc != EOK)
1388 return rc;
1389
1390 ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
1391 }
1392
1393 block_t *block;
1394 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
1395 uint32_t count = block_size / sizeof(uint32_t);
1396
1397 /* 2) Double indirect */
1398 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
1399 if (fblock != 0) {
1400 errno_t rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
1401 if (rc != EOK)
1402 return rc;
1403
1404 uint32_t ind_block;
1405 for (uint32_t offset = 0; offset < count; ++offset) {
1406 ind_block = uint32_t_le2host(((uint32_t *) block->data)[offset]);
1407
1408 if (ind_block != 0) {
1409 rc = ext4_balloc_free_block(inode_ref, ind_block);
1410 if (rc != EOK) {
1411 block_put(block);
1412 return rc;
1413 }
1414 }
1415 }
1416
1417 rc = block_put(block);
1418 if (rc != EOK)
1419 return rc;
1420
1421 rc = ext4_balloc_free_block(inode_ref, fblock);
1422 if (rc != EOK)
1423 return rc;
1424
1425 ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
1426 }
1427
1428 /* 3) Tripple indirect */
1429 block_t *subblock;
1430 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
1431 if (fblock != 0) {
1432 errno_t rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
1433 if (rc != EOK)
1434 return rc;
1435
1436 uint32_t ind_block;
1437 for (uint32_t offset = 0; offset < count; ++offset) {
1438 ind_block = uint32_t_le2host(((uint32_t *) block->data)[offset]);
1439
1440 if (ind_block != 0) {
1441 rc = block_get(&subblock, fs->device, ind_block,
1442 BLOCK_FLAGS_NONE);
1443 if (rc != EOK) {
1444 block_put(block);
1445 return rc;
1446 }
1447
1448 uint32_t ind_subblock;
1449 for (uint32_t suboffset = 0; suboffset < count;
1450 ++suboffset) {
1451 ind_subblock = uint32_t_le2host(((uint32_t *)
1452 subblock->data)[suboffset]);
1453
1454 if (ind_subblock != 0) {
1455 rc = ext4_balloc_free_block(inode_ref, ind_subblock);
1456 if (rc != EOK) {
1457 block_put(subblock);
1458 block_put(block);
1459 return rc;
1460 }
1461 }
1462 }
1463
1464 rc = block_put(subblock);
1465 if (rc != EOK) {
1466 block_put(block);
1467 return rc;
1468 }
1469 }
1470
1471 rc = ext4_balloc_free_block(inode_ref, ind_block);
1472 if (rc != EOK) {
1473 block_put(block);
1474 return rc;
1475 }
1476 }
1477
1478 rc = block_put(block);
1479 if (rc != EOK)
1480 return rc;
1481
1482 rc = ext4_balloc_free_block(inode_ref, fblock);
1483 if (rc != EOK)
1484 return rc;
1485
1486 ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
1487 }
1488
1489finish:
1490 /* Mark inode dirty for writing to the physical device */
1491 inode_ref->dirty = true;
1492
1493 /* Free block with extended attributes if present */
1494 uint32_t xattr_block = ext4_inode_get_file_acl(
1495 inode_ref->inode, fs->superblock);
1496 if (xattr_block) {
1497 errno_t rc = ext4_balloc_free_block(inode_ref, xattr_block);
1498 if (rc != EOK)
1499 return rc;
1500
1501 ext4_inode_set_file_acl(inode_ref->inode, fs->superblock, 0);
1502 }
1503
1504 /* Free inode by allocator */
1505 errno_t rc;
1506 if (ext4_inode_is_type(fs->superblock, inode_ref->inode,
1507 EXT4_INODE_MODE_DIRECTORY))
1508 rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
1509 else
1510 rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
1511
1512 return rc;
1513}
1514
1515/** Truncate i-node data blocks.
1516 *
1517 * @param inode_ref I-node to be truncated
1518 * @param new_size New size of inode (must be < current size)
1519 *
1520 * @return Error code
1521 *
1522 */
1523errno_t ext4_filesystem_truncate_inode(ext4_inode_ref_t *inode_ref,
1524 aoff64_t new_size)
1525{
1526 ext4_superblock_t *sb = inode_ref->fs->superblock;
1527
1528 /* Check flags, if i-node can be truncated */
1529 if (!ext4_inode_can_truncate(sb, inode_ref->inode))
1530 return EINVAL;
1531
1532 /* If sizes are equal, nothing has to be done. */
1533 aoff64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
1534 if (old_size == new_size)
1535 return EOK;
1536
1537 /* It's not suppported to make the larger file by truncate operation */
1538 if (old_size < new_size)
1539 return EINVAL;
1540
1541 /* Compute how many blocks will be released */
1542 aoff64_t size_diff = old_size - new_size;
1543 uint32_t block_size = ext4_superblock_get_block_size(sb);
1544 uint32_t diff_blocks_count = size_diff / block_size;
1545 if (size_diff % block_size != 0)
1546 diff_blocks_count++;
1547
1548 uint32_t old_blocks_count = old_size / block_size;
1549 if (old_size % block_size != 0)
1550 old_blocks_count++;
1551
1552 if ((ext4_superblock_has_feature_incompatible(inode_ref->fs->superblock,
1553 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1554 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1555 /* Extents require special operation */
1556 errno_t rc = ext4_extent_release_blocks_from(inode_ref,
1557 old_blocks_count - diff_blocks_count);
1558 if (rc != EOK)
1559 return rc;
1560 } else {
1561 /* Release data blocks from the end of file */
1562
1563 /* Starting from 1 because of logical blocks are numbered from 0 */
1564 for (uint32_t i = 1; i <= diff_blocks_count; ++i) {
1565 errno_t rc = ext4_filesystem_release_inode_block(inode_ref,
1566 old_blocks_count - i);
1567 if (rc != EOK)
1568 return rc;
1569 }
1570 }
1571
1572 /* Update i-node */
1573 ext4_inode_set_size(inode_ref->inode, new_size);
1574 inode_ref->dirty = true;
1575
1576 return EOK;
1577}
1578
1579/** Get physical block address by logical index of the block.
1580 *
1581 * @param inode_ref I-node to read block address from
1582 * @param iblock Logical index of block
1583 * @param fblock Output pointer for return physical block address
1584 *
1585 * @return Error code
1586 *
1587 */
1588errno_t ext4_filesystem_get_inode_data_block_index(ext4_inode_ref_t *inode_ref,
1589 aoff64_t iblock, uint32_t *fblock)
1590{
1591 ext4_filesystem_t *fs = inode_ref->fs;
1592
1593 /* For empty file is situation simple */
1594 if (ext4_inode_get_size(fs->superblock, inode_ref->inode) == 0) {
1595 *fblock = 0;
1596 return EOK;
1597 }
1598
1599 uint32_t current_block;
1600
1601 /* Handle i-node using extents */
1602 if ((ext4_superblock_has_feature_incompatible(fs->superblock,
1603 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1604 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1605 errno_t rc = ext4_extent_find_block(inode_ref, iblock, &current_block);
1606 if (rc != EOK)
1607 return rc;
1608
1609 *fblock = current_block;
1610 return EOK;
1611 }
1612
1613 ext4_inode_t *inode = inode_ref->inode;
1614
1615 /* Direct block are read directly from array in i-node structure */
1616 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1617 current_block = ext4_inode_get_direct_block(inode, (uint32_t) iblock);
1618 *fblock = current_block;
1619 return EOK;
1620 }
1621
1622 /* Determine indirection level of the target block */
1623 unsigned int level = 0;
1624 for (unsigned int i = 1; i < 4; i++) {
1625 if (iblock < fs->inode_block_limits[i]) {
1626 level = i;
1627 break;
1628 }
1629 }
1630
1631 if (level == 0)
1632 return EIO;
1633
1634 /* Compute offsets for the topmost level */
1635 aoff64_t block_offset_in_level =
1636 iblock - fs->inode_block_limits[level - 1];
1637 current_block = ext4_inode_get_indirect_block(inode, level - 1);
1638 uint32_t offset_in_block =
1639 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1640
1641 /* Sparse file */
1642 if (current_block == 0) {
1643 *fblock = 0;
1644 return EOK;
1645 }
1646
1647 block_t *block;
1648
1649 /*
1650 * Navigate through other levels, until we find the block number
1651 * or find null reference meaning we are dealing with sparse file
1652 */
1653 while (level > 0) {
1654 /* Load indirect block */
1655 errno_t rc = block_get(&block, fs->device, current_block, 0);
1656 if (rc != EOK)
1657 return rc;
1658
1659 /* Read block address from indirect block */
1660 current_block =
1661 uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
1662
1663 /* Put back indirect block untouched */
1664 rc = block_put(block);
1665 if (rc != EOK)
1666 return rc;
1667
1668 /* Check for sparse file */
1669 if (current_block == 0) {
1670 *fblock = 0;
1671 return EOK;
1672 }
1673
1674 /* Jump to the next level */
1675 level--;
1676
1677 /* Termination condition - we have address of data block loaded */
1678 if (level == 0)
1679 break;
1680
1681 /* Visit the next level */
1682 block_offset_in_level %= fs->inode_blocks_per_level[level];
1683 offset_in_block =
1684 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1685 }
1686
1687 *fblock = current_block;
1688
1689 return EOK;
1690}
1691
1692/** Set physical block address for the block logical address into the i-node.
1693 *
1694 * @param inode_ref I-node to set block address to
1695 * @param iblock Logical index of block
1696 * @param fblock Physical block address
1697 *
1698 * @return Error code
1699 *
1700 */
1701errno_t ext4_filesystem_set_inode_data_block_index(ext4_inode_ref_t *inode_ref,
1702 aoff64_t iblock, uint32_t fblock)
1703{
1704 ext4_filesystem_t *fs = inode_ref->fs;
1705
1706 /* Handle inode using extents */
1707 if ((ext4_superblock_has_feature_compatible(fs->superblock,
1708 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1709 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1710 /* Not reachable */
1711 return ENOTSUP;
1712 }
1713
1714 /* Handle simple case when we are dealing with direct reference */
1715 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1716 ext4_inode_set_direct_block(inode_ref->inode, (uint32_t) iblock, fblock);
1717 inode_ref->dirty = true;
1718
1719 return EOK;
1720 }
1721
1722 /* Determine the indirection level needed to get the desired block */
1723 unsigned int level = 0;
1724 for (unsigned int i = 1; i < 4; i++) {
1725 if (iblock < fs->inode_block_limits[i]) {
1726 level = i;
1727 break;
1728 }
1729 }
1730
1731 if (level == 0)
1732 return EIO;
1733
1734 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
1735
1736 /* Compute offsets for the topmost level */
1737 aoff64_t block_offset_in_level =
1738 iblock - fs->inode_block_limits[level - 1];
1739 uint32_t current_block =
1740 ext4_inode_get_indirect_block(inode_ref->inode, level - 1);
1741 uint32_t offset_in_block =
1742 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1743
1744 uint32_t new_block_addr;
1745 block_t *block;
1746 block_t *new_block;
1747
1748 /* Is needed to allocate indirect block on the i-node level */
1749 if (current_block == 0) {
1750 /* Allocate new indirect block */
1751 errno_t rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1752 if (rc != EOK)
1753 return rc;
1754
1755 /* Update i-node */
1756 ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
1757 new_block_addr);
1758 inode_ref->dirty = true;
1759
1760 /* Load newly allocated block */
1761 rc = block_get(&new_block, fs->device, new_block_addr,
1762 BLOCK_FLAGS_NOREAD);
1763 if (rc != EOK) {
1764 ext4_balloc_free_block(inode_ref, new_block_addr);
1765 return rc;
1766 }
1767
1768 /* Initialize new block */
1769 memset(new_block->data, 0, block_size);
1770 new_block->dirty = true;
1771
1772 /* Put back the allocated block */
1773 rc = block_put(new_block);
1774 if (rc != EOK)
1775 return rc;
1776
1777 current_block = new_block_addr;
1778 }
1779
1780 /*
1781 * Navigate through other levels, until we find the block number
1782 * or find null reference meaning we are dealing with sparse file
1783 */
1784 while (level > 0) {
1785 errno_t rc = block_get(&block, fs->device, current_block, 0);
1786 if (rc != EOK)
1787 return rc;
1788
1789 current_block =
1790 uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
1791
1792 if ((level > 1) && (current_block == 0)) {
1793 /* Allocate new block */
1794 rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1795 if (rc != EOK) {
1796 block_put(block);
1797 return rc;
1798 }
1799
1800 /* Load newly allocated block */
1801 rc = block_get(&new_block, fs->device, new_block_addr,
1802 BLOCK_FLAGS_NOREAD);
1803 if (rc != EOK) {
1804 block_put(block);
1805 return rc;
1806 }
1807
1808 /* Initialize allocated block */
1809 memset(new_block->data, 0, block_size);
1810 new_block->dirty = true;
1811
1812 rc = block_put(new_block);
1813 if (rc != EOK) {
1814 block_put(block);
1815 return rc;
1816 }
1817
1818 /* Write block address to the parent */
1819 ((uint32_t *) block->data)[offset_in_block] =
1820 host2uint32_t_le(new_block_addr);
1821 block->dirty = true;
1822 current_block = new_block_addr;
1823 }
1824
1825 /* Will be finished, write the fblock address */
1826 if (level == 1) {
1827 ((uint32_t *) block->data)[offset_in_block] =
1828 host2uint32_t_le(fblock);
1829 block->dirty = true;
1830 }
1831
1832 rc = block_put(block);
1833 if (rc != EOK)
1834 return rc;
1835
1836 level--;
1837
1838 /*
1839 * If we are on the last level, break here as
1840 * there is no next level to visit
1841 */
1842 if (level == 0)
1843 break;
1844
1845 /* Visit the next level */
1846 block_offset_in_level %= fs->inode_blocks_per_level[level];
1847 offset_in_block =
1848 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1849 }
1850
1851 return EOK;
1852}
1853
1854/** Release data block from i-node
1855 *
1856 * @param inode_ref I-node to release block from
1857 * @param iblock Logical block to be released
1858 *
1859 * @return Error code
1860 *
1861 */
1862errno_t ext4_filesystem_release_inode_block(ext4_inode_ref_t *inode_ref,
1863 uint32_t iblock)
1864{
1865 uint32_t fblock;
1866
1867 ext4_filesystem_t *fs = inode_ref->fs;
1868
1869 /* Extents are handled otherwise = there is not support in this function */
1870 assert(!(ext4_superblock_has_feature_incompatible(fs->superblock,
1871 EXT4_FEATURE_INCOMPAT_EXTENTS) &&
1872 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
1873
1874 ext4_inode_t *inode = inode_ref->inode;
1875
1876 /* Handle simple case when we are dealing with direct reference */
1877 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1878 fblock = ext4_inode_get_direct_block(inode, iblock);
1879
1880 /* Sparse file */
1881 if (fblock == 0)
1882 return EOK;
1883
1884 ext4_inode_set_direct_block(inode, iblock, 0);
1885 return ext4_balloc_free_block(inode_ref, fblock);
1886 }
1887
1888 /* Determine the indirection level needed to get the desired block */
1889 unsigned int level = 0;
1890 for (unsigned int i = 1; i < 4; i++) {
1891 if (iblock < fs->inode_block_limits[i]) {
1892 level = i;
1893 break;
1894 }
1895 }
1896
1897 if (level == 0)
1898 return EIO;
1899
1900 /* Compute offsets for the topmost level */
1901 aoff64_t block_offset_in_level =
1902 iblock - fs->inode_block_limits[level - 1];
1903 uint32_t current_block =
1904 ext4_inode_get_indirect_block(inode, level - 1);
1905 uint32_t offset_in_block =
1906 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1907
1908 /*
1909 * Navigate through other levels, until we find the block number
1910 * or find null reference meaning we are dealing with sparse file
1911 */
1912 block_t *block;
1913 while (level > 0) {
1914
1915 /* Sparse check */
1916 if (current_block == 0)
1917 return EOK;
1918
1919 errno_t rc = block_get(&block, fs->device, current_block, 0);
1920 if (rc != EOK)
1921 return rc;
1922
1923 current_block =
1924 uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
1925
1926 /* Set zero if physical data block address found */
1927 if (level == 1) {
1928 ((uint32_t *) block->data)[offset_in_block] =
1929 host2uint32_t_le(0);
1930 block->dirty = true;
1931 }
1932
1933 rc = block_put(block);
1934 if (rc != EOK)
1935 return rc;
1936
1937 level--;
1938
1939 /*
1940 * If we are on the last level, break here as
1941 * there is no next level to visit
1942 */
1943 if (level == 0)
1944 break;
1945
1946 /* Visit the next level */
1947 block_offset_in_level %= fs->inode_blocks_per_level[level];
1948 offset_in_block =
1949 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1950 }
1951
1952 fblock = current_block;
1953 if (fblock == 0)
1954 return EOK;
1955
1956 /* Physical block is not referenced, it can be released */
1957 return ext4_balloc_free_block(inode_ref, fblock);
1958}
1959
1960/** Append following logical block to the i-node.
1961 *
1962 * @param inode_ref I-node to append block to
1963 * @param fblock Output physical block address of newly allocated block
1964 * @param iblock Output logical number of newly allocated block
1965 *
1966 * @return Error code
1967 *
1968 */
1969errno_t ext4_filesystem_append_inode_block(ext4_inode_ref_t *inode_ref,
1970 uint32_t *fblock, uint32_t *iblock)
1971{
1972 /* Handle extents separately */
1973 if ((ext4_superblock_has_feature_incompatible(inode_ref->fs->superblock,
1974 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1975 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)))
1976 return ext4_extent_append_block(inode_ref, iblock, fblock, true);
1977
1978 ext4_superblock_t *sb = inode_ref->fs->superblock;
1979
1980 /* Compute next block index and allocate data block */
1981 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1982 uint32_t block_size = ext4_superblock_get_block_size(sb);
1983
1984 /* Align size i-node size */
1985 if ((inode_size % block_size) != 0)
1986 inode_size += block_size - (inode_size % block_size);
1987
1988 /* Logical blocks are numbered from 0 */
1989 uint32_t new_block_idx = inode_size / block_size;
1990
1991 /* Allocate new physical block */
1992 uint32_t phys_block;
1993 errno_t rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
1994 if (rc != EOK)
1995 return rc;
1996
1997 /* Add physical block address to the i-node */
1998 rc = ext4_filesystem_set_inode_data_block_index(inode_ref,
1999 new_block_idx, phys_block);
2000 if (rc != EOK) {
2001 ext4_balloc_free_block(inode_ref, phys_block);
2002 return rc;
2003 }
2004
2005 /* Update i-node */
2006 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
2007 inode_ref->dirty = true;
2008
2009 *fblock = phys_block;
2010 *iblock = new_block_idx;
2011
2012 return EOK;
2013}
2014
2015/** Get the number of inodes per block.
2016 *
2017 * @param sb Superblock
2018 * @return Number of inodes per block
2019 */
2020static uint32_t ext4_filesystem_inodes_per_block(ext4_superblock_t *sb)
2021{
2022 uint32_t inode_size = ext4_superblock_get_inode_size(sb);
2023 uint32_t block_size = ext4_superblock_get_block_size(sb);
2024
2025 return block_size / inode_size;
2026}
2027
2028/**
2029 * @}
2030 */
Note: See TracBrowser for help on using the repository browser.