~ubuntu-branches/ubuntu/utopic/xen/utopic

« back to all changes in this revision

Viewing changes to tools/blktap2/drivers/tapdisk-utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2010-05-06 15:47:38 UTC
  • mto: (1.3.1) (15.1.1 sid) (4.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20100506154738-agoz0rlafrh1fnq7
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
40
 
 
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"
 
47
 
 
48
void
 
49
tapdisk_start_logging(const char *name)
 
50
{
 
51
        static char buf[128];
 
52
 
 
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
}
 
57
 
 
58
void
 
59
tapdisk_stop_logging(void)
 
60
{
 
61
        closelog();
 
62
        close_tlog();
 
63
}
 
64
 
 
65
int
 
66
tapdisk_set_resource_limits(void)
 
67
{
 
68
        int err;
 
69
        struct rlimit rlim;
 
70
 
 
71
        rlim.rlim_cur = RLIM_INFINITY;
 
72
        rlim.rlim_max = RLIM_INFINITY;
 
73
 
 
74
        err = setrlimit(RLIMIT_MEMLOCK, &rlim);
 
75
        if (err == -1) {
 
76
                EPRINTF("RLIMIT_MEMLOCK failed: %d\n", errno);
 
77
                return -errno;
 
78
        }
 
79
 
 
80
        err = mlockall(MCL_CURRENT | MCL_FUTURE);
 
81
        if (err == -1) {
 
82
                EPRINTF("mlockall failed: %d\n", errno);
 
83
                return -errno;
 
84
        }
 
85
 
 
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
 
92
 
 
93
        return 0;
 
94
}
 
95
 
 
96
int
 
97
tapdisk_namedup(char **dup, const char *name)
 
98
{
 
99
        *dup = NULL;
 
100
 
 
101
        if (strnlen(name, MAX_NAME_LEN) >= MAX_NAME_LEN)
 
102
                return -ENAMETOOLONG;
 
103
        
 
104
        *dup = strdup(name);
 
105
        if (!*dup)
 
106
                return -ENOMEM;
 
107
 
 
108
        return 0;
 
109
}
 
110
 
 
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];
 
116
 
 
117
        if (strlen(params) + 1 >= MAX_NAME_LEN)
 
118
                return -ENAMETOOLONG;
 
119
 
 
120
        ptr = strchr(params, ':');
 
121
        if (!ptr)
 
122
                return -EINVAL;
 
123
 
 
124
        path = ptr + 1;
 
125
 
 
126
        handle_len = ptr - params;
 
127
        if (handle_len > sizeof(handle))
 
128
          return -ENAMETOOLONG;
 
129
        
 
130
        memcpy(handle, params, handle_len);
 
131
        handle[handle_len] = '\0';
 
132
               
 
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;
 
137
 
 
138
                if (dtypes[i]->idnum == -1)
 
139
                        return -ENODEV;
 
140
 
 
141
                *_type = dtypes[i]->idnum;
 
142
                *_path = path;
 
143
 
 
144
                return 0;
 
145
        }
 
146
 
 
147
        return -ENODEV;
 
148
}
 
149
 
 
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;
 
158
 
 
159
        sectors       = 0;
 
160
        sector_size   = 0;
 
161
        *_sectors     = 0;
 
162
        *_sector_size = 0;
 
163
 
 
164
        if (fstat(fd, &stat)) {
 
165
                DPRINTF("ERROR: fstat failed, Couldn't stat image");
 
166
                return -EINVAL;
 
167
        }
 
168
 
 
169
        if (S_ISBLK(stat.st_mode)) {
 
170
                /*Accessing block device directly*/
 
171
                if (blk_getimagesize(fd, &sectors) != 0)
 
172
                        return -EINVAL;
 
173
 
 
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
        }
 
182
 
 
183
        if (sectors == 0) {             
 
184
                sectors     = 16836057ULL;
 
185
                sector_size = DEFAULT_SECTOR_SIZE;
 
186
        }
 
187
 
 
188
        return 0;
 
189
}
 
190
 
 
191
#ifdef __linux__
 
192
 
 
193
int tapdisk_linux_version(void)
 
194
{
 
195
        struct utsname uts;
 
196
        unsigned int version, patchlevel, sublevel;
 
197
        int n, err;
 
198
 
 
199
        err = uname(&uts);
 
200
        if (err)
 
201
                return -errno;
 
202
 
 
203
        n = sscanf(uts.release, "%u.%u.%u", &version, &patchlevel, &sublevel);
 
204
        if (n != 3)
 
205
                return -ENOSYS;
 
206
 
 
207
        return KERNEL_VERSION(version, patchlevel, sublevel);
 
208
}
 
209
 
 
210
#else
 
211
 
 
212
int tapdisk_linux_version(void)
 
213
{
 
214
        return -ENOSYS;
 
215
}
 
216
 
 
217
#endif