rumpfs.c revision 1.90 1 /* $NetBSD: rumpfs.c,v 1.90 2011/02/02 14:41:55 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2009, 2010 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.90 2011/02/02 14:41:55 pooka 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 && cnp->cn_nameiop == LOOKUP) {
683 bool found;
684 mutex_enter(&etfs_lock);
685 found = etfs_find(cnp->cn_nameptr, &et, false);
686 mutex_exit(&etfs_lock);
687
688 if (found) {
689 rn = et->et_rn;
690 cnp->cn_consume += et->et_keylen - cnp->cn_namelen;
691 if (rn->rn_va.va_type != VDIR)
692 cnp->cn_flags &= ~REQUIREDIR;
693 goto getvnode;
694 }
695 }
696
697 if (rnd->rn_flags & RUMPNODE_DIR_ET) {
698 uint64_t fsize;
699 char *newpath;
700 size_t newpathlen;
701 int hft, error;
702
703 if (dotdot)
704 return EOPNOTSUPP;
705
706 newpathlen = strlen(rnd->rn_hostpath) + 1 + cnp->cn_namelen + 1;
707 newpath = malloc(newpathlen, M_TEMP, M_WAITOK);
708
709 strlcpy(newpath, rnd->rn_hostpath, newpathlen);
710 strlcat(newpath, "/", newpathlen);
711 strlcat(newpath, cnp->cn_nameptr, newpathlen);
712
713 if (rumpuser_getfileinfo(newpath, &fsize, &hft, &error)) {
714 free(newpath, M_TEMP);
715 return error;
716 }
717
718 /* allow only dirs and regular files */
719 if (hft != RUMPUSER_FT_REG && hft != RUMPUSER_FT_DIR) {
720 free(newpath, M_TEMP);
721 return ENOENT;
722 }
723
724 rn = makeprivate(hft_to_vtype(hft), NODEV, fsize, true);
725 rn->rn_flags |= RUMPNODE_CANRECLAIM;
726 if (rnd->rn_flags & RUMPNODE_DIR_ETSUBS) {
727 rn->rn_flags |= RUMPNODE_DIR_ET | RUMPNODE_DIR_ETSUBS;
728 rn->rn_flags |= RUMPNODE_ET_PHONE_HOST;
729 }
730 rn->rn_hostpath = newpath;
731
732 goto getvnode;
733 } else {
734 if (dotdot) {
735 rn = rnd->rn_parent;
736 goto getvnode;
737 } else {
738 LIST_FOREACH(rd, &rnd->rn_dir, rd_entries) {
739 if (rd->rd_namelen == cnp->cn_namelen &&
740 strncmp(rd->rd_name, cnp->cn_nameptr,
741 cnp->cn_namelen) == 0)
742 break;
743 }
744 }
745 }
746
747 if (!rd && ((cnp->cn_flags & ISLASTCN) == 0||cnp->cn_nameiop != CREATE))
748 return ENOENT;
749
750 if (!rd && (cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) {
751 if (dvp->v_mount->mnt_flag & MNT_RDONLY)
752 return EROFS;
753 return EJUSTRETURN;
754 }
755
756 if (RDENT_ISWHITEOUT(rd)) {
757 cnp->cn_flags |= ISWHITEOUT;
758 return ENOENT;
759 }
760
761 rn = rd->rd_node;
762
763 getvnode:
764 KASSERT(rn);
765 if (dotdot)
766 VOP_UNLOCK(dvp);
767 mutex_enter(&reclock);
768 if ((vp = rn->rn_vp)) {
769 mutex_enter(&vp->v_interlock);
770 mutex_exit(&reclock);
771 if (vget(vp, LK_EXCLUSIVE)) {
772 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
773 goto getvnode;
774 }
775 *vpp = vp;
776 } else {
777 mutex_exit(&reclock);
778 rv = makevnode(dvp->v_mount, rn, vpp);
779 }
780 if (dotdot)
781 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
782
783 return rv;
784 }
785
786 int
787 rump_vop_access(void *v)
788 {
789 struct vop_access_args /* {
790 const struct vnodeop_desc *a_desc;
791 struct vnode *a_vp;
792 int a_mode;
793 kauth_cred_t a_cred;
794 } */ *ap = v;
795 struct vnode *vp = ap->a_vp;
796 int mode = ap->a_mode;
797
798 if (mode & VWRITE) {
799 switch (vp->v_type) {
800 case VDIR:
801 case VLNK:
802 case VREG:
803 if ((vp->v_mount->mnt_flag & MNT_RDONLY))
804 return EROFS;
805 break;
806 default:
807 break;
808 }
809 }
810
811 return 0;
812 }
813
814 static int
815 rump_vop_getattr(void *v)
816 {
817 struct vop_getattr_args /* {
818 struct vnode *a_vp;
819 struct vattr *a_vap;
820 kauth_cred_t a_cred;
821 } */ *ap = v;
822 struct vnode *vp = ap->a_vp;
823 struct rumpfs_node *rn = vp->v_data;
824 struct vattr *vap = ap->a_vap;
825
826 memcpy(vap, &rn->rn_va, sizeof(struct vattr));
827 vap->va_size = vp->v_size;
828 return 0;
829 }
830
831 static int
832 rump_vop_setattr(void *v)
833 {
834 struct vop_getattr_args /* {
835 struct vnode *a_vp;
836 struct vattr *a_vap;
837 kauth_cred_t a_cred;
838 } */ *ap = v;
839 struct vnode *vp = ap->a_vp;
840 struct vattr *vap = ap->a_vap;
841 struct rumpfs_node *rn = vp->v_data;
842
843 #define SETIFVAL(a,t) if (vap->a != (t)VNOVAL) rn->rn_va.a = vap->a
844 SETIFVAL(va_mode, mode_t);
845 SETIFVAL(va_uid, uid_t);
846 SETIFVAL(va_gid, gid_t);
847 SETIFVAL(va_atime.tv_sec, time_t);
848 SETIFVAL(va_ctime.tv_sec, time_t);
849 SETIFVAL(va_mtime.tv_sec, time_t);
850 SETIFVAL(va_birthtime.tv_sec, time_t);
851 SETIFVAL(va_atime.tv_nsec, long);
852 SETIFVAL(va_ctime.tv_nsec, long);
853 SETIFVAL(va_mtime.tv_nsec, long);
854 SETIFVAL(va_birthtime.tv_nsec, long);
855 SETIFVAL(va_flags, u_long);
856 #undef SETIFVAL
857
858 if (vp->v_type == VREG &&
859 vap->va_size != VSIZENOTSET &&
860 vap->va_size != rn->rn_dlen) {
861 void *newdata;
862 size_t copylen, newlen;
863
864 newlen = vap->va_size;
865 newdata = rump_hypermalloc(newlen, 0, true, "rumpfs");
866
867 copylen = MIN(rn->rn_dlen, newlen);
868 memset(newdata, 0, newlen);
869 memcpy(newdata, rn->rn_data, copylen);
870 rump_hyperfree(rn->rn_data, rn->rn_dlen);
871
872 rn->rn_data = newdata;
873 rn->rn_dlen = newlen;
874 uvm_vnp_setsize(vp, newlen);
875 }
876 return 0;
877 }
878
879 static int
880 rump_vop_mkdir(void *v)
881 {
882 struct vop_mkdir_args /* {
883 struct vnode *a_dvp;
884 struct vnode **a_vpp;
885 struct componentname *a_cnp;
886 struct vattr *a_vap;
887 }; */ *ap = v;
888 struct vnode *dvp = ap->a_dvp;
889 struct vnode **vpp = ap->a_vpp;
890 struct componentname *cnp = ap->a_cnp;
891 struct rumpfs_node *rnd = dvp->v_data, *rn;
892 int rv = 0;
893
894 rn = makeprivate(VDIR, NODEV, DEV_BSIZE, false);
895 rn->rn_parent = rnd;
896 rv = makevnode(dvp->v_mount, rn, vpp);
897 if (rv)
898 goto out;
899
900 makedir(rnd, cnp, rn);
901
902 out:
903 vput(dvp);
904 return rv;
905 }
906
907 static int
908 rump_vop_rmdir(void *v)
909 {
910 struct vop_rmdir_args /* {
911 struct vnode *a_dvp;
912 struct vnode *a_vp;
913 struct componentname *a_cnp;
914 }; */ *ap = v;
915 struct vnode *dvp = ap->a_dvp;
916 struct vnode *vp = ap->a_vp;
917 struct componentname *cnp = ap->a_cnp;
918 struct rumpfs_node *rnd = dvp->v_data;
919 struct rumpfs_node *rn = vp->v_data;
920 int rv = 0;
921
922 if (!LIST_EMPTY(&rn->rn_dir)) {
923 rv = ENOTEMPTY;
924 goto out;
925 }
926
927 freedir(rnd, cnp);
928 rn->rn_flags |= RUMPNODE_CANRECLAIM;
929
930 out:
931 vput(dvp);
932 vput(vp);
933
934 return rv;
935 }
936
937 static int
938 rump_vop_remove(void *v)
939 {
940 struct vop_rmdir_args /* {
941 struct vnode *a_dvp;
942 struct vnode *a_vp;
943 struct componentname *a_cnp;
944 }; */ *ap = v;
945 struct vnode *dvp = ap->a_dvp;
946 struct vnode *vp = ap->a_vp;
947 struct componentname *cnp = ap->a_cnp;
948 struct rumpfs_node *rnd = dvp->v_data;
949 struct rumpfs_node *rn = vp->v_data;
950 int rv = 0;
951
952 if (rn->rn_flags & RUMPNODE_ET_PHONE_HOST)
953 return EOPNOTSUPP;
954
955 if (vp->v_type == VREG) {
956 rump_hyperfree(rn->rn_data, rn->rn_dlen);
957 }
958
959 freedir(rnd, cnp);
960 rn->rn_flags |= RUMPNODE_CANRECLAIM;
961
962 vput(dvp);
963 vput(vp);
964
965 return rv;
966 }
967
968 static int
969 rump_vop_mknod(void *v)
970 {
971 struct vop_mknod_args /* {
972 struct vnode *a_dvp;
973 struct vnode **a_vpp;
974 struct componentname *a_cnp;
975 struct vattr *a_vap;
976 }; */ *ap = v;
977 struct vnode *dvp = ap->a_dvp;
978 struct vnode **vpp = ap->a_vpp;
979 struct componentname *cnp = ap->a_cnp;
980 struct vattr *va = ap->a_vap;
981 struct rumpfs_node *rnd = dvp->v_data, *rn;
982 int rv;
983
984 rn = makeprivate(va->va_type, va->va_rdev, DEV_BSIZE, false);
985 rv = makevnode(dvp->v_mount, rn, vpp);
986 if (rv)
987 goto out;
988
989 makedir(rnd, cnp, rn);
990
991 out:
992 vput(dvp);
993 return rv;
994 }
995
996 static int
997 rump_vop_create(void *v)
998 {
999 struct vop_create_args /* {
1000 struct vnode *a_dvp;
1001 struct vnode **a_vpp;
1002 struct componentname *a_cnp;
1003 struct vattr *a_vap;
1004 }; */ *ap = v;
1005 struct vnode *dvp = ap->a_dvp;
1006 struct vnode **vpp = ap->a_vpp;
1007 struct componentname *cnp = ap->a_cnp;
1008 struct vattr *va = ap->a_vap;
1009 struct rumpfs_node *rnd = dvp->v_data, *rn;
1010 off_t newsize;
1011 int rv;
1012
1013 newsize = va->va_type == VSOCK ? DEV_BSIZE : 0;
1014 rn = makeprivate(va->va_type, NODEV, newsize, false);
1015 rv = makevnode(dvp->v_mount, rn, vpp);
1016 if (rv)
1017 goto out;
1018
1019 makedir(rnd, cnp, rn);
1020
1021 out:
1022 vput(dvp);
1023 return rv;
1024 }
1025
1026 static int
1027 rump_vop_symlink(void *v)
1028 {
1029 struct vop_symlink_args /* {
1030 struct vnode *a_dvp;
1031 struct vnode **a_vpp;
1032 struct componentname *a_cnp;
1033 struct vattr *a_vap;
1034 char *a_target;
1035 }; */ *ap = v;
1036 struct vnode *dvp = ap->a_dvp;
1037 struct vnode **vpp = ap->a_vpp;
1038 struct componentname *cnp = ap->a_cnp;
1039 struct rumpfs_node *rnd = dvp->v_data, *rn;
1040 const char *target = ap->a_target;
1041 size_t linklen;
1042 int rv;
1043
1044 linklen = strlen(target);
1045 KASSERT(linklen < MAXPATHLEN);
1046 rn = makeprivate(VLNK, NODEV, linklen, false);
1047 rv = makevnode(dvp->v_mount, rn, vpp);
1048 if (rv)
1049 goto out;
1050
1051 makedir(rnd, cnp, rn);
1052
1053 KASSERT(linklen < MAXPATHLEN);
1054 rn->rn_linktarg = PNBUF_GET();
1055 rn->rn_linklen = linklen;
1056 strcpy(rn->rn_linktarg, target);
1057
1058 out:
1059 vput(dvp);
1060 return rv;
1061 }
1062
1063 static int
1064 rump_vop_readlink(void *v)
1065 {
1066 struct vop_readlink_args /* {
1067 struct vnode *a_vp;
1068 struct uio *a_uio;
1069 kauth_cred_t a_cred;
1070 }; */ *ap = v;
1071 struct vnode *vp = ap->a_vp;
1072 struct rumpfs_node *rn = vp->v_data;
1073 struct uio *uio = ap->a_uio;
1074
1075 return uiomove(rn->rn_linktarg, rn->rn_linklen, uio);
1076 }
1077
1078 static int
1079 rump_vop_whiteout(void *v)
1080 {
1081 struct vop_whiteout_args /* {
1082 struct vnode *a_dvp;
1083 struct componentname *a_cnp;
1084 int a_flags;
1085 } */ *ap = v;
1086 struct vnode *dvp = ap->a_dvp;
1087 struct rumpfs_node *rnd = dvp->v_data;
1088 struct componentname *cnp = ap->a_cnp;
1089 int flags = ap->a_flags;
1090
1091 switch (flags) {
1092 case LOOKUP:
1093 break;
1094 case CREATE:
1095 makedir(rnd, cnp, RUMPFS_WHITEOUT);
1096 break;
1097 case DELETE:
1098 cnp->cn_flags &= ~DOWHITEOUT; /* cargo culting never fails ? */
1099 freedir(rnd, cnp);
1100 break;
1101 default:
1102 panic("unknown whiteout op %d", flags);
1103 }
1104
1105 return 0;
1106 }
1107
1108 static int
1109 rump_vop_open(void *v)
1110 {
1111 struct vop_open_args /* {
1112 struct vnode *a_vp;
1113 int a_mode;
1114 kauth_cred_t a_cred;
1115 } */ *ap = v;
1116 struct vnode *vp = ap->a_vp;
1117 struct rumpfs_node *rn = vp->v_data;
1118 int mode = ap->a_mode;
1119 int error = EINVAL;
1120
1121 if (vp->v_type != VREG || (rn->rn_flags & RUMPNODE_ET_PHONE_HOST) == 0)
1122 return 0;
1123
1124 if (mode & FREAD) {
1125 if (rn->rn_readfd != -1)
1126 return 0;
1127 rn->rn_readfd = rumpuser_open(rn->rn_hostpath,
1128 O_RDONLY, &error);
1129 }
1130
1131 if (mode & FWRITE) {
1132 if (rn->rn_writefd != -1)
1133 return 0;
1134 rn->rn_writefd = rumpuser_open(rn->rn_hostpath,
1135 O_WRONLY, &error);
1136 }
1137
1138 return error;
1139 }
1140
1141 /* simple readdir. event omits dotstuff and periods */
1142 static int
1143 rump_vop_readdir(void *v)
1144 {
1145 struct vop_readdir_args /* {
1146 struct vnode *a_vp;
1147 struct uio *a_uio;
1148 kauth_cred_t a_cred;
1149 int *a_eofflag;
1150 off_t **a_cookies;
1151 int *a_ncookies;
1152 } */ *ap = v;
1153 struct vnode *vp = ap->a_vp;
1154 struct uio *uio = ap->a_uio;
1155 struct rumpfs_node *rnd = vp->v_data;
1156 struct rumpfs_dent *rdent;
1157 unsigned i;
1158 int rv = 0;
1159
1160 /* seek to current entry */
1161 for (i = 0, rdent = LIST_FIRST(&rnd->rn_dir);
1162 (i < uio->uio_offset) && rdent;
1163 i++, rdent = LIST_NEXT(rdent, rd_entries))
1164 continue;
1165 if (!rdent)
1166 goto out;
1167
1168 /* copy entries */
1169 for (; rdent && uio->uio_resid > 0;
1170 rdent = LIST_NEXT(rdent, rd_entries), i++) {
1171 struct dirent dent;
1172
1173 strlcpy(dent.d_name, rdent->rd_name, sizeof(dent.d_name));
1174 dent.d_namlen = strlen(dent.d_name);
1175 dent.d_reclen = _DIRENT_RECLEN(&dent, dent.d_namlen);
1176
1177 if (__predict_false(RDENT_ISWHITEOUT(rdent))) {
1178 dent.d_fileno = INO_WHITEOUT;
1179 dent.d_type = DT_WHT;
1180 } else {
1181 dent.d_fileno = rdent->rd_node->rn_va.va_fileid;
1182 dent.d_type = vtype2dt(rdent->rd_node->rn_va.va_type);
1183 }
1184
1185 if (uio->uio_resid < dent.d_reclen) {
1186 i--;
1187 break;
1188 }
1189
1190 rv = uiomove(&dent, dent.d_reclen, uio);
1191 if (rv) {
1192 i--;
1193 break;
1194 }
1195 }
1196
1197 out:
1198 if (ap->a_cookies) {
1199 *ap->a_ncookies = 0;
1200 *ap->a_cookies = NULL;
1201 }
1202 if (rdent)
1203 *ap->a_eofflag = 0;
1204 else
1205 *ap->a_eofflag = 1;
1206 uio->uio_offset = i;
1207
1208 return rv;
1209 }
1210
1211 static int
1212 etread(struct rumpfs_node *rn, struct uio *uio)
1213 {
1214 uint8_t *buf;
1215 size_t bufsize;
1216 ssize_t n;
1217 int error = 0;
1218
1219 bufsize = uio->uio_resid;
1220 buf = kmem_alloc(bufsize, KM_SLEEP);
1221 if ((n = rumpuser_pread(rn->rn_readfd, buf, bufsize,
1222 uio->uio_offset + rn->rn_offset, &error)) == -1)
1223 goto out;
1224 KASSERT(n <= bufsize);
1225 error = uiomove(buf, n, uio);
1226
1227 out:
1228 kmem_free(buf, bufsize);
1229 return error;
1230
1231 }
1232
1233 static int
1234 rump_vop_read(void *v)
1235 {
1236 struct vop_read_args /* {
1237 struct vnode *a_vp;
1238 struct uio *a_uio;
1239 int ioflags a_ioflag;
1240 kauth_cred_t a_cred;
1241 }; */ *ap = v;
1242 struct vnode *vp = ap->a_vp;
1243 struct rumpfs_node *rn = vp->v_data;
1244 struct uio *uio = ap->a_uio;
1245 const int advice = IO_ADV_DECODE(ap->a_ioflag);
1246 off_t chunk;
1247 int error = 0;
1248
1249 /* et op? */
1250 if (rn->rn_flags & RUMPNODE_ET_PHONE_HOST)
1251 return etread(rn, uio);
1252
1253 /* otherwise, it's off to ubc with us */
1254 while (uio->uio_resid > 0) {
1255 chunk = MIN(uio->uio_resid, (off_t)rn->rn_dlen-uio->uio_offset);
1256 if (chunk == 0)
1257 break;
1258 error = ubc_uiomove(&vp->v_uobj, uio, chunk, advice,
1259 UBC_READ | UBC_PARTIALOK | UBC_WANT_UNMAP(vp)?UBC_UNMAP:0);
1260 if (error)
1261 break;
1262 }
1263
1264 return error;
1265 }
1266
1267 static int
1268 etwrite(struct rumpfs_node *rn, struct uio *uio)
1269 {
1270 uint8_t *buf;
1271 size_t bufsize;
1272 ssize_t n;
1273 int error = 0;
1274
1275 bufsize = uio->uio_resid;
1276 buf = kmem_alloc(bufsize, KM_SLEEP);
1277 error = uiomove(buf, bufsize, uio);
1278 if (error)
1279 goto out;
1280 KASSERT(uio->uio_resid == 0);
1281 n = rumpuser_pwrite(rn->rn_writefd, buf, bufsize,
1282 (uio->uio_offset-bufsize) + rn->rn_offset, &error);
1283 if (n >= 0) {
1284 KASSERT(n <= bufsize);
1285 uio->uio_resid = bufsize - n;
1286 }
1287
1288 out:
1289 kmem_free(buf, bufsize);
1290 return error;
1291 }
1292
1293 static int
1294 rump_vop_write(void *v)
1295 {
1296 struct vop_read_args /* {
1297 struct vnode *a_vp;
1298 struct uio *a_uio;
1299 int ioflags a_ioflag;
1300 kauth_cred_t a_cred;
1301 }; */ *ap = v;
1302 struct vnode *vp = ap->a_vp;
1303 struct rumpfs_node *rn = vp->v_data;
1304 struct uio *uio = ap->a_uio;
1305 const int advice = IO_ADV_DECODE(ap->a_ioflag);
1306 void *olddata;
1307 size_t oldlen, newlen;
1308 off_t chunk;
1309 int error = 0;
1310 bool allocd = false;
1311
1312 if (ap->a_ioflag & IO_APPEND)
1313 uio->uio_offset = vp->v_size;
1314
1315 /* consult et? */
1316 if (rn->rn_flags & RUMPNODE_ET_PHONE_HOST)
1317 return etwrite(rn, uio);
1318
1319 /*
1320 * Otherwise, it's a case of ubcmove.
1321 */
1322
1323 /*
1324 * First, make sure we have enough storage.
1325 *
1326 * No, you don't need to tell me it's not very efficient.
1327 * No, it doesn't really support sparse files, just fakes it.
1328 */
1329 newlen = uio->uio_offset + uio->uio_resid;
1330 oldlen = 0; /* XXXgcc */
1331 olddata = NULL;
1332 if (rn->rn_dlen < newlen) {
1333 oldlen = rn->rn_dlen;
1334 olddata = rn->rn_data;
1335
1336 rn->rn_data = rump_hypermalloc(newlen, 0, true, "rumpfs");
1337 rn->rn_dlen = newlen;
1338 memset(rn->rn_data, 0, newlen);
1339 memcpy(rn->rn_data, olddata, oldlen);
1340 allocd = true;
1341 uvm_vnp_setsize(vp, newlen);
1342 }
1343
1344 /* ok, we have enough stooorage. write */
1345 while (uio->uio_resid > 0) {
1346 chunk = MIN(uio->uio_resid, (off_t)rn->rn_dlen-uio->uio_offset);
1347 if (chunk == 0)
1348 break;
1349 error = ubc_uiomove(&vp->v_uobj, uio, chunk, advice,
1350 UBC_WRITE | UBC_PARTIALOK | UBC_WANT_UNMAP(vp)?UBC_UNMAP:0);
1351 if (error)
1352 break;
1353 }
1354
1355 if (allocd) {
1356 if (error) {
1357 rump_hyperfree(rn->rn_data, newlen);
1358 rn->rn_data = olddata;
1359 rn->rn_dlen = oldlen;
1360 uvm_vnp_setsize(vp, oldlen);
1361 } else {
1362 rump_hyperfree(olddata, oldlen);
1363 }
1364 }
1365
1366 return error;
1367 }
1368
1369 static int
1370 rump_vop_bmap(void *v)
1371 {
1372 struct vop_bmap_args /* {
1373 struct vnode *a_vp;
1374 daddr_t a_bn;
1375 struct vnode **a_vpp;
1376 daddr_t *a_bnp;
1377 int *a_runp;
1378 } */ *ap = v;
1379
1380 /* 1:1 mapping */
1381 if (ap->a_vpp)
1382 *ap->a_vpp = ap->a_vp;
1383 if (ap->a_bnp)
1384 *ap->a_bnp = ap->a_bn;
1385 if (ap->a_runp)
1386 *ap->a_runp = 16;
1387
1388 return 0;
1389 }
1390
1391 static int
1392 rump_vop_strategy(void *v)
1393 {
1394 struct vop_strategy_args /* {
1395 struct vnode *a_vp;
1396 struct buf *a_bp;
1397 } */ *ap = v;
1398 struct vnode *vp = ap->a_vp;
1399 struct rumpfs_node *rn = vp->v_data;
1400 struct buf *bp = ap->a_bp;
1401 off_t copylen, copyoff;
1402 int error;
1403
1404 if (vp->v_type != VREG || rn->rn_flags & RUMPNODE_ET_PHONE_HOST) {
1405 error = EINVAL;
1406 goto out;
1407 }
1408
1409 copyoff = bp->b_blkno << DEV_BSHIFT;
1410 copylen = MIN(rn->rn_dlen - copyoff, bp->b_bcount);
1411 if (BUF_ISWRITE(bp)) {
1412 memcpy((uint8_t *)rn->rn_data + copyoff, bp->b_data, copylen);
1413 } else {
1414 memset((uint8_t*)bp->b_data + copylen, 0, bp->b_bcount-copylen);
1415 memcpy(bp->b_data, (uint8_t *)rn->rn_data + copyoff, copylen);
1416 }
1417 bp->b_resid = 0;
1418 error = 0;
1419
1420 out:
1421 bp->b_error = error;
1422 biodone(bp);
1423 return 0;
1424 }
1425
1426 static int
1427 rump_vop_pathconf(void *v)
1428 {
1429 struct vop_pathconf_args /* {
1430 struct vnode *a_vp;
1431 int a_name;
1432 register_t *a_retval;
1433 }; */ *ap = v;
1434 int name = ap->a_name;
1435 register_t *retval = ap->a_retval;
1436
1437 switch (name) {
1438 case _PC_LINK_MAX:
1439 *retval = LINK_MAX;
1440 return 0;
1441 case _PC_NAME_MAX:
1442 *retval = NAME_MAX;
1443 return 0;
1444 case _PC_PATH_MAX:
1445 *retval = PATH_MAX;
1446 return 0;
1447 case _PC_PIPE_BUF:
1448 *retval = PIPE_BUF;
1449 return 0;
1450 case _PC_CHOWN_RESTRICTED:
1451 *retval = 1;
1452 return 0;
1453 case _PC_NO_TRUNC:
1454 *retval = 1;
1455 return 0;
1456 case _PC_SYNC_IO:
1457 *retval = 1;
1458 return 0;
1459 case _PC_FILESIZEBITS:
1460 *retval = 43; /* this one goes to 11 */
1461 return 0;
1462 case _PC_SYMLINK_MAX:
1463 *retval = MAXPATHLEN;
1464 return 0;
1465 case _PC_2_SYMLINKS:
1466 *retval = 1;
1467 return 0;
1468 default:
1469 return EINVAL;
1470 }
1471 }
1472
1473 static int
1474 rump_vop_success(void *v)
1475 {
1476
1477 return 0;
1478 }
1479
1480 static int
1481 rump_vop_inactive(void *v)
1482 {
1483 struct vop_inactive_args /* {
1484 struct vnode *a_vp;
1485 bool *a_recycle;
1486 } */ *ap = v;
1487 struct vnode *vp = ap->a_vp;
1488 struct rumpfs_node *rn = vp->v_data;
1489 int error;
1490
1491 if (rn->rn_flags & RUMPNODE_ET_PHONE_HOST && vp->v_type == VREG) {
1492 if (rn->rn_readfd != -1) {
1493 rumpuser_close(rn->rn_readfd, &error);
1494 rn->rn_readfd = -1;
1495 }
1496 if (rn->rn_writefd != -1) {
1497 rumpuser_close(rn->rn_writefd, &error);
1498 rn->rn_writefd = -1;
1499 }
1500 }
1501 *ap->a_recycle = (rn->rn_flags & RUMPNODE_CANRECLAIM) ? true : false;
1502
1503 VOP_UNLOCK(vp);
1504 return 0;
1505 }
1506
1507 static int
1508 rump_vop_reclaim(void *v)
1509 {
1510 struct vop_reclaim_args /* {
1511 struct vnode *a_vp;
1512 } */ *ap = v;
1513 struct vnode *vp = ap->a_vp;
1514 struct rumpfs_node *rn = vp->v_data;
1515
1516 mutex_enter(&reclock);
1517 rn->rn_vp = NULL;
1518 mutex_exit(&reclock);
1519 genfs_node_destroy(vp);
1520 vp->v_data = NULL;
1521
1522 if (rn->rn_flags & RUMPNODE_CANRECLAIM) {
1523 if (vp->v_type == VLNK)
1524 PNBUF_PUT(rn->rn_linktarg);
1525 if (rn->rn_hostpath)
1526 free(rn->rn_hostpath, M_TEMP);
1527 kmem_free(rn, sizeof(*rn));
1528 }
1529
1530 return 0;
1531 }
1532
1533 static int
1534 rump_vop_spec(void *v)
1535 {
1536 struct vop_generic_args *ap = v;
1537 int (**opvec)(void *);
1538
1539 switch (ap->a_desc->vdesc_offset) {
1540 case VOP_ACCESS_DESCOFFSET:
1541 case VOP_GETATTR_DESCOFFSET:
1542 case VOP_SETATTR_DESCOFFSET:
1543 case VOP_LOCK_DESCOFFSET:
1544 case VOP_UNLOCK_DESCOFFSET:
1545 case VOP_ISLOCKED_DESCOFFSET:
1546 case VOP_RECLAIM_DESCOFFSET:
1547 opvec = rump_vnodeop_p;
1548 break;
1549 default:
1550 opvec = spec_vnodeop_p;
1551 break;
1552 }
1553
1554 return VOCALL(opvec, ap->a_desc->vdesc_offset, v);
1555 }
1556
1557 static int
1558 rump_vop_advlock(void *v)
1559 {
1560 struct vop_advlock_args /* {
1561 const struct vnodeop_desc *a_desc;
1562 struct vnode *a_vp;
1563 void *a_id;
1564 int a_op;
1565 struct flock *a_fl;
1566 int a_flags;
1567 } */ *ap = v;
1568 struct vnode *vp = ap->a_vp;
1569 struct rumpfs_node *rn = vp->v_data;
1570
1571 return lf_advlock(ap, &rn->rn_lockf, vp->v_size);
1572 }
1573
1574 /*
1575 * Begin vfs-level stuff
1576 */
1577
1578 VFS_PROTOS(rumpfs);
1579 struct vfsops rumpfs_vfsops = {
1580 .vfs_name = MOUNT_RUMPFS,
1581 .vfs_min_mount_data = 0,
1582 .vfs_mount = rumpfs_mount,
1583 .vfs_start = (void *)nullop,
1584 .vfs_unmount = rumpfs_unmount,
1585 .vfs_root = rumpfs_root,
1586 .vfs_quotactl = (void *)eopnotsupp,
1587 .vfs_statvfs = genfs_statvfs,
1588 .vfs_sync = (void *)nullop,
1589 .vfs_vget = rumpfs_vget,
1590 .vfs_fhtovp = (void *)eopnotsupp,
1591 .vfs_vptofh = (void *)eopnotsupp,
1592 .vfs_init = rumpfs_init,
1593 .vfs_reinit = NULL,
1594 .vfs_done = rumpfs_done,
1595 .vfs_mountroot = rumpfs_mountroot,
1596 .vfs_snapshot = (void *)eopnotsupp,
1597 .vfs_extattrctl = (void *)eopnotsupp,
1598 .vfs_suspendctl = (void *)eopnotsupp,
1599 .vfs_renamelock_enter = genfs_renamelock_enter,
1600 .vfs_renamelock_exit = genfs_renamelock_exit,
1601 .vfs_opv_descs = rump_opv_descs,
1602 /* vfs_refcount */
1603 /* vfs_list */
1604 };
1605
1606 static int
1607 rumpfs_mountfs(struct mount *mp)
1608 {
1609 struct rumpfs_mount *rfsmp;
1610 struct rumpfs_node *rn;
1611 int error;
1612
1613 rfsmp = kmem_alloc(sizeof(*rfsmp), KM_SLEEP);
1614
1615 rn = makeprivate(VDIR, NODEV, DEV_BSIZE, false);
1616 rn->rn_parent = rn;
1617 if ((error = makevnode(mp, rn, &rfsmp->rfsmp_rvp)) != 0)
1618 return error;
1619
1620 rfsmp->rfsmp_rvp->v_vflag |= VV_ROOT;
1621 VOP_UNLOCK(rfsmp->rfsmp_rvp);
1622
1623 mp->mnt_data = rfsmp;
1624 mp->mnt_stat.f_namemax = MAXNAMLEN;
1625 mp->mnt_stat.f_iosize = 512;
1626 mp->mnt_flag |= MNT_LOCAL;
1627 mp->mnt_iflag |= IMNT_MPSAFE | IMNT_CAN_RWTORO;
1628 mp->mnt_fs_bshift = DEV_BSHIFT;
1629 vfs_getnewfsid(mp);
1630
1631 return 0;
1632 }
1633
1634 int
1635 rumpfs_mount(struct mount *mp, const char *mntpath, void *arg, size_t *alen)
1636 {
1637 int error;
1638
1639 if (mp->mnt_flag & MNT_UPDATE) {
1640 return 0;
1641 }
1642
1643 error = set_statvfs_info(mntpath, UIO_USERSPACE, "rumpfs", UIO_SYSSPACE,
1644 mp->mnt_op->vfs_name, mp, curlwp);
1645 if (error)
1646 return error;
1647
1648 return rumpfs_mountfs(mp);
1649 }
1650
1651 int
1652 rumpfs_unmount(struct mount *mp, int mntflags)
1653 {
1654 struct rumpfs_mount *rfsmp = mp->mnt_data;
1655 int flags = 0, error;
1656
1657 if (panicstr || mntflags & MNT_FORCE)
1658 flags |= FORCECLOSE;
1659
1660 if ((error = vflush(mp, rfsmp->rfsmp_rvp, flags)) != 0)
1661 return error;
1662 vgone(rfsmp->rfsmp_rvp); /* XXX */
1663
1664 kmem_free(rfsmp, sizeof(*rfsmp));
1665
1666 return 0;
1667 }
1668
1669 int
1670 rumpfs_root(struct mount *mp, struct vnode **vpp)
1671 {
1672 struct rumpfs_mount *rfsmp = mp->mnt_data;
1673
1674 vref(rfsmp->rfsmp_rvp);
1675 vn_lock(rfsmp->rfsmp_rvp, LK_EXCLUSIVE | LK_RETRY);
1676 *vpp = rfsmp->rfsmp_rvp;
1677 return 0;
1678 }
1679
1680 int
1681 rumpfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1682 {
1683
1684 return EOPNOTSUPP;
1685 }
1686
1687 void
1688 rumpfs_init()
1689 {
1690
1691 CTASSERT(RUMP_ETFS_SIZE_ENDOFF == RUMPBLK_SIZENOTSET);
1692
1693 mutex_init(&reclock, MUTEX_DEFAULT, IPL_NONE);
1694 mutex_init(&etfs_lock, MUTEX_DEFAULT, IPL_NONE);
1695 }
1696
1697 void
1698 rumpfs_done()
1699 {
1700
1701 mutex_destroy(&reclock);
1702 mutex_destroy(&etfs_lock);
1703 }
1704
1705 int
1706 rumpfs_mountroot()
1707 {
1708 struct mount *mp;
1709 int error;
1710
1711 if ((error = vfs_rootmountalloc(MOUNT_RUMPFS, "rootdev", &mp)) != 0) {
1712 vrele(rootvp);
1713 return error;
1714 }
1715
1716 if ((error = rumpfs_mountfs(mp)) != 0)
1717 panic("mounting rootfs failed: %d", error);
1718
1719 mutex_enter(&mountlist_lock);
1720 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1721 mutex_exit(&mountlist_lock);
1722
1723 error = set_statvfs_info("/", UIO_SYSSPACE, "rumpfs", UIO_SYSSPACE,
1724 mp->mnt_op->vfs_name, mp, curlwp);
1725 if (error)
1726 panic("set_statvfs_info failed for rootfs: %d", error);
1727
1728 mp->mnt_flag &= ~MNT_RDONLY;
1729 vfs_unbusy(mp, false, NULL);
1730
1731 return 0;
1732 }
1733