rump.c revision 1.55 1 /* $NetBSD: rump.c,v 1.55 2008/09/02 19:38:25 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/module.h>
36 #include <sys/mount.h>
37 #include <sys/namei.h>
38 #include <sys/queue.h>
39 #include <sys/resourcevar.h>
40 #include <sys/select.h>
41 #include <sys/vnode.h>
42 #include <sys/vfs_syscalls.h>
43 #include <sys/wapbl.h>
44 #include <sys/sysctl.h>
45
46 #include <miscfs/specfs/specdev.h>
47
48 #include <rump/rumpuser.h>
49
50 #include "rump_private.h"
51
52 struct proc proc0;
53 struct cwdinfo rump_cwdi;
54 struct pstats rump_stats;
55 struct plimit rump_limits;
56 kauth_cred_t rump_cred = RUMPCRED_SUSER;
57 struct cpu_info rump_cpu;
58 struct filedesc rump_filedesc0;
59 struct proclist allproc;
60 char machine[] = "rump";
61
62 kmutex_t rump_giantlock;
63
64 sigset_t sigcantmask;
65
66 #ifdef RUMP_WITHOUT_THREADS
67 int rump_threads = 0;
68 #else
69 int rump_threads = 1;
70 #endif
71
72 struct fakeblk {
73 char path[MAXPATHLEN];
74 LIST_ENTRY(fakeblk) entries;
75 };
76
77 static LIST_HEAD(, fakeblk) fakeblks = LIST_HEAD_INITIALIZER(fakeblks);
78
79 static void
80 rump_aiodone_worker(struct work *wk, void *dummy)
81 {
82 struct buf *bp = (struct buf *)wk;
83
84 KASSERT(&bp->b_work == wk);
85 bp->b_iodone(bp);
86 }
87
88 static int rump_inited;
89 static struct emul emul_rump;
90
91 void
92 rump_init()
93 {
94 extern char hostname[];
95 extern size_t hostnamelen;
96 extern kmutex_t rump_atomic_lock;
97 char buf[256];
98 struct proc *p;
99 struct lwp *l;
100 int error;
101
102 /* XXX */
103 if (rump_inited)
104 return;
105 rump_inited = 1;
106
107 if (rumpuser_getenv("RUMP_NVNODES", buf, sizeof(buf), &error) == 0) {
108 desiredvnodes = strtoul(buf, NULL, 10);
109 } else {
110 desiredvnodes = 1<<16;
111 }
112 if (rumpuser_getenv("RUMP_THREADS", buf, sizeof(buf), &error) == 0) {
113 rump_threads = *buf != '0';
114 }
115
116 rumpvm_init();
117 rump_sleepers_init();
118 #ifdef RUMP_USE_REAL_KMEM
119 kmem_init();
120 #endif
121
122 cache_cpu_init(&rump_cpu);
123 rw_init(&rump_cwdi.cwdi_lock);
124 l = &lwp0;
125 p = &proc0;
126 p->p_stats = &rump_stats;
127 p->p_cwdi = &rump_cwdi;
128 p->p_limit = &rump_limits;
129 p->p_pid = 0;
130 p->p_fd = &rump_filedesc0;
131 p->p_vmspace = &rump_vmspace;
132 p->p_emul = &emul_rump;
133 l->l_cred = rump_cred;
134 l->l_proc = p;
135 l->l_lid = 1;
136
137 LIST_INSERT_HEAD(&allproc, p, p_list);
138
139 mutex_init(&rump_atomic_lock, MUTEX_DEFAULT, IPL_NONE);
140
141 rump_limits.pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
142 rump_limits.pl_rlimit[RLIMIT_NOFILE].rlim_cur = RLIM_INFINITY;
143
144 syncdelay = 0;
145 dovfsusermount = 1;
146
147 rumpuser_thrinit();
148
149 fd_sys_init();
150 module_init();
151 sysctl_init();
152 vfsinit();
153 bufinit();
154 wapbl_init();
155
156 rumpvfs_init();
157
158 rumpuser_mutex_recursive_init(&rump_giantlock.kmtx_mtx);
159
160 /* aieeeedondest */
161 if (rump_threads) {
162 if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
163 rump_aiodone_worker, NULL, 0, 0, 0))
164 panic("aiodoned");
165 }
166
167 rumpuser_gethostname(hostname, MAXHOSTNAMELEN, &error);
168 hostnamelen = strlen(hostname);
169
170 sigemptyset(&sigcantmask);
171
172 fd_init(&rump_filedesc0);
173 rump_cwdi.cwdi_cdir = rootvnode;
174 }
175
176 struct mount *
177 rump_mnt_init(struct vfsops *vfsops, int mntflags)
178 {
179 struct mount *mp;
180
181 mp = kmem_zalloc(sizeof(struct mount), KM_SLEEP);
182
183 mp->mnt_op = vfsops;
184 mp->mnt_flag = mntflags;
185 TAILQ_INIT(&mp->mnt_vnodelist);
186 rw_init(&mp->mnt_unmounting);
187 mutex_init(&mp->mnt_updating, MUTEX_DEFAULT, IPL_NONE);
188 mutex_init(&mp->mnt_renamelock, MUTEX_DEFAULT, IPL_NONE);
189 mp->mnt_refcnt = 1;
190
191 mount_initspecific(mp);
192
193 return mp;
194 }
195
196 int
197 rump_mnt_mount(struct mount *mp, const char *path, void *data, size_t *dlen)
198 {
199 int rv;
200
201 rv = VFS_MOUNT(mp, path, data, dlen);
202 if (rv)
203 return rv;
204
205 (void) VFS_STATVFS(mp, &mp->mnt_stat);
206 rv = VFS_START(mp, 0);
207 if (rv)
208 VFS_UNMOUNT(mp, MNT_FORCE);
209
210 return rv;
211 }
212
213 void
214 rump_mnt_destroy(struct mount *mp)
215 {
216
217 mount_finispecific(mp);
218 kmem_free(mp, sizeof(*mp));
219 }
220
221 struct componentname *
222 rump_makecn(u_long nameiop, u_long flags, const char *name, size_t namelen,
223 kauth_cred_t creds, struct lwp *l)
224 {
225 struct componentname *cnp;
226 const char *cp = NULL;
227
228 cnp = kmem_zalloc(sizeof(struct componentname), KM_SLEEP);
229
230 cnp->cn_nameiop = nameiop;
231 cnp->cn_flags = flags;
232
233 cnp->cn_pnbuf = PNBUF_GET();
234 strcpy(cnp->cn_pnbuf, name);
235 cnp->cn_nameptr = cnp->cn_pnbuf;
236 cnp->cn_namelen = namelen;
237 cnp->cn_hash = namei_hash(name, &cp);
238
239 cnp->cn_cred = creds;
240
241 return cnp;
242 }
243
244 void
245 rump_freecn(struct componentname *cnp, int flags)
246 {
247
248 if (flags & RUMPCN_FREECRED)
249 rump_cred_destroy(cnp->cn_cred);
250
251 if ((flags & RUMPCN_HASNTBUF) == 0) {
252 if (cnp->cn_flags & SAVENAME) {
253 if (flags & RUMPCN_ISLOOKUP ||cnp->cn_flags & SAVESTART)
254 PNBUF_PUT(cnp->cn_pnbuf);
255 } else {
256 PNBUF_PUT(cnp->cn_pnbuf);
257 }
258 }
259 kmem_free(cnp, sizeof(*cnp));
260 }
261
262 /* hey baby, what's your namei? */
263 int
264 rump_namei(uint32_t op, uint32_t flags, const char *namep,
265 struct vnode **dvpp, struct vnode **vpp, struct componentname **cnpp)
266 {
267 struct nameidata nd;
268 int rv;
269
270 NDINIT(&nd, op, flags, UIO_SYSSPACE, namep);
271 rv = namei(&nd);
272 if (rv)
273 return rv;
274
275 if (dvpp) {
276 KASSERT(flags & LOCKPARENT);
277 *dvpp = nd.ni_dvp;
278 } else {
279 KASSERT((flags & LOCKPARENT) == 0);
280 }
281
282 if (vpp) {
283 *vpp = nd.ni_vp;
284 } else {
285 if (nd.ni_vp) {
286 if (flags & LOCKLEAF)
287 vput(nd.ni_vp);
288 else
289 vrele(nd.ni_vp);
290 }
291 }
292
293 if (cnpp) {
294 struct componentname *cnp;
295
296 cnp = kmem_alloc(sizeof(*cnp), KM_SLEEP);
297 memcpy(cnp, &nd.ni_cnd, sizeof(*cnp));
298 *cnpp = cnp;
299 } else if (nd.ni_cnd.cn_flags & HASBUF) {
300 panic("%s: pathbuf mismatch", __func__);
301 }
302
303 return rv;
304 }
305
306 static struct fakeblk *
307 _rump_fakeblk_find(const char *path)
308 {
309 char buf[MAXPATHLEN];
310 struct fakeblk *fblk;
311 int error;
312
313 if (rumpuser_realpath(path, buf, &error) == NULL)
314 return NULL;
315
316 LIST_FOREACH(fblk, &fakeblks, entries)
317 if (strcmp(fblk->path, buf) == 0)
318 return fblk;
319
320 return NULL;
321 }
322
323 int
324 rump_fakeblk_register(const char *path)
325 {
326 char buf[MAXPATHLEN];
327 struct fakeblk *fblk;
328 int error;
329
330 if (_rump_fakeblk_find(path))
331 return EEXIST;
332
333 if (rumpuser_realpath(path, buf, &error) == NULL)
334 return error;
335
336 fblk = kmem_alloc(sizeof(struct fakeblk), KM_NOSLEEP);
337 if (fblk == NULL)
338 return ENOMEM;
339
340 strlcpy(fblk->path, buf, MAXPATHLEN);
341 LIST_INSERT_HEAD(&fakeblks, fblk, entries);
342
343 return 0;
344 }
345
346 int
347 rump_fakeblk_find(const char *path)
348 {
349
350 return _rump_fakeblk_find(path) != NULL;
351 }
352
353 void
354 rump_fakeblk_deregister(const char *path)
355 {
356 struct fakeblk *fblk;
357
358 fblk = _rump_fakeblk_find(path);
359 if (fblk == NULL)
360 return;
361
362 LIST_REMOVE(fblk, entries);
363 kmem_free(fblk, sizeof(*fblk));
364 }
365
366 void
367 rump_getvninfo(struct vnode *vp, enum vtype *vtype, voff_t *vsize, dev_t *vdev)
368 {
369
370 *vtype = vp->v_type;
371 *vsize = vp->v_size;
372 if (vp->v_specnode)
373 *vdev = vp->v_rdev;
374 else
375 *vdev = 0;
376 }
377
378 struct vfsops *
379 rump_vfslist_iterate(struct vfsops *ops)
380 {
381
382 if (ops == NULL)
383 return LIST_FIRST(&vfs_list);
384 else
385 return LIST_NEXT(ops, vfs_list);
386 }
387
388 struct vfsops *
389 rump_vfs_getopsbyname(const char *name)
390 {
391
392 return vfs_getopsbyname(name);
393 }
394
395 struct vattr*
396 rump_vattr_init()
397 {
398 struct vattr *vap;
399
400 vap = kmem_alloc(sizeof(struct vattr), KM_SLEEP);
401 vattr_null(vap);
402
403 return vap;
404 }
405
406 void
407 rump_vattr_settype(struct vattr *vap, enum vtype vt)
408 {
409
410 vap->va_type = vt;
411 }
412
413 void
414 rump_vattr_setmode(struct vattr *vap, mode_t mode)
415 {
416
417 vap->va_mode = mode;
418 }
419
420 void
421 rump_vattr_setrdev(struct vattr *vap, dev_t dev)
422 {
423
424 vap->va_rdev = dev;
425 }
426
427 void
428 rump_vattr_free(struct vattr *vap)
429 {
430
431 kmem_free(vap, sizeof(*vap));
432 }
433
434 void
435 rump_vp_incref(struct vnode *vp)
436 {
437
438 mutex_enter(&vp->v_interlock);
439 ++vp->v_usecount;
440 mutex_exit(&vp->v_interlock);
441 }
442
443 int
444 rump_vp_getref(struct vnode *vp)
445 {
446
447 return vp->v_usecount;
448 }
449
450 void
451 rump_vp_decref(struct vnode *vp)
452 {
453
454 mutex_enter(&vp->v_interlock);
455 --vp->v_usecount;
456 mutex_exit(&vp->v_interlock);
457 }
458
459 /*
460 * Really really recycle with a cherry on top. We should be
461 * extra-sure we can do this. For example with p2k there is
462 * no problem, since puffs in the kernel takes care of refcounting
463 * for us.
464 */
465 void
466 rump_vp_recycle_nokidding(struct vnode *vp)
467 {
468
469 mutex_enter(&vp->v_interlock);
470 vp->v_usecount = 1;
471 vclean(vp, DOCLOSE);
472 vrelel(vp, 0);
473 }
474
475 void
476 rump_vp_rele(struct vnode *vp)
477 {
478
479 vrele(vp);
480 }
481
482 struct uio *
483 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
484 {
485 struct uio *uio;
486 enum uio_rw uiorw;
487
488 switch (rw) {
489 case RUMPUIO_READ:
490 uiorw = UIO_READ;
491 break;
492 case RUMPUIO_WRITE:
493 uiorw = UIO_WRITE;
494 break;
495 default:
496 panic("%s: invalid rw %d", __func__, rw);
497 }
498
499 uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
500 uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
501
502 uio->uio_iov->iov_base = buf;
503 uio->uio_iov->iov_len = bufsize;
504
505 uio->uio_iovcnt = 1;
506 uio->uio_offset = offset;
507 uio->uio_resid = bufsize;
508 uio->uio_rw = uiorw;
509 uio->uio_vmspace = UIO_VMSPACE_SYS;
510
511 return uio;
512 }
513
514 size_t
515 rump_uio_getresid(struct uio *uio)
516 {
517
518 return uio->uio_resid;
519 }
520
521 off_t
522 rump_uio_getoff(struct uio *uio)
523 {
524
525 return uio->uio_offset;
526 }
527
528 size_t
529 rump_uio_free(struct uio *uio)
530 {
531 size_t resid;
532
533 resid = uio->uio_resid;
534 kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
535 kmem_free(uio, sizeof(*uio));
536
537 return resid;
538 }
539
540 void
541 rump_vp_lock_exclusive(struct vnode *vp)
542 {
543
544 /* we can skip vn_lock() */
545 VOP_LOCK(vp, LK_EXCLUSIVE);
546 }
547
548 void
549 rump_vp_lock_shared(struct vnode *vp)
550 {
551
552 VOP_LOCK(vp, LK_SHARED);
553 }
554
555 void
556 rump_vp_unlock(struct vnode *vp)
557 {
558
559 VOP_UNLOCK(vp, 0);
560 }
561
562 int
563 rump_vp_islocked(struct vnode *vp)
564 {
565
566 return VOP_ISLOCKED(vp);
567 }
568
569 void
570 rump_vp_interlock(struct vnode *vp)
571 {
572
573 mutex_enter(&vp->v_interlock);
574 }
575
576 int
577 rump_vfs_unmount(struct mount *mp, int mntflags)
578 {
579
580 return VFS_UNMOUNT(mp, mntflags);
581 }
582
583 int
584 rump_vfs_root(struct mount *mp, struct vnode **vpp, int lock)
585 {
586 int rv;
587
588 rv = VFS_ROOT(mp, vpp);
589 if (rv)
590 return rv;
591
592 if (!lock)
593 VOP_UNLOCK(*vpp, 0);
594
595 return 0;
596 }
597
598 int
599 rump_vfs_statvfs(struct mount *mp, struct statvfs *sbp)
600 {
601
602 return VFS_STATVFS(mp, sbp);
603 }
604
605 int
606 rump_vfs_sync(struct mount *mp, int wait, kauth_cred_t cred)
607 {
608
609 return VFS_SYNC(mp, wait ? MNT_WAIT : MNT_NOWAIT, cred);
610 }
611
612 int
613 rump_vfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
614 {
615
616 return VFS_FHTOVP(mp, fid, vpp);
617 }
618
619 int
620 rump_vfs_vptofh(struct vnode *vp, struct fid *fid, size_t *fidsize)
621 {
622
623 return VFS_VPTOFH(vp, fid, fidsize);
624 }
625
626 /*ARGSUSED*/
627 void
628 rump_vfs_syncwait(struct mount *mp)
629 {
630 int n;
631
632 n = buf_syncwait();
633 if (n)
634 printf("syncwait: unsynced buffers: %d\n", n);
635 }
636
637 int
638 rump_vfs_load(struct modinfo **mi)
639 {
640
641 if (!module_compatible((*mi)->mi_version, __NetBSD_Version__))
642 return EPROGMISMATCH;
643
644 return (*mi)->mi_modcmd(MODULE_CMD_INIT, NULL);
645 }
646
647 void
648 rump_bioops_sync()
649 {
650
651 if (bioopsp)
652 bioopsp->io_sync(NULL);
653 }
654
655 struct lwp *
656 rump_setup_curlwp(pid_t pid, lwpid_t lid, int set)
657 {
658 struct lwp *l;
659 struct proc *p;
660
661 l = kmem_zalloc(sizeof(struct lwp), KM_SLEEP);
662 if (pid != 0) {
663 p = kmem_zalloc(sizeof(struct proc), KM_SLEEP);
664 p->p_cwdi = cwdinit();
665
666 p->p_stats = &rump_stats;
667 p->p_limit = &rump_limits;
668 p->p_pid = pid;
669 p->p_vmspace = &rump_vmspace;
670 p->p_fd = fd_init(NULL);
671 } else {
672 p = &proc0;
673 }
674
675 l->l_cred = rump_cred;
676 l->l_proc = p;
677 l->l_lid = lid;
678 l->l_fd = p->p_fd;
679
680 if (set)
681 rumpuser_set_curlwp(l);
682
683 return l;
684 }
685
686 void
687 rump_clear_curlwp()
688 {
689 struct lwp *l;
690
691 l = rumpuser_get_curlwp();
692 if (l->l_proc->p_pid != 0) {
693 fd_free();
694 cwdfree(l->l_proc->p_cwdi);
695 kmem_free(l->l_proc, sizeof(*l->l_proc));
696 }
697 kmem_free(l, sizeof(*l));
698 rumpuser_set_curlwp(NULL);
699 }
700
701 struct lwp *
702 rump_get_curlwp()
703 {
704 struct lwp *l;
705
706 l = rumpuser_get_curlwp();
707 if (l == NULL)
708 l = &lwp0;
709
710 return l;
711 }
712
713 int
714 rump_splfoo()
715 {
716
717 if (rumpuser_whatis_ipl() != RUMPUSER_IPL_INTR) {
718 rumpuser_rw_enter(&rumpspl, 0);
719 rumpuser_set_ipl(RUMPUSER_IPL_SPLFOO);
720 }
721
722 return 0;
723 }
724
725 static void
726 rump_intr_enter(void)
727 {
728
729 rumpuser_set_ipl(RUMPUSER_IPL_INTR);
730 rumpuser_rw_enter(&rumpspl, 1);
731 }
732
733 static void
734 rump_intr_exit(void)
735 {
736
737 rumpuser_rw_exit(&rumpspl);
738 rumpuser_clear_ipl(RUMPUSER_IPL_INTR);
739 }
740
741 void
742 rump_splx(int dummy)
743 {
744
745 if (rumpuser_whatis_ipl() != RUMPUSER_IPL_INTR) {
746 rumpuser_clear_ipl(RUMPUSER_IPL_SPLFOO);
747 rumpuser_rw_exit(&rumpspl);
748 }
749 }
750
751 void
752 rump_biodone(void *arg, size_t count, int error)
753 {
754 struct buf *bp = arg;
755
756 bp->b_resid = bp->b_bcount - count;
757 KASSERT(bp->b_resid >= 0);
758 bp->b_error = error;
759
760 rump_intr_enter();
761 biodone(bp);
762 rump_intr_exit();
763 }
764
765 int _syspuffs_stub(int, int *);
766 int
767 _syspuffs_stub(int fd, int *newfd)
768 {
769
770 return ENODEV;
771 }
772
773 __weak_alias(syspuffs_glueinit,_syspuffs_stub);
774