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         retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys),
57                                 (void **) &fs);
58         if (retval)
59                 return retval;
60         
61         memset(fs, 0, sizeof(struct struct_ext2_filsys));
62         fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
63         fs->flags = flags;
64         retval = manager->open(name, (flags & EXT2_FLAG_RW) ? IO_FLAG_RW : 0,
65                                &fs->io);
66         if (retval)
67                 goto cleanup;
68         fs->io->app_data = fs;
69         retval = ext2fs_get_mem(strlen(name)+1, (void **) &fs->device_name);
70         if (retval)
71                 goto cleanup;
72         strcpy(fs->device_name, name);
73         retval = ext2fs_get_mem(SUPERBLOCK_SIZE, (void **) &fs->super);
74         if (retval)
75                 goto cleanup;
76
77         /*
78          * If the user specifies a specific block # for the
79          * superblock, then he/she must also specify the block size!
80          * Otherwise, read the master superblock located at offset
81          * SUPERBLOCK_OFFSET from the start of the partition.
82          */
83         if (superblock) {
84                 if (!block_size) {
85                         retval = EXT2_ET_INVALID_ARGUMENT;
86                         goto cleanup;
87                 }
88                 io_channel_set_blksize(fs->io, block_size);
89                 group_block = superblock + 1;
90         } else {
91                 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
92                 superblock = 1;
93                 group_block = 0;
94         }
95         retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
96                                      fs->super);
97         if (retval)
98                 goto cleanup;
99
100         if ((fs->super->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) ||
101             (fs->flags & EXT2_FLAG_SWAP_BYTES)) {
102                 fs->flags |= EXT2_FLAG_SWAP_BYTES;
103
104                 ext2fs_swap_super(fs->super);
105         }
106         
107         if (fs->super->s_magic != EXT2_SUPER_MAGIC) {
108                 retval = EXT2_ET_BAD_MAGIC;
109                 goto cleanup;
110         }
111 #ifdef EXT2_DYNAMIC_REV
112         if (fs->super->s_rev_level > EXT2_DYNAMIC_REV) {
113                 retval = EXT2_ET_REV_TOO_HIGH;
114                 goto cleanup;
115         }
116 #else
117 #ifdef  EXT2_CURRENT_REV
118         if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
119                 retval = EXT2_ET_REV_TOO_HIGH;
120                 goto cleanup;
121         }
122 #endif
123 #endif
124         /*
125          * Check for feature set incompatibility
126          */
127         if (!(flags & EXT2_FLAG_FORCE)) {
128                 s = (struct ext2fs_sb *) fs->super;
129                 if (s->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
130                         retval = EXT2_ET_UNSUPP_FEATURE;
131                         goto cleanup;
132                 }
133                 if ((flags & EXT2_FLAG_RW) &&
134                     (s->s_feature_ro_compat &
135                      ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
136                         retval = EXT2_ET_RO_UNSUPP_FEATURE;
137                         goto cleanup;
138                 }
139         }
140         
141         fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
142         if (fs->blocksize == 0) {
143                 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
144                 goto cleanup;
145         }
146         fs->fragsize = EXT2_FRAG_SIZE(fs->super);
147         fs->inode_blocks_per_group = ((fs->super->s_inodes_per_group *
148                                        EXT2_INODE_SIZE(fs->super) +
149                                        EXT2_BLOCK_SIZE(fs->super) - 1) /
150                                       EXT2_BLOCK_SIZE(fs->super));
151         if (block_size) {
152                 if (block_size != fs->blocksize) {
153                         retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
154                         goto cleanup;
155                 }
156         }
157         /*
158          * Set the blocksize to the filesystem's blocksize.
159          */
160         io_channel_set_blksize(fs->io, fs->blocksize);
161         
162         /*
163          * Read group descriptors
164          */
165         fs->group_desc_count = (fs->super->s_blocks_count -
166                                 fs->super->s_first_data_block +
167                                 EXT2_BLOCKS_PER_GROUP(fs->super) - 1)
168                 / EXT2_BLOCKS_PER_GROUP(fs->super);
169         fs->desc_blocks = (fs->group_desc_count +
170                            EXT2_DESC_PER_BLOCK(fs->super) - 1)
171                 / EXT2_DESC_PER_BLOCK(fs->super);
172         retval = ext2fs_get_mem(fs->desc_blocks * fs->blocksize,
173                                 (void **) &fs->group_desc);
174         if (retval)
175                 goto cleanup;
176         if (!group_block)
177                 group_block = fs->super->s_first_data_block + 1;
178         dest = (char *) fs->group_desc;
179         for (i=0 ; i < fs->desc_blocks; i++) {
180                 retval = io_channel_read_blk(fs->io, group_block, 1, dest);
181                 if (retval)
182                         goto cleanup;
183                 group_block++;
184                 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
185                         gdp = (struct ext2_group_desc *) dest;
186                         groups_per_block = fs->blocksize /
187                                 sizeof(struct ext2_group_desc);
188                         for (j=0; j < groups_per_block; j++)
189                                 ext2fs_swap_group_desc(gdp++);
190                 }
191                 dest += fs->blocksize;
192         }
193
194         *ret_fs = fs;
195         return 0;
196 cleanup:
197         ext2fs_free(fs);
198         return retval;
199 }
200