Skip to contentSkip to conversation

md: sector_t conversions

PATCH In review Andre Noll 2 participants 12 messages Jul 10, 2008 – Jul 21, 2008 MD RAID

lore
Share & tools
lore

Andre Noll

author
#0 lore raw

cover md: sector_t conversions

Hi

here is the first batch of patches that aim to change the internal representations of sizes of raid devices from 1K blocks to 512 byte sectors.

Patches 1-3 change the internal functions update_size(), calc_dev_size() and calc_dev_sboffset() so that they take/return a sector count instead of a block count. Patch 4 changes the semantics of rdev->sb_offset so that its value now represents the number of the start sector instead of the start block. Patch 5 removes some macros that have become unused due to this conversion.

These changes are all simple and straight-forward, but it's easy to make mistakes because there's no help from the compiler. So please review carefully.

 drivers/md/bitmap.c       |   10 ++--
 drivers/md/md.c           |  111 +++++++++++++++++++++------------------------
 include/linux/raid/md_k.h |    2 +-
 include/linux/raid/md_p.h |    3 -
 4 files changed, 58 insertions(+), 68 deletions(-)

Regards
Andre

author
#1 lore raw

PATCH 1/5 md: Make update_size() take the number of sectors.

Changing the internal representations of sizes of raid devices from 1K blocks to sector counts (512B units) is desirable because it allows to get rid of many divisions/multiplications and unnecessary casts that are present in the current code.

This patch is a first step in this direction. It replaces the old 1K-based "size" argument of update_size() by "num_sectors" and fixes up its two callers.

---
 drivers/md/md.c |   36 ++++++++++++++++++------------------
 1 files changed, 18 insertions(+), 18 deletions(-)
drivers/md/md.c +18 -18
index 2580ac1..2ab13a3 100644
@@ -2785,7 +2785,7 @@ size_show(mddev_t *mddev, char *page)
 	return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
 }
 
-static int update_size(mddev_t *mddev, unsigned long size);
+static int update_size(mddev_t *mddev, sector_t num_sectors);
 
 static ssize_t
 size_store(mddev_t *mddev, const char *buf, size_t len)
@@ -2802,7 +2802,7 @@ size_store(mddev_t *mddev, const char *buf, size_t len)
 		return -EINVAL;
 
 	if (mddev->pers) {
-		err = update_size(mddev, size);
+		err = update_size(mddev, size * 2);
 		md_update_sb(mddev, 1);
 	} else {
 		if (mddev->size == 0 ||
@@ -4476,24 +4476,24 @@ static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
 	return 0;
 }
 
-static int update_size(mddev_t *mddev, unsigned long size)
+static int update_size(mddev_t *mddev, sector_t num_sectors)
 {
 	mdk_rdev_t * rdev;
 	int rv;
 	struct list_head *tmp;
-	int fit = (size == 0);
+	int fit = (num_sectors == 0);
 
 	if (mddev->pers->resize == NULL)
 		return -EINVAL;
-	/* The "size" is the amount of each device that is used.
-	 * This can only make sense for arrays with redundancy.
-	 * linear and raid0 always use whatever space is available
-	 * We can only consider changing the size if no resync
-	 * or reconstruction is happening, and if the new size
-	 * is acceptable. It must fit before the sb_offset or,
-	 * if that is <data_offset, it must fit before the
-	 * size of each device.
-	 * If size is zero, we find the largest size that fits.
+	/* The "num_sectors" is the number of sectors of each device that
+	 * is used.  This can only make sense for arrays with redundancy.
+	 * linear and raid0 always use whatever space is available. We can only
+	 * consider changing this number if no resync or reconstruction is
+	 * happening, and if the new size is acceptable. It must fit before the
+	 * sb_offset or, if that is <data_offset, it must fit before the size
+	 * of each device.  If num_sectors is zero, we find the largest size
+	 * that fits.
+
 	 */
 	if (mddev->sync_thread)
 		return -EBUSY;
@@ -4501,12 +4501,12 @@ static int update_size(mddev_t *mddev, unsigned long size)
 		sector_t avail;
 		avail = rdev->size * 2;
 
-		if (fit && (size == 0 || size > avail/2))
-			size = avail/2;
-		if (avail < ((sector_t)size << 1))
+		if (fit && (num_sectors == 0 || num_sectors > avail))
+			num_sectors = avail;
+		if (avail < num_sectors)
 			return -ENOSPC;
 	}
-	rv = mddev->pers->resize(mddev, (sector_t)size *2);
+	rv = mddev->pers->resize(mddev, num_sectors);
 	if (!rv) {
 		struct block_device *bdev;
 
@@ -4588,7 +4588,7 @@ static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
 			return mddev->pers->reconfig(mddev, info->layout, -1);
 	}
 	if (info->size >= 0 && mddev->size != info->size)
-		rv = update_size(mddev, info->size);
+		rv = update_size(mddev, info->size * 2);
 
 	if (mddev->raid_disks    != info->raid_disks)
 		rv = update_raid_disks(mddev, info->raid_disks);
Signature · 1 line
1.5.3.8
#2 lore raw

Re: [PATCH 1/5] md: Make update_size() take the number of sectors.

drivers/md/md.c static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) · line 4594

