source: mainline/uspace/lib/ext4/libext4_filesystem.c@ 22fb7ab

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 22fb7ab was eb94d84, checked in by Maurizio Lombardi <m.lombardi85@…>, 10 years ago

libext4: fix memory leak, release the superblock structure if the mount operation fails

  • Property mode set to 100644
File size: 37.0 KB
Line 
1/*
2 * Copyright (c) 2011 Martin Sucha
3 * Copyright (c) 2012 Frantisek Princ
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup libext4
31 * @{
32 */
33/**
34 * @file libext4_filesystem.c
35 * @brief More complex filesystem operations.
36 */
37
38#include <byteorder.h>
39#include <errno.h>
40#include <malloc.h>
41#include <ipc/vfs.h>
42#include "libext4.h"
43
44/** Initialize filesystem and read all needed data.
45 *
46 * @param fs Filesystem instance to be initialized
47 * @param service_id Identifier if device with the filesystem
48 *
49 * @return Error code
50 *
51 */
52int ext4_filesystem_init(ext4_filesystem_t *fs, service_id_t service_id,
53 enum cache_mode cmode)
54{
55 ext4_superblock_t *temp_superblock = NULL;
56
57 fs->device = service_id;
58
59 /* Initialize block library (4096 is size of communication channel) */
60 int rc = block_init(EXCHANGE_SERIALIZE, fs->device, 4096);
61 if (rc != EOK)
62 goto err;
63
64 /* Read superblock from device to memory */
65 rc = ext4_superblock_read_direct(fs->device, &temp_superblock);
66 if (rc != EOK)
67 goto err_1;
68
69 /* Read block size from superblock and check */
70 uint32_t block_size = ext4_superblock_get_block_size(temp_superblock);
71 if (block_size > EXT4_MAX_BLOCK_SIZE) {
72 rc = ENOTSUP;
73 goto err_1;
74 }
75
76 /* Initialize block caching by libblock */
77 rc = block_cache_init(service_id, block_size, 0, cmode);
78 if (rc != EOK)
79 goto err_1;
80
81 /* Compute limits for indirect block levels */
82 uint32_t block_ids_per_block = block_size / sizeof(uint32_t);
83 fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
84 fs->inode_blocks_per_level[0] = 1;
85 for (unsigned int i = 1; i < 4; i++) {
86 fs->inode_blocks_per_level[i] = fs->inode_blocks_per_level[i - 1] *
87 block_ids_per_block;
88 fs->inode_block_limits[i] = fs->inode_block_limits[i - 1] +
89 fs->inode_blocks_per_level[i];
90 }
91
92 /* Return loaded superblock */
93 fs->superblock = temp_superblock;
94
95 uint16_t state = ext4_superblock_get_state(fs->superblock);
96
97 if (((state & EXT4_SUPERBLOCK_STATE_VALID_FS) !=
98 EXT4_SUPERBLOCK_STATE_VALID_FS) ||
99 ((state & EXT4_SUPERBLOCK_STATE_ERROR_FS) ==
100 EXT4_SUPERBLOCK_STATE_ERROR_FS)) {
101 rc = ENOTSUP;
102 goto err_2;
103 }
104
105 /* Mark system as mounted */
106 ext4_superblock_set_state(fs->superblock, EXT4_SUPERBLOCK_STATE_ERROR_FS);
107 rc = ext4_superblock_write_direct(fs->device, fs->superblock);
108 if (rc != EOK)
109 goto err_2;
110
111 uint16_t mnt_count = ext4_superblock_get_mount_count(fs->superblock);
112 ext4_superblock_set_mount_count(fs->superblock, mnt_count + 1);
113
114 return EOK;
115
116err_2:
117 block_cache_fini(fs->device);
118err_1:
119 block_fini(fs->device);
120err:
121 if (temp_superblock)
122 ext4_superblock_release(temp_superblock);
123 return rc;
124}
125
126/** Destroy filesystem instance (used by unmount operation).
127 *
128 * @param fs Filesystem to be destroyed
129 *
130 * @return Error code
131 *
132 */
133int ext4_filesystem_fini(ext4_filesystem_t *fs)
134{
135 /* Write the superblock to the device */
136 ext4_superblock_set_state(fs->superblock, EXT4_SUPERBLOCK_STATE_VALID_FS);
137 int rc = ext4_superblock_write_direct(fs->device, fs->superblock);
138
139 /* Release memory space for superblock */
140 free(fs->superblock);
141
142 /* Finish work with block library */
143 block_cache_fini(fs->device);
144 block_fini(fs->device);
145
146 return rc;
147}
148
149/** Check sanity of the filesystem.
150 *
151 * Main is the check of the superblock structure.
152 *
153 * @param fs Filesystem to be checked
154 *
155 * @return Error code
156 *
157 */
158int ext4_filesystem_check_sanity(ext4_filesystem_t *fs)
159{
160 /* Check superblock */
161 return ext4_superblock_check_sanity(fs->superblock);
162}
163
164/** Check filesystem's features, if supported by this driver
165 *
166 * Function can return EOK and set read_only flag. It mean's that
167 * there are some not-supported features, that can cause problems
168 * during some write operations.
169 *
170 * @param fs Filesystem to be checked
171 * @param read_only Flag if filesystem should be mounted only for reading
172 *
173 * @return Error code
174 *
175 */
176int ext4_filesystem_check_features(ext4_filesystem_t *fs, bool *read_only)
177{
178 /* Feature flags are present only in higher revisions */
179 if (ext4_superblock_get_rev_level(fs->superblock) == 0) {
180 *read_only = false;
181 return EOK;
182 }
183
184 /*
185 * Check incompatible features - if filesystem has some,
186 * volume can't be mounted
187 */
188 uint32_t incompatible_features;
189 incompatible_features =
190 ext4_superblock_get_features_incompatible(fs->superblock);
191 incompatible_features &= ~EXT4_FEATURE_INCOMPAT_SUPP;
192 if (incompatible_features > 0)
193 return ENOTSUP;
194
195 /*
196 * Check read-only features, if filesystem has some,
197 * volume can be mount only in read-only mode
198 */
199 uint32_t compatible_read_only;
200 compatible_read_only =
201 ext4_superblock_get_features_read_only(fs->superblock);
202 compatible_read_only &= ~EXT4_FEATURE_RO_COMPAT_SUPP;
203 if (compatible_read_only > 0) {
204 *read_only = true;
205 return EOK;
206 }
207
208 return EOK;
209}
210
211
212/** Convert block address to relative index in block group.
213 *
214 * @param sb Superblock pointer
215 * @param block_addr Block number to convert
216 *
217 * @return Relative number of block
218 *
219 */
220uint32_t ext4_filesystem_blockaddr2_index_in_group(ext4_superblock_t *sb,
221 uint32_t block_addr)
222{
223 uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
224 uint32_t first_block = ext4_superblock_get_first_data_block(sb);
225
226 /* First block == 0 or 1 */
227 if (first_block == 0)
228 return block_addr % blocks_per_group;
229 else
230 return (block_addr - 1) % blocks_per_group;
231}
232
233
234/** Convert relative block address in group to absolute address.
235 *
236 * @param sb Superblock pointer
237 *
238 * @return Absolute block address
239 *
240 */
241uint32_t ext4_filesystem_index_in_group2blockaddr(ext4_superblock_t *sb,
242 uint32_t index, uint32_t bgid)
243{
244 uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
245
246 if (ext4_superblock_get_first_data_block(sb) == 0)
247 return bgid * blocks_per_group + index;
248 else
249 return bgid * blocks_per_group + index + 1;
250}
251
252/** Initialize block bitmap in block group.
253 *
254 * @param bg_ref Reference to block group
255 *
256 * @return Error code
257 *
258 */
259static int ext4_filesystem_init_block_bitmap(ext4_block_group_ref_t *bg_ref)
260{
261 /* Load bitmap */
262 uint32_t bitmap_block_addr = ext4_block_group_get_block_bitmap(
263 bg_ref->block_group, bg_ref->fs->superblock);
264
265 block_t *bitmap_block;
266 int rc = block_get(&bitmap_block, bg_ref->fs->device,
267 bitmap_block_addr, BLOCK_FLAGS_NOREAD);
268 if (rc != EOK)
269 return rc;
270
271 uint8_t *bitmap = bitmap_block->data;
272
273 /* Initialize all bitmap bits to zero */
274 uint32_t block_size = ext4_superblock_get_block_size(bg_ref->fs->superblock);
275 memset(bitmap, 0, block_size);
276
277 /* Determine first block and first data block in group */
278 uint32_t first_idx = 0;
279
280 uint32_t first_data = ext4_balloc_get_first_data_block_in_group(
281 bg_ref->fs->superblock, bg_ref);
282 uint32_t first_data_idx = ext4_filesystem_blockaddr2_index_in_group(
283 bg_ref->fs->superblock, first_data);
284
285 /* Set bits from to first block to first data block - 1 to one (allocated) */
286 for (uint32_t block = first_idx; block < first_data_idx; ++block)
287 ext4_bitmap_set_bit(bitmap, block);
288
289 bitmap_block->dirty = true;
290
291 /* Save bitmap */
292 return block_put(bitmap_block);
293}
294
295/** Initialize i-node bitmap in block group.
296 *
297 * @param bg_ref Reference to block group
298 *
299 * @return Error code
300 *
301 */
302static int ext4_filesystem_init_inode_bitmap(ext4_block_group_ref_t *bg_ref)
303{
304 /* Load bitmap */
305 uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
306 bg_ref->block_group, bg_ref->fs->superblock);
307 block_t *bitmap_block;
308
309 int rc = block_get(&bitmap_block, bg_ref->fs->device,
310 bitmap_block_addr, BLOCK_FLAGS_NOREAD);
311 if (rc != EOK)
312 return rc;
313
314 uint8_t *bitmap = bitmap_block->data;
315
316 /* Initialize all bitmap bits to zero */
317 uint32_t block_size = ext4_superblock_get_block_size(bg_ref->fs->superblock);
318 uint32_t inodes_per_group =
319 ext4_superblock_get_inodes_per_group(bg_ref->fs->superblock);
320 memset(bitmap, 0, (inodes_per_group + 7) / 8);
321
322 uint32_t start_bit = inodes_per_group;
323 uint32_t end_bit = block_size * 8;
324
325 uint32_t i;
326 for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
327 ext4_bitmap_set_bit(bitmap, i);
328
329 if (i < end_bit)
330 memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
331
332 bitmap_block->dirty = true;
333
334 /* Save bitmap */
335 return block_put(bitmap_block);
336}
337
338/** Initialize i-node table in block group.
339 *
340 * @param bg_ref Reference to block group
341 *
342 * @return Error code
343 *
344 */
345static int ext4_filesystem_init_inode_table(ext4_block_group_ref_t *bg_ref)
346{
347 ext4_superblock_t *sb = bg_ref->fs->superblock;
348
349 uint32_t inode_size = ext4_superblock_get_inode_size(sb);
350 uint32_t block_size = ext4_superblock_get_block_size(sb);
351 uint32_t inodes_per_block = block_size / inode_size;
352
353 uint32_t inodes_in_group =
354 ext4_superblock_get_inodes_in_group(sb, bg_ref->index);
355
356 uint32_t table_blocks = inodes_in_group / inodes_per_block;
357
358 if (inodes_in_group % inodes_per_block)
359 table_blocks++;
360
361 /* Compute initialization bounds */
362 uint32_t first_block = ext4_block_group_get_inode_table_first_block(
363 bg_ref->block_group, sb);
364
365 uint32_t last_block = first_block + table_blocks - 1;
366
367 /* Initialization of all itable blocks */
368 for (uint32_t fblock = first_block; fblock <= last_block; ++fblock) {
369 block_t *block;
370 int rc = block_get(&block, bg_ref->fs->device, fblock,
371 BLOCK_FLAGS_NOREAD);
372 if (rc != EOK)
373 return rc;
374
375 memset(block->data, 0, block_size);
376 block->dirty = true;
377
378 rc = block_put(block);
379 if (rc != EOK)
380 return rc;
381 }
382
383 return EOK;
384}
385
386/** Get reference to block group specified by index.
387 *
388 * @param fs Filesystem to find block group on
389 * @param bgid Index of block group to load
390 * @param ref Output pointer for reference
391 *
392 * @return Error code
393 *
394 */
395int ext4_filesystem_get_block_group_ref(ext4_filesystem_t *fs, uint32_t bgid,
396 ext4_block_group_ref_t **ref)
397{
398 /* Allocate memory for new structure */
399 ext4_block_group_ref_t *newref =
400 malloc(sizeof(ext4_block_group_ref_t));
401 if (newref == NULL)
402 return ENOMEM;
403
404 /* Compute number of descriptors, that fits in one data block */
405 uint32_t descriptors_per_block =
406 ext4_superblock_get_block_size(fs->superblock) /
407 ext4_superblock_get_desc_size(fs->superblock);
408
409 /* Block group descriptor table starts at the next block after superblock */
410 aoff64_t block_id =
411 ext4_superblock_get_first_data_block(fs->superblock) + 1;
412
413 /* Find the block containing the descriptor we are looking for */
414 block_id += bgid / descriptors_per_block;
415 uint32_t offset = (bgid % descriptors_per_block) *
416 ext4_superblock_get_desc_size(fs->superblock);
417
418 /* Load block with descriptors */
419 int rc = block_get(&newref->block, fs->device, block_id, 0);
420 if (rc != EOK) {
421 free(newref);
422 return rc;
423 }
424
425 /* Inititialize in-memory representation */
426 newref->block_group = newref->block->data + offset;
427 newref->fs = fs;
428 newref->index = bgid;
429 newref->dirty = false;
430
431 *ref = newref;
432
433 if (ext4_block_group_has_flag(newref->block_group,
434 EXT4_BLOCK_GROUP_BLOCK_UNINIT)) {
435 rc = ext4_filesystem_init_block_bitmap(newref);
436 if (rc != EOK) {
437 block_put(newref->block);
438 free(newref);
439 return rc;
440 }
441
442 ext4_block_group_clear_flag(newref->block_group,
443 EXT4_BLOCK_GROUP_BLOCK_UNINIT);
444
445 newref->dirty = true;
446 }
447
448 if (ext4_block_group_has_flag(newref->block_group,
449 EXT4_BLOCK_GROUP_INODE_UNINIT)) {
450 rc = ext4_filesystem_init_inode_bitmap(newref);
451 if (rc != EOK) {
452 block_put(newref->block);
453 free(newref);
454 return rc;
455 }
456
457 ext4_block_group_clear_flag(newref->block_group,
458 EXT4_BLOCK_GROUP_INODE_UNINIT);
459
460 if (!ext4_block_group_has_flag(newref->block_group,
461 EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
462 rc = ext4_filesystem_init_inode_table(newref);
463 if (rc != EOK)
464 return rc;
465
466 ext4_block_group_set_flag(newref->block_group,
467 EXT4_BLOCK_GROUP_ITABLE_ZEROED);
468 }
469
470 newref->dirty = true;
471 }
472
473 return EOK;
474}
475
476/** Compute checksum of block group descriptor.
477 *
478 * @param sb Superblock
479 * @param bgid Index of block group in the filesystem
480 * @param bg Block group to compute checksum for
481 *
482 * @return Checksum value
483 *
484 */
485static uint16_t ext4_filesystem_bg_checksum(ext4_superblock_t *sb, uint32_t bgid,
486 ext4_block_group_t *bg)
487{
488 /* If checksum not supported, 0 will be returned */
489 uint16_t crc = 0;
490
491 /* Compute the checksum only if the filesystem supports it */
492 if (ext4_superblock_has_feature_read_only(sb,
493 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
494 void *base = bg;
495 void *checksum = &bg->checksum;
496
497 uint32_t offset = (uint32_t) (checksum - base);
498
499 /* Convert block group index to little endian */
500 uint32_t le_group = host2uint32_t_le(bgid);
501
502 /* Initialization */
503 crc = crc16(~0, sb->uuid, sizeof(sb->uuid));
504
505 /* Include index of block group */
506 crc = crc16(crc, (uint8_t *) &le_group, sizeof(le_group));
507
508 /* Compute crc from the first part (stop before checksum field) */
509 crc = crc16(crc, (uint8_t *) bg, offset);
510
511 /* Skip checksum */
512 offset += sizeof(bg->checksum);
513
514 /* Checksum of the rest of block group descriptor */
515 if ((ext4_superblock_has_feature_incompatible(sb,
516 EXT4_FEATURE_INCOMPAT_64BIT)) &&
517 (offset < ext4_superblock_get_desc_size(sb)))
518 crc = crc16(crc, ((uint8_t *) bg) + offset,
519 ext4_superblock_get_desc_size(sb) - offset);
520 }
521
522 return crc;
523}
524
525/** Put reference to block group.
526 *
527 * @oaram ref Pointer for reference to be put back
528 *
529 * @return Error code
530 *
531 */
532int ext4_filesystem_put_block_group_ref(ext4_block_group_ref_t *ref)
533{
534 /* Check if reference modified */
535 if (ref->dirty) {
536 /* Compute new checksum of block group */
537 uint16_t checksum =
538 ext4_filesystem_bg_checksum(ref->fs->superblock, ref->index,
539 ref->block_group);
540 ext4_block_group_set_checksum(ref->block_group, checksum);
541
542 /* Mark block dirty for writing changes to physical device */
543 ref->block->dirty = true;
544 }
545
546 /* Put back block, that contains block group descriptor */
547 int rc = block_put(ref->block);
548 free(ref);
549
550 return rc;
551}
552
553/** Get reference to i-node specified by index.
554 *
555 * @param fs Filesystem to find i-node on
556 * @param index Index of i-node to load
557 * @oaram ref Output pointer for reference
558 *
559 * @return Error code
560 *
561 */
562int ext4_filesystem_get_inode_ref(ext4_filesystem_t *fs, uint32_t index,
563 ext4_inode_ref_t **ref)
564{
565 /* Allocate memory for new structure */
566 ext4_inode_ref_t *newref =
567 malloc(sizeof(ext4_inode_ref_t));
568 if (newref == NULL)
569 return ENOMEM;
570
571 /* Compute number of i-nodes, that fits in one data block */
572 uint32_t inodes_per_group =
573 ext4_superblock_get_inodes_per_group(fs->superblock);
574
575 /*
576 * Inode numbers are 1-based, but it is simpler to work with 0-based
577 * when computing indices
578 */
579 index -= 1;
580 uint32_t block_group = index / inodes_per_group;
581 uint32_t offset_in_group = index % inodes_per_group;
582
583 /* Load block group, where i-node is located */
584 ext4_block_group_ref_t *bg_ref;
585 int rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
586 if (rc != EOK) {
587 free(newref);
588 return rc;
589 }
590
591 /* Load block address, where i-node table is located */
592 uint32_t inode_table_start =
593 ext4_block_group_get_inode_table_first_block(bg_ref->block_group,
594 fs->superblock);
595
596 /* Put back block group reference (not needed more) */
597 rc = ext4_filesystem_put_block_group_ref(bg_ref);
598 if (rc != EOK) {
599 free(newref);
600 return rc;
601 }
602
603 /* Compute position of i-node in the block group */
604 uint16_t inode_size = ext4_superblock_get_inode_size(fs->superblock);
605 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
606 uint32_t byte_offset_in_group = offset_in_group * inode_size;
607
608 /* Compute block address */
609 aoff64_t block_id = inode_table_start + (byte_offset_in_group / block_size);
610 rc = block_get(&newref->block, fs->device, block_id, 0);
611 if (rc != EOK) {
612 free(newref);
613 return rc;
614 }
615
616 /* Compute position of i-node in the data block */
617 uint32_t offset_in_block = byte_offset_in_group % block_size;
618 newref->inode = newref->block->data + offset_in_block;
619
620 /* We need to store the original value of index in the reference */
621 newref->index = index + 1;
622 newref->fs = fs;
623 newref->dirty = false;
624
625 *ref = newref;
626
627 return EOK;
628}
629
630/** Put reference to i-node.
631 *
632 * @param ref Pointer for reference to be put back
633 *
634 * @return Error code
635 *
636 */
637int ext4_filesystem_put_inode_ref(ext4_inode_ref_t *ref)
638{
639 /* Check if reference modified */
640 if (ref->dirty) {
641 /* Mark block dirty for writing changes to physical device */
642 ref->block->dirty = true;
643 }
644
645 /* Put back block, that contains i-node */
646 int rc = block_put(ref->block);
647 free(ref);
648
649 return rc;
650}
651
652/** Allocate new i-node in the filesystem.
653 *
654 * @param fs Filesystem to allocated i-node on
655 * @param inode_ref Output pointer to return reference to allocated i-node
656 * @param flags Flags to be set for newly created i-node
657 *
658 * @return Error code
659 *
660 */
661int ext4_filesystem_alloc_inode(ext4_filesystem_t *fs,
662 ext4_inode_ref_t **inode_ref, int flags)
663{
664 /* Check if newly allocated i-node will be a directory */
665 bool is_dir = false;
666 if (flags & L_DIRECTORY)
667 is_dir = true;
668
669 /* Allocate inode by allocation algorithm */
670 uint32_t index;
671 int rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
672 if (rc != EOK)
673 return rc;
674
675 /* Load i-node from on-disk i-node table */
676 rc = ext4_filesystem_get_inode_ref(fs, index, inode_ref);
677 if (rc != EOK) {
678 ext4_ialloc_free_inode(fs, index, is_dir);
679 return rc;
680 }
681
682 /* Initialize i-node */
683 ext4_inode_t *inode = (*inode_ref)->inode;
684
685 uint16_t mode;
686 if (is_dir) {
687 /*
688 * Default directory permissions to be compatible with other systems
689 * 0777 (octal) == rwxrwxrwx
690 */
691
692 mode = 0777;
693 mode |= EXT4_INODE_MODE_DIRECTORY;
694 ext4_inode_set_mode(fs->superblock, inode, mode);
695 ext4_inode_set_links_count(inode, 1); /* '.' entry */
696 } else {
697 /*
698 * Default file permissions to be compatible with other systems
699 * 0666 (octal) == rw-rw-rw-
700 */
701
702 mode = 0666;
703 mode |= EXT4_INODE_MODE_FILE;
704 ext4_inode_set_mode(fs->superblock, inode, mode);
705 ext4_inode_set_links_count(inode, 0);
706 }
707
708 ext4_inode_set_uid(inode, 0);
709 ext4_inode_set_gid(inode, 0);
710 ext4_inode_set_size(inode, 0);
711 ext4_inode_set_access_time(inode, 0);
712 ext4_inode_set_change_inode_time(inode, 0);
713 ext4_inode_set_modification_time(inode, 0);
714 ext4_inode_set_deletion_time(inode, 0);
715 ext4_inode_set_blocks_count(fs->superblock, inode, 0);
716 ext4_inode_set_flags(inode, 0);
717 ext4_inode_set_generation(inode, 0);
718
719 /* Reset blocks array */
720 for (uint32_t i = 0; i < EXT4_INODE_BLOCKS; i++)
721 inode->blocks[i] = 0;
722
723 /* Initialize extents if needed */
724 if (ext4_superblock_has_feature_incompatible(
725 fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
726 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
727
728 /* Initialize extent root header */
729 ext4_extent_header_t *header = ext4_inode_get_extent_header(inode);
730 ext4_extent_header_set_depth(header, 0);
731 ext4_extent_header_set_entries_count(header, 0);
732 ext4_extent_header_set_generation(header, 0);
733 ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
734
735 uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) -
736 sizeof(ext4_extent_header_t)) / sizeof(ext4_extent_t);
737
738 ext4_extent_header_set_max_entries_count(header, max_entries);
739 }
740
741 (*inode_ref)->dirty = true;
742
743 return EOK;
744}
745
746/** Release i-node and mark it as free.
747 *
748 * @param inode_ref I-node to be released
749 *
750 * @return Error code
751 *
752 */
753int ext4_filesystem_free_inode(ext4_inode_ref_t *inode_ref)
754{
755 ext4_filesystem_t *fs = inode_ref->fs;
756
757 /* For extents must be data block destroyed by other way */
758 if ((ext4_superblock_has_feature_incompatible(fs->superblock,
759 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
760 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
761 /* Data structures are released during truncate operation... */
762 goto finish;
763 }
764
765 /* Release all indirect (no data) blocks */
766
767 /* 1) Single indirect */
768 uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
769 if (fblock != 0) {
770 int rc = ext4_balloc_free_block(inode_ref, fblock);
771 if (rc != EOK)
772 return rc;
773
774 ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
775 }
776
777 block_t *block;
778 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
779 uint32_t count = block_size / sizeof(uint32_t);
780
781 /* 2) Double indirect */
782 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
783 if (fblock != 0) {
784 int rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
785 if (rc != EOK)
786 return rc;
787
788 uint32_t ind_block;
789 for (uint32_t offset = 0; offset < count; ++offset) {
790 ind_block = uint32_t_le2host(((uint32_t *) block->data)[offset]);
791
792 if (ind_block != 0) {
793 rc = ext4_balloc_free_block(inode_ref, ind_block);
794 if (rc != EOK) {
795 block_put(block);
796 return rc;
797 }
798 }
799 }
800
801 rc = block_put(block);
802 if (rc != EOK)
803 return rc;
804
805 rc = ext4_balloc_free_block(inode_ref, fblock);
806 if (rc != EOK)
807 return rc;
808
809 ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
810 }
811
812 /* 3) Tripple indirect */
813 block_t *subblock;
814 fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
815 if (fblock != 0) {
816 int rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
817 if (rc != EOK)
818 return rc;
819
820 uint32_t ind_block;
821 for (uint32_t offset = 0; offset < count; ++offset) {
822 ind_block = uint32_t_le2host(((uint32_t *) block->data)[offset]);
823
824 if (ind_block != 0) {
825 rc = block_get(&subblock, fs->device, ind_block,
826 BLOCK_FLAGS_NONE);
827 if (rc != EOK) {
828 block_put(block);
829 return rc;
830 }
831
832 uint32_t ind_subblock;
833 for (uint32_t suboffset = 0; suboffset < count;
834 ++suboffset) {
835 ind_subblock = uint32_t_le2host(((uint32_t *)
836 subblock->data)[suboffset]);
837
838 if (ind_subblock != 0) {
839 rc = ext4_balloc_free_block(inode_ref, ind_subblock);
840 if (rc != EOK) {
841 block_put(subblock);
842 block_put(block);
843 return rc;
844 }
845 }
846 }
847
848 rc = block_put(subblock);
849 if (rc != EOK) {
850 block_put(block);
851 return rc;
852 }
853 }
854
855 rc = ext4_balloc_free_block(inode_ref, ind_block);
856 if (rc != EOK) {
857 block_put(block);
858 return rc;
859 }
860 }
861
862 rc = block_put(block);
863 if (rc != EOK)
864 return rc;
865
866 rc = ext4_balloc_free_block(inode_ref, fblock);
867 if (rc != EOK)
868 return rc;
869
870 ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
871 }
872
873finish:
874 /* Mark inode dirty for writing to the physical device */
875 inode_ref->dirty = true;
876
877 /* Free block with extended attributes if present */
878 uint32_t xattr_block = ext4_inode_get_file_acl(
879 inode_ref->inode, fs->superblock);
880 if (xattr_block) {
881 int rc = ext4_balloc_free_block(inode_ref, xattr_block);
882 if (rc != EOK)
883 return rc;
884
885 ext4_inode_set_file_acl(inode_ref->inode, fs->superblock, 0);
886 }
887
888 /* Free inode by allocator */
889 int rc;
890 if (ext4_inode_is_type(fs->superblock, inode_ref->inode,
891 EXT4_INODE_MODE_DIRECTORY))
892 rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
893 else
894 rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
895
896 return rc;
897}
898
899/** Truncate i-node data blocks.
900 *
901 * @param inode_ref I-node to be truncated
902 * @param new_size New size of inode (must be < current size)
903 *
904 * @return Error code
905 *
906 */
907int ext4_filesystem_truncate_inode(ext4_inode_ref_t *inode_ref,
908 aoff64_t new_size)
909{
910 ext4_superblock_t *sb = inode_ref->fs->superblock;
911
912 /* Check flags, if i-node can be truncated */
913 if (!ext4_inode_can_truncate(sb, inode_ref->inode))
914 return EINVAL;
915
916 /* If sizes are equal, nothing has to be done. */
917 aoff64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
918 if (old_size == new_size)
919 return EOK;
920
921 /* It's not suppported to make the larger file by truncate operation */
922 if (old_size < new_size)
923 return EINVAL;
924
925 /* Compute how many blocks will be released */
926 aoff64_t size_diff = old_size - new_size;
927 uint32_t block_size = ext4_superblock_get_block_size(sb);
928 uint32_t diff_blocks_count = size_diff / block_size;
929 if (size_diff % block_size != 0)
930 diff_blocks_count++;
931
932 uint32_t old_blocks_count = old_size / block_size;
933 if (old_size % block_size != 0)
934 old_blocks_count++;
935
936 if ((ext4_superblock_has_feature_incompatible(inode_ref->fs->superblock,
937 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
938 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
939 /* Extents require special operation */
940 int rc = ext4_extent_release_blocks_from(inode_ref,
941 old_blocks_count - diff_blocks_count);
942 if (rc != EOK)
943 return rc;
944 } else {
945 /* Release data blocks from the end of file */
946
947 /* Starting from 1 because of logical blocks are numbered from 0 */
948 for (uint32_t i = 1; i <= diff_blocks_count; ++i) {
949 int rc = ext4_filesystem_release_inode_block(inode_ref,
950 old_blocks_count - i);
951 if (rc != EOK)
952 return rc;
953 }
954 }
955
956 /* Update i-node */
957 ext4_inode_set_size(inode_ref->inode, new_size);
958 inode_ref->dirty = true;
959
960 return EOK;
961}
962
963/** Get physical block address by logical index of the block.
964 *
965 * @param inode_ref I-node to read block address from
966 * @param iblock Logical index of block
967 * @param fblock Output pointer for return physical block address
968 *
969 * @return Error code
970 *
971 */
972int ext4_filesystem_get_inode_data_block_index(ext4_inode_ref_t *inode_ref,
973 aoff64_t iblock, uint32_t *fblock)
974{
975 ext4_filesystem_t *fs = inode_ref->fs;
976
977 /* For empty file is situation simple */
978 if (ext4_inode_get_size(fs->superblock, inode_ref->inode) == 0) {
979 *fblock = 0;
980 return EOK;
981 }
982
983 uint32_t current_block;
984
985 /* Handle i-node using extents */
986 if ((ext4_superblock_has_feature_incompatible(fs->superblock,
987 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
988 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
989 int rc = ext4_extent_find_block(inode_ref, iblock, &current_block);
990 if (rc != EOK)
991 return rc;
992
993 *fblock = current_block;
994 return EOK;
995 }
996
997 ext4_inode_t *inode = inode_ref->inode;
998
999 /* Direct block are read directly from array in i-node structure */
1000 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1001 current_block = ext4_inode_get_direct_block(inode, (uint32_t) iblock);
1002 *fblock = current_block;
1003 return EOK;
1004 }
1005
1006 /* Determine indirection level of the target block */
1007 unsigned int level = 0;
1008 for (unsigned int i = 1; i < 4; i++) {
1009 if (iblock < fs->inode_block_limits[i]) {
1010 level = i;
1011 break;
1012 }
1013 }
1014
1015 if (level == 0)
1016 return EIO;
1017
1018 /* Compute offsets for the topmost level */
1019 aoff64_t block_offset_in_level =
1020 iblock - fs->inode_block_limits[level - 1];
1021 current_block = ext4_inode_get_indirect_block(inode, level - 1);
1022 uint32_t offset_in_block =
1023 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1024
1025 /* Sparse file */
1026 if (current_block == 0) {
1027 *fblock = 0;
1028 return EOK;
1029 }
1030
1031 block_t *block;
1032
1033 /*
1034 * Navigate through other levels, until we find the block number
1035 * or find null reference meaning we are dealing with sparse file
1036 */
1037 while (level > 0) {
1038 /* Load indirect block */
1039 int rc = block_get(&block, fs->device, current_block, 0);
1040 if (rc != EOK)
1041 return rc;
1042
1043 /* Read block address from indirect block */
1044 current_block =
1045 uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
1046
1047 /* Put back indirect block untouched */
1048 rc = block_put(block);
1049 if (rc != EOK)
1050 return rc;
1051
1052 /* Check for sparse file */
1053 if (current_block == 0) {
1054 *fblock = 0;
1055 return EOK;
1056 }
1057
1058 /* Jump to the next level */
1059 level--;
1060
1061 /* Termination condition - we have address of data block loaded */
1062 if (level == 0)
1063 break;
1064
1065 /* Visit the next level */
1066 block_offset_in_level %= fs->inode_blocks_per_level[level];
1067 offset_in_block =
1068 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1069 }
1070
1071 *fblock = current_block;
1072
1073 return EOK;
1074}
1075
1076/** Set physical block address for the block logical address into the i-node.
1077 *
1078 * @param inode_ref I-node to set block address to
1079 * @param iblock Logical index of block
1080 * @param fblock Physical block address
1081 *
1082 * @return Error code
1083 *
1084 */
1085int ext4_filesystem_set_inode_data_block_index(ext4_inode_ref_t *inode_ref,
1086 aoff64_t iblock, uint32_t fblock)
1087{
1088 ext4_filesystem_t *fs = inode_ref->fs;
1089
1090 /* Handle inode using extents */
1091 if ((ext4_superblock_has_feature_compatible(fs->superblock,
1092 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1093 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
1094 /* Not reachable */
1095 return ENOTSUP;
1096 }
1097
1098 /* Handle simple case when we are dealing with direct reference */
1099 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1100 ext4_inode_set_direct_block(inode_ref->inode, (uint32_t) iblock, fblock);
1101 inode_ref->dirty = true;
1102
1103 return EOK;
1104 }
1105
1106 /* Determine the indirection level needed to get the desired block */
1107 unsigned int level = 0;
1108 for (unsigned int i = 1; i < 4; i++) {
1109 if (iblock < fs->inode_block_limits[i]) {
1110 level = i;
1111 break;
1112 }
1113 }
1114
1115 if (level == 0)
1116 return EIO;
1117
1118 uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
1119
1120 /* Compute offsets for the topmost level */
1121 aoff64_t block_offset_in_level =
1122 iblock - fs->inode_block_limits[level - 1];
1123 uint32_t current_block =
1124 ext4_inode_get_indirect_block(inode_ref->inode, level - 1);
1125 uint32_t offset_in_block =
1126 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1127
1128 uint32_t new_block_addr;
1129 block_t *block;
1130 block_t *new_block;
1131
1132 /* Is needed to allocate indirect block on the i-node level */
1133 if (current_block == 0) {
1134 /* Allocate new indirect block */
1135 int rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1136 if (rc != EOK)
1137 return rc;
1138
1139 /* Update i-node */
1140 ext4_inode_set_indirect_block(inode_ref->inode, level - 1,
1141 new_block_addr);
1142 inode_ref->dirty = true;
1143
1144 /* Load newly allocated block */
1145 rc = block_get(&new_block, fs->device, new_block_addr,
1146 BLOCK_FLAGS_NOREAD);
1147 if (rc != EOK) {
1148 ext4_balloc_free_block(inode_ref, new_block_addr);
1149 return rc;
1150 }
1151
1152 /* Initialize new block */
1153 memset(new_block->data, 0, block_size);
1154 new_block->dirty = true;
1155
1156 /* Put back the allocated block */
1157 rc = block_put(new_block);
1158 if (rc != EOK)
1159 return rc;
1160
1161 current_block = new_block_addr;
1162 }
1163
1164 /*
1165 * Navigate through other levels, until we find the block number
1166 * or find null reference meaning we are dealing with sparse file
1167 */
1168 while (level > 0) {
1169 int rc = block_get(&block, fs->device, current_block, 0);
1170 if (rc != EOK)
1171 return rc;
1172
1173 current_block =
1174 uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
1175
1176 if ((level > 1) && (current_block == 0)) {
1177 /* Allocate new block */
1178 rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
1179 if (rc != EOK) {
1180 block_put(block);
1181 return rc;
1182 }
1183
1184 /* Load newly allocated block */
1185 rc = block_get(&new_block, fs->device, new_block_addr,
1186 BLOCK_FLAGS_NOREAD);
1187 if (rc != EOK) {
1188 block_put(block);
1189 return rc;
1190 }
1191
1192 /* Initialize allocated block */
1193 memset(new_block->data, 0, block_size);
1194 new_block->dirty = true;
1195
1196 rc = block_put(new_block);
1197 if (rc != EOK) {
1198 block_put(block);
1199 return rc;
1200 }
1201
1202 /* Write block address to the parent */
1203 ((uint32_t *) block->data)[offset_in_block] =
1204 host2uint32_t_le(new_block_addr);
1205 block->dirty = true;
1206 current_block = new_block_addr;
1207 }
1208
1209 /* Will be finished, write the fblock address */
1210 if (level == 1) {
1211 ((uint32_t *) block->data)[offset_in_block] =
1212 host2uint32_t_le(fblock);
1213 block->dirty = true;
1214 }
1215
1216 rc = block_put(block);
1217 if (rc != EOK)
1218 return rc;
1219
1220 level--;
1221
1222 /*
1223 * If we are on the last level, break here as
1224 * there is no next level to visit
1225 */
1226 if (level == 0)
1227 break;
1228
1229 /* Visit the next level */
1230 block_offset_in_level %= fs->inode_blocks_per_level[level];
1231 offset_in_block =
1232 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1233 }
1234
1235 return EOK;
1236}
1237
1238/** Release data block from i-node
1239 *
1240 * @param inode_ref I-node to release block from
1241 * @param iblock Logical block to be released
1242 *
1243 * @return Error code
1244 *
1245 */
1246int ext4_filesystem_release_inode_block(ext4_inode_ref_t *inode_ref,
1247 uint32_t iblock)
1248{
1249 uint32_t fblock;
1250
1251 ext4_filesystem_t *fs = inode_ref->fs;
1252
1253 /* Extents are handled otherwise = there is not support in this function */
1254 assert(!(ext4_superblock_has_feature_incompatible(fs->superblock,
1255 EXT4_FEATURE_INCOMPAT_EXTENTS) &&
1256 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))));
1257
1258 ext4_inode_t *inode = inode_ref->inode;
1259
1260 /* Handle simple case when we are dealing with direct reference */
1261 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
1262 fblock = ext4_inode_get_direct_block(inode, iblock);
1263
1264 /* Sparse file */
1265 if (fblock == 0)
1266 return EOK;
1267
1268 ext4_inode_set_direct_block(inode, iblock, 0);
1269 return ext4_balloc_free_block(inode_ref, fblock);
1270 }
1271
1272 /* Determine the indirection level needed to get the desired block */
1273 unsigned int level = 0;
1274 for (unsigned int i = 1; i < 4; i++) {
1275 if (iblock < fs->inode_block_limits[i]) {
1276 level = i;
1277 break;
1278 }
1279 }
1280
1281 if (level == 0)
1282 return EIO;
1283
1284 /* Compute offsets for the topmost level */
1285 aoff64_t block_offset_in_level =
1286 iblock - fs->inode_block_limits[level - 1];
1287 uint32_t current_block =
1288 ext4_inode_get_indirect_block(inode, level - 1);
1289 uint32_t offset_in_block =
1290 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1291
1292 /*
1293 * Navigate through other levels, until we find the block number
1294 * or find null reference meaning we are dealing with sparse file
1295 */
1296 block_t *block;
1297 while (level > 0) {
1298
1299 /* Sparse check */
1300 if (current_block == 0)
1301 return EOK;
1302
1303 int rc = block_get(&block, fs->device, current_block, 0);
1304 if (rc != EOK)
1305 return rc;
1306
1307 current_block =
1308 uint32_t_le2host(((uint32_t *) block->data)[offset_in_block]);
1309
1310 /* Set zero if physical data block address found */
1311 if (level == 1) {
1312 ((uint32_t *) block->data)[offset_in_block] =
1313 host2uint32_t_le(0);
1314 block->dirty = true;
1315 }
1316
1317 rc = block_put(block);
1318 if (rc != EOK)
1319 return rc;
1320
1321 level--;
1322
1323 /*
1324 * If we are on the last level, break here as
1325 * there is no next level to visit
1326 */
1327 if (level == 0)
1328 break;
1329
1330 /* Visit the next level */
1331 block_offset_in_level %= fs->inode_blocks_per_level[level];
1332 offset_in_block =
1333 block_offset_in_level / fs->inode_blocks_per_level[level - 1];
1334 }
1335
1336 fblock = current_block;
1337 if (fblock == 0)
1338 return EOK;
1339
1340 /* Physical block is not referenced, it can be released */
1341 return ext4_balloc_free_block(inode_ref, fblock);
1342}
1343
1344/** Append following logical block to the i-node.
1345 *
1346 * @param inode_ref I-node to append block to
1347 * @param fblock Output physical block address of newly allocated block
1348 * @param iblock Output logical number of newly allocated block
1349 *
1350 * @return Error code
1351 *
1352 */
1353int ext4_filesystem_append_inode_block(ext4_inode_ref_t *inode_ref,
1354 uint32_t *fblock, uint32_t *iblock)
1355{
1356 /* Handle extents separately */
1357 if ((ext4_superblock_has_feature_incompatible(inode_ref->fs->superblock,
1358 EXT4_FEATURE_INCOMPAT_EXTENTS)) &&
1359 (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)))
1360 return ext4_extent_append_block(inode_ref, iblock, fblock, true);
1361
1362 ext4_superblock_t *sb = inode_ref->fs->superblock;
1363
1364 /* Compute next block index and allocate data block */
1365 uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
1366 uint32_t block_size = ext4_superblock_get_block_size(sb);
1367
1368 /* Align size i-node size */
1369 if ((inode_size % block_size) != 0)
1370 inode_size += block_size - (inode_size % block_size);
1371
1372 /* Logical blocks are numbered from 0 */
1373 uint32_t new_block_idx = inode_size / block_size;
1374
1375 /* Allocate new physical block */
1376 uint32_t phys_block;
1377 int rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
1378 if (rc != EOK)
1379 return rc;
1380
1381 /* Add physical block address to the i-node */
1382 rc = ext4_filesystem_set_inode_data_block_index(inode_ref,
1383 new_block_idx, phys_block);
1384 if (rc != EOK) {
1385 ext4_balloc_free_block(inode_ref, phys_block);
1386 return rc;
1387 }
1388
1389 /* Update i-node */
1390 ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
1391 inode_ref->dirty = true;
1392
1393 *fblock = phys_block;
1394 *iblock = new_block_idx;
1395
1396 return EOK;
1397}
1398
1399/**
1400 * @}
1401 */
Note: See TracBrowser for help on using the repository browser.