| 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_filesystem.h"
|
|---|
| 37 | #include "libext2_superblock.h"
|
|---|
| 38 | #include "libext2_block_group.h"
|
|---|
| 39 | #include "libext2_inode.h"
|
|---|
| 40 | #include <errno.h>
|
|---|
| 41 | #include <libblock.h>
|
|---|
| 42 | #include <malloc.h>
|
|---|
| 43 |
|
|---|
| 44 | /**
|
|---|
| 45 | * Initialize an instance of filesystem on the device.
|
|---|
| 46 | * This function reads superblock from the device and
|
|---|
| 47 | * initializes libblock cache with appropriate logical block size.
|
|---|
| 48 | *
|
|---|
| 49 | * @param fs Pointer to ext2_filesystem_t to initialize
|
|---|
| 50 | * @param devmap_handle Device handle of the block device
|
|---|
| 51 | *
|
|---|
| 52 | * @return EOK on success or negative error code on failure
|
|---|
| 53 | */
|
|---|
| 54 | int ext2_filesystem_init(ext2_filesystem_t *fs, devmap_handle_t devmap_handle)
|
|---|
| 55 | {
|
|---|
| 56 | int rc;
|
|---|
| 57 | ext2_superblock_t *temp_superblock;
|
|---|
| 58 | size_t block_size;
|
|---|
| 59 |
|
|---|
| 60 | fs->device = devmap_handle;
|
|---|
| 61 |
|
|---|
| 62 | rc = block_init(fs->device, 2048);
|
|---|
| 63 | if (rc != EOK) {
|
|---|
| 64 | return rc;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | rc = ext2_superblock_read_direct(fs->device, &temp_superblock);
|
|---|
| 68 | if (rc != EOK) {
|
|---|
| 69 | block_fini(fs->device);
|
|---|
| 70 | return rc;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | block_size = ext2_superblock_get_block_size(temp_superblock);
|
|---|
| 74 |
|
|---|
| 75 | if (block_size > EXT2_MAX_BLOCK_SIZE) {
|
|---|
| 76 | block_fini(fs->device);
|
|---|
| 77 | return ENOTSUP;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | rc = block_cache_init(devmap_handle, block_size, 0, CACHE_MODE_WT);
|
|---|
| 81 | if (rc != EOK) {
|
|---|
| 82 | block_fini(fs->device);
|
|---|
| 83 | return rc;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | fs->superblock = temp_superblock;
|
|---|
| 87 |
|
|---|
| 88 | return EOK;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | /**
|
|---|
| 92 | * Check filesystem for sanity
|
|---|
| 93 | *
|
|---|
| 94 | * @param fs Pointer to ext2_filesystem_t to check
|
|---|
| 95 | * @return EOK on success or negative error code on failure
|
|---|
| 96 | */
|
|---|
| 97 | int ext2_filesystem_check_sanity(ext2_filesystem_t *fs)
|
|---|
| 98 | {
|
|---|
| 99 | int rc;
|
|---|
| 100 |
|
|---|
| 101 | rc = ext2_superblock_check_sanity(fs->superblock);
|
|---|
| 102 | if (rc != EOK) {
|
|---|
| 103 | return rc;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | return EOK;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /**
|
|---|
| 110 | * Get a reference to block descriptor
|
|---|
| 111 | *
|
|---|
| 112 | * @param fs Pointer to filesystem information
|
|---|
| 113 | * @param bgid Index of block group to find
|
|---|
| 114 | * @param ref Pointer where to store pointer to block group reference
|
|---|
| 115 | *
|
|---|
| 116 | * @return EOK on success or negative error code on failure
|
|---|
| 117 | */
|
|---|
| 118 | int ext2_filesystem_get_block_group_ref(ext2_filesystem_t *fs, uint32_t bgid,
|
|---|
| 119 | ext2_block_group_ref_t **ref)
|
|---|
| 120 | {
|
|---|
| 121 | int rc;
|
|---|
| 122 | aoff64_t block_id;
|
|---|
| 123 | uint32_t descriptors_per_block;
|
|---|
| 124 | size_t offset;
|
|---|
| 125 | ext2_block_group_ref_t *newref;
|
|---|
| 126 |
|
|---|
| 127 | newref = malloc(sizeof(ext2_block_group_ref_t));
|
|---|
| 128 | if (newref == NULL) {
|
|---|
| 129 | return ENOMEM;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | descriptors_per_block = ext2_superblock_get_block_size(fs->superblock)
|
|---|
| 133 | / EXT2_BLOCK_GROUP_DESCRIPTOR_SIZE;
|
|---|
| 134 |
|
|---|
| 135 | // Block group descriptor table starts at the next block after superblock
|
|---|
| 136 | block_id = ext2_superblock_get_first_block(fs->superblock) + 1;
|
|---|
| 137 |
|
|---|
| 138 | // Find the block containing the descriptor we are looking for
|
|---|
| 139 | block_id += bgid / descriptors_per_block;
|
|---|
| 140 | offset = (bgid % descriptors_per_block) * EXT2_BLOCK_GROUP_DESCRIPTOR_SIZE;
|
|---|
| 141 |
|
|---|
| 142 | rc = block_get(&newref->block, fs->device, block_id, 0);
|
|---|
| 143 | if (rc != EOK) {
|
|---|
| 144 | free(newref);
|
|---|
| 145 | return rc;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | newref->block_group = newref->block->data + offset;
|
|---|
| 149 |
|
|---|
| 150 | *ref = newref;
|
|---|
| 151 |
|
|---|
| 152 | return EOK;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | /**
|
|---|
| 156 | * Free a reference to block group
|
|---|
| 157 | *
|
|---|
| 158 | * @param ref Pointer to block group reference to free
|
|---|
| 159 | *
|
|---|
| 160 | * @return EOK on success or negative error code on failure
|
|---|
| 161 | */
|
|---|
| 162 | int ext2_filesystem_put_block_group_ref(ext2_block_group_ref_t *ref)
|
|---|
| 163 | {
|
|---|
| 164 | int rc;
|
|---|
| 165 |
|
|---|
| 166 | rc = block_put(ref->block);
|
|---|
| 167 | free(ref);
|
|---|
| 168 |
|
|---|
| 169 | return rc;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | /**
|
|---|
| 173 | * Get a reference to inode
|
|---|
| 174 | *
|
|---|
| 175 | * @param fs Pointer to filesystem information
|
|---|
| 176 | * @param index The index number of the inode
|
|---|
| 177 | * @param ref Pointer where to store pointer to inode reference
|
|---|
| 178 | *
|
|---|
| 179 | * @return EOK on success or negative error code on failure
|
|---|
| 180 | */
|
|---|
| 181 | int ext2_filesystem_get_inode_ref(ext2_filesystem_t *fs, uint32_t index,
|
|---|
| 182 | ext2_inode_ref_t **ref)
|
|---|
| 183 | {
|
|---|
| 184 | int rc;
|
|---|
| 185 | aoff64_t block_id;
|
|---|
| 186 | uint32_t block_group;
|
|---|
| 187 | uint32_t offset_in_group;
|
|---|
| 188 | uint32_t inodes_per_group;
|
|---|
| 189 | uint32_t inode_table_start;
|
|---|
| 190 | uint16_t inode_size;
|
|---|
| 191 | uint32_t block_size;
|
|---|
| 192 | ext2_block_group_ref_t *bg_ref;
|
|---|
| 193 | ext2_inode_ref_t *newref;
|
|---|
| 194 |
|
|---|
| 195 | newref = malloc(sizeof(ext2_inode_ref_t));
|
|---|
| 196 | if (newref == NULL) {
|
|---|
| 197 | return ENOMEM;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | inodes_per_group = ext2_superblock_get_inodes_per_group(fs->superblock);
|
|---|
| 201 |
|
|---|
| 202 | // inode numbers are 1-based
|
|---|
| 203 | index -= 1;
|
|---|
| 204 | block_group = index / inodes_per_group;
|
|---|
| 205 | offset_in_group = index % inodes_per_group;
|
|---|
| 206 |
|
|---|
| 207 | rc = ext2_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
|---|
| 208 | if (rc != EOK) {
|
|---|
| 209 | free(newref);
|
|---|
| 210 | return rc;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | inode_table_start = ext2_block_group_get_inode_table_first_block(
|
|---|
| 214 | bg_ref->block_group);
|
|---|
| 215 |
|
|---|
| 216 | inode_size = ext2_superblock_get_inode_size(fs->superblock);
|
|---|
| 217 | block_size = ext2_superblock_get_block_size(fs->superblock);
|
|---|
| 218 |
|
|---|
| 219 | block_id = inode_table_start + ((index * inode_size) / block_size);
|
|---|
| 220 |
|
|---|
| 221 | rc = block_get(&newref->block, fs->device, block_id, 0);
|
|---|
| 222 | if (rc != EOK) {
|
|---|
| 223 | free(newref);
|
|---|
| 224 | return rc;
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | newref->inode = newref->block->data + offset_in_group;
|
|---|
| 228 |
|
|---|
| 229 | *ref = newref;
|
|---|
| 230 |
|
|---|
| 231 | return EOK;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | /**
|
|---|
| 235 | * Free a reference to inode
|
|---|
| 236 | *
|
|---|
| 237 | * @param ref Pointer to inode reference to free
|
|---|
| 238 | *
|
|---|
| 239 | * @return EOK on success or negative error code on failure
|
|---|
| 240 | */
|
|---|
| 241 | int ext2_filesystem_put_inode_ref(ext2_inode_ref_t *ref)
|
|---|
| 242 | {
|
|---|
| 243 | int rc;
|
|---|
| 244 |
|
|---|
| 245 | rc = block_put(ref->block);
|
|---|
| 246 | free(ref);
|
|---|
| 247 |
|
|---|
| 248 | return rc;
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | /**
|
|---|
| 252 | * Finalize an instance of filesystem
|
|---|
| 253 | *
|
|---|
| 254 | * @param fs Pointer to ext2_filesystem_t to finalize
|
|---|
| 255 | */
|
|---|
| 256 | void ext2_filesystem_fini(ext2_filesystem_t *fs)
|
|---|
| 257 | {
|
|---|
| 258 | free(fs->superblock);
|
|---|
| 259 | block_fini(fs->device);
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 | /** @}
|
|---|
| 264 | */
|
|---|