#include <sys/cdefs.h>
#if !defined(lint)
__RCSID("$NetBSD: requests.c,v 1.24 2013/01/23 20:22:34 riastradh Exp $");
#endif
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <dev/putter/putter.h>
#include <assert.h>
#include <errno.h>
#include <puffs.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "puffs_priv.h"
int
puffs__fsframe_read(struct puffs_usermount *pu, struct puffs_framebuf *pb,
int fd, int *done)
{
struct putter_hdr phdr;
void *win;
size_t howmuch, winlen, curoff;
ssize_t n;
int lenstate;
the_next_level:
curoff = puffs_framebuf_telloff(pb);
if (curoff < sizeof(struct putter_hdr)) {
howmuch = sizeof(struct putter_hdr) - curoff;
lenstate = 1;
} else {
puffs_framebuf_getdata_atoff(pb, 0, &phdr, sizeof(phdr));
howmuch = phdr.pth_framelen - curoff;
lenstate = 0;
}
if (puffs_framebuf_reserve_space(pb, howmuch) == -1)
return errno;
while (howmuch) {
winlen = howmuch;
curoff = puffs_framebuf_telloff(pb);
if (puffs_framebuf_getwindow(pb, curoff, &win, &winlen) == -1)
return errno;
n = read(fd, win, winlen);
switch (n) {
case 0:
return ECONNRESET;
case -1:
if (errno == EAGAIN)
return 0;
return errno;
default:
howmuch -= n;
puffs_framebuf_seekset(pb, curoff + n);
break;
}
}
if (lenstate)
goto the_next_level;
puffs_framebuf_seekset(pb, 0);
*done = 1;
return 0;
}
int
puffs__fsframe_write(struct puffs_usermount *pu, struct puffs_framebuf *pb,
int fd, int *done)
{
void *win;
uint64_t flen;
size_t winlen, howmuch, curoff;
ssize_t n;
int rv;
if (puffs_framebuf_telloff(pb) == 0) {
struct puffs_req *preq;
winlen = sizeof(struct puffs_req);
rv = puffs_framebuf_getwindow(pb, 0, (void *)&preq, &winlen);
if (rv == -1)
return errno;
preq->preq_pth.pth_framelen = flen = preq->preq_buflen;
} else {
struct putter_hdr phdr;
puffs_framebuf_getdata_atoff(pb, 0, &phdr, sizeof(phdr));
flen = phdr.pth_framelen;
}
howmuch = flen - puffs_framebuf_telloff(pb);
while (howmuch) {
winlen = howmuch;
curoff = puffs_framebuf_telloff(pb);
if (puffs_framebuf_getwindow(pb, curoff, &win, &winlen) == -1)
return errno;
assert(winlen == howmuch);
#if 0
n = send(fd, win, winlen, MSG_NOSIGNAL);
#else
n = write(fd, win, winlen);
#endif
switch (n) {
case 0:
return ECONNRESET;
case -1:
if (errno == EAGAIN)
return 0;
return errno;
default:
howmuch -= n;
puffs_framebuf_seekset(pb, curoff + n);
break;
}
}
*done = 1;
return 0;
}
int
puffs__fsframe_cmp(struct puffs_usermount *pu,
struct puffs_framebuf *pb1, struct puffs_framebuf *pb2, int *notresp)
{
struct puffs_req *preq1, *preq2;
size_t winlen;
int rv;
winlen = sizeof(struct puffs_req);
rv = puffs_framebuf_getwindow(pb1, 0, (void *)&preq1, &winlen);
assert(rv == 0);
assert(winlen == sizeof(struct puffs_req));
if ((preq1->preq_opclass & PUFFSOPFLAG_ISRESPONSE) == 0) {
*notresp = 1;
return 0;
}
winlen = sizeof(struct puffs_req);
rv = puffs_framebuf_getwindow(pb2, 0, (void *)&preq2, &winlen);
assert(rv == 0);
assert(winlen == sizeof(struct puffs_req));
return preq1->preq_id != preq2->preq_id;
}
void
puffs__fsframe_gotframe(struct puffs_usermount *pu, struct puffs_framebuf *pb)
{
puffs_framebuf_seekset(pb, 0);
puffs__ml_dispatch(pu, pb);
}