openfs.c revision 544349270e4c74a6feb971123884a8cf5052a7ee
1/*
2 * openfs.c --- open an ext2 filesystem
3 *
4 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 */
11
12#include <stdio.h>
13#include <string.h>
14#if HAVE_UNISTD_H
15#include <unistd.h>
16#endif
17#include <fcntl.h>
18#include <time.h>
19#if HAVE_SYS_STAT_H
20#include <sys/stat.h>
21#endif
22#if HAVE_SYS_TYPES_H
23#include <sys/types.h>
24#endif
25
26#include "ext2_fs.h"
27
28
29#include "ext2fs.h"
30#include "e2image.h"
31
32blk_t ext2fs_descriptor_block_loc(ext2_filsys fs, blk_t group_block, dgrp_t i)
33{
34	int	bg;
35	int	has_super = 0;
36	int	ret_blk;
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
42	bg = (fs->blocksize / sizeof (struct ext2_group_desc)) * i;
43	if (ext2fs_bg_has_super(fs, bg))
44		has_super = 1;
45	ret_blk = (fs->super->s_first_data_block + has_super +
46		   (bg * fs->super->s_blocks_per_group));
47	/*
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.
54	 */
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;
59	return ret_blk;
60}
61
62/*
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.
65 *
66 * Valid flags for ext2fs_open()
67 *
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
72 */
73errcode_t ext2fs_open(const char *name, int flags, int superblock,
74		      unsigned int block_size, io_manager manager,
75		      ext2_filsys *ret_fs)
76{
77	ext2_filsys	fs;
78	errcode_t	retval;
79	unsigned long	i;
80	int		j, groups_per_block, blocks_per_group;
81	blk_t		group_block, blk;
82	char		*dest;
83	struct ext2_group_desc *gdp;
84
85	EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);
86
87	retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
88	if (retval)
89		return retval;
90
91	memset(fs, 0, sizeof(struct struct_ext2_filsys));
92	fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
93	fs->flags = flags;
94	fs->umask = 022;
95	retval = manager->open(name, (flags & EXT2_FLAG_RW) ? IO_FLAG_RW : 0,
96			       &fs->io);
97	if (retval)
98		goto cleanup;
99	fs->io->app_data = fs;
100	retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name);
101	if (retval)
102		goto cleanup;
103	strcpy(fs->device_name, name);
104	retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->super);
105	if (retval)
106		goto cleanup;
107	if (flags & EXT2_FLAG_IMAGE_FILE) {
108		retval = ext2fs_get_mem(sizeof(struct ext2_image_hdr),
109					&fs->image_header);
110		if (retval)
111			goto cleanup;
112		retval = io_channel_read_blk(fs->io, 0,
113					     -sizeof(struct ext2_image_hdr),
114					     fs->image_header);
115		if (retval)
116			goto cleanup;
117		if (fs->image_header->magic_number != EXT2_ET_MAGIC_E2IMAGE)
118			return EXT2_ET_MAGIC_E2IMAGE;
119		superblock = 1;
120		block_size = fs->image_header->fs_blocksize;
121	}
122
123	/*
124	 * If the user specifies a specific block # for the
125	 * superblock, then he/she must also specify the block size!
126	 * Otherwise, read the master superblock located at offset
127	 * SUPERBLOCK_OFFSET from the start of the partition.
128	 *
129	 * Note: we only save a backup copy of the superblock if we
130	 * are reading the superblock from the primary superblock location.
131	 */
132	if (superblock) {
133		if (!block_size) {
134			retval = EXT2_ET_INVALID_ARGUMENT;
135			goto cleanup;
136		}
137		io_channel_set_blksize(fs->io, block_size);
138		group_block = superblock;
139		fs->orig_super = 0;
140	} else {
141		io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
142		superblock = 1;
143		group_block = 0;
144		retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->orig_super);
145		if (retval)
146			goto cleanup;
147	}
148	retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
149				     fs->super);
150	if (retval)
151		goto cleanup;
152	if (fs->orig_super)
153		memcpy(fs->orig_super, fs->super, SUPERBLOCK_SIZE);
154
155#ifdef EXT2FS_ENABLE_SWAPFS
156	if ((fs->super->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) ||
157	    (fs->flags & EXT2_FLAG_SWAP_BYTES)) {
158		fs->flags |= EXT2_FLAG_SWAP_BYTES;
159
160		ext2fs_swap_super(fs->super);
161	}
162#endif
163
164	if (fs->super->s_magic != EXT2_SUPER_MAGIC) {
165		retval = EXT2_ET_BAD_MAGIC;
166		goto cleanup;
167	}
168	if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
169		retval = EXT2_ET_REV_TOO_HIGH;
170		goto cleanup;
171	}
172
173	/*
174	 * Check for feature set incompatibility
175	 */
176	if (!(flags & EXT2_FLAG_FORCE)) {
177		if (fs->super->s_feature_incompat &
178		    ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
179			retval = EXT2_ET_UNSUPP_FEATURE;
180			goto cleanup;
181		}
182		if ((flags & EXT2_FLAG_RW) &&
183		    (fs->super->s_feature_ro_compat &
184		     ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
185			retval = EXT2_ET_RO_UNSUPP_FEATURE;
186			goto cleanup;
187		}
188		if (!(flags & EXT2_FLAG_JOURNAL_DEV_OK) &&
189		    (fs->super->s_feature_incompat &
190		     EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
191			retval = EXT2_ET_UNSUPP_FEATURE;
192			goto cleanup;
193		}
194	}
195
196	fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
197	if (fs->blocksize == 0) {
198		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
199		goto cleanup;
200	}
201	fs->fragsize = EXT2_FRAG_SIZE(fs->super);
202	fs->inode_blocks_per_group = ((fs->super->s_inodes_per_group *
203				       EXT2_INODE_SIZE(fs->super) +
204				       EXT2_BLOCK_SIZE(fs->super) - 1) /
205				      EXT2_BLOCK_SIZE(fs->super));
206	if (block_size) {
207		if (block_size != fs->blocksize) {
208			retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
209			goto cleanup;
210		}
211	}
212	/*
213	 * Set the blocksize to the filesystem's blocksize.
214	 */
215	io_channel_set_blksize(fs->io, fs->blocksize);
216
217	/*
218	 * If this is an external journal device, don't try to read
219	 * the group descriptors, because they're not there.
220	 */
221	if (fs->super->s_feature_incompat &
222	    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
223		fs->group_desc_count = 0;
224		*ret_fs = fs;
225		return 0;
226	}
227
228	/*
229	 * Read group descriptors
230	 */
231	blocks_per_group = EXT2_BLOCKS_PER_GROUP(fs->super);
232	if (blocks_per_group == 0 ||
233	    blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(fs->super) ||
234	    fs->inode_blocks_per_group > EXT2_MAX_INODES_PER_GROUP(fs->super)) {
235		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
236		goto cleanup;
237	}
238	fs->group_desc_count = (fs->super->s_blocks_count -
239				fs->super->s_first_data_block +
240				blocks_per_group - 1) / blocks_per_group;
241	fs->desc_blocks = (fs->group_desc_count +
242			   EXT2_DESC_PER_BLOCK(fs->super) - 1)
243		/ EXT2_DESC_PER_BLOCK(fs->super);
244	retval = ext2fs_get_mem(fs->desc_blocks * fs->blocksize,
245				&fs->group_desc);
246	if (retval)
247		goto cleanup;
248	if (!group_block)
249		group_block = fs->super->s_first_data_block;
250	dest = (char *) fs->group_desc;
251	groups_per_block = fs->blocksize / sizeof(struct ext2_group_desc);
252	for (i=0 ; i < fs->desc_blocks; i++) {
253		blk = ext2fs_descriptor_block_loc(fs, group_block, i);
254		retval = io_channel_read_blk(fs->io, blk, 1, dest);
255		if (retval)
256			goto cleanup;
257#ifdef EXT2FS_ENABLE_SWAPFS
258		if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
259			gdp = (struct ext2_group_desc *) dest;
260			for (j=0; j < groups_per_block; j++)
261				ext2fs_swap_group_desc(gdp++);
262		}
263#endif
264		dest += fs->blocksize;
265	}
266
267	*ret_fs = fs;
268	return 0;
269cleanup:
270	ext2fs_free(fs);
271	return retval;
272}
273
274