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

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

libext4: fs creation should support different block sizes, not only 1k

This was not possible before because of some wrong assumptions in
ext4_superblock_get_blocks_in_group() and
ext4_superblock_get_group_backup_blocks()
and hardcoded values in ext4_superblock_create()

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