2 interface.c -- parted binding glue to libext2resize
3 Copyright (C) 1998-2000, 2007-2011 Free Software Foundation, Inc.
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.
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.
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/>.
19 /* VERSION: libext2resize 1.1.6 (by Lennert)
20 * merged 1.1.11 changes (by Andrew)
25 #include <parted/parted.h>
27 #include "parted_io.h"
29 static PedFileSystemType _ext2_type;
30 static PedFileSystemType _ext3_type;
32 struct ext2_dev_handle* ext2_make_dev_handle_from_parted_geometry(PedGeometry* geom);
35 _ext2_generic_probe (PedGeometry* geom, int expect_ext_ver)
38 if (!ped_geometry_read_alloc(geom, &sb_v, 2, 2))
40 struct ext2_super_block *sb = sb_v;
42 if (EXT2_SUPER_MAGIC(*sb) == EXT2_SUPER_MAGIC_CONST) {
43 PedSector block_size = 1 << (EXT2_SUPER_LOG_BLOCK_SIZE(*sb) + 1);
44 PedSector block_count = EXT2_SUPER_BLOCKS_COUNT(*sb);
45 PedSector group_blocks = EXT2_SUPER_BLOCKS_PER_GROUP(*sb);
46 PedSector group_nr = EXT2_SUPER_BLOCK_GROUP_NR(*sb);
47 PedSector first_data_block = EXT2_SUPER_FIRST_DATA_BLOCK(*sb);
48 int version = EXT2_SUPER_REV_LEVEL(*sb);
52 is_ext3 = (EXT2_SUPER_FEATURE_COMPAT (*sb)
53 & EXT3_FEATURE_COMPAT_HAS_JOURNAL) != 0;
55 is_ext4 = ((EXT2_SUPER_FEATURE_RO_COMPAT (*sb)
56 & EXT4_FEATURE_RO_COMPAT_HUGE_FILE)
57 || (EXT2_SUPER_FEATURE_RO_COMPAT (*sb)
58 & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
59 || (EXT2_SUPER_FEATURE_RO_COMPAT (*sb)
60 & EXT4_FEATURE_RO_COMPAT_DIR_NLINK)
61 || (EXT2_SUPER_FEATURE_INCOMPAT (*sb)
62 & EXT4_FEATURE_INCOMPAT_EXTENTS)
63 || (EXT2_SUPER_FEATURE_INCOMPAT (*sb)
64 & EXT4_FEATURE_INCOMPAT_64BIT)
65 || (EXT2_SUPER_FEATURE_INCOMPAT (*sb)
66 & EXT4_FEATURE_INCOMPAT_FLEX_BG));
72 if (expect_ext_ver == 2 && (is_ext3 || is_ext4))
74 if (expect_ext_ver == 3 && !is_ext3)
76 else if (expect_ext_ver == 4 && !is_ext4)
79 if (version > 0 && group_nr > 0) {
81 PedGeometry probe_geom;
84 - group_blocks * group_nr
89 ped_geometry_init (&probe_geom, geom->dev,
90 start, block_count * block_size);
91 return _ext2_generic_probe (&probe_geom,
94 return ped_geometry_new (geom->dev, geom->start,
95 block_count * block_size);
106 _ext2_probe (PedGeometry* geom)
108 return _ext2_generic_probe (geom, 2);
112 _ext3_probe (PedGeometry* geom)
114 return _ext2_generic_probe (geom, 3);
118 _ext4_probe (PedGeometry* geom)
120 return _ext2_generic_probe (geom, 4);
123 #ifndef DISCOVER_ONLY
125 _ext2_clobber (PedGeometry* geom)
130 if (ped_geometry_read_alloc(geom, &sb_v, 2, 2)) {
131 struct ext2_super_block *sb = sb_v;
132 /* Clobber only if there's a matching magic number. */
133 if (EXT2_SUPER_MAGIC(*sb) != EXT2_SUPER_MAGIC_CONST) {
137 ok = ped_geometry_write(geom, sb, 2, 2);
144 static PedFileSystem*
145 _ext2_open (PedGeometry* geom)
148 struct ext2_fs* fs_info;
149 struct ext2_dev_handle* handle;
151 fs = (PedFileSystem*) ped_malloc (sizeof (PedFileSystem));
154 fs->type = &_ext2_type;
155 fs->geom = ped_geometry_duplicate (geom);
158 handle = ext2_make_dev_handle_from_parted_geometry(fs->geom);
159 if (!handle) goto error_free_fs;
161 fs_info = (struct ext2_fs*) ext2_open(handle, 0);
162 if (!fs_info) goto error_free_handle;
164 fs->type_specific = (void*) fs_info;
165 fs_info->opt_verbose = 0;
170 ext2_destroy_dev_handle(handle);
177 static PedFileSystem*
178 _ext2_create (PedGeometry* geom, PedTimer* timer)
181 struct ext2_fs* fs_info;
182 struct ext2_dev_handle* handle;
184 fs = (PedFileSystem*) ped_malloc (sizeof (PedFileSystem));
187 fs->type = &_ext2_type;
188 fs->geom = ped_geometry_duplicate (geom);
190 handle = ext2_make_dev_handle_from_parted_geometry(fs->geom);
191 if (!handle) goto error_free_fs;
193 fs_info = ext2_mkfs (handle, 0, 0, 0, 0, -1, -1, timer);
194 if (!fs_info) goto error_free_handle;
196 fs->type_specific = (void*) fs_info;
197 fs_info->opt_verbose = 0;
202 ext2_destroy_dev_handle(handle);
210 _ext2_close (PedFileSystem *fs)
212 struct ext2_dev_handle* handle;
214 handle = ((struct ext2_fs*)fs->type_specific)->devhandle;
215 ext2_close(fs->type_specific);
216 ext2_destroy_dev_handle(handle);
223 _ext2_check (PedFileSystem *fs, PedTimer* timer)
225 ped_exception_throw (PED_EXCEPTION_INFORMATION, PED_EXCEPTION_OK,
226 _("The ext2 file system passed a basic check. For a more "
227 "comprehensive check, use the e2fsck program."));
232 _ext2_resize (PedFileSystem* fs, PedGeometry* geom, PedTimer* timer)
235 PedSector old_length = fs->geom->length;
237 PED_ASSERT (fs->geom->dev == geom->dev);
239 if (fs->geom->start != geom->start)
241 ped_exception_throw (PED_EXCEPTION_NO_FEATURE,
242 PED_EXCEPTION_CANCEL,
243 _("Sorry, can't move the start of ext2 partitions yet!"));
247 geom->dev->boot_dirty = 1;
249 f = (struct ext2_fs *) fs->type_specific;
251 /* ensure that the geometry contains the new and old geometry */
252 if (old_length > geom->length) {
253 if (!ext2_resize_fs(f, geom->length >> (f->logsize - 9),
257 fs->geom->length = geom->length;
258 fs->geom->end = fs->geom->start + geom->length - 1;
260 fs->geom->length = geom->length;
261 fs->geom->end = fs->geom->start + geom->length - 1;
263 if (!ext2_resize_fs(f, geom->length >> (f->logsize - 9),
273 static PedConstraint*
274 _ext2_get_create_constraint (const PedDevice* dev)
276 PedGeometry full_dev;
278 if (!ped_geometry_init (&full_dev, dev, 0, dev->length - 1))
281 return ped_constraint_new (
282 ped_alignment_any, ped_alignment_any,
283 &full_dev, &full_dev,
287 static PedConstraint*
288 _ext2_get_resize_constraint (const PedFileSystem* fs)
290 struct ext2_fs* f = (struct ext2_fs *) fs->type_specific;
291 PedDevice* dev = fs->geom->dev;
292 PedAlignment start_align;
293 PedGeometry start_sector;
294 PedGeometry full_dev;
297 if (!ped_alignment_init (&start_align, fs->geom->start, 0))
299 if (!ped_geometry_init (&full_dev, dev, 0, dev->length - 1))
301 if (!ped_geometry_init (&start_sector, dev, fs->geom->start, 1))
303 min_size = (EXT2_SUPER_BLOCKS_COUNT(f->sb)
304 - EXT2_SUPER_FREE_BLOCKS_COUNT(f->sb))
305 * (f->blocksize / dev->sector_size);
307 return ped_constraint_new (&start_align, ped_alignment_any,
308 &start_sector, &full_dev, min_size,
311 #endif /* !DISCOVER_ONLY */
313 static PedFileSystemOps _ext2_ops = {
317 static PedFileSystemOps _ext3_ops = {
321 static PedFileSystemOps _ext4_ops = {
325 #define EXT23_BLOCK_SIZES ((int[6]){512, 1024, 2048, 4096, 8192, 0})
327 static PedFileSystemType _ext2_type = {
331 block_sizes: EXT23_BLOCK_SIZES
334 static PedFileSystemType _ext3_type = {
338 block_sizes: EXT23_BLOCK_SIZES
341 static PedFileSystemType _ext4_type = {
345 block_sizes: EXT23_BLOCK_SIZES
348 void ped_file_system_ext2_init ()
350 ped_file_system_type_register (&_ext2_type);
351 ped_file_system_type_register (&_ext3_type);
352 ped_file_system_type_register (&_ext4_type);
355 void ped_file_system_ext2_done ()
357 ped_file_system_type_unregister (&_ext2_type);
358 ped_file_system_type_unregister (&_ext3_type);
359 ped_file_system_type_unregister (&_ext4_type);