Skip to contentSkip to conversation

rework check_disk_change()

PATCH In review Christoph Hellwig 6 participants 45 messages Sep 2, 2020 – Sep 8, 2020

Subsystems (6)
VFS SCSI Block layer libata +2 more subsystems
lore
Share & tools
lore
#0 lore raw

Hi Jens,

this series replaced the not very nice check_disk_change() function with a new bdev_media_changed that avoids having the ->revalidate_disk call at its end. As a result ->revalidate_disk can be removed from a lot of drivers.

Diffstat:

 block/genhd.c              |   29 ++++++++++++++++++++++++++-
 drivers/block/amiflop.c    |    2 -
 drivers/block/ataflop.c    |    7 +++---
 drivers/block/floppy.c     |    8 ++++---
 drivers/block/paride/pcd.c |    2 -
 drivers/block/swim.c       |   22 +-------------------
 drivers/block/swim3.c      |    4 +--
 drivers/block/xsysace.c    |   26 +++++++++---------------
 drivers/cdrom/gdrom.c      |    2 -
 drivers/ide/ide-cd.c       |   16 ++++-----------
 drivers/ide/ide-disk.c     |    5 ----
 drivers/ide/ide-floppy.c   |    2 -
 drivers/ide/ide-gd.c       |   48 +++++----------------------------------------
 drivers/md/md.c            |    2 -
 drivers/scsi/sd.c          |    7 +++---
 drivers/scsi/sr.c          |   36 +++++++++++++--------------------
 fs/block_dev.c             |   31 -----------------------------
 include/linux/genhd.h      |    3 --
 include/linux/ide.h        |    2 -
 19 files changed, 86 insertions(+), 168 deletions(-)
#1 lore raw

PATCH 1/19 block: add a bdev_check_media_change helper

Like check_disk_changed, except that it does not call ->revalidate_disk but leaves that to the caller.

---
 block/genhd.c         | 29 ++++++++++++++++++++++++++++-
 fs/block_dev.c        | 17 +++--------------
 include/linux/genhd.h |  2 +-
 3 files changed, 32 insertions(+), 16 deletions(-)
block/genhd.c +28 -1
index 081f1039d9367f..9d060e79eb31d8 100644
@@ -2052,7 +2052,7 @@ void disk_flush_events(struct gendisk *disk, unsigned int mask)
  * CONTEXT:
  * Might sleep.
  */
-unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
+static unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
 {
 	struct disk_events *ev = disk->ev;
 	unsigned int pending;
@@ -2090,6 +2090,33 @@ unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
 	return pending;
 }
 
+/**
+ * bdev_check_media_change - check if a removable media has been changed
+ * @bdev: block device to check
+ *
+ * Check whether a removable media has been changed, and attempt to free all
+ * dentries and inodes and invalidates all block device page cache entries in
+ * that case.
+ *
+ * Returns %true if the block device changed, or %false if not.
+ */
+bool bdev_check_media_change(struct block_device *bdev)
+{
+	unsigned int events;
+
+	events = disk_clear_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE |
+				   DISK_EVENT_EJECT_REQUEST);
+	if (!(events & DISK_EVENT_MEDIA_CHANGE))
+		return false;
+
+	if (__invalidate_device(bdev, true))
+		pr_warn("VFS: busy inodes on changed media %s\n",
+			bdev->bd_disk->disk_name);
+	set_bit(BDEV_NEED_PART_SCAN, &bdev->bd_flags);
+	return true;
+}
+EXPORT_SYMBOL(bdev_check_media_change);
+
 /*
  * Separate this part out so that a different pointer for clearing_ptr can be
  * passed in for disk_clear_events.
fs/block_dev.c +3 -14
index 9cb205405f9d99..37cb809b217926 100644
@@ -1350,21 +1350,10 @@ EXPORT_SYMBOL(revalidate_disk_size);
  */
 int check_disk_change(struct block_device *bdev)
 {
-	struct gendisk *disk = bdev->bd_disk;
-	const struct block_device_operations *bdops = disk->fops;
-	unsigned int events;
-
-	events = disk_clear_events(disk, DISK_EVENT_MEDIA_CHANGE |
-				   DISK_EVENT_EJECT_REQUEST);
-	if (!(events & DISK_EVENT_MEDIA_CHANGE))
+	if (!bdev_check_media_change(bdev))
 		return 0;
-
-	if (__invalidate_device(bdev, true))
-		pr_warn("VFS: busy inodes on changed media %s\n",
-			disk->disk_name);
-	set_bit(BDEV_NEED_PART_SCAN, &bdev->bd_flags);
-	if (bdops->revalidate_disk)
-		bdops->revalidate_disk(bdev->bd_disk);
+	if (bdev->bd_disk->fops->revalidate_disk)
+		bdev->bd_disk->fops->revalidate_disk(bdev->bd_disk);
 	return 1;
 }
 
include/linux/genhd.h +1 -1
index c618b27292fcc8..322d48a207728a 100644
@@ -315,7 +315,6 @@ extern void disk_unblock_events(struct gendisk *disk);
 extern void disk_flush_events(struct gendisk *disk, unsigned int mask);
 void set_capacity_revalidate_and_notify(struct gendisk *disk, sector_t size,
 		bool update_bdev);
-extern unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask);
 
 /* drivers/char/random.c */
 extern void add_disk_randomness(struct gendisk *disk) __latent_entropy;
