tools/obexctl: Add disconnect command

tools/obexctl: Add disconnect command

Add support for disconnect command which can be used to disconnect
sessions.

--- tools/obexctl.c
+++ tools/obexctl.c
@@ -197,6 +197,69 @@
 	rl_printf("Attempting to connect to %s\n", argv[1]);
 }

+static void disconnect_reply(DBusMessage *message, void *user_data)
+{
+	DBusError error;
+
+	dbus_error_init(&error);
+
+	if (dbus_set_error_from_message(&error, message) == TRUE) {
+		rl_printf("Failed to disconnect: %s\n", error.name);
+		dbus_error_free(&error);
+		return;
+	}
+
+	rl_printf("Disconnection successful\n");
+}
+
+static void disconnect_setup(DBusMessageIter *iter, void *user_data)
+{
+	GDBusProxy *proxy = user_data;
+	const char *path;
+
+	path = g_dbus_proxy_get_path(proxy);
+
+	dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
+}
+
+static GDBusProxy *find_session(const char *path)
+{
+	GSList *l;
+
+	for (l = sessions; l; l = g_slist_next(l)) {
+		GDBusProxy *proxy = l->data;
+
+		if (strcmp(path, g_dbus_proxy_get_path(proxy)) == 0)
+			return proxy;
+	}
+
+	return NULL;
+}
+
+static void cmd_disconnect(int argc, char *argv[])
+{
+	GDBusProxy *proxy;
+
+	if (argc > 1)
+		proxy = find_session(argv[1]);
+	else
+		proxy = default_session;
+
+	if (proxy == NULL) {
+		rl_printf("Session not available\n");
+		return;
+	}
+
+	if (g_dbus_proxy_method_call(client, "RemoveSession", disconnect_setup,
+				disconnect_reply, proxy, NULL) == FALSE) {
+		rl_printf("Failed to disconnect\n");
+		return;
+	}
+
+	rl_printf("Attempting to disconnect to %s\n",
+						g_dbus_proxy_get_path(proxy));
+}
+
 static const struct {
 	const char *cmd;
 	const char *arg;
@@ -204,6 +267,7 @@
 	const char *desc;
 } cmd_table[] = {
 	{ "connect",      "<dev> [uuid]", cmd_connect, "Connect session" },
+	{ "disconnect",   "[session]", cmd_disconnect, "Disconnect session" },
 	{ "quit",         NULL,       cmd_quit, "Quit program" },
 	{ "exit",         NULL,       cmd_quit },
 	{ "help" },


Request body: adjust b->pos when chunked parsing done.

This is a nop for the current code, though will allow to correctly parse
pipelined requests.

--- src/http/ngx_http_parse.c
+++ src/http/ngx_http_parse.c
@@ -2065,6 +2065,9 @@

 done:

+    ctx->state = 0;
+    b->pos = pos + 1;
+
     return NGX_DONE;

 invalid:


#91 code refactoring

#91 code refactoring

--- tcejdb/ejdb.c
+++ tcejdb/ejdb.c
@@ -67,7 +67,7 @@

 /* opaque data for `_bsonipathrowldr()` and `_bsonfpathrowldr()` functions */
 typedef struct {
-    EJCOLL *jcoll; //current collection
+    EJCOLL *coll; //current collection
     bool icase; //ignore case normalization
 } _BSONIPATHROWLDR;

@@ -81,6 +81,21 @@
     TCMAP *rmap;
     TCMAP *imap;
 } _DEFFEREDIDXCTX;
+
+/* query execution context. See `_qryexecute()`*/
+typedef struct {
+    bool imode;     //if true ifields are included otherwise excluded
+    int qflags;     //query flags (JBQRYCOUNT|JBQRYFINDONE) passed into `ejdbqryexecute()` method
+    EJCOLL *coll;   //collection
+    EJQ *q;         //query object
+    EJQF *mqf;      //main indexed query condition if exists
+    TCMAP *ifields; //include/exclude $fields
+    TCMAP *dfields; //$do fields
+    TCLIST *res;    //result set
+    TCXSTR *log;    //query debug log buffer
+    TCLIST *didxctx; //deffered indexes context
+} _QRYCTX;
+

 /* private function prototypes */
 static void _ejdbsetecode(EJDB *jb, int ecode, const char *filename, int line, const char *func);
@@ -101,18 +116,18 @@
 static bool _addcoldb0(const char *colname, EJDB *jb, EJCOLLOPTS *opts, EJCOLL **res);
 static void _delcoldb(EJCOLL *cdb);
 static void _delqfdata(const EJQ *q, const EJQF *ejqf);
-static bool _ejdbsavebsonimpl(EJCOLL *jcoll, bson *bs, bson_oid_t *oid, bool merge);
-static bool _updatebsonidx(EJCOLL *jcoll, const bson_oid_t *oid, const bson *bs,
+static bool _ejdbsavebsonimpl(EJCOLL *coll, bson *bs, bson_oid_t *oid, bool merge);
+static bool _updatebsonidx(EJCOLL *coll, const bson_oid_t *oid, const bson *bs,
         const void *obsdata, int obsdatasz, TCLIST *dlist);
 static bool _metasetopts(EJDB *jb, const char *colname, EJCOLLOPTS *opts);
 static bool _metagetopts(EJDB *jb, const char *colname, EJCOLLOPTS *opts);
 static bson* _metagetbson(EJDB *jb, const char *colname, int colnamesz, const char *mkey);
-static bson* _metagetbson2(EJCOLL *jcoll, const char *mkey) __attribute__((unused));
+static bson* _metagetbson2(EJCOLL *coll, const char *mkey) __attribute__((unused));
 static bool _metasetbson(EJDB *jb, const char *colname, int colnamesz,
         const char *mkey, bson *val, bool merge, bool mergeoverwrt);
-static bool _metasetbson2(EJCOLL *jcoll, const char *mkey, bson *val, bool merge, bool mergeoverwrt);
-static bson* _imetaidx(EJCOLL *jcoll, const char *ipath);
-static bool _qrypreprocess(EJCOLL *jcoll, EJQ *ejq, int qflags, EJQF **mqf, TCMAP **dfields, TCMAP **ifields, bool *imode);
+static bool _metasetbson2(EJCOLL *coll, const char *mkey, bson *val, bool merge, bool mergeoverwrt);
+static bson* _imetaidx(EJCOLL *coll, const char *ipath);
+static bool _qrypreprocess(_QRYCTX *ctx);
 static TCLIST* _parseqobj(EJDB *jb, EJQ *q, bson *qspec);
 static TCLIST* _parseqobj2(EJDB *jb, EJQ *q, const void *qspecbsdata);
 static int _parse_qobj_impl(EJDB *jb, EJQ *q, bson_iterator *it, TCLIST *qlist, TCLIST *pathStack, EJQF *pqf, int mgrp);
@@ -121,17 +136,18 @@
 static bool _qrycondcheckstror(const char *vbuf, const TCLIST *tokens);
 static bool _qrybsvalmatch(const EJQF *qf, bson_iterator *it, bool expandarrays, int *arridx);
 static bool _qrybsmatch(EJQF *qf, const void *bsbuf, int bsbufsz);
-static bool _qry_$and_$or_match(EJCOLL *jcoll, EJQ *ejq, const void *pkbuf, int pkbufsz);
-static bool _qryormatch2(EJCOLL *jcoll, EJQ *ejq, const void *bsbuf, int bsbufsz);
-static bool _qryormatch3(EJCOLL *jcoll, EJQ *ejq, EJQ *oq, const void *bsbuf, int bsbufsz);
-static bool _qryandmatch2(EJCOLL *jcoll, EJQ *ejq, const void *bsbuf, int bsbufsz);
-static bool _qryallcondsmatch(EJQ *ejq, int anum, EJCOLL *jcoll, EJQF **qfs, int qfsz, const void *pkbuf, int pkbufsz);
+static bool _qry_$and_$or_match(EJCOLL *coll, EJQ *ejq, const void *pkbuf, int pkbufsz);
+static bool _qryormatch2(EJCOLL *coll, EJQ *ejq, const void *bsbuf, int bsbufsz);
+static bool _qryormatch3(EJCOLL *coll, EJQ *ejq, EJQ *oq, const void *bsbuf, int bsbufsz);
+static bool _qryandmatch2(EJCOLL *coll, EJQ *ejq, const void *bsbuf, int bsbufsz);
+static bool _qryallcondsmatch(EJQ *ejq, int anum, EJCOLL *coll, EJQF **qfs, int qfsz, const void *pkbuf, int pkbufsz);
 static EJQ* _qryaddand(EJDB *jb, EJQ *q, const void *andbsdata);
 static bool _qrydup(const EJQ *src, EJQ *target, uint32_t qflags);
 static void _qrydel(EJQ *q, bool freequery);
-static bool _pushprocessedbson(EJDB *jb, EJQ *q, TCLIST *rs, TCMAP *dfields, TCMAP *ifields, bool imode, const void *bsbuf, int bsbufsz);
-static bool _exec$do(EJDB *jb, EJQ *q, TCMAP *dfields, const void *bsbuf, bson *bsout);
-static TCLIST* _qryexecute(EJCOLL *jcoll, const EJQ *q, uint32_t *count, int qflags, TCXSTR *log);
+static bool _pushprocessedbson(_QRYCTX *ctx, const void *bsbuf, int bsbufsz);
+static bool _exec$do(_QRYCTX *ctx, const void *bsbuf, bson *bsout);
+static void _qryctxclear(_QRYCTX *ctx);
+static TCLIST* _qryexecute(EJCOLL *coll, const EJQ *q, uint32_t *count, int qflags, TCXSTR *log);
 EJDB_INLINE void _nufetch(_EJDBNUM *nu, const char *sval, bson_type bt);
 EJDB_INLINE int _nucmp(_EJDBNUM *nu, const char *sval, bson_type bt);
 EJDB_INLINE int _nucmp2(_EJDBNUM *nu1, _EJDBNUM *nu2, bson_type bt);
@@ -140,7 +156,7 @@
 static bool _importcoll(EJDB *jb, const char *bspath, TCLIST *cnames, int flags, TCXSTR *log);
 static EJCOLL* _createcollimpl(EJDB *jb, const char *colname, EJCOLLOPTS *opts);
 static bool _rmcollimpl(EJDB *jb, EJCOLL *coll, bool unlinkfile);
-static bool _setindeximpl(EJCOLL *jcoll, const char *fpath, int flags, bool nolock);
+static bool _setindeximpl(EJCOLL *coll, const char *fpath, int flags, bool nolock);

 extern const char *utf8proc_errmsg(ssize_t errcode);
@@ -337,69 +353,69 @@
 }

 /* Save/Update BSON  */
