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 Public
23 #include <sys/types.h>
32 blk_t
ext2fs_descriptor_block_loc(ext2_filsys fs
, blk_t group_block
, dgrp_t i
)
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);
42 bg
= (fs
->blocksize
/ sizeof (struct ext2_group_desc
)) * i
;
43 if (ext2fs_bg_has_super(fs
, bg
))
45 ret_blk
= (fs
->super
->s_first_data_block
+ has_super
+
46 (bg
* fs
->super
->s_blocks_per_group
));
48 * If group_block is not the normal value, we're trying to use
49 * the backup group descriptors and superblock --- so use the
50 * alternate location of the second block group in the
51 * metablock group. Ideally we should be testing each bg
52 * descriptor block individually for correctness, but we don't
53 * have the infrastructure in place to do that.
55 if (group_block
!= fs
->super
->s_first_data_block
&&
56 ((ret_blk
+ fs
->super
->s_blocks_per_group
) <
57 fs
->super
->s_blocks_count
))
58 ret_blk
+= fs
->super
->s_blocks_per_group
;
63 * Note: if superblock is non-zero, block-size must also be non-zero.
64 * Superblock and block_size can be zero to use the default size.
66 * Valid flags for ext2fs_open()
68 * EXT2_FLAG_RW - Open the filesystem for read/write.
69 * EXT2_FLAG_FORCE - Open the filesystem even if some of the
70 * features aren't supported.
71 * EXT2_FLAG_JOURNAL_DEV_OK - Open an ext3 journal device
73 errcode_t
ext2fs_open(const char *name
, int flags
, int superblock
,
74 unsigned int block_size
, io_manager manager
,
80 int j
, groups_per_block
, blocks_per_group
;
81 blk_t group_block
, blk
;
83 struct ext2_group_desc
*gdp
;
85 EXT2_CHECK_MAGIC(manager
, EXT2_ET_MAGIC_IO_MANAGER
);
87 retval
= ext2fs_get_mem(sizeof(struct struct_ext2_filsys
), &fs
);
91 memset(fs
, 0, sizeof(struct struct_ext2_filsys
));
92 fs
->magic
= EXT2_ET_MAGIC_EXT2FS_FILSYS
;
95 retval
= manager
->open(name
, (flags
& EXT2_FLAG_RW
) ? IO_FLAG_RW
: 0,
99 fs
->image_io
= fs
->io
;
100 fs
->io
->app_data
= fs
;
101 retval
= ext2fs_get_mem(strlen(name
)+1, &fs
->device_name
);
104 strcpy(fs
->device_name
, name
);
105 retval
= ext2fs_get_mem(SUPERBLOCK_SIZE
, &fs
->super
);
108 if (flags
& EXT2_FLAG_IMAGE_FILE
) {
109 retval
= ext2fs_get_mem(sizeof(struct ext2_image_hdr
),
113 retval
= io_channel_read_blk(fs
->io
, 0,
114 -sizeof(struct ext2_image_hdr
),
118 if (fs
->image_header
->magic_number
!= EXT2_ET_MAGIC_E2IMAGE
)
119 return EXT2_ET_MAGIC_E2IMAGE
;
121 block_size
= fs
->image_header
->fs_blocksize
;
125 * If the user specifies a specific block # for the
126 * superblock, then he/she must also specify the block size!
127 * Otherwise, read the master superblock located at offset
128 * SUPERBLOCK_OFFSET from the start of the partition.
130 * Note: we only save a backup copy of the superblock if we
131 * are reading the superblock from the primary superblock location.
135 retval
= EXT2_ET_INVALID_ARGUMENT
;
138 io_channel_set_blksize(fs
->io
, block_size
);
139 group_block
= superblock
;
142 io_channel_set_blksize(fs
->io
, SUPERBLOCK_OFFSET
);
145 retval
= ext2fs_get_mem(SUPERBLOCK_SIZE
, &fs
->orig_super
);
149 retval
= io_channel_read_blk(fs
->io
, superblock
, -SUPERBLOCK_SIZE
,
154 memcpy(fs
->orig_super
, fs
->super
, SUPERBLOCK_SIZE
);
156 #ifdef EXT2FS_ENABLE_SWAPFS
157 if ((fs
->super
->s_magic
== ext2fs_swab16(EXT2_SUPER_MAGIC
)) ||
158 (fs
->flags
& EXT2_FLAG_SWAP_BYTES
)) {
159 fs
->flags
|= EXT2_FLAG_SWAP_BYTES
;
161 ext2fs_swap_super(fs
->super
);
165 if (fs
->super
->s_magic
!= EXT2_SUPER_MAGIC
) {
166 retval
= EXT2_ET_BAD_MAGIC
;
169 if (fs
->super
->s_rev_level
> EXT2_LIB_CURRENT_REV
) {
170 retval
= EXT2_ET_REV_TOO_HIGH
;
175 * Check for feature set incompatibility
177 if (!(flags
& EXT2_FLAG_FORCE
)) {
178 if (fs
->super
->s_feature_incompat
&
179 ~EXT2_LIB_FEATURE_INCOMPAT_SUPP
) {
180 retval
= EXT2_ET_UNSUPP_FEATURE
;
183 if ((flags
& EXT2_FLAG_RW
) &&
184 (fs
->super
->s_feature_ro_compat
&
185 ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP
)) {
186 retval
= EXT2_ET_RO_UNSUPP_FEATURE
;
189 if (!(flags
& EXT2_FLAG_JOURNAL_DEV_OK
) &&
190 (fs
->super
->s_feature_incompat
&
191 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV
)) {
192 retval
= EXT2_ET_UNSUPP_FEATURE
;
197 fs
->blocksize
= EXT2_BLOCK_SIZE(fs
->super
);
198 if (fs
->blocksize
== 0) {
199 retval
= EXT2_ET_CORRUPT_SUPERBLOCK
;
202 fs
->fragsize
= EXT2_FRAG_SIZE(fs
->super
);
203 fs
->inode_blocks_per_group
= ((fs
->super
->s_inodes_per_group
*
204 EXT2_INODE_SIZE(fs
->super
) +
205 EXT2_BLOCK_SIZE(fs
->super
) - 1) /
206 EXT2_BLOCK_SIZE(fs
->super
));
208 if (block_size
!= fs
->blocksize
) {
209 retval
= EXT2_ET_UNEXPECTED_BLOCK_SIZE
;
214 * Set the blocksize to the filesystem's blocksize.
216 io_channel_set_blksize(fs
->io
, fs
->blocksize
);
219 * If this is an external journal device, don't try to read
220 * the group descriptors, because they're not there.
222 if (fs
->super
->s_feature_incompat
&
223 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV
) {
224 fs
->group_desc_count
= 0;
230 * Read group descriptors
232 blocks_per_group
= EXT2_BLOCKS_PER_GROUP(fs
->super
);
233 if (blocks_per_group
== 0 ||
234 blocks_per_group
> EXT2_MAX_BLOCKS_PER_GROUP(fs
->super
) ||
235 fs
->inode_blocks_per_group
> EXT2_MAX_INODES_PER_GROUP(fs
->super
)) {
236 retval
= EXT2_ET_CORRUPT_SUPERBLOCK
;
239 fs
->group_desc_count
= (fs
->super
->s_blocks_count
-
240 fs
->super
->s_first_data_block
+
241 blocks_per_group
- 1) / blocks_per_group
;
242 fs
->desc_blocks
= (fs
->group_desc_count
+
243 EXT2_DESC_PER_BLOCK(fs
->super
) - 1)
244 / EXT2_DESC_PER_BLOCK(fs
->super
);
245 retval
= ext2fs_get_mem(fs
->desc_blocks
* fs
->blocksize
,
250 group_block
= fs
->super
->s_first_data_block
;
251 dest
= (char *) fs
->group_desc
;
252 groups_per_block
= fs
->blocksize
/ sizeof(struct ext2_group_desc
);
253 for (i
=0 ; i
< fs
->desc_blocks
; i
++) {
254 blk
= ext2fs_descriptor_block_loc(fs
, group_block
, i
);
255 retval
= io_channel_read_blk(fs
->io
, blk
, 1, dest
);
258 #ifdef EXT2FS_ENABLE_SWAPFS
259 if (fs
->flags
& EXT2_FLAG_SWAP_BYTES
) {
260 gdp
= (struct ext2_group_desc
*) dest
;
261 for (j
=0; j
< groups_per_block
; j
++)
262 ext2fs_swap_group_desc(gdp
++);
265 dest
+= fs
->blocksize
;
276 * Set/get the filesystem data I/O channel.
278 * These functions are only valid if EXT2_FLAG_IMAGE_FILE is true.
280 errcode_t
ext2fs_get_data_io(ext2_filsys fs
, io_channel
*old_io
)
282 if ((fs
->flags
& EXT2_FLAG_IMAGE_FILE
) == 0)
283 return EXT2_ET_NOT_IMAGE_FILE
;
285 *old_io
= (fs
->image_io
== fs
->io
) ? 0 : fs
->io
;
290 errcode_t
ext2fs_set_data_io(ext2_filsys fs
, io_channel new_io
)
292 if ((fs
->flags
& EXT2_FLAG_IMAGE_FILE
) == 0)
293 return EXT2_ET_NOT_IMAGE_FILE
;
294 fs
->io
= new_io
? new_io
: fs
->image_io
;
298 errcode_t
ext2fs_rewrite_to_io(ext2_filsys fs
, io_channel new_io
)
300 if ((fs
->flags
& EXT2_FLAG_IMAGE_FILE
) == 0)
301 return EXT2_ET_NOT_IMAGE_FILE
;
302 fs
->io
= fs
->image_io
= new_io
;
303 fs
->flags
|= EXT2_FLAG_DIRTY
| EXT2_FLAG_RW
|
304 EXT2_FLAG_BB_DIRTY
| EXT2_FLAG_IB_DIRTY
;
305 fs
->flags
&= ~EXT2_FLAG_IMAGE_FILE
;