All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/3] [SCSI] sd: consolidate sector size alignment check in sd_pref_fn
@ 2011-08-02  3:50 Namhyung Kim
  2011-08-02  3:50 ` [PATCH 2/3] [SCSI] sd: remove dead code Namhyung Kim
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Namhyung Kim @ 2011-08-02  3:50 UTC (permalink / raw)
  To: James E.J. Bottomley; +Cc: linux-scsi, linux-kernel

Generalize common code to reduce code duplication.

Signed-off-by: Namhyung Kim <namhyung@gmail.com>
---
 drivers/scsi/sd.c |   32 ++++++++------------------------
 1 files changed, 8 insertions(+), 24 deletions(-)

diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index 953773cb26d9..463a324835f4 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -735,36 +735,20 @@ static int sd_prep_fn(struct request_queue *q, struct request *rq)
 	 * and not force the scsi disk driver to use bounce buffers
 	 * for this.
 	 */
-	if (sdp->sector_size == 1024) {
-		if ((block & 1) || (blk_rq_sectors(rq) & 1)) {
-			scmd_printk(KERN_ERR, SCpnt,
-				    "Bad block number requested\n");
-			goto out;
-		} else {
-			block = block >> 1;
-			this_count = this_count >> 1;
-		}
-	}
-	if (sdp->sector_size == 2048) {
-		if ((block & 3) || (blk_rq_sectors(rq) & 3)) {
-			scmd_printk(KERN_ERR, SCpnt,
-				    "Bad block number requested\n");
-			goto out;
-		} else {
-			block = block >> 2;
-			this_count = this_count >> 2;
-		}
-	}
-	if (sdp->sector_size == 4096) {
-		if ((block & 7) || (blk_rq_sectors(rq) & 7)) {
+	if (sdp->sector_size > 512) {
+		int shift = ilog2(sdp->sector_size) - 9;
+
+		if (!IS_ALIGNED(block, 1 << shift) ||
+		    !IS_ALIGNED(blk_rq_sectors(rq), 1 << shift)) {
 			scmd_printk(KERN_ERR, SCpnt,
 				    "Bad block number requested\n");
 			goto out;
 		} else {
-			block = block >> 3;
-			this_count = this_count >> 3;
+			block = block >> shift;
+			this_count = this_count >> shift;
 		}
 	}
+
 	if (rq_data_dir(rq) == WRITE) {
 		if (!sdp->writeable) {
 			goto out;
-- 
1.7.6


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/3] [SCSI] sd: remove dead code
  2011-08-02  3:50 [PATCH 1/3] [SCSI] sd: consolidate sector size alignment check in sd_pref_fn Namhyung Kim
@ 2011-08-02  3:50 ` Namhyung Kim
  2011-08-02 16:29   ` James Bottomley
  2011-08-02  3:50 ` [PATCH 3/3] [SCSI] sd: update kernel doc comments Namhyung Kim
  2011-08-02 16:29 ` [PATCH 1/3] [SCSI] sd: consolidate sector size alignment check in sd_pref_fn James Bottomley
  2 siblings, 1 reply; 6+ messages in thread
From: Namhyung Kim @ 2011-08-02  3:50 UTC (permalink / raw)
  To: James E.J. Bottomley; +Cc: linux-scsi, linux-kernel

As rq_data_dir() only checks a single bit (REQ_WRITE), the result
can not be anything but READ or WRITE. Thus last ELSE clause is
not needed at all.

Signed-off-by: Namhyung Kim <namhyung@gmail.com>
---
 drivers/scsi/sd.c |    5 +----
 1 files changed, 1 insertions(+), 4 deletions(-)

diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index 463a324835f4..fbb522add124 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -760,12 +760,9 @@ static int sd_prep_fn(struct request_queue *q, struct request *rq)
 		    sd_dif_prepare(rq, block, sdp->sector_size) == -EIO)
 			goto out;
 
-	} else if (rq_data_dir(rq) == READ) {
+	} else { /* READ */
 		SCpnt->cmnd[0] = READ_6;
 		SCpnt->sc_data_direction = DMA_FROM_DEVICE;
-	} else {
-		scmd_printk(KERN_ERR, SCpnt, "Unknown command %x\n", rq->cmd_flags);
-		goto out;
 	}
 
 	SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
-- 
1.7.6


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/3] [SCSI] sd: update kernel doc comments
  2011-08-02  3:50 [PATCH 1/3] [SCSI] sd: consolidate sector size alignment check in sd_pref_fn Namhyung Kim
  2011-08-02  3:50 ` [PATCH 2/3] [SCSI] sd: remove dead code Namhyung Kim
@ 2011-08-02  3:50 ` Namhyung Kim
  2011-08-02 16:29 ` [PATCH 1/3] [SCSI] sd: consolidate sector size alignment check in sd_pref_fn James Bottomley
  2 siblings, 0 replies; 6+ messages in thread
From: Namhyung Kim @ 2011-08-02  3:50 UTC (permalink / raw)
  To: James E.J. Bottomley; +Cc: linux-scsi, linux-kernel, Randy Dunlap

Fix a typo and update argument names.

Cc: Randy Dunlap <rdunlap@xenotime.net>
Signed-off-by: Namhyung Kim <namhyung@gmail.com>
---
 drivers/scsi/sd.c |   31 +++++++++++++------------------
 1 files changed, 13 insertions(+), 18 deletions(-)

diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index fbb522add124..496a4d9ca670 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -529,7 +529,7 @@ static void sd_config_discard(struct scsi_disk *sdkp, unsigned int mode)
 
 /**
  * scsi_setup_discard_cmnd - unmap blocks on thinly provisioned device
- * @sdp: scsi device to operate one
+ * @sdp: scsi device to operate on
  * @rq: Request to prepare
  *
  * Will issue either UNMAP or WRITE SAME(16) depending on preference
@@ -635,8 +635,8 @@ static void sd_unprep_fn(struct request_queue *q, struct request *rq)
 /**
  *	sd_init_command - build a scsi (read or write) command from
  *	information in the request structure.
- *	@SCpnt: pointer to mid-level's per scsi command structure that
- *	contains request and into which the scsi command is written
+ *	@q: pointer to request queue structure
+ *	@rq: request to prepare
  *
  *	Returns 1 if successful and 0 if error (or cannot be done now).
  **/
@@ -890,17 +890,12 @@ static int sd_prep_fn(struct request_queue *q, struct request *rq)
 
 /**
  *	sd_open - open a scsi disk device
- *	@inode: only i_rdev member may be used
- *	@filp: only f_mode and f_flags may be used
+ *	@bdev: block device to open
+ *	@mode: file open mode
  *
  *	Returns 0 if successful. Returns a negated errno value in case 
  *	of error.
  *
- *	Note: This can be called from a user context (e.g. fsck(1) )
- *	or from within the kernel (e.g. as a result of a mount(1) ).
- *	In the latter case @inode and @filp carry an abridged amount
- *	of information as noted above.
- *
  *	Locking: called with bdev->bd_mutex held.
  **/
 static int sd_open(struct block_device *bdev, fmode_t mode)
