summaryrefslogtreecommitdiff
path: root/libparted/fs/ext2/interface.c
blob: 3b47866c64ca8e9d2809d7c428d541ffb2993eb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
    interface.c -- parted binding glue to libext2resize
    Copyright (C) 1998-2000, 2007-2014, 2019-2022 Free Software Foundation,
    Inc.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/* VERSION: libext2resize 1.1.6 (by Lennert)
 * merged 1.1.11 changes (by Andrew)
 */

#include <config.h>

#include <parted/parted.h>
#include "ext2.h"

static PedFileSystemType _ext2_type;
static PedFileSystemType _ext3_type;

struct ext2_dev_handle* ext2_make_dev_handle_from_parted_geometry(PedGeometry* geom);

static PedGeometry*
_ext2_generic_probe (PedGeometry* geom, int expect_ext_ver)
{
	struct ext2_super_block *sb;
	const int sectors = (4096 + geom->dev->sector_size - 1) /
			     geom->dev->sector_size;
	uint8_t *buf = alloca (sectors * geom->dev->sector_size);
	if (!ped_geometry_read(geom, buf, 0, sectors))
		return NULL;
	sb = (struct ext2_super_block *)(buf+1024);

	if (EXT2_SUPER_MAGIC(*sb) == EXT2_SUPER_MAGIC_CONST) {
		PedSector block_size = (EXT2_MIN_BLOCK_SIZE << (EXT2_SUPER_LOG_BLOCK_SIZE(*sb))) / geom->dev->sector_size;
		PedSector block_count = EXT2_SUPER_BLOCKS_COUNT(*sb);
		PedSector group_blocks = EXT2_SUPER_BLOCKS_PER_GROUP(*sb);
		PedSector group_nr = EXT2_SUPER_BLOCK_GROUP_NR(*sb);
		PedSector first_data_block = EXT2_SUPER_FIRST_DATA_BLOCK(*sb);
		int version = EXT2_SUPER_REV_LEVEL(*sb);
		int is_ext3 = 0;
		int is_ext4 = 0;

		is_ext3 = (EXT2_SUPER_FEATURE_COMPAT (*sb)
			   & EXT3_FEATURE_COMPAT_HAS_JOURNAL) != 0;
		if (is_ext3) {
			is_ext4 = ((EXT2_SUPER_FEATURE_RO_COMPAT (*sb)
				    & EXT4_FEATURE_RO_COMPAT_HUGE_FILE)
				   || (EXT2_SUPER_FEATURE_RO_COMPAT (*sb)
				       & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
				   || (EXT2_SUPER_FEATURE_RO_COMPAT (*sb)
				       & EXT4_FEATURE_RO_COMPAT_DIR_NLINK)
				   || (EXT2_SUPER_FEATURE_INCOMPAT (*sb)
				       & EXT4_FEATURE_INCOMPAT_EXTENTS)
				   || (EXT2_SUPER_FEATURE_INCOMPAT (*sb)
				       & EXT4_FEATURE_INCOMPAT_64BIT)
				   || (EXT2_SUPER_FEATURE_INCOMPAT (*sb)
				       & EXT4_FEATURE_INCOMPAT_FLEX_BG));
			if (is_ext4)
				is_ext3 = 0;
		}
		if (expect_ext_ver == 2 && (is_ext3 || is_ext4))
			return NULL;
		if (expect_ext_ver == 3 && !is_ext3)
			return NULL;
		else if (expect_ext_ver == 4 && !is_ext4)
			return NULL;

		if (version > 0 && group_nr > 0) {
			PedSector start;
			PedGeometry probe_geom;

			start = geom->start
					- group_blocks * group_nr
					- first_data_block;

			if (start < 0)
				return NULL;
			ped_geometry_init (&probe_geom, geom->dev,
					   start, block_count * block_size);
			return _ext2_generic_probe (&probe_geom,
                                                    expect_ext_ver);
		} else {
			return ped_geometry_new (geom->dev, geom->start,
						 block_count * block_size);
		}
	}

	return NULL;
}

static PedGeometry*
_ext2_probe (PedGeometry* geom)
{
	return _ext2_generic_probe (geom, 2);
}

static PedGeometry*
_ext3_probe (PedGeometry* geom)
{
	return _ext2_generic_probe (geom, 3);
}

static PedGeometry*
_ext4_probe (PedGeometry* geom)
{
	return _ext2_generic_probe (geom, 4);
}

static PedFileSystemOps _ext2_ops = {
	probe:		_ext2_probe,
};

static PedFileSystemOps _ext3_ops = {
	probe:		_ext3_probe,
};

static PedFileSystemOps _ext4_ops = {
	probe:		_ext4_probe,
};

static PedFileSystemType _ext2_type = {
       next:		 NULL,
       ops:		 &_ext2_ops,
       name:		 "ext2",
};

static PedFileSystemType _ext3_type = {
       next:		 NULL,
       ops:		 &_ext3_ops,
       name:		 "ext3",
};

static PedFileSystemType _ext4_type = {
       next:		 NULL,
       ops:		 &_ext4_ops,
       name:		 "ext4",
};

void ped_file_system_ext2_init ()
{
	ped_file_system_type_register (&_ext2_type);
	ped_file_system_type_register (&_ext3_type);
	ped_file_system_type_register (&_ext4_type);
}

void ped_file_system_ext2_done ()
{
	ped_file_system_type_unregister (&_ext2_type);
	ped_file_system_type_unregister (&_ext3_type);
	ped_file_system_type_unregister (&_ext4_type);
}