2 * openfs.c --- open an ext2 filesystem
4 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
23 #include <sys/types.h>
26 #if EXT2_FLAT_INCLUDES
29 #include <linux/ext2_fs.h>
35 * Note: if superblock is non-zero, block-size must also be non-zero.
36 * Superblock and block_size can be zero to use the default size.
38 * Valid flags for ext2fs_open()
40 * EXT2_FLAG_RW - Open the filesystem for read/write.
41 * EXT2_FLAG_FORCE - Open the filesystem even if some of the
42 * features aren't supported.
44 errcode_t ext2fs_open(const char *name, int flags, int superblock,
45 int block_size, io_manager manager, ext2_filsys *ret_fs)
49 int i, j, groups_per_block;
52 struct ext2_group_desc *gdp;
55 EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);
57 retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys),
62 memset(fs, 0, sizeof(struct struct_ext2_filsys));
63 fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
65 retval = manager->open(name, (flags & EXT2_FLAG_RW) ? IO_FLAG_RW : 0,
69 fs->io->app_data = fs;
70 retval = ext2fs_get_mem(strlen(name)+1, (void **) &fs->device_name);
73 strcpy(fs->device_name, name);
74 retval = ext2fs_get_mem(SUPERBLOCK_SIZE, (void **) &fs->super);
79 * If the user specifies a specific block # for the
80 * superblock, then he/she must also specify the block size!
81 * Otherwise, read the master superblock located at offset
82 * SUPERBLOCK_OFFSET from the start of the partition.
86 retval = EXT2_ET_INVALID_ARGUMENT;
89 io_channel_set_blksize(fs->io, block_size);
90 group_block = superblock + 1;
92 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
96 retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
101 if ((fs->super->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) ||
102 (fs->flags & EXT2_FLAG_SWAP_BYTES)) {
103 fs->flags |= EXT2_FLAG_SWAP_BYTES;
105 ext2fs_swap_super(fs->super);
108 if (fs->super->s_magic != EXT2_SUPER_MAGIC) {
109 retval = EXT2_ET_BAD_MAGIC;
112 #ifdef EXT2_DYNAMIC_REV
113 if (fs->super->s_rev_level > EXT2_DYNAMIC_REV) {
114 retval = EXT2_ET_REV_TOO_HIGH;
118 #ifdef EXT2_CURRENT_REV
119 if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
120 retval = EXT2_ET_REV_TOO_HIGH;
126 * Check for feature set incompatibility
128 if (!(flags & EXT2_FLAG_FORCE)) {
129 s = (struct ext2fs_sb *) fs->super;
130 if (s->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
131 retval = EXT2_ET_UNSUPP_FEATURE;
134 if ((flags & EXT2_FLAG_RW) &&
135 (s->s_feature_ro_compat &
136 ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
137 retval = EXT2_ET_RO_UNSUPP_FEATURE;
142 fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
143 if (fs->blocksize == 0) {
144 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
147 fs->fragsize = EXT2_FRAG_SIZE(fs->super);
148 fs->inode_blocks_per_group = ((fs->super->s_inodes_per_group *
149 EXT2_INODE_SIZE(fs->super) +
150 EXT2_BLOCK_SIZE(fs->super) - 1) /
151 EXT2_BLOCK_SIZE(fs->super));
153 if (block_size != fs->blocksize) {
154 retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
159 * Set the blocksize to the filesystem's blocksize.
161 io_channel_set_blksize(fs->io, fs->blocksize);
164 * Read group descriptors
166 fs->group_desc_count = (fs->super->s_blocks_count -
167 fs->super->s_first_data_block +
168 EXT2_BLOCKS_PER_GROUP(fs->super) - 1)
169 / EXT2_BLOCKS_PER_GROUP(fs->super);
170 fs->desc_blocks = (fs->group_desc_count +
171 EXT2_DESC_PER_BLOCK(fs->super) - 1)
172 / EXT2_DESC_PER_BLOCK(fs->super);
173 retval = ext2fs_get_mem(fs->desc_blocks * fs->blocksize,
174 (void **) &fs->group_desc);
178 group_block = fs->super->s_first_data_block + 1;
179 dest = (char *) fs->group_desc;
180 for (i=0 ; i < fs->desc_blocks; i++) {
181 retval = io_channel_read_blk(fs->io, group_block, 1, dest);
185 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
186 gdp = (struct ext2_group_desc *) dest;
187 groups_per_block = fs->blocksize /
188 sizeof(struct ext2_group_desc);
189 for (j=0; j < groups_per_block; j++)
190 ext2fs_swap_group_desc(gdp++);
192 dest += fs->blocksize;