Whamcloud - gitweb
4dcae98736126523041316d06c6d61008f5b6780
[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 #include <fcntl.h>
18 #include <time.h>
19 #if HAVE_SYS_STAT_H
20 #include <sys/stat.h>
21 #endif
22 #if HAVE_SYS_TYPES_H
23 #include <sys/types.h>
24 #endif
25
26 #include "ext2_fs.h"
27
28
29 #include "ext2fs.h"
30 #include "e2image.h"
31
32 blk_t ext2fs_descriptor_block_loc(ext2_filsys fs, blk_t group_block, dgrp_t i)
33 {
34         int     bg;
35         int     has_super = 0;
36
37         if (!(fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) ||
38             (i < fs->super->s_first_meta_bg))
39                 return (group_block + i + 1);
40
41         bg = (fs->blocksize / sizeof (struct ext2_group_desc)) * i;
42         if (ext2fs_bg_has_super(fs, bg))
43                 has_super = 1;
44         return (fs->super->s_first_data_block + has_super + 
45                 (bg * fs->super->s_blocks_per_group));
46 }
47
48 /*
49  *  Note: if superblock is non-zero, block-size must also be non-zero.
50  *      Superblock and block_size can be zero to use the default size.
51  *
52  * Valid flags for ext2fs_open()
53  * 
54  *      EXT2_FLAG_RW    - Open the filesystem for read/write.
55  *      EXT2_FLAG_FORCE - Open the filesystem even if some of the
56  *                              features aren't supported.
57  *      EXT2_FLAG_JOURNAL_DEV_OK - Open an ext3 journal device
58  */
59 errcode_t ext2fs_open(const char *name, int flags, int superblock,
60                       int block_size, io_manager manager, ext2_filsys *ret_fs)
61 {
62         ext2_filsys     fs;
63         errcode_t       retval;
64         int             i, j, groups_per_block, blocks_per_group;
65         blk_t           group_block, blk;
66         char            *dest;
67         struct ext2_group_desc *gdp;
68         
69         EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);
70
71         retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys),
72                                 (void **) &fs);
73         if (retval)
74                 return retval;
75         
76         memset(fs, 0, sizeof(struct struct_ext2_filsys));
77         fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
78         fs->flags = flags;
79         fs->umask = 022;
80         retval = manager->open(name, (flags & EXT2_FLAG_RW) ? IO_FLAG_RW : 0,
81                                &fs->io);
82         if (retval)
83                 goto cleanup;
84         fs->io->app_data = fs;
85         retval = ext2fs_get_mem(strlen(name)+1, (void **) &fs->device_name);
86         if (retval)
87                 goto cleanup;
88         strcpy(fs->device_name, name);
89         retval = ext2fs_get_mem(SUPERBLOCK_SIZE, (void **) &fs->super);
90         if (retval)
91                 goto cleanup;
92         if (flags & EXT2_FLAG_IMAGE_FILE) {
93                 retval = ext2fs_get_mem(sizeof(struct ext2_image_hdr),
94                                         (void **) &fs->image_header);
95                 if (retval)
96                         goto cleanup;
97                 retval = io_channel_read_blk(fs->io, 0,
98                                              -sizeof(struct ext2_image_hdr),
99                                              fs->image_header);
100                 if (retval)
101                         goto cleanup;
102                 if (fs->image_header->magic_number != EXT2_ET_MAGIC_E2IMAGE)
103                         return EXT2_ET_MAGIC_E2IMAGE;
104                 superblock = 1;
105                 block_size = fs->image_header->fs_blocksize;
106         }
107
108         /*
109          * If the user specifies a specific block # for the
110          * superblock, then he/she must also specify the block size!
111          * Otherwise, read the master superblock located at offset
112          * SUPERBLOCK_OFFSET from the start of the partition.
113          *
114          * Note: we only save a backup copy of the superblock if we
115          * are reading the superblock from the primary superblock location.
116          */
117         if (superblock) {
118                 if (!block_size) {
119                         retval = EXT2_ET_INVALID_ARGUMENT;
120                         goto cleanup;
121                 }
122                 io_channel_set_blksize(fs->io, block_size);
123                 group_block = superblock + 1;
124                 fs->orig_super = 0;
125         } else {
126                 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
127                 superblock = 1;
128                 group_block = 0;
129                 retval = ext2fs_get_mem(SUPERBLOCK_SIZE,
130                                         (void **) &fs->orig_super);
131                 if (retval)
132                         goto cleanup;
133         }
134         retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
135                                      fs->super);
136         if (retval)
137                 goto cleanup;
138         if (fs->orig_super)
139                 memcpy(fs->orig_super, fs->super, SUPERBLOCK_SIZE);
140
141 #ifdef EXT2FS_ENABLE_SWAPFS
142         if ((fs->super->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) ||
143             (fs->flags & EXT2_FLAG_SWAP_BYTES)) {
144                 fs->flags |= EXT2_FLAG_SWAP_BYTES;
145
146                 ext2fs_swap_super(fs->super);
147         }
148 #endif
149         
150         if (fs->super->s_magic != EXT2_SUPER_MAGIC) {
151                 retval = EXT2_ET_BAD_MAGIC;
152                 goto cleanup;
153         }
154         if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
155                 retval = EXT2_ET_REV_TOO_HIGH;
156                 goto cleanup;
157         }
158
159         /*
160          * Check for feature set incompatibility
161          */
162         if (!(flags & EXT2_FLAG_FORCE)) {
163                 if (fs->super->s_feature_incompat &
164                     ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
165                         retval = EXT2_ET_UNSUPP_FEATURE;
166                         goto cleanup;
167                 }
168                 if ((flags & EXT2_FLAG_RW) &&
169                     (fs->super->s_feature_ro_compat &
170                      ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
171                         retval = EXT2_ET_RO_UNSUPP_FEATURE;
172                         goto cleanup;
173                 }
174                 if (!(flags & EXT2_FLAG_JOURNAL_DEV_OK) &&
175                     (fs->super->s_feature_incompat &
176                      EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
177                         retval = EXT2_ET_UNSUPP_FEATURE;
178                         goto cleanup;
179                 }
180         }
181         
182         fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
183         if (fs->blocksize == 0) {
184                 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
185                 goto cleanup;
186         }
187         fs->fragsize = EXT2_FRAG_SIZE(fs->super);
188         fs->inode_blocks_per_group = ((fs->super->s_inodes_per_group *
189                                        EXT2_INODE_SIZE(fs->super) +
190                                        EXT2_BLOCK_SIZE(fs->super) - 1) /
191                                       EXT2_BLOCK_SIZE(fs->super));
192         if (block_size) {
193                 if (block_size != fs->blocksize) {
194                         retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
195                         goto cleanup;
196                 }
197         }
198         /*
199          * Set the blocksize to the filesystem's blocksize.
200          */
201         io_channel_set_blksize(fs->io, fs->blocksize);
202
203         /*
204          * If this is an external journal device, don't try to read
205          * the group descriptors, because they're not there.
206          */
207         if (fs->super->s_feature_incompat &
208             EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
209                 fs->group_desc_count = 0;
210                 *ret_fs = fs;
211                 return 0;
212         }
213         
214         /*
215          * Read group descriptors
216          */
217         blocks_per_group = EXT2_BLOCKS_PER_GROUP(fs->super);
218         if (blocks_per_group == 0 ||
219             blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(fs->super) ||
220             fs->inode_blocks_per_group > EXT2_MAX_INODES_PER_GROUP(fs->super)) {
221                 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
222                 goto cleanup;
223         }
224         fs->group_desc_count = (fs->super->s_blocks_count -
225                                 fs->super->s_first_data_block +
226                                 blocks_per_group - 1) / blocks_per_group;
227         fs->desc_blocks = (fs->group_desc_count +
228                            EXT2_DESC_PER_BLOCK(fs->super) - 1)
229                 / EXT2_DESC_PER_BLOCK(fs->super);
230         retval = ext2fs_get_mem(fs->desc_blocks * fs->blocksize,
231                                 (void **) &fs->group_desc);
232         if (retval)
233                 goto cleanup;
234         if (!group_block)
235                 group_block = fs->super->s_first_data_block;
236         dest = (char *) fs->group_desc;
237         groups_per_block = fs->blocksize / sizeof(struct ext2_group_desc);
238         for (i=0 ; i < fs->desc_blocks; i++) {
239                 blk = ext2fs_descriptor_block_loc(fs, group_block, i);
240                 retval = io_channel_read_blk(fs->io, blk, 1, dest);
241                 if (retval)
242                         goto cleanup;
243 #ifdef EXT2FS_ENABLE_SWAPFS
244                 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
245                         gdp = (struct ext2_group_desc *) dest;
246                         for (j=0; j < groups_per_block; j++)
247                                 ext2fs_swap_group_desc(gdp++);
248                 }
249 #endif
250                 dest += fs->blocksize;
251         }
252
253         *ret_fs = fs;
254         return 0;
255 cleanup:
256         ext2fs_free(fs);
257         return retval;
258 }
259