1 /*
2  * Copyright (c) 2010, XenSource Inc.
3  * All rights reserved.
4  *
5  * This  library is  free  software; you  can  redistribute it  and/or
6  * modify it under the terms  of the GNU Lesser General Public License
7  * as published by  the Free Software Foundation; either  version 2 of
8  * the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but
11  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
12  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should  have received a copy  of the GNU  Lesser General Public
16  * License along with this library; If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 /*
20  * kernel 2.6.21 added eventfd(2) support, kernel 2.6.22 eventfds for
21  * aio. libaio 0.3.107 updated the header file, but few systems have
22  * it. define a custom iocb_common struct instead, and work around a
23  * potentially missing sys/eventfd.h. this header should vanish over
24  * time.
25  */
26 
27 #ifndef __LIBAIO_COMPAT
28 #define __LIBAIO_COMPAT
29 
30 #include "../../config.h"
31 #include <libaio.h>
32 #include <unistd.h>
33 #include <sys/syscall.h>
34 
35 struct __compat_io_iocb_common {
36 	char             __pad_buf[8];
37 	char             __pad_nbytes[8];
38 	long long	offset;
39 	long long	__pad3;
40 	unsigned	flags;
41 	unsigned	resfd;
42 };
43 
__io_set_eventfd(struct iocb * iocb,int eventfd)44 static inline void __io_set_eventfd(struct iocb *iocb, int eventfd)
45 {
46 	struct __compat_io_iocb_common *c;
47 	c = (struct __compat_io_iocb_common*)&iocb->u.c;
48 	c->flags |= (1 << 0);
49 	c->resfd = eventfd;
50 }
51 
52 #ifdef HAVE_SYS_EVENTFD_H
53 
54 #include <sys/eventfd.h>
55 
tapdisk_sys_eventfd(int initval)56 static inline int tapdisk_sys_eventfd(int initval)
57 {
58 	return eventfd(initval, 0);
59 }
60 
61 #else /* Fallback */
62 #ifndef SYS_eventfd
63 #ifndef __NR_eventfd
64 # if defined(__alpha__)
65 #  define __NR_eventfd		478
66 # elif defined(__arm__)
67 #  define __NR_eventfd		(__NR_SYSCALL_BASE+351)
68 # elif defined(__ia64__)
69 #  define __NR_eventfd		1309
70 # elif defined(__i386__)
71 #  define __NR_eventfd		323
72 # elif defined(__m68k__)
73 #  define __NR_eventfd		319
74 # elif 0 && defined(__mips__)
75 #  error __NR_eventfd?
76 #  define __NR_eventfd		(__NR_Linux + 319)
77 #  define __NR_eventfd		(__NR_Linux + 278)
78 #  define __NR_eventfd		(__NR_Linux + 282)
79 # elif defined(__hppa__)
80 #  define __NR_eventfd		(__NR_Linux + 304)
81 # elif defined(__PPC__) || defined(__powerpc64__)
82 #  define __NR_eventfd		307
83 # elif defined(__s390__) || defined(__s390x__)
84 #  define __NR_eventfd		318
85 # elif defined(__sparc__)
86 #  define __NR_eventfd		313
87 # elif defined(__x86_64__)
88 #  define __NR_eventfd		284
89 # endif
90 #else
91 # error __NR_eventfd?
92 #endif
93 #define SYS_eventfd __NR_eventfd
94 #endif
95 
tapdisk_sys_eventfd(int initval)96 static inline int tapdisk_sys_eventfd(int initval)
97 {
98 	return syscall(SYS_eventfd, initval, 0);
99 }
100 #endif
101 
102 #endif /* __LIBAIO_COMPAT */
103