@@ -973,8 +968,8 @@ error_autopm:
 /**
  *	sd_release - invoked when the (last) close(2) is called on this
  *	scsi disk.
- *	@inode: only i_rdev member may be used
- *	@filp: only f_mode and f_flags may be used
+ *	@disk: generic disk structure
+ *	@mode: file open mode
  *
  *	Returns 0. 
  *
@@ -1031,8 +1026,8 @@ static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
 
 /**
  *	sd_ioctl - process an ioctl
- *	@inode: only i_rdev/i_bdev members may be used
- *	@filp: only f_mode and f_flags may be used
+ *	@bdev: block device to process
+ *	@mode: file open mode
  *	@cmd: ioctl command number
  *	@arg: this is third argument given to ioctl(2) system call.
  *	Often contains a pointer.
@@ -2198,7 +2193,7 @@ static void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer)
 
 /**
  * sd_read_block_limits - Query disk device for preferred I/O sizes.
- * @disk: disk to query
+ * @sdkp: disk to query
  */
 static void sd_read_block_limits(struct scsi_disk *sdkp)
 {
@@ -2264,7 +2259,7 @@ static void sd_read_block_limits(struct scsi_disk *sdkp)
 
 /**
  * sd_read_block_characteristics - Query block dev. characteristics
- * @disk: disk to query
+ * @sdkp: disk to query
  */
 static void sd_read_block_characteristics(struct scsi_disk *sdkp)
 {
@@ -2290,7 +2285,7 @@ static void sd_read_block_characteristics(struct scsi_disk *sdkp)
 
 /**
  * sd_read_block_provisioning - Query provisioning VPD page
- * @disk: disk to query
+ * @sdkp: disk to query
  */
 static void sd_read_block_provisioning(struct scsi_disk *sdkp)
 {
@@ -2629,7 +2624,7 @@ static int sd_probe(struct device *dev)
  *	sd_remove - called whenever a scsi disk (previously recognized by
  *	sd_probe) is detached from the system. It is called (potentially
  *	multiple times) during sd module unload.
- *	@sdp: pointer to mid level scsi device object
+ *	@dev: pointer to device object
  *
  *	Note: this function is invoked from the scsi mid-level.
  *	This function potentially frees up a device name (e.g. /dev/sdc)
-- 
1.7.6


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/3] [SCSI] sd: consolidate sector size alignment check in sd_pref_fn
  2011-08-02  3:50 [PATCH 1/3] [SCSI] sd: consolidate sector size alignment check in sd_pref_fn Namhyung Kim
  2011-08-02  3:50 ` [PATCH 2/3] [SCSI] sd: remove dead code Namhyung Kim
  2011-08-02  3:50 ` [PATCH 3/3] [SCSI] sd: update kernel doc comments Namhyung Kim
@ 2011-08-02 16:29 ` James Bottomley
  2011-08-03  1:03   ` [PATCH v2] [SCSI] sd: consolidate sector size alignment check in sd_prep_fn Namhyung Kim
  2 siblings, 1 reply; 6+ messages in thread
From: James Bottomley @ 2011-08-02 16:29 UTC (permalink / raw)
  To: Namhyung Kim; +Cc: linux-scsi, linux-kernel

On Tue, 2011-08-02 at 12:50 +0900, Namhyung Kim wrote:
> Generalize common code to reduce code duplication.

This code isn't equivalent because it won't fail sector sizes > 4096
which we can't support.

Plus I really don't see much point doing this: ilog2 is a far less
efficient operation on several platforms and this is the hot path.

James

> Signed-off-by: Namhyung Kim <namhyung@gmail.com>
> ---
>  drivers/scsi/sd.c |   32 ++++++++------------------------
>  1 files changed, 8 insertions(+), 24 deletions(-)
> 
> diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
> index 953773cb26d9..463a324835f4 100644
> --- a/drivers/scsi/sd.c
> +++ b/drivers/scsi/sd.c
> @@ -735,36 +735,20 @@ static int sd_prep_fn(struct request_queue *q, struct request *rq)
>  	 * and not force the scsi disk driver to use bounce buffers
>  	 * for this.
>  	 */
> -	if (sdp->sector_size == 1024) {
> -		if ((block & 1) || (blk_rq_sectors(rq) & 1)) {
> -			scmd_printk(KERN_ERR, SCpnt,
> -				    "Bad block number requested\n");
> -			goto out;
> -		} else {
> -			block = block >> 1;
> -			this_count = this_count >> 1;
> -		}
> -	}
> -	if (sdp->sector_size == 2048) {
> -		if ((block & 3) || (blk_rq_sectors(rq) & 3)) {
> -			scmd_printk(KERN_ERR, SCpnt,
> -				    "Bad block number requested\n");
> -			goto out;
> -		} else {
> -			block = block >> 2;
> -			this_count = this_count >> 2;
> -		}
> -	}
> -	if (sdp->sector_size == 4096) {
> -		if ((block & 7) || (blk_rq_sectors(rq) & 7)) {
> +	if (sdp->sector_size > 512) {
> +		int shift = ilog2(sdp->sector_size) - 9;
> +
> +		if (!IS_ALIGNED(block, 1 << shift) ||
> +		    !IS_ALIGNED(blk_rq_sectors(rq), 1 << shift)) {
>  			scmd_printk(KERN_ERR, SCpnt,
>  				    "Bad block number requested\n");
>  			goto out;
>  		} else {
> -			block = block >> 3;
> -			this_count = this_count >> 3;
> +			block = block >> shift;
> +			this_count = this_count >> shift;
>  		}
>  	}
> +
>  	if (rq_data_dir(rq) == WRITE) {
>  		if (!sdp->writeable) {
>  			goto out;



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/3] [SCSI] sd: remove dead code
  2011-08-02  3:50 ` [PATCH 2/3] [SCSI] sd: remove dead code Namhyung Kim
@ 2011-08-02 16:29   ` James Bottomley
  0 siblings, 0 replies; 6+ messages in thread
From: James Bottomley @ 2011-08-02 16:29 UTC (permalink / raw)
  To: Namhyung Kim; +Cc: linux-scsi, linux-kernel

On Tue, 2011-08-02 at 12:50 +0900, Namhyung Kim wrote:
> As rq_data_dir() only checks a single bit (REQ_WRITE), the result
> can not be anything but READ or WRITE. Thus last ELSE clause is
> not needed at all.

This isn't really dead code, it's future proofing.  Block can handle
bidirectional commands.  It's true that currently the rq_data_dir()
macro is wrong for them.  However, if anyone decided to fix it, we'd
need the check.

James


> Signed-off-by: Namhyung Kim <namhyung@gmail.com>
> ---
>  drivers/scsi/sd.c |    5 +----
>  1 files changed, 1 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
> index 463a324835f4..fbb522add124 100644
> --- a/drivers/scsi/sd.c
> +++ b/drivers/scsi/sd.c
> @@ -760,12 +760,9 @@ static int sd_prep_fn(struct request_queue *q, struct request *rq)
>  		    sd_dif_prepare(rq, block, sdp->sector_size) == -EIO)
>  			goto out;
>  
> -	} else if (rq_data_dir(rq) == READ) {
> +	} else { /* READ */
>  		SCpnt->cmnd[0] = READ_6;
>  		SCpnt->sc_data_direction = DMA_FROM_DEVICE;
> -	} else {
> -		scmd_printk(KERN_ERR, SCpnt, "Unknown command %x\n", rq->cmd_flags);
> -		goto out;
>  	}
>  
>  	SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2] [SCSI] sd: consolidate sector size alignment check in sd_prep_fn
  2011-08-02 16:29 ` [PATCH 1/3] [SCSI] sd: consolidate sector size alignment check in sd_pref_fn James Bottomley
@ 2011-08-03  1:03   ` Namhyung Kim
  0 siblings, 0 replies; 6+ messages in thread
From: Namhyung Kim @ 2011-08-03  1:03 UTC (permalink / raw)
  To: James Bottomley; +Cc: linux-scsi, linux-kernel

Generalize common code to reduce code duplication.

Signed-off-by: Namhyung Kim <namhyung@gmail.com>
---
 drivers/scsi/sd.c |   36 ++++++++++++------------------------
 1 files changed, 12 insertions(+), 24 deletions(-)

diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index 953773cb26d9..fb9d997ece9d 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -651,6 +651,7 @@ static int sd_prep_fn(struct request_queue *q, struct request *rq)
 	unsigned int this_count = blk_rq_sectors(rq);
 	int ret, host_dif;
 	unsigned char protect;
+	int sector_shift;
 
 	/*
 	 * Discard request come in as REQ_TYPE_FS but we turn them into
@@ -735,36 +736,23 @@ static int sd_prep_fn(struct request_queue *q, struct request *rq)
 	 * and not force the scsi disk driver to use bounce buffers
 	 * for this.
 	 */
-	if (sdp->sector_size == 1024) {
-		if ((block & 1) || (blk_rq_sectors(rq) & 1)) {
+	sector_shift = 0;
+	switch (sdp->sector_size) {
+	case 4096: sector_shift++;	/* fall through */
+	case 2048: sector_shift++;	/* fall through */
+	case 1024: sector_shift++;
+
+		if (!IS_ALIGNED(block, 1 << sector_shift) ||
+		    !IS_ALIGNED(blk_rq_sectors(rq), 1 << sector_shift)) {
 			scmd_printk(KERN_ERR, SCpnt,
 				    "Bad block number requested\n");
 			goto out;
 		} else {
-			block = block >> 1;
-			this_count = this_count >> 1;
-		}
-	}
-	if (sdp->sector_size == 2048) {
-		if ((block & 3) || (blk_rq_sectors(rq) & 3)) {
-			scmd_printk(KERN_ERR, SCpnt,
-				    "Bad block number requested\n");
-			goto out;
-		} else {
-			block = block >> 2;
-			this_count = this_count >> 2;
-		}
-	}
-	if (sdp->sector_size == 4096) {
-		if ((block & 7) || (blk_rq_sectors(rq) & 7)) {
-			scmd_printk(KERN_ERR, SCpnt,
-				    "Bad block number requested\n");
-			goto out;
-		} else {
-			block = block >> 3;
-			this_count = this_count >> 3;
+			block = block >> sector_shift;
+			this_count = this_count >> sector_shift;
 		}
 	}
+
 	if (rq_data_dir(rq) == WRITE) {
 		if (!sdp->writeable) {
 			goto out;
-- 
1.7.6


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2011-08-03  1:05 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-02  3:50 [PATCH 1/3] [SCSI] sd: consolidate sector size alignment check in sd_pref_fn Namhyung Kim
2011-08-02  3:50 ` [PATCH 2/3] [SCSI] sd: remove dead code Namhyung Kim
2011-08-02 16:29   ` James Bottomley
2011-08-02  3:50 ` [PATCH 3/3] [SCSI] sd: update kernel doc comments Namhyung Kim
2011-08-02 16:29 ` [PATCH 1/3] [SCSI] sd: consolidate sector size alignment check in sd_pref_fn James Bottomley
2011-08-03  1:03   ` [PATCH v2] [SCSI] sd: consolidate sector size alignment check in sd_prep_fn Namhyung Kim

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.