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