openfs.c revision df5e35fd63b42b66d5025fdca17bfdf6662a2827
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 Library
8 * General Public License, version 2.
9 * %End-Header%
10 */
11
12#include "config.h"
13#include <stdio.h>
14#include <string.h>
15#if HAVE_UNISTD_H
16#include <unistd.h>
17#endif
18#include <fcntl.h>
19#include <time.h>
20#if HAVE_SYS_STAT_H
21#include <sys/stat.h>
22#endif
23#if HAVE_SYS_TYPES_H
24#include <sys/types.h>
25#endif
26#ifdef HAVE_ERRNO_H
27#include <errno.h>
28#endif
29
30#include "ext2_fs.h"
31
32
33#include "ext2fs.h"
34#include "e2image.h"
35
36blk64_t ext2fs_descriptor_block_loc2(ext2_filsys fs, blk64_t group_block,
37				     dgrp_t i)
38{
39	int	bg;
40	int	has_super = 0;
41	blk64_t	ret_blk;
42
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);
46
47	bg = EXT2_DESC_PER_BLOCK(fs->super) * i;
48	if (ext2fs_bg_has_super(fs, bg))
49		has_super = 1;
50	ret_blk = ext2fs_group_first_block2(fs, bg) + has_super;
51	/*
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.
58	 */
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;
63	return ret_blk;
64}
65
66blk_t ext2fs_descriptor_block_loc(ext2_filsys fs, blk_t group_block, dgrp_t i)
67{
68	return ext2fs_descriptor_block_loc2(fs, group_block, i);
69}
70
71errcode_t ext2fs_open(const char *name, int flags, int superblock,
72		      unsigned int block_size, io_manager manager,
73		      ext2_filsys *ret_fs)
74{
75	return ext2fs_open2(name, 0, flags, superblock, block_size,
76			    manager, ret_fs);
77}
78
79/*
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.
82 *
83 * Valid flags for ext2fs_open()
84 *
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.
90 *	EXT2_FLAG_64BITS - Allow 64-bit bitfields (needed for large
91 *				filesystems)
92 */
93errcode_t ext2fs_open2(const char *name, const char *io_options,
94		       int flags, int superblock,
95		       unsigned int block_size, io_manager manager,
96		       ext2_filsys *ret_fs)
97{
98	ext2_filsys	fs;
99	errcode_t	retval;
100	unsigned long	i, first_meta_bg;
101	__u32		features;
102	unsigned int	blocks_per_group, io_flags;
103	blk64_t		group_block, blk;
104	char		*dest, *cp;
105#ifdef WORDS_BIGENDIAN
106	unsigned int	groups_per_block;
107	struct ext2_group_desc *gdp;
108	int		j;
109#endif
110
111	EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);
112
113	retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
114	if (retval)
115		return retval;
116
117	memset(fs, 0, sizeof(struct struct_ext2_filsys));
118	fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
119	fs->flags = flags;
120	/* don't overwrite sb backups unless flag is explicitly cleared */
121	fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
122	fs->umask = 022;
123	retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name);
124	if (retval)
125		goto cleanup;
126	strcpy(fs->device_name, name);
127	cp = strchr(fs->device_name, '?');
128	if (!io_options && cp) {
129		*cp++ = 0;
130		io_options = cp;
131	}
132
133	io_flags = 0;
134	if (flags & EXT2_FLAG_RW)
135		io_flags |= IO_FLAG_RW;
136	if (flags & EXT2_FLAG_EXCLUSIVE)
137		io_flags |= IO_FLAG_EXCLUSIVE;
138	if (flags & EXT2_FLAG_DIRECT_IO)
139		io_flags |= IO_FLAG_DIRECT_IO;
140	retval = manager->open(fs->device_name, io_flags, &fs->io);
141	if (retval)
142		goto cleanup;
143	if (io_options &&
144	    (retval = io_channel_set_options(fs->io, io_options)))
145		goto cleanup;
146	fs->image_io = fs->io;
147	fs->io->app_data = fs;
148	retval = io_channel_alloc_buf(fs->io, -SUPERBLOCK_SIZE, &fs->super);
149	if (retval)
150		goto cleanup;
151	if (flags & EXT2_FLAG_IMAGE_FILE) {
152		retval = ext2fs_get_mem(sizeof(struct ext2_image_hdr),
153					&fs->image_header);
154		if (retval)
155			goto cleanup;
156		retval = io_channel_read_blk(fs->io, 0,
157					     -(int)sizeof(struct ext2_image_hdr),
158					     fs->image_header);
159		if (retval)
160			goto cleanup;
161		if (fs->image_header->magic_number != EXT2_ET_MAGIC_E2IMAGE)
162			return EXT2_ET_MAGIC_E2IMAGE;
163		superblock = 1;
164		block_size = fs->image_header->fs_blocksize;
165	}
166
167	/*
168	 * If the user specifies a specific block # for the
169	 * superblock, then he/she must also specify the block size!
170	 * Otherwise, read the master superblock located at offset
171	 * SUPERBLOCK_OFFSET from the start of the partition.
172	 *
173	 * Note: we only save a backup copy of the superblock if we
174	 * are reading the superblock from the primary superblock location.
175	 */
176	if (superblock) {
177		if (!block_size) {
178			retval = EXT2_ET_INVALID_ARGUMENT;
179			goto cleanup;
180		}
181		io_channel_set_blksize(fs->io, block_size);
182		group_block = superblock;
183		fs->orig_super = 0;
184	} else {
185		io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
186		superblock = 1;
187		group_block = 0;
188		retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->orig_super);
189		if (retval)
190			goto cleanup;
191	}
192	retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
193				     fs->super);
194	if (retval)
195		goto cleanup;
196	if (fs->orig_super)
197		memcpy(fs->orig_super, fs->super, SUPERBLOCK_SIZE);
198
199	if (!(fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS)) {
200		retval = 0;
201		if (!ext2fs_verify_csum_type(fs, fs->super))
202			retval = EXT2_ET_UNKNOWN_CSUM;
203		if (!ext2fs_superblock_csum_verify(fs, fs->super))
204			retval = EXT2_ET_SB_CSUM_INVALID;
205		if (retval)
206			goto cleanup;
207	}
208
209#ifdef WORDS_BIGENDIAN
210	fs->flags |= EXT2_FLAG_SWAP_BYTES;
211	ext2fs_swap_super(fs->super);
212#else
213	if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
214		retval = EXT2_ET_UNIMPLEMENTED;
215		goto cleanup;
216	}
217#endif
218
219	if (fs->super->s_magic != EXT2_SUPER_MAGIC) {
220		retval = EXT2_ET_BAD_MAGIC;
221		goto cleanup;
222	}
223	if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
224		retval = EXT2_ET_REV_TOO_HIGH;
225		goto cleanup;
226	}
227
228	/*
229	 * Check for feature set incompatibility
230	 */
231	if (!(flags & EXT2_FLAG_FORCE)) {
232		features = fs->super->s_feature_incompat;
233#ifdef EXT2_LIB_SOFTSUPP_INCOMPAT
234		if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
235			features &= ~EXT2_LIB_SOFTSUPP_INCOMPAT;
236#endif
237		if (features & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
238			retval = EXT2_ET_UNSUPP_FEATURE;
239			goto cleanup;
240		}
241
242		features = fs->super->s_feature_ro_compat;
243#ifdef EXT2_LIB_SOFTSUPP_RO_COMPAT
244		if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
245			features &= ~EXT2_LIB_SOFTSUPP_RO_COMPAT;
246#endif
247		if ((flags & EXT2_FLAG_RW) &&
248		    (features & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
249			retval = EXT2_ET_RO_UNSUPP_FEATURE;
250			goto cleanup;
251		}
252
253		if (!(flags & EXT2_FLAG_JOURNAL_DEV_OK) &&
254		    (fs->super->s_feature_incompat &
255		     EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
256			retval = EXT2_ET_UNSUPP_FEATURE;
257			goto cleanup;
258		}
259	}
260
261	if ((fs->super->s_log_block_size + EXT2_MIN_BLOCK_LOG_SIZE) >
262	    EXT2_MAX_BLOCK_LOG_SIZE) {
263		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
264		goto cleanup;
265	}
266	if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
267					EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
268	    (fs->super->s_log_block_size != fs->super->s_log_cluster_size)) {
269		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
270		goto cleanup;
271	}
272	fs->fragsize = fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
273	if (EXT2_INODE_SIZE(fs->super) < EXT2_GOOD_OLD_INODE_SIZE) {
274		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
275		goto cleanup;
276	}
277
278	/* Enforce the block group descriptor size */
279	if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) {
280		if (fs->super->s_desc_size != EXT2_MIN_DESC_SIZE_64BIT) {
281			retval = EXT2_ET_BAD_DESC_SIZE;
282			goto cleanup;
283		}
284	} else {
285		if (fs->super->s_desc_size &&
286		    fs->super->s_desc_size != EXT2_MIN_DESC_SIZE) {
287			retval = EXT2_ET_BAD_DESC_SIZE;
288			goto cleanup;
289		}
290	}
291
292	fs->cluster_ratio_bits = fs->super->s_log_cluster_size -
293		fs->super->s_log_block_size;
294	if (EXT2_BLOCKS_PER_GROUP(fs->super) !=
295	    EXT2_CLUSTERS_PER_GROUP(fs->super) << fs->cluster_ratio_bits) {
296		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
297		goto cleanup;
298	}
299	fs->inode_blocks_per_group = ((EXT2_INODES_PER_GROUP(fs->super) *
300				       EXT2_INODE_SIZE(fs->super) +
301				       EXT2_BLOCK_SIZE(fs->super) - 1) /
302				      EXT2_BLOCK_SIZE(fs->super));
303	if (block_size) {
304		if (block_size != fs->blocksize) {
305			retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
306			goto cleanup;
307		}
308	}
309	/*
310	 * Set the blocksize to the filesystem's blocksize.
311	 */
312	io_channel_set_blksize(fs->io, fs->blocksize);
313
314	/*
315	 * If this is an external journal device, don't try to read
316	 * the group descriptors, because they're not there.
317	 */
318	if (fs->super->s_feature_incompat &
319	    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
320		fs->group_desc_count = 0;
321		*ret_fs = fs;
322		return 0;
323	}
324
325	if (EXT2_INODES_PER_GROUP(fs->super) == 0) {
326		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
327		goto cleanup;
328	}
329	/* Precompute the FS UUID to seed other checksums */
330	ext2fs_init_csum_seed(fs);
331
332	/*
333	 * Read group descriptors
334	 */
335	blocks_per_group = EXT2_BLOCKS_PER_GROUP(fs->super);
336	if (blocks_per_group == 0 ||
337	    blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(fs->super) ||
338	    fs->inode_blocks_per_group > EXT2_MAX_INODES_PER_GROUP(fs->super) ||
339           EXT2_DESC_PER_BLOCK(fs->super) == 0 ||
340           fs->super->s_first_data_block >= ext2fs_blocks_count(fs->super)) {
341		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
342		goto cleanup;
343	}
344	fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
345						 fs->super->s_first_data_block,
346						 blocks_per_group);
347	if (fs->group_desc_count * EXT2_INODES_PER_GROUP(fs->super) !=
348	    fs->super->s_inodes_count) {
349		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
350		goto cleanup;
351	}
352	fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
353					  EXT2_DESC_PER_BLOCK(fs->super));
354	retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
355				&fs->group_desc);
356	if (retval)
357		goto cleanup;
358	if (!group_block)
359		group_block = fs->super->s_first_data_block;
360	if (group_block == 0 && fs->blocksize == 1024)
361		group_block = 1; /* Deal with 1024 blocksize && bigalloc */
362	dest = (char *) fs->group_desc;
363#ifdef WORDS_BIGENDIAN
364	groups_per_block = EXT2_DESC_PER_BLOCK(fs->super);
365#endif
366	if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
367		first_meta_bg = fs->super->s_first_meta_bg;
368	else
369		first_meta_bg = fs->desc_blocks;
370	if (first_meta_bg) {
371		retval = io_channel_read_blk(fs->io, group_block+1,
372					     first_meta_bg, dest);
373		if (retval)
374			goto cleanup;
375#ifdef WORDS_BIGENDIAN
376		gdp = (struct ext2_group_desc *) dest;
377		for (j=0; j < groups_per_block*first_meta_bg; j++) {
378			gdp = ext2fs_group_desc(fs, fs->group_desc, j);
379			ext2fs_swap_group_desc2(fs, gdp);
380		}
381#endif
382		dest += fs->blocksize*first_meta_bg;
383	}
384	for (i=first_meta_bg ; i < fs->desc_blocks; i++) {
385		blk = ext2fs_descriptor_block_loc2(fs, group_block, i);
386		retval = io_channel_read_blk64(fs->io, blk, 1, dest);
387		if (retval)
388			goto cleanup;
389#ifdef WORDS_BIGENDIAN
390		for (j=0; j < groups_per_block; j++) {
391			gdp = ext2fs_group_desc(fs, fs->group_desc,
392						i * groups_per_block + j);
393			ext2fs_swap_group_desc2(fs, gdp);
394		}
395#endif
396		dest += fs->blocksize;
397	}
398
399	fs->stride = fs->super->s_raid_stride;
400
401	/*
402	 * If recovery is from backup superblock, Clear _UNININT flags &
403	 * reset bg_itable_unused to zero
404	 */
405	if (superblock > 1 && ext2fs_has_group_desc_csum(fs)) {
406		dgrp_t group;
407
408		for (group = 0; group < fs->group_desc_count; group++) {
409			ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
410			ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
411			ext2fs_bg_itable_unused_set(fs, group, 0);
412			/* The checksum will be reset later, but fix it here
413			 * anyway to avoid printing a lot of spurious errors. */
414			ext2fs_group_desc_csum_set(fs, group);
415		}
416		if (fs->flags & EXT2_FLAG_RW)
417			ext2fs_mark_super_dirty(fs);
418	}
419
420	if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) &&
421	    !(flags & EXT2_FLAG_SKIP_MMP) &&
422	    (flags & (EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE))) {
423		retval = ext2fs_mmp_start(fs);
424		if (retval) {
425			fs->flags |= EXT2_FLAG_SKIP_MMP; /* just do cleanup */
426			ext2fs_mmp_stop(fs);
427			goto cleanup;
428		}
429	}
430
431	fs->flags &= ~EXT2_FLAG_NOFREE_ON_ERROR;
432	*ret_fs = fs;
433
434	return 0;
435cleanup:
436	if (flags & EXT2_FLAG_NOFREE_ON_ERROR)
437		*ret_fs = fs;
438	else
439		ext2fs_free(fs);
440	return retval;
441}
442
443/*
444 * Set/get the filesystem data I/O channel.
445 *
446 * These functions are only valid if EXT2_FLAG_IMAGE_FILE is true.
447 */
448errcode_t ext2fs_get_data_io(ext2_filsys fs, io_channel *old_io)
449{
450	if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
451		return EXT2_ET_NOT_IMAGE_FILE;
452	if (old_io) {
453		*old_io = (fs->image_io == fs->io) ? 0 : fs->io;
454	}
455	return 0;
456}
457
458errcode_t ext2fs_set_data_io(ext2_filsys fs, io_channel new_io)
459{
460	if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
461		return EXT2_ET_NOT_IMAGE_FILE;
462	fs->io = new_io ? new_io : fs->image_io;
463	return 0;
464}
465
466errcode_t ext2fs_rewrite_to_io(ext2_filsys fs, io_channel new_io)
467{
468	if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
469		return EXT2_ET_NOT_IMAGE_FILE;
470	fs->io = fs->image_io = new_io;
471	fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_RW |
472		EXT2_FLAG_BB_DIRTY | EXT2_FLAG_IB_DIRTY;
473	fs->flags &= ~EXT2_FLAG_IMAGE_FILE;
474	return 0;
475}
476