[Devel] [PATCH RHEL8 COMMIT] ploop: Introduce helpers

Konstantin Khorenko khorenko at virtuozzo.com
Mon May 24 12:57:22 MSK 2021


The commit is pushed to "branch-rh8-4.18.0-240.1.1.vz8.5.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after rh8-4.18.0-240.1.1.vz8.5.31
------>
commit 3f906317b4d6ff59a275a08dc0d23c0ee5089f9c
Author: Kirill Tkhai <ktkhai at virtuozzo.com>
Date:   Mon May 24 12:57:22 2021 +0300

    ploop: Introduce helpers
    
    ... to reduce code.
    
    Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
    
    =====================
    Patchset description:
    
    ploop: Truncate top delta from kernel
    
    and refactoring at tail.
    
    https://jira.sw.ru/browse/PSBM-129278
    
    Signed-off-by: Kirill Tkhai <ktkhai at virtuozzo.com>
---
 drivers/md/dm-ploop-bat.c    | 11 +++++------
 drivers/md/dm-ploop-cmd.c    | 15 +++++++--------
 drivers/md/dm-ploop-map.c    | 11 +++++------
 drivers/md/dm-ploop-target.c |  2 +-
 drivers/md/dm-ploop.h        |  7 +++++--
 5 files changed, 23 insertions(+), 23 deletions(-)

diff --git a/drivers/md/dm-ploop-bat.c b/drivers/md/dm-ploop-bat.c
index 10126ce12473..56242e5c1285 100644
--- a/drivers/md/dm-ploop-bat.c
+++ b/drivers/md/dm-ploop-bat.c
@@ -286,7 +286,7 @@ int ploop_setup_metadata(struct ploop *ploop, struct page *page)
 
 	/* Header and BAT-occupied clusters at start of file */
 	size = (PLOOP_MAP_OFFSET + ploop->nr_bat_entries) * sizeof(map_index_t);
-	bat_clusters = DIV_ROUND_UP(size, 1 << (cluster_log + 9));
+	bat_clusters = DIV_ROUND_UP(size, CLU_SIZE(ploop));
 
 	/* Clusters from start of file to first data block */
 	offset_clusters = le32_to_cpu(m_hdr->m_FirstBlockOffset) >> cluster_log;
