Skip to contentSkip to conversation

master - bcache: do all writes through bcache

PATCH David Teigland 1 participant 1 message Apr 23, 2018

lore
Share & tools
lore
#0 lore raw
Gitweb:        https://sourceware.org/git/?p=lvm2.git;a=commitdiff;h=80654920468841251a7f940e410ca61de8383b41
Commit:        80654920468841251a7f940e410ca61de8383b41
Parent:        8b26a007b1b545333b40675b1c425b1ef5e0b653
Author:        David Teigland <[email protected]>
AuthorDate:    Mon Feb 19 15:40:44 2018 -0600
Committer:     David Teigland <[email protected]>
CommitterDate: Fri Apr 20 11:22:47 2018 -0500

bcache: do all writes through bcache

---
 lib/device/bcache.c           |   86 ++++++++++++++++++++++++++++++-
 lib/device/bcache.h           |    4 +-
 lib/device/dev-type.c         |    6 ++-
 lib/format_text/archiver.c    |   10 +---
 lib/format_text/format-text.c |   94 +++++++++++++---------------------
 lib/label/label.c             |  112 +++++++++++++++++++----------------------
 lib/label/label.h             |    1 +
 lib/metadata/metadata.c       |   19 +++----
 tools/toollib.c               |   17 ++----
 9 files changed, 193 insertions(+), 156 deletions(-)
lib/device/bcache.c +84 -2
index 499c6af..272de60 100644
@@ -897,7 +897,7 @@ bool bcache_get(struct bcache *cache, int fd, block_address index,
 	return false;
 }
 
-void bcache_put(struct block *b)
+static void _put_ref(struct block *b)
 {
 	if (!b->ref_count) {
 		log_warn("ref count on bcache block already zero");
@@ -907,6 +907,11 @@ void bcache_put(struct block *b)
 	b->ref_count--;
 	if (!b->ref_count)
 		b->cache->nr_locked--;
+}
+
+void bcache_put(struct block *b)
+{
+	_put_ref(b);
 
 	if (_test_flags(b, BF_DIRTY))
 		_preemptive_writeback(b->cache);
@@ -925,7 +930,7 @@ bool bcache_flush(struct bcache *cache)
 			// The superblock may well be still locked.
 			continue;
 		}
-
+		
 		_issue_write(b);
 	}
 
@@ -1058,5 +1063,82 @@ bool bcache_read_bytes(struct bcache *cache, int fd, off_t start, size_t len, vo
 	return errors ? false : true;
 }
 
+bool bcache_write_bytes(struct bcache *cache, int fd, off_t start, size_t len, void *data)
+{
+	struct block *b;
+	block_address bb, be, i;
+	unsigned char *udata = data;
+	off_t block_size = cache->block_sectors << SECTOR_SHIFT;
+	int errors = 0;
+
+	byte_range_to_block_range(cache, start, len, &bb, &be);
+	for (i = bb; i < be; i++)
+		bcache_prefetch(cache, fd, i);
+
+	for (i = bb; i < be; i++) {
+		if (!bcache_get(cache, fd, i, 0, &b)) {
+			errors++;
+			break;
+		}
+
+		if (i == bb) {
+			off_t block_offset = start % block_size;
+			size_t blen = _min(block_size - block_offset, len);
+			memcpy(((unsigned char *) b->data) + block_offset, udata, blen);
+			len -= blen;
+			udata += blen;
+		} else {
+			size_t blen = _min(block_size, len);
+			memcpy(b->data, udata, blen);
+			len -= blen;
+			udata += blen;
+		}
+
+		_set_flags(b, BF_DIRTY);
+		_unlink_block(b);
+		_link_block(b);
+		_put_ref(b);
+	}
+
+	if (!bcache_flush(cache))
+		errors++;
+
+	return errors ? false : true;
+}
+
+#define ZERO_BUF_LEN 4096
+
+bool bcache_write_zeros(struct bcache *cache, int fd, off_t start, size_t len)
+{
+	char zerobuf[ZERO_BUF_LEN];
+	size_t plen;
+	size_t poff;
+
+	memset(zerobuf, 0, sizeof(zerobuf));
+
+	if (len <= ZERO_BUF_LEN)
+		return bcache_write_bytes(cache, fd, start, len, &zerobuf);
+
+	poff = 0;
+	plen = ZERO_BUF_LEN;
+
+	while (1) {
+		if (!bcache_write_bytes(cache, fd, start + poff, plen, &zerobuf))
+			return false;
+
+		poff += plen;
+		len -= plen;
+
+		if (!len)
+			break;
+
+		if (len < ZERO_BUF_LEN)
+			plen = len;
+	}
+
+	return true;
+}
+
+
 //----------------------------------------------------------------
 
