diff mbox

[RESEND] floppy: fix open(O_ACCMODE) for ioctl-only open

Message ID alpine.LNX.2.00.1606301317290.6874@cbobk.fhfr.pm (mailing list archive)
State New, archived
Headers show

Commit Message

Jiri Kosina June 30, 2016, 11:18 a.m. UTC
From: Jiri Kosina <jkosina@suse.cz>

Commit 09954bad4 ("floppy: refactor open() flags handling"), as a 
side-effect, causes open(/dev/fdX, O_ACCMODE) to fail. It turns out that 
this is being used setfdprm userspace for ioctl-only open().

Reintroduce back the original behavior wrt !(FMODE_READ|FMODE_WRITE) 
modes, while still keeping the original O_NDELAY bug fixed.

Cc: stable@vger.kernel.org # v4.5+
Reported-by: Wim Osterholt <wim@djo.tudelft.nl>
Tested-by: Wim Osterholt <wim@djo.tudelft.nl>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
---

Jens, this should preferably go into 4.7-rcX and to -stable as well.

 drivers/block/floppy.c | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)

Comments

wim July 25, 2016, 6:09 p.m. UTC | #1
On Thu, Jun 30, 2016 at 01:18:00PM +0200, Jiri Kosina wrote:
> From: Jiri Kosina <jkosina@suse.cz>
> 
> Commit 09954bad4 ("floppy: refactor open() flags handling"), as a 
> side-effect, causes open(/dev/fdX, O_ACCMODE) to fail. It turns out that 
> this is being used setfdprm userspace for ioctl-only open().
> 
> Reintroduce back the original behavior wrt !(FMODE_READ|FMODE_WRITE) 
> modes, while still keeping the original O_NDELAY bug fixed.
> 
> Cc: stable@vger.kernel.org # v4.5+
> Reported-by: Wim Osterholt <wim@djo.tudelft.nl>
> Tested-by: Wim Osterholt <wim@djo.tudelft.nl>
> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
> ---
> 
> Jens, this should preferably go into 4.7-rcX and to -stable as well.
> 

How come this patch is still not in the recent release of 4.7 ?



Regards, Wim.



--
To unsubscribe from this list: send the line "unsubscribe linux-block" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Jens Axboe July 25, 2016, 8:48 p.m. UTC | #2
On 06/30/2016 05:18 AM, Jiri Kosina wrote:
> From: Jiri Kosina <jkosina@suse.cz>
>
> Commit 09954bad4 ("floppy: refactor open() flags handling"), as a
> side-effect, causes open(/dev/fdX, O_ACCMODE) to fail. It turns out that
> this is being used setfdprm userspace for ioctl-only open().
>
> Reintroduce back the original behavior wrt !(FMODE_READ|FMODE_WRITE)
> modes, while still keeping the original O_NDELAY bug fixed.
>
> Cc: stable@vger.kernel.org # v4.5+
> Reported-by: Wim Osterholt <wim@djo.tudelft.nl>
> Tested-by: Wim Osterholt <wim@djo.tudelft.nl>
> Signed-off-by: Jiri Kosina <jkosina@suse.cz>

Added for this series, thanks.
Jiri Kosina Jan. 19, 2021, 3:53 p.m. UTC | #3
On Mon, 25 Jul 2016, Jens Axboe wrote:

> > From: Jiri Kosina <jkosina@suse.cz>
> >
> > Commit 09954bad4 ("floppy: refactor open() flags handling"), as a
> > side-effect, causes open(/dev/fdX, O_ACCMODE) to fail. It turns out that
> > this is being used setfdprm userspace for ioctl-only open().
> >
> > Reintroduce back the original behavior wrt !(FMODE_READ|FMODE_WRITE)
> > modes, while still keeping the original O_NDELAY bug fixed.
> >
> > Cc: stable@vger.kernel.org # v4.5+
> > Reported-by: Wim Osterholt <wim@djo.tudelft.nl>
> > Tested-by: Wim Osterholt <wim@djo.tudelft.nl>
> > Signed-off-by: Jiri Kosina <jkosina@suse.cz>
> 
> Added for this series, thanks.

[ CCing Denis too ]

Let me revive this 4 years old thread.

I've just now noticed that instead of my patch above being merged, what 
happened instead was

	commit f2791e7eadf437633f30faa51b30878cf15650be
	Author: Jens Axboe <axboe@fb.com>
	Date:   Thu Aug 25 08:56:51 2016 -0600

	    Revert "floppy: refactor open() flags handling"
    
	    This reverts commit 09954bad448791ef01202351d437abdd9497a804.


which was plain revert of 09954bad4 (without any further explanation), 
which in turn reintroduced the O_NDELAY issue, and I've just been hit by 
it again.

I am not able to find any e-mail thread that'd indicate why ultimately 
revert happened, instead of mergin my fix.
Jens, do you have any idea?

Thanks,
Denis Efremov (Oracle) Jan. 21, 2021, 4:44 a.m. UTC | #4
Hi,

On 1/19/21 6:53 PM, Jiri Kosina wrote:
> On Mon, 25 Jul 2016, Jens Axboe wrote:
> 
>>> From: Jiri Kosina <jkosina@suse.cz>
>>>
>>> Commit 09954bad4 ("floppy: refactor open() flags handling"), as a
>>> side-effect, causes open(/dev/fdX, O_ACCMODE) to fail. It turns out that
>>> this is being used setfdprm userspace for ioctl-only open().
>>>
>>> Reintroduce back the original behavior wrt !(FMODE_READ|FMODE_WRITE)
>>> modes, while still keeping the original O_NDELAY bug fixed.
>>>
>>> Cc: stable@vger.kernel.org # v4.5+
>>> Reported-by: Wim Osterholt <wim@djo.tudelft.nl>
>>> Tested-by: Wim Osterholt <wim@djo.tudelft.nl>
>>> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
>>
>> Added for this series, thanks.
> 
> [ CCing Denis too ]
> 
> Let me revive this 4 years old thread.
> 
> I've just now noticed that instead of my patch above being merged, what 
> happened instead was
> 
> 	commit f2791e7eadf437633f30faa51b30878cf15650be
> 	Author: Jens Axboe <axboe@fb.com>
> 	Date:   Thu Aug 25 08:56:51 2016 -0600
> 
> 	    Revert "floppy: refactor open() flags handling"
>     
> 	    This reverts commit 09954bad448791ef01202351d437abdd9497a804.
> 
> 
> which was plain revert of 09954bad4 (without any further explanation), 
> which in turn reintroduced the O_NDELAY issue, and I've just been hit by 
> it again.
> 
> I am not able to find any e-mail thread that'd indicate why ultimately 
> revert happened, instead of mergin my fix.

I think it's hard to recall the exact reasons after so many years.
I'll send a patch today based on this one.

Best Regards,
Denis
Jiri Kosina Jan. 21, 2021, 10:25 a.m. UTC | #5
On Thu, 21 Jan 2021, Denis Efremov wrote:

> I think it's hard to recall the exact reasons after so many years. 

Yeah, I guess so :)

> I'll send a patch today based on this one.

I am currently waiting for confirmation by the original reporter that the 
patch below fixes the issue.



From: Jiri Kosina <jkosina@suse.cz>
Subject: [PATCH] floppy: reintroduce O_NDELAY fix

Originally fixed in 09954bad4 ("floppy: refactor open() flags handling")
then reverted for unknown reason in f2791e7eadf437 instead of taking
the open(O_ACCMODE) for ioctl-only open fix, which had the changelog below

====
Commit 09954bad4 ("floppy: refactor open() flags handling"), as a
side-effect, causes open(/dev/fdX, O_ACCMODE) to fail. It turns out that
this is being used setfdprm userspace for ioctl-only open().

Reintroduce back the original behavior wrt !(FMODE_READ|FMODE_WRITE)
modes, while still keeping the original O_NDELAY bug fixed.

Cc: stable@vger.kernel.org # v4.5+
Reported-by: Wim Osterholt <wim@djo.tudelft.nl>
Tested-by: Wim Osterholt <wim@djo.tudelft.nl>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
=====

Fixes: 09954bad4 ("floppy: refactor open() flags handling")
Fixes: f2791e7ead ("Revert "floppy: refactor open() flags handling"")
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
---
 drivers/block/floppy.c | 29 ++++++++++++++---------------
 1 file changed, 14 insertions(+), 15 deletions(-)

diff --git a/drivers/block/floppy.c b/drivers/block/floppy.c
index dfe1dfc901cc..bda9417aa0a8 100644
--- a/drivers/block/floppy.c
+++ b/drivers/block/floppy.c
@@ -4121,23 +4121,22 @@ static int floppy_open(struct block_device *bdev, fmode_t mode)
 	if (fdc_state[FDC(drive)].rawcmd == 1)
 		fdc_state[FDC(drive)].rawcmd = 2;
 
-	if (!(mode & FMODE_NDELAY)) {
-		if (mode & (FMODE_READ|FMODE_WRITE)) {
-			drive_state[drive].last_checked = 0;
-			clear_bit(FD_OPEN_SHOULD_FAIL_BIT,
-				  &drive_state[drive].flags);
-			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))
-				goto out;
-		}
-		res = -EROFS;
-		if ((mode & FMODE_WRITE) &&
-		    !test_bit(FD_DISK_WRITABLE_BIT, &drive_state[drive].flags))
+	if (mode & (FMODE_READ|FMODE_WRITE)) {
+		UDRS->last_checked = 0;
+		clear_bit(FD_OPEN_SHOULD_FAIL_BIT, &drive_state[drive].flags);
+		check_disk_change(bdev);
+		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))
 			goto out;
 	}
+
+	res = -EROFS;
+
+	if ((mode & FMODE_WRITE) &&
+			!test_bit(FD_DISK_WRITABLE_BIT, &drive_state[drive].flags))
+		goto out;
+
 	mutex_unlock(&open_lock);
 	mutex_unlock(&floppy_mutex);
 	return 0;
kernel test robot Jan. 21, 2021, 1:28 p.m. UTC | #6
Hi Jiri,

I love your patch! Yet something to improve:

[auto build test ERROR on block/for-next]
[also build test ERROR on linux/master linus/master v5.11-rc4 next-20210121]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Jiri-Kosina/floppy-reintroduce-O_NDELAY-fix/20210121-182951
base:   https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux-block.git for-next
config: sparc64-randconfig-r033-20210121 (attached as .config)
compiler: sparc64-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/0day-ci/linux/commit/605da67173ab7c362845b2f74c2914bfcec6db2e
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Jiri-Kosina/floppy-reintroduce-O_NDELAY-fix/20210121-182951
        git checkout 605da67173ab7c362845b2f74c2914bfcec6db2e
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=sparc64 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   In file included from arch/sparc/include/asm/floppy.h:5,
                    from drivers/block/floppy.c:251:
   arch/sparc/include/asm/floppy_64.h:200:13: warning: no previous prototype for 'sparc_floppy_irq' [-Wmissing-prototypes]
     200 | irqreturn_t sparc_floppy_irq(int irq, void *dev_cookie)
         |             ^~~~~~~~~~~~~~~~
   In file included from arch/sparc/include/asm/floppy.h:5,
                    from drivers/block/floppy.c:251:
   arch/sparc/include/asm/floppy_64.h:437:6: warning: no previous prototype for 'sun_pci_fd_dma_callback' [-Wmissing-prototypes]
     437 | void sun_pci_fd_dma_callback(struct ebus_dma_info *p, int event, void *cookie)
         |      ^~~~~~~~~~~~~~~~~~~~~~~
   drivers/block/floppy.c: In function 'floppy_open':