@@ -372,6 +371,7 @@ void unregister_blkdev(unsigned int major, const char *name);
 
 void revalidate_disk_size(struct gendisk *disk, bool verbose);
 int check_disk_change(struct block_device *bdev);
+bool bdev_check_media_change(struct block_device *bdev);
 int __invalidate_device(struct block_device *bdev, bool kill_dirty);
 void bd_set_nr_sectors(struct block_device *bdev, sector_t sectors);
 
Signature · 1 line
2.28.0
#3 lore raw

PATCH 2/19 amiflop: use bdev_check_media_change

In reply to Christoph Hellwig

The Amiga floppy driver does not have a ->revalidate_disk method, so it can just use bdev_check_media_change without any additional changes.

---
 drivers/block/amiflop.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/amiflop.c +1 -1
index 226219da3da6a7..71c2b156455860 100644
@@ -1670,7 +1670,7 @@ static int floppy_open(struct block_device *bdev, fmode_t mode)
 	}
 
 	if (mode & (FMODE_READ|FMODE_WRITE)) {
-		check_disk_change(bdev);
+		bdev_check_media_change(bdev);
 		if (mode & FMODE_WRITE) {
 			int wrprot;
 
Signature · 1 line
2.28.0
#5 lore raw

PATCH 3/19 ataflop: use bdev_check_media_change

In reply to Christoph Hellwig

Switch to use bdev_check_media_change instead of check_disk_change and call floppy_revalidate manually. Given that floppy_revalidate only deals with media change events, the extra call into ->revalidate_disk from bdev_disk_changed is not required either, so stop wiring up the method.

---
 drivers/block/ataflop.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/block/ataflop.c +4 -3
index a50e13af030526..3e881fdb06e0ad 100644
@@ -1732,7 +1732,8 @@ static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode,
 		/* invalidate the buffer track to force a reread */
 		BufferDrive = -1;
 		set_bit(drive, &fake_change);
-		check_disk_change(bdev);
+		if (bdev_check_media_change(bdev))
+			floppy_revalidate(bdev->bd_disk);
 		return 0;
 	default:
 		return -EINVAL;
@@ -1909,7 +1910,8 @@ static int floppy_open(struct block_device *bdev, fmode_t mode)
 		return 0;
 
 	if (mode & (FMODE_READ|FMODE_WRITE)) {
-		check_disk_change(bdev);
+		if (bdev_check_media_change(bdev))
+			floppy_revalidate(bdev->bd_disk);
 		if (mode & FMODE_WRITE) {
 			if (p->wpstat) {
 				if (p->ref < 0)
@@ -1953,7 +1955,6 @@ static const struct block_device_operations floppy_fops = {
 	.release	= floppy_release,
 	.ioctl		= fd_ioctl,
 	.check_events	= floppy_check_events,
-	.revalidate_disk= floppy_revalidate,
 };
 
 static const struct blk_mq_ops ataflop_mq_ops = {
Signature · 1 line
2.28.0
#7 lore raw

PATCH 4/19 floppy: use bdev_check_media_change

In reply to Christoph Hellwig

Switch to use bdev_check_media_change instead of check_disk_change and call floppy_revalidate manually. Given that floppy_revalidate only deals with media change events, the extra call into ->revalidate_disk from bdev_disk_changed is not required either, so stop wiring up the method.

---
 drivers/block/floppy.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/block/floppy.c +5 -3
index a563b023458a8b..7df79ae6b0a1e1 100644
@@ -561,6 +561,7 @@ static void floppy_release_irq_and_dma(void);
  * output_byte is automatically disabled when reset is set.
  */
 static void reset_fdc(void);
+static int floppy_revalidate(struct gendisk *disk);
 
 /*
  * These are global variables, as that's the easiest way to give
@@ -3275,7 +3276,8 @@ static int invalidate_drive(struct block_device *bdev)
 	/* invalidate the buffer track to force a reread */
 	set_bit((long)bdev->bd_disk->private_data, &fake_change);
 	process_fd_request();
-	check_disk_change(bdev);
+	if (bdev_check_media_change(bdev))
+		floppy_revalidate(bdev->bd_disk);
 	return 0;
 }
 
@@ -4123,7 +4125,8 @@ static int floppy_open(struct block_device *bdev, fmode_t mode)
 			drive_state[drive].last_checked = 0;
 			clear_bit(FD_OPEN_SHOULD_FAIL_BIT,
 				  &drive_state[drive].flags);
-			check_disk_change(bdev);
+			if (bdev_check_media_change(bdev))
+				floppy_revalidate(bdev->bd_disk);
 			if (test_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags))
 				goto out;
 			if (test_bit(FD_OPEN_SHOULD_FAIL_BIT, &drive_state[drive].flags))
@@ -4291,7 +4294,6 @@ static const struct block_device_operations floppy_fops = {
 	.ioctl			= fd_ioctl,
 	.getgeo			= fd_getgeo,
 	.check_events		= floppy_check_events,
-	.revalidate_disk	= floppy_revalidate,
 #ifdef CONFIG_COMPAT
 	.compat_ioctl		= fd_compat_ioctl,
 #endif
Signature · 1 line
2.28.0
#9 lore raw

PATCH 5/19 swim: use bdev_check_media_change

In reply to Christoph Hellwig

Switch to use bdev_check_media_change instead of check_disk_change and call floppy_revalidate manually. Given that floppy_revalidate only deals with media change events, the extra call into ->revalidate_disk from bdev_disk_changed is not required either, so stop wiring up the method.

---
 drivers/block/swim.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/block/swim.c +4 -2
index dd34504382e533..d4565c555b289f 100644
@@ -217,6 +217,8 @@ extern int swim_read_sector_header(struct swim __iomem *base,
 extern int swim_read_sector_data(struct swim __iomem *base,
 				 unsigned char *data);
 
+static int floppy_revalidate(struct gendisk *disk);
+
 static DEFINE_MUTEX(swim_mutex);
 static inline void set_swim_mode(struct swim __iomem *base, int enable)
 {
@@ -638,7 +640,8 @@ static int floppy_open(struct block_device *bdev, fmode_t mode)
 		return 0;
 
 	if (mode & (FMODE_READ|FMODE_WRITE)) {
-		check_disk_change(bdev);
+		if (bdev_check_media_change(bdev))
+			floppy_revalidate(bdev->bd_disk);
 		if ((mode & FMODE_WRITE) && fs->write_protected) {
 			err = -EROFS;
 			goto out;
@@ -760,7 +763,6 @@ static const struct block_device_operations floppy_fops = {
 	.ioctl		 = floppy_ioctl,
 	.getgeo		 = floppy_getgeo,
 	.check_events	 = floppy_check_events,
-	.revalidate_disk = floppy_revalidate,
 };
 
 static struct kobject *floppy_find(dev_t dev, int *part, void *data)
Signature · 1 line
2.28.0
Reviewed-by on v1 5/19
#10 lore raw

Re: [PATCH 05/19] swim: use bdev_check_media_change

Looks good,

Quoted 1 line

+static int floppy_revalidate(struct gendisk *disk);

Completely unrelated to this series but, this is the 3rd floppy driver in the series defining it's own floppy_revalidate() and naming it floppy_revalidate().

This makes grepping and reviewing a pain.

#11 lore raw

Re: [PATCH 05/19] swim: use bdev_check_media_change

Quoted 10 lines · Johannes Thumshirn

Looks good,

Quoted 1 line

+static int floppy_revalidate(struct gendisk *disk);

Completely unrelated to this series but, this is the 3rd floppy driver in the series defining it's own floppy_revalidate() and naming it floppy_revalidate().

This makes grepping and reviewing a pain.

Yes, I noticed it as well. They also use the floppy_ prefix for various other methods.

Quoted 1 line

---end quoted text---

#12 lore raw

PATCH 6/19 swim: simplify media change handling

In reply to Christoph Hellwig

floppy_revalidate mostly duplicates work already done in floppy_open despite only beeing called from floppy_open. Remove the function and just clear the ->ejected flag directly under the right condition.

---
 drivers/block/swim.c | 24 ++----------------------
 1 file changed, 2 insertions(+), 22 deletions(-)
drivers/block/swim.c +2 -22
index d4565c555b289f..52dd1efa00f9c5 100644
@@ -217,8 +217,6 @@ extern int swim_read_sector_header(struct swim __iomem *base,
 extern int swim_read_sector_data(struct swim __iomem *base,
 				 unsigned char *data);
 
-static int floppy_revalidate(struct gendisk *disk);
-
 static DEFINE_MUTEX(swim_mutex);
 static inline void set_swim_mode(struct swim __iomem *base, int enable)
 {
@@ -640,8 +638,8 @@ static int floppy_open(struct block_device *bdev, fmode_t mode)
 		return 0;
 
 	if (mode & (FMODE_READ|FMODE_WRITE)) {
-		if (bdev_check_media_change(bdev))
-			floppy_revalidate(bdev->bd_disk);
+		if (bdev_check_media_change(bdev) && fs->disk_in)
+			fs->ejected = 0;
 		if ((mode & FMODE_WRITE) && fs->write_protected) {
 			err = -EROFS;
 			goto out;
@@ -738,24 +736,6 @@ static unsigned int floppy_check_events(struct gendisk *disk,
 	return fs->ejected ? DISK_EVENT_MEDIA_CHANGE : 0;
 }
 
-static int floppy_revalidate(struct gendisk *disk)
-{
-	struct floppy_state *fs = disk->private_data;
-	struct swim __iomem *base = fs->swd->base;
-
-	swim_drive(base, fs->location);
-
-	if (fs->ejected)
-		setup_medium(fs);
-
-	if (!fs->disk_in)
-		swim_motor(base, OFF);
-	else
-		fs->ejected = 0;
-
-	return !fs->disk_in;
-}
-
 static const struct block_device_operations floppy_fops = {
 	.owner		 = THIS_MODULE,
 	.open		 = floppy_unlocked_open,
Signature · 1 line
2.28.0
#14 lore raw

PATCH 7/19 swim3: use bdev_check_media_changed

In reply to Christoph Hellwig

Switch to use bdev_check_media_changed instead of check_disk_change and call floppy_revalidate manually. Given that floppy_revalidate only deals with media change events, the extra call into ->revalidate_disk from bdev_disk_changed is not required either, so stop wiring up the method.

---
 drivers/block/swim3.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/block/swim3.c +2 -2
index aa77eb5fb7de88..c2d922d125e281 100644
@@ -945,7 +945,8 @@ static int floppy_open(struct block_device *bdev, fmode_t mode)
 
 	if (err == 0 && (mode & FMODE_NDELAY) == 0
 	    && (mode & (FMODE_READ|FMODE_WRITE))) {
-		check_disk_change(bdev);
+		if (bdev_check_media_change(bdev))
+			floppy_revalidate(bdev->bd_disk);
 		if (fs->ejected)
 			err = -ENXIO;
 	}
@@ -1055,7 +1056,6 @@ static const struct block_device_operations floppy_fops = {
 	.release	= floppy_release,
 	.ioctl		= floppy_ioctl,
 	.check_events	= floppy_check_events,
-	.revalidate_disk= floppy_revalidate,
 };
 
 static const struct blk_mq_ops swim3_mq_ops = {
Signature · 1 line
2.28.0
#16 lore raw

PATCH 8/19 xsysace: use bdev_check_media_change

In reply to Christoph Hellwig

Switch to use bdev_check_media_change instead of check_disk_change and call ace_revalidate_disk manually. Given that ace_revalidate_disk only deals with media change events, the extra call into ->revalidate_disk from bdev_disk_changed is not required either, so stop wiring up the method.

---
 drivers/block/xsysace.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/block/xsysace.c +2 -2
index 5d8e0ab3f054f5..eefe542f2d9fff 100644
@@ -922,7 +922,8 @@ static int ace_open(struct block_device *bdev, fmode_t mode)
 	ace->users++;
 	spin_unlock_irqrestore(&ace->lock, flags);
 
-	check_disk_change(bdev);
+	if (bdev_check_media_change(bdev))
+		ace_revalidate_disk(bdev->bd_disk);
 	mutex_unlock(&xsysace_mutex);
 
 	return 0;
@@ -966,7 +967,6 @@ static const struct block_device_operations ace_fops = {
 	.open = ace_open,
 	.release = ace_release,
 	.check_events = ace_check_events,
-	.revalidate_disk = ace_revalidate_disk,
 	.getgeo = ace_getgeo,
 };
 
Signature · 1 line
2.28.0
#18 lore raw

PATCH 9/19 xsysace: simplify media change handling

In reply to Christoph Hellwig

Pass a struct ace_device to ace_revalidate_disk, move the media changed check into the one caller that needs it, and give the routine a better name.

---
 drivers/block/xsysace.c | 26 ++++++++++----------------
 1 file changed, 10 insertions(+), 16 deletions(-)
drivers/block/xsysace.c +10 -16
index eefe542f2d9fff..8d581c7536fb51 100644
@@ -888,26 +888,20 @@ static unsigned int ace_check_events(struct gendisk *gd, unsigned int clearing)
 	return ace->media_change ? DISK_EVENT_MEDIA_CHANGE : 0;
 }
 
-static int ace_revalidate_disk(struct gendisk *gd)
+static void ace_media_changed(struct ace_device *ace)
 {
-	struct ace_device *ace = gd->private_data;
 	unsigned long flags;
 
-	dev_dbg(ace->dev, "ace_revalidate_disk()\n");
-
-	if (ace->media_change) {
-		dev_dbg(ace->dev, "requesting cf id and scheduling tasklet\n");
+	dev_dbg(ace->dev, "requesting cf id and scheduling tasklet\n");
 
-		spin_lock_irqsave(&ace->lock, flags);
-		ace->id_req_count++;
-		spin_unlock_irqrestore(&ace->lock, flags);
+	spin_lock_irqsave(&ace->lock, flags);
+	ace->id_req_count++;
+	spin_unlock_irqrestore(&ace->lock, flags);
 
-		tasklet_schedule(&ace->fsm_tasklet);
-		wait_for_completion(&ace->id_completion);
-	}
+	tasklet_schedule(&ace->fsm_tasklet);
+	wait_for_completion(&ace->id_completion);
 
 	dev_dbg(ace->dev, "revalidate complete\n");
-	return ace->id_result;
 }
 
 static int ace_open(struct block_device *bdev, fmode_t mode)
@@ -922,8 +916,8 @@ static int ace_open(struct block_device *bdev, fmode_t mode)
 	ace->users++;
 	spin_unlock_irqrestore(&ace->lock, flags);
 
-	if (bdev_check_media_change(bdev))
-		ace_revalidate_disk(bdev->bd_disk);
+	if (bdev_check_media_change(bdev) && ace->media_change)
+		ace_media_changed(ace);
 	mutex_unlock(&xsysace_mutex);
 
 	return 0;
@@ -1080,7 +1074,7 @@ static int ace_setup(struct ace_device *ace)
 		(unsigned long long) ace->physaddr, ace->baseaddr, ace->irq);
 
 	ace->media_change = 1;
-	ace_revalidate_disk(ace->gd);
+	ace_media_changed(ace);
 
 	/* Make the sysace device 'live' */
 	add_disk(ace->gd);
Signature · 1 line
2.28.0
#19 lore raw

PATCH 10/19 paride/pcd: use bdev_check_media_change

In reply to Christoph Hellwig

The pcd driver does not have a ->revalidate_disk method, so it can just use bdev_check_media_change without any additional changes.

---
 drivers/block/paride/pcd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/paride/pcd.c +1 -1
index 5124eca90e8337..70da8b86ce587d 100644
@@ -233,7 +233,7 @@ static int pcd_block_open(struct block_device *bdev, fmode_t mode)
 	struct pcd_unit *cd = bdev->bd_disk->private_data;
 	int ret;
 
-	check_disk_change(bdev);
+	bdev_check_media_change(bdev);
 
 	mutex_lock(&pcd_mutex);
 	ret = cdrom_open(&cd->info, bdev, mode);
Signature · 1 line
2.28.0
#21 lore raw

PATCH 11/19 gdrom: use bdev_check_media_change

In reply to Christoph Hellwig

The GD-ROM driver does not have a ->revalidate_disk method, so it can just use bdev_check_media_change without any additional changes.

---
 drivers/cdrom/gdrom.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cdrom/gdrom.c +1 -1
index 09b0cd292720fa..9874fc1c815b53 100644
@@ -479,7 +479,7 @@ static int gdrom_bdops_open(struct block_device *bdev, fmode_t mode)
 {
 	int ret;
 
-	check_disk_change(bdev);
+	bdev_check_media_change(bdev);
 
 	mutex_lock(&gdrom_mutex);
 	ret = cdrom_open(gd.cd_info, bdev, mode);
Signature · 1 line
2.28.0
#23 lore raw

Re: [PATCH 11/19] gdrom: use bdev_check_media_change

In reply to Christoph Hellwig
Quoted 1 line · Christoph Hellwig

The GD-ROM driver does not have a ->revalidate_disk method, so it can

      ^^ (sic)

Cheers,
Wol

#24 lore raw

Re: [PATCH 11/19] gdrom: use bdev_check_media_change

Quoted 3 lines · antlists
Quoted 1 line · Christoph Hellwig

The GD-ROM driver does not have a ->revalidate_disk method, so it can

      ^^ (sic)

No, this really is the GD-ROM and not the CD-ROM driver!

#25 lore raw

Re: [PATCH 11/19] gdrom: use bdev_check_media_change

Quoted 7 lines · Christoph Hellwig
Quoted 3 lines · antlists
Quoted 1 line · Christoph Hellwig

The GD-ROM driver does not have a ->revalidate_disk method, so it can

      ^^ (sic)

No, this really is the GD-ROM and not the CD-ROM driver!

Wow. I had to Google to find out what it was.

That says it's proprietary Sega/Nintendo, so dare I suggest you change that to "Sega GD-ROM" or similar to avoid other clueless people like me jumping on it?

Cheers,
Wol

#26 lore raw

PATCH 12/19 ide-cd: use bdev_check_media_changed

In reply to Christoph Hellwig

Switch to use bdev_check_media_changed instead of check_disk_change and call idecd_revalidate_disk manually. Given that idecd_revalidate_disk only re-reads the TOC, and we already do the same at probe time, the extra call into ->revalidate_disk from bdev_disk_changed is not required either, so stop wiring up the method.

---
 drivers/ide/ide-cd.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/ide/ide-cd.c +3 -2
index 212bb2d8bf346a..6a38cbc80aea0d 100644
@@ -56,6 +56,7 @@ static DEFINE_MUTEX(ide_cd_mutex);
 static DEFINE_MUTEX(idecd_ref_mutex);
 
 static void ide_cd_release(struct device *);
+static int idecd_revalidate_disk(struct gendisk *disk);
 
 static struct cdrom_info *ide_cd_get(struct gendisk *disk)
 {
@@ -1611,7 +1612,8 @@ static int idecd_open(struct block_device *bdev, fmode_t mode)
 	struct cdrom_info *info;
 	int rc = -ENXIO;
 
-	check_disk_change(bdev);
+	if (bdev_check_media_change(bdev))
+		idecd_revalidate_disk(bdev->bd_disk);
 
 	mutex_lock(&ide_cd_mutex);
 	info = ide_cd_get(bdev->bd_disk);
@@ -1770,7 +1772,6 @@ static const struct block_device_operations idecd_ops = {
 	.compat_ioctl		= IS_ENABLED(CONFIG_COMPAT) ?
 				  idecd_compat_ioctl : NULL,
 	.check_events		= idecd_check_events,
-	.revalidate_disk	= idecd_revalidate_disk
 };
 
 /* module options */
Signature · 1 line
2.28.0
#28 lore raw

PATCH 13/19 ide-cd: remove idecd_revalidate_disk

In reply to Christoph Hellwig

Just merge the trivial function into its only caller.

---
 drivers/ide/ide-cd.c | 17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)
drivers/ide/ide-cd.c +5 -12
index 6a38cbc80aea0d..25d2d88e82ada0 100644
@@ -56,7 +56,6 @@ static DEFINE_MUTEX(ide_cd_mutex);
 static DEFINE_MUTEX(idecd_ref_mutex);
 
 static void ide_cd_release(struct device *);
-static int idecd_revalidate_disk(struct gendisk *disk);
 
 static struct cdrom_info *ide_cd_get(struct gendisk *disk)
 {
@@ -1612,8 +1611,11 @@ static int idecd_open(struct block_device *bdev, fmode_t mode)
 	struct cdrom_info *info;
 	int rc = -ENXIO;
 
-	if (bdev_check_media_change(bdev))
-		idecd_revalidate_disk(bdev->bd_disk);
+	if (bdev_check_media_change(bdev)) {
+		info = ide_drv_g(bdev->bd_disk, cdrom_info);
+
+		ide_cd_read_toc(info->drive);
+	}
 
 	mutex_lock(&ide_cd_mutex);
 	info = ide_cd_get(bdev->bd_disk);
@@ -1755,15 +1757,6 @@ static unsigned int idecd_check_events(struct gendisk *disk,
 	return cdrom_check_events(&info->devinfo, clearing);
 }
 
-static int idecd_revalidate_disk(struct gendisk *disk)
-{
-	struct cdrom_info *info = ide_drv_g(disk, cdrom_info);
-
-	ide_cd_read_toc(info->drive);
-
-	return  0;
-}
-
 static const struct block_device_operations idecd_ops = {
 	.owner			= THIS_MODULE,
 	.open			= idecd_open,
Signature · 1 line
2.28.0
#30 lore raw

PATCH 14/19 ide-gd: stop using the disk events mechanism

In reply to Christoph Hellwig

ide-gd is only using the disk events mechanism to be able to force an invalidation and partition scan on opening removable media. Just open code the logic without invoving the block layer.

---
 drivers/ide/ide-disk.c   |  5 +----
 drivers/ide/ide-floppy.c |  2 --
 drivers/ide/ide-gd.c     | 48 +++++-----------------------------------
 include/linux/ide.h      |  2 --
 4 files changed, 7 insertions(+), 50 deletions(-)
drivers/ide/ide-disk.c +1 -4
index 1d3407d7e095fa..34b9441084f84f 100644
@@ -739,12 +739,9 @@ static void ide_disk_setup(ide_drive_t *drive)
 	set_wcache(drive, 1);
 
 	if ((drive->dev_flags & IDE_DFLAG_LBA) == 0 &&
-	    (drive->head == 0 || drive->head > 16)) {
+	    (drive->head == 0 || drive->head > 16))
 		printk(KERN_ERR "%s: invalid geometry: %d physical heads?\n",
 			drive->name, drive->head);
-		drive->dev_flags &= ~IDE_DFLAG_ATTACH;
-	} else
-		drive->dev_flags |= IDE_DFLAG_ATTACH;
 }
 
 static void ide_disk_flush(ide_drive_t *drive)
drivers/ide/ide-floppy.c +0 -2
index af7503b47dbe32..f5a2870aaf54bb 100644
@@ -516,8 +516,6 @@ static void ide_floppy_setup(ide_drive_t *drive)
 	(void) ide_floppy_get_capacity(drive);
 
 	ide_proc_register_driver(drive, floppy->driver);
-
-	drive->dev_flags |= IDE_DFLAG_ATTACH;
 }
 
 static void ide_floppy_flush(ide_drive_t *drive)
drivers/ide/ide-gd.c +6 -42

73 lines not on this page · raw message

include/linux/ide.h +0 -2
index a254841bd3156d..62653769509f89 100644
@@ -490,8 +490,6 @@ enum {
 	IDE_DFLAG_NOPROBE		= BIT(9),
 	/* need to do check_media_change() */
 	IDE_DFLAG_REMOVABLE		= BIT(10),
-	/* needed for removable devices */
-	IDE_DFLAG_ATTACH		= BIT(11),
 	IDE_DFLAG_FORCED_GEOM		= BIT(12),
 	/* disallow setting unmask bit */
 	IDE_DFLAG_NO_UNMASK		= BIT(13),
Signature · 1 line
2.28.0
#31 lore raw

PATCH 15/19 md: use bdev_check_media_change

In reply to Christoph Hellwig

The pcd driver does not have a ->revalidate_disk method, so it can just use bdev_check_media_change without any additional changes.

---
 drivers/md/md.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/md.c +1 -1
index 9562ef598ae1f4..27ed61197014ef 100644
@@ -7848,7 +7848,7 @@ static int md_open(struct block_device *bdev, fmode_t mode)
 	atomic_inc(&mddev->openers);
 	mutex_unlock(&mddev->open_mutex);
 
-	check_disk_change(bdev);
+	bdev_check_media_change(bdev);
  out:
 	if (err)
 		mddev_put(mddev);
Signature · 1 line
2.28.0
#33 lore raw

Re: [PATCH 15/19] md: use bdev_check_media_change

In reply to Christoph Hellwig

Hello!

Quoted 1 line · Christoph Hellwig

The pcd driver does not have a ->revalidate_disk method, so it can just

s/pcd/md/?

Quoted 6 lines

use bdev_check_media_change without any additional changes.

---
 drivers/md/md.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

[...]

MBR, Sergei

#34 lore raw

PATCH 16/19 sd: use bdev_check_media_change

In reply to Christoph Hellwig

Switch to use bdev_check_media_change instead of check_disk_change and call cd_revalidate_disk manually. As sd also calls sd_revalidate_disk manually during probe and open, , the extra call into ->revalidate_disk from bdev_disk_changed is not required either, so stop wiring up the method.

---
 drivers/scsi/sd.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/scsi/sd.c +4 -3
index 2bec8cd526164d..d020639c28c6ca 100644
@@ -1381,8 +1381,10 @@ static int sd_open(struct block_device *bdev, fmode_t mode)
 	if (!scsi_block_when_processing_errors(sdev))
 		goto error_out;
 
-	if (sdev->removable || sdkp->write_prot)
-		check_disk_change(bdev);
+	if (sdev->removable || sdkp->write_prot) {
+		if (bdev_check_media_change(bdev))
+			sd_revalidate_disk(bdev->bd_disk);
+	}
 
 	/*
 	 * If the drive is empty, just let the open fail.
@@ -1843,7 +1845,6 @@ static const struct block_device_operations sd_fops = {
 	.compat_ioctl		= sd_compat_ioctl,
 #endif
 	.check_events		= sd_check_events,
-	.revalidate_disk	= sd_revalidate_disk,
 	.unlock_native_capacity	= sd_unlock_native_capacity,
 	.report_zones		= sd_zbc_report_zones,
 	.pr_ops			= &sd_pr_ops,
Signature · 1 line
2.28.0
#36 lore raw

Re: [PATCH 16/19] sd: use bdev_check_media_change

In reply to Christoph Hellwig
Quoted 2 lines · Christoph Hellwig

Switch to use bdev_check_media_change instead of check_disk_change and call cd_revalidate_disk manually. As sd also calls sd_revalidate_disk

s/cd/sd/?

Quoted 16 lines

manually during probe and open, , the extra call into ->revalidate_disk from bdev_disk_changed is not required either, so stop wiring up the method.

---
 drivers/scsi/sd.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/scsi/sd.c +0 -0
index 2bec8cd526164d..d020639c28c6ca 100644
@@ -1381,8 +1381,10 @@ static int sd_open(struct block_device *bdev, fmode_t mode)
 	if (!scsi_block_when_processing_errors(sdev))
 		goto error_out;

drivers/scsi/sd.c static int sd_open(struct block_device *bdev, fmode_t mode) · line 1390

Quoted 10 lines
drivers/scsi/sd.c +4 -2
 
-	if (sdev->removable || sdkp->write_prot)
-		check_disk_change(bdev);
+	if (sdev->removable || sdkp->write_prot) {
+		if (bdev_check_media_change(bdev))
+			sd_revalidate_disk(bdev->bd_disk);
+	}
 
 	/*
 	 * If the drive is empty, just let the open fail.

[...]

MBR, Sergei

#37 lore raw

PATCH 17/19 sr: use bdev_check_media_change

In reply to Christoph Hellwig

Switch to use bdev_check_media_change instead of check_disk_change and call sr_block_revalidate_disk manually. Also add an explicit call to sr_block_revalidate_disk just before disk_add() to ensure we always read check for a ready unit and read the TOC and then stop wiring up ->revalidate_disk.

---
 drivers/scsi/sr.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/scsi/sr.c +4 -2
index 3b3a53c6a0de53..34be94b62523fa 100644
@@ -86,6 +86,7 @@ static int sr_remove(struct device *);
 static blk_status_t sr_init_command(struct scsi_cmnd *SCpnt);
 static int sr_done(struct scsi_cmnd *);
 static int sr_runtime_suspend(struct device *dev);
+static int sr_block_revalidate_disk(struct gendisk *disk);
 
 static const struct dev_pm_ops sr_pm_ops = {
 	.runtime_suspend	= sr_runtime_suspend,
@@ -529,7 +530,8 @@ static int sr_block_open(struct block_device *bdev, fmode_t mode)
 
 	sdev = cd->device;
 	scsi_autopm_get_device(sdev);
-	check_disk_change(bdev);
+	if (bdev_check_media_change(bdev))
+		sr_block_revalidate_disk(bdev->bd_disk);
 
 	mutex_lock(&cd->lock);
 	ret = cdrom_open(&cd->cdi, bdev, mode);
@@ -688,7 +690,6 @@ static const struct block_device_operations sr_bdops =
 	.compat_ioctl	= sr_block_compat_ioctl,
 #endif
 	.check_events	= sr_block_check_events,
-	.revalidate_disk = sr_block_revalidate_disk,
 };
 
 static int sr_open(struct cdrom_device_info *cdi, int purpose)
@@ -802,6 +803,7 @@ static int sr_probe(struct device *dev)
 
 	dev_set_drvdata(dev, cd);
 	disk->flags |= GENHD_FL_REMOVABLE;
+	sr_block_revalidate_disk(disk);
 	device_add_disk(&sdev->sdev_gendev, disk, NULL);
 
 	sdev_printk(KERN_DEBUG, sdev,
Signature · 1 line
2.28.0
#39 lore raw

PATCH 18/19 sr: simplify sr_block_revalidate_disk

In reply to Christoph Hellwig

Both callers have a valid CD struture available, so rely on that instead of getting another reference. Also move the function to avoid a forward declaration.

---
 drivers/scsi/sr.c | 36 +++++++++++++-----------------------
 1 file changed, 13 insertions(+), 23 deletions(-)
drivers/scsi/sr.c +13 -23
index 34be94b62523fa..2b43c0f97442d4 100644
@@ -86,7 +86,6 @@ static int sr_remove(struct device *);
 static blk_status_t sr_init_command(struct scsi_cmnd *SCpnt);
 static int sr_done(struct scsi_cmnd *);
 static int sr_runtime_suspend(struct device *dev);
-static int sr_block_revalidate_disk(struct gendisk *disk);
 
 static const struct dev_pm_ops sr_pm_ops = {
 	.runtime_suspend	= sr_runtime_suspend,
@@ -518,6 +517,17 @@ static blk_status_t sr_init_command(struct scsi_cmnd *SCpnt)
 	return ret;
 }
 
+static void sr_revalidate_disk(struct scsi_cd *cd)
+{
+	struct scsi_sense_hdr sshdr;
+
+	/* if the unit is not ready, nothing more to do */
+	if (scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr))
+		return;
+	sr_cd_check(&cd->cdi);
+	get_sectorsize(cd);
+}
+
 static int sr_block_open(struct block_device *bdev, fmode_t mode)
 {
 	struct scsi_cd *cd;
@@ -531,7 +541,7 @@ static int sr_block_open(struct block_device *bdev, fmode_t mode)
 	sdev = cd->device;
 	scsi_autopm_get_device(sdev);
 	if (bdev_check_media_change(bdev))
-		sr_block_revalidate_disk(bdev->bd_disk);
+		sr_revalidate_disk(cd);
 
 	mutex_lock(&cd->lock);
 	ret = cdrom_open(&cd->cdi, bdev, mode);
@@ -660,26 +670,6 @@ static unsigned int sr_block_check_events(struct gendisk *disk,
 	return ret;
 }
 
-static int sr_block_revalidate_disk(struct gendisk *disk)
-{
-	struct scsi_sense_hdr sshdr;
-	struct scsi_cd *cd;
-
-	cd = scsi_cd_get(disk);
-	if (!cd)
-		return -ENXIO;
-
-	/* if the unit is not ready, nothing more to do */
-	if (scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr))
-		goto out;
-
-	sr_cd_check(&cd->cdi);
-	get_sectorsize(cd);
-out:
-	scsi_cd_put(cd);
-	return 0;
-}
-
 static const struct block_device_operations sr_bdops =
 {
 	.owner		= THIS_MODULE,
@@ -803,7 +793,7 @@ static int sr_probe(struct device *dev)
 
 	dev_set_drvdata(dev, cd);
 	disk->flags |= GENHD_FL_REMOVABLE;
-	sr_block_revalidate_disk(disk);
+	sr_revalidate_disk(cd);
 	device_add_disk(&sdev->sdev_gendev, disk, NULL);
 
 	sdev_printk(KERN_DEBUG, sdev,
Signature · 1 line
2.28.0
#41 lore raw

PATCH 19/19 block: remove check_disk_change

In reply to Christoph Hellwig

Remove the now unused check_disk_change helper.

---
 fs/block_dev.c        | 20 --------------------
 include/linux/genhd.h |  1 -
 2 files changed, 21 deletions(-)
fs/block_dev.c +0 -20
index 37cb809b217926..c6baae5b85060c 100644
@@ -1339,26 +1339,6 @@ void revalidate_disk_size(struct gendisk *disk, bool verbose)
 }
 EXPORT_SYMBOL(revalidate_disk_size);
 
-/*
- * This routine checks whether a removable media has been changed,
- * and invalidates all buffer-cache-entries in that case. This
- * is a relatively slow routine, so we have to try to minimize using
- * it. Thus it is called only upon a 'mount' or 'open'. This
- * is the best way of combining speed and utility, I think.
- * People changing diskettes in the middle of an operation deserve
- * to lose :-)
- */
-int check_disk_change(struct block_device *bdev)
-{
-	if (!bdev_check_media_change(bdev))
-		return 0;
-	if (bdev->bd_disk->fops->revalidate_disk)
-		bdev->bd_disk->fops->revalidate_disk(bdev->bd_disk);
-	return 1;
-}
-
-EXPORT_SYMBOL(check_disk_change);
-
 void bd_set_nr_sectors(struct block_device *bdev, sector_t sectors)
 {
 	spin_lock(&bdev->bd_size_lock);
include/linux/genhd.h +0 -1
index 322d48a207728a..1c97cf84f011a7 100644
@@ -370,7 +370,6 @@ int register_blkdev(unsigned int major, const char *name);
 void unregister_blkdev(unsigned int major, const char *name);
 
 void revalidate_disk_size(struct gendisk *disk, bool verbose);
-int check_disk_change(struct block_device *bdev);
 bool bdev_check_media_change(struct block_device *bdev);
 int __invalidate_device(struct block_device *bdev, bool kill_dirty);
 void bd_set_nr_sectors(struct block_device *bdev, sector_t sectors);
Signature · 1 line
2.28.0
#43 lore raw
In reply to Christoph Hellwig
Quoted 7 lines · Christoph Hellwig

Hi Jens,

this series replaced the not very nice check_disk_change() function with a new bdev_media_changed that avoids having the ->revalidate_disk call at its end. As a result ->revalidate_disk can be removed from a lot of drivers.

For over 20 years the sg driver has been carrying this snippet that hangs off the completion callback:

       if (driver_stat & DRIVER_SENSE) {
                struct scsi_sense_hdr ssh;

                if (scsi_normalize_sense(sbp, sense_len, &ssh)) {
                        if (!scsi_sense_is_deferred(&ssh)) {
                                if (ssh.sense_key == UNIT_ATTENTION) {
                                        if (sdp->device->removable)
                                                sdp->device->changed = 1;
                                }
                        }
                }
        }

Is it needed? The unit attention (UA) may not be associated with the device changing. Shouldn't the SCSI mid-level monitor UAs if they impact the state of a scsi_device object?

Doug Gilbert

#44 lore raw
Quoted 25 lines · Douglas Gilbert
Quoted 7 lines · Christoph Hellwig

Hi Jens,

this series replaced the not very nice check_disk_change() function with a new bdev_media_changed that avoids having the ->revalidate_disk call at its end.  As a result ->revalidate_disk can be removed from a lot of drivers.

For over 20 years the sg driver has been carrying this snippet that hangs off the completion callback:

       if (driver_stat & DRIVER_SENSE) {
                struct scsi_sense_hdr ssh;

                if (scsi_normalize_sense(sbp, sense_len, &ssh)) { if (!scsi_sense_is_deferred(&ssh)) { if (ssh.sense_key == UNIT_ATTENTION) { if (sdp->device->removable) sdp->device->changed = 1; }
                        }
                }
        }

Quoted 4 lines

Is it needed? The unit attention (UA) may not be associated with the device changing. Shouldn't the SCSI mid-level monitor UAs if they impact the state of a scsi_device object?

We do; check scsi_io_completion_action() in drivers/scsi/scsi_lib.c So I don't think you'd need to keep it in sg.c.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer