Skip to contentSkip to conversation

scsi: st: skip buffer flush for information ioctls when there is no buffering

PATCH In review David Jeffery 2 participants 4 messages Oct 23, 2025 – Oct 24, 2025

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

PATCH 1/1 scsi: st: skip buffer flush for information ioctls when there is no buffering

With commit 9604eea5bd3a ("scsi: st: Add third party poweron reset handling") some customer tape applications fail from being unable to complete ioctls to verify ID information for the device when there has been any sort of reset event to their tape devices.

The st driver currently will fail all standard scsi ioctls if a call to flush_buffer fails in st_ioctl. This effectively allows ioctls which otherwise have no affect on tape state to function as an indirect method of flushing buffers when the tape is being used in a buffering mode.

But this also makes scsi information ioctls unreliable after a reset even if no buffering is in use. With a reset setting the pos_unknown field, flush_buffer will report failure and fail all ioctls. So any application expecting to use ioctls to check the identify the device will be unable to do so in such a state.

Instead of always failing the ioctls, allow the ioctls which will not otherwise interact with the tape to bypass the call to flush_buffer if there is no buffering occurring in the st driver.

---
 drivers/scsi/st.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
drivers/scsi/st.c +19 -0
index 74a6830b7ed8..d8ed3bb2304d 100644
@@ -3528,7 +3528,22 @@ static int partition_tape(struct scsi_tape *STp, int size)
 }
 
 
+static bool st_ioctl_bypass_flush(struct scsi_tape *STp, unsigned int cmd_in)
+{
+	switch (cmd_in) {
+	case SCSI_IOCTL_GET_IDLUN:
+	case SCSI_IOCTL_GET_BUS_NUMBER:
+	case SCSI_IOCTL_GET_PCI:
+		break;
+	default:
+		return false;
+	}
+
+	if (STp->buffer->writing || STp->buffer->buffer_bytes)
+		return false;
 
+	return true;
+}
 /* The ioctl command */
 static long st_ioctl(struct file *file, unsigned int cmd_in, unsigned long arg)
 {
@@ -3565,6 +3580,9 @@ static long st_ioctl(struct file *file, unsigned int cmd_in, unsigned long arg)
 	if (retval)
 		goto out;
 
+	if (st_ioctl_bypass_flush(STp, cmd_in))
+		goto unlock;
+
 	cmd_type = _IOC_TYPE(cmd_in);
 	cmd_nr = _IOC_NR(cmd_in);
 
@@ -3878,6 +3896,7 @@ static long st_ioctl(struct file *file, unsigned int cmd_in, unsigned long arg)
 		retval = put_user_mtpos(p, &mt_pos);
 		goto out;
 	}
