puffs_vfsops.c revision 1.2 1 /* $NetBSD: puffs_vfsops.c,v 1.2 2006/10/26 13:35: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.2 2006/10/26 13:35: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 pn = NULL;
156 if (pmp->pmp_root) {
157 pn = VPTOPP(pmp->pmp_root);
158 pn->pn_cookie = sreq->psr_cookie;
159 }
160
161 /* We're good to fly */
162 pmp->pmp_rootcookie = sreq->psr_cookie;
163 pmp->pmp_status = PUFFSTAT_RUNNING;
164 sreq->psr_fsidx = mp->mnt_stat.f_fsidx;
165
166 simple_unlock(&pmp->pmp_lock);
167
168 DPRINTF(("puffs_start2: root vp %p, cur root pnode %p, cookie %p\n",
169 pmp->pmp_root, pn, sreq->psr_cookie));
170
171 return 0;
172 }
173
174 int
175 puffs_start(struct mount *mp, int flags, struct lwp *l)
176 {
177
178 /*
179 * This cannot travel to userspace, as this is called from
180 * the kernel context of the process doing mount(2). But
181 * it's probably a safe bet that the process doing mount(2)
182 * realizes it needs to start the filesystem also...
183 */
184 return 0;
185 }
186
187 int
188 puffs_unmount(struct mount *mp, int mntflags, struct lwp *l)
189 {
190 struct puffs_mount *pmp;
191 int error, force;
192
193 PUFFS_VFSREQ(unmount);
194
195 error = 0;
196 force = mntflags & MNT_FORCE;
197 pmp = MPTOPUFFSMP(mp);
198
199 DPRINTF(("puffs_unmount: detach filesystem from vfs, current "
200 "status 0x%x\n", pmp->pmp_status));
201
202 /*
203 * flush all the vnodes. VOP_RECLAIM() takes care that the
204 * root vnode does not get flushed until unmount. The
205 * userspace root node cookie is stored in the mount
206 * structure, so we can always re-instantiate a root vnode,
207 * should userspace unmount decide it doesn't want to
208 * cooperate.
209 */
210 error = vflush(mp, NULLVP, force ? FORCECLOSE : 0);
211 if (error)
212 goto out;
213
214 /*
215 * If we are not DYING, we should ask userspace's opinion
216 * about the situation
217 */
218 if (pmp->pmp_status != PUFFSTAT_DYING) {
219 unmount_arg.pvfsr_flags = mntflags;
220 unmount_arg.pvfsr_pid = puffs_lwp2pid(l);
221 error = puffs_vfstouser(pmp, PUFFS_VFS_UNMOUNT,
222 &unmount_arg, sizeof(unmount_arg));
223 }
224
225 /*
226 * if userspace cooperated or we really need to die,
227 * screw what userland thinks and just die.
228 */
229 DPRINTF(("puffs_unmount: error %d force %d\n", error, force));
230 if (error == 0 || force) {
231 pmp->pmp_status = PUFFSTAT_DYING;
232 puffs_nukebypmp(pmp);
233 FREE(pmp, M_PUFFS);
234 error = 0;
235 }
236
237 out:
238 DPRINTF(("puffs_unmount: return %d\n", error));
239 return error;
240 }
241
242 /*
243 * This doesn't need to travel to userspace
244 */
245 int
246 puffs_root(struct mount *mp, struct vnode **vpp)
247 {
248 struct puffs_mount *pmp;
249 struct puffs_node *pn;
250 struct vnode *vp;
251
252 pmp = MPTOPUFFSMP(mp);
253
254 /*
255 * pmp_lock must be held if vref()'ing or vrele()'ing the
256 * root vnode.
257 */
258 simple_lock(&pmp->pmp_lock);
259 vp = pmp->pmp_root;
260 if (vp) {
261 pn = VPTOPP(vp);
262 if (pn->pn_stat & PNODE_INACTIVE) {
263 if (vget(vp, LK_NOWAIT)) {
264 pmp->pmp_root = NULL;
265 goto grabnew;
266 }
267 } else
268 vref(vp);
269 simple_unlock(&pmp->pmp_lock);
270 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
271 *vpp = vp;
272 return 0;
273 }
274 grabnew:
275 simple_unlock(&pmp->pmp_lock);
276
277 /*
278 * So, didn't have the magic root vnode available.
279 * No matter, grab another an stuff it with the cookie.
280 */
281 if (puffs_getvnode(mp, pmp->pmp_rootcookie, &vp))
282 panic("sloppy programming");
283
284 simple_lock(&pmp->pmp_lock);
285 /*
286 * check if by mysterious force someone else created a root
287 * vnode while we were executing.
288 */
289 if (pmp->pmp_root) {
290 vref(pmp->pmp_root);
291 simple_unlock(&pmp->pmp_lock);
292 puffs_putvnode(vp);
293 vn_lock(pmp->pmp_root, LK_EXCLUSIVE | LK_RETRY);
294 *vpp = pmp->pmp_root;
295 return 0;
296 }
297
298 /* store cache */
299 vp->v_type = VDIR;
300 vp->v_flag = VROOT;
301 pmp->pmp_root = vp;
302 simple_unlock(&pmp->pmp_lock);
303
304 vn_lock(pmp->pmp_root, LK_EXCLUSIVE | LK_RETRY);
305
306 *vpp = vp;
307 return 0;
308 }
309
310 int
311 puffs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg, struct lwp *l)
312 {
313
314 return EOPNOTSUPP;
315 }
316
317 int
318 puffs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
319 {
320 struct puffs_vfsreq_statvfs *statvfs_arg; /* too big for stack */
321 struct puffs_mount *pmp;
322 int error = 0;
323
324 pmp = MPTOPUFFSMP(mp);
325
326 /*
327 * If we are mounting, it means that the userspace counterpart
328 * is calling mount(2), but mount(2) also calls statvfs. So
329 * requesting statvfs from userspace would mean a deadlock.
330 * Compensate.
331 */
332 if (pmp->pmp_status == PUFFSTAT_MOUNTING)
333 return EINPROGRESS;
334
335 /* too big for stack */
336 MALLOC(statvfs_arg, struct puffs_vfsreq_statvfs *,
337 sizeof(struct puffs_vfsreq_statvfs), M_PUFFS, M_WAITOK | M_ZERO);
338 statvfs_arg->pvfsr_pid = puffs_lwp2pid(l);
339
340 error = puffs_vfstouser(pmp, PUFFS_VFS_STATVFS,
341 statvfs_arg, sizeof(*statvfs_arg));
342
343 /*
344 * Try to produce a sensible result even in the event
345 * of userspace error.
346 *
347 * XXX: cache the copy in non-error case
348 */
349 if (!error) {
350 copy_statvfs_info(&statvfs_arg->pvfsr_sb, mp);
351 (void)memcpy(sbp, &statvfs_arg->pvfsr_sb,
352 sizeof(struct statvfs));
353 } else {
354 copy_statvfs_info(sbp, mp);
355 }
356
357 FREE(statvfs_arg, M_PUFFS);
358
359 return 0;
360 }
361
362 int
363 puffs_sync(struct mount *mp, int waitfor, struct kauth_cred *cred,
364 struct lwp *l)
365 {
366 int error;
367
368 PUFFS_VFSREQ(sync);
369
370 sync_arg.pvfsr_waitfor = waitfor;
371 puffs_credcvt(&sync_arg.pvfsr_cred, cred);
372 sync_arg.pvfsr_pid = puffs_lwp2pid(l);
373
374 error = puffs_vfstouser(MPTOPUFFSMP(mp), PUFFS_VFS_SYNC,
375 &sync_arg, sizeof(sync_arg));
376
377 return error;
378 }
379
380 int
381 puffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
382 {
383
384 return EOPNOTSUPP;
385 }
386
387 #if 0
388 /*ARGSUSED*/
389 int
390 puffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
391 {
392
393 return EOPNOTSUPP;
394 }
395
396 /*ARGSUSED*/
397 int
398 puffs_vptofh(struct vnode *vp, struct fid *fhp)
399 {
400
401 return EOPNOTSUPP;
402 }
403 #endif
404
405 void
406 puffs_init()
407 {
408
409 return;
410 }
411
412 void
413 puffs_done()
414 {
415
416 return;
417 }
418
419 int
420 puffs_snapshot(struct mount *mp, struct vnode *vp, struct timespec *ts)
421 {
422
423 return EOPNOTSUPP;
424 }
425
426 extern const struct vnodeopv_desc puffs_vnodeop_opv_desc;
427
428 const struct vnodeopv_desc * const puffs_vnodeopv_descs[] = {
429 &puffs_vnodeop_opv_desc,
430 NULL,
431 };
432
433 struct vfsops puffs_vfsops = {
434 MOUNT_PUFFS,
435 puffs_mount, /* mount */
436 puffs_start, /* start */
437 puffs_unmount, /* unmount */
438 puffs_root, /* root */
439 puffs_quotactl, /* quotactl */
440 puffs_statvfs, /* statvfs */
441 puffs_sync, /* sync */
442 puffs_vget, /* vget */
443 NULL, /* fhtovp */
444 NULL, /* vptofh */
445 puffs_init, /* init */
446 NULL, /* reinit */
447 puffs_done, /* done */
448 NULL, /* mountroot */
449 puffs_snapshot, /* snapshot */
450 vfs_stdextattrctl, /* extattrctl */
451 puffs_vnodeopv_descs, /* vnodeops */
452 0, /* refcount */
453 { NULL, NULL }
454 };
455 VFS_ATTACH(puffs_vfsops);
456