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