openfs.c revision 96c6a3acd377698cb99ffd9925bec9b20ca4f6f9
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * openfs.c --- open an ext2 filesystem
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * %Begin-Header%
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * This file may be redistributed under the terms of the GNU Public
89ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch * License.
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * %End-Header%
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) */
113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stdio.h>
133551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include <string.h>
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if HAVE_UNISTD_H
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <unistd.h>
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <fcntl.h>
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <time.h>
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if HAVE_SYS_STAT_H
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <sys/stat.h>
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if HAVE_SYS_TYPES_H
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <sys/types.h>
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ext2_fs.h"
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ext2fs.h"
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "e2image.h"
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)blk_t ext2fs_descriptor_block_loc(ext2_filsys fs, blk_t group_block, dgrp_t i)
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles){
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	int	bg;
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	int	has_super = 0;
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	int	ret_blk;
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	if (!(fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) ||
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    (i < fs->super->s_first_meta_bg))
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)		return (group_block + i + 1);
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	bg = (fs->blocksize / sizeof (struct ext2_group_desc)) * i;
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	if (ext2fs_bg_has_super(fs, bg))
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)		has_super = 1;
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	ret_blk = (fs->super->s_first_data_block + has_super +
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)		   (bg * fs->super->s_blocks_per_group));
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	/*
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	 * If group_block is not the normal value, we're trying to use
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	 * the backup group descriptors and superblock --- so use the
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	 * alternate location of the second block group in the
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	 * metablock group.  Ideally we should be testing each bg
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	 * descriptor block individually for correctness, but we don't
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	 * have the infrastructure in place to do that.
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	 */
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	if (group_block != fs->super->s_first_data_block &&
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	    ((ret_blk + fs->super->s_blocks_per_group) <
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	     fs->super->s_blocks_count))
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)		ret_blk += fs->super->s_blocks_per_group;
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	return ret_blk;
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)errcode_t ext2fs_open(const char *name, int flags, int superblock,
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)		      unsigned int block_size, io_manager manager,
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)		      ext2_filsys *ret_fs)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	return ext2fs_open2(name, 0, flags, superblock, block_size,
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			    manager, ret_fs);
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/*
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) *  Note: if superblock is non-zero, block-size must also be non-zero.
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * 	Superblock and block_size can be zero to use the default size.
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) *
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Valid flags for ext2fs_open()
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 	EXT2_FLAG_RW	- Open the filesystem for read/write.
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * 	EXT2_FLAG_FORCE - Open the filesystem even if some of the
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) *				features aren't supported.
794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) *	EXT2_FLAG_JOURNAL_DEV_OK - Open an ext3 journal device
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)errcode_t ext2fs_open2(const char *name, const char *io_options,
824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)		       int flags, int superblock,
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		       unsigned int block_size, io_manager manager,
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		       ext2_filsys *ret_fs)
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	ext2_filsys	fs;
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	errcode_t	retval;
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	unsigned long	i;
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	__u32		features;
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int		j, groups_per_block, blocks_per_group, io_flags;
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	blk_t		group_block, blk;
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	char		*dest, *cp;
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct ext2_group_desc *gdp;
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)	if (retval)
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		return retval;
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	memset(fs, 0, sizeof(struct struct_ext2_filsys));
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	fs->flags = flags;
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	fs->umask = 022;
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name);
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	if (retval)
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)		goto cleanup;
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	strcpy(fs->device_name, name);
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cp = strchr(fs->device_name, '?');
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (!io_options && cp) {
1114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)		*cp++ = 0;
1124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)		io_options = cp;
1134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)	}
1144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)	io_flags = 0;
1164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)	if (flags & EXT2_FLAG_RW)
1174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)		io_flags |= IO_FLAG_RW;
1184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)	if (flags & EXT2_FLAG_EXCLUSIVE)
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		io_flags |= IO_FLAG_EXCLUSIVE;
1203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)	retval = manager->open(fs->device_name, io_flags, &fs->io);
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	if (retval)
1223551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)		goto cleanup;
1233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)	if (io_options &&
1243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)	    (retval = io_channel_set_options(fs->io, io_options)))
1253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)		goto cleanup;
1263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)	fs->image_io = fs->io;
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	fs->io->app_data = fs;
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->super);
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	if (retval)
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		goto cleanup;
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (flags & EXT2_FLAG_IMAGE_FILE) {
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		retval = ext2fs_get_mem(sizeof(struct ext2_image_hdr),
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)					&fs->image_header);
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (retval)
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			goto cleanup;
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		retval = io_channel_read_blk(fs->io, 0,
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)					     -(int)sizeof(struct ext2_image_hdr),
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)					     fs->image_header);
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (retval)
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			goto cleanup;
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (fs->image_header->magic_number != EXT2_ET_MAGIC_E2IMAGE)
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			return EXT2_ET_MAGIC_E2IMAGE;
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		superblock = 1;
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		block_size = fs->image_header->fs_blocksize;
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/*
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	 * If the user specifies a specific block # for the
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	 * superblock, then he/she must also specify the block size!
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	 * Otherwise, read the master superblock located at offset
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	 * SUPERBLOCK_OFFSET from the start of the partition.
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	 *
15358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)	 * Note: we only save a backup copy of the superblock if we
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	 * are reading the superblock from the primary superblock location.
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	 */
1564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)	if (superblock) {
1574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)		if (!block_size) {
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			retval = EXT2_ET_INVALID_ARGUMENT;
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			goto cleanup;
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		}
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		io_channel_set_blksize(fs->io, block_size);
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		group_block = superblock;
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fs->orig_super = 0;
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	} else {
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
166868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)		superblock = 1;
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		group_block = 0;
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->orig_super);
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (retval)
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			goto cleanup;
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
173868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)				     fs->super);
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (retval)
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		goto cleanup;
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (fs->orig_super)
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		memcpy(fs->orig_super, fs->super, SUPERBLOCK_SIZE);
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef EXT2FS_ENABLE_SWAPFS
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if ((fs->super->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) ||
181868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)	    (fs->flags & EXT2_FLAG_SWAP_BYTES)) {
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		fs->flags |= EXT2_FLAG_SWAP_BYTES;
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		ext2fs_swap_super(fs->super);
185	}
186#endif
187
188	if (fs->super->s_magic != EXT2_SUPER_MAGIC) {
189		retval = EXT2_ET_BAD_MAGIC;
190		goto cleanup;
191	}
192	if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
193		retval = EXT2_ET_REV_TOO_HIGH;
194		goto cleanup;
195	}
196
197	/*
198	 * Check for feature set incompatibility
199	 */
200	if (!(flags & EXT2_FLAG_FORCE)) {
201		features = fs->super->s_feature_incompat;
202#ifdef EXT2_LIB_SOFTSUPP_INCOMPAT
203		if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
204			features &= !EXT2_LIB_SOFTSUPP_INCOMPAT;
205#endif
206		if (features & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
207			retval = EXT2_ET_UNSUPP_FEATURE;
208			goto cleanup;
209		}
210
211		features = fs->super->s_feature_ro_compat;
212#ifdef EXT2_LIB_SOFTSUPP_RO_COMPAT
213		if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
214			features &= !EXT2_LIB_SOFTSUPP_RO_COMPAT;
215#endif
216		if ((flags & EXT2_FLAG_RW) &&
217		    (features & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
218			retval = EXT2_ET_RO_UNSUPP_FEATURE;
219			goto cleanup;
220		}
221
222		if (!(flags & EXT2_FLAG_JOURNAL_DEV_OK) &&
223		    (fs->super->s_feature_incompat &
224		     EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
225			retval = EXT2_ET_UNSUPP_FEATURE;
226			goto cleanup;
227		}
228	}
229
230	fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
231	if (fs->blocksize == 0) {
232		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
233		goto cleanup;
234	}
235	fs->fragsize = EXT2_FRAG_SIZE(fs->super);
236	fs->inode_blocks_per_group = ((fs->super->s_inodes_per_group *
237				       EXT2_INODE_SIZE(fs->super) +
238				       EXT2_BLOCK_SIZE(fs->super) - 1) /
239				      EXT2_BLOCK_SIZE(fs->super));
240	if (block_size) {
241		if (block_size != fs->blocksize) {
242			retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
243			goto cleanup;
244		}
245	}
246	/*
247	 * Set the blocksize to the filesystem's blocksize.
248	 */
249	io_channel_set_blksize(fs->io, fs->blocksize);
250
251	/*
252	 * If this is an external journal device, don't try to read
253	 * the group descriptors, because they're not there.
254	 */
255	if (fs->super->s_feature_incompat &
256	    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
257		fs->group_desc_count = 0;
258		*ret_fs = fs;
259		return 0;
260	}
261
262	/*
263	 * Read group descriptors
264	 */
265	blocks_per_group = EXT2_BLOCKS_PER_GROUP(fs->super);
266	if (blocks_per_group == 0 ||
267	    blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(fs->super) ||
268	    fs->inode_blocks_per_group > EXT2_MAX_INODES_PER_GROUP(fs->super)) {
269		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
270		goto cleanup;
271	}
272	fs->group_desc_count = ext2fs_div_ceil(fs->super->s_blocks_count -
273					       fs->super->s_first_data_block,
274					       blocks_per_group);
275	fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
276					  EXT2_DESC_PER_BLOCK(fs->super));
277	retval = ext2fs_get_mem(fs->desc_blocks * fs->blocksize,
278				&fs->group_desc);
279	if (retval)
280		goto cleanup;
281	if (!group_block)
282		group_block = fs->super->s_first_data_block;
283	dest = (char *) fs->group_desc;
284	groups_per_block = fs->blocksize / sizeof(struct ext2_group_desc);
285	for (i=0 ; i < fs->desc_blocks; i++) {
286		blk = ext2fs_descriptor_block_loc(fs, group_block, i);
287		retval = io_channel_read_blk(fs->io, blk, 1, dest);
288		if (retval)
289			goto cleanup;
290#ifdef EXT2FS_ENABLE_SWAPFS
291		if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
292			gdp = (struct ext2_group_desc *) dest;
293			for (j=0; j < groups_per_block; j++)
294				ext2fs_swap_group_desc(gdp++);
295		}
296#endif
297		dest += fs->blocksize;
298	}
299
300	fs->stride = fs->super->s_raid_stride;
301
302	*ret_fs = fs;
303	return 0;
304cleanup:
305	ext2fs_free(fs);
306	return retval;
307}
308
309/*
310 * Set/get the filesystem data I/O channel.
311 *
312 * These functions are only valid if EXT2_FLAG_IMAGE_FILE is true.
313 */
314errcode_t ext2fs_get_data_io(ext2_filsys fs, io_channel *old_io)
315{
316	if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
317		return EXT2_ET_NOT_IMAGE_FILE;
318	if (old_io) {
319		*old_io = (fs->image_io == fs->io) ? 0 : fs->io;
320	}
321	return 0;
322}
323
324errcode_t ext2fs_set_data_io(ext2_filsys fs, io_channel new_io)
325{
326	if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
327		return EXT2_ET_NOT_IMAGE_FILE;
328	fs->io = new_io ? new_io : fs->image_io;
329	return 0;
330}
331
332errcode_t ext2fs_rewrite_to_io(ext2_filsys fs, io_channel new_io)
333{
334	if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
335		return EXT2_ET_NOT_IMAGE_FILE;
336	fs->io = fs->image_io = new_io;
337	fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_RW |
338		EXT2_FLAG_BB_DIRTY | EXT2_FLAG_IB_DIRTY;
339	fs->flags &= ~EXT2_FLAG_IMAGE_FILE;
340	return 0;
341}
342