openfs.c revision 6a525069a99787ef3ae1156f12230044b3568f4b
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#include "ext2fs.h" 28#include "e2image.h" 29 30/* 31 * Note: if superblock is non-zero, block-size must also be non-zero. 32 * Superblock and block_size can be zero to use the default size. 33 * 34 * Valid flags for ext2fs_open() 35 * 36 * EXT2_FLAG_RW - Open the filesystem for read/write. 37 * EXT2_FLAG_FORCE - Open the filesystem even if some of the 38 * features aren't supported. 39 * EXT2_FLAG_JOURNAL_DEV_OK - Open an ext3 journal device 40 */ 41errcode_t ext2fs_open(const char *name, int flags, int superblock, 42 int block_size, io_manager manager, ext2_filsys *ret_fs) 43{ 44 ext2_filsys fs; 45 errcode_t retval; 46 int i, j, groups_per_block; 47 blk_t group_block; 48 char *dest; 49 struct ext2_group_desc *gdp; 50 51 EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER); 52 53 retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), 54 (void **) &fs); 55 if (retval) 56 return retval; 57 58 memset(fs, 0, sizeof(struct struct_ext2_filsys)); 59 fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS; 60 fs->flags = flags; 61 fs->umask = 022; 62 retval = manager->open(name, (flags & EXT2_FLAG_RW) ? IO_FLAG_RW : 0, 63 &fs->io); 64 if (retval) 65 goto cleanup; 66 fs->io->app_data = fs; 67 retval = ext2fs_get_mem(strlen(name)+1, (void **) &fs->device_name); 68 if (retval) 69 goto cleanup; 70 strcpy(fs->device_name, name); 71 retval = ext2fs_get_mem(SUPERBLOCK_SIZE, (void **) &fs->super); 72 if (retval) 73 goto cleanup; 74 if (flags & EXT2_FLAG_IMAGE_FILE) { 75 retval = ext2fs_get_mem(sizeof(struct ext2_image_hdr), 76 (void **) &fs->image_header); 77 if (retval) 78 goto cleanup; 79 retval = io_channel_read_blk(fs->io, 0, 80 -sizeof(struct ext2_image_hdr), 81 fs->image_header); 82 if (retval) 83 goto cleanup; 84 if (fs->image_header->magic_number != EXT2_ET_MAGIC_E2IMAGE) 85 return EXT2_ET_MAGIC_E2IMAGE; 86 superblock = 1; 87 block_size = fs->image_header->fs_blocksize; 88 } 89 90 /* 91 * If the user specifies a specific block # for the 92 * superblock, then he/she must also specify the block size! 93 * Otherwise, read the master superblock located at offset 94 * SUPERBLOCK_OFFSET from the start of the partition. 95 * 96 * Note: we only save a backup copy of the superblock if we 97 * are reading the superblock from the primary superblock location. 98 */ 99 if (superblock) { 100 if (!block_size) { 101 retval = EXT2_ET_INVALID_ARGUMENT; 102 goto cleanup; 103 } 104 io_channel_set_blksize(fs->io, block_size); 105 group_block = superblock + 1; 106 fs->orig_super = 0; 107 } else { 108 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET); 109 superblock = 1; 110 group_block = 0; 111 retval = ext2fs_get_mem(SUPERBLOCK_SIZE, 112 (void **) &fs->orig_super); 113 if (retval) 114 goto cleanup; 115 } 116 retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE, 117 fs->super); 118 if (retval) 119 goto cleanup; 120 if (fs->orig_super) 121 memcpy(fs->orig_super, fs->super, SUPERBLOCK_SIZE); 122 123#ifdef EXT2FS_ENABLE_SWAPFS 124 if ((fs->super->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) || 125 (fs->flags & EXT2_FLAG_SWAP_BYTES)) { 126 fs->flags |= EXT2_FLAG_SWAP_BYTES; 127 128 ext2fs_swap_super(fs->super); 129 } 130#endif 131 132 if (fs->super->s_magic != EXT2_SUPER_MAGIC) { 133 retval = EXT2_ET_BAD_MAGIC; 134 goto cleanup; 135 } 136 if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) { 137 retval = EXT2_ET_REV_TOO_HIGH; 138 goto cleanup; 139 } 140 141 /* 142 * Check for feature set incompatibility 143 */ 144 if (!(flags & EXT2_FLAG_FORCE)) { 145 if (fs->super->s_feature_incompat & 146 ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) { 147 retval = EXT2_ET_UNSUPP_FEATURE; 148 goto cleanup; 149 } 150 if ((flags & EXT2_FLAG_RW) && 151 (fs->super->s_feature_ro_compat & 152 ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) { 153 retval = EXT2_ET_RO_UNSUPP_FEATURE; 154 goto cleanup; 155 } 156 if (!(flags & EXT2_FLAG_JOURNAL_DEV_OK) && 157 (fs->super->s_feature_incompat & 158 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) { 159 retval = EXT2_ET_UNSUPP_FEATURE; 160 goto cleanup; 161 } 162 } 163 164 fs->blocksize = EXT2_BLOCK_SIZE(fs->super); 165 if (fs->blocksize == 0) { 166 retval = EXT2_ET_CORRUPT_SUPERBLOCK; 167 goto cleanup; 168 } 169 fs->fragsize = EXT2_FRAG_SIZE(fs->super); 170 fs->inode_blocks_per_group = ((fs->super->s_inodes_per_group * 171 EXT2_INODE_SIZE(fs->super) + 172 EXT2_BLOCK_SIZE(fs->super) - 1) / 173 EXT2_BLOCK_SIZE(fs->super)); 174 if (block_size) { 175 if (block_size != fs->blocksize) { 176 retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE; 177 goto cleanup; 178 } 179 } 180 /* 181 * Set the blocksize to the filesystem's blocksize. 182 */ 183 io_channel_set_blksize(fs->io, fs->blocksize); 184 185 /* 186 * If this is an external journal device, don't try to read 187 * the group descriptors, because they're not there. 188 */ 189 if (fs->super->s_feature_incompat & 190 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) { 191 fs->group_desc_count = 0; 192 *ret_fs = fs; 193 return 0; 194 } 195 196 /* 197 * Read group descriptors 198 */ 199 if ((EXT2_BLOCKS_PER_GROUP(fs->super)) == 0) { 200 retval = EXT2_ET_CORRUPT_SUPERBLOCK; 201 goto cleanup; 202 } 203 fs->group_desc_count = (fs->super->s_blocks_count - 204 fs->super->s_first_data_block + 205 EXT2_BLOCKS_PER_GROUP(fs->super) - 1) 206 / EXT2_BLOCKS_PER_GROUP(fs->super); 207 fs->desc_blocks = (fs->group_desc_count + 208 EXT2_DESC_PER_BLOCK(fs->super) - 1) 209 / EXT2_DESC_PER_BLOCK(fs->super); 210 retval = ext2fs_get_mem(fs->desc_blocks * fs->blocksize, 211 (void **) &fs->group_desc); 212 if (retval) 213 goto cleanup; 214 if (!group_block) 215 group_block = fs->super->s_first_data_block + 1; 216 dest = (char *) fs->group_desc; 217 for (i=0 ; i < fs->desc_blocks; i++) { 218 retval = io_channel_read_blk(fs->io, group_block, 1, dest); 219 if (retval) 220 goto cleanup; 221 group_block++; 222#ifdef EXT2FS_ENABLE_SWAPFS 223 if (fs->flags & EXT2_FLAG_SWAP_BYTES) { 224 gdp = (struct ext2_group_desc *) dest; 225 groups_per_block = fs->blocksize / 226 sizeof(struct ext2_group_desc); 227 for (j=0; j < groups_per_block; j++) 228 ext2fs_swap_group_desc(gdp++); 229 } 230#endif 231 dest += fs->blocksize; 232 } 233 234 *ret_fs = fs; 235 return 0; 236cleanup: 237 ext2fs_free(fs); 238 return retval; 239} 240 241