]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fdisk: API: add verify to label operations
authorDavidlohr Bueso <dave@gnu.org>
Tue, 24 Jul 2012 07:54:52 +0000 (09:54 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 24 Jul 2012 07:54:52 +0000 (09:54 +0200)
[kzak@redhat.com: - rename some functions]

Signed-off-by: Davidlohr Bueso <dave@gnu.org>
Signed-off-by: Karel Zak <kzak@redhat.com>
fdisks/fdisk.c
fdisks/fdisk.h
fdisks/fdiskaixlabel.c
fdisks/fdiskbsdlabel.c
fdisks/fdiskdoslabel.c
fdisks/fdiskmaclabel.c
fdisks/fdisksgilabel.c
fdisks/fdisksgilabel.h
fdisks/fdisksunlabel.c
fdisks/fdisksunlabel.h
fdisks/utils.c

index 355e313cbfbbb989bfb2d25213a1d07c5a82c67c..2b0f91a822fac7b79d8274227590d4b2641f9fca 100644 (file)
@@ -919,7 +919,8 @@ long2chs(struct fdisk_context *cxt, unsigned long ls,
        *s = ls % cxt->geom.sectors + 1;        /* sectors count from 1 */
 }
 
-static void check_consistency(struct fdisk_context *cxt, struct partition *p, int partition) {
+void check_consistency(struct fdisk_context *cxt, struct partition *p, int partition)
+{
        unsigned int pbc, pbh, pbs;     /* physical beginning c, h, s */
        unsigned int pec, peh, pes;     /* physical ending c, h, s */
        unsigned int lbc, lbh, lbs;     /* logical beginning c, h, s */
@@ -970,8 +971,7 @@ static void check_consistency(struct fdisk_context *cxt, struct partition *p, in
        }
 }
 
-static void
-check_alignment(struct fdisk_context *cxt, sector_t lba, int partition)
+void check_alignment(struct fdisk_context *cxt, sector_t lba, int partition)
 {
        if (!lba_is_aligned(cxt, lba))
                printf(_("Partition %i does not start on physical sector boundary.\n"),
@@ -1273,9 +1273,10 @@ void fill_bounds(sector_t *first, sector_t *last)
        }
 }
 
-static void
-check(struct fdisk_context *cxt, int n, unsigned int h, unsigned int s, unsigned int c,
-      unsigned int start) {
+void check(struct fdisk_context *cxt, int n, 
+          unsigned int h, unsigned int s, unsigned int c,
+          unsigned int start)
+{
        unsigned int total, real_s, real_c;
 
        real_s = sector(s) - 1;
@@ -1299,79 +1300,12 @@ check(struct fdisk_context *cxt, int n, unsigned int h, unsigned int s, unsigned
                        "total %d\n"), n, start, total);
 }
 
-static void
-verify(struct fdisk_context *cxt) {
-       int i, j;
-       sector_t total = 1, n_sectors = cxt->total_sectors;
-       unsigned long long first[partitions], last[partitions];
-       struct partition *p;
-
+static void verify(struct fdisk_context *cxt)
+{
        if (warn_geometry(cxt))
                return;
 
-       if (disklabel == SUN_LABEL) {
-               verify_sun(cxt);
-               return;
-       }
-
-       if (disklabel == SGI_LABEL) {
-               verify_sgi(cxt, 1);
-               return;
-       }
-
-       fill_bounds(first, last);
-       for (i = 0; i < partitions; i++) {
-               struct pte *pe = &ptes[i];
-
-               p = pe->part_table;
-               if (p->sys_ind && !IS_EXTENDED (p->sys_ind)) {
-                       check_consistency(cxt, p, i);
-                       check_alignment(cxt, get_partition_start(pe), i);
-                       if (get_partition_start(pe) < first[i])
-                               printf(_("Warning: bad start-of-data in "
-                                       "partition %d\n"), i + 1);
-                       check(cxt, i + 1, p->end_head, p->end_sector, p->end_cyl,
-                             last[i]);
-                       total += last[i] + 1 - first[i];
-                       for (j = 0; j < i; j++)
-                       if ((first[i] >= first[j] && first[i] <= last[j])
-                        || ((last[i] <= last[j] && last[i] >= first[j]))) {
-                               printf(_("Warning: partition %d overlaps "
-                                       "partition %d.\n"), j + 1, i + 1);
-                               total += first[i] >= first[j] ?
-                                       first[i] : first[j];
-                               total -= last[i] <= last[j] ?
-                                       last[i] : last[j];
-                       }
-               }
-       }
-
-       if (extended_offset) {
-               struct pte *pex = &ptes[ext_index];
-               sector_t e_last = get_start_sect(pex->part_table) +
-                       get_nr_sects(pex->part_table) - 1;
-
-               for (i = 4; i < partitions; i++) {
-                       total++;
-                       p = ptes[i].part_table;
-                       if (!p->sys_ind) {
-                               if (i != 4 || i + 1 < partitions)
-                                       printf(_("Warning: partition %d "
-                                               "is empty\n"), i + 1);
-                       }
-                       else if (first[i] < extended_offset ||
-                                       last[i] > e_last)
-                               printf(_("Logical partition %d not entirely in "
-                                       "partition %d\n"), i + 1, ext_index + 1);
-               }
-       }
-
-       if (total > n_sectors)
-               printf(_("Total allocated sectors %llu greater than the maximum"
-                       " %llu\n"), total, n_sectors);
-       else if (total < n_sectors)
-               printf(_("Remaining %lld unallocated %ld-byte sectors\n"),
-                      n_sectors - total, cxt->sector_size);
+       fdisk_verify_disklabel(cxt);
 }
 
 void print_partition_size(struct fdisk_context *cxt,
index 975ac4d9a357fb6c2c8b1cccf5a5395eb331b0a4..7f85020f1549354708125ecf8773369377c9d9a5 100644 (file)
@@ -139,6 +139,8 @@ struct fdisk_label {
        int (*probe)(struct fdisk_context *cxt);
        /* write in-memory changes to disk */
        int (*write)(struct fdisk_context *cxt);
+       /* verify the partition table */
+       int (*verify)(struct fdisk_context *cxt);
        /* new partition */
        void (*part_add)(struct fdisk_context *cxt, int partnum, int parttype);
        /* delete partition */
@@ -169,11 +171,17 @@ extern int fdisk_create_default_disklabel(struct fdisk_context *cxt);
 extern int fdisk_delete_partition(struct fdisk_context *cxt, int partnum);
 extern int fdisk_add_partition(struct fdisk_context *cxt, int partnum, int parttype);
 extern int fdisk_write_disklabel(struct fdisk_context *cxt);
+extern int fdisk_verify_disklabel(struct fdisk_context *cxt);
 
 /* prototypes for fdisk.c */
 extern char *disk_device, *line_ptr;
 extern int fd, partitions;
 extern unsigned int display_in_cyl_units, units_per_sector;
+
+extern void check_consistency(struct fdisk_context *cxt, struct partition *p, int partition);
+extern void check_alignment(struct fdisk_context *cxt, sector_t lba, int partition);
+extern void check(struct fdisk_context *cxt, int n, unsigned int h, unsigned int s, unsigned int c, unsigned int start);
+
 extern void change_units(struct fdisk_context *cxt);
 extern void fatal(struct fdisk_context *cxt, enum failure why);
 extern int  get_partition(struct fdisk_context *cxt, int warn, int max);
index c01af3dbc0bcfdbb7a031d3d373c3baf90d70cef..c692b0848c354dab4a9e08cd5ecfb2e5e0972ec4 100644 (file)
@@ -79,6 +79,7 @@ const struct fdisk_label aix_label =
        .name = "aix",
        .probe = aix_probe_label,
        .write = NULL,
+       .verify = NULL,
        .part_add = aix_add_partition,
        .part_delete = NULL,
 };
index 7e8f2ba12017ddb761ada2f32a92af753b7a08cc..3492aa83cf913eb6090e3f765ebd02c6a8081e75 100644 (file)
@@ -846,6 +846,7 @@ const struct fdisk_label bsd_label =
        .name = "bsd",
        .probe = osf_probe_label,
        .write = xbsd_write_disklabel,
-       .part_add= xbsd_add_part,
+       .verify = NULL,
+       .part_add = xbsd_add_part,
        .part_delete = xbsd_delete_part,
 };
index fde31a7a816b20e4fad52ae5e28cb3ae1cc77037..f81ada53a5038cda7f35d17d2482a221162db6f9 100644 (file)
@@ -643,6 +643,70 @@ static void add_logical(struct fdisk_context *cxt)
        add_partition(cxt, partitions - 1, LINUX_NATIVE);
 }
 
+static int dos_verify_disklabel(struct fdisk_context *cxt)
+{
+       int i, j;
+       sector_t total = 1, n_sectors = cxt->total_sectors;
+       unsigned long long first[partitions], last[partitions];
+       struct partition *p;
+
+       fill_bounds(first, last);
+       for (i = 0; i < partitions; i++) {
+               struct pte *pe = &ptes[i];
+
+               p = pe->part_table;
+               if (p->sys_ind && !IS_EXTENDED (p->sys_ind)) {
+                       check_consistency(cxt, p, i);
+                       check_alignment(cxt, get_partition_start(pe), i);
+                       if (get_partition_start(pe) < first[i])
+                               printf(_("Warning: bad start-of-data in "
+                                        "partition %d\n"), i + 1);
+                       check(cxt, i + 1, p->end_head, p->end_sector, p->end_cyl,
+                             last[i]);
+                       total += last[i] + 1 - first[i];
+                       for (j = 0; j < i; j++)
+                               if ((first[i] >= first[j] && first[i] <= last[j])
+                                   || ((last[i] <= last[j] && last[i] >= first[j]))) {
+                                       printf(_("Warning: partition %d overlaps "
+                                                "partition %d.\n"), j + 1, i + 1);
+                                       total += first[i] >= first[j] ?
+                                               first[i] : first[j];
+                                       total -= last[i] <= last[j] ?
+                                               last[i] : last[j];
+                               }
+               }
+       }
+
+       if (extended_offset) {
+               struct pte *pex = &ptes[ext_index];
+               sector_t e_last = get_start_sect(pex->part_table) +
+                       get_nr_sects(pex->part_table) - 1;
+
+               for (i = 4; i < partitions; i++) {
+                       total++;
+                       p = ptes[i].part_table;
+                       if (!p->sys_ind) {
+                               if (i != 4 || i + 1 < partitions)
+                                       printf(_("Warning: partition %d "
+                                                "is empty\n"), i + 1);
+                       }
+                       else if (first[i] < extended_offset ||
+                                       last[i] > e_last)
+                               printf(_("Logical partition %d not entirely in "
+                                       "partition %d\n"), i + 1, ext_index + 1);
+               }
+       }
+
+       if (total > n_sectors)
+               printf(_("Total allocated sectors %llu greater than the maximum"
+                        " %llu\n"), total, n_sectors);
+       else if (total < n_sectors)
+               printf(_("Remaining %lld unallocated %ld-byte sectors\n"),
+                      n_sectors - total, cxt->sector_size);
+
+       return 0;
+}
+
 /*
  * Ask the user for new partition type information (logical, extended).
  * This function calls the actual partition adding logic - add_partition.
@@ -757,6 +821,7 @@ const struct fdisk_label dos_label =
        .name = "dos",
        .probe = dos_probe_label,
        .write = dos_write_disklabel,
+       .verify = dos_verify_disklabel,
        .part_add = dos_add_partition,
        .part_delete = dos_delete_partition,
 };
index d69a92098453f9988c4b5099cb2d3f6bc4ec7380..28eaa93b22b1ba5f7ced107862439e8617455d03 100644 (file)
@@ -94,6 +94,7 @@ const struct fdisk_label mac_label =
        .name = "mac",
        .probe = mac_probe_label,
        .write = NULL,
+       .verify = NULL,
        .part_add = mac_add_partition,
        .part_delete = NULL,
 };
index 62fae99dfe3a70057284dece71408dcbc5a7f535..65f6dde64839828d748202dc43c36f8b31209dad 100644 (file)
@@ -391,17 +391,6 @@ compare_start(struct fdisk_context *cxt, const void *x, const void *y) {
        return (a > b) ? 1 : -1;
 }
 
-static int
-sgi_gaps(struct fdisk_context *cxt) {
-       /*
-        * returned value is:
-        *  = 0 : disk is properly filled to the rim
-        *  < 0 : there is an overlap
-        *  > 0 : there is still some vacant space
-        */
-       return verify_sgi(cxt, 0);
-}
-
 static void generic_swap(void *a, void *b, int size)
 {
        char t;
@@ -450,13 +439,11 @@ static void sort(void *base, size_t num, size_t size, struct fdisk_context *cxt,
        }
 }
 
-
-int
-verify_sgi(struct fdisk_context *cxt, int verbose)
+static int sgi_verify_disklabel(struct fdisk_context *cxt)
 {
        int Index[16];          /* list of valid partitions */
        int sortcount = 0;      /* number of used partitions, i.e. non-zero lengths */
-       int entire = 0, i = 0;
+       int entire = 0, i = 0, verbose = 1;
        unsigned int start = 0;
        long long gap = 0;      /* count unused blocks */
        unsigned int lastblock = sgi_get_lastblock(cxt);
@@ -575,6 +562,17 @@ verify_sgi(struct fdisk_context *cxt, int verbose)
        return (gap > 0) ? 1 : (gap == 0) ? 0 : -1;
 }
 
+static int
+sgi_gaps(struct fdisk_context *cxt) {
+       /*
+        * returned value is:
+        *  = 0 : disk is properly filled to the rim
+        *  < 0 : there is an overlap
+        *  > 0 : there is still some vacant space
+        */
+       return sgi_verify_disklabel(cxt);
+}
+
 int
 sgi_change_sysid(struct fdisk_context *cxt, int i, int sys)
 {
@@ -902,6 +900,7 @@ const struct fdisk_label sgi_label =
        .name = "sgi",
        .probe = sgi_probe_label,
        .write = sgi_write_disklabel,
+       .verify = sgi_verify_disklabel,
        .part_add = sgi_add_partition,
        .part_delete = sgi_delete_partition,
 };
index 83cf55effd0f34607bd77016df431e6163dfccda..cf52bdd03679d8c560da2195ce27cac4ca494bb2 100644 (file)
@@ -118,7 +118,6 @@ extern unsigned int sgi_get_num_sectors(struct fdisk_context *cxt, int i );
 extern int     sgi_get_sysid(struct fdisk_context *cxt, int i );
 extern void    create_sgilabel( struct fdisk_context *cxt );
 extern void    create_sgiinfo(struct fdisk_context *cxt);
-extern int     verify_sgi(struct fdisk_context *cxt, int verbose );
 extern void    sgi_set_ilfact( void );
 extern void    sgi_set_rspeed( void );
 extern void    sgi_set_pcylcount( void );
index 9743725758f262a6e4a73f5f9205c81bb59480a6..31c263ee437ba70dde5a1b0ed05b574f0a5911b7 100644 (file)
@@ -295,7 +295,7 @@ static int verify_sun_cmp(int *a, int *b)
     return -1;
 }
 
