rump.c revision 1.4 1 /* $NetBSD: rump.c,v 1.4 2007/08/08 14:09:07 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/filedesc.h>
32 #include <sys/kauth.h>
33 #include <sys/mount.h>
34 #include <sys/namei.h>
35 #include <sys/queue.h>
36 #include <sys/resourcevar.h>
37 #include <sys/vnode.h>
38
39 #include <machine/cpu.h>
40
41 #include <miscfs/specfs/specdev.h>
42
43 #include "rump.h"
44 #include "rumpuser.h"
45
46 struct proc rump_proc;
47 struct cwdinfo rump_cwdi;
48 struct pstats rump_stats;
49 struct plimit rump_limits;
50 kauth_cred_t rump_cred;
51 struct cpu_info rump_cpu;
52
53 struct fakeblk {
54 char path[MAXPATHLEN];
55 LIST_ENTRY(fakeblk) entries;
56 };
57
58 static LIST_HEAD(, fakeblk) fakeblks = LIST_HEAD_INITIALIZER(fakeblks);
59
60 void
61 rump_init()
62 {
63 extern char hostname[];
64 extern size_t hostnamelen;
65 int error;
66
67 curlwp = &lwp0;
68 rumpvm_init();
69
70 curlwp->l_proc = &rump_proc;
71 curlwp->l_cred = rump_cred;
72 rump_proc.p_stats = &rump_stats;
73 rump_proc.p_cwdi = &rump_cwdi;
74 rump_limits.pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
75 rump_proc.p_limit = &rump_limits;
76
77 vfsinit();
78
79 rumpuser_gethostname(hostname, MAXHOSTNAMELEN, &error);
80 hostnamelen = strlen(hostname);
81 }
82
83 void
84 rump_mountinit(struct mount **mpp, struct vfsops *vfsops)
85 {
86 struct mount *mp;
87
88 mp = rumpuser_malloc(sizeof(struct mount), 0);
89 memset(mp, 0, sizeof(struct mount));
90
91 mp->mnt_op = vfsops;
92 TAILQ_INIT(&mp->mnt_vnodelist);
93 *mpp = mp;
94 }
95
96 void
97 rump_mountdestroy(struct mount *mp)
98 {
99
100 rumpuser_free(mp);
101 }
102
103 struct componentname *
104 rump_makecn(u_long nameiop, u_long flags, const char *name, size_t namelen,
105 struct lwp *l)
106 {
107 struct componentname *cnp;
108
109 cnp = rumpuser_malloc(sizeof(struct componentname), 0);
110 memset(cnp, 0, sizeof(struct componentname));
111
112 cnp->cn_nameiop = nameiop;
113 cnp->cn_flags = flags;
114
115 cnp->cn_pnbuf = PNBUF_GET();
116 strcpy(cnp->cn_pnbuf, name);
117 cnp->cn_nameptr = cnp->cn_pnbuf;
118 cnp->cn_namelen = namelen;
119
120 cnp->cn_cred = l->l_cred;
121 cnp->cn_lwp = l;
122
123 return cnp;
124 }
125
126 void
127 rump_freecn(struct componentname *cnp, int islookup)
128 {
129
130 if (cnp->cn_flags & SAVENAME) {
131 if (islookup || cnp->cn_flags & SAVESTART)
132 PNBUF_PUT(cnp->cn_pnbuf);
133 } else {
134 PNBUF_PUT(cnp->cn_pnbuf);
135 }
136 rumpuser_free(cnp);
137 }
138
139 int
140 rump_recyclenode(struct vnode *vp)
141 {
142
143 return vrecycle(vp, NULL, curlwp);
144 }
145
146 static struct fakeblk *
147 _rump_fakeblk_find(const char *path)
148 {
149 char buf[MAXPATHLEN];
150 struct fakeblk *fblk;
151 int error;
152
153 if (rumpuser_realpath(path, buf, &error) == NULL)
154 return NULL;
155
156 LIST_FOREACH(fblk, &fakeblks, entries)
157 if (strcmp(fblk->path, buf) == 0)
158 return fblk;
159
160 return NULL;
161 }
162
163 int
164 rump_fakeblk_register(const char *path)
165 {
166 char buf[MAXPATHLEN];
167 struct fakeblk *fblk;
168 int error;
169
170 if (_rump_fakeblk_find(path))
171 return EEXIST;
172
173 if (rumpuser_realpath(path, buf, &error) == NULL)
174 return error;
175
176 fblk = rumpuser_malloc(sizeof(struct fakeblk), 1);
177 if (fblk == NULL)
178 return ENOMEM;
179
180 strlcpy(fblk->path, buf, MAXPATHLEN);
181 LIST_INSERT_HEAD(&fakeblks, fblk, entries);
182
183 return 0;
184 }
185
186 int
187 rump_fakeblk_find(const char *path)
188 {
189
190 return _rump_fakeblk_find(path) != NULL;
191 }
192
193 void
194 rump_fakeblk_deregister(const char *path)
195 {
196 struct fakeblk *fblk;
197
198 fblk = _rump_fakeblk_find(path);
199 if (fblk == NULL)
200 return;
201
202 LIST_REMOVE(fblk, entries);
203 rumpuser_free(fblk);
204 }
205
206 void
207 rump_getvninfo(struct vnode *vp, enum vtype *vtype, voff_t *vsize, dev_t *vdev)
208 {
209
210 *vtype = vp->v_type;
211 *vsize = vp->v_size;
212 if (vp->v_specinfo)
213 *vdev = vp->v_rdev;
214 else
215 *vdev = 0;
216 }
217