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