debuggers.hg

view tools/blktap2/drivers/tapdisk-utils.c @ 20918:b0ffb4912c46

blktap2: Prefer AIO eventfd support on kernels >= 2.6.22

Mainline kernel support for eventfd(2) in linux aio was added between
2.6.21 and 2.6.22. Libaio after 0.3.107 has the header file, but
presently few systems support it. Neither do we rely on an up-to-date
libc6.

Instead, this patch adds a header which defines custom iocb_common
struct, and works around a potentially missing sys/eventfd.h.

Signed-off-by: Daniel Stodden <daniel.stodden@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Fri Jan 29 08:55:27 2010 +0000 (2010-01-29)
parents b7f73a7f3078
children 3ffdb094c2c0
line source
1 /*
2 * Copyright (c) 2008, XenSource Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of XenSource Inc. nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include <errno.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/stat.h>
33 #include <sys/mman.h>
34 #include <sys/ioctl.h>
35 #include <sys/resource.h>
36 #include <sys/utsname.h>
37 #ifdef __linux__
38 #include <linux/version.h>
39 #endif
41 #include "blk.h"
42 #include "tapdisk.h"
43 #include "disktypes.h"
44 #include "blktaplib.h"
45 #include "tapdisk-log.h"
46 #include "tapdisk-utils.h"
48 void
49 tapdisk_start_logging(const char *name)
50 {
51 static char buf[128];
53 snprintf(buf, sizeof(buf), "%s[%d]", name, getpid());
54 openlog(buf, LOG_CONS | LOG_ODELAY, LOG_DAEMON);
55 open_tlog("/tmp/tapdisk.log", (64 << 10), TLOG_WARN, 0);
56 }
58 void
59 tapdisk_stop_logging(void)
60 {
61 closelog();
62 close_tlog();
63 }
65 int
66 tapdisk_set_resource_limits(void)
67 {
68 int err;
69 struct rlimit rlim;
71 rlim.rlim_cur = RLIM_INFINITY;
72 rlim.rlim_max = RLIM_INFINITY;
74 err = setrlimit(RLIMIT_MEMLOCK, &rlim);
75 if (err == -1) {
76 EPRINTF("RLIMIT_MEMLOCK failed: %d\n", errno);
77 return -errno;
78 }
80 err = mlockall(MCL_CURRENT | MCL_FUTURE);
81 if (err == -1) {
82 EPRINTF("mlockall failed: %d\n", errno);
83 return -errno;
84 }
86 #define CORE_DUMP
87 #if defined(CORE_DUMP)
88 err = setrlimit(RLIMIT_CORE, &rlim);
89 if (err == -1)
90 EPRINTF("RLIMIT_CORE failed: %d\n", errno);
91 #endif
93 return 0;
94 }
96 int
97 tapdisk_namedup(char **dup, const char *name)
98 {
99 *dup = NULL;
101 if (strnlen(name, MAX_NAME_LEN) >= MAX_NAME_LEN)
102 return -ENAMETOOLONG;
104 *dup = strdup(name);
105 if (!*dup)
106 return -ENOMEM;
108 return 0;
109 }
111 int
112 tapdisk_parse_disk_type(const char *params, char **_path, int *_type)
113 {
114 int i, err, size, handle_len;
115 char *ptr, *path, handle[10];
117 if (strlen(params) + 1 >= MAX_NAME_LEN)
118 return -ENAMETOOLONG;
120 ptr = strchr(params, ':');
121 if (!ptr)
122 return -EINVAL;
124 path = ptr + 1;
126 handle_len = ptr - params;
127 if (handle_len > sizeof(handle))
128 return -ENAMETOOLONG;
130 memcpy(handle, params, handle_len);
131 handle[handle_len] = '\0';
133 size = sizeof(dtypes) / sizeof(disk_info_t *);
134 for (i = 0; i < size; i++) {
135 if (strncmp(handle, dtypes[i]->handle, handle_len))
136 continue;
138 if (dtypes[i]->idnum == -1)
139 return -ENODEV;
141 *_type = dtypes[i]->idnum;
142 *_path = path;
144 return 0;
145 }
147 return -ENODEV;
148 }
150 /*Get Image size, secsize*/
151 int
152 tapdisk_get_image_size(int fd, uint64_t *_sectors, uint32_t *_sector_size)
153 {
154 int ret;
155 struct stat stat;
156 uint64_t sectors;
157 uint64_t sector_size;
159 sectors = 0;
160 sector_size = 0;
161 *_sectors = 0;
162 *_sector_size = 0;
164 if (fstat(fd, &stat)) {
165 DPRINTF("ERROR: fstat failed, Couldn't stat image");
166 return -EINVAL;
167 }
169 if (S_ISBLK(stat.st_mode)) {
170 /*Accessing block device directly*/
171 if (blk_getimagesize(fd, &sectors) != 0)
172 return -EINVAL;
174 /*Get the sector size*/
175 if (blk_getsectorsize(fd, &sector_size) != 0)
176 sector_size = DEFAULT_SECTOR_SIZE;
177 } else {
178 /*Local file? try fstat instead*/
179 sectors = (stat.st_size >> SECTOR_SHIFT);
180 sector_size = DEFAULT_SECTOR_SIZE;
181 }
183 if (sectors == 0) {
184 sectors = 16836057ULL;
185 sector_size = DEFAULT_SECTOR_SIZE;
186 }
188 return 0;
189 }
191 #ifdef __linux__
193 int tapdisk_linux_version(void)
194 {
195 struct utsname uts;
196 unsigned int version, patchlevel, sublevel;
197 int n, err;
199 err = uname(&uts);
200 if (err)
201 return -errno;
203 n = sscanf(uts.release, "%u.%u.%u", &version, &patchlevel, &sublevel);
204 if (n != 3)
205 return -ENOSYS;
207 return KERNEL_VERSION(version, patchlevel, sublevel);
208 }
210 #else
212 int tapdisk_linux_version(void)
213 {
214 return -ENOSYS;
215 }
217 #endif