source: mainline/uspace/lib/ext4/libext4_filesystem.c@ c15849c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c15849c was c15849c, checked in by Frantisek Princ <frantisek.princ@…>, 13 years ago

removed commented code

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