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