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