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 <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#ifdef HAVE_ERRNO_H
26#include <errno.h>
27#endif
28
29#include "ext2_fs.h"
30
31
32#include "ext2fs.h"
33#include "e2image.h"
34
35blk64_t ext2fs_descriptor_block_loc2(ext2_filsys fs, blk64_t group_block,
36				     dgrp_t i)
37{
38	int	bg;
39	int	has_super = 0;
40	blk64_t	ret_blk;
41
42	if (!(fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) ||
43	    (i < fs->super->s_first_meta_bg))
44		return (group_block + i + 1);
45
46	bg = EXT2_DESC_PER_BLOCK(fs->super) * i;
47	if (ext2fs_bg_has_super(fs, bg))
48		has_super = 1;
49	ret_blk = ext2fs_group_first_block2(fs, bg) + has_super;
50	/*
51	 * If group_block is not the normal value, we're trying to use
52	 * the backup group descriptors and superblock --- so use the
53	 * alternate location of the second block group in the
54	 * metablock group.  Ideally we should be testing each bg
55	 * descriptor block individually for correctness, but we don't
56	 * have the infrastructure in place to do that.
57	 */
58	if (group_block != fs->super->s_first_data_block &&
59	    ((ret_blk + fs->super->s_blocks_per_group) <
60	     ext2fs_blocks_count(fs->super)))
61		ret_blk += fs->super->s_blocks_per_group;
62	return ret_blk;
63}
64
65blk_t ext2fs_descriptor_block_loc(ext2_filsys fs, blk_t group_block, dgrp_t i)
66{
67	return ext2fs_descriptor_block_loc2(fs, group_block, i);
68}
69
70errcode_t ext2fs_open(const char *name, int flags, int superblock,
71		      unsigned int block_size, io_manager manager,
72		      ext2_filsys *ret_fs)
73{
74	return ext2fs_open2(name, 0, flags, superblock, block_size,
75			    manager, ret_fs);
76}
77
78/*
79 *  Note: if superblock is non-zero, block-size must also be non-zero.
80 * 	Superblock and block_size can be zero to use the default size.
81 *
82 * Valid flags for ext2fs_open()
83 *
84 * 	EXT2_FLAG_RW	- Open the filesystem for read/write.
85 * 	EXT2_FLAG_FORCE - Open the filesystem even if some of the
86 *				features aren't supported.
87 *	EXT2_FLAG_JOURNAL_DEV_OK - Open an ext3 journal device
88 *	EXT2_FLAG_SKIP_MMP - Open without multi-mount protection check.
89 *	EXT2_FLAG_64BITS - Allow 64-bit bitfields (needed for large
90 *				filesystems)
91 */
92errcode_t ext2fs_open2(const char *name, const char *io_options,
93		       int flags, int superblock,
94		       unsigned int block_size, io_manager manager,
95		       ext2_filsys *ret_fs)
96{
97	ext2_filsys	fs;
98	errcode_t	retval;
99	unsigned long	i, first_meta_bg;
100	__u32		features;
101	unsigned int	blocks_per_group, io_flags;
102	blk64_t		group_block, blk;
103	char		*dest, *cp;
104#ifdef WORDS_BIGENDIAN
105	unsigned int	groups_per_block;
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 = io_channel_alloc_buf(fs->io, -SUPERBLOCK_SIZE, &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
256	/*
257	 * bigalloc requires cluster-aware bitfield operations, which at the
258	 * moment means we need EXT2_FLAG_64BITS.
259	 */
260	if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
261				       EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
262	    !(flags & EXT2_FLAG_64BITS)) {
263		retval = EXT2_ET_CANT_USE_LEGACY_BITMAPS;
264		goto cleanup;
265	}
266
267	if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
268					EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
269	    (fs->super->s_log_block_size != fs->super->s_log_cluster_size)) {
270		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
271		goto cleanup;
272	}
273	fs->fragsize = fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
274	if (EXT2_INODE_SIZE(fs->super) < EXT2_GOOD_OLD_INODE_SIZE) {
275		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
276		goto cleanup;
277	}
278	fs->cluster_ratio_bits = fs->super->s_log_cluster_size -
279		fs->super->s_log_block_size;
280	if (EXT2_BLOCKS_PER_GROUP(fs->super) !=
281	    EXT2_CLUSTERS_PER_GROUP(fs->super) << fs->cluster_ratio_bits) {
282		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
283		goto cleanup;
284	}
285	fs->inode_blocks_per_group = ((EXT2_INODES_PER_GROUP(fs->super) *
286				       EXT2_INODE_SIZE(fs->super) +
287				       EXT2_BLOCK_SIZE(fs->super) - 1) /
288				      EXT2_BLOCK_SIZE(fs->super));
289	if (block_size) {
290		if (block_size != fs->blocksize) {
291			retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
292			goto cleanup;
293		}
294	}
295	/*
296	 * Set the blocksize to the filesystem's blocksize.
297	 */
298	io_channel_set_blksize(fs->io, fs->blocksize);
299
300	/*
301	 * If this is an external journal device, don't try to read
302	 * the group descriptors, because they're not there.
303	 */
304	if (fs->super->s_feature_incompat &
305	    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
306		fs->group_desc_count = 0;
307		*ret_fs = fs;
308		return 0;
309	}
310
311	if (EXT2_INODES_PER_GROUP(fs->super) == 0) {
312		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
313		goto cleanup;
314	}
315
316	/*
317	 * Read group descriptors
318	 */
319	blocks_per_group = EXT2_BLOCKS_PER_GROUP(fs->super);
320	if (blocks_per_group == 0 ||
321	    blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(fs->super) ||
322	    fs->inode_blocks_per_group > EXT2_MAX_INODES_PER_GROUP(fs->super) ||
323           EXT2_DESC_PER_BLOCK(fs->super) == 0 ||
324           fs->super->s_first_data_block >= ext2fs_blocks_count(fs->super)) {
325		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
326		goto cleanup;
327	}
328	fs->group_desc_count = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
329						 fs->super->s_first_data_block,
330						 blocks_per_group);
331	if (fs->group_desc_count * EXT2_INODES_PER_GROUP(fs->super) !=
332	    fs->super->s_inodes_count) {
333		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
334		goto cleanup;
335	}
336	fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
337					  EXT2_DESC_PER_BLOCK(fs->super));
338	retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
339				&fs->group_desc);
340	if (retval)
341		goto cleanup;
342	if (!group_block)
343		group_block = fs->super->s_first_data_block;
344	if (group_block == 0 && fs->blocksize == 1024)
345		group_block = 1; /* Deal with 1024 blocksize && bigalloc */
346	dest = (char *) fs->group_desc;
347#ifdef WORDS_BIGENDIAN
348	groups_per_block = EXT2_DESC_PER_BLOCK(fs->super);
349#endif
350	if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
351		first_meta_bg = fs->super->s_first_meta_bg;
352	else
353		first_meta_bg = fs->desc_blocks;
354	if (first_meta_bg) {
355		retval = io_channel_read_blk(fs->io, group_block+1,
356					     first_meta_bg, dest);
357		if (retval)
358			goto cleanup;
359#ifdef WORDS_BIGENDIAN
360		gdp = (struct ext2_group_desc *) dest;
361		for (j=0; j < groups_per_block*first_meta_bg; j++) {
362			gdp = ext2fs_group_desc(fs, fs->group_desc, j);
363			ext2fs_swap_group_desc2(fs, gdp);
364		}
365#endif
366		dest += fs->blocksize*first_meta_bg;
367	}
368	for (i=first_meta_bg ; i < fs->desc_blocks; i++) {
369		blk = ext2fs_descriptor_block_loc2(fs, group_block, i);
370		retval = io_channel_read_blk64(fs->io, blk, 1, dest);
371		if (retval)
372			goto cleanup;
373#ifdef WORDS_BIGENDIAN
374		for (j=0; j < groups_per_block; j++) {
375			gdp = ext2fs_group_desc(fs, fs->group_desc,
376						i * groups_per_block + j);
377			ext2fs_swap_group_desc2(fs, gdp);
378		}
379#endif
380		dest += fs->blocksize;
381	}
382
383	fs->stride = fs->super->s_raid_stride;
384
385	/*
386	 * If recovery is from backup superblock, Clear _UNININT flags &
387	 * reset bg_itable_unused to zero
388	 */
389	if (superblock > 1 && EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
390					EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
391		dgrp_t group;
392
393		for (group = 0; group < fs->group_desc_count; group++) {
394			ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
395			ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
396			ext2fs_bg_itable_unused_set(fs, group, 0);
397			/* The checksum will be reset later, but fix it here
398			 * anyway to avoid printing a lot of spurious errors. */
399			ext2fs_group_desc_csum_set(fs, group);
400		}
401		if (fs->flags & EXT2_FLAG_RW)
402			ext2fs_mark_super_dirty(fs);
403	}
404
405	if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) &&
406	    !(flags & EXT2_FLAG_SKIP_MMP) &&
407	    (flags & (EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE))) {
408		retval = ext2fs_mmp_start(fs);
409		if (retval) {
410			fs->flags |= EXT2_FLAG_SKIP_MMP; /* just do cleanup */
411			ext2fs_mmp_stop(fs);
412			goto cleanup;
413		}
414	}
415
416	fs->flags &= ~EXT2_FLAG_NOFREE_ON_ERROR;
417	*ret_fs = fs;
418
419	return 0;
420cleanup:
421	if (flags & EXT2_FLAG_NOFREE_ON_ERROR)
422		*ret_fs = fs;
423	else
424		ext2fs_free(fs);
425	return retval;
426}
427
428/*
429 * Set/get the filesystem data I/O channel.
430 *
431 * These functions are only valid if EXT2_FLAG_IMAGE_FILE is true.
432 */
433errcode_t ext2fs_get_data_io(ext2_filsys fs, io_channel *old_io)
434{
435	if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
436		return EXT2_ET_NOT_IMAGE_FILE;
437	if (old_io) {
438		*old_io = (fs->image_io == fs->io) ? 0 : fs->io;
439	}
440	return 0;
441}
442
443errcode_t ext2fs_set_data_io(ext2_filsys fs, io_channel new_io)
444{
445	if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
446		return EXT2_ET_NOT_IMAGE_FILE;
447	fs->io = new_io ? new_io : fs->image_io;
448	return 0;
449}
450
451errcode_t ext2fs_rewrite_to_io(ext2_filsys fs, io_channel new_io)
452{
453	errcode_t err;
454
455	if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
456		return EXT2_ET_NOT_IMAGE_FILE;
457	err = io_channel_set_blksize(new_io, fs->blocksize);
458	if (err)
459		return err;
460	if ((new_io == fs->image_io) || (new_io == fs->io))
461		return 0;
462	if ((fs->image_io != fs->io) &&
463	    fs->image_io)
464		io_channel_close(fs->image_io);
465	if (fs->io)
466		io_channel_close(fs->io);
467	fs->io = fs->image_io = new_io;
468	fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_RW |
469		EXT2_FLAG_BB_DIRTY | EXT2_FLAG_IB_DIRTY;
470	fs->flags &= ~EXT2_FLAG_IMAGE_FILE;
471	return 0;
472}
473