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