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