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