summaryrefslogtreecommitdiff
path: root/libparted/fs/ext2/interface.c
blob: 7b48f4c762d0cc0939bf738fe2b46e66cca2f4d2 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
    interface.c -- parted binding glue to libext2resize
    Copyright (C) 1998-2000 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 2 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, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/

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

#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <parted/parted.h>
#include "ext2.h"
#include "parted_io.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_ext3)
{
	struct ext2_super_block sb;

	if (!ped_geometry_read(geom, &sb, 2, 2))
		return NULL;

	if (EXT2_SUPER_MAGIC(sb) == EXT2_SUPER_MAGIC_CONST) {
		PedSector block_size = 1 << (EXT2_SUPER_LOG_BLOCK_SIZE(sb) + 1);
		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 = (EXT2_SUPER_FEATURE_COMPAT(sb) 
				& EXT3_FEATURE_COMPAT_HAS_JOURNAL) != 0;

		if (expect_ext3 != is_ext3)
			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_ext3);
		} 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, 0);
}

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

#ifndef DISCOVER_ONLY
static int
_ext2_clobber (PedGeometry* geom)
{
	struct ext2_super_block sb;

	if (!ped_geometry_read(geom, &sb, 2, 2))
		return 0;
	if (EXT2_SUPER_MAGIC(sb) != EXT2_SUPER_MAGIC_CONST)
		return 1;

	sb.s_magic = 0;
	return ped_geometry_write(geom, &sb, 2, 2);
}

static PedFileSystem*
_ext2_open (PedGeometry* geom)
{
	PedFileSystem*		fs;
	struct ext2_fs*		fs_info;
	struct ext2_dev_handle*	handle;

	fs = (PedFileSystem*) ped_malloc (sizeof (PedFileSystem));
	if (!fs) goto error;

	fs->type = &_ext2_type;
	fs->geom = ped_geometry_duplicate (geom);
	fs->checked = 1;

	handle = ext2_make_dev_handle_from_parted_geometry(fs->geom);
	if (!handle) goto error_free_fs;

	fs_info = (struct ext2_fs*) ext2_open(handle, 0);
	if (!fs_info) goto error_free_handle;

	fs->type_specific = (void*) fs_info;
	fs_info->opt_verbose = 0;

	return fs;

error_free_handle:
	ext2_destroy_dev_handle(handle);
error_free_fs:
	ped_free(fs);
error:
	return NULL;
}

static PedFileSystem*
_ext2_create (PedGeometry* geom, PedTimer* timer)
{
	PedFileSystem*		fs;
	struct ext2_fs*		fs_info;
	struct ext2_dev_handle*	handle;

	fs = (PedFileSystem*) ped_malloc (sizeof (PedFileSystem));
	if (!fs) goto error;

	fs->type = &_ext2_type;
	fs->geom = ped_geometry_duplicate (geom);

	handle = ext2_make_dev_handle_from_parted_geometry(fs->geom);
	if (!handle) goto error_free_fs;

	fs_info = ext2_mkfs (handle, 0, 0, 0, 0, -1, -1, timer);
	if (!fs_info) goto error_free_handle;

	fs->type_specific = (void*) fs_info;
	fs_info->opt_verbose = 0;

	return fs;

error_free_handle:
	ext2_destroy_dev_handle(handle);
error_free_fs:
	ped_free(fs);
error:
	return NULL;
}

static int
_ext2_close (PedFileSystem *fs)
{
	struct ext2_dev_handle* handle;

	handle = ((struct ext2_fs*)fs->type_specific)->devhandle;
	ext2_close(fs->type_specific);
	ext2_destroy_dev_handle(handle);

	ped_free(fs);
	return 1;
}

static int
_ext2_check (PedFileSystem *fs, PedTimer* timer)
{
	ped_exception_throw (PED_EXCEPTION_INFORMATION, PED_EXCEPTION_OK,
		_("The ext2 file system passed a basic check.  For a more "
		  "comprehensive check, use the e2fsck program."));
	return 1;
}

