[stgt] [PATCH 4/8] ssc: add helpers for accessing to the on-disk blk_header structure

FUJITA Tomonori fujita.tomonori at lab.ntt.co.jp
Sun Oct 12 06:38:49 CEST 2008


This is preparation for making the on-disk blk_header structure big
endian.

Signed-off-by: FUJITA Tomonori <fujita.tomonori at lab.ntt.co.jp>
---
 usr/bs_ssc.c    |   36 +++++++++++++++---------------------
 usr/dump_tape.c |   23 +++++++++--------------
 usr/libssc.c    |   22 ++++++++++++++++++++++
 usr/libssc.h    |    2 ++
 usr/mktape.c    |   10 +++++-----
 5 files changed, 53 insertions(+), 40 deletions(-)

diff --git a/usr/bs_ssc.c b/usr/bs_ssc.c
index d9f7036..1adc02f 100644
--- a/usr/bs_ssc.c
+++ b/usr/bs_ssc.c
@@ -69,14 +69,10 @@ static int32_t be24_to_2comp(uint8_t *c)
 
 static int skip_next_header(struct scsi_lu *lu)
 {
-	ssize_t rd;
 	struct ssc_info *ssc = dtype_priv(lu);
 	struct blk_header *h = ssc->c_blk;
 
-	rd = pread(lu->fd, h, sizeof(struct blk_header), h->next);
-	if (rd != sizeof(struct blk_header))
-		return 1;
-	return 0;
+	return ssc_read_blkhdr(lu->fd, h, h->next);
 }
 
 static int skip_prev_header(struct scsi_lu *lu)
@@ -85,8 +81,8 @@ static int skip_prev_header(struct scsi_lu *lu)
 	struct ssc_info *ssc = dtype_priv(lu);
 	struct blk_header *h = ssc->c_blk;
 
-	rd = pread(lu->fd, h, sizeof(struct blk_header), h->prev);
-	if (rd != sizeof(struct blk_header))
+	rd = ssc_read_blkhdr(lu->fd, h, h->prev);
+	if (rd)
 		return 1;
 	if (h->blk_type == BLK_BOT)
 		return skip_next_header(lu);
@@ -105,12 +101,11 @@ static int resp_rewind(struct scsi_lu *lu)
 
 	eprintf("*** Backing store fd: %s %d %d ***\n", lu->path, lu->fd, fd);
 
-	rd = pread(fd, h, sizeof(struct blk_header), 0);
-	if (rd < 0)
-		eprintf("Could not read %d bytes:%m\n",
-				(int)sizeof(struct blk_header));
-	if (rd != sizeof(struct blk_header))
+	rd = ssc_read_blkhdr(fd, h, 0);
+	if (rd) {
+		eprintf("Could not read %Zd bytes:%m\n", sizeof(*h));
 		return 1;
+	}
 
 	return skip_next_header(lu);
 }
@@ -172,15 +167,15 @@ static int append_blk(struct scsi_cmd *cmd, uint8_t *data,
 			eod->ondisk_sz);
 
 	/* Rewrite previous header with updated positioning info */
