openfs.c revision e3507739e4185bdb2394928eb6479e48f4e690a8
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 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super, 257 EXT4_FEATURE_RO_COMPAT_BIGALLOC) && 258 (fs->super->s_log_block_size != fs->super->s_log_cluster_size)) { 259 retval = EXT2_ET_CORRUPT_SUPERBLOCK; 260 goto cleanup; 261 } 262 fs->fragsize = fs->blocksize = EXT2_BLOCK_SIZE(fs->super); 263 if (EXT2_INODE_SIZE(fs->super) < EXT2_GOOD_OLD_INODE_SIZE) { 264 retval = EXT2_ET_CORRUPT_SUPERBLOCK; 265 goto cleanup; 266 } 267 fs->cluster_ratio_bits = fs->super->s_log_cluster_size - 268 fs->super->s_log_block_size; 269 if (EXT2_BLOCKS_PER_GROUP(fs->super) != 270 EXT2_CLUSTERS_PER_GROUP(fs->super) << fs->cluster_ratio_bits) { 271 retval = EXT2_ET_CORRUPT_SUPERBLOCK; 272 goto cleanup; 273 } 274 fs->inode_blocks_per_group = ((EXT2_INODES_PER_GROUP(fs->super) * 275 EXT2_INODE_SIZE(fs->super) + 276 EXT2_BLOCK_SIZE(fs->super) - 1) / 277 EXT2_BLOCK_SIZE(fs->super)); 278 if (block_size) { 279 if (block_size != fs->blocksize) { 280 retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE; 281 goto cleanup; 282 } 283 } 284 /* 285 * Set the blocksize to the filesystem's blocksize. 286 */ 287 io_channel_set_blksize(fs->io, fs->blocksize); 288 289 /* 290 * If this is an external journal device, don't try to read 291 * the group descriptors, because they're not there. 292 */ 293 if (fs->super->s_feature_incompat & 294 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) { 295 fs->group_desc_count = 0; 296 *ret_fs = fs; 297 return 0; 298 } 299 300 if (EXT2_INODES_PER_GROUP(fs->super) == 0) { 301 retval = EXT2_ET_CORRUPT_SUPERBLOCK; 302 goto cleanup; 303 } 304 305 /* 306 * Read group descriptors 307 */ 308 blocks_per_group = EXT2_BLOCKS_PER_GROUP(fs->super); 309 if (blocks_per_group == 0 || 310 blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(fs->super) || 311 fs->inode_blocks_per_group > EXT2_MAX_INODES_PER_GROUP(fs->super) || 312 EXT2_DESC_PER_BLOCK(fs->super) == 0 || 313 fs->super->s_first_data_block >= ext2fs_blocks_count(fs->super)) { 314 retval = EXT2_ET_CORRUPT_SUPERBLOCK; 315 goto cleanup; 316 } 317 fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) - 318 fs->super->s_first_data_block, 319 blocks_per_group); 320 if (fs->group_desc_count * EXT2_INODES_PER_GROUP(fs->super) != 321 fs->super->s_inodes_count) { 322 retval = EXT2_ET_CORRUPT_SUPERBLOCK; 323 goto cleanup; 324 } 325 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count, 326 EXT2_DESC_PER_BLOCK(fs->super)); 327 retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize, 328 &fs->group_desc); 329 if (retval) 330 goto cleanup; 331 if (!group_block) 332 group_block = fs->super->s_first_data_block; 333 if (group_block == 0 && fs->blocksize == 1024) 334 group_block = 1; /* Deal with 1024 blocksize && bigalloc */ 335 dest = (char *) fs->group_desc; 336#ifdef WORDS_BIGENDIAN 337 groups_per_block = EXT2_DESC_PER_BLOCK(fs->super); 338#endif 339 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) 340 first_meta_bg = fs->super->s_first_meta_bg; 341 else 342 first_meta_bg = fs->desc_blocks; 343 if (first_meta_bg) { 344 retval = io_channel_read_blk(fs->io, group_block+1, 345 first_meta_bg, dest); 346 if (retval) 347 goto cleanup; 348#ifdef WORDS_BIGENDIAN 349 gdp = (struct ext2_group_desc *) dest; 350 for (j=0; j < groups_per_block*first_meta_bg; j++) { 351 gdp = ext2fs_group_desc(fs, fs->group_desc, j); 352 ext2fs_swap_group_desc2(fs, gdp); 353 } 354#endif 355 dest += fs->blocksize*first_meta_bg; 356 } 357 for (i=first_meta_bg ; i < fs->desc_blocks; i++) { 358 blk = ext2fs_descriptor_block_loc2(fs, group_block, i); 359 retval = io_channel_read_blk64(fs->io, blk, 1, dest); 360 if (retval) 361 goto cleanup; 362#ifdef WORDS_BIGENDIAN 363 for (j=0; j < groups_per_block; j++) { 364 gdp = ext2fs_group_desc(fs, fs->group_desc, 365 i * groups_per_block + j); 366 ext2fs_swap_group_desc2(fs, gdp); 367 } 368#endif 369 dest += fs->blocksize; 370 } 371 372 fs->stride = fs->super->s_raid_stride; 373 374 /* 375 * If recovery is from backup superblock, Clear _UNININT flags & 376 * reset bg_itable_unused to zero 377 */ 378 if (superblock > 1 && EXT2_HAS_RO_COMPAT_FEATURE(fs->super, 379 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) { 380 dgrp_t group; 381 382 for (group = 0; group < fs->group_desc_count; group++) { 383 ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT); 384 ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT); 385 ext2fs_bg_itable_unused_set(fs, group, 0); 386 /* The checksum will be reset later, but fix it here 387 * anyway to avoid printing a lot of spurious errors. */ 388 ext2fs_group_desc_csum_set(fs, group); 389 } 390 if (fs->flags & EXT2_FLAG_RW) 391 ext2fs_mark_super_dirty(fs); 392 } 393 394 fs->flags &= ~EXT2_FLAG_NOFREE_ON_ERROR; 395 *ret_fs = fs; 396 397 if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) && 398 !(flags & EXT2_FLAG_SKIP_MMP) && 399 (flags & (EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE))) { 400 retval = ext2fs_mmp_start(fs); 401 if (retval) { 402 fs->flags |= EXT2_FLAG_SKIP_MMP; /* just do cleanup */ 403 ext2fs_mmp_stop(fs); 404 goto cleanup; 405 } 406 } 407 408 return 0; 409cleanup: 410 if (flags & EXT2_FLAG_NOFREE_ON_ERROR) 411 *ret_fs = fs; 412 else 413 ext2fs_free(fs); 414 return retval; 415} 416 417/* 418 * Set/get the filesystem data I/O channel. 419 * 420 * These functions are only valid if EXT2_FLAG_IMAGE_FILE is true. 421 */ 422errcode_t ext2fs_get_data_io(ext2_filsys fs, io_channel *old_io) 423{ 424 if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0) 425 return EXT2_ET_NOT_IMAGE_FILE; 426 if (old_io) { 427 *old_io = (fs->image_io == fs->io) ? 0 : fs->io; 428 } 429 return 0; 430} 431 432errcode_t ext2fs_set_data_io(ext2_filsys fs, io_channel new_io) 433{ 434 if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0) 435 return EXT2_ET_NOT_IMAGE_FILE; 436 fs->io = new_io ? new_io : fs->image_io; 437 return 0; 438} 439 440errcode_t ext2fs_rewrite_to_io(ext2_filsys fs, io_channel new_io) 441{ 442 if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0) 443 return EXT2_ET_NOT_IMAGE_FILE; 444 fs->io = fs->image_io = new_io; 445 fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_RW | 446 EXT2_FLAG_BB_DIRTY | EXT2_FLAG_IB_DIRTY; 447 fs->flags &= ~EXT2_FLAG_IMAGE_FILE; 448 return 0; 449} 450