Home | History | Annotate | Download | only in ext2fs
      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 
     36 blk64_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, group_zero_adjust = 0;
     41 	blk64_t	ret_blk;
     42 
     43 	/*
     44 	 * On a bigalloc FS with 1K blocks, block 0 is reserved for non-ext4
     45 	 * stuff, so adjust for that if we're being asked for group 0.
     46 	 */
     47 	if (i == 0 && fs->blocksize == 1024 && EXT2FS_CLUSTER_RATIO(fs) > 1)
     48 		group_zero_adjust = 1;
     49 
     50 	if (!ext2fs_has_feature_meta_bg(fs->super) ||
     51 	    (i < fs->super->s_first_meta_bg))
     52 		return group_block + i + 1 + group_zero_adjust;
     53 
     54 	bg = EXT2_DESC_PER_BLOCK(fs->super) * i;
     55 	if (ext2fs_bg_has_super(fs, bg))
     56 		has_super = 1;
     57 	ret_blk = ext2fs_group_first_block2(fs, bg);
     58 	/*
     59 	 * If group_block is not the normal value, we're trying to use
     60 	 * the backup group descriptors and superblock --- so use the
     61 	 * alternate location of the second block group in the
     62 	 * metablock group.  Ideally we should be testing each bg
     63 	 * descriptor block individually for correctness, but we don't
     64 	 * have the infrastructure in place to do that.
     65 	 */
     66 	if (group_block != fs->super->s_first_data_block &&
     67 	    ((ret_blk + has_super + fs->super->s_blocks_per_group) <
     68 	     ext2fs_blocks_count(fs->super))) {
     69 		ret_blk += fs->super->s_blocks_per_group;
     70 
     71 		/*
     72 		 * If we're going to jump forward a block group, make sure
     73 		 * that we adjust has_super to account for the next group's
     74 		 * backup superblock (or lack thereof).
     75 		 */
     76 		if (ext2fs_bg_has_super(fs, bg + 1))
     77 			has_super = 1;
     78 		else
     79 			has_super = 0;
     80 	}
     81 	return ret_blk + has_super + group_zero_adjust;
     82 }
     83 
     84 blk_t ext2fs_descriptor_block_loc(ext2_filsys fs, blk_t group_block, dgrp_t i)
     85 {
     86 	return ext2fs_descriptor_block_loc2(fs, group_block, i);
     87 }
     88 
     89 errcode_t ext2fs_open(const char *name, int flags, int superblock,
     90 		      unsigned int block_size, io_manager manager,
     91 		      ext2_filsys *ret_fs)
     92 {
     93 	return ext2fs_open2(name, 0, flags, superblock, block_size,
     94 			    manager, ret_fs);
     95 }
     96 
     97 static void block_sha_map_free_entry(void *data)
     98 {
     99 	free(data);
    100 	return;
    101 }
    102 
    103 /*
    104  *  Note: if superblock is non-zero, block-size must also be non-zero.
    105  * 	Superblock and block_size can be zero to use the default size.
    106  *
    107  * Valid flags for ext2fs_open()
    108  *
    109  * 	EXT2_FLAG_RW	- Open the filesystem for read/write.
    110  * 	EXT2_FLAG_FORCE - Open the filesystem even if some of the
    111  *				features aren't supported.
    112  *	EXT2_FLAG_JOURNAL_DEV_OK - Open an ext3 journal device
    113  *	EXT2_FLAG_SKIP_MMP - Open without multi-mount protection check.
    114  *	EXT2_FLAG_64BITS - Allow 64-bit bitfields (needed for large
    115  *				filesystems)
    116  */
    117 errcode_t ext2fs_open2(const char *name, const char *io_options,
    118 		       int flags, int superblock,
    119 		       unsigned int block_size, io_manager manager,
    120 		       ext2_filsys *ret_fs)
    121 {
    122 	ext2_filsys	fs;
    123 	errcode_t	retval;
    124 	unsigned long	i, first_meta_bg;
    125 	__u32		features;
    126 	unsigned int	blocks_per_group, io_flags;
    127 	blk64_t		group_block, blk;
    128 	char		*dest, *cp;
    129 	int		group_zero_adjust = 0;
    130 	unsigned int	inode_size;
    131 	__u64		groups_cnt;
    132 #ifdef WORDS_BIGENDIAN
    133 	unsigned int	groups_per_block;
    134 	struct ext2_group_desc *gdp;
    135 	int		j;
    136 #endif
    137 	char		*time_env;
    138 
    139 	EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);
    140 
    141 	retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
    142 	if (retval)
    143 		return retval;
    144 
    145 	memset(fs, 0, sizeof(struct struct_ext2_filsys));
    146 	fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
    147 	fs->flags = flags;
    148 	/* don't overwrite sb backups unless flag is explicitly cleared */
    149 	fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
    150 	fs->umask = 022;
    151 
    152 	time_env = getenv("E2FSPROGS_FAKE_TIME");
    153 	if (time_env)
    154 		fs->now = strtoul(time_env, NULL, 0);
    155 
    156 	retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name);
    157 	if (retval)
    158 		goto cleanup;
    159 	strcpy(fs->device_name, name);
    160 	cp = strchr(fs->device_name, '?');
    161 	if (!io_options && cp) {
    162 		*cp++ = 0;
    163 		io_options = cp;
    164 	}
    165 
    166 	io_flags = 0;
    167 	if (flags & EXT2_FLAG_RW)
    168 		io_flags |= IO_FLAG_RW;
    169 	if (flags & EXT2_FLAG_EXCLUSIVE)
    170 		io_flags |= IO_FLAG_EXCLUSIVE;
    171 	if (flags & EXT2_FLAG_DIRECT_IO)
    172 		io_flags |= IO_FLAG_DIRECT_IO;
    173 	retval = manager->open(fs->device_name, io_flags, &fs->io);
    174 	if (retval)
    175 		goto cleanup;
    176 	if (io_options &&
    177 	    (retval = io_channel_set_options(fs->io, io_options)))
    178 		goto cleanup;
    179 	fs->image_io = fs->io;
    180 	fs->io->app_data = fs;
    181 	retval = io_channel_alloc_buf(fs->io, -SUPERBLOCK_SIZE, &fs->super);
    182 	if (retval)
    183 		goto cleanup;
    184 	if (flags & EXT2_FLAG_IMAGE_FILE) {
    185 		retval = ext2fs_get_mem(sizeof(struct ext2_image_hdr),
    186 					&fs->image_header);
    187 		if (retval)
    188 			goto cleanup;
    189 		retval = io_channel_read_blk(fs->io, 0,
    190 					     -(int)sizeof(struct ext2_image_hdr),
    191 					     fs->image_header);
    192 		if (retval)
    193 			goto cleanup;
    194 		if (ext2fs_le32_to_cpu(fs->image_header->magic_number) != EXT2_ET_MAGIC_E2IMAGE)
    195 			return EXT2_ET_MAGIC_E2IMAGE;
    196 		superblock = 1;
    197 		block_size = ext2fs_le32_to_cpu(fs->image_header->fs_blocksize);
    198 	}
    199 
    200 	/*
    201 	 * If the user specifies a specific block # for the
    202 	 * superblock, then he/she must also specify the block size!
    203 	 * Otherwise, read the master superblock located at offset
    204 	 * SUPERBLOCK_OFFSET from the start of the partition.
    205 	 *
    206 	 * Note: we only save a backup copy of the superblock if we
    207 	 * are reading the superblock from the primary superblock location.
    208 	 */
    209 	if (superblock) {
    210 		if (!block_size) {
    211 			retval = EXT2_ET_INVALID_ARGUMENT;
    212 			goto cleanup;
    213 		}
    214 		io_channel_set_blksize(fs->io, block_size);
    215 		group_block = superblock;
    216 		fs->orig_super = 0;
    217 	} else {
    218 		io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
    219 		superblock = 1;
    220 		group_block = 0;
    221 		retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->orig_super);
    222 		if (retval)
    223 			goto cleanup;
    224 	}
    225 	retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
    226 				     fs->super);
    227 	if (retval)
    228 		goto cleanup;
    229 	if (fs->orig_super)
    230 		memcpy(fs->orig_super, fs->super, SUPERBLOCK_SIZE);
    231 
    232 	if (!(fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS)) {
    233 		retval = 0;
    234 		if (!ext2fs_verify_csum_type(fs, fs->super))
    235 			retval = EXT2_ET_UNKNOWN_CSUM;
    236 		if (!ext2fs_superblock_csum_verify(fs, fs->super))
    237 			retval = EXT2_ET_SB_CSUM_INVALID;
    238 	}
    239 
    240 #ifdef WORDS_BIGENDIAN
    241 	fs->flags |= EXT2_FLAG_SWAP_BYTES;
    242 	ext2fs_swap_super(fs->super);
    243 #else
    244 	if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
    245 		retval = EXT2_ET_UNIMPLEMENTED;
    246 		goto cleanup;
    247 	}
    248 #endif
    249 
    250 	if (fs->super->s_magic != EXT2_SUPER_MAGIC)
    251 		retval = EXT2_ET_BAD_MAGIC;
    252 	if (retval)
    253 		goto cleanup;
    254 
    255 	if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
    256 		retval = EXT2_ET_REV_TOO_HIGH;
    257 		goto cleanup;
    258 	}
    259 
    260 	/*
    261 	 * Check for feature set incompatibility
    262 	 */
    263 	if (!(flags & EXT2_FLAG_FORCE)) {
    264 		features = fs->super->s_feature_incompat;
    265 #ifdef EXT2_LIB_SOFTSUPP_INCOMPAT
    266 		if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
    267 			features &= ~EXT2_LIB_SOFTSUPP_INCOMPAT;
    268 #endif
    269 		if (features & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
    270 			retval = EXT2_ET_UNSUPP_FEATURE;
    271 			goto cleanup;
    272 		}
    273 
    274 		features = fs->super->s_feature_ro_compat;
    275 #ifdef EXT2_LIB_SOFTSUPP_RO_COMPAT
    276 		if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
    277 			features &= ~EXT2_LIB_SOFTSUPP_RO_COMPAT;
    278 #endif
    279 		if ((flags & EXT2_FLAG_RW) &&
    280 		    (features & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
    281 			retval = EXT2_ET_RO_UNSUPP_FEATURE;
    282 			goto cleanup;
    283 		}
    284 
    285 		if (!(flags & EXT2_FLAG_JOURNAL_DEV_OK) &&
    286 		    ext2fs_has_feature_journal_dev(fs->super)) {
    287 			retval = EXT2_ET_UNSUPP_FEATURE;
    288 			goto cleanup;
    289 		}
    290 	}
    291 
    292 	if (fs->super->s_log_block_size >
    293 	    (unsigned) (EXT2_MAX_BLOCK_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE)) {
    294 		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
    295 		goto cleanup;
    296 	}
    297 
    298 	/*
    299 	 * bigalloc requires cluster-aware bitfield operations, which at the
    300 	 * moment means we need EXT2_FLAG_64BITS.
    301 	 */
    302 	if (ext2fs_has_feature_bigalloc(fs->super) &&
    303 	    !(flags & EXT2_FLAG_64BITS)) {
    304 		retval = EXT2_ET_CANT_USE_LEGACY_BITMAPS;
    305 		goto cleanup;
    306 	}
    307 
    308 	if (!ext2fs_has_feature_bigalloc(fs->super) &&
    309 	    (fs->super->s_log_block_size != fs->super->s_log_cluster_size)) {
    310 		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
    311 		goto cleanup;
    312 	}
    313 	fs->fragsize = fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
    314 	inode_size = EXT2_INODE_SIZE(fs->super);
    315 	if ((inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
    316 	    (inode_size > fs->blocksize) ||
    317 	    (inode_size & (inode_size - 1))) {
    318 		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
    319 		goto cleanup;
    320 	}
    321 
    322 	/* Enforce the block group descriptor size */
    323 	if (ext2fs_has_feature_64bit(fs->super)) {
    324 		if (fs->super->s_desc_size < EXT2_MIN_DESC_SIZE_64BIT) {
    325 			retval = EXT2_ET_BAD_DESC_SIZE;
    326 			goto cleanup;
    327 		}
    328 	}
    329 
    330 	fs->cluster_ratio_bits = fs->super->s_log_cluster_size -
    331 		fs->super->s_log_block_size;
    332 	if (EXT2_BLOCKS_PER_GROUP(fs->super) !=
    333 	    EXT2_CLUSTERS_PER_GROUP(fs->super) << fs->cluster_ratio_bits) {
    334 		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
    335 		goto cleanup;
    336 	}
    337 	fs->inode_blocks_per_group = ((EXT2_INODES_PER_GROUP(fs->super) *
    338 				       EXT2_INODE_SIZE(fs->super) +
    339 				       EXT2_BLOCK_SIZE(fs->super) - 1) /
    340 				      EXT2_BLOCK_SIZE(fs->super));
    341 	if (block_size) {
    342 		if (block_size != fs->blocksize) {
    343 			retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
    344 			goto cleanup;
    345 		}
    346 	}
    347 	/*
    348 	 * Set the blocksize to the filesystem's blocksize.
    349 	 */
    350 	io_channel_set_blksize(fs->io, fs->blocksize);
    351 
    352 	/*
    353 	 * If this is an external journal device, don't try to read
    354 	 * the group descriptors, because they're not there.
    355 	 */
    356 	if (ext2fs_has_feature_journal_dev(fs->super)) {
    357 		fs->group_desc_count = 0;
    358 		*ret_fs = fs;
    359 		return 0;
    360 	}
    361 
    362 	if (EXT2_INODES_PER_GROUP(fs->super) == 0) {
    363 		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
    364 		goto cleanup;
    365 	}
    366 	/* Precompute the FS UUID to seed other checksums */
    367 	ext2fs_init_csum_seed(fs);
    368 
    369 	/*
    370 	 * Read group descriptors
    371 	 */
    372 	blocks_per_group = EXT2_BLOCKS_PER_GROUP(fs->super);
    373 	if (blocks_per_group == 0 ||
    374 	    blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(fs->super) ||
    375 	    fs->inode_blocks_per_group > EXT2_MAX_INODES_PER_GROUP(fs->super) ||
    376            EXT2_DESC_PER_BLOCK(fs->super) == 0 ||
    377            fs->super->s_first_data_block >= ext2fs_blocks_count(fs->super)) {
    378 		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
    379 		goto cleanup;
    380 	}
    381 	groups_cnt = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
    382 				       fs->super->s_first_data_block,
    383 				       blocks_per_group);
    384 	if (groups_cnt >> 32) {
    385 		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
    386 		goto cleanup;
    387 	}
    388 	fs->group_desc_count = 	groups_cnt;
    389 	if (!(flags & EXT2_FLAG_IGNORE_SB_ERRORS) &&
    390 	    (__u64)fs->group_desc_count * EXT2_INODES_PER_GROUP(fs->super) !=
    391 	    fs->super->s_inodes_count) {
    392 		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
    393 		goto cleanup;
    394 	}
    395 	fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
    396 					  EXT2_DESC_PER_BLOCK(fs->super));
    397 	retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
    398 				&fs->group_desc);
    399 	if (retval)
    400 		goto cleanup;
    401 	if (!group_block)
    402 		group_block = fs->super->s_first_data_block;
    403 	/*
    404 	 * On a FS with a 1K blocksize, block 0 is reserved for bootloaders
    405 	 * so we must increment block numbers to any group 0 items.
    406 	 *
    407 	 * However, we cannot touch group_block directly because in the meta_bg
    408 	 * case, the ext2fs_descriptor_block_loc2() function will interpret
    409 	 * group_block != s_first_data_block to mean that we want to access the
    410 	 * backup group descriptors.  This is not what we want if the caller
    411 	 * set superblock == 0 (i.e. auto-detect the superblock), which is
    412 	 * what's going on here.
    413 	 */
    414 	if (group_block == 0 && fs->blocksize == 1024)
    415 		group_zero_adjust = 1;
    416 	dest = (char *) fs->group_desc;
    417 #ifdef WORDS_BIGENDIAN
    418 	groups_per_block = EXT2_DESC_PER_BLOCK(fs->super);
    419 #endif
    420 	if (ext2fs_has_feature_meta_bg(fs->super) &&
    421 	    !(flags & EXT2_FLAG_IMAGE_FILE)) {
    422 		first_meta_bg = fs->super->s_first_meta_bg;
    423 		if (first_meta_bg > fs->desc_blocks)
    424 			first_meta_bg = fs->desc_blocks;
    425 	} else
    426 		first_meta_bg = fs->desc_blocks;
    427 	if (first_meta_bg) {
    428 		retval = io_channel_read_blk(fs->io, group_block +
    429 					     group_zero_adjust + 1,
    430 					     first_meta_bg, dest);
    431 		if (retval)
    432 			goto cleanup;
    433 #ifdef WORDS_BIGENDIAN
    434 		gdp = (struct ext2_group_desc *) dest;
    435 		for (j=0; j < groups_per_block*first_meta_bg; j++) {
    436 			gdp = ext2fs_group_desc(fs, fs->group_desc, j);
    437 			ext2fs_swap_group_desc2(fs, gdp);
    438 		}
    439 #endif
    440 		dest += fs->blocksize*first_meta_bg;
    441 	}
    442 
    443 	for (i = first_meta_bg ; i < fs->desc_blocks; i++) {
    444 		blk = ext2fs_descriptor_block_loc2(fs, group_block, i);
    445 		io_channel_cache_readahead(fs->io, blk, 1);
    446 	}
    447 
    448 	for (i=first_meta_bg ; i < fs->desc_blocks; i++) {
    449 		blk = ext2fs_descriptor_block_loc2(fs, group_block, i);
    450 		retval = io_channel_read_blk64(fs->io, blk, 1, dest);
    451 		if (retval)
    452 			goto cleanup;
    453 #ifdef WORDS_BIGENDIAN
    454 		for (j=0; j < groups_per_block; j++) {
    455 			gdp = ext2fs_group_desc(fs, fs->group_desc,
    456 						i * groups_per_block + j);
    457 			ext2fs_swap_group_desc2(fs, gdp);
    458 		}
    459 #endif
    460 		dest += fs->blocksize;
    461 	}
    462 
    463 	fs->stride = fs->super->s_raid_stride;
    464 
    465 	/*
    466 	 * If recovery is from backup superblock, Clear _UNININT flags &
    467 	 * reset bg_itable_unused to zero
    468 	 */
    469 	if (superblock > 1 && ext2fs_has_group_desc_csum(fs)) {
    470 		dgrp_t group;
    471 
    472 		for (group = 0; group < fs->group_desc_count; group++) {
    473 			ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
    474 			ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
    475 			ext2fs_bg_itable_unused_set(fs, group, 0);
    476 			/* The checksum will be reset later, but fix it here
    477 			 * anyway to avoid printing a lot of spurious errors. */
    478 			ext2fs_group_desc_csum_set(fs, group);
    479 		}
    480 		if (fs->flags & EXT2_FLAG_RW)
    481 			ext2fs_mark_super_dirty(fs);
    482 	}
    483 
    484 	if (ext2fs_has_feature_mmp(fs->super) &&
    485 	    !(flags & EXT2_FLAG_SKIP_MMP) &&
    486 	    (flags & (EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE))) {
    487 		retval = ext2fs_mmp_start(fs);
    488 		if (retval) {
    489 			fs->flags |= EXT2_FLAG_SKIP_MMP; /* just do cleanup */
    490 			ext2fs_mmp_stop(fs);
    491 			goto cleanup;
    492 		}
    493 	}
    494 
    495 	if (fs->flags & EXT2_FLAG_SHARE_DUP) {
    496 		fs->block_sha_map = ext2fs_hashmap_create(ext2fs_djb2_hash,
    497 					block_sha_map_free_entry, 4096);
    498 		if (!fs->block_sha_map) {
    499 			retval = EXT2_ET_NO_MEMORY;
    500 			goto cleanup;
    501 		}
    502 		ext2fs_set_feature_shared_blocks(fs->super);
    503 	}
    504 
    505 	fs->flags &= ~EXT2_FLAG_NOFREE_ON_ERROR;
    506 	*ret_fs = fs;
    507 
    508 	return 0;
    509 cleanup:
    510 	if (!(flags & EXT2_FLAG_NOFREE_ON_ERROR)) {
    511 		ext2fs_free(fs);
    512 		fs = NULL;
    513 	}
    514 	*ret_fs = fs;
    515 	return retval;
    516 }
    517 
    518 /*
    519  * Set/get the filesystem data I/O channel.
    520  *
    521  * These functions are only valid if EXT2_FLAG_IMAGE_FILE is true.
    522  */
    523 errcode_t ext2fs_get_data_io(ext2_filsys fs, io_channel *old_io)
    524 {
    525 	if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
    526 		return EXT2_ET_NOT_IMAGE_FILE;
    527 	if (old_io) {
    528 		*old_io = (fs->image_io == fs->io) ? 0 : fs->io;
    529 	}
    530 	return 0;
    531 }
    532 
    533 errcode_t ext2fs_set_data_io(ext2_filsys fs, io_channel new_io)
    534 {
    535 	if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
    536 		return EXT2_ET_NOT_IMAGE_FILE;
    537 	fs->io = new_io ? new_io : fs->image_io;
    538 	return 0;
    539 }
    540 
    541 errcode_t ext2fs_rewrite_to_io(ext2_filsys fs, io_channel new_io)
    542 {
    543 	errcode_t err;
    544 
    545 	if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
    546 		return EXT2_ET_NOT_IMAGE_FILE;
    547 	err = io_channel_set_blksize(new_io, fs->blocksize);
    548 	if (err)
    549 		return err;
    550 	if ((new_io == fs->image_io) || (new_io == fs->io))
    551 		return 0;
    552 	if ((fs->image_io != fs->io) &&
    553 	    fs->image_io)
    554 		io_channel_close(fs->image_io);
    555 	if (fs->io)
    556 		io_channel_close(fs->io);
    557 	fs->io = fs->image_io = new_io;
    558 	fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_RW |
    559 		EXT2_FLAG_BB_DIRTY | EXT2_FLAG_IB_DIRTY;
    560 	fs->flags &= ~EXT2_FLAG_IMAGE_FILE;
    561 	return 0;
    562 }
    563