Quoted 9 lines · On Thursday July 10, [email protected] wrote:
drivers/md/md.c +1 -1
@@ -4588,7 +4588,7 @@ static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
 			return mddev->pers->reconfig(mddev, info->layout, -1);
 	}
 	if (info->size >= 0 && mddev->size != info->size)
-		rv = update_size(mddev, info->size);
+		rv = update_size(mddev, info->size * 2);
 
 	if (mddev->raid_disks    != info->raid_disks)
 		rv = update_raid_disks(mddev, info->raid_disks);

I've made this

		rv = update_size(mddev, (sector_t)info->size * 2);

as info->size is an int and without the cast, a size of 2TB (2^31 K) would become 0.

Otherwise, it's good. Thanks.

NeilBrown

author
#3 lore raw

PATCH 2/5 md: Replace calc_dev_size() by calc_num_sectors().

In reply to Andre Noll

Number of sectors is the preferred unit for sizes of raid devices, so change calc_dev_size() so that it returns this unit instead of the number of 1K blocks.

---
 drivers/md/md.c |   18 +++++++-----------
 1 files changed, 7 insertions(+), 11 deletions(-)
drivers/md/md.c +7 -11
index 2ab13a3..1a6001e 100644
@@ -353,15 +353,13 @@ static inline sector_t calc_dev_sboffset(struct block_device *bdev)
 	return MD_NEW_SIZE_BLOCKS(size);
 }
 
-static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
+static sector_t calc_num_sectors(mdk_rdev_t *rdev, unsigned chunk_size)
 {
-	sector_t size;
-
-	size = rdev->sb_offset;
+	sector_t num_sectors = rdev->sb_offset * 2;
 
 	if (chunk_size)
-		size &= ~((sector_t)chunk_size/1024 - 1);
-	return size;
+		num_sectors &= ~((sector_t)chunk_size/512 - 1);
+	return num_sectors;
 }
 
 static int alloc_disk_sb(mdk_rdev_t * rdev)
@@ -759,7 +757,7 @@ static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version
 		else 
 			ret = 0;
 	}
-	rdev->size = calc_dev_size(rdev, sb->chunk_size);
+	rdev->size = calc_num_sectors(rdev, sb->chunk_size) / 2;
 
 	if (rdev->size < sb->size && sb->level > 1)
 		/* "this cannot possibly happen" ... */
@@ -4215,7 +4213,7 @@ static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
 			rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
 		} else 
 			rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
