rumpfs.c revision 1.43 1 /* $NetBSD: rumpfs.c,v 1.43 2010/04/29 22:32:49 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2009 Antti Kantee. All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: rumpfs.c,v 1.43 2010/04/29 22:32:49 pooka Exp $");
30
31 #include <sys/param.h>
32 #include <sys/atomic.h>
33 #include <sys/dirent.h>
34 #include <sys/errno.h>
35 #include <sys/filedesc.h>
36 #include <sys/fcntl.h>
37 #include <sys/kauth.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mount.h>
41 #include <sys/namei.h>
42 #include <sys/lock.h>
43 #include <sys/lockf.h>
44 #include <sys/queue.h>
45 #include <sys/stat.h>
46 #include <sys/syscallargs.h>
47 #include <sys/vnode.h>
48
49 #include <miscfs/fifofs/fifo.h>
50 #include <miscfs/specfs/specdev.h>
51 #include <miscfs/genfs/genfs.h>
52
53 #include <rump/rumpuser.h>
54
55 #include "rump_private.h"
56 #include "rump_vfs_private.h"
57
58 static int rump_vop_lookup(void *);
59 static int rump_vop_getattr(void *);
60 static int rump_vop_mkdir(void *);
61 static int rump_vop_mknod(void *);
62 static int rump_vop_create(void *);
63 static int rump_vop_inactive(void *);
64 static int rump_vop_reclaim(void *);
65 static int rump_vop_success(void *);
66 static int rump_vop_readdir(void *);
67 static int rump_vop_spec(void *);
68 static int rump_vop_read(void *);
69 static int rump_vop_write(void *);
70 static int rump_vop_open(void *);
71
72 int (**fifo_vnodeop_p)(void *);
73 const struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
74 { &vop_default_desc, vn_default_error },
75 { NULL, NULL }
76 };
77 const struct vnodeopv_desc fifo_vnodeop_opv_desc =
78 { &fifo_vnodeop_p, fifo_vnodeop_entries };
79
80 int (**rump_vnodeop_p)(void *);
81 const struct vnodeopv_entry_desc rump_vnodeop_entries[] = {
82 { &vop_default_desc, vn_default_error },
83 { &vop_lookup_desc, rump_vop_lookup },
84 { &vop_getattr_desc, rump_vop_getattr },
85 { &vop_mkdir_desc, rump_vop_mkdir },
86 { &vop_mknod_desc, rump_vop_mknod },
87 { &vop_create_desc, rump_vop_create },
88 { &vop_access_desc, rump_vop_success },
89 { &vop_readdir_desc, rump_vop_readdir },
90 { &vop_read_desc, rump_vop_read },
91 { &vop_write_desc, rump_vop_write },
92 { &vop_open_desc, rump_vop_open },
93 { &vop_putpages_desc, genfs_null_putpages },
94 { &vop_fsync_desc, rump_vop_success },
95 { &vop_lock_desc, genfs_lock },
96 { &vop_unlock_desc, genfs_unlock },
97 { &vop_inactive_desc, rump_vop_inactive },
98 { &vop_reclaim_desc, rump_vop_reclaim },
99 { NULL, NULL }
100 };
101 const struct vnodeopv_desc rump_vnodeop_opv_desc =
102 { &rump_vnodeop_p, rump_vnodeop_entries };
103
104 int (**rump_specop_p)(void *);
105 const struct vnodeopv_entry_desc rump_specop_entries[] = {
106 { &vop_default_desc, rump_vop_spec },
107 { NULL, NULL }
108 };
109 const struct vnodeopv_desc rump_specop_opv_desc =
110 { &rump_specop_p, rump_specop_entries };
111
112 const struct vnodeopv_desc * const rump_opv_descs[] = {
113 &rump_vnodeop_opv_desc,
114 &rump_specop_opv_desc,
115 NULL
116 };
117
118 struct rumpfs_dent {
119 char *rd_name;
120 struct rumpfs_node *rd_node;
121
122 LIST_ENTRY(rumpfs_dent) rd_entries;
123 };
124
125 struct rumpfs_node {
126 struct vattr rn_va;
127 struct vnode *rn_vp;
128 char *rn_hostpath;
129 int rn_flags;
130
131 union {
132 struct { /* VREG */
133 int readfd;
134 int writefd;
135 uint64_t offset;
136 } reg;
137 struct { /* VDIR */
138 LIST_HEAD(, rumpfs_dent) dents;
139 int flags;
140 } dir;
141 } rn_u;
142 };
143 #define rn_readfd rn_u.reg.readfd
144 #define rn_writefd rn_u.reg.writefd
145 #define rn_offset rn_u.reg.offset
146 #define rn_dir rn_u.dir.dents
147
148 #define RUMPNODE_CANRECLAIM 0x01
149 #define RUMPNODE_DIR_ET 0x02
150 #define RUMPNODE_DIR_ETSUBS 0x04
151
152 struct rumpfs_mount {
153 struct vnode *rfsmp_rvp;
154 };
155
156 static struct rumpfs_node *makeprivate(enum vtype, dev_t, off_t);
157
158 /*
159 * Extra Terrestrial stuff. We map a given key (pathname) to a file on
160 * the host FS. ET phones home only from the root node of rumpfs.
161 *
162 * When an etfs node is removed, a vnode potentially behind it is not
163 * immediately recycled.
164 */
165
166 struct etfs {
167 char et_key[MAXPATHLEN];
168 size_t et_keylen;
169 bool et_prefixkey;
170
171 LIST_ENTRY(etfs) et_entries;
172
173 struct rumpfs_node *et_rn;
174 };
175 static kmutex_t etfs_lock;
176 static LIST_HEAD(, etfs) etfs_list = LIST_HEAD_INITIALIZER(etfs_list);
177
178 static enum vtype
179 ettype_to_vtype(enum rump_etfs_type et)
180 {
181 enum vtype vt;
182
183 switch (et) {
184 case RUMP_ETFS_REG:
185 vt = VREG;
186 break;
187 case RUMP_ETFS_BLK:
188 vt = VBLK;
189 break;
190 case RUMP_ETFS_CHR:
191 vt = VCHR;
192 break;
193 case RUMP_ETFS_DIR:
194 vt = VDIR;
195 break;
196 case RUMP_ETFS_DIR_SUBDIRS:
197 vt = VDIR;
198 break;
199 default:
200 panic("invalid et type: %d", et);
201 }
202
203 return vt;
204 }
205
206 static enum vtype
207 hft_to_vtype(int hft)
208 {
209 enum vtype vt;
210
211 switch (hft) {
212 case RUMPUSER_FT_OTHER:
213 vt = VNON;
214 break;
215 case RUMPUSER_FT_DIR:
216 vt = VDIR;
217 break;
218 case RUMPUSER_FT_REG:
219 vt = VREG;
220 break;
221 case RUMPUSER_FT_BLK:
222 vt = VBLK;
223 break;
224 case RUMPUSER_FT_CHR:
225 vt = VCHR;
226 break;
227 default:
228 vt = VNON;
229 break;
230 }
231
232 return vt;
233 }
234
235 static bool
236 etfs_find(const char *key, struct etfs **etp, bool forceprefix)
237 {
238 struct etfs *et;
239 size_t keylen = strlen(key);
240
241 KASSERT(mutex_owned(&etfs_lock));
242
243 LIST_FOREACH(et, &etfs_list, et_entries) {
244 if ((keylen == et->et_keylen || et->et_prefixkey || forceprefix)
245 && strncmp(key, et->et_key, et->et_keylen) == 0) {
246 if (etp)
247 *etp = et;
248 return true;
249 }
250 }
251
252 return false;
253 }
254
255 #define REGDIR(ftype) \
256 ((ftype) == RUMP_ETFS_DIR || (ftype) == RUMP_ETFS_DIR_SUBDIRS)
257 static int
258 doregister(const char *key, const char *hostpath,
259 enum rump_etfs_type ftype, uint64_t begin, uint64_t size)
260 {
261 struct etfs *et;
262 struct rumpfs_node *rn;
263 uint64_t fsize;
264 dev_t rdev = NODEV;
265 devminor_t dmin;
266 int hft, error;
267
268 if (rumpuser_getfileinfo(hostpath, &fsize, &hft, &error))
269 return error;
270
271 /* etfs directory requires a directory on the host */
272 if (REGDIR(ftype)) {
273 if (hft != RUMPUSER_FT_DIR)
274 return ENOTDIR;
275 if (begin != 0)
276 return EISDIR;
277 if (size != RUMP_ETFS_SIZE_ENDOFF)
278 return EISDIR;
279 size = fsize;
280 } else {
281 if (begin > fsize)
282 return EINVAL;
283 if (size == RUMP_ETFS_SIZE_ENDOFF)
284 size = fsize - begin;
285 if (begin + size > fsize)
286 return EINVAL;
287 }
288
289 if (ftype == RUMP_ETFS_BLK || ftype == RUMP_ETFS_CHR) {
290 error = rumpblk_register(hostpath, &dmin, begin, size);
291 if (error != 0) {
292 return error;
293 }
294 rdev = makedev(RUMPBLK_DEVMAJOR, dmin);
295 }
296
297 et = kmem_alloc(sizeof(*et), KM_SLEEP);
298 strcpy(et->et_key, key);
299 et->et_keylen = strlen(et->et_key);
300 et->et_rn = rn = makeprivate(ettype_to_vtype(ftype), rdev, size);
301
302 if (ftype == RUMP_ETFS_REG || REGDIR(ftype)) {
303 size_t len = strlen(hostpath)+1;
304
305 rn->rn_hostpath = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
306 memcpy(rn->rn_hostpath, hostpath, len);
307 rn->rn_offset = begin;
308 }
309
310 if (REGDIR(ftype)) {
311 rn->rn_flags |= RUMPNODE_DIR_ET;
312 et->et_prefixkey = true;
313 } else {
314 et->et_prefixkey = false;
315 }
316
317 if (ftype == RUMP_ETFS_DIR_SUBDIRS)
318 rn->rn_flags |= RUMPNODE_DIR_ETSUBS;
319
320 mutex_enter(&etfs_lock);
321 if (etfs_find(key, NULL, REGDIR(ftype))) {
322 mutex_exit(&etfs_lock);
323 kmem_free(et, sizeof(*et));
324 /* XXX: rumpblk_deregister(hostpath); */
325 return EEXIST;
326 }
327 LIST_INSERT_HEAD(&etfs_list, et, et_entries);
328 mutex_exit(&etfs_lock);
329
330 return 0;
331 }
332 #undef REGDIR
333
334 int
335 rump_etfs_register(const char *key, const char *hostpath,
336 enum rump_etfs_type ftype)
337 {
338
339 return doregister(key, hostpath, ftype, 0, RUMP_ETFS_SIZE_ENDOFF);
340 }
341
342 int
343 rump_etfs_register_withsize(const char *key, const char *hostpath,
344 enum rump_etfs_type ftype, uint64_t begin, uint64_t size)
345 {
346
347 /*
348 * Check that we're mapping at block offsets. I guess this
349 * is not technically necessary except for BLK/CHR backends
350 * (i.e. what getfileinfo() returns, not ftype) and can be
351 * removed later if there are problems.
352 */
353 if ((begin & (DEV_BSIZE-1)) != 0)
354 return EINVAL;
355 if (size != RUMP_ETFS_SIZE_ENDOFF && (size & (DEV_BSIZE-1)) != 0)
356 return EINVAL;
357
358 return doregister(key, hostpath, ftype, begin, size);
359 }
360
361 int
362 rump_etfs_remove(const char *key)
363 {
364 struct etfs *et;
365 size_t keylen = strlen(key);
366
367 mutex_enter(&etfs_lock);
368 LIST_FOREACH(et, &etfs_list, et_entries) {
369 if (keylen == et->et_keylen && strcmp(et->et_key, key) == 0) {
370 LIST_REMOVE(et, et_entries);
371 kmem_free(et, sizeof(*et));
372 break;
373 }
374 }
375 mutex_exit(&etfs_lock);
376
377 if (!et)
378 return ENOENT;
379 return 0;
380 }
381
382 /*
383 * rumpfs
384 */
385
386 static int lastino = 1;
387 static kmutex_t reclock;
388
389 static struct rumpfs_node *
390 makeprivate(enum vtype vt, dev_t rdev, off_t size)
391 {
392 struct rumpfs_node *rn;
393 struct vattr *va;
394 struct timespec ts;
395
396 rn = kmem_zalloc(sizeof(*rn), KM_SLEEP);
397
398 switch (vt) {
399 case VDIR:
400 LIST_INIT(&rn->rn_dir);
401 break;
402 case VREG:
403 rn->rn_readfd = -1;
404 rn->rn_writefd = -1;
405 break;
406 default:
407 break;
408 }
409
410 nanotime(&ts);
411
412 va = &rn->rn_va;
413 va->va_type = vt;
414 va->va_mode = 0755;
415 if (vt == VDIR)
416 va->va_nlink = 2;
417 else
418 va->va_nlink = 1;
419 va->va_uid = 0;
420 va->va_gid = 0;
421 va->va_fsid =
422 va->va_fileid = atomic_inc_uint_nv(&lastino);
423 va->va_size = size;
424 va->va_blocksize = 512;
425 va->va_atime = ts;
426 va->va_mtime = ts;
427 va->va_ctime = ts;
428 va->va_birthtime = ts;
429 va->va_gen = 0;
430 va->va_flags = 0;
431 va->va_rdev = rdev;
432 va->va_bytes = 512;
433 va->va_filerev = 0;
434 va->va_vaflags = 0;
435
436 return rn;
437 }
438
439 static int
440 makevnode(struct mount *mp, struct rumpfs_node *rn, struct vnode **vpp)
441 {
442 struct vnode *vp;
443 int (**vpops)(void *);
444 struct vattr *va = &rn->rn_va;
445 int rv;
446
447 KASSERT(mutex_owned(&reclock));
448
449 if (va->va_type == VCHR || va->va_type == VBLK) {
450 vpops = rump_specop_p;
451 } else {
452 vpops = rump_vnodeop_p;
453 }
454 if (vpops != rump_specop_p && va->va_type != VDIR
455 && !(va->va_type == VREG && rn->rn_hostpath != NULL)
456 && va->va_type != VSOCK)
457 return EOPNOTSUPP;
458
459 rv = getnewvnode(VT_RUMP, mp, vpops, &vp);
460 if (rv)
461 return rv;
462
463 vp->v_size = vp->v_writesize = va->va_size;
464 vp->v_type = va->va_type;
465
466 if (vpops == rump_specop_p) {
467 spec_node_init(vp, va->va_rdev);
468 }
469 vp->v_data = rn;
470
471 vn_lock(vp, LK_RETRY | LK_EXCLUSIVE);
472 rn->rn_vp = vp;
473 *vpp = vp;
474
475 return 0;
476 }
477
478 /*
479 * Simple lookup for faking lookup of device entry for rump file systems
480 * and for locating/creating directories. Yes, this will panic if you
481 * call it with the wrong arguments.
482 *
483 * uhm, this is twisted. C F C C, hope of C C F C looming
484 */
485 static int
486 rump_vop_lookup(void *v)
487 {
488 struct vop_lookup_args /* {
489 struct vnode *a_dvp;
490 struct vnode **a_vpp;
491 struct componentname *a_cnp;
492 }; */ *ap = v;
493 struct componentname *cnp = ap->a_cnp;
494 struct vnode *dvp = ap->a_dvp;
495 struct vnode **vpp = ap->a_vpp;
496 struct vnode *vp;
497 struct rumpfs_node *rnd = dvp->v_data, *rn;
498 struct rumpfs_dent *rd = NULL;
499 struct etfs *et;
500 int rv;
501
502 /* we handle only some "non-special" cases */
503 if (!(((cnp->cn_flags & ISLASTCN) == 0)
504 || (cnp->cn_nameiop == LOOKUP || cnp->cn_nameiop == CREATE)))
505 return EOPNOTSUPP;
506 if (!((cnp->cn_flags & ISDOTDOT) == 0))
507 return EOPNOTSUPP;
508
509 /* check for dot, return directly if the case */
510 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
511 vref(dvp);
512 *vpp = dvp;
513 goto out;
514 }
515
516 /* check for etfs */
517 if (dvp == rootvnode && cnp->cn_nameiop == LOOKUP) {
518 bool found;
519 mutex_enter(&etfs_lock);
520 found = etfs_find(cnp->cn_pnbuf, &et, false);
521 mutex_exit(&etfs_lock);
522
523 if (found) {
524 char *offset;
525
526 offset = strstr(cnp->cn_pnbuf, et->et_key);
527 KASSERT(offset);
528
529 rn = et->et_rn;
530 cnp->cn_consume += et->et_keylen
531 - (cnp->cn_nameptr - offset) - cnp->cn_namelen;
532 if (rn->rn_va.va_type != VDIR)
533 cnp->cn_flags &= ~REQUIREDIR;
534 goto getvnode;
535 }
536 }
537
538 if (rnd->rn_flags & RUMPNODE_DIR_ET) {
539 uint64_t fsize;
540 char *newpath;
541 size_t newpathlen;
542 int hft, error;
543
544 newpathlen = strlen(rnd->rn_hostpath) + 1 + cnp->cn_namelen + 1;
545 newpath = malloc(newpathlen, M_TEMP, M_WAITOK);
546
547 strlcpy(newpath, rnd->rn_hostpath, newpathlen);
548 strlcat(newpath, "/", newpathlen);
549 strlcat(newpath, cnp->cn_nameptr, newpathlen);
550
551 if (rumpuser_getfileinfo(newpath, &fsize, &hft, &error)) {
552 free(newpath, M_TEMP);
553 return error;
554 }
555
556 /* allow only dirs and regular files */
557 if (hft != RUMPUSER_FT_REG && hft != RUMPUSER_FT_DIR) {
558 free(newpath, M_TEMP);
559 return ENOENT;
560 }
561
562 rn = makeprivate(hft_to_vtype(hft), NODEV, fsize);
563 rn->rn_flags |= RUMPNODE_CANRECLAIM;
564 if (rnd->rn_flags & RUMPNODE_DIR_ETSUBS) {
565 rn->rn_flags |= RUMPNODE_DIR_ET | RUMPNODE_DIR_ETSUBS;
566 }
567 rn->rn_hostpath = newpath;
568
569 goto getvnode;
570 } else {
571 LIST_FOREACH(rd, &rnd->rn_dir, rd_entries) {
572 if (strlen(rd->rd_name) == cnp->cn_namelen &&
573 strncmp(rd->rd_name, cnp->cn_nameptr,
574 cnp->cn_namelen) == 0)
575 break;
576 }
577 }
578
579 if (!rd && ((cnp->cn_flags & ISLASTCN) == 0||cnp->cn_nameiop != CREATE))
580 return ENOENT;
581
582 if (!rd && (cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) {
583 cnp->cn_flags |= SAVENAME;
584 return EJUSTRETURN;
585 }
586 rn = rd->rd_node;
587
588 getvnode:
589 KASSERT(rn);
590 mutex_enter(&reclock);
591 if ((vp = rn->rn_vp)) {
592 mutex_enter(&vp->v_interlock);
593 mutex_exit(&reclock);
594 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
595 goto getvnode;
596 *vpp = vp;
597 } else {
598 rv = makevnode(dvp->v_mount, rn, vpp);
599 rn->rn_vp = *vpp;
600 mutex_exit(&reclock);
601 if (rv)
602 return rv;
603 }
604
605 out:
606 return 0;
607 }
608
609 static int
610 rump_vop_getattr(void *v)
611 {
612 struct vop_getattr_args /* {
613 struct vnode *a_vp;
614 struct vattr *a_vap;
615 kauth_cred_t a_cred;
616 } */ *ap = v;
617 struct rumpfs_node *rn = ap->a_vp->v_data;
618
619 memcpy(ap->a_vap, &rn->rn_va, sizeof(struct vattr));
620 return 0;
621 }
622
623 static int
624 rump_vop_mkdir(void *v)
625 {
626 struct vop_mkdir_args /* {
627 struct vnode *a_dvp;
628 struct vnode **a_vpp;
629 struct componentname *a_cnp;
630 struct vattr *a_vap;
631 }; */ *ap = v;
632 struct vnode *dvp = ap->a_dvp;
633 struct vnode **vpp = ap->a_vpp;
634 struct componentname *cnp = ap->a_cnp;
635 struct rumpfs_node *rnd = dvp->v_data, *rn;
636 struct rumpfs_dent *rdent;
637 int rv = 0;
638
639 rn = makeprivate(VDIR, NODEV, DEV_BSIZE);
640 mutex_enter(&reclock);
641 rv = makevnode(dvp->v_mount, rn, vpp);
642 mutex_exit(&reclock);
643 if (rv)
644 goto out;
645
646 rdent = kmem_alloc(sizeof(*rdent), KM_SLEEP);
647 rdent->rd_name = kmem_alloc(cnp->cn_namelen+1, KM_SLEEP);
648 rdent->rd_node = (*vpp)->v_data;
649 strlcpy(rdent->rd_name, cnp->cn_nameptr, cnp->cn_namelen+1);
650
651 LIST_INSERT_HEAD(&rnd->rn_dir, rdent, rd_entries);
652
653 out:
654 vput(dvp);
655 return rv;
656 }
657
658 static int
659 rump_vop_mknod(void *v)
660 {
661 struct vop_mknod_args /* {
662 struct vnode *a_dvp;
663 struct vnode **a_vpp;
664 struct componentname *a_cnp;
665 struct vattr *a_vap;
666 }; */ *ap = v;
667 struct vnode *dvp = ap->a_dvp;
668 struct vnode **vpp = ap->a_vpp;
669 struct componentname *cnp = ap->a_cnp;
670 struct vattr *va = ap->a_vap;
671 struct rumpfs_node *rnd = dvp->v_data, *rn;
672 struct rumpfs_dent *rdent;
673 int rv;
674
675 rn = makeprivate(va->va_type, va->va_rdev, DEV_BSIZE);
676 mutex_enter(&reclock);
677 rv = makevnode(dvp->v_mount, rn, vpp);
678 mutex_exit(&reclock);
679 if (rv)
680 goto out;
681
682 rdent = kmem_alloc(sizeof(*rdent), KM_SLEEP);
683 rdent->rd_name = kmem_alloc(cnp->cn_namelen+1, KM_SLEEP);
684 rdent->rd_node = (*vpp)->v_data;
685 rdent->rd_node->rn_va.va_rdev = va->va_rdev;
686 strlcpy(rdent->rd_name, cnp->cn_nameptr, cnp->cn_namelen+1);
687
688 LIST_INSERT_HEAD(&rnd->rn_dir, rdent, rd_entries);
689
690 out:
691 vput(dvp);
692 return rv;
693 }
694
695 static int
696 rump_vop_create(void *v)
697 {
698 struct vop_create_args /* {
699 struct vnode *a_dvp;
700 struct vnode **a_vpp;
701 struct componentname *a_cnp;
702 struct vattr *a_vap;
703 }; */ *ap = v;
704 struct vnode *dvp = ap->a_dvp;
705 struct vnode **vpp = ap->a_vpp;
706 struct componentname *cnp = ap->a_cnp;
707 struct vattr *va = ap->a_vap;
708 struct rumpfs_node *rnd = dvp->v_data, *rn;
709 struct rumpfs_dent *rdent;
710 int rv;
711
712 if (va->va_type != VSOCK) {
713 rv = EOPNOTSUPP;
714 goto out;
715 }
716 rn = makeprivate(VSOCK, NODEV, DEV_BSIZE);
717 mutex_enter(&reclock);
718 rv = makevnode(dvp->v_mount, rn, vpp);
719 mutex_exit(&reclock);
720 if (rv)
721 goto out;
722
723 rdent = kmem_alloc(sizeof(*rdent), KM_SLEEP);
724 rdent->rd_name = kmem_alloc(cnp->cn_namelen+1, KM_SLEEP);
725 rdent->rd_node = (*vpp)->v_data;
726 rdent->rd_node->rn_va.va_rdev = NODEV;
727 strlcpy(rdent->rd_name, cnp->cn_nameptr, cnp->cn_namelen+1);
728
729 LIST_INSERT_HEAD(&rnd->rn_dir, rdent, rd_entries);
730
731 out:
732 vput(dvp);
733 return rv;
734 }
735
736 static int
737 rump_vop_open(void *v)
738 {
739 struct vop_open_args /* {
740 struct vnode *a_vp;
741 int a_mode;
742 kauth_cred_t a_cred;
743 } */ *ap = v;
744 struct vnode *vp = ap->a_vp;
745 struct rumpfs_node *rn = vp->v_data;
746 int mode = ap->a_mode;
747 int error = EINVAL;
748
749 if (vp->v_type != VREG)
750 return 0;
751
752 if (mode & FREAD) {
753 if (rn->rn_readfd != -1)
754 return 0;
755 rn->rn_readfd = rumpuser_open(rn->rn_hostpath,
756 O_RDONLY, &error);
757 } else if (mode & FWRITE) {
758 if (rn->rn_writefd != -1)
759 return 0;
760 rn->rn_writefd = rumpuser_open(rn->rn_hostpath,
761 O_WRONLY, &error);
762 }
763
764 return error;
765 }
766
767 /* copypaste from libpuffs. XXX: do this properly */
768 static int vdmap[] = {
769 DT_UNKNOWN, /* VNON */
770 DT_REG, /* VREG */
771 DT_DIR, /* VDIR */
772 DT_BLK, /* VBLK */
773 DT_CHR, /* VCHR */
774 DT_LNK, /* VLNK */
775 DT_SOCK, /* VSUCK*/
776 DT_FIFO, /* VFIFO*/
777 DT_UNKNOWN /* VBAD */
778 };
779 /* simple readdir. event omits dotstuff and periods */
780 static int
781 rump_vop_readdir(void *v)
782 {
783 struct vop_readdir_args /* {
784 struct vnode *a_vp;
785 struct uio *a_uio;
786 kauth_cred_t a_cred;
787 int *a_eofflag;
788 off_t **a_cookies;
789 int *a_ncookies;
790 } */ *ap = v;
791 struct vnode *vp = ap->a_vp;
792 struct uio *uio = ap->a_uio;
793 struct rumpfs_node *rnd = vp->v_data;
794 struct rumpfs_dent *rdent;
795 unsigned i;
796 int rv = 0;
797
798 /* seek to current entry */
799 for (i = 0, rdent = LIST_FIRST(&rnd->rn_dir);
800 (i < uio->uio_offset) && rdent;
801 i++, rdent = LIST_NEXT(rdent, rd_entries))
802 continue;
803 if (!rdent)
804 goto out;
805
806 /* copy entries */
807 for (; rdent && uio->uio_resid > 0;
808 rdent = LIST_NEXT(rdent, rd_entries), i++) {
809 struct dirent dent;
810
811 dent.d_fileno = rdent->rd_node->rn_va.va_fileid;
812 dent.d_namlen = strlen(dent.d_name);
813 strcpy(dent.d_name, rdent->rd_name);
814 dent.d_type = vdmap[rdent->rd_node->rn_va.va_type];
815 dent.d_reclen = _DIRENT_RECLEN(&dent, dent.d_namlen);
816
817 if (uio->uio_resid < dent.d_reclen) {
818 i--;
819 break;
820 }
821
822 rv = uiomove(&dent, dent.d_reclen, uio);
823 if (rv) {
824 i--;
825 break;
826 }
827 }
828
829 out:
830 if (ap->a_cookies) {
831 *ap->a_ncookies = 0;
832 *ap->a_cookies = NULL;
833 }
834 if (rdent)
835 *ap->a_eofflag = 0;
836 else
837 *ap->a_eofflag = 1;
838 uio->uio_offset = i;
839
840 return rv;
841 }
842
843 static int
844 rump_vop_read(void *v)
845 {
846 struct vop_read_args /* {
847 struct vnode *a_vp;
848 struct uio *a_uio;
849 int ioflags a_ioflag;
850 kauth_cred_t a_cred;
851 }; */ *ap = v;
852 struct vnode *vp = ap->a_vp;
853 struct rumpfs_node *rn = vp->v_data;
854 struct uio *uio = ap->a_uio;
855 uint8_t *buf;
856 size_t bufsize;
857 int error = 0;
858
859 bufsize = uio->uio_resid;
860 buf = kmem_alloc(bufsize, KM_SLEEP);
861 if (rumpuser_pread(rn->rn_readfd, buf, bufsize,
862 uio->uio_offset + rn->rn_offset, &error) == -1)
863 goto out;
864 error = uiomove(buf, bufsize, uio);
865
866 out:
867 kmem_free(buf, bufsize);
868 return error;
869 }
870
871 static int
872 rump_vop_write(void *v)
873 {
874 struct vop_read_args /* {
875 struct vnode *a_vp;
876 struct uio *a_uio;
877 int ioflags a_ioflag;
878 kauth_cred_t a_cred;
879 }; */ *ap = v;
880 struct vnode *vp = ap->a_vp;
881 struct rumpfs_node *rn = vp->v_data;
882 struct uio *uio = ap->a_uio;
883 uint8_t *buf;
884 size_t bufsize;
885 int error = 0;
886
887 bufsize = uio->uio_resid;
888 buf = kmem_alloc(bufsize, KM_SLEEP);
889 error = uiomove(buf, bufsize, uio);
890 if (error)
891 goto out;
892 KASSERT(uio->uio_resid == 0);
893 rumpuser_pwrite(rn->rn_writefd, buf, bufsize,
894 uio->uio_offset + rn->rn_offset, &error);
895
896 out:
897 kmem_free(buf, bufsize);
898 return error;
899 }
900
901 static int
902 rump_vop_success(void *v)
903 {
904
905 return 0;
906 }
907
908 static int
909 rump_vop_inactive(void *v)
910 {
911 struct vop_inactive_args *ap = v;
912 struct vnode *vp = ap->a_vp;
913 struct rumpfs_node *rn = vp->v_data;
914 int error;
915
916 if (vp->v_type == VREG) {
917 if (rn->rn_readfd != -1) {
918 rumpuser_close(rn->rn_readfd, &error);
919 rn->rn_readfd = -1;
920 }
921 if (rn->rn_writefd != -1) {
922 rumpuser_close(rn->rn_writefd, &error);
923 rn->rn_writefd = -1;
924 }
925 }
926
927 VOP_UNLOCK(vp, 0);
928 return 0;
929 }
930
931 static int
932 rump_vop_reclaim(void *v)
933 {
934 struct vop_reclaim_args /* {
935 struct vnode *a_vp;
936 } */ *ap = v;
937 struct vnode *vp = ap->a_vp;
938 struct rumpfs_node *rn = vp->v_data;
939
940 mutex_enter(&reclock);
941 rn->rn_vp = NULL;
942 mutex_exit(&reclock);
943 vp->v_data = NULL;
944
945 if (rn->rn_flags & RUMPNODE_CANRECLAIM) {
946 if (rn->rn_hostpath)
947 free(rn->rn_hostpath, M_TEMP);
948 kmem_free(rn, sizeof(*rn));
949 }
950
951 return 0;
952 }
953
954 static int
955 rump_vop_spec(void *v)
956 {
957 struct vop_generic_args *ap = v;
958 int (**opvec)(void *);
959
960 switch (ap->a_desc->vdesc_offset) {
961 case VOP_ACCESS_DESCOFFSET:
962 case VOP_GETATTR_DESCOFFSET:
963 case VOP_LOCK_DESCOFFSET:
964 case VOP_UNLOCK_DESCOFFSET:
965 opvec = rump_vnodeop_p;
966 break;
967 default:
968 opvec = spec_vnodeop_p;
969 break;
970 }
971
972 return VOCALL(opvec, ap->a_desc->vdesc_offset, v);
973 }
974
975 /*
976 * Begin vfs-level stuff
977 */
978
979 VFS_PROTOS(rumpfs);
980 struct vfsops rumpfs_vfsops = {
981 .vfs_name = MOUNT_RUMPFS,
982 .vfs_min_mount_data = 0,
983 .vfs_mount = rumpfs_mount,
984 .vfs_start = (void *)nullop,
985 .vfs_unmount = rumpfs_unmount,
986 .vfs_root = rumpfs_root,
987 .vfs_quotactl = (void *)eopnotsupp,
988 .vfs_statvfs = genfs_statvfs,
989 .vfs_sync = (void *)nullop,
990 .vfs_vget = rumpfs_vget,
991 .vfs_fhtovp = (void *)eopnotsupp,
992 .vfs_vptofh = (void *)eopnotsupp,
993 .vfs_init = rumpfs_init,
994 .vfs_reinit = NULL,
995 .vfs_done = rumpfs_done,
996 .vfs_mountroot = rumpfs_mountroot,
997 .vfs_snapshot = (void *)eopnotsupp,
998 .vfs_extattrctl = (void *)eopnotsupp,
999 .vfs_suspendctl = (void *)eopnotsupp,
1000 .vfs_opv_descs = rump_opv_descs,
1001 /* vfs_refcount */
1002 /* vfs_list */
1003 };
1004
1005 int
1006 rumpfs_mount(struct mount *mp, const char *mntpath, void *arg, size_t *alen)
1007 {
1008
1009 return EOPNOTSUPP;
1010 }
1011
1012 int
1013 rumpfs_unmount(struct mount *mp, int flags)
1014 {
1015
1016 /* if going for it, just lie about it */
1017 if (panicstr)
1018 return 0;
1019
1020 return EOPNOTSUPP; /* ;) */
1021 }
1022
1023 int
1024 rumpfs_root(struct mount *mp, struct vnode **vpp)
1025 {
1026 struct rumpfs_mount *rfsmp = mp->mnt_data;
1027
1028 vget(rfsmp->rfsmp_rvp, LK_EXCLUSIVE | LK_RETRY);
1029 *vpp = rfsmp->rfsmp_rvp;
1030 return 0;
1031 }
1032
1033 int
1034 rumpfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1035 {
1036
1037 return EOPNOTSUPP;
1038 }
1039
1040 void
1041 rumpfs_init()
1042 {
1043
1044 CTASSERT(RUMP_ETFS_SIZE_ENDOFF == RUMPBLK_SIZENOTSET);
1045
1046 mutex_init(&reclock, MUTEX_DEFAULT, IPL_NONE);
1047 mutex_init(&etfs_lock, MUTEX_DEFAULT, IPL_NONE);
1048 }
1049
1050 void
1051 rumpfs_done()
1052 {
1053
1054 mutex_destroy(&reclock);
1055 mutex_destroy(&etfs_lock);
1056 }
1057
1058 int
1059 rumpfs_mountroot()
1060 {
1061 struct mount *mp;
1062 struct rumpfs_mount *rfsmp;
1063 struct rumpfs_node *rn;
1064 int error;
1065
1066 if ((error = vfs_rootmountalloc(MOUNT_RUMPFS, "rootdev", &mp)) != 0) {
1067 vrele(rootvp);
1068 return error;
1069 }
1070
1071 rfsmp = kmem_alloc(sizeof(*rfsmp), KM_SLEEP);
1072
1073 rn = makeprivate(VDIR, NODEV, DEV_BSIZE);
1074 mutex_enter(&reclock);
1075 error = makevnode(mp, rn, &rfsmp->rfsmp_rvp);
1076 mutex_exit(&reclock);
1077 if (error)
1078 panic("could not create root vnode: %d", error);
1079 rfsmp->rfsmp_rvp->v_vflag |= VV_ROOT;
1080 VOP_UNLOCK(rfsmp->rfsmp_rvp, 0);
1081
1082 mutex_enter(&mountlist_lock);
1083 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1084 mutex_exit(&mountlist_lock);
1085
1086 mp->mnt_data = rfsmp;
1087 mp->mnt_stat.f_namemax = MAXNAMLEN;
1088 mp->mnt_stat.f_iosize = 512;
1089 mp->mnt_flag |= MNT_LOCAL;
1090 mp->mnt_iflag |= IMNT_MPSAFE;
1091 vfs_getnewfsid(mp);
1092
1093 error = set_statvfs_info("/", UIO_SYSSPACE, "rumpfs", UIO_SYSSPACE,
1094 mp->mnt_op->vfs_name, mp, curlwp);
1095 if (error)
1096 panic("set statvfsinfo for rootfs failed");
1097
1098 vfs_unbusy(mp, false, NULL);
1099
1100 return 0;
1101 }
1102