From 5bf4efbab5f2c51f0ca1763f47a9aa227dfb915c Mon Sep 17 00:00:00 2001 From: David Teigland Date: Wed, 3 Jul 2024 12:58:18 -0500 Subject: [PATCH] bcache: move from global variable to cmd struct --- lib/commands/toolcontext.h | 11 ++++++ lib/label/label.c | 77 ++++++++++++++++++-------------------- lib/label/label.h | 2 +- tools/lvmdevices.c | 6 +-- tools/pvck.c | 2 +- tools/pvscan.c | 2 +- tools/vgimportclone.c | 2 +- 7 files changed, 54 insertions(+), 48 deletions(-) diff --git a/lib/commands/toolcontext.h b/lib/commands/toolcontext.h index be637b82c..6558fd561 100644 --- a/lib/commands/toolcontext.h +++ b/lib/commands/toolcontext.h @@ -236,6 +236,17 @@ struct cmd_context { const char *devicesfile; /* from --devicesfile option */ struct dm_list deviceslist; /* from --devices option, struct dm_str_list */ + /* + * Cache of blocks read from devices, generally blocks containing + * headers and metadata from PVs. (It functions more as an i/o layer + * rather than a cache, the caching occurs when blocks are reused + * between label_scan and vg_read.) + * (FIXME: in some cases blocks from LVs are contained here, e.g. for + * zeroing, but that's not the intended use of bcache, and those cases + * should be changed to perform i/o to LVs separately.) + */ + struct bcache *dev_blocks; + /* * LV dev index. */ diff --git a/lib/label/label.c b/lib/label/label.c index 93a0b577d..9906ca82f 100644 --- a/lib/label/label.c +++ b/lib/label/label.c @@ -251,11 +251,6 @@ struct label *label_create(struct labeller *labeller) return label; } - -/* global variable for accessing the bcache populated by label scan */ -/* FIXME/TODO convert to cmd_context */ -static struct bcache *scan_bcache; - #define BCACHE_BLOCK_SIZE_IN_SECTORS 256 /* 256*512 = 128K */ static bool _in_bcache(struct device *dev) @@ -641,7 +636,7 @@ static int _scan_list(struct cmd_context *cmd, struct dev_filter *f, log_debug_devs("Scanning %d devices for VG info", dm_list_size(devs)); scan_more: - rem_prefetches = bcache_max_prefetches(scan_bcache); + rem_prefetches = bcache_max_prefetches(cmd->dev_blocks); submit_count = 0; dm_list_iterate_items_safe(devl, devl2, devs) { @@ -668,7 +663,7 @@ static int _scan_list(struct cmd_context *cmd, struct dev_filter *f, } } - bcache_prefetch(scan_bcache, devl->dev->bcache_di, 0); + bcache_prefetch(cmd->dev_blocks, devl->dev->bcache_di, 0); rem_prefetches--; submit_count++; @@ -683,7 +678,7 @@ static int _scan_list(struct cmd_context *cmd, struct dev_filter *f, bb = NULL; is_lvm_device = 0; - if (!bcache_get(scan_bcache, devl->dev->bcache_di, 0, 0, &bb)) { + if (!bcache_get(cmd->dev_blocks, devl->dev->bcache_di, 0, 0, &bb)) { log_debug_devs("Scan failed to read %s.", dev_name(devl->dev)); scan_read_errors++; scan_failed_count++; @@ -734,7 +729,7 @@ static int _scan_list(struct cmd_context *cmd, struct dev_filter *f, * e.g. to pvcreate them. */ if (!is_lvm_device && !want_other_devs) { - _invalidate_di(scan_bcache, devl->dev->bcache_di); + _invalidate_di(cmd->dev_blocks, devl->dev->bcache_di); _scan_dev_close(devl->dev); } @@ -769,14 +764,14 @@ static int _scan_list(struct cmd_context *cmd, struct dev_filter *f, #define MIN_BCACHE_BLOCKS 32 /* 4MB (32 * 128KB) */ #define MAX_BCACHE_BLOCKS 4096 /* 512MB (4096 * 128KB) */ -int label_scan_setup_bcache(void) +int label_scan_setup_bcache(struct cmd_context *cmd) { struct io_engine *ioe = NULL; int iomem_kb = io_memory_size(); int block_size_kb = (BCACHE_BLOCK_SIZE_IN_SECTORS * 512) / 1024; int cache_blocks; - if (scan_bcache) + if (cmd->dev_blocks) return 1; cache_blocks = iomem_kb / block_size_kb; @@ -803,7 +798,7 @@ int label_scan_setup_bcache(void) } } - if (!(scan_bcache = bcache_create(BCACHE_BLOCK_SIZE_IN_SECTORS, cache_blocks, ioe))) { + if (!(cmd->dev_blocks = bcache_create(BCACHE_BLOCK_SIZE_IN_SECTORS, cache_blocks, ioe))) { log_error("Failed to set up io layer with %d blocks.", cache_blocks); return 0; } @@ -890,7 +885,7 @@ int label_scan_for_pvid(struct cmd_context *cmd, char *pvid, struct device **dev return 0; } - if (!label_scan_setup_bcache()) + if (!label_scan_setup_bcache(cmd)) return_0; /* @@ -1128,7 +1123,7 @@ int label_scan_vg_online(struct cmd_context *cmd, const char *vgname, log_debug("label_scan_vg_online: read and filter devs"); - label_scan_setup_bcache(); + label_scan_setup_bcache(cmd); dm_list_iterate_items_safe(devl, devl2, &devs) { struct dev_use *du; @@ -1254,7 +1249,7 @@ int label_scan(struct cmd_context *cmd) dm_list_init(&scan_devs); dm_list_init(&hints_list); - if (!label_scan_setup_bcache()) + if (!label_scan_setup_bcache(cmd)) return_0; /* @@ -1553,7 +1548,7 @@ int label_read_pvid(struct device *dev, int *has_pvid) */ int label_scan_devs_cached(struct cmd_context *cmd, struct dev_filter *f, struct dm_list *devs) { - if (!scan_bcache) + if (!cmd->dev_blocks) return 0; _scan_list(cmd, f, devs, 0, NULL); @@ -1573,12 +1568,12 @@ int label_scan_devs(struct cmd_context *cmd, struct dev_filter *f, struct dm_lis { struct device_list *devl; - if (!label_scan_setup_bcache()) + if (!label_scan_setup_bcache(cmd)) return_0; dm_list_iterate_items(devl, devs) { if (_in_bcache(devl->dev)) - _invalidate_di(scan_bcache, devl->dev->bcache_di); + _invalidate_di(cmd->dev_blocks, devl->dev->bcache_di); } _scan_list(cmd, f, devs, 0, NULL); @@ -1590,12 +1585,12 @@ int label_scan_devs_rw(struct cmd_context *cmd, struct dev_filter *f, struct dm_ { struct device_list *devl; - if (!label_scan_setup_bcache()) + if (!label_scan_setup_bcache(cmd)) return_0; dm_list_iterate_items(devl, devs) { if (_in_bcache(devl->dev)) - _invalidate_di(scan_bcache, devl->dev->bcache_di); + _invalidate_di(cmd->dev_blocks, devl->dev->bcache_di); devl->dev->flags |= DEV_BCACHE_WRITE; } @@ -1629,7 +1624,7 @@ int label_scan_devs_excl(struct cmd_context *cmd, struct dev_filter *f, struct d void label_scan_invalidate(struct device *dev) { if (_in_bcache(dev)) { - _invalidate_di(scan_bcache, dev->bcache_di); + _invalidate_di(dev->cmd->dev_blocks, dev->bcache_di); _scan_dev_close(dev); } } @@ -1706,13 +1701,13 @@ void label_scan_drop(struct cmd_context *cmd) void label_scan_destroy(struct cmd_context *cmd) { - if (!scan_bcache) + if (!cmd->dev_blocks) return; label_scan_drop(cmd); - bcache_destroy(scan_bcache); - scan_bcache = NULL; + bcache_destroy(cmd->dev_blocks); + cmd->dev_blocks = NULL; } /* @@ -1764,7 +1759,7 @@ int label_scan_open_excl(struct device *dev) { if (_in_bcache(dev) && !(dev->flags & DEV_BCACHE_EXCL)) { log_debug("close and reopen excl %s", dev_name(dev)); - _invalidate_di(scan_bcache, dev->bcache_di); + _invalidate_di(dev->cmd->dev_blocks, dev->bcache_di); _scan_dev_close(dev); } dev->flags |= DEV_BCACHE_EXCL; @@ -1776,7 +1771,7 @@ int label_scan_open_rw(struct device *dev) { if (_in_bcache(dev) && !(dev->flags & DEV_BCACHE_WRITE)) { log_debug("close and reopen rw %s", dev_name(dev)); - _invalidate_di(scan_bcache, dev->bcache_di); + _invalidate_di(dev->cmd->dev_blocks, dev->bcache_di); _scan_dev_close(dev); } dev->flags |= DEV_BCACHE_WRITE; @@ -1863,7 +1858,7 @@ int label_scan_reopen_rw(struct device *dev) bool dev_read_bytes(struct device *dev, uint64_t start, size_t len, void *data) { - if (!scan_bcache) { + if (!dev->cmd->dev_blocks) { /* Should not happen */ log_error("dev_read bcache not set up %s", dev_name(dev)); return false; @@ -1878,7 +1873,7 @@ bool dev_read_bytes(struct device *dev, uint64_t start, size_t len, void *data) } } - if (!bcache_read_bytes(scan_bcache, dev->bcache_di, start, len, data)) { + if (!bcache_read_bytes(dev->cmd->dev_blocks, dev->bcache_di, start, len, data)) { log_error("Error reading device %s at %llu length %u.", dev_name(dev), (unsigned long long)start, (uint32_t)len); label_scan_invalidate(dev); @@ -1893,7 +1888,7 @@ bool dev_write_bytes(struct device *dev, uint64_t start, size_t len, void *data) if (test_mode()) return true; - if (!scan_bcache) { + if (!dev->cmd->dev_blocks) { /* Should not happen */ log_error("dev_write bcache not set up %s", dev_name(dev)); return false; @@ -1902,7 +1897,7 @@ bool dev_write_bytes(struct device *dev, uint64_t start, size_t len, void *data) if (_in_bcache(dev) && !(dev->flags & DEV_BCACHE_WRITE)) { /* FIXME: avoid tossing out bcache blocks just to replace fd. */ log_debug("close and reopen to write %s", dev_name(dev)); - _invalidate_di(scan_bcache, dev->bcache_di); + _invalidate_di(dev->cmd->dev_blocks, dev->bcache_di); _scan_dev_close(dev); dev->flags |= DEV_BCACHE_WRITE; @@ -1919,7 +1914,7 @@ bool dev_write_bytes(struct device *dev, uint64_t start, size_t len, void *data) } } - if (!bcache_write_bytes(scan_bcache, dev->bcache_di, start, len, data)) { + if (!bcache_write_bytes(dev->cmd->dev_blocks, dev->bcache_di, start, len, data)) { log_error("Error writing device %s at %llu length %u.", dev_name(dev), (unsigned long long)start, (uint32_t)len); dev_unset_last_byte(dev); @@ -1927,7 +1922,7 @@ bool dev_write_bytes(struct device *dev, uint64_t start, size_t len, void *data) return false; } - if (!bcache_flush(scan_bcache)) { + if (!bcache_flush(dev->cmd->dev_blocks)) { log_error("Error writing device %s at %llu length %u.", dev_name(dev), (unsigned long long)start, (uint32_t)len); dev_unset_last_byte(dev); @@ -1939,12 +1934,12 @@ bool dev_write_bytes(struct device *dev, uint64_t start, size_t len, void *data) bool dev_invalidate_bytes(struct device *dev, uint64_t start, size_t len) { - return bcache_invalidate_bytes(scan_bcache, dev->bcache_di, start, len); + return bcache_invalidate_bytes(dev->cmd->dev_blocks, dev->bcache_di, start, len); } void dev_invalidate(struct device *dev) { - bcache_invalidate_di(scan_bcache, dev->bcache_di); + bcache_invalidate_di(dev->cmd->dev_blocks, dev->bcache_di); } bool dev_write_zeros(struct device *dev, uint64_t start, size_t len) @@ -1959,14 +1954,14 @@ bool dev_set_bytes(struct device *dev, uint64_t start, size_t len, uint8_t val) if (test_mode()) return true; - if (!scan_bcache) { + if (!dev->cmd->dev_blocks) { log_error("dev_set_bytes bcache not set up %s", dev_name(dev)); return false; } if (_in_bcache(dev) && !(dev->flags & DEV_BCACHE_WRITE)) { log_debug("close and reopen to write %s", dev_name(dev)); - _invalidate_di(scan_bcache, dev->bcache_di); + _invalidate_di(dev->cmd->dev_blocks, dev->bcache_di); _scan_dev_close(dev); /* goes to label_scan_open() since bcache_di < 0 */ } @@ -1984,9 +1979,9 @@ bool dev_set_bytes(struct device *dev, uint64_t start, size_t len, uint8_t val) dev_set_last_byte(dev, start + len); if (!val) - rv = bcache_zero_bytes(scan_bcache, dev->bcache_di, start, len); + rv = bcache_zero_bytes(dev->cmd->dev_blocks, dev->bcache_di, start, len); else - rv = bcache_set_bytes(scan_bcache, dev->bcache_di, start, len, val); + rv = bcache_set_bytes(dev->cmd->dev_blocks, dev->bcache_di, start, len, val); if (!rv) { log_error("Error writing device value %s at %llu length %u.", @@ -1994,7 +1989,7 @@ bool dev_set_bytes(struct device *dev, uint64_t start, size_t len, uint8_t val) goto fail; } - if (!bcache_flush(scan_bcache)) { + if (!bcache_flush(dev->cmd->dev_blocks)) { log_error("Error writing device %s at %llu length %u.", dev_name(dev), (unsigned long long)start, (uint32_t)len); goto fail; @@ -2038,10 +2033,10 @@ void dev_set_last_byte(struct device *dev, uint64_t offset) bs = 512; } - bcache_set_last_byte(scan_bcache, dev->bcache_di, offset, bs); + bcache_set_last_byte(dev->cmd->dev_blocks, dev->bcache_di, offset, bs); } void dev_unset_last_byte(struct device *dev) { - bcache_unset_last_byte(scan_bcache, dev->bcache_di); + bcache_unset_last_byte(dev->cmd->dev_blocks, dev->bcache_di); } diff --git a/lib/label/label.h b/lib/label/label.h index 4c224dc50..62277ee72 100644 --- a/lib/label/label.h +++ b/lib/label/label.h @@ -111,7 +111,7 @@ void label_scan_invalidate_lv(struct cmd_context *cmd, struct logical_volume *lv void label_scan_invalidate_lvs(struct cmd_context *cmd, struct dm_list *lvs); void label_scan_drop(struct cmd_context *cmd); void label_scan_destroy(struct cmd_context *cmd); -int label_scan_setup_bcache(void); +int label_scan_setup_bcache(struct cmd_context *cmd); int label_scan_open(struct device *dev); int label_scan_open_excl(struct device *dev); int label_scan_open_rw(struct device *dev); diff --git a/tools/lvmdevices.c b/tools/lvmdevices.c index a97bd69e6..ed734bf81 100644 --- a/tools/lvmdevices.c +++ b/tools/lvmdevices.c @@ -628,7 +628,7 @@ int lvmdevices(struct cmd_context *cmd, int argc, char **argv) unlink_searched_devnames(cmd); - label_scan_setup_bcache(); + label_scan_setup_bcache(cmd); dm_list_iterate_items(du, &cmd->use_devices) { if (!du->dev) @@ -806,7 +806,7 @@ int lvmdevices(struct cmd_context *cmd, int argc, char **argv) * reads pvid from dev header, sets dev->pvid. * (it's ok if the device is not a PV and has no PVID) */ - label_scan_setup_bcache(); + label_scan_setup_bcache(cmd); if (!label_read_pvid(dev, NULL)) { log_error("Failed to read %s.", devname); goto bad; @@ -845,7 +845,7 @@ int lvmdevices(struct cmd_context *cmd, int argc, char **argv) char pvid[ID_LEN+1] = { 0 }; const char *pvid_arg; - label_scan_setup_bcache(); + label_scan_setup_bcache(cmd); /* * Iterate through all devs on the system, reading the diff --git a/tools/pvck.c b/tools/pvck.c index 08821e4d5..94f038d12 100644 --- a/tools/pvck.c +++ b/tools/pvck.c @@ -3073,7 +3073,7 @@ static int _pvck_mf(struct metadata_file *mf, struct cmd_context *cmd, int argc, return_ECMD_FAILED; } - label_scan_setup_bcache(); + label_scan_setup_bcache(cmd); if (dev) { char buf[4096]; diff --git a/tools/pvscan.c b/tools/pvscan.c index e4f8c4a1b..640b82ca8 100644 --- a/tools/pvscan.c +++ b/tools/pvscan.c @@ -1571,7 +1571,7 @@ static int _pvscan_cache_args(struct cmd_context *cmd, int argc, char **argv, log_debug("pvscan_cache_args: read and filter devs"); - label_scan_setup_bcache(); + label_scan_setup_bcache(cmd); dm_list_iterate_items_safe(devl, devl2, &pvscan_devs) { int has_pvid; diff --git a/tools/vgimportclone.c b/tools/vgimportclone.c index 0d07ce6bc..06f7e5dcf 100644 --- a/tools/vgimportclone.c +++ b/tools/vgimportclone.c @@ -287,7 +287,7 @@ int vgimportclone(struct cmd_context *cmd, int argc, char **argv) */ log_debug("scan new devs"); - label_scan_setup_bcache(); + label_scan_setup_bcache(cmd); cmd->filter_nodata_only = 0;