rump_vfs.c revision 1.15 1 /* $NetBSD: rump_vfs.c,v 1.15 2009/04/18 16:33:37 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");
33
34 #include <sys/param.h>
35 #include <sys/buf.h>
36 #include <sys/conf.h>
37 #include <sys/filedesc.h>
38 #include <sys/lockf.h>
39 #include <sys/kthread.h>
40 #include <sys/module.h>
41 #include <sys/namei.h>
42 #include <sys/queue.h>
43 #include <sys/vfs_syscalls.h>
44 #include <sys/vnode.h>
45 #include <sys/wapbl.h>
46
47 #include <miscfs/specfs/specdev.h>
48 #include <miscfs/syncfs/syncfs.h>
49
50 #include <rump/rump.h>
51 #include <rump/rumpuser.h>
52
53 #include "rump_private.h"
54 #include "rump_vfs_private.h"
55
56 struct fakeblk {
57 char path[MAXPATHLEN];
58 LIST_ENTRY(fakeblk) entries;
59 };
60 static LIST_HEAD(, fakeblk) fakeblks = LIST_HEAD_INITIALIZER(fakeblks);
61
62 static struct cwdinfo rump_cwdi;
63
64 static void rump_rcvp_lwpset(struct vnode *, struct vnode *, struct lwp *);
65
66 static void
67 pvfs_init(struct proc *p)
68 {
69
70 p->p_cwdi = cwdinit();
71 }
72
73 static void
74 pvfs_rele(struct proc *p)
75 {
76
77 cwdfree(p->p_cwdi);
78 }
79
80 void
81 rump_vfs_init(void)
82 {
83 char buf[64];
84 int error;
85
86 syncdelay = 0;
87 dovfsusermount = 1;
88
89 rumpblk_init();
90
91 if (rumpuser_getenv("RUMP_NVNODES", buf, sizeof(buf), &error) == 0) {
92 desiredvnodes = strtoul(buf, NULL, 10);
93 } else {
94 desiredvnodes = 1<<16;
95 }
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 rw_init(&rump_cwdi.cwdi_lock);
111 rump_cwdi.cwdi_cdir = rootvnode;
112 vref(rump_cwdi.cwdi_cdir);
113 proc0.p_cwdi = &rump_cwdi;
114
115 if (rump_threads) {
116 int rv;
117
118 if ((rv = kthread_create(PRI_IOFLUSH, KTHREAD_MPSAFE, NULL,
119 sched_sync, NULL, NULL, "ioflush")) != 0)
120 panic("syncer thread create failed: %d", rv);
121 }
122 }
123
124 struct mount *
125 rump_mnt_init(struct vfsops *vfsops, int mntflags)
126 {
127 struct mount *mp;
128
129 mp = kmem_zalloc(sizeof(struct mount), KM_SLEEP);
130
131 mp->mnt_op = vfsops;
132 mp->mnt_flag = mntflags;
133 TAILQ_INIT(&mp->mnt_vnodelist);
134 rw_init(&mp->mnt_unmounting);
135 mutex_init(&mp->mnt_updating, MUTEX_DEFAULT, IPL_NONE);
136 mutex_init(&mp->mnt_renamelock, MUTEX_DEFAULT, IPL_NONE);
137 mp->mnt_refcnt = 1;
138 mp->mnt_vnodecovered = rootvnode;
139
140 mount_initspecific(mp);
141
142 return mp;
143 }
144
145 int
146 rump_mnt_mount(struct mount *mp, const char *path, void *data, size_t *dlen)
147 {
148 struct vnode *rvp;
149 int rv;
150
151 rv = VFS_MOUNT(mp, path, data, dlen);
152 if (rv)
153 return rv;
154
155 (void) VFS_STATVFS(mp, &mp->mnt_stat);
156 rv = VFS_START(mp, 0);
157 if (rv)
158 VFS_UNMOUNT(mp, MNT_FORCE);
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_destroy(cnp->cn_cred);
214
215 if ((flags & RUMPCN_HASNTBUF) == 0) {
216 if (cnp->cn_flags & SAVENAME) {
217 if (flags & RUMPCN_ISLOOKUP ||cnp->cn_flags & SAVESTART)
218 PNBUF_PUT(cnp->cn_pnbuf);
219 } else {
220 PNBUF_PUT(cnp->cn_pnbuf);
221 }
222 }
223 kmem_free(cnp, sizeof(*cnp));
224 }
225
226 /* hey baby, what's your namei? */
227 int
228 rump_namei(uint32_t op, uint32_t flags, const char *namep,
229 struct vnode **dvpp, struct vnode **vpp, struct componentname **cnpp)
230 {
231 struct nameidata nd;
232 int rv;
233
234 NDINIT(&nd, op, flags, UIO_SYSSPACE, namep);
235 rv = namei(&nd);
236 if (rv)
237 return rv;
238
239 if (dvpp) {
240 KASSERT(flags & LOCKPARENT);
241 *dvpp = nd.ni_dvp;
242 } else {
243 KASSERT((flags & LOCKPARENT) == 0);
244 }
245
246 if (vpp) {
247 *vpp = nd.ni_vp;
248 } else {
249 if (nd.ni_vp) {
250 if (flags & LOCKLEAF)
251 vput(nd.ni_vp);
252 else
253 vrele(nd.ni_vp);
254 }
255 }
256
257 if (cnpp) {
258 struct componentname *cnp;
259
260 cnp = kmem_alloc(sizeof(*cnp), KM_SLEEP);
261 memcpy(cnp, &nd.ni_cnd, sizeof(*cnp));
262 *cnpp = cnp;
263 } else if (nd.ni_cnd.cn_flags & HASBUF) {
264 panic("%s: pathbuf mismatch", __func__);
265 }
266
267 return rv;
268 }
269
270 static struct fakeblk *
271 _rump_fakeblk_find(const char *path)
272 {
273 char buf[MAXPATHLEN];
274 struct fakeblk *fblk;
275 int error;
276
277 if (rumpuser_realpath(path, buf, &error) == NULL)
278 return NULL;
279
280 LIST_FOREACH(fblk, &fakeblks, entries)
281 if (strcmp(fblk->path, buf) == 0)
282 return fblk;
283
284 return NULL;
285 }
286
287 int
288 rump_fakeblk_register(const char *path)
289 {
290 char buf[MAXPATHLEN];
291 struct fakeblk *fblk;
292 int error;
293
294 if (_rump_fakeblk_find(path))
295 return EEXIST;
296
297 if (rumpuser_realpath(path, buf, &error) == NULL)
298 return error;
299
300 fblk = kmem_alloc(sizeof(struct fakeblk), KM_NOSLEEP);
301 if (fblk == NULL)
302 return ENOMEM;
303
304 strlcpy(fblk->path, buf, MAXPATHLEN);
305 LIST_INSERT_HEAD(&fakeblks, fblk, entries);
306
307 return 0;
308 }
309
310 int
311 rump_fakeblk_find(const char *path)
312 {
313
314 return _rump_fakeblk_find(path) != NULL;
315 }
316
317 void
318 rump_fakeblk_deregister(const char *path)
319 {
320 struct fakeblk *fblk;
321
322 fblk = _rump_fakeblk_find(path);
323 if (fblk == NULL)
324 return;
325
326 LIST_REMOVE(fblk, entries);
327 kmem_free(fblk, sizeof(*fblk));
328 }
329
330 void
331 rump_getvninfo(struct vnode *vp, enum vtype *vtype, voff_t *vsize, dev_t *vdev)
332 {
333
334 *vtype = vp->v_type;
335 *vsize = vp->v_size;
336 if (vp->v_specnode)
337 *vdev = vp->v_rdev;
338 else
339 *vdev = 0;
340 }
341
342 struct vfsops *
343 rump_vfslist_iterate(struct vfsops *ops)
344 {
345
346 if (ops == NULL)
347 return LIST_FIRST(&vfs_list);
348 else
349 return LIST_NEXT(ops, vfs_list);
350 }
351
352 struct vfsops *
353 rump_vfs_getopsbyname(const char *name)
354 {
355
356 return vfs_getopsbyname(name);
357 }
358
359 struct vattr*
360 rump_vattr_init(void)
361 {
362 struct vattr *vap;
363
364 vap = kmem_alloc(sizeof(struct vattr), KM_SLEEP);
365 vattr_null(vap);
366
367 return vap;
368 }
369
370 void
371 rump_vattr_settype(struct vattr *vap, enum vtype vt)
372 {
373
374 vap->va_type = vt;
375 }
376
377 void
378 rump_vattr_setmode(struct vattr *vap, mode_t mode)
379 {
380
381 vap->va_mode = mode;
382 }
383
384 void
385 rump_vattr_setrdev(struct vattr *vap, dev_t dev)
386 {
387
388 vap->va_rdev = dev;
389 }
390
391 void
392 rump_vattr_free(struct vattr *vap)
393 {
394
395 kmem_free(vap, sizeof(*vap));
396 }
397
398 void
399 rump_vp_incref(struct vnode *vp)
400 {
401
402 mutex_enter(&vp->v_interlock);
403 ++vp->v_usecount;
404 mutex_exit(&vp->v_interlock);
405 }
406
407 int
408 rump_vp_getref(struct vnode *vp)
409 {
410
411 return vp->v_usecount;
412 }
413
414 void
415 rump_vp_decref(struct vnode *vp)
416 {
417
418 mutex_enter(&vp->v_interlock);
419 --vp->v_usecount;
420 mutex_exit(&vp->v_interlock);
421 }
422
423 /*
424 * Really really recycle with a cherry on top. We should be
425 * extra-sure we can do this. For example with p2k there is
426 * no problem, since puffs in the kernel takes care of refcounting
427 * for us.
428 */
429 void
430 rump_vp_recycle_nokidding(struct vnode *vp)
431 {
432
433 mutex_enter(&vp->v_interlock);
434 vp->v_usecount = 1;
435 /*
436 * XXX: NFS holds a reference to the root vnode, so don't clean
437 * it out. This is very wrong, but fixing it properly would
438 * take too much effort for now
439 */
440 if (vp->v_tag == VT_NFS && vp->v_vflag & VV_ROOT) {
441 mutex_exit(&vp->v_interlock);
442 return;
443 }
444 vclean(vp, DOCLOSE);
445 vrelel(vp, 0);
446 }
447
448 void
449 rump_vp_rele(struct vnode *vp)
450 {
451
452 vrele(vp);
453 }
454
455 void
456 rump_vp_interlock(struct vnode *vp)
457 {
458
459 mutex_enter(&vp->v_interlock);
460 }
461
462 int
463 rump_vfs_unmount(struct mount *mp, int mntflags)
464 {
465
466 return VFS_UNMOUNT(mp, mntflags);
467 }
468
469 int
470 rump_vfs_root(struct mount *mp, struct vnode **vpp, int lock)
471 {
472 int rv;
473
474 rv = VFS_ROOT(mp, vpp);
475 if (rv)
476 return rv;
477
478 if (!lock)
479 VOP_UNLOCK(*vpp, 0);
480
481 return 0;
482 }
483
484 int
485 rump_vfs_statvfs(struct mount *mp, struct statvfs *sbp)
486 {
487
488 return VFS_STATVFS(mp, sbp);
489 }
490
491 int
492 rump_vfs_sync(struct mount *mp, int wait, kauth_cred_t cred)
493 {
494
495 return VFS_SYNC(mp, wait ? MNT_WAIT : MNT_NOWAIT, cred);
496 }
497
498 int
499 rump_vfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
500 {
501
502 return VFS_FHTOVP(mp, fid, vpp);
503 }
504
505 int
506 rump_vfs_vptofh(struct vnode *vp, struct fid *fid, size_t *fidsize)
507 {
508
509 return VFS_VPTOFH(vp, fid, fidsize);
510 }
511
512 /*ARGSUSED*/
513 void
514 rump_vfs_syncwait(struct mount *mp)
515 {
516 int n;
517
518 n = buf_syncwait();
519 if (n)
520 printf("syncwait: unsynced buffers: %d\n", n);
521 }
522
523 void
524 rump_biodone(void *arg, size_t count, int error)
525 {
526 struct buf *bp = arg;
527
528 bp->b_resid = bp->b_bcount - count;
529 KASSERT(bp->b_resid >= 0);
530 bp->b_error = error;
531
532 biodone(bp);
533 }
534
535 static void
536 rump_rcvp_lwpset(struct vnode *rvp, struct vnode *cvp, struct lwp *l)
537 {
538 struct cwdinfo *cwdi = l->l_proc->p_cwdi;
539
540 KASSERT(cvp);
541
542 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
543 if (cwdi->cwdi_rdir)
544 vrele(cwdi->cwdi_rdir);
545 if (rvp)
546 vref(rvp);
547 cwdi->cwdi_rdir = rvp;
548
549 vrele(cwdi->cwdi_cdir);
550 vref(cvp);
551 cwdi->cwdi_cdir = cvp;
552 rw_exit(&cwdi->cwdi_lock);
553 }
554
555 void
556 rump_rcvp_set(struct vnode *rvp, struct vnode *cvp)
557 {
558
559 rump_rcvp_lwpset(rvp, cvp, curlwp);
560 }
561
562 struct vnode *
563 rump_cdir_get(void)
564 {
565 struct vnode *vp;
566 struct cwdinfo *cwdi = curlwp->l_proc->p_cwdi;
567
568 rw_enter(&cwdi->cwdi_lock, RW_READER);
569 vp = cwdi->cwdi_cdir;
570 rw_exit(&cwdi->cwdi_lock);
571 vref(vp);
572
573 return vp;
574 }
575