[PATCH 2/2] usb: storage: rearrange triple nested CSW data phase check

Desnes Nunes posted 2 patches 1 month, 2 weeks ago
There is a newer version of this series
[PATCH 2/2] usb: storage: rearrange triple nested CSW data phase check
Posted by Desnes Nunes 1 month, 2 weeks ago
This rearranges the triple nested CSW data phase if clause, in order to
make usb_stor_Bulk_transport() code more readlable. No functional change.

Signed-off-by: Desnes Nunes <desnesn@redhat.com>
---
 drivers/usb/storage/transport.c | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/drivers/usb/storage/transport.c b/drivers/usb/storage/transport.c
index 96b81cf6adc7..3f2e1df5ad1e 100644
--- a/drivers/usb/storage/transport.c
+++ b/drivers/usb/storage/transport.c
@@ -1188,18 +1188,17 @@ int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
 		 * check whether it really is a CSW.
 		 */
 		if (result == USB_STOR_XFER_SHORT &&
-				srb->sc_data_direction == DMA_FROM_DEVICE &&
-				transfer_length - scsi_get_resid(srb) ==
-					US_BULK_CS_WRAP_LEN) {
+		    srb->sc_data_direction == DMA_FROM_DEVICE &&
+		    transfer_length - scsi_get_resid(srb) == US_BULK_CS_WRAP_LEN) {
 			struct scatterlist *sg = NULL;
-			unsigned int offset = 0;
-
-			if (usb_stor_access_xfer_buf((unsigned char *) bcs,
-					US_BULK_CS_WRAP_LEN, srb, &sg,
-					&offset, FROM_XFER_BUF) ==
-						US_BULK_CS_WRAP_LEN &&
-					bcs->Signature ==
-						cpu_to_le32(US_BULK_CS_SIGN)) {
+			unsigned int offset = 0, buflen = 0;
+
+			buflen = usb_stor_access_xfer_buf((unsigned char *) bcs,
+						US_BULK_CS_WRAP_LEN, srb, &sg,
+						&offset, FROM_XFER_BUF);
+
+			if (buflen == US_BULK_CS_WRAP_LEN &&
+			    bcs->Signature == cpu_to_le32(US_BULK_CS_SIGN)) {
 				unsigned char buf[US_BULK_CS_WRAP_LEN];
 
 				sg = NULL;
-- 
2.50.1
Re: [PATCH 2/2] usb: storage: rearrange triple nested CSW data phase check
Posted by Alan Stern 1 month, 2 weeks ago
On Wed, Oct 29, 2025 at 04:14:14PM -0300, Desnes Nunes wrote:
> This rearranges the triple nested CSW data phase if clause, in order to
> make usb_stor_Bulk_transport() code more readlable. No functional change.
> 
> Signed-off-by: Desnes Nunes <desnesn@redhat.com>
> ---
>  drivers/usb/storage/transport.c | 21 ++++++++++-----------
>  1 file changed, 10 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/usb/storage/transport.c b/drivers/usb/storage/transport.c
> index 96b81cf6adc7..3f2e1df5ad1e 100644
> --- a/drivers/usb/storage/transport.c
> +++ b/drivers/usb/storage/transport.c
> @@ -1188,18 +1188,17 @@ int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
>  		 * check whether it really is a CSW.
>  		 */
>  		if (result == USB_STOR_XFER_SHORT &&
> -				srb->sc_data_direction == DMA_FROM_DEVICE &&
> -				transfer_length - scsi_get_resid(srb) ==
> -					US_BULK_CS_WRAP_LEN) {
> +		    srb->sc_data_direction == DMA_FROM_DEVICE &&
> +		    transfer_length - scsi_get_resid(srb) == US_BULK_CS_WRAP_LEN) {

This change has nothing to do with the subject of the patch.  Please 
leave the code the way it was.

>  			struct scatterlist *sg = NULL;
> -			unsigned int offset = 0;
> -
> -			if (usb_stor_access_xfer_buf((unsigned char *) bcs,
> -					US_BULK_CS_WRAP_LEN, srb, &sg,
> -					&offset, FROM_XFER_BUF) ==
> -						US_BULK_CS_WRAP_LEN &&
> -					bcs->Signature ==
> -						cpu_to_le32(US_BULK_CS_SIGN)) {
> +			unsigned int offset = 0, buflen = 0;

It seems silly to initialize buflen to 0 when the very next statement is 
going to overwrite that value.

Also, "buflen" is not a good name for this variable, because the 
variable does not contain the length of a buffer.  Rather, it will 
contain the amount of data that got transferred by the 
usb_stor_access_xfer_buf() routine.  The following "if" statement then 
tests whether that amount is equal to the buffer length.

Alan Stern

> +
> +			buflen = usb_stor_access_xfer_buf((unsigned char *) bcs,
> +						US_BULK_CS_WRAP_LEN, srb, &sg,
> +						&offset, FROM_XFER_BUF);
> +
> +			if (buflen == US_BULK_CS_WRAP_LEN &&
> +			    bcs->Signature == cpu_to_le32(US_BULK_CS_SIGN)) {
>  				unsigned char buf[US_BULK_CS_WRAP_LEN];
>  
>  				sg = NULL;
> -- 
> 2.50.1
>
Re: [PATCH 2/2] usb: storage: rearrange triple nested CSW data phase check
Posted by Desnes Nunes 1 month, 2 weeks ago
Hello Alan,

On Wed, Oct 29, 2025 at 6:54 PM Alan Stern <stern@rowland.harvard.edu> wrote:
>
> On Wed, Oct 29, 2025 at 04:14:14PM -0300, Desnes Nunes wrote:
> > This rearranges the triple nested CSW data phase if clause, in order to
> > make usb_stor_Bulk_transport() code more readlable. No functional change.
> >
> > Signed-off-by: Desnes Nunes <desnesn@redhat.com>
> > ---
> >  drivers/usb/storage/transport.c | 21 ++++++++++-----------
> >  1 file changed, 10 insertions(+), 11 deletions(-)
> >
> > diff --git a/drivers/usb/storage/transport.c b/drivers/usb/storage/transport.c
> > index 96b81cf6adc7..3f2e1df5ad1e 100644
> > --- a/drivers/usb/storage/transport.c
> > +++ b/drivers/usb/storage/transport.c
> > @@ -1188,18 +1188,17 @@ int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
> >                * check whether it really is a CSW.
> >                */
> >               if (result == USB_STOR_XFER_SHORT &&
> > -                             srb->sc_data_direction == DMA_FROM_DEVICE &&
> > -                             transfer_length - scsi_get_resid(srb) ==
> > -                                     US_BULK_CS_WRAP_LEN) {
> > +                 srb->sc_data_direction == DMA_FROM_DEVICE &&
> > +                 transfer_length - scsi_get_resid(srb) == US_BULK_CS_WRAP_LEN) {
>
> This change has nothing to do with the subject of the patch.  Please
> leave the code the way it was.
>
> >                       struct scatterlist *sg = NULL;
> > -                     unsigned int offset = 0;
> > -
> > -                     if (usb_stor_access_xfer_buf((unsigned char *) bcs,
> > -                                     US_BULK_CS_WRAP_LEN, srb, &sg,
> > -                                     &offset, FROM_XFER_BUF) ==
> > -                                             US_BULK_CS_WRAP_LEN &&
> > -                                     bcs->Signature ==
> > -                                             cpu_to_le32(US_BULK_CS_SIGN)) {
> > +                     unsigned int offset = 0, buflen = 0;
>
> It seems silly to initialize buflen to 0 when the very next statement is
> going to overwrite that value.
>
> Also, "buflen" is not a good name for this variable, because the
> variable does not contain the length of a buffer.  Rather, it will
> contain the amount of data that got transferred by the
> usb_stor_access_xfer_buf() routine.  The following "if" statement then
> tests whether that amount is equal to the buffer length.
>
> Alan Stern

I tried to borrow some code from usb storage protocol, but after these
observations I do agree it is not a good name here.
Nonetheless, I will drop this patch from v2 as requested.

Thanks for the review,

-- 
Desnes Nunes
Re: [PATCH 2/2] usb: storage: rearrange triple nested CSW data phase check
Posted by Alan Stern 1 month, 2 weeks ago
On Wed, Oct 29, 2025 at 09:39:36PM -0300, Desnes Nunes wrote:
> Hello Alan,
> 
> On Wed, Oct 29, 2025 at 6:54 PM Alan Stern <stern@rowland.harvard.edu> wrote:
> >
> > On Wed, Oct 29, 2025 at 04:14:14PM -0300, Desnes Nunes wrote:
> > > This rearranges the triple nested CSW data phase if clause, in order to
> > > make usb_stor_Bulk_transport() code more readlable. No functional change.
> > >
> > > Signed-off-by: Desnes Nunes <desnesn@redhat.com>
> > > ---
> > >  drivers/usb/storage/transport.c | 21 ++++++++++-----------
> > >  1 file changed, 10 insertions(+), 11 deletions(-)
> > >
> > > diff --git a/drivers/usb/storage/transport.c b/drivers/usb/storage/transport.c
> > > index 96b81cf6adc7..3f2e1df5ad1e 100644
> > > --- a/drivers/usb/storage/transport.c
> > > +++ b/drivers/usb/storage/transport.c
> > > @@ -1188,18 +1188,17 @@ int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
> > >                * check whether it really is a CSW.
> > >                */
> > >               if (result == USB_STOR_XFER_SHORT &&
> > > -                             srb->sc_data_direction == DMA_FROM_DEVICE &&
> > > -                             transfer_length - scsi_get_resid(srb) ==
> > > -                                     US_BULK_CS_WRAP_LEN) {
> > > +                 srb->sc_data_direction == DMA_FROM_DEVICE &&
> > > +                 transfer_length - scsi_get_resid(srb) == US_BULK_CS_WRAP_LEN) {
> >
> > This change has nothing to do with the subject of the patch.  Please
> > leave the code the way it was.
> >
> > >                       struct scatterlist *sg = NULL;
> > > -                     unsigned int offset = 0;
> > > -
> > > -                     if (usb_stor_access_xfer_buf((unsigned char *) bcs,
> > > -                                     US_BULK_CS_WRAP_LEN, srb, &sg,
> > > -                                     &offset, FROM_XFER_BUF) ==
> > > -                                             US_BULK_CS_WRAP_LEN &&
> > > -                                     bcs->Signature ==
> > > -                                             cpu_to_le32(US_BULK_CS_SIGN)) {
> > > +                     unsigned int offset = 0, buflen = 0;
> >
> > It seems silly to initialize buflen to 0 when the very next statement is
> > going to overwrite that value.
> >
> > Also, "buflen" is not a good name for this variable, because the
> > variable does not contain the length of a buffer.  Rather, it will
> > contain the amount of data that got transferred by the
> > usb_stor_access_xfer_buf() routine.  The following "if" statement then
> > tests whether that amount is equal to the buffer length.
> >
> > Alan Stern
> 
> I tried to borrow some code from usb storage protocol, but after these
> observations I do agree it is not a good name here.
> Nonetheless, I will drop this patch from v2 as requested.

I didn't mean that the entire patch should be dropped, just the changes 
to the indentation of the first few lines.

As for the variable name, num_written or something like that would be 
preferable to buflen.  You can make up something better, or you can drop 
the entire patch -- your choice.

Alan Stern