openfs.c revision 5f7c04972fefa8198c34f231a9e8a5430705b4ab
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	groups_per_block, blocks_per_group, io_flags;
103	blk64_t		group_block, blk;
104	char		*dest, *cp;
105#ifdef WORDS_BIGENDIAN
106	struct ext2_group_desc *gdp;
107	int		j;
108#endif
109
110	EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);
111
112	retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
113	if (retval)
114		return retval;
115
116	memset(fs, 0, sizeof(struct struct_ext2_filsys));
117	fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
118	fs->flags = flags;
119	/* don't overwrite sb backups unless flag is explicitly cleared */
120	fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
121	fs->umask = 022;
122	retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name);
123	if (retval)
124		goto cleanup;
125	strcpy(fs->device_name, name);
126	cp = strchr(fs->device_name, '?');
127	if (!io_options && cp) {
128		*cp++ = 0;
129		io_options = cp;
130	}
131
132	io_flags = 0;
133	if (flags & EXT2_FLAG_RW)
134		io_flags |= IO_FLAG_RW;
135	if (flags & EXT2_FLAG_EXCLUSIVE)
136		io_flags |= IO_FLAG_EXCLUSIVE;
137	if (flags & EXT2_FLAG_DIRECT_IO)
138		io_flags |= IO_FLAG_DIRECT_IO;
139	retval = manager->open(fs->device_name, io_flags, &fs->io);
140	if (retval)
141		goto cleanup;
142	if (io_options &&
143	    (retval = io_channel_set_options(fs->io, io_options)))
144		goto cleanup;
145	fs->image_io = fs->io;
146	fs->io->app_data = fs;
147	retval = ext2fs_get_memalign(SUPERBLOCK_SIZE, 512, &fs->super);
148	if (retval)
149		goto cleanup;
150	if (flags & EXT2_FLAG_IMAGE_FILE) {
151		retval = ext2fs_get_mem(sizeof(struct ext2_image_hdr),
152					&fs->image_header);
153		if (retval)
154			goto cleanup;
155		retval = io_channel_read_blk(fs->io, 0,
156					     -(int)sizeof(struct ext2_image_hdr),
157					     fs->image_header);
158		if (retval)
159			goto cleanup;
160		if (fs->image_header->magic_number != EXT2_ET_MAGIC_E2IMAGE)
161			return EXT2_ET_MAGIC_E2IMAGE;
162		superblock = 1;
163		block_size = fs->image_header->fs_blocksize;
164	}
165
166	/*
167	 * If the user specifies a specific block # for the
168	 * superblock, then he/she must also specify the block size!
169	 * Otherwise, read the master superblock located at offset
170	 * SUPERBLOCK_OFFSET from the start of the partition.
171	 *
172	 * Note: we only save a backup copy of the superblock if we
173	 * are reading the superblock from the primary superblock location.
174	 */
175	if (superblock) {
176		if (!block_size) {
177			retval = EXT2_ET_INVALID_ARGUMENT;
178			goto cleanup;
179		}
180		io_channel_set_blksize(fs->io, block_size);
181		group_block = superblock;
182		fs->orig_super = 0;
183	} else {
184		io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
185		superblock = 1;
186		group_block = 0;
187		retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->orig_super);
188		if (retval)
189			goto cleanup;
190	}
191	retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
192				     fs->super);
193	if (retval)
194		goto cleanup;
195	if (fs->orig_super)
196		memcpy(fs->orig_super, fs->super, SUPERBLOCK_SIZE);
197
198#ifdef WORDS_BIGENDIAN
199	fs->flags |= EXT2_FLAG_SWAP_BYTES;
200	ext2fs_swap_super(fs->super);
201#else
202	if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
203		retval = EXT2_ET_UNIMPLEMENTED;
204		goto cleanup;
205	}
206#endif
207
208	if (fs->super->s_magic != EXT2_SUPER_MAGIC) {
209		retval = EXT2_ET_BAD_MAGIC;
210		goto cleanup;
211	}
212	if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
213		retval = EXT2_ET_REV_TOO_HIGH;
214		goto cleanup;
215	}
216
217	/*
218	 * Check for feature set incompatibility
219	 */
220	if (!(flags & EXT2_FLAG_FORCE)) {
221		features = fs->super->s_feature_incompat;
222#ifdef EXT2_LIB_SOFTSUPP_INCOMPAT
223		if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
224			features &= ~EXT2_LIB_SOFTSUPP_INCOMPAT;
225#endif
226		if (features & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
227			retval = EXT2_ET_UNSUPP_FEATURE;
228			goto cleanup;
229		}
230
231		features = fs->super->s_feature_ro_compat;
232#ifdef EXT2_LIB_SOFTSUPP_RO_COMPAT
233		if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
234			features &= ~EXT2_LIB_SOFTSUPP_RO_COMPAT;
235#endif
236		if ((flags & EXT2_FLAG_RW) &&
237		    (features & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
238			retval = EXT2_ET_RO_UNSUPP_FEATURE;
239			goto cleanup;
240		}
241
242		if (!(flags & EXT2_FLAG_JOURNAL_DEV_OK) &&
243		    (fs->super->s_feature_incompat &
244		     EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
245			retval = EXT2_ET_UNSUPP_FEATURE;
246			goto cleanup;
247		}
248	}
249
250	if ((fs->super->s_log_block_size + EXT2_MIN_BLOCK_LOG_SIZE) >
251	    EXT2_MAX_BLOCK_LOG_SIZE) {
252		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
253		goto cleanup;
254	}
255	if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
256					EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
257	    (fs->super->s_log_block_size != fs->super->s_log_cluster_size)) {
258		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
259		goto cleanup;
260	}
261	fs->fragsize = fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
262	if (EXT2_INODE_SIZE(fs->super) < EXT2_GOOD_OLD_INODE_SIZE) {
263		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
264		goto cleanup;
265	}
266	fs->cluster_ratio_bits = fs->super->s_log_cluster_size -
267		fs->super->s_log_block_size;
268	if (EXT2_BLOCKS_PER_GROUP(fs->super) !=
269	    EXT2_CLUSTERS_PER_GROUP(fs->super) << fs->cluster_ratio_bits) {
270		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
271		goto cleanup;
272	}
273	fs->inode_blocks_per_group = ((EXT2_INODES_PER_GROUP(fs->super) *
274				       EXT2_INODE_SIZE(fs->super) +
275				       EXT2_BLOCK_SIZE(fs->super) - 1) /
276				      EXT2_BLOCK_SIZE(fs->super));
277	if (block_size) {
278		if (block_size != fs->blocksize) {
279			retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
280			goto cleanup;
281		}
282	}
283	/*
284	 * Set the blocksize to the filesystem's blocksize.
285	 */
286	io_channel_set_blksize(fs->io, fs->blocksize);
287
288	/*
289	 * If this is an external journal device, don't try to read
290	 * the group descriptors, because they're not there.
291	 */
292	if (fs->super->s_feature_incompat &
293	    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
294		fs->group_desc_count = 0;
295		*ret_fs = fs;
296		return 0;
297	}
298
299	if (EXT2_INODES_PER_GROUP(fs->super) == 0) {
300		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
301		goto cleanup;
302	}
303
304	/*
305	 * Read group descriptors
306	 */
307	blocks_per_group = EXT2_BLOCKS_PER_GROUP(fs->super);
308	if (blocks_per_group == 0 ||
309	    blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(fs->super) ||
310	    fs->inode_blocks_per_group > EXT2_MAX_INODES_PER_GROUP(fs->super) ||
311           EXT2_DESC_PER_BLOCK(fs->super) == 0 ||
312           fs->super->s_first_data_block >= ext2fs_blocks_count(fs->super)) {
313		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
314		goto cleanup;
315	}
316	fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
317						 fs->super->s_first_data_block,
318						 blocks_per_group);
319	if (fs->group_desc_count * EXT2_INODES_PER_GROUP(fs->super) !=
320	    fs->super->s_inodes_count) {
321		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
322		goto cleanup;
323	}
324	fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
325					  EXT2_DESC_PER_BLOCK(fs->super));
326	retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
327				&fs->group_desc);
328	if (retval)
329		goto cleanup;
330	if (!group_block)
331		group_block = fs->super->s_first_data_block;
332	if (group_block == 0 && fs->blocksize == 1024)
333		group_block = 1; /* Deal with 1024 blocksize && bigalloc */
334	dest = (char *) fs->group_desc;
335	groups_per_block = EXT2_DESC_PER_BLOCK(fs->super);
336	if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
337		first_meta_bg = fs->super->s_first_meta_bg;
338	else
339		first_meta_bg = fs->desc_blocks;
340	if (first_meta_bg) {
341		retval = io_channel_read_blk(fs->io, group_block+1,
342					     first_meta_bg, dest);
343		if (retval)
344			goto cleanup;
345#ifdef WORDS_BIGENDIAN
346		gdp = (struct ext2_group_desc *) dest;
347		for (j=0; j < groups_per_block*first_meta_bg; j++) {
348			gdp = ext2fs_group_desc(fs, fs->group_desc, j);
349			ext2fs_swap_group_desc2(fs, gdp);
350		}
351#endif
352		dest += fs->blocksize*first_meta_bg;
353	}
354	for (i=first_meta_bg ; i < fs->desc_blocks; i++) {
355		blk = ext2fs_descriptor_block_loc2(fs, group_block, i);
356		retval = io_channel_read_blk64(fs->io, blk, 1, dest);
357		if (retval)
358			goto cleanup;
359#ifdef WORDS_BIGENDIAN
360		for (j=0; j < groups_per_block; j++) {
361			gdp = ext2fs_group_desc(fs, fs->group_desc,
362						i * groups_per_block + j);
363			ext2fs_swap_group_desc2(fs, gdp);
364		}
365#endif
366		dest += fs->blocksize;
367	}
368
369	fs->stride = fs->super->s_raid_stride;
370
371	/*
372	 * If recovery is from backup superblock, Clear _UNININT flags &
373	 * reset bg_itable_unused to zero
374	 */
375	if (superblock > 1 && EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
376					EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
377		dgrp_t group;
378
379		for (group = 0; group < fs->group_desc_count; group++) {
380			ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
381			ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
382			ext2fs_bg_itable_unused_set(fs, group, 0);
383			/* The checksum will be reset later, but fix it here
384			 * anyway to avoid printing a lot of spurious errors. */
385			ext2fs_group_desc_csum_set(fs, group);
386		}
387		if (fs->flags & EXT2_FLAG_RW)
388			ext2fs_mark_super_dirty(fs);
389	}
390
391	fs->flags &= ~EXT2_FLAG_NOFREE_ON_ERROR;
392	*ret_fs = fs;
393
394	if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) &&
395	    !(flags & EXT2_FLAG_SKIP_MMP) &&
396	    (flags & (EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE))) {
397		retval = ext2fs_mmp_start(fs);
398		if (retval) {
399			fs->flags |= EXT2_FLAG_SKIP_MMP; /* just do cleanup */
400			ext2fs_mmp_stop(fs);
401			goto cleanup;
402		}
403	}
404
405	return 0;
406cleanup:
407	if (flags & EXT2_FLAG_NOFREE_ON_ERROR)
408		*ret_fs = fs;
409	else
410		ext2fs_free(fs);
411	return retval;
412}
413
414/*
415 * Set/get the filesystem data I/O channel.
416 *
417 * These functions are only valid if EXT2_FLAG_IMAGE_FILE is true.
418 */
419errcode_t ext2fs_get_data_io(ext2_filsys fs, io_channel *old_io)
420{
421	if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
422		return EXT2_ET_NOT_IMAGE_FILE;
423	if (old_io) {
424		*old_io = (fs->image_io == fs->io) ? 0 : fs->io;
425	}
426	return 0;
427}
428
429errcode_t ext2fs_set_data_io(ext2_filsys fs, io_channel new_io)
430{
431	if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
432		return EXT2_ET_NOT_IMAGE_FILE;
433	fs->io = new_io ? new_io : fs->image_io;
434	return 0;
435}
436
437errcode_t ext2fs_rewrite_to_io(ext2_filsys fs, io_channel new_io)
438{
439	if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
440		return EXT2_ET_NOT_IMAGE_FILE;
441	fs->io = fs->image_io = new_io;
442	fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_RW |
443		EXT2_FLAG_BB_DIRTY | EXT2_FLAG_IB_DIRTY;
444	fs->flags &= ~EXT2_FLAG_IMAGE_FILE;
445	return 0;
446}
447