1 /* $NetBSD: requests.c,v 1.23 2008/01/29 14:54:08 pooka Exp $ */
4 * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
6 * Development of this software was supported by the
7 * Research Foundation of Helsinki University of Technology
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/types.h>
32 #include <sys/ioctl.h>
33 #include <sys/queue.h>
34 #include <sys/socket.h>
36 #include <dev/misc/putter/putter.h>
45 #include "puffs_priv.h"
48 * Read a frame from the upstream provider. First read the frame
49 * length and after this read the actual contents. Yes, optimize
54 puffs__fsframe_read(struct puffs_usermount
*pu
, struct puffs_framebuf
*pb
,
57 struct putter_hdr phdr
;
59 size_t howmuch
, winlen
, curoff
;
63 /* How much to read? */
65 curoff
= puffs_framebuf_telloff(pb
);
66 if (curoff
< sizeof(struct putter_hdr
)) {
67 howmuch
= sizeof(struct putter_hdr
) - curoff
;
70 puffs_framebuf_getdata_atoff(pb
, 0, &phdr
, sizeof(phdr
));
72 howmuch
= phdr
.pth_framelen
- curoff
;
76 if (puffs_framebuf_reserve_space(pb
, howmuch
) == -1)
82 curoff
= puffs_framebuf_telloff(pb
);
83 if (puffs_framebuf_getwindow(pb
, curoff
, &win
, &winlen
) == -1)
85 n
= read(fd
, win
, winlen
);
95 puffs_framebuf_seekset(pb
, curoff
+ n
);
103 puffs_framebuf_seekset(pb
, 0);
109 * Write a frame upstream
113 puffs__fsframe_write(struct puffs_usermount
*pu
, struct puffs_framebuf
*pb
,
118 size_t winlen
, howmuch
, curoff
;
123 * Finalize it if we haven't written anything yet (or we're still
124 * attempting to write the first byte)
126 * XXX: this shouldn't be here
128 if (puffs_framebuf_telloff(pb
) == 0) {
129 struct puffs_req
*preq
;
131 winlen
= sizeof(struct puffs_req
);
132 rv
= puffs_framebuf_getwindow(pb
, 0, (void *)&preq
, &winlen
);
135 preq
->preq_pth
.pth_framelen
= flen
= preq
->preq_buflen
;
137 struct putter_hdr phdr
;
139 puffs_framebuf_getdata_atoff(pb
, 0, &phdr
, sizeof(phdr
));
140 flen
= phdr
.pth_framelen
;
144 * Then write it. Chances are if we are talking to the kernel it'll
145 * just shlosh in all at once, but if we're e.g. talking to the
146 * network it might take a few tries.
149 howmuch
= flen
- puffs_framebuf_telloff(pb
);
153 curoff
= puffs_framebuf_telloff(pb
);
154 if (puffs_framebuf_getwindow(pb
, curoff
, &win
, &winlen
) == -1)
158 * XXX: we know from the framebuf implementation that we
159 * will always managed to map the entire window. But if
160 * that changes, this will catch it. Then we can do stuff
163 assert(winlen
== howmuch
);
165 /* XXX: want NOSIGNAL if writing to a pipe */
167 n
= send(fd
, win
, winlen
, MSG_NOSIGNAL
);
169 n
= write(fd
, win
, winlen
);
180 puffs_framebuf_seekset(pb
, curoff
+ n
);
190 * Compare if "pb1" is a response to a previously sent frame pb2.
191 * More often than not "pb1" is not a response to anything but
192 * rather a fresh request from the kernel.
196 puffs__fsframe_cmp(struct puffs_usermount
*pu
,
197 struct puffs_framebuf
*pb1
, struct puffs_framebuf
*pb2
, int *notresp
)
199 struct puffs_req
*preq1
, *preq2
;
203 /* map incoming preq */
204 winlen
= sizeof(struct puffs_req
);
205 rv
= puffs_framebuf_getwindow(pb1
, 0, (void *)&preq1
, &winlen
);
206 assert(rv
== 0); /* frames are always at least puffs_req in size */
207 assert(winlen
= sizeof(struct puffs_req
));
210 * Check if this is not a response in this slot. That's the
213 if ((preq1
->preq_opclass
& PUFFSOPFLAG_ISRESPONSE
) == 0) {
218 /* map second preq */
219 winlen
= sizeof(struct puffs_req
);
220 rv
= puffs_framebuf_getwindow(pb2
, 0, (void *)&preq2
, &winlen
);
221 assert(rv
== 0); /* frames are always at least puffs_req in size */
222 assert(winlen
= sizeof(struct puffs_req
));
224 /* then compare: resid equal? */
225 return preq1
->preq_id
!= preq2
->preq_id
;
229 puffs__fsframe_gotframe(struct puffs_usermount
*pu
, struct puffs_framebuf
*pb
)
232 puffs_framebuf_seekset(pb
, 0);
233 puffs__ml_dispatch(pu
, pb
);