requests.c revision 1.19 1 1.18 dogcow /* $NetBSD: requests.c,v 1.19 2007/12/05 10:13:37 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.17 pooka * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
5 1.17 pooka *
6 1.17 pooka * Development of this software was supported by the
7 1.17 pooka * Research Foundation of Helsinki University of Technology
8 1.1 pooka *
9 1.1 pooka * Redistribution and use in source and binary forms, with or without
10 1.1 pooka * modification, are permitted provided that the following conditions
11 1.1 pooka * are met:
12 1.1 pooka * 1. Redistributions of source code must retain the above copyright
13 1.1 pooka * notice, this list of conditions and the following disclaimer.
14 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 pooka * notice, this list of conditions and the following disclaimer in the
16 1.1 pooka * documentation and/or other materials provided with the distribution.
17 1.1 pooka *
18 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 pooka * SUCH DAMAGE.
29 1.1 pooka */
30 1.1 pooka
31 1.1 pooka #include <sys/cdefs.h>
32 1.1 pooka #if !defined(lint)
33 1.18 dogcow __RCSID("$NetBSD: requests.c,v 1.19 2007/12/05 10:13:37 pooka Exp $");
34 1.1 pooka #endif /* !lint */
35 1.1 pooka
36 1.1 pooka #include <sys/types.h>
37 1.1 pooka #include <sys/ioctl.h>
38 1.2 pooka #include <sys/queue.h>
39 1.17 pooka #include <sys/socket.h>
40 1.17 pooka
41 1.17 pooka #include <dev/putter/putter.h>
42 1.1 pooka
43 1.2 pooka #include <assert.h>
44 1.10 pooka #include <errno.h>
45 1.1 pooka #include <puffs.h>
46 1.1 pooka #include <stdio.h>
47 1.1 pooka #include <stdlib.h>
48 1.10 pooka #include <unistd.h>
49 1.1 pooka
50 1.1 pooka #include "puffs_priv.h"
51 1.1 pooka
52 1.10 pooka /*
53 1.17 pooka * Read a frame from the upstream provider. First read the frame
54 1.17 pooka * length and after this read the actual contents. Yes, optimize
55 1.17 pooka * me some day.
56 1.10 pooka */
57 1.10 pooka /*ARGSUSED*/
58 1.17 pooka int
59 1.17 pooka puffs_fsframe_read(struct puffs_usermount *pu, struct puffs_framebuf *pb,
60 1.17 pooka int fd, int *done)
61 1.1 pooka {
62 1.17 pooka struct putter_hdr phdr;
63 1.17 pooka void *win;
64 1.17 pooka size_t howmuch, winlen, curoff;
65 1.17 pooka ssize_t n;
66 1.17 pooka int lenstate;
67 1.1 pooka
68 1.17 pooka puffs_framebuf_reserve_space(pb, PUFFS_MSG_MAXSIZE);
69 1.1 pooka
70 1.17 pooka /* How much to read? */
71 1.17 pooka the_next_level:
72 1.17 pooka curoff = puffs_framebuf_telloff(pb);
73 1.17 pooka if (curoff < sizeof(struct putter_hdr)) {
74 1.17 pooka howmuch = sizeof(struct putter_hdr) - curoff;
75 1.17 pooka lenstate = 1;
76 1.17 pooka } else {
77 1.17 pooka puffs_framebuf_getdata_atoff(pb, 0, &phdr, sizeof(phdr));
78 1.17 pooka /*LINTED*/
79 1.17 pooka howmuch = phdr.pth_framelen - curoff;
80 1.17 pooka lenstate = 0;
81 1.17 pooka }
82 1.2 pooka
83 1.17 pooka if (puffs_framebuf_reserve_space(pb, howmuch) == -1)
84 1.17 pooka return errno;
85 1.2 pooka
86 1.17 pooka /* Read contents */
87 1.17 pooka while (howmuch) {
88 1.17 pooka winlen = howmuch;
89 1.17 pooka curoff = puffs_framebuf_telloff(pb);
90 1.17 pooka if (puffs_framebuf_getwindow(pb, curoff, &win, &winlen) == -1)
91 1.17 pooka return errno;
92 1.17 pooka n = read(fd, win, winlen);
93 1.17 pooka switch (n) {
94 1.17 pooka case 0:
95 1.17 pooka return ECONNRESET;
96 1.17 pooka case -1:
97 1.17 pooka if (errno == EAGAIN)
98 1.17 pooka return 0;
99 1.17 pooka return errno;
100 1.17 pooka default:
101 1.17 pooka howmuch -= n;
102 1.17 pooka puffs_framebuf_seekset(pb, curoff + n);
103 1.17 pooka break;
104 1.17 pooka }
105 1.10 pooka }
106 1.1 pooka
107 1.17 pooka if (lenstate)
108 1.17 pooka goto the_next_level;
109 1.17 pooka
110 1.17 pooka puffs_framebuf_seekset(pb, 0);
111 1.17 pooka *done = 1;
112 1.2 pooka return 0;
113 1.1 pooka }
114 1.1 pooka
115 1.17 pooka /*
116 1.17 pooka * Write a frame upstream
117 1.17 pooka */
118 1.17 pooka /*ARGSUSED*/
119 1.1 pooka int
120 1.17 pooka puffs_fsframe_write(struct puffs_usermount *pu, struct puffs_framebuf *pb,
121 1.17 pooka int fd, int *done)
122 1.1 pooka {
123 1.17 pooka void *win;
124 1.17 pooka uint64_t flen;
125 1.17 pooka size_t winlen, howmuch, curoff;
126 1.17 pooka ssize_t n;
127 1.17 pooka int rv;
128 1.1 pooka
129 1.17 pooka /*
130 1.17 pooka * Finalize it if we haven't written anything yet (or we're still
131 1.17 pooka * attempting to write the first byte)
132 1.17 pooka *
133 1.17 pooka * XXX: this shouldn't be here
134 1.17 pooka */
135 1.17 pooka if (puffs_framebuf_telloff(pb) == 0) {
136 1.19 pooka struct puffs_req *preq;
137 1.17 pooka
138 1.17 pooka winlen = sizeof(struct puffs_req);
139 1.19 pooka rv = puffs_framebuf_getwindow(pb, 0, (void *)&preq, &winlen);
140 1.17 pooka if (rv == -1)
141 1.17 pooka return errno;
142 1.19 pooka preq->preq_pth.pth_framelen = flen = preq->preq_buflen;
143 1.17 pooka } else {
144 1.17 pooka struct putter_hdr phdr;
145 1.1 pooka
146 1.17 pooka puffs_framebuf_getdata_atoff(pb, 0, &phdr, sizeof(phdr));
147 1.17 pooka flen = phdr.pth_framelen;
148 1.17 pooka }
149 1.2 pooka
150 1.17 pooka /*
151 1.17 pooka * Then write it. Chances are if we are talking to the kernel it'll
152 1.17 pooka * just shlosh in all at once, but if we're e.g. talking to the
153 1.17 pooka * network it might take a few tries.
154 1.17 pooka */
155 1.17 pooka /*LINTED*/
156 1.17 pooka howmuch = flen - puffs_framebuf_telloff(pb);
157 1.17 pooka
158 1.17 pooka while (howmuch) {
159 1.17 pooka winlen = howmuch;
160 1.17 pooka curoff = puffs_framebuf_telloff(pb);
161 1.17 pooka if (puffs_framebuf_getwindow(pb, curoff, &win, &winlen) == -1)
162 1.17 pooka return errno;
163 1.17 pooka
164 1.17 pooka /*
165 1.17 pooka * XXX: we know from the framebuf implementation that we
166 1.17 pooka * will always managed to map the entire window. But if
167 1.17 pooka * that changes, this will catch it. Then we can do stuff
168 1.17 pooka * iov stuff instead.
169 1.17 pooka */
170 1.17 pooka assert(winlen == howmuch);
171 1.17 pooka
172 1.17 pooka /* XXX: want NOSIGNAL if writing to a pipe */
173 1.17 pooka #if 0
174 1.17 pooka n = send(fd, win, winlen, MSG_NOSIGNAL);
175 1.17 pooka #else
176 1.17 pooka n = write(fd, win, winlen);
177 1.17 pooka #endif
178 1.17 pooka switch (n) {
179 1.17 pooka case 0:
180 1.17 pooka return ECONNRESET;
181 1.17 pooka case -1:
182 1.17 pooka if (errno == EAGAIN)
183 1.17 pooka return 0;
184 1.17 pooka return errno;
185 1.17 pooka default:
186 1.17 pooka howmuch -= n;
187 1.17 pooka puffs_framebuf_seekset(pb, curoff + n);
188 1.17 pooka break;
189 1.17 pooka }
190 1.17 pooka }
191 1.2 pooka
192 1.17 pooka *done = 1;
193 1.17 pooka return 0;
194 1.1 pooka }
195 1.1 pooka
196 1.2 pooka /*
197 1.17 pooka * Compare if "pb1" is a response to a previously sent frame pb2.
198 1.17 pooka * More often than not "pb1" is not a response to anything but
199 1.17 pooka * rather a fresh request from the kernel.
200 1.2 pooka */
201 1.10 pooka /*ARGSUSED*/
202 1.1 pooka int
203 1.17 pooka puffs_fsframe_cmp(struct puffs_usermount *pu,
204 1.17 pooka struct puffs_framebuf *pb1, struct puffs_framebuf *pb2, int *notresp)
205 1.1 pooka {
206 1.19 pooka struct puffs_req *preq1, *preq2;
207 1.17 pooka size_t winlen;
208 1.17 pooka int rv;
209 1.17 pooka
210 1.17 pooka /* map incoming preq */
211 1.17 pooka winlen = sizeof(struct puffs_req);
212 1.19 pooka rv = puffs_framebuf_getwindow(pb1, 0, (void *)&preq1, &winlen);
213 1.17 pooka assert(rv == 0); /* frames are always at least puffs_req in size */
214 1.17 pooka assert(winlen = sizeof(struct puffs_req));
215 1.17 pooka
216 1.17 pooka /*
217 1.17 pooka * Check if this is not a response in this slot. That's the
218 1.17 pooka * likely case.
219 1.17 pooka */
220 1.19 pooka if ((preq1->preq_opclass & PUFFSOPFLAG_ISRESPONSE) == 0) {
221 1.17 pooka *notresp = 1;
222 1.17 pooka return 0;
223 1.17 pooka }
224 1.1 pooka
225 1.17 pooka /* map second preq */
226 1.17 pooka winlen = sizeof(struct puffs_req);
227 1.19 pooka rv = puffs_framebuf_getwindow(pb2, 0, (void *)&preq2, &winlen);
228 1.17 pooka assert(rv == 0); /* frames are always at least puffs_req in size */
229 1.17 pooka assert(winlen = sizeof(struct puffs_req));
230 1.1 pooka
231 1.17 pooka /* then compare: resid equal? */
232 1.19 pooka return preq1->preq_id == preq2->preq_id;
233 1.2 pooka }
234 1.2 pooka
235 1.2 pooka void
236 1.17 pooka puffs_fsframe_gotframe(struct puffs_usermount *pu, struct puffs_framebuf *pb)
237 1.1 pooka {
238 1.17 pooka struct puffs_framebuf *newpb;
239 1.1 pooka
240 1.17 pooka if ((newpb = puffs_framebuf_make()) == NULL)
241 1.17 pooka abort();
242 1.17 pooka /* XXX: optimize */
243 1.17 pooka puffs__framebuf_moveinfo(pb, newpb);
244 1.17 pooka puffs_framebuf_seekset(newpb, 0);
245 1.17 pooka if (puffs_framebuf_reserve_space(newpb, PUFFS_MSG_MAXSIZE) == -1)
246 1.17 pooka abort();
247 1.3 pooka
248 1.17 pooka puffs_dopufbuf(pu, newpb);
249 1.3 pooka }
250