1 /* $NetBSD: requests.c,v 1.24 2013/01/23 20:22:34 riastradh 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/cdefs.h>
33 __RCSID("$NetBSD: requests.c,v 1.24 2013/01/23 20:22:34 riastradh Exp $");
36 #include <sys/types.h>
37 #include <sys/ioctl.h>
38 #include <sys/queue.h>
39 #include <sys/socket.h>
41 #include <dev/putter/putter.h>
50 #include "puffs_priv.h"
53 * Read a frame from the upstream provider. First read the frame
54 * length and after this read the actual contents. Yes, optimize
59 puffs__fsframe_read(struct puffs_usermount
*pu
, struct puffs_framebuf
*pb
,
62 struct putter_hdr phdr
;
64 size_t howmuch
, winlen
, curoff
;
68 /* How much to read? */
70 curoff
= puffs_framebuf_telloff(pb
);
71 if (curoff
< sizeof(struct putter_hdr
)) {
72 howmuch
= sizeof(struct putter_hdr
) - curoff
;
75 puffs_framebuf_getdata_atoff(pb
, 0, &phdr
, sizeof(phdr
));
77 howmuch
= phdr
.pth_framelen
- curoff
;
81 if (puffs_framebuf_reserve_space(pb
, howmuch
) == -1)
87 curoff
= puffs_framebuf_telloff(pb
);
88 if (puffs_framebuf_getwindow(pb
, curoff
, &win
, &winlen
) == -1)
90 n
= read(fd
, win
, winlen
);
100 puffs_framebuf_seekset(pb
, curoff
+ n
);
108 puffs_framebuf_seekset(pb
, 0);
114 * Write a frame upstream
118 puffs__fsframe_write(struct puffs_usermount
*pu
, struct puffs_framebuf
*pb
,
123 size_t winlen
, howmuch
, curoff
;
128 * Finalize it if we haven't written anything yet (or we're still
129 * attempting to write the first byte)
131 * XXX: this shouldn't be here
133 if (puffs_framebuf_telloff(pb
) == 0) {
134 struct puffs_req
*preq
;
136 winlen
= sizeof(struct puffs_req
);
137 rv
= puffs_framebuf_getwindow(pb
, 0, (void *)&preq
, &winlen
);
140 preq
->preq_pth
.pth_framelen
= flen
= preq
->preq_buflen
;
142 struct putter_hdr phdr
;
144 puffs_framebuf_getdata_atoff(pb
, 0, &phdr
, sizeof(phdr
));
145 flen
= phdr
.pth_framelen
;
149 * Then write it. Chances are if we are talking to the kernel it'll
150 * just shlosh in all at once, but if we're e.g. talking to the
151 * network it might take a few tries.
154 howmuch
= flen
- puffs_framebuf_telloff(pb
);
158 curoff
= puffs_framebuf_telloff(pb
);
159 if (puffs_framebuf_getwindow(pb
, curoff
, &win
, &winlen
) == -1)
163 * XXX: we know from the framebuf implementation that we
164 * will always managed to map the entire window. But if
165 * that changes, this will catch it. Then we can do stuff
168 assert(winlen
== howmuch
);
170 /* XXX: want NOSIGNAL if writing to a pipe */
172 n
= send(fd
, win
, winlen
, MSG_NOSIGNAL
);
174 n
= write(fd
, win
, winlen
);
185 puffs_framebuf_seekset(pb
, curoff
+ n
);
195 * Compare if "pb1" is a response to a previously sent frame pb2.
196 * More often than not "pb1" is not a response to anything but
197 * rather a fresh request from the kernel.
201 puffs__fsframe_cmp(struct puffs_usermount
*pu
,
202 struct puffs_framebuf
*pb1
, struct puffs_framebuf
*pb2
, int *notresp
)
204 struct puffs_req
*preq1
, *preq2
;
208 /* map incoming preq */
209 winlen
= sizeof(struct puffs_req
);
210 rv
= puffs_framebuf_getwindow(pb1
, 0, (void *)&preq1
, &winlen
);
211 assert(rv
== 0); /* frames are always at least puffs_req in size */
212 assert(winlen
== sizeof(struct puffs_req
));
215 * Check if this is not a response in this slot. That's the
218 if ((preq1
->preq_opclass
& PUFFSOPFLAG_ISRESPONSE
) == 0) {
223 /* map second preq */
224 winlen
= sizeof(struct puffs_req
);
225 rv
= puffs_framebuf_getwindow(pb2
, 0, (void *)&preq2
, &winlen
);
226 assert(rv
== 0); /* frames are always at least puffs_req in size */
227 assert(winlen
== sizeof(struct puffs_req
));
229 /* then compare: resid equal? */
230 return preq1
->preq_id
!= preq2
->preq_id
;
234 puffs__fsframe_gotframe(struct puffs_usermount
*pu
, struct puffs_framebuf
*pb
)
237 puffs_framebuf_seekset(pb
, 0);
238 puffs__ml_dispatch(pu
, pb
);