Skip to contentSkip to conversation

ide-floppy partitions

RFC PATCH In review Borislav Petkov 4 participants 9 messages Oct 29, 2008 – Nov 5, 2008

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

RFC PATCH 1/1 ide-floppy partitions

Hi Tejun,

recent changes at 0762b8bde9729f10f8e6249809660ff2ec3ad735 and around break ide-floppy. Since it is a removable media drive and the partition scan during boot returns empty (no media in the drive), when you later put in a disk and try to mount it, mount returns saying

/dev/hdc4 is not a valid block device.

Which brings me to the other possible issue: Since having a hdc4 partition as a single FAT16 partition on a ZIP drive is the "factory default" you could fabricate a case where you have a partition number> 1 as the only partition on a hard drive too, i.e. no continuous partition numbering and the mount would theoretically fail there too since, for example, there's a check in disk_get_part() which does:

if (likely(partno < ptbl->len)) {

and in this case the check will fail if partno >= 1 while you have only one partition on the disk with a number higher than the partition table length and the above described failure will happen too. Anyways, this is just a hypothesis, but it happens with the ZIP drive here so other block devices should behave similarly.

Here's a patch that fixes the ide-floppy case a bit clumsily, I admit.

---

fs/block_dev.c +17 -0
index 88a776f..b798ea0 100644
@@ -1011,6 +1011,23 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
 	disk = get_gendisk(bdev->bd_dev, &partno);
 	if (!disk)
 		goto out_unlock_kernel;
+
+	part = disk_get_part(disk, partno);
+	if (!part) {
+		struct block_device *whole;
+
+		mutex_lock_nested(&bdev->bd_mutex, for_part);
+		whole = bdget_disk(disk, 0);
+		ret = -ENOMEM;
+		if (!whole)
+			goto out_clear;
+		ret = __blkdev_get(whole, mode, 1);
+		if (ret)
+			goto out_clear;
+		bdev->bd_contains = whole;
+		mutex_unlock(&bdev->bd_mutex);
+	}
+
 	part = disk_get_part(disk, partno);
 	if (!part)
 		goto out_unlock_kernel;
Signature · 2 lines
Regards/Gruss,
    Boris.
#1 lore raw

PATCH 1/1 ide-gd: re-get floppy capacity on revalidate

Hi Bart,

by the way we need to following in ide-gd otherwise rescan_partitions returns early due to the capacity being 0.

---
From: Borislav Petkov <[email protected]>
Subject: ide-gd: re-get capacity on revalidate

We need to re-get a removable media's capacity when revalidating the disk so that its partitions get rescanned by the block layer.

drivers/ide/ide-gd.c +6 -1
index 7b66628..b8078b3 100644
@@ -281,7 +281,12 @@ static int ide_gd_media_changed(struct gendisk *disk)
 static int ide_gd_revalidate_disk(struct gendisk *disk)
 {
 	struct ide_disk_obj *idkp = ide_drv_g(disk, ide_disk_obj);
-	set_capacity(disk, ide_gd_capacity(idkp->drive));
+	ide_drive_t *drive = idkp->drive;
+
+	if (ide_gd_media_changed(disk))
+		drive->disk_ops->get_capacity(drive);
+
+	set_capacity(disk, ide_gd_capacity(drive));
 	return 0;
 }
 
Signature · 2 lines
Regards/Gruss,
    Boris.
#2 lore raw

Re: [PATCH] ide-gd: re-get floppy capacity on revalidate

Quoted 5 lines · Borislav Petkov

Hi Bart,

by the way we need to following in ide-gd otherwise rescan_partitions returns early due to the capacity being 0.

Quoted 8 lines

---
From: Borislav Petkov <[email protected]>
Subject: ide-gd: re-get capacity on revalidate

We need to re-get a removable media's capacity when revalidating the disk so that its partitions get rescanned by the block layer.

applied

#3 lore raw
In reply to Borislav Petkov
Quoted 45 lines · Borislav Petkov

Hi Tejun,

recent changes at 0762b8bde9729f10f8e6249809660ff2ec3ad735 and around break ide-floppy. Since it is a removable media drive and the partition scan during boot returns empty (no media in the drive), when you later put in a disk and try to mount it, mount returns saying

/dev/hdc4 is not a valid block device.

Which brings me to the other possible issue: Since having a hdc4 partition as a single FAT16 partition on a ZIP drive is the "factory default" you could fabricate a case where you have a partition number> 1 as the only partition on a hard drive too, i.e. no continuous partition numbering and the mount would theoretically fail there too since, for example, there's a check in disk_get_part() which does:

if (likely(partno < ptbl->len)) {

and in this case the check will fail if partno >= 1 while you have only one partition on the disk with a number higher than the partition table length and the above described failure will happen too. Anyways, this is just a hypothesis, but it happens with the ZIP drive here so other block devices should behave similarly.

Here's a patch that fixes the ide-floppy case a bit clumsily, I admit.

---

fs/block_dev.c +10 -0
index 88a776f..b798ea0 100644
@@ -1011,6 +1011,23 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
 	disk = get_gendisk(bdev->bd_dev, &partno);
 	if (!disk)
 		goto out_unlock_kernel;
+
+	part = disk_get_part(disk, partno);
+	if (!part) {
+		struct block_device *whole;
+
+		mutex_lock_nested(&bdev->bd_mutex, for_part);
+		whole = bdget_disk(disk, 0);
+		ret = -ENOMEM;
+		if (!whole)
+			goto out_clear;

fs/block_dev.c static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part) · line 1033

Quoted 10 lines
fs/block_dev.c +7 -0
+		ret = __blkdev_get(whole, mode, 1);
+		if (ret)
+			goto out_clear;
+		bdev->bd_contains = whole;
+		mutex_unlock(&bdev->bd_mutex);
+	}
+
 	part = disk_get_part(disk, partno);
 	if (!part)
 		goto out_unlock_kernel;

Tejun, Jens,

can you guys please ACK/NACK this so that we can have an agreed upon solution and so that I can stop applying it ontop of current git before testing any further.

Thanks.

Signature · 2 lines
Regards/Gruss,
    Boris.
#4 lore raw
Quoted 3 lines · Borislav Petkov

can you guys please ACK/NACK this so that we can have an agreed upon solution and so that I can stop applying it ontop of current git before testing any further.

Ah... right. I missed removable devices. I think it would be better to just expand the table rather than recursing into __blkdev_get() again. I'll try to come up with cleaner solution. Also, I don't think the discontinuous partition would be a problem. ptbl is expanded to the highest numbered partition not the number of partitions. I'll test that too. Thanks.

Signature · 1 line
tejun
#5 lore raw

Hi

Quoted 8 lines · Tejun Heo
Quoted 3 lines · Borislav Petkov

can you guys please ACK/NACK this so that we can have an agreed upon solution and so that I can stop applying it ontop of current git before testing any further.

Ah... right. I missed removable devices. I think it would be better to just expand the table rather than recursing into __blkdev_get() again. I'll try to come up with cleaner solution.

Sounds good. It should simply rescan partitions upon media change before it does disk_get_part so that the ptbl is updated.

Quoted 4 lines

Also, I don't think the
discontinuous partition would be a problem. ptbl is expanded to the highest numbered partition not the number of partitions. I'll test that too. Thanks.

You don't have to. I already did that with a harddrive in a external case over usb and it works since upon connection the usb core rescans partitions and ptbl is valid then.

Thanks.

Signature · 2 lines
Regards/Gruss,
Boris
#6 lore raw

Subject: [PATCH] block: fix __blkdev_get() for removable devices

Commit 0762b8bde9729f10f8e6249809660ff2ec3ad735 moved disk_get_part() in front of recursive get on the whole disk, which caused removable devices to try disk_get_part() before rescanning after a new media is inserted, which might fail legit open attempts or give the old partition.

This patch fixes the problem by moving disk_get_part() after __blkdev_get() on the whole disk.

This problem was spotted by Borislav Petkov.

Cc <Borislav Petkov> [email protected]

---
Borislav, can you please verify this patch? Thanks.

 fs/block_dev.c |   23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)
