ia64/xen-unstable

view tools/blktap2/drivers/tapdisk-utils.c @ 19817:b7f73a7f3078

blktap2: portability fixes for NetBSD

- Use standard off_t and lseek() instead of non-portable off64_t and
lseek64()
- Use uuid API as documented in DCE 1.1 RPC specification
- Add NetBSD implementation for blk_getimagesize() and
blk_getsectorsize()
- Use blk_getimagesize() and blk_getsectorsize()
- Fix uuid header check

Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Jun 23 17:24:14 2009 +0100 (2009-06-23)
parents 1c627434605e
children
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>
37 #include "blk.h"
38 #include "tapdisk.h"
39 #include "disktypes.h"
40 #include "blktaplib.h"
41 #include "tapdisk-log.h"
42 #include "tapdisk-utils.h"
44 void
45 tapdisk_start_logging(const char *name)
46 {
47 static char buf[128];
49 snprintf(buf, sizeof(buf), "%s[%d]", name, getpid());
50 openlog(buf, LOG_CONS | LOG_ODELAY, LOG_DAEMON);
51 open_tlog("/tmp/tapdisk.log", (64 << 10), TLOG_WARN, 0);
52 }
54 void
55 tapdisk_stop_logging(void)
56 {
57 closelog();
58 close_tlog();
59 }
61 int
62 tapdisk_set_resource_limits(void)
63 {
64 int err;
65 struct rlimit rlim;
67 rlim.rlim_cur = RLIM_INFINITY;
68 rlim.rlim_max = RLIM_INFINITY;
70 err = setrlimit(RLIMIT_MEMLOCK, &rlim);
71 if (err == -1) {
72 EPRINTF("RLIMIT_MEMLOCK failed: %d\n", errno);
73 return -errno;
74 }
76 err = mlockall(MCL_CURRENT | MCL_FUTURE);
77 if (err == -1) {
78 EPRINTF("mlockall failed: %d\n", errno);
79 return -errno;
80 }
82 #define CORE_DUMP
83 #if defined(CORE_DUMP)
84 err = setrlimit(RLIMIT_CORE, &rlim);
85 if (err == -1)
86 EPRINTF("RLIMIT_CORE failed: %d\n", errno);
87 #endif
89 return 0;
90 }
92 int
93 tapdisk_namedup(char **dup, const char *name)
94 {
95 *dup = NULL;
97 if (strnlen(name, MAX_NAME_LEN) >= MAX_NAME_LEN)
98 return -ENAMETOOLONG;
100 *dup = strdup(name);
101 if (!*dup)
102 return -ENOMEM;
104 return 0;
105 }
107 int
108 tapdisk_parse_disk_type(const char *params, char **_path, int *_type)
109 {
110 int i, err, size, handle_len;
111 char *ptr, *path, handle[10];
113 if (strlen(params) + 1 >= MAX_NAME_LEN)
114 return -ENAMETOOLONG;
116 ptr = strchr(params, ':');
117 if (!ptr)
118 return -EINVAL;
120 path = ptr + 1;
122 handle_len = ptr - params;
123 if (handle_len > sizeof(handle))
124 return -ENAMETOOLONG;
126 memcpy(handle, params, handle_len);
127 handle[handle_len] = '\0';
129 size = sizeof(dtypes) / sizeof(disk_info_t *);
130 for (i = 0; i < size; i++) {
131 if (strncmp(handle, dtypes[i]->handle, handle_len))
132 continue;
134 if (dtypes[i]->idnum == -1)
135 return -ENODEV;
137 *_type = dtypes[i]->idnum;
138 *_path = path;
140 return 0;
141 }
143 return -ENODEV;
144 }
146 /*Get Image size, secsize*/
147 int
148 tapdisk_get_image_size(int fd, uint64_t *_sectors, uint32_t *_sector_size)
149 {
150 int ret;
151 struct stat stat;
152 uint64_t sectors;
153 uint64_t sector_size;
155 sectors = 0;
156 sector_size = 0;
157 *_sectors = 0;
158 *_sector_size = 0;
160 if (fstat(fd, &stat)) {
161 DPRINTF("ERROR: fstat failed, Couldn't stat image");
162 return -EINVAL;
163 }
165 if (S_ISBLK(stat.st_mode)) {
166 /*Accessing block device directly*/
167 if (blk_getimagesize(fd, &sectors) != 0)
168 return -EINVAL;
170 /*Get the sector size*/
171 if (blk_getsectorsize(fd, &sector_size) != 0)
172 sector_size = DEFAULT_SECTOR_SIZE;
173 } else {
174 /*Local file? try fstat instead*/
175 sectors = (stat.st_size >> SECTOR_SHIFT);
176 sector_size = DEFAULT_SECTOR_SIZE;
177 }
179 if (sectors == 0) {
180 sectors = 16836057ULL;
181 sector_size = DEFAULT_SECTOR_SIZE;
182 }
184 return 0;
185 }