rumpuser_file.c revision 1.1 1 1.1 justin /*
2 1.1 justin * Copyright (c) 2007-2010 Antti Kantee. All Rights Reserved.
3 1.1 justin *
4 1.1 justin * Redistribution and use in source and binary forms, with or without
5 1.1 justin * modification, are permitted provided that the following conditions
6 1.1 justin * are met:
7 1.1 justin * 1. Redistributions of source code must retain the above copyright
8 1.1 justin * notice, this list of conditions and the following disclaimer.
9 1.1 justin * 2. Redistributions in binary form must reproduce the above copyright
10 1.1 justin * notice, this list of conditions and the following disclaimer in the
11 1.1 justin * documentation and/or other materials provided with the distribution.
12 1.1 justin *
13 1.1 justin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
14 1.1 justin * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 1.1 justin * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 1.1 justin * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 1.1 justin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 1.1 justin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 1.1 justin * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 1.1 justin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 1.1 justin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 1.1 justin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 1.1 justin * SUCH DAMAGE.
24 1.1 justin */
25 1.1 justin
26 1.1 justin /* NOTE this code will move to a new driver in the next hypercall revision */
27 1.1 justin
28 1.1 justin #include "rumpuser_port.h"
29 1.1 justin
30 1.1 justin #if !defined(lint)
31 1.1 justin __RCSID("$NetBSD: rumpuser_file.c,v 1.1 2014/07/09 23:41:40 justin Exp $");
32 1.1 justin #endif /* !lint */
33 1.1 justin
34 1.1 justin #include <sys/ioctl.h>
35 1.1 justin #include <sys/mman.h>
36 1.1 justin #include <sys/uio.h>
37 1.1 justin #include <sys/stat.h>
38 1.1 justin #include <sys/time.h>
39 1.1 justin #include <sys/types.h>
40 1.1 justin
41 1.1 justin #ifdef __NetBSD__
42 1.1 justin #include <sys/disk.h>
43 1.1 justin #include <sys/disklabel.h>
44 1.1 justin #include <sys/dkio.h>
45 1.1 justin #endif
46 1.1 justin
47 1.1 justin #if defined(__NetBSD__) || defined(__FreeBSD__) || \
48 1.1 justin defined(__DragonFly__) || defined(__APPLE__)
49 1.1 justin #define __BSD__
50 1.1 justin #endif
51 1.1 justin
52 1.1 justin #if defined(__BSD__)
53 1.1 justin #include <sys/sysctl.h>
54 1.1 justin #endif
55 1.1 justin
56 1.1 justin #include <assert.h>
57 1.1 justin #include <errno.h>
58 1.1 justin #include <fcntl.h>
59 1.1 justin #include <netdb.h>
60 1.1 justin #include <stdarg.h>
61 1.1 justin #include <stdint.h>
62 1.1 justin #include <stdio.h>
63 1.1 justin #include <stdlib.h>
64 1.1 justin #include <string.h>
65 1.1 justin #include <unistd.h>
66 1.1 justin
67 1.1 justin #include <rump/rumpuser.h>
68 1.1 justin
69 1.1 justin #include "rumpuser_int.h"
70 1.1 justin
71 1.1 justin int
72 1.1 justin rumpuser_getfileinfo(const char *path, uint64_t *sizep, int *ftp)
73 1.1 justin {
74 1.1 justin struct stat sb;
75 1.1 justin uint64_t size = 0;
76 1.1 justin int needsdev = 0, rv = 0, ft = 0;
77 1.1 justin int fd = -1;
78 1.1 justin
79 1.1 justin if (stat(path, &sb) == -1) {
80 1.1 justin rv = errno;
81 1.1 justin goto out;
82 1.1 justin }
83 1.1 justin
84 1.1 justin switch (sb.st_mode & S_IFMT) {
85 1.1 justin case S_IFDIR:
86 1.1 justin ft = RUMPUSER_FT_DIR;
87 1.1 justin break;
88 1.1 justin case S_IFREG:
89 1.1 justin ft = RUMPUSER_FT_REG;
90 1.1 justin break;
91 1.1 justin case S_IFBLK:
92 1.1 justin ft = RUMPUSER_FT_BLK;
93 1.1 justin needsdev = 1;
94 1.1 justin break;
95 1.1 justin case S_IFCHR:
96 1.1 justin ft = RUMPUSER_FT_CHR;
97 1.1 justin needsdev = 1;
98 1.1 justin break;
99 1.1 justin default:
100 1.1 justin ft = RUMPUSER_FT_OTHER;
101 1.1 justin break;
102 1.1 justin }
103 1.1 justin
104 1.1 justin if (!needsdev) {
105 1.1 justin size = sb.st_size;
106 1.1 justin } else if (sizep) {
107 1.1 justin /*
108 1.1 justin * Welcome to the jungle. Of course querying the kernel
109 1.1 justin * for a device partition size is supposed to be far from
110 1.1 justin * trivial. On NetBSD we use ioctl. On $other platform
111 1.1 justin * we have a problem. We try "the lseek trick" and just
112 1.1 justin * fail if that fails. Platform specific code can later
113 1.1 justin * be written here if appropriate.
114 1.1 justin *
115 1.1 justin * On NetBSD we hope and pray that for block devices nobody
116 1.1 justin * else is holding them open, because otherwise the kernel
117 1.1 justin * will not permit us to open it. Thankfully, this is
118 1.1 justin * usually called only in bootstrap and then we can
119 1.1 justin * forget about it.
120 1.1 justin */
121 1.1 justin #ifndef __NetBSD__
122 1.1 justin off_t off;
123 1.1 justin
124 1.1 justin fd = open(path, O_RDONLY);
125 1.1 justin if (fd == -1) {
126 1.1 justin rv = errno;
127 1.1 justin goto out;
128 1.1 justin }
129 1.1 justin
130 1.1 justin off = lseek(fd, 0, SEEK_END);
131 1.1 justin if (off != 0) {
132 1.1 justin size = off;
133 1.1 justin goto out;
134 1.1 justin }
135 1.1 justin fprintf(stderr, "error: device size query not implemented on "
136 1.1 justin "this platform\n");
137 1.1 justin rv = EOPNOTSUPP;
138 1.1 justin goto out;
139 1.1 justin #else
140 1.1 justin struct disklabel lab;
141 1.1 justin struct partition *parta;
142 1.1 justin struct dkwedge_info dkw;
143 1.1 justin
144 1.1 justin fd = open(path, O_RDONLY);
145 1.1 justin if (fd == -1) {
146 1.1 justin rv = errno;
147 1.1 justin goto out;
148 1.1 justin }
149 1.1 justin
150 1.1 justin if (ioctl(fd, DIOCGDINFO, &lab) == 0) {
151 1.1 justin parta = &lab.d_partitions[DISKPART(sb.st_rdev)];
152 1.1 justin size = (uint64_t)lab.d_secsize * parta->p_size;
153 1.1 justin goto out;
154 1.1 justin }
155 1.1 justin
156 1.1 justin if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == 0) {
157 1.1 justin /*
158 1.1 justin * XXX: should use DIOCGDISKINFO to query
159 1.1 justin * sector size, but that requires proplib,
160 1.1 justin * so just don't bother for now. it's nice
161 1.1 justin * that something as difficult as figuring out
162 1.1 justin * a partition's size has been made so easy.
163 1.1 justin */
164 1.1 justin size = dkw.dkw_size << DEV_BSHIFT;
165 1.1 justin goto out;
166 1.1 justin }
167 1.1 justin
168 1.1 justin rv = errno;
169 1.1 justin #endif /* __NetBSD__ */
170 1.1 justin }
171 1.1 justin
172 1.1 justin out:
173 1.1 justin if (rv == 0 && sizep)
174 1.1 justin *sizep = size;
175 1.1 justin if (rv == 0 && ftp)
176 1.1 justin *ftp = ft;
177 1.1 justin if (fd != -1)
178 1.1 justin close(fd);
179 1.1 justin
180 1.1 justin ET(rv);
181 1.1 justin }
182 1.1 justin
183 1.1 justin int
184 1.1 justin rumpuser_open(const char *path, int ruflags, int *fdp)
185 1.1 justin {
186 1.1 justin int fd, flags, rv;
187 1.1 justin
188 1.1 justin switch (ruflags & RUMPUSER_OPEN_ACCMODE) {
189 1.1 justin case RUMPUSER_OPEN_RDONLY:
190 1.1 justin flags = O_RDONLY;
191 1.1 justin break;
192 1.1 justin case RUMPUSER_OPEN_WRONLY:
193 1.1 justin flags = O_WRONLY;
194 1.1 justin break;
195 1.1 justin case RUMPUSER_OPEN_RDWR:
196 1.1 justin flags = O_RDWR;
197 1.1 justin break;
198 1.1 justin default:
199 1.1 justin rv = EINVAL;
200 1.1 justin goto out;
201 1.1 justin }
202 1.1 justin
203 1.1 justin #define TESTSET(_ru_, _h_) if (ruflags & _ru_) flags |= _h_;
204 1.1 justin TESTSET(RUMPUSER_OPEN_CREATE, O_CREAT);
205 1.1 justin TESTSET(RUMPUSER_OPEN_EXCL, O_EXCL);
206 1.1 justin #undef TESTSET
207 1.1 justin
208 1.1 justin KLOCK_WRAP(fd = open(path, flags, 0644));
209 1.1 justin if (fd == -1) {
210 1.1 justin rv = errno;
211 1.1 justin } else {
212 1.1 justin *fdp = fd;
213 1.1 justin rv = 0;
214 1.1 justin }
215 1.1 justin
216 1.1 justin out:
217 1.1 justin ET(rv);
218 1.1 justin }
219 1.1 justin
220 1.1 justin int
221 1.1 justin rumpuser_close(int fd)
222 1.1 justin {
223 1.1 justin int nlocks;
224 1.1 justin
225 1.1 justin rumpkern_unsched(&nlocks, NULL);
226 1.1 justin fsync(fd);
227 1.1 justin close(fd);
228 1.1 justin rumpkern_sched(nlocks, NULL);
229 1.1 justin
230 1.1 justin ET(0);
231 1.1 justin }
232 1.1 justin
233 1.1 justin /*
234 1.1 justin * Assume "struct rumpuser_iovec" and "struct iovec" are the same.
235 1.1 justin * If you encounter POSIX platforms where they aren't, add some
236 1.1 justin * translation for iovlen > 1.
237 1.1 justin */
238 1.1 justin int
239 1.1 justin rumpuser_iovread(int fd, struct rumpuser_iovec *ruiov, size_t iovlen,
240 1.1 justin int64_t roff, size_t *retp)
241 1.1 justin {
242 1.1 justin struct iovec *iov = (struct iovec *)ruiov;
243 1.1 justin off_t off = (off_t)roff;
244 1.1 justin ssize_t nn;
245 1.1 justin int rv;
246 1.1 justin
247 1.1 justin if (off == RUMPUSER_IOV_NOSEEK) {
248 1.1 justin KLOCK_WRAP(nn = readv(fd, iov, iovlen));
249 1.1 justin } else {
250 1.1 justin int nlocks;
251 1.1 justin
252 1.1 justin rumpkern_unsched(&nlocks, NULL);
253 1.1 justin if (lseek(fd, off, SEEK_SET) == off) {
254 1.1 justin nn = readv(fd, iov, iovlen);
255 1.1 justin } else {
256 1.1 justin nn = -1;
257 1.1 justin }
258 1.1 justin rumpkern_sched(nlocks, NULL);
259 1.1 justin }
260 1.1 justin
261 1.1 justin if (nn == -1) {
262 1.1 justin rv = errno;
263 1.1 justin } else {
264 1.1 justin *retp = (size_t)nn;
265 1.1 justin rv = 0;
266 1.1 justin }
267 1.1 justin
268 1.1 justin ET(rv);
269 1.1 justin }
270 1.1 justin
271 1.1 justin int
272 1.1 justin rumpuser_iovwrite(int fd, const struct rumpuser_iovec *ruiov, size_t iovlen,
273 1.1 justin int64_t roff, size_t *retp)
274 1.1 justin {
275 1.1 justin const struct iovec *iov = (const struct iovec *)ruiov;
276 1.1 justin off_t off = (off_t)roff;
277 1.1 justin ssize_t nn;
278 1.1 justin int rv;
279 1.1 justin
280 1.1 justin if (off == RUMPUSER_IOV_NOSEEK) {
281 1.1 justin KLOCK_WRAP(nn = writev(fd, iov, iovlen));
282 1.1 justin } else {
283 1.1 justin int nlocks;
284 1.1 justin
285 1.1 justin rumpkern_unsched(&nlocks, NULL);
286 1.1 justin if (lseek(fd, off, SEEK_SET) == off) {
287 1.1 justin nn = writev(fd, iov, iovlen);
288 1.1 justin } else {
289 1.1 justin nn = -1;
290 1.1 justin }
291 1.1 justin rumpkern_sched(nlocks, NULL);
292 1.1 justin }
293 1.1 justin
294 1.1 justin if (nn == -1) {
295 1.1 justin rv = errno;
296 1.1 justin } else {
297 1.1 justin *retp = (size_t)nn;
298 1.1 justin rv = 0;
299 1.1 justin }
300 1.1 justin
301 1.1 justin ET(rv);
302 1.1 justin }
303 1.1 justin
304 1.1 justin int
305 1.1 justin rumpuser_syncfd(int fd, int flags, uint64_t start, uint64_t len)
306 1.1 justin {
307 1.1 justin int rv = 0;
308 1.1 justin
309 1.1 justin /*
310 1.1 justin * For now, assume fd is regular file and does not care
311 1.1 justin * about read syncing
312 1.1 justin */
313 1.1 justin if ((flags & RUMPUSER_SYNCFD_BOTH) == 0) {
314 1.1 justin rv = EINVAL;
315 1.1 justin goto out;
316 1.1 justin }
317 1.1 justin if ((flags & RUMPUSER_SYNCFD_WRITE) == 0) {
318 1.1 justin rv = 0;
319 1.1 justin goto out;
320 1.1 justin }
321 1.1 justin
322 1.1 justin #ifdef __NetBSD__
323 1.1 justin {
324 1.1 justin int fsflags = FDATASYNC;
325 1.1 justin
326 1.1 justin if (fsflags & RUMPUSER_SYNCFD_SYNC)
327 1.1 justin fsflags |= FDISKSYNC;
328 1.1 justin if (fsync_range(fd, fsflags, start, len) == -1)
329 1.1 justin rv = errno;
330 1.1 justin }
331 1.1 justin #else
332 1.1 justin /* el-simplo */
333 1.1 justin if (fsync(fd) == -1)
334 1.1 justin rv = errno;
335 1.1 justin #endif
336 1.1 justin
337 1.1 justin out:
338 1.1 justin ET(rv);
339 1.1 justin }
340