[PATCH] remove duplicated function set_medium_error

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



function set_medium_error is implemented in serveral files. So move
it into scsi.c as common function.
renaming set_medium_error with suffix 'scsi_' fits the general style.

Signed-off-by: zhenwei pi <pizhenwei@xxxxxxxxxxxxx>
---
 usr/bs_glfs.c     | 21 +++++++--------------
 usr/bs_rbd.c      | 21 +++++++--------------
 usr/bs_rdwr.c     | 23 ++++++++---------------
 usr/bs_sheepdog.c | 13 +++----------
 usr/scsi.c        |  7 +++++++
 usr/tgtd.h        |  1 +
 6 files changed, 33 insertions(+), 53 deletions(-)

diff --git a/usr/bs_glfs.c b/usr/bs_glfs.c
index 93984b4..5d7bf4e 100644
--- a/usr/bs_glfs.c
+++ b/usr/bs_glfs.c
@@ -63,13 +63,6 @@ struct active_glfs {
 				sizeof(struct bs_thread_info)) \
 			)
 
-static void set_medium_error(int *result, uint8_t *key, uint16_t *asc)
-{
-	*result = SAM_STAT_CHECK_CONDITION;
-	*key = MEDIUM_ERROR;
-	*asc = ASC_READ_ERROR;
-}
-
 static int bs_glfs_discard(glfs_fd_t *gfd, off_t offset, size_t len)
 {
 #ifdef BS_GLFS_DISCARD
@@ -113,7 +106,7 @@ static void bs_glfs_request(struct scsi_cmd *cmd)
 		ret = glfs_pread(gfd, tmpbuf, length, offset, lu->bsoflags);
 
 		if (ret != length) {
-			set_medium_error(&result, &key, &asc);
+			scsi_set_medium_error(&result, &key, &asc);
 			free(tmpbuf);
 			break;
 		}
@@ -150,7 +143,7 @@ static void bs_glfs_request(struct scsi_cmd *cmd)
 		ret = glfs_pread(gfd, tmpbuf, length, offset, SEEK_SET);
 
 		if (ret != length) {
-			set_medium_error(&result, &key, &asc);
+			scsi_set_medium_error(&result, &key, &asc);
 			free(tmpbuf);
 			break;
 		}
@@ -224,7 +217,7 @@ write:
 			    !(pg->mode_data[0] & 0x04))
 				glfs_fdatasync(gfd);
 		} else
-			set_medium_error(&result, &key, &asc);
+			scsi_set_medium_error(&result, &key, &asc);
 
 		if (do_verify)
 			goto verify;
@@ -261,7 +254,7 @@ write:
 					offset, lu->bsoflags);
 
 			if (ret != blocksize)
-				set_medium_error(&result, &key, &asc);
+				scsi_set_medium_error(&result, &key, &asc);
 
 			offset += blocksize;
 			tl     -= blocksize;
@@ -277,13 +270,13 @@ write:
 
 		if (ret != length) {
 			eprintf("Error on read %x %x", ret, length);
-			set_medium_error(&result, &key, &asc);
+			scsi_set_medium_error(&result, &key, &asc);
 		}
 		break;
 	case PRE_FETCH_10:
 	case PRE_FETCH_16:
 		if (ret != 0)
-			set_medium_error(&result, &key, &asc);
+			scsi_set_medium_error(&result, &key, &asc);
 		break;
 	case VERIFY_10:
 	case VERIFY_12:
@@ -302,7 +295,7 @@ verify:
 		ret = glfs_pread(gfd, tmpbuf, length, offset, lu->bsoflags);
 
 		if (ret != length)
-			set_medium_error(&result, &key, &asc);
+			scsi_set_medium_error(&result, &key, &asc);
 		else if (memcmp(scsi_get_out_buffer(cmd), tmpbuf, length)) {
 			result = SAM_STAT_CHECK_CONDITION;
 			key = MISCOMPARE;
diff --git a/usr/bs_rbd.c b/usr/bs_rbd.c
index f48c4a7..4ef005d 100644
--- a/usr/bs_rbd.c
+++ b/usr/bs_rbd.c
@@ -89,13 +89,6 @@ static void parse_imagepath(char *path, char **pool, char **image, char **snap)
 	free(origp);
 }
 
-static void set_medium_error(int *result, uint8_t *key, uint16_t *asc)
-{
-	*result = SAM_STAT_CHECK_CONDITION;
-	*key = MEDIUM_ERROR;
-	*asc = ASC_READ_ERROR;
-}
-
 static void bs_sync_sync_range(struct scsi_cmd *cmd, uint32_t length,
 			       int *result, uint8_t *key, uint16_t *asc)
 {
@@ -103,7 +96,7 @@ static void bs_sync_sync_range(struct scsi_cmd *cmd, uint32_t length,
 
 	ret = rbd_flush(RBDP(cmd->dev)->rbd_image);
 	if (ret)
-		set_medium_error(result, key, asc);
+		scsi_set_medium_error(result, key, asc);
 }
 
 static void bs_rbd_request(struct scsi_cmd *cmd)
@@ -148,7 +141,7 @@ static void bs_rbd_request(struct scsi_cmd *cmd)
 		ret = rbd_read(rbd->rbd_image, offset, length, tmpbuf);
 
 		if (ret != length) {
-			set_medium_error(&result, &key, &asc);
+			scsi_set_medium_error(&result, &key, &asc);
 			free(tmpbuf);
 			break;
 		}
@@ -185,7 +178,7 @@ static void bs_rbd_request(struct scsi_cmd *cmd)
 		ret = rbd_read(rbd->rbd_image, offset, length, tmpbuf);
 
 		if (ret != length) {
-			set_medium_error(&result, &key, &asc);
+			scsi_set_medium_error(&result, &key, &asc);
 			free(tmpbuf);
 			break;
 		}
@@ -263,7 +256,7 @@ write:
 				bs_sync_sync_range(cmd, length, &result, &key,
 						   &asc);
 		} else
-			set_medium_error(&result, &key, &asc);
+			scsi_set_medium_error(&result, &key, &asc);
 
 		if (do_verify)
 			goto verify;
@@ -300,7 +293,7 @@ write:
 			ret = rbd_write(rbd->rbd_image, offset, blocksize,
 					tmpbuf);
 			if (ret != blocksize)
-				set_medium_error(&result, &key, &asc);
+				scsi_set_medium_error(&result, &key, &asc);
 
 			offset += blocksize;
 			tl     -= blocksize;
@@ -315,7 +308,7 @@ write:
 			       scsi_get_in_buffer(cmd));
 
 		if (ret != length)
-			set_medium_error(&result, &key, &asc);
+			scsi_set_medium_error(&result, &key, &asc);
 
 		break;
 	case PRE_FETCH_10:
@@ -338,7 +331,7 @@ verify:
 		ret = rbd_read(rbd->rbd_image, offset, length, tmpbuf);
 
 		if (ret != length)
