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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cc74cb5 was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

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