2
* Copyright (c) 2008, XenSource Inc.
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.
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.
34
#include <sys/ioctl.h>
35
#include <sys/resource.h>
36
#include <sys/utsname.h>
38
#include <linux/version.h>
43
#include "disktypes.h"
44
#include "blktaplib.h"
45
#include "tapdisk-log.h"
46
#include "tapdisk-utils.h"
49
tapdisk_start_logging(const char *name)
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);
59
tapdisk_stop_logging(void)
66
tapdisk_set_resource_limits(void)
71
rlim.rlim_cur = RLIM_INFINITY;
72
rlim.rlim_max = RLIM_INFINITY;
74
err = setrlimit(RLIMIT_MEMLOCK, &rlim);
76
EPRINTF("RLIMIT_MEMLOCK failed: %d\n", errno);
80
err = mlockall(MCL_CURRENT | MCL_FUTURE);
82
EPRINTF("mlockall failed: %d\n", errno);
87
#if defined(CORE_DUMP)
88
err = setrlimit(RLIMIT_CORE, &rlim);
90
EPRINTF("RLIMIT_CORE failed: %d\n", errno);
97
tapdisk_namedup(char **dup, const char *name)
101
if (strnlen(name, MAX_NAME_LEN) >= MAX_NAME_LEN)
102
return -ENAMETOOLONG;
112
tapdisk_parse_disk_type(const char *params, char **_path, int *_type)
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, ':');
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))
138
if (dtypes[i]->idnum == -1)
141
*_type = dtypes[i]->idnum;
150
/*Get Image size, secsize*/
152
tapdisk_get_image_size(int fd, uint64_t *_sectors, uint32_t *_sector_size)
157
uint64_t sector_size;
164
if (fstat(fd, &stat)) {
165
DPRINTF("ERROR: fstat failed, Couldn't stat image");
169
if (S_ISBLK(stat.st_mode)) {
170
/*Accessing block device directly*/
171
if (blk_getimagesize(fd, §ors) != 0)
174
/*Get the sector size*/
175
if (blk_getsectorsize(fd, §or_size) != 0)
176
sector_size = DEFAULT_SECTOR_SIZE;
178
/*Local file? try fstat instead*/
179
sectors = (stat.st_size >> SECTOR_SHIFT);
180
sector_size = DEFAULT_SECTOR_SIZE;
184
sectors = 16836057ULL;
185
sector_size = DEFAULT_SECTOR_SIZE;
193
int tapdisk_linux_version(void)
196
unsigned int version, patchlevel, sublevel;
203
n = sscanf(uts.release, "%u.%u.%u", &version, &patchlevel, &sublevel);
207
return KERNEL_VERSION(version, patchlevel, sublevel);
212
int tapdisk_linux_version(void)