+unlock:
 	mutex_unlock(&STp->lock);
 
 	switch (cmd_in) {
Signature · 1 line
2.51.0
#1 lore raw
Quoted 19 lines · David Jeffery

With commit 9604eea5bd3a ("scsi: st: Add third party poweron reset handling") some customer tape applications fail from being unable to complete ioctls to verify ID information for the device when there has been any sort of reset event to their tape devices.

The st driver currently will fail all standard scsi ioctls if a call to flush_buffer fails in st_ioctl. This effectively allows ioctls which otherwise have no affect on tape state to function as an indirect method of flushing buffers when the tape is being used in a buffering mode.

But this also makes scsi information ioctls unreliable after a reset even if no buffering is in use. With a reset setting the pos_unknown field, flush_buffer will report failure and fail all ioctls. So any application expecting to use ioctls to check the identify the device will be unable to do so in such a state.

Quoted 3 lines

Instead of always failing the ioctls, allow the ioctls which will not otherwise interact with the tape to bypass the call to flush_buffer if there is no buffering occurring in the st driver.

In principle, the driver should allow after reset commands that are not affected by rewind at reset. In practice, there are cases that have not been handled, like this one. This is, however, somewhat nasty case because st has to know about the internals of the scsi stack. But, something has to be done because this is a practical problem.

Your patch should solve this problem, but there are some things that I would like to address.

The patch includes a huge jump over most of the code. This makes it a little difficult to understand. I think it would be better to handle this condition locally around the call to flush_buffer(). This would make it clear to see what this does, without having to check the code being jumped over.

Another thing is handling the non-empty buffer. Could the patch just skip flush_buffer() unconditionally? I don't like to have code that mysteriously fails in some cases without a clear reason: why can't one ask the IDs even if the buffer is not empty?

This problem also provides an opportunity to slight cleaning the code by moving handling of pass-through to a new function. The the rest of st_ioctl() would then just concentrate on the MTIOC_* ioctls.

Thanks, Kai

P.S. The patch seems to miss a newline after the new function.

#2 lore raw
Quoted 30 lines · Kai Mäkisara (Kolumbus)
Quoted 20 lines · David Jeffery

With commit 9604eea5bd3a ("scsi: st: Add third party poweron reset handling") some customer tape applications fail from being unable to complete ioctls to verify ID information for the device when there has been any sort of reset event to their tape devices.

The st driver currently will fail all standard scsi ioctls if a call to flush_buffer fails in st_ioctl. This effectively allows ioctls which otherwise have no affect on tape state to function as an indirect method of flushing buffers when the tape is being used in a buffering mode.

But this also makes scsi information ioctls unreliable after a reset even if no buffering is in use. With a reset setting the pos_unknown field, flush_buffer will report failure and fail all ioctls. So any application expecting to use ioctls to check the identify the device will be unable to do so in such a state.

Quoted 3 lines

Instead of always failing the ioctls, allow the ioctls which will not otherwise interact with the tape to bypass the call to flush_buffer if there is no buffering occurring in the st driver.

In principle, the driver should allow after reset commands that are not affected by rewind at reset. In practice, there are cases that have not been handled, like this one. This is, however, somewhat nasty case because st has to know about the internals of the scsi stack. But, something has to be done because this is a practical problem.

Quoted 8 lines

Your patch should solve this problem, but there are some things that I would like to address.

The patch includes a huge jump over most of the code. This makes it a little difficult to understand. I think it would be better to handle this condition locally around the call to flush_buffer(). This would make it clear to see what this does, without having to check the code being jumped over.

If that is your preference, I have no problem with changing it. I wasn't thrilled with either location.

Quoted 4 lines

Another thing is handling the non-empty buffer. Could the patch just skip flush_buffer() unconditionally? I don't like to have code that mysteriously fails in some cases without a clear reason: why can't one ask the IDs even if the buffer is not empty?

I originally wrote a version which worked this way, but discarded it out of concern that there may be tape software which expects the current behavior and uses some of the ioctls as an overly clever method to flush the buffer. The flush behavior has been there a long time so I was concerned about completely removing it and risking breaking some other application.

Perhaps my paranoia about changing the behavior is unwarranted. Do you think it best to completely ignore any flushing or buffering for these IDing ioctls?

Quoted 3 lines

This problem also provides an opportunity to slight cleaning the code by moving handling of pass-through to a new function. The the rest of st_ioctl() would then just concentrate on the MTIOC_* ioctls.

st_ioctl is quite the mess with how things are laid out. I can see about making a version which moves the handling of generic scsi ioctls into its own function instead of adding to st_ioctl's complexity.

Thanks,
David Jeffery

#3 lore raw
Quoted 6 lines · David Jeffery
Quoted 1 line · Kai Mäkisara (Kolumbus)

...

Quoted 8 lines
Quoted 5 lines

The patch includes a huge jump over most of the code. This makes it a little difficult to understand. I think it would be better to handle this condition locally around the call to flush_buffer(). This would make it clear to see what this does, without having to check the code being jumped over.

If that is your preference, I have no problem with changing it. I wasn't thrilled with either location.

That is my preference and I wanted to say that. But it is not an absolute requirement.

Quoted 13 lines
Quoted 5 lines

Another thing is handling the non-empty buffer. Could the patch just skip flush_buffer() unconditionally? I don't like to have code that mysteriously fails in some cases without a clear reason: why can't one ask the IDs even if the buffer is not empty?

I originally wrote a version which worked this way, but discarded it out of concern that there may be tape software which expects the current behavior and uses some of the ioctls as an overly clever method to flush the buffer. The flush behavior has been there a long time so I was concerned about completely removing it and risking breaking some other application.

Quoted 3 lines

Perhaps my paranoia about changing the behavior is unwarranted. Do you think it best to completely ignore any flushing or buffering for these IDing ioctls?

It is always good to not break existing behavior and I also thought about that. But there are also other, maybe conflicting, aspects, like simplification of code paths and not to provide surprises to those who have not yet experienced the quirks.

Either behavior is OK for me.

Quoted 8 lines
Quoted 4 lines

This problem also provides an opportunity to slight cleaning the code by moving handling of pass-through to a new function. The the rest of st_ioctl() would then just concentrate on the MTIOC_* ioctls.

st_ioctl is quite the mess with how things are laid out. I can see about making a version which moves the handling of generic scsi ioctls into its own function instead of adding to st_ioctl's complexity.

st_ioctl() has grown a very long time: small bits of code have been added time after time. One strong motive has been to limit the testing effort. This is why I nowadays try to look for opportunities to safe simplification and clarification of the code when something has to be changed :-)

Thanks, Kai