1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
/*
* openfs.c --- open an ext2 filesystem
*
* Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
*
* %Begin-Header%
* This file may be redistributed under the terms of the GNU Library
* General Public License, version 2.
* %End-Header%
*/
#include <stdio.h>
#include <string.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <fcntl.h>
#include <time.h>
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include "ext2_fs.h"
#include "ext2fs.h"
#include "e2image.h"
blk_t ext2fs_descriptor_block_loc(ext2_filsys fs, blk_t group_block, dgrp_t i)
{
int bg;
int has_super = 0;
int ret_blk;
if (!(fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) ||
(i < fs->super->s_first_meta_bg))
return (group_block + i + 1);
bg = EXT2_DESC_PER_BLOCK(fs->super) * i;
if (ext2fs_bg_has_super(fs, bg))
has_super = 1;
ret_blk = ext2fs_group_first_block(fs, bg) + has_super;
/*
* If group_block is not the normal value, we're trying to use
* the backup group descriptors and superblock --- so use the
* alternate location of the second block group in the
* metablock group. Ideally we should be testing each bg
* descriptor block individually for correctness, but we don't
* have the infrastructure in place to do that.
*/
if (group_block != fs->super->s_first_data_block &&
((ret_blk + fs->super->s_blocks_per_group) <
fs->super->s_blocks_count))
ret_blk += fs->super->s_blocks_per_group;
return ret_blk;
}
errcode_t ext2fs_open(const char *name, int flags, int superblock,
unsigned int block_size, io_manager manager,
ext2_filsys *ret_fs)
{
return ext2fs_open2(name, 0, flags, superblock, block_size,
manager, ret_fs);
}
/*
* Note: if superblock is non-zero, block-size must also be non-zero.
* Superblock and block_size can be zero to use the default size.
*
* Valid flags for ext2fs_open()
*
* EXT2_FLAG_RW - Open the filesystem for read/write.
* EXT2_FLAG_FORCE - Open the filesystem even if some of the
* features aren't supported.
* EXT2_FLAG_JOURNAL_DEV_OK - Open an ext3 journal device
*/
errcode_t ext2fs_open2(const char *name, const char *io_options,
int flags, int superblock,
unsigned int block_size, io_manager manager,
ext2_filsys *ret_fs)
{
ext2_filsys fs;
errcode_t retval;
unsigned long i, first_meta_bg;
__u32 features;
int groups_per_block, blocks_per_group, io_flags;
blk_t group_block, blk;
char *dest, *cp;
#ifdef WORDS_BIGENDIAN
struct ext2_group_desc *gdp;
int j;
#endif
EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);
retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
if (retval)
return retval;
memset(fs, 0, sizeof(struct struct_ext2_filsys));
fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
fs->flags = flags;
/* don't overwrite sb backups unless flag is explicitly cleared */
fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
fs->umask = 022;
retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name);
if (retval)
goto cleanup;
strcpy(fs->device_name, name);
cp = strchr(fs->device_name, '?');
if (!io_options && cp) {
*cp++ = 0;
io_options = cp;
}
io_flags = 0;
if (flags & EXT2_FLAG_RW)
io_flags |= IO_FLAG_RW;
if (flags & EXT2_FLAG_EXCLUSIVE)
io_flags |= IO_FLAG_EXCLUSIVE;
if (flags & EXT2_FLAG_DIRECT_IO)
io_flags |= IO_FLAG_DIRECT_IO;
retval = manager->open(fs->device_name, io_flags, &fs->io);
if (retval)
goto cleanup;
if (io_options &&
(retval = io_channel_set_options(fs->io, io_options)))
goto cleanup;
fs->image_io = fs->io;
fs->io->app_data = fs;
retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->super);
if (retval)
goto cleanup;
if (flags & EXT2_FLAG_IMAGE_FILE) {
retval = ext2fs_get_mem(sizeof(struct ext2_image_hdr),
&fs->image_header);
if (retval)
goto cleanup;
retval = io_channel_read_blk(fs->io, 0,
-(int)sizeof(struct ext2_image_hdr),
fs->image_header);
if (retval)
goto cleanup;
if (fs->image_header->magic_number != EXT2_ET_MAGIC_E2IMAGE)
return EXT2_ET_MAGIC_E2IMAGE;
superblock = 1;
block_size = fs->image_header->fs_blocksize;
}
/*
* If the user specifies a specific block # for the
* superblock, then he/she must also specify the block size!
* Otherwise, read the master superblock located at offset
* SUPERBLOCK_OFFSET from the start of the partition.
*
* Note: we only save a backup copy of the superblock if we
* are reading the superblock from the primary superblock location.
*/
if (superblock) {
if (!block_size) {
retval = EXT2_ET_INVALID_ARGUMENT;
goto cleanup;
}
io_channel_set_blksize(fs->io, block_size);
group_block = superblock;
fs->orig_super = 0;
} else {
io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
superblock = 1;
group_block = 0;
retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->orig_super);
if (retval)
goto cleanup;
}
retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
fs->super);
if (retval)
goto cleanup;
if (fs->orig_super)
memcpy(fs->orig_super, fs->super, SUPERBLOCK_SIZE);
#ifdef WORDS_BIGENDIAN
fs->flags |= EXT2_FLAG_SWAP_BYTES;
ext2fs_swap_super(fs->super);
#else
if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
retval = EXT2_ET_UNIMPLEMENTED;
goto cleanup;
}
#endif
if (fs->super->s_magic != EXT2_SUPER_MAGIC) {
retval = EXT2_ET_BAD_MAGIC;
goto cleanup;
}
if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
retval = EXT2_ET_REV_TOO_HIGH;
goto cleanup;
}
/*
* Check for feature set incompatibility
*/
if (!(flags & EXT2_FLAG_FORCE)) {
features = fs->super->s_feature_incompat;
#ifdef EXT2_LIB_SOFTSUPP_INCOMPAT
if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
features &= !EXT2_LIB_SOFTSUPP_INCOMPAT;
#endif
if (features & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
retval = EXT2_ET_UNSUPP_FEATURE;
goto cleanup;
}
features = fs->super->s_feature_ro_compat;
#ifdef EXT2_LIB_SOFTSUPP_RO_COMPAT
if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
features &= !EXT2_LIB_SOFTSUPP_RO_COMPAT;
#endif
if ((flags & EXT2_FLAG_RW) &&
(features & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
retval = EXT2_ET_RO_UNSUPP_FEATURE;
goto cleanup;
}
if (!(flags & EXT2_FLAG_JOURNAL_DEV_OK) &&
(fs->super->s_feature_incompat &
EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
retval = EXT2_ET_UNSUPP_FEATURE;
goto cleanup;
}
}
if ((fs->super->s_log_block_size + EXT2_MIN_BLOCK_LOG_SIZE) >
EXT2_MAX_BLOCK_LOG_SIZE) {
retval = EXT2_ET_CORRUPT_SUPERBLOCK;
goto cleanup;
}
fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
if (EXT2_INODE_SIZE(fs->super) < EXT2_GOOD_OLD_INODE_SIZE) {
retval = EXT2_ET_CORRUPT_SUPERBLOCK;
goto cleanup;
}
fs->fragsize = EXT2_FRAG_SIZE(fs->super);
fs->inode_blocks_per_group = ((EXT2_INODES_PER_GROUP(fs->super) *
EXT2_INODE_SIZE(fs->super) +
EXT2_BLOCK_SIZE(fs->super) - 1) /
EXT2_BLOCK_SIZE(fs->super));
if (block_size) {
if (block_size != fs->blocksize) {
retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
goto cleanup;
}
}
/*
* Set the blocksize to the filesystem's blocksize.
*/
io_channel_set_blksize(fs->io, fs->blocksize);
/*
* If this is an external journal device, don't try to read
* the group descriptors, because they're not there.
*/
if (fs->super->s_feature_incompat &
EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
fs->group_desc_count = 0;
*ret_fs = fs;
return 0;
}
if (EXT2_INODES_PER_GROUP(fs->super) == 0) {
retval = EXT2_ET_CORRUPT_SUPERBLOCK;
goto cleanup;
}
/*
* Read group descriptors
*/
blocks_per_group = EXT2_BLOCKS_PER_GROUP(fs->super);
if (blocks_per_group == 0 ||
blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(fs->super) ||
fs->inode_blocks_per_group > EXT2_MAX_INODES_PER_GROUP(fs->super) ||
EXT2_DESC_PER_BLOCK(fs->super) == 0 ||
fs->super->s_first_data_block >= fs->super->s_blocks_count) {
retval = EXT2_ET_CORRUPT_SUPERBLOCK;
goto cleanup;
}
fs->group_desc_count = ext2fs_div_ceil(fs->super->s_blocks_count -
fs->super->s_first_data_block,
blocks_per_group);
if (fs->group_desc_count * EXT2_INODES_PER_GROUP(fs->super) !=
fs->super->s_inodes_count) {
retval = EXT2_ET_CORRUPT_SUPERBLOCK;
goto cleanup;
}
fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
EXT2_DESC_PER_BLOCK(fs->super));
retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
&fs->group_desc);
if (retval)
goto cleanup;
if (!group_block)
group_block = fs->super->s_first_data_block;
dest = (char *) fs->group_desc;
groups_per_block = EXT2_DESC_PER_BLOCK(fs->super);
if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
first_meta_bg = fs->super->s_first_meta_bg;
else
first_meta_bg = fs->desc_blocks;
if (first_meta_bg) {
retval = io_channel_read_blk(fs->io, group_block+1,
first_meta_bg, dest);
if (retval)
goto cleanup;
#ifdef WORDS_BIGENDIAN
gdp = (struct ext2_group_desc *) dest;
for (j=0; j < groups_per_block*first_meta_bg; j++)
ext2fs_swap_group_desc(gdp++);
#endif
dest += fs->blocksize*first_meta_bg;
}
for (i=first_meta_bg ; i < fs->desc_blocks; i++) {
blk = ext2fs_descriptor_block_loc(fs, group_block, i);
retval = io_channel_read_blk(fs->io, blk, 1, dest);
if (retval)
goto cleanup;
#ifdef WORDS_BIGENDIAN
gdp = (struct ext2_group_desc *) dest;
for (j=0; j < groups_per_block; j++)
ext2fs_swap_group_desc(gdp++);
#endif
dest += fs->blocksize;
}
fs->stride = fs->super->s_raid_stride;
/*
* If recovery is from backup superblock, Clear _UNININT flags &
* reset bg_itable_unused to zero
*/
if (superblock > 1 && EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
struct ext2_group_desc *gd;
for (i = 0, gd = fs->group_desc; i < fs->group_desc_count;
i++, gd++) {
gd->bg_flags &= ~EXT2_BG_BLOCK_UNINIT;
gd->bg_flags &= ~EXT2_BG_INODE_UNINIT;
gd->bg_itable_unused = 0;
}
ext2fs_mark_super_dirty(fs);
}
fs->flags &= ~EXT2_FLAG_NOFREE_ON_ERROR;
*ret_fs = fs;
return 0;
cleanup:
if (flags & EXT2_FLAG_NOFREE_ON_ERROR)
*ret_fs = fs;
else
ext2fs_free(fs);
return retval;
}
/*
* Set/get the filesystem data I/O channel.
*
* These functions are only valid if EXT2_FLAG_IMAGE_FILE is true.
*/
errcode_t ext2fs_get_data_io(ext2_filsys fs, io_channel *old_io)
{
if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
return EXT2_ET_NOT_IMAGE_FILE;
if (old_io) {
*old_io = (fs->image_io == fs->io) ? 0 : fs->io;
}
return 0;
}
errcode_t ext2fs_set_data_io(ext2_filsys fs, io_channel new_io)
{
if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
return EXT2_ET_NOT_IMAGE_FILE;
fs->io = new_io ? new_io : fs->image_io;
return 0;
}
errcode_t ext2fs_rewrite_to_io(ext2_filsys fs, io_channel new_io)
{
if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
return EXT2_ET_NOT_IMAGE_FILE;
fs->io = fs->image_io = new_io;
fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_RW |
EXT2_FLAG_BB_DIRTY | EXT2_FLAG_IB_DIRTY;
fs->flags &= ~EXT2_FLAG_IMAGE_FILE;
return 0;
}
|