Whamcloud - gitweb
Many files:
[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 Public
8  * License.
9  * %End-Header%
10  */
11
12 #include <stdio.h>
13 #include <string.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #if HAVE_STDLIB_H
18 #include <stdlib.h>
19 #endif
20 #include <fcntl.h>
21 #include <time.h>
22 #if HAVE_SYS_STAT_H
23 #include <sys/stat.h>
24 #endif
25 #if HAVE_SYS_TYPES_H
26 #include <sys/types.h>
27 #endif
28
29 #include <linux/ext2_fs.h>
30
31 #include "ext2fs.h"
32
33 /*
34  *  Note: if superblock is non-zero, block-size must also be non-zero.
35  *      Superblock and block_size can be zero to use the default size.
36  *
37  * Valid flags for ext2fs_open()
38  * 
39  *      EXT2_FLAG_RW    - Open the filesystem for read/write.
40  *      EXT2_FLAG_FORCE - Open the filesystem even if some of the
41  *                              features aren't supported.
42  */
43 errcode_t ext2fs_open(const char *name, int flags, int superblock,
44                       int block_size, io_manager manager, ext2_filsys *ret_fs)
45 {
46         ext2_filsys     fs;
47         errcode_t       retval;
48         int             i, j, groups_per_block;
49         blk_t           group_block;
50         char            *dest;
51         struct ext2_group_desc *gdp;
52         struct ext2fs_sb        *s;
53         
54         EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);
55         
56         fs = (ext2_filsys) malloc(sizeof(struct struct_ext2_filsys));
57         if (!fs)
58                 return EXT2_NO_MEMORY;
59         
60         memset(fs, 0, sizeof(struct struct_ext2_filsys));
61         fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
62         fs->flags = flags;
63         retval = manager->open(name, (flags & EXT2_FLAG_RW) ? IO_FLAG_RW : 0,
64                                &fs->io);
65         if (retval)
66                 goto cleanup;
67         fs->io->app_data = fs;
68         fs->device_name = malloc(strlen(name)+1);
69         if (!fs->device_name) {
70                 retval = EXT2_NO_MEMORY;
71                 goto cleanup;
72         }
73         strcpy(fs->device_name, name);
74         fs->super = malloc(SUPERBLOCK_SIZE);
75         if (!fs->super) {
76                 retval = EXT2_NO_MEMORY;
77                 goto cleanup;
78         }
79
80         /*
81          * If the user specifies a specific block # for the
82          * superblock, then he/she must also specify the block size!
83          * Otherwise, read the master superblock located at offset
84          * SUPERBLOCK_OFFSET from the start of the partition.
85          */
86         if (superblock) {
87                 if (!block_size) {
88                         retval = EXT2_INVALID_ARGUMENT;
89                         goto cleanup;
90                 }
91                 io_channel_set_blksize(fs->io, block_size);
92                 group_block = superblock + 1;
93         } else {
94                 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
95                 superblock = 1;
96                 group_block = 0;
97         }
98         retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
99                                      fs->super);
100         if (retval)
101                 goto cleanup;
102
103         if ((fs->super->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) ||
104             (fs->flags & EXT2_FLAG_SWAP_BYTES)) {
105                 fs->flags |= EXT2_FLAG_SWAP_BYTES;
106
107                 ext2fs_swap_super(fs->super);
108         }
109         
110         if (fs->super->s_magic != EXT2_SUPER_MAGIC) {
111                 retval = EXT2_ET_BAD_MAGIC;
112                 goto cleanup;
113         }
114 #ifdef EXT2_DYNAMIC_REV
115         if (fs->super->s_rev_level > EXT2_DYNAMIC_REV) {
116                 retval = EXT2_ET_REV_TOO_HIGH;
117                 goto cleanup;
118         }
119 #else
120 #ifdef  EXT2_CURRENT_REV
121         if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
122                 retval = EXT2_ET_REV_TOO_HIGH;
123                 goto cleanup;
124         }
125 #endif
126 #endif
127         /*
128          * Check for feature set incompatibility
129          */
130         if (!(flags & EXT2_FLAG_FORCE)) {
131                 s = (struct ext2fs_sb *) fs->super;
132                 if (s->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
133                         retval = EXT2_ET_UNSUPP_FEATURE;
134                         goto cleanup;
135                 }
136                 if ((flags & EXT2_FLAG_RW) &&
137                     (s->s_feature_ro_compat &
138                      ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
139                         retval = EXT2_ET_RO_UNSUPP_FEATURE;
140                         goto cleanup;
141                 }
142         }
143         
144         fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
145         if (fs->blocksize == 0) {
146                 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
147                 goto cleanup;
148         }
149         fs->fragsize = EXT2_FRAG_SIZE(fs->super);
150         fs->inode_blocks_per_group = ((fs->super->s_inodes_per_group *
151                                        EXT2_INODE_SIZE(fs->super) +
152                                        EXT2_BLOCK_SIZE(fs->super) - 1) /
153                                       EXT2_BLOCK_SIZE(fs->super));
154         if (block_size) {
155                 if (block_size != fs->blocksize) {
156                         retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
157                         goto cleanup;
158                 }
159         }
160         /*
161          * Set the blocksize to the filesystem's blocksize.
162          */
163         io_channel_set_blksize(fs->io, fs->blocksize);
164         
165         /*
166          * Read group descriptors
167          */
168         fs->group_desc_count = (fs->super->s_blocks_count -
169                                 fs->super->s_first_data_block +
170                                 EXT2_BLOCKS_PER_GROUP(fs->super) - 1)
171                 / EXT2_BLOCKS_PER_GROUP(fs->super);
172         fs->desc_blocks = (fs->group_desc_count +
173                            EXT2_DESC_PER_BLOCK(fs->super) - 1)
174                 / EXT2_DESC_PER_BLOCK(fs->super);
175         fs->group_desc = malloc((size_t) (fs->desc_blocks * fs->blocksize));
176         if (!fs->group_desc) {
177                 retval = EXT2_NO_MEMORY;
178                 goto cleanup;
179         }
180         if (!group_block)
181                 group_block = fs->super->s_first_data_block + 1;
182         dest = (char *) fs->group_desc;
183         for (i=0 ; i < fs->desc_blocks; i++) {
184                 retval = io_channel_read_blk(fs->io, group_block, 1, dest);
185                 if (retval)
186                         goto cleanup;
187                 group_block++;
188                 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
189                         gdp = (struct ext2_group_desc *) dest;
190                         groups_per_block = fs->blocksize /
191                                 sizeof(struct ext2_group_desc);
192                         for (j=0; j < groups_per_block; j++)
193                                 ext2fs_swap_group_desc(gdp++);
194                 }
195                 dest += fs->blocksize;
196         }
197
198         *ret_fs = fs;
199         return 0;
200 cleanup:
201         ext2fs_free(fs);
202         return retval;
203 }
204