ukfs.c revision 1.42 1 1.42 njoly /* $NetBSD: ukfs.c,v 1.42 2009/11/16 17:21:26 njoly Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.38 pooka * Copyright (c) 2007, 2008, 2009 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.38 pooka #include "ukfs_int_disklabel.h"
67 1.38 pooka
68 1.1 pooka #define UKFS_MODE_DEFAULT 0555
69 1.1 pooka
70 1.1 pooka struct ukfs {
71 1.1 pooka struct mount *ukfs_mp;
72 1.1 pooka struct vnode *ukfs_rvp;
73 1.37 pooka void *ukfs_specific;
74 1.1 pooka
75 1.1 pooka pthread_spinlock_t ukfs_spin;
76 1.1 pooka pid_t ukfs_nextpid;
77 1.1 pooka struct vnode *ukfs_cdir;
78 1.11 pooka int ukfs_devfd;
79 1.30 pooka char *ukfs_devpath;
80 1.30 pooka char *ukfs_mountpath;
81 1.1 pooka };
82 1.1 pooka
83 1.30 pooka static int builddirs(const char *, mode_t,
84 1.30 pooka int (*mkdirfn)(struct ukfs *, const char *, mode_t), struct ukfs *);
85 1.30 pooka
86 1.1 pooka struct mount *
87 1.1 pooka ukfs_getmp(struct ukfs *ukfs)
88 1.1 pooka {
89 1.1 pooka
90 1.1 pooka return ukfs->ukfs_mp;
91 1.1 pooka }
92 1.1 pooka
93 1.1 pooka struct vnode *
94 1.1 pooka ukfs_getrvp(struct ukfs *ukfs)
95 1.1 pooka {
96 1.1 pooka struct vnode *rvp;
97 1.1 pooka
98 1.1 pooka rvp = ukfs->ukfs_rvp;
99 1.40 pooka rump_pub_vp_incref(rvp);
100 1.1 pooka
101 1.1 pooka return rvp;
102 1.1 pooka }
103 1.1 pooka
104 1.37 pooka void
105 1.37 pooka ukfs_setspecific(struct ukfs *ukfs, void *priv)
106 1.37 pooka {
107 1.37 pooka
108 1.37 pooka ukfs->ukfs_specific = priv;
109 1.37 pooka }
110 1.37 pooka
111 1.37 pooka void *
112 1.37 pooka ukfs_getspecific(struct ukfs *ukfs)
113 1.37 pooka {
114 1.37 pooka
115 1.37 pooka return ukfs->ukfs_specific;
116 1.37 pooka }
117 1.37 pooka
118 1.20 pooka #ifdef DONT_WANT_PTHREAD_LINKAGE
119 1.20 pooka #define pthread_spin_lock(a)
120 1.20 pooka #define pthread_spin_unlock(a)
121 1.20 pooka #define pthread_spin_init(a,b)
122 1.20 pooka #define pthread_spin_destroy(a)
123 1.20 pooka #endif
124 1.20 pooka
125 1.1 pooka static pid_t
126 1.1 pooka nextpid(struct ukfs *ukfs)
127 1.1 pooka {
128 1.1 pooka pid_t npid;
129 1.1 pooka
130 1.1 pooka pthread_spin_lock(&ukfs->ukfs_spin);
131 1.5 pooka if (ukfs->ukfs_nextpid == 0)
132 1.5 pooka ukfs->ukfs_nextpid++;
133 1.1 pooka npid = ukfs->ukfs_nextpid++;
134 1.1 pooka pthread_spin_unlock(&ukfs->ukfs_spin);
135 1.1 pooka
136 1.1 pooka return npid;
137 1.1 pooka }
138 1.1 pooka
139 1.1 pooka static void
140 1.1 pooka precall(struct ukfs *ukfs)
141 1.1 pooka {
142 1.1 pooka struct vnode *rvp, *cvp;
143 1.1 pooka
144 1.41 pooka rump_pub_lwp_alloc_and_switch(nextpid(ukfs), 1);
145 1.1 pooka rvp = ukfs_getrvp(ukfs);
146 1.1 pooka pthread_spin_lock(&ukfs->ukfs_spin);
147 1.1 pooka cvp = ukfs->ukfs_cdir;
148 1.1 pooka pthread_spin_unlock(&ukfs->ukfs_spin);
149 1.40 pooka rump_pub_rcvp_set(rvp, cvp); /* takes refs */
150 1.40 pooka rump_pub_vp_rele(rvp);
151 1.1 pooka }
152 1.1 pooka
153 1.1 pooka static void
154 1.1 pooka postcall(struct ukfs *ukfs)
155 1.1 pooka {
156 1.1 pooka struct vnode *rvp;
157 1.1 pooka
158 1.1 pooka rvp = ukfs_getrvp(ukfs);
159 1.40 pooka rump_pub_rcvp_set(NULL, rvp);
160 1.40 pooka rump_pub_vp_rele(rvp);
161 1.41 pooka rump_pub_lwp_release(rump_pub_lwp_curlwp());
162 1.1 pooka }
163 1.1 pooka
164 1.1 pooka int
165 1.10 pooka _ukfs_init(int version)
166 1.1 pooka {
167 1.10 pooka int rv;
168 1.10 pooka
169 1.10 pooka if (version != UKFS_VERSION) {
170 1.10 pooka printf("incompatible ukfs version, %d vs. %d\n",
171 1.10 pooka version, UKFS_VERSION);
172 1.10 pooka errno = EPROGMISMATCH;
173 1.10 pooka return -1;
174 1.10 pooka }
175 1.1 pooka
176 1.10 pooka if ((rv = rump_init()) != 0) {
177 1.10 pooka errno = rv;
178 1.10 pooka return -1;
179 1.10 pooka }
180 1.1 pooka
181 1.1 pooka return 0;
182 1.1 pooka }
183 1.1 pooka
184 1.31 pooka /*ARGSUSED*/
185 1.30 pooka static int
186 1.30 pooka rumpmkdir(struct ukfs *dummy, const char *path, mode_t mode)
187 1.30 pooka {
188 1.30 pooka
189 1.30 pooka return rump_sys_mkdir(path, mode);
190 1.30 pooka }
191 1.30 pooka
192 1.38 pooka int
193 1.38 pooka ukfs_partition_probe(char *devpath, int *partition)
194 1.38 pooka {
195 1.38 pooka char *p;
196 1.38 pooka int rv = 0;
197 1.38 pooka
198 1.38 pooka /*
199 1.38 pooka * Check for disklabel magic in pathname:
200 1.38 pooka * /regularpath%PART:<char>%\0
201 1.38 pooka */
202 1.38 pooka #define MAGICADJ(p, n) (p+sizeof(UKFS_PARTITION_SCANMAGIC)-1+n)
203 1.38 pooka if ((p = strstr(devpath, UKFS_PARTITION_SCANMAGIC)) != NULL
204 1.38 pooka && strlen(p) == UKFS_PARTITION_MAGICLEN
205 1.38 pooka && *(MAGICADJ(p,1)) == '%') {
206 1.38 pooka if (*(MAGICADJ(p,0)) >= 'a' &&
207 1.38 pooka *(MAGICADJ(p,0)) < 'a' + UKFS_MAXPARTITIONS) {
208 1.38 pooka *partition = *(MAGICADJ(p,0)) - 'a';
209 1.38 pooka *p = '\0';
210 1.38 pooka } else {
211 1.38 pooka rv = EINVAL;
212 1.38 pooka }
213 1.38 pooka } else {
214 1.38 pooka *partition = UKFS_PARTITION_NONE;
215 1.38 pooka }
216 1.38 pooka
217 1.38 pooka return rv;
218 1.38 pooka }
219 1.38 pooka
220 1.38 pooka /*
221 1.38 pooka * Open the disk file and flock it. Also, if we are operation on
222 1.38 pooka * an embedded partition, find the partition offset and size from
223 1.38 pooka * the disklabel.
224 1.38 pooka *
225 1.38 pooka * We hard-fail only in two cases:
226 1.38 pooka * 1) we failed to get the partition info out (don't know what offset
227 1.38 pooka * to mount from)
228 1.38 pooka * 2) we failed to flock the source device (i.e. flock() fails,
229 1.38 pooka * not e.g. open() before it)
230 1.38 pooka *
231 1.38 pooka * Otherwise we let the code proceed to mount and let the file system
232 1.38 pooka * throw the proper error. The only questionable bit is that if we
233 1.38 pooka * soft-fail before flock() and mount does succeed...
234 1.38 pooka *
235 1.38 pooka * Returns: -1 error (errno reports error code)
236 1.38 pooka * 0 success
237 1.38 pooka *
238 1.38 pooka * dfdp: -1 device is not open
239 1.38 pooka * n device is open
240 1.38 pooka */
241 1.38 pooka static int
242 1.38 pooka process_diskdevice(const char *devpath, int partition, int rdonly,
243 1.38 pooka int *dfdp, uint64_t *devoff, uint64_t *devsize)
244 1.1 pooka {
245 1.38 pooka char buf[65536];
246 1.22 pooka struct stat sb;
247 1.38 pooka struct ukfs_disklabel dl;
248 1.38 pooka struct ukfs_partition *pp;
249 1.38 pooka int rv = 0, devfd;
250 1.38 pooka
251 1.38 pooka /* defaults */
252 1.38 pooka *devoff = 0;
253 1.38 pooka *devsize = RUMP_ETFS_SIZE_ENDOFF;
254 1.38 pooka *dfdp = -1;
255 1.38 pooka
256 1.38 pooka devfd = open(devpath, rdonly ? O_RDONLY : O_RDWR);
257 1.38 pooka if (devfd == -1) {
258 1.38 pooka if (UKFS_USEPARTITION(partition))
259 1.38 pooka rv = errno;
260 1.38 pooka goto out;
261 1.38 pooka }
262 1.38 pooka
263 1.38 pooka /*
264 1.38 pooka * Locate the disklabel and find the partition in question.
265 1.38 pooka */
266 1.38 pooka if (UKFS_USEPARTITION(partition)) {
267 1.38 pooka if (pread(devfd, buf, sizeof(buf), 0) == -1) {
268 1.38 pooka rv = errno;
269 1.38 pooka goto out;
270 1.38 pooka }
271 1.38 pooka
272 1.38 pooka if (ukfs_disklabel_scan(&dl, buf, sizeof(buf)) != 0) {
273 1.38 pooka rv = ENOENT;
274 1.38 pooka goto out;
275 1.38 pooka }
276 1.38 pooka
277 1.38 pooka if (dl.d_npartitions < partition) {
278 1.38 pooka rv = ENOENT;
279 1.38 pooka goto out;
280 1.38 pooka }
281 1.38 pooka
282 1.38 pooka pp = &dl.d_partitions[partition];
283 1.38 pooka *devoff = pp->p_offset << DEV_BSHIFT;
284 1.38 pooka *devsize = pp->p_size << DEV_BSHIFT;
285 1.38 pooka }
286 1.38 pooka
287 1.38 pooka if (fstat(devfd, &sb) == -1) {
288 1.38 pooka rv = errno;
289 1.38 pooka goto out;
290 1.38 pooka }
291 1.1 pooka
292 1.11 pooka /*
293 1.38 pooka * We do this only for non-block device since the
294 1.38 pooka * (NetBSD) kernel allows block device open only once.
295 1.38 pooka * We also need to close the device for fairly obvious reasons.
296 1.11 pooka */
297 1.38 pooka if (!S_ISBLK(sb.st_mode)) {
298 1.38 pooka if (flock(devfd, LOCK_NB | (rdonly ? LOCK_SH:LOCK_EX)) == -1) {
299 1.38 pooka warnx("ukfs_mount: cannot get %s lock on "
300 1.38 pooka "device", rdonly ? "shared" : "exclusive");
301 1.11 pooka rv = errno;
302 1.11 pooka goto out;
303 1.11 pooka }
304 1.38 pooka } else {
305 1.38 pooka close(devfd);
306 1.38 pooka devfd = -1;
307 1.38 pooka }
308 1.38 pooka *dfdp = devfd;
309 1.22 pooka
310 1.38 pooka out:
311 1.38 pooka if (rv) {
312 1.38 pooka if (devfd != -1)
313 1.22 pooka close(devfd);
314 1.38 pooka errno = rv;
315 1.38 pooka rv = -1;
316 1.11 pooka }
317 1.1 pooka
318 1.38 pooka return rv;
319 1.38 pooka }
320 1.38 pooka
321 1.38 pooka static struct ukfs *
322 1.38 pooka doukfsmount(const char *vfsname, const char *devpath, int partition,
323 1.38 pooka const char *mountpath, int mntflags, void *arg, size_t alen)
324 1.38 pooka {
325 1.38 pooka struct ukfs *fs = NULL;
326 1.39 pooka int rv = 0, devfd = -1;
327 1.38 pooka uint64_t devoff, devsize;
328 1.38 pooka int mounted = 0;
329 1.38 pooka int regged = 0;
330 1.38 pooka
331 1.39 pooka /* XXX: gcc whine */
332 1.39 pooka devoff = 0;
333 1.39 pooka devsize = 0;
334 1.39 pooka
335 1.38 pooka if (partition != UKFS_PARTITION_NA)
336 1.38 pooka process_diskdevice(devpath, partition, mntflags & MNT_RDONLY,
337 1.38 pooka &devfd, &devoff, &devsize);
338 1.38 pooka
339 1.1 pooka fs = malloc(sizeof(struct ukfs));
340 1.1 pooka if (fs == NULL) {
341 1.1 pooka rv = ENOMEM;
342 1.1 pooka goto out;
343 1.1 pooka }
344 1.1 pooka memset(fs, 0, sizeof(struct ukfs));
345 1.30 pooka
346 1.30 pooka /* create our mountpoint. this is never removed. */
347 1.30 pooka if (builddirs(mountpath, 0777, rumpmkdir, NULL) == -1) {
348 1.30 pooka if (errno != EEXIST) {
349 1.30 pooka rv = errno;
350 1.30 pooka goto out;
351 1.30 pooka }
352 1.30 pooka }
353 1.1 pooka
354 1.38 pooka if (partition != UKFS_PARTITION_NA) {
355 1.40 pooka rv = rump_pub_etfs_register_withsize(devpath, devpath,
356 1.38 pooka RUMP_ETFS_BLK, devoff, devsize);
357 1.33 pooka if (rv) {
358 1.33 pooka goto out;
359 1.33 pooka }
360 1.33 pooka regged = 1;
361 1.33 pooka }
362 1.38 pooka
363 1.30 pooka rv = rump_sys_mount(vfsname, mountpath, mntflags, arg, alen);
364 1.1 pooka if (rv) {
365 1.34 pooka rv = errno;
366 1.1 pooka goto out;
367 1.1 pooka }
368 1.30 pooka mounted = 1;
369 1.40 pooka rv = rump_pub_vfs_getmp(mountpath, &fs->ukfs_mp);
370 1.11 pooka if (rv) {
371 1.11 pooka goto out;
372 1.11 pooka }
373 1.40 pooka rv = rump_pub_vfs_root(fs->ukfs_mp, &fs->ukfs_rvp, 0);
374 1.30 pooka if (rv) {
375 1.30 pooka goto out;
376 1.30 pooka }
377 1.30 pooka
378 1.33 pooka if (regged) {
379 1.33 pooka fs->ukfs_devpath = strdup(devpath);
380 1.33 pooka }
381 1.30 pooka fs->ukfs_mountpath = strdup(mountpath);
382 1.11 pooka fs->ukfs_cdir = ukfs_getrvp(fs);
383 1.11 pooka pthread_spin_init(&fs->ukfs_spin, PTHREAD_PROCESS_SHARED);
384 1.11 pooka fs->ukfs_devfd = devfd;
385 1.11 pooka assert(rv == 0);
386 1.1 pooka
387 1.1 pooka out:
388 1.1 pooka if (rv) {
389 1.30 pooka if (fs) {
390 1.30 pooka if (fs->ukfs_rvp)
391 1.40 pooka rump_pub_vp_rele(fs->ukfs_rvp);
392 1.1 pooka free(fs);
393 1.30 pooka fs = NULL;
394 1.30 pooka }
395 1.30 pooka if (mounted)
396 1.30 pooka rump_sys_unmount(mountpath, MNT_FORCE);
397 1.33 pooka if (regged)
398 1.40 pooka rump_pub_etfs_remove(devpath);
399 1.11 pooka if (devfd != -1) {
400 1.11 pooka flock(devfd, LOCK_UN);
401 1.11 pooka close(devfd);
402 1.11 pooka }
403 1.34 pooka errno = rv;
404 1.1 pooka }
405 1.1 pooka
406 1.1 pooka return fs;
407 1.1 pooka }
408 1.1 pooka
409 1.38 pooka struct ukfs *
410 1.38 pooka ukfs_mount(const char *vfsname, const char *devpath,
411 1.38 pooka const char *mountpath, int mntflags, void *arg, size_t alen)
412 1.38 pooka {
413 1.38 pooka
414 1.38 pooka return doukfsmount(vfsname, devpath, UKFS_PARTITION_NA,
415 1.38 pooka mountpath, mntflags, arg, alen);
416 1.38 pooka }
417 1.38 pooka
418 1.38 pooka struct ukfs *
419 1.38 pooka ukfs_mount_disk(const char *vfsname, const char *devpath, int partition,
420 1.38 pooka const char *mountpath, int mntflags, void *arg, size_t alen)
421 1.38 pooka {
422 1.38 pooka
423 1.38 pooka return doukfsmount(vfsname, devpath, partition,
424 1.38 pooka mountpath, mntflags, arg, alen);
425 1.38 pooka }
426 1.38 pooka
427 1.30 pooka int
428 1.1 pooka ukfs_release(struct ukfs *fs, int flags)
429 1.1 pooka {
430 1.1 pooka
431 1.1 pooka if ((flags & UKFS_RELFLAG_NOUNMOUNT) == 0) {
432 1.37 pooka int rv, mntflag, error;
433 1.9 pooka
434 1.30 pooka ukfs_chdir(fs, "/");
435 1.30 pooka mntflag = 0;
436 1.30 pooka if (flags & UKFS_RELFLAG_FORCE)
437 1.30 pooka mntflag = MNT_FORCE;
438 1.41 pooka rump_pub_lwp_alloc_and_switch(nextpid(fs), 1);
439 1.40 pooka rump_pub_vp_rele(fs->ukfs_rvp);
440 1.37 pooka fs->ukfs_rvp = NULL;
441 1.30 pooka rv = rump_sys_unmount(fs->ukfs_mountpath, mntflag);
442 1.37 pooka if (rv == -1) {
443 1.37 pooka error = errno;
444 1.40 pooka rump_pub_vfs_root(fs->ukfs_mp, &fs->ukfs_rvp, 0);
445 1.41 pooka rump_pub_lwp_release(rump_pub_lwp_curlwp());
446 1.30 pooka ukfs_chdir(fs, fs->ukfs_mountpath);
447 1.37 pooka errno = error;
448 1.30 pooka return -1;
449 1.30 pooka }
450 1.41 pooka rump_pub_lwp_release(rump_pub_lwp_curlwp());
451 1.1 pooka }
452 1.1 pooka
453 1.33 pooka if (fs->ukfs_devpath) {
454 1.40 pooka rump_pub_etfs_remove(fs->ukfs_devpath);
455 1.33 pooka free(fs->ukfs_devpath);
456 1.33 pooka }
457 1.30 pooka free(fs->ukfs_mountpath);
458 1.1 pooka
459 1.1 pooka pthread_spin_destroy(&fs->ukfs_spin);
460 1.16 stacktic if (fs->ukfs_devfd != -1) {
461 1.11 pooka flock(fs->ukfs_devfd, LOCK_UN);
462 1.16 stacktic close(fs->ukfs_devfd);
463 1.16 stacktic }
464 1.1 pooka free(fs);
465 1.30 pooka
466 1.30 pooka return 0;
467 1.1 pooka }
468 1.1 pooka
469 1.1 pooka #define STDCALL(ukfs, thecall) \
470 1.1 pooka int rv = 0; \
471 1.1 pooka \
472 1.1 pooka precall(ukfs); \
473 1.21 pooka rv = thecall; \
474 1.1 pooka postcall(ukfs); \
475 1.21 pooka return rv;
476 1.1 pooka
477 1.1 pooka int
478 1.24 pooka ukfs_opendir(struct ukfs *ukfs, const char *dirname, struct ukfs_dircookie **c)
479 1.1 pooka {
480 1.1 pooka struct vnode *vp;
481 1.24 pooka int rv;
482 1.1 pooka
483 1.1 pooka precall(ukfs);
484 1.40 pooka rv = rump_pub_namei(RUMP_NAMEI_LOOKUP, RUMP_NAMEI_LOCKLEAF, dirname,
485 1.1 pooka NULL, &vp, NULL);
486 1.1 pooka postcall(ukfs);
487 1.24 pooka
488 1.24 pooka if (rv == 0) {
489 1.24 pooka RUMP_VOP_UNLOCK(vp, 0);
490 1.24 pooka } else {
491 1.24 pooka errno = rv;
492 1.24 pooka rv = -1;
493 1.24 pooka }
494 1.24 pooka
495 1.24 pooka /*LINTED*/
496 1.24 pooka *c = (struct ukfs_dircookie *)vp;
497 1.24 pooka return rv;
498 1.24 pooka }
499 1.24 pooka
500 1.24 pooka static int
501 1.24 pooka getmydents(struct vnode *vp, off_t *off, uint8_t *buf, size_t bufsize)
502 1.24 pooka {
503 1.24 pooka struct uio *uio;
504 1.24 pooka size_t resid;
505 1.24 pooka int rv, eofflag;
506 1.24 pooka kauth_cred_t cred;
507 1.24 pooka
508 1.40 pooka uio = rump_pub_uio_setup(buf, bufsize, *off, RUMPUIO_READ);
509 1.40 pooka cred = rump_pub_cred_suserget();
510 1.9 pooka rv = RUMP_VOP_READDIR(vp, uio, cred, &eofflag, NULL, NULL);
511 1.40 pooka rump_pub_cred_put(cred);
512 1.17 pooka RUMP_VOP_UNLOCK(vp, 0);
513 1.40 pooka *off = rump_pub_uio_getoff(uio);
514 1.40 pooka resid = rump_pub_uio_free(uio);
515 1.1 pooka
516 1.1 pooka if (rv) {
517 1.1 pooka errno = rv;
518 1.1 pooka return -1;
519 1.1 pooka }
520 1.1 pooka
521 1.1 pooka /* LINTED: not totally correct return type, but follows syscall */
522 1.1 pooka return bufsize - resid;
523 1.1 pooka }
524 1.1 pooka
525 1.24 pooka /*ARGSUSED*/
526 1.24 pooka int
527 1.24 pooka ukfs_getdents_cookie(struct ukfs *ukfs, struct ukfs_dircookie *c, off_t *off,
528 1.24 pooka uint8_t *buf, size_t bufsize)
529 1.24 pooka {
530 1.24 pooka /*LINTED*/
531 1.24 pooka struct vnode *vp = (struct vnode *)c;
532 1.24 pooka
533 1.24 pooka RUMP_VOP_LOCK(vp, RUMP_LK_SHARED);
534 1.24 pooka return getmydents(vp, off, buf, bufsize);
535 1.24 pooka }
536 1.24 pooka
537 1.24 pooka int
538 1.24 pooka ukfs_getdents(struct ukfs *ukfs, const char *dirname, off_t *off,
539 1.24 pooka uint8_t *buf, size_t bufsize)
540 1.24 pooka {
541 1.24 pooka struct vnode *vp;
542 1.24 pooka int rv;
543 1.24 pooka
544 1.24 pooka precall(ukfs);
545 1.40 pooka rv = rump_pub_namei(RUMP_NAMEI_LOOKUP, RUMP_NAMEI_LOCKLEAF, dirname,
546 1.24 pooka NULL, &vp, NULL);
547 1.24 pooka postcall(ukfs);
548 1.24 pooka if (rv) {
549 1.24 pooka errno = rv;
550 1.24 pooka return -1;
551 1.24 pooka }
552 1.24 pooka
553 1.24 pooka rv = getmydents(vp, off, buf, bufsize);
554 1.40 pooka rump_pub_vp_rele(vp);
555 1.24 pooka return rv;
556 1.24 pooka }
557 1.24 pooka
558 1.24 pooka /*ARGSUSED*/
559 1.24 pooka int
560 1.24 pooka ukfs_closedir(struct ukfs *ukfs, struct ukfs_dircookie *c)
561 1.24 pooka {
562 1.24 pooka
563 1.24 pooka /*LINTED*/
564 1.40 pooka rump_pub_vp_rele((struct vnode *)c);
565 1.24 pooka return 0;
566 1.24 pooka }
567 1.24 pooka
568 1.24 pooka int
569 1.24 pooka ukfs_open(struct ukfs *ukfs, const char *filename, int flags)
570 1.24 pooka {
571 1.24 pooka int fd;
572 1.24 pooka
573 1.24 pooka precall(ukfs);
574 1.24 pooka fd = rump_sys_open(filename, flags, 0);
575 1.24 pooka postcall(ukfs);
576 1.24 pooka if (fd == -1)
577 1.24 pooka return -1;
578 1.24 pooka
579 1.24 pooka return fd;
580 1.24 pooka }
581 1.24 pooka
582 1.1 pooka ssize_t
583 1.1 pooka ukfs_read(struct ukfs *ukfs, const char *filename, off_t off,
584 1.1 pooka uint8_t *buf, size_t bufsize)
585 1.1 pooka {
586 1.21 pooka int fd;
587 1.1 pooka ssize_t xfer = -1; /* XXXgcc */
588 1.1 pooka
589 1.1 pooka precall(ukfs);
590 1.21 pooka fd = rump_sys_open(filename, RUMP_O_RDONLY, 0);
591 1.21 pooka if (fd == -1)
592 1.1 pooka goto out;
593 1.1 pooka
594 1.27 pooka xfer = rump_sys_pread(fd, buf, bufsize, off);
595 1.21 pooka rump_sys_close(fd);
596 1.1 pooka
597 1.1 pooka out:
598 1.1 pooka postcall(ukfs);
599 1.21 pooka if (fd == -1) {
600 1.1 pooka return -1;
601 1.1 pooka }
602 1.1 pooka return xfer;
603 1.1 pooka }
604 1.1 pooka
605 1.24 pooka /*ARGSUSED*/
606 1.24 pooka ssize_t
607 1.24 pooka ukfs_read_fd(struct ukfs *ukfs, int fd, off_t off, uint8_t *buf, size_t buflen)
608 1.24 pooka {
609 1.24 pooka
610 1.27 pooka return rump_sys_pread(fd, buf, buflen, off);
611 1.24 pooka }
612 1.24 pooka
613 1.1 pooka ssize_t
614 1.1 pooka ukfs_write(struct ukfs *ukfs, const char *filename, off_t off,
615 1.1 pooka uint8_t *buf, size_t bufsize)
616 1.1 pooka {
617 1.21 pooka int fd;
618 1.1 pooka ssize_t xfer = -1; /* XXXgcc */
619 1.1 pooka
620 1.1 pooka precall(ukfs);
621 1.21 pooka fd = rump_sys_open(filename, RUMP_O_WRONLY, 0);
622 1.21 pooka if (fd == -1)
623 1.1 pooka goto out;
624 1.1 pooka
625 1.1 pooka /* write and commit */
626 1.27 pooka xfer = rump_sys_pwrite(fd, buf, bufsize, off);
627 1.21 pooka if (xfer > 0)
628 1.21 pooka rump_sys_fsync(fd);
629 1.1 pooka
630 1.21 pooka rump_sys_close(fd);
631 1.1 pooka
632 1.1 pooka out:
633 1.1 pooka postcall(ukfs);
634 1.21 pooka if (fd == -1) {
635 1.1 pooka return -1;
636 1.1 pooka }
637 1.1 pooka return xfer;
638 1.1 pooka }
639 1.1 pooka
640 1.24 pooka /*ARGSUSED*/
641 1.24 pooka ssize_t
642 1.24 pooka ukfs_write_fd(struct ukfs *ukfs, int fd, off_t off, uint8_t *buf, size_t buflen,
643 1.24 pooka int dosync)
644 1.24 pooka {
645 1.24 pooka ssize_t xfer;
646 1.24 pooka
647 1.27 pooka xfer = rump_sys_pwrite(fd, buf, buflen, off);
648 1.24 pooka if (xfer > 0 && dosync)
649 1.24 pooka rump_sys_fsync(fd);
650 1.24 pooka
651 1.24 pooka return xfer;
652 1.24 pooka }
653 1.24 pooka
654 1.24 pooka /*ARGSUSED*/
655 1.24 pooka int
656 1.24 pooka ukfs_close(struct ukfs *ukfs, int fd)
657 1.24 pooka {
658 1.24 pooka
659 1.24 pooka rump_sys_close(fd);
660 1.24 pooka return 0;
661 1.24 pooka }
662 1.24 pooka
663 1.1 pooka int
664 1.1 pooka ukfs_create(struct ukfs *ukfs, const char *filename, mode_t mode)
665 1.1 pooka {
666 1.21 pooka int fd;
667 1.1 pooka
668 1.1 pooka precall(ukfs);
669 1.21 pooka fd = rump_sys_open(filename, RUMP_O_WRONLY | RUMP_O_CREAT, mode);
670 1.21 pooka if (fd == -1)
671 1.21 pooka return -1;
672 1.21 pooka rump_sys_close(fd);
673 1.1 pooka
674 1.1 pooka postcall(ukfs);
675 1.1 pooka return 0;
676 1.1 pooka }
677 1.1 pooka
678 1.1 pooka int
679 1.1 pooka ukfs_mknod(struct ukfs *ukfs, const char *path, mode_t mode, dev_t dev)
680 1.1 pooka {
681 1.1 pooka
682 1.21 pooka STDCALL(ukfs, rump_sys_mknod(path, mode, dev));
683 1.1 pooka }
684 1.1 pooka
685 1.1 pooka int
686 1.1 pooka ukfs_mkfifo(struct ukfs *ukfs, const char *path, mode_t mode)
687 1.1 pooka {
688 1.1 pooka
689 1.21 pooka STDCALL(ukfs, rump_sys_mkfifo(path, mode));
690 1.1 pooka }
691 1.1 pooka
692 1.1 pooka int
693 1.1 pooka ukfs_mkdir(struct ukfs *ukfs, const char *filename, mode_t mode)
694 1.1 pooka {
695 1.1 pooka
696 1.21 pooka STDCALL(ukfs, rump_sys_mkdir(filename, mode));
697 1.1 pooka }
698 1.1 pooka
699 1.1 pooka int
700 1.1 pooka ukfs_remove(struct ukfs *ukfs, const char *filename)
701 1.1 pooka {
702 1.1 pooka
703 1.21 pooka STDCALL(ukfs, rump_sys_unlink(filename));
704 1.1 pooka }
705 1.1 pooka
706 1.1 pooka int
707 1.1 pooka ukfs_rmdir(struct ukfs *ukfs, const char *filename)
708 1.1 pooka {
709 1.1 pooka
710 1.21 pooka STDCALL(ukfs, rump_sys_rmdir(filename));
711 1.1 pooka }
712 1.1 pooka
713 1.1 pooka int
714 1.1 pooka ukfs_link(struct ukfs *ukfs, const char *filename, const char *f_create)
715 1.1 pooka {
716 1.1 pooka
717 1.21 pooka STDCALL(ukfs, rump_sys_link(filename, f_create));
718 1.1 pooka }
719 1.1 pooka
720 1.1 pooka int
721 1.1 pooka ukfs_symlink(struct ukfs *ukfs, const char *filename, const char *linkname)
722 1.1 pooka {
723 1.1 pooka
724 1.21 pooka STDCALL(ukfs, rump_sys_symlink(filename, linkname));
725 1.1 pooka }
726 1.1 pooka
727 1.1 pooka ssize_t
728 1.1 pooka ukfs_readlink(struct ukfs *ukfs, const char *filename,
729 1.1 pooka char *linkbuf, size_t buflen)
730 1.1 pooka {
731 1.1 pooka ssize_t rv;
732 1.1 pooka
733 1.1 pooka precall(ukfs);
734 1.21 pooka rv = rump_sys_readlink(filename, linkbuf, buflen);
735 1.1 pooka postcall(ukfs);
736 1.1 pooka return rv;
737 1.1 pooka }
738 1.1 pooka
739 1.1 pooka int
740 1.1 pooka ukfs_rename(struct ukfs *ukfs, const char *from, const char *to)
741 1.1 pooka {
742 1.1 pooka
743 1.21 pooka STDCALL(ukfs, rump_sys_rename(from, to));
744 1.1 pooka }
745 1.1 pooka
746 1.1 pooka int
747 1.1 pooka ukfs_chdir(struct ukfs *ukfs, const char *path)
748 1.1 pooka {
749 1.1 pooka struct vnode *newvp, *oldvp;
750 1.1 pooka int rv;
751 1.1 pooka
752 1.1 pooka precall(ukfs);
753 1.21 pooka rv = rump_sys_chdir(path);
754 1.21 pooka if (rv == -1)
755 1.1 pooka goto out;
756 1.1 pooka
757 1.40 pooka newvp = rump_pub_cdir_get();
758 1.1 pooka pthread_spin_lock(&ukfs->ukfs_spin);
759 1.1 pooka oldvp = ukfs->ukfs_cdir;
760 1.1 pooka ukfs->ukfs_cdir = newvp;
761 1.1 pooka pthread_spin_unlock(&ukfs->ukfs_spin);
762 1.1 pooka if (oldvp)
763 1.40 pooka rump_pub_vp_rele(oldvp);
764 1.1 pooka
765 1.1 pooka out:
766 1.1 pooka postcall(ukfs);
767 1.21 pooka return rv;
768 1.1 pooka }
769 1.1 pooka
770 1.28 pooka /*
771 1.28 pooka * If we want to use post-time_t file systems on pre-time_t hosts,
772 1.28 pooka * we must translate the stat structure. Since we don't currently
773 1.28 pooka * have a general method for making compat calls in rump, special-case
774 1.28 pooka * this one.
775 1.28 pooka *
776 1.28 pooka * Note that this does not allow making system calls to older rump
777 1.28 pooka * kernels from newer hosts.
778 1.28 pooka */
779 1.28 pooka #define VERS_TIMECHANGE 599000700
780 1.28 pooka
781 1.28 pooka static int
782 1.28 pooka needcompat(void)
783 1.28 pooka {
784 1.28 pooka
785 1.28 pooka #ifdef __NetBSD__
786 1.30 pooka /*LINTED*/
787 1.28 pooka return __NetBSD_Version__ < VERS_TIMECHANGE
788 1.40 pooka && rump_pub_getversion() >= VERS_TIMECHANGE;
789 1.28 pooka #else
790 1.28 pooka return 0;
791 1.28 pooka #endif
792 1.28 pooka }
793 1.28 pooka
794 1.1 pooka int
795 1.1 pooka ukfs_stat(struct ukfs *ukfs, const char *filename, struct stat *file_stat)
796 1.1 pooka {
797 1.28 pooka int rv;
798 1.1 pooka
799 1.28 pooka precall(ukfs);
800 1.28 pooka if (needcompat())
801 1.40 pooka rv = rump_pub_sys___stat30(filename, file_stat);
802 1.28 pooka else
803 1.28 pooka rv = rump_sys_stat(filename, file_stat);
804 1.28 pooka postcall(ukfs);
805 1.28 pooka
806 1.28 pooka return rv;
807 1.1 pooka }
808 1.1 pooka
809 1.1 pooka int
810 1.1 pooka ukfs_lstat(struct ukfs *ukfs, const char *filename, struct stat *file_stat)
811 1.1 pooka {
812 1.28 pooka int rv;
813 1.1 pooka
814 1.28 pooka precall(ukfs);
815 1.28 pooka if (needcompat())
816 1.40 pooka rv = rump_pub_sys___lstat30(filename, file_stat);
817 1.28 pooka else
818 1.28 pooka rv = rump_sys_lstat(filename, file_stat);
819 1.28 pooka postcall(ukfs);
820 1.28 pooka
821 1.28 pooka return rv;
822 1.1 pooka }
823 1.1 pooka
824 1.1 pooka int
825 1.1 pooka ukfs_chmod(struct ukfs *ukfs, const char *filename, mode_t mode)
826 1.1 pooka {
827 1.1 pooka
828 1.21 pooka STDCALL(ukfs, rump_sys_chmod(filename, mode));
829 1.1 pooka }
830 1.1 pooka
831 1.1 pooka int
832 1.1 pooka ukfs_lchmod(struct ukfs *ukfs, const char *filename, mode_t mode)
833 1.1 pooka {
834 1.1 pooka
835 1.21 pooka STDCALL(ukfs, rump_sys_lchmod(filename, mode));
836 1.1 pooka }
837 1.1 pooka
838 1.1 pooka int
839 1.1 pooka ukfs_chown(struct ukfs *ukfs, const char *filename, uid_t uid, gid_t gid)
840 1.1 pooka {
841 1.1 pooka
842 1.21 pooka STDCALL(ukfs, rump_sys_chown(filename, uid, gid));
843 1.1 pooka }
844 1.1 pooka
845 1.1 pooka int
846 1.1 pooka ukfs_lchown(struct ukfs *ukfs, const char *filename, uid_t uid, gid_t gid)
847 1.1 pooka {
848 1.1 pooka
849 1.21 pooka STDCALL(ukfs, rump_sys_lchown(filename, uid, gid));
850 1.1 pooka }
851 1.1 pooka
852 1.1 pooka int
853 1.1 pooka ukfs_chflags(struct ukfs *ukfs, const char *filename, u_long flags)
854 1.1 pooka {
855 1.1 pooka
856 1.21 pooka STDCALL(ukfs, rump_sys_chflags(filename, flags));
857 1.1 pooka }
858 1.1 pooka
859 1.1 pooka int
860 1.1 pooka ukfs_lchflags(struct ukfs *ukfs, const char *filename, u_long flags)
861 1.1 pooka {
862 1.1 pooka
863 1.21 pooka STDCALL(ukfs, rump_sys_lchflags(filename, flags));
864 1.1 pooka }
865 1.1 pooka
866 1.1 pooka int
867 1.1 pooka ukfs_utimes(struct ukfs *ukfs, const char *filename, const struct timeval *tptr)
868 1.1 pooka {
869 1.1 pooka
870 1.21 pooka STDCALL(ukfs, rump_sys_utimes(filename, tptr));
871 1.1 pooka }
872 1.1 pooka
873 1.1 pooka int
874 1.1 pooka ukfs_lutimes(struct ukfs *ukfs, const char *filename,
875 1.1 pooka const struct timeval *tptr)
876 1.1 pooka {
877 1.1 pooka
878 1.21 pooka STDCALL(ukfs, rump_sys_lutimes(filename, tptr));
879 1.1 pooka }
880 1.1 pooka
881 1.3 pooka /*
882 1.3 pooka * Dynamic module support
883 1.3 pooka */
884 1.3 pooka
885 1.3 pooka /* load one library */
886 1.3 pooka
887 1.3 pooka /*
888 1.3 pooka * XXX: the dlerror stuff isn't really threadsafe, but then again I
889 1.3 pooka * can't protect against other threads calling dl*() outside of ukfs,
890 1.3 pooka * so just live with it being flimsy
891 1.3 pooka */
892 1.3 pooka int
893 1.3 pooka ukfs_modload(const char *fname)
894 1.3 pooka {
895 1.26 pooka void *handle;
896 1.26 pooka struct modinfo **mi;
897 1.3 pooka int error;
898 1.3 pooka
899 1.42 njoly handle = dlopen(fname, RTLD_LAZY|RTLD_GLOBAL);
900 1.3 pooka if (handle == NULL) {
901 1.13 pooka const char *dlmsg = dlerror();
902 1.13 pooka if (strstr(dlmsg, "Undefined symbol"))
903 1.3 pooka return 0;
904 1.13 pooka warnx("dlopen %s failed: %s\n", fname, dlmsg);
905 1.3 pooka /* XXXerrno */
906 1.3 pooka return -1;
907 1.3 pooka }
908 1.3 pooka
909 1.26 pooka mi = dlsym(handle, "__start_link_set_modules");
910 1.26 pooka if (mi) {
911 1.40 pooka error = rump_pub_module_init(*mi, NULL);
912 1.3 pooka if (error)
913 1.3 pooka goto errclose;
914 1.3 pooka return 1;
915 1.3 pooka }
916 1.3 pooka error = EINVAL;
917 1.3 pooka
918 1.3 pooka errclose:
919 1.3 pooka dlclose(handle);
920 1.3 pooka errno = error;
921 1.3 pooka return -1;
922 1.3 pooka }
923 1.3 pooka
924 1.3 pooka struct loadfail {
925 1.3 pooka char *pname;
926 1.3 pooka
927 1.3 pooka LIST_ENTRY(loadfail) entries;
928 1.3 pooka };
929 1.3 pooka
930 1.3 pooka #define RUMPFSMOD_PREFIX "librumpfs_"
931 1.3 pooka #define RUMPFSMOD_SUFFIX ".so"
932 1.3 pooka
933 1.3 pooka int
934 1.3 pooka ukfs_modload_dir(const char *dir)
935 1.3 pooka {
936 1.3 pooka char nbuf[MAXPATHLEN+1], *p;
937 1.3 pooka struct dirent entry, *result;
938 1.3 pooka DIR *libdir;
939 1.3 pooka struct loadfail *lf, *nlf;
940 1.3 pooka int error, nloaded = 0, redo;
941 1.3 pooka LIST_HEAD(, loadfail) lfs;
942 1.3 pooka
943 1.3 pooka libdir = opendir(dir);
944 1.3 pooka if (libdir == NULL)
945 1.3 pooka return -1;
946 1.3 pooka
947 1.3 pooka LIST_INIT(&lfs);
948 1.3 pooka for (;;) {
949 1.3 pooka if ((error = readdir_r(libdir, &entry, &result)) != 0)
950 1.3 pooka break;
951 1.3 pooka if (!result)
952 1.3 pooka break;
953 1.3 pooka if (strncmp(result->d_name, RUMPFSMOD_PREFIX,
954 1.3 pooka strlen(RUMPFSMOD_PREFIX)) != 0)
955 1.3 pooka continue;
956 1.3 pooka if (((p = strstr(result->d_name, RUMPFSMOD_SUFFIX)) == NULL)
957 1.3 pooka || strlen(p) != strlen(RUMPFSMOD_SUFFIX))
958 1.3 pooka continue;
959 1.3 pooka strlcpy(nbuf, dir, sizeof(nbuf));
960 1.3 pooka strlcat(nbuf, "/", sizeof(nbuf));
961 1.3 pooka strlcat(nbuf, result->d_name, sizeof(nbuf));
962 1.3 pooka switch (ukfs_modload(nbuf)) {
963 1.3 pooka case 0:
964 1.3 pooka lf = malloc(sizeof(*lf));
965 1.3 pooka if (lf == NULL) {
966 1.3 pooka error = ENOMEM;
967 1.3 pooka break;
968 1.3 pooka }
969 1.3 pooka lf->pname = strdup(nbuf);
970 1.3 pooka if (lf->pname == NULL) {
971 1.3 pooka free(lf);
972 1.3 pooka error = ENOMEM;
973 1.3 pooka break;
974 1.3 pooka }
975 1.3 pooka LIST_INSERT_HEAD(&lfs, lf, entries);
976 1.3 pooka break;
977 1.3 pooka case 1:
978 1.3 pooka nloaded++;
979 1.3 pooka break;
980 1.3 pooka default:
981 1.3 pooka /* ignore errors */
982 1.3 pooka break;
983 1.3 pooka }
984 1.3 pooka }
985 1.3 pooka closedir(libdir);
986 1.3 pooka if (error && nloaded != 0)
987 1.3 pooka error = 0;
988 1.3 pooka
989 1.3 pooka /*
990 1.3 pooka * El-cheapo dependency calculator. Just try to load the
991 1.3 pooka * modules n times in a loop
992 1.3 pooka */
993 1.3 pooka for (redo = 1; redo;) {
994 1.3 pooka redo = 0;
995 1.3 pooka nlf = LIST_FIRST(&lfs);
996 1.3 pooka while ((lf = nlf) != NULL) {
997 1.3 pooka nlf = LIST_NEXT(lf, entries);
998 1.3 pooka if (ukfs_modload(lf->pname) == 1) {
999 1.3 pooka nloaded++;
1000 1.3 pooka redo = 1;
1001 1.3 pooka LIST_REMOVE(lf, entries);
1002 1.3 pooka free(lf->pname);
1003 1.3 pooka free(lf);
1004 1.3 pooka }
1005 1.3 pooka }
1006 1.3 pooka }
1007 1.3 pooka
1008 1.3 pooka while ((lf = LIST_FIRST(&lfs)) != NULL) {
1009 1.3 pooka LIST_REMOVE(lf, entries);
1010 1.3 pooka free(lf->pname);
1011 1.3 pooka free(lf);
1012 1.3 pooka }
1013 1.3 pooka
1014 1.3 pooka if (error && nloaded == 0) {
1015 1.3 pooka errno = error;
1016 1.3 pooka return -1;
1017 1.3 pooka }
1018 1.3 pooka
1019 1.3 pooka return nloaded;
1020 1.3 pooka }
1021 1.3 pooka
1022 1.4 pooka /* XXX: this code uses definitions from NetBSD, needs rumpdefs */
1023 1.4 pooka ssize_t
1024 1.4 pooka ukfs_vfstypes(char *buf, size_t buflen)
1025 1.4 pooka {
1026 1.4 pooka int mib[3];
1027 1.4 pooka struct sysctlnode q, ans[128];
1028 1.4 pooka size_t alen;
1029 1.21 pooka int i;
1030 1.4 pooka
1031 1.4 pooka mib[0] = CTL_VFS;
1032 1.4 pooka mib[1] = VFS_GENERIC;
1033 1.4 pooka mib[2] = CTL_QUERY;
1034 1.4 pooka alen = sizeof(ans);
1035 1.4 pooka
1036 1.4 pooka memset(&q, 0, sizeof(q));
1037 1.4 pooka q.sysctl_flags = SYSCTL_VERSION;
1038 1.4 pooka
1039 1.21 pooka if (rump_sys___sysctl(mib, 3, ans, &alen, &q, sizeof(q)) == -1) {
1040 1.4 pooka return -1;
1041 1.4 pooka }
1042 1.4 pooka
1043 1.4 pooka for (i = 0; i < alen/sizeof(ans[0]); i++)
1044 1.4 pooka if (strcmp("fstypes", ans[i].sysctl_name) == 0)
1045 1.4 pooka break;
1046 1.4 pooka if (i == alen/sizeof(ans[0])) {
1047 1.4 pooka errno = ENXIO;
1048 1.4 pooka return -1;
1049 1.4 pooka }
1050 1.4 pooka
1051 1.4 pooka mib[0] = CTL_VFS;
1052 1.4 pooka mib[1] = VFS_GENERIC;
1053 1.4 pooka mib[2] = ans[i].sysctl_num;
1054 1.4 pooka
1055 1.21 pooka if (rump_sys___sysctl(mib, 3, buf, &buflen, NULL, 0) == -1) {
1056 1.4 pooka return -1;
1057 1.4 pooka }
1058 1.4 pooka
1059 1.4 pooka return buflen;
1060 1.4 pooka }
1061 1.3 pooka
1062 1.3 pooka /*
1063 1.3 pooka * Utilities
1064 1.3 pooka */
1065 1.30 pooka static int
1066 1.30 pooka builddirs(const char *pathname, mode_t mode,
1067 1.30 pooka int (*mkdirfn)(struct ukfs *, const char *, mode_t), struct ukfs *fs)
1068 1.1 pooka {
1069 1.1 pooka char *f1, *f2;
1070 1.1 pooka int rv;
1071 1.1 pooka mode_t mask;
1072 1.1 pooka bool end;
1073 1.1 pooka
1074 1.1 pooka /*ukfs_umask((mask = ukfs_umask(0)));*/
1075 1.1 pooka umask((mask = umask(0)));
1076 1.1 pooka
1077 1.1 pooka f1 = f2 = strdup(pathname);
1078 1.1 pooka if (f1 == NULL) {
1079 1.1 pooka errno = ENOMEM;
1080 1.1 pooka return -1;
1081 1.1 pooka }
1082 1.1 pooka
1083 1.1 pooka end = false;
1084 1.1 pooka for (;;) {
1085 1.1 pooka /* find next component */
1086 1.1 pooka f2 += strspn(f2, "/");
1087 1.1 pooka f2 += strcspn(f2, "/");
1088 1.1 pooka if (*f2 == '\0')
1089 1.1 pooka end = true;
1090 1.1 pooka else
1091 1.1 pooka *f2 = '\0';
1092 1.1 pooka
1093 1.30 pooka rv = mkdirfn(fs, f1, mode & ~mask);
1094 1.1 pooka if (errno == EEXIST)
1095 1.1 pooka rv = 0;
1096 1.1 pooka
1097 1.1 pooka if (rv == -1 || *f2 != '\0' || end)
1098 1.1 pooka break;
1099 1.1 pooka
1100 1.1 pooka *f2 = '/';
1101 1.1 pooka }
1102 1.1 pooka
1103 1.1 pooka free(f1);
1104 1.1 pooka
1105 1.1 pooka return rv;
1106 1.1 pooka }
1107 1.30 pooka
1108 1.30 pooka int
1109 1.30 pooka ukfs_util_builddirs(struct ukfs *ukfs, const char *pathname, mode_t mode)
1110 1.30 pooka {
1111 1.30 pooka
1112 1.30 pooka return builddirs(pathname, mode, ukfs_mkdir, ukfs);
1113 1.30 pooka }
1114