Skip to contentSkip to conversation

block: Allow extending partition size online

RFC PATCH New Vivek Goyal 1 participant 3 messages Sep 1, 2011

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover block: Allow extending partition size online

Hi,

This is a patch to allow extending the partition size online. There are two patches in the series. One is kernel patch and other is util-linux patch to add another command "extendpart".

Kernel patch adds another ioctl to increase the size of partition even when partition is in use. Currently limitation is that after modification to partition table, one needs to reboot the system/stop using the partition before size changes become visible. It would be nice to allow changing the size online, hence this change.

This ioctl changes only kernel view of data structures and does not touch ondisk data structures. So right way to expand the partition table would be to fist modify on disk copy using fdisk or so and then extend partition in kernel so that kernel's table and ondisk table are in sync.

Any feedback is welcome.

Thanks
Vivek

#1 lore raw

PATCH 1/1 block: Add ioctl for extending partition size online

Currently one can change the partition table but new partition table does not take effect if there are any users/openers of the partition.

It will be nice if partition can be extended while in use. The use case is that a device size can grow and the one should be able to extend the last partition to use size increase.

This patch provides an ioctl BLKPG_EXTEND_PARTITION, to extend the partition online. It does not support partition shrinking. It also does not modify any of the on disk data structures. User must make sure that table is first updated on disk and then do relevant changes in kernel's view of table. That will make sure things are fine over a reboot.

Apart from updating partition size, it also updates the associated bdev's inode size so that tools like pvresize can come to know of new size of partition device.

I have added one patch for util-linux package to add a command line tool , extendpart (on the lines of addpart, delpart).

I did following test.

- Create a partition of 10MB on a disk using fdisk.
- Add this partition to a volume group
- Use fdisk to increase the partition size to 20MB. (First delete the partition and then create a new one of 20MB size).
- Use extendpart to extend partition size in kernel.

	extendpart /dev/sda 1 20480

- Do pvresize on partition so that physical volume can be incrased in size online.
pvresize /dev/sda1

pvresize does recognize the new size. Also lsblk and /proc/partitions report the new size of partition.

---
 block/genhd.c         |   10 +++---
 block/ioctl.c         |   74 +++++++++++++++++++++++++++++++++++++++++++++++-
 fs/partitions/check.c |   66 +++++++++++++++++++++++++++++++++++++++++++-
 include/linux/blkpg.h |    1 +
 include/linux/genhd.h |    8 +++++
 5 files changed, 151 insertions(+), 8 deletions(-)
block/genhd.c +5 -5
index e2f6790..90327a4 100644
@@ -154,7 +154,7 @@ struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter)
 		part = rcu_dereference(ptbl->part[piter->idx]);
 		if (!part)
 			continue;
-		if (!part->nr_sects &&
+		if (!part_nr_sects_read(part) &&
 		    !(piter->flags & DISK_PITER_INCL_EMPTY) &&
 		    !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 &&
 		      piter->idx == 0))
@@ -191,7 +191,7 @@ EXPORT_SYMBOL_GPL(disk_part_iter_exit);
 static inline int sector_in_part(struct hd_struct *part, sector_t sector)
 {
 	return part->start_sect <= sector &&
-		sector < part->start_sect + part->nr_sects;
+		sector < part->start_sect + part_nr_sects_read(part);
 }
 
 /**
@@ -760,8 +760,8 @@ void __init printk_all_partitions(void)
 
 			printk("%s%s %10llu %s %s", is_part0 ? "" : "  ",
 			       bdevt_str(part_devt(part), devt_buf),
-			       (unsigned long long)part->nr_sects >> 1,
-			       disk_name(disk, part->partno, name_buf), uuid);
+			       (unsigned long long)part_nr_sects_read(part) >> 1
+			       , disk_name(disk, part->partno, name_buf), uuid);
 			if (is_part0) {
 				if (disk->driverfs_dev != NULL &&
 				    disk->driverfs_dev->driver != NULL)
@@ -852,7 +852,7 @@ static int show_partition(struct seq_file *seqf, void *v)
 	while ((part = disk_part_iter_next(&piter)))
 		seq_printf(seqf, "%4d  %7d %10llu %s\n",
 			   MAJOR(part_devt(part)), MINOR(part_devt(part)),
-			   (unsigned long long)part->nr_sects >> 1,
+			   (unsigned long long)part_nr_sects_read(part) >> 1,
 			   disk_name(sgp, part->partno, buf));
 	disk_part_iter_exit(&piter);
 
block/ioctl.c +72 -2

90 lines not on this page · raw message

fs/partitions/check.c +65 -1

84 lines not on this page · raw message

include/linux/blkpg.h +1 -0
index faf8a45..2a7bce9 100644
@@ -40,6 +40,7 @@ struct blkpg_ioctl_arg {
 /* The subfunctions (for the op field) */
 #define BLKPG_ADD_PARTITION	1
 #define BLKPG_DEL_PARTITION	2