lib/device/bcache.h +3 -1
index e5d98e8..d5f6d0a 100644
@@ -148,9 +148,11 @@ void bcache_invalidate_fd(struct bcache *cache, int fd);
 void bcache_prefetch_bytes(struct bcache *cache, int fd, off_t start, size_t len);
 
 /*
- * Reads the bytes.
+ * Reads and writes the bytes.  Returns false if errors occur.
  */
 bool bcache_read_bytes(struct bcache *cache, int fd, off_t start, size_t len, void *data);
+bool bcache_write_bytes(struct bcache *cache, int fd, off_t start, size_t len, void *data);
+bool bcache_write_zeros(struct bcache *cache, int fd, off_t start, size_t len);
 
 /*----------------------------------------------------------------*/
 
lib/device/dev-type.c +4 -2
index 9608146..e69f7ca 100644
@@ -17,6 +17,8 @@
 #include "xlate.h"
 #include "config.h"
 #include "metadata.h"
+#include "bcache.h"
+#include "label.h"
 
 #include <libgen.h>
 #include <ctype.h>
@@ -675,7 +677,7 @@ static int _blkid_wipe(blkid_probe probe, struct device *dev, const char *name,
 	} else
 		log_verbose(_msg_wiping, type, name);
 
-	if (!dev_set(dev, offset_value, len, DEV_IO_SIGNATURES, 0)) {
+	if (!bcache_write_zeros(scan_bcache, dev->bcache_fd, offset_value, len)) {
 		log_error("Failed to wipe %s signature on %s.", type, name);
 		return 0;
 	}
@@ -772,7 +774,7 @@ static int _wipe_signature(struct device *dev, const char *type, const char *nam
 	}
 
 	log_print_unless_silent("Wiping %s on %s.", type, name);
-	if (!dev_set(dev, offset_found, wipe_len, DEV_IO_SIGNATURES, 0)) {
+	if (!bcache_write_zeros(scan_bcache, dev->bcache_fd, offset_found, wipe_len)) {
 		log_error("Failed to wipe %s on %s.", type, name);
 		return 0;
 	}
lib/format_text/archiver.c +1 -9
index 81b5da9..43ae4eb 100644
@@ -488,19 +488,11 @@ int backup_restore_vg(struct cmd_context *cmd, struct volume_group *vg,
 			}
 
 			log_verbose("Zeroing start of device %s", pv_name);
-			if (!dev_open_quiet(dev)) {
-				log_error("%s not opened: device not zeroed", pv_name);
-				return 0;
-			}
 
-			if (!dev_set(dev, UINT64_C(0), (size_t) 2048, DEV_IO_LABEL, 0)) {
+			if (!bcache_write_zeros(scan_bcache, dev->bcache_fd, 0, 2048)) {
 				log_error("%s not wiped: aborting", pv_name);
-				if (!dev_close(dev))
-					stack;
 				return 0;
 			}
-			if (!dev_close(dev))
-				stack;
 		}
 	}
 
lib/format_text/format-text.c +35 -59

176 lines not on this page · raw message

lib/label/label.c +51 -61

186 lines not on this page · raw message

lib/label/label.h +1 -0
index bf6e926..92f3e7b 100644
@@ -112,5 +112,6 @@ int label_read(struct device *dev, struct label **labelp, uint64_t unused_sector
 int label_read_sector(struct device *dev, struct label **labelp, uint64_t scan_sector);
 void label_scan_confirm(struct device *dev);
 int label_scan_setup_bcache(void);
+int label_scan_open(struct device *dev);
 
 #endif
lib/metadata/metadata.c +8 -11
index b588a04..4cad2a1 100644
@@ -692,6 +692,7 @@ int check_pv_dev_sizes(struct volume_group *vg)
  * . lvmcache_get_vgids()
  * . lvmcache_get_vgnames()
  * . the vg->pvs_to_write list and pv_to_write struct
+ * . _pvcreate_write()
  */
 
 int vg_extend_each_pv(struct volume_group *vg, struct pvcreate_params *pp)
@@ -1414,28 +1415,24 @@ static int _pvcreate_write(struct cmd_context *cmd, struct pv_to_write *pvw)
 	struct device *dev = pv->dev;
 	const char *pv_name = dev_name(dev);
 
+	if (!label_scan_open(dev)) {
+		log_error("%s not opened: device not written", pv_name);
+		return 0;
+	}
+
 	if (pvw->new_pv) {
 		/* Wipe existing label first */
-		if (!label_remove(pv_dev(pv))) {
+		if (!label_remove(dev)) {
 			log_error("Failed to wipe existing label on %s", pv_name);
 			return 0;
 		}
 
 		if (pvw->pp->zero) {
 			log_verbose("Zeroing start of device %s", pv_name);
-			if (!dev_open_quiet(dev)) {
-				log_error("%s not opened: device not zeroed", pv_name);
-				return 0;
-			}
-
-			if (!dev_set(dev, UINT64_C(0), (size_t) 2048, DEV_IO_LABEL, 0)) {
+			if (!bcache_write_zeros(scan_bcache, dev->bcache_fd, 0, 2048)) {
 				log_error("%s not wiped: aborting", pv_name);
-				if (!dev_close(dev))
-					stack;
 				return 0;
 			}
-			if (!dev_close(dev))
-				stack;
 		}
 	}
 
tools/toollib.c +6 -11
index 623bfbb..b0c0e8e 100644
@@ -5728,6 +5728,8 @@ do_command:
 	 * Wipe signatures on devices being created.
 	 */
 	dm_list_iterate_items_safe(pd, pd2, &pp->arg_create) {
+		label_scan_open(pd->dev);
+
 		log_verbose("Wiping signatures on new PV %s.", pd->name);
 
 		if (!wipe_known_signatures(cmd, pd->dev, pd->name, TYPE_LVM1_MEMBER | TYPE_LVM2_MEMBER,
@@ -5805,6 +5807,8 @@ do_command:
 
 		pv_name = pd->name;
 
+		label_scan_open(pd->dev);
+
 		log_debug("Creating a new PV on %s.", pv_name);
 
 		if (!(pv = pv_create(cmd, pd->dev, &pp->pva))) {
@@ -5816,6 +5820,7 @@ do_command:
 		log_verbose("Set up physical volume for \"%s\" with %" PRIu64
 			    " available sectors.", pv_name, pv_size(pv));
 
+
 		if (!label_remove(pv->dev)) {
 			log_error("Failed to wipe existing label on %s.", pv_name);
 			dm_list_move(&pp->arg_fail, &pd->list);
@@ -5825,21 +5830,11 @@ do_command:
 		if (pp->zero) {
 			log_verbose("Zeroing start of device %s.", pv_name);
 
-			if (!dev_open_quiet(pv->dev)) {
-				log_error("%s not opened: device not zeroed.", pv_name);
-				dm_list_move(&pp->arg_fail, &pd->list);
-				continue;
-			}
-
-			if (!dev_set(pv->dev, UINT64_C(0), (size_t) 2048, DEV_IO_LABEL, 0)) {
+			if (!bcache_write_zeros(scan_bcache, pv->dev->bcache_fd, 0, 2048)) {
 				log_error("%s not wiped: aborting.", pv_name);
-				if (!dev_close(pv->dev))
-					stack;
 				dm_list_move(&pp->arg_fail, &pd->list);
 				continue;
 			}
-			if (!dev_close(pv->dev))
-				stack;
 		}
 
 		log_verbose("Writing physical volume data to disk \"%s\".", pv_name);