-		rdev->size = calc_dev_size(rdev, mddev->chunk_size);
+		rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2;
 
 		err = bind_rdev_to_array(rdev, mddev);
 		if (err) {
@@ -4257,7 +4255,6 @@ static int hot_add_disk(mddev_t * mddev, dev_t dev)
 {
 	char b[BDEVNAME_SIZE];
 	int err;
-	unsigned int size;
 	mdk_rdev_t *rdev;
 
 	if (!mddev->pers)
@@ -4290,8 +4287,7 @@ static int hot_add_disk(mddev_t * mddev, dev_t dev)
 		rdev->sb_offset =
 			rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
 
-	size = calc_dev_size(rdev, mddev->chunk_size);
-	rdev->size = size;
+	rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2;
 
 	if (test_bit(Faulty, &rdev->flags)) {
 		printk(KERN_WARNING 
Signature · 1 line
1.5.3.8
author
#4 lore raw

PATCH 3/5 md: Make calc_dev_sboffset() return a sector count.

In reply to Andre Noll

As BLOCK_SIZE_BITS is 10 and

	MD_NEW_SIZE_SECTORS(2 * x) = 2 * NEW_SIZE_BLOCKS(x),

the return value of calc_dev_sboffset() doubles. Fix up all three callers accordingly.

---
 drivers/md/md.c |   11 ++++++-----
 1 files changed, 6 insertions(+), 5 deletions(-)
drivers/md/md.c +6 -5
index 1a6001e..6179e8f 100644
@@ -347,10 +347,11 @@ static struct mdk_personality *find_pers(int level, char *clevel)
 	return NULL;
 }
 
+/* return the offset of the super block in 512byte sectors */
 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
 {
-	sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
-	return MD_NEW_SIZE_BLOCKS(size);
+	sector_t num_sectors = bdev->bd_inode->i_size / 512;
+	return MD_NEW_SIZE_SECTORS(num_sectors);
 }
 
 static sector_t calc_num_sectors(mdk_rdev_t *rdev, unsigned chunk_size)
@@ -679,7 +680,7 @@ static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version
 	 *
 	 * It also happens to be a multiple of 4Kb.
 	 */
-	sb_offset = calc_dev_sboffset(rdev->bdev);
+	sb_offset = calc_dev_sboffset(rdev->bdev) / 2;
 	rdev->sb_offset = sb_offset;
 
 	ret = read_disk_sb(rdev, MD_SB_BYTES);
@@ -4212,7 +4213,7 @@ static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
 			printk(KERN_INFO "md: nonpersistent superblock ...\n");
 			rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
 		} else 
-			rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
+			rdev->sb_offset = calc_dev_sboffset(rdev->bdev) / 2;
 		rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2;
 
 		err = bind_rdev_to_array(rdev, mddev);
@@ -4282,7 +4283,7 @@ static int hot_add_disk(mddev_t * mddev, dev_t dev)
 	}
 
 	if (mddev->persistent)
-		rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
+		rdev->sb_offset = calc_dev_sboffset(rdev->bdev) / 2;
 	else
 		rdev->sb_offset =
 			rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
Signature · 1 line
1.5.3.8
#5 lore raw

Re: [PATCH 3/5] md: Make calc_dev_sboffset() return a sector count.

Quoted 6 lines · On Thursday July 10, [email protected] wrote:

As BLOCK_SIZE_BITS is 10 and

	MD_NEW_SIZE_SECTORS(2 * x) = 2 * NEW_SIZE_BLOCKS(x),

the return value of calc_dev_sboffset() doubles. Fix up all three callers accordingly.

Only I have four callers :-)
There is new code in my tree that isn't in Linus' tree which you are basing on.
Maybe it would be worth basing on linux-next? or pull the "for-next" branch from git://neil.brown.name/md/

I've fixed the extra caller and push it into my tree.

Thanks,
NeilBrown

author
#6 lore raw

PATCH 4/5 md: Turn rdev->sb_offset into a sector-based quantity.

In reply to Andre Noll

Rename it to sb_start to make sure all users have been converted.

---
 drivers/md/bitmap.c       |   10 ++++----
 drivers/md/md.c           |   56 +++++++++++++++++++++------------------------
 include/linux/raid/md_k.h |    2 +-
 3 files changed, 32 insertions(+), 36 deletions(-)
drivers/md/bitmap.c +5 -5
index b26927c..f1c44ff 100644
@@ -225,7 +225,7 @@ static struct page *read_sb_page(mddev_t *mddev, long offset, unsigned long inde
 		    || test_bit(Faulty, &rdev->flags))
 			continue;
 
-		target = (rdev->sb_offset << 1) + offset + index * (PAGE_SIZE/512);
+		target = rdev->sb_start + offset + index * (PAGE_SIZE/512);
 
 		if (sync_page_io(rdev->bdev, target, PAGE_SIZE, page, READ)) {
 			page->index = index;
@@ -262,12 +262,12 @@ static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
 					/* bitmap runs in to metadata */
 					return -EINVAL;
 				if (rdev->data_offset + mddev->size*2
-				    > rdev->sb_offset*2 + bitmap->offset)
+				    > rdev->sb_start + bitmap->offset)
 					/* data runs in to bitmap */
 					return -EINVAL;
