2 * openfs.c --- open an ext2 filesystem
4 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
24 #include <sys/types.h>
36 blk64_t ext2fs_descriptor_block_loc2(ext2_filsys fs, blk64_t group_block,
43 if (!(fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) ||
44 (i < fs->super->s_first_meta_bg))
45 return (group_block + i + 1);
47 bg = EXT2_DESC_PER_BLOCK(fs->super) * i;
48 if (ext2fs_bg_has_super(fs, bg))
50 ret_blk = ext2fs_group_first_block2(fs, bg) + has_super;
52 * If group_block is not the normal value, we're trying to use
53 * the backup group descriptors and superblock --- so use the
54 * alternate location of the second block group in the
55 * metablock group. Ideally we should be testing each bg
56 * descriptor block individually for correctness, but we don't
57 * have the infrastructure in place to do that.
59 if (group_block != fs->super->s_first_data_block &&
60 ((ret_blk + fs->super->s_blocks_per_group) <
61 ext2fs_blocks_count(fs->super)))
62 ret_blk += fs->super->s_blocks_per_group;
66 blk_t ext2fs_descriptor_block_loc(ext2_filsys fs, blk_t group_block, dgrp_t i)
68 return ext2fs_descriptor_block_loc2(fs, group_block, i);
71 errcode_t ext2fs_open(const char *name, int flags, int superblock,
72 unsigned int block_size, io_manager manager,
75 return ext2fs_open2(name, 0, flags, superblock, block_size,
80 * Note: if superblock is non-zero, block-size must also be non-zero.
81 * Superblock and block_size can be zero to use the default size.
83 * Valid flags for ext2fs_open()
85 * EXT2_FLAG_RW - Open the filesystem for read/write.
86 * EXT2_FLAG_FORCE - Open the filesystem even if some of the
87 * features aren't supported.
88 * EXT2_FLAG_JOURNAL_DEV_OK - Open an ext3 journal device
89 * EXT2_FLAG_SKIP_MMP - Open without multi-mount protection check.
91 errcode_t ext2fs_open2(const char *name, const char *io_options,
92 int flags, int superblock,
93 unsigned int block_size, io_manager manager,
98 unsigned long i, first_meta_bg;
100 unsigned int groups_per_block, blocks_per_group, io_flags;
101 blk64_t group_block, blk;
103 #ifdef WORDS_BIGENDIAN
104 struct ext2_group_desc *gdp;
108 EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);
110 retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
114 memset(fs, 0, sizeof(struct struct_ext2_filsys));
115 fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
117 /* don't overwrite sb backups unless flag is explicitly cleared */
118 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
120 retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name);
123 strcpy(fs->device_name, name);
124 cp = strchr(fs->device_name, '?');
125 if (!io_options && cp) {
131 if (flags & EXT2_FLAG_RW)
132 io_flags |= IO_FLAG_RW;
133 if (flags & EXT2_FLAG_EXCLUSIVE)
134 io_flags |= IO_FLAG_EXCLUSIVE;
135 if (flags & EXT2_FLAG_DIRECT_IO)
136 io_flags |= IO_FLAG_DIRECT_IO;
137 retval = manager->open(fs->device_name, io_flags, &fs->io);
141 (retval = io_channel_set_options(fs->io, io_options)))
143 fs->image_io = fs->io;
144 fs->io->app_data = fs;
145 retval = ext2fs_get_memalign(SUPERBLOCK_SIZE, 512, &fs->super);
148 if (flags & EXT2_FLAG_IMAGE_FILE) {
149 retval = ext2fs_get_mem(sizeof(struct ext2_image_hdr),
153 retval = io_channel_read_blk(fs->io, 0,
154 -(int)sizeof(struct ext2_image_hdr),
158 if (fs->image_header->magic_number != EXT2_ET_MAGIC_E2IMAGE)
159 return EXT2_ET_MAGIC_E2IMAGE;
161 block_size = fs->image_header->fs_blocksize;
165 * If the user specifies a specific block # for the
166 * superblock, then he/she must also specify the block size!
167 * Otherwise, read the master superblock located at offset
168 * SUPERBLOCK_OFFSET from the start of the partition.
170 * Note: we only save a backup copy of the superblock if we
171 * are reading the superblock from the primary superblock location.
175 retval = EXT2_ET_INVALID_ARGUMENT;
178 io_channel_set_blksize(fs->io, block_size);
179 group_block = superblock;
182 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
185 retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->orig_super);
189 retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
194 memcpy(fs->orig_super, fs->super, SUPERBLOCK_SIZE);
196 #ifdef WORDS_BIGENDIAN
197 fs->flags |= EXT2_FLAG_SWAP_BYTES;
198 ext2fs_swap_super(fs->super);
200 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
201 retval = EXT2_ET_UNIMPLEMENTED;
206 if (fs->super->s_magic != EXT2_SUPER_MAGIC) {
207 retval = EXT2_ET_BAD_MAGIC;
210 if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
211 retval = EXT2_ET_REV_TOO_HIGH;
216 * Check for feature set incompatibility
218 if (!(flags & EXT2_FLAG_FORCE)) {
219 features = fs->super->s_feature_incompat;
220 #ifdef EXT2_LIB_SOFTSUPP_INCOMPAT
221 if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
222 features &= ~EXT2_LIB_SOFTSUPP_INCOMPAT;
224 if (features & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
225 retval = EXT2_ET_UNSUPP_FEATURE;
229 features = fs->super->s_feature_ro_compat;
230 #ifdef EXT2_LIB_SOFTSUPP_RO_COMPAT
231 if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
232 features &= ~EXT2_LIB_SOFTSUPP_RO_COMPAT;
234 if ((flags & EXT2_FLAG_RW) &&
235 (features & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
236 retval = EXT2_ET_RO_UNSUPP_FEATURE;
240 if (!(flags & EXT2_FLAG_JOURNAL_DEV_OK) &&
241 (fs->super->s_feature_incompat &
242 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
243 retval = EXT2_ET_UNSUPP_FEATURE;
248 if ((fs->super->s_log_block_size + EXT2_MIN_BLOCK_LOG_SIZE) >
249 EXT2_MAX_BLOCK_LOG_SIZE) {
250 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
253 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
254 EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
255 (fs->super->s_log_block_size != fs->super->s_log_cluster_size)) {
256 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
259 fs->fragsize = fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
260 if (EXT2_INODE_SIZE(fs->super) < EXT2_GOOD_OLD_INODE_SIZE) {
261 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
264 fs->cluster_ratio_bits = fs->super->s_log_cluster_size -
265 fs->super->s_log_block_size;
266 if (EXT2_BLOCKS_PER_GROUP(fs->super) !=
267 EXT2_CLUSTERS_PER_GROUP(fs->super) << fs->cluster_ratio_bits) {
268 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
271 fs->inode_blocks_per_group = ((EXT2_INODES_PER_GROUP(fs->super) *
272 EXT2_INODE_SIZE(fs->super) +
273 EXT2_BLOCK_SIZE(fs->super) - 1) /
274 EXT2_BLOCK_SIZE(fs->super));
276 if (block_size != fs->blocksize) {
277 retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
282 * Set the blocksize to the filesystem's blocksize.
284 io_channel_set_blksize(fs->io, fs->blocksize);
287 * If this is an external journal device, don't try to read
288 * the group descriptors, because they're not there.
290 if (fs->super->s_feature_incompat &
291 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
292 fs->group_desc_count = 0;
297 if (EXT2_INODES_PER_GROUP(fs->super) == 0) {
298 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
303 * Read group descriptors
305 blocks_per_group = EXT2_BLOCKS_PER_GROUP(fs->super);
306 if (blocks_per_group == 0 ||
307 blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(fs->super) ||
308 fs->inode_blocks_per_group > EXT2_MAX_INODES_PER_GROUP(fs->super) ||
309 EXT2_DESC_PER_BLOCK(fs->super) == 0 ||
310 fs->super->s_first_data_block >= ext2fs_blocks_count(fs->super)) {
311 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
314 fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
315 fs->super->s_first_data_block,
317 if (fs->group_desc_count * EXT2_INODES_PER_GROUP(fs->super) !=
318 fs->super->s_inodes_count) {
319 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
322 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
323 EXT2_DESC_PER_BLOCK(fs->super));
324 retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
329 group_block = fs->super->s_first_data_block;
330 if (group_block == 0 && fs->blocksize == 1024)
331 group_block = 1; /* Deal with 1024 blocksize && bigalloc */
332 dest = (char *) fs->group_desc;
333 groups_per_block = EXT2_DESC_PER_BLOCK(fs->super);
334 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
335 first_meta_bg = fs->super->s_first_meta_bg;
337 first_meta_bg = fs->desc_blocks;
339 retval = io_channel_read_blk(fs->io, group_block+1,
340 first_meta_bg, dest);
343 #ifdef WORDS_BIGENDIAN
344 gdp = (struct ext2_group_desc *) dest;
345 for (j=0; j < groups_per_block*first_meta_bg; j++) {
346 gdp = ext2fs_group_desc(fs, fs->group_desc, j);
347 ext2fs_swap_group_desc2(fs, gdp);
350 dest += fs->blocksize*first_meta_bg;
352 for (i=first_meta_bg ; i < fs->desc_blocks; i++) {
353 blk = ext2fs_descriptor_block_loc2(fs, group_block, i);
354 retval = io_channel_read_blk64(fs->io, blk, 1, dest);
357 #ifdef WORDS_BIGENDIAN
358 for (j=0; j < groups_per_block; j++) {
359 gdp = ext2fs_group_desc(fs, fs->group_desc,
360 i * groups_per_block + j);
361 ext2fs_swap_group_desc2(fs, gdp);
364 dest += fs->blocksize;
367 fs->stride = fs->super->s_raid_stride;
370 * If recovery is from backup superblock, Clear _UNININT flags &
371 * reset bg_itable_unused to zero
373 if (superblock > 1 && EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
374 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
377 for (group = 0; group < fs->group_desc_count; group++) {
378 ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
379 ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
380 ext2fs_bg_itable_unused_set(fs, group, 0);
381 /* The checksum will be reset later, but fix it here
382 * anyway to avoid printing a lot of spurious errors. */
383 ext2fs_group_desc_csum_set(fs, group);
385 if (fs->flags & EXT2_FLAG_RW)
386 ext2fs_mark_super_dirty(fs);
389 fs->flags &= ~EXT2_FLAG_NOFREE_ON_ERROR;
392 if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) &&
393 !(flags & EXT2_FLAG_SKIP_MMP) &&
394 (flags & (EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE))) {
395 retval = ext2fs_mmp_start(fs);
397 fs->flags |= EXT2_FLAG_SKIP_MMP; /* just do cleanup */
405 if (flags & EXT2_FLAG_NOFREE_ON_ERROR)
413 * Set/get the filesystem data I/O channel.
415 * These functions are only valid if EXT2_FLAG_IMAGE_FILE is true.
417 errcode_t ext2fs_get_data_io(ext2_filsys fs, io_channel *old_io)
419 if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
420 return EXT2_ET_NOT_IMAGE_FILE;
422 *old_io = (fs->image_io == fs->io) ? 0 : fs->io;
427 errcode_t ext2fs_set_data_io(ext2_filsys fs, io_channel new_io)
429 if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
430 return EXT2_ET_NOT_IMAGE_FILE;
431 fs->io = new_io ? new_io : fs->image_io;
435 errcode_t ext2fs_rewrite_to_io(ext2_filsys fs, io_channel new_io)
437 if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
438 return EXT2_ET_NOT_IMAGE_FILE;
439 fs->io = fs->image_io = new_io;
440 fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_RW |
441 EXT2_FLAG_BB_DIRTY | EXT2_FLAG_IB_DIRTY;
442 fs->flags &= ~EXT2_FLAG_IMAGE_FILE;