[parted-devel] [PATCH 3/3] Put the label specific logic in the label files.

Joel Granados Moreno jgranado at redhat.com
Thu May 14 17:06:55 UTC 2009


* include/parted/disk.h : Definition of the new function to check for
  label specific restrictions.
* libparted/disk.c (_check_extended_partition) : Erase all the label
  specific requests.
* libparted/labels/dvh.c (dvh_partition_check) : Implement the new
  function.  Check if the sector count for the partition size and the
  starting sector exceed 2^32 sectors.
* libparted/labels/dos.c (dos_partition_check) : Likewise.
* libparted/labels/aix.c (aix_partition_check) : Implement the new
  function for restrictions in this label type.  For now it just returns
  true.
* libparted/labels/bsd.c (bsd_partition_check) : Likewise.
* libparted/labels/gpt.c (gpt_partition_check) : Likewise.
* libparted/labels/loop.c (loop_partition_check) : Likewise.
* libparted/labels/mac.c (mac_partition_check) : Likewise.
* libparted/labels/pc98.c (pc98_partition_check) : Likewise.
* libparted/labels/rdb.c (rdb_partition_check) : Likewise.
* libparted/labels/sun.c (sun_partition_check) : Likewise.
---
 include/parted/disk.h   |    1 +
 libparted/disk.c        |   64 +---------------------------------------------
 libparted/labels/aix.c  |    7 +++++
 libparted/labels/bsd.c  |    7 +++++
 libparted/labels/dos.c  |   34 +++++++++++++++++++++++++
 libparted/labels/dvh.c  |   35 +++++++++++++++++++++++++
 libparted/labels/gpt.c  |    7 +++++
 libparted/labels/loop.c |    7 +++++
 libparted/labels/mac.c  |    7 +++++
 libparted/labels/pc98.c |    7 +++++
 libparted/labels/rdb.c  |    8 +++++-
 libparted/labels/sun.c  |    7 +++++
 12 files changed, 128 insertions(+), 63 deletions(-)

diff --git a/include/parted/disk.h b/include/parted/disk.h
index 691f413..beec922 100644
--- a/include/parted/disk.h
+++ b/include/parted/disk.h
@@ -207,6 +207,7 @@ struct _PedDiskOps {
         int (*partition_align) (PedPartition* part,
                                 const PedConstraint* constraint);
         int (*partition_enumerate) (PedPartition* part);
+        bool (*partition_check) (const PedPartition* part);
 
         /* other */
         int (*alloc_metadata) (PedDisk* disk);
diff --git a/libparted/disk.c b/libparted/disk.c
index 5fb8060..5f37091 100644
--- a/libparted/disk.c
+++ b/libparted/disk.c
@@ -1693,31 +1693,6 @@ _check_extended_partition (PedDisk* disk, PedPartition* part)
 	return 1;
 }
 
-static PedSector
-_partition_max_start (char const *label_type)
-{
-  /* List partition table names (a la disk->type->name) for which
-     the partition length, in sectors, must fit in 32 bytes.  */
-  static char const *const max_32[] = {"msdos", "dvh"};
-  unsigned int i;
-
-  for (i = 0; i < sizeof max_32 / sizeof *max_32; i++)
-    if (strcmp (label_type, max_32[i]) == 0)
-      return UINT32_MAX;
-
-  return TYPE_MAXIMUM (PedSector);
-}
-
-static PedSector
-_partition_max_len (char const *label_type)
-{
-  /* NOTE: for now, they happen to be the same, so don't
-     duplicate needlessly.  Of course, if there's some format
-     with different length and starting sector limits, then
-     these functions will diverge.  */
-  return _partition_max_start (label_type);
-}
-
 static int
 _check_partition (PedDisk* disk, PedPartition* part)
 {
@@ -1758,44 +1733,9 @@ _check_partition (PedDisk* disk, PedPartition* part)
 		return 0;
 	}
 