-			} else if (rdev->sb_offset*2 < rdev->data_offset) {
+			} else if (rdev->sb_start < rdev->data_offset) {
 				/* METADATA BITMAP DATA */
-				if (rdev->sb_offset*2
+				if (rdev->sb_start
 				    + bitmap->offset
 				    + page->index*(PAGE_SIZE/512) + size/512
 				    > rdev->data_offset)
@@ -277,7 +277,7 @@ static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
 				/* DATA METADATA BITMAP - no problems */
 			}
 			md_super_write(mddev, rdev,
-				       (rdev->sb_offset<<1) + bitmap->offset
+				       rdev->sb_start + bitmap->offset
 				       + page->index * (PAGE_SIZE/512),
 				       size,
 				       page);
drivers/md/md.c +26 -30

171 lines not on this page · raw message

include/linux/raid/md_k.h +1 -1
index 3dea9f5..3df92af 100644
@@ -59,7 +59,7 @@ struct mdk_rdev_s
 	int		sb_loaded;
 	__u64		sb_events;
 	sector_t	data_offset;	/* start of data in array */
-	sector_t	sb_offset;
+	sector_t 	sb_start;	/* offset of the super block (in 512byte sectors) */
 	int		sb_size;	/* bytes in the superblock */
 	int		preferred_minor;	/* autorun support */
 
Signature · 1 line
1.5.3.8
#7 lore raw

Re: [PATCH 4/5] md: Turn rdev->sb_offset into a sector-based quantity.

Quoted 1 line · On Thursday July 10, [email protected] wrote:

Rename it to sb_start to make sure all users have been converted.

Good idea.

drivers/md/md.c static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) · line 1052

Quoted 13 lines
drivers/md/md.c +3 -4
@@ -1047,22 +1045,21 @@ static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
 	 */
 	switch(minor_version) {
 	case 0:
-		sb_offset = rdev->bdev->bd_inode->i_size >> 9;
-		sb_offset -= 8*2;
-		sb_offset &= ~(sector_t)(4*2-1);
+		sb_start = rdev->bdev->bd_inode->i_size >> 9;
+		sb_start -= 8*2;
+		sb_start &= ~(sector_t)(4*2-1);
 		/* convert from sectors to K */
-		sb_offset /= 2;
 		break;

That "convert.." comment can go.

drivers/md/md.c static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) · line 1113

Quoted 5 lines
drivers/md/md.c +1 -1
 	if (minor_version
-	    && rdev->data_offset < sb_offset + (rdev->sb_size/512))
+	    && rdev->data_offset < sb_start / 2 + (rdev->sb_size/512))
 		return -EINVAL;

That doesn't look right.
rdev->data_offset is in sectors. rdev->sb_size/512 is in sectors.
sb_offset is in K.
We had a bug there. Better fix it by dropping the "/ 2".

drivers/md/md.c static int do_md_run(mddev_t * mddev) · line 3431

Quoted 6 lines
drivers/md/md.c +1 -1
@@ -3431,16 +3428,16 @@ static int do_md_run(mddev_t * mddev)
 		 * We don't want the data to overlap the metadata,
 		 * Internal Bitmap issues has handled elsewhere.
 		 */
