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