]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - lib/ext2fs/openfs.c
libext2fs: reserve the error code EXT2_ET_NO_GDESC
[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 #include "ext2fs.h"
33 #include "e2image.h"
34
35 blk64_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, group_zero_adjust = 0;
40 blk64_t ret_blk;
41
42 /*
43 * On a bigalloc FS with 1K blocks, block 0 is reserved for non-ext4
44 * stuff, so adjust for that if we're being asked for group 0.
45 */
46 if (i == 0 && fs->blocksize == 1024 && EXT2FS_CLUSTER_RATIO(fs) > 1)
47 group_zero_adjust = 1;
48
49 if (!ext2fs_has_feature_meta_bg(fs->super) ||
50 (i < fs->super->s_first_meta_bg))
51 return group_block + i + 1 + group_zero_adjust;
52
53 bg = EXT2_DESC_PER_BLOCK(fs->super) * i;
54 if (ext2fs_bg_has_super(fs, bg))
55 has_super = 1;
56 ret_blk = ext2fs_group_first_block2(fs, bg);
57 /*
58 * If group_block is not the normal value, we're trying to use
59 * the backup group descriptors and superblock --- so use the
60 * alternate location of the second block group in the
61 * metablock group. Ideally we should be testing each bg
62 * descriptor block individually for correctness, but we don't
63 * have the infrastructure in place to do that.
64 */
65 if (group_block != fs->super->s_first_data_block &&
66 ((ret_blk + has_super + fs->super->s_blocks_per_group) <
67 ext2fs_blocks_count(fs->super))) {
68 ret_blk += fs->super->s_blocks_per_group;
69
70 /*
71 * If we're going to jump forward a block group, make sure
72 * that we adjust has_super to account for the next group's
73 * backup superblock (or lack thereof).
74 */
75 if (ext2fs_bg_has_super(fs, bg + 1))
76 has_super = 1;
77 else
78 has_super = 0;
79 }
80 return ret_blk + has_super + group_zero_adjust;
81 }
82
83 blk_t ext2fs_descriptor_block_loc(ext2_filsys fs, blk_t group_block, dgrp_t i)
84 {
85 return ext2fs_descriptor_block_loc2(fs, group_block, i);
86 }
87
88 errcode_t ext2fs_open(const char *name, int flags, int superblock,
89 unsigned int block_size, io_manager manager,
90 ext2_filsys *ret_fs)
91 {
92 return ext2fs_open2(name, 0, flags, superblock, block_size,
93 manager, ret_fs);
94 }
95
96 static void block_sha_map_free_entry(void *data)
97 {
98 free(data);
99 return;
100 }
101
102 /*
103 * Note: if superblock is non-zero, block-size must also be non-zero.
104 * Superblock and block_size can be zero to use the default size.
105 *
106 * Valid flags for ext2fs_open()
107 *
108 * EXT2_FLAG_RW - Open the filesystem for read/write.
109 * EXT2_FLAG_FORCE - Open the filesystem even if some of the
110 * features aren't supported.
111 * EXT2_FLAG_JOURNAL_DEV_OK - Open an ext3 journal device
112 * EXT2_FLAG_SKIP_MMP - Open without multi-mount protection check.
113 * EXT2_FLAG_64BITS - Allow 64-bit bitfields (needed for large
114 * filesystems)
115 */
116 errcode_t ext2fs_open2(const char *name, const char *io_options,
117 int flags, int superblock,
118 unsigned int block_size, io_manager manager,
119 ext2_filsys *ret_fs)
120 {
121 ext2_filsys fs;
122 errcode_t retval;
123 unsigned long i, first_meta_bg;
124 __u32 features;
125 unsigned int blocks_per_group, io_flags;
126 blk64_t group_block, blk;
127 char *dest, *cp;
128 int group_zero_adjust = 0;
129 unsigned int inode_size;
130 __u64 groups_cnt;
131 #ifdef WORDS_BIGENDIAN
132 unsigned int groups_per_block;
133 struct ext2_group_desc *gdp;
134 int j;
135 #endif
136 char *time_env;
137
138 EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);
139
140 retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
141 if (retval)
142 return retval;
143
144 memset(fs, 0, sizeof(struct struct_ext2_filsys));
145 fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
146 fs->flags = flags;
147 /* don't overwrite sb backups unless flag is explicitly cleared */
148 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
149 fs->umask = 022;
150
151 time_env = getenv("E2FSPROGS_FAKE_TIME");
152 if (time_env)
153 fs->now = strtoul(time_env, NULL, 0);
154
155 retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name);
156 if (retval)
157 goto cleanup;
158 strcpy(fs->device_name, name);
159 cp = strchr(fs->device_name, '?');
160 if (!io_options && cp) {
161 *cp++ = 0;
162 io_options = cp;
163 }
164
165 io_flags = 0;
166 if (flags & EXT2_FLAG_RW)
167 io_flags |= IO_FLAG_RW;
168 if (flags & EXT2_FLAG_EXCLUSIVE)
169 io_flags |= IO_FLAG_EXCLUSIVE;
170 if (flags & EXT2_FLAG_DIRECT_IO)
171 io_flags |= IO_FLAG_DIRECT_IO;
172 retval = manager->open(fs->device_name, io_flags, &fs->io);
173 if (retval)
174 goto cleanup;
175 if (io_options &&
176 (retval = io_channel_set_options(fs->io, io_options)))
177 goto cleanup;
178 fs->image_io = fs->io;
179 fs->io->app_data = fs;
180 retval = io_channel_alloc_buf(fs->io, -SUPERBLOCK_SIZE, &fs->super);
181 if (retval)
182 goto cleanup;
183 if (flags & EXT2_FLAG_IMAGE_FILE) {
184 retval = ext2fs_get_mem(sizeof(struct ext2_image_hdr),
185 &fs->image_header);
186 if (retval)
187 goto cleanup;
188 retval = io_channel_read_blk(fs->io, 0,
189 -(int)sizeof(struct ext2_image_hdr),
190 fs->image_header);
191 if (retval)
192 goto cleanup;
193 if (ext2fs_le32_to_cpu(fs->image_header->magic_number) != EXT2_ET_MAGIC_E2IMAGE)
194 return EXT2_ET_MAGIC_E2IMAGE;
195 superblock = 1;
196 block_size = ext2fs_le32_to_cpu(fs->image_header->fs_blocksize);
197 }
198
199 /*
200 * If the user specifies a specific block # for the
201 * superblock, then he/she must also specify the block size!
202 * Otherwise, read the master superblock located at offset
203 * SUPERBLOCK_OFFSET from the start of the partition.
204 *
205 * Note: we only save a backup copy of the superblock if we
206 * are reading the superblock from the primary superblock location.
207 */
208 if (superblock) {
209 if (!block_size) {
210 retval = EXT2_ET_INVALID_ARGUMENT;
211 goto cleanup;
212 }
213 io_channel_set_blksize(fs->io, block_size);
214 group_block = superblock;
215 fs->orig_super = 0;
216 } else {
217 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
218 superblock = 1;
219 group_block = 0;
220 retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->orig_super);
221 if (retval)
222 goto cleanup;
223 }
224 retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
225 fs->super);
226 if (retval)
227 goto cleanup;
228 if (fs->orig_super)
229 memcpy(fs->orig_super, fs->super, SUPERBLOCK_SIZE);
230
231 if (!(fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS)) {
232 retval = 0;
233 if (!ext2fs_verify_csum_type(fs, fs->super))
234 retval = EXT2_ET_UNKNOWN_CSUM;
235 if (!ext2fs_superblock_csum_verify(fs, fs->super))
236 retval = EXT2_ET_SB_CSUM_INVALID;
237 }
238
239 #ifdef WORDS_BIGENDIAN
240 fs->flags |= EXT2_FLAG_SWAP_BYTES;
241 ext2fs_swap_super(fs->super);
242 #else
243 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
244 retval = EXT2_ET_UNIMPLEMENTED;
245 goto cleanup;
246 }
247 #endif
248
249 if (fs->super->s_magic != EXT2_SUPER_MAGIC)
250 retval = EXT2_ET_BAD_MAGIC;
251 if (retval)
252 goto cleanup;
253
254 if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
255 retval = EXT2_ET_REV_TOO_HIGH;
256 goto cleanup;
257 }
258
259 /*
260 * Check for feature set incompatibility
261 */
262 if (!(flags & EXT2_FLAG_FORCE)) {
263 features = fs->super->s_feature_incompat;
264 #ifdef EXT2_LIB_SOFTSUPP_INCOMPAT
265 if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
266 features &= ~EXT2_LIB_SOFTSUPP_INCOMPAT;
267 #endif
268 if (features & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
269 retval = EXT2_ET_UNSUPP_FEATURE;
270 goto cleanup;
271 }
272
273 features = fs->super->s_feature_ro_compat;
274 #ifdef EXT2_LIB_SOFTSUPP_RO_COMPAT
275 if (flags & EXT2_FLAG_SOFTSUPP_FEATURES)
276 features &= ~EXT2_LIB_SOFTSUPP_RO_COMPAT;
277 #endif
278 if ((flags & EXT2_FLAG_RW) &&
279 (features & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
280 retval = EXT2_ET_RO_UNSUPP_FEATURE;
281 goto cleanup;
282 }
283
284 if (!(flags & EXT2_FLAG_JOURNAL_DEV_OK) &&
285 ext2fs_has_feature_journal_dev(fs->super)) {
286 retval = EXT2_ET_UNSUPP_FEATURE;
287 goto cleanup;
288 }
289 }
290
291 if (fs->super->s_log_block_size >
292 (unsigned) (EXT2_MAX_BLOCK_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE)) {
293 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
294 goto cleanup;
295 }
296
297 /*
298 * bigalloc requires cluster-aware bitfield operations, which at the
299 * moment means we need EXT2_FLAG_64BITS.
300 */
301 if (ext2fs_has_feature_bigalloc(fs->super) &&
302 !(flags & EXT2_FLAG_64BITS)) {
303 retval = EXT2_ET_CANT_USE_LEGACY_BITMAPS;
304 goto cleanup;
305 }
306
307 if (!ext2fs_has_feature_bigalloc(fs->super) &&
308 (fs->super->s_log_block_size != fs->super->s_log_cluster_size)) {
309 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
310 goto cleanup;
311 }
312 fs->fragsize = fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
313 inode_size = EXT2_INODE_SIZE(fs->super);
314 if ((inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
315 (inode_size > fs->blocksize) ||
316 (inode_size & (inode_size - 1))) {
317 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
318 goto cleanup;
319 }
320
321 /* Enforce the block group descriptor size */
322 if (ext2fs_has_feature_64bit(fs->super)) {
323 if (fs->super->s_desc_size < EXT2_MIN_DESC_SIZE_64BIT) {
324 retval = EXT2_ET_BAD_DESC_SIZE;
325 goto cleanup;
326 }
327 }
328
329 fs->cluster_ratio_bits = fs->super->s_log_cluster_size -
330 fs->super->s_log_block_size;
331 if (EXT2_BLOCKS_PER_GROUP(fs->super) !=
332 EXT2_CLUSTERS_PER_GROUP(fs->super) << fs->cluster_ratio_bits) {
333 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
334 goto cleanup;
335 }
336 fs->inode_blocks_per_group = ((EXT2_INODES_PER_GROUP(fs->super) *
337 EXT2_INODE_SIZE(fs->super) +
338 EXT2_BLOCK_SIZE(fs->super) - 1) /
339 EXT2_BLOCK_SIZE(fs->super));
340 if (block_size) {
341 if (block_size != fs->blocksize) {
342 retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
343 goto cleanup;
344 }
345 }
346 /*
347 * Set the blocksize to the filesystem's blocksize.
348 */
349 io_channel_set_blksize(fs->io, fs->blocksize);
350
351 /*
352 * If this is an external journal device, don't try to read
353 * the group descriptors, because they're not there.
354 */
355 if (ext2fs_has_feature_journal_dev(fs->super)) {
356 fs->group_desc_count = 0;
357 *ret_fs = fs;
358 return 0;
359 }
360
361 if (EXT2_INODES_PER_GROUP(fs->super) == 0) {
362 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
363 goto cleanup;
364 }
365 /* Precompute the FS UUID to seed other checksums */
366 ext2fs_init_csum_seed(fs);
367
368 /*
369 * Read group descriptors
370 */
371 blocks_per_group = EXT2_BLOCKS_PER_GROUP(fs->super);
372 if (blocks_per_group == 0 ||
373 blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(fs->super) ||
374 fs->inode_blocks_per_group > EXT2_MAX_INODES_PER_GROUP(fs->super) ||
375 EXT2_DESC_PER_BLOCK(fs->super) == 0 ||
376 fs->super->s_first_data_block >= ext2fs_blocks_count(fs->super)) {
377 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
378 goto cleanup;
379 }
380 groups_cnt = ext2fs_div64_ceil(ext2fs_blocks_count(fs->super) -
381 fs->super->s_first_data_block,
382 blocks_per_group);
383 if (groups_cnt >> 32) {
384 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
385 goto cleanup;
386 }
387 fs->group_desc_count = groups_cnt;
388 if (!(flags & EXT2_FLAG_IGNORE_SB_ERRORS) &&
389 (__u64)fs->group_desc_count * EXT2_INODES_PER_GROUP(fs->super) !=
390 fs->super->s_inodes_count) {
391 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
392 goto cleanup;
393 }
394 fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
395 EXT2_DESC_PER_BLOCK(fs->super));
396 retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
397 &fs->group_desc);
398 if (retval)
399 goto cleanup;
400 if (!group_block)
401 group_block = fs->super->s_first_data_block;
402 /*
403 * On a FS with a 1K blocksize, block 0 is reserved for bootloaders
404 * so we must increment block numbers to any group 0 items.
405 *
406 * However, we cannot touch group_block directly because in the meta_bg
407 * case, the ext2fs_descriptor_block_loc2() function will interpret
408 * group_block != s_first_data_block to mean that we want to access the
409 * backup group descriptors. This is not what we want if the caller
410 * set superblock == 0 (i.e. auto-detect the superblock), which is
411 * what's going on here.
412 */
413 if (group_block == 0 && fs->blocksize == 1024)
414 group_zero_adjust = 1;
415 dest = (char *) fs->group_desc;
416 #ifdef WORDS_BIGENDIAN
417 groups_per_block = EXT2_DESC_PER_BLOCK(fs->super);
418 #endif
419 if (ext2fs_has_feature_meta_bg(fs->super) &&
420 !(flags & EXT2_FLAG_IMAGE_FILE)) {
421 first_meta_bg = fs->super->s_first_meta_bg;
422 if (first_meta_bg > fs->desc_blocks)
423 first_meta_bg = fs->desc_blocks;
424 } else
425 first_meta_bg = fs->desc_blocks;
426 if (first_meta_bg) {
427 retval = io_channel_read_blk(fs->io, group_block +
428 group_zero_adjust + 1,
429 first_meta_bg, dest);
430 if (retval)
431 goto cleanup;
432 #ifdef WORDS_BIGENDIAN
433 gdp = (struct ext2_group_desc *) dest;
434 for (j=0; j < groups_per_block*first_meta_bg; j++) {
435 gdp = ext2fs_group_desc(fs, fs->group_desc, j);
436 ext2fs_swap_group_desc2(fs, gdp);
437 }
438 #endif
439 dest += fs->blocksize*first_meta_bg;
440 }
441
442 for (i = first_meta_bg ; i < fs->desc_blocks; i++) {
443 blk = ext2fs_descriptor_block_loc2(fs, group_block, i);
444 io_channel_cache_readahead(fs->io, blk, 1);
445 }
446
447 for (i=first_meta_bg ; i < fs->desc_blocks; i++) {
448 blk = ext2fs_descriptor_block_loc2(fs, group_block, i);
449 retval = io_channel_read_blk64(fs->io, blk, 1, dest);
450 if (retval)
451 goto cleanup;
452 #ifdef WORDS_BIGENDIAN
453 for (j=0; j < groups_per_block; j++) {
454 gdp = ext2fs_group_desc(fs, fs->group_desc,
455 i * groups_per_block + j);
456 ext2fs_swap_group_desc2(fs, gdp);
457 }
458 #endif
459 dest += fs->blocksize;
460 }
461
462 fs->stride = fs->super->s_raid_stride;
463
464 /*
465 * If recovery is from backup superblock, Clear _UNININT flags &
466 * reset bg_itable_unused to zero
467 */
468 if (superblock > 1 && ext2fs_has_group_desc_csum(fs)) {
469 dgrp_t group;
470
471 for (group = 0; group < fs->group_desc_count; group++) {
472 ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
473 ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
474 ext2fs_bg_itable_unused_set(fs, group, 0);
475 /* The checksum will be reset later, but fix it here
476 * anyway to avoid printing a lot of spurious errors. */
477 ext2fs_group_desc_csum_set(fs, group);
478 }
479 if (fs->flags & EXT2_FLAG_RW)
480 ext2fs_mark_super_dirty(fs);
481 }
482
483 if (ext2fs_has_feature_mmp(fs->super) &&
484 !(flags & EXT2_FLAG_SKIP_MMP) &&
485 (flags & (EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE))) {
486 retval = ext2fs_mmp_start(fs);
487 if (retval) {
488 fs->flags |= EXT2_FLAG_SKIP_MMP; /* just do cleanup */
489 ext2fs_mmp_stop(fs);
490 goto cleanup;
491 }
492 }
493
494 if (fs->flags & EXT2_FLAG_SHARE_DUP) {
495 fs->block_sha_map = ext2fs_hashmap_create(ext2fs_djb2_hash,
496 block_sha_map_free_entry, 4096);
497 if (!fs->block_sha_map) {
498 retval = EXT2_ET_NO_MEMORY;
499 goto cleanup;
500 }
501 ext2fs_set_feature_shared_blocks(fs->super);
502 }
503
504 if (ext2fs_has_feature_casefold(fs->super))
505 fs->encoding = ext2fs_load_nls_table(fs->super->s_encoding);
506
507 fs->flags &= ~EXT2_FLAG_NOFREE_ON_ERROR;
508 *ret_fs = fs;
509
510 return 0;
511 cleanup:
512 if (!(flags & EXT2_FLAG_NOFREE_ON_ERROR)) {
513 ext2fs_free(fs);
514 fs = NULL;
515 }
516 *ret_fs = fs;
517 return retval;
518 }
519
520 /*
521 * Set/get the filesystem data I/O channel.
522 *
523 * These functions are only valid if EXT2_FLAG_IMAGE_FILE is true.
524 */
525 errcode_t ext2fs_get_data_io(ext2_filsys fs, io_channel *old_io)
526 {
527 if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
528 return EXT2_ET_NOT_IMAGE_FILE;
529 if (old_io) {
530 *old_io = (fs->image_io == fs->io) ? 0 : fs->io;
531 }
532 return 0;
533 }
534
535 errcode_t ext2fs_set_data_io(ext2_filsys fs, io_channel new_io)
536 {
537 if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
538 return EXT2_ET_NOT_IMAGE_FILE;
539 fs->io = new_io ? new_io : fs->image_io;
540 return 0;
541 }
542
543 errcode_t ext2fs_rewrite_to_io(ext2_filsys fs, io_channel new_io)
544 {
545 errcode_t err;
546
547 if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
548 return EXT2_ET_NOT_IMAGE_FILE;
549 err = io_channel_set_blksize(new_io, fs->blocksize);
550 if (err)
551 return err;
552 if ((new_io == fs->image_io) || (new_io == fs->io))
553 return 0;
554 if ((fs->image_io != fs->io) &&
555 fs->image_io)
556 io_channel_close(fs->image_io);
557 if (fs->io)
558 io_channel_close(fs->io);
559 fs->io = fs->image_io = new_io;
560 fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_RW |
561 EXT2_FLAG_BB_DIRTY | EXT2_FLAG_IB_DIRTY;
562 fs->flags &= ~EXT2_FLAG_IMAGE_FILE;
563 return 0;
564 }