putter.c revision 1.3 1 /* $NetBSD: putter.c,v 1.3 2007/11/12 17:42:13 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.3 2007/11/12 17:42:13 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/putter_sys.h>
49
50 /*
51 * putter instance structures. these are always allocated and freed
52 * from the context of the transport user.
53 */
54 struct putter_instance {
55 pid_t pi_pid;
56 int pi_idx;
57 int pi_fd;
58 struct selinfo pi_sel;
59
60 void *pi_private;
61 struct putter_ops *pi_pop;
62
63 uint8_t *pi_curput;
64 size_t pi_curres;
65 void *pi_curopaq;
66
67 TAILQ_ENTRY(putter_instance) pi_entries;
68 };
69 #define PUTTER_EMBRYO ((void *)-1) /* before attach */
70 #define PUTTER_DEAD ((void *)-2) /* after detach */
71
72 static TAILQ_HEAD(, putter_instance) putter_ilist
73 = TAILQ_HEAD_INITIALIZER(putter_ilist);
74
75 static int get_pi_idx(struct putter_instance *);
76
77 #ifdef DEBUG
78 #ifndef PUTTERDEBUG
79 #define PUTTERDEBUG
80 #endif
81 #endif
82
83 #ifdef PUTTERDEBUG
84 static int putterdebug = 0;
85 #define DPRINTF(x) if (putterdebug > 0) printf x
86 #define DPRINTF_VERBOSE(x) if (putterdebug > 1) printf x
87 #else
88 #define DPRINTF(x)
89 #define DPRINTF_VERBOSE(x)
90 #endif
91
92 #define PUTTER_CLONER 0x7ffff
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 putter_hdr pth;
189 uint8_t *buf;
190 size_t frsize;
191 int error;
192
193 DPRINTF(("putter_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(&pth, sizeof(struct putter_hdr), uio);
202 if (error)
203 return error;
204
205 /* Sorry mate, the kernel doesn't buffer. */
206 frsize = pth.pth_framelen - sizeof(struct putter_hdr);
207 if (uio->uio_resid < frsize)
208 return EINVAL;
209
210 buf = kmem_alloc(frsize + sizeof(struct putter_hdr), KM_SLEEP);
211 memcpy(buf, &pth, sizeof(pth));
212 error = uiomove(buf+sizeof(struct putter_hdr), frsize, uio);
213 if (error == 0) {
214 pi->pi_pop->pop_dispatch(pi->pi_private,
215 (struct putter_hdr *)buf);
216 }
217 kmem_free(buf, frsize + sizeof(struct putter_hdr));
218
219 return error;
220 }
221
222 /*
223 * Poll query interface. The question is only if an event
224 * can be read from us.
225 */
226 #define PUTTERPOLL_EVSET (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)
227 static int
228 putter_fop_poll(struct file *fp, int events, struct lwp *l)
229 {
230 struct putter_instance *pi = fp->f_data;
231 int revents;
232
233 if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
234 printf("putter_fop_ioctl: putter %d not inited\n", pi->pi_idx);
235 return ENOENT;
236 }
237
238 revents = events & (POLLOUT | POLLWRNORM | POLLWRBAND);
239 if ((events & PUTTERPOLL_EVSET) == 0)
240 return revents;
241
242 /* check queue */
243 if (pi->pi_pop->pop_waitcount(pi->pi_private))
244 revents |= PUTTERPOLL_EVSET;
245 else
246 selrecord(l, &pi->pi_sel);
247
248 return revents;
249 }
250
251 /*
252 * device close = forced unmount.
253 *
254 * unmounting is a frightfully complex operation to avoid races
255 */
256 static int
257 putter_fop_close(struct file *fp, struct lwp *l)
258 {
259 struct putter_instance *pi = fp->f_data;
260 int rv;
261
262 DPRINTF(("putter_fop_close: device closed\n"));
263
264 restart:
265 mutex_enter(&pi_mtx);
266 /*
267 * First check if the fs was never mounted. In that case
268 * remove the instance from the list. If mount is attempted later,
269 * it will simply fail.
270 */
271 if (pi->pi_private == PUTTER_EMBRYO) {
272 TAILQ_REMOVE(&putter_ilist, pi, pi_entries);
273 mutex_exit(&pi_mtx);
274
275 DPRINTF(("putter_fop_close: data associated with fp %p was "
276 "embryonic\n", fp));
277
278 goto out;
279 }
280
281 /*
282 * Next, analyze if unmount was called and the instance is dead.
283 * In this case we can just free the structure and go home, it
284 * was removed from the list by putter_rmprivate().
285 */
286 if (pi->pi_private == PUTTER_DEAD) {
287 mutex_exit(&pi_mtx);
288
289 DPRINTF(("putter_fop_close: putter associated with fp %p (%d) "
290 "dead, freeing\n", fp, pi->pi_idx));
291
292 goto out;
293 }
294
295 /*
296 * So we have a reference. Proceed to unwrap the file system.
297 */
298 mutex_exit(&pi_mtx);
299
300 /* hmm? suspicious locking? */
301 while ((rv = pi->pi_pop->pop_close(pi->pi_private)) == ERESTART)
302 goto restart;
303
304 out:
305 /*
306 * Finally, release the instance information. It was already
307 * removed from the list by putter_rmprivate() and we know it's
308 * dead, so no need to lock.
309 */
310 kmem_free(pi, sizeof(struct putter_instance));
311
312 return 0;
313 }
314
315 static int
316 putter_fop_ioctl(struct file *fp, u_long cmd, void *data, struct lwp *l)
317 {
318
319 /*
320 * work already done in sys_ioctl(). skip sanity checks to enable
321 * setting non-blocking fd without yet having mounted the fs
322 */
323 if (cmd == FIONBIO)
324 return 0;
325
326 return EINVAL;
327 }
328
329 /* kqueue stuff */
330
331 static void
332 filt_putterdetach(struct knote *kn)
333 {
334 struct putter_instance *pi = kn->kn_hook;
335
336 mutex_enter(&pi_mtx);
337 SLIST_REMOVE(&pi->pi_sel.sel_klist, kn, knote, kn_selnext);
338 mutex_exit(&pi_mtx);
339 }
340
341 static int
342 filt_putterioctl(struct knote *kn, long hint)
343 {
344 struct putter_instance *pi = kn->kn_hook;
345 int error;
346
347 error = 0;
348 mutex_enter(&pi_mtx);
349 if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD)
350 error = 1;
351 mutex_exit(&pi_mtx);
352 if (error)
353 return 0;
354
355 kn->kn_data = pi->pi_pop->pop_waitcount(pi->pi_private);
356
357 return kn->kn_data != 0;
358 }
359
360 static const struct filterops putterioctl_filtops =
361 { 1, NULL, filt_putterdetach, filt_putterioctl };
362
363 static int
364 putter_fop_kqfilter(struct file *fp, struct knote *kn)
365 {
366 struct putter_instance *pi = fp->f_data;
367 struct klist *klist;
368
369 if (kn->kn_filter != EVFILT_READ)
370 return 1;
371
372 klist = &pi->pi_sel.sel_klist;
373 kn->kn_fop = &putterioctl_filtops;
374 kn->kn_hook = pi;
375
376 mutex_enter(&pi_mtx);
377 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
378 mutex_exit(&pi_mtx);
379
380 return 0;
381 }
382
383
384 /*
385 * Device routines. These are for when /dev/puffs is initially
386 * opened before it has been cloned.
387 */
388
389 dev_type_open(puttercdopen);
390 dev_type_close(puttercdclose);
391 dev_type_ioctl(puttercdioctl);
392
393 /* dev */
394 const struct cdevsw putter_cdevsw = {
395 puttercdopen, puttercdclose, noread, nowrite,
396 noioctl, nostop, notty, nopoll,
397 nommap, nokqfilter, D_OTHER
398 };
399 int
400 puttercdopen(dev_t dev, int flags, int fmt, struct lwp *l)
401 {
402 struct putter_instance *pi;
403 struct file *fp;
404 int error, fd, idx;
405
406 /*
407 * XXX: decide on some security model and check permissions
408 */
409
410 if (minor(dev) != PUTTER_CLONER)
411 return ENXIO;
412
413 if ((error = falloc(l, &fp, &fd)) != 0)
414 return error;
415
416 pi = kmem_alloc(sizeof(struct putter_instance), KM_SLEEP);
417
418 mutex_enter(&pi_mtx);
419 idx = get_pi_idx(pi);
420 if (idx == PUTTER_CLONER) {
421 mutex_exit(&pi_mtx);
422 kmem_free(pi, sizeof(struct putter_instance));
423 FILE_UNUSE(fp, l);
424 ffree(fp);
425 return EBUSY;
426 }
427
428 pi->pi_pid = l->l_proc->p_pid;
429 pi->pi_idx = idx;
430 selinit(&pi->pi_sel);
431 pi->pi_curput = NULL;
432 pi->pi_curres = 0;
433 pi->pi_curopaq = NULL;
434 mutex_exit(&pi_mtx);
435
436 DPRINTF(("puttercdopen: registered embryonic pmp for pid: %d\n",
437 pi->pi_pid));
438
439 return fdclone(l, fp, fd, FREAD|FWRITE, &putter_fileops, pi);
440 }
441
442 int
443 puttercdclose(dev_t dev, int flags, int fmt, struct lwp *l)
444 {
445
446 panic("puttercdclose impossible\n");
447
448 return 0;
449 }
450
451
452 /*
453 * Set the private structure for the file descriptor. This is
454 * typically done immediately when the counterpart has knowledge
455 * about the private structure's address and the file descriptor
456 * (e.g. vfs mount routine).
457 *
458 * We only want to make sure that the caller had the right to open the
459 * device, we don't so much care about which context it gets in case
460 * the same process opened multiple (since they are equal at this point).
461 */
462 struct putter_instance *
463 putter_attach(pid_t pid, int fd, void *ppriv, struct putter_ops *pop)
464 {
465 struct putter_instance *pi = NULL;
466
467 mutex_enter(&pi_mtx);
468 TAILQ_FOREACH(pi, &putter_ilist, pi_entries) {
469 if (pi->pi_pid == pid && pi->pi_private == PUTTER_EMBRYO) {
470 pi->pi_private = ppriv;
471 pi->pi_fd = fd;
472 pi->pi_pop = pop;
473 break;
474 }
475 }
476 mutex_exit(&pi_mtx);
477
478 DPRINTF(("putter_setprivate: pi at %p (%d/%d)\n", pi,
479 pi ? pi->pi_pid : 0, pi ? pi->pi_fd : 0));
480
481 return pi;
482 }
483
484 /*
485 * Remove fp <-> private mapping.
486 */
487 void
488 putter_detach(struct putter_instance *pi)
489 {
490
491 mutex_enter(&pi_mtx);
492 TAILQ_REMOVE(&putter_ilist, pi, pi_entries);
493 pi->pi_private = PUTTER_DEAD;
494 mutex_exit(&pi_mtx);
495
496 DPRINTF(("putter_nukebypmp: nuked %p\n", pi));
497 }
498
499 void
500 putter_notify(struct putter_instance *pi)
501 {
502
503 selnotify(&pi->pi_sel, 0);
504 }
505
506 /* search sorted list of instances for free minor, sorted insert arg */
507 static int
508 get_pi_idx(struct putter_instance *pi_i)
509 {
510 struct putter_instance *pi;
511 int i;
512
513 i = 0;
514 TAILQ_FOREACH(pi, &putter_ilist, pi_entries) {
515 if (i == PUTTER_CLONER)
516 return PUTTER_CLONER;
517 if (i != pi->pi_idx)
518 break;
519 i++;
520 }
521
522 pi_i->pi_private = PUTTER_EMBRYO;
523
524 if (pi == NULL)
525 TAILQ_INSERT_TAIL(&putter_ilist, pi_i, pi_entries);
526 else
527 TAILQ_INSERT_BEFORE(pi, pi_i, pi_entries);
528
529 return i;
530 }
531