+#define BLKPG_EXTEND_PARTITION	3
 
 /* Sizes of name fields. Unused at present. */
 #define BLKPG_DEVNAMELTH	64
include/linux/genhd.h +8 -0
index 02fa469..81c12d2 100644
@@ -98,7 +98,13 @@ struct partition_meta_info {
 
 struct hd_struct {
 	sector_t start_sect;
+	/*
+	 * nr_sects is protected by sequence counter. One might extend a
+	 * partition while IO is happening to it and update of nr_sects
+	 * can be non-atomic on 32bit machines with 64bit sector_t.
+	 */
 	sector_t nr_sects;
+	seqcount_t seq;
 	sector_t alignment_offset;
 	unsigned int discard_alignment;
 	struct device __dev;
@@ -600,8 +606,10 @@ extern struct hd_struct * __must_check add_partition(struct gendisk *disk,
 						     struct partition_meta_info
 						       *info);
 extern void __delete_partition(struct hd_struct *);
+extern int extend_partition(struct gendisk *disk, int partno, sector_t size);
 extern void delete_partition(struct gendisk *, int);
 extern void printk_all_partitions(void);
+extern sector_t part_nr_sects_read(struct hd_struct *part);
 
 extern struct gendisk *alloc_disk_node(int minors, int node_id);
 extern struct gendisk *alloc_disk(int minors);
Signature · 1 line
1.7.6
#2 lore raw

PATCH 1/1 extendpart: A new command line to extend an existing partition

In reply to Vivek Goyal

This is along the lines of addpart and delpart to modify in kernel partition table and does not touch the ondisk data structures.

---
 partx/Makefile.am  |    2 +-
 partx/extendpart.c |   29 +++++++++++++++++++++++++++++
 partx/partx.h      |   20 ++++++++++++++++++++
 3 files changed, 50 insertions(+), 1 deletions(-)
 create mode 100644 partx/extendpart.c
partx/Makefile.am +1 -1
index 6a72942..f84126d 100644
@@ -1,6 +1,6 @@
 include $(top_srcdir)/config/include-Makefile.am
 
-usrsbin_exec_PROGRAMS = addpart delpart
+usrsbin_exec_PROGRAMS = addpart delpart extendpart
 dist_man_MANS = addpart.8 delpart.8
 
 usrsbin_exec_PROGRAMS += partx
partx/extendpart.c new file +29 -0
new file mode 100644
index 0000000..a9577c2
@@ -0,0 +1,29 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+
+#include "partx.h"
+
+int
+main(int argc, char **argv)
+{
+	int fd;
+
+	if (argc != 4) {
+		fprintf(stderr,
+			"usage: %s diskdevice partitionnr size\n",
+			argv[0]);
+		exit(1);
+	}
+	if ((fd = open(argv[1], O_RDONLY)) < 0) {
+		perror(argv[1]);
+		exit(1);
+	}
+
+	if (partx_extend_partition(fd, atoi(argv[2]), atoll(argv[3]))) {
+		perror("BLKPG");
+		exit(1);
+	}
+
+	return 0;
+}
partx/partx.h +20 -0
index b40fa8f..1acbff9 100644
@@ -41,4 +41,24 @@ static inline int partx_add_partition(int fd, int partno,
 	return ioctl(fd, BLKPG, &a);
 }
 
+static inline int partx_extend_partition(int fd, int partno,
+				unsigned long size)
+{
+	struct blkpg_ioctl_arg a;
+	struct blkpg_partition p;
+
+	p.pno = partno;
+	/* p.start is unused for extend operation */
+	p.start = 0;
+	p.length = size << 9;
+	p.devname[0] = 0;
+	p.volname[0] = 0;
+	a.op = BLKPG_EXTEND_PARTITION;
+	a.flags = 0;
+	a.datalen = sizeof(p);
+	a.data = &p;
+
+	return ioctl(fd, BLKPG, &a);
+}
+
 #endif /*  UTIL_LINUX_PARTX_H */
Signature · 1 line
1.7.6