-	ret = pwrite(fd, curr, sizeof(struct blk_header), (off_t)curr->curr);
-	if (ret != sizeof(struct blk_header)) {
+	ret = ssc_write_blkhdr(fd, curr, curr->curr);
+	if (ret) {
 		eprintf("Rewrite of blk header failed: %m\n");
 		sense_data_build(cmd, MEDIUM_ERROR, ASC_WRITE_ERROR);
 		goto failed_write;
 	}
 	/* Write new EOD blk header */
-	ret = pwrite(fd, eod, sizeof(struct blk_header), (off_t)eod->curr);
-	if (ret != sizeof(struct blk_header)) {
+	ret = ssc_write_blkhdr(fd, eod, eod->curr);
+	if (ret) {
 		eprintf("Write of EOD blk header failed: %m\n");
 		sense_data_build(cmd, MEDIUM_ERROR, ASC_WRITE_ERROR);
 		goto failed_write;
@@ -630,8 +625,8 @@ static int bs_tape_open(struct scsi_lu *lu, char *path, int *fd, uint64_t *size)
 
 	/* Can't call 'resp_rewind() at this point as lu data not
 	 * setup */
-	rd = pread(*fd, ssc->c_blk, sizeof(struct blk_header), 0);
-	if (rd < sizeof(struct blk_header)) {
+	rd = ssc_read_blkhdr(*fd, ssc->c_blk, 0);
+	if (rd) {
 		eprintf("Failed to read complete blk header: %d %m\n", (int)rd);
 		goto read_failed;
 	}
@@ -642,9 +637,8 @@ static int bs_tape_open(struct scsi_lu *lu, char *path, int *fd, uint64_t *size)
 		goto read_failed;
 	}
 
-	rd = pread(*fd, ssc->c_blk, sizeof(struct blk_header),
-					ssc->c_blk->next);
-	if (rd < sizeof(struct blk_header)) {
+	rd = ssc_read_blkhdr(*fd, ssc->c_blk, ssc->c_blk->next);
+	if (rd) {
 		eprintf("Failed to read complete blk header: %d %m\n", (int)rd);
 		goto read_failed;
 	}
diff --git a/usr/dump_tape.c b/usr/dump_tape.c
index ff2124d..1816f8d 100644
--- a/usr/dump_tape.c
+++ b/usr/dump_tape.c
@@ -84,19 +84,13 @@ void print_current_header(struct blk_header *pos)
 
 int skip_to_next_header(int fd, struct blk_header *pos)
 {
-	loff_t nread;
+	int ret;
 
-	nread = lseek64(fd, (uint64_t)pos->next, SEEK_SET);
-	if ((uint64_t)pos->next != nread) {
-		printf("Error while seeking to next header\n");
-		return -1;
-	}
-	nread = read(fd, pos, sizeof(struct blk_header));
-	if (nread < sizeof(struct blk_header)) {
+	ret = ssc_read_blkhdr(fd, pos, pos->next);
+	if (ret)
 		printf("Could not read complete blk header - short read!!\n");
-		return -1;
-	}
-	return 0;
+
+	return ret;
 }
 
 int main(int argc, char *argv[])
@@ -147,8 +141,9 @@ int main(int argc, char *argv[])
 		perror("Could not open");
 		exit(1);
 	}
-	nread = read(ofp, &current_position, sizeof(struct blk_header));
-	if (nread < sizeof(current_position)) {
+
+	nread = ssc_read_blkhdr(ofp, &current_position, 0);
+	if (nread) {
 		perror("Could not read blk header");
 		exit(1);
 	}
@@ -203,7 +198,7 @@ int main(int argc, char *argv[])
 	print_current_header(&current_position);
 	while (current_position.blk_type != BLK_EOD) {
 		nread = skip_to_next_header(ofp, &current_position);
-		if (nread == -1)
+		if (nread)
 			break;
 		print_current_header(&current_position);
 	}
diff --git a/usr/libssc.c b/usr/libssc.c
index 297867e..eba7b74 100644
--- a/usr/libssc.c
+++ b/usr/libssc.c
@@ -178,3 +178,25 @@ int ssc_write_mam_info(int fd, struct MAM_info *i)
 
 	return  0;
 }
+
+int ssc_read_blkhdr(int fd, struct blk_header *h, off_t offset)
+{
+	size_t count;
+
+	count = pread(fd, h, sizeof(*h), offset);
+	if (count != sizeof(*h))
+		return 1;
+
+	return 0;
+}
+
+int ssc_write_blkhdr(int fd, struct blk_header *h, off_t offset)
+{
+	size_t count;
+
+	count = pwrite(fd, h, sizeof(*h), offset);
+	if (count != sizeof(*h))
+		return 1;
+
+	return 0;
+}
diff --git a/usr/libssc.h b/usr/libssc.h
index 891bb30..b278509 100644
--- a/usr/libssc.h
+++ b/usr/libssc.h
@@ -3,5 +3,7 @@
 
 extern int ssc_read_mam_info(int fd, struct MAM_info *i);
 extern int ssc_write_mam_info(int fd, struct MAM_info *i);
+extern int ssc_read_blkhdr(int fd, struct blk_header *h, off_t offset);
+extern int ssc_write_blkhdr(int fd, struct blk_header *h, off_t offset);
 
 #endif
diff --git a/usr/mktape.c b/usr/mktape.c
index 8278317..20cb709 100644
--- a/usr/mktape.c
+++ b/usr/mktape.c
@@ -52,7 +52,6 @@ int main(int argc, char *argv[])
 	struct blk_header h;
 	struct MAM_info mi;
 	uint8_t current_media[1024];
-	long nwrite;
 	char *progname = argv[0];
 	char *barcode = NULL;
 	char *media_type = NULL;
@@ -165,8 +164,9 @@ int main(int argc, char *argv[])
 		perror("Failed creating file");
 		exit(2);
 	}
-	nwrite = write(file, &h, sizeof(h));
-	if (nwrite <= 0) {
+
+	ret = ssc_write_blkhdr(file, &h, 0);
+	if (ret) {
 		perror("Unable to write header");
 		exit(1);
 	}
@@ -185,8 +185,8 @@ int main(int argc, char *argv[])
 	h.next = lseek64(file, 0, SEEK_CUR);
 	h.curr = h.next;
 
-	nwrite = write(file, &h, sizeof(h));
-	if (nwrite <= 0) {
+	ret = ssc_write_blkhdr(file, &h, h.next);
+	if (ret) {
 		perror("Unable to write header");
 		exit(1);
 	}
-- 
1.5.6.5

--
To unsubscribe from this list: send the line "unsubscribe stgt" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html



More information about the stgt mailing list