Skip to contentSkip to conversation

loop: cleanup partitions when detaching loop device

PATCH New Phillip Susi 1 participant 1 message Dec 2, 2011 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 loop: cleanup partitions when detaching loop device

Any partitions added by user space to the loop device were being left in place after detaching the loop device. This was because the detach path issued a BLKRRPART to clean up partitions if LO_FLAGS_PARTSCAN was set, meaning that the partitions were auto scanned on attach. Replace this BLKRRPART with code that unconditionally cleans up partitions on detach instead.

---
 drivers/block/loop.c |   15 +++++++++++++--
 1 files changed, 13 insertions(+), 2 deletions(-)
drivers/block/loop.c +13 -2
index 68b205a..49a5eb7 100644
@@ -1016,8 +1016,19 @@ static int loop_clr_fd(struct loop_device *lo)
 	lo->lo_state = Lo_unbound;
 	/* This is safe: open() is still holding a reference. */
 	module_put(THIS_MODULE);
-	if (lo->lo_flags & LO_FLAGS_PARTSCAN && bdev)
-		ioctl_by_bdev(bdev, BLKRRPART, 0);
+	if (bdev)
+	{
+		struct disk_part_iter piter;
+		struct hd_struct *part;
+
+		mutex_lock(&bdev->bd_mutex);
+		invalidate_partition(bdev->bd_disk, 0);
+		disk_part_iter_init(&piter, bdev->bd_disk, DISK_PITER_INCL_EMPTY);
+		while ((part = disk_part_iter_next(&piter)))
+			delete_partition(bdev->bd_disk, part->partno);
+		disk_part_iter_exit(&piter);
+		mutex_unlock(&bdev->bd_mutex);
+	}
 	lo->lo_flags = 0;
 	if (!part_shift)
 		lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN;
Signature · 1 line
1.7.5.4