openfs.c revision d1154eb460efe588eaed3d439c1caaca149fa362
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 Library 8 * General Public License, version 2. 9 * %End-Header% 10 */ 11 12#include "config.h" 13#include <stdio.h> 14#include <string.h> 15#if HAVE_UNISTD_H 16#include <unistd.h> 17#endif 18#include <fcntl.h> 19#include <time.h> 20#if HAVE_SYS_STAT_H 21#include <sys/stat.h> 22#endif 23#if HAVE_SYS_TYPES_H 24#include <sys/types.h> 25#endif 26 27#include "ext2_fs.h" 28 29 30#include "ext2fs.h" 31#include "e2image.h" 32 33blk64_t ext2fs_descriptor_block_loc2(ext2_filsys fs, blk64_t group_block, 34 dgrp_t i) 35{ 36 int bg; 37 int has_super = 0; 38 blk64_t ret_blk; 39 40 if (!(fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) || 41 (i < fs->super->s_first_meta_bg)) 42 return (group_block + i + 1); 43 44 bg = EXT2_DESC_PER_BLOCK(fs->super) * i; 45 if (ext2fs_bg_has_super(fs, bg)) 46 has_super = 1; 47 ret_blk = ext2fs_group_first_block2(fs, bg) + has_super; 48 /* 49 * If group_block is not the normal value, we're trying to use 50 * the backup group descriptors and superblock --- so use the 51 * alternate location of the second block group in the 52 * metablock group. Ideally we should be testing each bg 53 * descriptor block individually for correctness, but we don't 54 * have the infrastructure in place to do that. 55 */ 56 if (group_block != fs->super->s_first_data_block && 57 ((ret_blk + fs->super->s_blocks_per_group) < 58 ext2fs_blocks_count(fs->super))) 59 ret_blk += fs->super->s_blocks_per_group; 60 return ret_blk; 61} 62 63blk_t ext2fs_descriptor_block_loc(ext2_filsys fs, blk_t group_block, dgrp_t i) 64{ 65 return ext2fs_descriptor_block_loc2(fs, group_block, i); 66} 67 68errcode_t ext2fs_open(const char *name, int flags, int superblock, 69 unsigned int block_size, io_manager manager, 70 ext2_filsys *ret_fs) 71{ 72 return ext2fs_open2(name, 0, flags, superblock, block_size, 73 manager, ret_fs); 74} 75 76/* 77 * Note: if superblock is non-zero, block-size must also be non-zero. 78 * Superblock and block_size can be zero to use the default size. 79 * 80 * Valid flags for ext2fs_open() 81 * 82 * EXT2_FLAG_RW - Open the filesystem for read/write. 83 * EXT2_FLAG_FORCE - Open the filesystem even if some of the 84 * features aren't supported. 85 * EXT2_FLAG_JOURNAL_DEV_OK - Open an ext3 journal device 86 */ 87errcode_t ext2fs_open2(const char *name, const char *io_options, 88 int flags, int superblock, 89 unsigned int block_size, io_manager manager, 90 ext2_filsys *ret_fs) 91{ 92 ext2_filsys fs; 93 errcode_t retval; 94 unsigned long i, first_meta_bg; 95 __u32 features; 96 unsigned int groups_per_block, blocks_per_group, io_flags; 97 blk64_t group_block, blk; 98 char *dest, *cp; 99#ifdef WORDS_BIGENDIAN 100 struct ext2_group_desc *gdp; 101 int j; 102#endif 103 104 EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER); 105 106 retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs); 107 if (retval) 108 return retval; 109 110 memset(fs, 0, sizeof(struct struct_ext2_filsys)); 111 fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS; 112 fs->flags = flags; 113 /* don't overwrite sb backups unless flag is explicitly cleared */ 114 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY; 115 fs->umask = 022; 116 retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name); 117 if (retval) 118 goto cleanup; 119 strcpy(fs->device_name, name); 120 cp = strchr(fs->device_name, '?'); 121 if (!io_options && cp) { 122 *cp++ = 0; 123 io_options = cp; 124 } 125 126 io_flags = 0; 127 if (flags & EXT2_FLAG_RW) 128 io_flags |= IO_FLAG_RW; 129 if (flags & EXT2_FLAG_EXCLUSIVE) 130 io_flags |= IO_FLAG_EXCLUSIVE; 131 if (flags & EXT2_FLAG_DIRECT_IO) 132 io_flags |= IO_FLAG_DIRECT_IO; 133 retval = manager->open(fs->device_name, io_flags, &fs->io); 134 if (retval) 135 goto cleanup; 136 if (io_options && 137 (retval = io_channel_set_options(fs->io, io_options))) 138 goto cleanup; 139 fs->image_io = fs->io; 140 fs->io->app_data = fs; 141 retval = ext2fs_get_memalign(SUPERBLOCK_SIZE, 512, &fs->super); 142 if (retval) 143 goto cleanup; 144 if (flags & EXT2_FLAG_IMAGE_FILE) { 145 retval = ext2fs_get_mem(sizeof(struct ext2_image_hdr), 146 &fs->image_header); 147 if (retval) 148 goto cleanup; 149 retval = io_channel_read_blk(fs->io, 0, 150 -(int)sizeof(struct ext2_image_hdr), 151 fs->image_header); 152 if (retval) 153 goto cleanup; 154 if (fs->image_header->magic_number != EXT2_ET_MAGIC_E2IMAGE) 155 return EXT2_ET_MAGIC_E2IMAGE; 156 superblock = 1; 157 block_size = fs->image_header->fs_blocksize; 158 } 159 160 /* 161 * If the user specifies a specific block # for the 162 * superblock, then he/she must also specify the block size! 163 * Otherwise, read the master superblock located at offset 164 * SUPERBLOCK_OFFSET from the start of the partition. 165 * 166 * Note: we only save a backup copy of the superblock if we 167 * are reading the superblock from the primary superblock location. 168 */ 169 if (superblock) { 170 if (!block_size) { 171 retval = EXT2_ET_INVALID_ARGUMENT; 172 goto cleanup; 173 } 174 io_channel_set_blksize(fs->io, block_size); 175 group_block = superblock; 176 fs->orig_super = 0; 177 } else { 178 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET); 179 superblock = 1; 180 group_block = 0; 181 retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->orig_super); 182 if (retval) 183 goto cleanup; 184 } 185 retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE, 186 fs->super); 187 if (retval) 188 goto cleanup; 189 if (fs->orig_super) 190 memcpy(fs->orig_super, fs->super, SUPERBLOCK_SIZE); 191 192#ifdef WORDS_BIGENDIAN 193 fs->flags |= EXT2_FLAG_SWAP_BYTES; 194 ext2fs_swap_super(fs->super); 195#else 196 if (fs->flags & EXT2_FLAG_SWAP_BYTES) { 197 retval = EXT2_ET_UNIMPLEMENTED; 198 goto cleanup; 199 } 200#endif 201 202 if (fs->super->s_magic != EXT2_SUPER_MAGIC) { 203 retval = EXT2_ET_BAD_MAGIC; 204 goto cleanup; 205 } 206 if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) { 207 retval = EXT2_ET_REV_TOO_HIGH; 208 goto cleanup; 209 } 210 211 /* 212 * Check for feature set incompatibility 213 */ 214 if (!(flags & EXT2_FLAG_FORCE)) { 215 features = fs->super->s_feature_incompat; 216#ifdef EXT2_LIB_SOFTSUPP_INCOMPAT 217 if (flags & EXT2_FLAG_SOFTSUPP_FEATURES) 218 features &= ~EXT2_LIB_SOFTSUPP_INCOMPAT; 219#endif 220 if (features & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) { 221 retval = EXT2_ET_UNSUPP_FEATURE; 222 goto cleanup; 223 } 224 225 features = fs->super->s_feature_ro_compat; 226#ifdef EXT2_LIB_SOFTSUPP_RO_COMPAT 227 if (flags & EXT2_FLAG_SOFTSUPP_FEATURES) 228 features &= ~EXT2_LIB_SOFTSUPP_RO_COMPAT; 229#endif 230 if ((flags & EXT2_FLAG_RW) && 231 (features & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) { 232 retval = EXT2_ET_RO_UNSUPP_FEATURE; 233 goto cleanup; 234 } 235 236 if (!(flags & EXT2_FLAG_JOURNAL_DEV_OK) && 237 (fs->super->s_feature_incompat & 238 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) { 239 retval = EXT2_ET_UNSUPP_FEATURE; 240 goto cleanup; 241 } 242 } 243 244 if ((fs->super->s_log_block_size + EXT2_MIN_BLOCK_LOG_SIZE) > 245 EXT2_MAX_BLOCK_LOG_SIZE) { 246 retval = EXT2_ET_CORRUPT_SUPERBLOCK; 247 goto cleanup; 248 } 249 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super, 250 EXT4_FEATURE_RO_COMPAT_BIGALLOC) && 251 (fs->super->s_log_block_size != fs->super->s_log_cluster_size)) { 252 retval = EXT2_ET_CORRUPT_SUPERBLOCK; 253 goto cleanup; 254 } 255 fs->fragsize = fs->blocksize = EXT2_BLOCK_SIZE(fs->super); 256 if (EXT2_INODE_SIZE(fs->super) < EXT2_GOOD_OLD_INODE_SIZE) { 257 retval = EXT2_ET_CORRUPT_SUPERBLOCK; 258 goto cleanup; 259 } 260 fs->cluster_ratio_bits = fs->super->s_log_cluster_size - 261 fs->super->s_log_block_size; 262 if (EXT2_BLOCKS_PER_GROUP(fs->super) != 263 EXT2_CLUSTERS_PER_GROUP(fs->super) << fs->cluster_ratio_bits) { 264 retval = EXT2_ET_CORRUPT_SUPERBLOCK; 265 goto cleanup; 266 } 267 fs->inode_blocks_per_group = ((EXT2_INODES_PER_GROUP(fs->super) * 268 EXT2_INODE_SIZE(fs->super) + 269 EXT2_BLOCK_SIZE(fs->super) - 1) / 270 EXT2_BLOCK_SIZE(fs->super)); 271 if (block_size) { 272 if (block_size != fs->blocksize) { 273 retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE; 274 goto cleanup; 275 } 276 } 277 /* 278 * Set the blocksize to the filesystem's blocksize. 279 */ 280 io_channel_set_blksize(fs->io, fs->blocksize); 281 282 /* 283 * If this is an external journal device, don't try to read 284 * the group descriptors, because they're not there. 285 */ 286 if (fs->super->s_feature_incompat & 287 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) { 288 fs->group_desc_count = 0; 289 *ret_fs = fs; 290 return 0; 291 } 292 293 if (EXT2_INODES_PER_GROUP(fs->super) == 0) { 294 retval = EXT2_ET_CORRUPT_SUPERBLOCK; 295 goto cleanup; 296 } 297 298 /* 299 * Read group descriptors 300 */ 301 blocks_per_group = EXT2_BLOCKS_PER_GROUP(fs->super); 302 if (blocks_per_group == 0 || 303 blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(fs->super) || 304 fs->inode_blocks_per_group > EXT2_MAX_INODES_PER_GROUP(fs->super) || 305 EXT2_DESC_PER_BLOCK(fs->super) == 0 || 306 fs->super->s_first_data_block >= ext2fs_blocks_count(fs->super)) { 307 retval = EXT2_ET_CORRUPT_SUPERBLOCK; 308 goto cleanup; 309 } 310 fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) - 311 fs->super->s_first_data_block, 312 blocks_per_group); 313 if (fs->group_desc_count * EXT2_INODES_PER_GROUP(fs->super) != 314 fs->super->s_inodes_count) { 315 retval = EXT2_ET_CORRUPT_SUPERBLOCK; 316 goto cleanup; 317 } 318 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count, 319 EXT2_DESC_PER_BLOCK(fs->super)); 320 retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize, 321 &fs->group_desc); 322 if (retval) 323 goto cleanup; 324 if (!group_block) 325 group_block = fs->super->s_first_data_block; 326 if (group_block == 0 && fs->blocksize == 1024) 327 group_block = 1; /* Deal with 1024 blocksize && bigalloc */ 328 dest = (char *) fs->group_desc; 329 groups_per_block = EXT2_DESC_PER_BLOCK(fs->super); 330 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) 331 first_meta_bg = fs->super->s_first_meta_bg; 332 else 333 first_meta_bg = fs->desc_blocks; 334 if (first_meta_bg) { 335 retval = io_channel_read_blk(fs->io, group_block+1, 336 first_meta_bg, dest); 337 if (retval) 338 goto cleanup; 339#ifdef WORDS_BIGENDIAN 340 gdp = (struct ext2_group_desc *) dest; 341 for (j=0; j < groups_per_block*first_meta_bg; j++) { 342 gdp = ext2fs_group_desc(fs, fs->group_desc, j); 343 ext2fs_swap_group_desc2(fs, gdp); 344 } 345#endif 346 dest += fs->blocksize*first_meta_bg; 347 } 348 for (i=first_meta_bg ; i < fs->desc_blocks; i++) { 349 blk = ext2fs_descriptor_block_loc2(fs, group_block, i); 350 retval = io_channel_read_blk64(fs->io, blk, 1, dest); 351 if (retval) 352 goto cleanup; 353#ifdef WORDS_BIGENDIAN 354 for (j=0; j < groups_per_block; j++) { 355 /* The below happens to work... be careful. */ 356 gdp = ext2fs_group_desc(fs, fs->group_desc, j); 357 ext2fs_swap_group_desc2(fs, gdp); 358 } 359#endif 360 dest += fs->blocksize; 361 } 362 363 fs->stride = fs->super->s_raid_stride; 364 365 /* 366 * If recovery is from backup superblock, Clear _UNININT flags & 367 * reset bg_itable_unused to zero 368 */ 369 if (superblock > 1 && EXT2_HAS_RO_COMPAT_FEATURE(fs->super, 370 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) { 371 dgrp_t group; 372 373 for (group = 0; group < fs->group_desc_count; group++) { 374 ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT); 375 ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT); 376 ext2fs_bg_itable_unused_set(fs, group, 0); 377 } 378 ext2fs_mark_super_dirty(fs); 379 } 380 381 fs->flags &= ~EXT2_FLAG_NOFREE_ON_ERROR; 382 *ret_fs = fs; 383 return 0; 384cleanup: 385 if (flags & EXT2_FLAG_NOFREE_ON_ERROR) 386 *ret_fs = fs; 387 else 388 ext2fs_free(fs); 389 return retval; 390} 391 392/* 393 * Set/get the filesystem data I/O channel. 394 * 395 * These functions are only valid if EXT2_FLAG_IMAGE_FILE is true. 396 */ 397errcode_t ext2fs_get_data_io(ext2_filsys fs, io_channel *old_io) 398{ 399 if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0) 400 return EXT2_ET_NOT_IMAGE_FILE; 401 if (old_io) { 402 *old_io = (fs->image_io == fs->io) ? 0 : fs->io; 403 } 404 return 0; 405} 406 407errcode_t ext2fs_set_data_io(ext2_filsys fs, io_channel new_io) 408{ 409 if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0) 410 return EXT2_ET_NOT_IMAGE_FILE; 411 fs->io = new_io ? new_io : fs->image_io; 412 return 0; 413} 414 415errcode_t ext2fs_rewrite_to_io(ext2_filsys fs, io_channel new_io) 416{ 417 if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0) 418 return EXT2_ET_NOT_IMAGE_FILE; 419 fs->io = fs->image_io = new_io; 420 fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_RW | 421 EXT2_FLAG_BB_DIRTY | EXT2_FLAG_IB_DIRTY; 422 fs->flags &= ~EXT2_FLAG_IMAGE_FILE; 423 return 0; 424} 425