1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

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
This commit is contained in:
David Teigland 2020-09-18 14:42:23 -05:00
parent 1570e76233
commit 1404e5ee61
5 changed files with 90 additions and 0 deletions

16
lib/cache/lvmcache.c vendored
View File

@ -996,6 +996,22 @@ int lvmcache_label_rescan_vg_rw(struct cmd_context *cmd, const char *vgname, con
return _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

View File

@ -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,

View File

@ -1447,6 +1447,69 @@ int label_scan_open_rw(struct device *dev)
return label_scan_open(dev);
}
int label_scan_reopen_rw(struct device *dev)
{
int flags = 0;
int prev_fd = dev->bcache_fd;
int fd;
if (!(dev->flags & DEV_IN_BCACHE)) {
if ((dev->bcache_fd != -1) || (dev->bcache_di != -1)) {
/* shouldn't happen */
log_debug("Reopen writeable %s uncached fd %d di %d",
dev_name(dev), dev->bcache_fd, dev->bcache_di);
return 0;
}
goto do_open;
}
if ((dev->flags & DEV_BCACHE_WRITE))
return 1;
if (dev->bcache_fd == -1) {
log_error("Failed to open writable %s index %d fd none",
dev_name(dev), dev->bcache_di);
return 0;
}
if (dev->bcache_di == -1) {
log_error("Failed to open writeable %s index none fd %d",
dev_name(dev), dev->bcache_fd);
return 0;
}
do_open:
flags |= O_DIRECT;
flags |= O_NOATIME;
flags |= O_RDWR;
fd = open(dev_name(dev), flags, 0777);
if (fd < 0) {
log_error("Failed to open rw %s errno %d di %d fd %d.",
dev_name(dev), errno, dev->bcache_di, dev->bcache_fd);
return 0;
}
if (!bcache_change_fd(dev->bcache_di, fd)) {
log_error("Failed to change to rw fd %s di %d fd %d.",
dev_name(dev), dev->bcache_di, fd);
close(fd);
return 0;
}
if (close(dev->bcache_fd))
log_debug("reopen writeable %s close prev errno %d di %d fd %d.",
dev_name(dev), errno, dev->bcache_di, dev->bcache_fd);
dev->flags |= DEV_IN_BCACHE;
dev->flags |= DEV_BCACHE_WRITE;
dev->bcache_fd = fd;
log_debug("reopen writable %s di %d prev %d fd %d",
dev_name(dev), dev->bcache_di, prev_fd, fd);
return 1;
}
bool dev_read_bytes(struct device *dev, uint64_t start, size_t len, void *data)
{
if (!scan_bcache) {

View File

@ -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);

View File

@ -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,