Blame SOURCES/lvm2-2_02_182-metadata-prevent-writing-beyond-metadata-area.patch

9ba073
From 87bd4a350468023b14251b9bde98f88ffb419268 Mon Sep 17 00:00:00 2001
9ba073
From: David Teigland <teigland@redhat.com>
9ba073
Date: Mon, 29 Oct 2018 11:06:00 -0500
9ba073
Subject: [PATCH 2/2] metadata: prevent writing beyond metadata area
9ba073
9ba073
lvm uses a bcache block size of 128K.  A bcache block
9ba073
at the end of the metadata area will overlap the PEs
9ba073
from which LVs are allocated.  How much depends on
9ba073
alignments.  When lvm reads and writes one of these
9ba073
bcache blocks to update VG metadata, it can also be
9ba073
reading and writing PEs that belong to an LV.
9ba073
9ba073
If these overlapping PEs are being written to by the
9ba073
LV user (e.g. filesystem) at the same time that lvm
9ba073
is modifying VG metadata in the overlapping bcache
9ba073
block, then the user's updates to the PEs can be lost.
9ba073
9ba073
This patch is a quick hack to prevent lvm from writing
9ba073
past the end of the metadata area.
9ba073
9ba073
(cherry picked from commit ab27d5dc2a5c3bf23ab8fed438f1542015dc723d)
9ba073
---
9ba073
 lib/device/bcache.c           | 79 +++++++++++++++++++++++++++++++++++++++++--
9ba073
 lib/device/bcache.h           |  3 ++
9ba073
 lib/format_text/format-text.c | 10 ++++++
9ba073
 lib/label/label.c             | 35 ++++++++++++++++++-
9ba073
 lib/label/label.h             |  2 ++
9ba073
 lib/metadata/mirror.c         |  4 +++
9ba073
 6 files changed, 130 insertions(+), 3 deletions(-)
9ba073
9ba073
diff --git a/lib/device/bcache.c b/lib/device/bcache.c
9ba073
index b1f7d2a..5ac2558 100644
9ba073
--- a/lib/device/bcache.c
9ba073
+++ b/lib/device/bcache.c
9ba073
@@ -156,6 +156,10 @@ static void _async_destroy(struct io_engine *ioe)
9ba073
 	dm_free(e);
9ba073
 }
9ba073
 
9ba073
+static int _last_byte_fd;
9ba073
+static uint64_t _last_byte_offset;
9ba073
+static int _last_byte_sector_size;
9ba073
+
9ba073
 static bool _async_issue(struct io_engine *ioe, enum dir d, int fd,
9ba073
 			 sector_t sb, sector_t se, void *data, void *context)
