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