From: Karel Zak Date: Tue, 22 Jan 2013 17:05:18 +0000 (+0100) Subject: fdisk: remove fdisk_label from API functions, add asserts, use size_t X-Git-Tag: v2.23-rc1~130 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9ffeb235d6d260ad4e61cc892b72f060f820a546;p=thirdparty%2Futil-linux.git fdisk: remove fdisk_label from API functions, add asserts, use size_t Signed-off-by: Karel Zak --- diff --git a/fdisks/fdiskaixlabel.c b/fdisks/fdiskaixlabel.c index 41547bdbe4..3d95f465f8 100644 --- a/fdisks/fdiskaixlabel.c +++ b/fdisks/fdiskaixlabel.c @@ -55,11 +55,15 @@ static void aix_nolabel(struct fdisk_context *cxt) return; } -static int aix_probe_label( - struct fdisk_context *cxt, - struct fdisk_label *lb __attribute__((__unused__))) +static int aix_probe_label(struct fdisk_context *cxt) { - struct aix_partition *aixlabel = (struct aix_partition *) cxt->firstsector; + struct aix_partition *aixlabel; + + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, GPT)); + + aixlabel = (struct aix_partition *) cxt->firstsector; if (aixlabel->magic != AIX_LABEL_MAGIC && aixlabel->magic != AIX_LABEL_MAGIC_SWAPPED) { @@ -73,10 +77,10 @@ static int aix_probe_label( return 1; } +/* TODO: remove this, libfdisk has to return ENOSYS */ static int aix_add_partition( struct fdisk_context *cxt __attribute__((__unused__)), - struct fdisk_label *lb __attribute__((__unused__)), - int partnum __attribute__((__unused__)), + size_t partnum __attribute__((__unused__)), struct fdisk_parttype *t __attribute__((__unused__))) { printf(_("\tSorry - this fdisk cannot handle AIX disk labels." diff --git a/fdisks/fdiskbsdlabel.c b/fdisks/fdiskbsdlabel.c index b5d0b64ea2..5ef49cddb8 100644 --- a/fdisks/fdiskbsdlabel.c +++ b/fdisks/fdiskbsdlabel.c @@ -70,11 +70,10 @@ struct fdisk_bsd_label { }; -static int xbsd_delete_part (struct fdisk_context *cxt, - struct fdisk_label *lb, int partnum); +static int xbsd_delete_part (struct fdisk_context *cxt, size_t partnum); static void xbsd_edit_disklabel (struct fdisk_context *cxt); static void xbsd_write_bootstrap (struct fdisk_context *cxt); -static void xbsd_change_fstype (struct fdisk_context *cxt, struct fdisk_label *lb); +static void xbsd_change_fstype (struct fdisk_context *cxt); static int xbsd_get_part_index (struct fdisk_context *cxt, int max); static int xbsd_check_new_partition (struct fdisk_context *cxt, int *i); static unsigned short xbsd_dkcksum (struct xbsd_disklabel *lp); @@ -116,16 +115,20 @@ static struct xbsd_disklabel xbsd_dlabel; * so this does not mean that there is a BSD disk label. */ static int -osf_probe_label(struct fdisk_context *cxt, - struct fdisk_label *lb __attribute__((__unused__))) +osf_probe_label(struct fdisk_context *cxt) { + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, OSF)); + if (xbsd_readlabel (cxt, NULL, &xbsd_dlabel) == 0) return 0; return 1; } int -btrydev (struct fdisk_context *cxt) { +btrydev (struct fdisk_context *cxt) +{ if (xbsd_readlabel (cxt, NULL, &xbsd_dlabel) == 0) return -1; printf(_("\nBSD label for device: %s\n"), cxt->dev_path); @@ -134,13 +137,13 @@ btrydev (struct fdisk_context *cxt) { } #if !defined (__alpha__) -static int -hidden(int type) { +static int hidden(int type) +{ return type ^ 0x10; } -static int -is_bsd_partition_type(int type) { +static int is_bsd_partition_type(int type) +{ return (type == FREEBSD_PARTITION || type == hidden(FREEBSD_PARTITION) || type == NETBSD_PARTITION || @@ -148,8 +151,7 @@ is_bsd_partition_type(int type) { } #endif -static int xbsd_write_disklabel (struct fdisk_context *cxt, - struct fdisk_label *lb __attribute__((__unused__))) +static int xbsd_write_disklabel (struct fdisk_context *cxt) { #if defined (__alpha__) printf (_("Writing disklabel to %s.\n"), cxt->dev_path); @@ -165,14 +167,17 @@ static int xbsd_write_disklabel (struct fdisk_context *cxt, } static int xbsd_add_part (struct fdisk_context *cxt, - struct fdisk_label *lb, - int partnum __attribute__((__unused__)), + size_t partnum __attribute__((__unused__)), struct fdisk_parttype *t __attribute__((__unused__))) { unsigned int begin, end; char mesg[256]; int i, rc; + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, OSF)); + rc = xbsd_check_new_partition(cxt, &i); if (rc) return rc; @@ -204,17 +209,20 @@ static int xbsd_add_part (struct fdisk_context *cxt, xbsd_dlabel.d_partitions[i].p_offset = begin; xbsd_dlabel.d_partitions[i].p_fstype = BSD_FS_UNUSED; - lb->nparts_cur = xbsd_dlabel.d_npartitions; - fdisk_label_set_changed(lb, 1); + cxt->label->nparts_cur = xbsd_dlabel.d_npartitions; + fdisk_label_set_changed(cxt->label, 1); return 0; } -static int xbsd_create_disklabel(struct fdisk_context *cxt, - struct fdisk_label *lb) +static int xbsd_create_disklabel(struct fdisk_context *cxt) { char c; + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, OSF)); + #if defined (__alpha__) fprintf (stderr, _("%s contains no disklabel.\n"), cxt->dev_path); #else @@ -234,8 +242,8 @@ static int xbsd_create_disklabel(struct fdisk_context *cxt, #endif ) == 1) { xbsd_print_disklabel (cxt, 1); - lb->nparts_cur = xbsd_dlabel.d_npartitions; - lb->nparts_max = BSD_MAXPARTITIONS; + cxt->label->nparts_cur = xbsd_dlabel.d_npartitions; + cxt->label->nparts_max = BSD_MAXPARTITIONS; return 1; } else return 0; @@ -265,7 +273,7 @@ bsd_command_prompt (struct fdisk_context *cxt) printf (_("Reading disklabel of %s at sector %d.\n"), partname(cxt->dev_path, t+1, 0), ss + BSD_LABELSECTOR); if (xbsd_readlabel (cxt, xbsd_part, &xbsd_dlabel) == 0) - if (xbsd_create_disklabel (cxt, cxt->label) == 0) + if (xbsd_create_disklabel (cxt) == 0) return; break; } @@ -288,8 +296,7 @@ bsd_command_prompt (struct fdisk_context *cxt) putchar ('\n'); switch (tolower (read_char(cxt, _("BSD disklabel command (m for help): ")))) { case 'd': - xbsd_delete_part(cxt, cxt->label, - xbsd_get_part_index(cxt, xbsd_dlabel.d_npartitions)); + xbsd_delete_part(cxt, xbsd_get_part_index(cxt, xbsd_dlabel.d_npartitions)); break; case 'e': xbsd_edit_disklabel (cxt); @@ -301,7 +308,7 @@ bsd_command_prompt (struct fdisk_context *cxt) list_partition_types (cxt); break; case 'n': - xbsd_add_part (cxt, cxt->label, 0, 0); + xbsd_add_part (cxt, 0, 0); break; case 'p': xbsd_print_disklabel (cxt, 0); @@ -315,13 +322,13 @@ bsd_command_prompt (struct fdisk_context *cxt) xbsd_print_disklabel (cxt, 1); break; case 't': - xbsd_change_fstype (cxt, cxt->label); + xbsd_change_fstype (cxt); break; case 'u': change_units(cxt); break; case 'w': - xbsd_write_disklabel (cxt, cxt->label); + xbsd_write_disklabel (cxt); break; #if !defined (__alpha__) case 'x': @@ -336,10 +343,13 @@ bsd_command_prompt (struct fdisk_context *cxt) } static int xbsd_delete_part( - struct fdisk_context *cxt __attribute__((__unused__)), - struct fdisk_label *lb, - int partnum) + struct fdisk_context *cxt, + size_t partnum) { + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, OSF)); + xbsd_dlabel.d_partitions[partnum].p_size = 0; xbsd_dlabel.d_partitions[partnum].p_offset = 0; xbsd_dlabel.d_partitions[partnum].p_fstype = BSD_FS_UNUSED; @@ -347,13 +357,14 @@ static int xbsd_delete_part( while (!xbsd_dlabel.d_partitions[xbsd_dlabel.d_npartitions-1].p_size) xbsd_dlabel.d_npartitions--; - lb->nparts_cur = xbsd_dlabel.d_npartitions; - fdisk_label_set_changed(lb, 1); + cxt->label->nparts_cur = xbsd_dlabel.d_npartitions; + fdisk_label_set_changed(cxt->label, 1); return 0; } void -xbsd_print_disklabel (struct fdisk_context *cxt, int show_all) { +xbsd_print_disklabel (struct fdisk_context *cxt, int show_all) +{ struct xbsd_disklabel *lp = &xbsd_dlabel; struct xbsd_partition *pp; FILE *f = stdout; @@ -589,21 +600,22 @@ xbsd_write_bootstrap (struct fdisk_context *cxt) } /* TODO: remove this, use regular change_partition_type() in fdisk.c */ -static void -xbsd_change_fstype ( - struct fdisk_context *cxt, - struct fdisk_label *lb) +static void xbsd_change_fstype (struct fdisk_context *cxt) { int i; struct fdisk_parttype *t; + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, OSF)); + i = xbsd_get_part_index (cxt, xbsd_dlabel.d_npartitions); t = read_partition_type(cxt); if (t) { xbsd_dlabel.d_partitions[i].p_fstype = t->type; fdisk_free_parttype(t); - fdisk_label_set_changed(lb, 1); + fdisk_label_set_changed(cxt->label, 1); } } @@ -621,8 +633,8 @@ xbsd_get_part_index(struct fdisk_context *cxt, int max) } static int -xbsd_check_new_partition(struct fdisk_context *cxt, int *i) { - +xbsd_check_new_partition(struct fdisk_context *cxt, int *i) +{ /* room for more? various BSD flavours have different maxima */ if (xbsd_dlabel.d_npartitions == BSD_MAXPARTITIONS) { int t; @@ -886,11 +898,14 @@ alpha_bootblock_checksum (char *boot) static struct fdisk_parttype *xbsd_get_parttype( struct fdisk_context *cxt, - struct fdisk_label *lb __attribute__((__unused__)), - int n) + size_t n) { struct fdisk_parttype *t; + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, OSF)); + if (n >= xbsd_dlabel.d_npartitions) return NULL; @@ -901,13 +916,16 @@ static struct fdisk_parttype *xbsd_get_parttype( } static int xbsd_set_parttype( - struct fdisk_context *cxt __attribute__((__unused__)), - struct fdisk_label *lb, - int partnum, + struct fdisk_context *cxt, + size_t partnum, struct fdisk_parttype *t) { struct xbsd_partition *p; + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, OSF)); + if (partnum >= xbsd_dlabel.d_npartitions || !t || t->type > UINT8_MAX) return -EINVAL; @@ -916,7 +934,7 @@ static int xbsd_set_parttype( return 0; p->p_fstype = t->type; - fdisk_label_set_changed(lb, 1); + fdisk_label_set_changed(cxt->label, 1); return 0; } diff --git a/fdisks/fdiskdoslabel.c b/fdisks/fdiskdoslabel.c index acdbc86a59..e622ac5176 100644 --- a/fdisks/fdiskdoslabel.c +++ b/fdisks/fdiskdoslabel.c @@ -204,12 +204,8 @@ void dos_init(struct fdisk_context *cxt) warn_alignment(cxt); } -static int dos_delete_partition( - struct fdisk_context *cxt __attribute__ ((__unused__)), - struct fdisk_label *lb, - int n) +static int dos_delete_partition(struct fdisk_context *cxt, size_t partnum) { - size_t partnum = (size_t) n; /* TODO: use size_t in API */ struct pte *pe = &ptes[partnum]; struct partition *p = pe->part_table; struct partition *q = pe->ext_pointer; @@ -262,7 +258,7 @@ static int dos_delete_partition( clear_partition(ptes[partnum].part_table); } - fdisk_label_set_changed(lb, 1); + fdisk_label_set_changed(cxt->label, 1); return 0; } @@ -354,7 +350,7 @@ static void read_extended(struct fdisk_context *cxt, int ext) if (!get_nr_sects(pe->part_table) && (cxt->label->nparts_max > 5 || ptes[4].part_table->sys_ind)) { printf(_("omitting empty partition (%zd)\n"), i+1); - dos_delete_partition(cxt, cxt->label, i); + dos_delete_partition(cxt, i); goto remove; /* numbering changed */ } } @@ -365,11 +361,14 @@ void dos_print_mbr_id(struct fdisk_context *cxt) printf(_("Disk identifier: 0x%08x\n"), mbr_get_id(cxt->firstsector)); } -static int dos_create_disklabel(struct fdisk_context *cxt, - struct fdisk_label *lb __attribute__((__unused__))) +static int dos_create_disklabel(struct fdisk_context *cxt) { unsigned int id; + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, DOS)); + /* random disk signature */ random_get_bytes(&id, sizeof(id)); @@ -441,9 +440,12 @@ static void get_partition_table_geometry(struct fdisk_context *cxt, DBG(CONTEXT, dbgprint("DOS PT geometry: heads=%u, sectors=%u", *ph, *ps)); } -static int dos_reset_alignment(struct fdisk_context *cxt, - struct fdisk_label *lb __attribute__((__unused__))) +static int dos_reset_alignment(struct fdisk_context *cxt) { + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, DOS)); + /* overwrite necessary stuff by DOS deprecated stuff */ if (is_dos_compatible(cxt)) { if (cxt->geom.sectors) @@ -457,13 +459,14 @@ static int dos_reset_alignment(struct fdisk_context *cxt, return 0; } -static int dos_probe_label(struct fdisk_context *cxt, struct fdisk_label *lb) +static int dos_probe_label(struct fdisk_context *cxt) { size_t i; unsigned int h = 0, s = 0; assert(cxt); assert(cxt->label); + assert(fdisk_is_disklabel(cxt, DOS)); if (!mbr_is_valid_magic(cxt->firstsector)) return 0; @@ -500,7 +503,7 @@ static int dos_probe_label(struct fdisk_context *cxt, struct fdisk_label *lb) "table %zd will be corrected by w(rite)\n"), part_table_flag(pe->sectorbuffer), i + 1); pe->changed = 1; - fdisk_label_set_changed(lb, 1); + fdisk_label_set_changed(cxt->label, 1); } } @@ -859,8 +862,7 @@ static void check_consistency(struct fdisk_context *cxt, struct partition *p, } } -static int dos_verify_disklabel(struct fdisk_context *cxt, - struct fdisk_label *lb __attribute__((__unused__))) +static int dos_verify_disklabel(struct fdisk_context *cxt) { size_t i, j; sector_t total = 1, n_sectors = cxt->total_sectors; @@ -868,6 +870,10 @@ static int dos_verify_disklabel(struct fdisk_context *cxt, last[cxt->label->nparts_max]; struct partition *p; + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, DOS)); + fill_bounds(cxt, first, last); for (i = 0; i < cxt->label->nparts_max; i++) { struct pte *pe = &ptes[i]; @@ -933,13 +939,16 @@ static int dos_verify_disklabel(struct fdisk_context *cxt, */ static int dos_add_partition( struct fdisk_context *cxt, - struct fdisk_label *lb __attribute__((__unused__)), - int partnum __attribute__ ((__unused__)), + size_t partnum __attribute__ ((__unused__)), struct fdisk_parttype *t) { size_t i, free_primary = 0; int rc = 0; + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, DOS)); + for (i = 0; i < 4; i++) free_primary += !ptes[i].part_table->sys_ind; @@ -1019,12 +1028,15 @@ static int write_sector(struct fdisk_context *cxt, sector_t secno, return 0; } -static int dos_write_disklabel(struct fdisk_context *cxt, - struct fdisk_label *lb __attribute__((__unused__))) +static int dos_write_disklabel(struct fdisk_context *cxt) { size_t i; int rc = 0; + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, DOS)); + /* MBR (primary partitions) */ if (!MBRbuffer_changed) { for (i = 0; i < 4; i++) @@ -1055,13 +1067,16 @@ done: static struct fdisk_parttype *dos_get_parttype( struct fdisk_context *cxt, - struct fdisk_label *lb __attribute__((__unused__)), - int partnum) + size_t partnum) { struct fdisk_parttype *t; struct partition *p; - if (partnum < 0 || (size_t) partnum >= cxt->label->nparts_max) + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, OSF)); + + if (partnum >= cxt->label->nparts_max) return NULL; p = ptes[partnum].part_table; @@ -1073,14 +1088,16 @@ static struct fdisk_parttype *dos_get_parttype( static int dos_set_parttype( struct fdisk_context *cxt, - struct fdisk_label *lb, - int partnum, + size_t partnum, struct fdisk_parttype *t) { struct partition *p; - if (partnum < 0 || (size_t) partnum >= cxt->label->nparts_max - || !t || t->type > UINT8_MAX) + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, OSF)); + + if (partnum >= cxt->label->nparts_max || !t || t->type > UINT8_MAX) return -EINVAL; p = ptes[partnum].part_table; @@ -1100,7 +1117,7 @@ static int dos_set_parttype( "information.\n\n")); p->sys_ind = t->type; - fdisk_label_set_changed(lb, 1); + fdisk_label_set_changed(cxt->label, 1); return 0; } @@ -1159,6 +1176,7 @@ int dos_list_table(struct fdisk_context *cxt, size_t i, w; assert(cxt); + assert(cxt->label); assert(fdisk_is_disklabel(cxt, DOS)); if (is_garbage_table()) { @@ -1422,17 +1440,17 @@ void dos_toggle_active(struct fdisk_context *cxt, int i) static int dos_get_partition_status( struct fdisk_context *cxt, - struct fdisk_label *lb __attribute__((__unused__)), - int i, + size_t i, int *status) { struct pte *pe; struct partition *p; assert(cxt); + assert(cxt->label); assert(fdisk_is_disklabel(cxt, DOS)); - if (!status || i < 0 || (size_t) i >= cxt->label->nparts_max) + if (!status || i >= cxt->label->nparts_max) return -EINVAL; *status = FDISK_PARTSTAT_NONE; diff --git a/fdisks/fdiskmaclabel.c b/fdisks/fdiskmaclabel.c index 14307fab02..a91cabc094 100644 --- a/fdisks/fdiskmaclabel.c +++ b/fdisks/fdiskmaclabel.c @@ -56,8 +56,7 @@ mac_nolabel(struct fdisk_context *cxt) } static int -mac_probe_label(struct fdisk_context *cxt, - struct fdisk_label *lb __attribute__((__unused__))) +mac_probe_label(struct fdisk_context *cxt) { struct mac_partition *maclabel = (struct mac_partition *) cxt->firstsector; @@ -91,8 +90,7 @@ IS_MAC: static int mac_add_partition( struct fdisk_context *cxt __attribute__ ((__unused__)), - struct fdisk_label *lb __attribute__((__unused__)), - int partnum __attribute__ ((__unused__)), + size_t partnum __attribute__ ((__unused__)), struct fdisk_parttype *t __attribute__ ((__unused__))) { printf(_("\tSorry - this fdisk cannot handle Mac disk labels." diff --git a/fdisks/fdisksgilabel.c b/fdisks/fdisksgilabel.c index 486a3f618b..904c016b9b 100644 --- a/fdisks/fdisksgilabel.c +++ b/fdisks/fdisksgilabel.c @@ -150,9 +150,12 @@ static size_t count_used_partitions(struct fdisk_context *cxt) } static int -sgi_probe_label(struct fdisk_context *cxt, - struct fdisk_label *lb __attribute__((__unused__))) +sgi_probe_label(struct fdisk_context *cxt) { + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, SGI)); + if (sizeof(sgilabel) > 512) { fprintf(stderr, _("According to MIPS Computer Systems, Inc the " @@ -293,7 +296,8 @@ sgi_set_swappartition(struct fdisk_context *cxt, int i) { } static int -sgi_check_bootfile(struct fdisk_context *cxt, const char* aFile) { +sgi_check_bootfile(struct fdisk_context *cxt, const char* aFile) +{ if (strlen(aFile) < 3) /* "/a\n" is minimum */ { printf(_("\nInvalid Bootfile!\n" "\tThe bootfile must be an absolute non-zero pathname,\n" @@ -354,11 +358,15 @@ create_sgiinfo(struct fdisk_context *cxt) { } -static int sgi_write_disklabel(struct fdisk_context *cxt, - struct fdisk_label *lb __attribute__((__unused__))) +static int sgi_write_disklabel(struct fdisk_context *cxt) { + sgiinfo *info = NULL; + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, SGI)); + sgilabel->csum = 0; sgilabel->csum = SSWAP32(two_s_complement_32bit_sum( (unsigned int*)sgilabel, @@ -464,8 +472,7 @@ static void sort(void *base0, size_t num, size_t size, struct fdisk_context *cxt } } -static int sgi_verify_disklabel(struct fdisk_context *cxt, - struct fdisk_label *lb __attribute__((__unused__))) +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 */ @@ -474,6 +481,10 @@ static int sgi_verify_disklabel(struct fdisk_context *cxt, long long gap = 0; /* count unused blocks */ unsigned int lastblock = sgi_get_lastblock(cxt); + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, SGI)); + clearfreelist(); for (i=0; i<16; i++) { if (sgi_get_num_sectors(cxt, i) != 0) { @@ -597,7 +608,7 @@ sgi_gaps(struct fdisk_context *cxt) { * < 0 : there is an overlap * > 0 : there is still some vacant space */ - return sgi_verify_disklabel(cxt, cxt->label); + return sgi_verify_disklabel(cxt); } @@ -612,9 +623,13 @@ sgi_entire(struct fdisk_context *cxt) { return -1; } -static int sgi_set_partition(struct fdisk_context *cxt, int i, +static int sgi_set_partition(struct fdisk_context *cxt, size_t i, unsigned int start, unsigned int length, int sys) { + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, SGI)); + sgilabel->partitions[i].id = SSWAP32(sys); sgilabel->partitions[i].num_sectors = SSWAP32(length); sgilabel->partitions[i].start_sector = SSWAP32(start); @@ -660,16 +675,14 @@ sgi_set_volhdr(struct fdisk_context *cxt) } } -static int sgi_delete_partition(struct fdisk_context *cxt, - struct fdisk_label *lb __attribute__((__unused__)), - int partnum) +static int sgi_delete_partition(struct fdisk_context *cxt, size_t partnum) { int rc; assert(cxt); assert(cxt->label); - if (partnum < 0 || (size_t) partnum > cxt->label->nparts_max) + if (partnum > cxt->label->nparts_max) return -EINVAL; rc = sgi_set_partition(cxt, partnum, 0, 0, 0); @@ -680,21 +693,24 @@ static int sgi_delete_partition(struct fdisk_context *cxt, } static int sgi_add_partition(struct fdisk_context *cxt, - struct fdisk_label *lb __attribute__((__unused__)), - int n, + size_t n, struct fdisk_parttype *t) { char mesg[256]; unsigned int first=0, last=0; int sys = t ? t->type : SGI_XFS; + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, SGI)); + if (n == 10) sys = SGI_VOLUME; else if (n == 8) sys = 0; if (sgi_get_num_sectors(cxt, n)) { - printf(_("Partition %d is already defined. Delete " + printf(_("Partition %zd is already defined. Delete " "it before re-adding it.\n"), n + 1); return -EINVAL; } @@ -756,8 +772,7 @@ static int sgi_add_partition(struct fdisk_context *cxt, return 0; } -static int sgi_create_disklabel(struct fdisk_context *cxt, - struct fdisk_label *lb __attribute__((__unused__))) +static int sgi_create_disklabel(struct fdisk_context *cxt) { struct hd_geometry geometry; struct { @@ -770,6 +785,10 @@ static int sgi_create_disklabel(struct fdisk_context *cxt, int res; /* the result from the ioctl */ int sec_fac; /* the sector factor */ + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, SGI)); + sec_fac = cxt->sector_size / 512; /* determine the sector factor */ fprintf(stderr, @@ -929,13 +948,11 @@ static sgiinfo *fill_sgiinfo(void) return info; } -static struct fdisk_parttype *sgi_get_parttype(struct fdisk_context *cxt, - struct fdisk_label *lb __attribute__((__unused__)), - int n) +static struct fdisk_parttype *sgi_get_parttype(struct fdisk_context *cxt, size_t n) { struct fdisk_parttype *t; - if (n < 0 || (size_t) n >= cxt->label->nparts_max) + if (n >= cxt->label->nparts_max) return NULL; t = fdisk_get_parttype_from_code(cxt, sgi_get_sysid(cxt, n)); @@ -945,12 +962,10 @@ static struct fdisk_parttype *sgi_get_parttype(struct fdisk_context *cxt, } static int sgi_set_parttype(struct fdisk_context *cxt, - struct fdisk_label *lb __attribute__((__unused__)), - int i, + size_t i, struct fdisk_parttype *t) { - if (i < 0 || (size_t) i >= cxt->label->nparts_max - || !t || t->type > UINT32_MAX) + if (i >= cxt->label->nparts_max || !t || t->type > UINT32_MAX) return -EINVAL; if (sgi_get_num_sectors(cxt, i) == 0) /* caught already before, ... */ { @@ -981,15 +996,14 @@ static int sgi_set_parttype(struct fdisk_context *cxt, static int sgi_get_partition_status( struct fdisk_context *cxt, - struct fdisk_label *lb __attribute__((__unused__)), - int i, + size_t i, int *status) { assert(cxt); assert(fdisk_is_disklabel(cxt, SGI)); - if (!status || i < 0 || (size_t) i >= cxt->label->nparts_max) + if (!status || i >= cxt->label->nparts_max) return -EINVAL; *status = FDISK_PARTSTAT_NONE; diff --git a/fdisks/fdisksunlabel.c b/fdisks/fdisksunlabel.c index a5fa11edee..ecb6c1c55c 100644 --- a/fdisks/fdisksunlabel.c +++ b/fdisks/fdisksunlabel.c @@ -36,8 +36,6 @@ struct fdisk_sun_label { struct sun_disk_label *header; /* on-disk data (pointer to cxt->firstsector) */ }; -#define is_sun_label(_l) ((_l)->id == FDISK_DISKLABEL_SUN) - static struct fdisk_parttype sun_parttypes[] = { {SUN_TAG_UNASSIGNED, N_("Unassigned")}, {SUN_TAG_BOOT, N_("Boot")}, @@ -70,29 +68,38 @@ static inline uint32_t __swap32(uint32_t x) { #define SSWAP32(x) (other_endian ? __swap32(x) \ : (uint32_t)(x)) -static inline struct sun_disk_label * -label_get_sun_disklabel(struct fdisk_label *lb) +/* return poiter buffer with on-disk data */ +static inline struct sun_disk_label *self_disklabel(struct fdisk_context *cxt) { - assert(lb); - assert(is_sun_label(lb)); + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, SUN)); - return ((struct fdisk_sun_label *) lb)->header; + return ((struct fdisk_sun_label *) cxt->label)->header; } -#define context_get_sun_disklabel(_c) label_get_sun_disklabel((_c)->label) +/* return in-memory sun fdisk data */ +static inline struct fdisk_sun_label *self_label(struct fdisk_context *cxt) +{ + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, SUN)); + + return (struct fdisk_sun_label *) cxt->label; +} -int sun_is_empty_type(struct fdisk_context *cxt, int i) +int sun_is_empty_type(struct fdisk_context *cxt, size_t i) { - struct sun_disk_label *sunlabel = context_get_sun_disklabel(cxt); + struct sun_disk_label *sunlabel = self_disklabel(cxt); return (!sunlabel->partitions[i].num_sectors || !sunlabel->part_tags[i].tag); } -static void set_sun_partition(struct fdisk_context *cxt, - int i, uint32_t start, uint32_t stop, uint16_t sysid) +static void set_sun_partition(struct fdisk_context *cxt, size_t i, + uint32_t start,uint32_t stop, uint16_t sysid) { - struct sun_disk_label *sunlabel = context_get_sun_disklabel(cxt); + struct sun_disk_label *sunlabel = self_disklabel(cxt); sunlabel->part_tags[i].tag = SSWAP16(sysid); sunlabel->part_tags[i].flag = SSWAP16(0); @@ -104,21 +111,21 @@ static void set_sun_partition(struct fdisk_context *cxt, print_partition_size(cxt, i + 1, start, stop, sysid); } -static size_t partitions_in_use(struct fdisk_label *lb) +static size_t count_used_partitions(struct fdisk_context *cxt) { - struct sun_disk_label *sunlabel = label_get_sun_disklabel(lb); + struct sun_disk_label *sunlabel = self_disklabel(cxt); size_t ct = 0, i; assert(sunlabel); - for (i = 0; i < lb->nparts_max; i++) { + for (i = 0; i < cxt->label->nparts_max; i++) { if (sunlabel->partitions[i].num_sectors) ct++; } return ct; } -static int sun_probe_label(struct fdisk_context *cxt, struct fdisk_label *lb) +static int sun_probe_label(struct fdisk_context *cxt) { struct fdisk_sun_label *sun; struct sun_disk_label *sunlabel; @@ -127,10 +134,10 @@ static int sun_probe_label(struct fdisk_context *cxt, struct fdisk_label *lb) int need_fixing = 0; assert(cxt); - assert(lb); - assert(is_sun_label(lb)); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, SUN)); - sunlabel = (struct sun_disk_label *) cxt->firstsector; + sunlabel = self_disklabel(cxt); if (sunlabel->magic != SUN_LABEL_MAGIC && sunlabel->magic != SUN_LABEL_MAGIC_SWAPPED) { @@ -153,10 +160,10 @@ static int sun_probe_label(struct fdisk_context *cxt, struct fdisk_label *lb) } /* map first sector buffer to sun header */ - sun = (struct fdisk_sun_label *) lb; + sun = (struct fdisk_sun_label *) cxt->label; sun->header = (struct sun_disk_label *) cxt->firstsector; - lb->nparts_max = SUN_NUM_PARTITIONS; + cxt->label->nparts_max = SUN_NUM_PARTITIONS; cxt->geom.heads = SSWAP16(sunlabel->nhead); cxt->geom.cylinders = SSWAP16(sunlabel->ncyl); cxt->geom.sectors = SSWAP16(sunlabel->nsect); @@ -190,15 +197,15 @@ static int sun_probe_label(struct fdisk_context *cxt, struct fdisk_label *lb) csum ^= *ush++; sunlabel->cksum = csum; - fdisk_label_set_changed(lb, 1); + fdisk_label_set_changed(cxt->label, 1); } - lb->nparts_cur = partitions_in_use(lb); + cxt->label->nparts_cur = count_used_partitions(cxt); return 1; } -static int sun_create_disklabel(struct fdisk_context *cxt, struct fdisk_label *lb) +static int sun_create_disklabel(struct fdisk_context *cxt) { struct hd_geometry geometry; sector_t llsectors, llcyls; @@ -209,14 +216,14 @@ static int sun_create_disklabel(struct fdisk_context *cxt, struct fdisk_label *l struct sun_disk_label *sunlabel; /* on disk data */ assert(cxt); - assert(lb); - assert(is_sun_label(lb)); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, SUN)); fprintf(stderr, _("Building a new Sun disklabel.\n")); /* map first sector to header */ fdisk_zeroize_firstsector(cxt); - sun = (struct fdisk_sun_label *) lb; + sun = (struct fdisk_sun_label *) cxt->label; sun->header = (struct sun_disk_label *) cxt->firstsector; sunlabel = sun->header; @@ -226,7 +233,7 @@ static int sun_create_disklabel(struct fdisk_context *cxt, struct fdisk_label *l #else other_endian = 0; #endif - lb->nparts_max = SUN_NUM_PARTITIONS; + cxt->label->nparts_max = SUN_NUM_PARTITIONS; fdisk_zeroize_firstsector(cxt); sunlabel->magic = SSWAP16(SUN_LABEL_MAGIC); @@ -301,15 +308,15 @@ static int sun_create_disklabel(struct fdisk_context *cxt, struct fdisk_label *l sunlabel->cksum = csum; } - fdisk_label_set_changed(lb, 1); - lb->nparts_cur = partitions_in_use(lb); + fdisk_label_set_changed(cxt->label, 1); + cxt->label->nparts_cur = count_used_partitions(cxt); return 0; } -void toggle_sunflags(struct fdisk_context *cxt, int i, uint16_t mask) +void toggle_sunflags(struct fdisk_context *cxt, size_t i, uint16_t mask) { - struct sun_disk_label *sunlabel = context_get_sun_disklabel(cxt); + struct sun_disk_label *sunlabel = self_disklabel(cxt); struct sun_tag_flag *p = &sunlabel->part_tags[i]; p->flag ^= SSWAP16(mask); @@ -318,7 +325,6 @@ void toggle_sunflags(struct fdisk_context *cxt, int i, uint16_t mask) } static void fetch_sun(struct fdisk_context *cxt, - struct fdisk_label *lb, uint32_t *starts, uint32_t *lens, uint32_t *start, @@ -329,15 +335,16 @@ static void fetch_sun(struct fdisk_context *cxt, size_t i; assert(cxt); - assert(lb); - assert(is_sun_label(lb)); + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, SUN)); - sunlabel = label_get_sun_disklabel(lb); + sunlabel = self_disklabel(cxt); *start = 0; *stop = cxt->geom.cylinders * cxt->geom.heads * cxt->geom.sectors; - for (i = 0; i < lb->nparts_max; i++) { + for (i = 0; i < cxt->label->nparts_max; i++) { struct sun_partition *part = &sunlabel->partitions[i]; struct sun_tag_flag *tag = &sunlabel->part_tags[i]; @@ -377,20 +384,19 @@ static int verify_sun_cmp(int *a, int *b) return -1; } -static int sun_verify_disklabel(struct fdisk_context *cxt, - struct fdisk_label *lb) +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; int array[SUN_NUM_PARTITIONS]; assert(cxt); - assert(lb); - assert(is_sun_label(lb)); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, SUN)); verify_sun_starts = starts; - fetch_sun(cxt, lb, starts, lens, &start, &stop); + fetch_sun(cxt, starts, lens, &start, &stop); for (k = 0; k < 7; k++) { for (i = 0; i < SUN_NUM_PARTITIONS; i++) { @@ -453,11 +459,10 @@ static int sun_verify_disklabel(struct fdisk_context *cxt, static int sun_add_partition( struct fdisk_context *cxt, - struct fdisk_label *lb, - int n, + size_t n, struct fdisk_parttype *t) { - struct sun_disk_label *sunlabel = label_get_sun_disklabel(lb); + struct sun_disk_label *sunlabel = self_disklabel(cxt); uint32_t starts[SUN_NUM_PARTITIONS], lens[SUN_NUM_PARTITIONS]; struct sun_partition *part = &sunlabel->partitions[n]; struct sun_tag_flag *tag = &sunlabel->part_tags[n]; @@ -469,12 +474,12 @@ static int sun_add_partition( unsigned int first, last; if (part->num_sectors && tag->tag != SSWAP16(SUN_TAG_UNASSIGNED)) { - printf(_("Partition %d is already defined. Delete " + printf(_("Partition %zd is already defined. Delete " "it before re-adding it.\n"), n + 1); return -EINVAL; } - fetch_sun(cxt, lb, starts, lens, &start, &stop); + fetch_sun(cxt, starts, lens, &start, &stop); if (stop <= start) { if (n == 2) @@ -522,11 +527,11 @@ and is of type `Whole disk'\n")); /* On the other hand, one should not use partitions starting at block 0 in an md, or the label will be trashed. */ - for (i = 0; i < lb->nparts_max; i++) + for (i = 0; i < cxt->label->nparts_max; i++) if (lens[i] && starts[i] <= first && starts[i] + lens[i] > first) break; - if (i < lb->nparts_max && !whole_disk) { + if (i < cxt->label->nparts_max && !whole_disk) { if (n == 2 && !first) { whole_disk = 1; break; @@ -537,7 +542,7 @@ and is of type `Whole disk'\n")); } stop = cxt->geom.cylinders * cxt->geom.heads * cxt->geom.sectors; /* ancient */ stop2 = stop; - for (i = 0; i < lb->nparts_max; i++) { + for (i = 0; i < cxt->label->nparts_max; i++) { if (starts[i] > first && starts[i] < stop) stop = starts[i]; } @@ -575,13 +580,12 @@ and is of type `Whole disk'\n")); sys = SUN_TAG_BACKUP; set_sun_partition(cxt, n, first, last, sys); - lb->nparts_cur = partitions_in_use(lb); + cxt->label->nparts_cur = count_used_partitions(cxt); return 0; } static int sun_delete_partition(struct fdisk_context *cxt, - struct fdisk_label *lb, - int partnum) + size_t partnum) { struct sun_disk_label *sunlabel; struct sun_partition *part; @@ -589,10 +593,10 @@ static int sun_delete_partition(struct fdisk_context *cxt, unsigned int nsec; assert(cxt); - assert(lb); - assert(is_sun_label(lb)); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, SUN)); - sunlabel = label_get_sun_disklabel(lb); + sunlabel = self_disklabel(cxt); part = &sunlabel->partitions[partnum]; tag = &sunlabel->part_tags[partnum]; @@ -607,8 +611,8 @@ static int sun_delete_partition(struct fdisk_context *cxt, "sectors\n"), nsec); tag->tag = SSWAP16(SUN_TAG_UNASSIGNED); part->num_sectors = 0; - lb->nparts_cur = partitions_in_use(lb); - fdisk_label_set_changed(lb, 1); + cxt->label->nparts_cur = count_used_partitions(cxt); + fdisk_label_set_changed(cxt->label, 1); return 0; } @@ -623,7 +627,7 @@ void sun_list_table(struct fdisk_context *cxt, int xtra) assert(cxt->label); assert(fdisk_is_disklabel(cxt, SUN)); - sunlabel = context_get_sun_disklabel(cxt); + sunlabel = self_disklabel(cxt); w = strlen(cxt->dev_path); if (xtra) @@ -678,7 +682,7 @@ void sun_list_table(struct fdisk_context *cxt, int xtra) void sun_set_alt_cyl(struct fdisk_context *cxt) { - struct sun_disk_label *sunlabel = context_get_sun_disklabel(cxt); + struct sun_disk_label *sunlabel = self_disklabel(cxt); sunlabel->acyl = SSWAP16(read_int(cxt, 0,SSWAP16(sunlabel->acyl), 65535, 0, _("Number of alternate cylinders"))); @@ -686,13 +690,13 @@ void sun_set_alt_cyl(struct fdisk_context *cxt) void sun_set_ncyl(struct fdisk_context *cxt, int cyl) { - struct sun_disk_label *sunlabel = context_get_sun_disklabel(cxt); + struct sun_disk_label *sunlabel = self_disklabel(cxt); sunlabel->ncyl = SSWAP16(cyl); } void sun_set_xcyl(struct fdisk_context *cxt) { - struct sun_disk_label *sunlabel = context_get_sun_disklabel(cxt); + struct sun_disk_label *sunlabel = self_disklabel(cxt); sunlabel->apc = SSWAP16(read_int(cxt, 0, SSWAP16(sunlabel->apc), cxt->geom.sectors, 0, _("Extra sectors per cylinder"))); @@ -700,7 +704,7 @@ void sun_set_xcyl(struct fdisk_context *cxt) void sun_set_ilfact(struct fdisk_context *cxt) { - struct sun_disk_label *sunlabel = context_get_sun_disklabel(cxt); + struct sun_disk_label *sunlabel = self_disklabel(cxt); sunlabel->intrlv = SSWAP16(read_int(cxt, 1, SSWAP16(sunlabel->intrlv), 32, 0, _("Interleave factor"))); @@ -708,7 +712,7 @@ void sun_set_ilfact(struct fdisk_context *cxt) void sun_set_rspeed(struct fdisk_context *cxt) { - struct sun_disk_label *sunlabel = context_get_sun_disklabel(cxt); + struct sun_disk_label *sunlabel = self_disklabel(cxt); sunlabel->rpm = SSWAP16(read_int(cxt, 1, SSWAP16(sunlabel->rpm), 100000, 0, _("Rotation speed (rpm)"))); @@ -716,23 +720,23 @@ void sun_set_rspeed(struct fdisk_context *cxt) void sun_set_pcylcount(struct fdisk_context *cxt) { - struct sun_disk_label *sunlabel = context_get_sun_disklabel(cxt); + struct sun_disk_label *sunlabel = self_disklabel(cxt); sunlabel->pcyl = SSWAP16(read_int(cxt, 0, SSWAP16(sunlabel->pcyl), 65535, 0, _("Number of physical cylinders"))); } -static int sun_write_disklabel(struct fdisk_context *cxt, - struct fdisk_label *lb __attribute__((__unused__))) +static int sun_write_disklabel(struct fdisk_context *cxt) { struct sun_disk_label *sunlabel; unsigned short *ush; unsigned short csum = 0; assert(cxt); - assert(lb); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, SUN)); - sunlabel = label_get_sun_disklabel(lb); + sunlabel = self_disklabel(cxt); ush = (unsigned short *) sunlabel; while(ush < (unsigned short *)(&sunlabel->cksum)) @@ -748,17 +752,16 @@ static int sun_write_disklabel(struct fdisk_context *cxt, static struct fdisk_parttype *sun_get_parttype( struct fdisk_context *cxt, - struct fdisk_label *lb, - int n) + size_t n) { - struct sun_disk_label *sunlabel = context_get_sun_disklabel(cxt); + struct sun_disk_label *sunlabel = self_disklabel(cxt); struct fdisk_parttype *t; assert(cxt); - assert(lb); - assert(is_sun_label(lb)); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, SUN)); - if (n < 0 || (size_t) n >= lb->nparts_max) + if (n >= cxt->label->nparts_max) return NULL; t = fdisk_get_parttype_from_code(cxt, SSWAP16(sunlabel->part_tags[n].tag)); @@ -769,8 +772,7 @@ static struct fdisk_parttype *sun_get_parttype( static int sun_set_parttype( struct fdisk_context *cxt, - struct fdisk_label *lb, - int i, + size_t i, struct fdisk_parttype *t) { struct sun_disk_label *sunlabel; @@ -778,12 +780,12 @@ static int sun_set_parttype( struct sun_tag_flag *tag; assert(cxt); - assert(lb); - assert(is_sun_label(lb)); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, SUN)); - sunlabel = label_get_sun_disklabel(lb); + sunlabel = self_disklabel(cxt); - if (i < 0 || (size_t) i >= lb->nparts_max || !t || t->type > UINT16_MAX) + if (i >= cxt->label->nparts_max || !t || t->type > UINT16_MAX) return -EINVAL; if (i == 2 && t->type != SUN_TAG_BACKUP) @@ -821,8 +823,7 @@ static int sun_set_parttype( } -static int sun_reset_alignment(struct fdisk_context *cxt, - struct fdisk_label *lb __attribute__((__unused__))) +static int sun_reset_alignment(struct fdisk_context *cxt) { /* this is shared with DOS ... */ update_units(cxt); @@ -832,15 +833,19 @@ static int sun_reset_alignment(struct fdisk_context *cxt, static int sun_get_partition_status( struct fdisk_context *cxt, - struct fdisk_label *lb __attribute__((__unused__)), - int i, + size_t i, int *status) { - struct sun_disk_label *sunlabel = context_get_sun_disklabel(cxt); + struct sun_disk_label *sunlabel; + + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, SUN)); - if (!status || i < 0 || (size_t) i >= lb->nparts_max) + if (!status || i >= cxt->label->nparts_max) return -EINVAL; + sunlabel = self_disklabel(cxt); *status = FDISK_PARTSTAT_NONE; if (sunlabel->partitions[i].num_sectors) diff --git a/fdisks/fdisksunlabel.h b/fdisks/fdisksunlabel.h index e70171e8ab..e8291a242a 100644 --- a/fdisks/fdisksunlabel.h +++ b/fdisks/fdisksunlabel.h @@ -75,7 +75,7 @@ struct sun_disk_label { #define SUN_LABEL_MAGIC_SWAPPED 0xBEDA /* fdisksunlabel.c */ -extern int sun_change_sysid(struct fdisk_context *cxt, int i, uint16_t sys); +extern int sun_change_sysid(struct fdisk_context *cxt, size_t i, uint16_t sys); extern void sun_list_table(struct fdisk_context *cxt, int xtra); extern void sun_set_alt_cyl(struct fdisk_context *cxt); extern void sun_set_ncyl(struct fdisk_context *cxt, int cyl); @@ -83,8 +83,9 @@ extern void sun_set_xcyl(struct fdisk_context *cxt); extern void sun_set_ilfact(struct fdisk_context *cxt); extern void sun_set_rspeed(struct fdisk_context *cxt); extern void sun_set_pcylcount(struct fdisk_context *cxt); -extern void toggle_sunflags(struct fdisk_context *cxt, int i, uint16_t mask); -extern int sun_is_empty_type(struct fdisk_context *cxt, int i); +extern void toggle_sunflags(struct fdisk_context *cxt, size_t i, uint16_t mask); + +extern int sun_is_empty_type(struct fdisk_context *cxt, size_t i); #endif /* FDISK_SUN_LABEL_H */ diff --git a/libfdisk/src/alignment.c b/libfdisk/src/alignment.c index a89182172a..25323d1b54 100644 --- a/libfdisk/src/alignment.c +++ b/libfdisk/src/alignment.c @@ -369,7 +369,7 @@ int fdisk_reset_alignment(struct fdisk_context *cxt) /* overwrite default by label stuff */ if (cxt->label && cxt->label->op->reset_alignment) - rc = cxt->label->op->reset_alignment(cxt, cxt->label); + rc = cxt->label->op->reset_alignment(cxt); DBG(LABEL, dbgprint("%s alignment reseted to: " "first LBA=%ju, grain=%lu [rc=%d]", diff --git a/libfdisk/src/fdiskP.h b/libfdisk/src/fdiskP.h index bf39211822..1301695a6b 100644 --- a/libfdisk/src/fdiskP.h +++ b/libfdisk/src/fdiskP.h @@ -123,39 +123,39 @@ struct fdisk_geometry { */ struct fdisk_label_operations { /* probe disk label */ - int (*probe)(struct fdisk_context *cxt, struct fdisk_label *lb); + int (*probe)(struct fdisk_context *cxt); /* write in-memory changes to disk */ - int (*write)(struct fdisk_context *cxt, struct fdisk_label *lb); + int (*write)(struct fdisk_context *cxt); /* verify the partition table */ - int (*verify)(struct fdisk_context *cxt, struct fdisk_label *lb); + int (*verify)(struct fdisk_context *cxt); /* create new disk label */ - int (*create)(struct fdisk_context *cxt, struct fdisk_label *lb); + int (*create)(struct fdisk_context *cxt); /* new partition */ - int (*part_add)(struct fdisk_context *cxt, struct fdisk_label *lb, - int partnum, + int (*part_add)(struct fdisk_context *cxt, + size_t partnum, struct fdisk_parttype *t); /* delete partition */ - int (*part_delete)(struct fdisk_context *cxt, struct fdisk_label *lb, - int partnum); + int (*part_delete)(struct fdisk_context *cxt, + size_t partnum); /* get partition type */ struct fdisk_parttype *(*part_get_type)(struct fdisk_context *cxt, - struct fdisk_label *lb, - int partnum); + size_t partnum); /* set partition type */ - int (*part_set_type)(struct fdisk_context *cxt, struct fdisk_label *lb, - int partnum, + int (*part_set_type)(struct fdisk_context *cxt, + size_t partnum, struct fdisk_parttype *t); /* returns FDISK_PARTSTAT_* flags */ - int (*part_get_status)(struct fdisk_context *cxt, struct fdisk_label *lb, - int partnum, int *status); + int (*part_get_status)(struct fdisk_context *cxt, + size_t partnum, + int *status); /* refresh alignment setting */ - int (*reset_alignment)(struct fdisk_context *cxt, - struct fdisk_label *lb); + int (*reset_alignment)(struct fdisk_context *cxt); /* free in-memory label stuff */ void (*free)(struct fdisk_label *lb); + /* deinit in-memory label stuff */ void (*deinit)(struct fdisk_label *lb); }; diff --git a/libfdisk/src/gpt.c b/libfdisk/src/gpt.c index 03fad41efc..2472935dc9 100644 --- a/libfdisk/src/gpt.c +++ b/libfdisk/src/gpt.c @@ -268,12 +268,8 @@ struct fdisk_gpt_label { static void gpt_deinit(struct fdisk_label *lb); -static inline struct fdisk_gpt_label *gpt_label(struct fdisk_context *cxt) +static inline struct fdisk_gpt_label *self_label(struct fdisk_context *cxt) { - assert(cxt); - assert(cxt->label); - assert(fdisk_is_disklabel(cxt, GPT)); - return (struct fdisk_gpt_label *) cxt->label; } @@ -1034,13 +1030,16 @@ done: return totfound; } -static int gpt_probe_label(struct fdisk_context *cxt, struct fdisk_label *lb) +static int gpt_probe_label(struct fdisk_context *cxt) { int mbr_type; - struct fdisk_gpt_label *gpt = (struct fdisk_gpt_label *) lb; + struct fdisk_gpt_label *gpt; - if (!cxt || !lb) - goto failed; + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, GPT)); + + gpt = self_label(cxt); mbr_type = valid_pmbr(cxt); if (!mbr_type) @@ -1068,8 +1067,8 @@ static int gpt_probe_label(struct fdisk_context *cxt, struct fdisk_label *lb) /* OK, probing passed, now initialize backup header and fdisk variables. */ gpt->bheader = gpt_read_header(cxt, last_lba(cxt), NULL); - lb->nparts_max = le32_to_cpu(gpt->pheader->npartition_entries); - lb->nparts_cur = partitions_in_use(gpt->pheader, gpt->ents); + cxt->label->nparts_max = le32_to_cpu(gpt->pheader->npartition_entries); + cxt->label->nparts_cur = partitions_in_use(gpt->pheader, gpt->ents); printf(_("\nWARNING: fdisk GPT support is currently new, and therefore " "in an experimental phase. Use at your own discretion.\n\n")); @@ -1077,7 +1076,7 @@ static int gpt_probe_label(struct fdisk_context *cxt, struct fdisk_label *lb) return 1; failed: DBG(LABEL, dbgprint("GPT probe failed")); - gpt_deinit(lb); + gpt_deinit(cxt->label); return 0; } @@ -1131,9 +1130,17 @@ void gpt_list_table(struct fdisk_context *cxt, int xtra __attribute__ ((__unused__))) { uint32_t i; - struct fdisk_gpt_label *gpt = gpt_label(cxt); - uint64_t fu = le64_to_cpu(gpt->pheader->first_usable_lba); - uint64_t lu = le64_to_cpu(gpt->pheader->last_usable_lba); + struct fdisk_gpt_label *gpt; + uint64_t fu; + uint64_t lu; + + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, GPT)); + + gpt = self_label(cxt); + fu = le64_to_cpu(gpt->pheader->first_usable_lba); + lu = le64_to_cpu(gpt->pheader->last_usable_lba); printf("\n# Start End Size Type Name\n"); @@ -1226,8 +1233,8 @@ static int gpt_write_pmbr(struct fdisk_context *cxt) off_t offset; struct gpt_legacy_mbr *pmbr = NULL; - if (!cxt || !cxt->firstsector) - return -EINVAL; + assert(cxt); + assert(cxt->firstsector); pmbr = (struct gpt_legacy_mbr *) cxt->firstsector; @@ -1269,12 +1276,15 @@ fail: * Returns 0 if successful write, otherwise, a corresponding error. * Any indication of error will abort the operation. */ -static int gpt_write_disklabel(struct fdisk_context *cxt, struct fdisk_label *lb) +static int gpt_write_disklabel(struct fdisk_context *cxt) { - struct fdisk_gpt_label *gpt = (struct fdisk_gpt_label *) lb; + struct fdisk_gpt_label *gpt; - if (!cxt || !lb) - goto err0; + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, GPT)); + + gpt = self_label(cxt); /* we do not want to mess up hybrid MBRs by creating a valid pmbr */ if (valid_pmbr(cxt) == GPT_MBR_HYBRID) @@ -1329,10 +1339,16 @@ err1: * - primary and backup header validations * - paritition validations */ -static int gpt_verify_disklabel(struct fdisk_context *cxt, struct fdisk_label *lb) +static int gpt_verify_disklabel(struct fdisk_context *cxt) { int nerror = 0, ptnum; - struct fdisk_gpt_label *gpt = (struct fdisk_gpt_label *) lb; + struct fdisk_gpt_label *gpt; + + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, GPT)); + + gpt = self_label(cxt); if (!gpt || !gpt->bheader) { nerror++; @@ -1428,15 +1444,18 @@ static int gpt_verify_disklabel(struct fdisk_context *cxt, struct fdisk_label *l /* Delete a single GPT partition, specified by partnum. */ static int gpt_delete_partition(struct fdisk_context *cxt, - struct fdisk_label *lb, - int partnum) + size_t partnum) { - struct fdisk_gpt_label *gpt = (struct fdisk_gpt_label *) lb; + struct fdisk_gpt_label *gpt; - if (!cxt || partnum < 0 || !gpt) - return -EINVAL; + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, GPT)); - if (partition_unused(&gpt->ents[partnum])) + gpt = self_label(cxt); + + if (partnum >= cxt->label->nparts_max + || partition_unused(&gpt->ents[partnum])) return -EINVAL; /* hasta la vista, baby! */ @@ -1446,8 +1465,8 @@ static int gpt_delete_partition(struct fdisk_context *cxt, else { gpt_recompute_crc(gpt->pheader, gpt->ents); gpt_recompute_crc(gpt->bheader, gpt->ents); - lb->nparts_cur--; - fdisk_label_set_changed(lb, 1); + cxt->label->nparts_cur--; + fdisk_label_set_changed(cxt->label, 1); } return 0; @@ -1482,17 +1501,21 @@ static void gpt_entry_set_type(struct gpt_entry *e, struct gpt_guid *type) * Returns 0 on success, or negative upon failure. */ static int gpt_create_new_partition(struct fdisk_context *cxt, - int partnum, uint64_t fsect, uint64_t lsect, + size_t partnum, uint64_t fsect, uint64_t lsect, struct gpt_guid *type, struct gpt_entry *entries) { struct gpt_entry *e = NULL; struct fdisk_gpt_label *gpt; - if (!cxt || fsect > lsect || partnum < 0) - return -EINVAL; + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, GPT)); - gpt = gpt_label(cxt); + gpt = self_label(cxt); + + if (fsect > lsect || partnum >= cxt->label->nparts_max) + return -EINVAL; e = xcalloc(1, sizeof(*e)); e->lba_end = cpu_to_le64(lsect); @@ -1526,20 +1549,24 @@ static int gpt_create_new_partition(struct fdisk_context *cxt, /* Performs logical checks to add a new partition entry */ static int gpt_add_partition( struct fdisk_context *cxt, - struct fdisk_label *lb, - int partnum, + size_t partnum, struct fdisk_parttype *t) { uint64_t user_f, user_l; /* user input ranges for first and last sectors */ uint64_t disk_f, disk_l; /* first and last available sector ranges on device*/ uint64_t dflt_f, dflt_l; /* largest segment (default) */ struct gpt_guid uuid = GPT_DEFAULT_ENTRY_GUID; - struct fdisk_gpt_label *gpt = (struct fdisk_gpt_label *) lb; + struct fdisk_gpt_label *gpt; struct gpt_header *pheader; struct gpt_entry *ents; - /* check basic tests before even considering adding a new partition */ - if (!cxt || partnum < 0 || !gpt) + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, GPT)); + + gpt = self_label(cxt); + + if (partnum >= cxt->label->nparts_max) return -EINVAL; pheader = gpt->pheader; @@ -1614,8 +1641,8 @@ static int gpt_add_partition( printf(_("Could not create partition %d\n"), partnum + 1); else { printf(_("Created partition %d\n"), partnum + 1); - lb->nparts_cur++; - fdisk_label_set_changed(lb, 1); + cxt->label->nparts_cur++; + fdisk_label_set_changed(cxt->label, 1); } return 0; @@ -1624,15 +1651,20 @@ static int gpt_add_partition( /* * Create a new GPT disklabel - destroys any previous data. */ -static int gpt_create_disklabel(struct fdisk_context *cxt, struct fdisk_label *lb) +static int gpt_create_disklabel(struct fdisk_context *cxt) { int rc = 0; ssize_t entry_sz = 0; struct gpt_guid *uid; - struct fdisk_gpt_label *gpt = (struct fdisk_gpt_label *) lb; + struct fdisk_gpt_label *gpt; + + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, GPT)); + + gpt = self_label(cxt); /* label private stuff has to be empty, see gpt_deinit() */ - assert(gpt); assert(gpt->pheader == NULL); assert(gpt->bheader == NULL); @@ -1665,8 +1697,8 @@ static int gpt_create_disklabel(struct fdisk_context *cxt, struct fdisk_label *l gpt_recompute_crc(gpt->pheader, gpt->ents); gpt_recompute_crc(gpt->bheader, gpt->ents); - lb->nparts_max = le32_to_cpu(gpt->pheader->npartition_entries); - lb->nparts_cur = 0; + cxt->label->nparts_max = le32_to_cpu(gpt->pheader->npartition_entries); + cxt->label->nparts_cur = 0; uid = &gpt->pheader->disk_guid; fprintf(stderr, ("Building a new GPT disklabel " @@ -1678,26 +1710,27 @@ static int gpt_create_disklabel(struct fdisk_context *cxt, struct fdisk_label *l uid->node[0], uid->node[1], uid->node[2], uid->node[3], uid->node[4], uid->node[5]); - fdisk_label_set_changed(lb, 1); + fdisk_label_set_changed(cxt->label, 1); done: return rc; } static struct fdisk_parttype *gpt_get_partition_type( struct fdisk_context *cxt, - struct fdisk_label *lb, - int i) + size_t i) { struct fdisk_parttype *t; struct gpt_guid uuid; char str[37]; - struct fdisk_gpt_label *gpt = (struct fdisk_gpt_label *) lb; + struct fdisk_gpt_label *gpt; - if (!cxt) - return NULL; + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, GPT)); - if (!cxt || !gpt || i < 0 - || (uint32_t) i >= le32_to_cpu(gpt->pheader->npartition_entries)) + gpt = self_label(cxt); + + if ((uint32_t) i >= le32_to_cpu(gpt->pheader->npartition_entries)) return NULL; uuid = gpt->ents[i].partition_type_guid; @@ -1714,16 +1747,18 @@ static struct fdisk_parttype *gpt_get_partition_type( static int gpt_set_partition_type( struct fdisk_context *cxt, - struct fdisk_label *lb, - int i, + size_t i, struct fdisk_parttype *t) { struct gpt_guid uuid; - struct fdisk_gpt_label *gpt = (struct fdisk_gpt_label *) lb; + struct fdisk_gpt_label *gpt; + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, GPT)); - if (!cxt || !gpt || i < 0 - || (uint32_t) i >= le32_to_cpu(gpt->pheader->npartition_entries) + gpt = self_label(cxt); + if ((uint32_t) i >= le32_to_cpu(gpt->pheader->npartition_entries) || !t || !t->typestr || string_to_uuid(t->typestr, &uuid) != 0) return -EINVAL; @@ -1731,27 +1766,31 @@ static int gpt_set_partition_type( gpt_recompute_crc(gpt->pheader, gpt->ents); gpt_recompute_crc(gpt->bheader, gpt->ents); - fdisk_label_set_changed(lb, 1); + fdisk_label_set_changed(cxt->label, 1); return 0; } static int gpt_get_partition_status( struct fdisk_context *cxt, - struct fdisk_label *lb __attribute__((__unused__)), - int i, + size_t i, int *status) { - struct fdisk_gpt_label *gpt = gpt_label(cxt); + struct fdisk_gpt_label *gpt; struct gpt_entry *e; - if (!cxt || !gpt || i < 0 || !status - || (uint32_t) i >= le32_to_cpu(gpt->pheader->npartition_entries)) + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, GPT)); + + gpt = self_label(cxt); + + if (!status || (uint32_t) i >= le32_to_cpu(gpt->pheader->npartition_entries)) return -EINVAL; e = &gpt->ents[i]; *status = FDISK_PARTSTAT_NONE; - if (!partition_unused(&gpt->ents[i]) || gpt_partition_size(e)) + if (!partition_unused(e) || gpt_partition_size(e)) *status = FDISK_PARTSTAT_USED; return 0; diff --git a/libfdisk/src/label.c b/libfdisk/src/label.c index 4716b94a65..e631154817 100644 --- a/libfdisk/src/label.c +++ b/libfdisk/src/label.c @@ -21,7 +21,7 @@ int fdisk_probe_labels(struct fdisk_context *cxt) DBG(LABEL, dbgprint("probing for %s", lb->name)); cxt->label = lb; - rc = lb->op->probe(cxt, lb); + rc = lb->op->probe(cxt); cxt->label = org; if (rc != 1) { @@ -76,7 +76,7 @@ int fdisk_write_disklabel(struct fdisk_context *cxt) if (!cxt->label->op->write) return -ENOSYS; - return cxt->label->op->write(cxt, cxt->label); + return cxt->label->op->write(cxt); } /** @@ -94,7 +94,7 @@ int fdisk_verify_disklabel(struct fdisk_context *cxt) if (!cxt->label->op->verify) return -ENOSYS; - return cxt->label->op->verify(cxt, cxt->label); + return cxt->label->op->verify(cxt); } /** @@ -107,7 +107,7 @@ int fdisk_verify_disklabel(struct fdisk_context *cxt) * * Returns 0. */ -int fdisk_add_partition(struct fdisk_context *cxt, int partnum, +int fdisk_add_partition(struct fdisk_context *cxt, size_t partnum, struct fdisk_parttype *t) { if (!cxt || !cxt->label) @@ -115,8 +115,8 @@ int fdisk_add_partition(struct fdisk_context *cxt, int partnum, if (!cxt->label->op->part_add) return -ENOSYS; - DBG(LABEL, dbgprint("adding new partition number %d", partnum)); - cxt->label->op->part_add(cxt, cxt->label, partnum, t); + DBG(LABEL, dbgprint("adding new partition number %zd", partnum)); + cxt->label->op->part_add(cxt, partnum, t); return 0; } @@ -129,16 +129,16 @@ int fdisk_add_partition(struct fdisk_context *cxt, int partnum, * * Returns 0 on success, otherwise, a corresponding error. */ -int fdisk_delete_partition(struct fdisk_context *cxt, int partnum) +int fdisk_delete_partition(struct fdisk_context *cxt, size_t partnum) { if (!cxt || !cxt->label) return -EINVAL; if (!cxt->label->op->part_delete) return -ENOSYS; - DBG(LABEL, dbgprint("deleting %s partition number %d", + DBG(LABEL, dbgprint("deleting %s partition number %zd", cxt->label->name, partnum)); - return cxt->label->op->part_delete(cxt, cxt->label, partnum); + return cxt->label->op->part_delete(cxt, partnum); } /** @@ -176,7 +176,7 @@ int fdisk_create_disklabel(struct fdisk_context *cxt, const char *name) return -ENOSYS; fdisk_reset_alignment(cxt); - return cxt->label->op->create(cxt, cxt->label); + return cxt->label->op->create(cxt); } /** @@ -186,13 +186,14 @@ int fdisk_create_disklabel(struct fdisk_context *cxt, const char *name) * * Returns partition type or NULL upon failure. */ -struct fdisk_parttype *fdisk_get_partition_type(struct fdisk_context *cxt, int partnum) +struct fdisk_parttype *fdisk_get_partition_type(struct fdisk_context *cxt, + size_t partnum) { if (!cxt || !cxt->label || !cxt->label->op->part_get_type) return NULL; - DBG(LABEL, dbgprint("partition: %d: get type", partnum)); - return cxt->label->op->part_get_type(cxt, cxt->label, partnum); + DBG(LABEL, dbgprint("partition: %zd: get type", partnum)); + return cxt->label->op->part_get_type(cxt, partnum); } /** @@ -203,7 +204,8 @@ struct fdisk_parttype *fdisk_get_partition_type(struct fdisk_context *cxt, int p * * Returns 0 on success, < 0 on error. */ -int fdisk_set_partition_type(struct fdisk_context *cxt, int partnum, +int fdisk_set_partition_type(struct fdisk_context *cxt, + size_t partnum, struct fdisk_parttype *t) { if (!cxt || !cxt->label) @@ -211,8 +213,8 @@ int fdisk_set_partition_type(struct fdisk_context *cxt, int partnum, if (!cxt->label->op->part_set_type) return -ENOSYS; - DBG(LABEL, dbgprint("partition: %d: set type", partnum)); - return cxt->label->op->part_set_type(cxt, cxt->label, partnum, t); + DBG(LABEL, dbgprint("partition: %zd: set type", partnum)); + return cxt->label->op->part_set_type(cxt, partnum, t); } /** @@ -237,14 +239,16 @@ size_t fdisk_get_nparttypes(struct fdisk_context *cxt) * * Returns 0 on success, otherwise, a corresponding error. */ -int fdisk_partition_get_status(struct fdisk_context *cxt, int partnum, int *status) +int fdisk_partition_get_status(struct fdisk_context *cxt, + size_t partnum, + int *status) { if (!cxt || !cxt->label) return -EINVAL; if (!cxt->label->op->part_get_status) return -ENOSYS; - return cxt->label->op->part_get_status(cxt, cxt->label, partnum, status); + return cxt->label->op->part_get_status(cxt, partnum, status); } @@ -263,7 +267,6 @@ void fdisk_deinit_label(struct fdisk_label *lb) void fdisk_label_set_changed(struct fdisk_label *lb, int changed) { assert(lb); - lb->changed = changed ? 1 : 0; } diff --git a/libfdisk/src/libfdisk.h b/libfdisk/src/libfdisk.h index 967d21e9a5..18b14163ec 100644 --- a/libfdisk/src/libfdisk.h +++ b/libfdisk/src/libfdisk.h @@ -85,17 +85,17 @@ extern int fdisk_write_disklabel(struct fdisk_context *cxt); extern int fdisk_verify_disklabel(struct fdisk_context *cxt); extern int fdisk_create_disklabel(struct fdisk_context *cxt, const char *name); -extern int fdisk_add_partition(struct fdisk_context *cxt, int partnum, struct fdisk_parttype *t); -extern int fdisk_delete_partition(struct fdisk_context *cxt, int partnum); +extern int fdisk_add_partition(struct fdisk_context *cxt, size_t partnum, struct fdisk_parttype *t); +extern int fdisk_delete_partition(struct fdisk_context *cxt, size_t partnum); -extern struct fdisk_parttype *fdisk_get_partition_type(struct fdisk_context *cxt, int partnum); -extern int fdisk_set_partition_type(struct fdisk_context *cxt, int partnum, +extern struct fdisk_parttype *fdisk_get_partition_type(struct fdisk_context *cxt, size_t partnum); +extern int fdisk_set_partition_type(struct fdisk_context *cxt, size_t partnum, struct fdisk_parttype *t); extern void fdisk_label_set_changed(struct fdisk_label *lb, int changed); extern int fdisk_label_is_changed(struct fdisk_label *lb); -extern int fdisk_partition_get_status(struct fdisk_context *cxt, int partnum, int *status); +extern int fdisk_partition_get_status(struct fdisk_context *cxt, size_t partnum, int *status); /* alignment.c */ extern int fdisk_reset_alignment(struct fdisk_context *cxt);