openfs.c revision efc6f628e15de95bcd13e4f0ee223cb42115d520
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 = EXT2_DESC_PER_BLOCK(fs->super) * i;
43	if (ext2fs_bg_has_super(fs, bg))
44		has_super = 1;
45	ret_blk = ext2fs_group_first_block(fs, bg) + has_super;
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;
59}
60
61errcode_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
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.
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
77 *				features aren't supported.
78 *	EXT2_FLAG_JOURNAL_DEV_OK - Open an ext3 journal device
79 */
80errcode_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)
84{
85	ext2_filsys	fs;
86	errcode_t	retval;
87	unsigned long	i;
88	__u32		features;
89	int		groups_per_block, blocks_per_group, io_flags;
90	blk_t		group_block, blk;
91	char		*dest, *cp;
92#ifdef WORDS_BIGENDIAN
93	struct ext2_group_desc *gdp;
94	int		j;
95#endif
96
97	EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);
98
99	retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
100	if (retval)
101		return retval;
102
103	memset(fs, 0, sizeof(struct struct_ext2_filsys));
104	fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
105	fs->flags = flags;
106	/* don't overwrite sb backups unless flag is explicitly cleared */
107	fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
108	fs->umask = 022;
109	retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name);
110	if (retval)
111		goto cleanup;
112	strcpy(fs->device_name, name);
113	cp = strchr(fs->device_name, '?');
114	if (!io_options && cp) {
115		*cp++ = 0;
116		io_options = cp;
117	}
118
119	io_flags = 0;
120	if (flags & EXT2_FLAG_RW)
121		io_flags |= IO_FLAG_RW;
122	if (flags & EXT2_FLAG_EXCLUSIVE)
123		io_flags |= IO_FLAG_EXCLUSIVE;
124	retval = manager->open(fs->device_name, io_flags, &fs->io);
125	if (retval)
126		goto cleanup;
127	if (io_options &&
128	    (retval = io_channel_set_options(fs->io, io_options)))
129		goto cleanup;
130	fs->image_io = fs->io;
131	fs->io->app_data = fs;
132	retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->super);
133	if (retval)
134		goto cleanup;
135	if (flags & EXT2_FLAG_IMAGE_FILE) {
136		retval = ext2fs_get_mem(sizeof(struct ext2_image_hdr),
137					&fs->image_header);
138		if (retval)
139			goto cleanup;
140		retval = io_channel_read_blk(fs->io, 0,
141					     -(int)sizeof(struct ext2_image_hdr),
142					     fs->image_header);
143		if (retval)
144			goto cleanup;
145		if (fs->image_header->magic_number != EXT2_ET_MAGIC_E2IMAGE)
146			return EXT2_ET_MAGIC_E2IMAGE;
147		superblock = 1;
148		block_size = fs->image_header->fs_blocksize;
149	}
150
151	/*
152	 * If the user specifies a specific block # for the
153	 * superblock, then he/she must also specify the block size!
154	 * Otherwise, read the master superblock located at offset
155	 * SUPERBLOCK_OFFSET from the start of the partition.
156	 *
157	 * Note: we only save a backup copy of the superblock if we
158	 * are reading the superblock from the primary superblock location.
159	 */
160	if (superblock) {
161		if (!block_size) {
162			retval = EXT2_ET_INVALID_ARGUMENT;
163			goto cleanup;
164		}
165		io_channel_set_blksize(fs->io, block_size);
166		group_block = superblock;
167		fs->orig_super = 0;
168	} else {
169		io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
170		superblock = 1;
171		group_block = 0;
172		retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->orig_super);
173		if (retval)
174			goto cleanup;
175	}
176	retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
177				     fs->super);
178	if (retval)
179		goto cleanup;
180	if (fs->orig_super)
181		memcpy(fs->orig_super, fs->super, SUPERBLOCK_SIZE);
182
183#ifdef WORDS_BIGENDIAN
184	fs->flags |= EXT2_FLAG_SWAP_BYTES;
185	ext2fs_swap_super(fs->super);
186#else
187	if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
188		retval = EXT2_ET_UNIMPLEMENTED;
189		goto cleanup;
190	}
191#endif
192
193	if (fs->super->s_magic != EXT2_SUPER_MAGIC) {
194		retval = EXT2_ET_BAD_MAGIC;
195		goto cleanup;
196	}
197	if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
198		retval = EXT2_ET_REV_TOO_HIGH;
199		goto cleanup;
200	}
201
202	/*
203	 * Check for feature set incompatibility
204	 */
205	if (!(flags & EXT2_FLAG_FORCE)) {
206		features = fs->super->s_feature_incompat;
207#ifdef EXT2_LIB_SOFTSUPP_INCOMPAT
208		if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
209			features &= !EXT2_LIB_SOFTSUPP_INCOMPAT;
210#endif
211		if (features & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
212			retval = EXT2_ET_UNSUPP_FEATURE;
213			goto cleanup;
214		}
215
216		features = fs->super->s_feature_ro_compat;
217#ifdef EXT2_LIB_SOFTSUPP_RO_COMPAT
218		if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
219			features &= !EXT2_LIB_SOFTSUPP_RO_COMPAT;
220#endif
221		if ((flags & EXT2_FLAG_RW) &&
222		    (features & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
223			retval = EXT2_ET_RO_UNSUPP_FEATURE;
224			goto cleanup;
225		}
226
227		if (!(flags & EXT2_FLAG_JOURNAL_DEV_OK) &&
228		    (fs->super->s_feature_incompat &
229		     EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
230			retval = EXT2_ET_UNSUPP_FEATURE;
231			goto cleanup;
232		}
233	}
234
235	if ((fs->super->s_log_block_size + EXT2_MIN_BLOCK_LOG_SIZE) >
236	    EXT2_MAX_BLOCK_LOG_SIZE) {
237		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
238		goto cleanup;
239	}
240	fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
241	if (EXT2_INODE_SIZE(fs->super) < EXT2_GOOD_OLD_INODE_SIZE) {
242		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
243		goto cleanup;
244	}
245	fs->fragsize = EXT2_FRAG_SIZE(fs->super);
246	fs->inode_blocks_per_group = ((fs->super->s_inodes_per_group *
247				       EXT2_INODE_SIZE(fs->super) +
248				       EXT2_BLOCK_SIZE(fs->super) - 1) /
249				      EXT2_BLOCK_SIZE(fs->super));
250	if (block_size) {
251		if (block_size != fs->blocksize) {
252			retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
253			goto cleanup;
254		}
255	}
256	/*
257	 * Set the blocksize to the filesystem's blocksize.
258	 */
259	io_channel_set_blksize(fs->io, fs->blocksize);
260
261	/*
262	 * If this is an external journal device, don't try to read
263	 * the group descriptors, because they're not there.
264	 */
265	if (fs->super->s_feature_incompat &
266	    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
267		fs->group_desc_count = 0;
268		*ret_fs = fs;
269		return 0;
270	}
271
272	/*
273	 * Read group descriptors
274	 */
275	blocks_per_group = EXT2_BLOCKS_PER_GROUP(fs->super);
276	if (blocks_per_group == 0 ||
277	    blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(fs->super) ||
278	    fs->inode_blocks_per_group > EXT2_MAX_INODES_PER_GROUP(fs->super)) {
279		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
280		goto cleanup;
281	}
282	fs->group_desc_count = ext2fs_div_ceil(fs->super->s_blocks_count -
283					       fs->super->s_first_data_block,
284					       blocks_per_group);
285	fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
286					  EXT2_DESC_PER_BLOCK(fs->super));
287	retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
288				&fs->group_desc);
289	if (retval)
290		goto cleanup;
291	if (!group_block)
292		group_block = fs->super->s_first_data_block;
293	dest = (char *) fs->group_desc;
294	groups_per_block = EXT2_DESC_PER_BLOCK(fs->super);
295	for (i=0 ; i < fs->desc_blocks; i++) {
296		blk = ext2fs_descriptor_block_loc(fs, group_block, i);
297		retval = io_channel_read_blk(fs->io, blk, 1, dest);
298		if (retval)
299			goto cleanup;
300#ifdef WORDS_BIGENDIAN
301		gdp = (struct ext2_group_desc *) dest;
302		for (j=0; j < groups_per_block; j++)
303			ext2fs_swap_group_desc(gdp++);
304#endif
305		dest += fs->blocksize;
306	}
307
308	fs->stride = fs->super->s_raid_stride;
309
310	/*
311	 * If recovery is from backup superblock, Clear _UNININT flags &
312	 * reset bg_itable_unused to zero
313	 */
314	if (superblock > 1 && EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
315					EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
316		struct ext2_group_desc *gd;
317		for (i = 0, gd = fs->group_desc; i < fs->group_desc_count;
318		     i++, gd++) {
319			gd->bg_flags &= ~EXT2_BG_BLOCK_UNINIT;
320			gd->bg_flags &= ~EXT2_BG_INODE_UNINIT;
321			gd->bg_itable_unused = 0;
322		}
323		ext2fs_mark_super_dirty(fs);
324	}
325
326	fs->flags &= ~EXT2_FLAG_NOFREE_ON_ERROR;
327	*ret_fs = fs;
328	return 0;
329cleanup:
330	if (flags & EXT2_FLAG_NOFREE_ON_ERROR)
331		*ret_fs = fs;
332	else
333		ext2fs_free(fs);
334	return retval;
335}
336
337/*
338 * Set/get the filesystem data I/O channel.
339 *
340 * These functions are only valid if EXT2_FLAG_IMAGE_FILE is true.
341 */
342errcode_t ext2fs_get_data_io(ext2_filsys fs, io_channel *old_io)
343{
344	if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
345		return EXT2_ET_NOT_IMAGE_FILE;
346	if (old_io) {
347		*old_io = (fs->image_io == fs->io) ? 0 : fs->io;
348	}
349	return 0;
350}
351
352errcode_t ext2fs_set_data_io(ext2_filsys fs, io_channel new_io)
353{
354	if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
355		return EXT2_ET_NOT_IMAGE_FILE;
356	fs->io = new_io ? new_io : fs->image_io;
357	return 0;
358}
359
360errcode_t ext2fs_rewrite_to_io(ext2_filsys fs, io_channel new_io)
361{
362	if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
363		return EXT2_ET_NOT_IMAGE_FILE;
364	fs->io = fs->image_io = new_io;
365	fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_RW |
366		EXT2_FLAG_BB_DIRTY | EXT2_FLAG_IB_DIRTY;
367	fs->flags &= ~EXT2_FLAG_IMAGE_FILE;
368	return 0;
369}
370