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