openfs.c revision 51663e0d2aa2958b5ef590299b2018a2ae01d256
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, group_zero_adjust = 0;
41	blk64_t	ret_blk;
42
43	/*
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.
46	 */
47	if (i == 0 && fs->blocksize == 1024 && EXT2FS_CLUSTER_RATIO(fs) > 1)
48		group_zero_adjust = 1;
49
50	if (!ext2fs_has_feature_meta_bg(fs->super) ||
51	    (i < fs->super->s_first_meta_bg))
52		return group_block + i + 1 + group_zero_adjust;
53
54	bg = EXT2_DESC_PER_BLOCK(fs->super) * i;
55	if (ext2fs_bg_has_super(fs, bg))
56		has_super = 1;
57	ret_blk = ext2fs_group_first_block2(fs, bg);
58	/*
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.
65	 */
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;
70
71		/*
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).
75		 */
76		if (ext2fs_bg_has_super(fs, bg + 1))
77			has_super = 1;
78		else
79			has_super = 0;
80	}
81	return ret_blk + has_super + group_zero_adjust;
82}
83
84blk_t ext2fs_descriptor_block_loc(ext2_filsys fs, blk_t group_block, dgrp_t i)
85{
86	return ext2fs_descriptor_block_loc2(fs, group_block, i);
87}
88
89errcode_t ext2fs_open(const char *name, int flags, int superblock,
90		      unsigned int block_size, io_manager manager,
91		      ext2_filsys *ret_fs)
92{
93	return ext2fs_open2(name, 0, flags, superblock, block_size,
94			    manager, ret_fs);
95}
96
97/*
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.
100 *
101 * Valid flags for ext2fs_open()
102 *
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
109 *				filesystems)
110 */
111errcode_t ext2fs_open2(const char *name, const char *io_options,
112		       int flags, int superblock,
113		       unsigned int block_size, io_manager manager,
114		       ext2_filsys *ret_fs)
115{
116	ext2_filsys	fs;
117	errcode_t	retval;
118	unsigned long	i, first_meta_bg;
119	__u32		features;
120	unsigned int	blocks_per_group, io_flags;
121	blk64_t		group_block, blk;
122	char		*dest, *cp;
123	int		group_zero_adjust = 0;
124#ifdef WORDS_BIGENDIAN
125	unsigned int	groups_per_block;
126	struct ext2_group_desc *gdp;
127	int		j;
128#endif
129	char		*time_env;
130
131	EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);
132
133	retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
134	if (retval)
135		return retval;
136
137	memset(fs, 0, sizeof(struct struct_ext2_filsys));
138	fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
139	fs->flags = flags;
140	/* don't overwrite sb backups unless flag is explicitly cleared */
141	fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
142	fs->umask = 022;
143
144	time_env = getenv("E2FSPROGS_FAKE_TIME");
145	if (time_env)
146		fs->now = strtoul(time_env, NULL, 0);
147
148	retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name);
149	if (retval)
150		goto cleanup;
151	strcpy(fs->device_name, name);
152	cp = strchr(fs->device_name, '?');
153	if (!io_options && cp) {
154		*cp++ = 0;
155		io_options = cp;
156	}
157
158	io_flags = 0;
159	if (flags & EXT2_FLAG_RW)
160		io_flags |= IO_FLAG_RW;
161	if (flags & EXT2_FLAG_EXCLUSIVE)
162		io_flags |= IO_FLAG_EXCLUSIVE;
163	if (flags & EXT2_FLAG_DIRECT_IO)
164		io_flags |= IO_FLAG_DIRECT_IO;
165	retval = manager->open(fs->device_name, io_flags, &fs->io);
166	if (retval)
167		goto cleanup;
168	if (io_options &&
169	    (retval = io_channel_set_options(fs->io, io_options)))
170		goto cleanup;
171	fs->image_io = fs->io;
172	fs->io->app_data = fs;
173	retval = io_channel_alloc_buf(fs->io, -SUPERBLOCK_SIZE, &fs->super);
174	if (retval)
175		goto cleanup;
176	if (flags & EXT2_FLAG_IMAGE_FILE) {
177		retval = ext2fs_get_mem(sizeof(struct ext2_image_hdr),
178					&fs->image_header);
179		if (retval)
180			goto cleanup;
181		retval = io_channel_read_blk(fs->io, 0,
182					     -(int)sizeof(struct ext2_image_hdr),
183					     fs->image_header);
184		if (retval)
185			goto cleanup;
186		if (fs->image_header->magic_number != EXT2_ET_MAGIC_E2IMAGE)
187			return EXT2_ET_MAGIC_E2IMAGE;
188		superblock = 1;
189		block_size = fs->image_header->fs_blocksize;
190	}
191
192	/*
193	 * If the user specifies a specific block # for the
194	 * superblock, then he/she must also specify the block size!
195	 * Otherwise, read the master superblock located at offset
196	 * SUPERBLOCK_OFFSET from the start of the partition.
197	 *
198	 * Note: we only save a backup copy of the superblock if we
199	 * are reading the superblock from the primary superblock location.
200	 */
201	if (superblock) {
202		if (!block_size) {
203			retval = EXT2_ET_INVALID_ARGUMENT;
204			goto cleanup;
205		}
206		io_channel_set_blksize(fs->io, block_size);
207		group_block = superblock;
208		fs->orig_super = 0;
209	} else {
210		io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
211		superblock = 1;
212		group_block = 0;
213		retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->orig_super);
214		if (retval)
215			goto cleanup;
216	}
217	retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
218				     fs->super);
219	if (retval)
220		goto cleanup;
221	if (fs->orig_super)
222		memcpy(fs->orig_super, fs->super, SUPERBLOCK_SIZE);
223
224	if (!(fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS)) {
225		retval = 0;
226		if (!ext2fs_verify_csum_type(fs, fs->super))
227			retval = EXT2_ET_UNKNOWN_CSUM;
228		if (!ext2fs_superblock_csum_verify(fs, fs->super))
229			retval = EXT2_ET_SB_CSUM_INVALID;
230	}
231
232#ifdef WORDS_BIGENDIAN
233	fs->flags |= EXT2_FLAG_SWAP_BYTES;
234	ext2fs_swap_super(fs->super);
235#else
236	if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
237		retval = EXT2_ET_UNIMPLEMENTED;
238		goto cleanup;
239	}
240#endif
241
242	if (fs->super->s_magic != EXT2_SUPER_MAGIC)
243		retval = EXT2_ET_BAD_MAGIC;
244	if (retval)
245		goto cleanup;
246
247	if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
248		retval = EXT2_ET_REV_TOO_HIGH;
249		goto cleanup;
250	}
251
252	/*
253	 * Check for feature set incompatibility
254	 */
255	if (!(flags & EXT2_FLAG_FORCE)) {
256		features = fs->super->s_feature_incompat;
257#ifdef EXT2_LIB_SOFTSUPP_INCOMPAT
258		if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
259			features &= ~EXT2_LIB_SOFTSUPP_INCOMPAT;
260#endif
261		if (features & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
262			retval = EXT2_ET_UNSUPP_FEATURE;
263			goto cleanup;
264		}
265
266		features = fs->super->s_feature_ro_compat;
267#ifdef EXT2_LIB_SOFTSUPP_RO_COMPAT
268		if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
269			features &= ~EXT2_LIB_SOFTSUPP_RO_COMPAT;
270#endif
271		if ((flags & EXT2_FLAG_RW) &&
272		    (features & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
273			retval = EXT2_ET_RO_UNSUPP_FEATURE;
274			goto cleanup;
275		}
276
277		if (!(flags & EXT2_FLAG_JOURNAL_DEV_OK) &&
278		    ext2fs_has_feature_journal_dev(fs->super)) {
279			retval = EXT2_ET_UNSUPP_FEATURE;
280			goto cleanup;
281		}
282	}
283
284	if ((fs->super->s_log_block_size + EXT2_MIN_BLOCK_LOG_SIZE) >
285	    EXT2_MAX_BLOCK_LOG_SIZE) {
286		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
287		goto cleanup;
288	}
289
290	/*
291	 * bigalloc requires cluster-aware bitfield operations, which at the
292	 * moment means we need EXT2_FLAG_64BITS.
293	 */
294	if (ext2fs_has_feature_bigalloc(fs->super) &&
295	    !(flags & EXT2_FLAG_64BITS)) {
296		retval = EXT2_ET_CANT_USE_LEGACY_BITMAPS;
297		goto cleanup;
298	}
299
300	if (!ext2fs_has_feature_bigalloc(fs->super) &&
301	    (fs->super->s_log_block_size != fs->super->s_log_cluster_size)) {
302		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
303		goto cleanup;
304	}
305	fs->fragsize = fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
306	if (EXT2_INODE_SIZE(fs->super) < EXT2_GOOD_OLD_INODE_SIZE) {
307		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
308		goto cleanup;
309	}
310
311	/* Enforce the block group descriptor size */
312	if (ext2fs_has_feature_64bit(fs->super)) {
313		if (fs->super->s_desc_size < EXT2_MIN_DESC_SIZE_64BIT) {
314			retval = EXT2_ET_BAD_DESC_SIZE;
315			goto cleanup;
316		}
317	} else {
318		if (fs->super->s_desc_size &&
319		    fs->super->s_desc_size != EXT2_MIN_DESC_SIZE) {
320			retval = EXT2_ET_BAD_DESC_SIZE;
321			goto cleanup;
322		}
323	}
324
325	fs->cluster_ratio_bits = fs->super->s_log_cluster_size -
326		fs->super->s_log_block_size;
327	if (EXT2_BLOCKS_PER_GROUP(fs->super) !=
328	    EXT2_CLUSTERS_PER_GROUP(fs->super) << fs->cluster_ratio_bits) {
329		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
330		goto cleanup;
331	}
332	fs->inode_blocks_per_group = ((EXT2_INODES_PER_GROUP(fs->super) *
333				       EXT2_INODE_SIZE(fs->super) +
334				       EXT2_BLOCK_SIZE(fs->super) - 1) /
335				      EXT2_BLOCK_SIZE(fs->super));
336	if (block_size) {
337		if (block_size != fs->blocksize) {
338			retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
339			goto cleanup;
340		}
341	}
342	/*
343	 * Set the blocksize to the filesystem's blocksize.
344	 */
345	io_channel_set_blksize(fs->io, fs->blocksize);
346
347	/*
348	 * If this is an external journal device, don't try to read
349	 * the group descriptors, because they're not there.
350	 */
351	if (ext2fs_has_feature_journal_dev(fs->super)) {
352		fs->group_desc_count = 0;
353		*ret_fs = fs;
354		return 0;
355	}
356
357	if (EXT2_INODES_PER_GROUP(fs->super) == 0) {
358		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
359		goto cleanup;
360	}
361	/* Precompute the FS UUID to seed other checksums */
362	ext2fs_init_csum_seed(fs);
363
364	/*
365	 * Read group descriptors
366	 */
367	blocks_per_group = EXT2_BLOCKS_PER_GROUP(fs->super);
368	if (blocks_per_group == 0 ||
369	    blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(fs->super) ||
370	    fs->inode_blocks_per_group > EXT2_MAX_INODES_PER_GROUP(fs->super) ||
371           EXT2_DESC_PER_BLOCK(fs->super) == 0 ||
372           fs->super->s_first_data_block >= ext2fs_blocks_count(fs->super)) {
373		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
374		goto cleanup;
375	}
376	fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
377						 fs->super->s_first_data_block,
378						 blocks_per_group);
379	if (fs->group_desc_count * EXT2_INODES_PER_GROUP(fs->super) !=
380	    fs->super->s_inodes_count) {
381		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
382		goto cleanup;
383	}
384	fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
385					  EXT2_DESC_PER_BLOCK(fs->super));
386	retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
387				&fs->group_desc);
388	if (retval)
389		goto cleanup;
390	if (!group_block)
391		group_block = fs->super->s_first_data_block;
392	/*
393	 * On a FS with a 1K blocksize, block 0 is reserved for bootloaders
394	 * so we must increment block numbers to any group 0 items.
395	 *
396	 * However, we cannot touch group_block directly because in the meta_bg
397	 * case, the ext2fs_descriptor_block_loc2() function will interpret
398	 * group_block != s_first_data_block to mean that we want to access the
399	 * backup group descriptors.  This is not what we want if the caller
400	 * set superblock == 0 (i.e. auto-detect the superblock), which is
401	 * what's going on here.
402	 */
403	if (group_block == 0 && fs->blocksize == 1024)
404		group_zero_adjust = 1;
405	dest = (char *) fs->group_desc;
406#ifdef WORDS_BIGENDIAN
407	groups_per_block = EXT2_DESC_PER_BLOCK(fs->super);
408#endif
409	if (ext2fs_has_feature_meta_bg(fs->super)) {
410		first_meta_bg = fs->super->s_first_meta_bg;
411		if (first_meta_bg > fs->desc_blocks)
412			first_meta_bg = fs->desc_blocks;
413	} else
414		first_meta_bg = fs->desc_blocks;
415	if (first_meta_bg) {
416		retval = io_channel_read_blk(fs->io, group_block +
417					     group_zero_adjust + 1,
418					     first_meta_bg, dest);
419		if (retval)
420			goto cleanup;
421#ifdef WORDS_BIGENDIAN
422		gdp = (struct ext2_group_desc *) dest;
423		for (j=0; j < groups_per_block*first_meta_bg; j++) {
424			gdp = ext2fs_group_desc(fs, fs->group_desc, j);
425			ext2fs_swap_group_desc2(fs, gdp);
426		}
427#endif
428		dest += fs->blocksize*first_meta_bg;
429	}
430	for (i=first_meta_bg ; i < fs->desc_blocks; i++) {
431		blk = ext2fs_descriptor_block_loc2(fs, group_block, i);
432		retval = io_channel_read_blk64(fs->io, blk, 1, dest);
433		if (retval)
434			goto cleanup;
435#ifdef WORDS_BIGENDIAN
436		for (j=0; j < groups_per_block; j++) {
437			gdp = ext2fs_group_desc(fs, fs->group_desc,
438						i * groups_per_block + j);
439			ext2fs_swap_group_desc2(fs, gdp);
440		}
441#endif
442		dest += fs->blocksize;
443	}
444
445	fs->stride = fs->super->s_raid_stride;
446
447	/*
448	 * If recovery is from backup superblock, Clear _UNININT flags &
449	 * reset bg_itable_unused to zero
450	 */
451	if (superblock > 1 && ext2fs_has_group_desc_csum(fs)) {
452		dgrp_t group;
453
454		for (group = 0; group < fs->group_desc_count; group++) {
455			ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
456			ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
457			ext2fs_bg_itable_unused_set(fs, group, 0);
458			/* The checksum will be reset later, but fix it here
459			 * anyway to avoid printing a lot of spurious errors. */
460			ext2fs_group_desc_csum_set(fs, group);
461		}
462		if (fs->flags & EXT2_FLAG_RW)
463			ext2fs_mark_super_dirty(fs);
464	}
465
466	if (ext2fs_has_feature_mmp(fs->super) &&
467	    !(flags & EXT2_FLAG_SKIP_MMP) &&
468	    (flags & (EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE))) {
469		retval = ext2fs_mmp_start(fs);
470		if (retval) {
471			fs->flags |= EXT2_FLAG_SKIP_MMP; /* just do cleanup */
472			ext2fs_mmp_stop(fs);
473			goto cleanup;
474		}
475	}
476
477	fs->flags &= ~EXT2_FLAG_NOFREE_ON_ERROR;
478	*ret_fs = fs;
479
480	return 0;
481cleanup:
482	if (!(flags & EXT2_FLAG_NOFREE_ON_ERROR)) {
483		ext2fs_free(fs);
484		fs = NULL;
485	}
486	*ret_fs = fs;
487	return retval;
488}
489
490/*
491 * Set/get the filesystem data I/O channel.
492 *
493 * These functions are only valid if EXT2_FLAG_IMAGE_FILE is true.
494 */
495errcode_t ext2fs_get_data_io(ext2_filsys fs, io_channel *old_io)
496{
497	if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
498		return EXT2_ET_NOT_IMAGE_FILE;
499	if (old_io) {
500		*old_io = (fs->image_io == fs->io) ? 0 : fs->io;
501	}
502	return 0;
503}
504
505errcode_t ext2fs_set_data_io(ext2_filsys fs, io_channel new_io)
506{
507	if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
508		return EXT2_ET_NOT_IMAGE_FILE;
509	fs->io = new_io ? new_io : fs->image_io;
510	return 0;
511}
512
513errcode_t ext2fs_rewrite_to_io(ext2_filsys fs, io_channel new_io)
514{
515	errcode_t err;
516
517	if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
518		return EXT2_ET_NOT_IMAGE_FILE;
519	err = io_channel_set_blksize(new_io, fs->blocksize);
520	if (err)
521		return err;
522	if ((new_io == fs->image_io) || (new_io == fs->io))
523		return 0;
524	if ((fs->image_io != fs->io) &&
525	    fs->image_io)
526		io_channel_close(fs->image_io);
527	if (fs->io)
528		io_channel_close(fs->io);
529	fs->io = fs->image_io = new_io;
530	fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_RW |
531		EXT2_FLAG_BB_DIRTY | EXT2_FLAG_IB_DIRTY;
532	fs->flags &= ~EXT2_FLAG_IMAGE_FILE;
533	return 0;
534}
535