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