~youscribe/parted/3.1

« back to all changes in this revision

Viewing changes to libparted/fs/ext2/interface.c

  • Committer: Guilhem Lettron
  • Date: 2012-10-22 14:37:59 UTC
  • Revision ID: guilhem+ubuntu@lettron.fr-20121022143759-m403kecgz13sknvp
3.1 from tarball

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    interface.c -- parted binding glue to libext2resize
 
3
    Copyright (C) 1998-2000, 2007-2012 Free Software Foundation, Inc.
 
4
 
 
5
    This program is free software; you can redistribute it and/or modify
 
6
    it under the terms of the GNU General Public License as published by
 
7
    the Free Software Foundation; either version 3 of the License, or
 
8
    (at your option) any later version.
 
9
 
 
10
    This program is distributed in the hope that it will be useful,
 
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
    GNU General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU General Public License
 
16
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
*/
 
18
 
 
19
/* VERSION: libext2resize 1.1.6 (by Lennert)
 
20
 * merged 1.1.11 changes (by Andrew)
 
21
 */
 
22
 
 
23
#include <config.h>
 
24
 
 
25
#include <parted/parted.h>
 
26
#include "ext2.h"
 
27
 
 
28
static PedFileSystemType _ext2_type;
 
29
static PedFileSystemType _ext3_type;
 
30
 
 
31
struct ext2_dev_handle* ext2_make_dev_handle_from_parted_geometry(PedGeometry* geom);
 
32
 
 
33
static PedGeometry*
 
34
_ext2_generic_probe (PedGeometry* geom, int expect_ext_ver)
 
