requests.c revision 1.12 1 /* $NetBSD: requests.c,v 1.12 2007/10/21 16:29:40 he Exp $ */
2
3 /*
4 * Copyright (c) 2006 Antti Kantee. All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 #if !defined(lint)
30 __RCSID("$NetBSD: requests.c,v 1.12 2007/10/21 16:29:40 he Exp $");
31 #endif /* !lint */
32
33 #include <sys/types.h>
34 #include <sys/ioctl.h>
35 #include <sys/queue.h>
36
37 #include <assert.h>
38 #include <errno.h>
39 #include <puffs.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43
44 #include "puffs_priv.h"
45
46 /*
47 * XXX: a lot of this stuff is together just with string and bubblegum now
48 */
49
50 /*ARGSUSED*/
51 struct puffs_getreq *
52 puffs_req_makeget(struct puffs_usermount *pu, size_t buflen, int maxops)
53 {
54 struct puffs_getreq *pgr;
55
56 pgr = malloc(sizeof(struct puffs_getreq));
57 if (!pgr)
58 return NULL;
59
60 pgr->pgr_pu = pu;
61 pgr->pgr_buf = NULL;
62
63 return pgr;
64 }
65
66 int
67 puffs_req_loadget(struct puffs_getreq *pgr)
68 {
69 struct puffs_frame pfr;
70 uint8_t *buf;
71 size_t rlen;
72 int fd = pgr->pgr_pu->pu_fd;
73
74 assert(pgr->pgr_buf == NULL);
75
76 if (read(fd, &pfr, sizeof(struct puffs_frame)) == -1) {
77 if (errno == EWOULDBLOCK)
78 return 0;
79 return -1;
80 }
81 buf = malloc(pfr.pfr_alloclen);
82 assert(buf != NULL); /* XXX: a bit more grace here, thanks */
83 memcpy(buf, &pfr, sizeof(pfr));
84
85 rlen = pfr.pfr_len - sizeof(pfr);
86 if (read(fd, buf + sizeof(pfr), rlen) != rlen) { /* XXX */
87 free(buf);
88 return -1;
89 }
90 pgr->pgr_buf = buf;
91
92 return 0;
93 }
94
95 struct puffs_req *
96 puffs_req_get(struct puffs_getreq *pgr)
97 {
98 void *buf;
99
100 buf = pgr->pgr_buf;
101 pgr->pgr_buf = NULL;
102 return buf;
103 }
104
105 int
106 puffs_req_remainingget(struct puffs_getreq *pgr)
107 {
108
109 return pgr->pgr_buf != NULL;
110 }
111
112 /*ARGSUSED*/
113 void
114 puffs_req_setmaxget(struct puffs_getreq *pgr, int maxops)
115 {
116
117 /* nada */
118 }
119
120 void
121 puffs_req_destroyget(struct puffs_getreq *pgr)
122 {
123
124 free(pgr->pgr_buf);
125 free(pgr);
126 }
127
128
129 struct puffs_putreq *
130 puffs_req_makeput(struct puffs_usermount *pu)
131 {
132 struct puffs_putreq *ppr;
133
134 ppr = malloc(sizeof(struct puffs_putreq));
135 if (!ppr)
136 return NULL;
137 memset(ppr, 0, sizeof(*ppr));
138
139 TAILQ_INIT(&ppr->ppr_pccq);
140
141 ppr->ppr_pu = pu;
142
143 return ppr;
144 }
145
146 void
147 puffs_req_put(struct puffs_putreq *ppr, struct puffs_req *preq)
148 {
149 ssize_t n;
150
151 /* LINTED conversion is benign, says author; may revisit */
152 preq->preq_frhdr.pfr_len = preq->preq_buflen;
153 n = write(ppr->ppr_pu->pu_fd, preq, preq->preq_frhdr.pfr_len);
154 assert(n == preq->preq_frhdr.pfr_len);
155 free(preq);
156 }
157
158 /*
159 * instead of a direct preq, put a cc onto the push queue
160 */
161 void
162 puffs_req_putcc(struct puffs_putreq *ppr, struct puffs_cc *pcc)
163 {
164
165 puffs_req_put(ppr, pcc->pcc_preq);
166 TAILQ_INSERT_TAIL(&ppr->ppr_pccq, pcc, entries);
167 }
168
169 /*ARGSUSED*/
170 int
171 puffs_req_putput(struct puffs_putreq *ppr)
172 {
173
174 return 0;
175 }
176
177 void
178 puffs_req_resetput(struct puffs_putreq *ppr)
179 {
180 struct puffs_cc *pcc;
181
182 while ((pcc = TAILQ_FIRST(&ppr->ppr_pccq)) != NULL) {
183 TAILQ_REMOVE(&ppr->ppr_pccq, pcc, entries);
184 puffs_cc_destroy(pcc);
185 }
186 }
187
188 void
189 puffs_req_destroyput(struct puffs_putreq *ppr)
190 {
191
192 puffs_req_resetput(ppr);
193 free(ppr);
194 }
195
196 /*ARGSUSED*/
197 int
198 puffs_req_handle(struct puffs_getreq *pgr, struct puffs_putreq *ppr, int maxops)
199 {
200 struct puffs_usermount *pu;
201 struct puffs_req *preq;
202 int pval;
203
204 assert(pgr->pgr_pu == ppr->ppr_pu);
205 pu = pgr->pgr_pu;
206
207 if (puffs_req_loadget(pgr) == -1)
208 return -1;
209
210 pval = 0;
211 while ((preq = puffs_req_get(pgr)) != NULL
212 && puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED)
213 pval = puffs_dopreq(pu, preq, ppr);
214
215 return pval;
216 }
217