public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Stefanos Harhalakis <v13@priest.com>
To: rmk@arm.linux.org.uk
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH] Reread partition table when a partition is added
Date: Mon, 23 Aug 2004 01:19:06 +0300	[thread overview]
Message-ID: <200408230119.12878.v13@priest.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 2641 bytes --]

  This small patch rereads the partition table even if the block device is 
beeing used. It only rereads it when there are no changes at the current 
partitions and there are new partitions added at the end. Any existing 
partition change will/should make it fail.

  Is it OK and/or useful ?

  (I'm quite inexperienced about kernel coding so be gentle :)

Signed-off-by: Stefanos Harhalakis <v13@priest.com>

RCS file: /usr/local/cvsroot/linux-2.6/fs/partitions/check.c,v
retrieving revision 1.1.1.1
diff -u -p -r1.1.1.1 check.c
--- check.c	22 Aug 2004 11:08:27 -0000	1.1.1.1
+++ check.c	22 Aug 2004 22:09:11 -0000
@@ -391,19 +391,44 @@ void register_disk(struct gendisk *disk)
 	blkdev_put(bdev);
 }
 
+/*
+ * Determine the last partition.
+ * Return the partition number or 0 if there is no partition
+ */
+static int get_last_partition(struct gendisk *disk)
+{
+	int	n, ret = 0;
+
+	for (n=0; n < disk->minors ; n++) {
+		if (disk->part[n])
+			ret=n+1;
+	}
+
+	return(ret);
+}
+
 int rescan_partitions(struct gendisk *disk, struct block_device *bdev)
 {
 	struct parsed_partitions *state;
+	struct hd_struct *phd;
 	int p, res;
+	int is_busy = 0, last_partition = 0;
+
+	if (bdev->bd_part_count) {
+		is_busy = 1;
+		last_partition=get_last_partition(disk);
+	}
 
-	if (bdev->bd_part_count)
-		return -EBUSY;
 	res = invalidate_partition(disk, 0);
 	if (res)
 		return res;
 	bdev->bd_invalidated = 0;
-	for (p = 1; p < disk->minors; p++)
-		delete_partition(disk, p);
+
+	if (!is_busy) {
+		for (p = 1; p < disk->minors; p++)
+			delete_partition(disk, p);
+	}
+
 	if (disk->fops->revalidate_disk)
 		disk->fops->revalidate_disk(disk);
 	if (!get_capacity(disk) || !(state = check_partition(disk, bdev)))
@@ -411,13 +436,29 @@ int rescan_partitions(struct gendisk *di
 	for (p = 1; p < state->limit; p++) {
 		sector_t size = state->parts[p].size;
 		sector_t from = state->parts[p].from;
+
+		if (is_busy && p<=last_partition) {
+			phd=disk->part[p-1];
+			if (phd != NULL && (phd->start_sect != from ||
+			 	phd->nr_sects != size)) {
+				printk(KERN_INFO
+					"Existing partition was changed. "
+					"Reboot is required.\n");
+				res=-EBUSY;
+				break;
+			}
+		}
+
 		if (!size)
 			continue;
-		add_partition(disk, p, from, size);
+
+		if (!is_busy || p>last_partition) {
+			add_partition(disk, p, from, size);
 #ifdef CONFIG_BLK_DEV_MD
-		if (state->parts[p].flags)
-			md_autodetect_dev(bdev->bd_dev+p);
+			if (state->parts[p].flags)
+				md_autodetect_dev(bdev->bd_dev+p);
 #endif
+		}
 	}
 	kfree(state);
 	return res;

[-- Attachment #1.2: rescan_partitions.patch --]
[-- Type: text/x-diff, Size: 2219 bytes --]

RCS file: /usr/local/cvsroot/linux-2.6/fs/partitions/check.c,v
retrieving revision 1.1.1.1
diff -u -p -r1.1.1.1 check.c
--- check.c	22 Aug 2004 11:08:27 -0000	1.1.1.1
+++ check.c	22 Aug 2004 22:09:11 -0000
@@ -391,19 +391,44 @@ void register_disk(struct gendisk *disk)
 	blkdev_put(bdev);
 }
 
+/*
+ * Determine the last partition.
+ * Return the partition number or 0 if there is no partition
+ */
+static int get_last_partition(struct gendisk *disk)
+{
+	int	n, ret = 0;
+
+	for (n=0; n < disk->minors ; n++) {
+		if (disk->part[n])
+			ret=n+1;
+	}
+
+	return(ret);
+}
+
 int rescan_partitions(struct gendisk *disk, struct block_device *bdev)
 {
 	struct parsed_partitions *state;
+	struct hd_struct *phd;
 	int p, res;
+	int is_busy = 0, last_partition = 0;
+
+	if (bdev->bd_part_count) {
+		is_busy = 1;
+		last_partition=get_last_partition(disk);
+	}
 
-	if (bdev->bd_part_count)
-		return -EBUSY;
 	res = invalidate_partition(disk, 0);
 	if (res)
 		return res;
 	bdev->bd_invalidated = 0;
-	for (p = 1; p < disk->minors; p++)
-		delete_partition(disk, p);
+
+	if (!is_busy) {
+		for (p = 1; p < disk->minors; p++)
+			delete_partition(disk, p);
+	}
+
 	if (disk->fops->revalidate_disk)
 		disk->fops->revalidate_disk(disk);
 	if (!get_capacity(disk) || !(state = check_partition(disk, bdev)))
@@ -411,13 +436,29 @@ int rescan_partitions(struct gendisk *di
 	for (p = 1; p < state->limit; p++) {
 		sector_t size = state->parts[p].size;
 		sector_t from = state->parts[p].from;
+
+		if (is_busy && p<=last_partition) {
+			phd=disk->part[p-1];
+			if (phd != NULL && (phd->start_sect != from ||
+			 	phd->nr_sects != size)) {
+				printk(KERN_INFO
+					"Existing partition was changed. "
+					"Reboot is required.\n");
+				res=-EBUSY;
+				break;
+			}
+		}
+
 		if (!size)
 			continue;
-		add_partition(disk, p, from, size);
+
+		if (!is_busy || p>last_partition) {
+			add_partition(disk, p, from, size);
 #ifdef CONFIG_BLK_DEV_MD
-		if (state->parts[p].flags)
-			md_autodetect_dev(bdev->bd_dev+p);
+			if (state->parts[p].flags)
+				md_autodetect_dev(bdev->bd_dev+p);
 #endif
+		}
 	}
 	kfree(state);
 	return res;

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

             reply	other threads:[~2004-08-22 22:21 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-22 22:19 Stefanos Harhalakis [this message]
2004-08-23  9:53 ` [PATCH] Reread partition table when a partition is added Andries Brouwer
2004-08-23 16:14   ` V13

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200408230119.12878.v13@priest.com \
    --to=v13@priest.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rmk@arm.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox