rumpfs.c revision 1.93 1 /* $NetBSD: rumpfs.c,v 1.93 2011/03/21 16:41:09 pooka 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.93 2011/03/21 16:41:09 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 &&
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 buf = kmem_alloc(bufsize, KM_SLEEP);
1223 if ((n = rumpuser_pread(rn->rn_readfd, buf, bufsize,
1224 uio->uio_offset + rn->rn_offset, &error)) == -1)
1225 goto out;
1226 KASSERT(n <= bufsize);
1227 error = uiomove(buf, n, uio);
1228
1229 out:
1230 kmem_free(buf, bufsize);
1231 return error;
1232
1233 }
1234
1235 static int
1236 rump_vop_read(void *v)
1237 {
1238 struct vop_read_args /* {
1239 struct vnode *a_vp;
1240 struct uio *a_uio;
1241 int ioflags a_ioflag;
1242 kauth_cred_t a_cred;
1243 }; */ *ap = v;
1244 struct vnode *vp = ap->a_vp;
1245 struct rumpfs_node *rn = vp->v_data;
1246 struct uio *uio = ap->a_uio;
1247 const int advice = IO_ADV_DECODE(ap->a_ioflag);
1248 off_t chunk;
1249 int error = 0;
1250
1251 /* et op? */
1252 if (rn->rn_flags & RUMPNODE_ET_PHONE_HOST)
1253 return etread(rn, uio);
1254
1255 /* otherwise, it's off to ubc with us */
1256 while (uio->uio_resid > 0) {
1257 chunk = MIN(uio->uio_resid, (off_t)rn->rn_dlen-uio->uio_offset);
1258 if (chunk == 0)
1259 break;
1260 error = ubc_uiomove(&vp->v_uobj, uio, chunk, advice,
1261 UBC_READ | UBC_PARTIALOK | UBC_WANT_UNMAP(vp)?UBC_UNMAP:0);
1262 if (error)
1263 break;
1264 }
1265
1266 return error;
1267 }
1268
1269 static int
1270 etwrite(struct rumpfs_node *rn, struct uio *uio)
1271 {
1272 uint8_t *buf;
1273 size_t bufsize;
1274 ssize_t n;
1275 int error = 0;
1276
1277 bufsize = uio->uio_resid;
1278 buf = kmem_alloc(bufsize, KM_SLEEP);
1279 error = uiomove(buf, bufsize, uio);
1280 if (error)
1281 goto out;
1282 KASSERT(uio->uio_resid == 0);
1283 n = rumpuser_pwrite(rn->rn_writefd, buf, bufsize,
1284 (uio->uio_offset-bufsize) + rn->rn_offset, &error);
1285 if (n >= 0) {
1286 KASSERT(n <= bufsize);
1287 uio->uio_resid = bufsize - n;
1288 }
1289
1290 out:
1291 kmem_free(buf, bufsize);
1292 return error;
1293 }
1294
1295 static int
1296 rump_vop_write(void *v)
1297 {
1298 struct vop_read_args /* {
1299 struct vnode *a_vp;
1300 struct uio *a_uio;
1301 int ioflags a_ioflag;
1302 kauth_cred_t a_cred;
1303 }; */ *ap = v;
1304 struct vnode *vp = ap->a_vp;
1305 struct rumpfs_node *rn = vp->v_data;
1306 struct uio *uio = ap->a_uio;
1307 const int advice = IO_ADV_DECODE(ap->a_ioflag);
1308 void *olddata;
1309 size_t oldlen, newlen;
1310 off_t chunk;
1311 int error = 0;
1312 bool allocd = false;
1313
1314 if (ap->a_ioflag & IO_APPEND)
1315 uio->uio_offset = vp->v_size;
1316
1317 /* consult et? */
1318 if (rn->rn_flags & RUMPNODE_ET_PHONE_HOST)
1319 return etwrite(rn, uio);
1320
1321 /*
1322 * Otherwise, it's a case of ubcmove.
1323 */
1324
1325 /*
1326 * First, make sure we have enough storage.
1327 *
1328 * No, you don't need to tell me it's not very efficient.
1329 * No, it doesn't really support sparse files, just fakes it.
1330 */
1331 newlen = uio->uio_offset + uio->uio_resid;
1332 oldlen = 0; /* XXXgcc */
1333 olddata = NULL;
1334 if (rn->rn_dlen < newlen) {
1335 oldlen = rn->rn_dlen;
1336 olddata = rn->rn_data;
1337
1338 rn->rn_data = rump_hypermalloc(newlen, 0, true, "rumpfs");
1339 rn->rn_dlen = newlen;
1340 memset(rn->rn_data, 0, newlen);
1341 memcpy(rn->rn_data, olddata, oldlen);
1342 allocd = true;
1343 uvm_vnp_setsize(vp, newlen);
1344 }
1345
1346 /* ok, we have enough stooorage. write */
1347 while (uio->uio_resid > 0) {
1348 chunk = MIN(uio->uio_resid, (off_t)rn->rn_dlen-uio->uio_offset);
1349 if (chunk == 0)
1350 break;
1351 error = ubc_uiomove(&vp->v_uobj, uio, chunk, advice,
1352 UBC_WRITE | UBC_PARTIALOK | UBC_WANT_UNMAP(vp)?UBC_UNMAP:0);
1353 if (error)
1354 break;
1355 }
1356
1357 if (allocd) {
1358 if (error) {
1359 rump_hyperfree(rn->rn_data, newlen);
1360 rn->rn_data = olddata;
1361 rn->rn_dlen = oldlen;
1362 uvm_vnp_setsize(vp, oldlen);
1363 } else {
1364 rump_hyperfree(olddata, oldlen);
1365 }
1366 }
1367
1368 return error;
1369 }
1370
1371 static int
1372 rump_vop_bmap(void *v)
1373 {
1374 struct vop_bmap_args /* {
1375 struct vnode *a_vp;
1376 daddr_t a_bn;
1377 struct vnode **a_vpp;
1378 daddr_t *a_bnp;
1379 int *a_runp;
1380 } */ *ap = v;
1381
1382 /* 1:1 mapping */
1383 if (ap->a_vpp)
1384 *ap->a_vpp = ap->a_vp;
1385 if (ap->a_bnp)
1386 *ap->a_bnp = ap->a_bn;
1387 if (ap->a_runp)
1388 *ap->a_runp = 16;
1389
1390 return 0;
1391 }
1392
1393 static int
1394 rump_vop_strategy(void *v)
1395 {
1396 struct vop_strategy_args /* {
1397 struct vnode *a_vp;
1398 struct buf *a_bp;
1399 } */ *ap = v;
1400 struct vnode *vp = ap->a_vp;
1401 struct rumpfs_node *rn = vp->v_data;
1402 struct buf *bp = ap->a_bp;
1403 off_t copylen, copyoff;
1404 int error;
1405
1406 if (vp->v_type != VREG || rn->rn_flags & RUMPNODE_ET_PHONE_HOST) {
1407 error = EINVAL;
1408 goto out;
1409 }
1410
1411 copyoff = bp->b_blkno << DEV_BSHIFT;
1412 copylen = MIN(rn->rn_dlen - copyoff, bp->b_bcount);
1413 if (BUF_ISWRITE(bp)) {
1414 memcpy((uint8_t *)rn->rn_data + copyoff, bp->b_data, copylen);
1415 } else {
1416 memset((uint8_t*)bp->b_data + copylen, 0, bp->b_bcount-copylen);
1417 memcpy(bp->b_data, (uint8_t *)rn->rn_data + copyoff, copylen);
1418 }
1419 bp->b_resid = 0;
1420 error = 0;
1421
1422 out:
1423 bp->b_error = error;
1424 biodone(bp);
1425 return 0;
1426 }
1427
1428 static int
1429 rump_vop_pathconf(void *v)
1430 {
1431 struct vop_pathconf_args /* {
1432 struct vnode *a_vp;
1433 int a_name;
1434 register_t *a_retval;
1435 }; */ *ap = v;
1436 int name = ap->a_name;
1437 register_t *retval = ap->a_retval;
1438
1439 switch (name) {
1440 case _PC_LINK_MAX:
1441 *retval = LINK_MAX;
1442 return 0;
1443 case _PC_NAME_MAX:
1444 *retval = NAME_MAX;
1445 return 0;
1446 case _PC_PATH_MAX:
1447 *retval = PATH_MAX;
1448 return 0;
1449 case _PC_PIPE_BUF:
1450 *retval = PIPE_BUF;
1451 return 0;
1452 case _PC_CHOWN_RESTRICTED:
1453 *retval = 1;
1454 return 0;
1455 case _PC_NO_TRUNC:
1456 *retval = 1;
1457 return 0;
1458 case _PC_SYNC_IO:
1459 *retval = 1;
1460 return 0;
1461 case _PC_FILESIZEBITS:
1462 *retval = 43; /* this one goes to 11 */
1463 return 0;
1464 case _PC_SYMLINK_MAX:
1465 *retval = MAXPATHLEN;
1466 return 0;
1467 case _PC_2_SYMLINKS:
1468 *retval = 1;
1469 return 0;
1470 default:
1471 return EINVAL;
1472 }
1473 }
1474
1475 static int
1476 rump_vop_success(void *v)
1477 {
1478
1479 return 0;
1480 }
1481
1482 static int
1483 rump_vop_inactive(void *v)
1484 {
1485 struct vop_inactive_args /* {
1486 struct vnode *a_vp;
1487 bool *a_recycle;
1488 } */ *ap = v;
1489 struct vnode *vp = ap->a_vp;
1490 struct rumpfs_node *rn = vp->v_data;
1491 int error;
1492
1493 if (rn->rn_flags & RUMPNODE_ET_PHONE_HOST && vp->v_type == VREG) {
1494 if (rn->rn_readfd != -1) {
1495 rumpuser_close(rn->rn_readfd, &error);
1496 rn->rn_readfd = -1;
1497 }
1498 if (rn->rn_writefd != -1) {
1499 rumpuser_close(rn->rn_writefd, &error);
1500 rn->rn_writefd = -1;
1501 }
1502 }
1503 *ap->a_recycle = (rn->rn_flags & RUMPNODE_CANRECLAIM) ? true : false;
1504
1505 VOP_UNLOCK(vp);
1506 return 0;
1507 }
1508
1509 static int
1510 rump_vop_reclaim(void *v)
1511 {
1512 struct vop_reclaim_args /* {
1513 struct vnode *a_vp;
1514 } */ *ap = v;
1515 struct vnode *vp = ap->a_vp;
1516 struct rumpfs_node *rn = vp->v_data;
1517
1518 mutex_enter(&reclock);
1519 rn->rn_vp = NULL;
1520 mutex_exit(&reclock);
1521 genfs_node_destroy(vp);
1522 vp->v_data = NULL;
1523
1524 if (rn->rn_flags & RUMPNODE_CANRECLAIM) {
1525 if (vp->v_type == VLNK)
1526 PNBUF_PUT(rn->rn_linktarg);
1527 if (rn->rn_hostpath)
1528 free(rn->rn_hostpath, M_TEMP);
1529 kmem_free(rn, sizeof(*rn));
1530 }
1531
1532 return 0;
1533 }
1534
1535 static int
1536 rump_vop_spec(void *v)
1537 {
1538 struct vop_generic_args *ap = v;
1539 int (**opvec)(void *);
1540
1541 switch (ap->a_desc->vdesc_offset) {
1542 case VOP_ACCESS_DESCOFFSET:
1543 case VOP_GETATTR_DESCOFFSET:
1544 case VOP_SETATTR_DESCOFFSET:
1545 case VOP_LOCK_DESCOFFSET:
1546 case VOP_UNLOCK_DESCOFFSET:
1547 case VOP_ISLOCKED_DESCOFFSET:
1548 case VOP_RECLAIM_DESCOFFSET:
1549 opvec = rump_vnodeop_p;
1550 break;
1551 default:
1552 opvec = spec_vnodeop_p;
1553 break;
1554 }
1555
1556 return VOCALL(opvec, ap->a_desc->vdesc_offset, v);
1557 }
1558
1559 static int
1560 rump_vop_advlock(void *v)
1561 {
1562 struct vop_advlock_args /* {
1563 const struct vnodeop_desc *a_desc;
1564 struct vnode *a_vp;
1565 void *a_id;
1566 int a_op;
1567 struct flock *a_fl;
1568 int a_flags;
1569 } */ *ap = v;
1570 struct vnode *vp = ap->a_vp;
1571 struct rumpfs_node *rn = vp->v_data;
1572
1573 return lf_advlock(ap, &rn->rn_lockf, vp->v_size);
1574 }
1575
1576 /*
1577 * Begin vfs-level stuff
1578 */
1579
1580 VFS_PROTOS(rumpfs);
1581 struct vfsops rumpfs_vfsops = {
1582 .vfs_name = MOUNT_RUMPFS,
1583 .vfs_min_mount_data = 0,
1584 .vfs_mount = rumpfs_mount,
1585 .vfs_start = (void *)nullop,
1586 .vfs_unmount = rumpfs_unmount,
1587 .vfs_root = rumpfs_root,
1588 .vfs_quotactl = (void *)eopnotsupp,
1589 .vfs_statvfs = genfs_statvfs,
1590 .vfs_sync = (void *)nullop,
1591 .vfs_vget = rumpfs_vget,
1592 .vfs_fhtovp = (void *)eopnotsupp,
1593 .vfs_vptofh = (void *)eopnotsupp,
1594 .vfs_init = rumpfs_init,
1595 .vfs_reinit = NULL,
1596 .vfs_done = rumpfs_done,
1597 .vfs_mountroot = rumpfs_mountroot,
1598 .vfs_snapshot = (void *)eopnotsupp,
1599 .vfs_extattrctl = (void *)eopnotsupp,
1600 .vfs_suspendctl = (void *)eopnotsupp,
1601 .vfs_renamelock_enter = genfs_renamelock_enter,
1602 .vfs_renamelock_exit = genfs_renamelock_exit,
1603 .vfs_opv_descs = rump_opv_descs,
1604 /* vfs_refcount */
1605 /* vfs_list */
1606 };
1607
1608 static int
1609 rumpfs_mountfs(struct mount *mp)
1610 {
1611 struct rumpfs_mount *rfsmp;
1612 struct rumpfs_node *rn;
1613 int error;
1614
1615 rfsmp = kmem_alloc(sizeof(*rfsmp), KM_SLEEP);
1616
1617 rn = makeprivate(VDIR, NODEV, DEV_BSIZE, false);
1618 rn->rn_parent = rn;
1619 if ((error = makevnode(mp, rn, &rfsmp->rfsmp_rvp)) != 0)
1620 return error;
1621
1622 rfsmp->rfsmp_rvp->v_vflag |= VV_ROOT;
1623 VOP_UNLOCK(rfsmp->rfsmp_rvp);
1624
1625 mp->mnt_data = rfsmp;
1626 mp->mnt_stat.f_namemax = MAXNAMLEN;
1627 mp->mnt_stat.f_iosize = 512;
1628 mp->mnt_flag |= MNT_LOCAL;
1629 mp->mnt_iflag |= IMNT_MPSAFE | IMNT_CAN_RWTORO;
1630 mp->mnt_fs_bshift = DEV_BSHIFT;
1631 vfs_getnewfsid(mp);
1632
1633 return 0;
1634 }
1635
1636 int
1637 rumpfs_mount(struct mount *mp, const char *mntpath, void *arg, size_t *alen)
1638 {
1639 int error;
1640
1641 if (mp->mnt_flag & MNT_UPDATE) {
1642 return 0;
1643 }
1644
1645 error = set_statvfs_info(mntpath, UIO_USERSPACE, "rumpfs", UIO_SYSSPACE,
1646 mp->mnt_op->vfs_name, mp, curlwp);
1647 if (error)
1648 return error;
1649
1650 return rumpfs_mountfs(mp);
1651 }
1652
1653 int
1654 rumpfs_unmount(struct mount *mp, int mntflags)
1655 {
1656 struct rumpfs_mount *rfsmp = mp->mnt_data;
1657 int flags = 0, error;
1658
1659 if (panicstr || mntflags & MNT_FORCE)
1660 flags |= FORCECLOSE;
1661
1662 if ((error = vflush(mp, rfsmp->rfsmp_rvp, flags)) != 0)
1663 return error;
1664 vgone(rfsmp->rfsmp_rvp); /* XXX */
1665
1666 kmem_free(rfsmp, sizeof(*rfsmp));
1667
1668 return 0;
1669 }
1670
1671 int
1672 rumpfs_root(struct mount *mp, struct vnode **vpp)
1673 {
1674 struct rumpfs_mount *rfsmp = mp->mnt_data;
1675
1676 vref(rfsmp->rfsmp_rvp);
1677 vn_lock(rfsmp->rfsmp_rvp, LK_EXCLUSIVE | LK_RETRY);
1678 *vpp = rfsmp->rfsmp_rvp;
1679 return 0;
1680 }
1681
1682 int
1683 rumpfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1684 {
1685
1686 return EOPNOTSUPP;
1687 }
1688
1689 void
1690 rumpfs_init()
1691 {
1692
1693 CTASSERT(RUMP_ETFS_SIZE_ENDOFF == RUMPBLK_SIZENOTSET);
1694
1695 mutex_init(&reclock, MUTEX_DEFAULT, IPL_NONE);
1696 mutex_init(&etfs_lock, MUTEX_DEFAULT, IPL_NONE);
1697 }
1698
1699 void
1700 rumpfs_done()
1701 {
1702
1703 mutex_destroy(&reclock);
1704 mutex_destroy(&etfs_lock);
1705 }
1706
1707 int
1708 rumpfs_mountroot()
1709 {
1710 struct mount *mp;
1711 int error;
1712
1713 if ((error = vfs_rootmountalloc(MOUNT_RUMPFS, "rootdev", &mp)) != 0) {
1714 vrele(rootvp);
1715 return error;
1716 }
1717
1718 if ((error = rumpfs_mountfs(mp)) != 0)
1719 panic("mounting rootfs failed: %d", error);
1720
1721 mutex_enter(&mountlist_lock);
1722 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1723 mutex_exit(&mountlist_lock);
1724
1725 error = set_statvfs_info("/", UIO_SYSSPACE, "rumpfs", UIO_SYSSPACE,
1726 mp->mnt_op->vfs_name, mp, curlwp);
1727 if (error)
1728 panic("set_statvfs_info failed for rootfs: %d", error);
1729
1730 mp->mnt_flag &= ~MNT_RDONLY;
1731 vfs_unbusy(mp, false, NULL);
1732
1733 return 0;
1734 }
1735