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