rump_vfs.c revision 1.20.2.6 1 /* $NetBSD: rump_vfs.c,v 1.20.2.6 2009/08/19 18:48:30 yamt 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.20.2.6 2009/08/19 18:48:30 yamt 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/lockf.h>
40 #include <sys/kthread.h>
41 #include <sys/module.h>
42 #include <sys/namei.h>
43 #include <sys/queue.h>
44 #include <sys/vfs_syscalls.h>
45 #include <sys/vnode.h>
46 #include <sys/wapbl.h>
47
48 #include <miscfs/specfs/specdev.h>
49 #include <miscfs/syncfs/syncfs.h>
50
51 #include <rump/rump.h>
52 #include <rump/rumpuser.h>
53
54 #include "rump_private.h"
55 #include "rump_vfs_private.h"
56
57 static struct cwdinfo rump_cwdi;
58
59 static void rump_rcvp_lwpset(struct vnode *, struct vnode *, struct lwp *);
60
61 static void
62 pvfs_init(struct proc *p)
63 {
64
65 p->p_cwdi = cwdinit();
66 }
67
68 static void
69 pvfs_rele(struct proc *p)
70 {
71
72 cwdfree(p->p_cwdi);
73 }
74
75 void
76 rump_vfs_init(void)
77 {
78 char buf[64];
79 int error;
80
81 dovfsusermount = 1;
82
83 if (rumpuser_getenv("RUMP_NVNODES", buf, sizeof(buf), &error) == 0) {
84 desiredvnodes = strtoul(buf, NULL, 10);
85 } else {
86 desiredvnodes = 1<<16;
87 }
88
89 rumpblk_init();
90
91 cache_cpu_init(&rump_cpu);
92 vfsinit();
93 bufinit();
94 wapbl_init();
95 cwd_sys_init();
96 lf_init();
97
98 rumpuser_bioinit(rump_biodone);
99 rumpfs_init();
100
101 rump_proc_vfs_init = pvfs_init;
102 rump_proc_vfs_release = pvfs_rele;
103
104 /* bootstrap cwdi */
105 rw_init(&rump_cwdi.cwdi_lock);
106 rump_cwdi.cwdi_cdir = rootvnode;
107 vref(rump_cwdi.cwdi_cdir);
108 proc0.p_cwdi = &rump_cwdi;
109 proc0.p_cwdi = cwdinit();
110
111 if (rump_threads) {
112 int rv;
113
114 if ((rv = kthread_create(PRI_IOFLUSH, KTHREAD_MPSAFE, NULL,
115 sched_sync, NULL, NULL, "ioflush")) != 0)
116 panic("syncer thread create failed: %d", rv);
117 } else {
118 syncdelay = 0;
119 }
120 }
121
122 struct mount *
123 rump_mnt_init(struct vfsops *vfsops, int mntflags)
124 {
125 struct mount *mp;
126
127 mp = kmem_zalloc(sizeof(struct mount), KM_SLEEP);
128
129 mp->mnt_op = vfsops;
130 mp->mnt_flag = mntflags;
131 TAILQ_INIT(&mp->mnt_vnodelist);
132 rw_init(&mp->mnt_unmounting);
133 mutex_init(&mp->mnt_updating, MUTEX_DEFAULT, IPL_NONE);
134 mutex_init(&mp->mnt_renamelock, MUTEX_DEFAULT, IPL_NONE);
135 mp->mnt_refcnt = 1;
136 mp->mnt_vnodecovered = rootvnode;
137
138 mount_initspecific(mp);
139
140 return mp;
141 }
142
143 int
144 rump_mnt_mount(struct mount *mp, const char *path, void *data, size_t *dlen)
145 {
146 struct vnode *rvp;
147 int rv;
148
149 rv = VFS_MOUNT(mp, path, data, dlen);
150 if (rv)
151 return rv;
152
153 (void) VFS_STATVFS(mp, &mp->mnt_stat);
154 rv = VFS_START(mp, 0);
155 if (rv) {
156 VFS_UNMOUNT(mp, MNT_FORCE);
157 return rv;
158 }
159
160 /*
161 * XXX: set a root for lwp0. This is strictly not correct,
162 * but makes things work for single fs case without having
163 * to manually call rump_rcvp_set().
164 */
165 VFS_ROOT(mp, &rvp);
166 rump_rcvp_lwpset(rvp, rvp, &lwp0);
167 vput(rvp);
168
169 return rv;
170 }
171
172 void
173 rump_mnt_destroy(struct mount *mp)
174 {
175
176 /* See rcvp XXX above */
177 rump_cwdi.cwdi_rdir = NULL;
178 vref(rootvnode);
179 rump_cwdi.cwdi_cdir = rootvnode;
180
181 mount_finispecific(mp);
182 kmem_free(mp, sizeof(*mp));
183 }
184
185 struct componentname *
186 rump_makecn(u_long nameiop, u_long flags, const char *name, size_t namelen,
187 kauth_cred_t creds, struct lwp *l)
188 {
189 struct componentname *cnp;
190 const char *cp = NULL;
191
192 cnp = kmem_zalloc(sizeof(struct componentname), KM_SLEEP);
193
194 cnp->cn_nameiop = nameiop;
195 cnp->cn_flags = flags | HASBUF;
196
197 cnp->cn_pnbuf = PNBUF_GET();
198 strcpy(cnp->cn_pnbuf, name);
199 cnp->cn_nameptr = cnp->cn_pnbuf;
200 cnp->cn_namelen = namelen;
201 cnp->cn_hash = namei_hash(name, &cp);
202
203 cnp->cn_cred = creds;
204
205 return cnp;
206 }
207
208 void
209 rump_freecn(struct componentname *cnp, int flags)
210 {
211
212 if (flags & RUMPCN_FREECRED)
213 rump_cred_put(cnp->cn_cred);
214
215 if (cnp->cn_flags & SAVENAME) {
216 if (flags & RUMPCN_ISLOOKUP || cnp->cn_flags & SAVESTART)
217 PNBUF_PUT(cnp->cn_pnbuf);
218 } else {
219 PNBUF_PUT(cnp->cn_pnbuf);
220 }
221 kmem_free(cnp, sizeof(*cnp));
222 }
223
224 /* hey baby, what's your namei? */
225 int
226 rump_namei(uint32_t op, uint32_t flags, const char *namep,
227 struct vnode **dvpp, struct vnode **vpp, struct componentname **cnpp)
228 {
229 struct nameidata nd;
230 int rv;
231
232 NDINIT(&nd, op, flags, UIO_SYSSPACE, namep);
233 rv = namei(&nd);
234 if (rv)
235 return rv;
236
237 if (dvpp) {
238 KASSERT(flags & LOCKPARENT);
239 *dvpp = nd.ni_dvp;
240 } else {
241 KASSERT((flags & LOCKPARENT) == 0);
242 }
243
244 if (vpp) {
245 *vpp = nd.ni_vp;
246 } else {
247 if (nd.ni_vp) {
248 if (flags & LOCKLEAF)
249 vput(nd.ni_vp);
250 else
251 vrele(nd.ni_vp);
252 }
253 }
254
255 if (cnpp) {
256 struct componentname *cnp;
257
258 cnp = kmem_alloc(sizeof(*cnp), KM_SLEEP);
259 memcpy(cnp, &nd.ni_cnd, sizeof(*cnp));
260 *cnpp = cnp;
261 } else if (nd.ni_cnd.cn_flags & HASBUF) {
262 panic("%s: pathbuf mismatch", __func__);
263 }
264
265 return rv;
266 }
267
268 void
269 rump_getvninfo(struct vnode *vp, enum vtype *vtype, voff_t *vsize, dev_t *vdev)
270 {
271
272 *vtype = vp->v_type;
273 *vsize = vp->v_size;
274 if (vp->v_specnode)
275 *vdev = vp->v_rdev;
276 else
277 *vdev = 0;
278 }
279
280 struct vfsops *
281 rump_vfslist_iterate(struct vfsops *ops)
282 {
283
284 if (ops == NULL)
285 return LIST_FIRST(&vfs_list);
286 else
287 return LIST_NEXT(ops, vfs_list);
288 }
289
290 struct vfsops *
291 rump_vfs_getopsbyname(const char *name)
292 {
293
294 return vfs_getopsbyname(name);
295 }
296
297 int
298 rump_vfs_getmp(const char *path, struct mount **mpp)
299 {
300 struct vnode *vp;
301 int rv;
302
303 if ((rv = namei_simple_user(path, NSM_FOLLOW_TRYEMULROOT, &vp)) != 0)
304 return rv;
305
306 *mpp = vp->v_mount;
307 vrele(vp);
308 return 0;
309 }
310
311 struct vattr*
312 rump_vattr_init(void)
313 {
314 struct vattr *vap;
315
316 vap = kmem_alloc(sizeof(struct vattr), KM_SLEEP);
317 vattr_null(vap);
318
319 return vap;
320 }
321
322 void
323 rump_vattr_settype(struct vattr *vap, enum vtype vt)
324 {
325
326 vap->va_type = vt;
327 }
328
329 void
330 rump_vattr_setmode(struct vattr *vap, mode_t mode)
331 {
332
333 vap->va_mode = mode;
334 }
335
336 void
337 rump_vattr_setrdev(struct vattr *vap, dev_t dev)
338 {
339
340 vap->va_rdev = dev;
341 }
342
343 void
344 rump_vattr_free(struct vattr *vap)
345 {
346
347 kmem_free(vap, sizeof(*vap));
348 }
349
350 void
351 rump_vp_incref(struct vnode *vp)
352 {
353
354 mutex_enter(&vp->v_interlock);
355 ++vp->v_usecount;
356 mutex_exit(&vp->v_interlock);
357 }
358
359 int
360 rump_vp_getref(struct vnode *vp)
361 {
362
363 return vp->v_usecount;
364 }
365
366 void
367 rump_vp_decref(struct vnode *vp)
368 {
369
370 mutex_enter(&vp->v_interlock);
371 --vp->v_usecount;
372 mutex_exit(&vp->v_interlock);
373 }
374
375 /*
376 * Really really recycle with a cherry on top. We should be
377 * extra-sure we can do this. For example with p2k there is
378 * no problem, since puffs in the kernel takes care of refcounting
379 * for us.
380 */
381 void
382 rump_vp_recycle_nokidding(struct vnode *vp)
383 {
384
385 mutex_enter(&vp->v_interlock);
386 vp->v_usecount = 1;
387 /*
388 * XXX: NFS holds a reference to the root vnode, so don't clean
389 * it out. This is very wrong, but fixing it properly would
390 * take too much effort for now
391 */
392 if (vp->v_tag == VT_NFS && vp->v_vflag & VV_ROOT) {
393 mutex_exit(&vp->v_interlock);
394 return;
395 }
396 vclean(vp, DOCLOSE);
397 vrelel(vp, 0);
398 }
399
400 void
401 rump_vp_rele(struct vnode *vp)
402 {
403
404 vrele(vp);
405 }
406
407 void
408 rump_vp_interlock(struct vnode *vp)
409 {
410
411 mutex_enter(&vp->v_interlock);
412 }
413
414 int
415 rump_vfs_unmount(struct mount *mp, int mntflags)
416 {
417 #if 0
418 struct evcnt *ev;
419
420 printf("event counters:\n");
421 TAILQ_FOREACH(ev, &allevents, ev_list)
422 printf("%s: %llu\n", ev->ev_name, ev->ev_count);
423 #endif
424
425 return VFS_UNMOUNT(mp, mntflags);
426 }
427
428 int
429 rump_vfs_root(struct mount *mp, struct vnode **vpp, int lock)
430 {
431 int rv;
432
433 rv = VFS_ROOT(mp, vpp);
434 if (rv)
435 return rv;
436
437 if (!lock)
438 VOP_UNLOCK(*vpp, 0);
439
440 return 0;
441 }
442
443 int
444 rump_vfs_statvfs(struct mount *mp, struct statvfs *sbp)
445 {
446
447 return VFS_STATVFS(mp, sbp);
448 }
449
450 int
451 rump_vfs_sync(struct mount *mp, int wait, kauth_cred_t cred)
452 {
453
454 return VFS_SYNC(mp, wait ? MNT_WAIT : MNT_NOWAIT, cred);
455 }
456
457 int
458 rump_vfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
459 {
460
461 return VFS_FHTOVP(mp, fid, vpp);
462 }
463
464 int
465 rump_vfs_vptofh(struct vnode *vp, struct fid *fid, size_t *fidsize)
466 {
467
468 return VFS_VPTOFH(vp, fid, fidsize);
469 }
470
471 /*ARGSUSED*/
472 void
473 rump_vfs_syncwait(struct mount *mp)
474 {
475 int n;
476
477 n = buf_syncwait();
478 if (n)
479 printf("syncwait: unsynced buffers: %d\n", n);
480 }
481
482 void
483 rump_biodone(void *arg, size_t count, int error)
484 {
485 struct buf *bp = arg;
486
487 bp->b_resid = bp->b_bcount - count;
488 KASSERT(bp->b_resid >= 0);
489 bp->b_error = error;
490
491 biodone(bp);
492 }
493
494 static void
495 rump_rcvp_lwpset(struct vnode *rvp, struct vnode *cvp, struct lwp *l)
496 {
497 struct cwdinfo *cwdi = l->l_proc->p_cwdi;
498
499 KASSERT(cvp);
500
501 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
502 if (cwdi->cwdi_rdir)
503 vrele(cwdi->cwdi_rdir);
504 if (rvp)
505 vref(rvp);
506 cwdi->cwdi_rdir = rvp;
507
508 vrele(cwdi->cwdi_cdir);
509 vref(cvp);
510 cwdi->cwdi_cdir = cvp;
511 rw_exit(&cwdi->cwdi_lock);
512 }
513
514 void
515 rump_rcvp_set(struct vnode *rvp, struct vnode *cvp)
516 {
517
518 rump_rcvp_lwpset(rvp, cvp, curlwp);
519 }
520
521 struct vnode *
522 rump_cdir_get(void)
523 {
524 struct vnode *vp;
525 struct cwdinfo *cwdi = curlwp->l_proc->p_cwdi;
526
527 rw_enter(&cwdi->cwdi_lock, RW_READER);
528 vp = cwdi->cwdi_cdir;
529 rw_exit(&cwdi->cwdi_lock);
530 vref(vp);
531
532 return vp;
533 }
534