master - metadata: open rw fd before closing ro fd
Gitweb: https://sourceware.org/git/?p=lvm2.git;a=commitdiff;h=1404e5ee61bdaddeb07a5e9ecfe1aad477cc9bf9 Commit: 1404e5ee61bdaddeb07a5e9ecfe1aad477cc9bf9 Parent: 1570e76233398957911c3559b40de6e03c37df9a Author: David Teigland <[email protected]> AuthorDate: Fri Sep 18 14:42:23 2020 -0500 Committer: David Teigland <[email protected]> CommitterDate: Fri Sep 18 15:10:11 2020 -0500
metadata: open rw fd before closing ro fd
lvm opens devices readonly to scan them, but needs to open then readwrite to update the metadata. Previously, the ro fd was closed before the rw fd was opened, leaving a small gap where the dev was not held open, and during which the dev could possibly change which storage it referred to.
With the bcache_change_fd() interface, lvm opens a rw fd on a device to be written, tells bcache to change to the new rw fd, and closes the ro fd.
. open dev ro
. read dev with the ro fd (label_scan)
. lock vg (ex for writing)
. open dev rw
. close ro fd
. rescan dev to check if the metadata changed
between the scan and the lock
. if the metadata did change, reread in full . write the metadata
--- lib/cache/lvmcache.c | 16 +++++++++++++ lib/cache/lvmcache.h | 1 + lib/label/label.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/label/label.h | 1 + lib/metadata/metadata.c | 9 +++++++ 5 files changed, 90 insertions(+)
lib/cache/lvmcache.c +16 -0
@@ -996,6 +996,22 @@ int lvmcache_label_rescan_vg_rw(struct cmd_context *cmd, const char *vgname, conreturn _label_rescan_vg (cmd, vgname, vgid,1 ); } +int lvmcache_label_reopen_vg_rw (struct cmd_context *cmd,const char *vgname,const char *vgid) +{ +struct lvmcache_vginfo *vginfo; +struct lvmcache_info *info; + +if (!(vginfo =lvmcache_vginfo_from_vgname (vgname, vgid))) + return_0; + + dm_list_iterate_items(info, &vginfo->infos) { +if (!label_scan_reopen_rw (info->dev)) + return_0; + } + +return 1 ; +} +/* * Uses label_scan to populate lvmcache with 'vginfo' struct for each VG * and associated 'info' structs for those VGs. Only VG summary information
lib/cache/lvmcache.h +1 -0
@@ -69,6 +69,7 @@ void lvmcache_destroy(struct cmd_context *cmd, int retain_orphans, int reset);int lvmcache_label_scan (struct cmd_context *cmd);int lvmcache_label_rescan_vg (struct cmd_context *cmd,const char *vgname,const char *vgid);int lvmcache_label_rescan_vg_rw (struct cmd_context *cmd,const char *vgname,const char *vgid); +int lvmcache_label_reopen_vg_rw (struct cmd_context *cmd,const char *vgname,const char *vgid);/* Add/delete a device */ struct lvmcache_info *lvmcache_add(struct cmd_context *cmd, struct labeller *labeller, const char *pvid,
lib/label/label.c +63 -0
69 lines not on this page · raw message
lib/label/label.h +1 -0
@@ -117,6 +117,7 @@ int label_scan_setup_bcache(void);int label_scan_open (struct device *dev);int label_scan_open_excl (struct device *dev);int label_scan_open_rw (struct device *dev); +int label_scan_reopen_rw (struct device *dev);int label_scan_for_pvid (struct cmd_context *cmd,char *pvid,struct device **dev_out);
lib/metadata/metadata.c +9 -0
@@ -4688,6 +4688,15 @@ static struct volume_group *_vg_read(struct cmd_context *cmd,log_debug_metadata ("Reading VG %s %s" , vgname ?:"<no name>" , vgid ?:"<no vgid>" ); +/* +* Devices are generally open readonly from scanning, and we need to +* reopen them rw to update metadata. We want to reopen them rw before +* before rescanning and/or writing. Reopening rw preserves the existing +* bcache blocks for the devs. +*/ +if (writing) +lvmcache_label_reopen_vg_rw (cmd, vgname, vgid); +/* * Rescan the devices that are associated with this vg in lvmcache. * This repeats what was done by the command's initial label scan,