9ba073
 {
9ba073
@@ -163,12 +167,53 @@ static bool _async_issue(struct io_engine *ioe, enum dir d, int fd,
9ba073
 	struct iocb *cb_array[1];
9ba073
 	struct control_block *cb;
9ba073
 	struct async_engine *e = _to_async(ioe);
9ba073
+	sector_t offset;
9ba073
+	sector_t nbytes;
9ba073
+	sector_t limit_nbytes;
9ba073
+	sector_t extra_nbytes = 0;
9ba073
 
9ba073
 	if (((uintptr_t) data) & e->page_mask) {
9ba073
 		log_warn("misaligned data buffer");
9ba073
 		return false;
9ba073
 	}
9ba073
 
9ba073
+	offset = sb << SECTOR_SHIFT;
9ba073
+	nbytes = (se - sb) << SECTOR_SHIFT;
9ba073
+
9ba073
+	/*
9ba073
+	 * If bcache block goes past where lvm wants to write, then clamp it.
9ba073
+	 */
9ba073
+	if ((d == DIR_WRITE) && _last_byte_offset && (fd == _last_byte_fd)) {
9ba073
+		if (offset > _last_byte_offset) {
9ba073
+			log_error("Limit write at %llu len %llu beyond last byte %llu",
9ba073
+				  (unsigned long long)offset,
9ba073
+				  (unsigned long long)nbytes,
9ba073
+				  (unsigned long long)_last_byte_offset);
9ba073
+			return false;
9ba073
+		}
9ba073
+
9ba073
+		if (offset + nbytes > _last_byte_offset) {
9ba073
+			limit_nbytes = _last_byte_offset - offset;
9ba073
+			if (limit_nbytes % _last_byte_sector_size)
9ba073
+				extra_nbytes = _last_byte_sector_size - (limit_nbytes % _last_byte_sector_size);
9ba073
+
9ba073
+			if (extra_nbytes) {
9ba073
+				log_debug("Limit write at %llu len %llu to len %llu rounded to %llu",
9ba073
+					  (unsigned long long)offset,
9ba073
+					  (unsigned long long)nbytes,
9ba073
+					  (unsigned long long)limit_nbytes,
9ba073
+					  (unsigned long long)(limit_nbytes + extra_nbytes));
9ba073
+				nbytes = limit_nbytes + extra_nbytes;
9ba073
+			} else {
9ba073
+				log_debug("Limit write at %llu len %llu to len %llu",
9ba073
+					  (unsigned long long)offset,
9ba073
+					  (unsigned long long)nbytes,
9ba073
+					  (unsigned long long)limit_nbytes);
9ba073
+				nbytes = limit_nbytes;
9ba073
+			}
9ba073
+		}
9ba073
+	}
9ba073
+
9ba073
 	cb = _cb_alloc(e->cbs, context);
9ba073
 	if (!cb) {
9ba073
 		log_warn("couldn't allocate control block");
9ba073
@@ -179,10 +224,22 @@ static bool _async_issue(struct io_engine *ioe, enum dir d, int fd,
9ba073
 
9ba073
 	cb->cb.aio_fildes = (int) fd;
9ba073
 	cb->cb.u.c.buf = data;
9ba073
-	cb->cb.u.c.offset = sb << SECTOR_SHIFT;
9ba073
-	cb->cb.u.c.nbytes = (se - sb) << SECTOR_SHIFT;
9ba073
+	cb->cb.u.c.offset = offset;
9ba073
+	cb->cb.u.c.nbytes = nbytes;
9ba073
 	cb->cb.aio_lio_opcode = (d == DIR_READ) ? IO_CMD_PREAD : IO_CMD_PWRITE;
9ba073
 
9ba073
+#if 0
9ba073
+	if (d == DIR_READ) {
9ba073
+		log_debug("io R off %llu bytes %llu",
9ba073
+			  (unsigned long long)cb->cb.u.c.offset,
9ba073
+			  (unsigned long long)cb->cb.u.c.nbytes);
9ba073
+	} else {
9ba073
+		log_debug("io W off %llu bytes %llu",
9ba073
+			  (unsigned long long)cb->cb.u.c.offset,
9ba073
+			  (unsigned long long)cb->cb.u.c.nbytes);
9ba073
+	}
9ba073
+#endif
9ba073
+
9ba073
 	cb_array[0] = &cb->cb;
9ba073
 	do {
9ba073
 		r = io_submit(e->aio_context, 1, cb_array);
9ba073
@@ -1145,3 +1202,21 @@ bool bcache_invalidate_fd(struct bcache *cache, int fd)
9ba073
 
9ba073
 //----------------------------------------------------------------
9ba073
 
9ba073
+void bcache_set_last_byte(struct bcache *cache, int fd, uint64_t offset, int sector_size)
9ba073
+{
9ba073
+	_last_byte_fd = fd;
9ba073
+	_last_byte_offset = offset;
9ba073
+	_last_byte_sector_size = sector_size;
9ba073
+	if (!sector_size)
9ba073
+		_last_byte_sector_size = 512;
9ba073
+}
9ba073
+
9ba073
+void bcache_unset_last_byte(struct bcache *cache, int fd)
9ba073
+{
9ba073
+	if (_last_byte_fd == fd) {
9ba073
+		_last_byte_fd = 0;
9ba073
+		_last_byte_offset = 0;
9ba073
+		_last_byte_sector_size = 0;
9ba073
+	}
9ba073
+}
9ba073
+
9ba073
diff --git a/lib/device/bcache.h b/lib/device/bcache.h
9ba073
index b0aebb4..cb902ef 100644
9ba073
--- a/lib/device/bcache.h
9ba073
+++ b/lib/device/bcache.h
9ba073
@@ -158,6 +158,9 @@ bool bcache_write_bytes(struct bcache *cache, int fd, uint64_t start, size_t len
9ba073
 bool bcache_zero_bytes(struct bcache *cache, int fd, uint64_t start, size_t len);
9ba073
 bool bcache_set_bytes(struct bcache *cache, int fd, uint64_t start, size_t len, uint8_t val);
9ba073
 
9ba073
+void bcache_set_last_byte(struct bcache *cache, int fd, uint64_t offset, int sector_size);
9ba073
+void bcache_unset_last_byte(struct bcache *cache, int fd);
9ba073
+
9ba073
 //----------------------------------------------------------------
9ba073
 
9ba073
 #endif
9ba073
diff --git a/lib/format_text/format-text.c b/lib/format_text/format-text.c
9ba073
index 792d75a..36afba1 100644
9ba073
--- a/lib/format_text/format-text.c
9ba073
+++ b/lib/format_text/format-text.c
9ba073
@@ -400,10 +400,14 @@ static int _raw_write_mda_header(const struct format_type *fmt,
9ba073
 					     MDA_HEADER_SIZE -
9ba073
 					     sizeof(mdah->checksum_xl)));
9ba073
 
9ba073
+	dev_set_last_byte(dev, start_byte + MDA_HEADER_SIZE);
9ba073
+
9ba073
 	if (!dev_write_bytes(dev, start_byte, MDA_HEADER_SIZE, mdah)) {
9ba073
+		dev_unset_last_byte(dev);
9ba073
 		log_error("Failed to write mda header to %s fd %d", dev_name(dev), dev->bcache_fd);
9ba073
 		return 0;
9ba073
 	}
9ba073
+	dev_unset_last_byte(dev);
9ba073
 
9ba073
 	return 1;
9ba073
 }
9ba073
@@ -677,10 +681,13 @@ static int _vg_write_raw(struct format_instance *fid, struct volume_group *vg,
9ba073
 			    (unsigned long long)(mdac->rlocn.size - new_wrap),
9ba073
 			    (unsigned long long)new_wrap);
9ba073
 
9ba073
+	dev_set_last_byte(mdac->area.dev, mdac->area.start + mdah->size);
9ba073
+
9ba073
 	if (!dev_write_bytes(mdac->area.dev, mdac->area.start + mdac->rlocn.offset,
9ba073
 		                (size_t) (mdac->rlocn.size - new_wrap),
9ba073
 		                fidtc->raw_metadata_buf)) {
9ba073
 		log_error("Failed to write metadata to %s fd %d", dev_name(mdac->area.dev), mdac->area.dev->bcache_fd);
9ba073
+		dev_unset_last_byte(mdac->area.dev);
9ba073
 		goto out;
9ba073
 	}
9ba073
 
9ba073
@@ -694,10 +701,13 @@ static int _vg_write_raw(struct format_instance *fid, struct volume_group *vg,
9ba073
 			                (size_t) new_wrap,
9ba073
 			                fidtc->raw_metadata_buf + mdac->rlocn.size - new_wrap)) {
9ba073
 			log_error("Failed to write metadata wrap to %s fd %d", dev_name(mdac->area.dev), mdac->area.dev->bcache_fd);
9ba073
+			dev_unset_last_byte(mdac->area.dev);
9ba073
 			goto out;
9ba073
 		}
9ba073
 	}
9ba073
 
9ba073
+	dev_unset_last_byte(mdac->area.dev);
9ba073
+
9ba073
 	mdac->rlocn.checksum = calc_crc(INITIAL_CRC, (uint8_t *)fidtc->raw_metadata_buf,
9ba073
 					(uint32_t) (mdac->rlocn.size -
9ba073
 						    new_wrap));
9ba073
diff --git a/lib/label/label.c b/lib/label/label.c
9ba073
index bafa543..d2cfe62 100644
9ba073
--- a/lib/label/label.c
9ba073
+++ b/lib/label/label.c
9ba073
@@ -172,6 +172,7 @@ int label_write(struct device *dev, struct label *label)
9ba073
 {
9ba073
 	char buf[LABEL_SIZE] __attribute__((aligned(8)));
9ba073
 	struct label_header *lh = (struct label_header *) buf;
9ba073
+	uint64_t offset;
9ba073
 	int r = 1;
9ba073
 
9ba073
 	if (!label->labeller->ops->write) {
9ba073
@@ -206,11 +207,17 @@ int label_write(struct device *dev, struct label *label)
9ba073
 		return 0;
9ba073
 	}
9ba073
 
9ba073
-	if (!dev_write_bytes(dev, label->sector << SECTOR_SHIFT, LABEL_SIZE, buf)) {
9ba073
+	offset = label->sector << SECTOR_SHIFT;
9ba073
+
9ba073
+	dev_set_last_byte(dev, offset + LABEL_SIZE);
9ba073
+
9ba073
+	if (!dev_write_bytes(dev, offset, LABEL_SIZE, buf)) {
9ba073
 		log_debug_devs("Failed to write label to %s", dev_name(dev));
9ba073
 		r = 0;
9ba073
 	}
9ba073
 
9ba073
+	dev_unset_last_byte(dev);
9ba073
+
9ba073
 	return r;
9ba073
 }
9ba073
 
9ba073
@@ -1256,9 +1263,12 @@ bool dev_write_zeros(struct device *dev, uint64_t start, size_t len)
9ba073
 		}
9ba073
 	}
9ba073
 
9ba073
+	dev_set_last_byte(dev, start + len);
9ba073
+
9ba073
 	if (!bcache_zero_bytes(scan_bcache, dev->bcache_fd, start, len)) {
9ba073
 		log_error("Error writing device %s at %llu length %u.",
9ba073
 			  dev_name(dev), (unsigned long long)start, (uint32_t)len);
9ba073
+		dev_unset_last_byte(dev);
9ba073
 		label_scan_invalidate(dev);
9ba073
 		return false;
9ba073
 	}
9ba073
@@ -1266,9 +1276,11 @@ bool dev_write_zeros(struct device *dev, uint64_t start, size_t len)
9ba073
 	if (!bcache_flush(scan_bcache)) {
9ba073
 		log_error("Error writing device %s at %llu length %u.",
9ba073
 			  dev_name(dev), (unsigned long long)start, (uint32_t)len);
9ba073
+		dev_unset_last_byte(dev);
9ba073
 		label_scan_invalidate(dev);
9ba073
 		return false;
9ba073
 	}
9ba073
+	dev_unset_last_byte(dev);
9ba073
 	return true;
9ba073
 }
