New patchset, incorporating everybody's comments. Thanks for all that helped. Attached a "git-diff -M --stat -p" from the RFC. It's got lots of stuff by the idea is clear, (I think) Here is the list of patches: [PATCH 01/15] tgt: Don't interfere with internal OSD affairs [PATCH 02/15] tgt: usr/Makefile: Remove none existent include directories [PATCH 03/15] tgt: Fix eprintf warnings Three somewhat independent patches, before we start. (3rd patch here is new) [PATCH 04/15] tgt: Move away from global name space (FreeBSD) [PATCH 05/15] tgt: util.h BSD builds support [PATCH 06/15] tgt: BSD builds support - headers includes Changes that are needed in BSD but do nothing in Linux [PATCH 07/15] tgt: os.h: sync_file_range is OS dependent [PATCH 08/15] tgt: os.h: Services unavailable in BSD [PATCH 09/15] tgt: os.h - bind() is picky about names [PATCH 10/15] tgt: os.h: getnameinfo() is different on none-Linux systems Introduce a usr/os.h header that has portable API definitions. The implementations of this API is in usr/linux/os.c && usr/bsd/os.c. [PATCH 11/15] tgt: BSD builds support of sys/epoll.h via BSD's kevent [PATCH 12/15] tgt: usr/bsd headers, emulates some Linuxism in BSD [PATCH 13/15] tgt: BSD builds, some setsockopt are not supported [PATCH 14/15] tgt: os.h: semtimedop for BSD Some BSD only tricks, and Linux emulation on BSD. [PATCH 15/15] tgt: usr/Makefile BSD build Support Finally bring everything together to enable BSD build I do not think any of the patches should break anything in run time. They do compile with all make options turned on. On BSD I was only able to test with osd target and iscsi transport. Other backstores might work as well. These patches as well as osdemu, can be accessed at: git://git.open-osd.org/osc-osd/tgt - osd branch or viewed http://git.open-osd.org/gitweb.cgi?p=osc-osd/tgt/.git;a=summary Thanks in advance Boaz
git diff --stat -p -M d6cb6ceb2c147d456a378ed473184388c75a2c36 fcf171ebb3c04eb27ac1a8e2357abf672b0460bb usr/Makefile | 28 +++++--- usr/bs_aio.h | 24 ------- usr/bs_mmap.c | 2 +- usr/bs_rdwr.c | 2 +- {bsd_include => usr/bsd}/byteswap.h | 6 +- {bsd_include => usr/bsd}/endian.h | 6 +- usr/{bsd_epoll.c => bsd/epoll.c} | 48 +++++++------ {bsd_include => usr/bsd}/linux/fs.h | 9 ++- {bsd_include => usr/bsd}/netinet/in.h | 6 +- usr/bsd/os.c | 75 +++++++++++++++++++++ {bsd_include => usr/bsd}/sys/epoll.h | 4 +- usr/bsd/sys/socket.h | 23 +++++++ usr/fcoe/fcoe_dev.c | 2 +- usr/iscsi/iscsi_tcp.c | 15 +---- usr/iscsi/iscsid.c | 14 +--- usr/libssc.c | 3 + usr/linux/os.c | 116 +++++++++++++++++++++++++++++++++ usr/log.c | 1 + usr/mgmt.c | 43 +------------ usr/os.h | 30 +++++++++ usr/tgtd.c | 25 +------- usr/tgtif.c | 5 +- usr/tgtimg.c | 2 + usr/util.c | 7 +-- usr/util.h | 40 +----------- 25 files changed, 328 insertions(+), 208 deletions(-) diff --git a/usr/Makefile b/usr/Makefile index 168da20..f4e3819 100644 --- a/usr/Makefile +++ b/usr/Makefile @@ -4,6 +4,8 @@ EXTRAVERSION = $(if $(shell git-show-ref 2>/dev/null),-git-$(shell git-show-ref mandir = /usr/share/man docdir = /usr/share/doc/tgt +UNAME ?= $(shell uname) + ifneq ($(IBMVIO),) CFLAGS += -DIBMVIO -DUSE_KERNEL TGTD_OBJS += $(addprefix ibmvio/, ibmvio.o) @@ -15,7 +17,10 @@ CFLAGS += -DISCSI TGTD_OBJS += $(addprefix iscsi/, conn.o param.o session.o \ iscsid.o target.o chap.o transport.o iscsi_tcp.o \ isns.o) -TGTD_OBJS += bs_rdwr.o bs_aio.o +TGTD_OBJS += bs_rdwr.o +ifneq (FreeBSD,$(UNAME)) +TGTD_OBJS += bs_aio.o +endif LIBS += -lcrypto ifneq ($(ISCSI_RDMA),) @@ -57,14 +62,6 @@ CFLAGS += $(INCLUDES) CFLAGS += -g -O2 -Wall -Wstrict-prototypes -fPIC CFLAGS += -DTGT_VERSION=\"$(VERSION)$(EXTRAVERSION)\" -UNAME ?= $(shell uname) -ifeq (FreeBSD,$(UNAME)) -CFLAGS += -D__MAKE_TGT_ON_BSD__ -CFLAGS += -I../bsd_include -TGTD_OBJS += bsd_epoll.o -LIBS := -L/usr/local/lib $(LIBS) -endif - LIBS += -lpthread PROGRAMS += tgtd tgtadm tgtimg @@ -72,7 +69,18 @@ SCRIPTS += ../scripts/tgt-setup-lun ../scripts/tgt-admin 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 + bs_null.o bs.o libcrc32c.o + +ifneq (FreeBSD,$(UNAME)) + TGTD_OBJS += bs_sg.o + TGTD_OBJS += linux/os.o +else + CFLAGS += -D__MAKE_TGT_ON_BSD__ + CFLAGS += -Ibsd/ + TGTD_OBJS += bsd/epoll.o + TGTD_OBJS += bsd/os.o +endif + 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_aio.h b/usr/bs_aio.h index c028848..f62e99c 100644 --- a/usr/bs_aio.h +++ b/usr/bs_aio.h @@ -132,58 +132,34 @@ static inline void io_prep_pwrite(struct iocb *iocb, int fd, void const *buf, static inline int io_setup(unsigned nr_reqs, io_context_t *ctx) { -#ifndef __MAKE_TGT_ON_BSD__ return syscall(__NR_io_setup, nr_reqs, ctx); -#else - return -1; -#endif } static inline long io_destroy(io_context_t ctx) { -#ifndef __MAKE_TGT_ON_BSD__ return syscall(__NR_io_destroy, ctx); -#else - return -1; -#endif } static inline int io_submit(io_context_t ctx, long n, struct iocb **paiocb) { -#ifndef __MAKE_TGT_ON_BSD__ return syscall(__NR_io_submit, ctx, n, paiocb); -#else - return -1; -#endif } static inline long io_cancel(io_context_t ctx, struct iocb *aiocb, struct io_event *res) { -#ifndef __MAKE_TGT_ON_BSD__ return syscall(__NR_io_cancel, ctx, aiocb, res); -#else - return -1; -#endif } static inline long io_getevents(io_context_t ctx, long min_nr, long nr, struct io_event *events, struct timespec *tmo) { -#ifndef __MAKE_TGT_ON_BSD__ return syscall(__NR_io_getevents, ctx, min_nr, nr, events, tmo); -#else - return -1; -#endif } static inline int eventfd(int count) { -#ifndef __MAKE_TGT_ON_BSD__ return syscall(__NR_eventfd, count); -#else - return -1; -#endif } #endif diff --git a/usr/bs_mmap.c b/usr/bs_mmap.c index f0037e8..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 73adf57..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/bsd_include/byteswap.h b/usr/bsd/byteswap.h similarity index 57% rename from bsd_include/byteswap.h rename to usr/bsd/byteswap.h index 9cc2723..c24ad87 100644 --- a/bsd_include/byteswap.h +++ b/usr/bsd/byteswap.h @@ -1,5 +1,5 @@ -#ifndef _BSD_BYTESWAP_H -#define _BSD_BYTESWAP_H 1 +#ifndef __TGTBSD_BYTESWAP_H__ +#define __TGTBSD_BYTESWAP_H__ #include <sys/endian.h> @@ -7,4 +7,4 @@ #define bswap_32(x) __bswap32(x) #define bswap_64(x) __bswap64(x) -#endif /* _BSD_BYTESWAP_H */ +#endif /* __TGTBSD_BYTESWAP_H__ */ diff --git a/bsd_include/endian.h b/usr/bsd/endian.h similarity index 84% rename from bsd_include/endian.h rename to usr/bsd/endian.h index 9ccc631..e5b6437 100644 --- a/bsd_include/endian.h +++ b/usr/bsd/endian.h @@ -1,5 +1,5 @@ -#ifndef __BSD_ENDIAN_H__ -#define __BSD_ENDIAN_H__ +#ifndef __TGTBSD_ENDIAN_H__ +#define __TGTBSD_ENDIAN_H__ #include <machine/endian.h> @@ -23,4 +23,4 @@ #endif /* _BYTE_ORDER == _LITTLE_ENDIAN */ -#endif /* __BSD_ENDIAN_H__ */ +#endif /* __TGTBSD_ENDIAN_H__ */ diff --git a/usr/bsd_epoll.c b/usr/bsd/epoll.c similarity index 72% rename from usr/bsd_epoll.c rename to usr/bsd/epoll.c index 621d262..66c783a 100644 --- a/usr/bsd_epoll.c +++ b/usr/bsd/epoll.c @@ -7,30 +7,28 @@ * Inspired by code written by Roman Divacky * from the freebsd's linux emulation project at linux_epoll.c file * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. + * description: + * This file implements a workable set of the <sys/epoll.h> header file + * taken from the gcc-on-linux distribution. The implementation translates + * back and forth from epoll calls and constants to Kevent calls and + * constants. * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. + * License: * - * This file implements a workable set of the <sys/epoll.h> header file - * taken from the gcc-on-linux distribution. The implementation translates - * back and forth from epoll calls and constants to Kevent calls and constants. + * 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 <sys/types.h> @@ -145,6 +143,12 @@ int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event) read_flags = write_flags = EV_DELETE | EV_DISABLE; /*printf("EPOLL_CTL_DEL(fd=%d\n",fd);*/ break; + default: + fprintf(stderr, "Unsupported epoll operation=%d" + " epfd=%d, fd=%d events=%x\n", + op, epfd, fd, event->events); + errno = EINVAL; + return -1; } EV_SET(&kev[0], fd, EVFILT_READ, read_flags, 0, 0, diff --git a/bsd_include/linux/fs.h b/usr/bsd/linux/fs.h similarity index 81% rename from bsd_include/linux/fs.h rename to usr/bsd/linux/fs.h index 42d5449..46a8186 100644 --- a/bsd_include/linux/fs.h +++ b/usr/bsd/linux/fs.h @@ -1,5 +1,5 @@ -#ifndef __FOR_BSD__LINUX_FS_H__ -#define __FOR_BSD__LINUX_FS_H__ +#ifndef __TGTBSD__LINUX_FS_H__ +#define __TGTBSD__LINUX_FS_H__ #include <sys/stat.h> #include <limits.h> @@ -11,6 +11,7 @@ typedef off_t __off64_t; #define off64_t __off64_t +#define loff_t __off64_t #define O_LARGEFILE 0 @@ -37,4 +38,6 @@ static inline int fdatasync(int fd) #define stat64 stat #define fstat64 fstat -#endif /* ndef __FOR_BSD__LINUX_FS_H__ */ +#define lseek64 lseek + +#endif /* ndef __TGTBSD__LINUX_FS_H__ */ diff --git a/bsd_include/netinet/in.h b/usr/bsd/netinet/in.h similarity index 60% rename from bsd_include/netinet/in.h rename to usr/bsd/netinet/in.h index 5bcb0ab..c43d350 100644 --- a/bsd_include/netinet/in.h +++ b/usr/bsd/netinet/in.h @@ -1,5 +1,5 @@ -#ifndef _BSD_IN_H -#define _BSD_IN_H 1 +#ifndef __TGTBSD_NETINET_IN_H__ +#define __TGTBSD_NETINET_IN_H__ /*FIXME: define _KERNEL is needed in BSD for some of the ipv6 stuff in * usr/iscsi/target.c @@ -8,4 +8,4 @@ #define _KERNEL #include <../include/netinet/in.h> -#endif /* _BSD_IN_H */ +#endif /* __TGTBSD_NETINET_IN_H__ */ diff --git a/usr/bsd/os.c b/usr/bsd/os.c new file mode 100644 index 0000000..6c98d15 --- /dev/null +++ b/usr/bsd/os.c @@ -0,0 +1,75 @@ +/* +* os.c: OS dependent services implementation on BSD platform +* +* Description: +* Implements these services in os.h on the BSD platform +* +* Author: Boaz Harrosh <bharrosh@xxxxxxxxxxx>, (C) 2009 +* +* Copyright: See COPYING file that comes with this distribution +* +*/ + +#include <errno.h> +#include <stdio.h> +#include <string.h> +#include <inttypes.h> +#include <unistd.h> +#include <netdb.h> +#include <sys/socket.h> +#include <sys/un.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); +} + +int os_ipc_perm(int fd) +{ + return 0; +} + +int os_oom_adjust(void) +{ + return 0; +} + +int os_blockdev_size(int fd, uint64_t *size) +{ + /* FIXME: how to get block-dev size in BSD */ + *size = ~0ULL; + return 0; +} + +int os_bind_ipc(int fd, struct sockaddr_un *addr, const char *ipc_name_space) +{ + strcpy(addr->sun_path, ipc_name_space); + addr->sun_len = SUN_LEN(addr) + 1; + + return bind(fd, (struct sockaddr *)addr, addr->sun_len); +} + +int os_getnameinfo_ss(struct sockaddr_storage *ss, char *host, size_t hostlen, + char *serv, size_t servlen, int flags) +{ + size_t slen = ((struct sockaddr *)&ss)->sa_len; + + return getnameinfo((struct sockaddr *)ss, slen, host, hostlen, + serv, servlen, flags); +} + +int bsd_setsockopt(int s, int level, int optname, const void *optval, + socklen_t optlen) +{ + if (optname == BSD_NOT_SUPPORTED) + return 0; + +#undef setsockop + return setsockopt(s, level, optname, optval, optlen); +} + diff --git a/bsd_include/sys/epoll.h b/usr/bsd/sys/epoll.h similarity index 98% rename from bsd_include/sys/epoll.h rename to usr/bsd/sys/epoll.h index 22b7e52..d243f53 100644 --- a/bsd_include/sys/epoll.h +++ b/usr/bsd/sys/epoll.h @@ -16,8 +16,8 @@ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -#ifndef _SYS_EPOLL_H -#define _SYS_EPOLL_H 1 +#ifndef __TGTBSD_SYS_EPOLL_H__ +#define __TGTBSD_SYS_EPOLL_H__ #include <stdint.h> #include <sys/types.h> diff --git a/usr/bsd/sys/socket.h b/usr/bsd/sys/socket.h new file mode 100644 index 0000000..c7d7b13 --- /dev/null +++ b/usr/bsd/sys/socket.h @@ -0,0 +1,23 @@ +#ifndef __TGTBSD_SYS_SOCKET_H__ +#define __TGTBSD_SYS_SOCKET_H__ + +/* Overide socket.h on BSD to define Linux specific options, in which case + * just return success. + */ + +#include <../include/sys/socket.h> + +#define setsockopt bsd_setsockopt + +#define BSD_NOT_SUPPORTED -1 +#define TCP_KEEPIDLE BSD_NOT_SUPPORTED +#define TCP_KEEPCNT BSD_NOT_SUPPORTED +#define TCP_KEEPINTVL BSD_NOT_SUPPORTED +#define TCP_CORK BSD_NOT_SUPPORTED +#define SOL_TCP BSD_NOT_SUPPORTED + +/* is in bsd/os.c */ +int bsd_setsockopt(int s, int level, int optname, const void *optval, + socklen_t optlen); + +#endif /* __TGTBSD_SYS_SOCKET_H__ */ diff --git a/usr/fcoe/fcoe_dev.c b/usr/fcoe/fcoe_dev.c index 2d0149c..e5ebf8a 100644 --- a/usr/fcoe/fcoe_dev.c +++ b/usr/fcoe/fcoe_dev.c @@ -248,7 +248,7 @@ int fcoe_rcv(struct fcdev *fdev) hlen = fc->fcoe_hlen; if (hlen != sizeof(struct fcoe_hdr)) { eprintf("Wrong fcoe header size. Got %u, but should " - "be %u. Make sure you are using a initiator that " + "be %zu. Make sure you are using a initiator that " "is using the current header format\n", hlen, sizeof(struct fcoe_hdr)); stats->ErrorFrames++; diff --git a/usr/iscsi/iscsi_tcp.c b/usr/iscsi/iscsi_tcp.c index e730101..bee2145 100644 --- a/usr/iscsi/iscsi_tcp.c +++ b/usr/iscsi/iscsi_tcp.c @@ -66,7 +66,6 @@ static int set_keepalive(int fd) if (ret) return ret; -#ifndef __MAKE_TGT_ON_BSD__ opt = 1800; ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &opt, sizeof(opt)); if (ret) @@ -81,7 +80,6 @@ static int set_keepalive(int fd) ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &opt, sizeof(opt)); if (ret) return ret; -#endif return 0; } @@ -281,27 +279,18 @@ static size_t iscsi_tcp_write_begin(struct iscsi_connection *conn, void *buf, size_t nbytes) { struct iscsi_tcp_connection *tcp_conn = TCP_CONN(conn); + int opt = 1; -#ifndef __MAKE_TGT_ON_BSD__ - { - int opt = 1; - setsockopt(tcp_conn->fd, SOL_TCP, TCP_CORK, &opt, sizeof(opt)); - } -#endif - - dprintf("fd=%d buf=%p nbytes=%zd\n", tcp_conn->fd, buf, nbytes); + setsockopt(tcp_conn->fd, SOL_TCP, TCP_CORK, &opt, sizeof(opt)); return write(tcp_conn->fd, buf, nbytes); } static void iscsi_tcp_write_end(struct iscsi_connection *conn) { -#ifndef __MAKE_TGT_ON_BSD__ struct iscsi_tcp_connection *tcp_conn = TCP_CONN(conn); int opt = 0; setsockopt(tcp_conn->fd, SOL_TCP, TCP_CORK, &opt, sizeof(opt)); - dprintf("iscsi_tcp_write_end\n"); -#endif } static size_t iscsi_tcp_close(struct iscsi_connection *conn) diff --git a/usr/iscsi/iscsid.c b/usr/iscsi/iscsid.c index dca926e..3d0a976 100644 --- a/usr/iscsi/iscsid.c +++ b/usr/iscsi/iscsid.c @@ -39,6 +39,7 @@ #include "driver.h" #include "scsi.h" #include "crc32c.h" +#include "os.h" #define MAX_QUEUE_CMD 128 @@ -771,13 +772,8 @@ static void text_scan_text(struct iscsi_connection *conn) blen--; } -#ifndef __MAKE_TGT_ON_BSD__ - slen = sizeof(ss); -#else - slen = ((struct sockaddr *)&ss)->sa_len; -#endif - ret = getnameinfo((struct sockaddr *) &ss, slen, p, - blen, NULL, 0, NI_NUMERICHOST); + ret = os_getnameinfo_ss(&ss, p, blen, NULL, 0, + NI_NUMERICHOST); if (ret) { eprintf("getnameinfo Faild=>%d: %s\n", ret, gai_strerror(ret)); @@ -2087,10 +2083,8 @@ again: case IOSTATE_TX_DATA: ret = do_send(conn, ddigest ? IOSTATE_TX_INIT_DDIGEST : IOSTATE_TX_END); - if (ret < 0) { - dprintf("do_send => %d\n", ret); + if (ret < 0) goto out; - } if (conn->tx_iostate != IOSTATE_TX_INIT_DDIGEST) break; case IOSTATE_TX_INIT_DDIGEST: diff --git a/usr/libssc.c b/usr/libssc.c index e13a9f6..7cd965e 100644 --- a/usr/libssc.c +++ b/usr/libssc.c @@ -23,6 +23,9 @@ #include <string.h> #include <unistd.h> #include <stdio.h> + +#include <linux/fs.h> + #include "bs_ssc.h" #include "ssc.h" #include "be_byteshift.h" diff --git a/usr/linux/os.c b/usr/linux/os.c new file mode 100644 index 0000000..3392637 --- /dev/null +++ b/usr/linux/os.c @@ -0,0 +1,116 @@ +/* +* os.c: OS dependent services implementation on Linux platform +* +* Description: +* +* +* Author: Boaz Harrosh <bharrosh@xxxxxxxxxxx>, (C) 2009 +* +* Copyright: See COPYING file that comes with this distribution +* +*/ + +#include <errno.h> +#include <stdio.h> +#include <string.h> +#include <inttypes.h> +#include <netdb.h> +#include <unistd.h> +#include <fcntl.h> +#include <sys/ioctl.h> +#include <sys/socket.h> +#include <sys/syscall.h> +#include <sys/un.h> + +#include <linux/fs.h> + +#include "os.h" +#include "log.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; +} + +int os_ipc_perm(int fd) +{ + struct ucred cred; + socklen_t len; + int err; + + len = sizeof(cred); + err = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void *) &cred, &len); + if (err) { + eprintf("can't get sockopt, %m\n"); + return -1; + } + + if (cred.uid || cred.gid) + return -EPERM; + + return 0; +} + +int os_oom_adjust(void) +{ + int fd, err; + char path[64]; + + /* Avoid oom-killer */ + sprintf(path, "/proc/%d/oom_adj", getpid()); + fd = open(path, O_WRONLY); + if (fd < 0) { + eprintf("can't adjust oom-killer's pardon %s, %m\n", path); + return errno; + } + err = write(fd, "-17\n", 4); + if (err < 0) { + eprintf("can't adjust oom-killer's pardon %s, %m\n", path); + return errno; + } + close(fd); + return 0; +} + +int os_blockdev_size(int fd, uint64_t *size) +{ + return ioctl(fd, BLKGETSIZE64, size); +} + +int os_bind_ipc(int fd, struct sockaddr_un *addr, const char *ipc_name_space) +{ + memcpy((char *) addr->sun_path + 1, ipc_name_space, + strlen(ipc_name_space)); + + return bind(fd, (struct sockaddr *)addr, sizeof(*addr)); +} + + +int os_getnameinfo_ss(struct sockaddr_storage *ss, char *host, size_t hostlen, + char *serv, size_t servlen, int flags) +{ + return getnameinfo((struct sockaddr *)ss, sizeof(*ss), host, hostlen, + serv, servlen, flags); +} diff --git a/usr/log.c b/usr/log.c index 056314a..9154626 100644 --- a/usr/log.c +++ b/usr/log.c @@ -30,6 +30,7 @@ #include <sys/types.h> #include <sys/wait.h> +#include "os.h" #include "log.h" #define SEMKEY 0xA7L diff --git a/usr/mgmt.c b/usr/mgmt.c index d93ff5c..2cd8cf8 100644 --- a/usr/mgmt.c +++ b/usr/mgmt.c @@ -19,19 +19,13 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA */ -#include <ctype.h> -#include <dirent.h> #include <errno.h> -#include <fcntl.h> #include <inttypes.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <unistd.h> #include <sys/epoll.h> #include <sys/socket.h> -#include <sys/stat.h> -#include <sys/types.h> #include <sys/un.h> #include "list.h" @@ -379,26 +373,6 @@ static int ipc_accept(int accept_fd) return fd; } -static int ipc_perm(int fd) -{ -#ifndef __MAKE_TGT_ON_BSD__ - struct ucred cred; - socklen_t len; - int err; - - len = sizeof(cred); - err = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void *) &cred, &len); - if (err) { - eprintf("can't get sockopt, %m\n"); - return -1; - } - - if (cred.uid || cred.gid) - return -EPERM; -#endif /* ndef __MAKE_TGT_ON_BSD__ */ - return 0; -} - static void mtask_handler(int fd, int events, void *data) { int err, len; @@ -507,7 +481,7 @@ static void mgmt_event_handler(int accept_fd, int events, void *data) return; } - err = ipc_perm(fd); + err = os_ipc_perm(fd); if (err < 0) { eprintf("permission error\n"); goto out; @@ -563,25 +537,12 @@ int ipc_init(void) memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_LOCAL; -#ifndef __MAKE_TGT_ON_BSD__ - memcpy((char *) &addr.sun_path + 1, TGT_IPC_NAMESPACE, - strlen(TGT_IPC_NAMESPACE)); - - err = bind(fd, (struct sockaddr *) &addr, sizeof(addr)); - if (err) { - eprintf("can't bind a socket, %m\n"); - goto out; - } -#else - sprintf(addr.sun_path , TGT_IPC_NAMESPACE); - addr.sun_len = SUN_LEN(&addr) + 1; - err = bind(fd, (struct sockaddr *) &addr, addr.sun_len); + err = os_bind_ipc(fd, &addr, TGT_IPC_NAMESPACE); if (err) { eprintf("can't bind a socket, %d, %s\n", err, strerror(errno)); goto out; } -#endif err = listen(fd, 32); if (err) { diff --git a/usr/os.h b/usr/os.h new file mode 100644 index 0000000..77944b3 --- /dev/null +++ b/usr/os.h @@ -0,0 +1,30 @@ +#ifndef __TGT_OS_H__ +#define __TGT_OS_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); + +int os_ipc_perm(int fd); + +int os_oom_adjust(void); + +int os_blockdev_size(int fd, uint64_t *size); + +struct sockaddr_un; +int os_bind_ipc(int fd, struct sockaddr_un *addr, const char *ipc_name_space); + +struct sockaddr_storage; +int os_getnameinfo_ss(struct sockaddr_storage *ss, char *host, size_t hostlen, + char *serv, size_t servlen, int flags); + +#ifdef __MAKE_TGT_ON_BSD__ +#define semtimedop(semid, sops, nsops, timeout) \ + semop(semid, sops, nsops) +#endif + +#endif /* ndef __TGT_OS_H__*/ diff --git a/usr/tgtd.c b/usr/tgtd.c index 56f7d93..d243151 100644 --- a/usr/tgtd.c +++ b/usr/tgtd.c @@ -76,29 +76,6 @@ Target framework daemon, version %s\n\ static void signal_catch(int signo) { } -static int oom_adjust(void) -{ -#ifndef __MAKE_TGT_ON_BSD__ - int fd, err; - char path[64]; - - /* Avoid oom-killer */ - sprintf(path, "/proc/%d/oom_adj", getpid()); - fd = open(path, O_WRONLY); - if (fd < 0) { - fprintf(stderr, "can't adjust oom-killer's pardon %s, %m\n", path); - return errno; - } - err = write(fd, "-17\n", 4); - if (err < 0) { - fprintf(stderr, "can't adjust oom-killer's pardon %s, %m\n", path); - return errno; - } - close(fd); -#endif /* ndef __MAKE_TGT_ON_BSD__ */ - return 0; -} - static int nr_file_adjust(void) { int ret, fd, max = 1024 * 1024; @@ -370,7 +347,7 @@ int main(int argc, char **argv) if (is_daemon && daemon(0,0)) exit(1); - err = oom_adjust(); + err = os_oom_adjust(); if (err) exit(1); diff --git a/usr/tgtif.c b/usr/tgtif.c index fd5ad5b..adf70a4 100644 --- a/usr/tgtif.c +++ b/usr/tgtif.c @@ -246,8 +246,9 @@ static void kern_cmd_done(struct tgt_event *ev) } free(KCMD(cmd)); } else - eprintf("unknow command %d %" PRIu64 " %" PRIu64 "\n", - tid, ev->p.cmd_done.itn_id, ev->p.cmd_done.tag); + eprintf("unknow command %d %llu %llu\n", tid, + (unsigned long long)ev->p.cmd_done.itn_id, + (unsigned long long)ev->p.cmd_done.tag); } static int kspace_send_it_nexus_res(int host_no, uint64_t itn_id, diff --git a/usr/tgtimg.c b/usr/tgtimg.c index 43b0761..4cbc871 100644 --- a/usr/tgtimg.c +++ b/usr/tgtimg.c @@ -32,6 +32,8 @@ #include <sys/stat.h> #include <sys/types.h> +#include <linux/fs.h> + #include "media.h" #include "bs_ssc.h" #include "ssc.h" diff --git a/usr/util.c b/usr/util.c index 0fd006d..ac14a7b 100644 --- a/usr/util.c +++ b/usr/util.c @@ -101,16 +101,11 @@ int backed_file_open(char *path, int oflag, uint64_t *size) if (S_ISREG(st.st_mode)) *size = st.st_size; else if (S_ISBLK(st.st_mode)) { -#ifndef __MAKE_TGT_ON_BSD__ - err = ioctl(fd, BLKGETSIZE64, size); + err = os_blockdev_size(fd, size); if (err < 0) { eprintf("Cannot get size, %m\n"); goto close_fd; } -#else - /*FIXME:*/ - *size = ~0LLU; -#endif } else { eprintf("Cannot use this mode %x\n", st.st_mode); err = -EINVAL; diff --git a/usr/util.h b/usr/util.h index d5f0f1d..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,43 +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. -*/ - -#ifdef __CONF_TGT_NO_SYNC_FILE_RANGE - -#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 /* def __CONF_TGT_NO_SYNC_FILE_RANGE */ - #endif