requests.c revision 1.9.4.2 1 1.9.4.2 matt /* $NetBSD: requests.c,v 1.9.4.2 2008/01/09 01:36:52 matt Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.9.4.2 matt * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
5 1.9.4.2 matt *
6 1.9.4.2 matt * Development of this software was supported by the
7 1.9.4.2 matt * 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.9.4.2 matt __RCSID("$NetBSD: requests.c,v 1.9.4.2 2008/01/09 01:36:52 matt 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.9.4.2 matt #include <sys/socket.h>
40 1.9.4.2 matt
41 1.9.4.2 matt #include <dev/putter/putter.h>
42 1.1 pooka
43 1.2 pooka #include <assert.h>
44 1.9.4.1 matt #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.9.4.1 matt #include <unistd.h>
49 1.1 pooka
50 1.1 pooka #include "puffs_priv.h"
51 1.1 pooka
52 1.9.4.1 matt /*
53 1.9.4.2 matt * Read a frame from the upstream provider. First read the frame
54 1.9.4.2 matt * length and after this read the actual contents. Yes, optimize
55 1.9.4.2 matt * me some day.
56 1.9.4.1 matt */
57 1.9.4.1 matt /*ARGSUSED*/
58 1.9.4.2 matt int
59 1.9.4.2 matt puffs_fsframe_read(struct puffs_usermount *pu, struct puffs_framebuf *pb,
60 1.9.4.2 matt int fd, int *done)
61 1.1 pooka {
62 1.9.4.2 matt struct putter_hdr phdr;
63 1.9.4.2 matt void *win;
64 1.9.4.2 matt size_t howmuch, winlen, curoff;
65 1.9.4.2 matt ssize_t n;
66 1.9.4.2 matt int lenstate;
67 1.2 pooka
68 1.9.4.2 matt puffs_framebuf_reserve_space(pb, PUFFS_MSG_MAXSIZE);
69 1.2 pooka
70 1.9.4.2 matt /* How much to read? */
71 1.9.4.2 matt the_next_level:
72 1.9.4.2 matt curoff = puffs_framebuf_telloff(pb);
73 1.9.4.2 matt if (curoff < sizeof(struct putter_hdr)) {
74 1.9.4.2 matt howmuch = sizeof(struct putter_hdr) - curoff;
75 1.9.4.2 matt lenstate = 1;
76 1.9.4.2 matt } else {
77 1.9.4.2 matt puffs_framebuf_getdata_atoff(pb, 0, &phdr, sizeof(phdr));
78 1.9.4.2 matt /*LINTED*/
79 1.9.4.2 matt howmuch = phdr.pth_framelen - curoff;
80 1.9.4.2 matt lenstate = 0;
81 1.9.4.1 matt }
82 1.1 pooka
83 1.9.4.2 matt if (puffs_framebuf_reserve_space(pb, howmuch) == -1)
84 1.9.4.2 matt return errno;
85 1.1 pooka
86 1.9.4.2 matt /* Read contents */
87 1.9.4.2 matt while (howmuch) {
88 1.9.4.2 matt winlen = howmuch;
89 1.9.4.2 matt curoff = puffs_framebuf_telloff(pb);
90 1.9.4.2 matt if (puffs_framebuf_getwindow(pb, curoff, &win, &winlen) == -1)
91 1.9.4.2 matt return errno;
92 1.9.4.2 matt n = read(fd, win, winlen);
93 1.9.4.2 matt switch (n) {
94 1.9.4.2 matt case 0:
95 1.9.4.2 matt return ECONNRESET;
96 1.9.4.2 matt case -1:
97 1.9.4.2 matt if (errno == EAGAIN)
98 1.9.4.2 matt return 0;
99 1.9.4.2 matt return errno;
100 1.9.4.2 matt default:
101 1.9.4.2 matt howmuch -= n;
102 1.9.4.2 matt puffs_framebuf_seekset(pb, curoff + n);
103 1.9.4.2 matt break;
104 1.9.4.2 matt }
105 1.9.4.2 matt }
106 1.1 pooka
107 1.9.4.2 matt if (lenstate)
108 1.9.4.2 matt goto the_next_level;
109 1.1 pooka
110 1.9.4.2 matt puffs_framebuf_seekset(pb, 0);
111 1.9.4.2 matt *done = 1;
112 1.9.4.2 matt return 0;
113 1.1 pooka }
114 1.1 pooka
115 1.9.4.2 matt /*
116 1.9.4.2 matt * Write a frame upstream
117 1.9.4.2 matt */
118 1.9.4.1 matt /*ARGSUSED*/
119 1.9.4.2 matt int
120 1.9.4.2 matt puffs_fsframe_write(struct puffs_usermount *pu, struct puffs_framebuf *pb,
121 1.9.4.2 matt int fd, int *done)
122 1.1 pooka {
123 1.9.4.2 matt void *win;
124 1.9.4.2 matt uint64_t flen;
125 1.9.4.2 matt size_t winlen, howmuch, curoff;
126 1.9.4.2 matt ssize_t n;
127 1.9.4.2 matt int rv;
128 1.1 pooka
129 1.9.4.2 matt /*
130 1.9.4.2 matt * Finalize it if we haven't written anything yet (or we're still
131 1.9.4.2 matt * attempting to write the first byte)
132 1.9.4.2 matt *
133 1.9.4.2 matt * XXX: this shouldn't be here
134 1.9.4.2 matt */
135 1.9.4.2 matt if (puffs_framebuf_telloff(pb) == 0) {
136 1.9.4.2 matt struct puffs_req *preq;
137 1.9.4.2 matt
138 1.9.4.2 matt winlen = sizeof(struct puffs_req);
139 1.9.4.2 matt rv = puffs_framebuf_getwindow(pb, 0, (void *)&preq, &winlen);
140 1.9.4.2 matt if (rv == -1)
141 1.9.4.2 matt return errno;
142 1.9.4.2 matt preq->preq_pth.pth_framelen = flen = preq->preq_buflen;
143 1.9.4.2 matt } else {
144 1.9.4.2 matt struct putter_hdr phdr;
145 1.1 pooka
146 1.9.4.2 matt puffs_framebuf_getdata_atoff(pb, 0, &phdr, sizeof(phdr));
147 1.9.4.2 matt flen = phdr.pth_framelen;
148 1.9.4.2 matt }
149 1.1 pooka
150 1.9.4.2 matt /*
151 1.9.4.2 matt * Then write it. Chances are if we are talking to the kernel it'll
152 1.9.4.2 matt * just shlosh in all at once, but if we're e.g. talking to the
153 1.9.4.2 matt * network it might take a few tries.
154 1.9.4.2 matt */
155 1.9.4.2 matt /*LINTED*/
156 1.9.4.2 matt howmuch = flen - puffs_framebuf_telloff(pb);
157 1.9.4.2 matt
158 1.9.4.2 matt while (howmuch) {
159 1.9.4.2 matt winlen = howmuch;
160 1.9.4.2 matt curoff = puffs_framebuf_telloff(pb);
161 1.9.4.2 matt if (puffs_framebuf_getwindow(pb, curoff, &win, &winlen) == -1)
162 1.9.4.2 matt return errno;
163 1.9.4.2 matt
164 1.9.4.2 matt /*
165 1.9.4.2 matt * XXX: we know from the framebuf implementation that we
166 1.9.4.2 matt * will always managed to map the entire window. But if
167 1.9.4.2 matt * that changes, this will catch it. Then we can do stuff
168 1.9.4.2 matt * iov stuff instead.
169 1.9.4.2 matt */
170 1.9.4.2 matt assert(winlen == howmuch);
171 1.9.4.2 matt
172 1.9.4.2 matt /* XXX: want NOSIGNAL if writing to a pipe */
173 1.9.4.2 matt #if 0
174 1.9.4.2 matt n = send(fd, win, winlen, MSG_NOSIGNAL);
175 1.9.4.2 matt #else
176 1.9.4.2 matt n = write(fd, win, winlen);
177 1.9.4.2 matt #endif
178 1.9.4.2 matt switch (n) {
179 1.9.4.2 matt case 0:
180 1.9.4.2 matt return ECONNRESET;
181 1.9.4.2 matt case -1:
182 1.9.4.2 matt if (errno == EAGAIN)
183 1.9.4.2 matt return 0;
184 1.9.4.2 matt return errno;
185 1.9.4.2 matt default:
186 1.9.4.2 matt howmuch -= n;
187 1.9.4.2 matt puffs_framebuf_seekset(pb, curoff + n);
188 1.9.4.2 matt break;
189 1.9.4.2 matt }
190 1.9.4.2 matt }
191 1.1 pooka
192 1.9.4.2 matt *done = 1;
193 1.9.4.2 matt return 0;
194 1.1 pooka }
195 1.1 pooka
196 1.2 pooka /*
197 1.9.4.2 matt * Compare if "pb1" is a response to a previously sent frame pb2.
198 1.9.4.2 matt * More often than not "pb1" is not a response to anything but
199 1.9.4.2 matt * rather a fresh request from the kernel.
200 1.2 pooka */
201 1.9.4.1 matt /*ARGSUSED*/
202 1.1 pooka int
203 1.9.4.2 matt puffs_fsframe_cmp(struct puffs_usermount *pu,
204 1.9.4.2 matt struct puffs_framebuf *pb1, struct puffs_framebuf *pb2, int *notresp)
205 1.1 pooka {
206 1.9.4.2 matt struct puffs_req *preq1, *preq2;
207 1.9.4.2 matt size_t winlen;
208 1.9.4.2 matt int rv;
209 1.9.4.2 matt
210 1.9.4.2 matt /* map incoming preq */
211 1.9.4.2 matt winlen = sizeof(struct puffs_req);
212 1.9.4.2 matt rv = puffs_framebuf_getwindow(pb1, 0, (void *)&preq1, &winlen);
213 1.9.4.2 matt assert(rv == 0); /* frames are always at least puffs_req in size */
214 1.9.4.2 matt assert(winlen = sizeof(struct puffs_req));
215 1.9.4.2 matt
216 1.9.4.2 matt /*
217 1.9.4.2 matt * Check if this is not a response in this slot. That's the
218 1.9.4.2 matt * likely case.
219 1.9.4.2 matt */
220 1.9.4.2 matt if ((preq1->preq_opclass & PUFFSOPFLAG_ISRESPONSE) == 0) {
221 1.9.4.2 matt *notresp = 1;
222 1.9.4.2 matt return 0;
223 1.9.4.2 matt }
224 1.1 pooka
225 1.9.4.2 matt /* map second preq */
226 1.9.4.2 matt winlen = sizeof(struct puffs_req);
227 1.9.4.2 matt rv = puffs_framebuf_getwindow(pb2, 0, (void *)&preq2, &winlen);
228 1.9.4.2 matt assert(rv == 0); /* frames are always at least puffs_req in size */
229 1.9.4.2 matt assert(winlen = sizeof(struct puffs_req));
230 1.1 pooka
231 1.9.4.2 matt /* then compare: resid equal? */
232 1.9.4.2 matt return preq1->preq_id != preq2->preq_id;
233 1.2 pooka }
234 1.2 pooka
235 1.2 pooka void
236 1.9.4.2 matt puffs_fsframe_gotframe(struct puffs_usermount *pu, struct puffs_framebuf *pb)
237 1.1 pooka {
238 1.9.4.2 matt struct puffs_framebuf *newpb;
239 1.1 pooka
240 1.9.4.2 matt if ((newpb = puffs_framebuf_make()) == NULL)
241 1.9.4.2 matt abort();
242 1.9.4.2 matt /* XXX: optimize */
243 1.9.4.2 matt puffs__framebuf_moveinfo(pb, newpb);
244 1.9.4.2 matt puffs_framebuf_seekset(newpb, 0);
245 1.9.4.2 matt if (puffs_framebuf_reserve_space(newpb, PUFFS_MSG_MAXSIZE) == -1)
246 1.9.4.2 matt abort();
247 1.3 pooka
248 1.9.4.2 matt puffs_dopufbuf(pu, newpb);
249 1.3 pooka }
250