putter.c revision 1.1 1 /* $NetBSD: putter.c,v 1.1 2007/11/12 14:30:56 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2006, 2007 Antti Kantee. All Rights Reserved.
5 *
6 * Development of this software was supported by the
7 * Ulla Tuominen Foundation and the Finnish Cultural Foundation and the
8 * Research Foundation of Helsinki University of Technology
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 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Pass-to-Userspace TransporTER: generic kernel-user request-response
34 * transport interface.
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: putter.c,v 1.1 2007/11/12 14:30:56 pooka Exp $");
39
40 #include <sys/param.h>
41 #include <sys/conf.h>
42 #include <sys/file.h>
43 #include <sys/filedesc.h>
44 #include <sys/kmem.h>
45 #include <sys/poll.h>
46 #include <sys/socketvar.h>
47
48 #include <dev/putter/puttervar.h>
49
50 #include <fs/puffs/puffs_msgif.h> /* XXX: for frame headers, goes away soon */
51
52 /*
53 * putter instance structures. these are always allocated and freed
54 * from the context of the transport user.
55 */
56 struct putter_instance {
57 pid_t pi_pid;
58 int pi_idx;
59 int pi_fd;
60 struct selinfo pi_sel;
61
62 void *pi_private;
63 struct putter_ops *pi_pop;
64
65 uint8_t *pi_curput;
66 size_t pi_curres;
67 void *pi_curopaq;
68
69 TAILQ_ENTRY(putter_instance) pi_entries;
70 };
71 #define PUTTER_EMBRYO ((void *)-1) /* before attach */
72 #define PUTTER_DEAD ((void *)-2) /* after detach */
73
74 static TAILQ_HEAD(, putter_instance) putter_ilist
75 = TAILQ_HEAD_INITIALIZER(putter_ilist);
76
77 static int get_pi_idx(struct putter_instance *);
78
79 #ifdef DEBUG
80 #ifndef PUTTERDEBUG
81 #define PUTTERDEBUG
82 #endif
83 #endif
84
85 #ifdef PUTTERDEBUG
86 static int putterdebug = 0;
87 #define DPRINTF(x) if (putterdebug > 0) printf x
88 #define DPRINTF_VERBOSE(x) if (putterdebug > 1) printf x
89 #else
90 #define DPRINTF(x)
91 #define DPRINTF_VERBOSE(x)
92 #endif
93
94 /*
95 * public init / deinit
96 */
97
98 /* protects both the list and the contents of the list elements */
99 static kmutex_t pi_mtx;
100
101 void putterattach(void);
102
103 void
104 putterattach()
105 {
106
107 mutex_init(&pi_mtx, MUTEX_DEFAULT, IPL_NONE);
108 }
109
110 #if 0
111 void
112 putter_destroy()
113 {
114
115 mutex_destroy(&pi_mtx);
116 }
117 #endif
118
119 /*
120 * fd routines, for cloner
121 */
122 static int putter_fop_read(struct file *, off_t *, struct uio *,
123 kauth_cred_t, int);
124 static int putter_fop_write(struct file *, off_t *, struct uio *,
125 kauth_cred_t, int);
126 static int putter_fop_ioctl(struct file*, u_long, void *, struct lwp *);
127 static int putter_fop_poll(struct file *, int, struct lwp *);
128 static int putter_fop_close(struct file *, struct lwp *);
129 static int putter_fop_kqfilter(struct file *, struct knote *);
130
131
132 static const struct fileops putter_fileops = {
133 putter_fop_read,
134 putter_fop_write,
135 putter_fop_ioctl,
136 fnullop_fcntl,
137 putter_fop_poll,
138 fbadop_stat,
139 putter_fop_close,
140 putter_fop_kqfilter
141 };
142
143 static int
144 putter_fop_read(struct file *fp, off_t *off, struct uio *uio,
145 kauth_cred_t cred, int flags)
146 {
147 struct putter_instance *pi = fp->f_data;
148 size_t origres, moved;
149 int error;
150
151 if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
152 printf("putter_fop_read: private %d not inited\n", pi->pi_idx);
153 return ENOENT;
154 }
155
156 if (pi->pi_curput == NULL) {
157 error = pi->pi_pop->pop_getout(pi->pi_private, uio->uio_resid,
158 fp->f_flag & O_NONBLOCK, &pi->pi_curput,
159 &pi->pi_curres, &pi->pi_curopaq);
160 if (error)
161 return error;
162 }
163
164 origres = uio->uio_resid;
165 error = uiomove(pi->pi_curput, pi->pi_curres, uio);
166 moved = origres - uio->uio_resid;
167 DPRINTF(("putter_fop_read (%p): moved %zu bytes from %p, error %d\n",
168 pi, moved, pi->pi_curput, error));
169
170 KASSERT(pi->pi_curres >= moved);
171 pi->pi_curres -= moved;
172 pi->pi_curput += moved;
173
174 if (pi->pi_curres == 0) {
175 pi->pi_pop->pop_releaseout(pi->pi_private,
176 pi->pi_curopaq, error);
177 pi->pi_curput = NULL;
178 }
179
180 return error;
181 }
182
183 static int
184 putter_fop_write(struct file *fp, off_t *off, struct uio *uio,
185 kauth_cred_t cred, int flags)
186 {
187 struct putter_instance *pi = fp->f_data;
188 struct puffs_frame pfr;
189 uint8_t *buf;
190 size_t frsize;
191 int error;
192
193 DPRINTF(("puffs_fop_write (%p): writing response, resid %zu\n",
194 pi->pi_private, uio->uio_resid));
195
196 if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
197 printf("putter_fop_write: putter %d not inited\n", pi->pi_idx);
198 return ENOENT;
199 }
200
201 error = uiomove(&pfr, sizeof(struct puffs_frame), uio);
202 if (error)
203 return error;
204
205 /* Sorry mate, the kernel doesn't buffer. */
206 frsize = pfr.pfr_len - sizeof(struct puffs_frame);
207 if (uio->uio_resid < frsize)
208 return EINVAL;
209
210 buf = kmem_alloc(frsize + sizeof(struct puffs_frame), KM_SLEEP);
211 memcpy(buf, &pfr, sizeof(pfr));
212 error = uiomove(buf+sizeof(struct puffs_frame), frsize, uio);
213 if (error == 0) {
214 pi->pi_pop->pop_dispatch(pi->pi_private, buf);
215 }
216 kmem_free(buf, frsize + sizeof(struct puffs_frame));
217
218 return error;
219 }
220
221 /*
222 * Poll query interface. The question is only if an event
223 * can be read from us (and by read I mean ioctl... ugh).
224 */
225 #define PUTTERPOLL_EVSET (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)
226 static int
227 putter_fop_poll(struct file *fp, int events, struct lwp *l)
228 {
229 struct putter_instance *pi = fp->f_data;
230 int revents;
231
232 if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
233 printf("putter_fop_ioctl: putter %d not inited\n", pi->pi_idx);
234 return ENOENT;
235 }
236
237 revents = events & (POLLOUT | POLLWRNORM | POLLWRBAND);
238 if ((events & PUTTERPOLL_EVSET) == 0)
239 return revents;
240
241 /* check queue */
242 if (pi->pi_pop->pop_waitcount(pi->pi_private))
243 revents |= PUTTERPOLL_EVSET;
244 else
245 selrecord(l, &pi->pi_sel);
246
247 return revents;
248 }
249
250 /*
251 * device close = forced unmount.
252 *
253 * unmounting is a frightfully complex operation to avoid races
254 */
255 static int
256 putter_fop_close(struct file *fp, struct lwp *l)
257 {
258 struct putter_instance *pi = fp->f_data;
259 int rv;
260
261 DPRINTF(("putter_fop_close: device closed\n"));
262
263 restart:
264 mutex_enter(&pi_mtx);
265 /*
266 * First check if the fs was never mounted. In that case
267 * remove the instance from the list. If mount is attempted later,
268 * it will simply fail.
269 */
270 if (pi->pi_private == PUTTER_EMBRYO) {
271 TAILQ_REMOVE(&putter_ilist, pi, pi_entries);
272 mutex_exit(&pi_mtx);
273
274 DPRINTF(("putter_fop_close: data associated with fp %p was "
275 "embryonic\n", fp));
276
277 goto out;
278 }
279
280 /*
281 * Next, analyze if unmount was called and the instance is dead.
282 * In this case we can just free the structure and go home, it
283 * was removed from the list by putter_rmprivate().
284 */
285 if (pi->pi_private == PUTTER_DEAD) {
286 mutex_exit(&pi_mtx);
287
288 DPRINTF(("putter_fop_close: putter associated with fp %p (%d) "
289 "dead, freeing\n", fp, pi->pi_idx));
290
291 goto out;
292 }
293
294 /*
295 * So we have a reference. Proceed to unwrap the file system.
296 */
297 mutex_exit(&pi_mtx);
298
299 /* hmm? suspicious locking? */
300 while ((rv = pi->pi_pop->pop_close(pi->pi_private)) == ERESTART)
301 goto restart;
302
303 out:
304 /*
305 * Finally, release the instance information. It was already
306 * removed from the list by putter_rmprivate() and we know it's
307 * dead, so no need to lock.
308 */
309 kmem_free(pi, sizeof(struct putter_instance));
310
311 return 0;
312 }
313
314 static int
315 putter_fop_ioctl(struct file *fp, u_long cmd, void *data, struct lwp *l)
316 {
317
318 /*
319 * work already done in sys_ioctl(). skip sanity checks to enable
320 * setting non-blocking fd without yet having mounted the fs
321 */
322 if (cmd == FIONBIO)
323 return 0;
324
325 return EINVAL;
326 }
327
328 /* kqueue stuff */
329
330 static void
331 filt_putterdetach(struct knote *kn)
332 {
333 struct putter_instance *pi = kn->kn_hook;
334
335 mutex_enter(&pi_mtx);
336 SLIST_REMOVE(&pi->pi_sel.sel_klist, kn, knote, kn_selnext);
337 mutex_exit(&pi_mtx);
338 }
339
340 static int
341 filt_putterioctl(struct knote *kn, long hint)
342 {
343 struct putter_instance *pi = kn->kn_hook;
344 int error;
345
346 error = 0;
347 mutex_enter(&pi_mtx);
348 if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD)
349 error = 1;
350 mutex_exit(&pi_mtx);
351 if (error)
352 return 0;
353
354 kn->kn_data = pi->pi_pop->pop_waitcount(pi->pi_private);
355
356 return kn->kn_data != 0;
357 }
358
359 static const struct filterops putterioctl_filtops =
360 { 1, NULL, filt_putterdetach, filt_putterioctl };
361
362 static int
363 putter_fop_kqfilter(struct file *fp, struct knote *kn)
364 {
365 struct putter_instance *pi = fp->f_data;
366 struct klist *klist;
367
368 if (kn->kn_filter != EVFILT_READ)
369 return 1;
370
371 klist = &pi->pi_sel.sel_klist;
372 kn->kn_fop = &putterioctl_filtops;
373 kn->kn_hook = pi;
374
375 mutex_enter(&pi_mtx);
376 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
377 mutex_exit(&pi_mtx);
378
379 return 0;
380 }
381
382
383 /*
384 * Device routines. These are for when /dev/puffs is initially
385 * opened before it has been cloned.
386 */
387
388 dev_type_open(puttercdopen);
389 dev_type_close(puttercdclose);
390 dev_type_ioctl(puttercdioctl);
391
392 /* dev */
393 const struct cdevsw putter_cdevsw = {
394 puttercdopen, puttercdclose, noread, nowrite,
395 noioctl, nostop, notty, nopoll,
396 nommap, nokqfilter, D_OTHER
397 };
398 int
399 puttercdopen(dev_t dev, int flags, int fmt, struct lwp *l)
400 {
401 struct putter_instance *pi;
402 struct file *fp;
403 int error, fd, idx;
404
405 /*
406 * XXX: decide on some security model and check permissions
407 */
408
409 if (minor(dev) != PUFFS_CLONER)
410 return ENXIO;
411
412 if ((error = falloc(l, &fp, &fd)) != 0)
413 return error;
414
415 pi = kmem_alloc(sizeof(struct putter_instance), KM_SLEEP);
416
417 mutex_enter(&pi_mtx);
418 idx = get_pi_idx(pi);
419 if (idx == PUFFS_CLONER) {
420 mutex_exit(&pi_mtx);
421 kmem_free(pi, sizeof(struct putter_instance));
422 FILE_UNUSE(fp, l);
423 ffree(fp);
424 return EBUSY;
425 }
426
427 pi->pi_pid = l->l_proc->p_pid;
428 pi->pi_idx = idx;
429 selinit(&pi->pi_sel);
430 pi->pi_curput = NULL;
431 pi->pi_curres = 0;
432 pi->pi_curopaq = NULL;
433 mutex_exit(&pi_mtx);
434
435 DPRINTF(("puttercdopen: registered embryonic pmp for pid: %d\n",
436 pi->pi_pid));
437
438 return fdclone(l, fp, fd, FREAD|FWRITE, &putter_fileops, pi);
439 }
440
441 int
442 puttercdclose(dev_t dev, int flags, int fmt, struct lwp *l)
443 {
444
445 panic("puttercdclose impossible\n");
446
447 return 0;
448 }
449
450
451 /*
452 * Set the private structure for the file descriptor. This is
453 * typically done immediately when the counterpart has knowledge
454 * about the private structure's address and the file descriptor
455 * (e.g. vfs mount routine).
456 *
457 * We only want to make sure that the caller had the right to open the
458 * device, we don't so much care about which context it gets in case
459 * the same process opened multiple (since they are equal at this point).
460 */
461 struct putter_instance *
462 putter_attach(pid_t pid, int fd, void *ppriv, struct putter_ops *pop)
463 {
464 struct putter_instance *pi = NULL;
465
466 mutex_enter(&pi_mtx);
467 TAILQ_FOREACH(pi, &putter_ilist, pi_entries) {
468 if (pi->pi_pid == pid && pi->pi_private == PUTTER_EMBRYO) {
469 pi->pi_private = ppriv;
470 pi->pi_fd = fd;
471 pi->pi_pop = pop;
472 break;
473 }
474 }
475 mutex_exit(&pi_mtx);
476
477 DPRINTF(("putter_setprivate: pi at %p (%d/%d)\n", pi,
478 pi ? pi->pi_pid : 0, pi ? pi->pi_fd : 0));
479
480 return pi;
481 }
482
483 /*
484 * Remove fp <-> private mapping.
485 */
486 void
487 putter_detach(struct putter_instance *pi)
488 {
489
490 mutex_enter(&pi_mtx);
491 TAILQ_REMOVE(&putter_ilist, pi, pi_entries);
492 pi->pi_private = PUTTER_DEAD;
493 mutex_exit(&pi_mtx);
494
495 DPRINTF(("putter_nukebypmp: nuked %p\n", pi));
496 }
497
498 void
499 putter_notify(struct putter_instance *pi)
500 {
501
502 selnotify(&pi->pi_sel, 0);
503 }
504
505 /* search sorted list of instances for free minor, sorted insert arg */
506 static int
507 get_pi_idx(struct putter_instance *pi_i)
508 {
509 struct putter_instance *pi;
510 int i;
511
512 i = 0;
513 TAILQ_FOREACH(pi, &putter_ilist, pi_entries) {
514 if (i == PUFFS_CLONER)
515 return PUFFS_CLONER;
516 if (i != pi->pi_idx)
517 break;
518 i++;
519 }
520
521 pi_i->pi_private = PUTTER_EMBRYO;
522
523 if (pi == NULL)
524 TAILQ_INSERT_TAIL(&putter_ilist, pi_i, pi_entries);
525 else
526 TAILQ_INSERT_BEFORE(pi, pi_i, pi_entries);
527
528 return i;
529 }
530