requests.c revision 1.10 1 /* $NetBSD: requests.c,v 1.10 2007/10/11 19:41:15 pooka 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.10 2007/10/11 19:41:15 pooka 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_len);
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 n = write(ppr->ppr_pu->pu_fd, preq, preq->preq_frhdr.pfr_len);
152 assert(n == preq->preq_frhdr.pfr_len);
153 free(preq);
154 }
155
156 /*
157 * instead of a direct preq, put a cc onto the push queue
158 */
159 void
160 puffs_req_putcc(struct puffs_putreq *ppr, struct puffs_cc *pcc)
161 {
162
163 puffs_req_put(ppr, pcc->pcc_preq);
164 TAILQ_INSERT_TAIL(&ppr->ppr_pccq, pcc, entries);
165 }
166
167 /*ARGSUSED*/
168 int
169 puffs_req_putput(struct puffs_putreq *ppr)
170 {
171
172 return 0;
173 }
174
175 void
176 puffs_req_resetput(struct puffs_putreq *ppr)
177 {
178 struct puffs_cc *pcc;
179
180 while ((pcc = TAILQ_FIRST(&ppr->ppr_pccq)) != NULL) {
181 TAILQ_REMOVE(&ppr->ppr_pccq, pcc, entries);
182 puffs_cc_destroy(pcc);
183 }
184 }
185
186 void
187 puffs_req_destroyput(struct puffs_putreq *ppr)
188 {
189
190 puffs_req_resetput(ppr);
191 free(ppr);
192 }
193
194 /*ARGSUSED*/
195 int
196 puffs_req_handle(struct puffs_getreq *pgr, struct puffs_putreq *ppr, int maxops)
197 {
198 struct puffs_usermount *pu;
199 struct puffs_req *preq;
200 int pval;
201
202 assert(pgr->pgr_pu == ppr->ppr_pu);
203 pu = pgr->pgr_pu;
204
205 if (puffs_req_loadget(pgr) == -1)
206 return -1;
207
208 pval = 0;
209 while ((preq = puffs_req_get(pgr)) != NULL
210 && puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED)
211 pval = puffs_dopreq(pu, preq, ppr);
212
213 return pval;
214 }
215