st: separate st-unique ioctl handling from scsi common ioctl handling
Subsystems (2)
PATCH 1/2 st: separate st-unique ioctl handling from scsi common ioctl handling
The st ioctl function currently interleaves code for handling various st specific ioctls with parts of code needed for handling ioctls common to all scsi devices. Separate out st's code for the common ioctls into a more manageable, separate function.
---
drivers/scsi/st.c | 85 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 62 insertions(+), 23 deletions(-)
drivers/scsi/st.c +62 -23
@@ -3526,8 +3526,60 @@ static int partition_tape(struct scsi_tape *STp, int size) out:return result; } - +/* +* Handles any extra state needed for ioctls which are not st-specific. +* Called with the scsi_tape lock held, released before return +*/ +static long st_common_ioctl (struct scsi_tape *STp,struct st_modedef *STm, +struct file *file,unsigned int cmd_in, +unsigned long arg) +{ +int i, retval =0 ; + +if (!STm->defined) { + retval = -ENXIO ; + goto out; + } + +if ((i =flush_buffer (STp,0 )) <0 ) { + retval = i; + goto out; + }else {/* flush_buffer succeeds */ +if (STp->can_partitions) { + i =switch_partition (STp); +if (i <0 ) { + retval = i; + goto out; + } + } + } +mutex_unlock (&STp->lock); + +switch (cmd_in) { +case SG_IO : +case SCSI_IOCTL_SEND_COMMAND : +case CDROM_SEND_PACKET : +if (!capable (CAP_SYS_RAWIO )) +return -EPERM ; +break ; +default : +break ; + } + + retval =scsi_ioctl (STp->device, file->f_mode &FMODE_WRITE , + cmd_in, (void __user *)arg); +if (!retval && cmd_in ==SCSI_IOCTL_STOP_UNIT ) { +/* unload */ + STp->rew_at_close =0 ; + STp->ready =ST_NO_TAPE ; + } + +return retval; +out: +mutex_unlock (&STp->lock); +return retval; +}/* The ioctl command */ static long st_ioctl (struct file *file,unsigned int cmd_in,unsigned long arg)
@@ -3565,6 +3617,15 @@ static long st_ioctl(struct file *file, unsigned int cmd_in, unsigned long arg)if (retval) goto out; +switch (cmd_in) { +case MTIOCPOS : +case MTIOCGET : +case MTIOCTOP : +break ; +default : +return st_common_ioctl (STp, STm, file, cmd_in, arg); + } + cmd_type =_IOC_TYPE (cmd_in); cmd_nr =_IOC_NR (cmd_in);
@@ -3876,29 +3937,7 @@ static long st_ioctl(struct file *file, unsigned int cmd_in, unsigned long arg) } mt_pos.mt_blkno = blk; retval =put_user_mtpos (p, &mt_pos); - goto out; - } -mutex_unlock (&STp->lock); - -switch (cmd_in) { -case SG_IO : -case SCSI_IOCTL_SEND_COMMAND : -case CDROM_SEND_PACKET : -if (!capable (CAP_SYS_RAWIO )) -return -EPERM ; -break ; -default : -break ; } - - retval =scsi_ioctl (STp->device, file->f_mode &FMODE_WRITE , cmd_in, p); -if (!retval && cmd_in ==SCSI_IOCTL_STOP_UNIT ) { -/* unload */ - STp->rew_at_close =0 ; - STp->ready =ST_NO_TAPE ; - } -return retval; - out:mutex_unlock (&STp->lock);return retval;
Signature · 1 line
2.51.0
PATCH 2/2 st: skip buffer flush for information ioctls
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 type 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 causes ioctls which otherwise have no effect on tape state to succeed or fail based on events unrelated to the requested ioctl.
This 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.
For scsi information ioctls, avoid the need for a buffer flush and allow the ioctls to execute regardless of buffer state.
---
drivers/scsi/st.c | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-)
drivers/scsi/st.c +22 -18
@@ -3542,30 +3542,34 @@ static long st_common_ioctl(struct scsi_tape *STp, struct st_modedef *STm, goto out; } -if ((i =flush_buffer (STp,0 )) <0 ) { - retval = i; - goto out; - }else {/* flush_buffer succeeds */ -if (STp->can_partitions) { - i =switch_partition (STp); -if (i <0 ) { - retval = i; - goto out; - } - } - } -mutex_unlock (&STp->lock); -switch (cmd_in) { +case SCSI_IOCTL_GET_IDLUN : +case SCSI_IOCTL_GET_BUS_NUMBER : +case SCSI_IOCTL_GET_PCI : +break ;case SG_IO :case SCSI_IOCTL_SEND_COMMAND :case CDROM_SEND_PACKET : -if (!capable (CAP_SYS_RAWIO )) -return -EPERM ; -break ; +if (!capable (CAP_SYS_RAWIO )) { + retval = -EPERM ; + goto out; + } + fallthrough;default : -break ; +if ((i =flush_buffer (STp,0 )) <0 ) { + retval = i; + goto out; + }else {/* flush_buffer succeeds */ +if (STp->can_partitions) { + i =switch_partition (STp); +if (i <0 ) { + retval = i; + goto out; + } + } + } } +mutex_unlock (&STp->lock); retval =scsi_ioctl (STp->device, file->f_mode &FMODE_WRITE , cmd_in, (void __user *)arg);
Signature · 1 line
2.51.0
Re: [PATCH 2/2] st: skip buffer flush for information ioctls
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 type of reset event to their tape devices.The st driver currently will fail all standard scsi ioctls if a call to
flush_bufferfails inst_ioctl. This causes ioctls which otherwise have no effect on tape state to succeed or fail based on events unrelated to the requested ioctl.This makes scsi information ioctls unreliable after a reset even if no buffering is in use. With a reset setting the
pos_unknownfield,flush_bufferwill 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 5 lines
For scsi information ioctls, avoid the need for a buffer flush and allow the ioctls to execute regardless of buffer state.
Signed-off-by David Jeffery <[email protected]>Tested-by Laurence Oberman <[email protected]>
Thanks,
Kai
Re: [PATCH 2/2] st: skip buffer flush for information ioctls
In reply to David JefferyI've reviewed and tested this patch and it correctly fixes the problem.
Kia, please approve this patch.
Martin, please merge these two patches.
/John
Quoted 83 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 type of reset event to their tape devices.The st driver currently will fail all standard scsi ioctls if a call to
flush_bufferfails inst_ioctl. This causes ioctls which otherwise have no effect on tape state to succeed or fail based on events unrelated to the requested ioctl.This makes scsi information ioctls unreliable after a reset even if no buffering is in use. With a reset setting the
pos_unknownfield,flush_bufferwill 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.For scsi information ioctls, avoid the need for a buffer flush and allow the ioctls to execute regardless of buffer state.
Signed-off-by David Jeffery <[email protected]>Tested-by Laurence Oberman <[email protected]>---
drivers/scsi/st.c | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-)drivers/scsi/st.c +22
-18@@ -3542,30 +3542,34 @@ static long st_common_ioctl(struct scsi_tape *STp, struct st_modedef *STm, goto out; } -if ((i =flush_buffer (STp,0 )) <0 ) { - retval = i; - goto out; - }else {/* flush_buffer succeeds */ -if (STp->can_partitions) { - i =switch_partition (STp); -if (i <0 ) { - retval = i; - goto out; - } - } - } -mutex_unlock (&STp->lock); -switch (cmd_in) { +case SCSI_IOCTL_GET_IDLUN : +case SCSI_IOCTL_GET_BUS_NUMBER : +case SCSI_IOCTL_GET_PCI : +break ;case SG_IO :case SCSI_IOCTL_SEND_COMMAND :case CDROM_SEND_PACKET : -if (!capable (CAP_SYS_RAWIO )) -return -EPERM ; -break ; +if (!capable (CAP_SYS_RAWIO )) { + retval = -EPERM ; + goto out; + } + fallthrough;default : -break ; +if ((i =flush_buffer (STp,0 )) <0 ) { + retval = i; + goto out; + }else {/* flush_buffer succeeds */ +if (STp->can_partitions) { + i =switch_partition (STp); +if (i <0 ) { + retval = i; + goto out; + } + } + } } +mutex_unlock (&STp->lock); retval =scsi_ioctl (STp->device, file->f_mode &FMODE_WRITE , cmd_in, (void __user *)arg);
Martin K. Petersen
AppliedRe: [PATCH 2/2] st: skip buffer flush for information ioctls
In reply to David JefferyDavid,
Quoted 4 lines
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 type of reset event to their tape devices.
Applied to 6.19/scsi-staging, thanks!
Signature · 1 line
Martin K. Petersen
Quoted 11 lines · David Jeffery
The st ioctl function currently interleaves code for handling various st specific ioctls with parts of code needed for handling ioctls common to all scsi devices. Separate out st's code for the common ioctls into a more manageable, separate function.
Signed-off-by David Jeffery <[email protected]>Tested-by Laurence Oberman <[email protected]>
Thanks for doing this,
Kai
Quoted 132 lines · David Jeffery
The st ioctl function currently interleaves code for handling various st
specific ioctls with parts of code needed for handling ioctls common to
all scsi devices. Separate out st's code for the common ioctls into a more
manageable, separate function.Signed-off-by David Jeffery <[email protected]>Tested-by Laurence Oberman <[email protected]>---
drivers/scsi/st.c | 85 ++++++++++++++++++++++++++++++++++----------- --
1 file changed, 62 insertions(+), 23 deletions(-)drivers/scsi/st.c +0
-0@@ -3526,8 +3526,60 @@ static int partition_tape(struct scsi_tape *STp, int size)out:
return result;
}
-
+/*
+ * Handles any extra state needed for ioctls which are not st- specific.
+ * Called with thescsi_tapelock held, released before return
+ */
+static long st_common_ioctl(structscsi_tape*STp, structst_modedef*STm,drivers/scsi/st.c +48
-0+ struct file *file, unsigned int cmd_in, + unsigned long arg) +{ +int i, retval =0 ; + +if (!STm->defined) { + retval = -ENXIO ; + goto out; + } + +if ((i =flush_buffer (STp,0 )) <0 ) { + retval = i; + goto out; + }else {/* flush_buffer succeeds */ +if (STp->can_partitions) { + i =switch_partition (STp); +if (i <0 ) { + retval = i; + goto out; + } + } + } +mutex_unlock (&STp->lock); + +switch (cmd_in) { +case SG_IO : +case SCSI_IOCTL_SEND_COMMAND : +case CDROM_SEND_PACKET : +if (!capable (CAP_SYS_RAWIO )) +return -EPERM ; +break ; +default : +break ; + } + + retval =scsi_ioctl (STp->device, file->f_mode &FMODE_WRITE , + cmd_in, (void __user *)arg); +if (!retval && cmd_in ==SCSI_IOCTL_STOP_UNIT ) { +/* unload */ + STp->rew_at_close =0 ; + STp->ready =ST_NO_TAPE ; + } + +return retval; +out: +mutex_unlock (&STp->lock); +return retval; +}
/* The ioctl command */
static longst_ioctl(struct file *file, unsigned int cmd_in, unsigned long arg)drivers/scsi/st.c +0
-0@@ -3565,6 +3617,15 @@ static long st_ioctl(struct file *file, unsigned int cmd_in, unsigned long arg) if (retval) goto out;drivers/scsi/st.c +9
-0+switch (cmd_in) { +case MTIOCPOS : +case MTIOCGET : +case MTIOCTOP : +break ; +default : +return st_common_ioctl (STp, STm, file, cmd_in, arg); + } +
cmd_type=_IOC_TYPE(cmd_in);
cmd_nr=_IOC_NR(cmd_in);drivers/scsi/st.c +0
-22@@ -3876,29 +3937,7 @@ static long st_ioctl(struct file *file, unsigned int cmd_in, unsigned long arg) } mt_pos.mt_blkno = blk; retval = put_user_mtpos(p, &mt_pos); - goto out; - } -mutex_unlock (&STp->lock); - -switch (cmd_in) { -case SG_IO : -case SCSI_IOCTL_SEND_COMMAND : -case CDROM_SEND_PACKET : -if (!capable (CAP_SYS_RAWIO )) -return -EPERM ; -break ; -default : -break ; } - - retval =scsi_ioctl (STp->device, file->f_mode &FMODE_WRITE , cmd_in, p); -if (!retval && cmd_in ==SCSI_IOCTL_STOP_UNIT ) { -/* unload */ - STp->rew_at_close =0 ; - STp->ready =ST_NO_TAPE ; - } -return retval; -
Quoted 3 lines
out:
mutex_unlock(&STp->lock);
return retval;
For the series:
Reviewed by: Laurence Oberman <[email protected]>
Many hours of testing done here folks, this looks to be solid and fixes all our prior issues.
This diff is difficult to read. However, in reviewing this code manually by reading the results of this patch and comparing it with the previous file I believe this change is correct and functionally equivalent to the previous code.
I've also tested this patch with my new and improved tape_tests and reproduced the failing scsi ioctl(idlun) problem with this patch. So this confirms there is no functional change with this patch. It isn't pretty but it should provide a safe, bisect-able change that prepares for your next patch.
/John
https://github.com/johnmeneghini/tape_tests
[root@estor-t560 ~]# grep -E "TEST.FAILED|TEST.WARN|sg_reset|Power.on/reset.recognized|Unit.Attention" /root/tape_reset_tests.log --- sg_reset --target /dev/sg2
--- sg_map -st -x -i TEST WARN : device /dev/nst0 failed on scsi ioctl(idlun), skip: Input/output error
[ 247.493571] st 1:0:0:0: [st0] Power on/reset recognized. --- sg_map -st -x -i TEST WARN : device /dev/nst0 failed on scsi ioctl(idlun), skip: Input/output error --- stinit -f /home/jmeneghi/tape_tests/stinit.conf -v /dev/nst0 TEST WARN : The SCSI INQUIRY for device '/dev/nst0' failed (power off?): Input/output error --- sg_reset --target /dev/sg2 [ 265.727311] st 1:0:0:0: [st0] Power on/reset recognized. [ 265.727357] st 1:0:0:0: [st0] Sense Key : Unit Attention [current] --- sg_map -st -x -i TEST WARN : device /dev/nst0 failed on scsi ioctl(idlun), skip: Input/output error --- stinit -f /home/jmeneghi/tape_tests/stinit.conf -v /dev/nst0 TEST WARN : The SCSI INQUIRY for device '/dev/nst0' failed (power off?): Input/output error --- sg_reset --target /dev/sg2 [ 285.860196] st 1:0:0:0: [st0] Power on/reset recognized. [ 285.860241] st 1:0:0:0: [st0] Sense Key : Unit Attention [current] --- sg_reset --target /dev/sg2 [ 310.125953] st 1:0:0:0: [st0] Power on/reset recognized. --- sg_reset --target /dev/sg2 [ 351.444178] st 1:0:0:0: [st0] Power on/reset recognized. --- sg_reset --target /dev/sg2 [ 992.210217] st 1:0:0:0: [st0] Power on/reset recognized. --- sg_map -st -x -i TEST WARN : device /dev/nst0 failed on scsi ioctl(idlun), skip: Input/output error --- stinit -f /home/jmeneghi/tape_tests/stinit.conf -v /dev/nst0 TEST WARN : The SCSI INQUIRY for device '/dev/nst0' failed (power off?): Input/output error --- sg_reset --target /dev/sg2 [ 1002.023106] st 1:0:0:0: [st0] Power on/reset recognized.
[root@estor-t560 ~]#
Quoted 124 lines · David Jeffery
The st ioctl function currently interleaves code for handling various st specific ioctls with parts of code needed for handling ioctls common to all scsi devices. Separate out st's code for the common ioctls into a more manageable, separate function.
Signed-off-by David Jeffery <[email protected]>Tested-by Laurence Oberman <[email protected]>---
drivers/scsi/st.c | 85 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 62 insertions(+), 23 deletions(-)drivers/scsi/st.c +62
-23@@ -3526,8 +3526,60 @@ static int partition_tape(struct scsi_tape *STp, int size) out:return result; } - +/* +* Handles any extra state needed for ioctls which are not st-specific. +* Called with the scsi_tape lock held, released before return +*/ +static long st_common_ioctl (struct scsi_tape *STp,struct st_modedef *STm, +struct file *file,unsigned int cmd_in, +unsigned long arg) +{ +int i, retval =0 ; + +if (!STm->defined) { + retval = -ENXIO ; + goto out; + } + +if ((i =flush_buffer (STp,0 )) <0 ) { + retval = i; + goto out; + }else {/* flush_buffer succeeds */ +if (STp->can_partitions) { + i =switch_partition (STp); +if (i <0 ) { + retval = i; + goto out; + } + } + } +mutex_unlock (&STp->lock); + +switch (cmd_in) { +case SG_IO : +case SCSI_IOCTL_SEND_COMMAND : +case CDROM_SEND_PACKET : +if (!capable (CAP_SYS_RAWIO )) +return -EPERM ; +break ; +default : +break ; + } + + retval =scsi_ioctl (STp->device, file->f_mode &FMODE_WRITE , + cmd_in, (void __user *)arg); +if (!retval && cmd_in ==SCSI_IOCTL_STOP_UNIT ) { +/* unload */ + STp->rew_at_close =0 ; + STp->ready =ST_NO_TAPE ; + } + +return retval; +out: +mutex_unlock (&STp->lock); +return retval; +}/* The ioctl command */ static long st_ioctl (struct file *file,unsigned int cmd_in,unsigned long arg)@@ -3565,6 +3617,15 @@ static long st_ioctl(struct file *file, unsigned int cmd_in, unsigned long arg)if (retval) goto out; +switch (cmd_in) { +case MTIOCPOS : +case MTIOCGET : +case MTIOCTOP : +break ; +default : +return st_common_ioctl (STp, STm, file, cmd_in, arg); + } + cmd_type =_IOC_TYPE (cmd_in); cmd_nr =_IOC_NR (cmd_in);@@ -3876,29 +3937,7 @@ static long st_ioctl(struct file *file, unsigned int cmd_in, unsigned long arg) } mt_pos.mt_blkno = blk; retval =put_user_mtpos (p, &mt_pos); - goto out; - } -mutex_unlock (&STp->lock); - -switch (cmd_in) { -case SG_IO : -case SCSI_IOCTL_SEND_COMMAND : -case CDROM_SEND_PACKET : -if (!capable (CAP_SYS_RAWIO )) -return -EPERM ; -break ; -default : -break ; } - - retval =scsi_ioctl (STp->device, file->f_mode &FMODE_WRITE , cmd_in, p); -if (!retval && cmd_in ==SCSI_IOCTL_STOP_UNIT ) { -/* unload */ - STp->rew_at_close =0 ; - STp->ready =ST_NO_TAPE ; - } -return retval; - out:mutex_unlock (&STp->lock);return retval;
Martin K. Petersen
AppliedDavid,
Quoted 4 lines
The st ioctl function currently interleaves code for handling various st specific ioctls with parts of code needed for handling ioctls common to all scsi devices. Separate out st's code for the common ioctls into a more manageable, separate function.
Applied to 6.19/scsi-staging, thanks!
Signature · 1 line
Martin K. Petersen
Martin K. Petersen
AppliedQuoted 6 lines · David Jeffery
The st ioctl function currently interleaves code for handling various st specific ioctls with parts of code needed for handling ioctls common to all scsi devices. Separate out st's code for the common ioctls into a more manageable, separate function.
Applied to 6.19/scsi-queue, thanks!
[1/2] st: separate st-unique ioctl handling from scsi common ioctl handling
https://git.kernel.org/mkp/scsi/c/b37d70c0df85 [2/2] st: skip buffer flush for information ioctls https://git.kernel.org/mkp/scsi/c/d27418aaf8bc
Signature · 1 line
Martin K. Petersen