[stgt] [PATCH 07/15 ver2] tgt: os.h: sync_file_range is OS dependent

Boaz Harrosh bharrosh at panasas.com
Mon Mar 2 17:00:23 CET 2009


Introduce a new header that includes definitions of all
OS specific services. stgt code will use abstract API from
os.h. (start with os_xxx). Then a linux/os.c and bsd/os.c
implement these services for the needed platform.

First such service is sync_file_range which needs to be
emulated in bsd.

Signed-off-by: Boaz Harrosh <bharrosh at panasas.com>
---
 usr/Makefile   |    3 +++
 usr/bs_mmap.c  |    2 +-
 usr/bs_rdwr.c  |    2 +-
 usr/bsd/os.c   |   33 +++++++++++++++++++++++++++++++++
 usr/linux/os.c |   54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 usr/os.h       |   15 +++++++++++++++
 usr/util.h     |   38 +-------------------------------------
 7 files changed, 108 insertions(+), 39 deletions(-)
 create mode 100644 usr/bsd/os.c
 create mode 100644 usr/linux/os.c
 create mode 100644 usr/os.h

diff --git a/usr/Makefile b/usr/Makefile
index 3fc848e..03b3c90 100644
--- a/usr/Makefile
+++ b/usr/Makefile
@@ -65,6 +65,9 @@ TGTD_OBJS += tgtd.o mgmt.o target.o scsi.o log.o driver.o util.o work.o \
 		parser.o spc.o sbc.o mmc.o osd.o scc.o smc.o \
 		ssc.o bs_ssc.o libssc.o \
 		bs_null.o bs_sg.o bs.o libcrc32c.o
+
+TGTD_OBJS += linux/os.o
+
 MANPAGES = ../doc/manpages/tgtadm.8 ../doc/manpages/tgt-admin.8 \
 		../doc/manpages/tgt-setup-lun.8
 DOCS = ../doc/README.fcoe ../doc/README.ibmvstgt ../doc/README.iscsi ../doc/README.iser \
diff --git a/usr/bs_mmap.c b/usr/bs_mmap.c
index bb24f5e..8eb6a47 100644
--- a/usr/bs_mmap.c
+++ b/usr/bs_mmap.c
@@ -60,7 +60,7 @@ static void bs_mmap_request(struct scsi_cmd *cmd)
 		unsigned int flags =
 			SYNC_FILE_RANGE_WAIT_BEFORE| SYNC_FILE_RANGE_WRITE;
 
