putter.c revision 1.22 1 /* $NetBSD: putter.c,v 1.22 2009/04/11 15:47:33 christos 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.22 2009/04/11 15:47:33 christos Exp $");
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/conf.h>
43 #include <sys/file.h>
44 #include <sys/filedesc.h>
45 #include <sys/kmem.h>
46 #include <sys/poll.h>
47 #include <sys/stat.h>
48 #include <sys/socketvar.h>
49 #include <sys/module.h>
50
51 #include <dev/putter/putter_sys.h>
52
53 /*
54 * Device routines. These are for when /dev/puffs is initially
55 * opened before it has been cloned.
56 */
57
58 dev_type_open(puttercdopen);
59 dev_type_close(puttercdclose);
60 dev_type_ioctl(puttercdioctl);
61
62 /* dev */
63 const struct cdevsw putter_cdevsw = {
64 puttercdopen, puttercdclose, noread, nowrite,
65 noioctl, nostop, notty, nopoll,
66 nommap, nokqfilter, D_OTHER
67 };
68
69 /*
70 * Configuration data.
71 *
72 * This is static-size for now. Will be redone for devfs.
73 */
74
75 #define PUTTER_CONFSIZE 16
76
77 static struct putter_config {
78 int pc_minor;
79 int (*pc_config)(int, int, int);
80 } putterconf[PUTTER_CONFSIZE];
81
82 static int
83 putter_configure(dev_t dev, int flags, int fmt, int fd)
84 {
85 struct putter_config *pc;
86
87 /* are we the catch-all node? */
88 if (minor(dev) == PUTTER_MINOR_WILDCARD
89 || minor(dev) == PUTTER_MINOR_COMPAT)
90 return 0;
91
92 /* nopes? try to configure us */
93 for (pc = putterconf; pc->pc_config; pc++)
94 if (minor(dev) == pc->pc_minor)
95 return pc->pc_config(fd, flags, fmt);
96 return ENXIO;
97 }
98
99 int
100 putter_register(putter_config_fn pcfn, int minor)
101 {
102 int i;
103
104 for (i = 0; i < PUTTER_CONFSIZE; i++)
105 if (putterconf[i].pc_config == NULL)
106 break;
107 if (i == PUTTER_CONFSIZE)
108 return EBUSY;
109
110 putterconf[i].pc_minor = minor;
111 putterconf[i].pc_config = pcfn;
112 return 0;
113 }
114
115 /*
116 * putter instance structures. these are always allocated and freed
117 * from the context of the transport user.
118 */
119 struct putter_instance {
120 pid_t pi_pid;
121 int pi_idx;
122 int pi_fd;
123 struct selinfo pi_sel;
124
125 void *pi_private;
126 struct putter_ops *pi_pop;
127
128 uint8_t *pi_curput;
129 size_t pi_curres;
130 void *pi_curopaq;
131 struct timespec pi_atime;
132 struct timespec pi_mtime;
133 struct timespec pi_btime;
134
135 TAILQ_ENTRY(putter_instance) pi_entries;
136 };
137 #define PUTTER_EMBRYO ((void *)-1) /* before attach */
138 #define PUTTER_DEAD ((void *)-2) /* after detach */
139
140 static TAILQ_HEAD(, putter_instance) putter_ilist
141 = TAILQ_HEAD_INITIALIZER(putter_ilist);
142
143 static int get_pi_idx(struct putter_instance *);
144
145 #ifdef DEBUG
146 #ifndef PUTTERDEBUG
147 #define PUTTERDEBUG
148 #endif
149 #endif
150
151 #ifdef PUTTERDEBUG
152 int putterdebug = 0;
153 #define DPRINTF(x) if (putterdebug > 0) printf x
154 #define DPRINTF_VERBOSE(x) if (putterdebug > 1) printf x
155 #else
156 #define DPRINTF(x)
157 #define DPRINTF_VERBOSE(x)
158 #endif
159
160 /*
161 * public init / deinit
162 */
163
164 /* protects both the list and the contents of the list elements */
165 static kmutex_t pi_mtx;
166
167 void putterattach(void);
168
169 void
170 putterattach(void)
171 {
172
173 mutex_init(&pi_mtx, MUTEX_DEFAULT, IPL_NONE);
174 }
175
176 #if 0
177 void
178 putter_destroy(void)
179 {
180
181 mutex_destroy(&pi_mtx);
182 }
183 #endif
184
185 /*
186 * fd routines, for cloner
187 */
188 static int putter_fop_read(file_t *, off_t *, struct uio *,
189 kauth_cred_t, int);
190 static int putter_fop_write(file_t *, off_t *, struct uio *,
191 kauth_cred_t, int);
192 static int putter_fop_ioctl(file_t*, u_long, void *);
193 static int putter_fop_poll(file_t *, int);
194 static int putter_fop_stat(file_t *, struct stat *);
195 static int putter_fop_close(file_t *);
196 static int putter_fop_kqfilter(file_t *, struct knote *);
197
198
199 static const struct fileops putter_fileops = {
200 .fo_read = putter_fop_read,
201 .fo_write = putter_fop_write,
202 .fo_ioctl = putter_fop_ioctl,
203 .fo_fcntl = fnullop_fcntl,
204 .fo_poll = putter_fop_poll,
205 .fo_stat = putter_fop_stat,
206 .fo_close = putter_fop_close,
207 .fo_kqfilter = putter_fop_kqfilter,
208 .fo_drain = fnullop_drain,
209 };
210
211 static int
212 putter_fop_read(file_t *fp, off_t *off, struct uio *uio,
213 kauth_cred_t cred, int flags)
214 {
215 struct putter_instance *pi = fp->f_data;
216 size_t origres, moved;
217 int error;
218
219 getnanotime(&pi->pi_atime);
220 KERNEL_LOCK(1, NULL);
221
222 if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
223 printf("putter_fop_read: private %d not inited\n", pi->pi_idx);
224 KERNEL_UNLOCK_ONE(NULL);
225 return ENOENT;
226 }
227
228 if (pi->pi_curput == NULL) {
229 error = pi->pi_pop->pop_getout(pi->pi_private, uio->uio_resid,
230 fp->f_flag & O_NONBLOCK, &pi->pi_curput,
231 &pi->pi_curres, &pi->pi_curopaq);
232 if (error) {
233 KERNEL_UNLOCK_ONE(NULL);
234 return error;
235 }
236 }
237
238 origres = uio->uio_resid;
239 error = uiomove(pi->pi_curput, pi->pi_curres, uio);
240 moved = origres - uio->uio_resid;
241 DPRINTF(("putter_fop_read (%p): moved %zu bytes from %p, error %d\n",
242 pi, moved, pi->pi_curput, error));
243
244 KASSERT(pi->pi_curres >= moved);
245 pi->pi_curres -= moved;
246 pi->pi_curput += moved;
247
248 if (pi->pi_curres == 0) {
249 pi->pi_pop->pop_releaseout(pi->pi_private,
250 pi->pi_curopaq, error);
251 pi->pi_curput = NULL;
252 }
253
254 KERNEL_UNLOCK_ONE(NULL);
255 return error;
256 }
257
258 static int
259 putter_fop_write(file_t *fp, off_t *off, struct uio *uio,
260 kauth_cred_t cred, int flags)
261 {
262 struct putter_instance *pi = fp->f_data;
263 struct putter_hdr pth;
264 uint8_t *buf;
265 size_t frsize;
266 int error;
267
268 getnanotime(&pi->pi_mtime);
269 KERNEL_LOCK(1, NULL);
270
271 DPRINTF(("putter_fop_write (%p): writing response, resid %zu\n",
272 pi->pi_private, uio->uio_resid));
273
274 if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
275 printf("putter_fop_write: putter %d not inited\n", pi->pi_idx);
276 KERNEL_UNLOCK_ONE(NULL);
277 return ENOENT;
278 }
279
280 error = uiomove(&pth, sizeof(struct putter_hdr), uio);
281 if (error) {
282 KERNEL_UNLOCK_ONE(NULL);
283 return error;
284 }
285
286 /* Sorry mate, the kernel doesn't buffer. */
287 frsize = pth.pth_framelen - sizeof(struct putter_hdr);
288 if (uio->uio_resid < frsize) {
289 KERNEL_UNLOCK_ONE(NULL);
290 return EINVAL;
291 }
292
293 buf = kmem_alloc(frsize + sizeof(struct putter_hdr), KM_SLEEP);
294 memcpy(buf, &pth, sizeof(pth));
295 error = uiomove(buf+sizeof(struct putter_hdr), frsize, uio);
296 if (error == 0) {
297 pi->pi_pop->pop_dispatch(pi->pi_private,
298 (struct putter_hdr *)buf);
299 }
300 kmem_free(buf, frsize + sizeof(struct putter_hdr));
301
302 KERNEL_UNLOCK_ONE(NULL);
303 return error;
304 }
305
306 /*
307 * Poll query interface. The question is only if an event
308 * can be read from us.
309 */
310 #define PUTTERPOLL_EVSET (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)
311 static int
312 putter_fop_poll(file_t *fp, int events)
313 {
314 struct putter_instance *pi = fp->f_data;
315 int revents;
316
317 KERNEL_LOCK(1, NULL);
318
319 if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
320 printf("putter_fop_ioctl: putter %d not inited\n", pi->pi_idx);
321 KERNEL_UNLOCK_ONE(NULL);
322 return ENOENT;
323 }
324
325 revents = events & (POLLOUT | POLLWRNORM | POLLWRBAND);
326 if ((events & PUTTERPOLL_EVSET) == 0) {
327 KERNEL_UNLOCK_ONE(NULL);
328 return revents;
329 }
330
331 /* check queue */
332 if (pi->pi_pop->pop_waitcount(pi->pi_private))
333 revents |= PUTTERPOLL_EVSET;
334 else
335 selrecord(curlwp, &pi->pi_sel);
336
337 KERNEL_UNLOCK_ONE(NULL);
338 return revents;
339 }
340
341 /*
342 * device close = forced unmount.
343 *
344 * unmounting is a frightfully complex operation to avoid races
345 */
346 static int
347 putter_fop_close(file_t *fp)
348 {
349 struct putter_instance *pi = fp->f_data;
350 int rv;
351
352 DPRINTF(("putter_fop_close: device closed\n"));
353
354 KERNEL_LOCK(1, NULL);
355
356 restart:
357 mutex_enter(&pi_mtx);
358 /*
359 * First check if the fs was never mounted. In that case
360 * remove the instance from the list. If mount is attempted later,
361 * it will simply fail.
362 */
363 if (pi->pi_private == PUTTER_EMBRYO) {
364 TAILQ_REMOVE(&putter_ilist, pi, pi_entries);
365 mutex_exit(&pi_mtx);
366
367 DPRINTF(("putter_fop_close: data associated with fp %p was "
368 "embryonic\n", fp));
369
370 goto out;
371 }
372
373 /*
374 * Next, analyze if unmount was called and the instance is dead.
375 * In this case we can just free the structure and go home, it
376 * was removed from the list by putter_rmprivate().
377 */
378 if (pi->pi_private == PUTTER_DEAD) {
379 mutex_exit(&pi_mtx);
380
381 DPRINTF(("putter_fop_close: putter associated with fp %p (%d) "
382 "dead, freeing\n", fp, pi->pi_idx));
383
384 goto out;
385 }
386
387 /*
388 * So we have a reference. Proceed to unwrap the file system.
389 */
390 mutex_exit(&pi_mtx);
391
392 /* hmm? suspicious locking? */
393 while ((rv = pi->pi_pop->pop_close(pi->pi_private)) == ERESTART)
394 goto restart;
395
396 out:
397 KERNEL_UNLOCK_ONE(NULL);
398 /*
399 * Finally, release the instance information. It was already
400 * removed from the list by putter_rmprivate() and we know it's
401 * dead, so no need to lock.
402 */
403 kmem_free(pi, sizeof(struct putter_instance));
404
405 return 0;
406 }
407
408 static int
409 putter_fop_stat(file_t *fp, struct stat *st)
410 {
411 struct putter_instance *pi = fp->f_data;
412
413 (void)memset(st, 0, sizeof(*st));
414 KERNEL_LOCK(1, NULL);
415 st->st_dev = makedev(cdevsw_lookup_major(&putter_cdevsw), pi->pi_idx);
416 st->st_atimespec = pi->pi_atime;
417 st->st_mtimespec = pi->pi_mtime;
418 st->st_ctimespec = st->st_birthtimespec = pi->pi_btime;
419 KERNEL_UNLOCK_ONE(NULL);
420 return 0;
421 }
422
423 static int
424 putter_fop_ioctl(file_t *fp, u_long cmd, void *data)
425 {
426
427 /*
428 * work already done in sys_ioctl(). skip sanity checks to enable
429 * setting non-blocking fd without yet having mounted the fs
430 */
431 if (cmd == FIONBIO)
432 return 0;
433
434 return EINVAL;
435 }
436
437 /* kqueue stuff */
438
439 static void
440 filt_putterdetach(struct knote *kn)
441 {
442 struct putter_instance *pi = kn->kn_hook;
443
444 KERNEL_LOCK(1, NULL);
445 mutex_enter(&pi_mtx);
446 SLIST_REMOVE(&pi->pi_sel.sel_klist, kn, knote, kn_selnext);
447 mutex_exit(&pi_mtx);
448 KERNEL_UNLOCK_ONE(NULL);
449 }
450
451 static int
452 filt_putter(struct knote *kn, long hint)
453 {
454 struct putter_instance *pi = kn->kn_hook;
455 int error, rv;
456
457 KERNEL_LOCK(1, NULL);
458 error = 0;
459 mutex_enter(&pi_mtx);
460 if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD)
461 error = 1;
462 mutex_exit(&pi_mtx);
463 if (error) {
464 KERNEL_UNLOCK_ONE(NULL);
465 return 0;
466 }
467
468 kn->kn_data = pi->pi_pop->pop_waitcount(pi->pi_private);
469 rv = kn->kn_data != 0;
470 KERNEL_UNLOCK_ONE(NULL);
471 return rv;
472 }
473
474 static const struct filterops putter_filtops =
475 { 1, NULL, filt_putterdetach, filt_putter };
476
477 static int
478 putter_fop_kqfilter(file_t *fp, struct knote *kn)
479 {
480 struct putter_instance *pi = fp->f_data;
481 struct klist *klist;
482
483 KERNEL_LOCK(1, NULL);
484
485 switch (kn->kn_filter) {
486 case EVFILT_READ:
487 klist = &pi->pi_sel.sel_klist;
488 kn->kn_fop = &putter_filtops;
489 kn->kn_hook = pi;
490
491 mutex_enter(&pi_mtx);
492 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
493 mutex_exit(&pi_mtx);
494
495 break;
496 case EVFILT_WRITE:
497 kn->kn_fop = &seltrue_filtops;
498 break;
499 default:
500 KERNEL_UNLOCK_ONE(NULL);
501 return EINVAL;
502 }
503
504 KERNEL_UNLOCK_ONE(NULL);
505 return 0;
506 }
507
508 int
509 puttercdopen(dev_t dev, int flags, int fmt, struct lwp *l)
510 {
511 struct putter_instance *pi;
512 file_t *fp;
513 int error, fd, idx;
514 proc_t *p;
515
516 p = curproc;
517 pi = kmem_alloc(sizeof(struct putter_instance), KM_SLEEP);
518 mutex_enter(&pi_mtx);
519 idx = get_pi_idx(pi);
520
521 pi->pi_pid = p->p_pid;
522 pi->pi_idx = idx;
523 pi->pi_curput = NULL;
524 pi->pi_curres = 0;
525 pi->pi_curopaq = NULL;
526 getnanotime(&pi->pi_btime);
527 pi->pi_atime = pi->pi_mtime = pi->pi_btime;
528 selinit(&pi->pi_sel);
529 mutex_exit(&pi_mtx);
530
531 if ((error = fd_allocfile(&fp, &fd)) != 0)
532 goto bad1;
533
534 if ((error = putter_configure(dev, flags, fmt, fd)) != 0)
535 goto bad2;
536
537 DPRINTF(("puttercdopen: registered embryonic pmp for pid: %d\n",
538 pi->pi_pid));
539
540 error = fd_clone(fp, fd, FREAD|FWRITE, &putter_fileops, pi);
541 KASSERT(error == EMOVEFD);
542 return error;
543
544 bad2:
545 fd_abort(p, fp, fd);
546 bad1:
547 putter_detach(pi);
548 kmem_free(pi, sizeof(struct putter_instance));
549 return error;
550 }
551
552 int
553 puttercdclose(dev_t dev, int flags, int fmt, struct lwp *l)
554 {
555
556 panic("puttercdclose impossible\n");
557
558 return 0;
559 }
560
561
562 /*
563 * Set the private structure for the file descriptor. This is
564 * typically done immediately when the counterpart has knowledge
565 * about the private structure's address and the file descriptor
566 * (e.g. vfs mount routine).
567 *
568 * We only want to make sure that the caller had the right to open the
569 * device, we don't so much care about which context it gets in case
570 * the same process opened multiple (since they are equal at this point).
571 */
572 struct putter_instance *
573 putter_attach(pid_t pid, int fd, void *ppriv, struct putter_ops *pop)
574 {
575 struct putter_instance *pi = NULL;
576
577 mutex_enter(&pi_mtx);
578 TAILQ_FOREACH(pi, &putter_ilist, pi_entries) {
579 if (pi->pi_pid == pid && pi->pi_private == PUTTER_EMBRYO) {
580 pi->pi_private = ppriv;
581 pi->pi_fd = fd;
582 pi->pi_pop = pop;
583 break;
584 }
585 }
586 mutex_exit(&pi_mtx);
587
588 DPRINTF(("putter_setprivate: pi at %p (%d/%d)\n", pi,
589 pi ? pi->pi_pid : 0, pi ? pi->pi_fd : 0));
590
591 return pi;
592 }
593
594 /*
595 * Remove fp <-> private mapping.
596 */
597 void
598 putter_detach(struct putter_instance *pi)
599 {
600
601 mutex_enter(&pi_mtx);
602 TAILQ_REMOVE(&putter_ilist, pi, pi_entries);
603 pi->pi_private = PUTTER_DEAD;
604 mutex_exit(&pi_mtx);
605
606 DPRINTF(("putter_nukebypmp: nuked %p\n", pi));
607 }
608
609 void
610 putter_notify(struct putter_instance *pi)
611 {
612
613 selnotify(&pi->pi_sel, 0, 0);
614 }
615
616 /* search sorted list of instances for free minor, sorted insert arg */
617 static int
618 get_pi_idx(struct putter_instance *pi_i)
619 {
620 struct putter_instance *pi;
621 int i;
622
623 KASSERT(mutex_owned(&pi_mtx));
624
625 i = 0;
626 TAILQ_FOREACH(pi, &putter_ilist, pi_entries) {
627 if (i != pi->pi_idx)
628 break;
629 i++;
630 }
631
632 pi_i->pi_private = PUTTER_EMBRYO;
633
634 if (pi == NULL)
635 TAILQ_INSERT_TAIL(&putter_ilist, pi_i, pi_entries);
636 else
637 TAILQ_INSERT_BEFORE(pi, pi_i, pi_entries);
638
639 return i;
640 }
641
642 MODULE(MODULE_CLASS_MISC, putter, NULL);
643
644 static int
645 putter_modcmd(modcmd_t cmd, void *arg)
646 {
647 #ifdef _MODULE
648 devmajor_t bmajor = NODEVMAJOR, cmajor = NODEVMAJOR;
649
650 switch (cmd) {
651 case MODULE_CMD_INIT:
652 return devsw_attach("putter", NULL, &bmajor,
653 &putter_cdevsw, &cmajor);
654 case MODULE_CMD_FINI:
655 return devsw_detach(NULL, &putter_cdevsw);
656 default:
657 return ENOTTY;
658 }
659 #else
660 if (cmd == MODULE_CMD_INIT)
661 return 0;
662 return ENOTTY;
663 #endif
664 }
665