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