-		if (rdev->data_offset < rdev->sb_offset) {
+		if (rdev->data_offset < rdev->sb_start / 2) {

And another bug. We don't want the "/ 2". data_offset was always sectors, and now sb_offset is too.
That's two bugs we've found by doing this conversion - thanks!

NeilBrown

author
#8 lore raw

Re: [PATCH 4/5] md: Turn rdev->sb_offset into a sector-based quantity.

Quoted 11 lines · Neil Brown

Review

Quoted 4 lines
(quoted diff) +1 -1
 	if (minor_version
-	    && rdev->data_offset < sb_offset + (rdev->sb_size/512))
+	    && rdev->data_offset < sb_start / 2 + (rdev->sb_size/512))
 		return -EINVAL;

That doesn't look right.
rdev->data_offset is in sectors. rdev->sb_size/512 is in sectors.
sb_offset is in K.
We had a bug there. Better fix it by dropping the "/ 2".

Quoted 9 lines

Review static int do_md_run(mddev_t * mddev) · line 3431

Quoted 6 lines
(quoted diff) +1 -1
@@ -3431,16 +3428,16 @@ static int do_md_run(mddev_t * mddev)
 		 * We don't want the data to overlap the metadata,
 		 * Internal Bitmap issues has handled elsewhere.
 		 */
-		if (rdev->data_offset < rdev->sb_offset) {
+		if (rdev->data_offset < rdev->sb_start / 2) {

And another bug. We don't want the "/ 2". data_offset was always sectors, and now sb_offset is too.

It might be worth to get fixes for these two bugs into -stable.

Here's a patch against mainline. Please forward to stable@... if you agree.

Andre

commit c1be9a2d52dd2db1cf284e62d5e1cb8423482fb4

Author: Andre Noll <[email protected]>
Date:   Thu Jul 17 11:33:40 2008 +0200

md: Fix two bugs in md.c

sb_offset is kilobytes while data_offset and sb_size/512 are in sectors.

Spotted by Neil while reviewing the sector_t conversion patches.

drivers/md/md.c +2 -2
index 2580ac1..0c7aeb9 100644
@@ -1113,7 +1113,7 @@ static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
 		rdev->sb_size = (rdev->sb_size | bmask) + 1;
 
 	if (minor_version
-	    && rdev->data_offset < sb_offset + (rdev->sb_size/512))
+	    && rdev->data_offset < sb_offset * 2 + (rdev->sb_size/512))
 		return -EINVAL;
 
 	if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
@@ -3432,7 +3432,7 @@ static int do_md_run(mddev_t * mddev)
 		 * We don't want the data to overlap the metadata,
 		 * Internal Bitmap issues has handled elsewhere.
 		 */
-		if (rdev->data_offset < rdev->sb_offset) {
+		if (rdev->data_offset < rdev->sb_offset * 2) {
 			if (mddev->size &&
 			    rdev->data_offset + mddev->size*2
 			    > rdev->sb_offset*2) {
Signature · 1 line
The only person who always got his work done by Friday was Robinson Crusoe
#9 lore raw

Re: [PATCH 4/5] md: Turn rdev->sb_offset into a sector-based quantity.

Quoted 5 lines · On Thursday July 17, [email protected] wrote:

It might be worth to get fixes for these two bugs into -stable.

Here's a patch against mainline. Please forward to stable@... if you agree.

A question definitely worth considering.

I think in this case they don't qualify. The bug are in sanity checks, and they make the checks too weak. They don't actually cause incorrect behaviour in any credible scenario.

As the bugs don't open a security hole, don't cause data corruption, and don't trigger an 'oops', they don't really qualify for -stable.

Thanks,
NeilBrown

author
#10 lore raw

PATCH 5/5 md: Remove some unused macros.

In reply to Andre Noll
---
 include/linux/raid/md_p.h |    3 ---
 1 files changed, 0 insertions(+), 3 deletions(-)
include/linux/raid/md_p.h +0 -3
index 3f2cd98..8b4de4a 100644
@@ -43,14 +43,11 @@
  */
 #define MD_RESERVED_BYTES		(64 * 1024)
 #define MD_RESERVED_SECTORS		(MD_RESERVED_BYTES / 512)
-#define MD_RESERVED_BLOCKS		(MD_RESERVED_BYTES / BLOCK_SIZE)
 
 #define MD_NEW_SIZE_SECTORS(x)		((x & ~(MD_RESERVED_SECTORS - 1)) - MD_RESERVED_SECTORS)
-#define MD_NEW_SIZE_BLOCKS(x)		((x & ~(MD_RESERVED_BLOCKS - 1)) - MD_RESERVED_BLOCKS)
 
 #define MD_SB_BYTES			4096
 #define MD_SB_WORDS			(MD_SB_BYTES / 4)
-#define MD_SB_BLOCKS			(MD_SB_BYTES / BLOCK_SIZE)
 #define MD_SB_SECTORS			(MD_SB_BYTES / 512)
 
 /*
Signature · 1 line
1.5.3.8
#11 lore raw
In reply to Andre Noll
Quoted 6 lines · On Thursday July 10, [email protected] wrote:

Hi

here is the first batch of patches that aim to change the internal representations of sizes of raid devices from 1K blocks to 512 byte sectors.

Quoted 6 lines

Patches 1-3 change the internal functions update_size(), calc_dev_size() and calc_dev_sboffset() so that they take/return a sector count instead of a block count. Patch 4 changes the semantics of rdev->sb_offset so that its value now represents the number of the start sector instead of the start block. Patch 5 removes some macros that have become unused due to this conversion.

Thanks. I've pushed all of these (with some changes as mentioned in earlier emails) into my for-next tree
git://neil.brown.name/md/ for-next
http://neil.brown.name/git?p=md;a=shortlog;h=refs/heads/for-next

Thanks,
NeilBrown