rump_vfs.c revision 1.46 1 /* $NetBSD: rump_vfs.c,v 1.46 2010/04/21 16:51:24 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2008 Antti Kantee. All Rights Reserved.
5 *
6 * Development of this software was supported by the
7 * Finnish Cultural Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: rump_vfs.c,v 1.46 2010/04/21 16:51:24 pooka Exp $");
33
34 #include <sys/param.h>
35 #include <sys/buf.h>
36 #include <sys/conf.h>
37 #include <sys/evcnt.h>
38 #include <sys/filedesc.h>
39 #include <sys/fstrans.h>
40 #include <sys/lockf.h>
41 #include <sys/kthread.h>
42 #include <sys/module.h>
43 #include <sys/namei.h>
44 #include <sys/queue.h>
45 #include <sys/vfs_syscalls.h>
46 #include <sys/vnode.h>
47 #include <sys/wapbl.h>
48
49 #include <miscfs/specfs/specdev.h>
50 #include <miscfs/syncfs/syncfs.h>
51
52 #include <rump/rump.h>
53 #include <rump/rumpuser.h>
54
55 #include "rump_private.h"
56 #include "rump_vfs_private.h"
57
58 struct cwdinfo cwdi0;
59 const char *rootfstype = ROOT_FSTYPE_ANY;
60
61 static void rump_rcvp_lwpset(struct vnode *, struct vnode *, struct lwp *);
62
63 static void
64 pvfs_init(struct proc *p)
65 {
66
67 p->p_cwdi = cwdinit();
68 }
69
70 static void
71 pvfs_rele(struct proc *p)
72 {
73
74 cwdfree(p->p_cwdi);
75 }
76
77 void
78 rump_vfs_init(void)
79 {
80 extern struct vfsops rumpfs_vfsops;
81 char buf[64];
82 int error;
83 int rv, i;
84 extern int dovfsusermount; /* XXX */
85
86 dovfsusermount = 1; /* XXX */
87
88 if (rumpuser_getenv("RUMP_NVNODES", buf, sizeof(buf), &error) == 0) {
89 desiredvnodes = strtoul(buf, NULL, 10);
90 } else {
91 desiredvnodes = 1<<16;
92 }
93
94 rumpblk_init();
95
96 for (i = 0; i < ncpu; i++) {
97 struct cpu_info *ci = cpu_lookup(i);
98 cache_cpu_init(ci);
99 }
100 vfsinit();
101 bufinit();
102 cwd_sys_init();
103 lf_init();
104 spec_init();
105 fstrans_init();
106
107 if (rump_threads) {
108 if ((rv = kthread_create(PRI_BIO, KTHREAD_MPSAFE, NULL,
109 rumpuser_biothread, rump_biodone, NULL, "rmpabio")) != 0)
110 panic("syncer thread create failed: %d", rv);
111 }
112
113 root_device = &rump_rootdev;
114
115 /* bootstrap cwdi (rest done in vfs_mountroot() */
116 rw_init(&cwdi0.cwdi_lock);
117 proc0.p_cwdi = &cwdi0;
118 proc0.p_cwdi = cwdinit();
119
120 vfs_attach(&rumpfs_vfsops);
121 vfs_mountroot();
122
123 /* "mtree": create /dev */
124 do_sys_mkdir("/dev", 0777, UIO_SYSSPACE);
125 rump_devnull_init();
126
127 rump_proc_vfs_init = pvfs_init;
128 rump_proc_vfs_release = pvfs_rele;
129
130 if (rump_threads) {
131 if ((rv = kthread_create(PRI_IOFLUSH, KTHREAD_MPSAFE, NULL,
132 sched_sync, NULL, NULL, "ioflush")) != 0)
133 panic("syncer thread create failed: %d", rv);
134 } else {
135 syncdelay = 0;
136 }
137
138 module_init_class(MODULE_CLASS_VFS);
139 }
140
141 void
142 rump_vfs_fini(void)
143 {
144
145 vfs_shutdown();
146 }
147
148 struct componentname *
149 rump_makecn(u_long nameiop, u_long flags, const char *name, size_t namelen,
150 kauth_cred_t creds, struct lwp *l)
151 {
152 struct componentname *cnp;
153 const char *cp = NULL;
154
155 cnp = kmem_zalloc(sizeof(struct componentname), KM_SLEEP);
156
157 cnp->cn_nameiop = nameiop;
158 cnp->cn_flags = flags;
159
160 cnp->cn_pnbuf = PNBUF_GET();
161 strcpy(cnp->cn_pnbuf, name);
162 cnp->cn_nameptr = cnp->cn_pnbuf;
163 cnp->cn_namelen = namelen;
164 cnp->cn_hash = namei_hash(name, &cp);
165
166 cnp->cn_cred = creds;
167
168 return cnp;
169 }
170
171 void
172 rump_freecn(struct componentname *cnp, int flags)
173 {
174
175 if (flags & RUMPCN_FREECRED)
176 rump_cred_put(cnp->cn_cred);
177
178 if ((cnp->cn_flags & SAVENAME) == 0 || flags & RUMPCN_FORCEFREE)
179 PNBUF_PUT(cnp->cn_pnbuf);
180 kmem_free(cnp, sizeof(*cnp));
181 }
182
183 int
184 rump_checksavecn(struct componentname *cnp)
185 {
186
187 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
188 return 0;
189 } else {
190 cnp->cn_flags |= HASBUF;
191 return 1;
192 }
193 }
194
195 /* hey baby, what's your namei? */
196 int
197 rump_namei(uint32_t op, uint32_t flags, const char *namep,
198 struct vnode **dvpp, struct vnode **vpp, struct componentname **cnpp)
199 {
200 struct nameidata nd;
201 int rv;
202
203 NDINIT(&nd, op, flags, UIO_SYSSPACE, namep);
204 rv = namei(&nd);
205 if (rv)
206 return rv;
207
208 if (dvpp) {
209 KASSERT(flags & LOCKPARENT);
210 *dvpp = nd.ni_dvp;
211 } else {
212 KASSERT((flags & LOCKPARENT) == 0);
213 }
214
215 if (vpp) {
216 *vpp = nd.ni_vp;
217 } else {
218 if (nd.ni_vp) {
219 if (flags & LOCKLEAF)
220 vput(nd.ni_vp);
221 else
222 vrele(nd.ni_vp);
223 }
224 }
225
226 if (cnpp) {
227 struct componentname *cnp;
228
229 cnp = kmem_alloc(sizeof(*cnp), KM_SLEEP);
230 memcpy(cnp, &nd.ni_cnd, sizeof(*cnp));
231 *cnpp = cnp;
232 } else if (nd.ni_cnd.cn_flags & HASBUF) {
233 panic("%s: pathbuf mismatch", __func__);
234 }
235
236 return rv;
237 }
238
239 void
240 rump_getvninfo(struct vnode *vp, enum vtype *vtype,
241 voff_t *vsize, dev_t *vdev)
242 {
243
244 *vtype = vp->v_type;
245 *vsize = vp->v_size;
246 if (vp->v_specnode)
247 *vdev = vp->v_rdev;
248 else
249 *vdev = 0;
250 }
251
252 struct vfsops *
253 rump_vfslist_iterate(struct vfsops *ops)
254 {
255
256 if (ops == NULL)
257 return LIST_FIRST(&vfs_list);
258 else
259 return LIST_NEXT(ops, vfs_list);
260 }
261
262 struct vfsops *
263 rump_vfs_getopsbyname(const char *name)
264 {
265
266 return vfs_getopsbyname(name);
267 }
268
269 int
270 rump_vfs_getmp(const char *path, struct mount **mpp)
271 {
272 struct vnode *vp;
273 int rv;
274
275 if ((rv = namei_simple_user(path, NSM_FOLLOW_TRYEMULROOT, &vp)) != 0)
276 return rv;
277
278 *mpp = vp->v_mount;
279 vrele(vp);
280 return 0;
281 }
282
283 struct vattr*
284 rump_vattr_init(void)
285 {
286 struct vattr *vap;
287
288 vap = kmem_alloc(sizeof(struct vattr), KM_SLEEP);
289 vattr_null(vap);
290
291 return vap;
292 }
293
294 void
295 rump_vattr_settype(struct vattr *vap, enum vtype vt)
296 {
297
298 vap->va_type = vt;
299 }
300
301 void
302 rump_vattr_setmode(struct vattr *vap, mode_t mode)
303 {
304
305 vap->va_mode = mode;
306 }
307
308 void
309 rump_vattr_setrdev(struct vattr *vap, dev_t dev)
310 {
311
312 vap->va_rdev = dev;
313 }
314
315 void
316 rump_vattr_free(struct vattr *vap)
317 {
318
319 kmem_free(vap, sizeof(*vap));
320 }
321
322 void
323 rump_vp_incref(struct vnode *vp)
324 {
325
326 vref(vp);
327 }
328
329 int
330 rump_vp_getref(struct vnode *vp)
331 {
332
333 return vp->v_usecount;
334 }
335
336 void
337 rump_vp_rele(struct vnode *vp)
338 {
339
340 vrele(vp);
341 }
342
343 void
344 rump_vp_interlock(struct vnode *vp)
345 {
346
347 mutex_enter(&vp->v_interlock);
348 }
349
350 int
351 rump_vfs_unmount(struct mount *mp, int mntflags)
352 {
353 #if 0
354 struct evcnt *ev;
355
356 printf("event counters:\n");
357 TAILQ_FOREACH(ev, &allevents, ev_list)
358 printf("%s: %llu\n", ev->ev_name, ev->ev_count);
359 #endif
360
361 return VFS_UNMOUNT(mp, mntflags);
362 }
363
364 int
365 rump_vfs_root(struct mount *mp, struct vnode **vpp, int lock)
366 {
367 int rv;
368
369 rv = VFS_ROOT(mp, vpp);
370 if (rv)
371 return rv;
372
373 if (!lock)
374 VOP_UNLOCK(*vpp, 0);
375
376 return 0;
377 }
378
379 int
380 rump_vfs_statvfs(struct mount *mp, struct statvfs *sbp)
381 {
382
383 return VFS_STATVFS(mp, sbp);
384 }
385
386 int
387 rump_vfs_sync(struct mount *mp, int wait, kauth_cred_t cred)
388 {
389
390 return VFS_SYNC(mp, wait ? MNT_WAIT : MNT_NOWAIT, cred);
391 }
392
393 int
394 rump_vfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
395 {
396
397 return VFS_FHTOVP(mp, fid, vpp);
398 }
399
400 int
401 rump_vfs_vptofh(struct vnode *vp, struct fid *fid, size_t *fidsize)
402 {
403
404 return VFS_VPTOFH(vp, fid, fidsize);
405 }
406
407 /*ARGSUSED*/
408 void
409 rump_vfs_syncwait(struct mount *mp)
410 {
411 int n;
412
413 n = buf_syncwait();
414 if (n)
415 printf("syncwait: unsynced buffers: %d\n", n);
416 }
417
418 void
419 rump_biodone(void *arg, size_t count, int error)
420 {
421 struct buf *bp = arg;
422
423 bp->b_resid = bp->b_bcount - count;
424 KASSERT(bp->b_resid >= 0);
425 bp->b_error = error;
426
427 biodone(bp);
428 }
429
430 static void
431 rump_rcvp_lwpset(struct vnode *rvp, struct vnode *cvp, struct lwp *l)
432 {
433 struct cwdinfo *cwdi = l->l_proc->p_cwdi;
434
435 KASSERT(cvp);
436
437 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
438 if (cwdi->cwdi_rdir)
439 vrele(cwdi->cwdi_rdir);
440 if (rvp)
441 vref(rvp);
442 cwdi->cwdi_rdir = rvp;
443
444 vrele(cwdi->cwdi_cdir);
445 vref(cvp);
446 cwdi->cwdi_cdir = cvp;
447 rw_exit(&cwdi->cwdi_lock);
448 }
449
450 void
451 rump_rcvp_set(struct vnode *rvp, struct vnode *cvp)
452 {
453
454 rump_rcvp_lwpset(rvp, cvp, curlwp);
455 }
456
457 struct vnode *
458 rump_cdir_get(void)
459 {
460 struct vnode *vp;
461 struct cwdinfo *cwdi = curlwp->l_proc->p_cwdi;
462
463 rw_enter(&cwdi->cwdi_lock, RW_READER);
464 vp = cwdi->cwdi_cdir;
465 rw_exit(&cwdi->cwdi_lock);
466 vref(vp);
467
468 return vp;
469 }
470