-bool ejdbsavebson(EJCOLL *jcoll, bson *bs, bson_oid_t *oid) {
-    return ejdbsavebson2(jcoll, bs, oid, false);
-}
-
-bool ejdbsavebson2(EJCOLL *jcoll, bson *bs, bson_oid_t *oid, bool merge) {
-    assert(jcoll);
+bool ejdbsavebson(EJCOLL *coll, bson *bs, bson_oid_t *oid) {
+    return ejdbsavebson2(coll, bs, oid, false);
+}
+
+bool ejdbsavebson2(EJCOLL *coll, bson *bs, bson_oid_t *oid, bool merge) {
+    assert(coll);
     if (!bs || bs->err || !bs->finished) {
-        _ejdbsetecode(jcoll->jb, JBEINVALIDBSON, __FILE__, __LINE__, __func__);
+        _ejdbsetecode(coll->jb, JBEINVALIDBSON, __FILE__, __LINE__, __func__);
         return false;
     }
-    if (!JBISOPEN(jcoll->jb)) {
-        _ejdbsetecode(jcoll->jb, TCEINVALID, __FILE__, __LINE__, __func__);
+    if (!JBISOPEN(coll->jb)) {
+        _ejdbsetecode(coll->jb, TCEINVALID, __FILE__, __LINE__, __func__);
         return false;
     }
-    if (!JBCLOCKMETHOD(jcoll, true)) return false;
-    bool rv = _ejdbsavebsonimpl(jcoll, bs, oid, merge);
-    JBCUNLOCKMETHOD(jcoll);
+    if (!JBCLOCKMETHOD(coll, true)) return false;
+    bool rv = _ejdbsavebsonimpl(coll, bs, oid, merge);
+    JBCUNLOCKMETHOD(coll);
     return rv;
 }

-bool ejdbsavebson3(EJCOLL *jcoll, const void *bsdata, bson_oid_t *oid, bool merge) {
+bool ejdbsavebson3(EJCOLL *coll, const void *bsdata, bson_oid_t *oid, bool merge) {
     bson bs;
     bson_init_with_data(&bs, bsdata);
-    return ejdbsavebson2(jcoll, &bs, oid, merge);
-}
-
-bool ejdbrmbson(EJCOLL *jcoll, bson_oid_t *oid) {
-    assert(jcoll && oid);
-    if (!JBISOPEN(jcoll->jb)) {
-        _ejdbsetecode(jcoll->jb, TCEINVALID, __FILE__, __LINE__, __func__);
+    return ejdbsavebson2(coll, &bs, oid, merge);
+}
+
+bool ejdbrmbson(EJCOLL *coll, bson_oid_t *oid) {
+    assert(coll && oid);
+    if (!JBISOPEN(coll->jb)) {
+        _ejdbsetecode(coll->jb, TCEINVALID, __FILE__, __LINE__, __func__);
         return false;
     }
-    JBCLOCKMETHOD(jcoll, true);
+    JBCLOCKMETHOD(coll, true);
     bool rv = true;
     const void *olddata;
     int olddatasz = 0;
-    TCMAP *rmap = tctdbget(jcoll->tdb, oid, sizeof (*oid));
+    TCMAP *rmap = tctdbget(coll->tdb, oid, sizeof (*oid));
     if (!rmap) {
         goto finish;
     }
     olddata = tcmapget3(rmap, JDBCOLBSON, JDBCOLBSONL, &olddatasz);
-    if (!_updatebsonidx(jcoll, oid, NULL, olddata, olddatasz, NULL) ||
-            !tctdbout(jcoll->tdb, oid, sizeof (*oid))) {
+    if (!_updatebsonidx(coll, oid, NULL, olddata, olddatasz, NULL) ||
+            !tctdbout(coll->tdb, oid, sizeof (*oid))) {
         rv = false;
     }
 finish:
-    JBCUNLOCKMETHOD(jcoll);
+    JBCUNLOCKMETHOD(coll);
     if (rmap) {
         tcmapdel(rmap);
     }
     return rv;
 }

-bson* ejdbloadbson(EJCOLL *jcoll, const bson_oid_t *oid) {
-    assert(jcoll && oid);
-    if (!JBISOPEN(jcoll->jb)) {
-        _ejdbsetecode(jcoll->jb, TCEINVALID, __FILE__, __LINE__, __func__);
+bson* ejdbloadbson(EJCOLL *coll, const bson_oid_t *oid) {
+    assert(coll && oid);
+    if (!JBISOPEN(coll->jb)) {
+        _ejdbsetecode(coll->jb, TCEINVALID, __FILE__, __LINE__, __func__);
         return NULL;
     }
-    JBCLOCKMETHOD(jcoll, false);
+    JBCLOCKMETHOD(coll, false);
     bson *ret = NULL;
     int datasz;
-    void *cdata = tchdbget(jcoll->tdb->hdb, oid, sizeof (*oid), &datasz);
+    void *cdata = tchdbget(coll->tdb->hdb, oid, sizeof (*oid), &datasz);
     if (!cdata) {
         goto finish;
     }
@@ -414,7 +430,7 @@
     ret = bson_create();
     bson_init_finished_data(ret, bsdata);
 finish:
-    JBCUNLOCKMETHOD(jcoll);
+    JBCUNLOCKMETHOD(coll);
     if (cdata) {
         TCFREE(cdata);
     }
@@ -527,36 +543,36 @@
 }

 /** Set index */
-bool ejdbsetindex(EJCOLL *jcoll, const char *fpath, int flags) {
-    return _setindeximpl(jcoll, fpath, flags, false);
-}
-
-uint32_t ejdbupdate(EJCOLL *jcoll, bson *qobj, bson *orqobjs, int orqobjsnum, bson *hints, TCXSTR *log) {
-    assert(jcoll);
+bool ejdbsetindex(EJCOLL *coll, const char *fpath, int flags) {
+    return _setindeximpl(coll, fpath, flags, false);
+}
+
+uint32_t ejdbupdate(EJCOLL *coll, bson *qobj, bson *orqobjs, int orqobjsnum, bson *hints, TCXSTR *log) {
+    assert(coll);
     uint32_t count = 0;
-    EJQ *q = ejdbcreatequery(jcoll->jb, qobj, orqobjs, orqobjsnum, hints);
+    EJQ *q = ejdbcreatequery(coll->jb, qobj, orqobjs, orqobjsnum, hints);
     if (q == NULL) {
         return count;
     }
-    ejdbqryexecute(jcoll, q, &count, JBQRYCOUNT, log);
+    ejdbqryexecute(coll, q, &count, JBQRYCOUNT, log);
     ejdbquerydel(q);
     return count;
 }

-EJQRESULT ejdbqryexecute(EJCOLL *jcoll, const EJQ *q, uint32_t *count, int qflags, TCXSTR *log) {
-    assert(jcoll && q && q->qflist);
-    if (!JBISOPEN(jcoll->jb)) {
-        _ejdbsetecode(jcoll->jb, TCEINVALID, __FILE__, __LINE__, __func__);
+EJQRESULT ejdbqryexecute(EJCOLL *coll, const EJQ *q, uint32_t *count, int qflags, TCXSTR *log) {
+    assert(coll && q && q->qflist);
+    if (!JBISOPEN(coll->jb)) {
+        _ejdbsetecode(coll->jb, TCEINVALID, __FILE__, __LINE__, __func__);
         return NULL;
     }
-    JBCLOCKMETHOD(jcoll, (q->flags & EJQUPDATING) ? true : false);
-    _ejdbsetecode(jcoll->jb, TCESUCCESS, __FILE__, __LINE__, __func__);
-    if (ejdbecode(jcoll->jb) != TCESUCCESS) { //we are not in fatal state
-        JBCUNLOCKMETHOD(jcoll);
+    JBCLOCKMETHOD(coll, (q->flags & EJQUPDATING) ? true : false);
+    _ejdbsetecode(coll->jb, TCESUCCESS, __FILE__, __LINE__, __func__);
+    if (ejdbecode(coll->jb) != TCESUCCESS) { //we are not in fatal state
+        JBCUNLOCKMETHOD(coll);
         return NULL;
     }
-    TCLIST *res = _qryexecute(jcoll, q, count, qflags, log);
-    JBCUNLOCKMETHOD(jcoll);
+    TCLIST *res = _qryexecute(coll, q, count, qflags, log);
+    JBCUNLOCKMETHOD(coll);
     return res;
 }

@@ -580,16 +596,16 @@
     }
 }

-bool ejdbsyncoll(EJCOLL *jcoll) {
-    assert(jcoll);
-    if (!JBISOPEN(jcoll->jb)) {
-        _ejdbsetecode(jcoll->jb, TCEINVALID, __FILE__, __LINE__, __func__);
+bool ejdbsyncoll(EJCOLL *coll) {
+    assert(coll);
+    if (!JBISOPEN(coll->jb)) {
+        _ejdbsetecode(coll->jb, TCEINVALID, __FILE__, __LINE__, __func__);
         return false;
     }
     bool rv = false;
-    if (!JBCLOCKMETHOD(jcoll, true)) return false;
-    rv = tctdbsync(jcoll->tdb);
-    JBCUNLOCKMETHOD(jcoll);
+    if (!JBCLOCKMETHOD(coll, true)) return false;
+    rv = tctdbsync(coll->tdb);
+    JBCUNLOCKMETHOD(coll);
     return rv;
 }

@@ -609,80 +625,80 @@
     return rv;
 }

-bool ejdbtranbegin(EJCOLL *jcoll) {
-    assert(jcoll);
-    if (!JBISOPEN(jcoll->jb)) {
-        _ejdbsetecode(jcoll->jb, TCEINVALID, __FILE__, __LINE__, __func__);
+bool ejdbtranbegin(EJCOLL *coll) {
+    assert(coll);
+    if (!JBISOPEN(coll->jb)) {
+        _ejdbsetecode(coll->jb, TCEINVALID, __FILE__, __LINE__, __func__);
         return false;
     }
     for (double wsec = 1.0 / sysconf_SC_CLK_TCK; true; wsec *= 2) {
-        if (!JBCLOCKMETHOD(jcoll, true)) return false;
-        if (!jcoll->tdb->open || !jcoll->tdb->wmode) {
-            _ejdbsetecode(jcoll->jb, TCEINVALID, __FILE__, __LINE__, __func__);
-            JBCUNLOCKMETHOD(jcoll);
+        if (!JBCLOCKMETHOD(coll, true)) return false;
+        if (!coll->tdb->open || !coll->tdb->wmode) {
+            _ejdbsetecode(coll->jb, TCEINVALID, __FILE__, __LINE__, __func__);
+            JBCUNLOCKMETHOD(coll);
             return false;
         }
-        if (!jcoll->tdb->tran) break;
-        JBCUNLOCKMETHOD(jcoll);
+        if (!coll->tdb->tran) break;
+        JBCUNLOCKMETHOD(coll);
         if (wsec > 1.0) wsec = 1.0;
         tcsleep(wsec);
     }
-    if (!tctdbtranbeginimpl(jcoll->tdb)) {
-        JBCUNLOCKMETHOD(jcoll);
+    if (!tctdbtranbeginimpl(coll->tdb)) {
+        JBCUNLOCKMETHOD(coll);
         return false;
     }
-    jcoll->tdb->tran = true;
-    JBCUNLOCKMETHOD(jcoll);
+    coll->tdb->tran = true;
+    JBCUNLOCKMETHOD(coll);
     return true;
 }

-bool ejdbtrancommit(EJCOLL *jcoll) {
-    assert(jcoll);
-    if (!JBISOPEN(jcoll->jb)) {
-        _ejdbsetecode(jcoll->jb, TCEINVALID, __FILE__, __LINE__, __func__);
+bool ejdbtrancommit(EJCOLL *coll) {
+    assert(coll);
+    if (!JBISOPEN(coll->jb)) {
+        _ejdbsetecode(coll->jb, TCEINVALID, __FILE__, __LINE__, __func__);
         return false;
     }
-    if (!JBCLOCKMETHOD(jcoll, true)) return false;
-    if (!jcoll->tdb->open || !jcoll->tdb->wmode || !jcoll->tdb->tran) {
-        _ejdbsetecode(jcoll->jb, TCEINVALID, __FILE__, __LINE__, __func__);
-        JBCUNLOCKMETHOD(jcoll);
+    if (!JBCLOCKMETHOD(coll, true)) return false;
+    if (!coll->tdb->open || !coll->tdb->wmode || !coll->tdb->tran) {
+        _ejdbsetecode(coll->jb, TCEINVALID, __FILE__, __LINE__, __func__);
+        JBCUNLOCKMETHOD(coll);
         return false;
     }
-    jcoll->tdb->tran = false;
+    coll->tdb->tran = false;
     bool err = false;
-    if (!tctdbtrancommitimpl(jcoll->tdb)) err = true;
-    JBCUNLOCKMETHOD(jcoll);
+    if (!tctdbtrancommitimpl(coll->tdb)) err = true;
+    JBCUNLOCKMETHOD(coll);
     return !err;
 }

-bool ejdbtranabort(EJCOLL *jcoll) {
-    assert(jcoll);
-    if (!JBISOPEN(jcoll->jb)) {
-        _ejdbsetecode(jcoll->jb, TCEINVALID, __FILE__, __LINE__, __func__);
+bool ejdbtranabort(EJCOLL *coll) {
+    assert(coll);
+    if (!JBISOPEN(coll->jb)) {
+        _ejdbsetecode(coll->jb, TCEINVALID, __FILE__, __LINE__, __func__);
         return false;
     }
-    if (!JBCLOCKMETHOD(jcoll, true)) return false;
-    if (!jcoll->tdb->open || !jcoll->tdb->wmode || !jcoll->tdb->tran) {
-        _ejdbsetecode(jcoll->jb, TCEINVALID, __FILE__, __LINE__, __func__);
-        JBCUNLOCKMETHOD(jcoll);
+    if (!JBCLOCKMETHOD(coll, true)) return false;
+    if (!coll->tdb->open || !coll->tdb->wmode || !coll->tdb->tran) {
+        _ejdbsetecode(coll->jb, TCEINVALID, __FILE__, __LINE__, __func__);
+        JBCUNLOCKMETHOD(coll);
         return false;
     }
-    jcoll->tdb->tran = false;
+    coll->tdb->tran = false;
     bool err = false;
-    if (!tctdbtranabortimpl(jcoll->tdb)) err = true;
-    JBCUNLOCKMETHOD(jcoll);
+    if (!tctdbtranabortimpl(coll->tdb)) err = true;
+    JBCUNLOCKMETHOD(coll);
     return !err;
 }

-bool ejdbtranstatus(EJCOLL *jcoll, bool *txactive) {
-    assert(jcoll && txactive);
-    if (!JBISOPEN(jcoll->jb)) {
-        _ejdbsetecode(jcoll->jb, TCEINVALID, __FILE__, __LINE__, __func__);
+bool ejdbtranstatus(EJCOLL *coll, bool *txactive) {
+    assert(coll && txactive);
+    if (!JBISOPEN(coll->jb)) {
+        _ejdbsetecode(coll->jb, TCEINVALID, __FILE__, __LINE__, __func__);
         return false;
     }
-    if (!JBCLOCKMETHOD(jcoll, true)) return false;
-    *txactive = jcoll->tdb->tran;
-    JBCUNLOCKMETHOD(jcoll);
+    if (!JBCLOCKMETHOD(coll, true)) return false;
+    *txactive = coll->tdb->tran;
+    JBCUNLOCKMETHOD(coll);
     return true;
 }

@@ -938,8 +954,8 @@
  * private features
  *************************************************************************************************/

-static bool _setindeximpl(EJCOLL *jcoll, const char *fpath, int flags, bool nolock) {
-    assert(jcoll && fpath);
+static bool _setindeximpl(EJCOLL *coll, const char *fpath, int flags, bool nolock) {
+    assert(coll && fpath);
     bool rv = true;
     bson *imeta = NULL;
     bson_iterator it;
@@ -966,7 +982,7 @@
     char ikey[BSON_MAX_FPATH_LEN + 2]; //add 2 bytes for one char prefix and '\0'term
     int fpathlen = strlen(fpath);
     if (fpathlen > BSON_MAX_FPATH_LEN) {
-        _ejdbsetecode(jcoll->jb, JBEFPATHINVALID, __FILE__, __LINE__, __func__);
+        _ejdbsetecode(coll->jb, JBEFPATHINVALID, __FILE__, __LINE__, __func__);
         rv = false;
         goto finish;
     }
@@ -976,13 +992,13 @@
     ipath[0] = 's'; //defaulting to string index type

     if (!nolock) {
-        JBENSUREOPENLOCK(jcoll->jb, true, false);
-    }
-    imeta = _imetaidx(jcoll, fpath);
+        JBENSUREOPENLOCK(coll->jb, true, false);
+    }
+    imeta = _imetaidx(coll, fpath);
     if (!imeta) {
         if (idrop) { //Cannot drop/optimize not existent index;
             if (!nolock) {
-                JBUNLOCKMETHOD(jcoll->jb);
+                JBUNLOCKMETHOD(coll->jb);
             }
             goto finish;
         }
@@ -994,10 +1010,10 @@
         bson_append_string(imeta, "ipath", fpath);
         bson_append_int(imeta, "iflags", flags);
         bson_finish(imeta);
-        rv = _metasetbson2(jcoll, ikey, imeta, false, false);
+        rv = _metasetbson2(coll, ikey, imeta, false, false);
         if (!rv) {
             if (!nolock) {
-                JBUNLOCKMETHOD(jcoll->jb);
+                JBUNLOCKMETHOD(coll->jb);
             }
             goto finish;
         }
@@ -1010,18 +1026,18 @@
             bson_init(&imetadelta);
             bson_append_int(&imetadelta, "iflags", (flags | oldiflags));
             bson_finish(&imetadelta);
-            rv = _metasetbson2(jcoll, ikey, &imetadelta, true, true);
+            rv = _metasetbson2(coll, ikey, &imetadelta, true, true);
             bson_destroy(&imetadelta);
             if (!rv) {
                 if (!nolock) {
-                    JBUNLOCKMETHOD(jcoll->jb);
+                    JBUNLOCKMETHOD(coll->jb);
                 }
                 goto finish;
             }
         }
     }
     if (!nolock) {
-        JBUNLOCKMETHOD(jcoll->jb);
+        JBUNLOCKMETHOD(coll->jb);
     }
     if (idrop) {
         tcitype = TDBITVOID;
@@ -1036,31 +1052,31 @@
     }

     if (!nolock) {
-        if (!JBCLOCKMETHOD(jcoll, true)) {
+        if (!JBCLOCKMETHOD(coll, true)) {
             rv = false;
             goto finish;
         }
     }
     _BSONIPATHROWLDR op;
     op.icase = false;
-    op.jcoll = jcoll;
+    op.coll = coll;
     if (tcitype) {
         if (flags & JBIDXSTR) {
             ipath[0] = 's';
-            rv = tctdbsetindexrldr(jcoll->tdb, ipath, tcitype, _bsonipathrowldr, &op);
+            rv = tctdbsetindexrldr(coll->tdb, ipath, tcitype, _bsonipathrowldr, &op);
         }
         if (flags & JBIDXISTR) {
             ipath[0] = 'i';
             op.icase = true;
-            rv = tctdbsetindexrldr(jcoll->tdb, ipath, tcitype, _bsonipathrowldr, &op);
+            rv = tctdbsetindexrldr(coll->tdb, ipath, tcitype, _bsonipathrowldr, &op);
         }
         if (rv && (flags & JBIDXNUM)) {
             ipath[0] = 'n';
-            rv = tctdbsetindexrldr(jcoll->tdb, ipath, tcitype, _bsonipathrowldr, &op);
+            rv = tctdbsetindexrldr(coll->tdb, ipath, tcitype, _bsonipathrowldr, &op);
         }
         if (rv && (flags & JBIDXARR)) {
             ipath[0] = 'a';
-            rv = tctdbsetindexrldr(jcoll->tdb, ipath, tcitype, _bsonipathrowldr, &op);
+            rv = tctdbsetindexrldr(coll->tdb, ipath, tcitype, _bsonipathrowldr, &op);
         }
         if (idrop) { //Update index meta on drop
             oldiflags &= ~flags;
@@ -1069,33 +1085,33 @@
                 bson_init(&imetadelta);
                 bson_append_int(&imetadelta, "iflags", oldiflags);
                 bson_finish(&imetadelta);
-                rv = _metasetbson2(jcoll, ikey, &imetadelta, true, true);
+                rv = _metasetbson2(coll, ikey, &imetadelta, true, true);
                 bson_destroy(&imetadelta);
             } else { //Index dropped completely
-                rv = _metasetbson2(jcoll, ikey, NULL, false, false);
+                rv = _metasetbson2(coll, ikey, NULL, false, false);
             }
         }
     } else {
         if ((flags & JBIDXSTR) && (ibld || !(oldiflags & JBIDXSTR))) {
             ipath[0] = 's';
-            rv = tctdbsetindexrldr(jcoll->tdb, ipath, TDBITLEXICAL, _bsonipathrowldr, &op);
+            rv = tctdbsetindexrldr(coll->tdb, ipath, TDBITLEXICAL, _bsonipathrowldr, &op);
         }
         if ((flags & JBIDXISTR) && (ibld || !(oldiflags & JBIDXISTR))) {
             ipath[0] = 'i';
             op.icase = true;
-            rv = tctdbsetindexrldr(jcoll->tdb, ipath, TDBITLEXICAL, _bsonipathrowldr, &op);
+            rv = tctdbsetindexrldr(coll->tdb, ipath, TDBITLEXICAL, _bsonipathrowldr, &op);
         }
         if (rv && (flags & JBIDXNUM) && (ibld || !(oldiflags & JBIDXNUM))) {
             ipath[0] = 'n';
-            rv = tctdbsetindexrldr(jcoll->tdb, ipath, TDBITDECIMAL, _bsonipathrowldr, &op);
+            rv = tctdbsetindexrldr(coll->tdb, ipath, TDBITDECIMAL, _bsonipathrowldr, &op);
         }
         if (rv && (flags & JBIDXARR) && (ibld || !(oldiflags & JBIDXARR))) {
             ipath[0] = 'a';
-            rv = tctdbsetindexrldr(jcoll->tdb, ipath, TDBITTOKEN, _bsonipathrowldr, &op);
+            rv = tctdbsetindexrldr(coll->tdb, ipath, TDBITTOKEN, _bsonipathrowldr, &op);
         }
     }
     if (!nolock) {
-        JBCUNLOCKMETHOD(jcoll);
+        JBCUNLOCKMETHOD(coll);
     }
 finish:
     if (imeta) {
@@ -2112,7 +2128,7 @@
     return _qrybsrecurrmatch(qf, &ffpctx, 0);
 }

-static bool _qry_$and_$or_match(EJCOLL *jcoll, EJQ *ejq, const void *pkbuf, int pkbufsz) {
+static bool _qry_$and_$or_match(EJCOLL *coll, EJQ *ejq, const void *pkbuf, int pkbufsz) {
     bool isor = (ejq->orqlist && TCLISTNUM(ejq->orqlist) > 0);
     bool isand = (ejq->andqlist && TCLISTNUM(ejq->andqlist) > 0);
     if (!isor && !isand) {
@@ -2124,7 +2140,7 @@
     if (!bsbuf || bsbufsz <= 0) {
         tcxstrclear(ejq->colbuf);
         tcxstrclear(ejq->bsbuf);
-        if (tchdbgetintoxstr(jcoll->tdb->hdb, pkbuf, pkbufsz, ejq->colbuf) <= 0) {
+        if (tchdbgetintoxstr(coll->tdb->hdb, pkbuf, pkbufsz, ejq->colbuf) <= 0) {
             return false;
         }
         if (tcmaploadoneintoxstr(TCXSTRPTR(ejq->colbuf), TCXSTRSIZE(ejq->colbuf), JDBCOLBSON, JDBCOLBSONL, ejq->bsbuf) <= 0) {
@@ -2133,16 +2149,16 @@
         bsbufsz = TCXSTRSIZE(ejq->bsbuf);
         bsbuf = TCXSTRPTR(ejq->bsbuf);
     }
-    if (isand && !_qryandmatch2(jcoll, ejq, bsbuf, bsbufsz)) {
+    if (isand && !_qryandmatch2(coll, ejq, bsbuf, bsbufsz)) {
         return false;
     } else if (isor) {
-        return _qryormatch2(jcoll, ejq, bsbuf, bsbufsz);
+        return _qryormatch2(coll, ejq, bsbuf, bsbufsz);
     } else {
         return true;
     }
 }

-static bool _qryormatch3(EJCOLL *jcoll, EJQ *ejq, EJQ *oq, const void *bsbuf, int bsbufsz) {
+static bool _qryormatch3(EJCOLL *coll, EJQ *ejq, EJQ *oq, const void *bsbuf, int bsbufsz) {
     int j = 0;
     int jm = TCLISTNUM(oq->qflist);
     for (; j < jm; ++j) {
@@ -2158,11 +2174,11 @@
     }
     if (j == jm) { //all fields in oq are matched
         if (oq->andqlist && TCLISTNUM(oq->andqlist) > 0 &&
-                !_qryandmatch2(jcoll, oq, bsbuf, bsbufsz)) { //we have nested $and fields
+                !_qryandmatch2(coll, oq, bsbuf, bsbufsz)) { //we have nested $and fields
             return false;
         }
         if (oq->orqlist && TCLISTNUM(oq->orqlist) &&
-                !_qryormatch2(jcoll, oq, bsbuf, bsbufsz)) { //we have nested $or fields
+                !_qryormatch2(coll, oq, bsbuf, bsbufsz)) { //we have nested $or fields
             return false;
         }
         return true;
@@ -2170,11 +2186,11 @@
     return false;
 }

-static bool _qryormatch2(EJCOLL *jcoll, EJQ *ejq, const void *bsbuf, int bsbufsz) {
+static bool _qryormatch2(EJCOLL *coll, EJQ *ejq, const void *bsbuf, int bsbufsz) {
     if (!ejq->orqlist || TCLISTNUM(ejq->orqlist) < 1) {
         return true;
     }
-    if (ejq->lastmatchedorq && _qryormatch3(jcoll, ejq, ejq->lastmatchedorq, bsbuf, bsbufsz)) {
+    if (ejq->lastmatchedorq && _qryormatch3(coll, ejq, ejq->lastmatchedorq, bsbuf, bsbufsz)) {
         return true;
     }
     for (int i = 0; i < TCLISTNUM(ejq->orqlist); ++i) {
@@ -2183,7 +2199,7 @@
         if (ejq->lastmatchedorq == oq) {
             continue;
         }
-        if (_qryormatch3(jcoll, ejq, oq, bsbuf, bsbufsz)) {
+        if (_qryormatch3(coll, ejq, oq, bsbuf, bsbufsz)) {
             ejq->lastmatchedorq = oq;
             return true;
         }
@@ -2191,7 +2207,7 @@
     return false;
 }

-static bool _qryandmatch2(EJCOLL *jcoll, EJQ *ejq, const void *bsbuf, int bsbufsz) {
+static bool _qryandmatch2(EJCOLL *coll, EJQ *ejq, const void *bsbuf, int bsbufsz) {
     if (!ejq->andqlist || TCLISTNUM(ejq->andqlist) < 1) {
         return true;
     }
@@ -2210,11 +2226,11 @@
             }
         }
         if (aq->andqlist && TCLISTNUM(aq->andqlist) > 0 &&
-                !_qryandmatch2(jcoll, aq, bsbuf, bsbufsz)) { //we have nested $and fields
+                !_qryandmatch2(coll, aq, bsbuf, bsbufsz)) { //we have nested $and fields
             return false;
         }
         if (aq->orqlist && TCLISTNUM(aq->orqlist) > 0 &&
-                !_qryormatch2(jcoll, aq, bsbuf, bsbufsz)) { //we have nested $or fields
+                !_qryormatch2(coll, aq, bsbuf, bsbufsz)) { //we have nested $or fields
             return false;
         }
     }
@@ -2224,7 +2240,7 @@
 /** Return true if all main query conditions matched */
 static bool _qryallcondsmatch(
         EJQ *ejq, int anum,
-        EJCOLL *jcoll, EJQF **qfs, int qfsz,
+        EJCOLL *coll, EJQF **qfs, int qfsz,
         const void *pkbuf, int pkbufsz) {
     assert(ejq->colbuf && ejq->bsbuf);
     if (!(ejq->flags & EJQUPDATING) && (ejq->flags & EJQONLYCOUNT) && anum < 1) {
@@ -2232,7 +2248,7 @@
     }
     tcxstrclear(ejq->colbuf);
     tcxstrclear(ejq->bsbuf);
-    if (tchdbgetintoxstr(jcoll->tdb->hdb, pkbuf, pkbufsz, ejq->colbuf) <= 0) {
+    if (tchdbgetintoxstr(coll->tdb->hdb, pkbuf, pkbufsz, ejq->colbuf) <= 0) {
         return false;
     }
     if (tcmaploadoneintoxstr(TCXSTRPTR(ejq->colbuf), TCXSTRSIZE(ejq->colbuf), JDBCOLBSON, JDBCOLBSONL, ejq->bsbuf) <= 0) {
@@ -2420,7 +2436,7 @@

     assert(op);
     _BSON$DOVISITORCTX *ictx = op;
-    EJCOLL *jcoll;
+    EJCOLL *coll;
     TCMAP *dfields = ictx->dfields;
     bson_type lbt = BSON_ITERATOR_TYPE(it), bt;
     bson_iterator doit, bufit, sit;
@@ -2454,8 +2470,8 @@
             BSON_ITERATOR_INIT(&doit, dofield->updateobj);
             while ((bt = bson_iterator_next(&doit)) != BSON_EOO) {
                 if (bt != BSON_STRING || strcmp("$join", BSON_ITERATOR_KEY(&doit))) continue;
-                jcoll = _getcoll(ictx->jb, bson_iterator_string(&doit));
-                if (!jcoll) break;
+                coll = _getcoll(ictx->jb, bson_iterator_string(&doit));
+                if (!coll) break;
                 bson_oid_t loid;
                 if (lbt == BSON_STRING) {
                     sval = bson_iterator_string(it);
@@ -2467,7 +2483,7 @@
                 if (lbt == BSON_STRING || lbt == BSON_OID) {
                     tcxstrclear(ictx->q->colbuf);
                     tcxstrclear(ictx->q->tmpbuf);
-                    if (!tchdbgetintoxstr(jcoll->tdb->hdb, &loid, sizeof (loid), ictx->q->colbuf) ||
+                    if (!tchdbgetintoxstr(coll->tdb->hdb, &loid, sizeof (loid), ictx->q->colbuf) ||
                             !tcmaploadoneintoxstr(TCXSTRPTR(ictx->q->colbuf), TCXSTRSIZE(ictx->q->colbuf),
                             JDBCOLBSON, JDBCOLBSONL, ictx->q->tmpbuf)) {
                         break;
@@ -2493,7 +2509,7 @@
                         }
                         tcxstrclear(ictx->q->colbuf);
                         tcxstrclear(ictx->q->tmpbuf);
-                        if (!tchdbgetintoxstr(jcoll->tdb->hdb, &loid, sizeof (loid), ictx->q->colbuf) ||
+                        if (!tchdbgetintoxstr(coll->tdb->hdb, &loid, sizeof (loid), ictx->q->colbuf) ||
                                 !tcmaploadoneintoxstr(TCXSTRPTR(ictx->q->colbuf), TCXSTRSIZE(ictx->q->colbuf),
                                 JDBCOLBSON, JDBCOLBSONL, ictx->q->tmpbuf)) {
                             bson_append_field_from_iterator(&sit, ictx->sbson);
@@ -2515,21 +2531,23 @@
     return rv;
 }

-static bool _pushprocessedbson(EJDB *jb, EJQ *q, TCLIST *res, TCMAP *dfields, TCMAP *ifields,
-        bool imode, const void *bsbuf, int bsbufsz) {
+static bool _pushprocessedbson(_QRYCTX *ctx, const void *bsbuf, int bsbufsz) {
     assert(bsbuf && bsbufsz);
+    if (!ctx->dfields && !ctx->ifields && !ctx->q->$ifields) { //Trivial case: no $do operations or $fields
+        tclistpush(ctx->res, bsbuf, bsbufsz);
+        return true;
+    }
     bool rv = true;
-    if (!dfields && !ifields && !q->$ifields) { //Trivial case: no $do operations or $fields
-        tclistpush(res, bsbuf, bsbufsz);
-        return rv;
-    }
+    EJDB *jb = ctx->coll->jb;
+    EJQ *q = ctx->q;
+    TCMAP *ifields = ctx->ifields;
     int sp;
     char bstack[JBSBUFFERSZ];
     bson bsout;
     bson_init_on_stack(&bsout, bstack, bsbufsz, JBSBUFFERSZ);

-    if (dfields) { //$do fields exists
-        rv = _exec$do(jb, q, dfields, bsbuf, &bsout);
+    if (ctx->dfields) { //$do fields exists
+        rv = _exec$do(ctx, bsbuf, &bsout);
     }

     if (rv && (ifields || q->$ifields)) { //$fields hints
@@ -2540,7 +2558,7 @@
             bson_init_size(&bsout, bson_size(&bsout));
         }
         if (q->$ifields) { //we have positional $(projection)
-            assert(imode == true); //ensure we are in include mode
+            assert(ctx->imode == true); //ensure we are in include mode
             if (!_ifields) {
                 _ifields = tcmapnew2(TCMAPRNUM(q->$ifields));
             } else {
@@ -2584,7 +2602,7 @@
         BSONSTRIPCTX sctx = {
             .ifields = _ifields,
             .fkfields = _fkfields,
-            .imode = imode,
+            .imode = ctx->imode,
             .bsbuf = inbuf,
             .bsout = &bsout
         };
@@ -2605,9 +2623,9 @@
     if (rv) {
         assert(bsout.finished);
         if (bsout.flags & BSON_FLAG_STACK_ALLOCATED) {
-            TCLISTPUSH(res, bsout.data, bson_size(&bsout));
+            TCLISTPUSH(ctx->res, bsout.data, bson_size(&bsout));
         } else {
-            tclistpushmalloc(res, bsout.data, bson_size(&bsout));
+            tclistpushmalloc(ctx->res, bsout.data, bson_size(&bsout));
         }
     } else {
         bson_destroy(&bsout);
@@ -2615,56 +2633,56 @@
     return rv;
 }

-static bool _exec$do(EJDB *jb, EJQ *q, TCMAP *dfields, const void *bsbuf, bson *bsout) {
-    assert(dfields);
+static bool _exec$do(_QRYCTX *ctx, const void *bsbuf, bson *bsout) {
+    assert(ctx && ctx->dfields);
     _BSON$DOVISITORCTX ictx = {
-        .q = q,
-        .jb = jb,
-        .dfields = dfields,
+        .q = ctx->q,
+        .jb = ctx->coll->jb,
+        .dfields = ctx->dfields,
         .sbson = bsout
     };
     bson_iterator it;
     BSON_ITERATOR_FROM_BUFFER(&it, bsbuf);
     bson_visit_fields(&it, 0, _bson$dovisitor, &ictx);
     if (bson_finish(bsout) != BSON_OK) {
-        _ejdbsetecode(jb, JBEINVALIDBSON, __FILE__, __LINE__, __func__);
+        _ejdbsetecode(ctx->coll->jb, JBEINVALIDBSON, __FILE__, __LINE__, __func__);
         return false;
     }
     return true;
 }

-static bool _qryupdate(EJCOLL *jcoll, const EJQ *ejq, void *bsbuf, int bsbufsz, TCLIST *didxctx, TCXSTR *log) {
-    assert(ejq->flags & EJQUPDATING);
-    assert(bsbuf && didxctx);
+static bool _qryupdate(_QRYCTX *ctx, void *bsbuf, int bsbufsz) {
+    assert(ctx && ctx->q && (ctx->q->flags & EJQUPDATING) && bsbuf && ctx->didxctx);

     bool rv = true;
     bool update = false;
-
+    EJCOLL *coll = ctx->coll;
+    EJQ *q = ctx->q;
     bson_oid_t *oid;
     bson_type bt, bt2;
     bson_iterator it, it2;
     TCMAP *rowm = NULL;

-    if (ejq->flags & EJQDROPALL) { //Record will be dropped
+    if (q->flags & EJQDROPALL) { //Record will be dropped
         bt = bson_find_from_buffer(&it, bsbuf, JDBIDKEYNAME);
         if (bt != BSON_OID) {
-            _ejdbsetecode(jcoll->jb, JBEQUPDFAILED, __FILE__, __LINE__, __func__);
+            _ejdbsetecode(coll->jb, JBEQUPDFAILED, __FILE__, __LINE__, __func__);
             return false;
         }
         oid = bson_iterator_oid(&it);
         assert(oid);
-        if (log) {
+        if (ctx->log) {
             char xoid[25];
             bson_oid_to_string(oid, xoid);
-            tcxstrprintf(log, "$DROPALL ON: %s\n", xoid);
+            tcxstrprintf(ctx->log, "$DROPALL ON: %s\n", xoid);
         }
         const void *olddata;
         int olddatasz = 0;
-        TCMAP *rmap = tctdbget(jcoll->tdb, oid, sizeof (*oid));
+        TCMAP *rmap = tctdbget(coll->tdb, oid, sizeof (*oid));
         if (rmap) {
             olddata = tcmapget3(rmap, JDBCOLBSON, JDBCOLBSONL, &olddatasz);
-            if (!_updatebsonidx(jcoll, oid, NULL, olddata, olddatasz, didxctx) ||
-                    !tctdbout(jcoll->tdb, oid, sizeof (*oid))) {
+            if (!_updatebsonidx(coll, oid, NULL, olddata, olddatasz, ctx->didxctx) ||
+                    !tctdbout(coll->tdb, oid, sizeof (*oid))) {
                 rv = false;
             }
             tcmapdel(rmap);
@@ -2683,8 +2701,8 @@
     const EJQF * pullqf[2] = {NULL}; /*$pull, $pullAll*/

     //$set, $inc, $addToSet, $addToSetAll, $pull, $pullAll operations
-    for (int i = 0; i < TCLISTNUM(ejq->qflist); ++i) {
-        const EJQF *qf = TCLISTVALPTR(ejq->qflist, i);
+    for (int i = 0; i < TCLISTNUM(q->qflist); ++i) {
+        const EJQF *qf = TCLISTVALPTR(q->qflist, i);
         if (qf->updateobj == NULL) {
             continue;
         }
@@ -2717,7 +2735,7 @@
         int err = bson_merge3(bsbuf, bson_data(setqf->updateobj), &bsout);
         if (err) {
             rv = false;
-            _ejdbsetecode(jcoll->jb, JBEQUPDFAILED, __FILE__, __LINE__, __func__);
+            _ejdbsetecode(coll->jb, JBEQUPDFAILED, __FILE__, __LINE__, __func__);
         }
         bson_finish(&bsout);
     }
@@ -2747,7 +2765,7 @@
                 }
                 if (bson_inplace_set_double(&it2, v)) {
                     rv = false;
-                    _ejdbsetecode(jcoll->jb, JBEQUPDFAILED, __FILE__, __LINE__, __func__);
+                    _ejdbsetecode(coll->jb, JBEQUPDFAILED, __FILE__, __LINE__, __func__);
                     break;
                 }
                 update = true;
@@ -2756,7 +2774,7 @@
                 v += bson_iterator_long(&it);
                 if (bson_inplace_set_long(&it2, v)) {
                     rv = false;
-                    _ejdbsetecode(jcoll->jb, JBEQUPDFAILED, __FILE__, __LINE__, __func__);
+                    _ejdbsetecode(coll->jb, JBEQUPDFAILED, __FILE__, __LINE__, __func__);
                     break;
                 }
                 update = true;
@@ -2782,7 +2800,7 @@
             //$pull $pullAll merge
             if (bson_merge_array_sets(bson_data(qf->updateobj), inbuf, true, (qf->flags & EJCONDALL), &bsout)) {
                 rv = false;
-                _ejdbsetecode(jcoll->jb, JBEQUPDFAILED, __FILE__, __LINE__, __func__);
+                _ejdbsetecode(coll->jb, JBEQUPDFAILED, __FILE__, __LINE__, __func__);
             }
             if (inbuf != bsbuf) {
                 TCFREE(inbuf);
@@ -2811,7 +2829,7 @@
             //$addToSet $addToSetAll merge
             if (bson_merge_array_sets(bson_data(qf->updateobj), inbuf, false, (qf->flags & EJCONDALL), &bsout)) {
                 rv = false;
-                _ejdbsetecode(jcoll->jb, JBEQUPDFAILED, __FILE__, __LINE__, __func__);
+                _ejdbsetecode(coll->jb, JBEQUPDFAILED, __FILE__, __LINE__, __func__);
             }
             if (inbuf != bsbuf) {
                 TCFREE(inbuf);
@@ -2826,7 +2844,7 @@

     if (bsout.err) { //Resulting BSON is OK?
         rv = false;
-        _ejdbsetecode(jcoll->jb, JBEQUPDFAILED, __FILE__, __LINE__, __func__);
+        _ejdbsetecode(coll->jb, JBEQUPDFAILED, __FILE__, __LINE__, __func__);
         goto finish;
     }
     if (bson_size(&bsout) == 0) { //Record was not updated
@@ -2836,15 +2854,15 @@
     bt = bson_find_from_buffer(&it, bsbuf, JDBIDKEYNAME);
     if (bt != BSON_OID) {
         rv = false;
-        _ejdbsetecode(jcoll->jb, JBEQUPDFAILED, __FILE__, __LINE__, __func__);
+        _ejdbsetecode(coll->jb, JBEQUPDFAILED, __FILE__, __LINE__, __func__);
         goto finish;
     }
     oid = bson_iterator_oid(&it);
     rowm = tcmapnew2(TCMAPTINYBNUM);
     tcmapput(rowm, JDBCOLBSON, JDBCOLBSONL, bson_data(&bsout), bson_size(&bsout));
-    rv = tctdbput(jcoll->tdb, oid, sizeof (*oid), rowm);
+    rv = tctdbput(coll->tdb, oid, sizeof (*oid), rowm);
     if (rv) {
-        rv = _updatebsonidx(jcoll, oid, &bsout, bsbuf, bsbufsz, didxctx);
+        rv = _updatebsonidx(coll, oid, &bsout, bsbuf, bsbufsz, ctx->didxctx);
     }

 finish:
@@ -2856,36 +2874,35 @@
 }

 /** Query */
-static TCLIST* _qryexecute(EJCOLL *jcoll, const EJQ *q, uint32_t *outcount, int qflags, TCXSTR *log) {
-    assert(jcoll && jcoll->tdb && jcoll->tdb->hdb);
+static TCLIST* _qryexecute(EJCOLL *coll, const EJQ *_q, uint32_t *outcount, int qflags, TCXSTR *log) {
+    assert(coll && coll->tdb && coll->tdb->hdb);
     *outcount = 0;

-    //Clone the query object
-    EJQ *ejq;
-    TCMALLOC(ejq, sizeof (*ejq));
-    if (!_qrydup(q, ejq, EJQINTERNAL)) {
-        TCFREE(ejq);
+    _QRYCTX ctx = {NULL};
+    EJQ *q; //Clone the query object
+    TCMALLOC(q, sizeof (*q));
+    if (!_qrydup(_q, q, EJQINTERNAL)) {
+        TCFREE(q);
         return NULL;
     }
-    EJQF *mqf = NULL; //main indexed query condition if exists
-    bool imode = false; //include(true) / exclude(false) mode for $fields in query
-    TCMAP *ifields = NULL; //field names included in result set
-    TCMAP *dfields = NULL; //fields with $do operations
-    if (!_qrypreprocess(jcoll, ejq, qflags, &mqf, &dfields, &ifields, &imode)) {
-        ejdbquerydel(ejq);
+    ctx.log = log;
+    ctx.q = q;
+    ctx.qflags = qflags;
+    ctx.coll = coll;
+    if (!_qrypreprocess(&ctx)) {
+        _qryctxclear(&ctx);
         return NULL;
     }
-
     bool all = false; //if True we need all records to fetch (sorting)
-    EJDB *jb = jcoll->jb;
-    TCHDB *hdb = jcoll->tdb->hdb;
-    TCLIST *res = (ejq->flags & EJQONLYCOUNT) ? NULL : tclistnew2(4096);
+    TCHDB *hdb = coll->tdb->hdb;
+    TCLIST *res = ctx.res;
+    EJQF *mqf = ctx.mqf;

     int sz = 0; //generic size var
     int anum = 0; //number of active conditions
     int ofsz = 0; //order fields count
     int aofsz = 0; //active order fields count
-    const int qfsz = TCLISTNUM(ejq->qflist); //number of all condition fields
+    const int qfsz = TCLISTNUM(q->qflist); //number of all condition fields
     EJQF **ofs = NULL; //order fields
     EJQF **qfs = NULL; //condition fields array
     if (qfsz > 0) {
@@ -2898,10 +2915,9 @@
     int vbufsz;

     uint32_t count = 0; //current count
-    uint32_t max = (ejq->max > 0) ? ejq->max : UINT_MAX;
-    uint32_t skip = ejq->skip;
+    uint32_t max = (q->max > 0) ? q->max : UINT_MAX;
+    uint32_t skip = q->skip;
     const TDBIDX *midx = mqf ? mqf->idx : NULL;
-    TCLIST *didxctx = (ejq->flags & EJQUPDATING) ? tclistnew() : NULL; //deffered indexing context

     if (midx) { //Main index used for ordering
         if (mqf->orderseq == 1 &&
@@ -2910,7 +2926,7 @@
         }
     }
     for (int i = 0; i < qfsz; ++i) {
-        EJQF *qf = TCLISTVALPTR(ejq->qflist, i);
+        EJQF *qf = TCLISTVALPTR(q->qflist, i);
         assert(qf);
         if (log) {
             if (qf->exprmap) {
@@ -2920,14 +2936,14 @@
                 tcxstrprintf(log, "FIELD: %s HAS $do OPERATION\n", qf->fpath);
             }
         }
-        qf->jb = jcoll->jb;
+        qf->jb = coll->jb;
         qfs[i] = qf;
         if (qf->fpathsz > 0 && !(qf->flags & EJFEXCLUDED)) {
             anum++;
         }
         if (qf->orderseq) {
             ofsz++;
-            if (ejq->flags & EJQONLYCOUNT) {
+            if (q->flags & EJQONLYCOUNT) {
                 qf->flags |= EJFORDERUSED;
             }
         }
@@ -2940,11 +2956,11 @@
                     ofs[i] = qfs[j];
                     if (!(ofs[i]->flags & EJFORDERUSED)) {
                         aofsz++;
-                        if (ifields) { //Force order field to be included in result set
-                            if (imode) { //add field to the included set
-                                tcmapputkeep(ifields, ofs[i]->fpath, ofs[i]->fpathsz, &yes, sizeof (yes));
+                        if (ctx.ifields) { //Force order field to be included in result set
+                            if (ctx.imode) { //add field to the included set
+                                tcmapputkeep(ctx.ifields, ofs[i]->fpath, ofs[i]->fpathsz, &yes, sizeof (yes));
                             } else { //remove field from excluded
-                                tcmapout(ifields, ofs[i]->fpath, ofs[i]->fpathsz);
+                                tcmapout(ctx.ifields, ofs[i]->fpath, ofs[i]->fpathsz);
                             }
                         }
                     }
@@ -2955,30 +2971,30 @@
         for (int i = 0; i < ofsz; ++i) assert(ofs[i] != NULL);
     }

-    if ((ejq->flags & EJQONLYCOUNT) && qfsz == 0 &&
-            (ejq->orqlist == NULL || TCLISTNUM(ejq->orqlist) < 1) &&
-            (ejq->andqlist == NULL || TCLISTNUM(ejq->andqlist) < 1)) { //primitive count(*) query
-        count = jcoll->tdb->hdb->rnum;
+    if ((q->flags & EJQONLYCOUNT) && qfsz == 0 &&
+            (q->orqlist == NULL || TCLISTNUM(q->orqlist) < 1) &&
+            (q->andqlist == NULL || TCLISTNUM(q->andqlist) < 1)) { //primitive count(*) query
+        count = coll->tdb->hdb->rnum;
         if (log) {
             tcxstrprintf(log, "SIMPLE COUNT(*): %u\n", count);
         }
         goto finish;
     }

-    if (!(ejq->flags & EJQONLYCOUNT) && aofsz > 0 && (!midx || mqf->orderseq != 1)) { //Main index is not the main order field
+    if (!(q->flags & EJQONLYCOUNT) && aofsz > 0 && (!midx || mqf->orderseq != 1)) { //Main index is not the main order field
         all = true; //Need all records for ordering for some other fields
     }

     if (log) {
-        tcxstrprintf(log, "UPDATING MODE: %s\n", (ejq->flags & EJQUPDATING) ? "YES" : "NO");
+        tcxstrprintf(log, "UPDATING MODE: %s\n", (q->flags & EJQUPDATING) ? "YES" : "NO");
         tcxstrprintf(log, "MAX: %u\n", max);
         tcxstrprintf(log, "SKIP: %u\n", skip);
-        tcxstrprintf(log, "COUNT ONLY: %s\n", (ejq->flags & EJQONLYCOUNT) ? "YES" : "NO");
+        tcxstrprintf(log, "COUNT ONLY: %s\n", (q->flags & EJQONLYCOUNT) ? "YES" : "NO");
         tcxstrprintf(log, "MAIN IDX: '%s'\n", midx ? midx->name : "NONE");
         tcxstrprintf(log, "ORDER FIELDS: %d\n", ofsz);
         tcxstrprintf(log, "ACTIVE CONDITIONS: %d\n", anum);
-        tcxstrprintf(log, "ROOT $OR QUERIES: %d\n", ((ejq->orqlist) ? TCLISTNUM(ejq->orqlist) : 0));
-        tcxstrprintf(log, "ROOT $AND QUERIES: %d\n", ((ejq->andqlist) ? TCLISTNUM(ejq->andqlist) : 0));
+        tcxstrprintf(log, "ROOT $OR QUERIES: %d\n", ((q->orqlist) ? TCLISTNUM(q->orqlist) : 0));
+        tcxstrprintf(log, "ROOT $AND QUERIES: %d\n", ((q->andqlist) ? TCLISTNUM(q->andqlist) : 0));
         tcxstrprintf(log, "FETCH ALL: %s\n", all ? "YES" : "NO");
     }
     if (max < UINT_MAX - skip) {
@@ -2996,11 +3012,11 @@

 #define JBQREGREC(_pkbuf, _pkbufsz, _bsbuf, _bsbufsz)   \
     ++count; \
-    if (ejq->flags & EJQUPDATING) { \
-        _qryupdate(jcoll, ejq, (_bsbuf), (_bsbufsz), didxctx, log); \
+    if (q->flags & EJQUPDATING) { \
+        _qryupdate(&ctx, (_bsbuf), (_bsbufsz)); \
     } \
-    if (!(ejq->flags & EJQONLYCOUNT) && (all || count > skip)) { \
-        _pushprocessedbson(jb, ejq, res, dfields, ifields, imode, (_bsbuf), (_bsbufsz)); \
+    if (!(q->flags & EJQONLYCOUNT) && (all || count > skip)) { \
+        _pushprocessedbson(&ctx, (_bsbuf), (_bsbufsz)); \
     }
     //EOF #define JBQREGREC

@@ -3019,13 +3035,13 @@
             do {
                 bson_oid_t oid;
                 bson_oid_from_string(&oid, mqf->expr);
-                tcxstrclear(ejq->colbuf);
-                tcxstrclear(ejq->bsbuf);
-                sz = tchdbgetintoxstr(jcoll->tdb->hdb, &oid, sizeof (oid), ejq->colbuf);
+                tcxstrclear(q->colbuf);
+                tcxstrclear(q->bsbuf);
+                sz = tchdbgetintoxstr(coll->tdb->hdb, &oid, sizeof (oid), q->colbuf);
                 if (sz <= 0) {
                     break;
                 }
-                sz = tcmaploadoneintoxstr(TCXSTRPTR(ejq->colbuf), TCXSTRSIZE(ejq->colbuf), JDBCOLBSON, JDBCOLBSONL, ejq->bsbuf);
+                sz = tcmaploadoneintoxstr(TCXSTRPTR(q->colbuf), TCXSTRSIZE(q->colbuf), JDBCOLBSON, JDBCOLBSONL, q->bsbuf);
                 if (sz <= 0) {
                     break;
                 }
@@ -3034,13 +3050,13 @@
                 for (int i = 0; i < qfsz; ++i) {
                     EJQF *qf = qfs[i];
                     if (qf->mflags & EJFEXCLUDED) continue;
-                    if (!_qrybsmatch(qf, TCXSTRPTR(ejq->bsbuf), TCXSTRSIZE(ejq->bsbuf))) {
+                    if (!_qrybsmatch(qf, TCXSTRPTR(q->bsbuf), TCXSTRSIZE(q->bsbuf))) {
                         matched = false;
                         break;
                     }
                 }
-                if (matched && _qry_$and_$or_match(jcoll, ejq, &oid, sizeof (oid))) {
-                    JBQREGREC(&oid, sizeof (oid), TCXSTRPTR(ejq->bsbuf), TCXSTRSIZE(ejq->bsbuf));
+                if (matched && _qry_$and_$or_match(coll, q, &oid, sizeof (oid))) {
+                    JBQREGREC(&oid, sizeof (oid), TCXSTRPTR(q->bsbuf), TCXSTRSIZE(q->bsbuf));
                 }
             } while (false);
         } else if (mqf->tcop == TDBQCSTROREQ) {
@@ -3064,13 +3080,13 @@
                     continue;
                 }
                 bson_oid_from_string(&oid, token);
-                tcxstrclear(ejq->bsbuf);
-                tcxstrclear(ejq->colbuf);
-                sz = tchdbgetintoxstr(jcoll->tdb->hdb, &oid, sizeof (oid), ejq->colbuf);
+                tcxstrclear(q->bsbuf);
+                tcxstrclear(q->colbuf);
+                sz = tchdbgetintoxstr(coll->tdb->hdb, &oid, sizeof (oid), q->colbuf);
                 if (sz <= 0) {
                     continue;
                 }
-                sz = tcmaploadoneintoxstr(TCXSTRPTR(ejq->colbuf), TCXSTRSIZE(ejq->colbuf), JDBCOLBSON, JDBCOLBSONL, ejq->bsbuf);
+                sz = tcmaploadoneintoxstr(TCXSTRPTR(q->colbuf), TCXSTRSIZE(q->colbuf), JDBCOLBSON, JDBCOLBSONL, q->bsbuf);
                 if (sz <= 0) {
                     continue;
                 }
@@ -3078,13 +3094,13 @@
                 for (int i = 0; i < qfsz; ++i) {
                     EJQF *qf = qfs[i];
                     if (qf->mflags & EJFEXCLUDED) continue;
-                    if (!_qrybsmatch(qf, TCXSTRPTR(ejq->bsbuf), TCXSTRSIZE(ejq->bsbuf))) {
+                    if (!_qrybsmatch(qf, TCXSTRPTR(q->bsbuf), TCXSTRSIZE(q->bsbuf))) {
                         matched = false;
                         break;
                     }
                 }
-                if (matched && _qry_$and_$or_match(jcoll, ejq, &oid, sizeof (oid))) {
-                    JBQREGREC(&oid, sizeof (oid), TCXSTRPTR(ejq->bsbuf), TCXSTRSIZE(ejq->bsbuf));
+                if (matched && _qry_$and_$or_match(coll, q, &oid, sizeof (oid))) {
+                    JBQREGREC(&oid, sizeof (oid), TCXSTRPTR(q->bsbuf), TCXSTRSIZE(q->bsbuf));
                 }
             }
         } else {
@@ -3100,8 +3116,8 @@
         while ((all || count < max) && (kbuf = tcbdbcurkey3(cur, &kbufsz)) != NULL) {
             if (trim) kbufsz -= 3;
             vbuf = tcbdbcurval3(cur, &vbufsz);
-            if (_qryallcondsmatch(ejq, anum, jcoll, qfs, qfsz, vbuf, vbufsz) && _qry_$and_$or_match(jcoll, ejq, vbuf, vbufsz)) {
-                JBQREGREC(vbuf, vbufsz, TCXSTRPTR(ejq->bsbuf), TCXSTRSIZE(ejq->bsbuf));
+            if (_qryallcondsmatch(q, anum, coll, qfs, qfsz, vbuf, vbufsz) && _qry_$and_$or_match(coll, q, vbuf, vbufsz)) {
+                JBQREGREC(vbuf, vbufsz, TCXSTRPTR(q->bsbuf), TCXSTRSIZE(q->bsbuf));
             }
             if (mqf->order >= 0) {
                 tcbdbcurnext(cur);
@@ -3120,8 +3136,8 @@
             if (trim) kbufsz -= 3;
             if (kbufsz == exprsz && !memcmp(kbuf, expr, exprsz)) {
                 vbuf = tcbdbcurval3(cur, &vbufsz);
-                if (_qryallcondsmatch(ejq, anum, jcoll, qfs, qfsz, vbuf, vbufsz) && _qry_$and_$or_match(jcoll, ejq, vbuf, vbufsz)) {
-                    JBQREGREC(vbuf, vbufsz, TCXSTRPTR(ejq->bsbuf), TCXSTRSIZE(ejq->bsbuf));
+                if (_qryallcondsmatch(q, anum, coll, qfs, qfsz, vbuf, vbufsz) && _qry_$and_$or_match(coll, q, vbuf, vbufsz)) {
+                    JBQREGREC(vbuf, vbufsz, TCXSTRPTR(q->bsbuf), TCXSTRSIZE(q->bsbuf));
                 }
             } else {
                 break;
@@ -3139,8 +3155,8 @@
             if (trim) kbufsz -= 3;
             if (kbufsz >= exprsz && !memcmp(kbuf, expr, exprsz)) {
                 vbuf = tcbdbcurval3(cur, &vbufsz);
-                if (_qryallcondsmatch(ejq, anum, jcoll, qfs, qfsz, vbuf, vbufsz) && _qry_$and_$or_match(jcoll, ejq, vbuf, vbufsz)) {
-                    JBQREGREC(vbuf, vbufsz, TCXSTRPTR(ejq->bsbuf), TCXSTRSIZE(ejq->bsbuf));
+                if (_qryallcondsmatch(q, anum, coll, qfs, qfsz, vbuf, vbufsz) && _qry_$and_$or_match(coll, q, vbuf, vbufsz)) {
+                    JBQREGREC(vbuf, vbufsz, TCXSTRPTR(q->bsbuf), TCXSTRSIZE(q->bsbuf));
                 }
             } else {
                 break;
@@ -3175,8 +3191,8 @@
                 if (trim) kbufsz -= 3;
                 if (kbufsz >= tsiz && !memcmp(kbuf, token, tsiz)) {
                     vbuf = tcbdbcurval3(cur, &vbufsz);
-                    if (_qryallcondsmatch(ejq, anum, jcoll, qfs, qfsz, vbuf, vbufsz) && _qry_$and_$or_match(jcoll, ejq, vbuf, vbufsz)) {
-                        JBQREGREC(vbuf, vbufsz, TCXSTRPTR(ejq->bsbuf), TCXSTRSIZE(ejq->bsbuf));
+                    if (_qryallcondsmatch(q, anum, coll, qfs, qfsz, vbuf, vbufsz) && _qry_$and_$or_match(coll, q, vbuf, vbufsz)) {
+                        JBQREGREC(vbuf, vbufsz, TCXSTRPTR(q->bsbuf), TCXSTRSIZE(q->bsbuf));
                     }
                 } else {
                     break;
@@ -3212,8 +3228,8 @@
                 if (trim) kbufsz -= 3;
                 if (kbufsz == tsiz && !memcmp(kbuf, token, tsiz)) {
                     vbuf = tcbdbcurval3(cur, &vbufsz);
-                    if (_qryallcondsmatch(ejq, anum, jcoll, qfs, qfsz, vbuf, vbufsz) && _qry_$and_$or_match(jcoll, ejq, vbuf, vbufsz)) {
-                        JBQREGREC(vbuf, vbufsz, TCXSTRPTR(ejq->bsbuf), TCXSTRSIZE(ejq->bsbuf));
+                    if (_qryallcondsmatch(q, anum, coll, qfs, qfsz, vbuf, vbufsz) && _qry_$and_$or_match(coll, q, vbuf, vbufsz)) {
+                        JBQREGREC(vbuf, vbufsz, TCXSTRPTR(q->bsbuf), TCXSTRSIZE(q->bsbuf));
                     }
                 } else {
                     break;
@@ -3233,8 +3249,8 @@
         while ((all || count < max) && (kbuf = tcbdbcurkey3(cur, &kbufsz)) != NULL) {
             if (_nucmp(&num, kbuf, mqf->ftype) == 0) {
                 vbuf = tcbdbcurval3(cur, &vbufsz);
-                if (_qryallcondsmatch(ejq, anum, jcoll, qfs, qfsz, vbuf, vbufsz) && _qry_$and_$or_match(jcoll, ejq, vbuf, vbufsz)) {
-                    JBQREGREC(vbuf, vbufsz, TCXSTRPTR(ejq->bsbuf), TCXSTRSIZE(ejq->bsbuf));
+                if (_qryallcondsmatch(q, anum, coll, qfs, qfsz, vbuf, vbufsz) && _qry_$and_$or_match(coll, q, vbuf, vbufsz)) {
+                    JBQREGREC(vbuf, vbufsz, TCXSTRPTR(q->bsbuf), TCXSTRSIZE(q->bsbuf));
                 }
             } else {
                 break;
@@ -3259,8 +3275,8 @@
                 if (cmp < 0) break;
                 if (cmp > 0 || (mqf->tcop == TDBQCNUMGE && cmp >= 0)) {
                     vbuf = tcbdbcurval3(cur, &vbufsz);
-                    if (_qryallcondsmatch(ejq, anum, jcoll, qfs, qfsz, vbuf, vbufsz) && _qry_$and_$or_match(jcoll, ejq, vbuf, vbufsz)) {
-                        JBQREGREC(vbuf, vbufsz, TCXSTRPTR(ejq->bsbuf), TCXSTRSIZE(ejq->bsbuf));
+                    if (_qryallcondsmatch(q, anum, coll, qfs, qfsz, vbuf, vbufsz) && _qry_$and_$or_match(coll, q, vbuf, vbufsz)) {
+                        JBQREGREC(vbuf, vbufsz, TCXSTRPTR(q->bsbuf), TCXSTRSIZE(q->bsbuf));
                     }
                 }
                 tcbdbcurprev(cur);
@@ -3273,8 +3289,8 @@
                 int cmp = _nucmp2(&knum, &xnum, mqf->ftype);
                 if (cmp > 0 || (mqf->tcop == TDBQCNUMGE && cmp >= 0)) {
                     vbuf = tcbdbcurval3(cur, &vbufsz);
-                    if (_qryallcondsmatch(ejq, anum, jcoll, qfs, qfsz, vbuf, vbufsz) && _qry_$and_$or_match(jcoll, ejq, vbuf, vbufsz)) {
-                        JBQREGREC(vbuf, vbufsz, TCXSTRPTR(ejq->bsbuf), TCXSTRSIZE(ejq->bsbuf));
+                    if (_qryallcondsmatch(q, anum, coll, qfs, qfsz, vbuf, vbufsz) && _qry_$and_$or_match(coll, q, vbuf, vbufsz)) {
+                        JBQREGREC(vbuf, vbufsz, TCXSTRPTR(q->bsbuf), TCXSTRSIZE(q->bsbuf));
                     }
                 }
                 tcbdbcurnext(cur);
@@ -3298,8 +3314,8 @@
                 if (cmp > 0) break;
                 if (cmp < 0 || (cmp <= 0 && mqf->tcop == TDBQCNUMLE)) {
                     vbuf = tcbdbcurval3(cur, &vbufsz);
-                    if (_qryallcondsmatch(ejq, anum, jcoll, qfs, qfsz, vbuf, vbufsz) && _qry_$and_$or_match(jcoll, ejq, vbuf, vbufsz)) {
-                        JBQREGREC(vbuf, vbufsz, TCXSTRPTR(ejq->bsbuf), TCXSTRSIZE(ejq->bsbuf));
+                    if (_qryallcondsmatch(q, anum, coll, qfs, qfsz, vbuf, vbufsz) && _qry_$and_$or_match(coll, q, vbuf, vbufsz)) {
+                        JBQREGREC(vbuf, vbufsz, TCXSTRPTR(q->bsbuf), TCXSTRSIZE(q->bsbuf));
                     }
                 }
                 tcbdbcurnext(cur);
@@ -3312,8 +3328,8 @@
                 int cmp = _nucmp2(&knum, &xnum, mqf->ftype);
                 if (cmp < 0 || (cmp <= 0 && mqf->tcop == TDBQCNUMLE)) {
                     vbuf = tcbdbcurval3(cur, &vbufsz);
-                    if (_qryallcondsmatch(ejq, anum, jcoll, qfs, qfsz, vbuf, vbufsz) && _qry_$and_$or_match(jcoll, ejq, vbuf, vbufsz)) {
-                        JBQREGREC(vbuf, vbufsz, TCXSTRPTR(ejq->bsbuf), TCXSTRSIZE(ejq->bsbuf));
+                    if (_qryallcondsmatch(q, anum, coll, qfs, qfsz, vbuf, vbufsz) && _qry_$and_$or_match(coll, q, vbuf, vbufsz)) {
+                        JBQREGREC(vbuf, vbufsz, TCXSTRPTR(q->bsbuf), TCXSTRSIZE(q->bsbuf));
                     }
                 }
                 tcbdbcurprev(cur);
@@ -3342,13 +3358,13 @@
         while ((all || count < max) && (kbuf = tcbdbcurkey3(cur, &kbufsz)) != NULL) {
             if (tcatof2(kbuf) > upper) break;
             vbuf = tcbdbcurval3(cur, &vbufsz);
-            if (_qryallcondsmatch(ejq, anum, jcoll, qfs, qfsz, vbuf, vbufsz) && _qry_$and_$or_match(jcoll, ejq, vbuf, vbufsz)) {
-                JBQREGREC(vbuf, vbufsz, TCXSTRPTR(ejq->bsbuf), TCXSTRSIZE(ejq->bsbuf));
+            if (_qryallcondsmatch(q, anum, coll, qfs, qfsz, vbuf, vbufsz) && _qry_$and_$or_match(coll, q, vbuf, vbufsz)) {
+                JBQREGREC(vbuf, vbufsz, TCXSTRPTR(q->bsbuf), TCXSTRSIZE(q->bsbuf));
             }
             tcbdbcurnext(cur);
         }
         tcbdbcurdel(cur);
-        if (!all && !(ejq->flags & EJQONLYCOUNT) && mqf->order < 0 && (mqf->flags & EJFORDERUSED)) { //DESC
+        if (!all && !(q->flags & EJQONLYCOUNT) && mqf->order < 0 && (mqf->flags & EJFORDERUSED)) { //DESC
             tclistinvert(res);
         }
     } else if (mqf->tcop == TDBQCNUMOREQ) { /* number is equal to at least one token in */
@@ -3378,8 +3394,8 @@
             while ((all || count < max) && (kbuf = tcbdbcurkey3(cur, &kbufsz)) != NULL) {
                 if (tcatof2(kbuf) == xnum) {
                     vbuf = tcbdbcurval3(cur, &vbufsz);
-                    if (_qryallcondsmatch(ejq, anum, jcoll, qfs, qfsz, vbuf, vbufsz) && _qry_$and_$or_match(jcoll, ejq, vbuf, vbufsz)) {
-                        JBQREGREC(vbuf, vbufsz, TCXSTRPTR(ejq->bsbuf), TCXSTRSIZE(ejq->bsbuf));
+                    if (_qryallcondsmatch(q, anum, coll, qfs, qfsz, vbuf, vbufsz) && _qry_$and_$or_match(coll, q, vbuf, vbufsz)) {
+                        JBQREGREC(vbuf, vbufsz, TCXSTRPTR(q->bsbuf), TCXSTRSIZE(q->bsbuf));
                     }
                 } else {
                     break;
@@ -3411,17 +3427,17 @@
                 }
             }
         }
-        TCMAP *tres = tctdbidxgetbytokens(jcoll->tdb, midx, tokens, mqf->tcop, log);
+        TCMAP *tres = tctdbidxgetbytokens(coll->tdb, midx, tokens, mqf->tcop, log);
         tcmapiterinit(tres);
         while ((all || count < max) && (kbuf = tcmapiternext(tres, &kbufsz)) != NULL) {
-            if (_qryallcondsmatch(ejq, anum, jcoll, qfs, qfsz, kbuf, kbufsz) && _qry_$and_$or_match(jcoll, ejq, kbuf, kbufsz)) {
-                JBQREGREC(kbuf, kbufsz, TCXSTRPTR(ejq->bsbuf), TCXSTRSIZE(ejq->bsbuf));
+            if (_qryallcondsmatch(q, anum, coll, qfs, qfsz, kbuf, kbufsz) && _qry_$and_$or_match(coll, q, kbuf, kbufsz)) {
+                JBQREGREC(kbuf, kbufsz, TCXSTRPTR(q->bsbuf), TCXSTRSIZE(q->bsbuf));
             }
         }
         tcmapdel(tres);
     }

-    if (ejq->flags & EJQONLYCOUNT) {
+    if (q->flags & EJQONLYCOUNT) {
         goto finish;
     } else {
         goto sorting;
@@ -3431,15 +3447,15 @@
     assert(count == 0);
     assert(!res || TCLISTNUM(res) == 0);

-    if ((ejq->flags & EJQDROPALL) && (ejq->flags & EJQONLYCOUNT)) {
+    if ((q->flags & EJQDROPALL) && (q->flags & EJQONLYCOUNT)) {
         //if we are in primitive $dropall case. Query: {$dropall:true}
         if (qfsz == 1 && qfs[0]->tcop == TDBQTRUE) { //single $dropall field
             if (log) {
                 tcxstrprintf(log, "VANISH WHOLE COLLECTION ON $dropall\n");
             }
             //write lock already acquired so use impl
-            count = jcoll->tdb->hdb->rnum;
-            if (!tctdbvanish(jcoll->tdb)) {
+            count = coll->tdb->hdb->rnum;
+            if (!tctdbvanish(coll->tdb)) {
                 count = 0;
             }
             goto finish;
@@ -3449,18 +3465,18 @@
     if (log) {
         tcxstrprintf(log, "RUN FULLSCAN\n");
     }
-    TCMAP *updkeys = (ejq->flags & EJQUPDATING) ? tcmapnew2(100 * 1024) : NULL;
+    TCMAP *updkeys = (q->flags & EJQUPDATING) ? tcmapnew2(100 * 1024) : NULL;
     TCHDBITER *hdbiter = tchdbiter2init(hdb);
     if (!hdbiter) {
         goto finish;
     }
     TCXSTR *skbuf = tcxstrnew3(sizeof (bson_oid_t) + 1);
-    tcxstrclear(ejq->colbuf);
-    tcxstrclear(ejq->bsbuf);
+    tcxstrclear(q->colbuf);
+    tcxstrclear(q->bsbuf);
     int rows = 0;
-    while ((all || count < max) && tchdbiter2next(hdb, hdbiter, skbuf, ejq->colbuf)) {
+    while ((all || count < max) && tchdbiter2next(hdb, hdbiter, skbuf, q->colbuf)) {
         ++rows;
-        sz = tcmaploadoneintoxstr(TCXSTRPTR(ejq->colbuf), TCXSTRSIZE(ejq->colbuf), JDBCOLBSON, JDBCOLBSONL, ejq->bsbuf);
+        sz = tcmaploadoneintoxstr(TCXSTRPTR(q->colbuf), TCXSTRSIZE(q->colbuf), JDBCOLBSON, JDBCOLBSONL, q->bsbuf);
         if (sz <= 0) {
             goto wfinish;
         }
@@ -3471,24 +3487,24 @@
             if (qf->mflags & EJFEXCLUDED) {
                 continue;
             }
-            if (!_qrybsmatch(qf, TCXSTRPTR(ejq->bsbuf), TCXSTRSIZE(ejq->bsbuf))) {
+            if (!_qrybsmatch(qf, TCXSTRPTR(q->bsbuf), TCXSTRSIZE(q->bsbuf))) {
                 matched = false;
                 break;
             }
         }
-        if (matched && _qry_$and_$or_match(jcoll, ejq, TCXSTRPTR(skbuf), TCXSTRSIZE(skbuf))) {
+        if (matched && _qry_$and_$or_match(coll, q, TCXSTRPTR(skbuf), TCXSTRSIZE(skbuf))) {
             if (updkeys) { //we are in updating mode
                 if (tcmapputkeep(updkeys, TCXSTRPTR(skbuf), TCXSTRSIZE(skbuf), &yes, sizeof (yes))) {
-                    JBQREGREC(TCXSTRPTR(skbuf), TCXSTRSIZE(skbuf), TCXSTRPTR(ejq->bsbuf), TCXSTRSIZE(ejq->bsbuf));
+                    JBQREGREC(TCXSTRPTR(skbuf), TCXSTRSIZE(skbuf), TCXSTRPTR(q->bsbuf), TCXSTRSIZE(q->bsbuf));
                 }
             } else {
-                JBQREGREC(TCXSTRPTR(skbuf), TCXSTRSIZE(skbuf), TCXSTRPTR(ejq->bsbuf), TCXSTRSIZE(ejq->bsbuf));
+                JBQREGREC(TCXSTRPTR(skbuf), TCXSTRSIZE(skbuf), TCXSTRPTR(q->bsbuf), TCXSTRSIZE(q->bsbuf));
             }
         }
 wfinish:
         tcxstrclear(skbuf);
-        tcxstrclear(ejq->colbuf);
-        tcxstrclear(ejq->bsbuf);
+        tcxstrclear(q->colbuf);
+        tcxstrclear(q->bsbuf);
     }
     tchdbiter2dispose(hdb, hdbiter);
     tcxstrdel(skbuf);
@@ -3507,13 +3523,13 @@

 finish:
     //check $upsert operation
-    if (count == 0 && (ejq->flags & EJQUPDATING)) { //finding $upsert qf if no updates maden
+    if (count == 0 && (q->flags & EJQUPDATING)) { //finding $upsert qf if no updates maden
         for (int i = 0; i < qfsz; ++i) {
             if (qfs[i]->flags & EJCONDUPSERT) {
                 bson *updateobj = qfs[i]->updateobj;
                 assert(updateobj);
                 bson_oid_t oid;
-                if (_ejdbsavebsonimpl(jcoll, updateobj, &oid, false)) {
+                if (_ejdbsavebsonimpl(coll, updateobj, &oid, false)) {
                     bson *nbs = bson_create();
                     bson_init_size(nbs, bson_size(updateobj) + (strlen(JDBIDKEYNAME) + 1/*key*/ + 1/*type*/ + sizeof (oid)));
                     bson_append_oid(nbs, JDBIDKEYNAME, &oid);
@@ -3521,12 +3537,12 @@
                     bson_append(nbs, bson_data(updateobj) + 4, bson_size(updateobj) - (4 + 1/*BSON_EOO*/));
                     bson_finish(nbs);
                     if (nbs->err) {
-                        _ejdbsetecode(jcoll->jb, JBEINVALIDBSON, __FILE__, __LINE__, __func__);
+                        _ejdbsetecode(coll->jb, JBEINVALIDBSON, __FILE__, __LINE__, __func__);
                         break;
                     }
                     ++count;
-                    if (!(ejq->flags & EJQONLYCOUNT) && (all || count > skip)) {
-                        _pushprocessedbson(jb, ejq, res, dfields, ifields, imode, bson_data(nbs), bson_size(nbs));
+                    if (!(q->flags & EJQONLYCOUNT) && (all || count > skip)) {
+                        _pushprocessedbson(&ctx, bson_data(nbs), bson_size(nbs));
                     }
                     bson_del(nbs);
                 }
@@ -3568,26 +3584,24 @@
     if (log) {
         tcxstrprintf(log, "RS COUNT: %u\n", count);
         tcxstrprintf(log, "RS SIZE: %d\n", (res ? TCLISTNUM(res) : 0));
-        tcxstrprintf(log, "FINAL SORTING: %s\n", ((ejq->flags & EJQONLYCOUNT) || aofsz <= 0) ? "NO" : "YES");
+        tcxstrprintf(log, "FINAL SORTING: %s\n", ((q->flags & EJQONLYCOUNT) || aofsz <= 0) ? "NO" : "YES");
     }

     //Apply deffered index changes
-    if (didxctx) {
-        for (int i = TCLISTNUM(didxctx) - 1; i >= 0; --i) {
-            _DEFFEREDIDXCTX *di = TCLISTVALPTR(didxctx, i);
+    if (ctx.didxctx) {
+        for (int i = TCLISTNUM(ctx.didxctx) - 1; i >= 0; --i) {
+            _DEFFEREDIDXCTX *di = TCLISTVALPTR(ctx.didxctx, i);
             assert(di);
             if (di->rmap) {
-                tctdbidxout2(jcoll->tdb, &(di->oid), sizeof (di->oid), di->rmap);
+                tctdbidxout2(coll->tdb, &(di->oid), sizeof (di->oid), di->rmap);
                 tcmapdel(di->rmap);
             }
             if (di->imap) {
-                tctdbidxput2(jcoll->tdb, &(di->oid), sizeof (di->oid), di->imap);
+                tctdbidxput2(coll->tdb, &(di->oid), sizeof (di->oid), di->imap);
                 tcmapdel(di->imap);
             }
         }
-        tclistdel(didxctx);
-    }
-
+    }
     //Cleanup
     if (qfs) {
         TCFREE(qfs);
@@ -3595,19 +3609,33 @@
     if (ofs) {
         TCFREE(ofs);
     }
-    if (dfields) {
-        tcmapdel(dfields);
-    }
-    if (ifields) {
-        tcmapdel(ifields);
-    }
-    ejdbquerydel(ejq);
+    ctx.res = NULL; //save res from deleting in `_qryctxclear()`
+    _qryctxclear(&ctx);
 #undef JBQREGREC
     return res;
 }

-static TDBIDX* _qryfindidx(EJCOLL *jcoll, EJQF *qf, bson *idxmeta) {
-    TCTDB *tdb = jcoll->tdb;
+static void _qryctxclear(_QRYCTX *ctx) {
+    if (ctx->dfields) {
+        tcmapdel(ctx->dfields);
+    }
+    if (ctx->ifields) {
+        tcmapdel(ctx->ifields);
+    }
+    if (ctx->q) {
+        ejdbquerydel(ctx->q);
+    }
+    if (ctx->res) {
+        tclistdel(ctx->res);
+    }
+    if (ctx->didxctx) {
+        tclistdel(ctx->didxctx);
+    }
+    memset(ctx, 0, sizeof(*ctx));
+}
+
+static TDBIDX* _qryfindidx(EJCOLL *coll, EJQF *qf, bson *idxmeta) {
+    TCTDB *tdb = coll->tdb;
     char p = '\0';
     switch (qf->tcop) {
         case TDBQCSTREQ:
@@ -3701,29 +3729,22 @@
     return NULL;
 }

-static bool _qrypreprocess(EJCOLL *jcoll, EJQ *ejq, int qflags, EJQF **mqf,
-        TCMAP **dfields, TCMAP **ifields, bool *imode) {
-    assert(jcoll && ejq && ejq->qflist && mqf);
-
-    if (qflags & JBQRYCOUNT) {
-        ejq->flags |= EJQONLYCOUNT;
-    }
-
-    *imode = false;
-    *ifields = NULL;
-    *dfields = NULL;
-    *mqf = NULL;
-
+static bool _qrypreprocess(_QRYCTX *ctx) {
+    assert(ctx->coll && ctx->q && ctx->q->qflist);
+    EJQ *q = ctx->q;
+    if (ctx->qflags & JBQRYCOUNT) {
+        q->flags |= EJQONLYCOUNT;
+    }
     EJQF *oqf = NULL; //Order condition
-    TCLIST *qflist = ejq->qflist;
+    TCLIST *qflist = q->qflist;
     bson_iterator it;
     bson_type bt;

-    if (ejq->hints) {
+    if (q->hints) {
         bson_type bt;
         bson_iterator it, sit;
         //Process $orderby
-        bt = bson_find(&it, ejq->hints, "$orderby");
+        bt = bson_find(&it, q->hints, "$orderby");
         if (bt == BSON_OBJECT) {
             int orderseq = 1;
             BSON_ITERATOR_SUBITERATOR(&it, &sit);
@@ -3765,20 +3786,20 @@
                 }
             }
         }
-        bt = bson_find(&it, ejq->hints, "$skip");
+        bt = bson_find(&it, q->hints, "$skip");
         if (BSON_IS_NUM_TYPE(bt)) {
             int64_t v = bson_iterator_long(&it);
-            ejq->skip = (uint32_t) ((v < 0) ? 0 : v);
-        }
-        bt = bson_find(&it, ejq->hints, "$max");
-        if (qflags & JBQRYFINDONE) {
-            ejq->max = (uint32_t) 1;
+            q->skip = (uint32_t) ((v < 0) ? 0 : v);
+        }
+        bt = bson_find(&it, q->hints, "$max");
+        if (ctx->qflags & JBQRYFINDONE) {
+            q->max = (uint32_t) 1;
         } else if (BSON_IS_NUM_TYPE(bt)) {
             int64_t v = bson_iterator_long(&it);
-            ejq->max = (uint32_t) ((v < 0) ? 0 : v);
-        }
-        if (!(qflags & JBQRYCOUNT)) {
-            bt = bson_find(&it, ejq->hints, "$fields"); //Collect required fields
+            q->max = (uint32_t) ((v < 0) ? 0 : v);
+        }
+        if (!(ctx->qflags & JBQRYCOUNT)) {
+            bt = bson_find(&it, q->hints, "$fields"); //Collect required fields
             if (bt == BSON_OBJECT) {
                 TCMAP *fmap = tcmapnew2(TCMAPTINYBNUM);
                 BSON_ITERATOR_SUBITERATOR(&it, &sit);
@@ -3787,12 +3808,12 @@
                         continue;
                     }
                     bool inc = (bson_iterator_int(&sit) > 0 ? true : false);
-                    if (i > 0 && inc != *imode) { //$fields hint cannot mix include and exclude fields
+                    if (i > 0 && inc != ctx->imode) { //$fields hint cannot mix include and exclude fields
                         tcmapdel(fmap);
-                        _ejdbsetecode(jcoll->jb, JBEQINCEXCL, __FILE__, __LINE__, __func__);
+                        _ejdbsetecode(ctx->coll->jb, JBEQINCEXCL, __FILE__, __LINE__, __func__);
                         return false;
                     }
-                    *imode = inc;
+                    ctx->imode = inc;
                     const char *key = BSON_ITERATOR_KEY(&sit);
                     char *pptr;
                     //Checking the $(projection) operator.
@@ -3809,10 +3830,10 @@
                             int j;
                             for (j = 0; *(key + j) != '\0' && *(key + j) == *(qf->fpath + j); ++j);
                             if (key + j == pptr || key + j == pptr + 1) { //existing QF matched the $(projection) prefix
-                                if (!ejq->$ifields) {
-                                    ejq->$ifields = tcmapnew2(TCMAPTINYBNUM);
+                                if (!q->$ifields) {
+                                    q->$ifields = tcmapnew2(TCMAPTINYBNUM);
                                 }
-                                tcmapput(ejq->$ifields, qf->fpath, qf->fpathsz, key, strlen(key));
+                                tcmapput(q->$ifields, qf->fpath, qf->fpathsz, key, strlen(key));
                                 break;
                             }
                         }
@@ -3821,10 +3842,10 @@
                     tcmapputkeep(fmap, key, strlen(key), &yes, sizeof (yes));
                 }
                 if (TCMAPRNUM(fmap) == 0) { //if {$fields : {}} we will force {$fields : {_id:1}}
-                    *imode = true;
+                    ctx->imode = true;
                     tcmapputkeep(fmap, JDBIDKEYNAME, JDBIDKEYNAMEL, &yes, sizeof (yes));
                 }
-                *ifields = fmap;
+                ctx->ifields = fmap;
             }
         }
     } //eof hints
@@ -3849,10 +3870,10 @@
         assert(qf && qf->fpath);

         if (qf->flags & EJCONDOIT) { //$do field
-            TCMAP *dmap = *dfields;
+            TCMAP *dmap = ctx->dfields;
             if (!dmap) {
                 dmap = tcmapnew2(TCMAPTINYBNUM);
-                *dfields = dmap;
+                ctx->dfields = dmap;
             }
             tcmapputkeep(dmap, qf->fpath, qf->fpathsz, qf, sizeof (*qf));
         }
@@ -3863,13 +3884,13 @@
         //OID PK matching
         if (!qf->negate && (qf->tcop == TDBQCSTREQ || qf->tcop == TDBQCSTROREQ) && !strcmp(JDBIDKEYNAME, qf->fpath)) {
             qf->flags |= EJFPKMATCHING;
-            *mqf = qf;
+            ctx->mqf = qf;
             break;
         }

         bool firstorderqf = false;
-        qf->idxmeta = _imetaidx(jcoll, qf->fpath);
-        qf->idx = _qryfindidx(jcoll, qf, qf->idxmeta);
+        qf->idxmeta = _imetaidx(ctx->coll, qf->fpath);
+        qf->idx = _qryfindidx(ctx->coll, qf, qf->idxmeta);
         if (qf->order && qf->orderseq == 1) { //Index for first 'orderby' exists
             oqf = qf;
             firstorderqf = true;
@@ -3929,12 +3950,12 @@
                 break;
         }
         if (iscore >= maxiscore) {
-            *mqf = qf;
+            ctx->mqf = qf;
             maxiscore = iscore;
         }
     }
-    if (*mqf == NULL && (oqf && oqf->idx && !oqf->negate)) {
-        *mqf = oqf;
+    if (ctx->mqf == NULL && (oqf && oqf->idx && !oqf->negate)) {
+        ctx->mqf = oqf;
     }

 //    //Now reorg qf->$ufields #91
@@ -3966,11 +3987,12 @@
 //    }

     //Init query processing tctdb column & bson buffers
-    assert(!ejq->colbuf && !ejq->bsbuf);
-    ejq->colbuf = tcxstrnew3(1024);
-    ejq->bsbuf = tcxstrnew3(1024);
-    ejq->tmpbuf = tcxstrnew();
-
+    assert(!q->colbuf && !q->bsbuf);
+    q->colbuf = tcxstrnew3(1024);
+    q->bsbuf = tcxstrnew3(1024);
+    q->tmpbuf = tcxstrnew();
+    ctx->didxctx = (q->flags & EJQUPDATING) ? tclistnew() : NULL;
+    ctx->res = (q->flags & EJQONLYCOUNT) ? NULL : tclistnew2(4096);
     return true;
 }

@@ -4072,9 +4094,9 @@
     return rv;
 }

-static bool _metasetbson2(EJCOLL *jcoll, const char *mkey, bson *val, bool merge, bool mergeoverwrt) {
-    assert(jcoll);
-    return _metasetbson(jcoll->jb, jcoll->cname, jcoll->cnamesz, mkey, val, merge, mergeoverwrt);
+static bool _metasetbson2(EJCOLL *coll, const char *mkey, bson *val, bool merge, bool mergeoverwrt) {
+    assert(coll);
+    return _metasetbson(coll->jb, coll->cname, coll->cnamesz, mkey, val, merge, mergeoverwrt);
 }

 /**Returned meta BSON data must be freed by 'bson_del' */
@@ -4101,27 +4123,27 @@
     return rv;
 }

-static bson* _metagetbson2(EJCOLL *jcoll, const char *mkey) {
-    assert(jcoll);
-    return _metagetbson(jcoll->jb, jcoll->cname, jcoll->cnamesz, mkey);
+static bson* _metagetbson2(EJCOLL *coll, const char *mkey) {
+    assert(coll);
+    return _metagetbson(coll->jb, coll->cname, coll->cnamesz, mkey);
 }

 /**Returned index meta if not NULL it must be freed by 'bson_del' */
-static bson* _imetaidx(EJCOLL *jcoll, const char *ipath) {
-    assert(jcoll && ipath);
+static bson* _imetaidx(EJCOLL *coll, const char *ipath) {
+    assert(coll && ipath);
     if (*ipath == '\0') {
         return NULL;
     }
     bson *rv = NULL;
     char fpathkey[BSON_MAX_FPATH_LEN + 1];
-    TCMAP *cmeta = tctdbget(jcoll->jb->metadb, jcoll->cname, jcoll->cnamesz);
+    TCMAP *cmeta = tctdbget(coll->jb->metadb, coll->cname, coll->cnamesz);
     if (!cmeta) {
-        _ejdbsetecode(jcoll->jb, JBEMETANVALID, __FILE__, __LINE__, __func__);
+        _ejdbsetecode(coll->jb, JBEMETANVALID, __FILE__, __LINE__, __func__);
         goto finish;
     }
     int klen = snprintf(fpathkey, BSON_MAX_FPATH_LEN + 1, "i%s", ipath); //'i' prefix for all columns with index meta
     if (klen > BSON_MAX_FPATH_LEN) {
-        _ejdbsetecode(jcoll->jb, JBEFPATHINVALID, __FILE__, __LINE__, __func__);
+        _ejdbsetecode(coll->jb, JBEFPATHINVALID, __FILE__, __LINE__, __func__);
         goto finish;
     }
     int bsz;
@@ -4171,7 +4193,7 @@
     }
 }

-static bool _ejdbsavebsonimpl(EJCOLL *jcoll, bson *bs, bson_oid_t *oid, bool merge) {
+static bool _ejdbsavebsonimpl(EJCOLL *coll, bson *bs, bson_oid_t *oid, bool merge) {
     bool rv = false;
     bson *nbs = NULL;
     bson_type oidt = _bsonoidkey(bs, oid);
@@ -4186,10 +4208,10 @@
         assert(!nbs->err);
         bs = nbs;
     } else if (oidt != BSON_OID) { //_oid presented by it is not BSON_OID
-        _ejdbsetecode(jcoll->jb, JBEINVALIDBSONPK, __FILE__, __LINE__, __func__);
+        _ejdbsetecode(coll->jb, JBEINVALIDBSONPK, __FILE__, __LINE__, __func__);
         return false;
     }
-    TCTDB *tdb = jcoll->tdb;
+    TCTDB *tdb = coll->tdb;
     TCMAP *rowm = (tdb->hdb->rnum > 0) ? tctdbget(tdb, oid, sizeof (*oid)) : NULL;
     char *obsdata = NULL; //Old bson
     int obsdatasz = 0;
@@ -4215,7 +4237,7 @@
         goto finish;
     }
     //Update indexes
-    rv = _updatebsonidx(jcoll, oid, bs, obsdata, obsdatasz, NULL);
+    rv = _updatebsonidx(coll, oid, bs, obsdata, obsdatasz, NULL);
 finish:
     if (rowm) {
         tcmapdel(rowm);
@@ -4937,7 +4959,7 @@
 static char* _bsonfpathrowldr(TCLIST *tokens, const char *rowdata, int rowdatasz,
         const char *fpath, int fpathsz, void *op, int *vsz) {
     _BSONIPATHROWLDR *odata = (_BSONIPATHROWLDR*) op;
-    assert(odata && odata->jcoll);
+    assert(odata && odata->coll);
     char *ret = NULL;
     int bsize;
     bson_iterator it;
@@ -4948,17 +4970,17 @@
     }
     BSON_ITERATOR_FROM_BUFFER(&it, bsdata);
     bson_find_fieldpath_value2(fpath, fpathsz, &it);
-    ret = _bsonitstrval(odata->jcoll->jb, &it, vsz, tokens, (odata->icase ? JBICASE : 0));
+    ret = _bsonitstrval(odata->coll->jb, &it, vsz, tokens, (odata->icase ? JBICASE : 0));
     TCFREE(bsdata);
     return ret;
 }

-static bool _updatebsonidx(EJCOLL *jcoll, const bson_oid_t *oid, const bson *bs,
+static bool _updatebsonidx(EJCOLL *coll, const bson_oid_t *oid, const bson *bs,
         const void *obsdata, int obsdatasz, TCLIST *dlist) {
     bool rv = true;
-    TCMAP *cmeta = tctdbget(jcoll->jb->metadb, jcoll->cname, jcoll->cnamesz);
+    TCMAP *cmeta = tctdbget(coll->jb->metadb, coll->cname, coll->cnamesz);
     if (!cmeta) {
-        _ejdbsetecode(jcoll->jb, JBEMETANVALID, __FILE__, __LINE__, __func__);
+        _ejdbsetecode(coll->jb, JBEMETANVALID, __FILE__, __LINE__, __func__);
         return false;
     }
     TCMAP *imap = NULL; //New index map
@@ -4996,7 +5018,7 @@
             BSON_ITERATOR_FROM_BUFFER(&oit, obsdata);
             oft = bson_find_fieldpath_value2(mkey + 1, mkeysz - 1, &oit);
             TCLIST *tokens = (oft == BSON_ARRAY || (oft == BSON_STRING && (iflags & JBIDXARR))) ? tclistnew() : NULL;
-            ofvalue = BSON_IS_IDXSUPPORTED_TYPE(oft) ? _bsonitstrval(jcoll->jb, &oit, &ofvaluesz, tokens, textflags) : NULL;
+            ofvalue = BSON_IS_IDXSUPPORTED_TYPE(oft) ? _bsonitstrval(coll->jb, &oit, &ofvaluesz, tokens, textflags) : NULL;
             if (tokens) {
                 oft = BSON_ARRAY;
                 ofvalue = tclistdump(tokens, &ofvaluesz);
@@ -5007,7 +5029,7 @@
             BSON_ITERATOR_INIT(&fit, bs);
             ft = bson_find_fieldpath_value2(mkey + 1, mkeysz - 1, &fit);
             TCLIST *tokens = (ft == BSON_ARRAY || (ft == BSON_STRING && (iflags & JBIDXARR))) ? tclistnew() : NULL;
-            fvalue = BSON_IS_IDXSUPPORTED_TYPE(ft) ? _bsonitstrval(jcoll->jb, &fit, &fvaluesz, tokens, textflags) : NULL;
+            fvalue = BSON_IS_IDXSUPPORTED_TYPE(ft) ? _bsonitstrval(coll->jb, &fit, &fvaluesz, tokens, textflags) : NULL;
             if (tokens) {
                 ft = BSON_ARRAY;
                 fvalue = tclistdump(tokens, &fvaluesz);
@@ -5072,35 +5094,35 @@
                 _DEFFEREDIDXCTX *di = TCLISTVALPTR(dlist, i);
                 assert(di);
                 if (di->rmap) {
-                    tctdbidxout2(jcoll->tdb, &(di->oid), sizeof (di->oid), di->rmap);
+                    tctdbidxout2(coll->tdb, &(di->oid), sizeof (di->oid), di->rmap);
                     tcmapdel(di->rmap);
                 }
                 if (di->imap) {
-                    tctdbidxput2(jcoll->tdb, &(di->oid), sizeof (di->oid), di->imap);
+                    tctdbidxput2(coll->tdb, &(di->oid), sizeof (di->oid), di->imap);
                     tcmapdel(di->imap);
                 }
             }
             TCLISTTRUNC(dlist, 0);
         }
     } else { //apply index changes immediately
-        if (rimap && !tctdbidxout2(jcoll->tdb, oid, sizeof (*oid), rimap)) rv = false;
-        if (imap && !tctdbidxput2(jcoll->tdb, oid, sizeof (*oid), imap)) rv = false;
+        if (rimap && !tctdbidxout2(coll->tdb, oid, sizeof (*oid), rimap)) rv = false;
+        if (imap && !tctdbidxput2(coll->tdb, oid, sizeof (*oid), imap)) rv = false;
     }
     if (imap) tcmapdel(imap);
     if (rimap) tcmapdel(rimap);
     return rv;
 }

-static void _delcoldb(EJCOLL *jcoll) {
-    assert(jcoll);
-    tctdbdel(jcoll->tdb);
-    jcoll->tdb = NULL;
-    jcoll->jb = NULL;
-    jcoll->cnamesz = 0;
-    TCFREE(jcoll->cname);
-    if (jcoll->mmtx) {
-        pthread_rwlock_destroy(jcoll->mmtx);
-        TCFREE(jcoll->mmtx);
+static void _delcoldb(EJCOLL *coll) {
+    assert(coll);
+    tctdbdel(coll->tdb);
+    coll->tdb = NULL;
+    coll->jb = NULL;
+    coll->cnamesz = 0;
+    TCFREE(coll->cname);
+    if (coll->mmtx) {
+        pthread_rwlock_destroy(coll->mmtx);
+        TCFREE(coll->mmtx);
     }
 }

@@ -5119,17 +5141,17 @@
         *res = NULL;
         return rv;
     }
-    EJCOLL *jcoll;
-    TCCALLOC(jcoll, 1, sizeof (*jcoll));
-    jb->cdbs[i] = jcoll;
+    EJCOLL *coll;
+    TCCALLOC(coll, 1, sizeof (*coll));
+    jb->cdbs[i] = coll;
     ++jb->cdbsnum;
-    jcoll->cname = tcstrdup(cname);
-    jcoll->cnamesz = strlen(cname);
-    jcoll->tdb = cdb;
-    jcoll->jb = jb;
-    jcoll->mmtx = NULL;
-    _ejdbcolsetmutex(jcoll);
-    *res = jcoll;
+    coll->cname = tcstrdup(cname);
+    coll->cnamesz = strlen(cname);
+    coll->tdb = cdb;
+    coll->jb = jb;
+    coll->mmtx = NULL;
+    _ejdbcolsetmutex(coll);
+    *res = coll;
     return rv;
 }


Further standards compliance fixes: export LONG_BIT, WORD_BIT, and
NL_{ARG,LANG,MSG,SET,TEXT}MAX with newer POSIX.
Hide {FLT,DBL}_{DIG,MAX,MIN} and {PASS,TMP,NL_N}_MAX with newer XPG.
Make _POSIX_NGROUPS_MAX and _POSIX_OPEN_MAX conditional on the POSIX
version, like _POSIX_TZNAME_MAX already is.
Add some more _POSIX_* and _XOPEN_* 'specified' limits.

ok kettenis@

--- sys/limits.h
+++ sys/limits.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: limits.h,v 1.9 2012/06/26 16:12:42 deraadt Exp $ */
+/* $OpenBSD: limits.h,v 1.10 2012/06/30 20:21:10 guenther Exp $ */
 /*
  * Copyright (c) 2002 Marc Espie.
  *
@@ -84,16 +84,19 @@
 # define GID_MAX	UINT_MAX	/* max value for a gid_t */
 #endif

-#if __XPG_VISIBLE
+#if __XPG_VISIBLE || __POSIX_VISIBLE >= 200809
 # ifdef __LP64__
 #  define LONG_BIT	64
 # else
 #  define LONG_BIT	32
 # endif
 # define WORD_BIT	32
+#endif

+#if __XPG_VISIBLE < 600
 # include <machine/_float.h>

+/* XSI defines marked LEGACY in XPG5 and removed in IEEE Std 1003.1-2001 */
 # ifndef FLT_DIG
 #   define FLT_DIG	__FLT_DIG
 # endif
@@ -107,13 +110,16 @@
 #   define DBL_MAX	__DBL_MAX
 # endif

-# ifndef FLT_MIN
-#   define FLT_MIN	__FLT_MIN
-# endif
-# ifndef DBL_MIN
-#   define DBL_MIN	__DBL_MIN
+/* XSI defines marked LEGACY in XPG4v2 and removed in XPG5 */
+# if __XPG_VISIBLE < 500
+#  ifndef FLT_MIN
+#    define FLT_MIN	__FLT_MIN
+#  endif
+#  ifndef DBL_MIN
+#    define DBL_MIN	__DBL_MIN
+#  endif
 # endif

-#endif /* __XPG_VISIBLE */
+#endif /* __XPG_VISIBLE < 600 */

 #endif


webserver: fix null termination in http receive buffer

The http receive buffer is not initiaized to zero after mem alloc. So,
garbage data is included in the buffer.

Change-Id: Ic892619af675cba7aa8d95196d379a0720a219d9
Signed-off-by: Heuisam Kwag <2145cbb01e90577cd4983eccc3b7d9a9dfba6875@samsung.com>

--- apps/netutils/webserver/http_client.c
+++ apps/netutils/webserver/http_client.c
@@ -98,7 +98,7 @@
 		close(client->client_fd);
 		return HTTP_ERROR;
 	}
-
+	HTTP_MEMSET(buf, 0, HTTP_CONF_MAX_REQUEST_LENGTH);
 	if (getpeername(client->client_fd, (struct sockaddr *)&addr, &addr_len) < 0) {
 		HTTP_LOGE("Error: Fail to getpeername\n");
 		goto errout;


simple typo; maligned@attcanada.net

simple typo; maligned@attcanada.net

--- sys/malloc.h
+++ sys/malloc.h
@@ -1,4 +1,4 @@
-/*	$OpenBSD: malloc.h,v 1.28 2000/06/06 20:21:49 art Exp $	*/
+/*	$OpenBSD: malloc.h,v 1.29 2000/08/12 05:59:50 deraadt Exp $	*/
 /*	$NetBSD: malloc.h,v 1.39 1998/07/12 19:52:01 augustss Exp $	*/

 /*
@@ -284,7 +284,7 @@
 	"USB HC",	/* 103 M_USBHC */ \
 	"pipe", 	/* 104 M_PIPE */ \
 	"memdesc",	/* 105 M_MEMDESC */ \
-	"malloc debug",	/* 105 M_DEBUG */ \
+	"malloc debug",	/* 106 M_DEBUG */ \
 	NULL, NULL, NULL, NULL, NULL, \
 	NULL, NULL, NULL, NULL, NULL, \
 	NULL, NULL, NULL, NULL, NULL, \


Preliminaries for IPsec socket API.

Preliminaries for IPsec socket API.

--- sys/socket.h
+++ sys/socket.h
@@ -1,4 +1,4 @@
-/*	$OpenBSD: socket.h,v 1.9 1997/02/20 01:07:35 deraadt Exp $	*/
+/*	$OpenBSD: socket.h,v 1.10 1997/02/22 07:24:25 angelos Exp $	*/
 /*	$NetBSD: socket.h,v 1.14 1996/02/09 18:25:36 christos Exp $	*/

 /*
@@ -77,6 +77,26 @@
 #define SO_RCVTIMEO	0x1006		/* receive timeout */
 #define	SO_ERROR	0x1007		/* get error status and clear */
 #define	SO_TYPE		0x1008		/* get socket type */
+
+/*
+ * Security levels
+ */
+
+#define IPSEC_LEVEL_BYPASS	0x00	/* Bypass policy altogether */
+#define IPSEC_LEVEL_NONE	0x00	/* Send clear, accept any */
+#define IPSEC_LEVEL_AVAIL	0x01	/* Send secure if SA available */
+#define IPSEC_LEVEL_USE		0x02	/* Send secure, accept any */
+#define IPSEC_LEVEL_REQUIRE	0x03	/* Require secure inbound, also use */
+#define IPSEC_LEVEL_UNIQUE	0x04	/* Use outbound SA that is unique */
+#define IPSEC_LEVEL_DEFAULT	IPSEC_LEVEL_NONE
+
+/*
+ * Security categories
+ */
+
+#define IP_AUTH_LEVEL		0x00
+#define IP_ESP_TRANS_LEVEL	0x01
+#define IP_ESP_NETWORK_LEVEL	0x02

 /*
  * Structure used for manipulating linger option.


ARM: davinci: devices-da8xx: Clean up and correct the McASP device creation

Both DA830 and DA850 has McASP0 module, so do not restrict the use of
McASP0 for da850 only.

Signed-off-by: Peter Ujfalusi <e5c0b4cdf99ae1d408b9c497159e74b54e02e008@ti.com>
Signed-off-by: Sekhar Nori <946cfb81282bb55c59f600e7ee3d3b2c6973f7a3@ti.com>

--- arch/arm/mach-davinci/devices-da8xx.c
+++ arch/arm/mach-davinci/devices-da8xx.c
@@ -526,14 +526,25 @@

 void __init da8xx_register_mcasp(int id, struct snd_platform_data *pdata)
 {
-	/* DA830/OMAP-L137 has 3 instances of McASP */
-	if (cpu_is_davinci_da830() && id == 1) {
-		da830_mcasp1_device.dev.platform_data = pdata;
-		platform_device_register(&da830_mcasp1_device);
-	} else if (cpu_is_davinci_da850()) {
-		da850_mcasp_device.dev.platform_data = pdata;
-		platform_device_register(&da850_mcasp_device);
+	struct platform_device *pdev;
+
+	switch (id) {
+	case 0:
+		/* Valid for DA830/OMAP-L137 or DA850/OMAP-L138 */
+		pdev = &da850_mcasp_device;
+		break;
+	case 1:
+		/* Valid for DA830/OMAP-L137 only */
+		if (!cpu_is_davinci_da830())
+			return;
+		pdev = &da830_mcasp1_device;
+		break;
+	default:
+		return;
 	}
+
+	pdev->dev.platform_data = pdata;
+	platform_device_register(pdev);
 }

 static struct resource da8xx_pruss_resources[] = {


The _sx_assert() prototype should exist if either of INVARIANTS or
INVARIANT_SUPPORT is defined so you can build a kernel with
INVARIANT_SUPPORT, but build a module with just INVARIANTS on.

MFC after:	3 days
Reported by:	kuriyama

--- sys/sys/sx.h
+++ sys/sys/sx.h
@@ -57,7 +57,7 @@
 void	_sx_xunlock(struct sx *sx, const char *file, int line);
 int	_sx_try_upgrade(struct sx *sx, const char *file, int line);
 void	_sx_downgrade(struct sx *sx, const char *file, int line);
-#ifdef INVARIANT_SUPPORT
+#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
 void	_sx_assert(struct sx *sx, int what, const char *file, int line);
 #endif
 #ifdef DDB


ARM: 5907/1: ARM: Fix the reset on the RealView PBX Development board

Signed-off-by: Colin Tuckley <50862ddb74a0026b47e5501a528d82c09bb84e2f@arm.com>
Acked-by: Catalin Marinas <15ce75b290ebaf27c3f9fd73ab848685ed3d8261@arm.com>
Signed-off-by: Russell King <f6aa0246ff943bfa8602cdf60d40c481b38ed232@arm.linux.org.uk>

--- arch/arm/mach-realview/realview_pbx.c
+++ arch/arm/mach-realview/realview_pbx.c
@@ -334,8 +334,8 @@
 	 * in the system FPGA
 	 */
 	__raw_writel(REALVIEW_SYS_LOCK_VAL, lock_ctrl);
-	__raw_writel(0x0000, reset_ctrl);
-	__raw_writel(0x0004, reset_ctrl);
+	__raw_writel(0x00F0, reset_ctrl);
+	__raw_writel(0x00F4, reset_ctrl);
 }

 static void __init realview_pbx_init(void)


ARM: S5P6442: Enable I2C0 device on SMDK6442

This patch enables I2C0 device for WM8580 on SMDK6442.

Signed-off-by: Jassi Brar <fff9bc56b538f8fcf2fb0ab2988dc614622c06b3@samsung.com>
Acked-by: Mark Brown <b51b9a92386687a9ac927cebfa0f978adeb8cea5@opensource.wolfsonmicro.com>
[3fc711f4e08bc570a586748633ff7c76d0e1e253@samsung.com: minor changed title and description]
Signed-off-by: Kukjin Kim <3fc711f4e08bc570a586748633ff7c76d0e1e253@samsung.com>

--- arch/arm/mach-s5p6442/mach-smdk6442.c
+++ arch/arm/mach-s5p6442/mach-smdk6442.c
@@ -12,6 +12,7 @@
 #include <linux/types.h>
 #include <linux/init.h>
 #include <linux/serial_core.h>
+#include <linux/i2c.h>

 #include <asm/mach/arch.h>
 #include <asm/mach/map.h>
@@ -25,6 +26,7 @@
 #include <plat/s5p6442.h>
 #include <plat/devs.h>
 #include <plat/cpu.h>
+#include <plat/iic.h>

 /* Following are default values for UCON, ULCON and UFCON UART registers */
 #define SMDK6442_UCON_DEFAULT	(S3C2410_UCON_TXILEVEL |	\
@@ -65,8 +67,13 @@
 };

 static struct platform_device *smdk6442_devices[] __initdata = {
+	&s3c_device_i2c0,
 	&s5p6442_device_iis0,
 	&s3c_device_wdt,
+};
+
+static struct i2c_board_info smdk6442_i2c_devs0[] __initdata = {
+	{ I2C_BOARD_INFO("wm8580", 0x1b), },
 };

 static void __init smdk6442_map_io(void)
@@ -78,6 +85,9 @@

 static void __init smdk6442_machine_init(void)
 {
+	s3c_i2c0_set_platdata(NULL);
+	i2c_register_board_info(0, smdk6442_i2c_devs0,
+			ARRAY_SIZE(smdk6442_i2c_devs0));
 	platform_add_devices(smdk6442_devices, ARRAY_SIZE(smdk6442_devices));
 }


ARM: shmobile: bonito: Register pinctrl mapping for SCIF

Replace the GPIO-based SCIF pinmux configuration by a pinctrl mapping.

Signed-off-by: Laurent Pinchart <ae960578cc5eca7b9b1dbc37d9caa6cb634f35e0@ideasonboard.com>
Signed-off-by: Simon Horman <9662bddcc379be37df16f02a449c344b4718c1a1@verge.net.au>

--- arch/arm/mach-shmobile/board-bonito.c
+++ arch/arm/mach-shmobile/board-bonito.c
@@ -369,6 +369,12 @@
 #define VCCQ1CR		IOMEM(0xE6058140)
 #define VCCQ1LCDCR	IOMEM(0xE6058186)

+static const struct pinctrl_map scifa5_pinctrl_map[] = {
+	/* SCIFA5 */
+	PIN_MAP_MUX_GROUP_DEFAULT("sh-sci.5", "pfc-r8a7740",
+				  "scifa5_data_2", "scifa5"),
+};
+
 static void __init bonito_init(void)
 {
 	u16 val;
@@ -423,8 +429,8 @@
 		if (BIT_OFF(bsw2, 1) &&	/* S38.3 = ON */
 		    BIT_OFF(bsw3, 9) &&	/* S39.6 = ON */
 		    BIT_OFF(bsw4, 4)) {	/* S43.1 = ON */
-			gpio_request(GPIO_FN_SCIFA5_TXD_PORT91,	NULL);
-			gpio_request(GPIO_FN_SCIFA5_RXD_PORT92,	NULL);
+			pinctrl_register_mappings(scifa5_pinctrl_map,
+						  ARRAY_SIZE(scifa5_pinctrl_map));
 		}

 		/*


ARM: shmobile: sh73a0: Add smp ops to DT_MACHINE_START

This a board to be brought up with SMP enabled
without a board file present.

Signed-off-by: Simon Horman <9662bddcc379be37df16f02a449c344b4718c1a1@verge.net.au>

--- arch/arm/mach-shmobile/setup-sh73a0.c
+++ arch/arm/mach-shmobile/setup-sh73a0.c
@@ -919,6 +919,7 @@
 };

 DT_MACHINE_START(SH73A0_DT, "Generic SH73A0 (Flattened Device Tree)")
+	.smp		= smp_ops(sh73a0_smp_ops),
 	.map_io		= sh73a0_map_io,
 	.init_early	= sh73a0_add_early_devices_dt,
 	.nr_irqs	= NR_IRQS_LEGACY,


imx: reorder mx2x.h

imx: reorder mx2x.h

Signed-off-by: Uwe Kleine-König <181323dd666a182bc2d953e52d0ac3ab54cd8391@pengutronix.de>

--- arch/arm/plat-mxc/include/mach/mx2x.h
+++ arch/arm/plat-mxc/include/mach/mx2x.h
@@ -105,78 +105,78 @@
 	(((x) - X_MEMC_BASE_ADDR) + X_MEMC_BASE_ADDR_VIRT)

 /* fixed interrupt numbers */
+#define MXC_INT_CSPI3		6
+#define MXC_INT_GPIO		8
+#define MXC_INT_SDHC2		10
+#define MXC_INT_SDHC1		11
+#define MXC_INT_I2C		12
+#define MXC_INT_SSI2		13
+#define MXC_INT_SSI1		14
+#define MXC_INT_CSPI2		15
+#define MXC_INT_CSPI1		16
+#define MXC_INT_UART4		17
+#define MXC_INT_UART3		18
+#define MXC_INT_UART2		19
+#define MXC_INT_UART1		20
+#define MXC_INT_KPP		21
+#define MXC_INT_RTC		22
+#define MXC_INT_PWM		23
+#define MXC_INT_GPT		INT_GPT1
+#define MXC_INT_GPT3		24
+#define MXC_INT_GPT2		25
+#define MXC_INT_GPT1		26
+#define MXC_INT_WDOG		27
+#define MXC_INT_PCMCIA		28
+#define MXC_INT_NANDFC		29
+#define MXC_INT_CSI		31
+#define MXC_INT_DMACH0		32
+#define MXC_INT_DMACH1		33
+#define MXC_INT_DMACH2		34
+#define MXC_INT_DMACH3		35
+#define MXC_INT_DMACH4		36
+#define MXC_INT_DMACH5		37
+#define MXC_INT_DMACH6		38
+#define MXC_INT_DMACH7		39
+#define MXC_INT_DMACH8		40
+#define MXC_INT_DMACH9		41
+#define MXC_INT_DMACH10		42
+#define MXC_INT_DMACH11		43
+#define MXC_INT_DMACH12		44
+#define MXC_INT_DMACH13		45
+#define MXC_INT_DMACH14		46
+#define MXC_INT_DMACH15		47
+#define MXC_INT_EMMAPRP		51
+#define MXC_INT_EMMAPP		52
+#define MXC_INT_SLCDC		60
 #define MXC_INT_LCDC		61
-#define MXC_INT_SLCDC		60
-#define MXC_INT_EMMAPP		52
-#define MXC_INT_EMMAPRP		51
-#define MXC_INT_DMACH15		47
-#define MXC_INT_DMACH14		46
-#define MXC_INT_DMACH13		45
-#define MXC_INT_DMACH12		44
-#define MXC_INT_DMACH11		43
-#define MXC_INT_DMACH10		42
-#define MXC_INT_DMACH9		41
-#define MXC_INT_DMACH8		40
-#define MXC_INT_DMACH7		39
-#define MXC_INT_DMACH6		38
-#define MXC_INT_DMACH5		37
-#define MXC_INT_DMACH4		36
-#define MXC_INT_DMACH3		35
-#define MXC_INT_DMACH2		34
-#define MXC_INT_DMACH1		33
-#define MXC_INT_DMACH0		32
-#define MXC_INT_CSI		31
-#define MXC_INT_NANDFC		29
-#define MXC_INT_PCMCIA		28
-#define MXC_INT_WDOG		27
-#define MXC_INT_GPT1		26
-#define MXC_INT_GPT2		25
-#define MXC_INT_GPT3		24
-#define MXC_INT_GPT		INT_GPT1
-#define MXC_INT_PWM		23
-#define MXC_INT_RTC		22
-#define MXC_INT_KPP		21
-#define MXC_INT_UART1		20
-#define MXC_INT_UART2		19
-#define MXC_INT_UART3		18
-#define MXC_INT_UART4		17
-#define MXC_INT_CSPI1		16
-#define MXC_INT_CSPI2		15
-#define MXC_INT_SSI1		14
-#define MXC_INT_SSI2		13
-#define MXC_INT_I2C		12
-#define MXC_INT_SDHC1		11
-#define MXC_INT_SDHC2		10
-#define MXC_INT_GPIO		8
-#define MXC_INT_CSPI3		6

 /* fixed DMA request numbers */
+#define DMA_REQ_CSPI3_RX        1
+#define DMA_REQ_CSPI3_TX        2
+#define DMA_REQ_EXT             3
+#define DMA_REQ_SDHC2           6
+#define DMA_REQ_SDHC1           7
+#define DMA_REQ_SSI2_RX0        8
+#define DMA_REQ_SSI2_TX0        9
+#define DMA_REQ_SSI2_RX1        10
+#define DMA_REQ_SSI2_TX1        11
+#define DMA_REQ_SSI1_RX0        12
+#define DMA_REQ_SSI1_TX0        13
+#define DMA_REQ_SSI1_RX1        14
+#define DMA_REQ_SSI1_TX1        15
+#define DMA_REQ_CSPI2_RX        16
+#define DMA_REQ_CSPI2_TX        17
+#define DMA_REQ_CSPI1_RX        18
+#define DMA_REQ_CSPI1_TX        19
+#define DMA_REQ_UART4_RX        20
+#define DMA_REQ_UART4_TX        21
+#define DMA_REQ_UART3_RX        22
+#define DMA_REQ_UART3_TX        23
+#define DMA_REQ_UART2_RX        24
+#define DMA_REQ_UART2_TX        25
+#define DMA_REQ_UART1_RX        26
+#define DMA_REQ_UART1_TX        27
+#define DMA_REQ_CSI_STAT        30
 #define DMA_REQ_CSI_RX          31
-#define DMA_REQ_CSI_STAT        30
-#define DMA_REQ_UART1_TX        27
-#define DMA_REQ_UART1_RX        26
-#define DMA_REQ_UART2_TX        25
-#define DMA_REQ_UART2_RX        24
-#define DMA_REQ_UART3_TX        23
-#define DMA_REQ_UART3_RX        22
-#define DMA_REQ_UART4_TX        21
-#define DMA_REQ_UART4_RX        20
-#define DMA_REQ_CSPI1_TX        19
-#define DMA_REQ_CSPI1_RX        18
-#define DMA_REQ_CSPI2_TX        17
-#define DMA_REQ_CSPI2_RX        16
-#define DMA_REQ_SSI1_TX1        15
-#define DMA_REQ_SSI1_RX1        14
-#define DMA_REQ_SSI1_TX0        13
-#define DMA_REQ_SSI1_RX0        12
-#define DMA_REQ_SSI2_TX1        11
-#define DMA_REQ_SSI2_RX1        10
-#define DMA_REQ_SSI2_TX0        9
-#define DMA_REQ_SSI2_RX0        8
-#define DMA_REQ_SDHC1           7
-#define DMA_REQ_SDHC2           6
-#define DMA_REQ_EXT             3
-#define DMA_REQ_CSPI3_TX        2
-#define DMA_REQ_CSPI3_RX        1

 #endif /* __ASM_ARCH_MXC_MX2x_H__ */


musb: fix power field to hold all possible values

MUSB can supply upto 500mA such as, AM3517 and OMAP3EVM Rev >=E and thus
the 'power' field has to hold values above 255.

Signed-off-by: Ajay Kumar Gupta <e07c675cf8f418a4c15b581efbf445af628cd457@ti.com>
Signed-off-by: Felipe Balbi <d79216928cfdb58376b0b00a1a44a75e52dbc84e@nokia.com>
Signed-off-by: Greg Kroah-Hartman <4645f7897fd33786a2ee1264d590b3c400559d85@suse.de>

--- arch/arm/plat-omap/include/plat/usb.h
+++ arch/arm/plat-omap/include/plat/usb.h
@@ -46,7 +46,7 @@
 struct omap_musb_board_data {
 	u8	interface_type;
 	u8	mode;
-	u8	power;
+	u16	power;
 };

 enum musb_interface    {MUSB_INTERFACE_ULPI, MUSB_INTERFACE_UTMI};


arm64: make ll/sc __cmpxchg_case_##name asm consistent

The ll/sc __cmpxchg_case_##name assembly mostly uses symbolic names for
operands, but in a single case uses %2 to refer to what is otherwise
known as %[v]. This makes the code more painful to read than is
necessary.

Use %[v] instead.

Signed-off-by: Mark Rutland <1db9dd262beaa64f35756dd39531d9747b7778bb@arm.com>
Cc: Will Deacon <b913f13ef92ac157955c954ccaaff7be065faba4@arm.com>
Signed-off-by: Will Deacon <b913f13ef92ac157955c954ccaaff7be065faba4@arm.com>

--- arch/arm64/include/asm/atomic_ll_sc.h
+++ arch/arm64/include/asm/atomic_ll_sc.h
@@ -181,7 +181,7 @@
 	unsigned long tmp, oldval;					\
 									\
 	asm volatile(							\
-	"	prfm	pstl1strm, %2\n"				\
+	"	prfm	pstl1strm, %[v]\n"				\
 	"1:	ldxr" #sz "\t%" #w "[oldval], %[v]\n"			\
 	"	eor	%" #w "[tmp], %" #w "[oldval], %" #w "[old]\n"	\
 	"	cbnz	%" #w "[tmp], 2f\n"				\