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