-	if (!(part->type & PED_PARTITION_METADATA)) {
-		char const *label_type = disk->type->name;
-		/* Enforce some restrictions inherent in the DOS
-		   partition table format.  Without these, one would be able
-		   to create a 2TB partition (or larger), and it would work,
-		   but only until the next reboot.  This was insidious: the
-		   too-large partition would work initially, because with
-		   Linux-2.4.x and newer we set the partition start sector
-		   and length (in sectors) accurately and directly via the
-		   BLKPG ioctl.  However, only the last 32 bits of each
-		   number would be written to the partition table, and the
-		   next time the system would read/use those corrupted numbers
-		   it would usually complain about an invalid partition.
-                   The same applies to the starting sector number.  */
-
-		if (part->geom.length > _partition_max_len (label_type)) {
-			ped_exception_throw (
-				PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
-				_("partition length of %jd sectors exceeds the "
-                                  "%s-partition-table-imposed maximum of %jd"),
-				part->geom.length,
-                                label_type,
-                                _partition_max_len (label_type));
+	if (!(part->type & PED_PARTITION_METADATA))
+		if (!disk->type->ops->partition_check(part))
 			return 0;
-		}
-
-		/* The starting sector number must fit in 32 bytes.  */
-		if (part->geom.start > _partition_max_start (label_type)) {
-			ped_exception_throw (
-				PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
-				_("starting sector number, %jd exceeds the"
-                                  " %s-partition-table-imposed maximum of %jd"),
-                                part->geom.start,
-                                label_type,
-                                _partition_max_start (label_type));
-			return 0;
-		}
-	}
 
 	return 1;
 }
diff --git a/libparted/labels/aix.c b/libparted/labels/aix.c
index de81270..34fbb6c 100644
--- a/libparted/labels/aix.c
+++ b/libparted/labels/aix.c
@@ -250,6 +250,12 @@ aix_alloc_metadata (PedDisk* disk)
 	return 1;
 }
 