9ba073
 
9ba073
@@ -1302,9 +1314,12 @@ bool dev_set_bytes(struct device *dev, uint64_t start, size_t len, uint8_t val)
9ba073
 		}
9ba073
 	}
9ba073
 
9ba073
+	dev_set_last_byte(dev, start + len);
9ba073
+
9ba073
 	if (!bcache_set_bytes(scan_bcache, dev->bcache_fd, start, len, val)) {
9ba073
 		log_error("Error writing device %s at %llu length %u.",
9ba073
 			  dev_name(dev), (unsigned long long)start, (uint32_t)len);
9ba073
+		dev_unset_last_byte(dev);
9ba073
 		label_scan_invalidate(dev);
9ba073
 		return false;
9ba073
 	}
9ba073
@@ -1312,9 +1327,27 @@ bool dev_set_bytes(struct device *dev, uint64_t start, size_t len, uint8_t val)
9ba073
 	if (!bcache_flush(scan_bcache)) {
9ba073
 		log_error("Error writing device %s at %llu length %u.",
9ba073
 			  dev_name(dev), (unsigned long long)start, (uint32_t)len);
9ba073
+		dev_unset_last_byte(dev);
9ba073
 		label_scan_invalidate(dev);
9ba073
 		return false;
9ba073
 	}
