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