fs/block_dev.c +11 -12
index 88a776f..db831ef 100644
@@ -986,7 +986,6 @@ static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
 static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
 {
 	struct gendisk *disk;
-	struct hd_struct *part = NULL;
 	int ret;
 	int partno;
 	int perm = 0;
@@ -1004,24 +1003,25 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
 		return ret;
 	}
 
-	ret = -ENXIO;
-
 	lock_kernel();
 
+	ret = -ENXIO;
 	disk = get_gendisk(bdev->bd_dev, &partno);
 	if (!disk)
 		goto out_unlock_kernel;
-	part = disk_get_part(disk, partno);
-	if (!part)
-		goto out_unlock_kernel;
 
 	mutex_lock_nested(&bdev->bd_mutex, for_part);
 	if (!bdev->bd_openers) {
 		bdev->bd_disk = disk;
-		bdev->bd_part = part;
 		bdev->bd_contains = bdev;
 		if (!partno) {
 			struct backing_dev_info *bdi;
+
+			ret = -ENXIO;
+			bdev->bd_part = disk_get_part(disk, partno);
+			if (!bdev->bd_part)
+				goto out_clear;
+
 			if (disk->fops->open) {
 				ret = disk->fops->open(bdev, mode);
 				if (ret)
@@ -1049,18 +1049,17 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
 			bdev->bd_contains = whole;
 			bdev->bd_inode->i_data.backing_dev_info =
 			   whole->bd_inode->i_data.backing_dev_info;
+			bdev->bd_part = disk_get_part(disk, partno);
 			if (!(disk->flags & GENHD_FL_UP) ||
-			    !part || !part->nr_sects) {
+			    !bdev->bd_part || !bdev->bd_part->nr_sects) {
 				ret = -ENXIO;
 				goto out_clear;
 			}
-			bd_set_size(bdev, (loff_t)part->nr_sects << 9);
+			bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
 		}
 	} else {
-		disk_put_part(part);
 		put_disk(disk);
 		module_put(disk->fops->owner);
-		part = NULL;
 		disk = NULL;
 		if (bdev->bd_contains == bdev) {
 			if (bdev->bd_disk->fops->open) {
@@ -1080,6 +1079,7 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
 	return 0;
 
  out_clear:
+	disk_put_part(bdev->bd_part);
 	bdev->bd_disk = NULL;
 	bdev->bd_part = NULL;
 	bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
@@ -1091,7 +1091,6 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
  out_unlock_kernel:
 	unlock_kernel();
 
-	disk_put_part(part);
 	if (disk)
 		module_put(disk->fops->owner);
 	put_disk(disk);
author Tested-by
#7 lore raw

Re: Subject: [PATCH] block: fix __blkdev_get() for removable devices

Quoted 9 lines · Tejun Heo

Commit 0762b8bde9729f10f8e6249809660ff2ec3ad735 moved disk_get_part() in front of recursive get on the whole disk, which caused removable devices to try disk_get_part() before rescanning after a new media is inserted, which might fail legit open attempts or give the old partition.

This patch fixes the problem by moving disk_get_part() after __blkdev_get() on the whole disk.

Quoted 6 lines

This problem was spotted by Borislav Petkov.

Cc <Borislav Petkov> [email protected]

---
Borislav, can you please verify this patch? Thanks.

Yep, it works. Thanks.

Signature · 2 lines
Regards/Gruss,
    Boris.
#8 lore raw

Re: Subject: [PATCH] block: fix __blkdev_get() for removable devices

Quoted 12 lines · Borislav Petkov
Quoted 11 lines · Tejun Heo

Commit 0762b8bde9729f10f8e6249809660ff2ec3ad735 moved disk_get_part() in front of recursive get on the whole disk, which caused removable devices to try disk_get_part() before rescanning after a new media is inserted, which might fail legit open attempts or give the old partition.

This patch fixes the problem by moving disk_get_part() after __blkdev_get() on the whole disk.

This problem was spotted by Borislav Petkov.

Quoted 8 lines
Quoted 4 lines
Cc <Borislav Petkov> [email protected]

---
Borislav, can you please verify this patch? Thanks.

Yep, it works. Thanks.

I merged it and added your tested-by.

Signature · 1 line
Jens Axboe