35
{
 
36
        void *sb_v;
 
37
        if (!ped_geometry_read_alloc(geom, &sb_v, 2, 2))
 
38
                return NULL;
 
39
        struct ext2_super_block *sb = sb_v;
 
40
 
 
41
        if (EXT2_SUPER_MAGIC(*sb) == EXT2_SUPER_MAGIC_CONST) {
 
42
                PedSector block_size = 1 << (EXT2_SUPER_LOG_BLOCK_SIZE(*sb) + 1);
 
43
                PedSector block_count = EXT2_SUPER_BLOCKS_COUNT(*sb);
 
44
                PedSector group_blocks = EXT2_SUPER_BLOCKS_PER_GROUP(*sb);
 
45
                PedSector group_nr = EXT2_SUPER_BLOCK_GROUP_NR(*sb);
 
46
                PedSector first_data_block = EXT2_SUPER_FIRST_DATA_BLOCK(*sb);
 
47
                int version = EXT2_SUPER_REV_LEVEL(*sb);
 
48
                int is_ext3 = 0;
 
49
                int is_ext4 = 0;
 
50
 
 
51
                is_ext3 = (EXT2_SUPER_FEATURE_COMPAT (*sb)
 
52
                           & EXT3_FEATURE_COMPAT_HAS_JOURNAL) != 0;
 
53
                if (is_ext3) {
 
54
                        is_ext4 = ((EXT2_SUPER_FEATURE_RO_COMPAT (*sb)
 
55
                                    & EXT4_FEATURE_RO_COMPAT_HUGE_FILE)
 
56
                                   || (EXT2_SUPER_FEATURE_RO_COMPAT (*sb)
 
57
                                       & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
 
58
                                   || (EXT2_SUPER_FEATURE_RO_COMPAT (*sb)
 
59
                                       & EXT4_FEATURE_RO_COMPAT_DIR_NLINK)
 
60
                                   || (EXT2_SUPER_FEATURE_INCOMPAT (*sb)
 
61
                                       & EXT4_FEATURE_INCOMPAT_EXTENTS)
 
62
                                   || (EXT2_SUPER_FEATURE_INCOMPAT (*sb)
 
63
                                       & EXT4_FEATURE_INCOMPAT_64BIT)
 
64
                                   || (EXT2_SUPER_FEATURE_INCOMPAT (*sb)
 
65
                                       & EXT4_FEATURE_INCOMPAT_FLEX_BG));
 
66
                        if (is_ext4)
 
67
                                is_ext3 = 0;
 
68
                }
 
69
                free (sb);
 
70
 
 
71
                if (expect_ext_ver == 2 && (is_ext3 || is_ext4))
 
72
                        return NULL;
 
73
                if (expect_ext_ver == 3 && !is_ext3)
 
74
                        return NULL;
 
75
                else if (expect_ext_ver == 4 && !is_ext4)
 
76
                        return NULL;
 
77
 
 
78
                if (version > 0 && group_nr > 0) {
 
79
                        PedSector start;
 
80
                        PedGeometry probe_geom;
 
81
 
 
82
                        start = geom->start
 
83
                                        - group_blocks * group_nr
 
84
                                        - first_data_block;
 
85
 
 
86
                        if (start < 0)
 
87
                                return NULL;
 
88
                        ped_geometry_init (&probe_geom, geom->dev,
 
89
                                           start, block_count * block_size);
 
90
                        return _ext2_generic_probe (&probe_geom,
 
91
                                                    expect_ext_ver);
 
92
                } else {
 
93
                        return ped_geometry_new (geom->dev, geom->start,
 
94
                                                 block_count * block_size);
 
95
                }
 
96
        }
 
97
        else {
 
98
                free (sb);
 
99
        }
 
100
 
 
101
        return NULL;
 
102
}
 
103
 
 
104
static PedGeometry*
 
105
_ext2_probe (PedGeometry* geom)
 
106
{
 
107
        return _ext2_generic_probe (geom, 2);
 
108
}
 
109
 
 
110
static PedGeometry*
 
111
_ext3_probe (PedGeometry* geom)
 
112
{
 
113
        return _ext2_generic_probe (geom, 3);
 
114
}
 
115
 
 
116
static PedGeometry*
 
117
_ext4_probe (PedGeometry* geom)
 
118
{
 
119
        return _ext2_generic_probe (geom, 4);
 
120
}
 
121
 
 
122
static PedFileSystemOps _ext2_ops = {
 
123
        probe:          _ext2_probe,
 
124
};
 
125
 
 
126
static PedFileSystemOps _ext3_ops = {
 
127
        probe:          _ext3_probe,
 
128
};
 
129
 
 
130
static PedFileSystemOps _ext4_ops = {
 
131
        probe:          _ext4_probe,
 
132
};
 
133
 
 
134
#define EXT23_BLOCK_SIZES ((int[6]){512, 1024, 2048, 4096, 8192, 0})
 
135
 
 
136
static PedFileSystemType _ext2_type = {
 
137
       next:             NULL,
 
138
       ops:              &_ext2_ops,
 
139
       name:             "ext2",
 
140
       block_sizes:      EXT23_BLOCK_SIZES
 
141
};
 
142
 
 
143
static PedFileSystemType _ext3_type = {
 
144
       next:             NULL,
 
145
       ops:              &_ext3_ops,
 
146
       name:             "ext3",
 
147
       block_sizes:      EXT23_BLOCK_SIZES
 
148
};
 
149
 
 
150
static PedFileSystemType _ext4_type = {
 
151
       next:             NULL,
 
152
       ops:              &_ext4_ops,
 
153
       name:             "ext4",
 
154
       block_sizes:      EXT23_BLOCK_SIZES
 
155
};
 
156
 
 
157
void ped_file_system_ext2_init ()
 
158
{
 
159
        ped_file_system_type_register (&_ext2_type);
 
160
        ped_file_system_type_register (&_ext3_type);
 
161
        ped_file_system_type_register (&_ext4_type);
 
162
}
 
163
 
 
164
void ped_file_system_ext2_done ()
 
165
{
 
166
        ped_file_system_type_unregister (&_ext2_type);
 
167
        ped_file_system_type_unregister (&_ext3_type);
 
168
        ped_file_system_type_unregister (&_ext4_type);
 
169
}