>> drivers/block/floppy.c:4125:3: error: 'UDRS' undeclared (first use in this function)
    4125 |   UDRS->last_checked = 0;
         |   ^~~~
   drivers/block/floppy.c:4125:3: note: each undeclared identifier is reported only once for each function it appears in
>> drivers/block/floppy.c:4127:3: error: implicit declaration of function 'check_disk_change'; did you mean 'bdev_disk_changed'? [-Werror=implicit-function-declaration]
    4127 |   check_disk_change(bdev);
         |   ^~~~~~~~~~~~~~~~~
         |   bdev_disk_changed
   cc1: some warnings being treated as errors

Kconfig warnings: (for reference only)
   WARNING: unmet direct dependencies detected for COMPAT_BINFMT_ELF
   Depends on COMPAT && BINFMT_ELF
   Selected by
   - COMPAT && SPARC64
   WARNING: unmet direct dependencies detected for FRAME_POINTER
   Depends on DEBUG_KERNEL && (M68K || UML || SUPERH) || ARCH_WANT_FRAME_POINTERS || MCOUNT
   Selected by
   - LOCKDEP && DEBUG_KERNEL && LOCK_DEBUGGING_SUPPORT && !MIPS && !PPC && !ARM && !S390 && !MICROBLAZE && !ARC && !X86


vim +/UDRS +4125 drivers/block/floppy.c

  4052	
  4053	/*
  4054	 * floppy_open check for aliasing (/dev/fd0 can be the same as
  4055	 * /dev/PS0 etc), and disallows simultaneous access to the same
  4056	 * drive with different device numbers.
  4057	 */
  4058	static int floppy_open(struct block_device *bdev, fmode_t mode)
  4059	{
  4060		int drive = (long)bdev->bd_disk->private_data;
  4061		int old_dev, new_dev;
  4062		int try;
  4063		int res = -EBUSY;
  4064		char *tmp;
  4065	
  4066		mutex_lock(&floppy_mutex);
  4067		mutex_lock(&open_lock);
  4068		old_dev = drive_state[drive].fd_device;
  4069		if (opened_bdev[drive] && opened_bdev[drive] != bdev)
  4070			goto out2;
  4071	
  4072		if (!drive_state[drive].fd_ref && (drive_params[drive].flags & FD_BROKEN_DCL)) {
  4073			set_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags);
  4074			set_bit(FD_VERIFY_BIT, &drive_state[drive].flags);
  4075		}
  4076	
  4077		drive_state[drive].fd_ref++;
  4078	
  4079		opened_bdev[drive] = bdev;
  4080	
  4081		res = -ENXIO;
  4082	
  4083		if (!floppy_track_buffer) {
  4084			/* if opening an ED drive, reserve a big buffer,
  4085			 * else reserve a small one */
  4086			if ((drive_params[drive].cmos == 6) || (drive_params[drive].cmos == 5))
  4087				try = 64;	/* Only 48 actually useful */
  4088			else
  4089				try = 32;	/* Only 24 actually useful */
  4090	
  4091			tmp = (char *)fd_dma_mem_alloc(1024 * try);
  4092			if (!tmp && !floppy_track_buffer) {
  4093				try >>= 1;	/* buffer only one side */
  4094				INFBOUND(try, 16);
  4095				tmp = (char *)fd_dma_mem_alloc(1024 * try);
  4096			}
  4097			if (!tmp && !floppy_track_buffer)
  4098				fallback_on_nodma_alloc(&tmp, 2048 * try);
  4099			if (!tmp && !floppy_track_buffer) {
  4100				DPRINT("Unable to allocate DMA memory\n");
  4101				goto out;
  4102			}
  4103			if (floppy_track_buffer) {
  4104				if (tmp)
  4105					fd_dma_mem_free((unsigned long)tmp, try * 1024);
  4106			} else {
  4107				buffer_min = buffer_max = -1;
  4108				floppy_track_buffer = tmp;
  4109				max_buffer_sectors = try;
  4110			}
  4111		}
  4112	
  4113		new_dev = MINOR(bdev->bd_dev);
  4114		drive_state[drive].fd_device = new_dev;
  4115		set_capacity(disks[drive][ITYPE(new_dev)], floppy_sizes[new_dev]);
  4116		if (old_dev != -1 && old_dev != new_dev) {
  4117			if (buffer_drive == drive)
  4118				buffer_track = -1;
  4119		}
  4120	
  4121		if (fdc_state[FDC(drive)].rawcmd == 1)
  4122			fdc_state[FDC(drive)].rawcmd = 2;
  4123	
  4124		if (mode & (FMODE_READ|FMODE_WRITE)) {
> 4125			UDRS->last_checked = 0;
  4126			clear_bit(FD_OPEN_SHOULD_FAIL_BIT, &drive_state[drive].flags);
> 4127			check_disk_change(bdev);
  4128			if (test_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags))
  4129				goto out;
  4130			if (test_bit(FD_OPEN_SHOULD_FAIL_BIT, &drive_state[drive].flags))
  4131				goto out;
  4132		}
  4133	
  4134		res = -EROFS;
  4135	
  4136		if ((mode & FMODE_WRITE) &&
  4137				!test_bit(FD_DISK_WRITABLE_BIT, &drive_state[drive].flags))
  4138			goto out;
  4139	
  4140		mutex_unlock(&open_lock);
  4141		mutex_unlock(&floppy_mutex);
  4142		return 0;
  4143	out:
  4144		drive_state[drive].fd_ref--;
  4145	
  4146		if (!drive_state[drive].fd_ref)
  4147			opened_bdev[drive] = NULL;
  4148	out2:
  4149		mutex_unlock(&open_lock);
  4150		mutex_unlock(&floppy_mutex);
  4151		return res;
  4152	}
  4153	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
