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 Theodore Ts'o.
5  * 
6  * This file may be redistributed under the terms of the GNU Public
7  * License.
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #if HAVE_UNISTD_H
13 #include <unistd.h>
14 #endif
15 #if HAVE_STDLIB_H
16 #include <stdlib.h>
17 #endif
18 #include <fcntl.h>
19 #include <time.h>
20 #if HAVE_ERRNO_H
21 #include <errno.h>
22 #endif
23 #include <sys/stat.h>
24 #include <sys/types.h>
25
26 #include <linux/ext2_fs.h>
27
28 #include "ext2fs.h"
29
30 /*
31  *  Note: if superblock is non-zero, block-size must also be non-zero.
32  *      Superblock and block_size can be zero to use the default size.
33  */
34 errcode_t ext2fs_open(const char *name, int flags, int superblock,
35                       int block_size, io_manager manager, ext2_filsys *ret_fs)
36 {
37         ext2_filsys     fs;
38         errcode_t       retval;
39         int             i, j, group_block, groups_per_block;
40         char            *dest;
41         struct ext2_group_desc *gdp;
42         
43         EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);
44         
45         fs = (ext2_filsys) malloc(sizeof(struct struct_ext2_filsys));
46         if (!fs)
47                 return ENOMEM;
48         
49         memset(fs, 0, sizeof(struct struct_ext2_filsys));
50         fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
51         fs->flags = flags;
52         retval = manager->open(name, (flags & EXT2_FLAG_RW) ? IO_FLAG_RW : 0,
53                                &fs->io);
54         if (retval)
55                 goto cleanup;
56         fs->device_name = malloc(strlen(name)+1);
57         if (!fs->device_name) {
58                 retval = ENOMEM;
59                 goto cleanup;
60         }
61         strcpy(fs->device_name, name);
62         fs->super = malloc(SUPERBLOCK_SIZE);
63         if (!fs->super) {
64                 retval = ENOMEM;
65                 goto cleanup;
66         }
67
68         /*
69          * If the user specifies a specific block # for the
70          * superblock, then he/she must also specify the block size!
71          * Otherwise, read the master superblock located at offset
72          * SUPERBLOCK_OFFSET from the start of the partition.
73          */
74         if (superblock) {
75                 if (!block_size) {
76                         retval = EINVAL;
77                         goto cleanup;
78                 }
79                 io_channel_set_blksize(fs->io, block_size);
80                 group_block = superblock + 1;
81         } else {
82                 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
83                 superblock = 1;
84                 group_block = 0;
85         }
86         retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
87                                      fs->super);
88         if (retval)
89                 goto cleanup;
90
91         if ((fs->super->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) ||
92             (fs->flags & EXT2_SWAP_BYTES)) {
93                 fs->flags |= EXT2_SWAP_BYTES;
94
95                 ext2fs_swap_super(fs->super);
96         }
97         
98         if (fs->super->s_magic != EXT2_SUPER_MAGIC) {
99                 retval = EXT2_ET_BAD_MAGIC;
100                 goto cleanup;
101         }
102 #ifdef  EXT2_CURRENT_REV
103         if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
104                 retval = EXT2_ET_REV_TOO_HIGH;
105                 goto cleanup;
106         }
107 #endif
108         fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
109         fs->fragsize = EXT2_FRAG_SIZE(fs->super);
110         fs->inode_blocks_per_group = (fs->super->s_inodes_per_group /
111                                       EXT2_INODES_PER_BLOCK(fs->super));
112         if (block_size) {
113                 if (block_size != fs->blocksize) {
114                         retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
115                         goto cleanup;
116                 }
117         }
118         /*
119          * Set the blocksize to the filesystem's blocksize.
120          */
121         io_channel_set_blksize(fs->io, fs->blocksize);
122         
123         /*
124          * Read group descriptors
125          */
126         fs->group_desc_count = (fs->super->s_blocks_count -
127                                 fs->super->s_first_data_block +
128                                 EXT2_BLOCKS_PER_GROUP(fs->super) - 1)
129                 / EXT2_BLOCKS_PER_GROUP(fs->super);
130         fs->desc_blocks = (fs->group_desc_count +
131                            EXT2_DESC_PER_BLOCK(fs->super) - 1)
132                 / EXT2_DESC_PER_BLOCK(fs->super);
133         fs->group_desc = malloc(fs->desc_blocks * fs->blocksize);
134         if (!fs->group_desc) {
135                 retval = ENOMEM;
136                 goto cleanup;
137         }
138         if (!group_block)
139                 group_block = fs->super->s_first_data_block + 1;
140         dest = (char *) fs->group_desc;
141         for (i=0 ; i < fs->desc_blocks; i++) {
142                 retval = io_channel_read_blk(fs->io, group_block, 1, dest);
143                 if (retval)
144                         goto cleanup;
145                 group_block++;
146                 if (fs->flags & EXT2_SWAP_BYTES) {
147                         gdp = (struct ext2_group_desc *) dest;
148                         groups_per_block = fs->blocksize /
149                                 sizeof(struct ext2_group_desc);
150                         for (j=0; j < groups_per_block; j++)
151                                 ext2fs_swap_group_desc(gdp++);
152                 }
153                 dest += fs->blocksize;
154         }
155
156         *ret_fs = fs;
157         return 0;
158 cleanup:
159         ext2fs_free(fs);
160         return retval;
161 }
162