rumpfs.c revision 1.94 1 /* $NetBSD: rumpfs.c,v 1.94 2011/03/27 21:16:52 riz Exp $ */
2
3 /*
4 * Copyright (c) 2009, 2010, 2011 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.94 2011/03/27 21:16:52 riz Exp $");
30
31 #include <sys/param.h>
32 #include <sys/atomic.h>
33 #include <sys/buf.h>
34 #include <sys/dirent.h>
35 #include <sys/errno.h>
36 #include <sys/filedesc.h>
37 #include <sys/fcntl.h>
38 #include <sys/kauth.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/mount.h>
42 #include <sys/namei.h>
43 #include <sys/lock.h>
44 #include <sys/lockf.h>
45 #include <sys/queue.h>
46 #include <sys/stat.h>
47 #include <sys/syscallargs.h>
48 #include <sys/vnode.h>
49 #include <sys/unistd.h>
50
51 #include <miscfs/fifofs/fifo.h>
52 #include <miscfs/specfs/specdev.h>
53 #include <miscfs/genfs/genfs.h>
54 #include <miscfs/genfs/genfs_node.h>
55
56 #include <uvm/uvm_extern.h>
57
58 #include <rump/rumpuser.h>
59
60 #include "rump_private.h"
61 #include "rump_vfs_private.h"
62
63 static int rump_vop_lookup(void *);
64 static int rump_vop_getattr(void *);
65 static int rump_vop_setattr(void *);
66 static int rump_vop_mkdir(void *);
67 static int rump_vop_rmdir(void *);
68 static int rump_vop_remove(void *);
69 static int rump_vop_mknod(void *);
70 static int rump_vop_create(void *);
71 static int rump_vop_inactive(void *);
72 static int rump_vop_reclaim(void *);
73 static int rump_vop_success(void *);
74 static int rump_vop_readdir(void *);
75 static int rump_vop_spec(void *);
76 static int rump_vop_read(void *);
77 static int rump_vop_write(void *);
78 static int rump_vop_open(void *);
79 static int rump_vop_symlink(void *);
80 static int rump_vop_readlink(void *);
81 static int rump_vop_whiteout(void *);
82 static int rump_vop_pathconf(void *);
83 static int rump_vop_bmap(void *);
84 static int rump_vop_strategy(void *);
85 static int rump_vop_advlock(void *);
86 static int rump_vop_access(void *);
87
88 int (**fifo_vnodeop_p)(void *);
89 const struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
90 { &vop_default_desc, vn_default_error },
91 { NULL, NULL }
92 };
93 const struct vnodeopv_desc fifo_vnodeop_opv_desc =
94 { &fifo_vnodeop_p, fifo_vnodeop_entries };
95
96 int (**rump_vnodeop_p)(void *);
97 const struct vnodeopv_entry_desc rump_vnodeop_entries[] = {
98 { &vop_default_desc, vn_default_error },
99 { &vop_lookup_desc, rump_vop_lookup },
100 { &vop_getattr_desc, rump_vop_getattr },
101 { &vop_setattr_desc, rump_vop_setattr },
102 { &vop_mkdir_desc, rump_vop_mkdir },
103 { &vop_rmdir_desc, rump_vop_rmdir },
104 { &vop_remove_desc, rump_vop_remove },
105 { &vop_mknod_desc, rump_vop_mknod },
106 { &vop_create_desc, rump_vop_create },
107 { &vop_symlink_desc, rump_vop_symlink },
108 { &vop_readlink_desc, rump_vop_readlink },
109 { &vop_access_desc, rump_vop_access },
110 { &vop_readdir_desc, rump_vop_readdir },
111 { &vop_read_desc, rump_vop_read },
112 { &vop_write_desc, rump_vop_write },
113 { &vop_open_desc, rump_vop_open },
114 { &vop_close_desc, genfs_nullop },
115 { &vop_seek_desc, genfs_seek },
116 { &vop_getpages_desc, genfs_getpages },
117 { &vop_putpages_desc, genfs_putpages },
118 { &vop_whiteout_desc, rump_vop_whiteout },
119 { &vop_fsync_desc, rump_vop_success },
120 { &vop_lock_desc, genfs_lock },
121 { &vop_unlock_desc, genfs_unlock },
122 { &vop_islocked_desc, genfs_islocked },
123 { &vop_inactive_desc, rump_vop_inactive },
124 { &vop_reclaim_desc, rump_vop_reclaim },
125 { &vop_link_desc, genfs_eopnotsupp },
126 { &vop_pathconf_desc, rump_vop_pathconf },
127 { &vop_bmap_desc, rump_vop_bmap },
128 { &vop_strategy_desc, rump_vop_strategy },
129 { &vop_advlock_desc, rump_vop_advlock },
130 { NULL, NULL }
131 };
132 const struct vnodeopv_desc rump_vnodeop_opv_desc =
133 { &rump_vnodeop_p, rump_vnodeop_entries };
134
135 int (**rump_specop_p)(void *);
136 const struct vnodeopv_entry_desc rump_specop_entries[] = {
137 { &vop_default_desc, rump_vop_spec },
138 { NULL, NULL }
139 };
140 const struct vnodeopv_desc rump_specop_opv_desc =
141 { &rump_specop_p, rump_specop_entries };
142
143 const struct vnodeopv_desc * const rump_opv_descs[] = {
144 &rump_vnodeop_opv_desc,
145 &rump_specop_opv_desc,
146 NULL
147 };
148
149 #define RUMPFS_WHITEOUT ((void *)-1)
150 #define RDENT_ISWHITEOUT(rdp) (rdp->rd_node == RUMPFS_WHITEOUT)
151 struct rumpfs_dent {
152 char *rd_name;
153 int rd_namelen;
154 struct rumpfs_node *rd_node;
155
156 LIST_ENTRY(rumpfs_dent) rd_entries;
157 };
158
159 struct genfs_ops rumpfs_genfsops = {
160 .gop_size = genfs_size,
161 .gop_write = genfs_gop_write,
162
163 /* optional */
164 .gop_alloc = NULL,
165 .gop_markupdate = NULL,
166 };
167
168 struct rumpfs_node {
169 struct genfs_node rn_gn;
170 struct vattr rn_va;
171 struct vnode *rn_vp;
172 char *rn_hostpath;
173 int rn_flags;
174 struct lockf *rn_lockf;
175
176 union {
177 struct { /* VREG */
178 int readfd;
179 int writefd;
180 uint64_t offset;
181 } reg;
182 struct {
183 void *data;
184 size_t dlen;
185 } reg_noet;
186 struct { /* VDIR */
187 LIST_HEAD(, rumpfs_dent) dents;
188 struct rumpfs_node *parent;
189 int flags;
190 } dir;
191 struct {
192 char *target;
193 size_t len;
194 } link;
195 } rn_u;
196 };
197 #define rn_readfd rn_u.reg.readfd
198 #define rn_writefd rn_u.reg.writefd
199 #define rn_offset rn_u.reg.offset
200 #define rn_data rn_u.reg_noet.data
201 #define rn_dlen rn_u.reg_noet.dlen
202 #define rn_dir rn_u.dir.dents
203 #define rn_parent rn_u.dir.parent
204 #define rn_linktarg rn_u.link.target
205 #define rn_linklen rn_u.link.len
206
207 #define RUMPNODE_CANRECLAIM 0x01
208 #define RUMPNODE_DIR_ET 0x02
209 #define RUMPNODE_DIR_ETSUBS 0x04
210 #define RUMPNODE_ET_PHONE_HOST 0x10
211
212 struct rumpfs_mount {
213 struct vnode *rfsmp_rvp;
214 };
215
216 #define INO_WHITEOUT 1
217 static int lastino = 2;
218 static kmutex_t reclock;
219
220 static struct rumpfs_node *makeprivate(enum vtype, dev_t, off_t, bool);
221
222 /*
223 * Extra Terrestrial stuff. We map a given key (pathname) to a file on
224 * the host FS. ET phones home only from the root node of rumpfs.
225 *
226 * When an etfs node is removed, a vnode potentially behind it is not
227 * immediately recycled.
228 */
229
230 struct etfs {
231 char et_key[MAXPATHLEN];
232 size_t et_keylen;
233 bool et_prefixkey;
234 bool et_removing;
235 devminor_t et_blkmin;
236
237 LIST_ENTRY(etfs) et_entries;
238
239 struct rumpfs_node *et_rn;
240 };
241 static kmutex_t etfs_lock;
242 static LIST_HEAD(, etfs) etfs_list = LIST_HEAD_INITIALIZER(etfs_list);
243
244 static enum vtype
245 ettype_to_vtype(enum rump_etfs_type et)
246 {
247 enum vtype vt;
248
249 switch (et) {
250 case RUMP_ETFS_REG:
251 vt = VREG;
252 break;
253 case RUMP_ETFS_BLK:
254 vt = VBLK;
255 break;
256 case RUMP_ETFS_CHR:
257 vt = VCHR;
258 break;
259 case RUMP_ETFS_DIR:
260 vt = VDIR;
261 break;
262 case RUMP_ETFS_DIR_SUBDIRS:
263 vt = VDIR;
264 break;
265 default:
266 panic("invalid et type: %d", et);
267 }
268
269 return vt;
270 }
271
272 static enum vtype
273 hft_to_vtype(int hft)
274 {
275 enum vtype vt;
276
277 switch (hft) {
278 case RUMPUSER_FT_OTHER:
279 vt = VNON;
280 break;
281 case RUMPUSER_FT_DIR:
282 vt = VDIR;
283 break;
284 case RUMPUSER_FT_REG:
285 vt = VREG;
286 break;
287 case RUMPUSER_FT_BLK:
288 vt = VBLK;
289 break;
290 case RUMPUSER_FT_CHR:
291 vt = VCHR;
292 break;
293 default:
294 vt = VNON;
295 break;
296 }
297
298 return vt;
299 }
300
301 static bool
302 etfs_find(const char *key, struct etfs **etp, bool forceprefix)
303 {
304 struct etfs *et;
305 size_t keylen = strlen(key);
306
307 KASSERT(mutex_owned(&etfs_lock));
308
309 LIST_FOREACH(et, &etfs_list, et_entries) {
310 if ((keylen == et->et_keylen || et->et_prefixkey || forceprefix)
311 && strncmp(key, et->et_key, et->et_keylen) == 0) {
312 if (etp)
313 *etp = et;
314 return true;
315 }
316 }
317
318 return false;
319 }
320
321 #define REGDIR(ftype) \
322 ((ftype) == RUMP_ETFS_DIR || (ftype) == RUMP_ETFS_DIR_SUBDIRS)
323 static int
324 doregister(const char *key, const char *hostpath,
325 enum rump_etfs_type ftype, uint64_t begin, uint64_t size)
326 {
327 char buf[9];
328 struct etfs *et;
329 struct rumpfs_node *rn;
330 uint64_t fsize;
331 dev_t rdev = NODEV;
332 devminor_t dmin = -1;
333 int hft, error;
334
335 if (key[0] != '/') {
336 return EINVAL;
337 }
338 while (key[0] == '/') {
339 key++;
340 }
341
342 if (rumpuser_getfileinfo(hostpath, &fsize, &hft, &error))
343 return error;
344
345 /* etfs directory requires a directory on the host */
346 if (REGDIR(ftype)) {
347 if (hft != RUMPUSER_FT_DIR)
348 return ENOTDIR;
349 if (begin != 0)
350 return EISDIR;
351 if (size != RUMP_ETFS_SIZE_ENDOFF)
352 return EISDIR;
353 size = fsize;
354 } else {
355 if (begin > fsize)
356 return EINVAL;
357 if (size == RUMP_ETFS_SIZE_ENDOFF)
358 size = fsize - begin;
359 if (begin + size > fsize)
360 return EINVAL;
361 }
362
363 if (ftype == RUMP_ETFS_BLK || ftype == RUMP_ETFS_CHR) {
364 error = rumpblk_register(hostpath, &dmin, begin, size);
365 if (error != 0) {
366 return error;
367 }
368 rdev = makedev(RUMPBLK_DEVMAJOR, dmin);
369 }
370
371 et = kmem_alloc(sizeof(*et), KM_SLEEP);
372 strcpy(et->et_key, key);
373 et->et_keylen = strlen(et->et_key);
374 et->et_rn = rn = makeprivate(ettype_to_vtype(ftype), rdev, size, true);
375 et->et_removing = false;
376 et->et_blkmin = dmin;
377
378 rn->rn_flags |= RUMPNODE_ET_PHONE_HOST;
379
380 if (ftype == RUMP_ETFS_REG || REGDIR(ftype) || et->et_blkmin != -1) {
381 size_t len = strlen(hostpath)+1;
382
383 rn->rn_hostpath = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
384 memcpy(rn->rn_hostpath, hostpath, len);
385 rn->rn_offset = begin;
386 }
387
388 if (REGDIR(ftype)) {
389 rn->rn_flags |= RUMPNODE_DIR_ET;
390 et->et_prefixkey = true;
391 } else {
392 et->et_prefixkey = false;
393 }
394
395 if (ftype == RUMP_ETFS_DIR_SUBDIRS)
396 rn->rn_flags |= RUMPNODE_DIR_ETSUBS;
397
398 mutex_enter(&etfs_lock);
399 if (etfs_find(key, NULL, REGDIR(ftype))) {
400 mutex_exit(&etfs_lock);
401 if (et->et_blkmin != -1)
402 rumpblk_deregister(hostpath);
403 if (et->et_rn->rn_hostpath != NULL)
404 free(et->et_rn->rn_hostpath, M_TEMP);
405 kmem_free(et->et_rn, sizeof(*et->et_rn));
406 kmem_free(et, sizeof(*et));
407 return EEXIST;
408 }
409 LIST_INSERT_HEAD(&etfs_list, et, et_entries);
410 mutex_exit(&etfs_lock);
411
412 if (ftype == RUMP_ETFS_BLK) {
413 format_bytes(buf, sizeof(buf), size);
414 aprint_verbose("/%s: hostpath %s (%s)\n", key, hostpath, buf);
415 }
416
417 return 0;
418 }
419 #undef REGDIR
420
421 int
422 rump_etfs_register(const char *key, const char *hostpath,
423 enum rump_etfs_type ftype)
424 {
425
426 return doregister(key, hostpath, ftype, 0, RUMP_ETFS_SIZE_ENDOFF);
427 }
428
429 int
430 rump_etfs_register_withsize(const char *key, const char *hostpath,
431 enum rump_etfs_type ftype, uint64_t begin, uint64_t size)
432 {
433
434 return doregister(key, hostpath, ftype, begin, size);
435 }
436
437 /* remove etfs mapping. caller's responsibility to make sure it's not in use */
438 int
439 rump_etfs_remove(const char *key)
440 {
441 struct etfs *et;
442 size_t keylen;
443 int rv;
444
445 if (key[0] != '/') {
446 return EINVAL;
447 }
448 while (key[0] == '/') {
449 key++;
450 }
451
452 keylen = strlen(key);
453
454 mutex_enter(&etfs_lock);
455 LIST_FOREACH(et, &etfs_list, et_entries) {
456 if (keylen == et->et_keylen && strcmp(et->et_key, key) == 0) {
457 if (et->et_removing)
458 et = NULL;
459 else
460 et->et_removing = true;
461 break;
462 }
463 }
464 mutex_exit(&etfs_lock);
465 if (!et)
466 return ENOENT;
467
468 /*
469 * ok, we know what we want to remove and have signalled there
470 * actually are men at work. first, unregister from rumpblk
471 */
472 if (et->et_blkmin != -1) {
473 rv = rumpblk_deregister(et->et_rn->rn_hostpath);
474 } else {
475 rv = 0;
476 }
477 KASSERT(rv == 0);
478
479 /* then do the actual removal */
480 mutex_enter(&etfs_lock);
481 LIST_REMOVE(et, et_entries);
482 mutex_exit(&etfs_lock);
483
484 /* node is unreachable, safe to nuke all device copies */
485 if (et->et_blkmin != -1) {
486 vdevgone(RUMPBLK_DEVMAJOR, et->et_blkmin, et->et_blkmin, VBLK);
487 } else {
488 struct vnode *vp;
489
490 mutex_enter(&reclock);
491 if ((vp = et->et_rn->rn_vp) != NULL)
492 mutex_enter(&vp->v_interlock);
493 mutex_exit(&reclock);
494 if (vp && vget(vp, 0) == 0)
495 vgone(vp);
496 }
497
498 if (et->et_rn->rn_hostpath != NULL)
499 free(et->et_rn->rn_hostpath, M_TEMP);
500 kmem_free(et->et_rn, sizeof(*et->et_rn));
501 kmem_free(et, sizeof(*et));
502
503 return 0;
504 }
505
506 /*
507 * rumpfs
508 */
509
510 static struct rumpfs_node *
511 makeprivate(enum vtype vt, dev_t rdev, off_t size, bool et)
512 {
513 struct rumpfs_node *rn;
514 struct vattr *va;
515 struct timespec ts;
516
517 rn = kmem_zalloc(sizeof(*rn), KM_SLEEP);
518
519 switch (vt) {
520 case VDIR:
521 LIST_INIT(&rn->rn_dir);
522 break;
523 case VREG:
524 if (et) {
525 rn->rn_readfd = -1;
526 rn->rn_writefd = -1;
527 }
528 break;
529 default:
530 break;
531 }
532
533 nanotime(&ts);
534
535 va = &rn->rn_va;
536 va->va_type = vt;
537 va->va_mode = 0755;
538 if (vt == VDIR)
539 va->va_nlink = 2;
540 else
541 va->va_nlink = 1;
542 va->va_uid = 0;
543 va->va_gid = 0;
544 va->va_fsid =
545 va->va_fileid = atomic_inc_uint_nv(&lastino);
546 va->va_size = size;
547 va->va_blocksize = 512;
548 va->va_atime = ts;
549 va->va_mtime = ts;
550 va->va_ctime = ts;
551 va->va_birthtime = ts;
552 va->va_gen = 0;
553 va->va_flags = 0;
554 va->va_rdev = rdev;
555 va->va_bytes = 512;
556 va->va_filerev = 0;
557 va->va_vaflags = 0;
558
559 return rn;
560 }
561
562 static int
563 makevnode(struct mount *mp, struct rumpfs_node *rn, struct vnode **vpp)
564 {
565 struct vnode *vp;
566 int (**vpops)(void *);
567 struct vattr *va = &rn->rn_va;
568 int rv;
569
570 KASSERT(!mutex_owned(&reclock));
571
572 if (va->va_type == VCHR || va->va_type == VBLK) {
573 vpops = rump_specop_p;
574 } else {
575 vpops = rump_vnodeop_p;
576 }
577
578 rv = getnewvnode(VT_RUMP, mp, vpops, &vp);
579 if (rv)
580 return rv;
581
582 vp->v_size = vp->v_writesize = va->va_size;
583 vp->v_type = va->va_type;
584
585 if (vpops == rump_specop_p) {
586 spec_node_init(vp, va->va_rdev);
587 }
588 vp->v_data = rn;
589
590 genfs_node_init(vp, &rumpfs_genfsops);
591 vn_lock(vp, LK_RETRY | LK_EXCLUSIVE);
592 mutex_enter(&reclock);
593 rn->rn_vp = vp;
594 mutex_exit(&reclock);
595
596 *vpp = vp;
597
598 return 0;
599 }
600
601
602 static void
603 makedir(struct rumpfs_node *rnd,
604 struct componentname *cnp, struct rumpfs_node *rn)
605 {
606 struct rumpfs_dent *rdent;
607
608 rdent = kmem_alloc(sizeof(*rdent), KM_SLEEP);
609 rdent->rd_name = kmem_alloc(cnp->cn_namelen+1, KM_SLEEP);
610 rdent->rd_node = rn;
611 strlcpy(rdent->rd_name, cnp->cn_nameptr, cnp->cn_namelen+1);
612 rdent->rd_namelen = strlen(rdent->rd_name);
613
614 LIST_INSERT_HEAD(&rnd->rn_dir, rdent, rd_entries);
615 }
616
617 static void
618 freedir(struct rumpfs_node *rnd, struct componentname *cnp)
619 {
620 struct rumpfs_dent *rd = NULL;
621
622 LIST_FOREACH(rd, &rnd->rn_dir, rd_entries) {
623 if (rd->rd_namelen == cnp->cn_namelen &&
624 strncmp(rd->rd_name, cnp->cn_nameptr,
625 cnp->cn_namelen) == 0)
626 break;
627 }
628 if (rd == NULL)
629 panic("could not find directory entry: %s", cnp->cn_nameptr);
630
631 if (cnp->cn_flags & DOWHITEOUT) {
632 rd->rd_node = RUMPFS_WHITEOUT;
633 } else {
634 LIST_REMOVE(rd, rd_entries);
635 kmem_free(rd->rd_name, rd->rd_namelen+1);
636 kmem_free(rd, sizeof(*rd));
637 }
638 }
639
640 /*
641 * Simple lookup for rump file systems.
642 *
643 * uhm, this is twisted. C F C C, hope of C C F C looming
644 */
645 static int
646 rump_vop_lookup(void *v)
647 {
648 struct vop_lookup_args /* {
649 struct vnode *a_dvp;
650 struct vnode **a_vpp;
651 struct componentname *a_cnp;
652 }; */ *ap = v;
653 struct componentname *cnp = ap->a_cnp;
654 struct vnode *dvp = ap->a_dvp;
655 struct vnode **vpp = ap->a_vpp;
656 struct vnode *vp;
657 struct rumpfs_node *rnd = dvp->v_data, *rn;
658 struct rumpfs_dent *rd = NULL;
659 struct etfs *et;
660 bool dotdot = (cnp->cn_flags & ISDOTDOT) != 0;
661 int rv = 0;
662
663 *vpp = NULL;
664
665 if ((cnp->cn_flags & ISLASTCN)
666 && (dvp->v_mount->mnt_flag & MNT_RDONLY)
667 && (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
668 return EROFS;
669
670 /* check for dot, return directly if the case */
671 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
672 vref(dvp);
673 *vpp = dvp;
674 return 0;
675 }
676
677 /* we don't do rename */
678 if (!(((cnp->cn_flags & ISLASTCN) == 0) || (cnp->cn_nameiop != RENAME)))
679 return EOPNOTSUPP;
680
681 /* check for etfs */
682 if (dvp == rootvnode &&
683 (cnp->cn_nameiop == LOOKUP || cnp->cn_nameiop == CREATE)) {
684 bool found;
685 mutex_enter(&etfs_lock);
686 found = etfs_find(cnp->cn_nameptr, &et, false);
687 mutex_exit(&etfs_lock);
688
689 if (found) {
690 rn = et->et_rn;
691 cnp->cn_consume += et->et_keylen - cnp->cn_namelen;
692 if (rn->rn_va.va_type != VDIR)
693 cnp->cn_flags &= ~REQUIREDIR;
694 goto getvnode;
695 }
696 }
697
698 if (rnd->rn_flags & RUMPNODE_DIR_ET) {
699 uint64_t fsize;
700 char *newpath;
701 size_t newpathlen;
702 int hft, error;
703
704 if (dotdot)
705 return EOPNOTSUPP;
706
707 newpathlen = strlen(rnd->rn_hostpath) + 1 + cnp->cn_namelen + 1;
708 newpath = malloc(newpathlen, M_TEMP, M_WAITOK);
709
710 strlcpy(newpath, rnd->rn_hostpath, newpathlen);
711 strlcat(newpath, "/", newpathlen);
712 strlcat(newpath, cnp->cn_nameptr, newpathlen);
713
714 if (rumpuser_getfileinfo(newpath, &fsize, &hft, &error)) {
715 free(newpath, M_TEMP);
716 return error;
717 }
718
719 /* allow only dirs and regular files */
720 if (hft != RUMPUSER_FT_REG && hft != RUMPUSER_FT_DIR) {
721 free(newpath, M_TEMP);
722 return ENOENT;
723 }
724
725 rn = makeprivate(hft_to_vtype(hft), NODEV, fsize, true);
726 rn->rn_flags |= RUMPNODE_CANRECLAIM;
727 if (rnd->rn_flags & RUMPNODE_DIR_ETSUBS) {
728 rn->rn_flags |= RUMPNODE_DIR_ET | RUMPNODE_DIR_ETSUBS;
729 rn->rn_flags |= RUMPNODE_ET_PHONE_HOST;
730 }
731 rn->rn_hostpath = newpath;
732
733 goto getvnode;
734 } else {
735 if (dotdot) {
736 if ((rn = rnd->rn_parent) != NULL)
737 goto getvnode;
738 } else {
739 LIST_FOREACH(rd, &rnd->rn_dir, rd_entries) {
740 if (rd->rd_namelen == cnp->cn_namelen &&
741 strncmp(rd->rd_name, cnp->cn_nameptr,
742 cnp->cn_namelen) == 0)
743 break;
744 }
745 }
746 }
747
748 if (!rd && ((cnp->cn_flags & ISLASTCN) == 0||cnp->cn_nameiop != CREATE))
749 return ENOENT;
750
751 if (!rd && (cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) {
752 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
753 return EROFS;
754 return EJUSTRETURN;
755 }
756
757 if (RDENT_ISWHITEOUT(rd)) {
758 cnp->cn_flags |= ISWHITEOUT;
759 return ENOENT;
760 }
761
762 rn = rd->rd_node;
763
764 getvnode:
765 KASSERT(rn);
766 if (dotdot)
767 VOP_UNLOCK(dvp);
768 mutex_enter(&reclock);
769 if ((vp = rn->rn_vp)) {
770 mutex_enter(&vp->v_interlock);
771 mutex_exit(&reclock);
772 if (vget(vp, LK_EXCLUSIVE)) {
773 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
774 goto getvnode;
775 }
776 *vpp = vp;
777 } else {
778 mutex_exit(&reclock);
779 rv = makevnode(dvp->v_mount, rn, vpp);
780 }
781 if (dotdot)
782 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
783
784 return rv;
785 }
786
787 int
788 rump_vop_access(void *v)
789 {
790 struct vop_access_args /* {
791 const struct vnodeop_desc *a_desc;
792 struct vnode *a_vp;
793 int a_mode;
794 kauth_cred_t a_cred;
795 } */ *ap = v;
796 struct vnode *vp = ap->a_vp;
797 int mode = ap->a_mode;
798
799 if (mode & VWRITE) {
800 switch (vp->v_type) {
801 case VDIR:
802 case VLNK:
803 case VREG:
804 if ((vp->v_mount->mnt_flag & MNT_RDONLY))
805 return EROFS;
806 break;
807 default:
808 break;
809 }
810 }
811
812 return 0;
813 }
814
815 static int
816 rump_vop_getattr(void *v)
817 {
818 struct vop_getattr_args /* {
819 struct vnode *a_vp;
820 struct vattr *a_vap;
821 kauth_cred_t a_cred;
822 } */ *ap = v;
823 struct vnode *vp = ap->a_vp;
824 struct rumpfs_node *rn = vp->v_data;
825 struct vattr *vap = ap->a_vap;
826
827 memcpy(vap, &rn->rn_va, sizeof(struct vattr));
828 vap->va_size = vp->v_size;
829 return 0;
830 }
831
832 static int
833 rump_vop_setattr(void *v)
834 {
835 struct vop_getattr_args /* {
836 struct vnode *a_vp;
837 struct vattr *a_vap;
838 kauth_cred_t a_cred;
839 } */ *ap = v;
840 struct vnode *vp = ap->a_vp;
841 struct vattr *vap = ap->a_vap;
842 struct rumpfs_node *rn = vp->v_data;
843
844 #define SETIFVAL(a,t) if (vap->a != (t)VNOVAL) rn->rn_va.a = vap->a
845 SETIFVAL(va_mode, mode_t);
846 SETIFVAL(va_uid, uid_t);
847 SETIFVAL(va_gid, gid_t);
848 SETIFVAL(va_atime.tv_sec, time_t);
849 SETIFVAL(va_ctime.tv_sec, time_t);
850 SETIFVAL(va_mtime.tv_sec, time_t);
851 SETIFVAL(va_birthtime.tv_sec, time_t);
852 SETIFVAL(va_atime.tv_nsec, long);
853 SETIFVAL(va_ctime.tv_nsec, long);
854 SETIFVAL(va_mtime.tv_nsec, long);
855 SETIFVAL(va_birthtime.tv_nsec, long);
856 SETIFVAL(va_flags, u_long);
857 #undef SETIFVAL
858
859 if (vp->v_type == VREG &&
860 vap->va_size != VSIZENOTSET &&
861 vap->va_size != rn->rn_dlen) {
862 void *newdata;
863 size_t copylen, newlen;
864
865 newlen = vap->va_size;
866 newdata = rump_hypermalloc(newlen, 0, true, "rumpfs");
867
868 copylen = MIN(rn->rn_dlen, newlen);
869 memset(newdata, 0, newlen);
870 memcpy(newdata, rn->rn_data, copylen);
871 rump_hyperfree(rn->rn_data, rn->rn_dlen);
872
873 rn->rn_data = newdata;
874 rn->rn_dlen = newlen;
875 uvm_vnp_setsize(vp, newlen);
876 }
877 return 0;
878 }
879
880 static int
881 rump_vop_mkdir(void *v)
882 {
883 struct vop_mkdir_args /* {
884 struct vnode *a_dvp;
885 struct vnode **a_vpp;
886 struct componentname *a_cnp;
887 struct vattr *a_vap;
888 }; */ *ap = v;
889 struct vnode *dvp = ap->a_dvp;
890 struct vnode **vpp = ap->a_vpp;
891 struct componentname *cnp = ap->a_cnp;
892 struct rumpfs_node *rnd = dvp->v_data, *rn;
893 int rv = 0;
894
895 rn = makeprivate(VDIR, NODEV, DEV_BSIZE, false);
896 rn->rn_parent = rnd;
897 rv = makevnode(dvp->v_mount, rn, vpp);
898 if (rv)
899 goto out;
900
901 makedir(rnd, cnp, rn);
902
903 out:
904 vput(dvp);
905 return rv;
906 }
907
908 static int
909 rump_vop_rmdir(void *v)
910 {
911 struct vop_rmdir_args /* {
912 struct vnode *a_dvp;
913 struct vnode *a_vp;
914 struct componentname *a_cnp;
915 }; */ *ap = v;
916 struct vnode *dvp = ap->a_dvp;
917 struct vnode *vp = ap->a_vp;
918 struct componentname *cnp = ap->a_cnp;
919 struct rumpfs_node *rnd = dvp->v_data;
920 struct rumpfs_node *rn = vp->v_data;
921 int rv = 0;
922
923 if (!LIST_EMPTY(&rn->rn_dir)) {
924 rv = ENOTEMPTY;
925 goto out;
926 }
927
928 freedir(rnd, cnp);
929 rn->rn_flags |= RUMPNODE_CANRECLAIM;
930 rn->rn_parent = NULL;
931
932 out:
933 vput(dvp);
934 vput(vp);
935
936 return rv;
937 }
938
939 static int
940 rump_vop_remove(void *v)
941 {
942 struct vop_rmdir_args /* {
943 struct vnode *a_dvp;
944 struct vnode *a_vp;
945 struct componentname *a_cnp;
946 }; */ *ap = v;
947 struct vnode *dvp = ap->a_dvp;
948 struct vnode *vp = ap->a_vp;
949 struct componentname *cnp = ap->a_cnp;
950 struct rumpfs_node *rnd = dvp->v_data;
951 struct rumpfs_node *rn = vp->v_data;
952 int rv = 0;
953
954 if (rn->rn_flags & RUMPNODE_ET_PHONE_HOST)
955 return EOPNOTSUPP;
956
957 if (vp->v_type == VREG) {
958 rump_hyperfree(rn->rn_data, rn->rn_dlen);
959 }
960
961 freedir(rnd, cnp);
962 rn->rn_flags |= RUMPNODE_CANRECLAIM;
963
964 vput(dvp);
965 vput(vp);
966
967 return rv;
968 }
969
970 static int
971 rump_vop_mknod(void *v)
972 {
973 struct vop_mknod_args /* {
974 struct vnode *a_dvp;
975 struct vnode **a_vpp;
976 struct componentname *a_cnp;
977 struct vattr *a_vap;
978 }; */ *ap = v;
979 struct vnode *dvp = ap->a_dvp;
980 struct vnode **vpp = ap->a_vpp;
981 struct componentname *cnp = ap->a_cnp;
982 struct vattr *va = ap->a_vap;
983 struct rumpfs_node *rnd = dvp->v_data, *rn;
984 int rv;
985
986 rn = makeprivate(va->va_type, va->va_rdev, DEV_BSIZE, false);
987 rv = makevnode(dvp->v_mount, rn, vpp);
988 if (rv)
989 goto out;
990
991 makedir(rnd, cnp, rn);
992
993 out:
994 vput(dvp);
995 return rv;
996 }
997
998 static int
999 rump_vop_create(void *v)
1000 {
1001 struct vop_create_args /* {
1002 struct vnode *a_dvp;
1003 struct vnode **a_vpp;
1004 struct componentname *a_cnp;
1005 struct vattr *a_vap;
1006 }; */ *ap = v;
1007 struct vnode *dvp = ap->a_dvp;
1008 struct vnode **vpp = ap->a_vpp;
1009 struct componentname *cnp = ap->a_cnp;
1010 struct vattr *va = ap->a_vap;
1011 struct rumpfs_node *rnd = dvp->v_data, *rn;
1012 off_t newsize;
1013 int rv;
1014
1015 newsize = va->va_type == VSOCK ? DEV_BSIZE : 0;
1016 rn = makeprivate(va->va_type, NODEV, newsize, false);
1017 rv = makevnode(dvp->v_mount, rn, vpp);
1018 if (rv)
1019 goto out;
1020
1021 makedir(rnd, cnp, rn);
1022
1023 out:
1024 vput(dvp);
1025 return rv;
1026 }
1027
1028 static int
1029 rump_vop_symlink(void *v)
1030 {
1031 struct vop_symlink_args /* {
1032 struct vnode *a_dvp;
1033 struct vnode **a_vpp;
1034 struct componentname *a_cnp;
1035 struct vattr *a_vap;
1036 char *a_target;
1037 }; */ *ap = v;
1038 struct vnode *dvp = ap->a_dvp;
1039 struct vnode **vpp = ap->a_vpp;
1040 struct componentname *cnp = ap->a_cnp;
1041 struct rumpfs_node *rnd = dvp->v_data, *rn;
1042 const char *target = ap->a_target;
1043 size_t linklen;
1044 int rv;
1045
1046 linklen = strlen(target);
1047 KASSERT(linklen < MAXPATHLEN);
1048 rn = makeprivate(VLNK, NODEV, linklen, false);
1049 rv = makevnode(dvp->v_mount, rn, vpp);
1050 if (rv)
1051 goto out;
1052
1053 makedir(rnd, cnp, rn);
1054
1055 KASSERT(linklen < MAXPATHLEN);
1056 rn->rn_linktarg = PNBUF_GET();
1057 rn->rn_linklen = linklen;
1058 strcpy(rn->rn_linktarg, target);
1059
1060 out:
1061 vput(dvp);
1062 return rv;
1063 }
1064
1065 static int
1066 rump_vop_readlink(void *v)
1067 {
1068 struct vop_readlink_args /* {
1069 struct vnode *a_vp;
1070 struct uio *a_uio;
1071 kauth_cred_t a_cred;
1072 }; */ *ap = v;
1073 struct vnode *vp = ap->a_vp;
1074 struct rumpfs_node *rn = vp->v_data;
1075 struct uio *uio = ap->a_uio;
1076
1077 return uiomove(rn->rn_linktarg, rn->rn_linklen, uio);
1078 }
1079
1080 static int
1081 rump_vop_whiteout(void *v)
1082 {
1083 struct vop_whiteout_args /* {
1084 struct vnode *a_dvp;
1085 struct componentname *a_cnp;
1086 int a_flags;
1087 } */ *ap = v;
1088 struct vnode *dvp = ap->a_dvp;
1089 struct rumpfs_node *rnd = dvp->v_data;
1090 struct componentname *cnp = ap->a_cnp;
1091 int flags = ap->a_flags;
1092
1093 switch (flags) {
1094 case LOOKUP:
1095 break;
1096 case CREATE:
1097 makedir(rnd, cnp, RUMPFS_WHITEOUT);
1098 break;
1099 case DELETE:
1100 cnp->cn_flags &= ~DOWHITEOUT; /* cargo culting never fails ? */
1101 freedir(rnd, cnp);
1102 break;
1103 default:
1104 panic("unknown whiteout op %d", flags);
1105 }
1106
1107 return 0;
1108 }
1109
1110 static int
1111 rump_vop_open(void *v)
1112 {
1113 struct vop_open_args /* {
1114 struct vnode *a_vp;
1115 int a_mode;
1116 kauth_cred_t a_cred;
1117 } */ *ap = v;
1118 struct vnode *vp = ap->a_vp;
1119 struct rumpfs_node *rn = vp->v_data;
1120 int mode = ap->a_mode;
1121 int error = EINVAL;
1122
1123 if (vp->v_type != VREG || (rn->rn_flags & RUMPNODE_ET_PHONE_HOST) == 0)
1124 return 0;
1125
1126 if (mode & FREAD) {
1127 if (rn->rn_readfd != -1)
1128 return 0;
1129 rn->rn_readfd = rumpuser_open(rn->rn_hostpath,
1130 O_RDONLY, &error);
1131 }
1132
1133 if (mode & FWRITE) {
1134 if (rn->rn_writefd != -1)
1135 return 0;
1136 rn->rn_writefd = rumpuser_open(rn->rn_hostpath,
1137 O_WRONLY, &error);
1138 }
1139
1140 return error;
1141 }
1142
1143 /* simple readdir. event omits dotstuff and periods */
1144 static int
1145 rump_vop_readdir(void *v)
1146 {
1147 struct vop_readdir_args /* {
1148 struct vnode *a_vp;
1149 struct uio *a_uio;
1150 kauth_cred_t a_cred;
1151 int *a_eofflag;
1152 off_t **a_cookies;
1153 int *a_ncookies;
1154 } */ *ap = v;
1155 struct vnode *vp = ap->a_vp;
1156 struct uio *uio = ap->a_uio;
1157 struct rumpfs_node *rnd = vp->v_data;
1158 struct rumpfs_dent *rdent;
1159 unsigned i;
1160 int rv = 0;
1161
1162 /* seek to current entry */
1163 for (i = 0, rdent = LIST_FIRST(&rnd->rn_dir);
1164 (i < uio->uio_offset) && rdent;
1165 i++, rdent = LIST_NEXT(rdent, rd_entries))
1166 continue;
1167 if (!rdent)
1168 goto out;
1169
1170 /* copy entries */
1171 for (; rdent && uio->uio_resid > 0;
1172 rdent = LIST_NEXT(rdent, rd_entries), i++) {
1173 struct dirent dent;
1174
1175 strlcpy(dent.d_name, rdent->rd_name, sizeof(dent.d_name));
1176 dent.d_namlen = strlen(dent.d_name);
1177 dent.d_reclen = _DIRENT_RECLEN(&dent, dent.d_namlen);
1178
1179 if (__predict_false(RDENT_ISWHITEOUT(rdent))) {
1180 dent.d_fileno = INO_WHITEOUT;
1181 dent.d_type = DT_WHT;
1182 } else {
1183 dent.d_fileno = rdent->rd_node->rn_va.va_fileid;
1184 dent.d_type = vtype2dt(rdent->rd_node->rn_va.va_type);
1185 }
1186
1187 if (uio->uio_resid < dent.d_reclen) {
1188 i--;
1189 break;
1190 }
1191
1192 rv = uiomove(&dent, dent.d_reclen, uio);
1193 if (rv) {
1194 i--;
1195 break;
1196 }
1197 }
1198
1199 out:
1200 if (ap->a_cookies) {
1201 *ap->a_ncookies = 0;
1202 *ap->a_cookies = NULL;
1203 }
1204 if (rdent)
1205 *ap->a_eofflag = 0;
1206 else
1207 *ap->a_eofflag = 1;
1208 uio->uio_offset = i;
1209
1210 return rv;
1211 }
1212
1213 static int
1214 etread(struct rumpfs_node *rn, struct uio *uio)
1215 {
1216 uint8_t *buf;
1217 size_t bufsize;
1218 ssize_t n;
1219 int error = 0;
1220
1221 bufsize = uio->uio_resid;
1222 if (bufsize == 0)
1223 return 0;
1224 buf = kmem_alloc(bufsize, KM_SLEEP);
1225 if ((n = rumpuser_pread(rn->rn_readfd, buf, bufsize,
1226 uio->uio_offset + rn->rn_offset, &error)) == -1)
1227 goto out;
1228 KASSERT(n <= bufsize);
1229 error = uiomove(buf, n, uio);
1230
1231 out:
1232 kmem_free(buf, bufsize);
1233 return error;
1234
1235 }
1236
1237 static int
1238 rump_vop_read(void *v)
1239 {
1240 struct vop_read_args /* {
1241 struct vnode *a_vp;
1242 struct uio *a_uio;
1243 int ioflags a_ioflag;
1244 kauth_cred_t a_cred;
1245 }; */ *ap = v;
1246 struct vnode *vp = ap->a_vp;
1247 struct rumpfs_node *rn = vp->v_data;
1248 struct uio *uio = ap->a_uio;
1249 const int advice = IO_ADV_DECODE(ap->a_ioflag);
1250 off_t chunk;
1251 int error = 0;
1252
1253 /* et op? */
1254 if (rn->rn_flags & RUMPNODE_ET_PHONE_HOST)
1255 return etread(rn, uio);
1256
1257 /* otherwise, it's off to ubc with us */
1258 while (uio->uio_resid > 0) {
1259 chunk = MIN(uio->uio_resid, (off_t)rn->rn_dlen-uio->uio_offset);
1260 if (chunk == 0)
1261 break;
1262 error = ubc_uiomove(&vp->v_uobj, uio, chunk, advice,
1263 UBC_READ | UBC_PARTIALOK | UBC_WANT_UNMAP(vp)?UBC_UNMAP:0);
1264 if (error)
1265 break;
1266 }
1267
1268 return error;
1269 }
1270
1271 static int
1272 etwrite(struct rumpfs_node *rn, struct uio *uio)
1273 {
1274 uint8_t *buf;
1275 size_t bufsize;
1276 ssize_t n;
1277 int error = 0;
1278
1279 bufsize = uio->uio_resid;
1280 if (bufsize == 0)
1281 return 0;
1282 buf = kmem_alloc(bufsize, KM_SLEEP);
1283 error = uiomove(buf, bufsize, uio);
1284 if (error)
1285 goto out;
1286 KASSERT(uio->uio_resid == 0);
1287 n = rumpuser_pwrite(rn->rn_writefd, buf, bufsize,
1288 (uio->uio_offset-bufsize) + rn->rn_offset, &error);
1289 if (n >= 0) {
1290 KASSERT(n <= bufsize);
1291 uio->uio_resid = bufsize - n;
1292 }
1293
1294 out:
1295 kmem_free(buf, bufsize);
1296 return error;
1297 }
1298
1299 static int
1300 rump_vop_write(void *v)
1301 {
1302 struct vop_read_args /* {
1303 struct vnode *a_vp;
1304 struct uio *a_uio;
1305 int ioflags a_ioflag;
1306 kauth_cred_t a_cred;
1307 }; */ *ap = v;
1308 struct vnode *vp = ap->a_vp;
1309 struct rumpfs_node *rn = vp->v_data;
1310 struct uio *uio = ap->a_uio;
1311 const int advice = IO_ADV_DECODE(ap->a_ioflag);
1312 void *olddata;
1313 size_t oldlen, newlen;
1314 off_t chunk;
1315 int error = 0;
1316 bool allocd = false;
1317
1318 if (ap->a_ioflag & IO_APPEND)
1319 uio->uio_offset = vp->v_size;
1320
1321 /* consult et? */
1322 if (rn->rn_flags & RUMPNODE_ET_PHONE_HOST)
1323 return etwrite(rn, uio);
1324
1325 /*
1326 * Otherwise, it's a case of ubcmove.
1327 */
1328
1329 /*
1330 * First, make sure we have enough storage.
1331 *
1332 * No, you don't need to tell me it's not very efficient.
1333 * No, it doesn't really support sparse files, just fakes it.
1334 */
1335 newlen = uio->uio_offset + uio->uio_resid;
1336 oldlen = 0; /* XXXgcc */
1337 olddata = NULL;
1338 if (rn->rn_dlen < newlen) {
1339 oldlen = rn->rn_dlen;
1340 olddata = rn->rn_data;
1341
1342 rn->rn_data = rump_hypermalloc(newlen, 0, true, "rumpfs");
1343 rn->rn_dlen = newlen;
1344 memset(rn->rn_data, 0, newlen);
1345 memcpy(rn->rn_data, olddata, oldlen);
1346 allocd = true;
1347 uvm_vnp_setsize(vp, newlen);
1348 }
1349
1350 /* ok, we have enough stooorage. write */
1351 while (uio->uio_resid > 0) {
1352 chunk = MIN(uio->uio_resid, (off_t)rn->rn_dlen-uio->uio_offset);
1353 if (chunk == 0)
1354 break;
1355 error = ubc_uiomove(&vp->v_uobj, uio, chunk, advice,
1356 UBC_WRITE | UBC_PARTIALOK | UBC_WANT_UNMAP(vp)?UBC_UNMAP:0);
1357 if (error)
1358 break;
1359 }
1360
1361 if (allocd) {
1362 if (error) {
1363 rump_hyperfree(rn->rn_data, newlen);
1364 rn->rn_data = olddata;
1365 rn->rn_dlen = oldlen;
1366 uvm_vnp_setsize(vp, oldlen);
1367 } else {
1368 rump_hyperfree(olddata, oldlen);
1369 }
1370 }
1371
1372 return error;
1373 }
1374
1375 static int
1376 rump_vop_bmap(void *v)
1377 {
1378 struct vop_bmap_args /* {
1379 struct vnode *a_vp;
1380 daddr_t a_bn;
1381 struct vnode **a_vpp;
1382 daddr_t *a_bnp;
1383 int *a_runp;
1384 } */ *ap = v;
1385
1386 /* 1:1 mapping */
1387 if (ap->a_vpp)
1388 *ap->a_vpp = ap->a_vp;
1389 if (ap->a_bnp)
1390 *ap->a_bnp = ap->a_bn;
1391 if (ap->a_runp)
1392 *ap->a_runp = 16;
1393
1394 return 0;
1395 }
1396
1397 static int
1398 rump_vop_strategy(void *v)
1399 {
1400 struct vop_strategy_args /* {
1401 struct vnode *a_vp;
1402 struct buf *a_bp;
1403 } */ *ap = v;
1404 struct vnode *vp = ap->a_vp;
1405 struct rumpfs_node *rn = vp->v_data;
1406 struct buf *bp = ap->a_bp;
1407 off_t copylen, copyoff;
1408 int error;
1409
1410 if (vp->v_type != VREG || rn->rn_flags & RUMPNODE_ET_PHONE_HOST) {
1411 error = EINVAL;
1412 goto out;
1413 }
1414
1415 copyoff = bp->b_blkno << DEV_BSHIFT;
1416 copylen = MIN(rn->rn_dlen - copyoff, bp->b_bcount);
1417 if (BUF_ISWRITE(bp)) {
1418 memcpy((uint8_t *)rn->rn_data + copyoff, bp->b_data, copylen);
1419 } else {
1420 memset((uint8_t*)bp->b_data + copylen, 0, bp->b_bcount-copylen);
1421 memcpy(bp->b_data, (uint8_t *)rn->rn_data + copyoff, copylen);
1422 }
1423 bp->b_resid = 0;
1424 error = 0;
1425
1426 out:
1427 bp->b_error = error;
1428 biodone(bp);
1429 return 0;
1430 }
1431
1432 static int
1433 rump_vop_pathconf(void *v)
1434 {
1435 struct vop_pathconf_args /* {
1436 struct vnode *a_vp;
1437 int a_name;
1438 register_t *a_retval;
1439 }; */ *ap = v;
1440 int name = ap->a_name;
1441 register_t *retval = ap->a_retval;
1442
1443 switch (name) {
1444 case _PC_LINK_MAX:
1445 *retval = LINK_MAX;
1446 return 0;
1447 case _PC_NAME_MAX:
1448 *retval = NAME_MAX;
1449 return 0;
1450 case _PC_PATH_MAX:
1451 *retval = PATH_MAX;
1452 return 0;
1453 case _PC_PIPE_BUF:
1454 *retval = PIPE_BUF;
1455 return 0;
1456 case _PC_CHOWN_RESTRICTED:
1457 *retval = 1;
1458 return 0;
1459 case _PC_NO_TRUNC:
1460 *retval = 1;
1461 return 0;
1462 case _PC_SYNC_IO:
1463 *retval = 1;
1464 return 0;
1465 case _PC_FILESIZEBITS:
1466 *retval = 43; /* this one goes to 11 */
1467 return 0;
1468 case _PC_SYMLINK_MAX:
1469 *retval = MAXPATHLEN;
1470 return 0;
1471 case _PC_2_SYMLINKS:
1472 *retval = 1;
1473 return 0;
1474 default:
1475 return EINVAL;
1476 }
1477 }
1478
1479 static int
1480 rump_vop_success(void *v)
1481 {
1482
1483 return 0;
1484 }
1485
1486 static int
1487 rump_vop_inactive(void *v)
1488 {
1489 struct vop_inactive_args /* {
1490 struct vnode *a_vp;
1491 bool *a_recycle;
1492 } */ *ap = v;
1493 struct vnode *vp = ap->a_vp;
1494 struct rumpfs_node *rn = vp->v_data;
1495 int error;
1496
1497 if (rn->rn_flags & RUMPNODE_ET_PHONE_HOST && vp->v_type == VREG) {
1498 if (rn->rn_readfd != -1) {
1499 rumpuser_close(rn->rn_readfd, &error);
1500 rn->rn_readfd = -1;
1501 }
1502 if (rn->rn_writefd != -1) {
1503 rumpuser_close(rn->rn_writefd, &error);
1504 rn->rn_writefd = -1;
1505 }
1506 }
1507 *ap->a_recycle = (rn->rn_flags & RUMPNODE_CANRECLAIM) ? true : false;
1508
1509 VOP_UNLOCK(vp);
1510 return 0;
1511 }
1512
1513 static int
1514 rump_vop_reclaim(void *v)
1515 {
1516 struct vop_reclaim_args /* {
1517 struct vnode *a_vp;
1518 } */ *ap = v;
1519 struct vnode *vp = ap->a_vp;
1520 struct rumpfs_node *rn = vp->v_data;
1521
1522 mutex_enter(&reclock);
1523 rn->rn_vp = NULL;
1524 mutex_exit(&reclock);
1525 genfs_node_destroy(vp);
1526 vp->v_data = NULL;
1527
1528 if (rn->rn_flags & RUMPNODE_CANRECLAIM) {
1529 if (vp->v_type == VLNK)
1530 PNBUF_PUT(rn->rn_linktarg);
1531 if (rn->rn_hostpath)
1532 free(rn->rn_hostpath, M_TEMP);
1533 kmem_free(rn, sizeof(*rn));
1534 }
1535
1536 return 0;
1537 }
1538
1539 static int
1540 rump_vop_spec(void *v)
1541 {
1542 struct vop_generic_args *ap = v;
1543 int (**opvec)(void *);
1544
1545 switch (ap->a_desc->vdesc_offset) {
1546 case VOP_ACCESS_DESCOFFSET:
1547 case VOP_GETATTR_DESCOFFSET:
1548 case VOP_SETATTR_DESCOFFSET:
1549 case VOP_LOCK_DESCOFFSET:
1550 case VOP_UNLOCK_DESCOFFSET:
1551 case VOP_ISLOCKED_DESCOFFSET:
1552 case VOP_RECLAIM_DESCOFFSET:
1553 opvec = rump_vnodeop_p;
1554 break;
1555 default:
1556 opvec = spec_vnodeop_p;
1557 break;
1558 }
1559
1560 return VOCALL(opvec, ap->a_desc->vdesc_offset, v);
1561 }
1562
1563 static int
1564 rump_vop_advlock(void *v)
1565 {
1566 struct vop_advlock_args /* {
1567 const struct vnodeop_desc *a_desc;
1568 struct vnode *a_vp;
1569 void *a_id;
1570 int a_op;
1571 struct flock *a_fl;
1572 int a_flags;
1573 } */ *ap = v;
1574 struct vnode *vp = ap->a_vp;
1575 struct rumpfs_node *rn = vp->v_data;
1576
1577 return lf_advlock(ap, &rn->rn_lockf, vp->v_size);
1578 }
1579
1580 /*
1581 * Begin vfs-level stuff
1582 */
1583
1584 VFS_PROTOS(rumpfs);
1585 struct vfsops rumpfs_vfsops = {
1586 .vfs_name = MOUNT_RUMPFS,
1587 .vfs_min_mount_data = 0,
1588 .vfs_mount = rumpfs_mount,
1589 .vfs_start = (void *)nullop,
1590 .vfs_unmount = rumpfs_unmount,
1591 .vfs_root = rumpfs_root,
1592 .vfs_quotactl = (void *)eopnotsupp,
1593 .vfs_statvfs = genfs_statvfs,
1594 .vfs_sync = (void *)nullop,
1595 .vfs_vget = rumpfs_vget,
1596 .vfs_fhtovp = (void *)eopnotsupp,
1597 .vfs_vptofh = (void *)eopnotsupp,
1598 .vfs_init = rumpfs_init,
1599 .vfs_reinit = NULL,
1600 .vfs_done = rumpfs_done,
1601 .vfs_mountroot = rumpfs_mountroot,
1602 .vfs_snapshot = (void *)eopnotsupp,
1603 .vfs_extattrctl = (void *)eopnotsupp,
1604 .vfs_suspendctl = (void *)eopnotsupp,
1605 .vfs_renamelock_enter = genfs_renamelock_enter,
1606 .vfs_renamelock_exit = genfs_renamelock_exit,
1607 .vfs_opv_descs = rump_opv_descs,
1608 /* vfs_refcount */
1609 /* vfs_list */
1610 };
1611
1612 static int
1613 rumpfs_mountfs(struct mount *mp)
1614 {
1615 struct rumpfs_mount *rfsmp;
1616 struct rumpfs_node *rn;
1617 int error;
1618
1619 rfsmp = kmem_alloc(sizeof(*rfsmp), KM_SLEEP);
1620
1621 rn = makeprivate(VDIR, NODEV, DEV_BSIZE, false);
1622 rn->rn_parent = rn;
1623 if ((error = makevnode(mp, rn, &rfsmp->rfsmp_rvp)) != 0)
1624 return error;
1625
1626 rfsmp->rfsmp_rvp->v_vflag |= VV_ROOT;
1627 VOP_UNLOCK(rfsmp->rfsmp_rvp);
1628
1629 mp->mnt_data = rfsmp;
1630 mp->mnt_stat.f_namemax = MAXNAMLEN;
1631 mp->mnt_stat.f_iosize = 512;
1632 mp->mnt_flag |= MNT_LOCAL;
1633 mp->mnt_iflag |= IMNT_MPSAFE | IMNT_CAN_RWTORO;
1634 mp->mnt_fs_bshift = DEV_BSHIFT;
1635 vfs_getnewfsid(mp);
1636
1637 return 0;
1638 }
1639
1640 int
1641 rumpfs_mount(struct mount *mp, const char *mntpath, void *arg, size_t *alen)
1642 {
1643 int error;
1644
1645 if (mp->mnt_flag & MNT_UPDATE) {
1646 return 0;
1647 }
1648
1649 error = set_statvfs_info(mntpath, UIO_USERSPACE, "rumpfs", UIO_SYSSPACE,
1650 mp->mnt_op->vfs_name, mp, curlwp);
1651 if (error)
1652 return error;
1653
1654 return rumpfs_mountfs(mp);
1655 }
1656
1657 int
1658 rumpfs_unmount(struct mount *mp, int mntflags)
1659 {
1660 struct rumpfs_mount *rfsmp = mp->mnt_data;
1661 int flags = 0, error;
1662
1663 if (panicstr || mntflags & MNT_FORCE)
1664 flags |= FORCECLOSE;
1665
1666 if ((error = vflush(mp, rfsmp->rfsmp_rvp, flags)) != 0)
1667 return error;
1668 vgone(rfsmp->rfsmp_rvp); /* XXX */
1669
1670 kmem_free(rfsmp, sizeof(*rfsmp));
1671
1672 return 0;
1673 }
1674
1675 int
1676 rumpfs_root(struct mount *mp, struct vnode **vpp)
1677 {
1678 struct rumpfs_mount *rfsmp = mp->mnt_data;
1679
1680 vref(rfsmp->rfsmp_rvp);
1681 vn_lock(rfsmp->rfsmp_rvp, LK_EXCLUSIVE | LK_RETRY);
1682 *vpp = rfsmp->rfsmp_rvp;
1683 return 0;
1684 }
1685
1686 int
1687 rumpfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1688 {
1689
1690 return EOPNOTSUPP;
1691 }
1692
1693 void
1694 rumpfs_init()
1695 {
1696
1697 CTASSERT(RUMP_ETFS_SIZE_ENDOFF == RUMPBLK_SIZENOTSET);
1698
1699 mutex_init(&reclock, MUTEX_DEFAULT, IPL_NONE);
1700 mutex_init(&etfs_lock, MUTEX_DEFAULT, IPL_NONE);
1701 }
1702
1703 void
1704 rumpfs_done()
1705 {
1706
1707 mutex_destroy(&reclock);
1708 mutex_destroy(&etfs_lock);
1709 }
1710
1711 int
1712 rumpfs_mountroot()
1713 {
1714 struct mount *mp;
1715 int error;
1716
1717 if ((error = vfs_rootmountalloc(MOUNT_RUMPFS, "rootdev", &mp)) != 0) {
1718 vrele(rootvp);
1719 return error;
1720 }
1721
1722 if ((error = rumpfs_mountfs(mp)) != 0)
1723 panic("mounting rootfs failed: %d", error);
1724
1725 mutex_enter(&mountlist_lock);
1726 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1727 mutex_exit(&mountlist_lock);
1728
1729 error = set_statvfs_info("/", UIO_SYSSPACE, "rumpfs", UIO_SYSSPACE,
1730 mp->mnt_op->vfs_name, mp, curlwp);
1731 if (error)
1732 panic("set_statvfs_info failed for rootfs: %d", error);
1733
1734 mp->mnt_flag &= ~MNT_RDONLY;
1735 vfs_unbusy(mp, false, NULL);
1736
1737 return 0;
1738 }
1739