Fix pvresize to handle the second metadata area properly
PATCH 1/1 Fix pvresize to handle the second metadata area properly
Hi,
this is a patch for pvresize that solves the problem of second metadata copy not beeing written to new position at the end of PV when the PV is resized.
Some code from _pv_resize_single function (pvresize.c) is now placed in pv_resize (pv_manip.c) and new format handler function called _text_pv_resize is added. This way we can call format specific recalculation of metadata area offsets, sizes and alignment, which is useful for resizing the PV and moving the second metadata copy to new position properly. If the PV contains a VG, the VG is reduced or extended and finally the _text_pv_write is called to write new metadata headers (_text_pv_write was modified so it can handle the PV with a VG now).
Peter
lib/format_text/format-text.c +112 -9
@@ -1294,7 +1294,35 @@ static int _mda_setup(const struct format_type *fmt,return 1 ; } -/* Only for orphans */ +static void _pv_get_mdacs (struct physical_volume *pv,struct dm_list *mdas, +struct mda_context **mdac1,struct mda_context **mdac2) +{ +struct metadata_area *mda; +struct mda_context *mdac; + + *mdac1 =NULL ; + *mdac2 =NULL ; + + dm_list_iterate_items(mda, mdas) { + mdac = (struct mda_context *) mda->metadata_locn; + +if (mdac->area.dev != pv->dev) +continue ; + +if (*mdac1) + *mdac2 = mdac; +else + *mdac1 = mdac; + } + +if ((*mdac1 !=NULL ) && (*mdac2 !=NULL )) +if ((*mdac1)->area.start > (*mdac2)->area.start) { + mdac = *mdac1; + *mdac1 = *mdac2; + *mdac2 = mdac; + } +} +/* Set label_sector to -1 if rewriting existing label into same sector */ static int _text_pv_write (const struct format_type *fmt,struct physical_volume *pv,struct dm_list *mdas,int64_t label_sector)
@@ -1305,13 +1333,21 @@ static int _text_pv_write(const struct format_type *fmt, struct physical_volumestruct metadata_area *mda; char buf[MDA_HEADER_SIZE] __attribute((aligned(8)));struct mda_header *mdah = (struct mda_header *) buf; +struct mda_context *mdac1 =NULL , *mdac2 =NULL ;uint64_t adjustment;/* FIXME Test mode don't update cache? */ -if (!(info =lvmcache_add (fmt->labeller, (char *) &pv->id, pv->dev, -FMT_TEXT_ORPHAN_VG_NAME ,NULL ,0 ))) - return_0; +if (is_orphan_vg (pv_vg_name (pv))) { +if (!(info =lvmcache_add (fmt->labeller, (char *) &pv->id, pv->dev, +FMT_TEXT_ORPHAN_VG_NAME ,NULL ,0 ))) + return_0; + } +else { +if (!(info =lvmcache_add (fmt->labeller, (char *) &pv->id, pv->dev, +pv_vg_name (pv), (char *) pv->vgid.uuid,0 ))) + return_0; + } label = info->label;if (label_sector !=-1 )
@@ -1370,12 +1406,31 @@ static int _text_pv_write(const struct format_type *fmt, struct physical_volumeif (!dev_open (pv->dev)) return_0; - dm_list_iterate_items(mda, &info->mdas) { - mdac = mda->metadata_locn; +_pv_get_mdacs (pv, &info->mdas, &mdac1, &mdac2); + +/* If PV contains VG, we keep the pointer to current metadata in +circular buffer so the old metadata records won't be overwritten. +We can do this for the first metadata area only, the other ones +could be moved and the pointer information lost. */ + +/* first metadata area header */ +if (mdac1 &&is_orphan_vg (pv_vg_name (pv))) {memset (&buf,0 ,sizeof (buf)); - mdah->size = mdac->area.size; -if (!_raw_write_mda_header (fmt, mdac->area.dev, - mdac->area.start, mdah)) { + mdah->size = mdac1->area.size; +if (!_raw_write_mda_header (fmt, mdac1->area.dev, + mdac1->area.start, mdah)) { +if (!dev_close (pv->dev)) + stack; + return_0; + } + } + +/* second metadata area header */ +if (mdac2) { +memset (&buf,0 ,sizeof (buf)); + mdah->size = mdac2->area.size; +if (!_raw_write_mda_header (fmt, mdac2->area.dev, + mdac2->area.start, mdah)) {if (!dev_close (pv->dev)) stack; return_0;
@@ -1393,6 +1448,53 @@ static int _text_pv_write(const struct format_type *fmt, struct physical_volumereturn 1 ; } +static int _text_pv_resize (const struct format_type *fmt,struct physical_volume *pv, +struct volume_group *vg,struct dm_list *mdas,uint64_t new_size) +{ +const char *pv_name =pv_dev_name (pv); +struct mda_context *mdac1, *mdac2; +uint64_t mda_start2 =0 , mda_size2 =0 ; +uint64_t mda_adjustment, disk_size; + + pv->size = new_size; + disk_size = pv->size <<SECTOR_SHIFT ; + +if (pv->size < pv->pe_start) { +log_error ("%s: Size must exceed physical extent start of " +"%" PRIu64" sectors." , pv_name, pv->pe_start); +return 0 ; + } + +_pv_get_mdacs (pv, mdas, &mdac1, &mdac2); + +if (!mdac1) +return 0 ; + +if (mdac2) { + mda_size2 = mdac1->area.size; + mda_adjustment = (disk_size - mda_size2) % (pe_align (pv) <<SECTOR_SHIFT ); +if (mda_adjustment) + mda_size2 += mda_adjustment; + + mda_start2 = disk_size - mda_size2; + +if ((mda_start2 >>SECTOR_SHIFT ) < pv->pe_start) { +log_error ("%s: Size must leave space for second " +"metadata copy of %" PRIu64" sectors." , + pv_name, mda_size2 >>SECTOR_SHIFT ); +return 0 ; + } + + mdac2->area.start = mda_start2; + mdac2->area.size = mda_size2; + } + +if (vg) + pv->size -= pv->pe_start + (mda_size2 >>SECTOR_SHIFT ); + +return 1 ; +} +static int _add_raw (struct dm_list *raw_list,struct device_area *dev_area) {struct raw_list *rl;
@@ -1829,6 +1931,7 @@ static struct format_handler _text_handler = { .pv_read = _text_pv_read, .pv_setup = _text_pv_setup, .pv_write = _text_pv_write, + .pv_resize = _text_pv_resize, .vg_setup = _text_vg_setup, .lv_setup = _text_lv_setup, .create_instance = _text_create_text_instance,
lib/metadata/metadata-exported.h +2 -2
@@ -364,10 +364,10 @@ pv_t *pv_create(const struct cmd_context *cmd,uint32_t existing_extent_size, int pvmetadatacopies, uint64_t pvmetadatasize, struct dm_list *mdas); -int pv_resize (struct physical_volume *pv,struct volume_group *vg, -uint32_t new_pe_count);int pv_analyze (struct cmd_context *cmd,const char *pv_name,uint64_t label_sector); +int pv_resize (struct physical_volume *pv,struct volume_group *vg, +struct dm_list *mdas,uint64_t new_size);/* FIXME: move internal to library */ uint32_t pv_list_extents_free (const struct dm_list *pvh);
lib/metadata/metadata.h +9 -0
@@ -223,6 +223,15 @@ struct format_handler { int64_t label_sector);/* +* Recalculate existing metadata offsets and sizes if new +* PV size is set. If the PV contains a VG, the PV size is +* decreased by the size of metadata areas. +*/ +int (*pv_resize) (const struct format_type * fmt, +struct physical_volume * pv,struct volume_group * vg, +struct dm_list * mdas,uint64_t new_size); + +/* * Tweak an already filled out a lv eg, check there * aren't too many extents. */
lib/metadata/pv_manip.c +68 -14
90 lines not on this page · raw message
tools/pvresize.c +8 -64
112 lines not on this page · raw message