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