static int
_ext2_resize (PedFileSystem* fs, PedGeometry* geom, PedTimer* timer)
{
	struct ext2_fs* f;
	PedSector	old_length = fs->geom->length;

	PED_ASSERT (fs->geom->dev == geom->dev, return 0);

	if (fs->geom->start != geom->start)
	{
		ped_exception_throw (PED_EXCEPTION_NO_FEATURE,
		      PED_EXCEPTION_CANCEL,
		      _("Sorry, can't move the start of ext2 partitions yet!"));
		return 0;
	}

	geom->dev->boot_dirty = 1;

	f = (struct ext2_fs *) fs->type_specific;

/* ensure that the geometry contains the new and old geometry */
	if (old_length > geom->length) {
		if (!ext2_resize_fs(f, geom->length >> (f->logsize - 9),
				    timer))
			goto error;

		fs->geom->length = geom->length;
		fs->geom->end = fs->geom->start + geom->length - 1;
	} else {
		fs->geom->length = geom->length;
		fs->geom->end = fs->geom->start + geom->length - 1;

		if (!ext2_resize_fs(f, geom->length >> (f->logsize - 9),
				    timer))
			goto error;
	}
	return 1;

error:
	return 0;
}

static PedConstraint*
_ext2_get_create_constraint (const PedDevice* dev)
{
	PedGeometry	full_dev;

	if (!ped_geometry_init (&full_dev, dev, 0, dev->length - 1))
		return NULL;

	return ped_constraint_new (
			ped_alignment_any, ped_alignment_any,
			&full_dev, &full_dev,
			64, dev->length);
}

static PedConstraint*
_ext2_get_resize_constraint (const PedFileSystem* fs)
{
	struct ext2_fs* f = (struct ext2_fs *) fs->type_specific;
	PedDevice*	dev = fs->geom->dev;
	PedAlignment	start_align;
	PedGeometry	start_sector;
	PedGeometry	full_dev;
	PedSector	min_size;

	if (!ped_alignment_init (&start_align, fs->geom->start, 0))
		return NULL;
	if (!ped_geometry_init (&full_dev, dev, 0, dev->length - 1))
		return NULL;
	if (!ped_geometry_init (&start_sector, dev, fs->geom->start, 1))
		return NULL;
	min_size = (EXT2_SUPER_BLOCKS_COUNT(f->sb)
		   	- EXT2_SUPER_FREE_BLOCKS_COUNT(f->sb))
		   * (f->blocksize / dev->sector_size);

	return ped_constraint_new (&start_align, ped_alignment_any,
				   &start_sector, &full_dev, min_size,
				   dev->length);
}
#endif /* !DISCOVER_ONLY */

static PedFileSystemOps _ext2_ops = {
	probe:		_ext2_probe,
#ifndef DISCOVER_ONLY
	clobber:	_ext2_clobber,
	open:		_ext2_open,
	create:         _ext2_create,
	close:		_ext2_close,
	check:          _ext2_check,
	resize:		_ext2_resize,
	copy:           NULL,
	get_create_constraint:	_ext2_get_create_constraint,
	get_copy_constraint:	NULL,
	get_resize_constraint:	_ext2_get_resize_constraint
#else /* !DISCOVER_ONLY */
	clobber:	NULL,
	open:		NULL,
	create:         NULL,
	close:		NULL,
	check:          NULL,
	resize:		NULL,
	copy:           NULL,
	get_create_constraint:	NULL,
	get_copy_constraint:	NULL,
	get_resize_constraint:	NULL
#endif /* !DISCOVER_ONLY */
};

static PedFileSystemOps _ext3_ops = {
	probe:		_ext3_probe,
#ifndef DISCOVER_ONLY
	clobber:	_ext2_clobber,
	open:		_ext2_open,
	create:         NULL,
	close:		_ext2_close,
	check:          _ext2_check,
	resize:		_ext2_resize,
	copy:           NULL,
	get_create_constraint:	_ext2_get_create_constraint,
	get_copy_constraint:	NULL,
	get_resize_constraint:	_ext2_get_resize_constraint
#else /* !DISCOVER_ONLY */
	clobber:	NULL,
	open:		NULL,
	create:         NULL,
	close:		NULL,
	check:          NULL,
	resize:		NULL,
	copy:           NULL,
	get_create_constraint:	NULL,
	get_copy_constraint:	NULL,
	get_resize_constraint:	NULL
#endif /* !DISCOVER_ONLY */
};

#define EXT23_BLOCK_SIZES ((int[6]){512, 1024, 2048, 4096, 8192, 0})

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

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

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

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