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