From 9f4664ee40618b3d393161d82d000f2bca4ec45a Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 7 Apr 2010 09:39:05 +0200 Subject: [PATCH] libblkid: fix 'partno' usage Linux kernel ignores empty partitions, but partition number is always allocated for the partition. (This rule is used for primary partitions only.) For example: part 1. size=12345 part 2. size=0 part 3. size=24567 the final list of partitions: sda1 sda3 'sda2' is not defined and partno=2 is not used for any other partitions. The libblkid library has to be compatible with this rule. Signed-off-by: Karel Zak --- shlibs/blkid/src/partitions/dos.c | 7 +++++-- shlibs/blkid/src/partitions/gpt.c | 6 ++++-- shlibs/blkid/src/partitions/partitions.c | 11 +++++++++-- shlibs/blkid/src/partitions/partitions.h | 2 ++ shlibs/blkid/src/partitions/sgi.c | 5 +++-- shlibs/blkid/src/partitions/sun.c | 5 +++-- 6 files changed, 26 insertions(+), 10 deletions(-) diff --git a/shlibs/blkid/src/partitions/dos.c b/shlibs/blkid/src/partitions/dos.c index c9865b75..7c95b8be 100644 --- a/shlibs/blkid/src/partitions/dos.c +++ b/shlibs/blkid/src/partitions/dos.c @@ -203,9 +203,12 @@ static int probe_dos_pt(blkid_probe pr, const struct blkid_idmag *mag) start = dos_partition_start(p) * ssf; size = dos_partition_size(p) * ssf; - if (!size) + if (!size) { + /* Linux kernel ignores empty partitions, but partno for + * the empty primary partitions is not reused */ + blkid_partlist_increment_partno(ls); continue; - + } par = blkid_partlist_add_partition(ls, tab, start, size); if (!par) goto err; diff --git a/shlibs/blkid/src/partitions/gpt.c b/shlibs/blkid/src/partitions/gpt.c index 56949013..12100e0f 100644 --- a/shlibs/blkid/src/partitions/gpt.c +++ b/shlibs/blkid/src/partitions/gpt.c @@ -328,14 +328,16 @@ static int probe_gpt_pt(blkid_probe pr, const struct blkid_idmag *mag) le64_to_cpu(e->starting_lba) + 1ULL; /* 00000000-0000-0000-0000-000000000000 entry */ - if (!guidcmp(e->partition_type_guid, GPT_UNUSED_ENTRY_GUID)) + if (!guidcmp(e->partition_type_guid, GPT_UNUSED_ENTRY_GUID)) { + blkid_partlist_increment_partno(ls); continue; - + } /* the partition has to inside usable range */ if (start < fu || start + size - 1 > lu) { DBG(DEBUG_LOWPROBE, printf( "GPT entry[%d] overflows usable area - ignore\n", i)); + blkid_partlist_increment_partno(ls); continue; } diff --git a/shlibs/blkid/src/partitions/partitions.c b/shlibs/blkid/src/partitions/partitions.c index a5c56c88..219a1973 100644 --- a/shlibs/blkid/src/partitions/partitions.c +++ b/shlibs/blkid/src/partitions/partitions.c @@ -27,7 +27,9 @@ * @short_description: partitions tables detection and parsing * * This chain supports binary and NAME=value interfaces, but complete PT - * description is provided by binary interface only. + * description is provided by binary interface only. The libblkid prober is + * compatible with kernel partition tables parser. The parser does not return + * empty (size=0) partitions or special hidden partitions. * * NAME=value interface, supported tags: * @@ -424,7 +426,7 @@ static blkid_partition new_partition(blkid_partlist ls, blkid_parttable tab) ref_parttable(tab); par->tab = tab; - par->partno = ls->next_partno++; + par->partno = blkid_partlist_increment_partno(ls); return par; } @@ -457,6 +459,11 @@ int blkid_partlist_set_partno(blkid_partlist ls, int partno) return 0; } +int blkid_partlist_increment_partno(blkid_partlist ls) +{ + return ls ? ls->next_partno++ : -1; +} + /* allows to set "parent" for the next nested partition */ int blkid_partlist_set_parent(blkid_partlist ls, blkid_partition par) { diff --git a/shlibs/blkid/src/partitions/partitions.h b/shlibs/blkid/src/partitions/partitions.h index 190918f9..2a4d20af 100644 --- a/shlibs/blkid/src/partitions/partitions.h +++ b/shlibs/blkid/src/partitions/partitions.h @@ -14,6 +14,8 @@ extern blkid_partition blkid_partlist_add_partition(blkid_partlist ls, blkid_loff_t start, blkid_loff_t size); extern int blkid_partlist_set_partno(blkid_partlist ls, int partno); +extern int blkid_partlist_increment_partno(blkid_partlist ls); + extern blkid_partition blkid_partlist_get_parent(blkid_partlist ls); extern int blkid_partitions_do_subprobe(blkid_probe pr, diff --git a/shlibs/blkid/src/partitions/sgi.c b/shlibs/blkid/src/partitions/sgi.c index 3a9cd0a7..945ead54 100644 --- a/shlibs/blkid/src/partitions/sgi.c +++ b/shlibs/blkid/src/partitions/sgi.c @@ -126,9 +126,10 @@ static int probe_sgi_pt(blkid_probe pr, const struct blkid_idmag *mag) blkid_partition par; if (size == 0 || type == SGI_TYPE_VOLULME || - type == SGI_TYPE_VOLHDR) + type == SGI_TYPE_VOLHDR) { + blkid_partlist_increment_partno(ls); continue; - + } par = blkid_partlist_add_partition(ls, tab, start, size); if (!par) goto err; diff --git a/shlibs/blkid/src/partitions/sun.c b/shlibs/blkid/src/partitions/sun.c index d70200c8..dffab24c 100644 --- a/shlibs/blkid/src/partitions/sun.c +++ b/shlibs/blkid/src/partitions/sun.c @@ -148,9 +148,10 @@ static int probe_sun_pt(blkid_probe pr, const struct blkid_idmag *mag) flags = be16_to_cpu(l->vtoc.infos[i].flags); } - if (type == SUN_TAG_WHOLEDISK || !size) + if (type == SUN_TAG_WHOLEDISK || !size) { + blkid_partlist_increment_partno(ls); continue; - + } par = blkid_partlist_add_partition(ls, tab, start, size); if (!par) goto err; -- 2.39.5