-void verify_sun(struct fdisk_context *cxt)
+static int sun_verify_disklabel(struct fdisk_context *cxt)
 {
     uint32_t starts[SUN_NUM_PARTITIONS], lens[SUN_NUM_PARTITIONS], start, stop;
     uint32_t i,j,k,starto,endo;
@@ -347,7 +347,7 @@ void verify_sun(struct fdisk_context *cxt)
 
     if (array[0] == -1) {
        printf(_("No partitions defined\n"));
-       return;
+       return 0;
     }
     stop = cxt->geom.cylinders * cxt->geom.heads * cxt->geom.sectors;
     if (starts[array[0]])
@@ -360,6 +360,8 @@ void verify_sun(struct fdisk_context *cxt)
     start = (starts[array[i]] + lens[array[i]]);
     if (start < stop)
         printf(_("Unused gap - sectors %d-%d\n"), start, stop);
+    
+    return 0;
 }
 
 static void sun_add_partition(struct fdisk_context *cxt, int n, int sys)
@@ -652,6 +654,7 @@ const struct fdisk_label sun_label =
        .name = "sun",
        .probe = sun_probe_label,
        .write = sun_write_disklabel,
+       .verify = sun_verify_disklabel,
        .part_add = sun_add_partition,
        .part_delete = sun_delete_partition,
 };