-			set_medium_error(&result, &key, &asc);
+			scsi_set_medium_error(&result, &key, &asc);
 		else if (memcmp(scsi_get_out_buffer(cmd), tmpbuf, length)) {
 			result = SAM_STAT_CHECK_CONDITION;
 			key = MISCOMPARE;
diff --git a/usr/bs_rdwr.c b/usr/bs_rdwr.c
index 09f6bb8..3f618ee 100644
--- a/usr/bs_rdwr.c
+++ b/usr/bs_rdwr.c
@@ -41,13 +41,6 @@
 #include "spc.h"
 #include "bs_thread.h"
 
-static void set_medium_error(int *result, uint8_t *key, uint16_t *asc)
-{
-	*result = SAM_STAT_CHECK_CONDITION;
-	*key = MEDIUM_ERROR;
-	*asc = ASC_READ_ERROR;
-}
-
 static void bs_sync_sync_range(struct scsi_cmd *cmd, uint32_t length,
 			       int *result, uint8_t *key, uint16_t *asc)
 {
@@ -55,7 +48,7 @@ static void bs_sync_sync_range(struct scsi_cmd *cmd, uint32_t length,
 
 	ret = fdatasync(cmd->dev->fd);
 	if (ret)
-		set_medium_error(result, key, asc);
+		scsi_set_medium_error(result, key, asc);
 }
 
 static void bs_rdwr_request(struct scsi_cmd *cmd)
@@ -92,7 +85,7 @@ static void bs_rdwr_request(struct scsi_cmd *cmd)
 		ret = pread64(fd, tmpbuf, length, offset);
 
 		if (ret != length) {
-			set_medium_error(&result, &key, &asc);
+			scsi_set_medium_error(&result, &key, &asc);
 			free(tmpbuf);
 			break;
 		}
@@ -129,7 +122,7 @@ static void bs_rdwr_request(struct scsi_cmd *cmd)
 		ret = pread64(fd, tmpbuf, length, offset);
 
 		if (ret != length) {
-			set_medium_error(&result, &key, &asc);
+			scsi_set_medium_error(&result, &key, &asc);
 			free(tmpbuf);
 			break;
 		}
@@ -207,7 +200,7 @@ write:
 				bs_sync_sync_range(cmd, length, &result, &key,
 						   &asc);
 		} else
-			set_medium_error(&result, &key, &asc);
+			scsi_set_medium_error(&result, &key, &asc);
 
 		if ((cmd->scb[0] != WRITE_6) && (cmd->scb[1] & 0x10))
 			posix_fadvise(fd, offset, length,
@@ -246,7 +239,7 @@ write:
 
 			ret = pwrite64(fd, tmpbuf, blocksize, offset);
 			if (ret != blocksize)
-				set_medium_error(&result, &key, &asc);
+				scsi_set_medium_error(&result, &key, &asc);
 
 			offset += blocksize;
 			tl     -= blocksize;
@@ -261,7 +254,7 @@ write:
 			      offset);
 
 		if (ret != length)
-			set_medium_error(&result, &key, &asc);
+			scsi_set_medium_error(&result, &key, &asc);
 
 		if ((cmd->scb[0] != READ_6) && (cmd->scb[1] & 0x10))
 			posix_fadvise(fd, offset, length,
@@ -274,7 +267,7 @@ write:
 				POSIX_FADV_WILLNEED);
 
 		if (ret != 0)
-			set_medium_error(&result, &key, &asc);
+			scsi_set_medium_error(&result, &key, &asc);
 		break;
 	case VERIFY_10:
 	case VERIFY_12:
@@ -293,7 +286,7 @@ verify:
 		ret = pread64(fd, tmpbuf, length, offset);
 
 		if (ret != length)
-			set_medium_error(&result, &key, &asc);
+			scsi_set_medium_error(&result, &key, &asc);
 		else if (memcmp(scsi_get_out_buffer(cmd), tmpbuf, length)) {
 			result = SAM_STAT_CHECK_CONDITION;
 			key = MISCOMPARE;
diff --git a/usr/bs_sheepdog.c b/usr/bs_sheepdog.c
index f702c12..c8e5e47 100644
--- a/usr/bs_sheepdog.c
+++ b/usr/bs_sheepdog.c
@@ -1285,13 +1285,6 @@ static void sd_close(struct sheepdog_access_info *ai)
 		eprintf("%s, %s", sd_strerror(rsp->result), ai->inode.name);
 }
 
-static void set_medium_error(int *result, uint8_t *key, uint16_t *asc)
-{
-	*result = SAM_STAT_CHECK_CONDITION;
-	*key = MEDIUM_ERROR;
-	*asc = ASC_READ_ERROR;
-}
-
 struct inflight_thread {
 	unsigned long min_idx, max_idx;
 	struct list_head list;
@@ -1350,7 +1343,7 @@ static void bs_sheepdog_request(struct scsi_cmd *cmd)
 	case SYNCHRONIZE_CACHE_16:
 		ret = sd_sync(ai);
 		if (ret)
-			set_medium_error(&result, &key, &asc);
+			scsi_set_medium_error(&result, &key, &asc);
 		break;
 	case WRITE_6:
 	case WRITE_10:
@@ -1368,7 +1361,7 @@ static void bs_sheepdog_request(struct scsi_cmd *cmd)
 			    length, cmd->offset);
 
 		if (ret)
-			set_medium_error(&result, &key, &asc);
+			scsi_set_medium_error(&result, &key, &asc);
 		break;
 	case READ_6:
 	case READ_10:
@@ -1385,7 +1378,7 @@ static void bs_sheepdog_request(struct scsi_cmd *cmd)
 		ret = sd_io(ai, 0, scsi_get_in_buffer(cmd),
 			    length, cmd->offset);
 		if (ret)
-			set_medium_error(&result, &key, &asc);
+			scsi_set_medium_error(&result, &key, &asc);
 		break;
 	default:
 		eprintf("cmd->scb[0]: %x\n", cmd->scb[0]);
diff --git a/usr/scsi.c b/usr/scsi.c
index 4eccf13..51caa0a 100644
--- a/usr/scsi.c
+++ b/usr/scsi.c
@@ -595,3 +595,10 @@ enum data_direction scsi_data_dir_opcode(unsigned char op)
 
 	return dir;
 }
+
+void scsi_set_medium_error(int *result, uint8_t *key, uint16_t *asc)
+{
+	*result = SAM_STAT_CHECK_CONDITION;
+	*key = MEDIUM_ERROR;
+	*asc = ASC_READ_ERROR;
+}
diff --git a/usr/tgtd.h b/usr/tgtd.h
index cf3ade3..e418b36 100644
--- a/usr/tgtd.h
+++ b/usr/tgtd.h
@@ -339,6 +339,7 @@ extern int get_scsi_cdb_size(struct scsi_cmd *cmd);
 extern int get_scsi_command_size(unsigned char op);
 extern const unsigned char *get_scsi_cdb_usage_data(unsigned char op,
 						    unsigned char sa);
+extern void scsi_set_medium_error(int *result, uint8_t *key, uint16_t *asc);
 
 extern enum scsi_target_state tgt_get_target_state(int tid);
 extern tgtadm_err tgt_set_target_state(int tid, char *str);
-- 
2.11.0




[Index of Archives]     [Linux SCSI]     [Linux RAID]     [Linux Clusters]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]

  Powered by Linux