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