+static bool
+aix_partition_check (const PedPartition* part)
+{
+	return true;
+}
+
 static PedDiskOps aix_disk_ops = {
 	probe:			aix_probe,
 #ifndef DISCOVER_ONLY
@@ -276,6 +282,7 @@ static PedDiskOps aix_disk_ops = {
 	partition_is_flag_available:	aix_partition_is_flag_available,
 	partition_align:	aix_partition_align,
 	partition_enumerate:	aix_partition_enumerate,
+	partition_check:	aix_partition_check,
 	alloc_metadata:		aix_alloc_metadata,
 	get_max_primary_partition_count:
 				aix_get_max_primary_partition_count,
diff --git a/libparted/labels/bsd.c b/libparted/labels/bsd.c
index 3d6b5ab..ccfa2b4 100644
--- a/libparted/labels/bsd.c
+++ b/libparted/labels/bsd.c
@@ -633,6 +633,12 @@ error:
 	return 0;
 }
 
+static bool
+bsd_partition_check (const PedPartition* part)
+{
+	return true;
+}
+
 static PedDiskOps bsd_disk_ops = {
 	probe:			bsd_probe,
 #ifndef DISCOVER_ONLY
@@ -661,6 +667,7 @@ static PedDiskOps bsd_disk_ops = {
 	partition_get_name:	NULL,
 	partition_align:	bsd_partition_align,
 	partition_enumerate:	bsd_partition_enumerate,
+	partition_check:	bsd_partition_check,
 
 	alloc_metadata:		bsd_alloc_metadata,
 	get_max_primary_partition_count:
diff --git a/libparted/labels/dos.c b/libparted/labels/dos.c
index f724722..7f4d698 100644
--- a/libparted/labels/dos.c
+++ b/libparted/labels/dos.c
@@ -2269,6 +2269,39 @@ msdos_get_max_supported_partition_count(const PedDisk* disk, int *max_n)
 	return true;
 }
 
+/*
+ * Enforce some restrictions inherent in the DOS partition table format.
+ * 1. Partition size can not exceed 2^32 (unsigned int) sectors.  If sector
+ *    size is 512 bytes, this results in 2T aprox.
+ * 2. Partition starting sector can not exceed 2.^32 (unsigned int) sectors.
+ */
+static bool
+msdos_partition_check (const PedPartition* part)
+{
+	if (part->geom.length > UINT32_MAX) {
+		ped_exception_throw (
+			PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
+			_("partition length of %jd sectors exceeds the "
+			  "msdos-partition-table-imposed maximum of %jd"),
+			part->geom.length,
+			UINT32_MAX);
+		return false;
+	}
+
+	/* The starting sector number must fit in 32 bytes.  */
+	if (part->geom.start > UINT32_MAX) {
+		ped_exception_throw (
+			PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
+			_("starting sector number, %jd exceeds the"
+			  " msdos-partition-table-imposed maximum of %jd"),
+			part->geom.start,
+			UINT32_MAX);
+		return false;
+	}
+
+	return true;
+}
+
 static PedDiskOps msdos_disk_ops = {
 	probe:			msdos_probe,
 #ifndef DISCOVER_ONLY
@@ -2297,6 +2330,7 @@ static PedDiskOps msdos_disk_ops = {
 	partition_get_name:	NULL,
 	partition_align:	msdos_partition_align,
 	partition_enumerate:	msdos_partition_enumerate,
+	partition_check:	msdos_partition_check,
 
 	alloc_metadata:		msdos_alloc_metadata,
 	get_max_primary_partition_count:
diff --git a/libparted/labels/dvh.c b/libparted/labels/dvh.c
index 93de8f9..9a4693e 100644
--- a/libparted/labels/dvh.c
+++ b/libparted/labels/dvh.c
@@ -865,6 +865,40 @@ error:
 	return 0;
 }
 
+/*
+ * Enforce some restrictions inherent in the bvh partition table format.
+ * 1. Partition size can not exceed 2^32 (unsigned int) sectors.  If sector
+ *    size is 512 bytes, this results in 2T aprox.
+ * 2. Partition starting sector can not exceed 2.^32 (unsigned int) sectors.
+ */
+static bool
+dvh_partition_check (const PedPartition* part)
+{
+	if (part->geom.length > UINT32_MAX) {
+		ped_exception_throw (
+			PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
+			_("partition length of %jd sectors exceeds the "
+			  "dhv-partition-table-imposed maximum of %jd"),
+			part->geom.length,
+			UINT32_MAX);
+		return false;
+	}
+
+	/* The starting sector number must fit in 32 bytes.  */
+	if (part->geom.start > UINT32_MAX) {
+		ped_exception_throw (
+			PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
+			_("starting sector number, %jd exceeds the"
+			  " dhv-partition-table-imposed maximum of %jd"),
+			part->geom.start,
+			UINT32_MAX);
+		return false;
+	}
+
+	return true;
+
+}
+
 static PedDiskOps dvh_disk_ops = {
 	probe:			dvh_probe,
 #ifndef DISCOVER_ONLY
@@ -893,6 +927,7 @@ static PedDiskOps dvh_disk_ops = {
 	partition_get_name:	dvh_partition_get_name,
 	partition_align:	dvh_partition_align,
 	partition_enumerate:	dvh_partition_enumerate,
+	partition_check:	dvh_partition_check,
 
 	alloc_metadata:		dvh_alloc_metadata,
 	get_max_primary_partition_count: dvh_get_max_primary_partition_count,
diff --git a/libparted/labels/gpt.c b/libparted/labels/gpt.c
index 765c8a5..2dfd77e 100644
--- a/libparted/labels/gpt.c
+++ b/libparted/labels/gpt.c
@@ -1551,6 +1551,12 @@ gpt_partition_align (PedPartition* part, const PedConstraint* constraint)
 	return 0;
 }
 
+static bool
+gpt_partition_check (const PedPartition* part)
+{
+	return true;
+}
+
 static PedDiskOps gpt_disk_ops = {
 	probe:		gpt_probe,
 #ifndef DISCOVER_ONLY
@@ -1578,6 +1584,7 @@ static PedDiskOps gpt_disk_ops = {
 	partition_get_name:		gpt_partition_get_name,
 	partition_align:		gpt_partition_align,
 	partition_enumerate:		gpt_partition_enumerate,
+	partition_check:		gpt_partition_check,
 	alloc_metadata:			gpt_alloc_metadata,
 	get_max_primary_partition_count: gpt_get_max_primary_partition_count,
 	get_max_supported_partition_count: gpt_get_max_supported_partition_count
diff --git a/libparted/labels/loop.c b/libparted/labels/loop.c
index 10ba29e..9e4014e 100644
--- a/libparted/labels/loop.c
+++ b/libparted/labels/loop.c
@@ -288,6 +288,12 @@ loop_get_max_supported_partition_count (const PedDisk* disk, int *max_n)
 	return true;
 }
 
+static bool
+loop_partition_check (const PedPartition* part)
+{
+	return true;
+}
+
 static PedDiskOps loop_disk_ops = {
 	probe:			loop_probe,
 #ifndef DISCOVER_ONLY
@@ -316,6 +322,7 @@ static PedDiskOps loop_disk_ops = {
 	partition_get_name:	NULL,
 	partition_align:	loop_partition_align,
 	partition_enumerate:	loop_partition_enumerate,
+	partition_check:	loop_partition_check,
 
 	alloc_metadata:		loop_alloc_metadata,
 	get_max_primary_partition_count:
diff --git a/libparted/labels/mac.c b/libparted/labels/mac.c
index c1f3dc7..2cd65c8 100644
--- a/libparted/labels/mac.c
+++ b/libparted/labels/mac.c
@@ -1571,6 +1571,12 @@ mac_get_max_supported_partition_count (const PedDisk* disk, int *max_n)
 	return true;
 }
 
+static bool
+mac_partition_check (const PedPartition* part)
+{
+	return true;
+}
+
 static PedDiskOps mac_disk_ops = {
 	probe:			mac_probe,
 #ifndef DISCOVER_ONLY
@@ -1601,6 +1607,7 @@ static PedDiskOps mac_disk_ops = {
 	partition_get_name:	mac_partition_get_name,
 	partition_align:	mac_partition_align,
 	partition_enumerate:	mac_partition_enumerate,
+	partition_check:	mac_partition_check,
 
 	alloc_metadata:		mac_alloc_metadata,
 	get_max_primary_partition_count:
diff --git a/libparted/labels/pc98.c b/libparted/labels/pc98.c
index f392bea..70986d6 100644
--- a/libparted/labels/pc98.c
+++ b/libparted/labels/pc98.c
@@ -839,6 +839,12 @@ pc98_get_max_supported_partition_count (const PedDisk* disk, int *max_n)
 	return true;
 }
 
+static bool
+pc98_partition_check (const PedPartition* part)
+{
+	return true;
+}
+
 static PedDiskOps pc98_disk_ops = {
 	probe:			pc98_probe,
 #ifndef DISCOVER_ONLY
@@ -867,6 +873,7 @@ static PedDiskOps pc98_disk_ops = {
 	partition_get_name:	pc98_partition_get_name,
 	partition_align:	pc98_partition_align,
 	partition_enumerate:	pc98_partition_enumerate,
+	partition_check:	pc98_partition_check,
 
 	alloc_metadata:		pc98_alloc_metadata,
 	get_max_primary_partition_count:
diff --git a/libparted/labels/rdb.c b/libparted/labels/rdb.c
index c39230d..074f599 100644
--- a/libparted/labels/rdb.c
+++ b/libparted/labels/rdb.c
@@ -1131,6 +1131,12 @@ amiga_get_max_supported_partition_count (const PedDisk* disk, int *max_n)
 	return true;
 }
 
+static bool
+amiga_partition_check (const PedPartition* part)
+{
+	return true;
+}
+
 static PedDiskOps amiga_disk_ops = {
 	probe:			amiga_probe,
 #ifndef DISCOVER_ONLY
@@ -1160,7 +1166,7 @@ static PedDiskOps amiga_disk_ops = {
 	partition_get_name:	amiga_partition_get_name,
 	partition_align:	amiga_partition_align,
 	partition_enumerate:	amiga_partition_enumerate,
-
+	partition_check:	amiga_partition_check,
 
 	alloc_metadata:		amiga_alloc_metadata,
 	get_max_primary_partition_count:
diff --git a/libparted/labels/sun.c b/libparted/labels/sun.c
index 41580a4..88b71a9 100644
--- a/libparted/labels/sun.c
+++ b/libparted/labels/sun.c
@@ -836,6 +836,12 @@ error:
 	return 0;
 }
 
+static bool
+sun_partition_check (const PedPartition* part)
+{
+	return true;
+}
+
 static PedDiskOps sun_disk_ops = {
 	probe:			sun_probe,
 #ifndef DISCOVER_ONLY
@@ -862,6 +868,7 @@ static PedDiskOps sun_disk_ops = {
 	partition_is_flag_available:	sun_partition_is_flag_available,
 	partition_align:	sun_partition_align,
 	partition_enumerate:	sun_partition_enumerate,
+	partition_check:	sun_partition_check,
 	alloc_metadata:		sun_alloc_metadata,
 	get_max_primary_partition_count:
 				sun_get_max_primary_partition_count,
-- 
1.6.0.6




More information about the parted-devel mailing list