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 "blktaplib.h"
44 #include "tapdisk-log.h"
45 #include "tapdisk-utils.h"
46
47 void
tapdisk_start_logging(const char * name)48 tapdisk_start_logging(const char *name)
49 {
50 static char buf[128];
51
52 snprintf(buf, sizeof(buf), "%s[%d]", name, getpid());
53 openlog(buf, LOG_CONS | LOG_ODELAY, LOG_DAEMON);
54 open_tlog("/tmp/tapdisk.log", (64 << 10), TLOG_WARN, 0);
55 }
56
57 void
tapdisk_stop_logging(void)58 tapdisk_stop_logging(void)
59 {
60 closelog();
61 close_tlog();
62 }
63
64 int
tapdisk_set_resource_limits(void)65 tapdisk_set_resource_limits(void)
66 {
67 int err;
68 struct rlimit rlim;
69
70 rlim.rlim_cur = RLIM_INFINITY;
71 rlim.rlim_max = RLIM_INFINITY;
72
73 err = setrlimit(RLIMIT_MEMLOCK, &rlim);
74 if (err == -1) {
75 EPRINTF("RLIMIT_MEMLOCK failed: %d\n", errno);
76 return -errno;
77 }
78
79 err = mlockall(MCL_CURRENT | MCL_FUTURE);
80 if (err == -1) {
81 EPRINTF("mlockall failed: %d\n", errno);
82 return -errno;
83 }
84
85 #define CORE_DUMP
86 #if defined(CORE_DUMP)
87 err = setrlimit(RLIMIT_CORE, &rlim);
88 if (err == -1)
89 EPRINTF("RLIMIT_CORE failed: %d\n", errno);
90 #endif
91
92 return 0;
93 }
94
95 int
tapdisk_namedup(char ** dup,const char * name)96 tapdisk_namedup(char **dup, const char *name)
97 {
98 *dup = NULL;
99
100 if (strnlen(name, MAX_NAME_LEN) >= MAX_NAME_LEN)
101 return -ENAMETOOLONG;
102
103 *dup = strdup(name);
104 if (!*dup)
105 return -ENOMEM;
106
107 return 0;
108 }
109
110 /*Get Image size, secsize*/
111 int
tapdisk_get_image_size(int fd,uint64_t * _sectors,uint32_t * _sector_size)112 tapdisk_get_image_size(int fd, uint64_t *_sectors, uint32_t *_sector_size)
113 {
114 int ret;
115 struct stat stat;
116 uint64_t sectors;
117 uint64_t sector_size;
118
119 sectors = 0;
120 sector_size = 0;
121 *_sectors = 0;
122 *_sector_size = 0;
123
124 if (fstat(fd, &stat)) {
125 DPRINTF("ERROR: fstat failed, Couldn't stat image");
126 return -EINVAL;
127 }
128
129 if (S_ISBLK(stat.st_mode)) {
130 /*Accessing block device directly*/
131 if (blk_getimagesize(fd, §ors) != 0)
132 return -EINVAL;
133
134 /*Get the sector size*/
135 if (blk_getsectorsize(fd, §or_size) != 0)
136 sector_size = DEFAULT_SECTOR_SIZE;
137 } else {
138 /*Local file? try fstat instead*/
139 sectors = (stat.st_size >> SECTOR_SHIFT);
140 sector_size = DEFAULT_SECTOR_SIZE;
141 }
142
143 if (sectors == 0) {
144 sectors = 16836057ULL;
145 sector_size = DEFAULT_SECTOR_SIZE;
146 }
147
148 return 0;
149 }
150
151 #ifdef __linux__
152
tapdisk_linux_version(void)153 int tapdisk_linux_version(void)
154 {
155 struct utsname uts;
156 unsigned int version, patchlevel, sublevel;
157 int n, err;
158
159 err = uname(&uts);
160 if (err)
161 return -errno;
162
163 n = sscanf(uts.release, "%u.%u.%u", &version, &patchlevel, &sublevel);
164 if (n != 3)
165 return -ENOSYS;
166
167 return KERNEL_VERSION(version, patchlevel, sublevel);
168 }
169
170 #else
171
tapdisk_linux_version(void)172 int tapdisk_linux_version(void)
173 {
174 return -ENOSYS;
175 }
176
177 #endif
read_exact(int fd,void * data,size_t size)178 int read_exact(int fd, void *data, size_t size)
179 {
180 size_t offset = 0;
181 ssize_t len;
182
183 while ( offset < size )
184 {
185 len = read(fd, (char *)data + offset, size - offset);
186 if ( (len == -1) && (errno == EINTR) )
187 continue;
188 if ( len == 0 )
189 errno = 0;
190 if ( len <= 0 )
191 return -1;
192 offset += len;
193 }
194
195 return 0;
196 }
197
write_exact(int fd,const void * data,size_t size)198 int write_exact(int fd, const void *data, size_t size)
199 {
200 size_t offset = 0;
201 ssize_t len;
202
203 while ( offset < size )
204 {
205 len = write(fd, (const char *)data + offset, size - offset);
206 if ( (len == -1) && (errno == EINTR) )
207 continue;
208 if ( len <= 0 )
209 return -1;
210 offset += len;
211 }
212
213 return 0;
214 }
215