e2fsprogs/lib/ext2fs/openfs.c

240 lines
6.1 KiB
C
Raw Normal View History

1997-04-26 17:21:57 +04:00
/*
* openfs.c --- open an ext2 filesystem
*
1997-04-29 20:17:09 +04:00
* Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
1997-04-26 17:58:21 +04:00
*
1997-04-29 20:17:09 +04:00
* %Begin-Header%
1997-04-26 17:58:21 +04:00
* This file may be redistributed under the terms of the GNU Public
* License.
1997-04-29 20:17:09 +04:00
* %End-Header%
1997-04-26 17:21:57 +04:00
*/
#include <stdio.h>
#include <string.h>
1997-04-26 17:58:21 +04:00
#if HAVE_UNISTD_H
1997-04-26 17:21:57 +04:00
#include <unistd.h>
1997-04-26 17:58:21 +04:00
#endif
1997-04-26 17:21:57 +04:00
#include <fcntl.h>
#include <time.h>
#if HAVE_SYS_STAT_H
1997-04-26 17:21:57 +04:00
#include <sys/stat.h>
#endif
#if HAVE_SYS_TYPES_H
1997-04-26 17:21:57 +04:00
#include <sys/types.h>
#endif
1997-04-26 17:21:57 +04:00
Many files: inode.c (ext2fs_open_inode_scan): Initialize the group variables so that we don't need to call get_next_blockgroup() the first time around. Saves a bit of time, and prevents us from needing to assign -1 to current_group (which is an unsigned value). icount.c (insert_icount_el): Cast the estimated number of inodes from a float to an ino_t. alloc.c, alloc_tables.c, badlbocks.c, bb_compat.c, bb_inode.c, bitmaps.c, bitops.c, block.c, bmap.c, bmove.c, brel_ma.c, check_desc.c, closefs.c, cmp_bitmaps.c, dblist.c, dblist_dir.c, dir_iterate.c, dirblock.c, dupfs.c, expanddir.c, ext2fs.h, fileio.c, freefs.c, get_pathname.c, getsize.c, icount.c, initialize.c, inline.c, inode.c, irel_ma.c, ismounted.c, link.c, lookup.c, mkdir.c, namei.c, native.c, newdir.c, openfs.c, read_bb.c, read_bb_file.c, rs_bitmap.c, rw_bitmaps.c, swapfs.c, test_io.c, tst_badblocks.c, tst_getsize.c, tst_iscan.c, unix_io.c, unlink.c, valid_blk.c, version.c: If EXT2_FLAT_INCLUDES is defined, then assume all of the ext2-specific header files are in a flat directory. block.c, bmove.c, dirblock.c, fileio.c: Explicitly cast all assignments from void * to be compatible with C++. closefs.c (ext2fs_flush): Add a call to io_channel_flush() to make sure the contents of the disk are flushed to disk. dblist.c (ext2fs_add_dir_block): Change new to be new_entry to avoid C++ namespace clash. bitmaps.c (ext2fs_copy_bitmap): Change new to be new_map to avoid C++ namespace clash. ext2fs.h, bb_inode.c, block.c, bmove.c, brel.h, brel_ma.c, irel.h, irel_ma.c, dblist.c, dblist_dir.c, dir_iterate.c, ext2fsP.h, expanddir.c, get_pathname.c, inode.c, link.c, unlink.c: Change private to be priv_data (to avoid C++ namespace clash)
1998-01-19 17:47:53 +03:00
#if EXT2_FLAT_INCLUDES
#include "ext2_fs.h"
#else
1997-04-26 17:21:57 +04:00
#include <linux/ext2_fs.h>
Many files: inode.c (ext2fs_open_inode_scan): Initialize the group variables so that we don't need to call get_next_blockgroup() the first time around. Saves a bit of time, and prevents us from needing to assign -1 to current_group (which is an unsigned value). icount.c (insert_icount_el): Cast the estimated number of inodes from a float to an ino_t. alloc.c, alloc_tables.c, badlbocks.c, bb_compat.c, bb_inode.c, bitmaps.c, bitops.c, block.c, bmap.c, bmove.c, brel_ma.c, check_desc.c, closefs.c, cmp_bitmaps.c, dblist.c, dblist_dir.c, dir_iterate.c, dirblock.c, dupfs.c, expanddir.c, ext2fs.h, fileio.c, freefs.c, get_pathname.c, getsize.c, icount.c, initialize.c, inline.c, inode.c, irel_ma.c, ismounted.c, link.c, lookup.c, mkdir.c, namei.c, native.c, newdir.c, openfs.c, read_bb.c, read_bb_file.c, rs_bitmap.c, rw_bitmaps.c, swapfs.c, test_io.c, tst_badblocks.c, tst_getsize.c, tst_iscan.c, unix_io.c, unlink.c, valid_blk.c, version.c: If EXT2_FLAT_INCLUDES is defined, then assume all of the ext2-specific header files are in a flat directory. block.c, bmove.c, dirblock.c, fileio.c: Explicitly cast all assignments from void * to be compatible with C++. closefs.c (ext2fs_flush): Add a call to io_channel_flush() to make sure the contents of the disk are flushed to disk. dblist.c (ext2fs_add_dir_block): Change new to be new_entry to avoid C++ namespace clash. bitmaps.c (ext2fs_copy_bitmap): Change new to be new_map to avoid C++ namespace clash. ext2fs.h, bb_inode.c, block.c, bmove.c, brel.h, brel_ma.c, irel.h, irel_ma.c, dblist.c, dblist_dir.c, dir_iterate.c, ext2fsP.h, expanddir.c, get_pathname.c, inode.c, link.c, unlink.c: Change private to be priv_data (to avoid C++ namespace clash)
1998-01-19 17:47:53 +03:00
#endif
1997-04-26 17:21:57 +04:00
#include "ext2fs.h"
#include "e2image.h"
1997-04-26 17:21:57 +04:00
/*
* Note: if superblock is non-zero, block-size must also be non-zero.
* Superblock and block_size can be zero to use the default size.
1997-04-29 20:17:09 +04:00
*
* Valid flags for ext2fs_open()
*
* EXT2_FLAG_RW - Open the filesystem for read/write.
* EXT2_FLAG_FORCE - Open the filesystem even if some of the
* features aren't supported.
1997-04-26 17:21:57 +04:00
*/
errcode_t ext2fs_open(const char *name, int flags, int superblock,
int block_size, io_manager manager, ext2_filsys *ret_fs)
{
ext2_filsys fs;
errcode_t retval;
int i, j, groups_per_block;
blk_t group_block;
1997-04-26 17:21:57 +04:00
char *dest;
1997-04-26 17:58:21 +04:00
struct ext2_group_desc *gdp;
1997-04-26 17:21:57 +04:00
1997-04-26 17:34:30 +04:00
EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);
retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys),
(void **) &fs);
if (retval)
return retval;
1997-04-26 17:21:57 +04:00
memset(fs, 0, sizeof(struct struct_ext2_filsys));
1997-04-26 17:34:30 +04:00
fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
1997-04-26 17:21:57 +04:00
fs->flags = flags;
retval = manager->open(name, (flags & EXT2_FLAG_RW) ? IO_FLAG_RW : 0,
&fs->io);
if (retval)
goto cleanup;
fs->io->app_data = fs;
retval = ext2fs_get_mem(strlen(name)+1, (void **) &fs->device_name);
if (retval)
1997-04-26 17:21:57 +04:00
goto cleanup;
strcpy(fs->device_name, name);
retval = ext2fs_get_mem(SUPERBLOCK_SIZE, (void **) &fs->super);
if (retval)
1997-04-26 17:21:57 +04:00
goto cleanup;
if (flags & EXT2_FLAG_IMAGE_FILE) {
retval = ext2fs_get_mem(sizeof(struct ext2_image_hdr),
(void **) &fs->image_header);
if (retval)
goto cleanup;
retval = io_channel_read_blk(fs->io, 0,
-sizeof(struct ext2_image_hdr),
fs->image_header);
if (retval)
goto cleanup;
if (fs->image_header->magic_number != EXT2_ET_MAGIC_E2IMAGE)
return EXT2_ET_MAGIC_E2IMAGE;
superblock = 1;
block_size = fs->image_header->fs_blocksize;
}
1997-04-26 17:21:57 +04:00
/*
* If the user specifies a specific block # for the
* superblock, then he/she must also specify the block size!
* Otherwise, read the master superblock located at offset
* SUPERBLOCK_OFFSET from the start of the partition.
*
* Note: we only save a backup copy of the superblock if we
* are reading the superblock from the primary superblock location.
1997-04-26 17:21:57 +04:00
*/
if (superblock) {
if (!block_size) {
retval = EXT2_ET_INVALID_ARGUMENT;
1997-04-26 17:21:57 +04:00
goto cleanup;
}
io_channel_set_blksize(fs->io, block_size);
1997-04-26 17:34:30 +04:00
group_block = superblock + 1;
fs->orig_super = 0;
1997-04-26 17:21:57 +04:00
} else {
io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
superblock = 1;
1997-04-26 17:34:30 +04:00
group_block = 0;
retval = ext2fs_get_mem(SUPERBLOCK_SIZE,
(void **) &fs->orig_super);
if (retval)
goto cleanup;
1997-04-26 17:21:57 +04:00
}
retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
fs->super);
if (retval)
goto cleanup;
if (fs->orig_super)
memcpy(fs->orig_super, fs->super, SUPERBLOCK_SIZE);
1997-04-26 17:58:21 +04:00
if ((fs->super->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) ||
1997-04-29 19:29:49 +04:00
(fs->flags & EXT2_FLAG_SWAP_BYTES)) {
fs->flags |= EXT2_FLAG_SWAP_BYTES;
1997-04-26 17:58:21 +04:00
ext2fs_swap_super(fs->super);
}
1997-04-26 17:21:57 +04:00
if (fs->super->s_magic != EXT2_SUPER_MAGIC) {
retval = EXT2_ET_BAD_MAGIC;
goto cleanup;
}
1997-04-26 17:34:30 +04:00
if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
retval = EXT2_ET_REV_TOO_HIGH;
goto cleanup;
}
1997-04-29 20:17:09 +04:00
/*
* Check for feature set incompatibility
*/
if (!(flags & EXT2_FLAG_FORCE)) {
if (fs->super->s_feature_incompat &
~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
1997-04-29 20:17:09 +04:00
retval = EXT2_ET_UNSUPP_FEATURE;
goto cleanup;
}
if ((flags & EXT2_FLAG_RW) &&
(fs->super->s_feature_ro_compat &
1997-04-29 21:48:10 +04:00
~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
1997-04-29 20:17:09 +04:00
retval = EXT2_ET_RO_UNSUPP_FEATURE;
goto cleanup;
}
if (!(flags & EXT2_FLAG_JOURNAL_DEV_OK) &&
(fs->super->s_feature_incompat &
EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
retval = EXT2_ET_UNSUPP_FEATURE;
goto cleanup;
}
1997-04-29 20:17:09 +04:00
}
1997-04-26 17:21:57 +04:00
fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
1997-04-29 18:53:37 +04:00
if (fs->blocksize == 0) {
retval = EXT2_ET_CORRUPT_SUPERBLOCK;
goto cleanup;
}
1997-04-26 17:21:57 +04:00
fs->fragsize = EXT2_FRAG_SIZE(fs->super);
1997-04-26 18:48:50 +04:00
fs->inode_blocks_per_group = ((fs->super->s_inodes_per_group *
EXT2_INODE_SIZE(fs->super) +
EXT2_BLOCK_SIZE(fs->super) - 1) /
EXT2_BLOCK_SIZE(fs->super));
1997-04-26 17:21:57 +04:00
if (block_size) {
if (block_size != fs->blocksize) {
retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
goto cleanup;
}
}
/*
* Set the blocksize to the filesystem's blocksize.
*/
io_channel_set_blksize(fs->io, fs->blocksize);
/*
* If this is an external journal device, don't try to read
* the group descriptors, because they're not there.
*/
if (fs->super->s_feature_incompat &
EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
fs->group_desc_count = 0;
*ret_fs = fs;
return 0;
}
1997-04-26 17:21:57 +04:00
/*
* Read group descriptors
*/
if ((EXT2_BLOCKS_PER_GROUP(fs->super)) == 0) {
retval = EXT2_ET_CORRUPT_SUPERBLOCK;
goto cleanup;
}
1997-04-26 17:21:57 +04:00
fs->group_desc_count = (fs->super->s_blocks_count -
fs->super->s_first_data_block +
EXT2_BLOCKS_PER_GROUP(fs->super) - 1)
/ EXT2_BLOCKS_PER_GROUP(fs->super);
fs->desc_blocks = (fs->group_desc_count +
EXT2_DESC_PER_BLOCK(fs->super) - 1)
/ EXT2_DESC_PER_BLOCK(fs->super);
retval = ext2fs_get_mem(fs->desc_blocks * fs->blocksize,
(void **) &fs->group_desc);
if (retval)
1997-04-26 17:21:57 +04:00
goto cleanup;
1997-04-26 17:34:30 +04:00
if (!group_block)
group_block = fs->super->s_first_data_block + 1;
1997-04-26 17:21:57 +04:00
dest = (char *) fs->group_desc;
for (i=0 ; i < fs->desc_blocks; i++) {
retval = io_channel_read_blk(fs->io, group_block, 1, dest);
if (retval)
goto cleanup;
group_block++;
1997-04-29 19:29:49 +04:00
if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
1997-04-26 17:58:21 +04:00
gdp = (struct ext2_group_desc *) dest;
groups_per_block = fs->blocksize /
sizeof(struct ext2_group_desc);
for (j=0; j < groups_per_block; j++)
ext2fs_swap_group_desc(gdp++);
}
1997-04-26 17:21:57 +04:00
dest += fs->blocksize;
}
*ret_fs = fs;
return 0;
cleanup:
ext2fs_free(fs);
return retval;
}