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