@@ -322,7 +322,7 @@ static int ploop_delta_check_header(struct ploop *ploop, struct page *page,
 	cluster_log = ploop->cluster_log;
 	offset_clusters = le32_to_cpu(d_hdr->m_FirstBlockOffset) >> cluster_log;
 	bytes = (PLOOP_MAP_OFFSET + delta_nr_be) * sizeof(map_index_t);
-	bat_clusters = DIV_ROUND_UP(bytes, 1 << (cluster_log + 9));
+	bat_clusters = DIV_ROUND_UP(bytes, CLU_SIZE(ploop));
 
 	if (delta_nr_be > ploop->nr_bat_entries ||
 	    bat_clusters != offset_clusters)
@@ -501,9 +501,8 @@ static int ploop_check_delta_length(struct ploop *ploop, struct file *file,
 				    loff_t *file_size)
 {
 	loff_t loff = i_size_read(file->f_mapping->host);
-	unsigned int cluster_log = ploop->cluster_log;
 
-	if (loff & ((1 << (cluster_log + SECTOR_SHIFT)) - 1))
+	if (loff & (CLU_SIZE(ploop) - 1))
 		return -EPROTO;
 	*file_size = loff;
 	return 0;
@@ -515,10 +514,10 @@ static int ploop_check_delta_length(struct ploop *ploop, struct file *file,
  */
 int ploop_add_delta(struct ploop *ploop, u32 level, struct file *file, bool is_raw)
 {
-	u32 size_in_clus, cluster_log = ploop->cluster_log;
 	struct ploop_delta *deltas = ploop->deltas;
 	struct ploop_pvd_header *hdr = NULL;
 	loff_t file_size;
+	u32 size_in_clus;
 	int ret;
 
 	ret = ploop_check_delta_length(ploop, file, &file_size);
@@ -531,7 +530,7 @@ int ploop_add_delta(struct ploop *ploop, u32 level, struct file *file, bool is_r
 			goto out;
 		size_in_clus = le32_to_cpu(hdr->m_Size);
 	} else {
-		size_in_clus = to_sector(file_size) >> cluster_log;
+		size_in_clus = POS_TO_CLU(ploop, file_size);
 	}
 
 	ret = -EBADSLT;
diff --git a/drivers/md/dm-ploop-cmd.c b/drivers/md/dm-ploop-cmd.c
index 63cca8ab0e05..0dd68b1f4f97 100644
--- a/drivers/md/dm-ploop-cmd.c
+++ b/drivers/md/dm-ploop-cmd.c
@@ -174,7 +174,7 @@ void pio_prepare_offsets(struct ploop *ploop, struct pio *pio,
 		pio->bi_io_vec[i].bv_len = PAGE_SIZE;
 	}
 	pio->bi_iter.bi_sector = cluster << cluster_log;
-	pio->bi_iter.bi_size = 1 << (cluster_log + 9);
+	pio->bi_iter.bi_size = CLU_SIZE(ploop);
 }
 
 static void wake_completion(struct pio *pio, void *data, blk_status_t status)
@@ -331,7 +331,7 @@ static int ploop_grow_update_header(struct ploop *ploop,
 {
 	unsigned int size, first_block_off, cluster_log = ploop->cluster_log;
 	struct ploop_pvd_header *hdr;
-	u32 nr_be, offset;
+	u32 nr_be, offset, clus;
 	u64 sectors;
 	int ret;
 
@@ -342,8 +342,8 @@ static int ploop_grow_update_header(struct ploop *ploop,
 
 	size = (PLOOP_MAP_OFFSET + cmd->resize.nr_bat_entries);
 	size *= sizeof(map_index_t);
-	size = DIV_ROUND_UP(size, 1 << (cluster_log + 9));
-	first_block_off = size << cluster_log;
+	clus = DIV_ROUND_UP(size, CLU_SIZE(ploop));
+	first_block_off = clus << cluster_log;
 
 	hdr = kmap_atomic(piwb->bat_page);
 	/* TODO: head and cylinders */
@@ -435,7 +435,6 @@ static void process_resize_cmd(struct ploop *ploop, struct ploop_index_wb *piwb,
 
 struct pio *alloc_pio_with_pages(struct ploop *ploop)
 {
-	unsigned int cluster_log = ploop->cluster_log;
 	int i, nr_pages = nr_pages_in_cluster(ploop);
 	struct pio *pio;
 	u32 size;
@@ -455,7 +454,7 @@ struct pio *alloc_pio_with_pages(struct ploop *ploop)
 	}
 
 	pio->bi_vcnt = nr_pages;
-	pio->bi_iter.bi_size = 1 << (cluster_log + 9);
+	pio->bi_iter.bi_size = CLU_SIZE(ploop);
 
 	return pio;
 err:
@@ -528,7 +527,7 @@ static int ploop_resize(struct ploop *ploop, u64 new_size)
 		goto err;
 
 	size = (PLOOP_MAP_OFFSET + nr_bat_entries) * sizeof(map_index_t);
-	nr_bat_clusters = DIV_ROUND_UP(size, 1 << (cluster_log + 9));
+	nr_bat_clusters = DIV_ROUND_UP(size, CLU_SIZE(ploop));
 	hb_nr = nr_bat_clusters + nr_bat_entries;
 	size = round_up(DIV_ROUND_UP(hb_nr, 8), sizeof(unsigned long));
 
@@ -947,7 +946,7 @@ static void process_flip_upper_deltas(struct ploop *ploop, struct ploop_cmd *cmd
 	struct md_page *md;
 
 	size = (PLOOP_MAP_OFFSET + ploop->nr_bat_entries) * sizeof(map_index_t);
-        bat_clusters = DIV_ROUND_UP(size, 1 << (ploop->cluster_log + 9));
+        bat_clusters = DIV_ROUND_UP(size, CLU_SIZE(ploop));
 	hb_nr = ploop->hb_nr;
 
 	write_lock_irq(&ploop->bat_rwlock);
diff --git a/drivers/md/dm-ploop-map.c b/drivers/md/dm-ploop-map.c
index 6c0153218124..b6c134ffe503 100644
--- a/drivers/md/dm-ploop-map.c
+++ b/drivers/md/dm-ploop-map.c
@@ -114,7 +114,7 @@ static int ploop_pio_valid(struct ploop *ploop, struct pio *pio)
 	loff_t end_byte;
 
 	end_byte = to_bytes(sector) + pio->bi_iter.bi_size - 1;
-	end_cluster = to_sector(end_byte) >> ploop->cluster_log;
+	end_cluster = POS_TO_CLU(ploop, end_byte);
 
 	if (unlikely(end_cluster >= ploop->nr_bat_entries)) {
 		/*
@@ -228,7 +228,7 @@ static struct pio * split_and_chain_pio(struct ploop *ploop,
 static int split_pio_to_list(struct ploop *ploop, struct pio *pio,
 			     struct list_head *list)
 {
-	u32 clu_size = to_bytes(1 << ploop->cluster_log);
+	u32 clu_size = CLU_SIZE(ploop);
 	struct pio *split;
 
 	while (1) {
@@ -926,8 +926,7 @@ static int truncate_prealloc_safe(struct ploop_delta *delta, loff_t len, const c
 static int allocate_cluster(struct ploop *ploop, unsigned int *dst_cluster)
 {
 	struct ploop_delta *top = top_delta(ploop);
-	u32 cluster_log = ploop->cluster_log;
-	u32 clu_size = to_bytes(1 << cluster_log);
+	u32 clu_size = CLU_SIZE(ploop);
 	loff_t off, pos, end, old_size;
 	struct file *file = top->file;
 	int ret;
@@ -935,7 +934,7 @@ static int allocate_cluster(struct ploop *ploop, unsigned int *dst_cluster)
 	if (find_dst_cluster_bit(ploop, dst_cluster) < 0)
 		return -EIO;
 
-	pos = to_bytes(*dst_cluster << cluster_log);
+	pos = CLU_TO_POS(ploop, *dst_cluster);
 	end = pos + clu_size;
 	old_size = top->file_size;
 
@@ -1557,7 +1556,7 @@ void ploop_submit_index_wb_sync(struct ploop *ploop,
 	if (ret)
 		status = errno_to_blk_status(ret);
 
-	dst_cluster = ((u64)piwb->page_nr << PAGE_SHIFT) / to_bytes(1 << ploop->cluster_log);
+	dst_cluster = ((u64)piwb->page_nr << PAGE_SHIFT) / CLU_SIZE(ploop);
 	track_dst_cluster(ploop, dst_cluster);
 
 	ploop_bat_write_complete(piwb, status);
diff --git a/drivers/md/dm-ploop-target.c b/drivers/md/dm-ploop-target.c
index 2a2d36b51de9..a33147165de3 100644
--- a/drivers/md/dm-ploop-target.c
+++ b/drivers/md/dm-ploop-target.c
@@ -373,7 +373,7 @@ static void ploop_io_hints(struct dm_target *ti, struct queue_limits *limits)
 
 	limits->max_discard_sectors = 1 << cluster_log;
 	limits->max_hw_discard_sectors = 1 << cluster_log;
-	limits->discard_granularity = 1 << (cluster_log + SECTOR_SHIFT);
+	limits->discard_granularity = CLU_SIZE(ploop);
 	limits->discard_alignment = 0;
 	limits->discard_misaligned = 0;
 }
diff --git a/drivers/md/dm-ploop.h b/drivers/md/dm-ploop.h
index 5e73bed446a5..a53abd0a041e 100644
--- a/drivers/md/dm-ploop.h
+++ b/drivers/md/dm-ploop.h
@@ -104,6 +104,9 @@ struct ploop_cmd {
 #define PLOOP_BIOS_HTABLE_BITS	8
 #define PLOOP_BIOS_HTABLE_SIZE	(1 << PLOOP_BIOS_HTABLE_BITS)
 #define CLU_OFF(ploop, pos) (pos & (to_bytes(1 << ploop->cluster_log) - 1))
+#define CLU_TO_POS(ploop, clu) to_bytes((loff_t)clu << ploop->cluster_log)
+#define POS_TO_CLU(ploop, pos) (to_sector(pos) >> ploop->cluster_log)
+#define CLU_SIZE(ploop) to_bytes((u32)1 << ploop->cluster_log)
 
 enum piwb_type {
 	PIWB_TYPE_ALLOC = 0,	/* Allocation of new clusters */
@@ -323,7 +326,7 @@ static inline bool whole_cluster(struct ploop *ploop, struct pio *pio)
 {
 	sector_t end_sector = bvec_iter_end_sector(pio->bi_iter);
 
-	if (pio->bi_iter.bi_size != to_bytes(1 << ploop->cluster_log))
+	if (pio->bi_iter.bi_size != CLU_SIZE(ploop))
 		return false;
 	/*
 	 * There is no sacral meaning in bio_end_sector(),
@@ -380,7 +383,7 @@ static inline unsigned int ploop_nr_bat_clusters(struct ploop *ploop,
 	unsigned long size, bat_clusters;
 
 	size = (PLOOP_MAP_OFFSET + nr_bat_entries) * sizeof(map_index_t);
-	bat_clusters = DIV_ROUND_UP(size, 1 << (ploop->cluster_log + 9));
+	bat_clusters = DIV_ROUND_UP(size, CLU_SIZE(ploop));
 
 	return bat_clusters;
 }


More information about the Devel mailing list