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
|
|
|
|
#if HAVE_STDLIB_H
|
1997-04-26 17:21:57 +04:00
|
|
|
#include <stdlib.h>
|
1997-04-26 17:58:21 +04:00
|
|
|
#endif
|
1997-04-26 17:21:57 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <time.h>
|
1997-10-20 03:00:21 +04:00
|
|
|
#if HAVE_SYS_STAT_H
|
1997-04-26 17:21:57 +04:00
|
|
|
#include <sys/stat.h>
|
1997-10-20 03:00:21 +04:00
|
|
|
#endif
|
|
|
|
#if HAVE_SYS_TYPES_H
|
1997-04-26 17:21:57 +04:00
|
|
|
#include <sys/types.h>
|
1997-10-20 03:00:21 +04:00
|
|
|
#endif
|
1997-04-26 17:21:57 +04:00
|
|
|
|
|
|
|
#include <linux/ext2_fs.h>
|
|
|
|
|
|
|
|
#include "ext2fs.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
Many files:
dosio.c: New file to do DOS/BIOS disk accesses.
namei.c (open_namei): Make pathlen be of type size_t.
llseek.c: Always #include stdlib.h since it's need to define
size_t.
io.h: Use errcode_t for magic numbers.
icount.c (get_icount_el), dupfs.c (ext2fs_dup_handle), dblist.c
(dir_block_cmp): Use size_t where appropriate.
read_bb.c (ext2fs_read_bb_inode), cmp_bitmaps.c
(ext2fs_compare_inode_bitmap): Use blk_t, ino_t and size_t
where appropriate.
closefs.c (ext2fs_flush): Use dgrp_t instead of int where
appropriate.
openfs.c (ext2fs_open), check_desc.c (ext2fs_check_desc): Use blk_t
instead of int where appropriate.
rw_bitmaps.c (read_bitmaps), irel_ma.c, inode.c (ext2fs_write_inode),
initialize.c (ext2fs_initialize): brel_ma.c: Fix to make be
16-bit safe.
link.c (ext2fs_link), unlink.c (ext2fs_unlink), lookup.c (lookup_proc),
ismounted.c (ext2fs_check_if_mounted), block.c (xlate_func):
Add #pragma argsused for Turbo C.
1997-08-12 00:29:22 +04:00
|
|
|
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-29 20:17:09 +04:00
|
|
|
struct ext2fs_sb *s;
|
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);
|
|
|
|
|
1997-04-26 17:21:57 +04:00
|
|
|
fs = (ext2_filsys) malloc(sizeof(struct struct_ext2_filsys));
|
|
|
|
if (!fs)
|
1997-10-25 08:16:53 +04:00
|
|
|
return EXT2_NO_MEMORY;
|
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;
|
1997-08-24 06:57:55 +04:00
|
|
|
fs->io->app_data = fs;
|
1997-04-26 17:21:57 +04:00
|
|
|
fs->device_name = malloc(strlen(name)+1);
|
|
|
|
if (!fs->device_name) {
|
1997-10-25 08:16:53 +04:00
|
|
|
retval = EXT2_NO_MEMORY;
|
1997-04-26 17:21:57 +04:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
strcpy(fs->device_name, name);
|
|
|
|
fs->super = malloc(SUPERBLOCK_SIZE);
|
|
|
|
if (!fs->super) {
|
1997-10-25 08:16:53 +04:00
|
|
|
retval = EXT2_NO_MEMORY;
|
1997-04-26 17:21:57 +04:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
if (superblock) {
|
|
|
|
if (!block_size) {
|
1997-10-25 08:16:53 +04:00
|
|
|
retval = EXT2_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;
|
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;
|
1997-04-26 17:21:57 +04:00
|
|
|
}
|
|
|
|
retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
|
|
|
|
fs->super);
|
|
|
|
if (retval)
|
|
|
|
goto cleanup;
|
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 18:48:50 +04:00
|
|
|
#ifdef EXT2_DYNAMIC_REV
|
|
|
|
if (fs->super->s_rev_level > EXT2_DYNAMIC_REV) {
|
|
|
|
retval = EXT2_ET_REV_TOO_HIGH;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
#else
|
1997-04-26 17:34:30 +04:00
|
|
|
#ifdef EXT2_CURRENT_REV
|
|
|
|
if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
|
|
|
|
retval = EXT2_ET_REV_TOO_HIGH;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
1997-04-26 18:48:50 +04:00
|
|
|
#endif
|
1997-04-26 17:34:30 +04:00
|
|
|
#endif
|
1997-04-29 20:17:09 +04:00
|
|
|
/*
|
|
|
|
* Check for feature set incompatibility
|
|
|
|
*/
|
|
|
|
if (!(flags & EXT2_FLAG_FORCE)) {
|
|
|
|
s = (struct ext2fs_sb *) fs->super;
|
1997-04-29 21:48:10 +04:00
|
|
|
if (s->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) &&
|
1997-04-29 21:48:10 +04:00
|
|
|
(s->s_feature_ro_compat &
|
|
|
|
~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
|
1997-04-29 20:17:09 +04:00
|
|
|
retval = EXT2_ET_RO_UNSUPP_FEATURE;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read group descriptors
|
|
|
|
*/
|
|
|
|
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);
|
Many files:
dosio.c: New file to do DOS/BIOS disk accesses.
namei.c (open_namei): Make pathlen be of type size_t.
llseek.c: Always #include stdlib.h since it's need to define
size_t.
io.h: Use errcode_t for magic numbers.
icount.c (get_icount_el), dupfs.c (ext2fs_dup_handle), dblist.c
(dir_block_cmp): Use size_t where appropriate.
read_bb.c (ext2fs_read_bb_inode), cmp_bitmaps.c
(ext2fs_compare_inode_bitmap): Use blk_t, ino_t and size_t
where appropriate.
closefs.c (ext2fs_flush): Use dgrp_t instead of int where
appropriate.
openfs.c (ext2fs_open), check_desc.c (ext2fs_check_desc): Use blk_t
instead of int where appropriate.
rw_bitmaps.c (read_bitmaps), irel_ma.c, inode.c (ext2fs_write_inode),
initialize.c (ext2fs_initialize): brel_ma.c: Fix to make be
16-bit safe.
link.c (ext2fs_link), unlink.c (ext2fs_unlink), lookup.c (lookup_proc),
ismounted.c (ext2fs_check_if_mounted), block.c (xlate_func):
Add #pragma argsused for Turbo C.
1997-08-12 00:29:22 +04:00
|
|
|
fs->group_desc = malloc((size_t) (fs->desc_blocks * fs->blocksize));
|
1997-04-26 17:21:57 +04:00
|
|
|
if (!fs->group_desc) {
|
1997-10-25 08:16:53 +04:00
|
|
|
retval = EXT2_NO_MEMORY;
|
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;
|
|
|
|
}
|
|
|
|
|