| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * openfs.c --- open an ext2 filesystem |
| 3 | * |
| Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 4 | * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o. |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 5 | * |
| Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 6 | * %Begin-Header% |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 7 | * This file may be redistributed under the terms of the GNU Public |
| 8 | * License. |
| Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 9 | * %End-Header% |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <string.h> |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 14 | #if HAVE_UNISTD_H |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 15 | #include <unistd.h> |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 16 | #endif |
| 17 | #if HAVE_STDLIB_H |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 18 | #include <stdlib.h> |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 19 | #endif |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 20 | #include <fcntl.h> |
| 21 | #include <time.h> |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 22 | #if HAVE_ERRNO_H |
| 23 | #include <errno.h> |
| 24 | #endif |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 25 | #include <sys/stat.h> |
| 26 | #include <sys/types.h> |
| 27 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 28 | #include <linux/ext2_fs.h> |
| 29 | |
| 30 | #include "ext2fs.h" |
| 31 | |
| 32 | /* |
| 33 | * Note: if superblock is non-zero, block-size must also be non-zero. |
| 34 | * Superblock and block_size can be zero to use the default size. |
| Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 35 | * |
| 36 | * Valid flags for ext2fs_open() |
| 37 | * |
| 38 | * EXT2_FLAG_RW - Open the filesystem for read/write. |
| 39 | * EXT2_FLAG_FORCE - Open the filesystem even if some of the |
| 40 | * features aren't supported. |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 41 | */ |
| 42 | errcode_t ext2fs_open(const char *name, int flags, int superblock, |
| 43 | int block_size, io_manager manager, ext2_filsys *ret_fs) |
| 44 | { |
| 45 | ext2_filsys fs; |
| 46 | errcode_t retval; |
| Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 47 | int i, j, groups_per_block; |
| 48 | blk_t group_block; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 49 | char *dest; |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 50 | struct ext2_group_desc *gdp; |
| Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 51 | struct ext2fs_sb *s; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 52 | |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 53 | EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER); |
| 54 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 55 | fs = (ext2_filsys) malloc(sizeof(struct struct_ext2_filsys)); |
| 56 | if (!fs) |
| 57 | return ENOMEM; |
| 58 | |
| 59 | memset(fs, 0, sizeof(struct struct_ext2_filsys)); |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 60 | fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 61 | fs->flags = flags; |
| 62 | retval = manager->open(name, (flags & EXT2_FLAG_RW) ? IO_FLAG_RW : 0, |
| 63 | &fs->io); |
| 64 | if (retval) |
| 65 | goto cleanup; |
| Theodore Ts'o | e9affb7 | 1997-08-24 02:57:55 +0000 | [diff] [blame^] | 66 | fs->io->app_data = fs; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 67 | fs->device_name = malloc(strlen(name)+1); |
| 68 | if (!fs->device_name) { |
| 69 | retval = ENOMEM; |
| 70 | goto cleanup; |
| 71 | } |
| 72 | strcpy(fs->device_name, name); |
| 73 | fs->super = malloc(SUPERBLOCK_SIZE); |
| 74 | if (!fs->super) { |
| 75 | retval = ENOMEM; |
| 76 | goto cleanup; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * If the user specifies a specific block # for the |
| 81 | * superblock, then he/she must also specify the block size! |
| 82 | * Otherwise, read the master superblock located at offset |
| 83 | * SUPERBLOCK_OFFSET from the start of the partition. |
| 84 | */ |
| 85 | if (superblock) { |
| 86 | if (!block_size) { |
| 87 | retval = EINVAL; |
| 88 | goto cleanup; |
| 89 | } |
| 90 | io_channel_set_blksize(fs->io, block_size); |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 91 | group_block = superblock + 1; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 92 | } else { |
| 93 | io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET); |
| 94 | superblock = 1; |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 95 | group_block = 0; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 96 | } |
| 97 | retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE, |
| 98 | fs->super); |
| 99 | if (retval) |
| 100 | goto cleanup; |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 101 | |
| 102 | if ((fs->super->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) || |
| Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 103 | (fs->flags & EXT2_FLAG_SWAP_BYTES)) { |
| 104 | fs->flags |= EXT2_FLAG_SWAP_BYTES; |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 105 | |
| 106 | ext2fs_swap_super(fs->super); |
| 107 | } |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 108 | |
| 109 | if (fs->super->s_magic != EXT2_SUPER_MAGIC) { |
| 110 | retval = EXT2_ET_BAD_MAGIC; |
| 111 | goto cleanup; |
| 112 | } |
| Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 113 | #ifdef EXT2_DYNAMIC_REV |
| 114 | if (fs->super->s_rev_level > EXT2_DYNAMIC_REV) { |
| 115 | retval = EXT2_ET_REV_TOO_HIGH; |
| 116 | goto cleanup; |
| 117 | } |
| 118 | #else |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 119 | #ifdef EXT2_CURRENT_REV |
| 120 | if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) { |
| 121 | retval = EXT2_ET_REV_TOO_HIGH; |
| 122 | goto cleanup; |
| 123 | } |
| 124 | #endif |
| Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 125 | #endif |
| Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 126 | /* |
| 127 | * Check for feature set incompatibility |
| 128 | */ |
| 129 | if (!(flags & EXT2_FLAG_FORCE)) { |
| 130 | s = (struct ext2fs_sb *) fs->super; |
| Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 131 | if (s->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) { |
| Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 132 | retval = EXT2_ET_UNSUPP_FEATURE; |
| 133 | goto cleanup; |
| 134 | } |
| 135 | if ((flags & EXT2_FLAG_RW) && |
| Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 136 | (s->s_feature_ro_compat & |
| 137 | ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) { |
| Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 138 | retval = EXT2_ET_RO_UNSUPP_FEATURE; |
| 139 | goto cleanup; |
| 140 | } |
| 141 | } |
| 142 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 143 | fs->blocksize = EXT2_BLOCK_SIZE(fs->super); |
| Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 144 | if (fs->blocksize == 0) { |
| 145 | retval = EXT2_ET_CORRUPT_SUPERBLOCK; |
| 146 | goto cleanup; |
| 147 | } |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 148 | fs->fragsize = EXT2_FRAG_SIZE(fs->super); |
| Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 149 | fs->inode_blocks_per_group = ((fs->super->s_inodes_per_group * |
| 150 | EXT2_INODE_SIZE(fs->super) + |
| 151 | EXT2_BLOCK_SIZE(fs->super) - 1) / |
| 152 | EXT2_BLOCK_SIZE(fs->super)); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 153 | if (block_size) { |
| 154 | if (block_size != fs->blocksize) { |
| 155 | retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE; |
| 156 | goto cleanup; |
| 157 | } |
| 158 | } |
| 159 | /* |
| 160 | * Set the blocksize to the filesystem's blocksize. |
| 161 | */ |
| 162 | io_channel_set_blksize(fs->io, fs->blocksize); |
| 163 | |
| 164 | /* |
| 165 | * Read group descriptors |
| 166 | */ |
| 167 | fs->group_desc_count = (fs->super->s_blocks_count - |
| 168 | fs->super->s_first_data_block + |
| 169 | EXT2_BLOCKS_PER_GROUP(fs->super) - 1) |
| 170 | / EXT2_BLOCKS_PER_GROUP(fs->super); |
| 171 | fs->desc_blocks = (fs->group_desc_count + |
| 172 | EXT2_DESC_PER_BLOCK(fs->super) - 1) |
| 173 | / EXT2_DESC_PER_BLOCK(fs->super); |
| Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 174 | fs->group_desc = malloc((size_t) (fs->desc_blocks * fs->blocksize)); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 175 | if (!fs->group_desc) { |
| 176 | retval = ENOMEM; |
| 177 | goto cleanup; |
| 178 | } |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 179 | if (!group_block) |
| 180 | group_block = fs->super->s_first_data_block + 1; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 181 | dest = (char *) fs->group_desc; |
| 182 | for (i=0 ; i < fs->desc_blocks; i++) { |
| 183 | retval = io_channel_read_blk(fs->io, group_block, 1, dest); |
| 184 | if (retval) |
| 185 | goto cleanup; |
| 186 | group_block++; |
| Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 187 | if (fs->flags & EXT2_FLAG_SWAP_BYTES) { |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 188 | gdp = (struct ext2_group_desc *) dest; |
| 189 | groups_per_block = fs->blocksize / |
| 190 | sizeof(struct ext2_group_desc); |
| 191 | for (j=0; j < groups_per_block; j++) |
| 192 | ext2fs_swap_group_desc(gdp++); |
| 193 | } |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 194 | dest += fs->blocksize; |
| 195 | } |
| 196 | |
| 197 | *ret_fs = fs; |
| 198 | return 0; |
| 199 | cleanup: |
| 200 | ext2fs_free(fs); |
| 201 | return retval; |
| 202 | } |
| 203 | |