index 47c7cf86986958de32cf659073e93ba1fc1c19d8..86db5fbc9ef9762955ae6810aac083f4102ecbe0 100644 (file)
@@ -80,7 +80,6 @@ extern struct systypes sun_sys_types[];
 extern int create_sunlabel(struct fdisk_context *cxt);
 extern int sun_change_sysid(struct fdisk_context *cxt, int i, uint16_t sys);
 extern void sun_list_table(struct fdisk_context *cxt, int xtra);
-extern void verify_sun(struct fdisk_context *cxt);
 extern void sun_set_alt_cyl(struct fdisk_context *cxt);
 extern void sun_set_ncyl(struct fdisk_context *cxt, int cyl);
 extern void sun_set_xcyl(struct fdisk_context *cxt);
index 4ab9f630ac62c360429cd998c9d34312fe089f68..9ed2ca97f4940475145b4ed26e1557a3c85967f8 100644 (file)
@@ -63,6 +63,22 @@ int fdisk_write_disklabel(struct fdisk_context *cxt)
        return cxt->label->write(cxt);
 }
 
+/**
+ * fdisk_verify_disklabel:
+ * @cxt: fdisk context
+ *
+ * Verifies the partition tabe.
+ *
+ * Returns 0.
+ */
+int fdisk_verify_disklabel(struct fdisk_context *cxt)
+{
+       if (!cxt)
+               return -EINVAL;
+
+       return cxt->label->verify(cxt);
+}
+
 /*
  * fdisk_add_partition:
  * @cxt: fdisk context