| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 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_superblock.c
|
|---|
| 35 | * @brief Ext4 superblock operations.
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include <byteorder.h>
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #include <libblock.h>
|
|---|
| 41 | #include <malloc.h>
|
|---|
| 42 | #include "libext4.h"
|
|---|
| 43 |
|
|---|
| 44 | uint32_t ext4_superblock_get_inodes_count(ext4_superblock_t *sb)
|
|---|
| 45 | {
|
|---|
| 46 | return uint32_t_le2host(sb->inodes_count);
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | uint64_t ext4_superblock_get_blocks_count(ext4_superblock_t *sb)
|
|---|
| 50 | {
|
|---|
| 51 | return ((uint64_t)uint32_t_le2host(sb->blocks_count_hi) << 32) |
|
|---|
| 52 | uint32_t_le2host(sb->blocks_count_lo);
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | uint64_t ext4_superblock_get_reserved_blocks_count(ext4_superblock_t *sb)
|
|---|
| 56 | {
|
|---|
| 57 | return ((uint64_t)uint32_t_le2host(sb->reserved_blocks_count_hi) << 32) |
|
|---|
| 58 | uint32_t_le2host(sb->reserved_blocks_count_lo);
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | uint64_t ext4_superblock_get_free_blocks_count(ext4_superblock_t *sb)
|
|---|
| 62 | {
|
|---|
| 63 | return ((uint64_t)uint32_t_le2host(sb->free_blocks_count_hi) << 32) |
|
|---|
| 64 | uint32_t_le2host(sb->free_blocks_count_lo);
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | uint32_t ext4_superblock_get_free_inodes_count(ext4_superblock_t *sb)
|
|---|
| 68 | {
|
|---|
| 69 | return uint32_t_le2host(sb->free_inodes_count);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | uint32_t ext4_superblock_get_first_data_block(ext4_superblock_t *sb)
|
|---|
| 73 | {
|
|---|
| 74 | return uint32_t_le2host(sb->first_data_block);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | uint32_t ext4_superblock_get_log_block_size(ext4_superblock_t *sb)
|
|---|
| 78 | {
|
|---|
| 79 | return uint32_t_le2host(sb->log_block_size);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | uint32_t ext4_superblock_get_block_size(ext4_superblock_t *sb)
|
|---|
| 83 | {
|
|---|
| 84 | return 1024 << ext4_superblock_get_log_block_size(sb);
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 | uint32_t ext4_superblock_get_blocks_per_group(ext4_superblock_t *sb)
|
|---|
| 89 | {
|
|---|
| 90 | return uint32_t_le2host(sb->blocks_per_group);
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | uint32_t ext4_superblock_get_inodes_per_group(ext4_superblock_t *sb)
|
|---|
| 94 | {
|
|---|
| 95 | return uint32_t_le2host(sb->inodes_per_group);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | uint32_t ext4_superblock_get_mount_time(ext4_superblock_t *sb)
|
|---|
| 99 | {
|
|---|
| 100 | return uint32_t_le2host(sb->mount_time);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | uint32_t ext4_superblock_get_write_time(ext4_superblock_t *sb)
|
|---|
| 104 | {
|
|---|
| 105 | return uint32_t_le2host(sb->write_time);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | uint16_t ext4_superblock_get_mount_count(ext4_superblock_t *sb)
|
|---|
| 109 | {
|
|---|
| 110 | return uint16_t_le2host(sb->mount_count);
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | uint16_t ext4_superblock_get_max_mount_count(ext4_superblock_t *sb)
|
|---|
| 114 | {
|
|---|
| 115 | return uint16_t_le2host(sb->max_mount_count);
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | uint16_t ext4_superblock_get_magic(ext4_superblock_t *sb)
|
|---|
| 119 | {
|
|---|
| 120 | return uint16_t_le2host(sb->magic);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | uint16_t ext4_superblock_get_state(ext4_superblock_t *sb)
|
|---|
| 124 | {
|
|---|
| 125 | return uint16_t_le2host(sb->state);
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | uint16_t ext4_superblock_get_errors(ext4_superblock_t *sb)
|
|---|
| 129 | {
|
|---|
| 130 | return uint16_t_le2host(sb->errors);
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 | uint16_t ext4_superblock_get_minor_rev_level(ext4_superblock_t *sb)
|
|---|
| 135 | {
|
|---|
| 136 | return uint16_t_le2host(sb->minor_rev_level);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | uint32_t ext4_superblock_get_last_check_time(ext4_superblock_t *sb)
|
|---|
| 140 | {
|
|---|
| 141 | return uint32_t_le2host(sb->last_check_time);
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | uint32_t ext4_superblock_get_check_interval(ext4_superblock_t *sb){
|
|---|
| 145 | return uint32_t_le2host(sb->check_interval);
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | uint32_t ext4_superblock_get_creator_os(ext4_superblock_t *sb)
|
|---|
| 149 | {
|
|---|
| 150 | return uint32_t_le2host(sb->creator_os);
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | uint32_t ext4_superblock_get_rev_level(ext4_superblock_t *sb)
|
|---|
| 154 | {
|
|---|
| 155 | return uint32_t_le2host(sb->rev_level);
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | uint16_t ext4_superblock_get_inode_size(ext4_superblock_t *sb)
|
|---|
| 159 | {
|
|---|
| 160 | if (ext4_superblock_get_rev_level(sb) == 0) {
|
|---|
| 161 | return EXT4_REV0_INODE_SIZE;
|
|---|
| 162 | }
|
|---|
| 163 | return uint16_t_le2host(sb->inode_size);
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | uint16_t ext4_superblock_get_block_group_number(ext4_superblock_t *sb)
|
|---|
| 167 | {
|
|---|
| 168 | return uint16_t_le2host(sb->block_group_number);
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | uint32_t ext4_superblock_get_features_compatible(ext4_superblock_t *sb)
|
|---|
| 172 | {
|
|---|
| 173 | return uint32_t_le2host(sb->features_compatible);
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | uint32_t ext4_superblock_get_features_incompatible(ext4_superblock_t *sb)
|
|---|
| 177 | {
|
|---|
| 178 | return uint32_t_le2host(sb->features_incompatible);
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | uint32_t ext4_superblock_get_features_read_only(ext4_superblock_t *sb)
|
|---|
| 182 | {
|
|---|
| 183 | return uint32_t_le2host(sb->features_read_only);
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 | /*
|
|---|
| 188 | * More complex superblock functions
|
|---|
| 189 | */
|
|---|
| 190 |
|
|---|
| 191 | int ext4_superblock_read_direct(service_id_t service_id,
|
|---|
| 192 | ext4_superblock_t **superblock)
|
|---|
| 193 | {
|
|---|
| 194 | void *data;
|
|---|
| 195 | int rc;
|
|---|
| 196 |
|
|---|
| 197 | data = malloc(EXT4_SUPERBLOCK_SIZE);
|
|---|
| 198 | if (data == NULL) {
|
|---|
| 199 | return ENOMEM;
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | rc = block_read_bytes_direct(service_id, EXT4_SUPERBLOCK_OFFSET,
|
|---|
| 203 | EXT4_SUPERBLOCK_SIZE, data);
|
|---|
| 204 |
|
|---|
| 205 | if (rc != EOK) {
|
|---|
| 206 | free(data);
|
|---|
| 207 | return rc;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | (*superblock) = data;
|
|---|
| 211 |
|
|---|
| 212 | return EOK;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | int ext4_superblock_check_sanity(ext4_superblock_t *sb)
|
|---|
| 216 | {
|
|---|
| 217 | if (ext4_superblock_get_magic(sb) != EXT4_SUPERBLOCK_MAGIC) {
|
|---|
| 218 | return ENOTSUP;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | // TODO more checks !!!
|
|---|
| 222 |
|
|---|
| 223 | return EOK;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | /**
|
|---|
| 227 | * @}
|
|---|
| 228 | */
|
|---|