rump.c revision 1.31 1 /* $NetBSD: rump.c,v 1.31 2008/01/24 22:41:08 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
5 *
6 * Development of this software was supported by Google Summer of Code.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/param.h>
31 #include <sys/cpu.h>
32 #include <sys/filedesc.h>
33 #include <sys/kauth.h>
34 #include <sys/kmem.h>
35 #include <sys/mount.h>
36 #include <sys/namei.h>
37 #include <sys/queue.h>
38 #include <sys/resourcevar.h>
39 #include <sys/select.h>
40 #include <sys/vnode.h>
41
42 #include <miscfs/specfs/specdev.h>
43
44 #include "rump_private.h"
45 #include "rumpuser.h"
46
47 struct proc rump_proc;
48 struct cwdinfo rump_cwdi;
49 struct pstats rump_stats;
50 struct plimit rump_limits;
51 kauth_cred_t rump_cred = RUMPCRED_SUSER;
52 struct cpu_info rump_cpu;
53 struct filedesc0 rump_filedesc0;
54
55 kmutex_t rump_giantlock;
56
57 sigset_t sigcantmask;
58
59 struct fakeblk {
60 char path[MAXPATHLEN];
61 LIST_ENTRY(fakeblk) entries;
62 };
63
64 static LIST_HEAD(, fakeblk) fakeblks = LIST_HEAD_INITIALIZER(fakeblks);
65
66 #ifndef RUMP_WITHOUT_THREADS
67 static void
68 rump_aiodone_worker(struct work *wk, void *dummy)
69 {
70 struct buf *bp = (struct buf *)wk;
71
72 KASSERT(&bp->b_work == wk);
73 bp->b_iodone(bp);
74 }
75 #endif /* RUMP_WITHOUT_THREADS */
76
77 int rump_inited;
78
79 void
80 rump_init()
81 {
82 extern char hostname[];
83 extern size_t hostnamelen;
84 extern kmutex_t rump_atomic_lock;
85 struct proc *p;
86 struct lwp *l;
87 int error;
88
89 /* XXX */
90 if (rump_inited)
91 return;
92 rump_inited = 1;
93
94 l = &lwp0;
95 p = &rump_proc;
96 p->p_stats = &rump_stats;
97 p->p_cwdi = &rump_cwdi;
98 p->p_limit = &rump_limits;
99 p->p_pid = 0;
100 p->p_fd = &rump_filedesc0.fd_fd;
101 p->p_vmspace = &rump_vmspace;
102 l->l_cred = rump_cred;
103 l->l_proc = p;
104 l->l_lid = 1;
105 rw_init(&rump_cwdi.cwdi_lock);
106
107 mutex_init(&rump_atomic_lock, MUTEX_DEFAULT, IPL_NONE);
108 rumpvm_init();
109
110 rump_limits.pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
111 rump_limits.pl_rlimit[RLIMIT_NOFILE].rlim_cur = RLIM_INFINITY;
112
113 /* should be "enough" */
114 syncdelay = 0;
115
116 vfsinit();
117 bufinit();
118 filedesc_init();
119 selsysinit();
120
121 rumpvfs_init();
122
123 rump_sleepers_init();
124 rumpuser_thrinit();
125
126 rumpuser_mutex_recursive_init(&rump_giantlock.kmtx_mtx);
127
128 #ifndef RUMP_WITHOUT_THREADS
129 /* aieeeedondest */
130 if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
131 rump_aiodone_worker, NULL, 0, 0, 0))
132 panic("aiodoned");
133 #endif /* RUMP_WITHOUT_THREADS */
134
135 rumpuser_gethostname(hostname, MAXHOSTNAMELEN, &error);
136 hostnamelen = strlen(hostname);
137
138 sigemptyset(&sigcantmask);
139
140 fdinit1(&rump_filedesc0);
141 }
142
143 struct mount *
144 rump_mnt_init(struct vfsops *vfsops, int mntflags)
145 {
146 struct mount *mp;
147
148 mp = kmem_zalloc(sizeof(struct mount), KM_SLEEP);
149
150 mp->mnt_op = vfsops;
151 mp->mnt_flag = mntflags;
152 TAILQ_INIT(&mp->mnt_vnodelist);
153
154 mount_initspecific(mp);
155
156 return mp;
157 }
158
159 int
160 rump_mnt_mount(struct mount *mp, const char *path, void *data, size_t *dlen)
161 {
162 int rv;
163
164 rv = VFS_MOUNT(mp, path, data, dlen);
165 if (rv)
166 return rv;
167
168 (void) VFS_STATVFS(mp, &mp->mnt_stat);
169 rv = VFS_START(mp, 0);
170 if (rv)
171 VFS_UNMOUNT(mp, MNT_FORCE);
172
173 return rv;
174 }
175
176 void
177 rump_mnt_destroy(struct mount *mp)
178 {
179
180 mount_finispecific(mp);
181 kmem_free(mp, sizeof(*mp));
182 }
183
184 struct componentname *
185 rump_makecn(u_long nameiop, u_long flags, const char *name, size_t namelen,
186 kauth_cred_t creds, struct lwp *l)
187 {
188 struct componentname *cnp;
189 const char *cp = NULL;
190
191 cnp = kmem_zalloc(sizeof(struct componentname), KM_SLEEP);
192
193 cnp->cn_nameiop = nameiop;
194 cnp->cn_flags = flags;
195
196 cnp->cn_pnbuf = PNBUF_GET();
197 strcpy(cnp->cn_pnbuf, name);
198 cnp->cn_nameptr = cnp->cn_pnbuf;
199 cnp->cn_namelen = namelen;
200 cnp->cn_hash = namei_hash(name, &cp);
201
202 cnp->cn_cred = creds;
203
204 return cnp;
205 }
206
207 void
208 rump_freecn(struct componentname *cnp, int flags)
209 {
210
211 if (flags & RUMPCN_FREECRED)
212 rump_cred_destroy(cnp->cn_cred);
213
214 if (cnp->cn_flags & SAVENAME) {
215 if (flags & RUMPCN_ISLOOKUP || cnp->cn_flags & SAVESTART)
216 PNBUF_PUT(cnp->cn_pnbuf);
217 } else {
218 PNBUF_PUT(cnp->cn_pnbuf);
219 }
220 kmem_free(cnp, sizeof(*cnp));
221 }
222
223 int
224 rump_recyclenode(struct vnode *vp)
225 {
226
227 return vrecycle(vp, NULL, curlwp);
228 }
229
230 static struct fakeblk *
231 _rump_fakeblk_find(const char *path)
232 {
233 char buf[MAXPATHLEN];
234 struct fakeblk *fblk;
235 int error;
236
237 if (rumpuser_realpath(path, buf, &error) == NULL)
238 return NULL;
239
240 LIST_FOREACH(fblk, &fakeblks, entries)
241 if (strcmp(fblk->path, buf) == 0)
242 return fblk;
243
244 return NULL;
245 }
246
247 int
248 rump_fakeblk_register(const char *path)
249 {
250 char buf[MAXPATHLEN];
251 struct fakeblk *fblk;
252 int error;
253
254 if (_rump_fakeblk_find(path))
255 return EEXIST;
256
257 if (rumpuser_realpath(path, buf, &error) == NULL)
258 return error;
259
260 fblk = kmem_alloc(sizeof(struct fakeblk), KM_NOSLEEP);
261 if (fblk == NULL)
262 return ENOMEM;
263
264 strlcpy(fblk->path, buf, MAXPATHLEN);
265 LIST_INSERT_HEAD(&fakeblks, fblk, entries);
266
267 return 0;
268 }
269
270 int
271 rump_fakeblk_find(const char *path)
272 {
273
274 return _rump_fakeblk_find(path) != NULL;
275 }
276
277 void
278 rump_fakeblk_deregister(const char *path)
279 {
280 struct fakeblk *fblk;
281
282 fblk = _rump_fakeblk_find(path);
283 if (fblk == NULL)
284 return;
285
286 LIST_REMOVE(fblk, entries);
287 kmem_free(fblk, sizeof(*fblk));
288 }
289
290 void
291 rump_getvninfo(struct vnode *vp, enum vtype *vtype, voff_t *vsize, dev_t *vdev)
292 {
293
294 *vtype = vp->v_type;
295 *vsize = vp->v_size;
296 if (vp->v_specnode)
297 *vdev = vp->v_rdev;
298 else
299 *vdev = 0;
300 }
301
302 struct vfsops *
303 rump_vfslist_iterate(struct vfsops *ops)
304 {
305
306 if (ops == NULL)
307 return LIST_FIRST(&vfs_list);
308 else
309 return LIST_NEXT(ops, vfs_list);
310 }
311
312 struct vfsops *
313 rump_vfs_getopsbyname(const char *name)
314 {
315
316 return vfs_getopsbyname(name);
317 }
318
319 struct vattr*
320 rump_vattr_init()
321 {
322 struct vattr *vap;
323
324 vap = kmem_alloc(sizeof(struct vattr), KM_SLEEP);
325 vattr_null(vap);
326
327 return vap;
328 }
329
330 void
331 rump_vattr_settype(struct vattr *vap, enum vtype vt)
332 {
333
334 vap->va_type = vt;
335 }
336
337 void
338 rump_vattr_setmode(struct vattr *vap, mode_t mode)
339 {
340
341 vap->va_mode = mode;
342 }
343
344 void
345 rump_vattr_setrdev(struct vattr *vap, dev_t dev)
346 {
347
348 vap->va_rdev = dev;
349 }
350
351 void
352 rump_vattr_free(struct vattr *vap)
353 {
354
355 kmem_free(vap, sizeof(*vap));
356 }
357
358 void
359 rump_vp_incref(struct vnode *vp)
360 {
361
362 ++vp->v_usecount;
363 }
364
365 int
366 rump_vp_getref(struct vnode *vp)
367 {
368
369 return vp->v_usecount;
370 }
371
372 void
373 rump_vp_decref(struct vnode *vp)
374 {
375
376 --vp->v_usecount;
377 }
378
379 struct uio *
380 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
381 {
382 struct uio *uio;
383 enum uio_rw uiorw;
384
385 switch (rw) {
386 case RUMPUIO_READ:
387 uiorw = UIO_READ;
388 break;
389 case RUMPUIO_WRITE:
390 uiorw = UIO_WRITE;
391 break;
392 default:
393 panic("%s: invalid rw %d", __func__, rw);
394 }
395
396 uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
397 uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
398
399 uio->uio_iov->iov_base = buf;
400 uio->uio_iov->iov_len = bufsize;
401
402 uio->uio_iovcnt = 1;
403 uio->uio_offset = offset;
404 uio->uio_resid = bufsize;
405 uio->uio_rw = uiorw;
406 uio->uio_vmspace = UIO_VMSPACE_SYS;
407
408 return uio;
409 }
410
411 size_t
412 rump_uio_getresid(struct uio *uio)
413 {
414
415 return uio->uio_resid;
416 }
417
418 off_t
419 rump_uio_getoff(struct uio *uio)
420 {
421
422 return uio->uio_offset;
423 }
424
425 size_t
426 rump_uio_free(struct uio *uio)
427 {
428 size_t resid;
429
430 resid = uio->uio_resid;
431 kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
432 kmem_free(uio, sizeof(*uio));
433
434 return resid;
435 }
436
437 void
438 rump_vp_lock_exclusive(struct vnode *vp)
439 {
440
441 /* we can skip vn_lock() */
442 VOP_LOCK(vp, LK_EXCLUSIVE);
443 }
444
445 void
446 rump_vp_lock_shared(struct vnode *vp)
447 {
448
449 VOP_LOCK(vp, LK_SHARED);
450 }
451
452 void
453 rump_vp_unlock(struct vnode *vp)
454 {
455
456 VOP_UNLOCK(vp, 0);
457 }
458
459 int
460 rump_vp_islocked(struct vnode *vp)
461 {
462
463 return VOP_ISLOCKED(vp);
464 }
465
466 void
467 rump_vp_interlock(struct vnode *vp)
468 {
469
470 mutex_enter(&vp->v_interlock);
471 }
472
473 int
474 rump_vfs_unmount(struct mount *mp, int mntflags)
475 {
476
477 return VFS_UNMOUNT(mp, mntflags);
478 }
479
480 int
481 rump_vfs_root(struct mount *mp, struct vnode **vpp, int lock)
482 {
483 int rv;
484
485 rv = VFS_ROOT(mp, vpp);
486 if (rv)
487 return rv;
488
489 if (!lock)
490 VOP_UNLOCK(*vpp, 0);
491
492 return 0;
493 }
494
495 /* XXX: statvfs is different from system to system */
496 #if 0
497 int
498 rump_vfs_statvfs(struct mount *mp, struct statvfs *sbp)
499 {
500
501 return VFS_STATVFS(mp, sbp);
502 }
503 #endif
504
505 int
506 rump_vfs_sync(struct mount *mp, int wait, kauth_cred_t cred)
507 {
508
509 return VFS_SYNC(mp, wait ? MNT_WAIT : MNT_NOWAIT, cred);
510 }
511
512 int
513 rump_vfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
514 {
515
516 return VFS_FHTOVP(mp, fid, vpp);
517 }
518
519 int
520 rump_vfs_vptofh(struct vnode *vp, struct fid *fid, size_t *fidsize)
521 {
522
523 return VFS_VPTOFH(vp, fid, fidsize);
524 }
525
526 /*ARGSUSED*/
527 void
528 rump_vfs_syncwait(struct mount *mp)
529 {
530 int n;
531
532 n = buf_syncwait();
533 if (n)
534 printf("syncwait: unsynced buffers: %d\n", n);
535 }
536
537 void
538 rump_bioops_sync()
539 {
540
541 if (bioopsp)
542 bioopsp->io_sync(NULL);
543 }
544
545 struct lwp *
546 rump_setup_curlwp(pid_t pid, lwpid_t lid, int set)
547 {
548 struct lwp *l;
549 struct proc *p;
550
551 l = kmem_alloc(sizeof(struct lwp), KM_SLEEP);
552 p = kmem_alloc(sizeof(struct proc), KM_SLEEP);
553 p->p_stats = &rump_stats;
554 p->p_cwdi = &rump_cwdi;
555 p->p_limit = &rump_limits;
556 p->p_pid = pid;
557 p->p_vmspace = &rump_vmspace;
558 l->l_cred = rump_cred;
559 l->l_proc = p;
560 l->l_lid = lid;
561
562 if (set)
563 rumpuser_set_curlwp(l);
564
565 return l;
566 }
567
568 void
569 rump_clear_curlwp()
570 {
571 struct lwp *l;
572
573 l = rumpuser_get_curlwp();
574 kmem_free(l->l_proc, sizeof(struct proc));
575 kmem_free(l, sizeof(struct lwp));
576 rumpuser_set_curlwp(NULL);
577 }
578
579 struct lwp *
580 rump_get_curlwp()
581 {
582 struct lwp *l;
583
584 l = rumpuser_get_curlwp();
585 if (l == NULL)
586 l = &lwp0;
587
588 return l;
589 }
590
591 int
592 rump_splfoo()
593 {
594
595 if (rumpuser_whatis_ipl() != RUMPUSER_IPL_INTR) {
596 rumpuser_rw_enter(&rumpspl, 0);
597 rumpuser_set_ipl(RUMPUSER_IPL_SPLFOO);
598 }
599
600 return 0;
601 }
602
603 static void
604 rump_intr_enter(void)
605 {
606
607 rumpuser_set_ipl(RUMPUSER_IPL_INTR);
608 rumpuser_rw_enter(&rumpspl, 1);
609 }
610
611 static void
612 rump_intr_exit(void)
613 {
614
615 rumpuser_rw_exit(&rumpspl);
616 rumpuser_clear_ipl(RUMPUSER_IPL_INTR);
617 }
618
619 void
620 rump_splx(int dummy)
621 {
622
623 if (rumpuser_whatis_ipl() != RUMPUSER_IPL_INTR) {
624 rumpuser_clear_ipl(RUMPUSER_IPL_SPLFOO);
625 rumpuser_rw_exit(&rumpspl);
626 }
627 }
628
629 void
630 rump_biodone(void *arg, size_t count, int error)
631 {
632 struct buf *bp = arg;
633
634 bp->b_resid = bp->b_bcount - count;
635 KASSERT(bp->b_resid >= 0);
636 bp->b_error = error;
637
638 rump_intr_enter();
639 biodone(bp);
640 rump_intr_exit();
641 }
642