Jiri Kosina Jan. 21, 2021, 2:44 p.m. UTC | #7
On Thu, 21 Jan 2021, Jiri Kosina wrote:

> I am currently waiting for confirmation by the original reporter that the 
> patch below fixes the issue.

... a now a patch that actually compiles :) (made a mistake when 
forward-porting from the older kernel on which this has been reported).



From: Jiri Kosina <jkosina@suse.cz>
Subject: [PATCH v2] floppy: reintroduce O_NDELAY fix

Originally fixed in 09954bad4 ("floppy: refactor open() flags handling")
then reverted for unknown reason in f2791e7eadf437 instead of taking
the open(O_ACCMODE) for ioctl-only open fix, which had the changelog below

====
Commit 09954bad4 ("floppy: refactor open() flags handling"), as a
side-effect, causes open(/dev/fdX, O_ACCMODE) to fail. It turns out that
this is being used setfdprm userspace for ioctl-only open().

Reintroduce back the original behavior wrt !(FMODE_READ|FMODE_WRITE)
modes, while still keeping the original O_NDELAY bug fixed.

Cc: stable@vger.kernel.org # v4.5+
Reported-by: Wim Osterholt <wim@djo.tudelft.nl>
Tested-by: Wim Osterholt <wim@djo.tudelft.nl>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
=====

Fixes: 09954bad4 ("floppy: refactor open() flags handling")
Fixes: f2791e7ead ("Revert "floppy: refactor open() flags handling"")
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
---

v1 -> v2: fix build issue due to bad forward-port

 drivers/block/floppy.c | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)

diff --git a/drivers/block/floppy.c b/drivers/block/floppy.c
index dfe1dfc901cc..f9e839c8c5aa 100644
--- a/drivers/block/floppy.c
+++ b/drivers/block/floppy.c
@@ -4121,23 +4121,23 @@ static int floppy_open(struct block_device *bdev, fmode_t mode)
 	if (fdc_state[FDC(drive)].rawcmd == 1)
 		fdc_state[FDC(drive)].rawcmd = 2;
 
