xref: /netbsd-src/lib/libpuffs/requests.c (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /*	$NetBSD: requests.c,v 1.16 2007/11/12 16:39:35 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.16 2007/11/12 16:39:35 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 putter_hdr pth;
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, &pth, sizeof(struct putter_hdr)) == -1) {
77 		if (errno == EWOULDBLOCK)
78 			return 0;
79 		return -1;
80 	}
81 	buf = malloc(PUFFS_MSG_MAXSIZE); /* XXX */
82 	assert(buf != NULL); /* XXX: a bit more grace here, thanks */
83 	memcpy(buf, &pth, sizeof(pth));
84 
85 	/* LINTED */
86 	rlen = pth.pth_framelen - sizeof(pth);
87 	if (read(fd, buf + sizeof(pth), rlen) != rlen) { /* XXX */
88 		free(buf);
89 		return -1;
90 	}
91 	pgr->pgr_buf = buf;
92 
93 	return 0;
94 }
95 
96 struct puffs_req *
97 puffs_req_get(struct puffs_getreq *pgr)
98 {
99 	void *buf;
100 
101 	buf = pgr->pgr_buf;
102 	pgr->pgr_buf = NULL;
103 	return buf;
104 }
105 
106 int
107 puffs_req_remainingget(struct puffs_getreq *pgr)
108 {
109 
110 	return pgr->pgr_buf != NULL;
111 }
112 
113 /*ARGSUSED*/
114 void
115 puffs_req_setmaxget(struct puffs_getreq *pgr, int maxops)
116 {
117 
118 	/* nada */
119 }
120 
121 void
122 puffs_req_destroyget(struct puffs_getreq *pgr)
123 {
124 
125 	free(pgr->pgr_buf);
126 	free(pgr);
127 }
128 
129 
130 struct puffs_putreq *
131 puffs_req_makeput(struct puffs_usermount *pu)
132 {
133 	struct puffs_putreq *ppr;
134 
135 	ppr = malloc(sizeof(struct puffs_putreq));
136 	if (!ppr)
137 		return NULL;
138 	memset(ppr, 0, sizeof(*ppr));
139 
140 	TAILQ_INIT(&ppr->ppr_pccq);
141 
142 	ppr->ppr_pu = pu;
143 
144 	return ppr;
145 }
146 
147 void
148 puffs_req_put(struct puffs_putreq *ppr, struct puffs_req *preq)
149 {
150 	ssize_t n;
151 
152 	/* LINTED conversion is benign, says author; may revisit */
153 	preq->preq_pth.pth_framelen = preq->preq_buflen;
154 	n = write(ppr->ppr_pu->pu_fd, preq, preq->preq_pth.pth_framelen);
155 	assert(n == preq->preq_pth.pth_framelen);
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 	PU_LOCK();
166 	puffs_req_put(ppr, pcc->pcc_preq);
167 	TAILQ_INSERT_TAIL(&ppr->ppr_pccq, pcc, entries);
168 	PU_UNLOCK();
169 }
170 
171 /*ARGSUSED*/
172 int
173 puffs_req_putput(struct puffs_putreq *ppr)
174 {
175 
176 	return 0;
177 }
178 
179 void
180 puffs_req_resetput(struct puffs_putreq *ppr)
181 {
182 	struct puffs_cc *pcc;
183 
184 	PU_LOCK();
185 	while ((pcc = TAILQ_FIRST(&ppr->ppr_pccq)) != NULL) {
186 		TAILQ_REMOVE(&ppr->ppr_pccq, pcc, entries);
187 		puffs_cc_destroy(pcc);
188 	}
189 	PU_UNLOCK();
190 }
191 
192 void
193 puffs_req_destroyput(struct puffs_putreq *ppr)
194 {
195 
196 	puffs_req_resetput(ppr);
197 	free(ppr);
198 }
199 
200 /*ARGSUSED*/
201 int
202 puffs_req_handle(struct puffs_getreq *pgr, struct puffs_putreq *ppr, int maxops)
203 {
204 	struct puffs_usermount *pu;
205 	struct puffs_req *preq;
206 	int pval;
207 
208 	assert(pgr->pgr_pu == ppr->ppr_pu);
209 	pu = pgr->pgr_pu;
210 
211 	if (puffs_req_loadget(pgr) == -1)
212 		return -1;
213 
214 	pval = 0;
215 	while ((preq = puffs_req_get(pgr)) != NULL
216 	    && puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED)
217 		pval = puffs_dopreq(pu, preq, ppr);
218 
219 	return pval;
220 }
221