| 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 | #include "ext2_fs.h" |
| Theodore Ts'o | c046ac7 | 2002-10-20 00:38:57 -0400 | [diff] [blame] | 27 | |
| 28 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 29 | #include "ext2fs.h" |
| Theodore Ts'o | a78926e | 2001-05-03 04:02:29 +0000 | [diff] [blame] | 30 | #include "e2image.h" |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 31 | |
| Theodore Ts'o | c046ac7 | 2002-10-20 00:38:57 -0400 | [diff] [blame] | 32 | blk_t ext2fs_descriptor_block_loc(ext2_filsys fs, blk_t group_block, dgrp_t i) |
| 33 | { |
| 34 | int bg; |
| 35 | int has_super = 0; |
| Theodore Ts'o | 9ed06a1 | 2002-10-31 11:45:06 -0500 | [diff] [blame] | 36 | int ret_blk; |
| Theodore Ts'o | c046ac7 | 2002-10-20 00:38:57 -0400 | [diff] [blame] | 37 | |
| 38 | if (!(fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) || |
| 39 | (i < fs->super->s_first_meta_bg)) |
| 40 | return (group_block + i + 1); |
| 41 | |
| Valerie Clement | f2de1d3 | 2007-08-30 17:38:13 +0200 | [diff] [blame^] | 42 | bg = EXT2_DESC_PER_BLOCK(fs->super) * i; |
| Theodore Ts'o | c046ac7 | 2002-10-20 00:38:57 -0400 | [diff] [blame] | 43 | if (ext2fs_bg_has_super(fs, bg)) |
| 44 | has_super = 1; |
| Valerie Clement | 3bfca9a | 2007-08-30 17:31:36 +0200 | [diff] [blame] | 45 | ret_blk = ext2fs_group_first_block(fs, bg) + has_super; |
| Theodore Ts'o | 9ed06a1 | 2002-10-31 11:45:06 -0500 | [diff] [blame] | 46 | /* |
| 47 | * If group_block is not the normal value, we're trying to use |
| 48 | * the backup group descriptors and superblock --- so use the |
| 49 | * alternate location of the second block group in the |
| 50 | * metablock group. Ideally we should be testing each bg |
| 51 | * descriptor block individually for correctness, but we don't |
| 52 | * have the infrastructure in place to do that. |
| 53 | */ |
| 54 | if (group_block != fs->super->s_first_data_block && |
| 55 | ((ret_blk + fs->super->s_blocks_per_group) < |
| 56 | fs->super->s_blocks_count)) |
| 57 | ret_blk += fs->super->s_blocks_per_group; |
| 58 | return ret_blk; |
| Theodore Ts'o | c046ac7 | 2002-10-20 00:38:57 -0400 | [diff] [blame] | 59 | } |
| 60 | |
| Theodore Ts'o | 2e8ca9a | 2004-11-30 14:07:11 -0500 | [diff] [blame] | 61 | errcode_t ext2fs_open(const char *name, int flags, int superblock, |
| 62 | unsigned int block_size, io_manager manager, |
| 63 | ext2_filsys *ret_fs) |
| 64 | { |
| 65 | return ext2fs_open2(name, 0, flags, superblock, block_size, |
| 66 | manager, ret_fs); |
| 67 | } |
| 68 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 69 | /* |
| 70 | * Note: if superblock is non-zero, block-size must also be non-zero. |
| 71 | * 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] | 72 | * |
| 73 | * Valid flags for ext2fs_open() |
| 74 | * |
| 75 | * EXT2_FLAG_RW - Open the filesystem for read/write. |
| 76 | * EXT2_FLAG_FORCE - Open the filesystem even if some of the |
| Theodore Ts'o | a777397 | 2001-05-13 23:12:10 +0000 | [diff] [blame] | 77 | * features aren't supported. |
| 78 | * EXT2_FLAG_JOURNAL_DEV_OK - Open an ext3 journal device |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 79 | */ |
| Theodore Ts'o | 2e8ca9a | 2004-11-30 14:07:11 -0500 | [diff] [blame] | 80 | errcode_t ext2fs_open2(const char *name, const char *io_options, |
| 81 | int flags, int superblock, |
| 82 | unsigned int block_size, io_manager manager, |
| 83 | ext2_filsys *ret_fs) |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 84 | { |
| 85 | ext2_filsys fs; |
| 86 | errcode_t retval; |
| Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 87 | unsigned long i; |
| Theodore Ts'o | cf8272e | 2006-11-12 23:26:46 -0500 | [diff] [blame] | 88 | __u32 features; |
| Theodore Ts'o | 39c47ce | 2006-03-18 19:16:10 -0500 | [diff] [blame] | 89 | int j, groups_per_block, blocks_per_group, io_flags; |
| Theodore Ts'o | c046ac7 | 2002-10-20 00:38:57 -0400 | [diff] [blame] | 90 | blk_t group_block, blk; |
| Theodore Ts'o | 2e8ca9a | 2004-11-30 14:07:11 -0500 | [diff] [blame] | 91 | char *dest, *cp; |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 92 | struct ext2_group_desc *gdp; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 93 | |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 94 | EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER); |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 95 | |
| Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 96 | retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs); |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 97 | if (retval) |
| 98 | return retval; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 99 | |
| 100 | memset(fs, 0, sizeof(struct struct_ext2_filsys)); |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 101 | fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 102 | fs->flags = flags; |
| Theodore Ts'o | 058ad1c | 2007-06-18 18:26:50 -0400 | [diff] [blame] | 103 | /* don't overwrite sb backups unless flag is explicitly cleared */ |
| 104 | fs->flags |= EXT2_FLAG_MASTER_SB_ONLY; |
| Theodore Ts'o | 6a52506 | 2001-12-24 09:40:00 -0500 | [diff] [blame] | 105 | fs->umask = 022; |
| Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 106 | retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name); |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 107 | if (retval) |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 108 | goto cleanup; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 109 | strcpy(fs->device_name, name); |
| Theodore Ts'o | 2e8ca9a | 2004-11-30 14:07:11 -0500 | [diff] [blame] | 110 | cp = strchr(fs->device_name, '?'); |
| 111 | if (!io_options && cp) { |
| 112 | *cp++ = 0; |
| 113 | io_options = cp; |
| 114 | } |
| 115 | |
| Theodore Ts'o | 39c47ce | 2006-03-18 19:16:10 -0500 | [diff] [blame] | 116 | io_flags = 0; |
| 117 | if (flags & EXT2_FLAG_RW) |
| 118 | io_flags |= IO_FLAG_RW; |
| 119 | if (flags & EXT2_FLAG_EXCLUSIVE) |
| 120 | io_flags |= IO_FLAG_EXCLUSIVE; |
| 121 | retval = manager->open(fs->device_name, io_flags, &fs->io); |
| Theodore Ts'o | 2e8ca9a | 2004-11-30 14:07:11 -0500 | [diff] [blame] | 122 | if (retval) |
| 123 | goto cleanup; |
| 124 | if (io_options && |
| 125 | (retval = io_channel_set_options(fs->io, io_options))) |
| 126 | goto cleanup; |
| 127 | fs->image_io = fs->io; |
| 128 | fs->io->app_data = fs; |
| Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 129 | retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->super); |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 130 | if (retval) |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 131 | goto cleanup; |
| Theodore Ts'o | a78926e | 2001-05-03 04:02:29 +0000 | [diff] [blame] | 132 | if (flags & EXT2_FLAG_IMAGE_FILE) { |
| 133 | retval = ext2fs_get_mem(sizeof(struct ext2_image_hdr), |
| Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 134 | &fs->image_header); |
| Theodore Ts'o | a78926e | 2001-05-03 04:02:29 +0000 | [diff] [blame] | 135 | if (retval) |
| 136 | goto cleanup; |
| 137 | retval = io_channel_read_blk(fs->io, 0, |
| Theodore Ts'o | 85f93ff | 2006-05-21 19:26:45 -0400 | [diff] [blame] | 138 | -(int)sizeof(struct ext2_image_hdr), |
| Theodore Ts'o | a78926e | 2001-05-03 04:02:29 +0000 | [diff] [blame] | 139 | fs->image_header); |
| 140 | if (retval) |
| 141 | goto cleanup; |
| 142 | if (fs->image_header->magic_number != EXT2_ET_MAGIC_E2IMAGE) |
| 143 | return EXT2_ET_MAGIC_E2IMAGE; |
| 144 | superblock = 1; |
| 145 | block_size = fs->image_header->fs_blocksize; |
| 146 | } |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 147 | |
| 148 | /* |
| 149 | * If the user specifies a specific block # for the |
| 150 | * superblock, then he/she must also specify the block size! |
| 151 | * Otherwise, read the master superblock located at offset |
| 152 | * SUPERBLOCK_OFFSET from the start of the partition. |
| Theodore Ts'o | c180ac8 | 2000-10-26 20:24:43 +0000 | [diff] [blame] | 153 | * |
| 154 | * Note: we only save a backup copy of the superblock if we |
| 155 | * are reading the superblock from the primary superblock location. |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 156 | */ |
| 157 | if (superblock) { |
| 158 | if (!block_size) { |
| Theodore Ts'o | 1f0b6c1 | 1997-10-31 06:07:47 +0000 | [diff] [blame] | 159 | retval = EXT2_ET_INVALID_ARGUMENT; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 160 | goto cleanup; |
| 161 | } |
| 162 | io_channel_set_blksize(fs->io, block_size); |
| Theodore Ts'o | 9ed06a1 | 2002-10-31 11:45:06 -0500 | [diff] [blame] | 163 | group_block = superblock; |
| Theodore Ts'o | c180ac8 | 2000-10-26 20:24:43 +0000 | [diff] [blame] | 164 | fs->orig_super = 0; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 165 | } else { |
| 166 | io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET); |
| 167 | superblock = 1; |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 168 | group_block = 0; |
| Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 169 | retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->orig_super); |
| Theodore Ts'o | c180ac8 | 2000-10-26 20:24:43 +0000 | [diff] [blame] | 170 | if (retval) |
| 171 | goto cleanup; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 172 | } |
| 173 | retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE, |
| 174 | fs->super); |
| 175 | if (retval) |
| 176 | goto cleanup; |
| Theodore Ts'o | c180ac8 | 2000-10-26 20:24:43 +0000 | [diff] [blame] | 177 | if (fs->orig_super) |
| 178 | memcpy(fs->orig_super, fs->super, SUPERBLOCK_SIZE); |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 179 | |
| Theodore Ts'o | 126a291 | 2007-08-11 01:56:48 -0400 | [diff] [blame] | 180 | #ifdef WORDS_BIGENDIAN |
| 181 | fs->flags |= EXT2_FLAG_SWAP_BYTES; |
| 182 | ext2fs_swap_super(fs->super); |
| 183 | #else |
| 184 | if (fs->flags & EXT2_FLAG_SWAP_BYTES) { |
| 185 | retval = EXT2_ET_UNIMPLEMENTED; |
| 186 | goto cleanup; |
| Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 187 | } |
| Theodore Ts'o | 5df55d7 | 2001-06-11 07:00:04 +0000 | [diff] [blame] | 188 | #endif |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 189 | |
| 190 | if (fs->super->s_magic != EXT2_SUPER_MAGIC) { |
| 191 | retval = EXT2_ET_BAD_MAGIC; |
| 192 | goto cleanup; |
| 193 | } |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 194 | if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) { |
| 195 | retval = EXT2_ET_REV_TOO_HIGH; |
| 196 | goto cleanup; |
| 197 | } |
| Theodore Ts'o | e5b38a5 | 2001-01-01 16:17:12 +0000 | [diff] [blame] | 198 | |
| Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 199 | /* |
| 200 | * Check for feature set incompatibility |
| 201 | */ |
| 202 | if (!(flags & EXT2_FLAG_FORCE)) { |
| Theodore Ts'o | cf8272e | 2006-11-12 23:26:46 -0500 | [diff] [blame] | 203 | features = fs->super->s_feature_incompat; |
| 204 | #ifdef EXT2_LIB_SOFTSUPP_INCOMPAT |
| 205 | if (flags & EXT2_FLAG_SOFTSUPP_FEATURES) |
| 206 | features &= !EXT2_LIB_SOFTSUPP_INCOMPAT; |
| 207 | #endif |
| 208 | if (features & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) { |
| Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 209 | retval = EXT2_ET_UNSUPP_FEATURE; |
| 210 | goto cleanup; |
| 211 | } |
| Theodore Ts'o | cf8272e | 2006-11-12 23:26:46 -0500 | [diff] [blame] | 212 | |
| 213 | features = fs->super->s_feature_ro_compat; |
| 214 | #ifdef EXT2_LIB_SOFTSUPP_RO_COMPAT |
| 215 | if (flags & EXT2_FLAG_SOFTSUPP_FEATURES) |
| 216 | features &= !EXT2_LIB_SOFTSUPP_RO_COMPAT; |
| 217 | #endif |
| Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 218 | if ((flags & EXT2_FLAG_RW) && |
| Theodore Ts'o | cf8272e | 2006-11-12 23:26:46 -0500 | [diff] [blame] | 219 | (features & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) { |
| Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 220 | retval = EXT2_ET_RO_UNSUPP_FEATURE; |
| 221 | goto cleanup; |
| 222 | } |
| Theodore Ts'o | cf8272e | 2006-11-12 23:26:46 -0500 | [diff] [blame] | 223 | |
| Theodore Ts'o | a112847 | 2001-01-16 06:56:14 +0000 | [diff] [blame] | 224 | if (!(flags & EXT2_FLAG_JOURNAL_DEV_OK) && |
| 225 | (fs->super->s_feature_incompat & |
| 226 | EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) { |
| 227 | retval = EXT2_ET_UNSUPP_FEATURE; |
| 228 | goto cleanup; |
| 229 | } |
| Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 232 | fs->blocksize = EXT2_BLOCK_SIZE(fs->super); |
| Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 233 | if (fs->blocksize == 0) { |
| 234 | retval = EXT2_ET_CORRUPT_SUPERBLOCK; |
| 235 | goto cleanup; |
| 236 | } |
| Theodore Ts'o | ba9d929 | 2007-09-07 16:40:25 -0400 | [diff] [blame] | 237 | if (EXT2_INODE_SIZE(fs->super) < EXT2_GOOD_OLD_INODE_SIZE) { |
| 238 | retval = EXT2_ET_CORRUPT_SUPERBLOCK; |
| 239 | goto cleanup; |
| 240 | } |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 241 | fs->fragsize = EXT2_FRAG_SIZE(fs->super); |
| Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 242 | fs->inode_blocks_per_group = ((fs->super->s_inodes_per_group * |
| 243 | EXT2_INODE_SIZE(fs->super) + |
| 244 | EXT2_BLOCK_SIZE(fs->super) - 1) / |
| 245 | EXT2_BLOCK_SIZE(fs->super)); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 246 | if (block_size) { |
| 247 | if (block_size != fs->blocksize) { |
| 248 | retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE; |
| 249 | goto cleanup; |
| 250 | } |
| 251 | } |
| 252 | /* |
| 253 | * Set the blocksize to the filesystem's blocksize. |
| 254 | */ |
| 255 | io_channel_set_blksize(fs->io, fs->blocksize); |
| Theodore Ts'o | a112847 | 2001-01-16 06:56:14 +0000 | [diff] [blame] | 256 | |
| 257 | /* |
| 258 | * If this is an external journal device, don't try to read |
| 259 | * the group descriptors, because they're not there. |
| 260 | */ |
| 261 | if (fs->super->s_feature_incompat & |
| 262 | EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) { |
| 263 | fs->group_desc_count = 0; |
| 264 | *ret_fs = fs; |
| 265 | return 0; |
| 266 | } |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 267 | |
| 268 | /* |
| 269 | * Read group descriptors |
| 270 | */ |
| Andreas Dilger | b21bf26 | 2002-06-10 11:05:56 -0600 | [diff] [blame] | 271 | blocks_per_group = EXT2_BLOCKS_PER_GROUP(fs->super); |
| 272 | if (blocks_per_group == 0 || |
| 273 | blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(fs->super) || |
| 274 | fs->inode_blocks_per_group > EXT2_MAX_INODES_PER_GROUP(fs->super)) { |
| Theodore Ts'o | f0687a5 | 1999-05-29 21:48:03 +0000 | [diff] [blame] | 275 | retval = EXT2_ET_CORRUPT_SUPERBLOCK; |
| 276 | goto cleanup; |
| 277 | } |
| Theodore Ts'o | 69022e0 | 2006-08-30 01:57:00 -0400 | [diff] [blame] | 278 | fs->group_desc_count = ext2fs_div_ceil(fs->super->s_blocks_count - |
| 279 | fs->super->s_first_data_block, |
| 280 | blocks_per_group); |
| 281 | fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count, |
| 282 | EXT2_DESC_PER_BLOCK(fs->super)); |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 283 | retval = ext2fs_get_mem(fs->desc_blocks * fs->blocksize, |
| Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 284 | &fs->group_desc); |
| Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 285 | if (retval) |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 286 | goto cleanup; |
| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 287 | if (!group_block) |
| Theodore Ts'o | c046ac7 | 2002-10-20 00:38:57 -0400 | [diff] [blame] | 288 | group_block = fs->super->s_first_data_block; |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 289 | dest = (char *) fs->group_desc; |
| Valerie Clement | f2de1d3 | 2007-08-30 17:38:13 +0200 | [diff] [blame^] | 290 | groups_per_block = EXT2_DESC_PER_BLOCK(fs->super); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 291 | for (i=0 ; i < fs->desc_blocks; i++) { |
| Theodore Ts'o | c046ac7 | 2002-10-20 00:38:57 -0400 | [diff] [blame] | 292 | blk = ext2fs_descriptor_block_loc(fs, group_block, i); |
| 293 | retval = io_channel_read_blk(fs->io, blk, 1, dest); |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 294 | if (retval) |
| 295 | goto cleanup; |
| Theodore Ts'o | 126a291 | 2007-08-11 01:56:48 -0400 | [diff] [blame] | 296 | #ifdef WORDS_BIGENDIAN |
| 297 | gdp = (struct ext2_group_desc *) dest; |
| 298 | for (j=0; j < groups_per_block; j++) |
| 299 | ext2fs_swap_group_desc(gdp++); |
| Theodore Ts'o | 5df55d7 | 2001-06-11 07:00:04 +0000 | [diff] [blame] | 300 | #endif |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 301 | dest += fs->blocksize; |
| 302 | } |
| 303 | |
| Theodore Ts'o | 96c6a3a | 2007-05-18 22:06:53 -0400 | [diff] [blame] | 304 | fs->stride = fs->super->s_raid_stride; |
| 305 | |
| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 306 | *ret_fs = fs; |
| 307 | return 0; |
| 308 | cleanup: |
| 309 | ext2fs_free(fs); |
| 310 | return retval; |
| 311 | } |
| 312 | |
| Theodore Ts'o | 1ad54a9 | 2004-07-28 21:11:48 -0400 | [diff] [blame] | 313 | /* |
| 314 | * Set/get the filesystem data I/O channel. |
| 315 | * |
| 316 | * These functions are only valid if EXT2_FLAG_IMAGE_FILE is true. |
| 317 | */ |
| 318 | errcode_t ext2fs_get_data_io(ext2_filsys fs, io_channel *old_io) |
| 319 | { |
| 320 | if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0) |
| 321 | return EXT2_ET_NOT_IMAGE_FILE; |
| 322 | if (old_io) { |
| 323 | *old_io = (fs->image_io == fs->io) ? 0 : fs->io; |
| 324 | } |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | errcode_t ext2fs_set_data_io(ext2_filsys fs, io_channel new_io) |
| 329 | { |
| 330 | if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0) |
| 331 | return EXT2_ET_NOT_IMAGE_FILE; |
| 332 | fs->io = new_io ? new_io : fs->image_io; |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | errcode_t ext2fs_rewrite_to_io(ext2_filsys fs, io_channel new_io) |
| 337 | { |
| 338 | if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0) |
| 339 | return EXT2_ET_NOT_IMAGE_FILE; |
| 340 | fs->io = fs->image_io = new_io; |
| 341 | fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_RW | |
| 342 | EXT2_FLAG_BB_DIRTY | EXT2_FLAG_IB_DIRTY; |
| 343 | fs->flags &= ~EXT2_FLAG_IMAGE_FILE; |
| 344 | return 0; |
| 345 | } |