-	if (!(mode & FMODE_NDELAY)) {
-		if (mode & (FMODE_READ|FMODE_WRITE)) {
-			drive_state[drive].last_checked = 0;
-			clear_bit(FD_OPEN_SHOULD_FAIL_BIT,
-				  &drive_state[drive].flags);
-			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))
-				goto out;
-		}
-		res = -EROFS;
-		if ((mode & FMODE_WRITE) &&
-		    !test_bit(FD_DISK_WRITABLE_BIT, &drive_state[drive].flags))
+	if (mode & (FMODE_READ|FMODE_WRITE)) {
+		UDRS->last_checked = 0;
+		clear_bit(FD_OPEN_SHOULD_FAIL_BIT, &drive_state[drive].flags);
+		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))
 			goto out;
 	}
+
+	res = -EROFS;
+
+	if ((mode & FMODE_WRITE) &&
+			!test_bit(FD_DISK_WRITABLE_BIT, &drive_state[drive].flags))
+		goto out;
+
 	mutex_unlock(&open_lock);
 	mutex_unlock(&floppy_mutex);
 	return 0;
Denis Efremov (Oracle) Jan. 21, 2021, 2:45 p.m. UTC | #8
On 1/21/21 1:25 PM, Jiri Kosina wrote:
> On Thu, 21 Jan 2021, Denis Efremov wrote:
> 
>> I think it's hard to recall the exact reasons after so many years. 
> 
> Yeah, I guess so :)
> 
>> I'll send a patch today based on this one.
> 
> I am currently waiting for confirmation by the original reporter that the 
> patch below fixes the issue.
> 
> 
> 
> From: Jiri Kosina <jkosina@suse.cz>
> Subject: [PATCH] floppy: reintroduce O_NDELAY fix
> 
> Originally fixed in 09954bad4 ("floppy: refactor open() flags handling")
> then reverted for unknown reason in f2791e7eadf437 instead of taking
> the open(O_ACCMODE) for ioctl-only open fix, which had the changelog below
> 
> ====
> Commit 09954bad4 ("floppy: refactor open() flags handling"), as a
> side-effect, causes open(/dev/fdX, O_ACCMODE) to fail. It turns out that
> this is being used setfdprm userspace for ioctl-only open().
> 
> Reintroduce back the original behavior wrt !(FMODE_READ|FMODE_WRITE)
> modes, while still keeping the original O_NDELAY bug fixed.
> 
> Cc: stable@vger.kernel.org # v4.5+
> Reported-by: Wim Osterholt <wim@djo.tudelft.nl>
> Tested-by: Wim Osterholt <wim@djo.tudelft.nl>
> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
> =====
> 
> Fixes: 09954bad4 ("floppy: refactor open() flags handling")
> Fixes: f2791e7ead ("Revert "floppy: refactor open() flags handling"")
> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
> ---
>  drivers/block/floppy.c | 29 ++++++++++++++---------------
>  1 file changed, 14 insertions(+), 15 deletions(-)
> 
> diff --git a/drivers/block/floppy.c b/drivers/block/floppy.c
> index dfe1dfc901cc..bda9417aa0a8 100644
> --- a/drivers/block/floppy.c
> +++ b/drivers/block/floppy.c
> @@ -4121,23 +4121,22 @@ static int floppy_open(struct block_device *bdev, fmode_t mode)
>  	if (fdc_state[FDC(drive)].rawcmd == 1)
>  		fdc_state[FDC(drive)].rawcmd = 2;
>  
> -	if (!(mode & FMODE_NDELAY)) {
> -		if (mode & (FMODE_READ|FMODE_WRITE)) {
> -			drive_state[drive].last_checked = 0;
> -			clear_bit(FD_OPEN_SHOULD_FAIL_BIT,
> -				  &drive_state[drive].flags);
> -			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))
> -				goto out;
> -		}
> -		res = -EROFS;
> -		if ((mode & FMODE_WRITE) &&
> -		    !test_bit(FD_DISK_WRITABLE_BIT, &drive_state[drive].flags))
> +	if (mode & (FMODE_READ|FMODE_WRITE)) {


As the bot points out this was refactored a bit in:
8d9d34e25a37 ("floppy: cleanup: expand macro UDRS")
4a6f3d480edc ("floppy: use bdev_check_media_change")

Should be something like:
+               drive_state[drive].last_checked = 0;
+               clear_bit(FD_OPEN_SHOULD_FAIL_BIT,
+                         &drive_state[drive].flags);
+               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))
>  			goto out;
>  	}
> +
> +	res = -EROFS;
> +
> +	if ((mode & FMODE_WRITE) &&
> +			!test_bit(FD_DISK_WRITABLE_BIT, &drive_state[drive].flags))
> +		goto out;
> +
>  	mutex_unlock(&open_lock);
>  	mutex_unlock(&floppy_mutex);
>  	return 0;
>
Denis Efremov (Oracle) Jan. 21, 2021, 3:02 p.m. UTC | #9
On 1/21/21 5:44 PM, Jiri Kosina wrote:
> On Thu, 21 Jan 2021, Jiri Kosina wrote:
> 
>> I am currently waiting for confirmation by the original reporter that the 
>> patch below fixes the issue.
> 
> ... a now a patch that actually compiles :) (made a mistake when 
> forward-porting from the older kernel on which this has been reported).

