source: mainline/uspace/lib/ext4/libext4_filesystem.c@ 2d53cfc

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2d53cfc was 0d8322da, checked in by Jakub Jermar <jakub@…>, 13 years ago

Remove GPL licensed code from libext4.

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