puffs_msgif.c revision 1.24 1 1.24 pooka /* $NetBSD: puffs_msgif.c,v 1.24 2007/03/30 17:48:57 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.16 pooka * Copyright (c) 2005, 2006, 2007 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Development of this software was supported by the
7 1.1 pooka * Google Summer of Code program and the Ulla Tuominen Foundation.
8 1.1 pooka * The Google SoC project was mentored by Bill Studenmund.
9 1.1 pooka *
10 1.1 pooka * Redistribution and use in source and binary forms, with or without
11 1.1 pooka * modification, are permitted provided that the following conditions
12 1.1 pooka * are met:
13 1.1 pooka * 1. Redistributions of source code must retain the above copyright
14 1.1 pooka * notice, this list of conditions and the following disclaimer.
15 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 pooka * notice, this list of conditions and the following disclaimer in the
17 1.1 pooka * documentation and/or other materials provided with the distribution.
18 1.1 pooka * 3. The name of the company nor the name of the author may be used to
19 1.1 pooka * endorse or promote products derived from this software without specific
20 1.1 pooka * prior written permission.
21 1.1 pooka *
22 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
23 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 pooka * SUCH DAMAGE.
33 1.1 pooka */
34 1.1 pooka
35 1.1 pooka #include <sys/cdefs.h>
36 1.24 pooka __KERNEL_RCSID(0, "$NetBSD: puffs_msgif.c,v 1.24 2007/03/30 17:48:57 pooka Exp $");
37 1.1 pooka
38 1.1 pooka #include <sys/param.h>
39 1.16 pooka #include <sys/fstrans.h>
40 1.1 pooka #include <sys/malloc.h>
41 1.1 pooka #include <sys/mount.h>
42 1.1 pooka #include <sys/vnode.h>
43 1.1 pooka #include <sys/lock.h>
44 1.1 pooka
45 1.1 pooka #include <fs/puffs/puffs_msgif.h>
46 1.1 pooka #include <fs/puffs/puffs_sys.h>
47 1.1 pooka
48 1.22 pooka /*
49 1.22 pooka * waitq data structures
50 1.22 pooka */
51 1.22 pooka
52 1.22 pooka /*
53 1.22 pooka * While a request is going to userspace, park the caller within the
54 1.22 pooka * kernel. This is the kernel counterpart of "struct puffs_req".
55 1.22 pooka */
56 1.22 pooka struct puffs_park {
57 1.22 pooka struct puffs_req *park_preq; /* req followed by buf */
58 1.22 pooka uint64_t park_id; /* duplicate of preq_id */
59 1.22 pooka
60 1.22 pooka size_t park_copylen; /* userspace copylength */
61 1.22 pooka size_t park_maxlen; /* max size in comeback */
62 1.24 pooka
63 1.24 pooka parkdone_fn park_done;
64 1.24 pooka void *park_donearg;
65 1.22 pooka
66 1.22 pooka int park_flags;
67 1.22 pooka
68 1.22 pooka kcondvar_t park_cv;
69 1.22 pooka TAILQ_ENTRY(puffs_park) park_entries;
70 1.22 pooka };
71 1.22 pooka #define PARKFLAG_WAITERGONE 0x01
72 1.22 pooka #define PARKFLAG_PROCESSING 0x02
73 1.24 pooka #define PARKFLAG_CALL 0x04
74 1.22 pooka
75 1.22 pooka static struct pool_cache parkpc;
76 1.22 pooka static struct pool parkpool;
77 1.22 pooka
78 1.22 pooka static int
79 1.22 pooka makepark(void *arg, void *obj, int flags)
80 1.22 pooka {
81 1.22 pooka struct puffs_park *park = obj;
82 1.22 pooka
83 1.22 pooka cv_init(&park->park_cv, "puffsrpl");
84 1.22 pooka
85 1.22 pooka return 0;
86 1.22 pooka }
87 1.22 pooka
88 1.22 pooka static void
89 1.22 pooka nukepark(void *arg, void *obj)
90 1.22 pooka {
91 1.22 pooka struct puffs_park *park = obj;
92 1.22 pooka
93 1.22 pooka cv_destroy(&park->park_cv);
94 1.22 pooka }
95 1.22 pooka
96 1.22 pooka void
97 1.22 pooka puffs_msgif_init()
98 1.22 pooka {
99 1.22 pooka
100 1.22 pooka pool_init(&parkpool, sizeof(struct puffs_park), 0, 0, 0,
101 1.22 pooka "puffprkl", &pool_allocator_nointr, IPL_NONE);
102 1.22 pooka pool_cache_init(&parkpc, &parkpool, makepark, nukepark, NULL);
103 1.22 pooka }
104 1.22 pooka
105 1.22 pooka void
106 1.22 pooka puffs_msgif_destroy()
107 1.22 pooka {
108 1.22 pooka
109 1.22 pooka pool_cache_destroy(&parkpc);
110 1.22 pooka pool_destroy(&parkpool);
111 1.22 pooka }
112 1.22 pooka
113 1.22 pooka void *
114 1.22 pooka puffs_parkmem_alloc(int waitok)
115 1.22 pooka {
116 1.22 pooka
117 1.22 pooka return pool_cache_get(&parkpc, waitok ? PR_WAITOK : PR_NOWAIT);
118 1.22 pooka }
119 1.22 pooka
120 1.22 pooka void
121 1.22 pooka puffs_parkmem_free(void *ppark)
122 1.22 pooka {
123 1.22 pooka
124 1.22 pooka pool_cache_put(&parkpc, ppark);
125 1.22 pooka }
126 1.22 pooka
127 1.22 pooka
128 1.22 pooka /*
129 1.22 pooka * Converts a non-FAF op to a FAF. This simply involves making copies
130 1.22 pooka * of the park and request structures and tagging the request as a FAF.
131 1.22 pooka * It is safe to block here, since the original op is not a FAF.
132 1.22 pooka */
133 1.22 pooka #if 0
134 1.22 pooka static void
135 1.22 pooka puffs_reqtofaf(struct puffs_park *ppark)
136 1.22 pooka {
137 1.22 pooka struct puffs_req *newpreq;
138 1.22 pooka
139 1.22 pooka KASSERT((ppark->park_preq->preq_opclass & PUFFSOPFLAG_FAF) == 0);
140 1.22 pooka
141 1.22 pooka MALLOC(newpreq, struct puffs_req *, ppark->park_copylen,
142 1.22 pooka M_PUFFS, M_ZERO | M_WAITOK);
143 1.22 pooka
144 1.22 pooka memcpy(newpreq, ppark->park_preq, ppark->park_copylen);
145 1.22 pooka
146 1.22 pooka ppark->park_preq = newpreq;
147 1.22 pooka ppark->park_preq->preq_opclass |= PUFFSOPFLAG_FAF;
148 1.22 pooka }
149 1.22 pooka #endif
150 1.22 pooka
151 1.1 pooka
152 1.1 pooka /*
153 1.1 pooka * kernel-user-kernel waitqueues
154 1.1 pooka */
155 1.1 pooka
156 1.4 pooka static int touser(struct puffs_mount *, struct puffs_park *, uint64_t,
157 1.1 pooka struct vnode *, struct vnode *);
158 1.1 pooka
159 1.4 pooka uint64_t
160 1.1 pooka puffs_getreqid(struct puffs_mount *pmp)
161 1.1 pooka {
162 1.14 pooka uint64_t rv;
163 1.1 pooka
164 1.22 pooka mutex_enter(&pmp->pmp_lock);
165 1.1 pooka rv = pmp->pmp_nextreq++;
166 1.22 pooka mutex_exit(&pmp->pmp_lock);
167 1.1 pooka
168 1.1 pooka return rv;
169 1.1 pooka }
170 1.1 pooka
171 1.1 pooka /* vfs request */
172 1.1 pooka int
173 1.1 pooka puffs_vfstouser(struct puffs_mount *pmp, int optype, void *kbuf, size_t buflen)
174 1.1 pooka {
175 1.22 pooka struct puffs_park *ppark;
176 1.1 pooka
177 1.24 pooka ppark = puffs_parkmem_alloc(1);
178 1.22 pooka ppark->park_preq = kbuf;
179 1.1 pooka
180 1.22 pooka ppark->park_preq->preq_opclass = PUFFSOP_VFS;
181 1.22 pooka ppark->park_preq->preq_optype = optype;
182 1.1 pooka
183 1.22 pooka ppark->park_maxlen = ppark->park_copylen = buflen;
184 1.22 pooka ppark->park_flags = 0;
185 1.1 pooka
186 1.22 pooka return touser(pmp, ppark, puffs_getreqid(pmp), NULL, NULL);
187 1.1 pooka }
188 1.1 pooka
189 1.16 pooka void
190 1.16 pooka puffs_suspendtouser(struct puffs_mount *pmp, int status)
191 1.16 pooka {
192 1.16 pooka struct puffs_vfsreq_suspend *pvfsr_susp;
193 1.16 pooka struct puffs_park *ppark;
194 1.16 pooka
195 1.16 pooka pvfsr_susp = malloc(sizeof(struct puffs_vfsreq_suspend),
196 1.16 pooka M_PUFFS, M_WAITOK | M_ZERO);
197 1.24 pooka ppark = puffs_parkmem_alloc(1);
198 1.16 pooka
199 1.16 pooka pvfsr_susp->pvfsr_status = status;
200 1.16 pooka ppark->park_preq = (struct puffs_req *)pvfsr_susp;
201 1.16 pooka
202 1.16 pooka ppark->park_preq->preq_opclass = PUFFSOP_VFS | PUFFSOPFLAG_FAF;
203 1.16 pooka ppark->park_preq->preq_optype = PUFFS_VFS_SUSPEND;
204 1.16 pooka
205 1.16 pooka ppark->park_maxlen = ppark->park_copylen
206 1.16 pooka = sizeof(struct puffs_vfsreq_suspend);
207 1.19 pooka ppark->park_flags = 0;
208 1.16 pooka
209 1.16 pooka (void)touser(pmp, ppark, 0, NULL, NULL);
210 1.16 pooka }
211 1.16 pooka
212 1.1 pooka /*
213 1.1 pooka * vnode level request
214 1.1 pooka */
215 1.1 pooka int
216 1.1 pooka puffs_vntouser(struct puffs_mount *pmp, int optype,
217 1.24 pooka void *kbuf, size_t buflen, size_t maxdelta, void *cookie,
218 1.1 pooka struct vnode *vp1, struct vnode *vp2)
219 1.1 pooka {
220 1.22 pooka struct puffs_park *ppark;
221 1.1 pooka
222 1.24 pooka ppark = puffs_parkmem_alloc(1);
223 1.22 pooka ppark->park_preq = kbuf;
224 1.1 pooka
225 1.22 pooka ppark->park_preq->preq_opclass = PUFFSOP_VN;
226 1.22 pooka ppark->park_preq->preq_optype = optype;
227 1.22 pooka ppark->park_preq->preq_cookie = cookie;
228 1.9 pooka
229 1.24 pooka ppark->park_copylen = buflen;
230 1.24 pooka ppark->park_maxlen = buflen + maxdelta;
231 1.22 pooka ppark->park_flags = 0;
232 1.1 pooka
233 1.22 pooka return touser(pmp, ppark, puffs_getreqid(pmp), vp1, vp2);
234 1.1 pooka }
235 1.1 pooka
236 1.1 pooka /*
237 1.1 pooka * vnode level request, caller-controller req id
238 1.1 pooka */
239 1.1 pooka int
240 1.1 pooka puffs_vntouser_req(struct puffs_mount *pmp, int optype,
241 1.24 pooka void *kbuf, size_t buflen, size_t maxdelta, void *cookie,
242 1.24 pooka uint64_t reqid, struct vnode *vp1, struct vnode *vp2)
243 1.1 pooka {
244 1.22 pooka struct puffs_park *ppark;
245 1.1 pooka
246 1.24 pooka ppark = puffs_parkmem_alloc(1);
247 1.22 pooka ppark->park_preq = kbuf;
248 1.9 pooka
249 1.22 pooka ppark->park_preq->preq_opclass = PUFFSOP_VN;
250 1.22 pooka ppark->park_preq->preq_optype = optype;
251 1.22 pooka ppark->park_preq->preq_cookie = cookie;
252 1.1 pooka
253 1.24 pooka ppark->park_copylen = buflen;
254 1.24 pooka ppark->park_maxlen = buflen + maxdelta;
255 1.22 pooka ppark->park_flags = 0;
256 1.1 pooka
257 1.22 pooka return touser(pmp, ppark, reqid, vp1, vp2);
258 1.1 pooka }
259 1.1 pooka
260 1.24 pooka void
261 1.24 pooka puffs_vntouser_call(struct puffs_mount *pmp, int optype,
262 1.24 pooka void *kbuf, size_t buflen, size_t maxdelta, void *cookie,
263 1.24 pooka parkdone_fn donefn, void *donearg,
264 1.24 pooka struct vnode *vp1, struct vnode *vp2)
265 1.1 pooka {
266 1.22 pooka struct puffs_park *ppark;
267 1.1 pooka
268 1.24 pooka ppark = puffs_parkmem_alloc(1);
269 1.22 pooka ppark->park_preq = kbuf;
270 1.1 pooka
271 1.22 pooka ppark->park_preq->preq_opclass = PUFFSOP_VN;
272 1.22 pooka ppark->park_preq->preq_optype = optype;
273 1.22 pooka ppark->park_preq->preq_cookie = cookie;
274 1.9 pooka
275 1.22 pooka ppark->park_copylen = buflen;
276 1.22 pooka ppark->park_maxlen = buflen + maxdelta;
277 1.24 pooka ppark->park_done = donefn;
278 1.24 pooka ppark->park_donearg = donearg;
279 1.24 pooka ppark->park_flags = PARKFLAG_CALL;
280 1.1 pooka
281 1.24 pooka (void) touser(pmp, ppark, puffs_getreqid(pmp), vp1, vp2);
282 1.20 pooka }
283 1.20 pooka
284 1.20 pooka /*
285 1.4 pooka * Notice: kbuf will be free'd later. I must be allocated from the
286 1.4 pooka * kernel heap and it's ownership is shifted to this function from
287 1.4 pooka * now on, i.e. the caller is not allowed to use it anymore!
288 1.4 pooka */
289 1.4 pooka void
290 1.4 pooka puffs_vntouser_faf(struct puffs_mount *pmp, int optype,
291 1.4 pooka void *kbuf, size_t buflen, void *cookie)
292 1.4 pooka {
293 1.4 pooka struct puffs_park *ppark;
294 1.4 pooka
295 1.4 pooka /* XXX: is it allowable to sleep here? */
296 1.24 pooka ppark = puffs_parkmem_alloc(0);
297 1.4 pooka if (ppark == NULL)
298 1.4 pooka return; /* 2bad */
299 1.4 pooka
300 1.9 pooka ppark->park_preq = kbuf;
301 1.9 pooka
302 1.9 pooka ppark->park_preq->preq_opclass = PUFFSOP_VN | PUFFSOPFLAG_FAF;
303 1.9 pooka ppark->park_preq->preq_optype = optype;
304 1.9 pooka ppark->park_preq->preq_cookie = cookie;
305 1.9 pooka
306 1.9 pooka ppark->park_maxlen = ppark->park_copylen = buflen;
307 1.19 pooka ppark->park_flags = 0;
308 1.4 pooka
309 1.4 pooka (void)touser(pmp, ppark, 0, NULL, NULL);
310 1.4 pooka }
311 1.4 pooka
312 1.21 pooka void
313 1.21 pooka puffs_cacheop(struct puffs_mount *pmp, struct puffs_park *ppark,
314 1.21 pooka struct puffs_cacheinfo *pcinfo, size_t pcilen, void *cookie)
315 1.21 pooka {
316 1.21 pooka
317 1.21 pooka ppark->park_preq = (struct puffs_req *)pcinfo;
318 1.21 pooka ppark->park_preq->preq_opclass = PUFFSOP_CACHE | PUFFSOPFLAG_FAF;
319 1.21 pooka ppark->park_preq->preq_optype = PCACHE_TYPE_WRITE; /* XXX */
320 1.21 pooka ppark->park_preq->preq_cookie = cookie;
321 1.21 pooka
322 1.21 pooka ppark->park_maxlen = ppark->park_copylen = pcilen;
323 1.21 pooka
324 1.21 pooka (void)touser(pmp, ppark, 0, NULL, NULL);
325 1.21 pooka }
326 1.21 pooka
327 1.4 pooka /*
328 1.1 pooka * Wait for the userspace ping-pong game in calling process context.
329 1.1 pooka *
330 1.1 pooka * This unlocks vnodes if they are supplied. vp1 is the vnode
331 1.1 pooka * before in the locking order, i.e. the one which must be locked
332 1.1 pooka * before accessing vp2. This is done here so that operations are
333 1.1 pooka * already ordered in the queue when vnodes are unlocked (I'm not
334 1.1 pooka * sure if that's really necessary, but it can't hurt). Okok, maybe
335 1.1 pooka * there's a slight ugly-factor also, but let's not worry about that.
336 1.1 pooka */
337 1.1 pooka static int
338 1.4 pooka touser(struct puffs_mount *pmp, struct puffs_park *ppark, uint64_t reqid,
339 1.1 pooka struct vnode *vp1, struct vnode *vp2)
340 1.1 pooka {
341 1.16 pooka struct mount *mp;
342 1.9 pooka struct puffs_req *preq;
343 1.19 pooka int rv = 0;
344 1.1 pooka
345 1.16 pooka mp = PMPTOMP(pmp);
346 1.19 pooka preq = ppark->park_preq;
347 1.19 pooka preq->preq_id = ppark->park_id = reqid;
348 1.19 pooka preq->preq_buflen = ALIGN(ppark->park_maxlen);
349 1.19 pooka
350 1.22 pooka #if 0
351 1.22 pooka /*
352 1.22 pooka * We don't trap signals currently
353 1.22 pooka */
354 1.22 pooka struct lwp *l = curlwp;
355 1.22 pooka
356 1.19 pooka /*
357 1.19 pooka * To support PCATCH, yet another movie: check if there are signals
358 1.19 pooka * pending and we are issueing a non-FAF. If so, return an error
359 1.19 pooka * directly UNLESS we are issueing INACTIVE. In that case, convert
360 1.19 pooka * it to a FAF, fire off to the file server and return an error.
361 1.19 pooka * Yes, this is bordering disgusting. Barfbags are on me.
362 1.19 pooka */
363 1.22 pooka if (PUFFSOP_WANTREPLY(preq->preq_opclass)
364 1.24 pooka && (ppark->park_flags & PARKFLAG_CALL) == 0
365 1.19 pooka && (l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0)) {
366 1.19 pooka if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN
367 1.19 pooka && preq->preq_optype == PUFFS_VN_INACTIVE) {
368 1.22 pooka puffs_reqtofaf(ppark);
369 1.22 pooka DPRINTF(("puffs touser: converted to FAF %p\n", ppark));
370 1.19 pooka rv = EINTR;
371 1.19 pooka } else {
372 1.19 pooka return EINTR;
373 1.19 pooka }
374 1.19 pooka }
375 1.22 pooka #endif
376 1.16 pooka
377 1.16 pooka /*
378 1.16 pooka * test for suspension lock.
379 1.16 pooka *
380 1.16 pooka * Note that we *DO NOT* keep the lock, since that might block
381 1.16 pooka * lock acquiring PLUS it would give userlandia control over
382 1.16 pooka * the lock. The operation queue enforces a strict ordering:
383 1.16 pooka * when the fs server gets in the op stream, it knows things
384 1.16 pooka * are in order. The kernel locks can't guarantee that for
385 1.16 pooka * userspace, in any case.
386 1.16 pooka *
387 1.16 pooka * BUT: this presents a problem for ops which have a consistency
388 1.16 pooka * clause based on more than one operation. Unfortunately such
389 1.16 pooka * operations (read, write) do not reliably work yet.
390 1.16 pooka *
391 1.16 pooka * Ya, Ya, it's wrong wong wrong, me be fixink this someday.
392 1.18 pooka *
393 1.18 pooka * XXX: and there is one more problem. We sometimes need to
394 1.18 pooka * take a lazy lock in case the fs is suspending and we are
395 1.18 pooka * executing as the fs server context. This might happen
396 1.18 pooka * e.g. in the case that the user server triggers a reclaim
397 1.18 pooka * in the kernel while the fs is suspending. It's not a very
398 1.18 pooka * likely event, but it needs to be fixed some day.
399 1.16 pooka */
400 1.22 pooka
401 1.22 pooka /*
402 1.22 pooka * MOREXXX: once PUFFS_WCACHEINFO is enabled, we can't take
403 1.22 pooka * the mutex here, since getpages() might be called locked.
404 1.22 pooka */
405 1.18 pooka fstrans_start(mp, FSTRANS_NORMAL);
406 1.22 pooka mutex_enter(&pmp->pmp_lock);
407 1.16 pooka fstrans_done(mp);
408 1.16 pooka
409 1.13 pooka if (pmp->pmp_status != PUFFSTAT_RUNNING) {
410 1.22 pooka mutex_exit(&pmp->pmp_lock);
411 1.24 pooka puffs_parkmem_free(ppark);
412 1.1 pooka return ENXIO;
413 1.1 pooka }
414 1.1 pooka
415 1.4 pooka TAILQ_INSERT_TAIL(&pmp->pmp_req_touser, ppark, park_entries);
416 1.22 pooka pmp->pmp_req_waiters++;
417 1.1 pooka
418 1.22 pooka #if 0
419 1.1 pooka /*
420 1.1 pooka * Don't do unlock-relock dance yet. There are a couple of
421 1.1 pooka * unsolved issues with it. If we don't unlock, we can have
422 1.1 pooka * processes wanting vn_lock in case userspace hangs. But
423 1.1 pooka * that can be "solved" by killing the userspace process. It
424 1.1 pooka * would of course be nicer to have antilocking in the userspace
425 1.1 pooka * interface protocol itself.. your patience will be rewarded.
426 1.1 pooka */
427 1.1 pooka /* unlock */
428 1.1 pooka if (vp2)
429 1.1 pooka VOP_UNLOCK(vp2, 0);
430 1.1 pooka if (vp1)
431 1.1 pooka VOP_UNLOCK(vp1, 0);
432 1.1 pooka #endif
433 1.1 pooka
434 1.20 pooka DPRINTF(("touser: req %" PRIu64 ", preq: %p, park: %p, "
435 1.20 pooka "c/t: 0x%x/0x%x, f: 0x%x\n", preq->preq_id, preq, ppark,
436 1.20 pooka preq->preq_opclass, preq->preq_optype, ppark->park_flags));
437 1.15 pooka
438 1.22 pooka cv_broadcast(&pmp->pmp_req_waiter_cv);
439 1.1 pooka selnotify(pmp->pmp_sel, 0);
440 1.1 pooka
441 1.22 pooka if (PUFFSOP_WANTREPLY(preq->preq_opclass)
442 1.24 pooka && (ppark->park_flags & PARKFLAG_CALL) == 0) {
443 1.19 pooka int error;
444 1.19 pooka
445 1.22 pooka error = 0; /* XXX: no interrupt for now */
446 1.19 pooka
447 1.22 pooka cv_wait(&ppark->park_cv, &pmp->pmp_lock);
448 1.19 pooka if (error) {
449 1.22 pooka ppark->park_flags |= PARKFLAG_WAITERGONE;
450 1.22 pooka if (ppark->park_flags & PARKFLAG_PROCESSING) {
451 1.22 pooka cv_wait(&ppark->park_cv, &pmp->pmp_lock);
452 1.22 pooka rv = preq->preq_rv;
453 1.19 pooka } else {
454 1.22 pooka rv = error;
455 1.19 pooka }
456 1.22 pooka } else {
457 1.22 pooka rv = preq->preq_rv;
458 1.19 pooka }
459 1.22 pooka mutex_exit(&pmp->pmp_lock);
460 1.24 pooka puffs_parkmem_free(ppark);
461 1.22 pooka
462 1.16 pooka /*
463 1.16 pooka * retake the lock and release. This makes sure (haha,
464 1.16 pooka * I'm humorous) that we don't process the same vnode in
465 1.16 pooka * multiple threads due to the locks hacks we have in
466 1.16 pooka * puffs_lock(). In reality this is well protected by
467 1.16 pooka * the biglock, but once that's gone, well, hopefully
468 1.16 pooka * this will be fixed for real. (and when you read this
469 1.16 pooka * comment in 2017 and subsequently barf, my condolences ;).
470 1.16 pooka */
471 1.19 pooka if (rv == 0 && !fstrans_is_owner(mp)) {
472 1.17 hannken fstrans_start(mp, FSTRANS_NORMAL);
473 1.16 pooka fstrans_done(mp);
474 1.16 pooka }
475 1.22 pooka } else {
476 1.22 pooka mutex_exit(&pmp->pmp_lock);
477 1.16 pooka }
478 1.16 pooka
479 1.1 pooka #if 0
480 1.1 pooka /* relock */
481 1.1 pooka if (vp1)
482 1.1 pooka KASSERT(vn_lock(vp1, LK_EXCLUSIVE | LK_RETRY) == 0);
483 1.1 pooka if (vp2)
484 1.1 pooka KASSERT(vn_lock(vp2, LK_EXCLUSIVE | LK_RETRY) == 0);
485 1.1 pooka #endif
486 1.1 pooka
487 1.22 pooka mutex_enter(&pmp->pmp_lock);
488 1.22 pooka if (--pmp->pmp_req_waiters == 0) {
489 1.22 pooka KASSERT(cv_has_waiters(&pmp->pmp_req_waitersink_cv) <= 1);
490 1.22 pooka cv_signal(&pmp->pmp_req_waitersink_cv);
491 1.22 pooka }
492 1.22 pooka mutex_exit(&pmp->pmp_lock);
493 1.16 pooka
494 1.19 pooka return rv;
495 1.1 pooka }
496 1.1 pooka
497 1.1 pooka
498 1.9 pooka /*
499 1.9 pooka * getop: scan through queued requests until:
500 1.9 pooka * 1) max number of requests satisfied
501 1.9 pooka * OR
502 1.9 pooka * 2) buffer runs out of space
503 1.9 pooka * OR
504 1.9 pooka * 3) nonblocking is set AND there are no operations available
505 1.9 pooka * OR
506 1.9 pooka * 4) at least one operation was transferred AND there are no more waiting
507 1.9 pooka */
508 1.10 pooka int
509 1.10 pooka puffs_getop(struct puffs_mount *pmp, struct puffs_reqh_get *phg, int nonblock)
510 1.1 pooka {
511 1.1 pooka struct puffs_park *park;
512 1.9 pooka struct puffs_req *preq;
513 1.9 pooka uint8_t *bufpos;
514 1.9 pooka int error, donesome;
515 1.9 pooka
516 1.9 pooka donesome = error = 0;
517 1.9 pooka bufpos = phg->phg_buf;
518 1.1 pooka
519 1.22 pooka mutex_enter(&pmp->pmp_lock);
520 1.9 pooka while (phg->phg_nops == 0 || donesome != phg->phg_nops) {
521 1.1 pooka again:
522 1.9 pooka if (pmp->pmp_status != PUFFSTAT_RUNNING) {
523 1.9 pooka /* if we got some, they don't really matter anymore */
524 1.9 pooka error = ENXIO;
525 1.9 pooka goto out;
526 1.9 pooka }
527 1.9 pooka if (TAILQ_EMPTY(&pmp->pmp_req_touser)) {
528 1.12 pooka if (donesome)
529 1.12 pooka goto out;
530 1.12 pooka
531 1.12 pooka if (nonblock) {
532 1.12 pooka error = EWOULDBLOCK;
533 1.9 pooka goto out;
534 1.9 pooka }
535 1.9 pooka
536 1.22 pooka error = cv_wait_sig(&pmp->pmp_req_waiter_cv,
537 1.22 pooka &pmp->pmp_lock);
538 1.11 pooka if (error)
539 1.11 pooka goto out;
540 1.11 pooka else
541 1.11 pooka goto again;
542 1.9 pooka }
543 1.9 pooka
544 1.9 pooka park = TAILQ_FIRST(&pmp->pmp_req_touser);
545 1.9 pooka preq = park->park_preq;
546 1.9 pooka if (phg->phg_buflen < preq->preq_buflen) {
547 1.9 pooka if (!donesome)
548 1.9 pooka error = E2BIG;
549 1.9 pooka goto out;
550 1.9 pooka }
551 1.9 pooka TAILQ_REMOVE(&pmp->pmp_req_touser, park, park_entries);
552 1.22 pooka
553 1.22 pooka /* If it's a goner, don't process any furher */
554 1.22 pooka if (park->park_flags & PARKFLAG_WAITERGONE) {
555 1.22 pooka panic("impossible for now");
556 1.24 pooka puffs_parkmem_free(park);
557 1.22 pooka continue;
558 1.22 pooka }
559 1.22 pooka
560 1.22 pooka mutex_exit(&pmp->pmp_lock);
561 1.9 pooka
562 1.9 pooka DPRINTF(("puffsgetop: get op %" PRIu64 " (%d.), from %p "
563 1.9 pooka "len %zu (buflen %zu), target %p\n", preq->preq_id,
564 1.9 pooka donesome, preq, park->park_copylen, preq->preq_buflen,
565 1.9 pooka bufpos));
566 1.9 pooka
567 1.9 pooka if ((error = copyout(preq, bufpos, park->park_copylen)) != 0) {
568 1.22 pooka DPRINTF(("puffs_getop: copyout failed\n"));
569 1.9 pooka /*
570 1.9 pooka * ok, user server is probably trying to cheat.
571 1.9 pooka * stuff op back & return error to user
572 1.9 pooka */
573 1.22 pooka mutex_enter(&pmp->pmp_lock);
574 1.9 pooka TAILQ_INSERT_HEAD(&pmp->pmp_req_touser, park,
575 1.9 pooka park_entries);
576 1.9 pooka
577 1.9 pooka if (donesome)
578 1.9 pooka error = 0;
579 1.9 pooka goto out;
580 1.9 pooka }
581 1.9 pooka bufpos += preq->preq_buflen;
582 1.9 pooka phg->phg_buflen -= preq->preq_buflen;
583 1.9 pooka donesome++;
584 1.9 pooka
585 1.22 pooka mutex_enter(&pmp->pmp_lock);
586 1.9 pooka if (PUFFSOP_WANTREPLY(preq->preq_opclass)) {
587 1.22 pooka TAILQ_INSERT_TAIL(&pmp->pmp_req_replywait, park,
588 1.22 pooka park_entries);
589 1.9 pooka } else {
590 1.9 pooka free(preq, M_PUFFS);
591 1.24 pooka puffs_parkmem_free(park);
592 1.1 pooka }
593 1.1 pooka }
594 1.1 pooka
595 1.9 pooka out:
596 1.22 pooka phg->phg_more = pmp->pmp_req_waiters;
597 1.22 pooka mutex_exit(&pmp->pmp_lock);
598 1.1 pooka
599 1.9 pooka phg->phg_nops = donesome;
600 1.4 pooka
601 1.9 pooka return error;
602 1.1 pooka }
603 1.1 pooka
604 1.10 pooka int
605 1.10 pooka puffs_putop(struct puffs_mount *pmp, struct puffs_reqh_put *php)
606 1.1 pooka {
607 1.1 pooka struct puffs_park *park;
608 1.19 pooka struct puffs_req tmpreq;
609 1.19 pooka struct puffs_req *nextpreq;
610 1.9 pooka void *userbuf;
611 1.9 pooka uint64_t id;
612 1.9 pooka size_t reqlen;
613 1.19 pooka int donesome, error, wgone;
614 1.9 pooka
615 1.19 pooka donesome = error = wgone = 0;
616 1.9 pooka
617 1.9 pooka id = php->php_id;
618 1.9 pooka userbuf = php->php_buf;
619 1.9 pooka reqlen = php->php_buflen;
620 1.1 pooka
621 1.22 pooka mutex_enter(&pmp->pmp_lock);
622 1.9 pooka while (donesome != php->php_nops) {
623 1.19 pooka #ifdef PUFFSDEBUG
624 1.9 pooka DPRINTF(("puffsputop: searching for %" PRIu64 ", ubuf: %p, "
625 1.9 pooka "len %zu\n", id, userbuf, reqlen));
626 1.9 pooka #endif
627 1.9 pooka TAILQ_FOREACH(park, &pmp->pmp_req_replywait, park_entries) {
628 1.19 pooka if (park->park_id == id)
629 1.9 pooka break;
630 1.9 pooka }
631 1.9 pooka
632 1.9 pooka if (park == NULL) {
633 1.24 pooka DPRINTF(("puffsputop: no request: %" PRIu64 "\n", id));
634 1.9 pooka error = EINVAL;
635 1.1 pooka break;
636 1.1 pooka }
637 1.24 pooka
638 1.24 pooka if (reqlen == 0 || reqlen > park->park_maxlen) {
639 1.24 pooka DPRINTF(("puffsputop: invalid buffer length: "
640 1.24 pooka "%zu\n", reqlen));
641 1.24 pooka error = E2BIG;
642 1.24 pooka break;
643 1.24 pooka }
644 1.9 pooka TAILQ_REMOVE(&pmp->pmp_req_replywait, park, park_entries);
645 1.22 pooka wgone = park->park_flags & PARKFLAG_WAITERGONE;
646 1.22 pooka park->park_flags |= PARKFLAG_PROCESSING;
647 1.22 pooka mutex_exit(&pmp->pmp_lock);
648 1.9 pooka
649 1.19 pooka /*
650 1.19 pooka * If the caller has gone south, go to next, collect
651 1.19 pooka * $200 and free the structure there instead of wakeup.
652 1.19 pooka * We also need to copyin the
653 1.19 pooka */
654 1.22 pooka if (wgone) {
655 1.22 pooka panic("puffs: wgone impossible for now\n");
656 1.19 pooka DPRINTF(("puffs_putop: bad service - waiter gone for "
657 1.19 pooka "park %p\n", park));
658 1.19 pooka error = copyin(userbuf, &tmpreq,
659 1.19 pooka sizeof(struct puffs_req));
660 1.19 pooka if (error)
661 1.19 pooka goto loopout;
662 1.19 pooka nextpreq = &tmpreq;
663 1.19 pooka goto next;
664 1.19 pooka }
665 1.19 pooka
666 1.9 pooka DPRINTF(("puffsputpop: copyin from %p to %p, len %zu\n",
667 1.9 pooka userbuf, park->park_preq, reqlen));
668 1.9 pooka error = copyin(userbuf, park->park_preq, reqlen);
669 1.9 pooka if (error)
670 1.9 pooka goto loopout;
671 1.19 pooka nextpreq = park->park_preq;
672 1.9 pooka
673 1.19 pooka next:
674 1.9 pooka /* all's well, prepare for next op */
675 1.19 pooka id = nextpreq->preq_id;
676 1.19 pooka reqlen = nextpreq->preq_buflen;
677 1.19 pooka userbuf = nextpreq->preq_nextbuf;
678 1.9 pooka donesome++;
679 1.9 pooka
680 1.9 pooka loopout:
681 1.24 pooka if (error)
682 1.9 pooka park->park_preq->preq_rv = error;
683 1.24 pooka
684 1.24 pooka if (park->park_flags & PARKFLAG_CALL) {
685 1.24 pooka park->park_done(park->park_preq, park->park_donearg);
686 1.24 pooka puffs_parkmem_free(park);
687 1.20 pooka }
688 1.1 pooka
689 1.22 pooka mutex_enter(&pmp->pmp_lock);
690 1.22 pooka if (!wgone) {
691 1.24 pooka DPRINTF(("puffs_putop: flagging done for "
692 1.24 pooka "park %p\n", park));
693 1.22 pooka
694 1.24 pooka cv_signal(&park->park_cv);
695 1.19 pooka }
696 1.19 pooka
697 1.9 pooka if (error)
698 1.9 pooka break;
699 1.19 pooka wgone = 0;
700 1.1 pooka }
701 1.1 pooka
702 1.22 pooka mutex_exit(&pmp->pmp_lock);
703 1.9 pooka php->php_nops -= donesome;
704 1.1 pooka
705 1.1 pooka return error;
706 1.1 pooka }
707 1.1 pooka
708 1.22 pooka /*
709 1.22 pooka * We're dead, kaput, RIP, slightly more than merely pining for the
710 1.22 pooka * fjords, belly-up, fallen, lifeless, finished, expired, gone to meet
711 1.22 pooka * our maker, ceased to be, etcetc. YASD. It's a dead FS!
712 1.22 pooka *
713 1.22 pooka * Caller must hold puffs mutex.
714 1.22 pooka */
715 1.22 pooka void
716 1.22 pooka puffs_userdead(struct puffs_mount *pmp)
717 1.22 pooka {
718 1.22 pooka struct puffs_park *park;
719 1.22 pooka
720 1.22 pooka /*
721 1.22 pooka * Mark filesystem status as dying so that operations don't
722 1.22 pooka * attempt to march to userspace any longer.
723 1.22 pooka */
724 1.22 pooka pmp->pmp_status = PUFFSTAT_DYING;
725 1.22 pooka
726 1.22 pooka /* signal waiters on REQUEST TO file server queue */
727 1.22 pooka TAILQ_FOREACH(park, &pmp->pmp_req_touser, park_entries) {
728 1.24 pooka uint8_t opclass;
729 1.24 pooka
730 1.24 pooka opclass = park->park_preq->preq_opclass;
731 1.24 pooka park->park_preq->preq_rv = ENXIO;
732 1.22 pooka TAILQ_REMOVE(&pmp->pmp_req_touser, park, park_entries);
733 1.22 pooka
734 1.24 pooka if (park->park_flags & PARKFLAG_CALL) {
735 1.24 pooka park->park_done(park->park_preq, park->park_donearg);
736 1.24 pooka puffs_parkmem_free(park);
737 1.24 pooka } else if (!PUFFSOP_WANTREPLY(opclass)) {
738 1.22 pooka free(park->park_preq, M_PUFFS);
739 1.24 pooka puffs_parkmem_free(park);
740 1.22 pooka } else {
741 1.23 pooka park->park_preq->preq_rv = ENXIO;
742 1.22 pooka cv_signal(&park->park_cv);
743 1.22 pooka }
744 1.22 pooka }
745 1.22 pooka
746 1.22 pooka /* signal waiters on RESPONSE FROM file server queue */
747 1.22 pooka TAILQ_FOREACH(park, &pmp->pmp_req_replywait, park_entries) {
748 1.23 pooka TAILQ_REMOVE(&pmp->pmp_req_replywait, park, park_entries);
749 1.22 pooka KASSERT(PUFFSOP_WANTREPLY(park->park_preq->preq_opclass));
750 1.22 pooka
751 1.24 pooka park->park_preq->preq_rv = ENXIO;
752 1.24 pooka if (park->park_flags & PARKFLAG_CALL) {
753 1.24 pooka park->park_done(park->park_preq, park->park_donearg);
754 1.24 pooka puffs_parkmem_free(park);
755 1.22 pooka } else {
756 1.22 pooka cv_signal(&park->park_cv);
757 1.22 pooka }
758 1.22 pooka }
759 1.22 pooka }
760 1.22 pooka
761 1.1 pooka /* this is probably going to die away at some point? */
762 1.9 pooka /*
763 1.9 pooka * XXX: currently bitrotted
764 1.9 pooka */
765 1.9 pooka #if 0
766 1.1 pooka static int
767 1.1 pooka puffssizeop(struct puffs_mount *pmp, struct puffs_sizeop *psop_user)
768 1.1 pooka {
769 1.1 pooka struct puffs_sizepark *pspark;
770 1.1 pooka void *kernbuf;
771 1.1 pooka size_t copylen;
772 1.1 pooka int error;
773 1.1 pooka
774 1.1 pooka /* locate correct op */
775 1.22 pooka mutex_enter(&pmp->pmp_lock);
776 1.1 pooka TAILQ_FOREACH(pspark, &pmp->pmp_req_sizepark, pkso_entries) {
777 1.1 pooka if (pspark->pkso_reqid == psop_user->pso_reqid) {
778 1.1 pooka TAILQ_REMOVE(&pmp->pmp_req_sizepark, pspark,
779 1.1 pooka pkso_entries);
780 1.1 pooka break;
781 1.1 pooka }
782 1.1 pooka }
783 1.22 pooka mutex_exit(&pmp->pmp_lock);
784 1.1 pooka
785 1.1 pooka if (pspark == NULL)
786 1.1 pooka return EINVAL;
787 1.1 pooka
788 1.1 pooka error = 0;
789 1.1 pooka copylen = MIN(pspark->pkso_bufsize, psop_user->pso_bufsize);
790 1.1 pooka
791 1.1 pooka /*
792 1.1 pooka * XXX: uvm stuff to avoid bouncy-bouncy copying?
793 1.1 pooka */
794 1.1 pooka if (PUFFS_SIZEOP_UIO(pspark->pkso_reqtype)) {
795 1.1 pooka kernbuf = malloc(copylen, M_PUFFS, M_WAITOK | M_ZERO);
796 1.1 pooka if (pspark->pkso_reqtype == PUFFS_SIZEOPREQ_UIO_IN) {
797 1.1 pooka error = copyin(psop_user->pso_userbuf,
798 1.1 pooka kernbuf, copylen);
799 1.1 pooka if (error) {
800 1.1 pooka printf("psop ERROR1 %d\n", error);
801 1.1 pooka goto escape;
802 1.1 pooka }
803 1.1 pooka }
804 1.1 pooka error = uiomove(kernbuf, copylen, pspark->pkso_uio);
805 1.1 pooka if (error) {
806 1.1 pooka printf("uiomove from kernel %p, len %d failed: %d\n",
807 1.1 pooka kernbuf, (int)copylen, error);
808 1.1 pooka goto escape;
809 1.1 pooka }
810 1.1 pooka
811 1.1 pooka if (pspark->pkso_reqtype == PUFFS_SIZEOPREQ_UIO_OUT) {
812 1.1 pooka error = copyout(kernbuf,
813 1.1 pooka psop_user->pso_userbuf, copylen);
814 1.1 pooka if (error) {
815 1.1 pooka printf("psop ERROR2 %d\n", error);
816 1.1 pooka goto escape;
817 1.1 pooka }
818 1.1 pooka }
819 1.1 pooka escape:
820 1.1 pooka free(kernbuf, M_PUFFS);
821 1.1 pooka } else if (PUFFS_SIZEOP_BUF(pspark->pkso_reqtype)) {
822 1.1 pooka copylen = MAX(pspark->pkso_bufsize, psop_user->pso_bufsize);
823 1.1 pooka if (pspark->pkso_reqtype == PUFFS_SIZEOPREQ_BUF_IN) {
824 1.1 pooka error = copyin(psop_user->pso_userbuf,
825 1.1 pooka pspark->pkso_copybuf, copylen);
826 1.1 pooka } else {
827 1.1 pooka error = copyout(pspark->pkso_copybuf,
828 1.1 pooka psop_user->pso_userbuf, copylen);
829 1.1 pooka }
830 1.1 pooka }
831 1.1 pooka #ifdef DIAGNOSTIC
832 1.1 pooka else
833 1.1 pooka panic("puffssizeop: invalid reqtype %d\n",
834 1.1 pooka pspark->pkso_reqtype);
835 1.1 pooka #endif /* DIAGNOSTIC */
836 1.1 pooka
837 1.1 pooka return error;
838 1.1 pooka }
839 1.9 pooka #endif
840