Oh, sorry for the last message (forgot to check the inbox before hitting
the send button). I'll test the patch. A couple of nitpicks below.

> 
> From: Jiri Kosina <jkosina@suse.cz>
> Subject: [PATCH v2] floppy: reintroduce O_NDELAY fix
> 
> Originally fixed in 09954bad4 ("floppy: refactor open() flags handling")
> then reverted for unknown reason in f2791e7eadf437 instead of taking
> the open(O_ACCMODE) for ioctl-only open fix, which had the changelog below
> 
> ====
> Commit 09954bad4 ("floppy: refactor open() flags handling"), as a
> side-effect, causes open(/dev/fdX, O_ACCMODE) to fail. It turns out that
> this is being used setfdprm userspace for ioctl-only open().
> 
> Reintroduce back the original behavior wrt !(FMODE_READ|FMODE_WRITE)
> modes, while still keeping the original O_NDELAY bug fixed.
> 
> Cc: stable@vger.kernel.org # v4.5+

Are you sure that it's not worth to backport it to LTS v4.4?
Because f2791e7ead is just a revert and 09954bad4 is not
presented in v4.4 I'm not sure what fixes tag is better to
use in this case.

> Reported-by: Wim Osterholt <wim@djo.tudelft.nl>
> Tested-by: Wim Osterholt <wim@djo.tudelft.nl>
> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
> =====
> 
> Fixes: 09954bad4 ("floppy: refactor open() flags handling")
> Fixes: f2791e7ead ("Revert "floppy: refactor open() flags handling"")
> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
> ---
> 
> v1 -> v2: fix build issue due to bad forward-port
> 
>  drivers/block/floppy.c | 30 +++++++++++++++---------------
>  1 file changed, 15 insertions(+), 15 deletions(-)
> 
> diff --git a/drivers/block/floppy.c b/drivers/block/floppy.c
> index dfe1dfc901cc..f9e839c8c5aa 100644
> --- a/drivers/block/floppy.c
> +++ b/drivers/block/floppy.c
> @@ -4121,23 +4121,23 @@ static int floppy_open(struct block_device *bdev, fmode_t mode)
>  	if (fdc_state[FDC(drive)].rawcmd == 1)
>  		fdc_state[FDC(drive)].rawcmd = 2;
>  
> -	if (!(mode & FMODE_NDELAY)) {
> -		if (mode & (FMODE_READ|FMODE_WRITE)) {
> -			drive_state[drive].last_checked = 0;
> -			clear_bit(FD_OPEN_SHOULD_FAIL_BIT,
> -				  &drive_state[drive].flags);
> -			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))
> -				goto out;
> -		}
> -		res = -EROFS;
> -		if ((mode & FMODE_WRITE) &&
> -		    !test_bit(FD_DISK_WRITABLE_BIT, &drive_state[drive].flags))
> +	if (mode & (FMODE_READ|FMODE_WRITE)) {
> +		UDRS->last_checked = 0;

UDRS will still break the compilation here.

> +		clear_bit(FD_OPEN_SHOULD_FAIL_BIT, &drive_state[drive].flags);
> +		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))
>  			goto out;
>  	}
> +
> +	res = -EROFS;
> +
> +	if ((mode & FMODE_WRITE) &&
> +			!test_bit(FD_DISK_WRITABLE_BIT, &drive_state[drive].flags))
> +		goto out;
> +
>  	mutex_unlock(&open_lock);
>  	mutex_unlock(&floppy_mutex);
>  	return 0;
>
Jiri Kosina Jan. 21, 2021, 3:05 p.m. UTC | #10
On Thu, 21 Jan 2021, Denis Efremov wrote:

