rump_vfs.c revision 1.12.2.1 1 /* $NetBSD: rump_vfs.c,v 1.12.2.1 2009/05/13 17:22:58 jym 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");
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 nameidata nd;
367 struct vnode *vp;
368 int rv;
369
370 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, path);
371 if ((rv = namei(&nd)) != 0)
372 return rv;
373 vp = nd.ni_vp;
374
375 *mpp = vp->v_mount;
376 vrele(vp);
377 return 0;
378 }
379
380 struct vattr*
381 rump_vattr_init(void)
382 {
383 struct vattr *vap;
384
385 vap = kmem_alloc(sizeof(struct vattr), KM_SLEEP);
386 vattr_null(vap);
387
388 return vap;
389 }
390
391 void
392 rump_vattr_settype(struct vattr *vap, enum vtype vt)
393 {
394
395 vap->va_type = vt;
396 }
397
398 void
399 rump_vattr_setmode(struct vattr *vap, mode_t mode)
400 {
401
402 vap->va_mode = mode;
403 }
404
405 void
406 rump_vattr_setrdev(struct vattr *vap, dev_t dev)
407 {
408
409 vap->va_rdev = dev;
410 }
411
412 void
413 rump_vattr_free(struct vattr *vap)
414 {
415
416 kmem_free(vap, sizeof(*vap));
417 }
418
419 void
420 rump_vp_incref(struct vnode *vp)
421 {
422
423 mutex_enter(&vp->v_interlock);
424 ++vp->v_usecount;
425 mutex_exit(&vp->v_interlock);
426 }
427
428 int
429 rump_vp_getref(struct vnode *vp)
430 {
431
432 return vp->v_usecount;
433 }
434
435 void
436 rump_vp_decref(struct vnode *vp)
437 {
438
439 mutex_enter(&vp->v_interlock);
440 --vp->v_usecount;
441 mutex_exit(&vp->v_interlock);
442 }
443
444 /*
445 * Really really recycle with a cherry on top. We should be
446 * extra-sure we can do this. For example with p2k there is
447 * no problem, since puffs in the kernel takes care of refcounting
448 * for us.
449 */
450 void
451 rump_vp_recycle_nokidding(struct vnode *vp)
452 {
453
454 mutex_enter(&vp->v_interlock);
455 vp->v_usecount = 1;
456 /*
457 * XXX: NFS holds a reference to the root vnode, so don't clean
458 * it out. This is very wrong, but fixing it properly would
459 * take too much effort for now
460 */
461 if (vp->v_tag == VT_NFS && vp->v_vflag & VV_ROOT) {
462 mutex_exit(&vp->v_interlock);
463 return;
464 }
465 vclean(vp, DOCLOSE);
466 vrelel(vp, 0);
467 }
468
469 void
470 rump_vp_rele(struct vnode *vp)
471 {
472
473 vrele(vp);
474 }
475
476 void
477 rump_vp_interlock(struct vnode *vp)
478 {
479
480 mutex_enter(&vp->v_interlock);
481 }
482
483 int
484 rump_vfs_unmount(struct mount *mp, int mntflags)
485 {
486 #if 0
487 struct evcnt *ev;
488
489 printf("event counters:\n");
490 TAILQ_FOREACH(ev, &allevents, ev_list)
491 printf("%s: %llu\n", ev->ev_name, ev->ev_count);
492 #endif
493
494 return VFS_UNMOUNT(mp, mntflags);
495 }
496
497 int
498 rump_vfs_root(struct mount *mp, struct vnode **vpp, int lock)
499 {
500 int rv;
501
502 rv = VFS_ROOT(mp, vpp);
503 if (rv)
504 return rv;
505
506 if (!lock)
507 VOP_UNLOCK(*vpp, 0);
508
509 return 0;
510 }
511
512 int
513 rump_vfs_statvfs(struct mount *mp, struct statvfs *sbp)
514 {
515
516 return VFS_STATVFS(mp, sbp);
517 }
518
519 int
520 rump_vfs_sync(struct mount *mp, int wait, kauth_cred_t cred)
521 {
522
523 return VFS_SYNC(mp, wait ? MNT_WAIT : MNT_NOWAIT, cred);
524 }
525
526 int
527 rump_vfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
528 {
529
530 return VFS_FHTOVP(mp, fid, vpp);
531 }
532
533 int
534 rump_vfs_vptofh(struct vnode *vp, struct fid *fid, size_t *fidsize)
535 {
536
537 return VFS_VPTOFH(vp, fid, fidsize);
538 }
539
540 /*ARGSUSED*/
541 void
542 rump_vfs_syncwait(struct mount *mp)
543 {
544 int n;
545
546 n = buf_syncwait();
547 if (n)
548 printf("syncwait: unsynced buffers: %d\n", n);
549 }
550
551 void
552 rump_biodone(void *arg, size_t count, int error)
553 {
554 struct buf *bp = arg;
555
556 bp->b_resid = bp->b_bcount - count;
557 KASSERT(bp->b_resid >= 0);
558 bp->b_error = error;
559
560 biodone(bp);
561 }
562
563 static void
564 rump_rcvp_lwpset(struct vnode *rvp, struct vnode *cvp, struct lwp *l)
565 {
566 struct cwdinfo *cwdi = l->l_proc->p_cwdi;
567
568 KASSERT(cvp);
569
570 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
571 if (cwdi->cwdi_rdir)
572 vrele(cwdi->cwdi_rdir);
573 if (rvp)
574 vref(rvp);
575 cwdi->cwdi_rdir = rvp;
576
577 vrele(cwdi->cwdi_cdir);
578 vref(cvp);
579 cwdi->cwdi_cdir = cvp;
580 rw_exit(&cwdi->cwdi_lock);
581 }
582
583 void
584 rump_rcvp_set(struct vnode *rvp, struct vnode *cvp)
585 {
586
587 rump_rcvp_lwpset(rvp, cvp, curlwp);
588 }
589
590 struct vnode *
591 rump_cdir_get(void)
592 {
593 struct vnode *vp;
594 struct cwdinfo *cwdi = curlwp->l_proc->p_cwdi;
595
596 rw_enter(&cwdi->cwdi_lock, RW_READER);
597 vp = cwdi->cwdi_cdir;
598 rw_exit(&cwdi->cwdi_lock);
599 vref(vp);
600
601 return vp;
602 }
603