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