> > From: Jiri Kosina <jkosina@suse.cz>
> > Subject: [PATCH v2] floppy: reintroduce O_NDELAY fix
> > 
> > Originally fixed in 09954bad4 ("floppy: refactor open() flags handling")
> > then reverted for unknown reason in f2791e7eadf437 instead of taking
> > the open(O_ACCMODE) for ioctl-only open fix, which had the changelog below
> > 
> > ====
> > Commit 09954bad4 ("floppy: refactor open() flags handling"), as a
> > side-effect, causes open(/dev/fdX, O_ACCMODE) to fail. It turns out that
> > this is being used setfdprm userspace for ioctl-only open().
> > 
> > Reintroduce back the original behavior wrt !(FMODE_READ|FMODE_WRITE)
> > modes, while still keeping the original O_NDELAY bug fixed.
> > 
> > Cc: stable@vger.kernel.org # v4.5+
> 
> Are you sure that it's not worth to backport it to LTS v4.4? Because 
> f2791e7ead is just a revert and 09954bad4 is not presented in v4.4 I'm 
> not sure what fixes tag is better to use in this case.

You are right; I'll drop the '4.5+' indicator and will backport it once/if 
it hits Linus' tree.

> > +	if (mode & (FMODE_READ|FMODE_WRITE)) {
> > +		UDRS->last_checked = 0;
> 
> UDRS will still break the compilation here.

Doh, forgot to refresh before sending, sorry for the noise.
I'll send the final version once I get confirmation from the reporter that 
it's fixing the issue properly, add his Reported-by: etc.

Thanks,
diff mbox

Patch

diff --git a/drivers/block/floppy.c b/drivers/block/floppy.c
index 84708a5..a1dcf12 100644
--- a/drivers/block/floppy.c
+++ b/drivers/block/floppy.c
@@ -3663,11 +3663,6 @@  static int floppy_open(struct block_device *bdev, fmode_t mode)
 
 	opened_bdev[drive] = bdev;
 
-	if (!(mode & (FMODE_READ|FMODE_WRITE))) {
-		res = -EINVAL;
-		goto out;
-	}
-
 	res = -ENXIO;
 
 	if (!floppy_track_buffer) {
@@ -3711,13 +3706,15 @@  static int floppy_open(struct block_device *bdev, fmode_t mode)
 	if (UFDCS->rawcmd == 1)
 		UFDCS->rawcmd = 2;
 
-	UDRS->last_checked = 0;
-	clear_bit(FD_OPEN_SHOULD_FAIL_BIT, &UDRS->flags);
-	check_disk_change(bdev);
-	if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags))
-		goto out;
-	if (test_bit(FD_OPEN_SHOULD_FAIL_BIT, &UDRS->flags))
-		goto out;
+	if (mode & (FMODE_READ|FMODE_WRITE)) {
+		UDRS->last_checked = 0;
+		clear_bit(FD_OPEN_SHOULD_FAIL_BIT, &UDRS->flags);
+		check_disk_change(bdev);
+		if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags))
+			goto out;
+		if (test_bit(FD_OPEN_SHOULD_FAIL_BIT, &UDRS->flags))
+			goto out;
+	}
 
 	res = -EROFS;