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_CURRENT_REV
103 if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
104 retval = EXT2_ET_REV_TOO_HIGH;
108 fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
109 fs->fragsize = EXT2_FRAG_SIZE(fs->super);
110 fs->inode_blocks_per_group = (fs->super->s_inodes_per_group /
111 EXT2_INODES_PER_BLOCK(fs->super));
113 if (block_size != fs->blocksize) {
114 retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
119 * Set the blocksize to the filesystem's blocksize.
121 io_channel_set_blksize(fs->io, fs->blocksize);
124 * Read group descriptors
126 fs->group_desc_count = (fs->super->s_blocks_count -
127 fs->super->s_first_data_block +
128 EXT2_BLOCKS_PER_GROUP(fs->super) - 1)
129 / EXT2_BLOCKS_PER_GROUP(fs->super);
130 fs->desc_blocks = (fs->group_desc_count +
131 EXT2_DESC_PER_BLOCK(fs->super) - 1)
132 / EXT2_DESC_PER_BLOCK(fs->super);
133 fs->group_desc = malloc(fs->desc_blocks * fs->blocksize);
134 if (!fs->group_desc) {
139 group_block = fs->super->s_first_data_block + 1;
140 dest = (char *) fs->group_desc;
141 for (i=0 ; i < fs->desc_blocks; i++) {
142 retval = io_channel_read_blk(fs->io, group_block, 1, dest);
146 if (fs->flags & EXT2_SWAP_BYTES) {
147 gdp = (struct ext2_group_desc *) dest;
148 groups_per_block = fs->blocksize /
149 sizeof(struct ext2_group_desc);
150 for (j=0; j < groups_per_block; j++)
151 ext2fs_swap_group_desc(gdp++);
153 dest += fs->blocksize;