| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Martin Sucha
|
|---|
| 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 libext2
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include "libext2.h"
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <malloc.h>
|
|---|
| 39 | #include <libblock.h>
|
|---|
| 40 |
|
|---|
| 41 | /**
|
|---|
| 42 | * Return a magic number from ext2 superblock, this should be equal to
|
|---|
| 43 | * EXT_SUPERBLOCK_MAGIC for valid ext2 superblock
|
|---|
| 44 | *
|
|---|
| 45 | * @param sb pointer to superblock
|
|---|
| 46 | */
|
|---|
| 47 | inline uint16_t ext2_superblock_get_magic(ext2_superblock_t *sb)
|
|---|
| 48 | {
|
|---|
| 49 | return uint16_t_le2host(sb->magic);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | /**
|
|---|
| 53 | * Get the position of first ext2 data block (i.e. the block number
|
|---|
| 54 | * containing main superblock)
|
|---|
| 55 | *
|
|---|
| 56 | * @param sb pointer to superblock
|
|---|
| 57 | */
|
|---|
| 58 | inline uint32_t ext2_superblock_get_first_block(ext2_superblock_t *sb)
|
|---|
| 59 | {
|
|---|
| 60 | return uint32_t_le2host(sb->first_block);
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * Get the number of bits to shift a value of 1024 to the left necessary
|
|---|
| 65 | * to get the size of a block
|
|---|
| 66 | *
|
|---|
| 67 | * @param sb pointer to superblock
|
|---|
| 68 | */
|
|---|
| 69 | inline uint32_t ext2_superblock_get_block_size_log2(ext2_superblock_t *sb)
|
|---|
| 70 | {
|
|---|
| 71 | return uint32_t_le2host(sb->block_size_log2);
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | /**
|
|---|
| 75 | * Get the size of a block, in bytes
|
|---|
| 76 | *
|
|---|
| 77 | * @param sb pointer to superblock
|
|---|
| 78 | */
|
|---|
| 79 | inline uint32_t ext2_superblock_get_block_size(ext2_superblock_t *sb)
|
|---|
| 80 | {
|
|---|
| 81 | return 1024 << ext2_superblock_get_block_size_log2(sb);
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | /**
|
|---|
| 85 | * Get the number of bits to shift a value of 1024 to the left necessary
|
|---|
| 86 | * to get the size of a fragment (note that this is a signed integer and
|
|---|
| 87 | * if negative, the value should be shifted to the right instead)
|
|---|
| 88 | *
|
|---|
| 89 | * @param sb pointer to superblock
|
|---|
| 90 | */
|
|---|
| 91 | inline int32_t ext2_superblock_get_fragment_size_log2(ext2_superblock_t *sb)
|
|---|
| 92 | {
|
|---|
| 93 | return uint32_t_le2host(sb->fragment_size_log2);
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /**
|
|---|
| 97 | * Get the size of a fragment, in bytes
|
|---|
| 98 | *
|
|---|
| 99 | * @param sb pointer to superblock
|
|---|
| 100 | */
|
|---|
| 101 | inline uint32_t ext2_superblock_get_fragment_size(ext2_superblock_t *sb)
|
|---|
| 102 | {
|
|---|
| 103 | int32_t log = ext2_superblock_get_fragment_size_log2(sb);
|
|---|
| 104 | if (log >= 0) {
|
|---|
| 105 | return 1024 << log;
|
|---|
| 106 | }
|
|---|
| 107 | else {
|
|---|
| 108 | return 1024 >> -log;
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | /**
|
|---|
| 113 | * Get number of blocks per block group
|
|---|
| 114 | *
|
|---|
| 115 | * @param sb pointer to superblock
|
|---|
| 116 | */
|
|---|
| 117 | inline uint32_t ext2_superblock_get_blocks_per_group(ext2_superblock_t *sb)
|
|---|
| 118 | {
|
|---|
| 119 | return uint32_t_le2host(sb->blocks_per_group);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /**
|
|---|
| 123 | * Get number of fragments per block group
|
|---|
| 124 | *
|
|---|
| 125 | * @param sb pointer to superblock
|
|---|
| 126 | */
|
|---|
| 127 | inline uint32_t ext2_superblock_get_fragments_per_group(ext2_superblock_t *sb)
|
|---|
| 128 | {
|
|---|
| 129 | return uint32_t_le2host(sb->fragments_per_group);
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | /**
|
|---|
| 133 | * Get filesystem state
|
|---|
| 134 | *
|
|---|
| 135 | * @param sb pointer to superblock
|
|---|
| 136 | */
|
|---|
| 137 | inline uint16_t ext2_superblock_get_state(ext2_superblock_t *sb)
|
|---|
| 138 | {
|
|---|
| 139 | return uint16_t_le2host(sb->state);
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | /**
|
|---|
| 143 | * Get minor revision number
|
|---|
| 144 | *
|
|---|
| 145 | * @param sb pointer to superblock
|
|---|
| 146 | */
|
|---|
| 147 | inline uint16_t ext2_superblock_get_rev_minor(ext2_superblock_t *sb)
|
|---|
| 148 | {
|
|---|
| 149 | return uint16_t_le2host(sb->rev_minor);
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | /**
|
|---|
| 153 | * Get major revision number
|
|---|
| 154 | *
|
|---|
| 155 | * @param sb pointer to superblock
|
|---|
| 156 | */
|
|---|
| 157 | inline uint32_t ext2_superblock_get_rev_major(ext2_superblock_t *sb)
|
|---|
| 158 | {
|
|---|
| 159 | return uint32_t_le2host(sb->rev_major);
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | /**
|
|---|
| 163 | * Get index of first regular inode
|
|---|
| 164 | *
|
|---|
| 165 | * @param sb pointer to superblock
|
|---|
| 166 | */
|
|---|
| 167 | inline uint32_t ext2_superblock_get_first_inode(ext2_superblock_t *sb)
|
|---|
| 168 | {
|
|---|
| 169 | if (ext2_superblock_get_rev_major(sb) == 0) {
|
|---|
| 170 | return EXT2_REV0_FIRST_INODE;
|
|---|
| 171 | }
|
|---|
| 172 | return uint32_t_le2host(sb->first_inode);
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | /**
|
|---|
| 176 | * Get size of inode
|
|---|
| 177 | *
|
|---|
| 178 | * @param sb pointer to superblock
|
|---|
| 179 | */
|
|---|
| 180 | inline uint16_t ext2_superblock_get_inode_size(ext2_superblock_t *sb)
|
|---|
| 181 | {
|
|---|
| 182 | if (ext2_superblock_get_rev_major(sb) == 0) {
|
|---|
| 183 | return EXT2_REV0_INODE_SIZE;
|
|---|
| 184 | }
|
|---|
| 185 | return uint32_t_le2host(sb->inode_size);
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 | /** Read a superblock directly from device (i.e. no libblock cache)
|
|---|
| 190 | *
|
|---|
| 191 | * @param devmap_handle Device handle of the block device.
|
|---|
| 192 | * @param superblock Pointer where to store pointer to new superblock
|
|---|
| 193 | *
|
|---|
| 194 | * @return EOK on success or negative error code on failure.
|
|---|
| 195 | */
|
|---|
| 196 | int ext2_superblock_read_direct(devmap_handle_t devmap_handle,
|
|---|
| 197 | ext2_superblock_t **superblock)
|
|---|
| 198 | {
|
|---|
| 199 | void *data;
|
|---|
| 200 | int rc;
|
|---|
| 201 |
|
|---|
| 202 | data = malloc(EXT2_SUPERBLOCK_SIZE);
|
|---|
| 203 | if (data == NULL) {
|
|---|
| 204 | return ENOMEM;
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | rc = block_read_bytes_direct(devmap_handle, EXT2_SUPERBLOCK_OFFSET,
|
|---|
| 208 | EXT2_SUPERBLOCK_SIZE, data);
|
|---|
| 209 | if (rc != EOK) {
|
|---|
| 210 | free(data);
|
|---|
| 211 | return rc;
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | (*superblock) = data;
|
|---|
| 215 | return EOK;
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 | /** @}
|
|---|
| 220 | */
|
|---|