puffs_msgif.c revision 1.51.2.3 1 1.51.2.3 mjf /* $NetBSD: puffs_msgif.c,v 1.51.2.3 2008/02/18 21:06:39 mjf 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 *
19 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 pooka * SUCH DAMAGE.
30 1.1 pooka */
31 1.1 pooka
32 1.1 pooka #include <sys/cdefs.h>
33 1.51.2.3 mjf __KERNEL_RCSID(0, "$NetBSD: puffs_msgif.c,v 1.51.2.3 2008/02/18 21:06:39 mjf Exp $");
34 1.1 pooka
35 1.1 pooka #include <sys/param.h>
36 1.51.2.3 mjf #include <sys/atomic.h>
37 1.16 pooka #include <sys/fstrans.h>
38 1.46 pooka #include <sys/kmem.h>
39 1.51.2.1 mjf #include <sys/kthread.h>
40 1.51.2.1 mjf #include <sys/lock.h>
41 1.1 pooka #include <sys/malloc.h>
42 1.1 pooka #include <sys/mount.h>
43 1.51.2.1 mjf #include <sys/namei.h>
44 1.39 ad #include <sys/proc.h>
45 1.51.2.1 mjf #include <sys/vnode.h>
46 1.51.2.1 mjf
47 1.51.2.1 mjf #include <dev/putter/putter_sys.h>
48 1.1 pooka
49 1.1 pooka #include <fs/puffs/puffs_msgif.h>
50 1.1 pooka #include <fs/puffs/puffs_sys.h>
51 1.1 pooka
52 1.51.2.1 mjf #include <miscfs/syncfs/syncfs.h> /* XXX: for syncer_mutex reference */
53 1.51.2.1 mjf
54 1.22 pooka /*
55 1.22 pooka * waitq data structures
56 1.22 pooka */
57 1.22 pooka
58 1.22 pooka /*
59 1.22 pooka * While a request is going to userspace, park the caller within the
60 1.22 pooka * kernel. This is the kernel counterpart of "struct puffs_req".
61 1.22 pooka */
62 1.46 pooka struct puffs_msgpark {
63 1.22 pooka struct puffs_req *park_preq; /* req followed by buf */
64 1.22 pooka
65 1.22 pooka size_t park_copylen; /* userspace copylength */
66 1.22 pooka size_t park_maxlen; /* max size in comeback */
67 1.24 pooka
68 1.46 pooka parkdone_fn park_done; /* "biodone" a'la puffs */
69 1.24 pooka void *park_donearg;
70 1.22 pooka
71 1.22 pooka int park_flags;
72 1.26 pooka int park_refcount;
73 1.22 pooka
74 1.22 pooka kcondvar_t park_cv;
75 1.26 pooka kmutex_t park_mtx;
76 1.26 pooka
77 1.46 pooka TAILQ_ENTRY(puffs_msgpark) park_entries;
78 1.22 pooka };
79 1.22 pooka #define PARKFLAG_WAITERGONE 0x01
80 1.26 pooka #define PARKFLAG_DONE 0x02
81 1.26 pooka #define PARKFLAG_ONQUEUE1 0x04
82 1.26 pooka #define PARKFLAG_ONQUEUE2 0x08
83 1.26 pooka #define PARKFLAG_CALL 0x10
84 1.31 pooka #define PARKFLAG_WANTREPLY 0x20
85 1.51.2.1 mjf #define PARKFLAG_HASERROR 0x40
86 1.22 pooka
87 1.51.2.1 mjf static pool_cache_t parkpc;
88 1.51.2.1 mjf #ifdef PUFFSDEBUG
89 1.51.2.1 mjf static int totalpark;
90 1.51.2.1 mjf #endif
91 1.22 pooka
92 1.22 pooka static int
93 1.22 pooka makepark(void *arg, void *obj, int flags)
94 1.22 pooka {
95 1.46 pooka struct puffs_msgpark *park = obj;
96 1.22 pooka
97 1.26 pooka mutex_init(&park->park_mtx, MUTEX_DEFAULT, IPL_NONE);
98 1.22 pooka cv_init(&park->park_cv, "puffsrpl");
99 1.22 pooka
100 1.22 pooka return 0;
101 1.22 pooka }
102 1.22 pooka
103 1.22 pooka static void
104 1.22 pooka nukepark(void *arg, void *obj)
105 1.22 pooka {
106 1.46 pooka struct puffs_msgpark *park = obj;
107 1.22 pooka
108 1.22 pooka cv_destroy(&park->park_cv);
109 1.26 pooka mutex_destroy(&park->park_mtx);
110 1.22 pooka }
111 1.22 pooka
112 1.22 pooka void
113 1.22 pooka puffs_msgif_init()
114 1.22 pooka {
115 1.22 pooka
116 1.51.2.1 mjf parkpc = pool_cache_init(sizeof(struct puffs_msgpark), 0, 0, 0,
117 1.51.2.1 mjf "puffprkl", NULL, IPL_NONE, makepark, nukepark, NULL);
118 1.22 pooka }
119 1.22 pooka
120 1.22 pooka void
121 1.22 pooka puffs_msgif_destroy()
122 1.22 pooka {
123 1.22 pooka
124 1.51.2.1 mjf pool_cache_destroy(parkpc);
125 1.22 pooka }
126 1.22 pooka
127 1.46 pooka static int alloced;
128 1.46 pooka
129 1.46 pooka static struct puffs_msgpark *
130 1.46 pooka puffs_msgpark_alloc(int waitok)
131 1.26 pooka {
132 1.46 pooka struct puffs_msgpark *park;
133 1.26 pooka
134 1.51.2.1 mjf park = pool_cache_get(parkpc, waitok ? PR_WAITOK : PR_NOWAIT);
135 1.46 pooka if (park == NULL)
136 1.46 pooka return park;
137 1.46 pooka
138 1.46 pooka park->park_refcount = 1;
139 1.46 pooka park->park_preq = NULL;
140 1.46 pooka park->park_flags = PARKFLAG_WANTREPLY;
141 1.26 pooka
142 1.51.2.1 mjf #ifdef PUFFSDEBUG
143 1.51.2.1 mjf totalpark++;
144 1.51.2.1 mjf #endif
145 1.51.2.1 mjf
146 1.26 pooka return park;
147 1.26 pooka }
148 1.26 pooka
149 1.26 pooka static void
150 1.46 pooka puffs_msgpark_reference(struct puffs_msgpark *park)
151 1.22 pooka {
152 1.22 pooka
153 1.46 pooka KASSERT(mutex_owned(&park->park_mtx));
154 1.26 pooka park->park_refcount++;
155 1.22 pooka }
156 1.22 pooka
157 1.46 pooka /*
158 1.46 pooka * Release reference to park structure.
159 1.46 pooka */
160 1.46 pooka static void
161 1.46 pooka puffs_msgpark_release1(struct puffs_msgpark *park, int howmany)
162 1.22 pooka {
163 1.46 pooka struct puffs_req *preq = park->park_preq;
164 1.46 pooka int refcnt;
165 1.22 pooka
166 1.26 pooka KASSERT(mutex_owned(&park->park_mtx));
167 1.46 pooka refcnt = park->park_refcount -= howmany;
168 1.46 pooka mutex_exit(&park->park_mtx);
169 1.46 pooka
170 1.46 pooka KASSERT(refcnt >= 0);
171 1.26 pooka
172 1.46 pooka if (refcnt == 0) {
173 1.46 pooka alloced--;
174 1.46 pooka if (preq)
175 1.46 pooka kmem_free(preq, park->park_maxlen);
176 1.51.2.1 mjf pool_cache_put(parkpc, park);
177 1.51.2.1 mjf
178 1.51.2.1 mjf #ifdef PUFFSDEBUG
179 1.51.2.1 mjf totalpark--;
180 1.51.2.1 mjf #endif
181 1.46 pooka }
182 1.22 pooka }
183 1.46 pooka #define puffs_msgpark_release(a) puffs_msgpark_release1(a, 1)
184 1.22 pooka
185 1.26 pooka #ifdef PUFFSDEBUG
186 1.26 pooka static void
187 1.46 pooka parkdump(struct puffs_msgpark *park)
188 1.26 pooka {
189 1.26 pooka
190 1.26 pooka DPRINTF(("park %p, preq %p, id %" PRIu64 "\n"
191 1.26 pooka "\tcopy %zu, max %zu - done: %p/%p\n"
192 1.26 pooka "\tflags 0x%08x, refcount %d, cv/mtx: %p/%p\n",
193 1.46 pooka park, park->park_preq, park->park_preq->preq_id,
194 1.26 pooka park->park_copylen, park->park_maxlen,
195 1.26 pooka park->park_done, park->park_donearg,
196 1.26 pooka park->park_flags, park->park_refcount,
197 1.26 pooka &park->park_cv, &park->park_mtx));
198 1.26 pooka }
199 1.26 pooka
200 1.26 pooka static void
201 1.26 pooka parkqdump(struct puffs_wq *q, int dumpall)
202 1.26 pooka {
203 1.46 pooka struct puffs_msgpark *park;
204 1.26 pooka int total = 0;
205 1.26 pooka
206 1.26 pooka TAILQ_FOREACH(park, q, park_entries) {
207 1.26 pooka if (dumpall)
208 1.26 pooka parkdump(park);
209 1.26 pooka total++;
210 1.26 pooka }
211 1.29 pooka DPRINTF(("puffs waitqueue at %p dumped, %d total\n", q, total));
212 1.26 pooka
213 1.26 pooka }
214 1.26 pooka #endif /* PUFFSDEBUG */
215 1.22 pooka
216 1.22 pooka /*
217 1.46 pooka * A word about locking in the park structures: the lock protects the
218 1.46 pooka * fields of the *park* structure (not preq) and acts as an interlock
219 1.46 pooka * in cv operations. The lock is always internal to this module and
220 1.46 pooka * callers do not need to worry about it.
221 1.22 pooka */
222 1.46 pooka
223 1.46 pooka int
224 1.46 pooka puffs_msgmem_alloc(size_t len, struct puffs_msgpark **ppark, void **mem,
225 1.46 pooka int cansleep)
226 1.46 pooka {
227 1.46 pooka struct puffs_msgpark *park;
228 1.46 pooka void *m;
229 1.46 pooka
230 1.46 pooka m = kmem_zalloc(len, cansleep ? KM_SLEEP : KM_NOSLEEP);
231 1.46 pooka if (m == NULL) {
232 1.46 pooka KASSERT(cansleep == 0);
233 1.46 pooka return ENOMEM;
234 1.46 pooka }
235 1.46 pooka
236 1.46 pooka park = puffs_msgpark_alloc(cansleep);
237 1.46 pooka if (park == NULL) {
238 1.46 pooka KASSERT(cansleep == 0);
239 1.46 pooka kmem_free(m, len);
240 1.46 pooka return ENOMEM;
241 1.46 pooka }
242 1.46 pooka
243 1.46 pooka park->park_preq = m;
244 1.51.2.1 mjf park->park_maxlen = park->park_copylen = len;
245 1.46 pooka
246 1.46 pooka *ppark = park;
247 1.46 pooka *mem = m;
248 1.46 pooka
249 1.46 pooka return 0;
250 1.46 pooka }
251 1.46 pooka
252 1.46 pooka void
253 1.46 pooka puffs_msgmem_release(struct puffs_msgpark *park)
254 1.22 pooka {
255 1.22 pooka
256 1.46 pooka if (park == NULL)
257 1.46 pooka return;
258 1.22 pooka
259 1.46 pooka mutex_enter(&park->park_mtx);
260 1.46 pooka puffs_msgpark_release(park);
261 1.46 pooka }
262 1.22 pooka
263 1.46 pooka void
264 1.46 pooka puffs_msg_setfaf(struct puffs_msgpark *park)
265 1.46 pooka {
266 1.22 pooka
267 1.51.2.1 mjf KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
268 1.36 pooka park->park_flags &= ~PARKFLAG_WANTREPLY;
269 1.22 pooka }
270 1.22 pooka
271 1.51.2.1 mjf void
272 1.51.2.1 mjf puffs_msg_setdelta(struct puffs_msgpark *park, size_t delta)
273 1.1 pooka {
274 1.1 pooka
275 1.51.2.1 mjf KASSERT(delta < park->park_maxlen); /* "<=" wouldn't make sense */
276 1.51.2.1 mjf park->park_copylen = park->park_maxlen - delta;
277 1.1 pooka }
278 1.1 pooka
279 1.51.2.1 mjf void
280 1.51.2.3 mjf puffs_msg_setinfo(struct puffs_msgpark *park, int class, int type,
281 1.51.2.3 mjf puffs_cookie_t ck)
282 1.1 pooka {
283 1.1 pooka
284 1.51.2.1 mjf park->park_preq->preq_opclass = PUFFSOP_OPCLASS(class);
285 1.51.2.1 mjf park->park_preq->preq_optype = type;
286 1.51.2.3 mjf park->park_preq->preq_cookie = ck;
287 1.1 pooka }
288 1.1 pooka
289 1.24 pooka void
290 1.51.2.1 mjf puffs_msg_setcall(struct puffs_msgpark *park, parkdone_fn donefn, void *donearg)
291 1.1 pooka {
292 1.1 pooka
293 1.51.2.1 mjf KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
294 1.25 pooka park->park_done = donefn;
295 1.25 pooka park->park_donearg = donearg;
296 1.46 pooka park->park_flags |= PARKFLAG_CALL;
297 1.20 pooka }
298 1.20 pooka
299 1.51.2.1 mjf /*
300 1.51.2.1 mjf * kernel-user-kernel waitqueues
301 1.51.2.1 mjf */
302 1.4 pooka
303 1.51.2.1 mjf static uint64_t
304 1.51.2.1 mjf puffs_getmsgid(struct puffs_mount *pmp)
305 1.41 pooka {
306 1.51.2.1 mjf uint64_t rv;
307 1.41 pooka
308 1.51.2.1 mjf mutex_enter(&pmp->pmp_lock);
309 1.51.2.1 mjf rv = pmp->pmp_nextmsgid++;
310 1.51.2.1 mjf mutex_exit(&pmp->pmp_lock);
311 1.41 pooka
312 1.51.2.1 mjf return rv;
313 1.41 pooka }
314 1.41 pooka
315 1.4 pooka /*
316 1.51.2.1 mjf * A word about reference counting of parks. A reference must be taken
317 1.51.2.1 mjf * when accessing a park and additionally when it is on a queue. So
318 1.51.2.1 mjf * when taking it off a queue and releasing the access reference, the
319 1.51.2.1 mjf * reference count is generally decremented by 2.
320 1.1 pooka */
321 1.51.2.1 mjf
322 1.51.2.1 mjf void
323 1.51.2.1 mjf puffs_msg_enqueue(struct puffs_mount *pmp, struct puffs_msgpark *park)
324 1.1 pooka {
325 1.26 pooka struct lwp *l = curlwp;
326 1.16 pooka struct mount *mp;
327 1.9 pooka struct puffs_req *preq;
328 1.1 pooka
329 1.16 pooka mp = PMPTOMP(pmp);
330 1.25 pooka preq = park->park_preq;
331 1.46 pooka preq->preq_buflen = park->park_maxlen;
332 1.51.2.2 mjf KASSERT(preq->preq_id == 0
333 1.51.2.2 mjf || (preq->preq_opclass & PUFFSOPFLAG_ISRESPONSE));
334 1.46 pooka
335 1.46 pooka if ((park->park_flags & PARKFLAG_WANTREPLY) == 0)
336 1.46 pooka preq->preq_opclass |= PUFFSOPFLAG_FAF;
337 1.46 pooka else
338 1.46 pooka preq->preq_id = puffs_getmsgid(pmp);
339 1.31 pooka
340 1.49 pooka /* fill in caller information */
341 1.49 pooka preq->preq_pid = l->l_proc->p_pid;
342 1.49 pooka preq->preq_lid = l->l_lid;
343 1.49 pooka
344 1.19 pooka /*
345 1.51 pooka * To support cv_sig, yet another movie: check if there are signals
346 1.19 pooka * pending and we are issueing a non-FAF. If so, return an error
347 1.51.2.1 mjf * directly UNLESS we are issueing INACTIVE/RECLAIM. In that case,
348 1.51.2.1 mjf * convert it to a FAF, fire off to the file server and return
349 1.51.2.1 mjf * an error. Yes, this is bordering disgusting. Barfbags are on me.
350 1.19 pooka */
351 1.51.2.1 mjf if (__predict_false((park->park_flags & PARKFLAG_WANTREPLY)
352 1.25 pooka && (park->park_flags & PARKFLAG_CALL) == 0
353 1.51.2.1 mjf && (l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0))) {
354 1.51.2.1 mjf park->park_flags |= PARKFLAG_HASERROR;
355 1.51.2.1 mjf preq->preq_rv = EINTR;
356 1.19 pooka if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN
357 1.51.2.1 mjf && (preq->preq_optype == PUFFS_VN_INACTIVE
358 1.51.2.1 mjf || preq->preq_optype == PUFFS_VN_RECLAIM)) {
359 1.46 pooka park->park_preq->preq_opclass |= PUFFSOPFLAG_FAF;
360 1.46 pooka park->park_flags &= ~PARKFLAG_WANTREPLY;
361 1.51.2.1 mjf DPRINTF(("puffs_msg_enqueue: converted to FAF %p\n",
362 1.51.2.1 mjf park));
363 1.19 pooka } else {
364 1.51.2.1 mjf return;
365 1.19 pooka }
366 1.19 pooka }
367 1.16 pooka
368 1.16 pooka /*
369 1.16 pooka * test for suspension lock.
370 1.16 pooka *
371 1.16 pooka * Note that we *DO NOT* keep the lock, since that might block
372 1.16 pooka * lock acquiring PLUS it would give userlandia control over
373 1.16 pooka * the lock. The operation queue enforces a strict ordering:
374 1.16 pooka * when the fs server gets in the op stream, it knows things
375 1.16 pooka * are in order. The kernel locks can't guarantee that for
376 1.16 pooka * userspace, in any case.
377 1.16 pooka *
378 1.16 pooka * BUT: this presents a problem for ops which have a consistency
379 1.16 pooka * clause based on more than one operation. Unfortunately such
380 1.16 pooka * operations (read, write) do not reliably work yet.
381 1.16 pooka *
382 1.16 pooka * Ya, Ya, it's wrong wong wrong, me be fixink this someday.
383 1.18 pooka *
384 1.18 pooka * XXX: and there is one more problem. We sometimes need to
385 1.18 pooka * take a lazy lock in case the fs is suspending and we are
386 1.18 pooka * executing as the fs server context. This might happen
387 1.18 pooka * e.g. in the case that the user server triggers a reclaim
388 1.18 pooka * in the kernel while the fs is suspending. It's not a very
389 1.18 pooka * likely event, but it needs to be fixed some day.
390 1.16 pooka */
391 1.22 pooka
392 1.22 pooka /*
393 1.22 pooka * MOREXXX: once PUFFS_WCACHEINFO is enabled, we can't take
394 1.22 pooka * the mutex here, since getpages() might be called locked.
395 1.22 pooka */
396 1.18 pooka fstrans_start(mp, FSTRANS_NORMAL);
397 1.22 pooka mutex_enter(&pmp->pmp_lock);
398 1.16 pooka fstrans_done(mp);
399 1.16 pooka
400 1.13 pooka if (pmp->pmp_status != PUFFSTAT_RUNNING) {
401 1.22 pooka mutex_exit(&pmp->pmp_lock);
402 1.51.2.1 mjf park->park_flags |= PARKFLAG_HASERROR;
403 1.51.2.1 mjf preq->preq_rv = ENXIO;
404 1.51.2.1 mjf return;
405 1.1 pooka }
406 1.1 pooka
407 1.26 pooka #ifdef PUFFSDEBUG
408 1.46 pooka parkqdump(&pmp->pmp_msg_touser, puffsdebug > 1);
409 1.46 pooka parkqdump(&pmp->pmp_msg_replywait, puffsdebug > 1);
410 1.26 pooka #endif
411 1.26 pooka
412 1.51.2.1 mjf /*
413 1.51.2.1 mjf * Note: we don't need to lock park since we have the only
414 1.51.2.1 mjf * reference to it at this point.
415 1.51.2.1 mjf */
416 1.46 pooka TAILQ_INSERT_TAIL(&pmp->pmp_msg_touser, park, park_entries);
417 1.26 pooka park->park_flags |= PARKFLAG_ONQUEUE1;
418 1.46 pooka pmp->pmp_msg_touser_count++;
419 1.51.2.1 mjf park->park_refcount++;
420 1.26 pooka mutex_exit(&pmp->pmp_lock);
421 1.1 pooka
422 1.51.2.1 mjf cv_broadcast(&pmp->pmp_msg_waiter_cv);
423 1.51.2.1 mjf putter_notify(pmp->pmp_pi);
424 1.51.2.1 mjf
425 1.20 pooka DPRINTF(("touser: req %" PRIu64 ", preq: %p, park: %p, "
426 1.25 pooka "c/t: 0x%x/0x%x, f: 0x%x\n", preq->preq_id, preq, park,
427 1.25 pooka preq->preq_opclass, preq->preq_optype, park->park_flags));
428 1.51.2.1 mjf }
429 1.15 pooka
430 1.51.2.1 mjf int
431 1.51.2.1 mjf puffs_msg_wait(struct puffs_mount *pmp, struct puffs_msgpark *park)
432 1.51.2.1 mjf {
433 1.51.2.1 mjf struct puffs_req *preq = park->park_preq; /* XXX: hmmm */
434 1.51.2.1 mjf struct mount *mp = PMPTOMP(pmp);
435 1.51.2.1 mjf int error = 0;
436 1.51.2.1 mjf int rv;
437 1.1 pooka
438 1.51.2.1 mjf mutex_enter(&pmp->pmp_lock);
439 1.51.2.1 mjf puffs_mp_reference(pmp);
440 1.51.2.1 mjf mutex_exit(&pmp->pmp_lock);
441 1.46 pooka
442 1.51.2.1 mjf mutex_enter(&park->park_mtx);
443 1.51.2.1 mjf if ((park->park_flags & PARKFLAG_WANTREPLY) == 0
444 1.51.2.1 mjf || (park->park_flags & PARKFLAG_CALL)) {
445 1.51.2.1 mjf mutex_exit(&park->park_mtx);
446 1.51.2.1 mjf rv = 0;
447 1.51.2.1 mjf goto skipwait;
448 1.51.2.1 mjf }
449 1.46 pooka
450 1.51.2.1 mjf /* did the response beat us to the wait? */
451 1.51.2.1 mjf if (__predict_false((park->park_flags & PARKFLAG_DONE)
452 1.51.2.1 mjf || (park->park_flags & PARKFLAG_HASERROR))) {
453 1.51.2.1 mjf rv = park->park_preq->preq_rv;
454 1.51.2.1 mjf mutex_exit(&park->park_mtx);
455 1.51.2.1 mjf goto skipwait;
456 1.51.2.1 mjf }
457 1.26 pooka
458 1.51.2.1 mjf error = cv_wait_sig(&park->park_cv, &park->park_mtx);
459 1.51.2.1 mjf DPRINTF(("puffs_touser: waiter for %p woke up with %d\n",
460 1.51.2.1 mjf park, error));
461 1.51.2.1 mjf if (error) {
462 1.51.2.1 mjf park->park_flags |= PARKFLAG_WAITERGONE;
463 1.51.2.1 mjf if (park->park_flags & PARKFLAG_DONE) {
464 1.22 pooka rv = preq->preq_rv;
465 1.51.2.1 mjf mutex_exit(&park->park_mtx);
466 1.51.2.1 mjf } else {
467 1.51.2.1 mjf /*
468 1.51.2.1 mjf * ok, we marked it as going away, but
469 1.51.2.1 mjf * still need to do queue ops. take locks
470 1.51.2.1 mjf * in correct order.
471 1.51.2.1 mjf *
472 1.51.2.1 mjf * We don't want to release our reference
473 1.51.2.1 mjf * if it's on replywait queue to avoid error
474 1.51.2.1 mjf * to file server. putop() code will DTRT.
475 1.51.2.1 mjf */
476 1.51.2.1 mjf mutex_exit(&park->park_mtx);
477 1.51.2.1 mjf mutex_enter(&pmp->pmp_lock);
478 1.51.2.1 mjf mutex_enter(&park->park_mtx);
479 1.51.2.1 mjf
480 1.51.2.1 mjf /*
481 1.51.2.1 mjf * Still on queue1? We can safely remove it
482 1.51.2.1 mjf * without any consequences since the file
483 1.51.2.1 mjf * server hasn't seen it. "else" we need to
484 1.51.2.1 mjf * wait for the response and just ignore it
485 1.51.2.1 mjf * to avoid signalling an incorrect error to
486 1.51.2.1 mjf * the file server.
487 1.51.2.1 mjf */
488 1.51.2.1 mjf if (park->park_flags & PARKFLAG_ONQUEUE1) {
489 1.51.2.1 mjf TAILQ_REMOVE(&pmp->pmp_msg_touser,
490 1.51.2.1 mjf park, park_entries);
491 1.51.2.1 mjf puffs_msgpark_release(park);
492 1.51.2.1 mjf pmp->pmp_msg_touser_count--;
493 1.51.2.1 mjf park->park_flags &= ~PARKFLAG_ONQUEUE1;
494 1.51.2.1 mjf } else {
495 1.51.2.1 mjf mutex_exit(&park->park_mtx);
496 1.51.2.1 mjf }
497 1.51.2.1 mjf mutex_exit(&pmp->pmp_lock);
498 1.22 pooka
499 1.51.2.2 mjf rv = EINTR;
500 1.16 pooka }
501 1.22 pooka } else {
502 1.51.2.1 mjf rv = preq->preq_rv;
503 1.51.2.1 mjf mutex_exit(&park->park_mtx);
504 1.16 pooka }
505 1.16 pooka
506 1.51.2.1 mjf /*
507 1.51.2.1 mjf * retake the lock and release. This makes sure (haha,
508 1.51.2.1 mjf * I'm humorous) that we don't process the same vnode in
509 1.51.2.1 mjf * multiple threads due to the locks hacks we have in
510 1.51.2.1 mjf * puffs_lock(). In reality this is well protected by
511 1.51.2.1 mjf * the biglock, but once that's gone, well, hopefully
512 1.51.2.1 mjf * this will be fixed for real. (and when you read this
513 1.51.2.1 mjf * comment in 2017 and subsequently barf, my condolences ;).
514 1.51.2.1 mjf */
515 1.51.2.1 mjf if (rv == 0 && !fstrans_is_owner(mp)) {
516 1.51.2.1 mjf fstrans_start(mp, FSTRANS_NORMAL);
517 1.51.2.1 mjf fstrans_done(mp);
518 1.51.2.1 mjf }
519 1.46 pooka
520 1.51.2.1 mjf skipwait:
521 1.22 pooka mutex_enter(&pmp->pmp_lock);
522 1.34 pooka puffs_mp_release(pmp);
523 1.22 pooka mutex_exit(&pmp->pmp_lock);
524 1.16 pooka
525 1.19 pooka return rv;
526 1.1 pooka }
527 1.1 pooka
528 1.9 pooka /*
529 1.51.2.1 mjf * XXX: this suuuucks. Hopefully I'll get rid of this lossage once
530 1.51.2.1 mjf * the whole setback-nonsense gets fixed.
531 1.51.2.1 mjf */
532 1.51.2.1 mjf int
533 1.51.2.1 mjf puffs_msg_wait2(struct puffs_mount *pmp, struct puffs_msgpark *park,
534 1.51.2.1 mjf struct puffs_node *pn1, struct puffs_node *pn2)
535 1.51.2.1 mjf {
536 1.51.2.1 mjf struct puffs_req *preq;
537 1.51.2.1 mjf int rv;
538 1.51.2.1 mjf
539 1.51.2.1 mjf rv = puffs_msg_wait(pmp, park);
540 1.51.2.1 mjf
541 1.51.2.1 mjf preq = park->park_preq;
542 1.51.2.1 mjf if (pn1 && preq->preq_setbacks & PUFFS_SETBACK_INACT_N1)
543 1.51.2.1 mjf pn1->pn_stat |= PNODE_DOINACT;
544 1.51.2.1 mjf if (pn2 && preq->preq_setbacks & PUFFS_SETBACK_INACT_N2)
545 1.51.2.1 mjf pn2->pn_stat |= PNODE_DOINACT;
546 1.51.2.1 mjf
547 1.51.2.1 mjf if (pn1 && preq->preq_setbacks & PUFFS_SETBACK_NOREF_N1)
548 1.51.2.1 mjf pn1->pn_stat |= PNODE_NOREFS;
549 1.51.2.1 mjf if (pn2 && preq->preq_setbacks & PUFFS_SETBACK_NOREF_N2)
550 1.51.2.1 mjf pn2->pn_stat |= PNODE_NOREFS;
551 1.51.2.1 mjf
552 1.51.2.1 mjf return rv;
553 1.51.2.1 mjf
554 1.51.2.1 mjf }
555 1.51.2.1 mjf
556 1.51.2.1 mjf /*
557 1.51.2.2 mjf * XXX: lazy bum. please, for the love of foie gras, fix me.
558 1.51.2.2 mjf * This should *NOT* depend on setfaf. Also "memcpy" could
559 1.51.2.2 mjf * be done more nicely.
560 1.51.2.2 mjf */
561 1.51.2.2 mjf void
562 1.51.2.2 mjf puffs_msg_sendresp(struct puffs_mount *pmp, struct puffs_req *origpreq, int rv)
563 1.51.2.2 mjf {
564 1.51.2.2 mjf struct puffs_msgpark *park;
565 1.51.2.2 mjf struct puffs_req *preq;
566 1.51.2.2 mjf
567 1.51.2.3 mjf puffs_msgmem_alloc(sizeof(struct puffs_req), &park, (void *)&preq, 1);
568 1.51.2.2 mjf puffs_msg_setfaf(park); /* XXXXXX: avoids reqid override */
569 1.51.2.2 mjf
570 1.51.2.2 mjf memcpy(preq, origpreq, sizeof(struct puffs_req));
571 1.51.2.2 mjf preq->preq_rv = rv;
572 1.51.2.2 mjf preq->preq_opclass |= PUFFSOPFLAG_ISRESPONSE;
573 1.51.2.2 mjf
574 1.51.2.2 mjf puffs_msg_enqueue(pmp, park);
575 1.51.2.2 mjf puffs_msgmem_release(park);
576 1.51.2.2 mjf }
577 1.51.2.2 mjf
578 1.51.2.2 mjf /*
579 1.46 pooka * Get next request in the outgoing queue. "maxsize" controls the
580 1.46 pooka * size the caller can accommodate and "nonblock" signals if this
581 1.46 pooka * should block while waiting for input. Handles all locking internally.
582 1.9 pooka */
583 1.10 pooka int
584 1.46 pooka puffs_msgif_getout(void *this, size_t maxsize, int nonblock,
585 1.46 pooka uint8_t **data, size_t *dlen, void **parkptr)
586 1.1 pooka {
587 1.46 pooka struct puffs_mount *pmp = this;
588 1.46 pooka struct puffs_msgpark *park;
589 1.9 pooka struct puffs_req *preq;
590 1.46 pooka int error;
591 1.1 pooka
592 1.46 pooka error = 0;
593 1.22 pooka mutex_enter(&pmp->pmp_lock);
594 1.50 pooka puffs_mp_reference(pmp);
595 1.46 pooka for (;;) {
596 1.46 pooka /* RIP? */
597 1.9 pooka if (pmp->pmp_status != PUFFSTAT_RUNNING) {
598 1.9 pooka error = ENXIO;
599 1.46 pooka break;
600 1.9 pooka }
601 1.12 pooka
602 1.46 pooka /* need platinum yendorian express card? */
603 1.46 pooka if (TAILQ_EMPTY(&pmp->pmp_msg_touser)) {
604 1.46 pooka DPRINTF(("puffs_getout: no outgoing op, "));
605 1.12 pooka if (nonblock) {
606 1.46 pooka DPRINTF(("returning EWOULDBLOCK\n"));
607 1.12 pooka error = EWOULDBLOCK;
608 1.46 pooka break;
609 1.9 pooka }
610 1.46 pooka DPRINTF(("waiting ...\n"));
611 1.9 pooka
612 1.46 pooka error = cv_wait_sig(&pmp->pmp_msg_waiter_cv,
613 1.22 pooka &pmp->pmp_lock);
614 1.11 pooka if (error)
615 1.46 pooka break;
616 1.11 pooka else
617 1.46 pooka continue;
618 1.9 pooka }
619 1.9 pooka
620 1.46 pooka park = TAILQ_FIRST(&pmp->pmp_msg_touser);
621 1.50 pooka if (park == NULL)
622 1.50 pooka continue;
623 1.50 pooka
624 1.46 pooka mutex_enter(&park->park_mtx);
625 1.46 pooka puffs_msgpark_reference(park);
626 1.46 pooka
627 1.46 pooka DPRINTF(("puffs_getout: found park at %p, ", park));
628 1.22 pooka
629 1.22 pooka /* If it's a goner, don't process any furher */
630 1.22 pooka if (park->park_flags & PARKFLAG_WAITERGONE) {
631 1.46 pooka DPRINTF(("waitergone!\n"));
632 1.46 pooka puffs_msgpark_release(park);
633 1.22 pooka continue;
634 1.22 pooka }
635 1.51.2.1 mjf preq = park->park_preq;
636 1.22 pooka
637 1.51.2.1 mjf #if 0
638 1.46 pooka /* check size */
639 1.51.2.1 mjf /*
640 1.51.2.1 mjf * XXX: this check is not valid for now, we don't know
641 1.51.2.1 mjf * the size of the caller's input buffer. i.e. this
642 1.51.2.1 mjf * will most likely go away
643 1.51.2.1 mjf */
644 1.46 pooka if (maxsize < preq->preq_frhdr.pfr_len) {
645 1.46 pooka DPRINTF(("buffer too small\n"));
646 1.46 pooka puffs_msgpark_release(park);
647 1.46 pooka error = E2BIG;
648 1.46 pooka break;
649 1.26 pooka }
650 1.51.2.1 mjf #endif
651 1.28 pooka
652 1.46 pooka DPRINTF(("returning\n"));
653 1.46 pooka
654 1.46 pooka /*
655 1.46 pooka * Ok, we found what we came for. Release it from the
656 1.46 pooka * outgoing queue but do not unlock. We will unlock
657 1.46 pooka * only after we "releaseout" it to avoid complications:
658 1.46 pooka * otherwise it is (theoretically) possible for userland
659 1.46 pooka * to race us into "put" before we have a change to put
660 1.46 pooka * this baby on the receiving queue.
661 1.46 pooka */
662 1.46 pooka TAILQ_REMOVE(&pmp->pmp_msg_touser, park, park_entries);
663 1.28 pooka KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
664 1.28 pooka park->park_flags &= ~PARKFLAG_ONQUEUE1;
665 1.46 pooka mutex_exit(&park->park_mtx);
666 1.46 pooka
667 1.46 pooka pmp->pmp_msg_touser_count--;
668 1.46 pooka KASSERT(pmp->pmp_msg_touser_count >= 0);
669 1.26 pooka
670 1.46 pooka break;
671 1.46 pooka }
672 1.50 pooka puffs_mp_release(pmp);
673 1.46 pooka mutex_exit(&pmp->pmp_lock);
674 1.9 pooka
675 1.46 pooka if (error == 0) {
676 1.46 pooka *data = (uint8_t *)preq;
677 1.51.2.1 mjf preq->preq_pth.pth_framelen = park->park_copylen;
678 1.51.2.1 mjf *dlen = preq->preq_pth.pth_framelen;
679 1.46 pooka *parkptr = park;
680 1.46 pooka }
681 1.51 pooka
682 1.46 pooka return error;
683 1.46 pooka }
684 1.9 pooka
685 1.46 pooka /*
686 1.46 pooka * Release outgoing structure. Now, depending on the success of the
687 1.46 pooka * outgoing send, it is either going onto the result waiting queue
688 1.46 pooka * or the death chamber.
689 1.46 pooka */
690 1.46 pooka void
691 1.46 pooka puffs_msgif_releaseout(void *this, void *parkptr, int status)
692 1.46 pooka {
693 1.46 pooka struct puffs_mount *pmp = this;
694 1.46 pooka struct puffs_msgpark *park = parkptr;
695 1.32 pooka
696 1.46 pooka DPRINTF(("puffs_releaseout: returning park %p, errno %d: " ,
697 1.46 pooka park, status));
698 1.46 pooka mutex_enter(&pmp->pmp_lock);
699 1.46 pooka mutex_enter(&park->park_mtx);
700 1.46 pooka if (park->park_flags & PARKFLAG_WANTREPLY) {
701 1.46 pooka if (status == 0) {
702 1.46 pooka DPRINTF(("enqueue replywait\n"));
703 1.46 pooka TAILQ_INSERT_TAIL(&pmp->pmp_msg_replywait, park,
704 1.22 pooka park_entries);
705 1.26 pooka park->park_flags |= PARKFLAG_ONQUEUE2;
706 1.9 pooka } else {
707 1.46 pooka DPRINTF(("error path!\n"));
708 1.46 pooka park->park_preq->preq_rv = status;
709 1.46 pooka park->park_flags |= PARKFLAG_DONE;
710 1.46 pooka cv_signal(&park->park_cv);
711 1.1 pooka }
712 1.46 pooka puffs_msgpark_release(park);
713 1.46 pooka } else {
714 1.46 pooka DPRINTF(("release\n"));
715 1.46 pooka puffs_msgpark_release1(park, 2);
716 1.1 pooka }
717 1.22 pooka mutex_exit(&pmp->pmp_lock);
718 1.1 pooka }
719 1.1 pooka
720 1.51.2.1 mjf size_t
721 1.51.2.1 mjf puffs_msgif_waitcount(void *this)
722 1.51.2.1 mjf {
723 1.51.2.1 mjf struct puffs_mount *pmp = this;
724 1.51.2.1 mjf size_t rv;
725 1.51.2.1 mjf
726 1.51.2.1 mjf mutex_enter(&pmp->pmp_lock);
727 1.51.2.1 mjf rv = pmp->pmp_msg_touser_count;
728 1.51.2.1 mjf mutex_exit(&pmp->pmp_lock);
729 1.51.2.1 mjf
730 1.51.2.1 mjf return rv;
731 1.51.2.1 mjf }
732 1.51.2.1 mjf
733 1.50 pooka /*
734 1.50 pooka * XXX: locking with this one?
735 1.50 pooka */
736 1.51.2.1 mjf static void
737 1.51.2.1 mjf puffsop_msg(void *this, struct puffs_req *preq)
738 1.1 pooka {
739 1.46 pooka struct puffs_mount *pmp = this;
740 1.51.2.1 mjf struct putter_hdr *pth = &preq->preq_pth;
741 1.46 pooka struct puffs_msgpark *park;
742 1.51.2.1 mjf int wgone;
743 1.1 pooka
744 1.22 pooka mutex_enter(&pmp->pmp_lock);
745 1.9 pooka
746 1.46 pooka /* Locate waiter */
747 1.46 pooka TAILQ_FOREACH(park, &pmp->pmp_msg_replywait, park_entries) {
748 1.46 pooka if (park->park_preq->preq_id == preq->preq_id)
749 1.1 pooka break;
750 1.46 pooka }
751 1.46 pooka if (park == NULL) {
752 1.51.2.1 mjf DPRINTF(("puffsop_msg: no request: %" PRIu64 "\n",
753 1.46 pooka preq->preq_id));
754 1.46 pooka mutex_exit(&pmp->pmp_lock);
755 1.46 pooka return; /* XXX send error */
756 1.46 pooka }
757 1.26 pooka
758 1.46 pooka mutex_enter(&park->park_mtx);
759 1.46 pooka puffs_msgpark_reference(park);
760 1.51.2.1 mjf if (pth->pth_framelen > park->park_maxlen) {
761 1.51.2.1 mjf DPRINTF(("puffsop_msg: invalid buffer length: "
762 1.51.2.1 mjf "%" PRIu64 " (req %" PRIu64 ", \n", pth->pth_framelen,
763 1.51.2.1 mjf preq->preq_id));
764 1.46 pooka park->park_preq->preq_rv = EPROTO;
765 1.46 pooka cv_signal(&park->park_cv);
766 1.51.2.1 mjf puffs_msgpark_release1(park, 2);
767 1.22 pooka mutex_exit(&pmp->pmp_lock);
768 1.46 pooka return; /* XXX: error */
769 1.46 pooka }
770 1.46 pooka wgone = park->park_flags & PARKFLAG_WAITERGONE;
771 1.9 pooka
772 1.46 pooka KASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
773 1.46 pooka TAILQ_REMOVE(&pmp->pmp_msg_replywait, park, park_entries);
774 1.46 pooka park->park_flags &= ~PARKFLAG_ONQUEUE2;
775 1.46 pooka mutex_exit(&pmp->pmp_lock);
776 1.24 pooka
777 1.46 pooka if (wgone) {
778 1.51.2.1 mjf DPRINTF(("puffsop_msg: bad service - waiter gone for "
779 1.46 pooka "park %p\n", park));
780 1.46 pooka } else {
781 1.24 pooka if (park->park_flags & PARKFLAG_CALL) {
782 1.51.2.1 mjf DPRINTF(("puffsop_msg: call for %p, arg %p\n",
783 1.40 pooka park->park_preq, park->park_donearg));
784 1.51.2.1 mjf park->park_done(pmp, preq, park->park_donearg);
785 1.46 pooka } else {
786 1.46 pooka /* XXX: yes, I know */
787 1.51.2.1 mjf memcpy(park->park_preq, preq, pth->pth_framelen);
788 1.20 pooka }
789 1.46 pooka }
790 1.1 pooka
791 1.46 pooka if (!wgone) {
792 1.46 pooka DPRINTF(("puffs_putop: flagging done for "
793 1.46 pooka "park %p\n", park));
794 1.46 pooka cv_signal(&park->park_cv);
795 1.1 pooka }
796 1.1 pooka
797 1.46 pooka park->park_flags |= PARKFLAG_DONE;
798 1.51.2.1 mjf puffs_msgpark_release1(park, 2);
799 1.51.2.1 mjf }
800 1.51.2.1 mjf
801 1.51.2.1 mjf /*
802 1.51.2.1 mjf * helpers
803 1.51.2.1 mjf */
804 1.51.2.1 mjf static void
805 1.51.2.1 mjf dosuspendresume(void *arg)
806 1.51.2.1 mjf {
807 1.51.2.1 mjf struct puffs_mount *pmp = arg;
808 1.51.2.1 mjf struct mount *mp;
809 1.51.2.1 mjf int rv;
810 1.51.2.1 mjf
811 1.51.2.1 mjf mp = PMPTOMP(pmp);
812 1.51.2.1 mjf /*
813 1.51.2.1 mjf * XXX? does this really do any good or is it just
814 1.51.2.1 mjf * paranoid stupidity? or stupid paranoia?
815 1.51.2.1 mjf */
816 1.51.2.1 mjf if (mp->mnt_iflag & IMNT_UNMOUNT) {
817 1.51.2.1 mjf printf("puffs dosuspendresume(): detected suspend on "
818 1.51.2.1 mjf "unmounting fs\n");
819 1.51.2.1 mjf goto out;
820 1.51.2.1 mjf }
821 1.51.2.1 mjf
822 1.51.2.1 mjf /* Do the dance. Allow only one concurrent suspend */
823 1.51.2.1 mjf rv = vfs_suspend(PMPTOMP(pmp), 1);
824 1.51.2.1 mjf if (rv == 0)
825 1.51.2.1 mjf vfs_resume(PMPTOMP(pmp));
826 1.51.2.1 mjf
827 1.51.2.1 mjf out:
828 1.51.2.1 mjf mutex_enter(&pmp->pmp_lock);
829 1.51.2.1 mjf KASSERT(pmp->pmp_suspend == 1);
830 1.51.2.1 mjf pmp->pmp_suspend = 0;
831 1.51.2.1 mjf puffs_mp_release(pmp);
832 1.51.2.1 mjf mutex_exit(&pmp->pmp_lock);
833 1.51.2.1 mjf
834 1.51.2.1 mjf kthread_exit(0);
835 1.51.2.1 mjf }
836 1.51.2.1 mjf
837 1.51.2.1 mjf static void
838 1.51.2.1 mjf puffsop_suspend(struct puffs_mount *pmp)
839 1.51.2.1 mjf {
840 1.51.2.1 mjf int rv = 0;
841 1.51.2.1 mjf
842 1.51.2.1 mjf mutex_enter(&pmp->pmp_lock);
843 1.51.2.1 mjf if (pmp->pmp_suspend || pmp->pmp_status != PUFFSTAT_RUNNING) {
844 1.51.2.1 mjf rv = EBUSY;
845 1.51.2.1 mjf } else {
846 1.51.2.1 mjf puffs_mp_reference(pmp);
847 1.51.2.1 mjf pmp->pmp_suspend = 1;
848 1.51.2.1 mjf }
849 1.51.2.1 mjf mutex_exit(&pmp->pmp_lock);
850 1.51.2.1 mjf if (rv)
851 1.51.2.1 mjf return;
852 1.51.2.1 mjf rv = kthread_create(PRI_NONE, 0, NULL, dosuspendresume,
853 1.51.2.1 mjf pmp, NULL, "puffsusp");
854 1.51.2.1 mjf
855 1.51.2.1 mjf /* XXX: "return" rv */
856 1.51.2.1 mjf }
857 1.51.2.1 mjf
858 1.51.2.2 mjf static void
859 1.51.2.1 mjf puffsop_flush(struct puffs_mount *pmp, struct puffs_flush *pf)
860 1.51.2.1 mjf {
861 1.51.2.1 mjf struct vnode *vp;
862 1.51.2.1 mjf voff_t offlo, offhi;
863 1.51.2.1 mjf int rv, flags = 0;
864 1.51.2.1 mjf
865 1.51.2.2 mjf if (pf->pf_req.preq_pth.pth_framelen != sizeof(struct puffs_flush)) {
866 1.51.2.2 mjf rv = EINVAL;
867 1.51.2.2 mjf goto out;
868 1.51.2.2 mjf }
869 1.51.2.2 mjf
870 1.51.2.1 mjf /* XXX: slurry */
871 1.51.2.1 mjf if (pf->pf_op == PUFFS_INVAL_NAMECACHE_ALL) {
872 1.51.2.1 mjf cache_purgevfs(PMPTOMP(pmp));
873 1.51.2.2 mjf rv = 0;
874 1.51.2.2 mjf goto out;
875 1.51.2.1 mjf }
876 1.51.2.1 mjf
877 1.51.2.1 mjf /*
878 1.51.2.1 mjf * Get vnode, don't lock it. Namecache is protected by its own lock
879 1.51.2.1 mjf * and we have a reference to protect against premature harvesting.
880 1.51.2.1 mjf *
881 1.51.2.1 mjf * The node we want here might be locked and the op is in
882 1.51.2.1 mjf * userspace waiting for us to complete ==> deadlock. Another
883 1.51.2.1 mjf * reason we need to eventually bump locking to userspace, as we
884 1.51.2.1 mjf * will need to lock the node if we wish to do flushes.
885 1.51.2.1 mjf */
886 1.51.2.1 mjf rv = puffs_cookie2vnode(pmp, pf->pf_cookie, 0, 0, &vp);
887 1.51.2.1 mjf if (rv) {
888 1.51.2.1 mjf if (rv == PUFFS_NOSUCHCOOKIE)
889 1.51.2.2 mjf rv = ENOENT;
890 1.51.2.2 mjf goto out;
891 1.51.2.1 mjf }
892 1.51.2.1 mjf
893 1.51.2.1 mjf switch (pf->pf_op) {
894 1.51.2.1 mjf #if 0
895 1.51.2.1 mjf /* not quite ready, yet */
896 1.51.2.1 mjf case PUFFS_INVAL_NAMECACHE_NODE:
897 1.51.2.1 mjf struct componentname *pf_cn;
898 1.51.2.1 mjf char *name;
899 1.51.2.1 mjf /* get comfortab^Wcomponentname */
900 1.51.2.2 mjf pf_cn = kmem_alloc(componentname);
901 1.51.2.1 mjf memset(pf_cn, 0, sizeof(struct componentname));
902 1.51.2.1 mjf break;
903 1.51.2.1 mjf
904 1.51.2.1 mjf #endif
905 1.51.2.1 mjf case PUFFS_INVAL_NAMECACHE_DIR:
906 1.51.2.1 mjf if (vp->v_type != VDIR) {
907 1.51.2.1 mjf rv = EINVAL;
908 1.51.2.1 mjf break;
909 1.51.2.1 mjf }
910 1.51.2.1 mjf cache_purge1(vp, NULL, PURGE_CHILDREN);
911 1.51.2.1 mjf break;
912 1.51.2.1 mjf
913 1.51.2.1 mjf case PUFFS_INVAL_PAGECACHE_NODE_RANGE:
914 1.51.2.1 mjf flags = PGO_FREE;
915 1.51.2.1 mjf /*FALLTHROUGH*/
916 1.51.2.1 mjf case PUFFS_FLUSH_PAGECACHE_NODE_RANGE:
917 1.51.2.1 mjf if (flags == 0)
918 1.51.2.1 mjf flags = PGO_CLEANIT;
919 1.51.2.1 mjf
920 1.51.2.1 mjf if (pf->pf_end > vp->v_size || vp->v_type != VREG) {
921 1.51.2.1 mjf rv = EINVAL;
922 1.51.2.1 mjf break;
923 1.51.2.1 mjf }
924 1.51.2.1 mjf
925 1.51.2.1 mjf offlo = trunc_page(pf->pf_start);
926 1.51.2.1 mjf offhi = round_page(pf->pf_end);
927 1.51.2.1 mjf if (offhi != 0 && offlo >= offhi) {
928 1.51.2.1 mjf rv = EINVAL;
929 1.51.2.1 mjf break;
930 1.51.2.1 mjf }
931 1.51.2.1 mjf
932 1.51.2.3 mjf mutex_enter(&vp->v_uobj.vmobjlock);
933 1.51.2.1 mjf rv = VOP_PUTPAGES(vp, offlo, offhi, flags);
934 1.51.2.1 mjf break;
935 1.51.2.1 mjf
936 1.51.2.1 mjf default:
937 1.51.2.1 mjf rv = EINVAL;
938 1.51.2.1 mjf }
939 1.51.2.1 mjf
940 1.51.2.1 mjf vrele(vp);
941 1.51.2.1 mjf
942 1.51.2.2 mjf out:
943 1.51.2.2 mjf puffs_msg_sendresp(pmp, &pf->pf_req, rv);
944 1.51.2.1 mjf }
945 1.51.2.1 mjf
946 1.51.2.1 mjf int
947 1.51.2.1 mjf puffs_msgif_dispatch(void *this, struct putter_hdr *pth)
948 1.51.2.1 mjf {
949 1.51.2.1 mjf struct puffs_mount *pmp = this;
950 1.51.2.1 mjf struct puffs_req *preq = (struct puffs_req *)pth;
951 1.51.2.1 mjf
952 1.51.2.1 mjf /* XXX: need to send error to userspace */
953 1.51.2.2 mjf if (pth->pth_framelen < sizeof(struct puffs_req)) {
954 1.51.2.2 mjf puffs_msg_sendresp(pmp, preq, EINVAL); /* E2SMALL */
955 1.51.2.2 mjf return 0;
956 1.51.2.2 mjf }
957 1.51.2.1 mjf
958 1.51.2.1 mjf switch (PUFFSOP_OPCLASS(preq->preq_opclass)) {
959 1.51.2.1 mjf case PUFFSOP_VN:
960 1.51.2.1 mjf case PUFFSOP_VFS:
961 1.51.2.2 mjf DPRINTF(("dispatch: vn/vfs message 0x%x\n", preq->preq_optype));
962 1.51.2.1 mjf puffsop_msg(pmp, preq);
963 1.51.2.1 mjf break;
964 1.51.2.1 mjf case PUFFSOP_FLUSH:
965 1.51.2.2 mjf DPRINTF(("dispatch: flush 0x%x\n", preq->preq_optype));
966 1.51.2.1 mjf puffsop_flush(pmp, (struct puffs_flush *)preq);
967 1.51.2.1 mjf break;
968 1.51.2.1 mjf case PUFFSOP_SUSPEND:
969 1.51.2.2 mjf DPRINTF(("dispatch: suspend\n"));
970 1.51.2.1 mjf puffsop_suspend(pmp);
971 1.51.2.1 mjf break;
972 1.51.2.1 mjf default:
973 1.51.2.2 mjf DPRINTF(("dispatch: invalid class 0x%x\n", preq->preq_opclass));
974 1.51.2.2 mjf puffs_msg_sendresp(pmp, preq, EINVAL);
975 1.51.2.1 mjf break;
976 1.51.2.1 mjf }
977 1.51.2.1 mjf
978 1.51.2.1 mjf return 0;
979 1.51.2.1 mjf }
980 1.51.2.1 mjf
981 1.51.2.1 mjf int
982 1.51.2.1 mjf puffs_msgif_close(void *this)
983 1.51.2.1 mjf {
984 1.51.2.1 mjf struct puffs_mount *pmp = this;
985 1.51.2.1 mjf struct mount *mp = PMPTOMP(pmp);
986 1.51.2.3 mjf int rv;
987 1.51.2.1 mjf
988 1.51.2.1 mjf mutex_enter(&pmp->pmp_lock);
989 1.51.2.1 mjf puffs_mp_reference(pmp);
990 1.51.2.1 mjf
991 1.51.2.1 mjf /*
992 1.51.2.1 mjf * Free the waiting callers before proceeding any further.
993 1.51.2.1 mjf * The syncer might be jogging around in this file system
994 1.51.2.1 mjf * currently. If we allow it to go to the userspace of no
995 1.51.2.1 mjf * return while trying to get the syncer lock, well ...
996 1.51.2.1 mjf */
997 1.51.2.1 mjf puffs_userdead(pmp);
998 1.51.2.1 mjf
999 1.51.2.1 mjf /*
1000 1.51.2.1 mjf * Make sure someone from puffs_unmount() isn't currently in
1001 1.51.2.1 mjf * userspace. If we don't take this precautionary step,
1002 1.51.2.1 mjf * they might notice that the mountpoint has disappeared
1003 1.51.2.1 mjf * from under them once they return. Especially note that we
1004 1.51.2.1 mjf * cannot simply test for an unmounter before calling
1005 1.51.2.1 mjf * dounmount(), since it might be possible that that particular
1006 1.51.2.1 mjf * invocation of unmount was called without MNT_FORCE. Here we
1007 1.51.2.1 mjf * *must* make sure unmount succeeds. Also, restart is necessary
1008 1.51.2.1 mjf * since pmp isn't locked. We might end up with PUTTER_DEAD after
1009 1.51.2.1 mjf * restart and exit from there.
1010 1.51.2.1 mjf */
1011 1.51.2.1 mjf if (pmp->pmp_unmounting) {
1012 1.51.2.1 mjf cv_wait(&pmp->pmp_unmounting_cv, &pmp->pmp_lock);
1013 1.51.2.1 mjf puffs_mp_release(pmp);
1014 1.51.2.1 mjf mutex_exit(&pmp->pmp_lock);
1015 1.51.2.1 mjf DPRINTF(("puffs_fop_close: unmount was in progress for pmp %p, "
1016 1.51.2.1 mjf "restart\n", pmp));
1017 1.51.2.1 mjf return ERESTART;
1018 1.51.2.1 mjf }
1019 1.51.2.1 mjf
1020 1.51.2.1 mjf /* Won't access pmp from here anymore */
1021 1.51.2.1 mjf puffs_mp_release(pmp);
1022 1.51.2.1 mjf mutex_exit(&pmp->pmp_lock);
1023 1.51.2.1 mjf
1024 1.51.2.1 mjf /*
1025 1.51.2.1 mjf * Detach from VFS. First do necessary XXX-dance (from
1026 1.51.2.1 mjf * sys_unmount() & other callers of dounmount()
1027 1.51.2.1 mjf *
1028 1.51.2.1 mjf * XXX Freeze syncer. Must do this before locking the
1029 1.51.2.1 mjf * mount point. See dounmount() for details.
1030 1.51.2.1 mjf *
1031 1.51.2.1 mjf * XXX2: take a reference to the mountpoint before starting to
1032 1.51.2.1 mjf * wait for syncer_mutex. Otherwise the mointpoint can be
1033 1.51.2.1 mjf * wiped out while we wait.
1034 1.51.2.1 mjf */
1035 1.51.2.3 mjf atomic_inc_uint((unsigned int*)&mp->mnt_refcnt);
1036 1.51.2.1 mjf mutex_enter(&syncer_mutex);
1037 1.51.2.3 mjf if (mp->mnt_iflag & IMNT_GONE) {
1038 1.51.2.1 mjf mutex_exit(&syncer_mutex);
1039 1.51.2.3 mjf vfs_destroy(mp);
1040 1.51.2.1 mjf return 0;
1041 1.51.2.1 mjf }
1042 1.51.2.1 mjf
1043 1.51.2.1 mjf /*
1044 1.51.2.1 mjf * microscopic race condition here (although not with the current
1045 1.51.2.1 mjf * kernel), but can't really fix it without starting a crusade
1046 1.51.2.1 mjf * against vfs_busy(), so let it be, let it be, let it be
1047 1.51.2.1 mjf */
1048 1.51.2.1 mjf
1049 1.51.2.1 mjf /*
1050 1.51.2.1 mjf * The only way vfs_busy() will fail for us is if the filesystem
1051 1.51.2.1 mjf * is already a goner.
1052 1.51.2.1 mjf * XXX: skating on the thin ice of modern calling conventions ...
1053 1.51.2.1 mjf */
1054 1.51.2.3 mjf if (vfs_busy(mp, RW_WRITER, NULL)) {
1055 1.51.2.1 mjf mutex_exit(&syncer_mutex);
1056 1.51.2.3 mjf vfs_destroy(mp);
1057 1.51.2.1 mjf return 0;
1058 1.51.2.1 mjf }
1059 1.51.2.1 mjf
1060 1.51.2.1 mjf /*
1061 1.51.2.1 mjf * Once we have the mount point, unmount() can't interfere..
1062 1.51.2.1 mjf * or at least in theory it shouldn't. dounmount() reentracy
1063 1.51.2.1 mjf * might require some visiting at some point.
1064 1.51.2.1 mjf */
1065 1.51.2.1 mjf rv = dounmount(mp, MNT_FORCE, curlwp);
1066 1.51.2.1 mjf KASSERT(rv == 0);
1067 1.51.2.3 mjf vfs_destroy(mp);
1068 1.51.2.1 mjf
1069 1.51.2.1 mjf return 0;
1070 1.1 pooka }
1071 1.1 pooka
1072 1.22 pooka /*
1073 1.22 pooka * We're dead, kaput, RIP, slightly more than merely pining for the
1074 1.22 pooka * fjords, belly-up, fallen, lifeless, finished, expired, gone to meet
1075 1.22 pooka * our maker, ceased to be, etcetc. YASD. It's a dead FS!
1076 1.22 pooka *
1077 1.22 pooka * Caller must hold puffs mutex.
1078 1.22 pooka */
1079 1.22 pooka void
1080 1.22 pooka puffs_userdead(struct puffs_mount *pmp)
1081 1.22 pooka {
1082 1.46 pooka struct puffs_msgpark *park, *park_next;
1083 1.22 pooka
1084 1.22 pooka /*
1085 1.22 pooka * Mark filesystem status as dying so that operations don't
1086 1.22 pooka * attempt to march to userspace any longer.
1087 1.22 pooka */
1088 1.22 pooka pmp->pmp_status = PUFFSTAT_DYING;
1089 1.22 pooka
1090 1.22 pooka /* signal waiters on REQUEST TO file server queue */
1091 1.46 pooka for (park = TAILQ_FIRST(&pmp->pmp_msg_touser); park; park = park_next) {
1092 1.24 pooka uint8_t opclass;
1093 1.24 pooka
1094 1.46 pooka mutex_enter(&park->park_mtx);
1095 1.46 pooka puffs_msgpark_reference(park);
1096 1.32 pooka park_next = TAILQ_NEXT(park, park_entries);
1097 1.26 pooka
1098 1.26 pooka KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
1099 1.46 pooka TAILQ_REMOVE(&pmp->pmp_msg_touser, park, park_entries);
1100 1.26 pooka park->park_flags &= ~PARKFLAG_ONQUEUE1;
1101 1.46 pooka pmp->pmp_msg_touser_count--;
1102 1.22 pooka
1103 1.31 pooka /*
1104 1.51 pooka * Even though waiters on QUEUE1 are removed in touser()
1105 1.51 pooka * in case of WAITERGONE, it is still possible for us to
1106 1.51 pooka * get raced here due to having to retake locks in said
1107 1.51 pooka * touser(). In the race case simply "ignore" the item
1108 1.51 pooka * on the queue and move on to the next one.
1109 1.31 pooka */
1110 1.31 pooka if (park->park_flags & PARKFLAG_WAITERGONE) {
1111 1.31 pooka KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
1112 1.31 pooka KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
1113 1.46 pooka puffs_msgpark_release(park);
1114 1.51 pooka
1115 1.22 pooka } else {
1116 1.31 pooka opclass = park->park_preq->preq_opclass;
1117 1.23 pooka park->park_preq->preq_rv = ENXIO;
1118 1.31 pooka
1119 1.31 pooka if (park->park_flags & PARKFLAG_CALL) {
1120 1.42 pooka park->park_done(pmp, park->park_preq,
1121 1.31 pooka park->park_donearg);
1122 1.46 pooka puffs_msgpark_release1(park, 2);
1123 1.31 pooka } else if ((park->park_flags & PARKFLAG_WANTREPLY)==0) {
1124 1.46 pooka puffs_msgpark_release1(park, 2);
1125 1.31 pooka } else {
1126 1.31 pooka park->park_preq->preq_rv = ENXIO;
1127 1.31 pooka cv_signal(&park->park_cv);
1128 1.46 pooka puffs_msgpark_release(park);
1129 1.31 pooka }
1130 1.22 pooka }
1131 1.22 pooka }
1132 1.22 pooka
1133 1.22 pooka /* signal waiters on RESPONSE FROM file server queue */
1134 1.46 pooka for (park=TAILQ_FIRST(&pmp->pmp_msg_replywait); park; park=park_next) {
1135 1.46 pooka mutex_enter(&park->park_mtx);
1136 1.46 pooka puffs_msgpark_reference(park);
1137 1.32 pooka park_next = TAILQ_NEXT(park, park_entries);
1138 1.26 pooka
1139 1.26 pooka KASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
1140 1.31 pooka KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
1141 1.26 pooka
1142 1.46 pooka TAILQ_REMOVE(&pmp->pmp_msg_replywait, park, park_entries);
1143 1.26 pooka park->park_flags &= ~PARKFLAG_ONQUEUE2;
1144 1.26 pooka
1145 1.31 pooka if (park->park_flags & PARKFLAG_WAITERGONE) {
1146 1.31 pooka KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
1147 1.46 pooka puffs_msgpark_release(park);
1148 1.22 pooka } else {
1149 1.31 pooka park->park_preq->preq_rv = ENXIO;
1150 1.31 pooka if (park->park_flags & PARKFLAG_CALL) {
1151 1.42 pooka park->park_done(pmp, park->park_preq,
1152 1.31 pooka park->park_donearg);
1153 1.46 pooka puffs_msgpark_release1(park, 2);
1154 1.31 pooka } else {
1155 1.31 pooka cv_signal(&park->park_cv);
1156 1.46 pooka puffs_msgpark_release(park);
1157 1.31 pooka }
1158 1.22 pooka }
1159 1.22 pooka }
1160 1.50 pooka
1161 1.50 pooka cv_broadcast(&pmp->pmp_msg_waiter_cv);
1162 1.22 pooka }
1163