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