blob: a1c0d5bcbf083b2f42d6268a947d7a2d71f12751 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * openfs.c --- open an ext2 filesystem
3 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
Theodore Ts'o50e1e101997-04-26 13:58:21 +00005 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00006 * %Begin-Header%
Theodore Ts'o50e1e101997-04-26 13:58:21 +00007 * This file may be redistributed under the terms of the GNU Public
8 * License.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00009 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000010 */
11
12#include <stdio.h>
13#include <string.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000014#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000015#include <unistd.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000016#endif
17#if HAVE_STDLIB_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000018#include <stdlib.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000019#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000020#include <fcntl.h>
21#include <time.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000022#if HAVE_ERRNO_H
23#include <errno.h>
24#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000025#include <sys/stat.h>
26#include <sys/types.h>
27
Theodore Ts'o3839e651997-04-26 13:21:57 +000028#include <linux/ext2_fs.h>
29
30#include "ext2fs.h"
31
32/*
33 * Note: if superblock is non-zero, block-size must also be non-zero.
34 * Superblock and block_size can be zero to use the default size.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000035 *
36 * Valid flags for ext2fs_open()
37 *
38 * EXT2_FLAG_RW - Open the filesystem for read/write.
39 * EXT2_FLAG_FORCE - Open the filesystem even if some of the
40 * features aren't supported.
Theodore Ts'o3839e651997-04-26 13:21:57 +000041 */
42errcode_t ext2fs_open(const char *name, int flags, int superblock,
43 int block_size, io_manager manager, ext2_filsys *ret_fs)
44{
45 ext2_filsys fs;
46 errcode_t retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000047 int i, j, group_block, groups_per_block;
Theodore Ts'o3839e651997-04-26 13:21:57 +000048 char *dest;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000049 struct ext2_group_desc *gdp;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000050 struct ext2fs_sb *s;
Theodore Ts'o3839e651997-04-26 13:21:57 +000051
Theodore Ts'of3db3561997-04-26 13:34:30 +000052 EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);
53
Theodore Ts'o3839e651997-04-26 13:21:57 +000054 fs = (ext2_filsys) malloc(sizeof(struct struct_ext2_filsys));
55 if (!fs)
56 return ENOMEM;
57
58 memset(fs, 0, sizeof(struct struct_ext2_filsys));
Theodore Ts'of3db3561997-04-26 13:34:30 +000059 fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
Theodore Ts'o3839e651997-04-26 13:21:57 +000060 fs->flags = flags;
61 retval = manager->open(name, (flags & EXT2_FLAG_RW) ? IO_FLAG_RW : 0,
62 &fs->io);
63 if (retval)
64 goto cleanup;
65 fs->device_name = malloc(strlen(name)+1);
66 if (!fs->device_name) {
67 retval = ENOMEM;
68 goto cleanup;
69 }
70 strcpy(fs->device_name, name);
71 fs->super = malloc(SUPERBLOCK_SIZE);
72 if (!fs->super) {
73 retval = ENOMEM;
74 goto cleanup;
75 }
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 = EINVAL;
86 goto cleanup;
87 }
88 io_channel_set_blksize(fs->io, block_size);
Theodore Ts'of3db3561997-04-26 13:34:30 +000089 group_block = superblock + 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +000090 } else {
91 io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
92 superblock = 1;
Theodore Ts'of3db3561997-04-26 13:34:30 +000093 group_block = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000094 }
95 retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
96 fs->super);
97 if (retval)
98 goto cleanup;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000099
100 if ((fs->super->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) ||
Theodore Ts'o5c576471997-04-29 15:29:49 +0000101 (fs->flags & EXT2_FLAG_SWAP_BYTES)) {
102 fs->flags |= EXT2_FLAG_SWAP_BYTES;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000103
104 ext2fs_swap_super(fs->super);
105 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000106
107 if (fs->super->s_magic != EXT2_SUPER_MAGIC) {
108 retval = EXT2_ET_BAD_MAGIC;
109 goto cleanup;
110 }
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000111#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
Theodore Ts'of3db3561997-04-26 13:34:30 +0000117#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
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000123#endif
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000124 /*
125 * Check for feature set incompatibility
126 */
127 if (!(flags & EXT2_FLAG_FORCE)) {
128 s = (struct ext2fs_sb *) fs->super;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000129 if (s->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000130 retval = EXT2_ET_UNSUPP_FEATURE;
131 goto cleanup;
132 }
133 if ((flags & EXT2_FLAG_RW) &&
Theodore Ts'o521e3681997-04-29 17:48:10 +0000134 (s->s_feature_ro_compat &
135 ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000136 retval = EXT2_ET_RO_UNSUPP_FEATURE;
137 goto cleanup;
138 }
139 }
140
Theodore Ts'o3839e651997-04-26 13:21:57 +0000141 fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000142 if (fs->blocksize == 0) {
143 retval = EXT2_ET_CORRUPT_SUPERBLOCK;
144 goto cleanup;
145 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000146 fs->fragsize = EXT2_FRAG_SIZE(fs->super);
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000147 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));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000151 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 fs->group_desc = malloc(fs->desc_blocks * fs->blocksize);
173 if (!fs->group_desc) {
174 retval = ENOMEM;
175 goto cleanup;
176 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000177 if (!group_block)
178 group_block = fs->super->s_first_data_block + 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000179 dest = (char *) fs->group_desc;
180 for (i=0 ; i < fs->desc_blocks; i++) {
181 retval = io_channel_read_blk(fs->io, group_block, 1, dest);
182 if (retval)
183 goto cleanup;
184 group_block++;
Theodore Ts'o5c576471997-04-29 15:29:49 +0000185 if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000186 gdp = (struct ext2_group_desc *) dest;
187 groups_per_block = fs->blocksize /
188 sizeof(struct ext2_group_desc);
189 for (j=0; j < groups_per_block; j++)
190 ext2fs_swap_group_desc(gdp++);
191 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000192 dest += fs->blocksize;
193 }
194
195 *ret_fs = fs;
196 return 0;
197cleanup:
198 ext2fs_free(fs);
199 return retval;
200}
201