-		ret = __sync_file_range(cmd->dev->fd, cmd->offset, length, flags);
+		ret = os_sync_file_range(cmd->dev->fd, cmd->offset, length, flags);
 		if (ret) {
 			result = SAM_STAT_CHECK_CONDITION;
 			key = MEDIUM_ERROR;
diff --git a/usr/bs_rdwr.c b/usr/bs_rdwr.c
index 65a6136..0c21e01 100644
--- a/usr/bs_rdwr.c
+++ b/usr/bs_rdwr.c
@@ -51,7 +51,7 @@ static void bs_sync_sync_range(struct scsi_cmd *cmd, uint32_t length,
 	int ret;
 	unsigned int flags = SYNC_FILE_RANGE_WAIT_BEFORE| SYNC_FILE_RANGE_WRITE;
 
-	ret = __sync_file_range(cmd->dev->fd, cmd->offset, length, flags);
+	ret = os_sync_file_range(cmd->dev->fd, cmd->offset, length, flags);
 	if (ret)
 		set_medium_error(result, key, asc);
 }
diff --git a/usr/bsd/os.c b/usr/bsd/os.c
new file mode 100644
index 0000000..7fbcd5c
--- /dev/null
+++ b/usr/bsd/os.c
@@ -0,0 +1,33 @@
+/*
+ *  OS dependent services implementation for BSD platform
+ *
+ * Copyright (C) 2009 Boaz Harrosh <bharrosh at panasas.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation, version 2 of the
+ * License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+#include <errno.h>
+#include <unistd.h>
+
+#include <linux/fs.h>
+
+#include "os.h"
+
+int os_sync_file_range(int fd, __off64_t offset, __off64_t bytes,
+			unsigned int flags)
+{
+	return fsync(fd);
+}
diff --git a/usr/linux/os.c b/usr/linux/os.c
new file mode 100644
index 0000000..2094eec
--- /dev/null
+++ b/usr/linux/os.c
@@ -0,0 +1,54 @@
+/*
+ *  OS dependent services implementation for Linux platform
+ *
+ * Copyright (C) 2009 Boaz Harrosh <bharrosh at panasas.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation, version 2 of the
+ * License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+#include <errno.h>
+#include <unistd.h>
+#include <sys/syscall.h>
+
+#include "os.h"
+
+/*
+ * the latest glibc have a proper sync_file_range definition but
+ * most of the distributions aren't shipped with it yet.
+*/
+#ifndef __NR_sync_file_range
+#if defined(__i386)
+#define __NR_sync_file_range	314
+#elif defined(__x86_64__)
+#define __NR_sync_file_range	277
+#elif defined(__ia64__)
+#define __NR_sync_file_range	1300
+#elif defined(__powerpc64__) || defined(__PPC__)
+#define __NR_sync_file_range	308
+#endif
+#endif
+
+int os_sync_file_range(int fd, __off64_t offset, __off64_t bytes,
+			unsigned int flags)
+{
+	int ret;
+
+	ret = syscall(__NR_sync_file_range, fd, offset, bytes, flags);
+	if (ret == -EPERM)
+		ret = fsync(fd);
+	return ret;
+}
+
diff --git a/usr/os.h b/usr/os.h
new file mode 100644
index 0000000..c32ce29
--- /dev/null
+++ b/usr/os.h
@@ -0,0 +1,15 @@
+#ifndef __TGT_OS_H__
+#define __TGT_OS_H__
+
+#include <sys/types.h>
+#include <linux/fs.h>
+
+#ifndef SYNC_FILE_RANGE_WAIT_BEFORE
+#define SYNC_FILE_RANGE_WAIT_BEFORE	1
+#define SYNC_FILE_RANGE_WRITE		2
+#define SYNC_FILE_RANGE_WAIT_AFTER	4
+#endif
+int os_sync_file_range(int fd, __off64_t offset, __off64_t bytes,
+			unsigned int flags);
+
+#endif /* ndef __TGT_OS_H__*/
diff --git a/usr/util.h b/usr/util.h
index 419f648..18e0eb0 100644
--- a/usr/util.h
+++ b/usr/util.h
@@ -7,6 +7,7 @@
 #include <errno.h>
 #include <endian.h>
 #include "be_byteshift.h"
+#include "os.h"
 
 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
@@ -101,41 +102,4 @@ do {									\
 
 extern unsigned long pagesize, pageshift;
 
-
-/*
- * the latest glibc have a proper sync_file_range definition but
- * most of the distributions aren't shipped with it yet.
-*/
-
-#ifndef __NR_sync_file_range
-#if defined(__i386)
-#define __NR_sync_file_range	314
-#elif defined(__x86_64__)
-#define __NR_sync_file_range	277
-#elif defined(__ia64__)
-#define __NR_sync_file_range	1300
-#elif defined(__powerpc64__) || defined(__PPC__)
-#define __NR_sync_file_range	308
-#endif
-#endif
-
-#ifndef SYNC_FILE_RANGE_WAIT_BEFORE
-#define SYNC_FILE_RANGE_WAIT_BEFORE	1
-#define SYNC_FILE_RANGE_WRITE		2
-#define SYNC_FILE_RANGE_WAIT_AFTER	4
-#endif
-
-extern long int syscall(long int sysno, ...);
-
-static inline int __sync_file_range(int fd, __off64_t offset, __off64_t bytes,
-				    unsigned int flags)
-{
-	int ret;
-
-	ret = syscall(__NR_sync_file_range, fd, offset, bytes, flags);
-	if (ret == -EPERM)
-		ret = fsync(fd);
-	return ret;
-}
-
 #endif
-- 
1.6.0.6


--
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