rumpfs.c revision 1.10 1 /* $NetBSD: rumpfs.c,v 1.10 2009/03/20 08:30:52 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
5 *
6 * Development of this software was supported by Google Summer of Code.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: rumpfs.c,v 1.10 2009/03/20 08:30:52 pooka Exp $");
32
33 #include <sys/param.h>
34 #include <sys/mount.h>
35 #include <sys/vnode.h>
36 #include <sys/errno.h>
37 #include <sys/kauth.h>
38 #include <sys/lock.h>
39 #include <sys/lockf.h>
40 #include <sys/stat.h>
41 #include <sys/namei.h>
42 #include <sys/queue.h>
43 #include <sys/filedesc.h>
44 #include <sys/syscallargs.h>
45 #include <sys/atomic.h>
46
47 #include <miscfs/fifofs/fifo.h>
48 #include <miscfs/specfs/specdev.h>
49 #include <miscfs/genfs/genfs.h>
50
51 #include <rump/rumpuser.h>
52
53 #include "rump_private.h"
54 #include "rump_vfs_private.h"
55
56 static int rump_vop_lookup(void *);
57 static int rump_vop_getattr(void *);
58 static int rump_vop_success(void *);
59
60 int (**dead_vnodeop_p)(void *);
61 const struct vnodeopv_entry_desc dead_vnodeop_entries[] = {
62 { &vop_default_desc, vn_default_error },
63 { NULL, NULL }
64 };
65 const struct vnodeopv_desc dead_vnodeop_opv_desc =
66 { &dead_vnodeop_p, dead_vnodeop_entries };
67
68 int (**fifo_vnodeop_p)(void *);
69 const struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
70 { &vop_default_desc, vn_default_error },
71 { NULL, NULL }
72 };
73 const struct vnodeopv_desc fifo_vnodeop_opv_desc =
74 { &fifo_vnodeop_p, fifo_vnodeop_entries };
75
76 int (**rump_vnodeop_p)(void *);
77 const struct vnodeopv_entry_desc rump_vnodeop_entries[] = {
78 { &vop_default_desc, vn_default_error },
79 { &vop_lookup_desc, rump_vop_lookup },
80 { &vop_getattr_desc, rump_vop_getattr },
81 { &vop_putpages_desc, rump_vop_success },
82 { &vop_fsync_desc, rump_vop_success },
83 { &vop_lock_desc, genfs_lock },
84 { &vop_unlock_desc, genfs_unlock },
85 { NULL, NULL }
86 };
87 const struct vnodeopv_desc rump_vnodeop_opv_desc =
88 { &rump_vnodeop_p, rump_vnodeop_entries };
89 const struct vnodeopv_desc * const rump_opv_descs[] = {
90 &rump_vnodeop_opv_desc,
91 NULL
92 };
93
94 static struct mount rump_mnt;
95 static int lastino = 1;
96
97 static struct vattr *
98 makevattr(enum vtype vt)
99 {
100 struct vattr *va;
101 struct timespec ts;
102
103 nanotime(&ts);
104
105 va = kmem_alloc(sizeof(*va), KM_SLEEP);
106 va->va_type = vt;
107 va->va_mode = 0755;
108 va->va_nlink = 2;
109 va->va_uid = 0;
110 va->va_gid = 0;
111 va->va_fsid =
112 va->va_fileid = atomic_inc_uint_nv(&lastino);
113 va->va_size = 512;
114 va->va_blocksize = 512;
115 va->va_atime = ts;
116 va->va_mtime = ts;
117 va->va_ctime = ts;
118 va->va_birthtime = ts;
119 va->va_gen = 0;
120 va->va_flags = 0;
121 va->va_rdev = -1;
122 va->va_bytes = 512;
123 va->va_filerev = 0;
124 va->va_vaflags = 0;
125
126 return va;
127 }
128
129 static int
130 rump_makevnode(const char *path, size_t size, enum vtype vt, struct vnode **vpp)
131 {
132 struct vnode *vp;
133 int rv;
134
135 vp = kmem_alloc(sizeof(struct vnode), KM_SLEEP);
136 vp->v_size = vp->v_writesize = size;
137 vp->v_type = vt;
138 if (vp->v_type == VREG)
139 vp->v_type = VBLK;
140
141 if (vp->v_type != VBLK && vp->v_type != VDIR)
142 panic("rump_makevnode: only VBLK/VDIR vnodes supported");
143
144 if (vp->v_type == VBLK) {
145 rv = rumpblk_register(path);
146 if (rv == -1) {
147 rv = EINVAL;
148 goto bad;
149 }
150 spec_node_init(vp, makedev(RUMPBLK, rv));
151 vp->v_op = spec_vnodeop_p;
152 } else {
153 vp->v_op = rump_vnodeop_p;
154 }
155 vp->v_tag = VT_RUMP;
156 vp->v_mount = &rump_mnt;
157 vp->v_vnlock = &vp->v_lock;
158 vp->v_usecount = 1;
159 vp->v_data = makevattr(vp->v_type);
160 mutex_init(&vp->v_interlock, MUTEX_DEFAULT, IPL_NONE);
161 memset(&vp->v_lock, 0, sizeof(vp->v_lock));
162 rw_init(&vp->v_lock.vl_lock);
163 cv_init(&vp->v_cv, "vnode");
164 *vpp = vp;
165
166 return 0;
167
168 bad:
169 kmem_free(vp, sizeof(*vp));
170 return rv;
171 }
172
173 /*
174 * Simple lookup for faking lookup of device entry for rump file systems
175 */
176 static int
177 rump_vop_lookup(void *v)
178 {
179 struct vop_lookup_args /* {
180 struct vnode *a_dvp;
181 struct vnode **a_vpp;
182 struct componentname *a_cnp;
183 }; */ *ap = v;
184 struct componentname *cnp = ap->a_cnp;
185 uint64_t fsize;
186 enum vtype vt;
187 int rv, error, ft;
188
189 /* we handle only some "non-special" cases */
190 KASSERT(cnp->cn_nameiop == LOOKUP);
191 KASSERT(cnp->cn_flags & FOLLOW);
192 KASSERT((cnp->cn_flags & ISDOTDOT) == 0);
193 KASSERT(cnp->cn_namelen != 0 && cnp->cn_pnbuf[0] != '.');
194
195 rv = rumpuser_getfileinfo(cnp->cn_pnbuf, &fsize, &ft, &error);
196 if (rv)
197 return error;
198 switch (ft) {
199 case RUMPUSER_FT_DIR:
200 vt = VDIR;
201 break;
202 case RUMPUSER_FT_REG:
203 vt = VREG;
204 break;
205 case RUMPUSER_FT_BLK:
206 vt = VBLK;
207 break;
208 default:
209 vt = VBAD;
210 break;
211 }
212
213 error = rump_makevnode(cnp->cn_pnbuf, fsize, vt, ap->a_vpp);
214 if (error)
215 return error;
216
217 vn_lock(*ap->a_vpp, LK_RETRY | LK_EXCLUSIVE);
218 cnp->cn_consume = strlen(cnp->cn_nameptr + cnp->cn_namelen);
219 cnp->cn_flags &= ~REQUIREDIR;
220
221 return 0;
222 }
223
224 static int
225 rump_vop_getattr(void *v)
226 {
227 struct vop_getattr_args /* {
228 struct vnode *a_vp;
229 struct vattr *a_vap;
230 kauth_cred_t a_cred;
231 } */ *ap = v;
232
233 memcpy(ap->a_vap, ap->a_vp->v_data, sizeof(struct vattr));
234 return 0;
235 }
236
237 static int
238 rump_vop_success(void *v)
239 {
240
241 return 0;
242 }
243
244 void
245 rumpfs_init(void)
246 {
247 int rv;
248
249 vfs_opv_init(rump_opv_descs);
250 rv = rump_makevnode("/", 0, VDIR, &rootvnode);
251 if (rv)
252 panic("could not create root vnode: %d", rv);
253 rootvnode->v_vflag |= VV_ROOT;
254 }
255