ukfs.c revision 1.22 1 1.22 pooka /* $NetBSD: ukfs.c,v 1.22 2009/02/11 14:35:58 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.3 pooka * Copyright (c) 2007, 2008 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Development of this software was supported by the
7 1.1 pooka * Finnish Cultural Foundation.
8 1.1 pooka *
9 1.1 pooka * Redistribution and use in source and binary forms, with or without
10 1.1 pooka * modification, are permitted provided that the following conditions
11 1.1 pooka * are met:
12 1.1 pooka * 1. Redistributions of source code must retain the above copyright
13 1.1 pooka * notice, this list of conditions and the following disclaimer.
14 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 pooka * notice, this list of conditions and the following disclaimer in the
16 1.1 pooka * documentation and/or other materials provided with the distribution.
17 1.1 pooka *
18 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 pooka * SUCH DAMAGE.
29 1.1 pooka */
30 1.1 pooka
31 1.1 pooka /*
32 1.1 pooka * This library enables access to files systems directly without
33 1.1 pooka * involving system calls.
34 1.1 pooka */
35 1.1 pooka
36 1.1 pooka #ifdef __linux__
37 1.1 pooka #define _XOPEN_SOURCE 500
38 1.1 pooka #define _BSD_SOURCE
39 1.1 pooka #define _FILE_OFFSET_BITS 64
40 1.1 pooka #endif
41 1.1 pooka
42 1.3 pooka #include <sys/param.h>
43 1.3 pooka #include <sys/queue.h>
44 1.1 pooka #include <sys/stat.h>
45 1.4 pooka #include <sys/sysctl.h>
46 1.4 pooka #include <sys/mount.h>
47 1.1 pooka
48 1.1 pooka #include <assert.h>
49 1.3 pooka #include <dirent.h>
50 1.3 pooka #include <dlfcn.h>
51 1.1 pooka #include <err.h>
52 1.1 pooka #include <errno.h>
53 1.11 pooka #include <fcntl.h>
54 1.1 pooka #include <pthread.h>
55 1.1 pooka #include <stdio.h>
56 1.1 pooka #include <stdlib.h>
57 1.1 pooka #include <string.h>
58 1.1 pooka #include <unistd.h>
59 1.1 pooka #include <stdint.h>
60 1.1 pooka
61 1.1 pooka #include <rump/ukfs.h>
62 1.1 pooka
63 1.1 pooka #include <rump/rump.h>
64 1.1 pooka #include <rump/rump_syscalls.h>
65 1.1 pooka
66 1.1 pooka #define UKFS_MODE_DEFAULT 0555
67 1.1 pooka
68 1.1 pooka struct ukfs {
69 1.1 pooka struct mount *ukfs_mp;
70 1.1 pooka struct vnode *ukfs_rvp;
71 1.1 pooka
72 1.1 pooka pthread_spinlock_t ukfs_spin;
73 1.1 pooka pid_t ukfs_nextpid;
74 1.1 pooka struct vnode *ukfs_cdir;
75 1.11 pooka int ukfs_devfd;
76 1.1 pooka };
77 1.1 pooka
78 1.1 pooka struct mount *
79 1.1 pooka ukfs_getmp(struct ukfs *ukfs)
80 1.1 pooka {
81 1.1 pooka
82 1.1 pooka return ukfs->ukfs_mp;
83 1.1 pooka }
84 1.1 pooka
85 1.1 pooka struct vnode *
86 1.1 pooka ukfs_getrvp(struct ukfs *ukfs)
87 1.1 pooka {
88 1.1 pooka struct vnode *rvp;
89 1.1 pooka
90 1.1 pooka rvp = ukfs->ukfs_rvp;
91 1.1 pooka rump_vp_incref(rvp);
92 1.1 pooka
93 1.1 pooka return rvp;
94 1.1 pooka }
95 1.1 pooka
96 1.20 pooka #ifdef DONT_WANT_PTHREAD_LINKAGE
97 1.20 pooka #define pthread_spin_lock(a)
98 1.20 pooka #define pthread_spin_unlock(a)
99 1.20 pooka #define pthread_spin_init(a,b)
100 1.20 pooka #define pthread_spin_destroy(a)
101 1.20 pooka #endif
102 1.20 pooka
103 1.1 pooka static pid_t
104 1.1 pooka nextpid(struct ukfs *ukfs)
105 1.1 pooka {
106 1.1 pooka pid_t npid;
107 1.1 pooka
108 1.1 pooka pthread_spin_lock(&ukfs->ukfs_spin);
109 1.5 pooka if (ukfs->ukfs_nextpid == 0)
110 1.5 pooka ukfs->ukfs_nextpid++;
111 1.1 pooka npid = ukfs->ukfs_nextpid++;
112 1.1 pooka pthread_spin_unlock(&ukfs->ukfs_spin);
113 1.1 pooka
114 1.1 pooka return npid;
115 1.1 pooka }
116 1.1 pooka
117 1.1 pooka static void
118 1.1 pooka precall(struct ukfs *ukfs)
119 1.1 pooka {
120 1.1 pooka struct vnode *rvp, *cvp;
121 1.1 pooka
122 1.1 pooka rump_setup_curlwp(nextpid(ukfs), 1, 1);
123 1.1 pooka rvp = ukfs_getrvp(ukfs);
124 1.1 pooka pthread_spin_lock(&ukfs->ukfs_spin);
125 1.1 pooka cvp = ukfs->ukfs_cdir;
126 1.1 pooka pthread_spin_unlock(&ukfs->ukfs_spin);
127 1.1 pooka rump_rcvp_set(rvp, cvp); /* takes refs */
128 1.1 pooka rump_vp_rele(rvp);
129 1.1 pooka }
130 1.1 pooka
131 1.1 pooka static void
132 1.1 pooka postcall(struct ukfs *ukfs)
133 1.1 pooka {
134 1.1 pooka struct vnode *rvp;
135 1.1 pooka
136 1.1 pooka rvp = ukfs_getrvp(ukfs);
137 1.1 pooka rump_rcvp_set(NULL, rvp);
138 1.1 pooka rump_vp_rele(rvp);
139 1.1 pooka rump_clear_curlwp();
140 1.1 pooka }
141 1.1 pooka
142 1.1 pooka int
143 1.10 pooka _ukfs_init(int version)
144 1.1 pooka {
145 1.10 pooka int rv;
146 1.10 pooka
147 1.10 pooka if (version != UKFS_VERSION) {
148 1.10 pooka printf("incompatible ukfs version, %d vs. %d\n",
149 1.10 pooka version, UKFS_VERSION);
150 1.10 pooka errno = EPROGMISMATCH;
151 1.10 pooka return -1;
152 1.10 pooka }
153 1.1 pooka
154 1.10 pooka if ((rv = rump_init()) != 0) {
155 1.10 pooka errno = rv;
156 1.10 pooka return -1;
157 1.10 pooka }
158 1.1 pooka
159 1.1 pooka return 0;
160 1.1 pooka }
161 1.1 pooka
162 1.1 pooka struct ukfs *
163 1.1 pooka ukfs_mount(const char *vfsname, const char *devpath, const char *mountpath,
164 1.1 pooka int mntflags, void *arg, size_t alen)
165 1.1 pooka {
166 1.22 pooka struct stat sb;
167 1.1 pooka struct ukfs *fs = NULL;
168 1.1 pooka struct vfsops *vfsops;
169 1.11 pooka struct mount *mp = NULL;
170 1.11 pooka int rv = 0, devfd = -1, rdonly;
171 1.1 pooka
172 1.1 pooka vfsops = rump_vfs_getopsbyname(vfsname);
173 1.1 pooka if (vfsops == NULL) {
174 1.15 pooka rv = ENODEV;
175 1.1 pooka goto out;
176 1.1 pooka }
177 1.1 pooka
178 1.11 pooka /*
179 1.11 pooka * Try open and lock the device. if we can't open it, assume
180 1.11 pooka * it's a file system which doesn't use a real device and let
181 1.11 pooka * it slide. The mount will fail anyway if the fs requires a
182 1.11 pooka * device.
183 1.11 pooka *
184 1.11 pooka * XXX: strictly speaking this is not 100% correct, as virtual
185 1.11 pooka * file systems can use a device path which does exist and can
186 1.11 pooka * be opened. E.g. tmpfs should be mountable multiple times
187 1.11 pooka * with "device" path "/swap", but now isn't. But I think the
188 1.11 pooka * chances are so low that it's currently acceptable to let
189 1.11 pooka * this one slip.
190 1.11 pooka */
191 1.11 pooka rdonly = mntflags & MNT_RDONLY;
192 1.11 pooka devfd = open(devpath, rdonly ? O_RDONLY : O_RDWR);
193 1.11 pooka if (devfd != -1) {
194 1.22 pooka if (fstat(devfd, &sb) == -1) {
195 1.11 pooka close(devfd);
196 1.11 pooka devfd = -1;
197 1.11 pooka rv = errno;
198 1.11 pooka goto out;
199 1.11 pooka }
200 1.22 pooka
201 1.22 pooka /*
202 1.22 pooka * We do this only for non-block device since the
203 1.22 pooka * (NetBSD) kernel allows block device open only once.
204 1.22 pooka */
205 1.22 pooka if (!S_ISBLK(sb.st_mode)) {
206 1.22 pooka if (flock(devfd, LOCK_NB | (rdonly ? LOCK_SH:LOCK_EX))
207 1.22 pooka == -1) {
208 1.22 pooka warnx("ukfs_mount: cannot get %s lock on "
209 1.22 pooka "device", rdonly ? "shared" : "exclusive");
210 1.22 pooka close(devfd);
211 1.22 pooka devfd = -1;
212 1.22 pooka rv = errno;
213 1.22 pooka goto out;
214 1.22 pooka }
215 1.22 pooka } else {
216 1.22 pooka close(devfd);
217 1.22 pooka devfd = -1;
218 1.22 pooka }
219 1.11 pooka }
220 1.1 pooka
221 1.1 pooka fs = malloc(sizeof(struct ukfs));
222 1.1 pooka if (fs == NULL) {
223 1.1 pooka rv = ENOMEM;
224 1.1 pooka goto out;
225 1.1 pooka }
226 1.1 pooka memset(fs, 0, sizeof(struct ukfs));
227 1.11 pooka mp = rump_mnt_init(vfsops, mntflags);
228 1.1 pooka
229 1.1 pooka rump_fakeblk_register(devpath);
230 1.1 pooka rv = rump_mnt_mount(mp, mountpath, arg, &alen);
231 1.1 pooka rump_fakeblk_deregister(devpath);
232 1.1 pooka if (rv) {
233 1.1 pooka goto out;
234 1.1 pooka }
235 1.11 pooka rv = rump_vfs_root(mp, &fs->ukfs_rvp, 0);
236 1.11 pooka if (rv) {
237 1.11 pooka goto out;
238 1.11 pooka }
239 1.11 pooka fs->ukfs_cdir = ukfs_getrvp(fs);
240 1.11 pooka
241 1.1 pooka fs->ukfs_mp = mp;
242 1.11 pooka pthread_spin_init(&fs->ukfs_spin, PTHREAD_PROCESS_SHARED);
243 1.11 pooka fs->ukfs_devfd = devfd;
244 1.11 pooka assert(rv == 0);
245 1.1 pooka
246 1.1 pooka out:
247 1.1 pooka if (rv) {
248 1.11 pooka if (mp)
249 1.11 pooka rump_mnt_destroy(mp);
250 1.1 pooka if (fs)
251 1.1 pooka free(fs);
252 1.1 pooka errno = rv;
253 1.1 pooka fs = NULL;
254 1.11 pooka if (devfd != -1) {
255 1.11 pooka flock(devfd, LOCK_UN);
256 1.11 pooka close(devfd);
257 1.11 pooka }
258 1.1 pooka }
259 1.1 pooka
260 1.1 pooka return fs;
261 1.1 pooka }
262 1.1 pooka
263 1.1 pooka void
264 1.1 pooka ukfs_release(struct ukfs *fs, int flags)
265 1.1 pooka {
266 1.1 pooka int rv;
267 1.1 pooka
268 1.1 pooka if ((flags & UKFS_RELFLAG_NOUNMOUNT) == 0) {
269 1.9 pooka kauth_cred_t cred;
270 1.9 pooka
271 1.1 pooka rump_vp_rele(fs->ukfs_cdir);
272 1.9 pooka cred = rump_cred_suserget();
273 1.9 pooka rv = rump_vfs_sync(fs->ukfs_mp, 1, cred);
274 1.9 pooka rump_cred_suserput(cred);
275 1.1 pooka rump_vp_recycle_nokidding(ukfs_getrvp(fs));
276 1.1 pooka rv |= rump_vfs_unmount(fs->ukfs_mp, 0);
277 1.1 pooka assert(rv == 0);
278 1.1 pooka }
279 1.1 pooka
280 1.1 pooka rump_vfs_syncwait(fs->ukfs_mp);
281 1.1 pooka rump_mnt_destroy(fs->ukfs_mp);
282 1.1 pooka
283 1.1 pooka pthread_spin_destroy(&fs->ukfs_spin);
284 1.16 stacktic if (fs->ukfs_devfd != -1) {
285 1.11 pooka flock(fs->ukfs_devfd, LOCK_UN);
286 1.16 stacktic close(fs->ukfs_devfd);
287 1.16 stacktic }
288 1.1 pooka free(fs);
289 1.1 pooka }
290 1.1 pooka
291 1.1 pooka #define STDCALL(ukfs, thecall) \
292 1.1 pooka int rv = 0; \
293 1.1 pooka \
294 1.1 pooka precall(ukfs); \
295 1.21 pooka rv = thecall; \
296 1.1 pooka postcall(ukfs); \
297 1.21 pooka return rv;
298 1.1 pooka
299 1.1 pooka int
300 1.1 pooka ukfs_getdents(struct ukfs *ukfs, const char *dirname, off_t *off,
301 1.1 pooka uint8_t *buf, size_t bufsize)
302 1.1 pooka {
303 1.1 pooka struct uio *uio;
304 1.1 pooka struct vnode *vp;
305 1.1 pooka size_t resid;
306 1.9 pooka kauth_cred_t cred;
307 1.1 pooka int rv, eofflag;
308 1.1 pooka
309 1.1 pooka precall(ukfs);
310 1.1 pooka rv = rump_namei(RUMP_NAMEI_LOOKUP, RUMP_NAMEI_LOCKLEAF, dirname,
311 1.1 pooka NULL, &vp, NULL);
312 1.1 pooka postcall(ukfs);
313 1.1 pooka if (rv)
314 1.1 pooka goto out;
315 1.1 pooka
316 1.1 pooka uio = rump_uio_setup(buf, bufsize, *off, RUMPUIO_READ);
317 1.9 pooka cred = rump_cred_suserget();
318 1.9 pooka rv = RUMP_VOP_READDIR(vp, uio, cred, &eofflag, NULL, NULL);
319 1.9 pooka rump_cred_suserput(cred);
320 1.17 pooka RUMP_VOP_UNLOCK(vp, 0);
321 1.1 pooka *off = rump_uio_getoff(uio);
322 1.1 pooka resid = rump_uio_free(uio);
323 1.1 pooka rump_vp_rele(vp);
324 1.1 pooka
325 1.1 pooka out:
326 1.1 pooka if (rv) {
327 1.1 pooka errno = rv;
328 1.1 pooka return -1;
329 1.1 pooka }
330 1.1 pooka
331 1.1 pooka /* LINTED: not totally correct return type, but follows syscall */
332 1.1 pooka return bufsize - resid;
333 1.1 pooka }
334 1.1 pooka
335 1.1 pooka ssize_t
336 1.1 pooka ukfs_read(struct ukfs *ukfs, const char *filename, off_t off,
337 1.1 pooka uint8_t *buf, size_t bufsize)
338 1.1 pooka {
339 1.21 pooka int fd;
340 1.1 pooka ssize_t xfer = -1; /* XXXgcc */
341 1.1 pooka
342 1.1 pooka precall(ukfs);
343 1.21 pooka fd = rump_sys_open(filename, RUMP_O_RDONLY, 0);
344 1.21 pooka if (fd == -1)
345 1.1 pooka goto out;
346 1.1 pooka
347 1.21 pooka xfer = rump_sys_pread(fd, buf, bufsize, 0, off);
348 1.21 pooka rump_sys_close(fd);
349 1.1 pooka
350 1.1 pooka out:
351 1.1 pooka postcall(ukfs);
352 1.21 pooka if (fd == -1) {
353 1.1 pooka return -1;
354 1.1 pooka }
355 1.1 pooka return xfer;
356 1.1 pooka }
357 1.1 pooka
358 1.1 pooka ssize_t
359 1.1 pooka ukfs_write(struct ukfs *ukfs, const char *filename, off_t off,
360 1.1 pooka uint8_t *buf, size_t bufsize)
361 1.1 pooka {
362 1.21 pooka int fd;
363 1.1 pooka ssize_t xfer = -1; /* XXXgcc */
364 1.1 pooka
365 1.1 pooka precall(ukfs);
366 1.21 pooka fd = rump_sys_open(filename, RUMP_O_WRONLY, 0);
367 1.21 pooka if (fd == -1)
368 1.1 pooka goto out;
369 1.1 pooka
370 1.1 pooka /* write and commit */
371 1.21 pooka xfer = rump_sys_pwrite(fd, buf, bufsize, 0, off);
372 1.21 pooka if (xfer > 0)
373 1.21 pooka rump_sys_fsync(fd);
374 1.1 pooka
375 1.21 pooka rump_sys_close(fd);
376 1.1 pooka
377 1.1 pooka out:
378 1.1 pooka postcall(ukfs);
379 1.21 pooka if (fd == -1) {
380 1.1 pooka return -1;
381 1.1 pooka }
382 1.1 pooka return xfer;
383 1.1 pooka }
384 1.1 pooka
385 1.1 pooka int
386 1.1 pooka ukfs_create(struct ukfs *ukfs, const char *filename, mode_t mode)
387 1.1 pooka {
388 1.21 pooka int fd;
389 1.1 pooka
390 1.1 pooka precall(ukfs);
391 1.21 pooka fd = rump_sys_open(filename, RUMP_O_WRONLY | RUMP_O_CREAT, mode);
392 1.21 pooka if (fd == -1)
393 1.21 pooka return -1;
394 1.21 pooka rump_sys_close(fd);
395 1.1 pooka
396 1.1 pooka postcall(ukfs);
397 1.1 pooka return 0;
398 1.1 pooka }
399 1.1 pooka
400 1.1 pooka int
401 1.1 pooka ukfs_mknod(struct ukfs *ukfs, const char *path, mode_t mode, dev_t dev)
402 1.1 pooka {
403 1.1 pooka
404 1.21 pooka STDCALL(ukfs, rump_sys_mknod(path, mode, dev));
405 1.1 pooka }
406 1.1 pooka
407 1.1 pooka int
408 1.1 pooka ukfs_mkfifo(struct ukfs *ukfs, const char *path, mode_t mode)
409 1.1 pooka {
410 1.1 pooka
411 1.21 pooka STDCALL(ukfs, rump_sys_mkfifo(path, mode));
412 1.1 pooka }
413 1.1 pooka
414 1.1 pooka int
415 1.1 pooka ukfs_mkdir(struct ukfs *ukfs, const char *filename, mode_t mode)
416 1.1 pooka {
417 1.1 pooka
418 1.21 pooka STDCALL(ukfs, rump_sys_mkdir(filename, mode));
419 1.1 pooka }
420 1.1 pooka
421 1.1 pooka int
422 1.1 pooka ukfs_remove(struct ukfs *ukfs, const char *filename)
423 1.1 pooka {
424 1.1 pooka
425 1.21 pooka STDCALL(ukfs, rump_sys_unlink(filename));
426 1.1 pooka }
427 1.1 pooka
428 1.1 pooka int
429 1.1 pooka ukfs_rmdir(struct ukfs *ukfs, const char *filename)
430 1.1 pooka {
431 1.1 pooka
432 1.21 pooka STDCALL(ukfs, rump_sys_rmdir(filename));
433 1.1 pooka }
434 1.1 pooka
435 1.1 pooka int
436 1.1 pooka ukfs_link(struct ukfs *ukfs, const char *filename, const char *f_create)
437 1.1 pooka {
438 1.1 pooka
439 1.21 pooka STDCALL(ukfs, rump_sys_link(filename, f_create));
440 1.1 pooka }
441 1.1 pooka
442 1.1 pooka int
443 1.1 pooka ukfs_symlink(struct ukfs *ukfs, const char *filename, const char *linkname)
444 1.1 pooka {
445 1.1 pooka
446 1.21 pooka STDCALL(ukfs, rump_sys_symlink(filename, linkname));
447 1.1 pooka }
448 1.1 pooka
449 1.1 pooka ssize_t
450 1.1 pooka ukfs_readlink(struct ukfs *ukfs, const char *filename,
451 1.1 pooka char *linkbuf, size_t buflen)
452 1.1 pooka {
453 1.1 pooka ssize_t rv;
454 1.1 pooka
455 1.1 pooka precall(ukfs);
456 1.21 pooka rv = rump_sys_readlink(filename, linkbuf, buflen);
457 1.1 pooka postcall(ukfs);
458 1.1 pooka return rv;
459 1.1 pooka }
460 1.1 pooka
461 1.1 pooka int
462 1.1 pooka ukfs_rename(struct ukfs *ukfs, const char *from, const char *to)
463 1.1 pooka {
464 1.1 pooka
465 1.21 pooka STDCALL(ukfs, rump_sys_rename(from, to));
466 1.1 pooka }
467 1.1 pooka
468 1.1 pooka int
469 1.1 pooka ukfs_chdir(struct ukfs *ukfs, const char *path)
470 1.1 pooka {
471 1.1 pooka struct vnode *newvp, *oldvp;
472 1.1 pooka int rv;
473 1.1 pooka
474 1.1 pooka precall(ukfs);
475 1.21 pooka rv = rump_sys_chdir(path);
476 1.21 pooka if (rv == -1)
477 1.1 pooka goto out;
478 1.1 pooka
479 1.1 pooka newvp = rump_cdir_get();
480 1.1 pooka pthread_spin_lock(&ukfs->ukfs_spin);
481 1.1 pooka oldvp = ukfs->ukfs_cdir;
482 1.1 pooka ukfs->ukfs_cdir = newvp;
483 1.1 pooka pthread_spin_unlock(&ukfs->ukfs_spin);
484 1.1 pooka if (oldvp)
485 1.1 pooka rump_vp_rele(oldvp);
486 1.1 pooka
487 1.1 pooka out:
488 1.1 pooka postcall(ukfs);
489 1.21 pooka return rv;
490 1.1 pooka }
491 1.1 pooka
492 1.1 pooka int
493 1.1 pooka ukfs_stat(struct ukfs *ukfs, const char *filename, struct stat *file_stat)
494 1.1 pooka {
495 1.1 pooka
496 1.21 pooka STDCALL(ukfs, rump_sys_stat(filename, file_stat));
497 1.1 pooka }
498 1.1 pooka
499 1.1 pooka int
500 1.1 pooka ukfs_lstat(struct ukfs *ukfs, const char *filename, struct stat *file_stat)
501 1.1 pooka {
502 1.1 pooka
503 1.21 pooka STDCALL(ukfs, rump_sys_lstat(filename, file_stat));
504 1.1 pooka }
505 1.1 pooka
506 1.1 pooka int
507 1.1 pooka ukfs_chmod(struct ukfs *ukfs, const char *filename, mode_t mode)
508 1.1 pooka {
509 1.1 pooka
510 1.21 pooka STDCALL(ukfs, rump_sys_chmod(filename, mode));
511 1.1 pooka }
512 1.1 pooka
513 1.1 pooka int
514 1.1 pooka ukfs_lchmod(struct ukfs *ukfs, const char *filename, mode_t mode)
515 1.1 pooka {
516 1.1 pooka
517 1.21 pooka STDCALL(ukfs, rump_sys_lchmod(filename, mode));
518 1.1 pooka }
519 1.1 pooka
520 1.1 pooka int
521 1.1 pooka ukfs_chown(struct ukfs *ukfs, const char *filename, uid_t uid, gid_t gid)
522 1.1 pooka {
523 1.1 pooka
524 1.21 pooka STDCALL(ukfs, rump_sys_chown(filename, uid, gid));
525 1.1 pooka }
526 1.1 pooka
527 1.1 pooka int
528 1.1 pooka ukfs_lchown(struct ukfs *ukfs, const char *filename, uid_t uid, gid_t gid)
529 1.1 pooka {
530 1.1 pooka
531 1.21 pooka STDCALL(ukfs, rump_sys_lchown(filename, uid, gid));
532 1.1 pooka }
533 1.1 pooka
534 1.1 pooka int
535 1.1 pooka ukfs_chflags(struct ukfs *ukfs, const char *filename, u_long flags)
536 1.1 pooka {
537 1.1 pooka
538 1.21 pooka STDCALL(ukfs, rump_sys_chflags(filename, flags));
539 1.1 pooka }
540 1.1 pooka
541 1.1 pooka int
542 1.1 pooka ukfs_lchflags(struct ukfs *ukfs, const char *filename, u_long flags)
543 1.1 pooka {
544 1.1 pooka
545 1.21 pooka STDCALL(ukfs, rump_sys_lchflags(filename, flags));
546 1.1 pooka }
547 1.1 pooka
548 1.1 pooka int
549 1.1 pooka ukfs_utimes(struct ukfs *ukfs, const char *filename, const struct timeval *tptr)
550 1.1 pooka {
551 1.1 pooka
552 1.21 pooka STDCALL(ukfs, rump_sys_utimes(filename, tptr));
553 1.1 pooka }
554 1.1 pooka
555 1.1 pooka int
556 1.1 pooka ukfs_lutimes(struct ukfs *ukfs, const char *filename,
557 1.1 pooka const struct timeval *tptr)
558 1.1 pooka {
559 1.1 pooka
560 1.21 pooka STDCALL(ukfs, rump_sys_lutimes(filename, tptr));
561 1.1 pooka }
562 1.1 pooka
563 1.3 pooka /*
564 1.3 pooka * Dynamic module support
565 1.3 pooka */
566 1.3 pooka
567 1.3 pooka /* load one library */
568 1.3 pooka
569 1.3 pooka /*
570 1.3 pooka * XXX: the dlerror stuff isn't really threadsafe, but then again I
571 1.3 pooka * can't protect against other threads calling dl*() outside of ukfs,
572 1.3 pooka * so just live with it being flimsy
573 1.3 pooka */
574 1.3 pooka #define UFSLIB "librumpfs_ufs.so"
575 1.3 pooka int
576 1.3 pooka ukfs_modload(const char *fname)
577 1.3 pooka {
578 1.3 pooka void *handle, *thesym;
579 1.3 pooka struct stat sb;
580 1.3 pooka const char *p;
581 1.3 pooka int error;
582 1.3 pooka
583 1.3 pooka if (stat(fname, &sb) == -1)
584 1.3 pooka return -1;
585 1.3 pooka
586 1.3 pooka handle = dlopen(fname, RTLD_GLOBAL);
587 1.3 pooka if (handle == NULL) {
588 1.13 pooka const char *dlmsg = dlerror();
589 1.13 pooka if (strstr(dlmsg, "Undefined symbol"))
590 1.3 pooka return 0;
591 1.13 pooka warnx("dlopen %s failed: %s\n", fname, dlmsg);
592 1.3 pooka /* XXXerrno */
593 1.3 pooka return -1;
594 1.3 pooka }
595 1.3 pooka
596 1.3 pooka /*
597 1.3 pooka * XXX: the ufs module is not loaded in the same fashion as the
598 1.3 pooka * others. But we can't do dlclose() for it, since that would
599 1.3 pooka * lead to not being able to load ffs/ext2fs/lfs. Hence hardcode
600 1.3 pooka * and kludge around the issue for now. But this should really
601 1.3 pooka * be fixed by fixing sys/ufs/ufs to be a kernel module.
602 1.3 pooka */
603 1.3 pooka if ((p = strrchr(fname, '/')) != NULL)
604 1.3 pooka p++;
605 1.3 pooka else
606 1.3 pooka p = fname;
607 1.3 pooka if (strcmp(p, UFSLIB) == 0)
608 1.3 pooka return 1;
609 1.3 pooka
610 1.3 pooka thesym = dlsym(handle, "__start_link_set_modules");
611 1.3 pooka if (thesym) {
612 1.14 pooka error = rump_module_load(thesym);
613 1.3 pooka if (error)
614 1.3 pooka goto errclose;
615 1.3 pooka return 1;
616 1.3 pooka }
617 1.3 pooka error = EINVAL;
618 1.3 pooka
619 1.3 pooka errclose:
620 1.3 pooka dlclose(handle);
621 1.3 pooka errno = error;
622 1.3 pooka return -1;
623 1.3 pooka }
624 1.3 pooka
625 1.3 pooka struct loadfail {
626 1.3 pooka char *pname;
627 1.3 pooka
628 1.3 pooka LIST_ENTRY(loadfail) entries;
629 1.3 pooka };
630 1.3 pooka
631 1.3 pooka #define RUMPFSMOD_PREFIX "librumpfs_"
632 1.3 pooka #define RUMPFSMOD_SUFFIX ".so"
633 1.3 pooka
634 1.3 pooka int
635 1.3 pooka ukfs_modload_dir(const char *dir)
636 1.3 pooka {
637 1.3 pooka char nbuf[MAXPATHLEN+1], *p;
638 1.3 pooka struct dirent entry, *result;
639 1.3 pooka DIR *libdir;
640 1.3 pooka struct loadfail *lf, *nlf;
641 1.3 pooka int error, nloaded = 0, redo;
642 1.3 pooka LIST_HEAD(, loadfail) lfs;
643 1.3 pooka
644 1.3 pooka libdir = opendir(dir);
645 1.3 pooka if (libdir == NULL)
646 1.3 pooka return -1;
647 1.3 pooka
648 1.3 pooka LIST_INIT(&lfs);
649 1.3 pooka for (;;) {
650 1.3 pooka if ((error = readdir_r(libdir, &entry, &result)) != 0)
651 1.3 pooka break;
652 1.3 pooka if (!result)
653 1.3 pooka break;
654 1.3 pooka if (strncmp(result->d_name, RUMPFSMOD_PREFIX,
655 1.3 pooka strlen(RUMPFSMOD_PREFIX)) != 0)
656 1.3 pooka continue;
657 1.3 pooka if (((p = strstr(result->d_name, RUMPFSMOD_SUFFIX)) == NULL)
658 1.3 pooka || strlen(p) != strlen(RUMPFSMOD_SUFFIX))
659 1.3 pooka continue;
660 1.3 pooka strlcpy(nbuf, dir, sizeof(nbuf));
661 1.3 pooka strlcat(nbuf, "/", sizeof(nbuf));
662 1.3 pooka strlcat(nbuf, result->d_name, sizeof(nbuf));
663 1.3 pooka switch (ukfs_modload(nbuf)) {
664 1.3 pooka case 0:
665 1.3 pooka lf = malloc(sizeof(*lf));
666 1.3 pooka if (lf == NULL) {
667 1.3 pooka error = ENOMEM;
668 1.3 pooka break;
669 1.3 pooka }
670 1.3 pooka lf->pname = strdup(nbuf);
671 1.3 pooka if (lf->pname == NULL) {
672 1.3 pooka free(lf);
673 1.3 pooka error = ENOMEM;
674 1.3 pooka break;
675 1.3 pooka }
676 1.3 pooka LIST_INSERT_HEAD(&lfs, lf, entries);
677 1.3 pooka break;
678 1.3 pooka case 1:
679 1.3 pooka nloaded++;
680 1.3 pooka break;
681 1.3 pooka default:
682 1.3 pooka /* ignore errors */
683 1.3 pooka break;
684 1.3 pooka }
685 1.3 pooka }
686 1.3 pooka closedir(libdir);
687 1.3 pooka if (error && nloaded != 0)
688 1.3 pooka error = 0;
689 1.3 pooka
690 1.3 pooka /*
691 1.3 pooka * El-cheapo dependency calculator. Just try to load the
692 1.3 pooka * modules n times in a loop
693 1.3 pooka */
694 1.3 pooka for (redo = 1; redo;) {
695 1.3 pooka redo = 0;
696 1.3 pooka nlf = LIST_FIRST(&lfs);
697 1.3 pooka while ((lf = nlf) != NULL) {
698 1.3 pooka nlf = LIST_NEXT(lf, entries);
699 1.3 pooka if (ukfs_modload(lf->pname) == 1) {
700 1.3 pooka nloaded++;
701 1.3 pooka redo = 1;
702 1.3 pooka LIST_REMOVE(lf, entries);
703 1.3 pooka free(lf->pname);
704 1.3 pooka free(lf);
705 1.3 pooka }
706 1.3 pooka }
707 1.3 pooka }
708 1.3 pooka
709 1.3 pooka while ((lf = LIST_FIRST(&lfs)) != NULL) {
710 1.3 pooka LIST_REMOVE(lf, entries);
711 1.3 pooka free(lf->pname);
712 1.3 pooka free(lf);
713 1.3 pooka }
714 1.3 pooka
715 1.3 pooka if (error && nloaded == 0) {
716 1.3 pooka errno = error;
717 1.3 pooka return -1;
718 1.3 pooka }
719 1.3 pooka
720 1.3 pooka return nloaded;
721 1.3 pooka }
722 1.3 pooka
723 1.4 pooka /* XXX: this code uses definitions from NetBSD, needs rumpdefs */
724 1.4 pooka ssize_t
725 1.4 pooka ukfs_vfstypes(char *buf, size_t buflen)
726 1.4 pooka {
727 1.4 pooka int mib[3];
728 1.4 pooka struct sysctlnode q, ans[128];
729 1.4 pooka size_t alen;
730 1.21 pooka int i;
731 1.4 pooka
732 1.4 pooka mib[0] = CTL_VFS;
733 1.4 pooka mib[1] = VFS_GENERIC;
734 1.4 pooka mib[2] = CTL_QUERY;
735 1.4 pooka alen = sizeof(ans);
736 1.4 pooka
737 1.4 pooka memset(&q, 0, sizeof(q));
738 1.4 pooka q.sysctl_flags = SYSCTL_VERSION;
739 1.4 pooka
740 1.21 pooka if (rump_sys___sysctl(mib, 3, ans, &alen, &q, sizeof(q)) == -1) {
741 1.4 pooka return -1;
742 1.4 pooka }
743 1.4 pooka
744 1.4 pooka for (i = 0; i < alen/sizeof(ans[0]); i++)
745 1.4 pooka if (strcmp("fstypes", ans[i].sysctl_name) == 0)
746 1.4 pooka break;
747 1.4 pooka if (i == alen/sizeof(ans[0])) {
748 1.4 pooka errno = ENXIO;
749 1.4 pooka return -1;
750 1.4 pooka }
751 1.4 pooka
752 1.4 pooka mib[0] = CTL_VFS;
753 1.4 pooka mib[1] = VFS_GENERIC;
754 1.4 pooka mib[2] = ans[i].sysctl_num;
755 1.4 pooka
756 1.21 pooka if (rump_sys___sysctl(mib, 3, buf, &buflen, NULL, 0) == -1) {
757 1.4 pooka return -1;
758 1.4 pooka }
759 1.4 pooka
760 1.4 pooka return buflen;
761 1.4 pooka }
762 1.3 pooka
763 1.3 pooka /*
764 1.3 pooka * Utilities
765 1.3 pooka */
766 1.1 pooka int
767 1.1 pooka ukfs_util_builddirs(struct ukfs *ukfs, const char *pathname, mode_t mode)
768 1.1 pooka {
769 1.1 pooka char *f1, *f2;
770 1.1 pooka int rv;
771 1.1 pooka mode_t mask;
772 1.1 pooka bool end;
773 1.1 pooka
774 1.1 pooka /*ukfs_umask((mask = ukfs_umask(0)));*/
775 1.1 pooka umask((mask = umask(0)));
776 1.1 pooka
777 1.1 pooka f1 = f2 = strdup(pathname);
778 1.1 pooka if (f1 == NULL) {
779 1.1 pooka errno = ENOMEM;
780 1.1 pooka return -1;
781 1.1 pooka }
782 1.1 pooka
783 1.1 pooka end = false;
784 1.1 pooka for (;;) {
785 1.1 pooka /* find next component */
786 1.1 pooka f2 += strspn(f2, "/");
787 1.1 pooka f2 += strcspn(f2, "/");
788 1.1 pooka if (*f2 == '\0')
789 1.1 pooka end = true;
790 1.1 pooka else
791 1.1 pooka *f2 = '\0';
792 1.1 pooka
793 1.1 pooka rv = ukfs_mkdir(ukfs, f1, mode & ~mask);
794 1.1 pooka if (errno == EEXIST)
795 1.1 pooka rv = 0;
796 1.1 pooka
797 1.1 pooka if (rv == -1 || *f2 != '\0' || end)
798 1.1 pooka break;
799 1.1 pooka
800 1.1 pooka *f2 = '/';
801 1.1 pooka }
802 1.1 pooka
803 1.1 pooka free(f1);
804 1.1 pooka
805 1.1 pooka return rv;
806 1.1 pooka }
807