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

Last change on this file since e43d658 was aab85d90, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Basic Ext4 filesystem creation (can only create Ext2-old, 1K blocks).

  • 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
532/** Convert block address to relative index in block group.
533 *
534 * @param sb Superblock pointer
535 * @param block_addr Block number to convert
536 *
537 * @return Relative number of block
538 *
539 */
540uint32_t ext4_filesystem_blockaddr2_index_in_group(ext4_superblock_t *sb,
541 uint32_t block_addr)
542{
543 uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
544 uint32_t first_block = ext4_superblock_get_first_data_block(sb);
545
546 /* First block == 0 or 1 */
547 if (first_block == 0)
548 return block_addr % blocks_per_group;
549 else
550 return (block_addr - 1) % blocks_per_group;
551}
552
553
554/** Convert relative block address in group to absolute address.
555 *
556 * @param sb Superblock pointer
557 *
558 * @return Absolute block address
559 *
560 */
561uint32_t ext4_filesystem_index_in_group2blockaddr(ext4_superblock_t *sb,
562 uint32_t index, uint32_t bgid)
563{
564 uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
565
566 if (ext4_superblock_get_first_data_block(sb) == 0)
567 return bgid * blocks_per_group + index;
568 else
569 return bgid * blocks_per_group + index + 1;
570}
571
572/** Convert the absolute block number to group number
573 *
574 * @param sb Pointer to the superblock
575 * @param b Absolute block number
576 *
577 * @return Group number
578 */
579uint32_t ext4_filesystem_blockaddr2group(ext4_superblock_t *sb, uint64_t b)
580{
581 uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
582 uint32_t first_block = ext4_superblock_get_first_data_block(sb);
583
584 return (b - first_block) / blocks_per_group;
585}
586
587/** Initialize block group structures
588 */
589static errno_t ext4_filesystem_init_block_groups(ext4_filesystem_t *fs)
590{
591 errno_t rc;
592 block_t *block;
593 aoff64_t b;
594 ext4_block_group_t *bg;
595 ext4_superblock_t *sb = fs->superblock;
596 ext4_block_group_ref_t *bg_ref;
597
598 uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
599 uint32_t block_size = ext4_superblock_get_block_size(sb);
600 uint32_t desc_size = ext4_superblock_get_desc_size(fs->superblock);
601 /* Number of descriptors per block */
602 uint32_t descriptors_per_block =
603 ext4_superblock_get_block_size(fs->superblock) / desc_size;
604 /* Block where block group descriptor (and first block group) starts */
605 aoff64_t block_id =
606 ext4_superblock_get_first_data_block(fs->superblock) + 1;
607 /* Number of blocks containing descriptor table */
608 uint32_t dtable_blocks =
609 (block_group_count + descriptors_per_block - 1) /
610 descriptors_per_block;
611
612 uint32_t bg_index;
613 aoff64_t bg_block0;
614 uint32_t dcnt;
615 uint32_t i;
616 uint32_t now;
617
618 aoff64_t block_bitmap;
619 aoff64_t inode_bitmap;
620 aoff64_t inode_table;
621 uint32_t free_blocks;
622 uint32_t free_inodes;
623 uint32_t used_dirs;
624 uint32_t reserved;
625 uint32_t inode_table_blocks;
626
627 dcnt = block_group_count;
628
629 /* Fill in block descriptors */
630 b = block_id;
631 bg_index = 0;
632 bg_block0 = block_id;
633 while (dcnt > 0) {
634 rc = block_get(&block, fs->device, b, BLOCK_FLAGS_NOREAD);
635 if (rc != EOK)
636 return rc;
637
638 if (dcnt > descriptors_per_block)
639 now = descriptors_per_block;
640 else
641 now = dcnt;
642
643 memset(block->data, 0, block_size);
644
645 for (i = 0; i < now; i++) {
646 bg = (ext4_block_group_t *) (block->data + i *
647 desc_size);
648
649 block_bitmap = bg_block0 + dtable_blocks;
650 inode_bitmap = block_bitmap + 1;
651 inode_table = inode_bitmap + 1;
652
653 free_blocks = ext4_superblock_get_blocks_in_group(sb,
654 bg_index);
655
656 free_inodes =
657 ext4_filesystem_bg_get_itable_size(sb, bg_index) *
658 ext4_filesystem_inodes_per_block(sb);
659 used_dirs = 0;
660
661 ext4_block_group_set_block_bitmap(bg, sb, block_bitmap);
662 ext4_block_group_set_inode_bitmap(bg, sb, inode_bitmap);
663 ext4_block_group_set_inode_table_first_block(bg, sb,
664 inode_table);
665 ext4_block_group_set_free_blocks_count(bg, sb,
666 free_blocks);
667 ext4_block_group_set_free_inodes_count(bg, sb,
668 free_inodes);
669 ext4_block_group_set_used_dirs_count(bg, sb,
670 used_dirs);
671
672 /// XX Lazy
673 ext4_block_group_set_flag(bg,
674 EXT4_BLOCK_GROUP_BLOCK_UNINIT);
675 ext4_block_group_set_flag(bg,
676 EXT4_BLOCK_GROUP_INODE_UNINIT);
677
678 bg_index++;
679 bg_block0 += ext4_superblock_get_blocks_per_group(sb);
680 }
681
682 block->dirty = true;
683
684 rc = block_put(block);
685 if (rc != EOK)
686 return rc;
687
688 ++b;
689 dcnt -= now;
690 }
691
692 /* This initializes the bitmaps and inode table */
693 for (bg_index = 0; bg_index < block_group_count; bg_index++) {
694 rc = ext4_filesystem_get_block_group_ref(fs, bg_index, &bg_ref);
695 if (rc != EOK)
696 return rc;
697
698 /*
699 * Adjust number of free blocks
700 */
701 free_blocks = ext4_superblock_get_blocks_in_group(sb, bg_index);
702 reserved = ext4_filesystem_bg_get_backup_blocks(bg_ref);
703 inode_table_blocks = ext4_filesystem_bg_get_itable_size(sb,
704 bg_ref->index);
705 /* One for block bitmap one for inode bitmap */
706 free_blocks = free_blocks - reserved - 2 - inode_table_blocks;
707
708 ext4_block_group_set_free_blocks_count(bg_ref->block_group,
709 sb, free_blocks);
710 bg_ref->dirty = true;
711
712 rc = ext4_filesystem_put_block_group_ref(bg_ref);
713 if (rc != EOK)
714 return rc;
715 }
716
717 return EOK;
718}
719
720/** Initialize block bitmap in block group.
721 *
722 * @param bg_ref Reference to block group
723 *
724 * @return Error code
725 *
726 */
727static errno_t ext4_filesystem_init_block_bitmap(ext4_block_group_ref_t *bg_ref)
728{
729 uint64_t itb;
730 uint32_t sz;
731 uint32_t i;
732
733 /* Load bitmap */
734 ext4_superblock_t *sb = bg_ref->fs->superblock;
735 uint64_t bitmap_block_addr = ext4_block_group_get_block_bitmap(
736 bg_ref->block_group, bg_ref->fs->superblock);
737 uint64_t bitmap_inode_addr = ext4_block_group_get_inode_bitmap(
738 bg_ref->block_group, bg_ref->fs->superblock);
739 uint32_t blocks_group = ext4_superblock_get_blocks_per_group(sb);
740 uint32_t bg_blocks = ext4_superblock_get_blocks_in_group(sb,
741 bg_ref->index);
742
743 block_t *bitmap_block;
744 errno_t rc = block_get(&bitmap_block, bg_ref->fs->device,
745 bitmap_block_addr, BLOCK_FLAGS_NOREAD);
746 if (rc != EOK)
747 return rc;
748
749 uint8_t *bitmap = bitmap_block->data;
750
751 /* Initialize all bitmap bits to zero */
752 uint32_t block_size = ext4_superblock_get_block_size(sb);
753 memset(bitmap, 0, block_size);
754
755 /* Determine the number of reserved blocks in the group */
756 uint32_t reserved_cnt = ext4_filesystem_bg_get_backup_blocks(bg_ref);
757
758 /* Set bits from to first block to first data block - 1 to one (allocated) */
759 for (uint32_t block = 0; block < reserved_cnt; ++block)
760 ext4_bitmap_set_bit(bitmap, block);
761
762 uint32_t bitmap_block_gid = ext4_filesystem_blockaddr2group(sb,
763 bitmap_block_addr);
764 if (bitmap_block_gid == bg_ref->index) {
765 ext4_bitmap_set_bit(bitmap,
766 ext4_filesystem_blockaddr2_index_in_group(sb, bitmap_block_addr));
767 }
768
769 uint32_t bitmap_inode_gid = ext4_filesystem_blockaddr2group(sb,
770 bitmap_inode_addr);
771 if (bitmap_inode_gid == bg_ref->index) {
772 ext4_bitmap_set_bit(bitmap,
773 ext4_filesystem_blockaddr2_index_in_group(sb, bitmap_inode_addr));
774 }
775
776 itb = ext4_block_group_get_inode_table_first_block(bg_ref->block_group,
777 sb);
778 sz = ext4_filesystem_bg_get_itable_size(sb, bg_ref->index);
779
780 for (i = 0; i < sz; ++i, ++itb) {
781 uint32_t gid = ext4_filesystem_blockaddr2group(sb, itb);
782 if (gid == bg_ref->index) {
783 ext4_bitmap_set_bit(bitmap,
784 ext4_filesystem_blockaddr2_index_in_group(sb, itb));
785 }
786 }
787
788 /* For last group need to mark blocks which are outside of the FS */
789 for (uint32_t block = bg_blocks; block < blocks_group; block++) {
790 ext4_bitmap_set_bit(bitmap, block);
791 }
792
793 bitmap_block->dirty = true;
794
795 /* Save bitmap */
796 return block_put(bitmap_block);
797}
798
799/** Initialize i-node bitmap in block group.
800 *
801 * @param bg_ref Reference to block group
802 *
803 * @return Error code
804 *
805 */
806static errno_t ext4_filesystem_init_inode_bitmap(ext4_block_group_ref_t *bg_ref)
807{
808 /* Load bitmap */
809 uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
810 bg_ref->block_group, bg_ref->fs->superblock);
811 block_t *bitmap_block;
812
813 errno_t rc = block_get(&bitmap_block, bg_ref->fs->device,
814 bitmap_block_addr, BLOCK_FLAGS_NOREAD);
815 if (rc != EOK)
816 return rc;
817
818 uint8_t *bitmap = bitmap_block->data;
819
820 /* Initialize all bitmap bits to zero */
821 uint32_t block_size = ext4_superblock_get_block_size(bg_ref->fs->superblock);
822 uint32_t inodes_per_group =
823 ext4_superblock_get_inodes_per_group(bg_ref->fs->superblock);
824 memset(bitmap, 0, (inodes_per_group + 7) / 8);
825
826 uint32_t start_bit = inodes_per_group;
827 uint32_t end_bit = block_size * 8;
828
829 uint32_t i;
830 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
831 ext4_bitmap_set_bit(bitmap, i);
832
833 if (i < end_bit)
834 memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
835
836 bitmap_block->dirty = true;
837
838 /* Save bitmap */
839 return block_put(bitmap_block);
840}
841
842/** Initialize i-node table in block group.
843 *
844 * @param bg_ref Reference to block group
845 *
846 * @return Error code
847 *
848 */
849static errno_t ext4_filesystem_init_inode_table(ext4_block_group_ref_t *bg_ref)
850{
851 ext4_superblock_t *sb = bg_ref->fs->superblock;
852
853 uint32_t block_size = ext4_superblock_get_block_size(sb);
854 uint32_t inodes_per_block = ext4_filesystem_inodes_per_block(sb);
855
856 uint32_t inodes_in_group =
857 ext4_superblock_get_inodes_in_group(sb, bg_ref->index);
858
859 uint32_t table_blocks = inodes_in_group / inodes_per_block;
860
861 if (inodes_in_group % inodes_per_block)
862 table_blocks++;
863
864 /* Compute initialization bounds */
865 uint32_t first_block = ext4_block_group_get_inode_table_first_block(
866 bg_ref->block_group, sb);
867
868 uint32_t last_block = first_block + table_blocks - 1;
869
870 /* Initialization of all itable blocks */
871 for (uint32_t fblock = first_block; fblock <= last_block; ++fblock) {
872 block_t *block;
873 errno_t rc = block_get(&block, bg_ref->fs->device, fblock,
874 BLOCK_FLAGS_NOREAD);
875 if (rc != EOK)
876 return rc;
877
878 memset(block->data, 0, block_size);
879 block->dirty = true;
880
881 rc = block_put(block);
882 if (rc != EOK)
883 return rc;
884 }
885
886 return EOK;
887}
888
889/** Get reference to block group specified by index.
890 *
891 * @param fs Filesystem to find block group on
892 * @param bgid Index of block group to load
893 * @param ref Output pointer for reference
894 *
895 * @return Error code
896 *
897 */
898errno_t ext4_filesystem_get_block_group_ref(ext4_filesystem_t *fs, uint32_t bgid,
899 ext4_block_group_ref_t **ref)
900{
901 /* Allocate memory for new structure */
902 ext4_block_group_ref_t *newref =
903 malloc(sizeof(ext4_block_group_ref_t));
904 if (newref == NULL)
905 return ENOMEM;
906
907 /* Compute number of descriptors, that fits in one data block */
908 uint32_t descriptors_per_block =
909 ext4_superblock_get_block_size(fs->superblock) /
910 ext4_superblock_get_desc_size(fs->superblock);
911
912 /* Block group descriptor table starts at the next block after superblock */
913 aoff64_t block_id =
914 ext4_superblock_get_first_data_block(fs->superblock) + 1;
915
916 /* Find the block containing the descriptor we are looking for */
917 block_id += bgid / descriptors_per_block;
918 uint32_t offset = (bgid % descriptors_per_block) *
919 ext4_superblock_get_desc_size(fs->superblock);
920
921 /* Load block with descriptors */
922 errno_t rc = block_get(&newref->block, fs->device, block_id, 0);
923 if (rc != EOK) {
924 free(newref);
925 return rc;
926 }
927
928 /* Initialize in-memory representation */
929 newref->block_group = newref->block->data + offset;
930 newref->fs = fs;
931 newref->index = bgid;
932 newref->dirty = false;
933
934 *ref = newref;
935
936 if (ext4_block_group_has_flag(newref->block_group,
937 EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
938 rc = ext4_filesystem_init_block_bitmap(newref);
939 if (rc != EOK) {
940 block_put(newref->block);
941 free(newref);
942 return rc;
943 }
944
945 ext4_block_group_clear_flag(newref->block_group,
946 EXT4_BLOCK_GROUP_BLOCK_UNINIT);
947
948 newref->dirty = true;
949 }
950
951 if (ext4_block_group_has_flag(newref->block_group,
952 EXT4_BLOCK_GROUP_INODE_UNINIT)) {
953 rc = ext4_filesystem_init_inode_bitmap(newref);
954 if (rc != EOK) {
955 block_put(newref->block);
956 free(newref);
957 return rc;
958 }
959
960 ext4_block_group_clear_flag(newref->block_group,
961 EXT4_BLOCK_GROUP_INODE_UNINIT);
962
963 if (!ext4_block_group_has_flag(newref->block_group,
964 EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
965 rc = ext4_filesystem_init_inode_table(newref);
966 if (rc != EOK) {
967 block_put(newref->block);
968 free(newref);
969 return rc;
970 }
971
972 ext4_block_group_set_flag(newref->block_group,
973 EXT4_BLOCK_GROUP_ITABLE_ZEROED);
974 }
975
976 newref->dirty = true;
977 }
978
979 return EOK;
980}
981
982/** Compute checksum of block group descriptor.
983 *
984 * @param sb Superblock
985 * @param bgid Index of block group in the filesystem
986 * @param bg Block group to compute checksum for
987 *
988 * @return Checksum value
989 *
990 */
991static uint16_t ext4_filesystem_bg_checksum(ext4_superblock_t *sb, uint32_t bgid,
992 ext4_block_group_t *bg)
993{
994 /* If checksum not supported, 0 will be returned */
995 uint16_t crc = 0;
996
997 /* Compute the checksum only if the filesystem supports it */
998 if (ext4_superblock_has_feature_read_only(sb,
999 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1000 void *base = bg;
1001 void *checksum = &bg->checksum;
1002
1003 uint32_t offset = (uint32_t) (checksum - base);
1004
1005 /* Convert block group index to little endian */
1006 uint32_t le_group = host2uint32_t_le(bgid);
1007
1008 /* Initialization */
1009 crc = crc16_ibm(~0, sb->uuid, sizeof(sb->uuid));
1010
1011 /* Include index of block group */
1012 crc = crc16_ibm(crc, (uint8_t *) &le_group, sizeof(le_group));
1013
1014 /* Compute crc from the first part (stop before checksum field) */
1015 crc = crc16_ibm(crc, (uint8_t *) bg, offset);
1016
1017 /* Skip checksum */
1018 offset += sizeof(bg->checksum);
1019
1020 /* Checksum of the rest of block group descriptor */
1021 if ((ext4_superblock_has_feature_incompatible(sb,
1022 EXT4_FEATURE_INCOMPAT_64BIT)) &&
1023 (offset < ext4_superblock_get_desc_size(sb)))
1024 crc = crc16_ibm(crc, ((uint8_t *) bg) + offset,
1025 ext4_superblock_get_desc_size(sb) - offset);
1026 }
1027
1028 return crc;
1029}
1030
1031/** Get the size of the block group's inode table
1032 *
1033 * @param sb Pointer to the superblock
1034 * @param bg_index Block group index
1035 *
1036 * @return Size of the inode table in blocks.
1037 */
1038uint32_t ext4_filesystem_bg_get_itable_size(ext4_superblock_t *sb,
1039 uint32_t bg_index)
1040{
1041 uint32_t itable_size;
1042 uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
1043 uint16_t inode_table_item_size = ext4_superblock_get_inode_size(sb);
1044 uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
1045 uint32_t block_size = ext4_superblock_get_block_size(sb);
1046
1047 if (bg_index < block_group_count - 1) {
1048 itable_size = inodes_per_group * inode_table_item_size;
1049 } else {
1050 /* Last block group could be smaller */
1051 uint32_t inodes_count_total = ext4_superblock_get_inodes_count(sb);
1052 itable_size =
1053 (inodes_count_total - ((block_group_count - 1) * inodes_per_group)) *
1054 inode_table_item_size;
1055 }
1056
1057 return ROUND_UP(itable_size, block_size) / block_size;
1058}
1059
1060/** Get the number of blocks used by superblock + gdt + reserved gdt backups
1061 *
1062 * @param bg Pointer to block group
1063 *
1064 * @return Number of blocks
1065 */
1066uint32_t ext4_filesystem_bg_get_backup_blocks(ext4_block_group_ref_t *bg)
1067{
1068 return ext4_superblock_get_group_backup_blocks(bg->fs->superblock,
1069 bg->index);
1070}
1071
1072/** Put reference to block group.
1073 *
1074 * @param ref Pointer for reference to be put back
1075 *
1076 * @return Error code
1077 *
1078 */
1079errno_t ext4_filesystem_put_block_group_ref(ext4_block_group_ref_t *ref)
1080{
1081 /* Check if reference modified */
1082 if (ref->dirty) {
1083 /* Compute new checksum of block group */
1084 uint16_t checksum =
1085 ext4_filesystem_bg_checksum(ref->fs->superblock, ref->index,
1086 ref->block_group);
1087 ext4_block_group_set_checksum(ref->block_group, checksum);
1088
1089 /* Mark block dirty for writing changes to physical device */
1090 ref->block->dirty = true;
1091 }
1092
1093 /* Put back block, that contains block group descriptor */
1094 errno_t rc = block_put(ref->block);
1095 free(ref);
1096
1097 return rc;
1098}
1099
1100/** Get reference to i-node specified by index.
1101 *
1102 * @param fs Filesystem to find i-node on
1103 * @param index Index of i-node to load
1104 * @oaram ref Output pointer for reference
1105 *
1106 * @return Error code
1107 *
1108 */
1109errno_t ext4_filesystem_get_inode_ref(ext4_filesystem_t *fs, uint32_t index,
1110 ext4_inode_ref_t **ref)
1111{
1112 /* Allocate memory for new structure */
1113 ext4_inode_ref_t *newref =
1114 malloc(sizeof(ext4_inode_ref_t));
1115 if (newref == NULL)
1116 return ENOMEM;
1117
1118 /* Compute number of i-nodes, that fits in one data block */
1119 uint32_t inodes_per_group =
1120 ext4_superblock_get_inodes_per_group(fs->superblock);
1121
1122 /*
1123 * Inode numbers are 1-based, but it is simpler to work with 0-based
1124 * when computing indices
1125 */
1126 index -= 1;
1127 uint32_t block_group = index / inodes_per_group;
1128 uint32_t offset_in_group = index % inodes_per_group;
1129
1130 /* Load block group, where i-node is located */
1131 ext4_block_group_ref_t *bg_ref;
1132 errno_t rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
1133 if (rc != EOK) {
1134 free(newref);
1135 return rc;
1136 }
1137
1138 /* Load block address, where i-node table is located */
1139 uint32_t inode_table_start =
1140 ext4_block_group_get_inode_table_first_block(bg_ref->block_group,
1141 fs->superblock);
1142
1143 /* Put back block group reference (not needed more) */
1144 rc = ext4_filesystem_put_block_group_ref(bg_ref);
1145 if (rc != EOK) {
1146 free(newref);
1147 return rc;
1148 }
1149
1150 /* Compute position of i-node in the block group */
1151 uint16_t inode_size = ext4_superblock_get_inode_size(fs->superblock);
1152 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
1153 uint32_t byte_offset_in_group = offset_in_group * inode_size;
1154
1155 /* Compute block address */
1156 aoff64_t block_id = inode_table_start + (byte_offset_in_group / block_size);
1157 rc = block_get(&newref->block, fs->device, block_id, 0);
1158 if (rc != EOK) {
1159 free(newref);
1160 return rc;
1161 }
1162
1163 /* Compute position of i-node in the data block */
1164 uint32_t offset_in_block = byte_offset_in_group % block_size;
1165 newref->inode = newref->block->data + offset_in_block;
1166
1167 /* We need to store the original value of index in the reference */
1168 newref->index = index + 1;
1169 newref->fs = fs;
1170 newref->dirty = false;
1171
1172 *ref = newref;
1173
1174 return EOK;
1175}
1176
1177/** Put reference to i-node.
1178 *
1179 * @param ref Pointer for reference to be put back
1180 *
1181 * @return Error code
1182 *
1183 */
1184errno_t ext4_filesystem_put_inode_ref(ext4_inode_ref_t *ref)
1185{
1186 /* Check if reference modified */
1187 if (ref->dirty) {
1188 /* Mark block dirty for writing changes to physical device */
1189 ref->block->dirty = true;
1190 }
1191
1192 /* Put back block, that contains i-node */
1193 errno_t rc = block_put(ref->block);
1194 free(ref);
1195
1196 return rc;
1197}
1198
1199/** Initialize newly allocated i-node in the filesystem.
1200 *
1201 * @param fs Filesystem to initialize i-node on
1202 * @param index I-node index
1203 * @param inode_ref Output pointer to return reference to allocated i-node
1204 * @param flags Flags to be set for newly created i-node
1205 *
1206 * @return Error code
1207 *
1208 */
1209static errno_t ext4_filesystem_init_inode(ext4_filesystem_t *fs, uint32_t index,
1210 ext4_inode_ref_t **inode_ref, int flags)
1211{
1212 /* Check if newly allocated i-node will be a directory */
1213 bool is_dir = false;
1214 if (flags & L_DIRECTORY)
1215 is_dir = true;
1216
1217 /* Load i-node from on-disk i-node table */
1218 errno_t rc = ext4_filesystem_get_inode_ref(fs, index, inode_ref);
1219 if (rc != EOK) {
1220 ext4_ialloc_free_inode(fs, index, is_dir);
1221 return rc;
1222 }
1223
1224 /* Initialize i-node */
1225 ext4_inode_t *inode = (*inode_ref)->inode;
1226
1227 uint16_t mode;
1228 if (is_dir) {
1229 /*
1230 * Default directory permissions to be compatible with other systems
1231 * 0777 (octal) == rwxrwxrwx
1232 */
1233
1234 mode = 0777;
1235 mode |= EXT4_INODE_MODE_DIRECTORY;
1236 ext4_inode_set_mode(fs->superblock, inode, mode);
1237 ext4_inode_set_links_count(inode, 1); /* '.' entry */
1238 } else {
1239 /*
1240 * Default file permissions to be compatible with other systems
1241 * 0666 (octal) == rw-rw-rw-
1242 */
1243
1244 mode = 0666;
1245 mode |= EXT4_INODE_MODE_FILE;
1246 ext4_inode_set_mode(fs->superblock, inode, mode);
1247 ext4_inode_set_links_count(inode, 0);
1248 }
1249
1250 ext4_inode_set_uid(inode, 0);
1251 ext4_inode_set_gid(inode, 0);
1252 ext4_inode_set_size(inode, 0);
1253 ext4_inode_set_access_time(inode, 0);
1254 ext4_inode_set_change_inode_time(inode, 0);
1255 ext4_inode_set_modification_time(inode, 0);
1256 ext4_inode_set_deletion_time(inode, 0);
1257 ext4_inode_set_blocks_count(fs->superblock, inode, 0);
1258 ext4_inode_set_flags(inode, 0);
1259 ext4_inode_set_generation(inode, 0);
1260
1261 /* Reset blocks array */
1262 for (uint32_t i = 0; i < EXT4_INODE_BLOCKS; i++)
1263 inode->blocks[i] = 0;
1264
1265 /* Initialize extents if needed */
1266 if (ext4_superblock_has_feature_incompatible(
1267 fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
1268 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
1269
1270 /* Initialize extent root header */
1271 ext4_extent_header_t *header = ext4_inode_get_extent_header(inode);
1272 ext4_extent_header_set_depth(header, 0);
1273 ext4_extent_header_set_entries_count(header, 0);
1274 ext4_extent_header_set_generation(header, 0);
1275 ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
1276
1277 uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) -
1278 sizeof(ext4_extent_header_t)) / sizeof(ext4_extent_t);
1279
1280 ext4_extent_header_set_max_entries_count(header, max_entries);
1281 }
1282
1283 (*inode_ref)->dirty = true;
1284
1285 return EOK;
1286}
1287
1288/** Allocate new i-node in the filesystem.
1289 *
1290 * @param fs Filesystem to allocated i-node on
1291 * @param inode_ref Output pointer to return reference to allocated i-node
1292 * @param flags Flags to be set for newly created i-node
1293 *
1294 * @return Error code
1295 *
1296 */
1297errno_t ext4_filesystem_alloc_inode(ext4_filesystem_t *fs,
1298 ext4_inode_ref_t **inode_ref, int flags)
1299{
1300 /* Check if newly allocated i-node will be a directory */
1301 bool is_dir = false;
1302 if (flags & L_DIRECTORY)
1303 is_dir = true;
1304
1305 /* Allocate inode by allocation algorithm */
1306 uint32_t index;
1307 errno_t rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
1308 if (rc != EOK)
1309 return rc;
1310
1311 rc = ext4_filesystem_init_inode(fs, index, inode_ref, flags);
1312 if (rc != EOK) {
1313 ext4_ialloc_free_inode(fs, index, is_dir);
1314 return rc;
1315 }
1316
1317 return EOK;
1318}
1319
1320/** Allocate specific i-node in the filesystem.
1321 *
1322 * @param fs Filesystem to allocated i-node on
1323 * @param index Index of i-node to allocate
1324 * @param inode_ref Output pointer to return reference to allocated i-node
1325 * @param flags Flags to be set for newly created i-node
1326 *
1327 * @return Error code
1328 *
1329 */
1330static errno_t ext4_filesystem_alloc_this_inode(ext4_filesystem_t *fs,
1331 uint32_t index, ext4_inode_ref_t **inode_ref, int flags)
1332{
1333 /* Check if newly allocated i-node will be a directory */
1334 bool is_dir = false;
1335 if (flags & L_DIRECTORY)
1336 is_dir = true;
1337
1338 /* Allocate inode by allocation algorithm */
1339 errno_t rc = ext4_ialloc_alloc_this_inode(fs, index, is_dir);
1340 if (rc != EOK)
1341 return rc;
1342
1343 rc = ext4_filesystem_init_inode(fs, index, inode_ref, flags);
1344 if (rc != EOK) {
1345 ext4_ialloc_free_inode(fs, index, is_dir);
1346 return rc;
1347 }
1348
1349 return EOK;
1350}
1351
1352/** Release i-node and mark it as free.
1353 *
1354 * @param inode_ref I-node to be released
1355 *
1356 * @return Error code
1357 *
1358 */
1359errno_t ext4_filesystem_free_inode(ext4_inode_ref_t *inode_ref)
1360{
1361 ext4_filesystem_t *fs = inode_ref->fs;
1362
1363 /* For extents must be data block destroyed by other way */
1364 if ((ext4_superblock_has_feature_incompatible(fs->superblock,
1365 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1366 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1367 /* Data structures are released during truncate operation... */
1368 goto finish;
1369 }
1370
1371 /* Release all indirect (no data) blocks */
1372
1373 /* 1) Single indirect */
1374 uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
1375 if (fblock != 0) {
1376 errno_t rc = ext4_balloc_free_block(inode_ref, fblock);
1377 if (rc != EOK)
1378 return rc;
1379
1380 ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
1381 }
1382
1383 block_t *block;
1384 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
1385 uint32_t count = block_size / sizeof(uint32_t);
1386
1387 /* 2) Double indirect */
1388 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
1389 if (fblock != 0) {
1390 errno_t rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
1391 if (rc != EOK)
1392 return rc;
1393
1394 uint32_t ind_block;
1395 for (uint32_t offset = 0; offset < count; ++offset) {
1396 ind_block = uint32_t_le2host(((uint32_t *) block->data)[offset]);
1397
1398 if (ind_block != 0) {
1399 rc = ext4_balloc_free_block(inode_ref, ind_block);
1400 if (rc != EOK) {
1401 block_put(block);
1402 return rc;
1403 }
1404 }
1405 }
1406
1407 rc = block_put(block);
1408 if (rc != EOK)
1409 return rc;
1410
1411 rc = ext4_balloc_free_block(inode_ref, fblock);
1412 if (rc != EOK)
1413 return rc;
1414
1415 ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
1416 }
1417
1418 /* 3) Tripple indirect */
1419 block_t *subblock;
1420 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
1421 if (fblock != 0) {
1422 errno_t rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
1423 if (rc != EOK)
1424 return rc;
1425
1426 uint32_t ind_block;
1427 for (uint32_t offset = 0; offset < count; ++offset) {
1428 ind_block = uint32_t_le2host(((uint32_t *) block->data)[offset]);
1429
1430 if (ind_block != 0) {
1431 rc = block_get(&subblock, fs->device, ind_block,
1432 BLOCK_FLAGS_NONE);
1433 if (rc != EOK) {
1434 block_put(block);
1435 return rc;
1436 }
1437
1438 uint32_t ind_subblock;
1439 for (uint32_t suboffset = 0; suboffset < count;
1440 ++suboffset) {
1441 ind_subblock = uint32_t_le2host(((uint32_t *)
1442 subblock->data)[suboffset]);
1443
1444 if (ind_subblock != 0) {
1445 rc = ext4_balloc_free_block(inode_ref, ind_subblock);
1446 if (rc != EOK) {
1447 block_put(subblock);
1448 block_put(block);
1449 return rc;
1450 }
1451 }
1452 }
1453
1454 rc = block_put(subblock);
1455 if (rc != EOK) {
1456 block_put(block);
1457 return rc;
1458 }
1459 }
1460
1461 rc = ext4_balloc_free_block(inode_ref, ind_block);
1462 if (rc != EOK) {
1463 block_put(block);
1464 return rc;
1465 }
1466 }
1467
1468 rc = block_put(block);
1469 if (rc != EOK)
1470 return rc;
1471
1472 rc = ext4_balloc_free_block(inode_ref, fblock);
1473 if (rc != EOK)
1474 return rc;
1475
1476 ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
1477 }
1478
1479finish:
1480 /* Mark inode dirty for writing to the physical device */
1481 inode_ref->dirty = true;
1482
1483 /* Free block with extended attributes if present */
1484 uint32_t xattr_block = ext4_inode_get_file_acl(
1485 inode_ref->inode, fs->superblock);
1486 if (xattr_block) {
1487 errno_t rc = ext4_balloc_free_block(inode_ref, xattr_block);
1488 if (rc != EOK)
1489 return rc;
1490
1491 ext4_inode_set_file_acl(inode_ref->inode, fs->superblock, 0);
1492 }
1493
1494 /* Free inode by allocator */
1495 errno_t rc;
1496 if (ext4_inode_is_type(fs->superblock, inode_ref->inode,
1497 EXT4_INODE_MODE_DIRECTORY))
1498 rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
1499 else
1500 rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
1501
1502 return rc;
1503}
1504
1505/** Truncate i-node data blocks.
1506 *
1507 * @param inode_ref I-node to be truncated
1508 * @param new_size New size of inode (must be < current size)
1509 *
1510 * @return Error code
1511 *
1512 */
1513errno_t ext4_filesystem_truncate_inode(ext4_inode_ref_t *inode_ref,
1514 aoff64_t new_size)
1515{
1516 ext4_superblock_t *sb = inode_ref->fs->superblock;
1517
1518 /* Check flags, if i-node can be truncated */
1519 if (!ext4_inode_can_truncate(sb, inode_ref->inode))
1520 return EINVAL;
1521
1522 /* If sizes are equal, nothing has to be done. */
1523 aoff64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
1524 if (old_size == new_size)
1525 return EOK;
1526
1527 /* It's not suppported to make the larger file by truncate operation */
1528 if (old_size < new_size)
1529 return EINVAL;
1530
1531 /* Compute how many blocks will be released */
1532 aoff64_t size_diff = old_size - new_size;
1533 uint32_t block_size = ext4_superblock_get_block_size(sb);
1534 uint32_t diff_blocks_count = size_diff / block_size;
1535 if (size_diff % block_size != 0)
1536 diff_blocks_count++;
1537
1538 uint32_t old_blocks_count = old_size / block_size;
1539 if (old_size % block_size != 0)
1540 old_blocks_count++;
1541
1542 if ((ext4_superblock_has_feature_incompatible(inode_ref->fs->superblock,
1543 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1544 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1545 /* Extents require special operation */
1546 errno_t rc = ext4_extent_release_blocks_from(inode_ref,
1547 old_blocks_count - diff_blocks_count);
1548 if (rc != EOK)
1549 return rc;
1550 } else {
1551 /* Release data blocks from the end of file */
1552
1553 /* Starting from 1 because of logical blocks are numbered from 0 */
1554 for (uint32_t i = 1; i <= diff_blocks_count; ++i) {
1555 errno_t rc = ext4_filesystem_release_inode_block(inode_ref,
1556 old_blocks_count - i);
1557 if (rc != EOK)
1558 return rc;
1559 }
1560 }
1561
1562 /* Update i-node */
1563 ext4_inode_set_size(inode_ref->inode, new_size);
1564 inode_ref->dirty = true;
1565
1566 return EOK;
1567}
1568
1569/** Get physical block address by logical index of the block.
1570 *
1571 * @param inode_ref I-node to read block address from
1572 * @param iblock Logical index of block
1573 * @param fblock Output pointer for return physical block address
1574 *
1575 * @return Error code
1576 *
1577 */
1578errno_t ext4_filesystem_get_inode_data_block_index(ext4_inode_ref_t *inode_ref,
1579 aoff64_t iblock, uint32_t *fblock)
1580{
1581 ext4_filesystem_t *fs = inode_ref->fs;
1582
1583 /* For empty file is situation simple */
1584 if (ext4_inode_get_size(fs->superblock, inode_ref->inode) == 0) {
1585 *fblock = 0;
1586 return EOK;
1587 }
1588
1589 uint32_t current_block;
1590
1591 /* Handle i-node using extents */
1592 if ((ext4_superblock_has_feature_incompatible(fs->superblock,
1593 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1594 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1595 errno_t rc = ext4_extent_find_block(inode_ref, iblock, &current_block);
1596 if (rc != EOK)
1597 return rc;
1598
1599 *fblock = current_block;
1600 return EOK;
1601 }
1602
1603 ext4_inode_t *inode = inode_ref->inode;
1604
1605 /* Direct block are read directly from array in i-node structure */
1606 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1607 current_block = ext4_inode_get_direct_block(inode, (uint32_t) iblock);
1608 *fblock = current_block;
1609 return EOK;
1610 }
1611
1612 /* Determine indirection level of the target block */
1613 unsigned int level = 0;
1614 for (unsigned int i = 1; i < 4; i++) {
1615 if (iblock < fs->inode_block_limits[i]) {
1616 level = i;
1617 break;
1618 }
1619 }
1620
1621 if (level == 0)
1622 return EIO;
1623
1624 /* Compute offsets for the topmost level */
1625 aoff64_t block_offset_in_level =
1626 iblock - fs->inode_block_limits[level - 1];
1627 current_block = ext4_inode_get_indirect_block(inode, level - 1);
1628 uint32_t offset_in_block =
1629 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1630
1631 /* Sparse file */
1632 if (current_block == 0) {
1633 *fblock = 0;
1634 return EOK;
1635 }
1636
1637 block_t *block;
1638
1639 /*
1640 * Navigate through other levels, until we find the block number
1641 * or find null reference meaning we are dealing with sparse file
1642 */
1643 while (level > 0) {
1644 /* Load indirect block */
1645 errno_t rc = block_get(&block, fs->device, current_block, 0);
1646 if (rc != EOK)
1647 return rc;
1648
1649 /* Read block address from indirect block */
1650 current_block =
1651 uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
1652
1653 /* Put back indirect block untouched */
1654 rc = block_put(block);
1655 if (rc != EOK)
1656 return rc;
1657
1658 /* Check for sparse file */
1659 if (current_block == 0) {
1660 *fblock = 0;
1661 return EOK;
1662 }
1663
1664 /* Jump to the next level */
1665 level--;
1666
1667 /* Termination condition - we have address of data block loaded */
1668 if (level == 0)
1669 break;
1670
1671 /* Visit the next level */
1672 block_offset_in_level %= fs->inode_blocks_per_level[level];
1673 offset_in_block =
1674 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1675 }
1676
1677 *fblock = current_block;
1678
1679 return EOK;
1680}
1681
1682/** Set physical block address for the block logical address into the i-node.
1683 *
1684 * @param inode_ref I-node to set block address to
1685 * @param iblock Logical index of block
1686 * @param fblock Physical block address
1687 *
1688 * @return Error code
1689 *
1690 */
1691errno_t ext4_filesystem_set_inode_data_block_index(ext4_inode_ref_t *inode_ref,
1692 aoff64_t iblock, uint32_t fblock)
1693{
1694 ext4_filesystem_t *fs = inode_ref->fs;
1695
1696 /* Handle inode using extents */
1697 if ((ext4_superblock_has_feature_compatible(fs->superblock,
1698 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1699 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1700 /* Not reachable */
1701 return ENOTSUP;
1702 }
1703
1704 /* Handle simple case when we are dealing with direct reference */
1705 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1706 ext4_inode_set_direct_block(inode_ref->inode, (uint32_t) iblock, fblock);
1707 inode_ref->dirty = true;
1708
1709 return EOK;
1710 }
1711
1712 /* Determine the indirection level needed to get the desired block */
1713 unsigned int level = 0;
1714 for (unsigned int i = 1; i < 4; i++) {
1715 if (iblock < fs->inode_block_limits[i]) {
1716 level = i;
1717 break;
1718 }
1719 }
1720
1721 if (level == 0)
1722 return EIO;
1723
1724 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
1725
1726 /* Compute offsets for the topmost level */
1727 aoff64_t block_offset_in_level =
1728 iblock - fs->inode_block_limits[level - 1];
1729 uint32_t current_block =
1730 ext4_inode_get_indirect_block(inode_ref->inode, level - 1);
1731 uint32_t offset_in_block =
1732 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1733
1734 uint32_t new_block_addr;
1735 block_t *block;
1736 block_t *new_block;
1737
1738 /* Is needed to allocate indirect block on the i-node level */
1739 if (current_block == 0) {
1740 /* Allocate new indirect block */
1741 errno_t rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1742 if (rc != EOK)
1743 return rc;
1744
1745 /* Update i-node */
1746 ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
1747 new_block_addr);
1748 inode_ref->dirty = true;
1749
1750 /* Load newly allocated block */
1751 rc = block_get(&new_block, fs->device, new_block_addr,
1752 BLOCK_FLAGS_NOREAD);
1753 if (rc != EOK) {
1754 ext4_balloc_free_block(inode_ref, new_block_addr);
1755 return rc;
1756 }
1757
1758 /* Initialize new block */
1759 memset(new_block->data, 0, block_size);
1760 new_block->dirty = true;
1761
1762 /* Put back the allocated block */
1763 rc = block_put(new_block);
1764 if (rc != EOK)
1765 return rc;
1766
1767 current_block = new_block_addr;
1768 }
1769
1770 /*
1771 * Navigate through other levels, until we find the block number
1772 * or find null reference meaning we are dealing with sparse file
1773 */
1774 while (level > 0) {
1775 errno_t rc = block_get(&block, fs->device, current_block, 0);
1776 if (rc != EOK)
1777 return rc;
1778
1779 current_block =
1780 uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
1781
1782 if ((level > 1) && (current_block == 0)) {
1783 /* Allocate new block */
1784 rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1785 if (rc != EOK) {
1786 block_put(block);
1787 return rc;
1788 }
1789
1790 /* Load newly allocated block */
1791 rc = block_get(&new_block, fs->device, new_block_addr,
1792 BLOCK_FLAGS_NOREAD);
1793 if (rc != EOK) {
1794 block_put(block);
1795 return rc;
1796 }
1797
1798 /* Initialize allocated block */
1799 memset(new_block->data, 0, block_size);
1800 new_block->dirty = true;
1801
1802 rc = block_put(new_block);
1803 if (rc != EOK) {
1804 block_put(block);
1805 return rc;
1806 }
1807
1808 /* Write block address to the parent */
1809 ((uint32_t *) block->data)[offset_in_block] =
1810 host2uint32_t_le(new_block_addr);
1811 block->dirty = true;
1812 current_block = new_block_addr;
1813 }
1814
1815 /* Will be finished, write the fblock address */
1816 if (level == 1) {
1817 ((uint32_t *) block->data)[offset_in_block] =
1818 host2uint32_t_le(fblock);
1819 block->dirty = true;
1820 }
1821
1822 rc = block_put(block);
1823 if (rc != EOK)
1824 return rc;
1825
1826 level--;
1827
1828 /*
1829 * If we are on the last level, break here as
1830 * there is no next level to visit
1831 */
1832 if (level == 0)
1833 break;
1834
1835 /* Visit the next level */
1836 block_offset_in_level %= fs->inode_blocks_per_level[level];
1837 offset_in_block =
1838 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1839 }
1840
1841 return EOK;
1842}
1843
1844/** Release data block from i-node
1845 *
1846 * @param inode_ref I-node to release block from
1847 * @param iblock Logical block to be released
1848 *
1849 * @return Error code
1850 *
1851 */
1852errno_t ext4_filesystem_release_inode_block(ext4_inode_ref_t *inode_ref,
1853 uint32_t iblock)
1854{
1855 uint32_t fblock;
1856
1857 ext4_filesystem_t *fs = inode_ref->fs;
1858
1859 /* Extents are handled otherwise = there is not support in this function */
1860 assert(!(ext4_superblock_has_feature_incompatible(fs->superblock,
1861 EXT4_FEATURE_INCOMPAT_EXTENTS) &&
1862 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
1863
1864 ext4_inode_t *inode = inode_ref->inode;
1865
1866 /* Handle simple case when we are dealing with direct reference */
1867 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1868 fblock = ext4_inode_get_direct_block(inode, iblock);
1869
1870 /* Sparse file */
1871 if (fblock == 0)
1872 return EOK;
1873
1874 ext4_inode_set_direct_block(inode, iblock, 0);
1875 return ext4_balloc_free_block(inode_ref, fblock);
1876 }
1877
1878 /* Determine the indirection level needed to get the desired block */
1879 unsigned int level = 0;
1880 for (unsigned int i = 1; i < 4; i++) {
1881 if (iblock < fs->inode_block_limits[i]) {
1882 level = i;
1883 break;
1884 }
1885 }
1886
1887 if (level == 0)
1888 return EIO;
1889
1890 /* Compute offsets for the topmost level */
1891 aoff64_t block_offset_in_level =
1892 iblock - fs->inode_block_limits[level - 1];
1893 uint32_t current_block =
1894 ext4_inode_get_indirect_block(inode, level - 1);
1895 uint32_t offset_in_block =
1896 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1897
1898 /*
1899 * Navigate through other levels, until we find the block number
1900 * or find null reference meaning we are dealing with sparse file
1901 */
1902 block_t *block;
1903 while (level > 0) {
1904
1905 /* Sparse check */
1906 if (current_block == 0)
1907 return EOK;
1908
1909 errno_t rc = block_get(&block, fs->device, current_block, 0);
1910 if (rc != EOK)
1911 return rc;
1912
1913 current_block =
1914 uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
1915
1916 /* Set zero if physical data block address found */
1917 if (level == 1) {
1918 ((uint32_t *) block->data)[offset_in_block] =
1919 host2uint32_t_le(0);
1920 block->dirty = true;
1921 }
1922
1923 rc = block_put(block);
1924 if (rc != EOK)
1925 return rc;
1926
1927 level--;
1928
1929 /*
1930 * If we are on the last level, break here as
1931 * there is no next level to visit
1932 */
1933 if (level == 0)
1934 break;
1935
1936 /* Visit the next level */
1937 block_offset_in_level %= fs->inode_blocks_per_level[level];
1938 offset_in_block =
1939 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1940 }
1941
1942 fblock = current_block;
1943 if (fblock == 0)
1944 return EOK;
1945
1946 /* Physical block is not referenced, it can be released */
1947 return ext4_balloc_free_block(inode_ref, fblock);
1948}
1949
1950/** Append following logical block to the i-node.
1951 *
1952 * @param inode_ref I-node to append block to
1953 * @param fblock Output physical block address of newly allocated block
1954 * @param iblock Output logical number of newly allocated block
1955 *
1956 * @return Error code
1957 *
1958 */
1959errno_t ext4_filesystem_append_inode_block(ext4_inode_ref_t *inode_ref,
1960 uint32_t *fblock, uint32_t *iblock)
1961{
1962 /* Handle extents separately */
1963 if ((ext4_superblock_has_feature_incompatible(inode_ref->fs->superblock,
1964 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1965 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)))
1966 return ext4_extent_append_block(inode_ref, iblock, fblock, true);
1967
1968 ext4_superblock_t *sb = inode_ref->fs->superblock;
1969
1970 /* Compute next block index and allocate data block */
1971 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1972 uint32_t block_size = ext4_superblock_get_block_size(sb);
1973
1974 /* Align size i-node size */
1975 if ((inode_size % block_size) != 0)
1976 inode_size += block_size - (inode_size % block_size);
1977
1978 /* Logical blocks are numbered from 0 */
1979 uint32_t new_block_idx = inode_size / block_size;
1980
1981 /* Allocate new physical block */
1982 uint32_t phys_block;
1983 errno_t rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
1984 if (rc != EOK)
1985 return rc;
1986
1987 /* Add physical block address to the i-node */
1988 rc = ext4_filesystem_set_inode_data_block_index(inode_ref,
1989 new_block_idx, phys_block);
1990 if (rc != EOK) {
1991 ext4_balloc_free_block(inode_ref, phys_block);
1992 return rc;
1993 }
1994
1995 /* Update i-node */
1996 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1997 inode_ref->dirty = true;
1998
1999 *fblock = phys_block;
2000 *iblock = new_block_idx;
2001
2002 return EOK;
2003}
2004
2005/** Get the number of inodes per block.
2006 *
2007 * @param sb Superblock
2008 * @return Number of inodes per block
2009 */
2010static uint32_t ext4_filesystem_inodes_per_block(ext4_superblock_t *sb)
2011{
2012 uint32_t inode_size = ext4_superblock_get_inode_size(sb);
2013 uint32_t block_size = ext4_superblock_get_block_size(sb);
2014
2015 return block_size / inode_size;
2016}
2017
2018/**
2019 * @}
2020 */
Note: See TracBrowser for help on using the repository browser.