puffs_vfsops.c revision 1.1 1 /* $NetBSD: puffs_vfsops.c,v 1.1 2006/10/22 22:43:23 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2005, 2006 Antti Kantee. All Rights Reserved.
5 *
6 * Development of this software was supported by the
7 * Google Summer of Code program and the Ulla Tuominen Foundation.
8 * The Google SoC project was mentored by Bill Studenmund.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the company nor the name of the author may be used to
19 * endorse or promote products derived from this software without specific
20 * prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
23 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: puffs_vfsops.c,v 1.1 2006/10/22 22:43:23 pooka Exp $");
37
38 #include <sys/param.h>
39 #include <sys/mount.h>
40 #include <sys/malloc.h>
41 #include <sys/extattr.h>
42 #include <sys/queue.h>
43 #include <sys/vnode.h>
44 #include <sys/dirent.h>
45 #include <sys/kauth.h>
46
47 #include <lib/libkern/libkern.h>
48
49 #include <fs/puffs/puffs_msgif.h>
50 #include <fs/puffs/puffs_sys.h>
51
52 VFS_PROTOS(puffs);
53
54 MALLOC_DEFINE(M_PUFFS, "puffs", "pass-to-userspace file system structures");
55
56 int
57 puffs_mount(struct mount *mp, const char *path, void *data,
58 struct nameidata *ndp, struct lwp *l)
59 {
60 struct puffs_mount *pmp;
61 struct puffs_args args;
62 char namebuf[PUFFSNAMESIZE+sizeof("puffs:")+1]; /* do I get a prize? */
63 int error;
64
65 if (mp->mnt_flag & MNT_GETARGS) {
66 pmp = MPTOPUFFSMP(mp);
67 return copyout(&pmp->pmp_args, data, sizeof(struct puffs_args));
68 }
69
70 /* update is not supported currently */
71 if (mp->mnt_flag & MNT_UPDATE)
72 return EOPNOTSUPP;
73
74 /*
75 * We need the file system name
76 */
77 if (!data)
78 return EINVAL;
79
80 error = copyin(data, &args, sizeof(struct puffs_args));
81 if (error)
82 return error;
83
84 /* build real name */
85 (void)strlcpy(namebuf, "puffs:", sizeof(namebuf));
86 (void)strlcat(namebuf, args.pa_name, sizeof(namebuf));
87
88 /* inform user server if it got the max request size it wanted */
89 if (args.pa_maxreqlen == 0 || args.pa_maxreqlen > PUFFS_REQ_MAXSIZE)
90 args.pa_maxreqlen = PUFFS_REQ_MAXSIZE;
91 else if (args.pa_maxreqlen < PUFFS_REQSTRUCT_MAX)
92 args.pa_maxreqlen = PUFFS_REQSTRUCT_MAX;
93 (void)strlcpy(args.pa_name, namebuf, sizeof(args.pa_name));
94
95 error = copyout(&args, data, sizeof(struct puffs_args));
96 if (error)
97 return error;
98
99 error = set_statvfs_info(path, UIO_USERSPACE, namebuf,
100 UIO_SYSSPACE, mp, l);
101 if (error)
102 return error;
103
104 MALLOC(pmp, struct puffs_mount *, sizeof(struct puffs_mount),
105 M_PUFFS, M_WAITOK | M_ZERO);
106
107 mp->mnt_data = pmp;
108 pmp->pmp_status = PUFFSTAT_MOUNTING;
109 pmp->pmp_nextreq = 0;
110 pmp->pmp_mp = mp;
111 pmp->pmp_req_maxsize = args.pa_maxreqlen;
112 pmp->pmp_args = args;
113
114 /*
115 * Inform the fileops processing code that we have a mountpoint.
116 * If it doesn't know about anyone with our pid/fd having the
117 * device open, punt
118 */
119 if (puffs_setpmp(l->l_proc->p_pid, args.pa_fd, pmp)) {
120 FREE(pmp, M_PUFFS);
121 return ENOENT;
122 }
123
124 simple_lock_init(&pmp->pmp_lock);
125 TAILQ_INIT(&pmp->pmp_req_touser);
126 TAILQ_INIT(&pmp->pmp_req_replywait);
127 TAILQ_INIT(&pmp->pmp_req_sizepark);
128
129 DPRINTF(("puffs_mount: mount point at %p, puffs specific at %p\n",
130 mp, MPTOPUFFSMP(mp)));
131
132 vfs_getnewfsid(mp);
133
134 return 0;
135 }
136
137 /*
138 * This is called from the first "Hello, I'm alive" ioctl
139 * from userspace.
140 */
141 int
142 puffs_start2(struct puffs_mount *pmp, struct puffs_vfsreq_start *sreq)
143 {
144 struct puffs_node *pn;
145 struct mount *mp;
146
147 mp = PMPTOMP(pmp);
148
149 simple_lock(&pmp->pmp_lock);
150
151 /*
152 * if someone has issued a VFS_ROOT() already, fill in the
153 * vnode cookie.
154 */
155 if (pmp->pmp_root) {
156 pn = VPTOPP(pmp->pmp_root);
157 pn->pn_cookie = sreq->psr_cookie;
158 }
159
160 /* We're good to fly */
161 pmp->pmp_rootcookie = sreq->psr_cookie;
162 pmp->pmp_status = PUFFSTAT_RUNNING;
163 sreq->psr_fsidx = mp->mnt_stat.f_fsidx;
164
165 simple_unlock(&pmp->pmp_lock);
166
167 DPRINTF(("puffs_start2: root vp %p, root pnode %p, cookie %p\n",
168 pmp->pmp_root, pn, sreq->psr_cookie));
169
170 return 0;
171 }
172
173 int
174 puffs_start(struct mount *mp, int flags, struct lwp *l)
175 {
176
177 /*
178 * This cannot travel to userspace, as this is called from
179 * the kernel context of the process doing mount(2). But
180 * it's probably a safe bet that the process doing mount(2)
181 * realizes it needs to start the filesystem also...
182 */
183 return 0;
184 }
185
186 int
187 puffs_unmount(struct mount *mp, int mntflags, struct lwp *l)
188 {
189 struct puffs_mount *pmp;
190 int error, force;
191
192 PUFFS_VFSREQ(unmount);
193
194 error = 0;
195 force = mntflags & MNT_FORCE;
196 pmp = MPTOPUFFSMP(mp);
197
198 DPRINTF(("puffs_unmount: detach filesystem from vfs, current "
199 "status 0x%x\n", pmp->pmp_status));
200
201 /*
202 * flush all the vnodes. VOP_RECLAIM() takes care that the
203 * root vnode does not get flushed until unmount. The
204 * userspace root node cookie is stored in the mount
205 * structure, so we can always re-instantiate a root vnode,
206 * should userspace unmount decide it doesn't want to
207 * cooperate.
208 */
209 error = vflush(mp, NULLVP, force ? FORCECLOSE : 0);
210 if (error)
211 goto out;
212
213 /*
214 * If we are not DYING, we should ask userspace's opinion
215 * about the situation
216 */
217 if (pmp->pmp_status != PUFFSTAT_DYING) {
218 unmount_arg.pvfsr_flags = mntflags;
219 unmount_arg.pvfsr_pid = puffs_lwp2pid(l);
220 error = puffs_vfstouser(pmp, PUFFS_VFS_UNMOUNT,
221 &unmount_arg, sizeof(unmount_arg));
222 }
223
224 /*
225 * if userspace cooperated or we really need to die,
226 * screw what userland thinks and just die.
227 */
228 DPRINTF(("puffs_umount: error %d force %d\n", error, force));
229 if (error == 0 || force) {
230 pmp->pmp_status = PUFFSTAT_DYING;
231 puffs_nukebypmp(pmp);
232 FREE(pmp, M_PUFFS);
233 error = 0;
234 }
235
236 out:
237 DPRINTF(("puffs_umount: return %d\n", error));
238 return error;
239 }
240
241 /*
242 * This doesn't need to travel to userspace
243 */
244 int
245 puffs_root(struct mount *mp, struct vnode **vpp)
246 {
247 struct puffs_mount *pmp;
248 struct puffs_node *pn;
249 struct vnode *vp;
250
251 pmp = MPTOPUFFSMP(mp);
252
253 /*
254 * pmp_lock must be held if vref()'ing or vrele()'ing the
255 * root vnode.
256 */
257 simple_lock(&pmp->pmp_lock);
258 vp = pmp->pmp_root;
259 if (vp) {
260 pn = VPTOPP(vp);
261 if (pn->pn_stat & PNODE_INACTIVE) {
262 if (vget(vp, LK_NOWAIT)) {
263 pmp->pmp_root = NULL;
264 goto grabnew;
265 }
266 } else
267 vref(vp);
268 simple_unlock(&pmp->pmp_lock);
269 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
270 *vpp = vp;
271 return 0;
272 }
273 grabnew:
274 simple_unlock(&pmp->pmp_lock);
275
276 /*
277 * So, didn't have the magic root vnode available.
278 * No matter, grab another an stuff it with the cookie.
279 */
280 if (puffs_getvnode(mp, pmp->pmp_rootcookie, &vp))
281 panic("sloppy programming");
282
283 simple_lock(&pmp->pmp_lock);
284 /*
285 * check if by mysterious force someone else created a root
286 * vnode while we were executing.
287 */
288 if (pmp->pmp_root) {
289 vref(pmp->pmp_root);
290 simple_unlock(&pmp->pmp_lock);
291 puffs_putvnode(vp);
292 vn_lock(pmp->pmp_root, LK_EXCLUSIVE | LK_RETRY);
293 *vpp = pmp->pmp_root;
294 return 0;
295 }
296
297 /* store cache */
298 vp->v_type = VDIR;
299 vp->v_flag = VROOT;
300 pmp->pmp_root = vp;
301 simple_unlock(&pmp->pmp_lock);
302
303 vn_lock(pmp->pmp_root, LK_EXCLUSIVE | LK_RETRY);
304
305 *vpp = vp;
306 return 0;
307 }
308
309 int
310 puffs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg, struct lwp *l)
311 {
312
313 return EOPNOTSUPP;
314 }
315
316 int
317 puffs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
318 {
319 struct puffs_vfsreq_statvfs *statvfs_arg; /* too big for stack */
320 struct puffs_mount *pmp;
321 int error = 0;
322
323 pmp = MPTOPUFFSMP(mp);
324
325 /*
326 * If we are mounting, it means that the userspace counterpart
327 * is calling mount(2), but mount(2) also calls statvfs. So
328 * requesting statvfs from userspace would mean a deadlock.
329 * Compensate.
330 */
331 if (pmp->pmp_status == PUFFSTAT_MOUNTING)
332 return EINPROGRESS;
333
334 /* too big for stack */
335 MALLOC(statvfs_arg, struct puffs_vfsreq_statvfs *,
336 sizeof(struct puffs_vfsreq_statvfs), M_PUFFS, M_WAITOK | M_ZERO);
337 statvfs_arg->pvfsr_pid = puffs_lwp2pid(l);
338
339 error = puffs_vfstouser(pmp, PUFFS_VFS_STATVFS,
340 statvfs_arg, sizeof(*statvfs_arg));
341
342 /*
343 * Try to produce a sensible result even in the event
344 * of userspace error.
345 *
346 * XXX: cache the copy in non-error case
347 */
348 if (!error) {
349 copy_statvfs_info(&statvfs_arg->pvfsr_sb, mp);
350 (void)memcpy(sbp, &statvfs_arg->pvfsr_sb,
351 sizeof(struct statvfs));
352 } else {
353 copy_statvfs_info(sbp, mp);
354 }
355
356 FREE(statvfs_arg, M_PUFFS);
357
358 return 0;
359 }
360
361 int
362 puffs_sync(struct mount *mp, int waitfor, struct kauth_cred *cred,
363 struct lwp *l)
364 {
365 int error;
366
367 PUFFS_VFSREQ(sync);
368
369 sync_arg.pvfsr_waitfor = waitfor;
370 puffs_credcvt(&sync_arg.pvfsr_cred, cred);
371 sync_arg.pvfsr_pid = puffs_lwp2pid(l);
372
373 error = puffs_vfstouser(MPTOPUFFSMP(mp), PUFFS_VFS_SYNC,
374 &sync_arg, sizeof(sync_arg));
375
376 return error;
377 }
378
379 int
380 puffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
381 {
382
383 return EOPNOTSUPP;
384 }
385
386 #if 0
387 /*ARGSUSED*/
388 int
389 puffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
390 {
391
392 return EOPNOTSUPP;
393 }
394
395 /*ARGSUSED*/
396 int
397 puffs_vptofh(struct vnode *vp, struct fid *fhp)
398 {
399
400 return EOPNOTSUPP;
401 }
402 #endif
403
404 void
405 puffs_init()
406 {
407
408 return;
409 }
410
411 void
412 puffs_done()
413 {
414
415 return;
416 }
417
418 int
419 puffs_snapshot(struct mount *mp, struct vnode *vp, struct timespec *ts)
420 {
421
422 return EOPNOTSUPP;
423 }
424
425 extern const struct vnodeopv_desc puffs_vnodeop_opv_desc;
426
427 const struct vnodeopv_desc * const puffs_vnodeopv_descs[] = {
428 &puffs_vnodeop_opv_desc,
429 NULL,
430 };
431
432 struct vfsops puffs_vfsops = {
433 MOUNT_PUFFS,
434 puffs_mount, /* mount */
435 puffs_start, /* start */
436 puffs_unmount, /* unmount */
437 puffs_root, /* root */
438 puffs_quotactl, /* quotactl */
439 puffs_statvfs, /* statvfs */
440 puffs_sync, /* sync */
441 puffs_vget, /* vget */
442 NULL, /* fhtovp */
443 NULL, /* vptofh */
444 puffs_init, /* init */
445 NULL, /* reinit */
446 puffs_done, /* done */
447 NULL, /* mountroot */
448 puffs_snapshot, /* snapshot */
449 vfs_stdextattrctl, /* extattrctl */
450 puffs_vnodeopv_descs, /* vnodeops */
451 0, /* refcount */
452 { NULL, NULL }
453 };
454 VFS_ATTACH(puffs_vfsops);
455