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