2 * openfs.c --- open an ext2 filesystem
4 * Copyright (C) 1993, 1994, 1995 Theodore Ts'o.
6 * This file may be redistributed under the terms of the GNU Public
24 #include <sys/types.h>
26 #include <linux/ext2_fs.h>
31 * Note: if superblock is non-zero, block-size must also be non-zero.
32 * Superblock and block_size can be zero to use the default size.
34 errcode_t ext2fs_open(const char *name, int flags, int superblock,
35 int block_size, io_manager manager, ext2_filsys *ret_fs)
39 int i, j, group_block, groups_per_block;
41 struct ext2_group_desc *gdp;
43 EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);
45 fs = (ext2_filsys) malloc(sizeof(struct struct_ext2_filsys));
49 memset(fs, 0, sizeof(struct struct_ext2_filsys));
50 fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
52 retval = manager->open(name, (flags & EXT2_FLAG_RW) ? IO_FLAG_RW : 0,
56 fs->device_name = malloc(strlen(name)+1);
57 if (!fs->device_name) {
61 strcpy(fs->device_name, name);
62 fs->super = malloc(SUPERBLOCK_SIZE);
69 * If the user specifies a specific block # for the
70 * superblock, then he/she must also specify the block size!
71 * Otherwise, read the master superblock located at offset
72 * SUPERBLOCK_OFFSET from the start of the partition.
79 io_channel_set_blksize(fs->io, block_size);
80 group_block = superblock + 1;
82 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
86 retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
91 if ((fs->super->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) ||
92 (fs->flags & EXT2_SWAP_BYTES)) {
93 fs->flags |= EXT2_SWAP_BYTES;
95 ext2fs_swap_super(fs->super);
98 if (fs->super->s_magic != EXT2_SUPER_MAGIC) {
99 retval = EXT2_ET_BAD_MAGIC;
102 #ifdef EXT2_DYNAMIC_REV
103 if (fs->super->s_rev_level > EXT2_DYNAMIC_REV) {
104 retval = EXT2_ET_REV_TOO_HIGH;
108 #ifdef EXT2_CURRENT_REV
109 if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
110 retval = EXT2_ET_REV_TOO_HIGH;
115 fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
116 fs->fragsize = EXT2_FRAG_SIZE(fs->super);
117 fs->inode_blocks_per_group = ((fs->super->s_inodes_per_group *
118 EXT2_INODE_SIZE(fs->super) +
119 EXT2_BLOCK_SIZE(fs->super) - 1) /
120 EXT2_BLOCK_SIZE(fs->super));
122 if (block_size != fs->blocksize) {
123 retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
128 * Set the blocksize to the filesystem's blocksize.
130 io_channel_set_blksize(fs->io, fs->blocksize);
133 * Read group descriptors
135 fs->group_desc_count = (fs->super->s_blocks_count -
136 fs->super->s_first_data_block +
137 EXT2_BLOCKS_PER_GROUP(fs->super) - 1)
138 / EXT2_BLOCKS_PER_GROUP(fs->super);
139 fs->desc_blocks = (fs->group_desc_count +
140 EXT2_DESC_PER_BLOCK(fs->super) - 1)
141 / EXT2_DESC_PER_BLOCK(fs->super);
142 fs->group_desc = malloc(fs->desc_blocks * fs->blocksize);
143 if (!fs->group_desc) {
148 group_block = fs->super->s_first_data_block + 1;
149 dest = (char *) fs->group_desc;
150 for (i=0 ; i < fs->desc_blocks; i++) {
151 retval = io_channel_read_blk(fs->io, group_block, 1, dest);
155 if (fs->flags & EXT2_SWAP_BYTES) {
156 gdp = (struct ext2_group_desc *) dest;
157 groups_per_block = fs->blocksize /
158 sizeof(struct ext2_group_desc);
159 for (j=0; j < groups_per_block; j++)
160 ext2fs_swap_group_desc(gdp++);
162 dest += fs->blocksize;