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
,
40 int has_super
= 0, group_zero_adjust
= 0;
44 * On a bigalloc FS with 1K blocks, block 0 is reserved for non-ext4
45 * stuff, so adjust for that if we're being asked for group 0.
47 if (i
== 0 && fs
->blocksize
== 1024 && EXT2FS_CLUSTER_RATIO(fs
) > 1)
48 group_zero_adjust
= 1;
50 if (!(fs
->super
->s_feature_incompat
& EXT2_FEATURE_INCOMPAT_META_BG
) ||
51 (i
< fs
->super
->s_first_meta_bg
))
52 return group_block
+ i
+ 1 + group_zero_adjust
;
54 bg
= EXT2_DESC_PER_BLOCK(fs
->super
) * i
;
55 if (ext2fs_bg_has_super(fs
, bg
))
57 ret_blk
= ext2fs_group_first_block2(fs
, bg
);
59 * If group_block is not the normal value, we're trying to use
60 * the backup group descriptors and superblock --- so use the
61 * alternate location of the second block group in the
62 * metablock group. Ideally we should be testing each bg
63 * descriptor block individually for correctness, but we don't
64 * have the infrastructure in place to do that.
66 if (group_block
!= fs
->super
->s_first_data_block
&&
67 ((ret_blk
+ has_super
+ fs
->super
->s_blocks_per_group
) <
68 ext2fs_blocks_count(fs
->super
))) {
69 ret_blk
+= fs
->super
->s_blocks_per_group
;
72 * If we're going to jump forward a block group, make sure
73 * that we adjust has_super to account for the next group's
74 * backup superblock (or lack thereof).
76 if (ext2fs_bg_has_super(fs
, bg
+ 1))
81 return ret_blk
+ has_super
+ group_zero_adjust
;
84 blk_t
ext2fs_descriptor_block_loc(ext2_filsys fs
, blk_t group_block
, dgrp_t i
)
86 return ext2fs_descriptor_block_loc2(fs
, group_block
, i
);
89 errcode_t
ext2fs_open(const char *name
, int flags
, int superblock
,
90 unsigned int block_size
, io_manager manager
,
93 return ext2fs_open2(name
, 0, flags
, superblock
, block_size
,
98 * Note: if superblock is non-zero, block-size must also be non-zero.
99 * Superblock and block_size can be zero to use the default size.
101 * Valid flags for ext2fs_open()
103 * EXT2_FLAG_RW - Open the filesystem for read/write.
104 * EXT2_FLAG_FORCE - Open the filesystem even if some of the
105 * features aren't supported.
106 * EXT2_FLAG_JOURNAL_DEV_OK - Open an ext3 journal device
107 * EXT2_FLAG_SKIP_MMP - Open without multi-mount protection check.
108 * EXT2_FLAG_64BITS - Allow 64-bit bitfields (needed for large
111 errcode_t
ext2fs_open2(const char *name
, const char *io_options
,
112 int flags
, int superblock
,
113 unsigned int block_size
, io_manager manager
,
118 unsigned long i
, first_meta_bg
;
120 unsigned int blocks_per_group
, io_flags
;
121 blk64_t group_block
, blk
;
123 int group_zero_adjust
= 0;
124 #ifdef WORDS_BIGENDIAN
125 unsigned int groups_per_block
;
126 struct ext2_group_desc
*gdp
;
130 EXT2_CHECK_MAGIC(manager
, EXT2_ET_MAGIC_IO_MANAGER
);
132 retval
= ext2fs_get_mem(sizeof(struct struct_ext2_filsys
), &fs
);
136 memset(fs
, 0, sizeof(struct struct_ext2_filsys
));
137 fs
->magic
= EXT2_ET_MAGIC_EXT2FS_FILSYS
;
139 /* don't overwrite sb backups unless flag is explicitly cleared */
140 fs
->flags
|= EXT2_FLAG_MASTER_SB_ONLY
;
142 retval
= ext2fs_get_mem(strlen(name
)+1, &fs
->device_name
);
145 strcpy(fs
->device_name
, name
);
146 cp
= strchr(fs
->device_name
, '?');
147 if (!io_options
&& cp
) {
153 if (flags
& EXT2_FLAG_RW
)
154 io_flags
|= IO_FLAG_RW
;
155 if (flags
& EXT2_FLAG_EXCLUSIVE
)
156 io_flags
|= IO_FLAG_EXCLUSIVE
;
157 if (flags
& EXT2_FLAG_DIRECT_IO
)
158 io_flags
|= IO_FLAG_DIRECT_IO
;
159 retval
= manager
->open(fs
->device_name
, io_flags
, &fs
->io
);
163 (retval
= io_channel_set_options(fs
->io
, io_options
)))
165 fs
->image_io
= fs
->io
;
166 fs
->io
->app_data
= fs
;
167 retval
= io_channel_alloc_buf(fs
->io
, -SUPERBLOCK_SIZE
, &fs
->super
);
170 if (flags
& EXT2_FLAG_IMAGE_FILE
) {
171 retval
= ext2fs_get_mem(sizeof(struct ext2_image_hdr
),
175 retval
= io_channel_read_blk(fs
->io
, 0,
176 -(int)sizeof(struct ext2_image_hdr
),
180 if (fs
->image_header
->magic_number
!= EXT2_ET_MAGIC_E2IMAGE
)
181 return EXT2_ET_MAGIC_E2IMAGE
;
183 block_size
= fs
->image_header
->fs_blocksize
;
187 * If the user specifies a specific block # for the
188 * superblock, then he/she must also specify the block size!
189 * Otherwise, read the master superblock located at offset
190 * SUPERBLOCK_OFFSET from the start of the partition.
192 * Note: we only save a backup copy of the superblock if we
193 * are reading the superblock from the primary superblock location.
197 retval
= EXT2_ET_INVALID_ARGUMENT
;
200 io_channel_set_blksize(fs
->io
, block_size
);
201 group_block
= superblock
;
204 io_channel_set_blksize(fs
->io
, SUPERBLOCK_OFFSET
);
207 retval
= ext2fs_get_mem(SUPERBLOCK_SIZE
, &fs
->orig_super
);
211 retval
= io_channel_read_blk(fs
->io
, superblock
, -SUPERBLOCK_SIZE
,
216 memcpy(fs
->orig_super
, fs
->super
, SUPERBLOCK_SIZE
);
218 #ifdef WORDS_BIGENDIAN
219 fs
->flags
|= EXT2_FLAG_SWAP_BYTES
;
220 ext2fs_swap_super(fs
->super
);
222 if (fs
->flags
& EXT2_FLAG_SWAP_BYTES
) {
223 retval
= EXT2_ET_UNIMPLEMENTED
;
228 if (fs
->super
->s_magic
!= EXT2_SUPER_MAGIC
) {
229 retval
= EXT2_ET_BAD_MAGIC
;
232 if (fs
->super
->s_rev_level
> EXT2_LIB_CURRENT_REV
) {
233 retval
= EXT2_ET_REV_TOO_HIGH
;
238 * Check for feature set incompatibility
240 if (!(flags
& EXT2_FLAG_FORCE
)) {
241 features
= fs
->super
->s_feature_incompat
;
242 #ifdef EXT2_LIB_SOFTSUPP_INCOMPAT
243 if (flags
& EXT2_FLAG_SOFTSUPP_FEATURES
)
244 features
&= ~EXT2_LIB_SOFTSUPP_INCOMPAT
;
246 if (features
& ~EXT2_LIB_FEATURE_INCOMPAT_SUPP
) {
247 retval
= EXT2_ET_UNSUPP_FEATURE
;
251 features
= fs
->super
->s_feature_ro_compat
;
252 #ifdef EXT2_LIB_SOFTSUPP_RO_COMPAT
253 if (flags
& EXT2_FLAG_SOFTSUPP_FEATURES
)
254 features
&= ~EXT2_LIB_SOFTSUPP_RO_COMPAT
;
256 if ((flags
& EXT2_FLAG_RW
) &&
257 (features
& ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP
)) {
258 retval
= EXT2_ET_RO_UNSUPP_FEATURE
;
262 if (!(flags
& EXT2_FLAG_JOURNAL_DEV_OK
) &&
263 (fs
->super
->s_feature_incompat
&
264 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV
)) {
265 retval
= EXT2_ET_UNSUPP_FEATURE
;
270 if ((fs
->super
->s_log_block_size
+ EXT2_MIN_BLOCK_LOG_SIZE
) >
271 EXT2_MAX_BLOCK_LOG_SIZE
) {
272 retval
= EXT2_ET_CORRUPT_SUPERBLOCK
;
277 * bigalloc requires cluster-aware bitfield operations, which at the
278 * moment means we need EXT2_FLAG_64BITS.
280 if (EXT2_HAS_RO_COMPAT_FEATURE(fs
->super
,
281 EXT4_FEATURE_RO_COMPAT_BIGALLOC
) &&
282 !(flags
& EXT2_FLAG_64BITS
)) {
283 retval
= EXT2_ET_CANT_USE_LEGACY_BITMAPS
;
287 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs
->super
,
288 EXT4_FEATURE_RO_COMPAT_BIGALLOC
) &&
289 (fs
->super
->s_log_block_size
!= fs
->super
->s_log_cluster_size
)) {
290 retval
= EXT2_ET_CORRUPT_SUPERBLOCK
;
293 fs
->fragsize
= fs
->blocksize
= EXT2_BLOCK_SIZE(fs
->super
);
294 if (EXT2_INODE_SIZE(fs
->super
) < EXT2_GOOD_OLD_INODE_SIZE
) {
295 retval
= EXT2_ET_CORRUPT_SUPERBLOCK
;
298 fs
->cluster_ratio_bits
= fs
->super
->s_log_cluster_size
-
299 fs
->super
->s_log_block_size
;
300 if (EXT2_BLOCKS_PER_GROUP(fs
->super
) !=
301 EXT2_CLUSTERS_PER_GROUP(fs
->super
) << fs
->cluster_ratio_bits
) {
302 retval
= EXT2_ET_CORRUPT_SUPERBLOCK
;
305 fs
->inode_blocks_per_group
= ((EXT2_INODES_PER_GROUP(fs
->super
) *
306 EXT2_INODE_SIZE(fs
->super
) +
307 EXT2_BLOCK_SIZE(fs
->super
) - 1) /
308 EXT2_BLOCK_SIZE(fs
->super
));
310 if (block_size
!= fs
->blocksize
) {
311 retval
= EXT2_ET_UNEXPECTED_BLOCK_SIZE
;
316 * Set the blocksize to the filesystem's blocksize.
318 io_channel_set_blksize(fs
->io
, fs
->blocksize
);
321 * If this is an external journal device, don't try to read
322 * the group descriptors, because they're not there.
324 if (fs
->super
->s_feature_incompat
&
325 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV
) {
326 fs
->group_desc_count
= 0;
331 if (EXT2_INODES_PER_GROUP(fs
->super
) == 0) {
332 retval
= EXT2_ET_CORRUPT_SUPERBLOCK
;
337 * Read group descriptors
339 blocks_per_group
= EXT2_BLOCKS_PER_GROUP(fs
->super
);
340 if (blocks_per_group
== 0 ||
341 blocks_per_group
> EXT2_MAX_BLOCKS_PER_GROUP(fs
->super
) ||
342 fs
->inode_blocks_per_group
> EXT2_MAX_INODES_PER_GROUP(fs
->super
) ||
343 EXT2_DESC_PER_BLOCK(fs
->super
) == 0 ||
344 fs
->super
->s_first_data_block
>= ext2fs_blocks_count(fs
->super
)) {
345 retval
= EXT2_ET_CORRUPT_SUPERBLOCK
;
348 fs
->group_desc_count
= ext2fs_div64_ceil(ext2fs_blocks_count(fs
->super
) -
349 fs
->super
->s_first_data_block
,
351 if (fs
->group_desc_count
* EXT2_INODES_PER_GROUP(fs
->super
) !=
352 fs
->super
->s_inodes_count
) {
353 retval
= EXT2_ET_CORRUPT_SUPERBLOCK
;
356 fs
->desc_blocks
= ext2fs_div_ceil(fs
->group_desc_count
,
357 EXT2_DESC_PER_BLOCK(fs
->super
));
358 retval
= ext2fs_get_array(fs
->desc_blocks
, fs
->blocksize
,
363 group_block
= fs
->super
->s_first_data_block
;
365 * On a FS with a 1K blocksize, block 0 is reserved for bootloaders
366 * so we must increment block numbers to any group 0 items.
368 * However, we cannot touch group_block directly because in the meta_bg
369 * case, the ext2fs_descriptor_block_loc2() function will interpret
370 * group_block != s_first_data_block to mean that we want to access the
371 * backup group descriptors. This is not what we want if the caller
372 * set superblock == 0 (i.e. auto-detect the superblock), which is
373 * what's going on here.
375 if (group_block
== 0 && fs
->blocksize
== 1024)
376 group_zero_adjust
= 1;
377 dest
= (char *) fs
->group_desc
;
378 #ifdef WORDS_BIGENDIAN
379 groups_per_block
= EXT2_DESC_PER_BLOCK(fs
->super
);
381 if (fs
->super
->s_feature_incompat
& EXT2_FEATURE_INCOMPAT_META_BG
)
382 first_meta_bg
= fs
->super
->s_first_meta_bg
;
384 first_meta_bg
= fs
->desc_blocks
;
386 retval
= io_channel_read_blk(fs
->io
, group_block
+
387 group_zero_adjust
+ 1,
388 first_meta_bg
, dest
);
391 #ifdef WORDS_BIGENDIAN
392 gdp
= (struct ext2_group_desc
*) dest
;
393 for (j
=0; j
< groups_per_block
*first_meta_bg
; j
++) {
394 gdp
= ext2fs_group_desc(fs
, fs
->group_desc
, j
);
395 ext2fs_swap_group_desc2(fs
, gdp
);
398 dest
+= fs
->blocksize
*first_meta_bg
;
400 for (i
=first_meta_bg
; i
< fs
->desc_blocks
; i
++) {
401 blk
= ext2fs_descriptor_block_loc2(fs
, group_block
, i
);
402 retval
= io_channel_read_blk64(fs
->io
, blk
, 1, dest
);
405 #ifdef WORDS_BIGENDIAN
406 for (j
=0; j
< groups_per_block
; j
++) {
407 gdp
= ext2fs_group_desc(fs
, fs
->group_desc
,
408 i
* groups_per_block
+ j
);
409 ext2fs_swap_group_desc2(fs
, gdp
);
412 dest
+= fs
->blocksize
;
415 fs
->stride
= fs
->super
->s_raid_stride
;
418 * If recovery is from backup superblock, Clear _UNININT flags &
419 * reset bg_itable_unused to zero
421 if (superblock
> 1 && EXT2_HAS_RO_COMPAT_FEATURE(fs
->super
,
422 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
)) {
425 for (group
= 0; group
< fs
->group_desc_count
; group
++) {
426 ext2fs_bg_flags_clear(fs
, group
, EXT2_BG_BLOCK_UNINIT
);
427 ext2fs_bg_flags_clear(fs
, group
, EXT2_BG_INODE_UNINIT
);
428 ext2fs_bg_itable_unused_set(fs
, group
, 0);
429 /* The checksum will be reset later, but fix it here
430 * anyway to avoid printing a lot of spurious errors. */
431 ext2fs_group_desc_csum_set(fs
, group
);
433 if (fs
->flags
& EXT2_FLAG_RW
)
434 ext2fs_mark_super_dirty(fs
);
437 if ((fs
->super
->s_feature_incompat
& EXT4_FEATURE_INCOMPAT_MMP
) &&
438 !(flags
& EXT2_FLAG_SKIP_MMP
) &&
439 (flags
& (EXT2_FLAG_RW
| EXT2_FLAG_EXCLUSIVE
))) {
440 retval
= ext2fs_mmp_start(fs
);
442 fs
->flags
|= EXT2_FLAG_SKIP_MMP
; /* just do cleanup */
448 fs
->flags
&= ~EXT2_FLAG_NOFREE_ON_ERROR
;
453 if (!(flags
& EXT2_FLAG_NOFREE_ON_ERROR
)) {
462 * Set/get the filesystem data I/O channel.
464 * These functions are only valid if EXT2_FLAG_IMAGE_FILE is true.
466 errcode_t
ext2fs_get_data_io(ext2_filsys fs
, io_channel
*old_io
)
468 if ((fs
->flags
& EXT2_FLAG_IMAGE_FILE
) == 0)
469 return EXT2_ET_NOT_IMAGE_FILE
;
471 *old_io
= (fs
->image_io
== fs
->io
) ? 0 : fs
->io
;
476 errcode_t
ext2fs_set_data_io(ext2_filsys fs
, io_channel new_io
)
478 if ((fs
->flags
& EXT2_FLAG_IMAGE_FILE
) == 0)
479 return EXT2_ET_NOT_IMAGE_FILE
;
480 fs
->io
= new_io
? new_io
: fs
->image_io
;
484 errcode_t
ext2fs_rewrite_to_io(ext2_filsys fs
, io_channel new_io
)
488 if ((fs
->flags
& EXT2_FLAG_IMAGE_FILE
) == 0)
489 return EXT2_ET_NOT_IMAGE_FILE
;
490 err
= io_channel_set_blksize(new_io
, fs
->blocksize
);
493 if ((new_io
== fs
->image_io
) || (new_io
== fs
->io
))
495 if ((fs
->image_io
!= fs
->io
) &&
497 io_channel_close(fs
->image_io
);
499 io_channel_close(fs
->io
);
500 fs
->io
= fs
->image_io
= new_io
;
501 fs
->flags
|= EXT2_FLAG_DIRTY
| EXT2_FLAG_RW
|
502 EXT2_FLAG_BB_DIRTY
| EXT2_FLAG_IB_DIRTY
;
503 fs
->flags
&= ~EXT2_FLAG_IMAGE_FILE
;