• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
26 #include "ext2_fs.h"
27 
28 
29 #include "ext2fs.h"
30 #include "e2image.h"
31 
ext2fs_descriptor_block_loc(ext2_filsys fs,blk_t group_block,dgrp_t i)32 blk_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 
ext2fs_open(const char * name,int flags,int superblock,unsigned int block_size,io_manager manager,ext2_filsys * ret_fs)61 errcode_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  */
ext2fs_open2(const char * name,const char * io_options,int flags,int superblock,unsigned int block_size,io_manager manager,ext2_filsys * ret_fs)80 errcode_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, first_meta_bg;
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 	if (flags & EXT2_FLAG_DIRECT_IO)
125 		io_flags |= IO_FLAG_DIRECT_IO;
126 	retval = manager->open(fs->device_name, io_flags, &fs->io);
127 	if (retval)
128 		goto cleanup;
129 	if (io_options &&
130 	    (retval = io_channel_set_options(fs->io, io_options)))
131 		goto cleanup;
132 	fs->image_io = fs->io;
133 	fs->io->app_data = fs;
134 	retval = ext2fs_get_memalign(SUPERBLOCK_SIZE, 512, &fs->super);
135 	if (retval)
136 		goto cleanup;
137 	if (flags & EXT2_FLAG_IMAGE_FILE) {
138 		retval = ext2fs_get_mem(sizeof(struct ext2_image_hdr),
139 					&fs->image_header);
140 		if (retval)
141 			goto cleanup;
142 		retval = io_channel_read_blk(fs->io, 0,
143 					     -(int)sizeof(struct ext2_image_hdr),
144 					     fs->image_header);
145 		if (retval)
146 			goto cleanup;
147 		if (fs->image_header->magic_number != EXT2_ET_MAGIC_E2IMAGE)
148 			return EXT2_ET_MAGIC_E2IMAGE;
149 		superblock = 1;
150 		block_size = fs->image_header->fs_blocksize;
151 	}
152 
153 	/*
154 	 * If the user specifies a specific block # for the
155 	 * superblock, then he/she must also specify the block size!
156 	 * Otherwise, read the master superblock located at offset
157 	 * SUPERBLOCK_OFFSET from the start of the partition.
158 	 *
159 	 * Note: we only save a backup copy of the superblock if we
160 	 * are reading the superblock from the primary superblock location.
161 	 */
162 	if (superblock) {
163 		if (!block_size) {
164 			retval = EXT2_ET_INVALID_ARGUMENT;
165 			goto cleanup;
166 		}
167 		io_channel_set_blksize(fs->io, block_size);
168 		group_block = superblock;
169 		fs->orig_super = 0;
170 	} else {
171 		io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
172 		superblock = 1;
173 		group_block = 0;
174 		retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->orig_super);
175 		if (retval)
176 			goto cleanup;
177 	}
178 	retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
179 				     fs->super);
180 	if (retval)
181 		goto cleanup;
182 	if (fs->orig_super)
183 		memcpy(fs->orig_super, fs->super, SUPERBLOCK_SIZE);
184 
185 #ifdef WORDS_BIGENDIAN
186 	fs->flags |= EXT2_FLAG_SWAP_BYTES;
187 	ext2fs_swap_super(fs->super);
188 #else
189 	if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
190 		retval = EXT2_ET_UNIMPLEMENTED;
191 		goto cleanup;
192 	}
193 #endif
194 
195 	if (fs->super->s_magic != EXT2_SUPER_MAGIC) {
196 		retval = EXT2_ET_BAD_MAGIC;
197 		goto cleanup;
198 	}
199 	if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
200 		retval = EXT2_ET_REV_TOO_HIGH;
201 		goto cleanup;
202 	}
203 
204 	/*
205 	 * Check for feature set incompatibility
206 	 */
207 	if (!(flags & EXT2_FLAG_FORCE)) {
208 		features = fs->super->s_feature_incompat;
209 #ifdef EXT2_LIB_SOFTSUPP_INCOMPAT
210 		if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
211 			features &= !EXT2_LIB_SOFTSUPP_INCOMPAT;
212 #endif
213 		if (features & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
214 			retval = EXT2_ET_UNSUPP_FEATURE;
215 			goto cleanup;
216 		}
217 
218 		features = fs->super->s_feature_ro_compat;
219 #ifdef EXT2_LIB_SOFTSUPP_RO_COMPAT
220 		if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
221 			features &= !EXT2_LIB_SOFTSUPP_RO_COMPAT;
222 #endif
223 		if ((flags & EXT2_FLAG_RW) &&
224 		    (features & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
225 			retval = EXT2_ET_RO_UNSUPP_FEATURE;
226 			goto cleanup;
227 		}
228 
229 		if (!(flags & EXT2_FLAG_JOURNAL_DEV_OK) &&
230 		    (fs->super->s_feature_incompat &
231 		     EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
232 			retval = EXT2_ET_UNSUPP_FEATURE;
233 			goto cleanup;
234 		}
235 	}
236 
237 	if ((fs->super->s_log_block_size + EXT2_MIN_BLOCK_LOG_SIZE) >
238 	    EXT2_MAX_BLOCK_LOG_SIZE) {
239 		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
240 		goto cleanup;
241 	}
242 	fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
243 	if (EXT2_INODE_SIZE(fs->super) < EXT2_GOOD_OLD_INODE_SIZE) {
244 		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
245 		goto cleanup;
246 	}
247 	fs->fragsize = EXT2_FRAG_SIZE(fs->super);
248 	fs->inode_blocks_per_group = ((EXT2_INODES_PER_GROUP(fs->super) *
249 				       EXT2_INODE_SIZE(fs->super) +
250 				       EXT2_BLOCK_SIZE(fs->super) - 1) /
251 				      EXT2_BLOCK_SIZE(fs->super));
252 	if (block_size) {
253 		if (block_size != fs->blocksize) {
254 			retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
255 			goto cleanup;
256 		}
257 	}
258 	/*
259 	 * Set the blocksize to the filesystem's blocksize.
260 	 */
261 	io_channel_set_blksize(fs->io, fs->blocksize);
262 
263 	/*
264 	 * If this is an external journal device, don't try to read
265 	 * the group descriptors, because they're not there.
266 	 */
267 	if (fs->super->s_feature_incompat &
268 	    EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
269 		fs->group_desc_count = 0;
270 		*ret_fs = fs;
271 		return 0;
272 	}
273 
274 	if (EXT2_INODES_PER_GROUP(fs->super) == 0) {
275 		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
276 		goto cleanup;
277 	}
278 
279 	/*
280 	 * Read group descriptors
281 	 */
282 	blocks_per_group = EXT2_BLOCKS_PER_GROUP(fs->super);
283 	if (blocks_per_group == 0 ||
284 	    blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(fs->super) ||
285 	    fs->inode_blocks_per_group > EXT2_MAX_INODES_PER_GROUP(fs->super) ||
286            EXT2_DESC_PER_BLOCK(fs->super) == 0 ||
287            fs->super->s_first_data_block >= fs->super->s_blocks_count) {
288 		retval = EXT2_ET_CORRUPT_SUPERBLOCK;
289 		goto cleanup;
290 	}
291 	fs->group_desc_count = ext2fs_div_ceil(fs->super->s_blocks_count -
292 					       fs->super->s_first_data_block,
293 					       blocks_per_group);
294        if (fs->group_desc_count * EXT2_INODES_PER_GROUP(fs->super) !=
295            fs->super->s_inodes_count) {
296                retval = EXT2_ET_CORRUPT_SUPERBLOCK;
297 		goto cleanup;
298        }
299 	fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
300 					  EXT2_DESC_PER_BLOCK(fs->super));
301 	retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
302 				&fs->group_desc);
303 	if (retval)
304 		goto cleanup;
305 	if (!group_block)
306 		group_block = fs->super->s_first_data_block;
307 	dest = (char *) fs->group_desc;
308 	groups_per_block = EXT2_DESC_PER_BLOCK(fs->super);
309 	if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
310 		first_meta_bg = fs->super->s_first_meta_bg;
311 	else
312 		first_meta_bg = fs->desc_blocks;
313 	if (first_meta_bg) {
314 		retval = io_channel_read_blk(fs->io, group_block+1,
315 					     first_meta_bg, dest);
316 		if (retval)
317 			goto cleanup;
318 #ifdef WORDS_BIGENDIAN
319 		gdp = (struct ext2_group_desc *) dest;
320 		for (j=0; j < groups_per_block*first_meta_bg; j++)
321 			ext2fs_swap_group_desc(gdp++);
322 #endif
323 		dest += fs->blocksize*first_meta_bg;
324 	}
325 	for (i=first_meta_bg ; i < fs->desc_blocks; i++) {
326 		blk = ext2fs_descriptor_block_loc(fs, group_block, i);
327 		retval = io_channel_read_blk(fs->io, blk, 1, dest);
328 		if (retval)
329 			goto cleanup;
330 #ifdef WORDS_BIGENDIAN
331 		gdp = (struct ext2_group_desc *) dest;
332 		for (j=0; j < groups_per_block; j++)
333 			ext2fs_swap_group_desc(gdp++);
334 #endif
335 		dest += fs->blocksize;
336 	}
337 
338 	fs->stride = fs->super->s_raid_stride;
339 
340 	/*
341 	 * If recovery is from backup superblock, Clear _UNININT flags &
342 	 * reset bg_itable_unused to zero
343 	 */
344 	if (superblock > 1 && EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
345 					EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
346 		struct ext2_group_desc *gd;
347 		for (i = 0, gd = fs->group_desc; i < fs->group_desc_count;
348 		     i++, gd++) {
349 			gd->bg_flags &= ~EXT2_BG_BLOCK_UNINIT;
350 			gd->bg_flags &= ~EXT2_BG_INODE_UNINIT;
351 			gd->bg_itable_unused = 0;
352 		}
353 		ext2fs_mark_super_dirty(fs);
354 	}
355 
356 	fs->flags &= ~EXT2_FLAG_NOFREE_ON_ERROR;
357 	*ret_fs = fs;
358 	return 0;
359 cleanup:
360 	if (flags & EXT2_FLAG_NOFREE_ON_ERROR)
361 		*ret_fs = fs;
362 	else
363 		ext2fs_free(fs);
364 	return retval;
365 }
366 
367 /*
368  * Set/get the filesystem data I/O channel.
369  *
370  * These functions are only valid if EXT2_FLAG_IMAGE_FILE is true.
371  */
ext2fs_get_data_io(ext2_filsys fs,io_channel * old_io)372 errcode_t ext2fs_get_data_io(ext2_filsys fs, io_channel *old_io)
373 {
374 	if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
375 		return EXT2_ET_NOT_IMAGE_FILE;
376 	if (old_io) {
377 		*old_io = (fs->image_io == fs->io) ? 0 : fs->io;
378 	}
379 	return 0;
380 }
381 
ext2fs_set_data_io(ext2_filsys fs,io_channel new_io)382 errcode_t ext2fs_set_data_io(ext2_filsys fs, io_channel new_io)
383 {
384 	if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
385 		return EXT2_ET_NOT_IMAGE_FILE;
386 	fs->io = new_io ? new_io : fs->image_io;
387 	return 0;
388 }
389 
ext2fs_rewrite_to_io(ext2_filsys fs,io_channel new_io)390 errcode_t ext2fs_rewrite_to_io(ext2_filsys fs, io_channel new_io)
391 {
392 	if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
393 		return EXT2_ET_NOT_IMAGE_FILE;
394 	fs->io = fs->image_io = new_io;
395 	fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_RW |
396 		EXT2_FLAG_BB_DIRTY | EXT2_FLAG_IB_DIRTY;
397 	fs->flags &= ~EXT2_FLAG_IMAGE_FILE;
398 	return 0;
399 }
400