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