rump_vfs.c revision 1.71 1 /* $NetBSD: rump_vfs.c,v 1.71 2013/01/14 16:45:47 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.71 2013/01/14 16:45:47 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/stat.h>
46 #include <sys/vfs_syscalls.h>
47 #include <sys/vnode.h>
48 #include <sys/wapbl.h>
49
50 #include <miscfs/specfs/specdev.h>
51 #include <miscfs/syncfs/syncfs.h>
52
53 #include <rump/rump.h>
54 #include <rump/rumpuser.h>
55
56 #include "rump_private.h"
57 #include "rump_vfs_private.h"
58
59 extern struct cwdinfo cwdi0;
60 const char *rootfstype = ROOT_FSTYPE_ANY;
61
62 static void
63 pvfs_init(struct proc *p)
64 {
65
66 p->p_cwdi = cwdinit();
67 }
68
69 static void
70 pvfs_rele(struct proc *p)
71 {
72
73 cwdfree(p->p_cwdi);
74 }
75
76 static void
77 fini(void)
78 {
79
80 vfs_shutdown();
81 }
82
83 static void
84 drainbufs(int npages)
85 {
86
87 mutex_enter(&bufcache_lock);
88 buf_drain(npages);
89 mutex_exit(&bufcache_lock);
90 }
91
92 RUMP_COMPONENT(RUMP__FACTION_VFS)
93 {
94 extern struct devsw_conv devsw_conv0[];
95 extern int max_devsw_convs;
96 extern struct vfsops rumpfs_vfsops;
97 char buf[64];
98 int error;
99 int rv, i;
100
101 /* initialize indirect interfaces */
102 rump_vfs_fini = fini;
103 rump_vfs_drainbufs = drainbufs;
104
105 if (rumpuser_getenv("RUMP_NVNODES", buf, sizeof(buf), &error) == 0) {
106 desiredvnodes = strtoul(buf, NULL, 10);
107 } else {
108 desiredvnodes = 1<<10;
109 }
110
111 rumpblk_init();
112
113 for (i = 0; i < ncpu; i++) {
114 struct cpu_info *ci = cpu_lookup(i);
115 cache_cpu_init(ci);
116 }
117
118 /* make number of bufpages 5% of total memory limit */
119 if (rump_physmemlimit != RUMPMEM_UNLIMITED) {
120 extern u_int bufpages;
121 bufpages = rump_physmemlimit / (20 * PAGE_SIZE);
122 }
123
124 vfsinit();
125 bufinit();
126 cwd_sys_init();
127 lf_init();
128 spec_init();
129 fstrans_init();
130
131 if (rump_threads) {
132 if ((rv = kthread_create(PRI_BIO, KTHREAD_MPSAFE, NULL,
133 rumpuser_biothread, rump_biodone, NULL, "rmpabio")) != 0)
134 panic("syncer thread create failed: %d", rv);
135 }
136
137 root_device = &rump_rootdev;
138
139 /* bootstrap cwdi (rest done in vfs_mountroot() */
140 proc0.p_cwdi = &cwdi0;
141 proc0.p_cwdi = cwdinit();
142
143 vfs_attach(&rumpfs_vfsops);
144 vfs_mountroot();
145
146 /* "mtree": create /dev */
147 do_sys_mkdir("/dev", 0777, UIO_SYSSPACE);
148 rump_devnull_init();
149
150 rump_proc_vfs_init = pvfs_init;
151 rump_proc_vfs_release = pvfs_rele;
152
153 if (rump_threads) {
154 if ((rv = kthread_create(PRI_IOFLUSH, KTHREAD_MPSAFE, NULL,
155 sched_sync, NULL, NULL, "ioflush")) != 0)
156 panic("syncer thread create failed: %d", rv);
157 } else {
158 syncdelay = 0;
159 }
160
161 /*
162 * On archs where the native kernel ABI is supported, map
163 * host module directory to rump. This means that kernel
164 * modules from the host will be autoloaded to rump kernels.
165 */
166 #ifdef _RUMP_NATIVE_ABI
167 {
168 char *mbase;
169
170 if (rumpuser_getenv("RUMP_MODULEBASE", buf, sizeof(buf), &error) == 0)
171 mbase = buf;
172 else
173 mbase = module_base;
174
175 if (strlen(mbase) != 0 && *mbase != '0') {
176 rump_etfs_register(module_base, mbase, RUMP_ETFS_DIR_SUBDIRS);
177 }
178 }
179 #endif
180
181 module_init_class(MODULE_CLASS_VFS);
182
183 rump_vfs_builddevs(devsw_conv0, max_devsw_convs);
184
185 rump_component_init(RUMP_COMPONENT_VFS);
186 }
187
188 struct rumpcn {
189 struct componentname rcn_cn;
190 char *rcn_path;
191 };
192
193 struct componentname *
194 rump_makecn(u_long nameiop, u_long flags, const char *name, size_t namelen,
195 kauth_cred_t creds, struct lwp *l)
196 {
197 struct rumpcn *rcn;
198 struct componentname *cnp;
199
200 rcn = kmem_zalloc(sizeof(*rcn), KM_SLEEP);
201 cnp = &rcn->rcn_cn;
202
203 rcn->rcn_path = PNBUF_GET();
204 strlcpy(rcn->rcn_path, name, MAXPATHLEN);
205 cnp->cn_nameptr = rcn->rcn_path;
206
207 cnp->cn_nameiop = nameiop;
208 cnp->cn_flags = flags & (MODMASK | PARAMASK);
209
210 cnp->cn_namelen = namelen;
211
212 cnp->cn_cred = creds;
213
214 return cnp;
215 }
216
217 void
218 rump_freecn(struct componentname *cnp, int flags)
219 {
220 struct rumpcn *rcn = (void *)cnp;
221
222 if (flags & RUMPCN_FREECRED)
223 rump_cred_put(cnp->cn_cred);
224
225 PNBUF_PUT(rcn->rcn_path);
226 kmem_free(rcn, sizeof(*rcn));
227 }
228
229 /* hey baby, what's your namei? */
230 int
231 rump_namei(uint32_t op, uint32_t flags, const char *namep,
232 struct vnode **dvpp, struct vnode **vpp, struct componentname **cnpp)
233 {
234 struct pathbuf *pb;
235 struct nameidata nd;
236 int rv;
237
238 pb = pathbuf_create(namep);
239 if (pb == NULL) {
240 return ENOMEM;
241 }
242 NDINIT(&nd, op, flags, pb);
243 rv = namei(&nd);
244 if (rv) {
245 pathbuf_destroy(pb);
246 return rv;
247 }
248
249 if (dvpp) {
250 KASSERT(flags & LOCKPARENT);
251 *dvpp = nd.ni_dvp;
252 } else {
253 KASSERT((flags & LOCKPARENT) == 0);
254 }
255
256 if (vpp) {
257 *vpp = nd.ni_vp;
258 } else {
259 if (nd.ni_vp) {
260 if (flags & LOCKLEAF)
261 vput(nd.ni_vp);
262 else
263 vrele(nd.ni_vp);
264 }
265 }
266
267 if (cnpp) {
268 struct componentname *cnp;
269
270 cnp = kmem_alloc(sizeof(*cnp), KM_SLEEP);
271 memcpy(cnp, &nd.ni_cnd, sizeof(*cnp));
272 *cnpp = cnp;
273 }
274 pathbuf_destroy(pb);
275
276 return rv;
277 }
278
279 void
280 rump_getvninfo(struct vnode *vp, enum rump_vtype *vtype,
281 voff_t *vsize, dev_t *vdev)
282 {
283
284 *vtype = (enum rump_vtype)vp->v_type;
285 *vsize = vp->v_size;
286 if (vp->v_specnode)
287 *vdev = vp->v_rdev;
288 else
289 *vdev = 0;
290 }
291
292 struct vfsops *
293 rump_vfslist_iterate(struct vfsops *ops)
294 {
295
296 if (ops == NULL)
297 return LIST_FIRST(&vfs_list);
298 else
299 return LIST_NEXT(ops, vfs_list);
300 }
301
302 struct vfsops *
303 rump_vfs_getopsbyname(const char *name)
304 {
305
306 return vfs_getopsbyname(name);
307 }
308
309 int
310 rump_vfs_getmp(const char *path, struct mount **mpp)
311 {
312 struct vnode *vp;
313 int rv;
314
315 if ((rv = namei_simple_user(path, NSM_FOLLOW_TRYEMULROOT, &vp)) != 0)
316 return rv;
317
318 *mpp = vp->v_mount;
319 vrele(vp);
320 return 0;
321 }
322
323 struct vattr*
324 rump_vattr_init(void)
325 {
326 struct vattr *vap;
327
328 vap = kmem_alloc(sizeof(struct vattr), KM_SLEEP);
329 vattr_null(vap);
330
331 return vap;
332 }
333
334 void
335 rump_vattr_settype(struct vattr *vap, enum rump_vtype vt)
336 {
337
338 vap->va_type = (enum vtype)vt;
339 }
340
341 void
342 rump_vattr_setmode(struct vattr *vap, mode_t mode)
343 {
344
345 vap->va_mode = mode;
346 }
347
348 void
349 rump_vattr_setrdev(struct vattr *vap, dev_t dev)
350 {
351
352 vap->va_rdev = dev;
353 }
354
355 void
356 rump_vattr_free(struct vattr *vap)
357 {
358
359 kmem_free(vap, sizeof(*vap));
360 }
361
362 void
363 rump_vp_incref(struct vnode *vp)
364 {
365
366 vref(vp);
367 }
368
369 int
370 rump_vp_getref(struct vnode *vp)
371 {
372
373 return vp->v_usecount;
374 }
375
376 void
377 rump_vp_rele(struct vnode *vp)
378 {
379
380 vrele(vp);
381 }
382
383 void
384 rump_vp_interlock(struct vnode *vp)
385 {
386
387 mutex_enter(vp->v_interlock);
388 }
389
390 int
391 rump_vfs_unmount(struct mount *mp, int mntflags)
392 {
393
394 return VFS_UNMOUNT(mp, mntflags);
395 }
396
397 int
398 rump_vfs_root(struct mount *mp, struct vnode **vpp, int lock)
399 {
400 int rv;
401
402 rv = VFS_ROOT(mp, vpp);
403 if (rv)
404 return rv;
405
406 if (!lock)
407 VOP_UNLOCK(*vpp);
408
409 return 0;
410 }
411
412 int
413 rump_vfs_statvfs(struct mount *mp, struct statvfs *sbp)
414 {
415
416 return VFS_STATVFS(mp, sbp);
417 }
418
419 int
420 rump_vfs_sync(struct mount *mp, int wait, kauth_cred_t cred)
421 {
422
423 return VFS_SYNC(mp, wait ? MNT_WAIT : MNT_NOWAIT, cred);
424 }
425
426 int
427 rump_vfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
428 {
429
430 return VFS_FHTOVP(mp, fid, vpp);
431 }
432
433 int
434 rump_vfs_vptofh(struct vnode *vp, struct fid *fid, size_t *fidsize)
435 {
436
437 return VFS_VPTOFH(vp, fid, fidsize);
438 }
439
440 int
441 rump_vfs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
442 int attrnamespace, const char *attrname)
443 {
444
445 return VFS_EXTATTRCTL(mp, cmd, vp, attrnamespace, attrname);
446 }
447
448 /*ARGSUSED*/
449 void
450 rump_vfs_syncwait(struct mount *mp)
451 {
452 int n;
453
454 n = buf_syncwait();
455 if (n)
456 printf("syncwait: unsynced buffers: %d\n", n);
457 }
458
459 /*
460 * Dump info about mount point. No locking.
461 */
462 void
463 rump_vfs_mount_print(const char *path, int full)
464 {
465 #ifdef DEBUGPRINT
466 struct vnode *mvp;
467 struct vnode *vp;
468 int error;
469
470 rumpuser_dprintf("\n==== dumping mountpoint at ``%s'' ====\n\n", path);
471 if ((error = namei_simple_user(path, NSM_FOLLOW_NOEMULROOT, &mvp))!=0) {
472 rumpuser_dprintf("==== lookup error %d ====\n\n", error);
473 return;
474 }
475 vfs_mount_print(mvp->v_mount, full, (void *)rumpuser_dprintf);
476 if (full) {
477 rumpuser_dprintf("\n== dumping vnodes ==\n\n");
478 TAILQ_FOREACH(vp, &mvp->v_mount->mnt_vnodelist, v_mntvnodes) {
479 vfs_vnode_print(vp, full, (void *)rumpuser_dprintf);
480 }
481 }
482 vrele(mvp);
483 rumpuser_dprintf("\n==== done ====\n\n");
484 #else
485 rumpuser_dprintf("mount dump not supported without DEBUGPRINT\n");
486 #endif
487 }
488
489 void
490 rump_biodone(void *arg, size_t count, int error)
491 {
492 struct buf *bp = arg;
493
494 bp->b_resid = bp->b_bcount - count;
495 KASSERT(bp->b_resid >= 0);
496 bp->b_error = error;
497
498 biodone(bp);
499 }
500