rump.c revision 1.6 1 1.6 pooka /* $NetBSD: rump.c,v 1.6 2007/08/14 15:56:17 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.1 pooka * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Development of this software was supported by Google Summer of Code.
7 1.1 pooka *
8 1.1 pooka * Redistribution and use in source and binary forms, with or without
9 1.1 pooka * modification, are permitted provided that the following conditions
10 1.1 pooka * are met:
11 1.1 pooka * 1. Redistributions of source code must retain the above copyright
12 1.1 pooka * notice, this list of conditions and the following disclaimer.
13 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 pooka * notice, this list of conditions and the following disclaimer in the
15 1.1 pooka * documentation and/or other materials provided with the distribution.
16 1.1 pooka *
17 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 pooka * SUCH DAMAGE.
28 1.1 pooka */
29 1.1 pooka
30 1.1 pooka #include <sys/param.h>
31 1.1 pooka #include <sys/filedesc.h>
32 1.1 pooka #include <sys/kauth.h>
33 1.1 pooka #include <sys/mount.h>
34 1.1 pooka #include <sys/namei.h>
35 1.1 pooka #include <sys/queue.h>
36 1.1 pooka #include <sys/resourcevar.h>
37 1.1 pooka #include <sys/vnode.h>
38 1.1 pooka
39 1.1 pooka #include <machine/cpu.h>
40 1.1 pooka
41 1.4 pooka #include <miscfs/specfs/specdev.h>
42 1.4 pooka
43 1.1 pooka #include "rump.h"
44 1.1 pooka #include "rumpuser.h"
45 1.1 pooka
46 1.1 pooka struct proc rump_proc;
47 1.1 pooka struct cwdinfo rump_cwdi;
48 1.1 pooka struct pstats rump_stats;
49 1.1 pooka struct plimit rump_limits;
50 1.1 pooka kauth_cred_t rump_cred;
51 1.1 pooka struct cpu_info rump_cpu;
52 1.1 pooka
53 1.2 pooka struct fakeblk {
54 1.2 pooka char path[MAXPATHLEN];
55 1.2 pooka LIST_ENTRY(fakeblk) entries;
56 1.2 pooka };
57 1.2 pooka
58 1.2 pooka static LIST_HEAD(, fakeblk) fakeblks = LIST_HEAD_INITIALIZER(fakeblks);
59 1.2 pooka
60 1.1 pooka void
61 1.1 pooka rump_init()
62 1.1 pooka {
63 1.1 pooka extern char hostname[];
64 1.1 pooka extern size_t hostnamelen;
65 1.1 pooka int error;
66 1.1 pooka
67 1.1 pooka curlwp = &lwp0;
68 1.1 pooka rumpvm_init();
69 1.1 pooka
70 1.1 pooka curlwp->l_proc = &rump_proc;
71 1.1 pooka curlwp->l_cred = rump_cred;
72 1.1 pooka rump_proc.p_stats = &rump_stats;
73 1.1 pooka rump_proc.p_cwdi = &rump_cwdi;
74 1.1 pooka rump_limits.pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
75 1.1 pooka rump_proc.p_limit = &rump_limits;
76 1.1 pooka
77 1.1 pooka vfsinit();
78 1.5 pooka bufinit();
79 1.1 pooka
80 1.1 pooka rumpuser_gethostname(hostname, MAXHOSTNAMELEN, &error);
81 1.1 pooka hostnamelen = strlen(hostname);
82 1.1 pooka }
83 1.1 pooka
84 1.1 pooka void
85 1.1 pooka rump_mountinit(struct mount **mpp, struct vfsops *vfsops)
86 1.1 pooka {
87 1.1 pooka struct mount *mp;
88 1.1 pooka
89 1.1 pooka mp = rumpuser_malloc(sizeof(struct mount), 0);
90 1.1 pooka memset(mp, 0, sizeof(struct mount));
91 1.1 pooka
92 1.1 pooka mp->mnt_op = vfsops;
93 1.1 pooka TAILQ_INIT(&mp->mnt_vnodelist);
94 1.1 pooka *mpp = mp;
95 1.1 pooka }
96 1.1 pooka
97 1.1 pooka void
98 1.1 pooka rump_mountdestroy(struct mount *mp)
99 1.1 pooka {
100 1.1 pooka
101 1.1 pooka rumpuser_free(mp);
102 1.1 pooka }
103 1.1 pooka
104 1.1 pooka struct componentname *
105 1.1 pooka rump_makecn(u_long nameiop, u_long flags, const char *name, size_t namelen,
106 1.1 pooka struct lwp *l)
107 1.1 pooka {
108 1.1 pooka struct componentname *cnp;
109 1.1 pooka
110 1.1 pooka cnp = rumpuser_malloc(sizeof(struct componentname), 0);
111 1.1 pooka memset(cnp, 0, sizeof(struct componentname));
112 1.1 pooka
113 1.1 pooka cnp->cn_nameiop = nameiop;
114 1.1 pooka cnp->cn_flags = flags;
115 1.1 pooka
116 1.1 pooka cnp->cn_pnbuf = PNBUF_GET();
117 1.1 pooka strcpy(cnp->cn_pnbuf, name);
118 1.1 pooka cnp->cn_nameptr = cnp->cn_pnbuf;
119 1.1 pooka cnp->cn_namelen = namelen;
120 1.1 pooka
121 1.1 pooka cnp->cn_cred = l->l_cred;
122 1.1 pooka cnp->cn_lwp = l;
123 1.1 pooka
124 1.1 pooka return cnp;
125 1.1 pooka }
126 1.1 pooka
127 1.1 pooka void
128 1.1 pooka rump_freecn(struct componentname *cnp, int islookup)
129 1.1 pooka {
130 1.1 pooka
131 1.1 pooka if (cnp->cn_flags & SAVENAME) {
132 1.1 pooka if (islookup || cnp->cn_flags & SAVESTART)
133 1.1 pooka PNBUF_PUT(cnp->cn_pnbuf);
134 1.1 pooka } else {
135 1.1 pooka PNBUF_PUT(cnp->cn_pnbuf);
136 1.1 pooka }
137 1.1 pooka rumpuser_free(cnp);
138 1.1 pooka }
139 1.1 pooka
140 1.1 pooka int
141 1.1 pooka rump_recyclenode(struct vnode *vp)
142 1.1 pooka {
143 1.1 pooka
144 1.1 pooka return vrecycle(vp, NULL, curlwp);
145 1.1 pooka }
146 1.2 pooka
147 1.2 pooka static struct fakeblk *
148 1.2 pooka _rump_fakeblk_find(const char *path)
149 1.2 pooka {
150 1.2 pooka char buf[MAXPATHLEN];
151 1.2 pooka struct fakeblk *fblk;
152 1.3 pooka int error;
153 1.2 pooka
154 1.3 pooka if (rumpuser_realpath(path, buf, &error) == NULL)
155 1.3 pooka return NULL;
156 1.2 pooka
157 1.2 pooka LIST_FOREACH(fblk, &fakeblks, entries)
158 1.2 pooka if (strcmp(fblk->path, buf) == 0)
159 1.2 pooka return fblk;
160 1.2 pooka
161 1.2 pooka return NULL;
162 1.2 pooka }
163 1.2 pooka
164 1.2 pooka int
165 1.2 pooka rump_fakeblk_register(const char *path)
166 1.2 pooka {
167 1.2 pooka char buf[MAXPATHLEN];
168 1.2 pooka struct fakeblk *fblk;
169 1.3 pooka int error;
170 1.2 pooka
171 1.2 pooka if (_rump_fakeblk_find(path))
172 1.2 pooka return EEXIST;
173 1.2 pooka
174 1.3 pooka if (rumpuser_realpath(path, buf, &error) == NULL)
175 1.3 pooka return error;
176 1.3 pooka
177 1.2 pooka fblk = rumpuser_malloc(sizeof(struct fakeblk), 1);
178 1.2 pooka if (fblk == NULL)
179 1.2 pooka return ENOMEM;
180 1.2 pooka
181 1.3 pooka strlcpy(fblk->path, buf, MAXPATHLEN);
182 1.2 pooka LIST_INSERT_HEAD(&fakeblks, fblk, entries);
183 1.2 pooka
184 1.2 pooka return 0;
185 1.2 pooka }
186 1.2 pooka
187 1.2 pooka int
188 1.2 pooka rump_fakeblk_find(const char *path)
189 1.2 pooka {
190 1.2 pooka
191 1.2 pooka return _rump_fakeblk_find(path) != NULL;
192 1.2 pooka }
193 1.2 pooka
194 1.2 pooka void
195 1.2 pooka rump_fakeblk_deregister(const char *path)
196 1.2 pooka {
197 1.2 pooka struct fakeblk *fblk;
198 1.2 pooka
199 1.2 pooka fblk = _rump_fakeblk_find(path);
200 1.2 pooka if (fblk == NULL)
201 1.3 pooka return;
202 1.2 pooka
203 1.2 pooka LIST_REMOVE(fblk, entries);
204 1.2 pooka rumpuser_free(fblk);
205 1.2 pooka }
206 1.4 pooka
207 1.4 pooka void
208 1.4 pooka rump_getvninfo(struct vnode *vp, enum vtype *vtype, voff_t *vsize, dev_t *vdev)
209 1.4 pooka {
210 1.4 pooka
211 1.4 pooka *vtype = vp->v_type;
212 1.4 pooka *vsize = vp->v_size;
213 1.4 pooka if (vp->v_specinfo)
214 1.4 pooka *vdev = vp->v_rdev;
215 1.4 pooka else
216 1.4 pooka *vdev = 0;
217 1.4 pooka }
218 1.6 pooka
219 1.6 pooka struct vfsops *
220 1.6 pooka rump_vfslist_iterate(struct vfsops *ops)
221 1.6 pooka {
222 1.6 pooka
223 1.6 pooka if (ops == NULL)
224 1.6 pooka return LIST_FIRST(&vfs_list);
225 1.6 pooka else
226 1.6 pooka return LIST_NEXT(ops, vfs_list);
227 1.6 pooka }
228 1.6 pooka
229 1.6 pooka struct vfsops *
230 1.6 pooka rump_vfs_getopsbyname(const char *name)
231 1.6 pooka {
232 1.6 pooka
233 1.6 pooka return vfs_getopsbyname(name);
234 1.6 pooka }
235