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