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