9ba073
+
9ba073
+	dev_unset_last_byte(dev);
9ba073
 	return true;
9ba073
 }
9ba073
 
9ba073
+void dev_set_last_byte(struct device *dev, uint64_t offset)
9ba073
+{
9ba073
+	unsigned int phys_block_size = 0;
9ba073
+	unsigned int block_size = 0;
9ba073
+
9ba073
+	dev_get_block_size(dev, &phys_block_size, &block_size);
9ba073
+
9ba073
+	bcache_set_last_byte(scan_bcache, dev->bcache_fd, offset, phys_block_size);
9ba073
+}
9ba073
+
9ba073
+void dev_unset_last_byte(struct device *dev)
9ba073
+{
9ba073
+	bcache_unset_last_byte(scan_bcache, dev->bcache_fd);
9ba073
+}
9ba073
+
9ba073
diff --git a/lib/label/label.h b/lib/label/label.h
9ba073
index 5ed8bc8..e2b8263 100644
9ba073
--- a/lib/label/label.h
9ba073
+++ b/lib/label/label.h
9ba073
@@ -125,5 +125,7 @@ bool dev_read_bytes(struct device *dev, uint64_t start, size_t len, void *data);
9ba073
 bool dev_write_bytes(struct device *dev, uint64_t start, size_t len, void *data);
9ba073
 bool dev_write_zeros(struct device *dev, uint64_t start, size_t len);
9ba073
 bool dev_set_bytes(struct device *dev, uint64_t start, size_t len, uint8_t val);
9ba073
+void dev_set_last_byte(struct device *dev, uint64_t offset);
9ba073
+void dev_unset_last_byte(struct device *dev);
9ba073
 
9ba073
 #endif
9ba073
diff --git a/lib/metadata/mirror.c b/lib/metadata/mirror.c
9ba073
index c7d8a9e..b1dcaa0 100644
9ba073
--- a/lib/metadata/mirror.c
9ba073
+++ b/lib/metadata/mirror.c
9ba073
@@ -302,10 +302,14 @@ static int _write_log_header(struct cmd_context *cmd, struct logical_volume *lv)
9ba073
 		return 0;
9ba073
 	}
9ba073
 
9ba073
+	dev_set_last_byte(dev, sizeof(log_header));
9ba073
+
9ba073
 	if (!dev_write_bytes(dev, UINT64_C(0), sizeof(log_header), &log_header)) {
9ba073
+		dev_unset_last_byte(dev);
9ba073
 		log_error("Failed to write log header to %s.", name);
9ba073
 		return 0;
9ba073
 	}
9ba073
+	dev_unset_last_byte(dev);
9ba073
 
9ba073
 	label_scan_invalidate(dev);
9ba073
 
9ba073
-- 
9ba073
1.8.3.1
9ba073