openfs.c revision 2e8ca9a26b0bd7dae546a3f9a98df67b043fe3be
1/* 2 * openfs.c --- open an ext2 filesystem 3 * 4 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o. 5 * 6 * %Begin-Header% 7 * This file may be redistributed under the terms of the GNU Public 8 * License. 9 * %End-Header% 10 */ 11 12#include <stdio.h> 13#include <string.h> 14#if HAVE_UNISTD_H 15#include <unistd.h> 16#endif 17#include <fcntl.h> 18#include <time.h> 19#if HAVE_SYS_STAT_H 20#include <sys/stat.h> 21#endif 22#if HAVE_SYS_TYPES_H 23#include <sys/types.h> 24#endif 25 26#include "ext2_fs.h" 27 28 29#include "ext2fs.h" 30#include "e2image.h" 31 32blk_t ext2fs_descriptor_block_loc(ext2_filsys fs, blk_t group_block, dgrp_t i) 33{ 34 int bg; 35 int has_super = 0; 36 int ret_blk; 37 38 if (!(fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) || 39 (i < fs->super->s_first_meta_bg)) 40 return (group_block + i + 1); 41 42 bg = (fs->blocksize / sizeof (struct ext2_group_desc)) * i; 43 if (ext2fs_bg_has_super(fs, bg)) 44 has_super = 1; 45 ret_blk = (fs->super->s_first_data_block + has_super + 46 (bg * fs->super->s_blocks_per_group)); 47 /* 48 * If group_block is not the normal value, we're trying to use 49 * the backup group descriptors and superblock --- so use the 50 * alternate location of the second block group in the 51 * metablock group. Ideally we should be testing each bg 52 * descriptor block individually for correctness, but we don't 53 * have the infrastructure in place to do that. 54 */ 55 if (group_block != fs->super->s_first_data_block && 56 ((ret_blk + fs->super->s_blocks_per_group) < 57 fs->super->s_blocks_count)) 58 ret_blk += fs->super->s_blocks_per_group; 59 return ret_blk; 60} 61 62errcode_t ext2fs_open(const char *name, int flags, int superblock, 63 unsigned int block_size, io_manager manager, 64 ext2_filsys *ret_fs) 65{ 66 return ext2fs_open2(name, 0, flags, superblock, block_size, 67 manager, ret_fs); 68} 69 70/* 71 * Note: if superblock is non-zero, block-size must also be non-zero. 72 * Superblock and block_size can be zero to use the default size. 73 * 74 * Valid flags for ext2fs_open() 75 * 76 * EXT2_FLAG_RW - Open the filesystem for read/write. 77 * EXT2_FLAG_FORCE - Open the filesystem even if some of the 78 * features aren't supported. 79 * EXT2_FLAG_JOURNAL_DEV_OK - Open an ext3 journal device 80 */ 81errcode_t ext2fs_open2(const char *name, const char *io_options, 82 int flags, int superblock, 83 unsigned int block_size, io_manager manager, 84 ext2_filsys *ret_fs) 85{ 86 ext2_filsys fs; 87 errcode_t retval; 88 unsigned long i; 89 int j, groups_per_block, blocks_per_group; 90 blk_t group_block, blk; 91 char *dest, *cp; 92 struct ext2_group_desc *gdp; 93 94 EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER); 95 96 retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs); 97 if (retval) 98 return retval; 99 100 memset(fs, 0, sizeof(struct struct_ext2_filsys)); 101 fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS; 102 fs->flags = flags; 103 fs->umask = 022; 104 retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name); 105 if (retval) 106 goto cleanup; 107 strcpy(fs->device_name, name); 108 cp = strchr(fs->device_name, '?'); 109 if (!io_options && cp) { 110 *cp++ = 0; 111 io_options = cp; 112 } 113 114 retval = manager->open(fs->device_name, 115 (flags & EXT2_FLAG_RW) ? IO_FLAG_RW : 0, 116 &fs->io); 117 if (retval) 118 goto cleanup; 119 if (io_options && 120 (retval = io_channel_set_options(fs->io, io_options))) 121 goto cleanup; 122 fs->image_io = fs->io; 123 fs->io->app_data = fs; 124 retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->super); 125 if (retval) 126 goto cleanup; 127 if (flags & EXT2_FLAG_IMAGE_FILE) { 128 retval = ext2fs_get_mem(sizeof(struct ext2_image_hdr), 129 &fs->image_header); 130 if (retval) 131 goto cleanup; 132 retval = io_channel_read_blk(fs->io, 0, 133 -sizeof(struct ext2_image_hdr), 134 fs->image_header); 135 if (retval) 136 goto cleanup; 137 if (fs->image_header->magic_number != EXT2_ET_MAGIC_E2IMAGE) 138 return EXT2_ET_MAGIC_E2IMAGE; 139 superblock = 1; 140 block_size = fs->image_header->fs_blocksize; 141 } 142 143 /* 144 * If the user specifies a specific block # for the 145 * superblock, then he/she must also specify the block size! 146 * Otherwise, read the master superblock located at offset 147 * SUPERBLOCK_OFFSET from the start of the partition. 148 * 149 * Note: we only save a backup copy of the superblock if we 150 * are reading the superblock from the primary superblock location. 151 */ 152 if (superblock) { 153 if (!block_size) { 154 retval = EXT2_ET_INVALID_ARGUMENT; 155 goto cleanup; 156 } 157 io_channel_set_blksize(fs->io, block_size); 158 group_block = superblock; 159 fs->orig_super = 0; 160 } else { 161 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET); 162 superblock = 1; 163 group_block = 0; 164 retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->orig_super); 165 if (retval) 166 goto cleanup; 167 } 168 retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE, 169 fs->super); 170 if (retval) 171 goto cleanup; 172 if (fs->orig_super) 173 memcpy(fs->orig_super, fs->super, SUPERBLOCK_SIZE); 174 175#ifdef EXT2FS_ENABLE_SWAPFS 176 if ((fs->super->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) || 177 (fs->flags & EXT2_FLAG_SWAP_BYTES)) { 178 fs->flags |= EXT2_FLAG_SWAP_BYTES; 179 180 ext2fs_swap_super(fs->super); 181 } 182#endif 183 184 if (fs->super->s_magic != EXT2_SUPER_MAGIC) { 185 retval = EXT2_ET_BAD_MAGIC; 186 goto cleanup; 187 } 188 if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) { 189 retval = EXT2_ET_REV_TOO_HIGH; 190 goto cleanup; 191 } 192 193 /* 194 * Check for feature set incompatibility 195 */ 196 if (!(flags & EXT2_FLAG_FORCE)) { 197 if (fs->super->s_feature_incompat & 198 ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) { 199 retval = EXT2_ET_UNSUPP_FEATURE; 200 goto cleanup; 201 } 202 if ((flags & EXT2_FLAG_RW) && 203 (fs->super->s_feature_ro_compat & 204 ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) { 205 retval = EXT2_ET_RO_UNSUPP_FEATURE; 206 goto cleanup; 207 } 208 if (!(flags & EXT2_FLAG_JOURNAL_DEV_OK) && 209 (fs->super->s_feature_incompat & 210 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) { 211 retval = EXT2_ET_UNSUPP_FEATURE; 212 goto cleanup; 213 } 214 } 215 216 fs->blocksize = EXT2_BLOCK_SIZE(fs->super); 217 if (fs->blocksize == 0) { 218 retval = EXT2_ET_CORRUPT_SUPERBLOCK; 219 goto cleanup; 220 } 221 fs->fragsize = EXT2_FRAG_SIZE(fs->super); 222 fs->inode_blocks_per_group = ((fs->super->s_inodes_per_group * 223 EXT2_INODE_SIZE(fs->super) + 224 EXT2_BLOCK_SIZE(fs->super) - 1) / 225 EXT2_BLOCK_SIZE(fs->super)); 226 if (block_size) { 227 if (block_size != fs->blocksize) { 228 retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE; 229 goto cleanup; 230 } 231 } 232 /* 233 * Set the blocksize to the filesystem's blocksize. 234 */ 235 io_channel_set_blksize(fs->io, fs->blocksize); 236 237 /* 238 * If this is an external journal device, don't try to read 239 * the group descriptors, because they're not there. 240 */ 241 if (fs->super->s_feature_incompat & 242 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) { 243 fs->group_desc_count = 0; 244 *ret_fs = fs; 245 return 0; 246 } 247 248 /* 249 * Read group descriptors 250 */ 251 blocks_per_group = EXT2_BLOCKS_PER_GROUP(fs->super); 252 if (blocks_per_group == 0 || 253 blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(fs->super) || 254 fs->inode_blocks_per_group > EXT2_MAX_INODES_PER_GROUP(fs->super)) { 255 retval = EXT2_ET_CORRUPT_SUPERBLOCK; 256 goto cleanup; 257 } 258 fs->group_desc_count = (fs->super->s_blocks_count - 259 fs->super->s_first_data_block + 260 blocks_per_group - 1) / blocks_per_group; 261 fs->desc_blocks = (fs->group_desc_count + 262 EXT2_DESC_PER_BLOCK(fs->super) - 1) 263 / EXT2_DESC_PER_BLOCK(fs->super); 264 retval = ext2fs_get_mem(fs->desc_blocks * fs->blocksize, 265 &fs->group_desc); 266 if (retval) 267 goto cleanup; 268 if (!group_block) 269 group_block = fs->super->s_first_data_block; 270 dest = (char *) fs->group_desc; 271 groups_per_block = fs->blocksize / sizeof(struct ext2_group_desc); 272 for (i=0 ; i < fs->desc_blocks; i++) { 273 blk = ext2fs_descriptor_block_loc(fs, group_block, i); 274 retval = io_channel_read_blk(fs->io, blk, 1, dest); 275 if (retval) 276 goto cleanup; 277#ifdef EXT2FS_ENABLE_SWAPFS 278 if (fs->flags & EXT2_FLAG_SWAP_BYTES) { 279 gdp = (struct ext2_group_desc *) dest; 280 for (j=0; j < groups_per_block; j++) 281 ext2fs_swap_group_desc(gdp++); 282 } 283#endif 284 dest += fs->blocksize; 285 } 286 287 *ret_fs = fs; 288 return 0; 289cleanup: 290 ext2fs_free(fs); 291 return retval; 292} 293 294/* 295 * Set/get the filesystem data I/O channel. 296 * 297 * These functions are only valid if EXT2_FLAG_IMAGE_FILE is true. 298 */ 299errcode_t ext2fs_get_data_io(ext2_filsys fs, io_channel *old_io) 300{ 301 if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0) 302 return EXT2_ET_NOT_IMAGE_FILE; 303 if (old_io) { 304 *old_io = (fs->image_io == fs->io) ? 0 : fs->io; 305 } 306 return 0; 307} 308 309errcode_t ext2fs_set_data_io(ext2_filsys fs, io_channel new_io) 310{ 311 if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0) 312 return EXT2_ET_NOT_IMAGE_FILE; 313 fs->io = new_io ? new_io : fs->image_io; 314 return 0; 315} 316 317errcode_t ext2fs_rewrite_to_io(ext2_filsys fs, io_channel new_io) 318{ 319 if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0) 320 return EXT2_ET_NOT_IMAGE_FILE; 321 fs->io = fs->image_io = new_io; 322 fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_RW | 323 EXT2_FLAG_BB_DIRTY | EXT2_FLAG_IB_DIRTY; 324 fs->flags &= ~EXT2_FLAG_IMAGE_FILE; 325 return 0; 326} 327