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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d1538a1 was d1538a1, checked in by Martin Sucha <sucha14@…>, 13 years ago

Add missing copyright headers to ext4

Those files are based on ext2 filesystem driver code.

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