Merge "init.rc: Set owner for /sys/power/autosleep"
diff --git a/adb/adb.c b/adb/adb.c
index 44eaebb..6ec4f7a 100644
--- a/adb/adb.c
+++ b/adb/adb.c
@@ -21,6 +21,7 @@
 #include <ctype.h>
 #include <stdarg.h>
 #include <errno.h>
+#include <stddef.h>
 #include <string.h>
 #include <time.h>
 #include <sys/time.h>
@@ -28,6 +29,8 @@
 #include "sysdeps.h"
 #include "adb.h"
 
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
+
 #if !ADB_HOST
 #include <private/android_filesystem_config.h>
 #include <linux/capability.h>
@@ -42,7 +45,9 @@
 
 int HOST = 0;
 
+#if !ADB_HOST
 static const char *adb_device_banner = "device";
+#endif
 
 void fatal(const char *fmt, ...)
 {
@@ -269,6 +274,36 @@
     send_packet(p, t);
 }
 
+static size_t fill_connect_data(char *buf, size_t bufsize)
+{
+#if ADB_HOST
+    return snprintf(buf, bufsize, "host::") + 1;
+#else
+    static const char *cnxn_props[] = {
+        "ro.product.name",
+        "ro.product.model",
+        "ro.product.device",
+    };
+    static const int num_cnxn_props = ARRAY_SIZE(cnxn_props);
+    int i;
+    size_t remaining = bufsize;
+    size_t len;
+
+    len = snprintf(buf, remaining, "%s::", adb_device_banner);
+    remaining -= len;
+    buf += len;
+    for (i = 0; i < num_cnxn_props; i++) {
+        char value[PROPERTY_VALUE_MAX];
+        property_get(cnxn_props[i], value, "");
+        len = snprintf(buf, remaining, "%s=%s;", cnxn_props[i], value);
+        remaining -= len;
+        buf += len;
+    }
+
+    return bufsize - remaining + 1;
+#endif
+}
+
 static void send_connect(atransport *t)
 {
     D("Calling send_connect \n");
@@ -276,9 +311,8 @@
     cp->msg.command = A_CNXN;
     cp->msg.arg0 = A_VERSION;
     cp->msg.arg1 = MAX_PAYLOAD;
-    snprintf((char*) cp->data, sizeof cp->data, "%s::",
-            HOST ? "host" : adb_device_banner);
-    cp->msg.data_length = strlen((char*) cp->data) + 1;
+    cp->msg.data_length = fill_connect_data((char *)cp->data,
+                                            sizeof(cp->data));
     send_packet(cp, t);
 #if ADB_HOST
         /* XXX why sleep here? */
@@ -305,29 +339,56 @@
     }
 }
 
+/* qual_overwrite is used to overwrite a qualifier string.  dst is a
+ * pointer to a char pointer.  It is assumed that if *dst is non-NULL, it
+ * was malloc'ed and needs to freed.  *dst will be set to a dup of src.
+ */
+static void qual_overwrite(char **dst, const char *src)
+{
+    if (!dst)
+        return;
+
+    free(*dst);
+    *dst = NULL;
+
+    if (!src || !*src)
+        return;
+
+    *dst = strdup(src);
+}
+
 void parse_banner(char *banner, atransport *t)
 {
-    char *type, *product, *end;
+    static const char *prop_seps = ";";
+    static const char key_val_sep = '=';
+    char *cp;
+    char *type;
 
     D("parse_banner: %s\n", banner);
     type = banner;
-    product = strchr(type, ':');
-    if(product) {
-        *product++ = 0;
-    } else {
-        product = "";
-    }
-
-        /* remove trailing ':' */
-    end = strchr(product, ':');
-    if(end) *end = 0;
-
-        /* save product name in device structure */
-    if (t->product == NULL) {
-        t->product = strdup(product);
-    } else if (strcmp(product, t->product) != 0) {
-        free(t->product);
-        t->product = strdup(product);
+    cp = strchr(type, ':');
+    if (cp) {
+        *cp++ = 0;
+        /* Nothing is done with second field. */
+        cp = strchr(cp, ':');
+        if (cp) {
+            char *save;
+            char *key;
+            key = adb_strtok_r(cp + 1, prop_seps, &save);
+            while (key) {
+                cp = strchr(key, key_val_sep);
+                if (cp) {
+                    *cp++ = '\0';
+                    if (!strcmp(key, "ro.product.name"))
+                        qual_overwrite(&t->product, cp);
+                    else if (!strcmp(key, "ro.product.model"))
+                        qual_overwrite(&t->model, cp);
+                    else if (!strcmp(key, "ro.product.device"))
+                        qual_overwrite(&t->device, cp);
+                }
+                key = adb_strtok_r(NULL, prop_seps, &save);
+            }
+        }
     }
 
     if(!strcmp(type, "bootloader")){
@@ -1025,7 +1086,8 @@
     if (sscanf(value, "%d", &port) == 1 && port > 0) {
         // listen on TCP port specified by service.adb.tcp.port property
         local_init(port);
-    } else if (access("/dev/android_adb", F_OK) == 0) {
+    } else if (access(USB_ADB_PATH, F_OK) == 0 ||
+               access(USB_FFS_ADB_EP0, F_OK) == 0) {
         // listen on USB
         usb_init();
     } else {
@@ -1067,7 +1129,7 @@
 
     strncpy(hostbuf, host, sizeof(hostbuf) - 1);
     if (portstr) {
-        if (portstr - host >= sizeof(hostbuf)) {
+        if (portstr - host >= (ptrdiff_t)sizeof(hostbuf)) {
             snprintf(buffer, buffer_size, "bad host name %s", host);
             return;
         }
diff --git a/adb/adb.h b/adb/adb.h
index 4a9b53c..df88896 100644
--- a/adb/adb.h
+++ b/adb/adb.h
@@ -190,6 +190,8 @@
         /* used to identify transports for clients */
     char *serial;
     char *product;
+    char *model;
+    char *device;
     char *devpath;
     int adb_port; // Use for emulators (local transport)
 
@@ -254,7 +256,7 @@
 ** get_device_transport does an acquire on your behalf before returning
 */
 void init_transport_registration(void);
-int  list_transports(char *buf, size_t  bufsize, int show_devpath);
+int  list_transports(char *buf, size_t  bufsize, int long_listing);
 void update_transports(void);
 
 asocket*  create_device_tracker(void);
@@ -462,6 +464,17 @@
 
 #define CHUNK_SIZE (64*1024)
 
+#if !ADB_HOST
+#define USB_ADB_PATH     "/dev/android_adb"
+
+#define USB_FFS_ADB_PATH  "/dev/usb-ffs/adb/"
+#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x
+
+#define USB_FFS_ADB_EP0   USB_FFS_ADB_EP(ep0)
+#define USB_FFS_ADB_OUT   USB_FFS_ADB_EP(ep1)
+#define USB_FFS_ADB_IN    USB_FFS_ADB_EP(ep2)
+#endif
+
 int sendfailmsg(int fd, const char *reason);
 int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s);
 
diff --git a/adb/commandline.c b/adb/commandline.c
index a6f1ce2..10b2332 100644
--- a/adb/commandline.c
+++ b/adb/commandline.c
@@ -85,7 +85,7 @@
         " -e                            - directs command to the only running emulator.\n"
         "                                 returns an error if more than one emulator is running.\n"
         " -s <specific device>          - directs command to the device or emulator with the given\n"
-        "                                 serial number or device path. Overrides ANDROID_SERIAL\n"
+        "                                 serial number or qualifier. Overrides ANDROID_SERIAL\n"
         "                                 environment variable.\n"
         " -p <product name or path>     - simple product name like 'sooner', or\n"
         "                                 a relative/absolute path to a product\n"
@@ -94,7 +94,7 @@
         "                                 environment variable is used, which must\n"
         "                                 be an absolute path.\n"
         " devices [-l]                  - list all connected devices\n"
-        "                                 ('-l' means list device paths)\n"
+        "                                 ('-l' will also list device qualifiers)\n"
         " connect <host>[:<port>]       - connect to a device via TCP/IP\n"
         "                                 Port 5555 is used by default if no port number is specified.\n"
         " disconnect [<host>[:<port>]]  - disconnect from a TCP/IP device.\n"
diff --git a/adb/protocol.txt b/adb/protocol.txt
index 398d042..abd63f9 100644
--- a/adb/protocol.txt
+++ b/adb/protocol.txt
@@ -72,7 +72,7 @@
 The system identity string should be "<systemtype>:<serialno>:<banner>"
 where systemtype is "bootloader", "device", or "host", serialno is some
 kind of unique ID (or empty), and banner is a human-readable version
-or identifier string (informational only).
+or identifier string.  The banner is used to transmit useful properties.
 
 
 --- OPEN(local-id, 0, "destination") -----------------------------------
diff --git a/adb/sockets.c b/adb/sockets.c
index 91db951..b77c38c 100644
--- a/adb/sockets.c
+++ b/adb/sockets.c
@@ -608,12 +608,30 @@
     return n;
 }
 
+#define PREFIX(str) { str, sizeof(str) - 1 }
+static const struct prefix_struct {
+    const char *str;
+    const size_t len;
+} prefixes[] = {
+    PREFIX("usb:"),
+    PREFIX("product:"),
+    PREFIX("model:"),
+    PREFIX("device:"),
+};
+static const int num_prefixes = (sizeof(prefixes) / sizeof(prefixes[0]));
+
 /* skip_host_serial return the position in a string
    skipping over the 'serial' parameter in the ADB protocol,
    where parameter string may be a host:port string containing
    the protocol delimiter (colon). */
 char *skip_host_serial(char *service) {
     char *first_colon, *serial_end;
+    int i;
+
+    for (i = 0; i < num_prefixes; i++) {
+        if (!strncmp(service, prefixes[i].str, prefixes[i].len))
+            return strchr(service + prefixes[i].len, ':');
+    }
 
     first_colon = strchr(service, ':');
     if (!first_colon) {
diff --git a/adb/sysdeps.h b/adb/sysdeps.h
index b518076..605fa17 100644
--- a/adb/sysdeps.h
+++ b/adb/sysdeps.h
@@ -254,6 +254,8 @@
     return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
 }
 
+extern char*  adb_strtok_r(char *str, const char *delim, char **saveptr);
+
 #else /* !_WIN32 a.k.a. Unix */
 
 #include "fdevent.h"
@@ -490,6 +492,13 @@
     return path[0] == '/';
 }
 
+static __inline__ char*  adb_strtok_r(char *str, const char *delim, char **saveptr)
+{
+    return strtok_r(str, delim, saveptr);
+}
+#undef   strtok_r
+#define  strtok_r  ___xxx_strtok_r
+
 #endif /* !_WIN32 */
 
 #endif /* _ADB_SYSDEPS_H */
diff --git a/adb/sysdeps_win32.c b/adb/sysdeps_win32.c
index c426718..d41c42c 100644
--- a/adb/sysdeps_win32.c
+++ b/adb/sysdeps_win32.c
@@ -2140,3 +2140,81 @@
     InitializeCriticalSection( &_win32_lock );
 }
 
+/* Windows doesn't have strtok_r.  Use the one from bionic. */
+
+/*
+ * Copyright (c) 1988 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+char *
+adb_strtok_r(char *s, const char *delim, char **last)
+{
+	char *spanp;
+	int c, sc;
+	char *tok;
+
+
+	if (s == NULL && (s = *last) == NULL)
+		return (NULL);
+
+	/*
+	 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
+	 */
+cont:
+	c = *s++;
+	for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
+		if (c == sc)
+			goto cont;
+	}
+
+	if (c == 0) {		/* no non-delimiter characters */
+		*last = NULL;
+		return (NULL);
+	}
+	tok = s - 1;
+
+	/*
+	 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
+	 * Note that delim must have one NUL; we stop if we see that, too.
+	 */
+	for (;;) {
+		c = *s++;
+		spanp = (char *)delim;
+		do {
+			if ((sc = *spanp++) == c) {
+				if (c == 0)
+					s = NULL;
+				else
+					s[-1] = 0;
+				*last = s;
+				return (tok);
+			}
+		} while (sc != 0);
+	}
+	/* NOTREACHED */
+}
diff --git a/adb/transport.c b/adb/transport.c
index 29cb582..9fd6cc2 100644
--- a/adb/transport.c
+++ b/adb/transport.c
@@ -601,6 +601,10 @@
             free(t->product);
         if (t->serial)
             free(t->serial);
+        if (t->model)
+            free(t->model);
+        if (t->device)
+            free(t->device);
         if (t->devpath)
             free(t->devpath);
 
@@ -739,6 +743,45 @@
     dis->next = dis->prev = dis;
 }
 
+static int qual_char_is_invalid(char ch)
+{
+    if ('A' <= ch && ch <= 'Z')
+        return 0;
+    if ('a' <= ch && ch <= 'z')
+        return 0;
+    if ('0' <= ch && ch <= '9')
+        return 0;
+    return 1;
+}
+
+static int qual_match(const char *to_test,
+                      const char *prefix, const char *qual, int sanitize_qual)
+{
+    if (!to_test || !*to_test)
+        /* Return true if both the qual and to_test are null strings. */
+        return !qual || !*qual;
+
+    if (!qual)
+        return 0;
+
+    if (prefix) {
+        while (*prefix) {
+            if (*prefix++ != *to_test++)
+                return 0;
+        }
+    }
+
+    while (*qual) {
+        char ch = *qual++;
+        if (sanitize_qual && qual_char_is_invalid(ch))
+            ch = '_';
+        if (ch != *to_test++)
+            return 0;
+    }
+
+    /* Everything matched so far.  Return true if *to_test is a NUL. */
+    return !*to_test;
+}
 
 atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char** error_out)
 {
@@ -760,13 +803,19 @@
 
         /* check for matching serial number */
         if (serial) {
-            if (t->serial && !strcmp(serial, t->serial)) {
+            if ((t->serial && !strcmp(serial, t->serial)) ||
+                (t->devpath && !strcmp(serial, t->devpath)) ||
+                qual_match(serial, "product:", t->product, 0) ||
+                qual_match(serial, "model:", t->model, 1) ||
+                qual_match(serial, "device:", t->device, 0)) {
+                if (result) {
+                    if (error_out)
+                        *error_out = "more than one device";
+                    ambiguous = 1;
+                    result = NULL;
+                    break;
+                }
                 result = t;
-                break;
-            }
-            if (t->devpath && !strcmp(serial, t->devpath)) {
-                result = t;
-                break;
             }
         } else {
             if (ttype == kTransportUsb && t->type == kTransportUsb) {
@@ -843,7 +892,58 @@
     }
 }
 
-int list_transports(char *buf, size_t  bufsize, int show_devpath)
+static void add_qual(char **buf, size_t *buf_size,
+                     const char *prefix, const char *qual, int sanitize_qual)
+{
+    size_t len;
+    int prefix_len;
+
+    if (!buf || !*buf || !buf_size || !*buf_size || !qual || !*qual)
+        return;
+
+    len = snprintf(*buf, *buf_size, "%s%n%s", prefix, &prefix_len, qual);
+
+    if (sanitize_qual) {
+        char *cp;
+        for (cp = *buf + prefix_len; cp < *buf + len; cp++) {
+            if (qual_char_is_invalid(*cp))
+                *cp = '_';
+        }
+    }
+
+    *buf_size -= len;
+    *buf += len;
+}
+
+static size_t format_transport(atransport *t, char *buf, size_t bufsize,
+                               int long_listing)
+{
+    const char* serial = t->serial;
+    if (!serial || !serial[0])
+        serial = "????????????";
+
+    if (!long_listing) {
+        return snprintf(buf, bufsize, "%s\t%s\n", serial, statename(t));
+    } else {
+        size_t len, remaining = bufsize;
+
+        len = snprintf(buf, remaining, "%-22s %s", serial, statename(t));
+        remaining -= len;
+        buf += len;
+
+        add_qual(&buf, &remaining, " ", t->devpath, 0);
+        add_qual(&buf, &remaining, " product:", t->product, 0);
+        add_qual(&buf, &remaining, " model:", t->model, 1);
+        add_qual(&buf, &remaining, " device:", t->device, 0);
+
+        len = snprintf(buf, remaining, "\n");
+        remaining -= len;
+
+        return bufsize - remaining;
+    }
+}
+
+int list_transports(char *buf, size_t  bufsize, int long_listing)
 {
     char*       p   = buf;
     char*       end = buf + bufsize;
@@ -853,17 +953,7 @@
         /* XXX OVERRUN PROBLEMS XXX */
     adb_mutex_lock(&transport_lock);
     for(t = transport_list.next; t != &transport_list; t = t->next) {
-        const char* serial = t->serial;
-        if (!serial || !serial[0])
-            serial = "????????????";
-        if (show_devpath) {
-            const char* devpath = t->devpath;
-            if (!devpath || !devpath[0])
-                devpath = "????????????";
-            len = snprintf(p, end - p, "%s\t%s\t%s\n", serial, devpath, statename(t));
-        } else
-            len = snprintf(p, end - p, "%s\t%s\n", serial, statename(t));
-
+        len = format_transport(t, p, end - p, long_listing);
         if (p + len >= end) {
             /* discard last line if buffer is too short */
             break;
@@ -923,9 +1013,6 @@
         if (t->serial && !strcmp(serial, t->serial)) {
             break;
         }
-        if (t->devpath && !strcmp(serial, t->devpath)) {
-            break;
-        }
      }
     adb_mutex_unlock(&transport_lock);
 
diff --git a/adb/usb_linux_client.c b/adb/usb_linux_client.c
index b110ed8..fb1dad0 100644
--- a/adb/usb_linux_client.c
+++ b/adb/usb_linux_client.c
@@ -19,6 +19,8 @@
 #include <unistd.h>
 #include <string.h>
 
+#include <linux/usb/ch9.h>
+#include <linux/usb/functionfs.h>
 #include <sys/ioctl.h>
 #include <sys/types.h>
 #include <dirent.h>
@@ -29,20 +31,122 @@
 #define   TRACE_TAG  TRACE_USB
 #include "adb.h"
 
+#define MAX_PACKET_SIZE_FS	64
+#define MAX_PACKET_SIZE_HS	512
+
+#define cpu_to_le16(x)  htole16(x)
+#define cpu_to_le32(x)  htole32(x)
 
 struct usb_handle
 {
-    int fd;
     adb_cond_t notify;
     adb_mutex_t lock;
+
+    int (*write)(usb_handle *h, const void *data, int len);
+    int (*read)(usb_handle *h, void *data, int len);
+    void (*kick)(usb_handle *h);
+
+    // Legacy f_adb
+    int fd;
+
+    // FunctionFS
+    int control;
+    int bulk_out; /* "out" from the host's perspective => source for adbd */
+    int bulk_in;  /* "in" from the host's perspective => sink for adbd */
 };
 
-void usb_cleanup()
-{
-    // nothing to do here
-}
+static const struct {
+    struct usb_functionfs_descs_head header;
+    struct {
+        struct usb_interface_descriptor intf;
+        struct usb_endpoint_descriptor_no_audio source;
+        struct usb_endpoint_descriptor_no_audio sink;
+    } __attribute__((packed)) fs_descs, hs_descs;
+} __attribute__((packed)) descriptors = {
+    .header = {
+        .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC),
+        .length = cpu_to_le32(sizeof(descriptors)),
+        .fs_count = 3,
+        .hs_count = 3,
+    },
+    .fs_descs = {
+        .intf = {
+            .bLength = sizeof(descriptors.fs_descs.intf),
+            .bDescriptorType = USB_DT_INTERFACE,
+            .bInterfaceNumber = 0,
+            .bNumEndpoints = 2,
+            .bInterfaceClass = ADB_CLASS,
+            .bInterfaceSubClass = ADB_SUBCLASS,
+            .bInterfaceProtocol = ADB_PROTOCOL,
+            .iInterface = 1, /* first string from the provided table */
+        },
+        .source = {
+            .bLength = sizeof(descriptors.fs_descs.source),
+            .bDescriptorType = USB_DT_ENDPOINT,
+            .bEndpointAddress = 1 | USB_DIR_OUT,
+            .bmAttributes = USB_ENDPOINT_XFER_BULK,
+            .wMaxPacketSize = MAX_PACKET_SIZE_FS,
+        },
+        .sink = {
+            .bLength = sizeof(descriptors.fs_descs.sink),
+            .bDescriptorType = USB_DT_ENDPOINT,
+            .bEndpointAddress = 2 | USB_DIR_IN,
+            .bmAttributes = USB_ENDPOINT_XFER_BULK,
+            .wMaxPacketSize = MAX_PACKET_SIZE_FS,
+        },
+    },
+    .hs_descs = {
+        .intf = {
+            .bLength = sizeof(descriptors.hs_descs.intf),
+            .bDescriptorType = USB_DT_INTERFACE,
+            .bInterfaceNumber = 0,
+            .bNumEndpoints = 2,
+            .bInterfaceClass = ADB_CLASS,
+            .bInterfaceSubClass = ADB_SUBCLASS,
+            .bInterfaceProtocol = ADB_PROTOCOL,
+            .iInterface = 1, /* first string from the provided table */
+        },
+        .source = {
+            .bLength = sizeof(descriptors.hs_descs.source),
+            .bDescriptorType = USB_DT_ENDPOINT,
+            .bEndpointAddress = 1 | USB_DIR_OUT,
+            .bmAttributes = USB_ENDPOINT_XFER_BULK,
+            .wMaxPacketSize = MAX_PACKET_SIZE_HS,
+        },
+        .sink = {
+            .bLength = sizeof(descriptors.hs_descs.sink),
+            .bDescriptorType = USB_DT_ENDPOINT,
+            .bEndpointAddress = 2 | USB_DIR_IN,
+            .bmAttributes = USB_ENDPOINT_XFER_BULK,
+            .wMaxPacketSize = MAX_PACKET_SIZE_HS,
+        },
+    },
+};
 
-static void *usb_open_thread(void *x)
+#define STR_INTERFACE_ "ADB Interface"
+
+static const struct {
+    struct usb_functionfs_strings_head header;
+    struct {
+        __le16 code;
+        const char str1[sizeof(STR_INTERFACE_)];
+    } __attribute__((packed)) lang0;
+} __attribute__((packed)) strings = {
+    .header = {
+        .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
+        .length = cpu_to_le32(sizeof(strings)),
+        .str_count = cpu_to_le32(1),
+        .lang_count = cpu_to_le32(1),
+    },
+    .lang0 = {
+        cpu_to_le16(0x0409), /* en-us */
+        STR_INTERFACE_,
+    },
+};
+
+
+
+static void *usb_adb_open_thread(void *x)
 {
     struct usb_handle *usb = (struct usb_handle *)x;
     int fd;
@@ -79,7 +183,7 @@
     return 0;
 }
 
-int usb_write(usb_handle *h, const void *data, int len)
+static int usb_adb_write(usb_handle *h, const void *data, int len)
 {
     int n;
 
@@ -94,7 +198,7 @@
     return 0;
 }
 
-int usb_read(usb_handle *h, void *data, int len)
+static int usb_adb_read(usb_handle *h, void *data, int len)
 {
     int n;
 
@@ -109,14 +213,31 @@
     return 0;
 }
 
-void usb_init()
+static void usb_adb_kick(usb_handle *h)
+{
+    D("usb_kick\n");
+    adb_mutex_lock(&h->lock);
+    adb_close(h->fd);
+    h->fd = -1;
+
+    // notify usb_adb_open_thread that we are disconnected
+    adb_cond_signal(&h->notify);
+    adb_mutex_unlock(&h->lock);
+}
+
+static void usb_adb_init()
 {
     usb_handle *h;
     adb_thread_t tid;
     int fd;
 
     h = calloc(1, sizeof(usb_handle));
+
+    h->write = usb_adb_write;
+    h->read = usb_adb_read;
+    h->kick = usb_adb_kick;
     h->fd = -1;
+
     adb_cond_init(&h->notify, 0);
     adb_mutex_init(&h->lock, 0);
 
@@ -133,25 +254,239 @@
     }
 
     D("[ usb_init - starting thread ]\n");
-    if(adb_thread_create(&tid, usb_open_thread, h)){
+    if(adb_thread_create(&tid, usb_adb_open_thread, h)){
         fatal_errno("cannot create usb thread");
     }
 }
 
-void usb_kick(usb_handle *h)
-{
-    D("usb_kick\n");
-    adb_mutex_lock(&h->lock);
-    adb_close(h->fd);
-    h->fd = -1;
 
-    // notify usb_open_thread that we are disconnected
+static void init_functionfs(struct usb_handle *h)
+{
+    ssize_t ret;
+
+    D("OPENING %s\n", USB_FFS_ADB_EP0);
+    h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR);
+    if (h->control < 0) {
+        D("[ %s: cannot open control endpoint: errno=%d]\n", USB_FFS_ADB_EP0, errno);
+        goto err;
+    }
+
+    ret = adb_write(h->control, &descriptors, sizeof(descriptors));
+    if (ret < 0) {
+        D("[ %s: write descriptors failed: errno=%d ]\n", USB_FFS_ADB_EP0, errno);
+        goto err;
+    }
+
+    ret = adb_write(h->control, &strings, sizeof(strings));
+    if (ret < 0) {
+        D("[ %s: writing strings failed: errno=%d]\n", USB_FFS_ADB_EP0, errno);
+        goto err;
+    }
+
+    h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR);
+    if (h->bulk_out < 0) {
+        D("[ %s: cannot open bulk-out ep: errno=%d ]\n", USB_FFS_ADB_OUT, errno);
+        goto err;
+    }
+
+    h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR);
+    if (h->bulk_in < 0) {
+        D("[ %s: cannot open bulk-in ep: errno=%d ]\n", USB_FFS_ADB_IN, errno);
+        goto err;
+    }
+
+    return;
+
+err:
+    if (h->bulk_in > 0) {
+        adb_close(h->bulk_in);
+        h->bulk_in = -1;
+    }
+    if (h->bulk_out > 0) {
+        adb_close(h->bulk_out);
+        h->bulk_out = -1;
+    }
+    if (h->control > 0) {
+        adb_close(h->control);
+        h->control = -1;
+    }
+    return;
+}
+
+static void *usb_ffs_open_thread(void *x)
+{
+    struct usb_handle *usb = (struct usb_handle *)x;
+
+    while (1) {
+        // wait until the USB device needs opening
+        adb_mutex_lock(&usb->lock);
+        while (usb->control != -1)
+            adb_cond_wait(&usb->notify, &usb->lock);
+        adb_mutex_unlock(&usb->lock);
+
+        while (1) {
+            init_functionfs(usb);
+
+            if (usb->control >= 0)
+                break;
+
+            adb_sleep_ms(1000);
+        }
+
+        D("[ usb_thread - registering device ]\n");
+        register_usb_transport(usb, 0, 0, 1);
+    }
+
+    // never gets here
+    return 0;
+}
+
+static int bulk_write(int bulk_in, const char *buf, size_t length)
+{
+    size_t count = 0;
+    int ret;
+
+    do {
+        ret = adb_write(bulk_in, buf + count, length - count);
+        if (ret < 0) {
+            if (errno != EINTR)
+                return ret;
+        } else {
+            count += ret;
+        }
+    } while (count < length);
+
+    D("[ bulk_write done fd=%d ]\n", bulk_in);
+    return count;
+}
+
+static int usb_ffs_write(usb_handle *h, const void *data, int len)
+{
+    int n;
+
+    D("about to write (fd=%d, len=%d)\n", h->bulk_in, len);
+    n = bulk_write(h->bulk_in, data, len);
+    if (n != len) {
+        D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
+            h->bulk_in, n, errno, strerror(errno));
+        return -1;
+    }
+    D("[ done fd=%d ]\n", h->bulk_in);
+    return 0;
+}
+
+static int bulk_read(int bulk_out, char *buf, size_t length)
+{
+    size_t count = 0;
+    int ret;
+
+    do {
+        ret = adb_read(bulk_out, buf + count, length - count);
+        if (ret < 0) {
+            if (errno != EINTR) {
+                D("[ bulk_read failed fd=%d length=%d count=%d ]\n",
+                                           bulk_out, length, count);
+                return ret;
+            }
+        } else {
+            count += ret;
+        }
+    } while (count < length);
+
+    return count;
+}
+
+static int usb_ffs_read(usb_handle *h, void *data, int len)
+{
+    int n;
+
+    D("about to read (fd=%d, len=%d)\n", h->bulk_out, len);
+    n = bulk_read(h->bulk_out, data, len);
+    if (n != len) {
+        D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
+            h->bulk_out, n, errno, strerror(errno));
+        return -1;
+    }
+    D("[ done fd=%d ]\n", h->bulk_out);
+    return 0;
+}
+
+static void usb_ffs_kick(usb_handle *h)
+{
+    int err;
+
+    err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT);
+    if (err < 0)
+        D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in, errno);
+
+    err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT);
+    if (err < 0)
+        D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out, errno);
+
+    adb_mutex_lock(&h->lock);
+    adb_close(h->control);
+    adb_close(h->bulk_out);
+    adb_close(h->bulk_in);
+    h->control = h->bulk_out = h->bulk_in = -1;
+
+    // notify usb_ffs_open_thread that we are disconnected
     adb_cond_signal(&h->notify);
     adb_mutex_unlock(&h->lock);
 }
 
+static void usb_ffs_init()
+{
+    usb_handle *h;
+    adb_thread_t tid;
+
+    D("[ usb_init - using FunctionFS ]\n");
+
+    h = calloc(1, sizeof(usb_handle));
+
+    h->write = usb_ffs_write;
+    h->read = usb_ffs_read;
+    h->kick = usb_ffs_kick;
+
+    h->control  = -1;
+    h->bulk_out = -1;
+    h->bulk_out = -1;
+
+    adb_cond_init(&h->notify, 0);
+    adb_mutex_init(&h->lock, 0);
+
+    D("[ usb_init - starting thread ]\n");
+    if (adb_thread_create(&tid, usb_ffs_open_thread, h)){
+        fatal_errno("[ cannot create usb thread ]\n");
+    }
+}
+
+void usb_init()
+{
+    if (access(USB_FFS_ADB_EP0, F_OK) == 0)
+        usb_ffs_init();
+    else
+        usb_adb_init();
+}
+
+void usb_cleanup()
+{
+}
+
+int usb_write(usb_handle *h, const void *data, int len)
+{
+    return h->write(h, data, len);
+}
+
+int usb_read(usb_handle *h, void *data, int len)
+{
+    return h->read(h, data, len);
+}
 int usb_close(usb_handle *h)
 {
-    // nothing to do here
     return 0;
 }
+
+void usb_kick(usb_handle *h)
+{
+    h->kick(h);
+}
diff --git a/adb/usb_vendors.c b/adb/usb_vendors.c
index f5aa8cc..b988115 100644
--- a/adb/usb_vendors.c
+++ b/adb/usb_vendors.c
@@ -125,6 +125,8 @@
 #define VENDOR_ID_SONY          0x054C
 // Lab126's USB Vendor ID
 #define VENDOR_ID_LAB126        0x1949
+// Yulong Coolpad's USB Vendor ID
+#define VENDOR_ID_YULONG_COOLPAD 0x1EBF
 
 /** built-in vendor list */
 int builtInVendorIds[] = {
@@ -173,6 +175,7 @@
     VENDOR_ID_INQ_MOBILE,
     VENDOR_ID_SONY,
     VENDOR_ID_LAB126,
+    VENDOR_ID_YULONG_COOLPAD,
 };
 
 #define BUILT_IN_VENDOR_COUNT    (sizeof(builtInVendorIds)/sizeof(builtInVendorIds[0]))
diff --git a/adb/usb_windows.c b/adb/usb_windows.c
index a2fbb79..4936b77 100644
--- a/adb/usb_windows.c
+++ b/adb/usb_windows.c
@@ -255,7 +255,7 @@
 }
 
 int usb_write(usb_handle* handle, const void* data, int len) {
-  unsigned long time_out = 500 + len * 8;
+  unsigned long time_out = 5000;
   unsigned long written = 0;
   int ret;
 
@@ -300,7 +300,7 @@
 }
 
 int usb_read(usb_handle *handle, void* data, int len) {
-  unsigned long time_out = 500 + len * 8;
+  unsigned long time_out = 0;
   unsigned long read = 0;
   int ret;
 
@@ -322,7 +322,7 @@
 
         if (len == 0)
           return 0;
-      } else if (saved_errno != ERROR_SEM_TIMEOUT) {
+      } else {
         // assume ERROR_INVALID_HANDLE indicates we are disconnected
         if (saved_errno == ERROR_INVALID_HANDLE)
           usb_kick(handle);
diff --git a/cpio/mkbootfs.c b/cpio/mkbootfs.c
index f672336..323a09d 100644
--- a/cpio/mkbootfs.c
+++ b/cpio/mkbootfs.c
@@ -3,6 +3,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
+#include <ctype.h>
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -34,12 +35,51 @@
     exit(1);
 }
 
+struct fs_config_entry {
+    char* name;
+    int uid, gid, mode;
+};
+
+static struct fs_config_entry* canned_config = NULL;
+
+/* Each line in the canned file should be a path plus three ints (uid,
+ * gid, mode). */
+#ifdef PATH_MAX
+#define CANNED_LINE_LENGTH  (PATH_MAX+100)
+#else
+#define CANNED_LINE_LENGTH  (1024)
+#endif
+
 static int verbose = 0;
 static int total_size = 0;
 
 static void fix_stat(const char *path, struct stat *s)
 {
-    fs_config(path, S_ISDIR(s->st_mode), &s->st_uid, &s->st_gid, &s->st_mode);
+    if (canned_config) {
+        // Use the list of file uid/gid/modes loaded from the file
+        // given with -f.
+
+        struct fs_config_entry* empty_path_config = NULL;
+        struct fs_config_entry* p;
+        for (p = canned_config; p->name; ++p) {
+            if (!p->name[0]) {
+                empty_path_config = p;
+            }
+            if (strcmp(p->name, path) == 0) {
+                s->st_uid = p->uid;
+                s->st_gid = p->gid;
+                s->st_mode = p->mode | (s->st_mode & ~07777);
+                return;
+            }
+        }
+        s->st_uid = empty_path_config->uid;
+        s->st_gid = empty_path_config->gid;
+        s->st_mode = empty_path_config->mode | (s->st_mode & ~07777);
+    } else {
+        // Use the compiled-in fs_config() function.
+
+        fs_config(path, S_ISDIR(s->st_mode), &s->st_uid, &s->st_gid, &s->st_mode);
+    }
 }
 
 static void _eject(struct stat *s, char *out, int olen, char *data, unsigned datasize)
@@ -79,7 +119,7 @@
 
     total_size += 6 + 8*13 + olen + 1;
 
-    if(strlen(out) != olen) die("ACK!");
+    if(strlen(out) != (unsigned int)olen) die("ACK!");
 
     while(total_size & 3) {
         total_size++;
@@ -235,11 +275,61 @@
     _archive_dir(in, out, strlen(in), strlen(out));
 }
 
+static void read_canned_config(char* filename)
+{
+    int allocated = 8;
+    int used = 0;
+
+    canned_config =
+        (struct fs_config_entry*)malloc(allocated * sizeof(struct fs_config_entry));
+
+    char line[CANNED_LINE_LENGTH];
+    FILE* f = fopen(filename, "r");
+    if (f == NULL) die("failed to open canned file");
+
+    while (fgets(line, CANNED_LINE_LENGTH, f) != NULL) {
+        if (!line[0]) break;
+        if (used >= allocated) {
+            allocated *= 2;
+            canned_config = (struct fs_config_entry*)realloc(
+                canned_config, allocated * sizeof(struct fs_config_entry));
+        }
+
+        struct fs_config_entry* cc = canned_config + used;
+
+        if (isspace(line[0])) {
+            cc->name = strdup("");
+            cc->uid = atoi(strtok(line, " \n"));
+        } else {
+            cc->name = strdup(strtok(line, " \n"));
+            cc->uid = atoi(strtok(NULL, " \n"));
+        }
+        cc->gid = atoi(strtok(NULL, " \n"));
+        cc->mode = strtol(strtok(NULL, " \n"), NULL, 8);
+        ++used;
+    }
+    if (used >= allocated) {
+        ++allocated;
+        canned_config = (struct fs_config_entry*)realloc(
+            canned_config, allocated * sizeof(struct fs_config_entry));
+    }
+    canned_config[used].name = NULL;
+
+    fclose(f);
+}
+
+
 int main(int argc, char *argv[])
 {
     argc--;
     argv++;
 
+    if (argc > 1 && strcmp(argv[0], "-f") == 0) {
+        read_canned_config(argv[1]);
+        argc -= 2;
+        argv += 2;
+    }
+
     if(argc == 0) die("no directories to process?!");
 
     while(argc-- > 0){
diff --git a/debuggerd/utility.c b/debuggerd/utility.c
index 2ccf947..8eb52ba 100644
--- a/debuggerd/utility.c
+++ b/debuggerd/utility.c
@@ -267,13 +267,11 @@
      * Search for a match, or for a hole where the match would be.  The list
      * is backward from the file content, so it starts at high addresses.
      */
-    bool found = false;
     map_info_t* map = context->map_info_list;
     map_info_t *next = NULL;
     map_info_t *prev = NULL;
     while (map != NULL) {
         if (addr >= map->start && addr < map->end) {
-            found = true;
             next = map->next;
             break;
         } else if (addr >= map->end) {
diff --git a/fastboot/engine.c b/fastboot/engine.c
index a1b6539..f5215cf 100644
--- a/fastboot/engine.c
+++ b/fastboot/engine.c
@@ -255,6 +255,7 @@
 #else
     fd = fileno(tmpfile());
 #endif
+    /* reset ext4fs info so we can be called multiple times */
     reset_ext4fs_info();
     info.len = image->partition_size;
     make_ext4fs_internal(fd, NULL, NULL, NULL, 0, 1, 0, 0, 0, NULL);
@@ -266,7 +267,7 @@
     close(fd);
 }
 
-int fb_format(Action *a, usb_handle *usb)
+int fb_format(Action *a, usb_handle *usb, int skip_if_not_supported)
 {
     const char *partition = a->cmd;
     char response[FB_RESPONSE_SZ+1];
@@ -281,6 +282,13 @@
     snprintf(cmd, sizeof(cmd), "getvar:partition-type:%s", partition);
     status = fb_command_response(usb, cmd, response);
     if (status) {
+        if (skip_if_not_supported) {
+            fprintf(stderr,
+                    "Erase successful, but not automatically formatting.\n");
+            fprintf(stderr,
+                    "Can't determine partition type.\n");
+            return 0;
+        }
         fprintf(stderr,"FAILED (%s)\n", fb_get_error());
         return status;
     }
@@ -292,6 +300,13 @@
         }
     }
     if (!generator) {
+        if (skip_if_not_supported) {
+            fprintf(stderr,
+                    "Erase successful, but not automatically formatting.\n");
+            fprintf(stderr,
+                    "File system type %s not supported.\n", response);
+            return 0;
+        }
         fprintf(stderr,"Formatting is not supported for filesystem with type '%s'.\n",
                 response);
         return -1;
@@ -301,6 +316,12 @@
     snprintf(cmd, sizeof(cmd), "getvar:partition-size:%s", partition);
     status = fb_command_response(usb, cmd, response);
     if (status) {
+        if (skip_if_not_supported) {
+            fprintf(stderr,
+                    "Erase successful, but not automatically formatting.\n");
+            fprintf(stderr, "Unable to get partition size\n.");
+            return 0;
+        }
         fprintf(stderr,"FAILED (%s)\n", fb_get_error());
         return status;
     }
@@ -329,11 +350,12 @@
     return status;
 }
 
-void fb_queue_format(const char *partition)
+void fb_queue_format(const char *partition, int skip_if_not_supported)
 {
     Action *a;
 
     a = queue_action(OP_FORMAT, partition);
+    a->data = (void*)skip_if_not_supported;
     a->msg = mkmsg("formatting '%s' partition", partition);
 }
 
@@ -547,7 +569,7 @@
         } else if (a->op == OP_NOTICE) {
             fprintf(stderr,"%s\n",(char*)a->data);
         } else if (a->op == OP_FORMAT) {
-            status = fb_format(a, usb);
+            status = fb_format(a, usb, (int)a->data);
             status = a->func(a, status, status ? fb_get_error() : "");
             if (status) break;
         } else {
diff --git a/fastboot/fastboot.c b/fastboot/fastboot.c
index b96d93b..c44f937 100644
--- a/fastboot/fastboot.c
+++ b/fastboot/fastboot.c
@@ -185,15 +185,13 @@
         if (!serial[0]) {
             serial = "????????????";
         }
+        // output compatible with "adb devices"
         if (!long_listing) {
-            // output compatible with "adb devices"
             printf("%s\tfastboot\n", serial);
+        } else if (!info->device_path) {
+            printf("%-22s fastboot\n", serial);
         } else {
-            char* device_path = info->device_path;
-            if (!device_path[0]) {
-                device_path = "????????????";
-            }
-            printf("%s\t%s\tfastboot\n", serial, device_path);
+            printf("%-22s fastboot %s\n", serial, info->device_path);
         }
     }
 
@@ -654,7 +652,7 @@
             skip(2);
         } else if(!strcmp(*argv, "format")) {
             require(2);
-            fb_queue_format(argv[1]);
+            fb_queue_format(argv[1], 0);
             skip(2);
         } else if(!strcmp(*argv, "signature")) {
             require(2);
@@ -744,7 +742,9 @@
 
     if (wants_wipe) {
         fb_queue_erase("userdata");
+        fb_queue_format("userdata", 1);
         fb_queue_erase("cache");
+        fb_queue_format("cache", 1);
     }
     if (wants_reboot) {
         fb_queue_reboot();
diff --git a/fastboot/fastboot.h b/fastboot/fastboot.h
index c249a8f..90d8a6a 100644
--- a/fastboot/fastboot.h
+++ b/fastboot/fastboot.h
@@ -43,7 +43,7 @@
 /* engine.c - high level command queue engine */
 void fb_queue_flash(const char *ptn, void *data, unsigned sz);;
 void fb_queue_erase(const char *ptn);
-void fb_queue_format(const char *ptn);
+void fb_queue_format(const char *ptn, int skip_if_not_supported);
 void fb_queue_require(const char *prod, const char *var, int invert,
         unsigned nvalues, const char **value);
 void fb_queue_display(const char *var, const char *prettyname);
diff --git a/include/arch/darwin-x86/AndroidConfig.h b/include/arch/darwin-x86/AndroidConfig.h
index 48f8d9a..9da01c5 100644
--- a/include/arch/darwin-x86/AndroidConfig.h
+++ b/include/arch/darwin-x86/AndroidConfig.h
@@ -175,7 +175,7 @@
  * with a memory address.  If not defined, stack crawls will not have symbolic
  * information.
  */
-#define HAVE_DLADDR 0
+#define HAVE_DLADDR 1
 
 /*
  * Defined if we have the cxxabi.h header for demangling C++ symbols.  If
diff --git a/include/corkscrew/map_info.h b/include/corkscrew/map_info.h
index c5cd8f8..ea1d35f 100644
--- a/include/corkscrew/map_info.h
+++ b/include/corkscrew/map_info.h
@@ -21,6 +21,7 @@
 
 #include <sys/types.h>
 #include <stdbool.h>
+#include <stdint.h>
 
 #ifdef __cplusplus
 extern "C" {
diff --git a/include/corkscrew/ptrace.h b/include/corkscrew/ptrace.h
index 172e348..0040c22 100644
--- a/include/corkscrew/ptrace.h
+++ b/include/corkscrew/ptrace.h
@@ -24,6 +24,7 @@
 
 #include <sys/types.h>
 #include <stdbool.h>
+#include <stdint.h>
 
 #ifdef __cplusplus
 extern "C" {
diff --git a/include/corkscrew/symbol_table.h b/include/corkscrew/symbol_table.h
index 020c8b8..4998750 100644
--- a/include/corkscrew/symbol_table.h
+++ b/include/corkscrew/symbol_table.h
@@ -17,6 +17,7 @@
 #ifndef _CORKSCREW_SYMBOL_TABLE_H
 #define _CORKSCREW_SYMBOL_TABLE_H
 
+#include <stdint.h>
 #include <sys/types.h>
 
 #ifdef __cplusplus
diff --git a/include/private/android_filesystem_config.h b/include/private/android_filesystem_config.h
index 68928eb..6521cbe 100644
--- a/include/private/android_filesystem_config.h
+++ b/include/private/android_filesystem_config.h
@@ -138,7 +138,7 @@
 
 #define android_id_count \
     (sizeof(android_ids) / sizeof(android_ids[0]))
-    
+
 struct fs_path_config {
     unsigned mode;
     unsigned uid;
@@ -234,7 +234,7 @@
 {
     struct fs_path_config *pc;
     int plen;
-    
+
     pc = dir ? android_dirs : android_files;
     plen = strlen(path);
     for(; pc->prefix; pc++){
@@ -254,9 +254,9 @@
     *uid = pc->uid;
     *gid = pc->gid;
     *mode = (*mode & (~07777)) | pc->mode;
-    
+
 #if 0
-    fprintf(stderr,"< '%s' '%s' %d %d %o >\n", 
+    fprintf(stderr,"< '%s' '%s' %d %d %o >\n",
             path, pc->prefix ? pc->prefix : "", *uid, *gid, *mode);
 #endif
 }
diff --git a/include/system/audio_policy.h b/include/system/audio_policy.h
index 57f7110..91b8e9c 100644
--- a/include/system/audio_policy.h
+++ b/include/system/audio_policy.h
@@ -42,6 +42,7 @@
     AUDIO_POLICY_FORCE_BT_DESK_DOCK,
     AUDIO_POLICY_FORCE_ANALOG_DOCK,
     AUDIO_POLICY_FORCE_DIGITAL_DOCK,
+    AUDIO_POLICY_FORCE_NO_BT_A2DP, /* A2DP sink is not preferred to speaker or wired HS */
 
     AUDIO_POLICY_FORCE_CFG_CNT,
     AUDIO_POLICY_FORCE_CFG_MAX = AUDIO_POLICY_FORCE_CFG_CNT - 1,
diff --git a/include/system/camera.h b/include/system/camera.h
index b8389b1..e4cacc5 100644
--- a/include/system/camera.h
+++ b/include/system/camera.h
@@ -137,7 +137,8 @@
      * KEY_FOCUS_AREAS and KEY_METERING_AREAS have no effect.
      *
      * arg1 is the face detection type. It can be CAMERA_FACE_DETECTION_HW or
-     * CAMERA_FACE_DETECTION_SW.
+     * CAMERA_FACE_DETECTION_SW. If the type of face detection requested is not
+     * supported, the HAL must return BAD_VALUE.
      */
     CAMERA_CMD_START_FACE_DETECTION = 6,
 
diff --git a/include/system/window.h b/include/system/window.h
index 1a036df..8e00bcd 100644
--- a/include/system/window.h
+++ b/include/system/window.h
@@ -248,7 +248,7 @@
     NATIVE_WINDOW_API_CONNECT               = 13,   /* private */
     NATIVE_WINDOW_API_DISCONNECT            = 14,   /* private */
     NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS = 15, /* private */
-    NATIVE_WINDOW_SET_ACTIVE_RECT           = 16,   /* private */
+    NATIVE_WINDOW_SET_POST_TRANSFORM_CROP   = 16,   /* private */
 };
 
 /* parameter for NATIVE_WINDOW_[API_][DIS]CONNECT */
@@ -298,6 +298,11 @@
      * of the buffer matches the window size (cropping in the process)
      */
     NATIVE_WINDOW_SCALING_MODE_SCALE_CROP       = 2,
+    /* the window is clipped to the size of the buffer's crop rectangle; pixels
+     * outside the crop rectangle are treated as if they are completely
+     * transparent.
+     */
+    NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP    = 3,
 };
 
 /* values returned by the NATIVE_WINDOW_CONCRETE_TYPE query */
@@ -440,7 +445,7 @@
      *     NATIVE_WINDOW_API_CONNECT            (private)
      *     NATIVE_WINDOW_API_DISCONNECT         (private)
      *     NATIVE_WINDOW_SET_BUFFERS_USER_DIMENSIONS (private)
-     *     NATIVE_WINDOW_SET_ACTIVE_RECT        (private)
+     *     NATIVE_WINDOW_SET_POST_TRANSFORM_CROP (private)
      *
      */
 
@@ -504,14 +509,16 @@
 /*
  * native_window_set_crop(..., crop)
  * Sets which region of the next queued buffers needs to be considered.
- * A buffer's crop region is scaled to match the surface's size.
+ * Depending on the scaling mode, a buffer's crop region is scaled and/or
+ * cropped to match the surface's size.  This function sets the crop in
+ * pre-transformed buffer pixel coordinates.
  *
  * The specified crop region applies to all buffers queued after it is called.
  *
- * if 'crop' is NULL, subsequently queued buffers won't be cropped.
+ * If 'crop' is NULL, subsequently queued buffers won't be cropped.
  *
- * An error is returned if for instance the crop region is invalid,
- * out of the buffer's bound or if the window is invalid.
+ * An error is returned if for instance the crop region is invalid, out of the
+ * buffer's bound or if the window is invalid.
  */
 static inline int native_window_set_crop(
         struct ANativeWindow* window,
@@ -521,23 +528,38 @@
 }
 
 /*
+ * native_window_set_post_transform_crop(..., crop)
+ * Sets which region of the next queued buffers needs to be considered.
+ * Depending on the scaling mode, a buffer's crop region is scaled and/or
+ * cropped to match the surface's size.  This function sets the crop in
+ * post-transformed pixel coordinates.
+ *
+ * The specified crop region applies to all buffers queued after it is called.
+ *
+ * If 'crop' is NULL, subsequently queued buffers won't be cropped.
+ *
+ * An error is returned if for instance the crop region is invalid, out of the
+ * buffer's bound or if the window is invalid.
+ */
+static inline int native_window_set_post_transform_crop(
+        struct ANativeWindow* window,
+        android_native_rect_t const * crop)
+{
+    return window->perform(window, NATIVE_WINDOW_SET_POST_TRANSFORM_CROP, crop);
+}
+
+/*
  * native_window_set_active_rect(..., active_rect)
- * Sets the region of future queued buffers that are 'active'.  Pixels outside
- * this 'active' region are considered to be completely transparent regardless
- * of the pixel values in the buffer.  The active_rect argument specifies the
- * active rectangle in buffer pixel coordinates.
  *
- * The specified active rectangle applies to all buffers queued after it is
- * called.
- *
- * An error is returned if for instance the crop region is invalid,
- * out of the buffer's bound or if the window is invalid.
+ * This function is deprectated and will be removed soon.  For now it simply
+ * sets the post-transform crop for compatibility while multi-project commits
+ * get checked.
  */
 static inline int native_window_set_active_rect(
         struct ANativeWindow* window,
         android_native_rect_t const * active_rect)
 {
-    return window->perform(window, NATIVE_WINDOW_SET_ACTIVE_RECT, active_rect);
+    return native_window_set_post_transform_crop(window, active_rect);
 }
 
 /*
diff --git a/libcorkscrew/Android.mk b/libcorkscrew/Android.mk
index 433f93c..9019986 100644
--- a/libcorkscrew/Android.mk
+++ b/libcorkscrew/Android.mk
@@ -14,9 +14,7 @@
 
 LOCAL_PATH:= $(call my-dir)
 
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES := \
+generic_src_files := \
 	backtrace.c \
 	backtrace-helper.c \
 	demangle.c \
@@ -24,16 +22,24 @@
 	ptrace.c \
 	symbol_table.c
 
-ifeq ($(TARGET_ARCH),arm)
-LOCAL_SRC_FILES += \
+arm_src_files := \
 	arch-arm/backtrace-arm.c \
 	arch-arm/ptrace-arm.c
+
+x86_src_files := \
+	arch-x86/backtrace-x86.c \
+	arch-x86/ptrace-x86.c
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := $(generic_src_files)
+
+ifeq ($(TARGET_ARCH),arm)
+LOCAL_SRC_FILES += $(arm_src_files)
 LOCAL_CFLAGS += -DCORKSCREW_HAVE_ARCH
 endif
 ifeq ($(TARGET_ARCH),x86)
-LOCAL_SRC_FILES += \
-	arch-x86/backtrace-x86.c \
-	arch-x86/ptrace-x86.c
+LOCAL_SRC_FILES += $(x86_src_files)
 LOCAL_CFLAGS += -DCORKSCREW_HAVE_ARCH
 endif
 
@@ -44,3 +50,38 @@
 LOCAL_MODULE_TAGS := optional
 
 include $(BUILD_SHARED_LIBRARY)
+
+# Build test.
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := test.c
+LOCAL_CFLAGS += -std=gnu99 -Werror -fno-inline-small-functions
+LOCAL_SHARED_LIBRARIES := libcorkscrew
+LOCAL_MODULE := libcorkscrew_test
+LOCAL_MODULE_TAGS := optional
+include $(BUILD_EXECUTABLE)
+
+
+ifeq ($(HOST_OS)-$(HOST_ARCH),linux-x86)
+
+# Build libcorkscrew.
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES += $(generic_src_files) $(x86_src_files)
+LOCAL_CFLAGS += -DCORKSCREW_HAVE_ARCH
+LOCAL_SHARED_LIBRARIES += libgccdemangle
+LOCAL_STATIC_LIBRARIES += libcutils
+LOCAL_LDLIBS += -ldl -lrt
+LOCAL_CFLAGS += -std=gnu99 -Werror
+LOCAL_MODULE := libcorkscrew
+LOCAL_MODULE_TAGS := optional
+include $(BUILD_HOST_SHARED_LIBRARY)
+
+# Build test.
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := test.c
+LOCAL_CFLAGS += -std=gnu99 -Werror -fno-inline-small-functions
+LOCAL_SHARED_LIBRARIES := libcorkscrew
+LOCAL_MODULE := libcorkscrew_test
+LOCAL_MODULE_TAGS := optional
+include $(BUILD_HOST_EXECUTABLE)
+
+endif # linux-x86
diff --git a/libcorkscrew/arch-x86/backtrace-x86.c b/libcorkscrew/arch-x86/backtrace-x86.c
index 24fadcb..01d9ed5 100644
--- a/libcorkscrew/arch-x86/backtrace-x86.c
+++ b/libcorkscrew/arch-x86/backtrace-x86.c
@@ -31,40 +31,22 @@
 #include <limits.h>
 #include <errno.h>
 #include <sys/ptrace.h>
-#include <sys/exec_elf.h>
 #include <cutils/log.h>
 
-/* Machine context at the time a signal was raised. */
-typedef struct ucontext {
-    uint32_t uc_flags;
-    struct ucontext* uc_link;
-    stack_t uc_stack;
-    struct sigcontext {
-        uint32_t gs;
-        uint32_t fs;
-        uint32_t es;
-        uint32_t ds;
-        uint32_t edi;
-        uint32_t esi;
-        uint32_t ebp;
-        uint32_t esp;
-        uint32_t ebx;
-        uint32_t edx;
-        uint32_t ecx;
-        uint32_t eax;
-        uint32_t trapno;
-        uint32_t err;
-        uint32_t eip;
-        uint32_t cs;
-        uint32_t efl;
-        uint32_t uesp;
-        uint32_t ss;
-        void* fpregs;
-        uint32_t oldmask;
-        uint32_t cr2;
-    } uc_mcontext;
-    uint32_t uc_sigmask;
-} ucontext_t;
+#if defined(__BIONIC__)
+
+// Bionic offers the Linux kernel headers.
+#include <asm/sigcontext.h>
+#include <asm/ucontext.h>
+typedef struct ucontext ucontext_t;
+
+#else
+
+// glibc has its own renaming of the Linux kernel's structures.
+#define __USE_GNU // For REG_EBP, REG_ESP, and REG_EIP.
+#include <ucontext.h>
+
+#endif
 
 /* Unwind state. */
 typedef struct {
@@ -114,9 +96,15 @@
     const ucontext_t* uc = (const ucontext_t*)sigcontext;
 
     unwind_state_t state;
+#if defined(__BIONIC__)
     state.ebp = uc->uc_mcontext.ebp;
-    state.eip = uc->uc_mcontext.eip;
     state.esp = uc->uc_mcontext.esp;
+    state.eip = uc->uc_mcontext.eip;
+#else
+    state.ebp = uc->uc_mcontext.gregs[REG_EBP];
+    state.esp = uc->uc_mcontext.gregs[REG_ESP];
+    state.eip = uc->uc_mcontext.gregs[REG_EIP];
+#endif
 
     memory_t memory;
     init_memory(&memory, map_info_list);
diff --git a/libcorkscrew/backtrace.c b/libcorkscrew/backtrace.c
index fa21574..eec53a2 100644
--- a/libcorkscrew/backtrace.c
+++ b/libcorkscrew/backtrace.c
@@ -27,16 +27,41 @@
 
 #include <unistd.h>
 #include <signal.h>
+#include <stdlib.h>
+#include <string.h>
 #include <pthread.h>
 #include <unwind.h>
-#include <sys/exec_elf.h>
 #include <cutils/log.h>
 #include <cutils/atomic.h>
+#include <elf.h>
 
 #if HAVE_DLADDR
+#define __USE_GNU // For dladdr(3) in glibc.
 #include <dlfcn.h>
 #endif
 
+#if defined(__BIONIC__)
+
+// Bionic implements and exports gettid but only implements tgkill.
+extern int tgkill(int tgid, int tid, int sig);
+
+#else
+
+// glibc doesn't implement or export either gettid or tgkill.
+
+#include <unistd.h>
+#include <sys/syscall.h>
+
+static pid_t gettid() {
+  return syscall(__NR_gettid);
+}
+
+static int tgkill(int tgid, int tid, int sig) {
+  return syscall(__NR_tgkill, tgid, tid, sig);
+}
+
+#endif
+
 typedef struct {
     backtrace_frame_t* backtrace;
     size_t ignore_depth;
@@ -115,8 +140,6 @@
 }
 #endif
 
-extern int tgkill(int tgid, int tid, int sig);
-
 ssize_t unwind_backtrace_thread(pid_t tid, backtrace_frame_t* backtrace,
         size_t ignore_depth, size_t max_depth) {
     if (tid == gettid()) {
diff --git a/libcorkscrew/map_info.c b/libcorkscrew/map_info.c
index f33378f..3c52854 100644
--- a/libcorkscrew/map_info.c
+++ b/libcorkscrew/map_info.c
@@ -21,6 +21,7 @@
 
 #include <ctype.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <limits.h>
 #include <pthread.h>
diff --git a/libcorkscrew/ptrace.c b/libcorkscrew/ptrace.c
index cbea8ca..6496d5e 100644
--- a/libcorkscrew/ptrace.c
+++ b/libcorkscrew/ptrace.c
@@ -21,6 +21,7 @@
 #include <corkscrew/ptrace.h>
 
 #include <errno.h>
+#include <stdlib.h>
 #include <sys/ptrace.h>
 #include <cutils/log.h>
 
diff --git a/libcorkscrew/symbol_table.c b/libcorkscrew/symbol_table.c
index 1b97180..29e4a79 100644
--- a/libcorkscrew/symbol_table.c
+++ b/libcorkscrew/symbol_table.c
@@ -19,14 +19,22 @@
 
 #include <corkscrew/symbol_table.h>
 
+#include <stdbool.h>
 #include <stdlib.h>
+#include <elf.h>
 #include <fcntl.h>
 #include <string.h>
 #include <sys/stat.h>
 #include <sys/mman.h>
-#include <sys/exec_elf.h>
 #include <cutils/log.h>
 
+static bool is_elf(Elf32_Ehdr* e) {
+    return (e->e_ident[EI_MAG0] == ELFMAG0 &&
+            e->e_ident[EI_MAG1] == ELFMAG1 &&
+            e->e_ident[EI_MAG2] == ELFMAG2 &&
+            e->e_ident[EI_MAG3] == ELFMAG3);
+}
+
 // Compare function for qsort
 static int qcompar(const void *a, const void *b) {
     const symbol_t* asym = (const symbol_t*)a;
@@ -67,7 +75,7 @@
 
     // Parse the file header
     Elf32_Ehdr *hdr = (Elf32_Ehdr*)base;
-    if (!IS_ELF(*hdr)) {
+    if (!is_elf(hdr)) {
         goto out_close;
     }
     Elf32_Shdr *shdr = (Elf32_Shdr*)(base + hdr->e_shoff);
diff --git a/libcorkscrew/test.c b/libcorkscrew/test.c
new file mode 100644
index 0000000..af34c03
--- /dev/null
+++ b/libcorkscrew/test.c
@@ -0,0 +1,64 @@
+#include <corkscrew/backtrace.h>
+#include <corkscrew/symbol_table.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+void do_backtrace() {
+  const size_t MAX_DEPTH = 32;
+  backtrace_frame_t* frames = (backtrace_frame_t*) malloc(sizeof(backtrace_frame_t) * MAX_DEPTH);
+  ssize_t frame_count = unwind_backtrace(frames, 0, MAX_DEPTH);
+  fprintf(stderr, "frame_count=%d\n", (int) frame_count);
+
+  backtrace_symbol_t* backtrace_symbols = (backtrace_symbol_t*) malloc(sizeof(backtrace_symbol_t) * frame_count);
+  get_backtrace_symbols(frames, frame_count, backtrace_symbols);
+
+  for (size_t i = 0; i < (size_t) frame_count; ++i) {
+    char line[MAX_BACKTRACE_LINE_LENGTH];
+    format_backtrace_line(i, &frames[i], &backtrace_symbols[i],
+                          line, MAX_BACKTRACE_LINE_LENGTH);
+    if (backtrace_symbols[i].symbol_name != NULL) {
+      // get_backtrace_symbols found the symbol's name with dladdr(3).
+      fprintf(stderr, "  %s\n", line);
+    } else {
+      // We don't have a symbol. Maybe this is a static symbol, and
+      // we can look it up?
+      symbol_table_t* symbols = NULL;
+      if (backtrace_symbols[i].map_name != NULL) {
+        symbols = load_symbol_table(backtrace_symbols[i].map_name);
+      }
+      const symbol_t* symbol = NULL;
+      if (symbols != NULL) {
+        symbol = find_symbol(symbols, frames[i].absolute_pc);
+      }
+      if (symbol != NULL) {
+        uintptr_t offset = frames[i].absolute_pc - symbol->start;
+        fprintf(stderr, "  %s (%s%+d)\n", line, symbol->name, offset);
+      } else {
+        fprintf(stderr, "  %s (\?\?\?)\n", line);
+      }
+      free_symbol_table(symbols);
+    }
+  }
+
+  free_backtrace_symbols(backtrace_symbols, frame_count);
+  free(backtrace_symbols);
+  free(frames);
+}
+
+__attribute__ ((noinline)) void g() {
+  fprintf(stderr, "g()\n");
+  do_backtrace();
+}
+
+__attribute__ ((noinline)) int f(int i) {
+  fprintf(stderr, "f(%i)\n", i);
+  if (i == 0) {
+    g();
+    return 0;
+  }
+  return f(i - 1);
+}
+
+int main() {
+  return f(5);
+}
diff --git a/libcutils/qtaguid.c b/libcutils/qtaguid.c
index 1c57774..5bb8176 100644
--- a/libcutils/qtaguid.c
+++ b/libcutils/qtaguid.c
@@ -99,9 +99,7 @@
 int qtaguid_tagSocket(int sockfd, int tag, uid_t uid) {
     char lineBuf[CTRL_MAX_INPUT_LEN];
     int res;
-    /* Doing java-land a favor, enforcing "long" */
-    uint64_t kTag = ((uint64_t)tag << 32) & ~(1LLU<<63);
-
+    uint64_t kTag = ((uint64_t)tag << 32);
 
     pthread_once(&resTrackInitDone, qtaguid_resTrack);
 
diff --git a/libcutils/sched_policy.c b/libcutils/sched_policy.c
index 20771c0..d20d217 100644
--- a/libcutils/sched_policy.c
+++ b/libcutils/sched_policy.c
@@ -16,24 +16,31 @@
 ** limitations under the License.
 */
 
+#define LOG_TAG "SchedPolicy"
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <cutils/sched_policy.h>
+#include <cutils/log.h>
 
-#define LOG_TAG "SchedPolicy"
-#include "cutils/log.h"
+/* Re-map SP_DEFAULT to the system default policy, and leave other values unchanged.
+ * Call this any place a SchedPolicy is used as an input parameter.
+ * Returns the possibly re-mapped policy.
+ */
+static inline SchedPolicy _policy(SchedPolicy p)
+{
+   return p == SP_DEFAULT ? SP_SYSTEM_DEFAULT : p;
+}
 
-#ifdef HAVE_SCHED_H
-#ifdef HAVE_PTHREADS
+#if defined(HAVE_ANDROID_OS) && defined(HAVE_SCHED_H) && defined(HAVE_PTHREADS)
 
 #include <sched.h>
 #include <pthread.h>
 
-#include <cutils/sched_policy.h>
-
 #ifndef SCHED_NORMAL
   #define SCHED_NORMAL 0
 #endif
@@ -56,8 +63,6 @@
 #if CAN_SET_SP_SYSTEM
 static int system_cgroup_fd = -1;
 #endif
-static int audio_app_cgroup_fd = -1;
-static int audio_sys_cgroup_fd = -1;
 
 /* Add tid to the scheduling group defined by the policy */
 static int add_tid_to_cgroup(int tid, SchedPolicy policy)
@@ -69,6 +74,8 @@
         fd = bg_cgroup_fd;
         break;
     case SP_FOREGROUND:
+    case SP_AUDIO_APP:
+    case SP_AUDIO_SYS:
         fd = fg_cgroup_fd;
         break;
 #if CAN_SET_SP_SYSTEM
@@ -76,12 +83,6 @@
         fd = system_cgroup_fd;
         break;
 #endif
-    case SP_AUDIO_APP:
-        fd = audio_app_cgroup_fd;
-        break;
-    case SP_AUDIO_SYS:
-        fd = audio_sys_cgroup_fd;
-        break;
     default:
         fd = -1;
         break;
@@ -124,36 +125,23 @@
 
 #if CAN_SET_SP_SYSTEM
         filename = "/dev/cpuctl/tasks";
-        system_cgroup_fd = open(filename, O_WRONLY);
+        system_cgroup_fd = open(filename, O_WRONLY | O_CLOEXEC);
         if (system_cgroup_fd < 0) {
             SLOGV("open of %s failed: %s\n", filename, strerror(errno));
         }
 #endif
 
-        filename = "/dev/cpuctl/foreground/tasks";
-        fg_cgroup_fd = open(filename, O_WRONLY);
+        filename = "/dev/cpuctl/apps/tasks";
+        fg_cgroup_fd = open(filename, O_WRONLY | O_CLOEXEC);
         if (fg_cgroup_fd < 0) {
             SLOGE("open of %s failed: %s\n", filename, strerror(errno));
         }
 
-        filename = "/dev/cpuctl/bg_non_interactive/tasks";
-        bg_cgroup_fd = open(filename, O_WRONLY);
+        filename = "/dev/cpuctl/apps/bg_non_interactive/tasks";
+        bg_cgroup_fd = open(filename, O_WRONLY | O_CLOEXEC);
         if (bg_cgroup_fd < 0) {
             SLOGE("open of %s failed: %s\n", filename, strerror(errno));
         }
-
-        filename = "/dev/cpuctl/audio_app/tasks";
-        audio_app_cgroup_fd = open(filename, O_WRONLY);
-        if (audio_app_cgroup_fd < 0) {
-            SLOGV("open of %s failed: %s\n", filename, strerror(errno));
-        }
-
-        filename = "/dev/cpuctl/audio_sys/tasks";
-        audio_sys_cgroup_fd = open(filename, O_WRONLY);
-        if (audio_sys_cgroup_fd < 0) {
-            SLOGV("open of %s failed: %s\n", filename, strerror(errno));
-        }
-
     } else {
         __sys_supports_schedgroups = 0;
     }
@@ -246,14 +234,10 @@
             return -1;
         if (grpBuf[0] == '\0') {
             *policy = SP_SYSTEM;
-        } else if (!strcmp(grpBuf, "bg_non_interactive")) {
+        } else if (!strcmp(grpBuf, "apps/bg_non_interactive")) {
             *policy = SP_BACKGROUND;
-        } else if (!strcmp(grpBuf, "foreground")) {
+        } else if (!strcmp(grpBuf, "apps")) {
             *policy = SP_FOREGROUND;
-        } else if (!strcmp(grpBuf, "audio_app")) {
-            *policy = SP_AUDIO_APP;
-        } else if (!strcmp(grpBuf, "audio_sys")) {
-            *policy = SP_AUDIO_SYS;
         } else {
             errno = ERANGE;
             return -1;
@@ -274,15 +258,6 @@
     return 0;
 }
 
-/* Re-map SP_DEFAULT to the system default policy, and leave other values unchanged.
- * Call this any place a SchedPolicy is used as an input parameter.
- * Returns the possibly re-mapped policy.
- */
-static inline SchedPolicy _policy(SchedPolicy p)
-{
-   return p == SP_DEFAULT ? SP_SYSTEM_DEFAULT : p;
-}
-
 int set_sched_policy(int tid, SchedPolicy policy)
 {
 #ifdef HAVE_GETTID
@@ -321,17 +296,13 @@
         SLOGD("vvv tid %d (%s)", tid, thread_name);
         break;
     case SP_FOREGROUND:
+    case SP_AUDIO_APP:
+    case SP_AUDIO_SYS:
         SLOGD("^^^ tid %d (%s)", tid, thread_name);
         break;
     case SP_SYSTEM:
         SLOGD("/// tid %d (%s)", tid, thread_name);
         break;
-    case SP_AUDIO_APP:
-        SLOGD("aaa tid %d (%s)", tid, thread_name);
-        break;
-    case SP_AUDIO_SYS:
-        SLOGD("sss tid %d (%s)", tid, thread_name);
-        break;
     default:
         SLOGD("??? tid %d (%s)", tid, thread_name);
         break;
@@ -356,6 +327,23 @@
     return 0;
 }
 
+#else
+
+/* Stubs for non-Android targets. */
+
+int set_sched_policy(int tid, SchedPolicy policy)
+{
+    return 0;
+}
+
+int get_sched_policy(int tid, SchedPolicy *policy)
+{
+    *policy = SP_SYSTEM_DEFAULT;
+    return 0;
+}
+
+#endif
+
 const char *get_sched_policy_name(SchedPolicy policy)
 {
     policy = _policy(policy);
@@ -372,5 +360,3 @@
         return "error";
 }
 
-#endif /* HAVE_PTHREADS */
-#endif /* HAVE_SCHED_H */
diff --git a/liblog/Android.mk b/liblog/Android.mk
index bd2590e..be5cec2 100644
--- a/liblog/Android.mk
+++ b/liblog/Android.mk
@@ -41,7 +41,7 @@
 liblog_host_sources := $(liblog_sources) fake_log_device.c
 
 
-# Static library for host
+# Shared and static library for host
 # ========================================================
 LOCAL_MODULE := liblog
 LOCAL_SRC_FILES := $(liblog_host_sources)
@@ -49,6 +49,11 @@
 LOCAL_CFLAGS := -DFAKE_LOG_DEVICE=1
 include $(BUILD_HOST_STATIC_LIBRARY)
 
+include $(CLEAR_VARS)
+LOCAL_MODULE := liblog
+LOCAL_WHOLE_STATIC_LIBRARIES := liblog
+include $(BUILD_HOST_SHARED_LIBRARY)
+
 
 # Static library for host, 64-bit
 # ========================================================
diff --git a/libnetutils/dhcp_utils.c b/libnetutils/dhcp_utils.c
index 398d9c4..903a1db 100644
--- a/libnetutils/dhcp_utils.c
+++ b/libnetutils/dhcp_utils.c
@@ -33,8 +33,28 @@
                                   /* when polling for property values */
 static const char DAEMON_NAME_RENEW[]  = "iprenew";
 static char errmsg[100];
-/* interface suffix on dhcpcd */
-#define MAX_DAEMON_SUFFIX 25
+/* interface length for dhcpcd daemon start (dhcpcd_<interface> as defined in init.rc file)
+ * or for filling up system properties dhcpcd.<interface>.ipaddress, dhcpcd.<interface>.dns1
+ * and other properties on a successful bind
+ */
+#define MAX_INTERFACE_LENGTH 25
+
+/*
+ * P2p interface names increase sequentially p2p-p2p0-1, p2p-p2p0-2.. after
+ * group formation. This does not work well with system properties which can quickly
+ * exhaust or for specifiying a dhcp start target in init which requires
+ * interface to be pre-defined in init.rc file.
+ *
+ * This function returns a common string p2p for all p2p interfaces.
+ */
+void get_p2p_interface_replacement(const char *interface, char *p2p_interface) {
+    /* Use p2p for any interface starting with p2p. */
+    if (strncmp(interface, "p2p",3) == 0) {
+        strncpy(p2p_interface, "p2p", MAX_INTERFACE_LENGTH);
+    } else {
+        strncpy(p2p_interface, interface, MAX_INTERFACE_LENGTH);
+    }
+}
 
 /*
  * Wait for a system property to be assigned a specified value.
@@ -75,14 +95,18 @@
 {
     char prop_name[PROPERTY_KEY_MAX];
     char prop_value[PROPERTY_VALUE_MAX];
+    /* Interface name after converting p2p0-p2p0-X to p2p to reuse system properties */
+    char p2p_interface[MAX_INTERFACE_LENGTH];
 
-    snprintf(prop_name, sizeof(prop_name), "%s.%s.ipaddress", DHCP_PROP_NAME_PREFIX, interface);
+    get_p2p_interface_replacement(interface, p2p_interface);
+
+    snprintf(prop_name, sizeof(prop_name), "%s.%s.ipaddress", DHCP_PROP_NAME_PREFIX, p2p_interface);
     property_get(prop_name, ipaddr, NULL);
 
-    snprintf(prop_name, sizeof(prop_name), "%s.%s.gateway", DHCP_PROP_NAME_PREFIX, interface);
+    snprintf(prop_name, sizeof(prop_name), "%s.%s.gateway", DHCP_PROP_NAME_PREFIX, p2p_interface);
     property_get(prop_name, gateway, NULL);
 
-    snprintf(prop_name, sizeof(prop_name), "%s.%s.server", DHCP_PROP_NAME_PREFIX, interface);
+    snprintf(prop_name, sizeof(prop_name), "%s.%s.server", DHCP_PROP_NAME_PREFIX, p2p_interface);
     property_get(prop_name, server, NULL);
 
     //TODO: Handle IPv6 when we change system property usage
@@ -91,7 +115,7 @@
         strncpy(gateway, server, PROPERTY_VALUE_MAX);
     }
 
-    snprintf(prop_name, sizeof(prop_name), "%s.%s.mask", DHCP_PROP_NAME_PREFIX, interface);
+    snprintf(prop_name, sizeof(prop_name), "%s.%s.mask", DHCP_PROP_NAME_PREFIX, p2p_interface);
     if (property_get(prop_name, prop_value, NULL)) {
         int p;
         // this conversion is v4 only, but this dhcp client is v4 only anyway
@@ -113,18 +137,19 @@
         }
         *prefixLength = p;
     }
-    snprintf(prop_name, sizeof(prop_name), "%s.%s.dns1", DHCP_PROP_NAME_PREFIX, interface);
+    snprintf(prop_name, sizeof(prop_name), "%s.%s.dns1", DHCP_PROP_NAME_PREFIX, p2p_interface);
     property_get(prop_name, dns1, NULL);
 
-    snprintf(prop_name, sizeof(prop_name), "%s.%s.dns2", DHCP_PROP_NAME_PREFIX, interface);
+    snprintf(prop_name, sizeof(prop_name), "%s.%s.dns2", DHCP_PROP_NAME_PREFIX, p2p_interface);
     property_get(prop_name, dns2, NULL);
 
-    snprintf(prop_name, sizeof(prop_name), "%s.%s.leasetime", DHCP_PROP_NAME_PREFIX, interface);
+    snprintf(prop_name, sizeof(prop_name), "%s.%s.leasetime", DHCP_PROP_NAME_PREFIX, p2p_interface);
     if (property_get(prop_name, prop_value, NULL)) {
         *lease = atol(prop_value);
     }
 
-    snprintf(prop_name, sizeof(prop_name), "%s.%s.vendorInfo", DHCP_PROP_NAME_PREFIX, interface);
+    snprintf(prop_name, sizeof(prop_name), "%s.%s.vendorInfo", DHCP_PROP_NAME_PREFIX,
+            p2p_interface);
     property_get(prop_name, vendorInfo, NULL);
 
     return 0;
@@ -138,15 +163,6 @@
     return inet_ntoa(in_addr);
 }
 
-void get_daemon_suffix(const char *interface, char *daemon_suffix) {
-    /* Use p2p suffix for any p2p interface. */
-    if (strncmp(interface, "p2p",3) == 0) {
-        sprintf(daemon_suffix, "p2p");
-    } else {
-        snprintf(daemon_suffix, MAX_DAEMON_SUFFIX, "%s", interface);
-    }
-}
-
 /*
  * Start the dhcp client daemon, and wait for it to finish
  * configuring the interface.
@@ -172,27 +188,28 @@
     char daemon_cmd[PROPERTY_VALUE_MAX * 2];
     const char *ctrl_prop = "ctl.start";
     const char *desired_status = "running";
-    char daemon_suffix[MAX_DAEMON_SUFFIX];
+    /* Interface name after converting p2p0-p2p0-X to p2p to reuse system properties */
+    char p2p_interface[MAX_INTERFACE_LENGTH];
 
-    get_daemon_suffix(interface, daemon_suffix);
+    get_p2p_interface_replacement(interface, p2p_interface);
 
     snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
             DHCP_PROP_NAME_PREFIX,
-            interface);
+            p2p_interface);
 
     snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
             DAEMON_PROP_NAME,
-            daemon_suffix);
+            p2p_interface);
 
     /* Erase any previous setting of the dhcp result property */
     property_set(result_prop_name, "");
 
     /* Start the daemon and wait until it's ready */
     if (property_get(HOSTNAME_PROP_NAME, prop_value, NULL) && (prop_value[0] != '\0'))
-        snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-h %s %s", DAEMON_NAME, daemon_suffix,
+        snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:-h %s %s", DAEMON_NAME, p2p_interface,
                  prop_value, interface);
     else
-        snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME, daemon_suffix, interface);
+        snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME, p2p_interface, interface);
     memset(prop_value, '\0', PROPERTY_VALUE_MAX);
     property_set(ctrl_prop, daemon_cmd);
     if (wait_for_property(daemon_prop_name, desired_status, 10) < 0) {
@@ -242,19 +259,19 @@
     const char *ctrl_prop = "ctl.stop";
     const char *desired_status = "stopped";
 
-    char daemon_suffix[MAX_DAEMON_SUFFIX];
+    char p2p_interface[MAX_INTERFACE_LENGTH];
 
-    get_daemon_suffix(interface, daemon_suffix);
+    get_p2p_interface_replacement(interface, p2p_interface);
 
     snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
             DHCP_PROP_NAME_PREFIX,
-            interface);
+            p2p_interface);
 
     snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
             DAEMON_PROP_NAME,
-            daemon_suffix);
+            p2p_interface);
 
-    snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, daemon_suffix);
+    snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, p2p_interface);
 
     /* Stop the daemon and wait until it's reported to be stopped */
     property_set(ctrl_prop, daemon_cmd);
@@ -275,15 +292,15 @@
     const char *ctrl_prop = "ctl.stop";
     const char *desired_status = "stopped";
 
-    char daemon_suffix[MAX_DAEMON_SUFFIX];
+    char p2p_interface[MAX_INTERFACE_LENGTH];
 
-    get_daemon_suffix(interface, daemon_suffix);
+    get_p2p_interface_replacement(interface, p2p_interface);
 
     snprintf(daemon_prop_name, sizeof(daemon_prop_name), "%s_%s",
             DAEMON_PROP_NAME,
-            daemon_suffix);
+            p2p_interface);
 
-    snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, daemon_suffix);
+    snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s", DAEMON_NAME, p2p_interface);
 
     /* Stop the daemon and wait until it's reported to be stopped */
     property_set(ctrl_prop, daemon_cmd);
@@ -305,12 +322,12 @@
  *
  */
 int dhcp_do_request_renew(const char *interface,
-                    in_addr_t *ipaddr,
-                    in_addr_t *gateway,
+                    char *ipaddr,
+                    char *gateway,
                     uint32_t *prefixLength,
-                    in_addr_t *dns1,
-                    in_addr_t *dns2,
-                    in_addr_t *server,
+                    char *dns1,
+                    char *dns2,
+                    char *server,
                     uint32_t *lease,
                     char *vendorInfo)
 {
@@ -319,20 +336,20 @@
     char daemon_cmd[PROPERTY_VALUE_MAX * 2];
     const char *ctrl_prop = "ctl.start";
 
-    char daemon_suffix[MAX_DAEMON_SUFFIX];
+    char p2p_interface[MAX_INTERFACE_LENGTH];
 
-    get_daemon_suffix(interface, daemon_suffix);
+    get_p2p_interface_replacement(interface, p2p_interface);
 
     snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
             DHCP_PROP_NAME_PREFIX,
-            interface);
+            p2p_interface);
 
     /* Erase any previous setting of the dhcp result property */
     property_set(result_prop_name, "");
 
     /* Start the renew daemon and wait until it's ready */
     snprintf(daemon_cmd, sizeof(daemon_cmd), "%s_%s:%s", DAEMON_NAME_RENEW,
-            daemon_suffix, interface);
+            p2p_interface, interface);
     memset(prop_value, '\0', PROPERTY_VALUE_MAX);
     property_set(ctrl_prop, daemon_cmd);
 
diff --git a/libsuspend/Android.mk b/libsuspend/Android.mk
new file mode 100644
index 0000000..45cb701
--- /dev/null
+++ b/libsuspend/Android.mk
@@ -0,0 +1,23 @@
+# Copyright 2012 The Android Open Source Project
+
+LOCAL_PATH:= $(call my-dir)
+
+libsuspend_src_files := \
+	autosuspend.c \
+	autosuspend_autosleep.c \
+	autosuspend_earlysuspend.c \
+	autosuspend_wakeup_count.c \
+
+libsuspend_libraries := \
+	liblog libcutils
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := $(libsuspend_src_files)
+LOCAL_MODULE := libsuspend
+LOCAL_MODULE_TAGS := optional
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
+LOCAL_SHARED_LIBRARIES := $(libsuspend_libraries)
+#LOCAL_CFLAGS += -DLOG_NDEBUG=0
+include $(BUILD_SHARED_LIBRARY)
diff --git a/libsuspend/autosuspend.c b/libsuspend/autosuspend.c
new file mode 100644
index 0000000..7d1d973
--- /dev/null
+++ b/libsuspend/autosuspend.c
@@ -0,0 +1,109 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdbool.h>
+
+#define LOG_TAG "libsuspend"
+#include <cutils/log.h>
+
+#include <suspend/autosuspend.h>
+
+#include "autosuspend_ops.h"
+
+static struct autosuspend_ops *autosuspend_ops;
+static bool autosuspend_enabled;
+static bool autosuspend_inited;
+
+static int autosuspend_init(void)
+{
+    if (autosuspend_inited) {
+        return 0;
+    }
+
+    autosuspend_inited = true;
+
+    autosuspend_ops = autosuspend_earlysuspend_init();
+    if (autosuspend_ops) {
+        goto out;
+    }
+
+    autosuspend_ops = autosuspend_autosleep_init();
+    if (autosuspend_ops) {
+        goto out;
+    }
+
+    autosuspend_ops = autosuspend_wakeup_count_init();
+    if (autosuspend_ops) {
+        goto out;
+    }
+
+    if (!autosuspend_ops) {
+        ALOGE("failed to initialize autosuspend\n");
+        return -1;
+    }
+
+out:
+    ALOGV("autosuspend initialized\n");
+    return 0;
+}
+
+int autosuspend_enable(void)
+{
+    int ret;
+
+    ret = autosuspend_init();
+    if (ret) {
+        return ret;
+    }
+
+    ALOGV("autosuspend_enable\n");
+
+    if (autosuspend_enabled) {
+        return 0;
+    }
+
+    ret = autosuspend_ops->enable();
+    if (ret) {
+        return ret;
+    }
+
+    autosuspend_enabled = true;
+    return 0;
+}
+
+int autosuspend_disable(void)
+{
+    int ret;
+
+    ret = autosuspend_init();
+    if (ret) {
+        return ret;
+    }
+
+    ALOGV("autosuspend_disable\n");
+
+    if (!autosuspend_enabled) {
+        return 0;
+    }
+
+    ret = autosuspend_ops->disable();
+    if (ret) {
+        return ret;
+    }
+
+    autosuspend_enabled = false;
+    return 0;
+}
diff --git a/libsuspend/autosuspend_autosleep.c b/libsuspend/autosuspend_autosleep.c
new file mode 100644
index 0000000..dd0dfef
--- /dev/null
+++ b/libsuspend/autosuspend_autosleep.c
@@ -0,0 +1,99 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stddef.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#define LOG_TAG "libsuspend"
+#include <cutils/log.h>
+
+#include "autosuspend_ops.h"
+
+#define SYS_POWER_AUTOSLEEP "/sys/power/autosleep"
+
+static int autosleep_fd;
+static const char *sleep_state = "mem";
+static const char *on_state = "off";
+
+static int autosuspend_autosleep_enable(void)
+{
+    char buf[80];
+    int ret;
+
+    ALOGV("autosuspend_autosleep_enable\n");
+
+    ret = write(autosleep_fd, sleep_state, strlen(sleep_state));
+    if (ret < 0) {
+        strerror_r(errno, buf, sizeof(buf));
+        ALOGE("Error writing to %s: %s\n", SYS_POWER_AUTOSLEEP, buf);
+        goto err;
+    }
+
+    ALOGV("autosuspend_autosleep_enable done\n");
+
+    return 0;
+
+err:
+    return ret;
+}
+
+static int autosuspend_autosleep_disable(void)
+{
+    char buf[80];
+    int ret;
+
+    ALOGV("autosuspend_autosleep_disable\n");
+
+    ret = write(autosleep_fd, on_state, strlen(on_state));
+    if (ret < 0) {
+        strerror_r(errno, buf, sizeof(buf));
+        ALOGE("Error writing to %s: %s\n", SYS_POWER_AUTOSLEEP, buf);
+        goto err;
+    }
+
+    ALOGV("autosuspend_autosleep_disable done\n");
+
+    return 0;
+
+err:
+    return ret;
+}
+
+struct autosuspend_ops autosuspend_autosleep_ops = {
+        .enable = autosuspend_autosleep_enable,
+        .disable = autosuspend_autosleep_disable,
+};
+
+struct autosuspend_ops *autosuspend_autosleep_init(void)
+{
+    int ret;
+    char buf[80];
+
+    autosleep_fd = open(SYS_POWER_AUTOSLEEP, O_WRONLY);
+    if (autosleep_fd < 0) {
+        strerror_r(errno, buf, sizeof(buf));
+        ALOGE("Error opening %s: %s\n", SYS_POWER_AUTOSLEEP, buf);
+        return NULL;
+    }
+
+    ALOGI("Selected autosleep\n");
+    return &autosuspend_autosleep_ops;
+}
diff --git a/libsuspend/autosuspend_earlysuspend.c b/libsuspend/autosuspend_earlysuspend.c
new file mode 100644
index 0000000..2c2aa36
--- /dev/null
+++ b/libsuspend/autosuspend_earlysuspend.c
@@ -0,0 +1,113 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stddef.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#define LOG_TAG "libsuspend"
+#include <cutils/log.h>
+
+#include "autosuspend_ops.h"
+
+#define EARLYSUSPEND_SYS_POWER_STATE "/sys/power/state"
+#define EARLYSUSPEND_WAIT_FOR_FB_SLEEP "/sys/power/wait_for_fb_sleep"
+#define EARLYSUSPEND_WAIT_FOR_FB_WAKE "/sys/power/wait_for_fb_wake"
+
+
+static int sPowerStatefd;
+static const char *pwr_state_mem = "mem";
+static const char *pwr_state_on = "on";
+
+static int autosuspend_earlysuspend_enable(void)
+{
+    char buf[80];
+    int ret;
+
+    ALOGV("autosuspend_earlysuspend_enable\n");
+
+    ret = write(sPowerStatefd, pwr_state_mem, strlen(pwr_state_mem));
+    if (ret < 0) {
+        strerror_r(errno, buf, sizeof(buf));
+        ALOGE("Error writing to %s: %s\n", EARLYSUSPEND_SYS_POWER_STATE, buf);
+        goto err;
+    }
+
+    ALOGV("autosuspend_earlysuspend_enable done\n");
+
+    return 0;
+
+err:
+    return ret;
+}
+
+static int autosuspend_earlysuspend_disable(void)
+{
+    char buf[80];
+    int ret;
+
+    ALOGV("autosuspend_earlysuspend_disable\n");
+
+    ret = write(sPowerStatefd, pwr_state_on, strlen(pwr_state_on));
+    if (ret < 0) {
+        strerror_r(errno, buf, sizeof(buf));
+        ALOGE("Error writing to %s: %s\n", EARLYSUSPEND_SYS_POWER_STATE, buf);
+        goto err;
+    }
+
+    ALOGV("autosuspend_earlysuspend_disable done\n");
+
+    return 0;
+
+err:
+    return ret;
+}
+
+struct autosuspend_ops autosuspend_earlysuspend_ops = {
+        .enable = autosuspend_earlysuspend_enable,
+        .disable = autosuspend_earlysuspend_disable,
+};
+
+struct autosuspend_ops *autosuspend_earlysuspend_init(void)
+{
+    char buf[80];
+    int ret;
+
+    ret = access(EARLYSUSPEND_WAIT_FOR_FB_SLEEP, F_OK);
+    if (ret < 0) {
+        return NULL;
+    }
+
+    ret = access(EARLYSUSPEND_WAIT_FOR_FB_WAKE, F_OK);
+    if (ret < 0) {
+        return NULL;
+    }
+
+    sPowerStatefd = open(EARLYSUSPEND_SYS_POWER_STATE, O_RDWR);
+
+    if (sPowerStatefd < 0) {
+        strerror_r(errno, buf, sizeof(buf));
+        ALOGE("Error opening %s: %s\n", EARLYSUSPEND_SYS_POWER_STATE, buf);
+        return NULL;
+    }
+
+    ALOGI("Selected early suspend\n");
+    return &autosuspend_earlysuspend_ops;
+}
diff --git a/libsuspend/autosuspend_ops.h b/libsuspend/autosuspend_ops.h
new file mode 100644
index 0000000..698e25b
--- /dev/null
+++ b/libsuspend/autosuspend_ops.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _LIBSUSPEND_AUTOSUSPEND_OPS_H_
+#define _LIBSUSPEND_AUTOSUSPEND_OPS_H_
+
+struct autosuspend_ops {
+    int (*enable)(void);
+    int (*disable)(void);
+};
+
+struct autosuspend_ops *autosuspend_autosleep_init(void);
+struct autosuspend_ops *autosuspend_earlysuspend_init(void);
+struct autosuspend_ops *autosuspend_wakeup_count_init(void);
+
+#endif
diff --git a/libsuspend/autosuspend_wakeup_count.c b/libsuspend/autosuspend_wakeup_count.c
new file mode 100644
index 0000000..b038e20
--- /dev/null
+++ b/libsuspend/autosuspend_wakeup_count.c
@@ -0,0 +1,182 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <stddef.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#define LOG_TAG "libsuspend"
+#include <cutils/log.h>
+
+#include "autosuspend_ops.h"
+
+#define SYS_POWER_STATE "/sys/power/state"
+#define SYS_POWER_WAKEUP_COUNT "/sys/power/wakeup_count"
+
+static int state_fd;
+static int wakeup_count_fd;
+static pthread_t suspend_thread;
+static sem_t suspend_lockout;
+static const char *sleep_state = "mem";
+
+static void *suspend_thread_func(void *arg)
+{
+    char buf[80];
+    char wakeup_count[20];
+    int wakeup_count_len;
+    int ret;
+
+    while (1) {
+        usleep(100000);
+        ALOGV("%s: read wakeup_count\n", __func__);
+        lseek(wakeup_count_fd, 0, SEEK_SET);
+        wakeup_count_len = read(wakeup_count_fd, wakeup_count, sizeof(wakeup_count));
+        if (wakeup_count_len < 0) {
+            strerror_r(errno, buf, sizeof(buf));
+            ALOGE("Error reading from %s: %s\n", SYS_POWER_WAKEUP_COUNT, buf);
+            wakeup_count_len = 0;
+            continue;
+        }
+        if (!wakeup_count_len) {
+            ALOGE("Empty wakeup count\n");
+            continue;
+        }
+
+        ALOGV("%s: wait\n", __func__);
+        ret = sem_wait(&suspend_lockout);
+        if (ret < 0) {
+            strerror_r(errno, buf, sizeof(buf));
+            ALOGE("Error waiting on semaphore: %s\n", buf);
+            continue;
+        }
+
+        ALOGV("%s: write %*s to wakeup_count\n", __func__, wakeup_count_len, wakeup_count);
+        ret = write(wakeup_count_fd, wakeup_count, wakeup_count_len);
+        if (ret < 0) {
+            strerror_r(errno, buf, sizeof(buf));
+            ALOGE("Error writing to %s: %s\n", SYS_POWER_WAKEUP_COUNT, buf);
+        } else {
+            ALOGV("%s: write %s to %s\n", __func__, sleep_state, SYS_POWER_STATE);
+            ret = write(state_fd, sleep_state, strlen(sleep_state));
+            if (ret < 0) {
+                strerror_r(errno, buf, sizeof(buf));
+                ALOGE("Error writing to %s: %s\n", SYS_POWER_STATE, buf);
+            }
+        }
+
+        ALOGV("%s: release sem\n", __func__);
+        ret = sem_post(&suspend_lockout);
+        if (ret < 0) {
+            strerror_r(errno, buf, sizeof(buf));
+            ALOGE("Error releasing semaphore: %s\n", buf);
+        }
+    }
+    return NULL;
+}
+
+static int autosuspend_wakeup_count_enable(void)
+{
+    char buf[80];
+    int ret;
+
+    ALOGV("autosuspend_wakeup_count_enable\n");
+
+    ret = sem_post(&suspend_lockout);
+
+    if (ret < 0) {
+        strerror_r(errno, buf, sizeof(buf));
+        ALOGE("Error changing semaphore: %s\n", buf);
+    }
+
+    ALOGV("autosuspend_wakeup_count_enable done\n");
+
+    return ret;
+}
+
+static int autosuspend_wakeup_count_disable(void)
+{
+    char buf[80];
+    int ret;
+
+    ALOGV("autosuspend_wakeup_count_disable\n");
+
+    ret = sem_wait(&suspend_lockout);
+
+    if (ret < 0) {
+        strerror_r(errno, buf, sizeof(buf));
+        ALOGE("Error changing semaphore: %s\n", buf);
+    }
+
+    ALOGV("autosuspend_wakeup_count_disable done\n");
+
+    return ret;
+}
+
+struct autosuspend_ops autosuspend_wakeup_count_ops = {
+        .enable = autosuspend_wakeup_count_enable,
+        .disable = autosuspend_wakeup_count_disable,
+};
+
+struct autosuspend_ops *autosuspend_wakeup_count_init(void)
+{
+    int ret;
+    char buf[80];
+
+    state_fd = open(SYS_POWER_STATE, O_RDWR);
+    if (state_fd < 0) {
+        strerror_r(errno, buf, sizeof(buf));
+        ALOGE("Error opening %s: %s\n", SYS_POWER_STATE, buf);
+        goto err_open_state;
+    }
+
+    wakeup_count_fd = open(SYS_POWER_WAKEUP_COUNT, O_RDWR);
+    if (wakeup_count_fd < 0) {
+        strerror_r(errno, buf, sizeof(buf));
+        ALOGE("Error opening %s: %s\n", SYS_POWER_WAKEUP_COUNT, buf);
+        goto err_open_wakeup_count;
+    }
+
+    ret = sem_init(&suspend_lockout, 0, 0);
+    if (ret < 0) {
+        strerror_r(errno, buf, sizeof(buf));
+        ALOGE("Error creating semaphore: %s\n", buf);
+        goto err_sem_init;
+    }
+    ret = pthread_create(&suspend_thread, NULL, suspend_thread_func, NULL);
+    if (ret) {
+        strerror_r(ret, buf, sizeof(buf));
+        ALOGE("Error creating thread: %s\n", buf);
+        goto err_pthread_create;
+    }
+
+    ALOGI("Selected wakeup count\n");
+    return &autosuspend_wakeup_count_ops;
+
+err_pthread_create:
+    sem_destroy(&suspend_lockout);
+err_sem_init:
+    close(wakeup_count_fd);
+err_open_wakeup_count:
+    close(state_fd);
+err_open_state:
+    return NULL;
+}
diff --git a/libsuspend/include/suspend/autosuspend.h b/libsuspend/include/suspend/autosuspend.h
new file mode 100644
index 0000000..f56fc6a
--- /dev/null
+++ b/libsuspend/include/suspend/autosuspend.h
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _LIBSUSPEND_AUTOSUSPEND_H_
+#define _LIBSUSPEND_AUTOSUSPEND_H_
+
+#include <sys/cdefs.h>
+
+__BEGIN_DECLS
+
+/*
+ * autosuspend_enable
+ *
+ * Turn on autosuspend in the kernel, allowing it to enter suspend if no
+ * wakelocks/wakeup_sources are held.
+ *
+ *
+ *
+ * Returns 0 on success, -1 if autosuspend was not enabled.
+ */
+int autosuspend_enable(void);
+
+/*
+ * autosuspend_disable
+ *
+ * Turn off autosuspend in the kernel, preventing suspend and synchronizing
+ * with any in-progress resume.
+ *
+ * Returns 0 on success, -1 if autosuspend was not disabled.
+ */
+int autosuspend_disable(void);
+
+__END_DECLS
+
+#endif
diff --git a/libsysutils/src/SocketClient.cpp b/libsysutils/src/SocketClient.cpp
index 4a1227f..3d4984d 100644
--- a/libsysutils/src/SocketClient.cpp
+++ b/libsysutils/src/SocketClient.cpp
@@ -131,11 +131,6 @@
 
 
 int SocketClient::sendMsg(const char *msg) {
-    if (mSocket < 0) {
-        errno = EHOSTUNREACH;
-        return -1;
-    }
-
     // Send the message including null character
     if (sendData(msg, strlen(msg) + 1) != 0) {
         SLOGW("Unable to send msg '%s'", msg);
@@ -158,6 +153,11 @@
     const char *p = (const char*) data;
     int brtw = len;
 
+    if (mSocket < 0) {
+        errno = EHOSTUNREACH;
+        return -1;
+    }
+
     if (len == 0) {
         return 0;
     }
diff --git a/rootdir/init.rc b/rootdir/init.rc
index bfa4209..7fb50a3 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -93,34 +93,20 @@
     write /dev/cpuctl/cpu.rt_runtime_us 950000
     write /dev/cpuctl/cpu.rt_period_us 1000000
 
-    mkdir /dev/cpuctl/foreground
-    chown system system /dev/cpuctl/foreground/tasks
-    chmod 0666 /dev/cpuctl/foreground/tasks
-    write /dev/cpuctl/foreground/cpu.shares 1024
-    write /dev/cpuctl/foreground/cpu.rt_runtime_us 0
-    write /dev/cpuctl/foreground/cpu.rt_period_us 1000000
+    mkdir /dev/cpuctl/apps
+    chown system system /dev/cpuctl/apps/tasks
+    chmod 0666 /dev/cpuctl/apps/tasks
+    write /dev/cpuctl/apps/cpu.shares 1024
+    write /dev/cpuctl/apps/cpu.rt_runtime_us 800000
+    write /dev/cpuctl/apps/cpu.rt_period_us 1000000
 
-    mkdir /dev/cpuctl/bg_non_interactive
-    chown system system /dev/cpuctl/bg_non_interactive/tasks
-    chmod 0666 /dev/cpuctl/bg_non_interactive/tasks
+    mkdir /dev/cpuctl/apps/bg_non_interactive
+    chown system system /dev/cpuctl/apps/bg_non_interactive/tasks
+    chmod 0666 /dev/cpuctl/apps/bg_non_interactive/tasks
     # 5.0 %
-    write /dev/cpuctl/bg_non_interactive/cpu.shares 52
-    write /dev/cpuctl/bg_non_interactive/cpu.rt_runtime_us 0
-    write /dev/cpuctl/bg_non_interactive/cpu.rt_period_us 1000000
-
-    mkdir /dev/cpuctl/audio_app
-    chown system system /dev/cpuctl/audio_app/tasks
-    chmod 0660 /dev/cpuctl/audio_app/tasks
-    write /dev/cpuctl/audio_app/cpu.shares 10
-    write /dev/cpuctl/audio_app/cpu.rt_runtime_us 100000
-    write /dev/cpuctl/audio_app/cpu.rt_period_us 1000000
-
-    mkdir /dev/cpuctl/audio_sys
-    chown system system /dev/cpuctl/audio_sys/tasks
-    chmod 0660 /dev/cpuctl/audio_sys/tasks
-    write /dev/cpuctl/audio_sys/cpu.shares 10
-    write /dev/cpuctl/audio_sys/cpu.rt_runtime_us 100000
-    write /dev/cpuctl/audio_sys/cpu.rt_period_us 1000000
+    write /dev/cpuctl/apps/bg_non_interactive/cpu.shares 52
+    write /dev/cpuctl/apps/bg_non_interactive/cpu.rt_runtime_us 700000
+    write /dev/cpuctl/apps/bg_non_interactive/cpu.rt_period_us 1000000
 
 # Allow everybody to read the xt_qtaguid resource tracking misc dev.
 # This is needed by any process that uses socket tagging.
@@ -275,6 +261,7 @@
     chmod 0660 /sys/devices/system/cpu/cpufreq/interactive/above_hispeed_delay
     chown system system /sys/devices/system/cpu/cpufreq/interactive/boost
     chmod 0660 /sys/devices/system/cpu/cpufreq/interactive/boost
+    chown system system /sys/devices/system/cpu/cpufreq/interactive/boostpulse
     chown system system /sys/devices/system/cpu/cpufreq/interactive/input_boost
     chmod 0660 /sys/devices/system/cpu/cpufreq/interactive/input_boost
 
diff --git a/sdcard/Android.mk b/sdcard/Android.mk
index c430ac8..fb04d6d 100644
--- a/sdcard/Android.mk
+++ b/sdcard/Android.mk
@@ -4,6 +4,7 @@
 
 LOCAL_SRC_FILES:= sdcard.c
 LOCAL_MODULE:= sdcard
+LOCAL_CFLAGS := -Wall -Wno-unused-parameter
 
 LOCAL_SHARED_LIBRARIES := libc
 
diff --git a/sdcard/sdcard.c b/sdcard/sdcard.c
index a95513c..316588c 100644
--- a/sdcard/sdcard.c
+++ b/sdcard/sdcard.c
@@ -25,7 +25,9 @@
 #include <sys/statfs.h>
 #include <sys/uio.h>
 #include <dirent.h>
+#include <limits.h>
 #include <ctype.h>
+#include <pthread.h>
 
 #include <private/android_filesystem_config.h>
 
@@ -72,28 +74,42 @@
 
 #define MOUNT_POINT "/storage/sdcard0"
 
+/* Maximum number of bytes to write in one request. */
+#define MAX_WRITE (256 * 1024)
+
+/* Maximum number of bytes to read in one request. */
+#define MAX_READ (128 * 1024)
+
+/* Largest possible request.
+ * The request size is bounded by the maximum size of a FUSE_WRITE request because it has
+ * the largest possible data payload. */
+#define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE)
+
+/* Default number of threads. */
+#define DEFAULT_NUM_THREADS 2
+
+/* Pseudo-error constant used to indicate that no fuse status is needed
+ * or that a reply has already been written. */
+#define NO_STATUS 1
+
 struct handle {
-    struct node *node;
     int fd;
 };
 
 struct dirhandle {
-    struct node *node;
     DIR *d;
 };
 
 struct node {
+    __u32 refcount;
     __u64 nid;
     __u64 gen;
 
     struct node *next;          /* per-dir sibling list */
     struct node *child;         /* first contained file by this dir */
-    struct node *all;           /* global node list */
     struct node *parent;        /* containing directory */
 
-    __u32 refcount;
-    __u32 namelen;
-
+    size_t namelen;
     char *name;
     /* If non-null, this is the real name of the file in the underlying storage.
      * This may differ from the field "name" only by case.
@@ -103,98 +119,166 @@
     char *actual_name;
 };
 
+/* Global data structure shared by all fuse handlers. */
 struct fuse {
+    pthread_mutex_t lock;
+
     __u64 next_generation;
-    __u64 next_node_id;
-
     int fd;
-
-    struct node *all;
-
     struct node root;
-    char rootpath[1024];
+    char rootpath[PATH_MAX];
 };
 
-static unsigned uid = -1;
-static unsigned gid = -1;
+/* Private data used by a single fuse handler. */
+struct fuse_handler {
+    struct fuse* fuse;
+    int token;
 
-#define PATH_BUFFER_SIZE 1024
+    /* To save memory, we never use the contents of the request buffer and the read
+     * buffer at the same time.  This allows us to share the underlying storage. */
+    union {
+        __u8 request_buffer[MAX_REQUEST_SIZE];
+        __u8 read_buffer[MAX_READ];
+    };
+};
 
-#define NO_CASE_SENSITIVE_MATCH 0
-#define CASE_SENSITIVE_MATCH 1
-
-/*
- * Get the real-life absolute path to a node.
- *   node: start at this node
- *   buf: storage for returned string
- *   name: append this string to path if set
- */
-char *do_node_get_path(struct node *node, char *buf, const char *name, int match_case_insensitive)
+static inline void *id_to_ptr(__u64 nid)
 {
-    struct node *in_node = node;
-    const char *in_name = name;
-    char *out = buf + PATH_BUFFER_SIZE - 1;
-    int len;
-    out[0] = 0;
+    return (void *) (uintptr_t) nid;
+}
 
-    if (name) {
-        len = strlen(name);
-        goto start;
-    }
+static inline __u64 ptr_to_id(void *ptr)
+{
+    return (__u64) (uintptr_t) ptr;
+}
 
-    while (node) {
-        name = (node->actual_name ? node->actual_name : node->name);
-        len = node->namelen;
-        node = node->parent;
-    start:
-        if ((len + 1) > (out - buf))
-            return 0;
-        out -= len;
-        memcpy(out, name, len);
-        /* avoid double slash at beginning of path */
-        if (out[0] != '/') {
-            out --;
-            out[0] = '/';
+static void acquire_node_locked(struct node* node)
+{
+    node->refcount++;
+    TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
+}
+
+static void remove_node_from_parent_locked(struct node* node);
+
+static void release_node_locked(struct node* node)
+{
+    TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
+    if (node->refcount > 0) {
+        node->refcount--;
+        if (!node->refcount) {
+            TRACE("DESTROY %p (%s)\n", node, node->name);
+            remove_node_from_parent_locked(node);
+
+                /* TODO: remove debugging - poison memory */
+            memset(node->name, 0xef, node->namelen);
+            free(node->name);
+            free(node->actual_name);
+            memset(node, 0xfc, sizeof(*node));
+            free(node);
         }
+    } else {
+        ERROR("Zero refcnt %p\n", node);
+    }
+}
+
+static void add_node_to_parent_locked(struct node *node, struct node *parent) {
+    node->parent = parent;
+    node->next = parent->child;
+    parent->child = node;
+    acquire_node_locked(parent);
+}
+
+static void remove_node_from_parent_locked(struct node* node)
+{
+    if (node->parent) {
+        if (node->parent->child == node) {
+            node->parent->child = node->parent->child->next;
+        } else {
+            struct node *node2;
+            node2 = node->parent->child;
+            while (node2->next != node)
+                node2 = node2->next;
+            node2->next = node->next;
+        }
+        release_node_locked(node->parent);
+        node->parent = NULL;
+        node->next = NULL;
+    }
+}
+
+/* Gets the absolute path to a node into the provided buffer.
+ *
+ * Populates 'buf' with the path and returns the length of the path on success,
+ * or returns -1 if the path is too long for the provided buffer.
+ */
+static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize)
+{
+    size_t namelen = node->namelen;
+    if (bufsize < namelen + 1) {
+        return -1;
     }
 
-    /* If we are searching for a file within node (rather than computing node's path)
-     * and fail, then we need to look for a case insensitive match.
-     */
-    if (in_name && match_case_insensitive && access(out, F_OK) != 0) {
-        char *path, buffer[PATH_BUFFER_SIZE];
-        DIR* dir;
+    ssize_t pathlen = 0;
+    if (node->parent) {
+        pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2);
+        if (pathlen < 0) {
+            return -1;
+        }
+        buf[pathlen++] = '/';
+    }
+
+    const char* name = node->actual_name ? node->actual_name : node->name;
+    memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
+    return pathlen + namelen;
+}
+
+/* Finds the absolute path of a file within a given directory.
+ * Performs a case-insensitive search for the file and sets the buffer to the path
+ * of the first matching file.  If 'search' is zero or if no match is found, sets
+ * the buffer to the path that the file would have, assuming the name were case-sensitive.
+ *
+ * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
+ * or returns NULL if the path is too long for the provided buffer.
+ */
+static char* find_file_within(const char* path, const char* name,
+        char* buf, size_t bufsize, int search)
+{
+    size_t pathlen = strlen(path);
+    size_t namelen = strlen(name);
+    size_t childlen = pathlen + namelen + 1;
+    char* actual;
+
+    if (bufsize <= childlen) {
+        return NULL;
+    }
+
+    memcpy(buf, path, pathlen);
+    buf[pathlen] = '/';
+    actual = buf + pathlen + 1;
+    memcpy(actual, name, namelen + 1);
+
+    if (search && access(buf, F_OK)) {
         struct dirent* entry;
-        path = do_node_get_path(in_node, buffer, NULL, NO_CASE_SENSITIVE_MATCH);
-        dir = opendir(path);
+        DIR* dir = opendir(path);
         if (!dir) {
             ERROR("opendir %s failed: %s", path, strerror(errno));
-            return out;
+            return actual;
         }
-
         while ((entry = readdir(dir))) {
-            if (!strcasecmp(entry->d_name, in_name)) {
-                /* we have a match - replace the name */
-                len = strlen(in_name);
-                memcpy(buf + PATH_BUFFER_SIZE - len - 1, entry->d_name, len);
+            if (!strcasecmp(entry->d_name, name)) {
+                /* we have a match - replace the name, don't need to copy the null again */
+                memcpy(actual, entry->d_name, namelen);
                 break;
             }
         }
         closedir(dir);
     }
-
-   return out;
+    return actual;
 }
 
-char *node_get_path(struct node *node, char *buf, const char *name)
+static void attr_from_stat(struct fuse_attr *attr, const struct stat *s, __u64 nid)
 {
-    /* We look for case insensitive matches by default */
-    return do_node_get_path(node, buf, name, CASE_SENSITIVE_MATCH);
-}
-
-void attr_from_stat(struct fuse_attr *attr, struct stat *s)
-{
-    attr->ino = s->st_ino;
+    attr->ino = nid;
     attr->size = s->st_size;
     attr->blocks = s->st_blocks;
     attr->atime = s->st_atime;
@@ -221,129 +305,81 @@
     attr->gid = AID_SDCARD_RW;
 }
 
-int node_get_attr(struct node *node, struct fuse_attr *attr)
-{
-    int res;
-    struct stat s;
-    char *path, buffer[PATH_BUFFER_SIZE];
-
-    path = node_get_path(node, buffer, 0);
-    res = lstat(path, &s);
-    if (res < 0) {
-        ERROR("lstat('%s') errno %d\n", path, errno);
-        return -1;
-    }
-
-    attr_from_stat(attr, &s);    
-    attr->ino = node->nid;
-
-    return 0;
-}
-
-static void add_node_to_parent(struct node *node, struct node *parent) {
-    node->parent = parent;
-    node->next = parent->child;
-    parent->child = node;
-    parent->refcount++;
-}
-
-/* Check to see if our parent directory already has a file with a name
- * that differs only by case.  If we find one, store it in the actual_name
- * field so node_get_path will map it to this file in the underlying storage.
- */
-static void node_find_actual_name(struct node *node)
-{
-    char *path, buffer[PATH_BUFFER_SIZE];
-    const char *node_name = node->name;
-    DIR* dir;
-    struct dirent* entry;
-
-    if (!node->parent) return;
-
-    path = node_get_path(node->parent, buffer, 0);
-    dir = opendir(path);
-    if (!dir) {
-        ERROR("opendir %s failed: %s", path, strerror(errno));
-        return;
-    }
-
-    while ((entry = readdir(dir))) {
-        const char *test_name = entry->d_name;
-        if (strcmp(test_name, node_name) && !strcasecmp(test_name, node_name)) {
-            /* we have a match - differs but only by case */
-            node->actual_name = strdup(test_name);
-            if (!node->actual_name) {
-                ERROR("strdup failed - out of memory\n");
-                exit(1);
-            }
-            break;
-        }
-    }
-    closedir(dir);
-}
-
-struct node *node_create(struct node *parent, const char *name, __u64 nid, __u64 gen)
+struct node *create_node_locked(struct fuse* fuse,
+        struct node *parent, const char *name, const char* actual_name)
 {
     struct node *node;
-    int namelen = strlen(name);
+    size_t namelen = strlen(name);
 
     node = calloc(1, sizeof(struct node));
-    if (node == 0) {
-        return 0;
+    if (!node) {
+        return NULL;
     }
     node->name = malloc(namelen + 1);
-    if (node->name == 0) {
+    if (!node->name) {
         free(node);
-        return 0;
+        return NULL;
     }
-
-    node->nid = nid;
-    node->gen = gen;
-    add_node_to_parent(node, parent);
     memcpy(node->name, name, namelen + 1);
+    if (strcmp(name, actual_name)) {
+        node->actual_name = malloc(namelen + 1);
+        if (!node->actual_name) {
+            free(node->name);
+            free(node);
+            return NULL;
+        }
+        memcpy(node->actual_name, actual_name, namelen + 1);
+    }
     node->namelen = namelen;
-    node_find_actual_name(node);
+    node->nid = ptr_to_id(node);
+    node->gen = fuse->next_generation++;
+    acquire_node_locked(node);
+    add_node_to_parent_locked(node, parent);
     return node;
 }
 
-static char *rename_node(struct node *node, const char *name)
+static int rename_node_locked(struct node *node, const char *name,
+        const char* actual_name)
 {
-    node->namelen = strlen(name);
-    char *newname = realloc(node->name, node->namelen + 1);
-    if (newname == 0)
-        return 0;
-    node->name = newname;
-    memcpy(node->name, name, node->namelen + 1);
-    node_find_actual_name(node);
-    return node->name;
+    size_t namelen = strlen(name);
+    int need_actual_name = strcmp(name, actual_name);
+
+    /* make the storage bigger without actually changing the name
+     * in case an error occurs part way */
+    if (namelen > node->namelen) {
+        char* new_name = realloc(node->name, namelen + 1);
+        if (!new_name) {
+            return -ENOMEM;
+        }
+        node->name = new_name;
+        if (need_actual_name && node->actual_name) {
+            char* new_actual_name = realloc(node->actual_name, namelen + 1);
+            if (!new_actual_name) {
+                return -ENOMEM;
+            }
+            node->actual_name = new_actual_name;
+        }
+    }
+
+    /* update the name, taking care to allocate storage before overwriting the old name */
+    if (need_actual_name) {
+        if (!node->actual_name) {
+            node->actual_name = malloc(namelen + 1);
+            if (!node->actual_name) {
+                return -ENOMEM;
+            }
+        }
+        memcpy(node->actual_name, actual_name, namelen + 1);
+    } else {
+        free(node->actual_name);
+        node->actual_name = NULL;
+    }
+    memcpy(node->name, name, namelen + 1);
+    node->namelen = namelen;
+    return 0;
 }
 
-void fuse_init(struct fuse *fuse, int fd, const char *path)
-{
-    fuse->fd = fd;
-    fuse->next_node_id = 2;
-    fuse->next_generation = 0;
-
-    fuse->all = &fuse->root;
-
-    memset(&fuse->root, 0, sizeof(fuse->root));
-    fuse->root.nid = FUSE_ROOT_ID; /* 1 */
-    fuse->root.refcount = 2;
-    rename_node(&fuse->root, path);
-}
-
-static inline void *id_to_ptr(__u64 nid)
-{
-    return (void *) nid;
-}
-
-static inline __u64 ptr_to_id(void *ptr)
-{
-    return (__u64) ptr;
-}
-
-
-struct node *lookup_by_inode(struct fuse *fuse, __u64 nid)
+static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
 {
     if (nid == FUSE_ROOT_ID) {
         return &fuse->root;
@@ -352,9 +388,23 @@
     }
 }
 
-struct node *lookup_child_by_name(struct node *node, const char *name)
+static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
+        char* buf, size_t bufsize)
+{
+    struct node* node = lookup_node_by_id_locked(fuse, nid);
+    if (node && get_node_path_locked(node, buf, bufsize) < 0) {
+        node = NULL;
+    }
+    return node;
+}
+
+static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
 {
     for (node = node->child; node; node = node->next) {
+        /* use exact string comparison, nodes that differ by case
+         * must be considered distinct even if they refer to the same
+         * underlying file as otherwise operations such as "mv x x"
+         * will not work because the source and target nodes are the same. */
         if (!strcmp(name, node->name)) {
             return node;
         }
@@ -362,123 +412,43 @@
     return 0;
 }
 
-struct node *lookup_child_by_inode(struct node *node, __u64 nid)
+static struct node* acquire_or_create_child_locked(
+        struct fuse* fuse, struct node* parent,
+        const char* name, const char* actual_name)
 {
-    for (node = node->child; node; node = node->next) {
-        if (node->nid == nid) {
-            return node;
-        }
-    }
-    return 0;
-}
-
-static void dec_refcount(struct node *node) {
-    if (node->refcount > 0) {
-        node->refcount--;
-        TRACE("dec_refcount %p(%s) -> %d\n", node, node->name, node->refcount);
+    struct node* child = lookup_child_by_name_locked(parent, name);
+    if (child) {
+        acquire_node_locked(child);
     } else {
-        ERROR("Zero refcnt %p\n", node);
+        child = create_node_locked(fuse, parent, name, actual_name);
     }
- }
-
-static struct node *remove_child(struct node *parent, __u64 nid)
-{
-    struct node *prev = 0;
-    struct node *node;
-
-    for (node = parent->child; node; node = node->next) {
-        if (node->nid == nid) {
-            if (prev) {
-                prev->next = node->next;
-            } else {
-                parent->child = node->next;
-            }
-            node->next = 0;
-            node->parent = 0;
-            dec_refcount(parent);
-            return node;
-        }
-        prev = node;
-    }
-    return 0;
+    return child;
 }
 
-struct node *node_lookup(struct fuse *fuse, struct node *parent, const char *name,
-                         struct fuse_attr *attr)
+static void fuse_init(struct fuse *fuse, int fd, const char *path)
 {
-    int res;
-    struct stat s;
-    char *path, buffer[PATH_BUFFER_SIZE];
-    struct node *node;
+    pthread_mutex_init(&fuse->lock, NULL);
 
-    path = node_get_path(parent, buffer, name);
-        /* XXX error? */
+    fuse->fd = fd;
+    fuse->next_generation = 0;
 
-    res = lstat(path, &s);
-    if (res < 0)
-        return 0;
-    
-    node = lookup_child_by_name(parent, name);
-    if (!node) {
-        node = node_create(parent, name, fuse->next_node_id++, fuse->next_generation++);
-        if (!node)
-            return 0;
-        node->nid = ptr_to_id(node);
-        node->all = fuse->all;
-        fuse->all = node;
-    }
-
-    attr_from_stat(attr, &s);
-    attr->ino = node->nid;
-
-    return node;
+    memset(&fuse->root, 0, sizeof(fuse->root));
+    fuse->root.nid = FUSE_ROOT_ID; /* 1 */
+    fuse->root.refcount = 2;
+    fuse->root.namelen = strlen(path);
+    fuse->root.name = strdup(path);
 }
 
-void node_release(struct node *node)
-{
-    TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
-    dec_refcount(node);
-    if (node->refcount == 0) {
-        if (node->parent->child == node) {
-            node->parent->child = node->parent->child->next;
-        } else {
-            struct node *node2;
-
-            node2 = node->parent->child;
-            while (node2->next != node)
-                node2 = node2->next;
-            node2->next = node->next;            
-        }
-
-        TRACE("DESTROY %p (%s)\n", node, node->name);
-
-        node_release(node->parent);
-
-        node->parent = 0;
-        node->next = 0;
-
-            /* TODO: remove debugging - poison memory */
-        memset(node->name, 0xef, node->namelen);
-        free(node->name);
-        free(node->actual_name);
-        memset(node, 0xfc, sizeof(*node));
-        free(node);
-    }
-}
-
-void fuse_status(struct fuse *fuse, __u64 unique, int err)
+static void fuse_status(struct fuse *fuse, __u64 unique, int err)
 {
     struct fuse_out_header hdr;
     hdr.len = sizeof(hdr);
     hdr.error = err;
     hdr.unique = unique;
-    if (err) {
-//        ERROR("*** %d ***\n", err);
-    }
     write(fuse->fd, &hdr, sizeof(hdr));
 }
 
-void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
+static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
 {
     struct fuse_out_header hdr;
     struct iovec vec[2];
@@ -499,481 +469,856 @@
     }
 }
 
-void lookup_entry(struct fuse *fuse, struct node *node,
-                  const char *name, __u64 unique)
+static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
+        struct node* parent, const char* name, const char* actual_name,
+        const char* path)
 {
+    struct node* node;
     struct fuse_entry_out out;
-    
-    memset(&out, 0, sizeof(out));
+    struct stat s;
 
-    node = node_lookup(fuse, node, name, &out.attr);
-    if (!node) {
-        fuse_status(fuse, unique, -ENOENT);
-        return;
+    if (lstat(path, &s) < 0) {
+        return -errno;
     }
-    
-    node->refcount++;
-//    fprintf(stderr,"ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
+
+    pthread_mutex_lock(&fuse->lock);
+    node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
+    if (!node) {
+        pthread_mutex_unlock(&fuse->lock);
+        return -ENOMEM;
+    }
+    memset(&out, 0, sizeof(out));
+    attr_from_stat(&out.attr, &s, node->nid);
+    out.attr_valid = 10;
+    out.entry_valid = 10;
     out.nodeid = node->nid;
     out.generation = node->gen;
-    out.entry_valid = 10;
-    out.attr_valid = 10;
-    
+    pthread_mutex_unlock(&fuse->lock);
     fuse_reply(fuse, unique, &out, sizeof(out));
+    return NO_STATUS;
 }
 
-void handle_fuse_request(struct fuse *fuse, struct fuse_in_header *hdr, void *data, unsigned len)
+static int fuse_reply_attr(struct fuse* fuse, __u64 unique, __u64 nid,
+        const char* path)
 {
-    struct node *node;
+    struct fuse_attr_out out;
+    struct stat s;
 
-    if ((len < sizeof(*hdr)) || (hdr->len != len)) {
-        ERROR("malformed header\n");
-        return;
+    if (lstat(path, &s) < 0) {
+        return -errno;
     }
+    memset(&out, 0, sizeof(out));
+    attr_from_stat(&out.attr, &s, nid);
+    out.attr_valid = 10;
+    fuse_reply(fuse, unique, &out, sizeof(out));
+    return NO_STATUS;
+}
 
-    len -= hdr->len;
+static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
+        const struct fuse_in_header *hdr, const char* name)
+{
+    struct node* parent_node;
+    char parent_path[PATH_MAX];
+    char child_path[PATH_MAX];
+    const char* actual_name;
 
-    if (hdr->nodeid) {
-        node = lookup_by_inode(fuse, hdr->nodeid);
-        if (!node) {
-            fuse_status(fuse, hdr->unique, -ENOENT);
-            return;
+    pthread_mutex_lock(&fuse->lock);
+    parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
+            parent_path, sizeof(parent_path));
+    TRACE("[%d] LOOKUP %s @ %llx (%s)\n", handler->token, name, hdr->nodeid,
+        parent_node ? parent_node->name : "?");
+    pthread_mutex_unlock(&fuse->lock);
+
+    if (!parent_node || !(actual_name = find_file_within(parent_path, name,
+            child_path, sizeof(child_path), 1))) {
+        return -ENOENT;
+    }
+    return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
+}
+
+static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
+        const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
+{
+    struct node* node;
+
+    pthread_mutex_lock(&fuse->lock);
+    node = lookup_node_by_id_locked(fuse, hdr->nodeid);
+    TRACE("[%d] FORGET #%lld @ %llx (%s)\n", handler->token, req->nlookup,
+            hdr->nodeid, node ? node->name : "?");
+    if (node) {
+        __u64 n = req->nlookup;
+        while (n--) {
+            release_node_locked(node);
         }
-    } else {
-        node = 0;
+    }
+    pthread_mutex_unlock(&fuse->lock);
+    return NO_STATUS; /* no reply */
+}
+
+static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
+        const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
+{
+    struct node* node;
+    char path[PATH_MAX];
+
+    pthread_mutex_lock(&fuse->lock);
+    node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
+    TRACE("[%d] GETATTR flags=%x fh=%llx @ %llx (%s)\n", handler->token,
+            req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
+    pthread_mutex_unlock(&fuse->lock);
+
+    if (!node) {
+        return -ENOENT;
+    }
+    return fuse_reply_attr(fuse, hdr->unique, hdr->nodeid, path);
+}
+
+static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
+        const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
+{
+    struct node* node;
+    char path[PATH_MAX];
+    struct timespec times[2];
+
+    pthread_mutex_lock(&fuse->lock);
+    node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
+    TRACE("[%d] SETATTR fh=%llx valid=%x @ %llx (%s)\n", handler->token,
+            req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
+    pthread_mutex_unlock(&fuse->lock);
+
+    if (!node) {
+        return -ENOENT;
     }
 
+    /* XXX: incomplete implementation on purpose.
+     * chmod/chown should NEVER be implemented.*/
+
+    if ((req->valid & FATTR_SIZE) && truncate(path, req->size) < 0) {
+        return -errno;
+    }
+
+    /* Handle changing atime and mtime.  If FATTR_ATIME_and FATTR_ATIME_NOW
+     * are both set, then set it to the current time.  Else, set it to the
+     * time specified in the request.  Same goes for mtime.  Use utimensat(2)
+     * as it allows ATIME and MTIME to be changed independently, and has
+     * nanosecond resolution which fuse also has.
+     */
+    if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
+        times[0].tv_nsec = UTIME_OMIT;
+        times[1].tv_nsec = UTIME_OMIT;
+        if (req->valid & FATTR_ATIME) {
+            if (req->valid & FATTR_ATIME_NOW) {
+              times[0].tv_nsec = UTIME_NOW;
+            } else {
+              times[0].tv_sec = req->atime;
+              times[0].tv_nsec = req->atimensec;
+            }
+        }
+        if (req->valid & FATTR_MTIME) {
+            if (req->valid & FATTR_MTIME_NOW) {
+              times[1].tv_nsec = UTIME_NOW;
+            } else {
+              times[1].tv_sec = req->mtime;
+              times[1].tv_nsec = req->mtimensec;
+            }
+        }
+        TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
+                handler->token, path, times[0].tv_sec, times[1].tv_sec);
+        if (utimensat(-1, path, times, 0) < 0) {
+            return -errno;
+        }
+    }
+    return fuse_reply_attr(fuse, hdr->unique, hdr->nodeid, path);
+}
+
+static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
+        const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
+{
+    struct node* parent_node;
+    char parent_path[PATH_MAX];
+    char child_path[PATH_MAX];
+    const char* actual_name;
+
+    pthread_mutex_lock(&fuse->lock);
+    parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
+            parent_path, sizeof(parent_path));
+    TRACE("[%d] MKNOD %s 0%o @ %llx (%s)\n", handler->token,
+            name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
+    pthread_mutex_unlock(&fuse->lock);
+
+    if (!parent_node || !(actual_name = find_file_within(parent_path, name,
+            child_path, sizeof(child_path), 1))) {
+        return -ENOENT;
+    }
+    __u32 mode = (req->mode & (~0777)) | 0664;
+    if (mknod(child_path, mode, req->rdev) < 0) {
+        return -errno;
+    }
+    return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
+}
+
+static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
+        const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
+{
+    struct node* parent_node;
+    char parent_path[PATH_MAX];
+    char child_path[PATH_MAX];
+    const char* actual_name;
+
+    pthread_mutex_lock(&fuse->lock);
+    parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
+            parent_path, sizeof(parent_path));
+    TRACE("[%d] MKDIR %s 0%o @ %llx (%s)\n", handler->token,
+            name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
+    pthread_mutex_unlock(&fuse->lock);
+
+    if (!parent_node || !(actual_name = find_file_within(parent_path, name,
+            child_path, sizeof(child_path), 1))) {
+        return -ENOENT;
+    }
+    __u32 mode = (req->mode & (~0777)) | 0775;
+    if (mkdir(child_path, mode) < 0) {
+        return -errno;
+    }
+    return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
+}
+
+static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
+        const struct fuse_in_header* hdr, const char* name)
+{
+    struct node* parent_node;
+    char parent_path[PATH_MAX];
+    char child_path[PATH_MAX];
+
+    pthread_mutex_lock(&fuse->lock);
+    parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
+            parent_path, sizeof(parent_path));
+    TRACE("[%d] UNLINK %s @ %llx (%s)\n", handler->token,
+            name, hdr->nodeid, parent_node ? parent_node->name : "?");
+    pthread_mutex_unlock(&fuse->lock);
+
+    if (!parent_node || !find_file_within(parent_path, name,
+            child_path, sizeof(child_path), 1)) {
+        return -ENOENT;
+    }
+    if (unlink(child_path) < 0) {
+        return -errno;
+    }
+    return 0;
+}
+
+static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
+        const struct fuse_in_header* hdr, const char* name)
+{
+    struct node* parent_node;
+    char parent_path[PATH_MAX];
+    char child_path[PATH_MAX];
+
+    pthread_mutex_lock(&fuse->lock);
+    parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
+            parent_path, sizeof(parent_path));
+    TRACE("[%d] RMDIR %s @ %llx (%s)\n", handler->token,
+            name, hdr->nodeid, parent_node ? parent_node->name : "?");
+    pthread_mutex_unlock(&fuse->lock);
+
+    if (!parent_node || !find_file_within(parent_path, name,
+            child_path, sizeof(child_path), 1)) {
+        return -ENOENT;
+    }
+    if (rmdir(child_path) < 0) {
+        return -errno;
+    }
+    return 0;
+}
+
+static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
+        const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
+        const char* old_name, const char* new_name)
+{
+    struct node* old_parent_node;
+    struct node* new_parent_node;
+    struct node* child_node;
+    char old_parent_path[PATH_MAX];
+    char new_parent_path[PATH_MAX];
+    char old_child_path[PATH_MAX];
+    char new_child_path[PATH_MAX];
+    const char* new_actual_name;
+    int res;
+
+    pthread_mutex_lock(&fuse->lock);
+    old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
+            old_parent_path, sizeof(old_parent_path));
+    new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
+            new_parent_path, sizeof(new_parent_path));
+    TRACE("[%d] RENAME %s->%s @ %llx (%s) -> %llx (%s)\n", handler->token,
+            old_name, new_name,
+            hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
+            req->newdir, new_parent_node ? new_parent_node->name : "?");
+    if (!old_parent_node || !new_parent_node) {
+        res = -ENOENT;
+        goto lookup_error;
+    }
+    child_node = lookup_child_by_name_locked(old_parent_node, old_name);
+    if (!child_node || get_node_path_locked(child_node,
+            old_child_path, sizeof(old_child_path)) < 0) {
+        res = -ENOENT;
+        goto lookup_error;
+    }
+    acquire_node_locked(child_node);
+    pthread_mutex_unlock(&fuse->lock);
+
+    /* Special case for renaming a file where destination is same path
+     * differing only by case.  In this case we don't want to look for a case
+     * insensitive match.  This allows commands like "mv foo FOO" to work as expected.
+     */
+    int search = old_parent_node != new_parent_node
+            || strcasecmp(old_name, new_name);
+    if (!(new_actual_name = find_file_within(new_parent_path, new_name,
+            new_child_path, sizeof(new_child_path), search))) {
+        res = -ENOENT;
+        goto io_error;
+    }
+
+    TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
+    res = rename(old_child_path, new_child_path);
+    if (res < 0) {
+        res = -errno;
+        goto io_error;
+    }
+
+    pthread_mutex_lock(&fuse->lock);
+    res = rename_node_locked(child_node, new_name, new_actual_name);
+    if (!res) {
+        remove_node_from_parent_locked(child_node);
+        add_node_to_parent_locked(child_node, new_parent_node);
+    }
+    goto done;
+
+io_error:
+    pthread_mutex_lock(&fuse->lock);
+done:
+    release_node_locked(child_node);
+lookup_error:
+    pthread_mutex_unlock(&fuse->lock);
+    return res;
+}
+
+static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
+        const struct fuse_in_header* hdr, const struct fuse_open_in* req)
+{
+    struct node* node;
+    char path[PATH_MAX];
+    struct fuse_open_out out;
+    struct handle *h;
+
+    pthread_mutex_lock(&fuse->lock);
+    node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
+    TRACE("[%d] OPEN 0%o @ %llx (%s)\n", handler->token,
+            req->flags, hdr->nodeid, node ? node->name : "?");
+    pthread_mutex_unlock(&fuse->lock);
+
+    if (!node) {
+        return -ENOENT;
+    }
+    h = malloc(sizeof(*h));
+    if (!h) {
+        return -ENOMEM;
+    }
+    TRACE("[%d] OPEN %s\n", handler->token, path);
+    h->fd = open(path, req->flags);
+    if (h->fd < 0) {
+        free(h);
+        return -errno;
+    }
+    out.fh = ptr_to_id(h);
+    out.open_flags = 0;
+    out.padding = 0;
+    fuse_reply(fuse, hdr->unique, &out, sizeof(out));
+    return NO_STATUS;
+}
+
+static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
+        const struct fuse_in_header* hdr, const struct fuse_read_in* req)
+{
+    struct handle *h = id_to_ptr(req->fh);
+    __u64 unique = hdr->unique;
+    __u32 size = req->size;
+    __u64 offset = req->offset;
+    int res;
+
+    /* Don't access any other fields of hdr or req beyond this point, the read buffer
+     * overlaps the request buffer and will clobber data in the request.  This
+     * saves us 128KB per request handler thread at the cost of this scary comment. */
+
+    TRACE("[%d] READ %p(%d) %u@%llu\n", handler->token,
+            h, h->fd, size, offset);
+    if (size > sizeof(handler->read_buffer)) {
+        return -EINVAL;
+    }
+    res = pread64(h->fd, handler->read_buffer, size, offset);
+    if (res < 0) {
+        return -errno;
+    }
+    fuse_reply(fuse, unique, handler->read_buffer, res);
+    return NO_STATUS;
+}
+
+static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
+        const struct fuse_in_header* hdr, const struct fuse_write_in* req,
+        const void* buffer)
+{
+    struct fuse_write_out out;
+    struct handle *h = id_to_ptr(req->fh);
+    int res;
+
+    TRACE("[%d] WRITE %p(%d) %u@%llu\n", handler->token,
+            h, h->fd, req->size, req->offset);
+    res = pwrite64(h->fd, buffer, req->size, req->offset);
+    if (res < 0) {
+        return -errno;
+    }
+    out.size = res;
+    fuse_reply(fuse, hdr->unique, &out, sizeof(out));
+    return NO_STATUS;
+}
+
+static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
+        const struct fuse_in_header* hdr)
+{
+    char path[PATH_MAX];
+    struct statfs stat;
+    struct fuse_statfs_out out;
+    int res;
+
+    pthread_mutex_lock(&fuse->lock);
+    TRACE("[%d] STATFS\n", handler->token);
+    res = get_node_path_locked(&fuse->root, path, sizeof(path));
+    pthread_mutex_unlock(&fuse->lock);
+    if (res < 0) {
+        return -ENOENT;
+    }
+    if (statfs(fuse->root.name, &stat) < 0) {
+        return -errno;
+    }
+    memset(&out, 0, sizeof(out));
+    out.st.blocks = stat.f_blocks;
+    out.st.bfree = stat.f_bfree;
+    out.st.bavail = stat.f_bavail;
+    out.st.files = stat.f_files;
+    out.st.ffree = stat.f_ffree;
+    out.st.bsize = stat.f_bsize;
+    out.st.namelen = stat.f_namelen;
+    out.st.frsize = stat.f_frsize;
+    fuse_reply(fuse, hdr->unique, &out, sizeof(out));
+    return NO_STATUS;
+}
+
+static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
+        const struct fuse_in_header* hdr, const struct fuse_release_in* req)
+{
+    struct handle *h = id_to_ptr(req->fh);
+
+    TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
+    close(h->fd);
+    free(h);
+    return 0;
+}
+
+static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
+        const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
+{
+    int is_data_sync = req->fsync_flags & 1;
+    struct handle *h = id_to_ptr(req->fh);
+    int res;
+
+    TRACE("[%d] FSYNC %p(%d) is_data_sync=%d\n", handler->token,
+            h, h->fd, is_data_sync);
+    res = is_data_sync ? fdatasync(h->fd) : fsync(h->fd);
+    if (res < 0) {
+        return -errno;
+    }
+    return 0;
+}
+
+static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
+        const struct fuse_in_header* hdr)
+{
+    TRACE("[%d] FLUSH\n", handler->token);
+    return 0;
+}
+
+static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
+        const struct fuse_in_header* hdr, const struct fuse_open_in* req)
+{
+    struct node* node;
+    char path[PATH_MAX];
+    struct fuse_open_out out;
+    struct dirhandle *h;
+
+    pthread_mutex_lock(&fuse->lock);
+    node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
+    TRACE("[%d] OPENDIR @ %llx (%s)\n", handler->token,
+            hdr->nodeid, node ? node->name : "?");
+    pthread_mutex_unlock(&fuse->lock);
+
+    if (!node) {
+        return -ENOENT;
+    }
+    h = malloc(sizeof(*h));
+    if (!h) {
+        return -ENOMEM;
+    }
+    TRACE("[%d] OPENDIR %s\n", handler->token, path);
+    h->d = opendir(path);
+    if (!h->d) {
+        free(h);
+        return -errno;
+    }
+    out.fh = ptr_to_id(h);
+    fuse_reply(fuse, hdr->unique, &out, sizeof(out));
+    return NO_STATUS;
+}
+
+static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
+        const struct fuse_in_header* hdr, const struct fuse_read_in* req)
+{
+    char buffer[8192];
+    struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
+    struct dirent *de;
+    struct dirhandle *h = id_to_ptr(req->fh);
+
+    TRACE("[%d] READDIR %p\n", handler->token, h);
+    if (req->offset == 0) {
+        /* rewinddir() might have been called above us, so rewind here too */
+        TRACE("[%d] calling rewinddir()\n", handler->token);
+        rewinddir(h->d);
+    }
+    de = readdir(h->d);
+    if (!de) {
+        return 0;
+    }
+    fde->ino = FUSE_UNKNOWN_INO;
+    /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
+    fde->off = req->offset + 1;
+    fde->type = de->d_type;
+    fde->namelen = strlen(de->d_name);
+    memcpy(fde->name, de->d_name, fde->namelen + 1);
+    fuse_reply(fuse, hdr->unique, fde,
+            FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
+    return NO_STATUS;
+}
+
+static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
+        const struct fuse_in_header* hdr, const struct fuse_release_in* req)
+{
+    struct dirhandle *h = id_to_ptr(req->fh);
+
+    TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
+    closedir(h->d);
+    free(h);
+    return 0;
+}
+
+static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
+        const struct fuse_in_header* hdr, const struct fuse_init_in* req)
+{
+    struct fuse_init_out out;
+
+    TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
+            handler->token, req->major, req->minor, req->max_readahead, req->flags);
+    out.major = FUSE_KERNEL_VERSION;
+    out.minor = FUSE_KERNEL_MINOR_VERSION;
+    out.max_readahead = req->max_readahead;
+    out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
+    out.max_background = 32;
+    out.congestion_threshold = 32;
+    out.max_write = MAX_WRITE;
+    fuse_reply(fuse, hdr->unique, &out, sizeof(out));
+    return NO_STATUS;
+}
+
+static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
+        const struct fuse_in_header *hdr, const void *data, size_t data_len)
+{
     switch (hdr->opcode) {
     case FUSE_LOOKUP: { /* bytez[] -> entry_out */
-        TRACE("LOOKUP %llx %s\n", hdr->nodeid, (char*) data);
-        lookup_entry(fuse, node, (char*) data, hdr->unique);
-        return;
+        const char* name = data;
+        return handle_lookup(fuse, handler, hdr, name);
     }
+
     case FUSE_FORGET: {
-        struct fuse_forget_in *req = data;
-        TRACE("FORGET %llx (%s) #%lld\n", hdr->nodeid, node->name, req->nlookup);
-            /* no reply */
-        while (req->nlookup--)
-            node_release(node);
-        return;
+        const struct fuse_forget_in *req = data;
+        return handle_forget(fuse, handler, hdr, req);
     }
+
     case FUSE_GETATTR: { /* getattr_in -> attr_out */
-        struct fuse_getattr_in *req = data;
-        struct fuse_attr_out out;
-
-        TRACE("GETATTR flags=%x fh=%llx\n", req->getattr_flags, req->fh);
-
-        memset(&out, 0, sizeof(out));
-        node_get_attr(node, &out.attr);
-        out.attr_valid = 10;
-
-        fuse_reply(fuse, hdr->unique, &out, sizeof(out));
-        return;
+        const struct fuse_getattr_in *req = data;
+        return handle_getattr(fuse, handler, hdr, req);
     }
+
     case FUSE_SETATTR: { /* setattr_in -> attr_out */
-        struct fuse_setattr_in *req = data;
-        struct fuse_attr_out out;
-        char *path, buffer[PATH_BUFFER_SIZE];
-        int res = 0;
-        struct timespec times[2];
-
-        TRACE("SETATTR fh=%llx id=%llx valid=%x\n",
-              req->fh, hdr->nodeid, req->valid);
-
-        /* XXX: incomplete implementation on purpose.   chmod/chown
-         * should NEVER be implemented.*/
-
-        path = node_get_path(node, buffer, 0);
-        if (req->valid & FATTR_SIZE)
-            res = truncate(path, req->size);
-        if (res)
-            goto getout;
-
-        /* Handle changing atime and mtime.  If FATTR_ATIME_and FATTR_ATIME_NOW
-         * are both set, then set it to the current time.  Else, set it to the
-         * time specified in the request.  Same goes for mtime.  Use utimensat(2)
-         * as it allows ATIME and MTIME to be changed independently, and has
-         * nanosecond resolution which fuse also has.
-         */
-        if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
-            times[0].tv_nsec = UTIME_OMIT;
-            times[1].tv_nsec = UTIME_OMIT;
-            if (req->valid & FATTR_ATIME) {
-                if (req->valid & FATTR_ATIME_NOW) {
-                  times[0].tv_nsec = UTIME_NOW;
-                } else {
-                  times[0].tv_sec = req->atime;
-                  times[0].tv_nsec = req->atimensec;
-                }
-            }
-            if (req->valid & FATTR_MTIME) {
-                if (req->valid & FATTR_MTIME_NOW) {
-                  times[1].tv_nsec = UTIME_NOW;
-                } else {
-                  times[1].tv_sec = req->mtime;
-                  times[1].tv_nsec = req->mtimensec;
-                }
-            }
-            TRACE("Calling utimensat on %s with atime %ld, mtime=%ld\n", path, times[0].tv_sec, times[1].tv_sec);
-            res = utimensat(-1, path, times, 0);
-        }
-
-        getout:
-        memset(&out, 0, sizeof(out));
-        node_get_attr(node, &out.attr);
-        out.attr_valid = 10;
-
-        if (res)
-            fuse_status(fuse, hdr->unique, -errno);
-        else
-            fuse_reply(fuse, hdr->unique, &out, sizeof(out));
-        return;
+        const struct fuse_setattr_in *req = data;
+        return handle_setattr(fuse, handler, hdr, req);
     }
+
 //    case FUSE_READLINK:
 //    case FUSE_SYMLINK:
     case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
-        struct fuse_mknod_in *req = data;
-        char *path, buffer[PATH_BUFFER_SIZE];
-        char *name = ((char*) data) + sizeof(*req);
-        int res;
-
-        TRACE("MKNOD %s @ %llx\n", name, hdr->nodeid);
-        path = node_get_path(node, buffer, name);
-
-        req->mode = (req->mode & (~0777)) | 0664;
-        res = mknod(path, req->mode, req->rdev); /* XXX perm?*/
-        if (res < 0) {
-            fuse_status(fuse, hdr->unique, -errno);
-        } else {
-            lookup_entry(fuse, node, name, hdr->unique);
-        }
-        return;
+        const struct fuse_mknod_in *req = data;
+        const char *name = ((const char*) data) + sizeof(*req);
+        return handle_mknod(fuse, handler, hdr, req, name);
     }
+
     case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
-        struct fuse_mkdir_in *req = data;
-        struct fuse_entry_out out;
-        char *path, buffer[PATH_BUFFER_SIZE];
-        char *name = ((char*) data) + sizeof(*req);
-        int res;
-
-        TRACE("MKDIR %s @ %llx 0%o\n", name, hdr->nodeid, req->mode);
-        path = node_get_path(node, buffer, name);
-
-        req->mode = (req->mode & (~0777)) | 0775;
-        res = mkdir(path, req->mode);
-        if (res < 0) {
-            fuse_status(fuse, hdr->unique, -errno);
-        } else {
-            lookup_entry(fuse, node, name, hdr->unique);
-        }
-        return;
+        const struct fuse_mkdir_in *req = data;
+        const char *name = ((const char*) data) + sizeof(*req);
+        return handle_mkdir(fuse, handler, hdr, req, name);
     }
+
     case FUSE_UNLINK: { /* bytez[] -> */
-        char *path, buffer[PATH_BUFFER_SIZE];
-        int res;
-        TRACE("UNLINK %s @ %llx\n", (char*) data, hdr->nodeid);
-        path = node_get_path(node, buffer, (char*) data);
-        res = unlink(path);
-        fuse_status(fuse, hdr->unique, res ? -errno : 0);
-        return;
+        const char* name = data;
+        return handle_unlink(fuse, handler, hdr, name);
     }
+
     case FUSE_RMDIR: { /* bytez[] -> */
-        char *path, buffer[PATH_BUFFER_SIZE];
-        int res;
-        TRACE("RMDIR %s @ %llx\n", (char*) data, hdr->nodeid);
-        path = node_get_path(node, buffer, (char*) data);
-        res = rmdir(path);
-        fuse_status(fuse, hdr->unique, res ? -errno : 0);
-        return;
+        const char* name = data;
+        return handle_rmdir(fuse, handler, hdr, name);
     }
+
     case FUSE_RENAME: { /* rename_in, oldname, newname ->  */
-        struct fuse_rename_in *req = data;
-        char *oldname = ((char*) data) + sizeof(*req);
-        char *newname = oldname + strlen(oldname) + 1;
-        char *oldpath, oldbuffer[PATH_BUFFER_SIZE];
-        char *newpath, newbuffer[PATH_BUFFER_SIZE];
-        struct node *target;
-        struct node *newparent;
-        int res;
-
-        TRACE("RENAME %s->%s @ %llx\n", oldname, newname, hdr->nodeid);
-
-        target = lookup_child_by_name(node, oldname);
-        if (!target) {
-            fuse_status(fuse, hdr->unique, -ENOENT);
-            return;
-        }
-        oldpath = node_get_path(node, oldbuffer, oldname);
-
-        newparent = lookup_by_inode(fuse, req->newdir);
-        if (!newparent) {
-            fuse_status(fuse, hdr->unique, -ENOENT);
-            return;
-        }
-        if (newparent == node) {
-            /* Special case for renaming a file where destination
-             * is same path differing only by case.
-             * In this case we don't want to look for a case insensitive match.
-             * This allows commands like "mv foo FOO" to work as expected.
-             */
-            newpath = do_node_get_path(newparent, newbuffer, newname, NO_CASE_SENSITIVE_MATCH);
-        } else {
-            newpath = node_get_path(newparent, newbuffer, newname);
-        }
-
-        if (!remove_child(node, target->nid)) {
-            ERROR("RENAME remove_child not found");
-            fuse_status(fuse, hdr->unique, -ENOENT);
-            return;
-        }
-        if (!rename_node(target, newname)) {
-            fuse_status(fuse, hdr->unique, -ENOMEM);
-            return;
-        }
-        add_node_to_parent(target, newparent);
-
-        res = rename(oldpath, newpath);
-        TRACE("RENAME result %d\n", res);
-
-        fuse_status(fuse, hdr->unique, res ? -errno : 0);
-        return;
+        const struct fuse_rename_in *req = data;
+        const char *old_name = ((const char*) data) + sizeof(*req);
+        const char *new_name = old_name + strlen(old_name) + 1;
+        return handle_rename(fuse, handler, hdr, req, old_name, new_name);
     }
-//    case FUSE_LINK:        
+
+//    case FUSE_LINK:
     case FUSE_OPEN: { /* open_in -> open_out */
-        struct fuse_open_in *req = data;
-        struct fuse_open_out out;
-        char *path, buffer[PATH_BUFFER_SIZE];
-        struct handle *h;
-
-        h = malloc(sizeof(*h));
-        if (!h) {
-            fuse_status(fuse, hdr->unique, -ENOMEM);
-            return;
-        }
-
-        path = node_get_path(node, buffer, 0);
-        TRACE("OPEN %llx '%s' 0%o fh=%p\n", hdr->nodeid, path, req->flags, h);
-        h->fd = open(path, req->flags);
-        if (h->fd < 0) {
-            ERROR("ERROR\n");
-            fuse_status(fuse, hdr->unique, -errno);
-            free(h);
-            return;
-        }
-        out.fh = ptr_to_id(h);
-        out.open_flags = 0;
-        out.padding = 0;
-        fuse_reply(fuse, hdr->unique, &out, sizeof(out));
-        return;
+        const struct fuse_open_in *req = data;
+        return handle_open(fuse, handler, hdr, req);
     }
+
     case FUSE_READ: { /* read_in -> byte[] */
-        char buffer[128 * 1024];
-        struct fuse_read_in *req = data;
-        struct handle *h = id_to_ptr(req->fh);
-        int res;
-        TRACE("READ %p(%d) %u@%llu\n", h, h->fd, req->size, req->offset);
-        if (req->size > sizeof(buffer)) {
-            fuse_status(fuse, hdr->unique, -EINVAL);
-            return;
-        }
-        res = pread64(h->fd, buffer, req->size, req->offset);
-        if (res < 0) {
-            fuse_status(fuse, hdr->unique, -errno);
-            return;
-        }
-        fuse_reply(fuse, hdr->unique, buffer, res);
-        return;
+        const struct fuse_read_in *req = data;
+        return handle_read(fuse, handler, hdr, req);
     }
+
     case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
-        struct fuse_write_in *req = data;
-        struct fuse_write_out out;
-        struct handle *h = id_to_ptr(req->fh);
-        int res;
-        TRACE("WRITE %p(%d) %u@%llu\n", h, h->fd, req->size, req->offset);
-        res = pwrite64(h->fd, ((char*) data) + sizeof(*req), req->size, req->offset);
-        if (res < 0) {
-            fuse_status(fuse, hdr->unique, -errno);
-            return;
-        }
-        out.size = res;
-        fuse_reply(fuse, hdr->unique, &out, sizeof(out));
-        goto oops;
+        const struct fuse_write_in *req = data;
+        const void* buffer = (const __u8*)data + sizeof(*req);
+        return handle_write(fuse, handler, hdr, req, buffer);
     }
+
     case FUSE_STATFS: { /* getattr_in -> attr_out */
-        struct statfs stat;
-        struct fuse_statfs_out out;
-        int res;
-
-        TRACE("STATFS\n");
-
-        if (statfs(fuse->root.name, &stat)) {
-            fuse_status(fuse, hdr->unique, -errno);
-            return;
-        }
-
-        memset(&out, 0, sizeof(out));
-        out.st.blocks = stat.f_blocks;
-        out.st.bfree = stat.f_bfree;
-        out.st.bavail = stat.f_bavail;
-        out.st.files = stat.f_files;
-        out.st.ffree = stat.f_ffree;
-        out.st.bsize = stat.f_bsize;
-        out.st.namelen = stat.f_namelen;
-        out.st.frsize = stat.f_frsize;
-        fuse_reply(fuse, hdr->unique, &out, sizeof(out));
-        return;
+        return handle_statfs(fuse, handler, hdr);
     }
+
     case FUSE_RELEASE: { /* release_in -> */
-        struct fuse_release_in *req = data;
-        struct handle *h = id_to_ptr(req->fh);
-        TRACE("RELEASE %p(%d)\n", h, h->fd);
-        close(h->fd);
-        free(h);
-        fuse_status(fuse, hdr->unique, 0);
-        return;
+        const struct fuse_release_in *req = data;
+        return handle_release(fuse, handler, hdr, req);
     }
-//    case FUSE_FSYNC:
+
+    case FUSE_FSYNC: {
+        const struct fuse_fsync_in *req = data;
+        return handle_fsync(fuse, handler, hdr, req);
+    }
+
 //    case FUSE_SETXATTR:
 //    case FUSE_GETXATTR:
 //    case FUSE_LISTXATTR:
 //    case FUSE_REMOVEXATTR:
-    case FUSE_FLUSH:
-        fuse_status(fuse, hdr->unique, 0);
-        return;
+    case FUSE_FLUSH: {
+        return handle_flush(fuse, handler, hdr);
+    }
+
     case FUSE_OPENDIR: { /* open_in -> open_out */
-        struct fuse_open_in *req = data;
-        struct fuse_open_out out;
-        char *path, buffer[PATH_BUFFER_SIZE];
-        struct dirhandle *h;
-
-        h = malloc(sizeof(*h));
-        if (!h) {
-            fuse_status(fuse, hdr->unique, -ENOMEM);
-            return;
-        }
-
-        path = node_get_path(node, buffer, 0);
-        TRACE("OPENDIR %llx '%s'\n", hdr->nodeid, path);
-        h->d = opendir(path);
-        if (h->d == 0) {
-            ERROR("ERROR\n");
-            fuse_status(fuse, hdr->unique, -errno);
-            free(h);
-            return;
-        }
-        out.fh = ptr_to_id(h);
-        fuse_reply(fuse, hdr->unique, &out, sizeof(out));
-        return;
+        const struct fuse_open_in *req = data;
+        return handle_opendir(fuse, handler, hdr, req);
     }
+
     case FUSE_READDIR: {
-        struct fuse_read_in *req = data;
-        char buffer[8192];
-        struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
-        struct dirent *de;
-        struct dirhandle *h = id_to_ptr(req->fh);
-        TRACE("READDIR %p\n", h);
-        if (req->offset == 0) {
-            /* rewinddir() might have been called above us, so rewind here too */
-            TRACE("calling rewinddir()\n");
-            rewinddir(h->d);
-        }
-        de = readdir(h->d);
-        if (!de) {
-            fuse_status(fuse, hdr->unique, 0);
-            return;
-        }
-        fde->ino = FUSE_UNKNOWN_INO;
-        /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
-        fde->off = req->offset + 1;
-        fde->type = de->d_type;
-        fde->namelen = strlen(de->d_name);
-        memcpy(fde->name, de->d_name, fde->namelen + 1);
-        fuse_reply(fuse, hdr->unique, fde,
-                   FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
-        return;
+        const struct fuse_read_in *req = data;
+        return handle_readdir(fuse, handler, hdr, req);
     }
+
     case FUSE_RELEASEDIR: { /* release_in -> */
-        struct fuse_release_in *req = data;
-        struct dirhandle *h = id_to_ptr(req->fh);
-        TRACE("RELEASEDIR %p\n",h);
-        closedir(h->d);
-        free(h);
-        fuse_status(fuse, hdr->unique, 0);
-        return;
+        const struct fuse_release_in *req = data;
+        return handle_releasedir(fuse, handler, hdr, req);
     }
+
 //    case FUSE_FSYNCDIR:
     case FUSE_INIT: { /* init_in -> init_out */
-        struct fuse_init_in *req = data;
-        struct fuse_init_out out;
-        
-        TRACE("INIT ver=%d.%d maxread=%d flags=%x\n",
-                req->major, req->minor, req->max_readahead, req->flags);
-
-        out.major = FUSE_KERNEL_VERSION;
-        out.minor = FUSE_KERNEL_MINOR_VERSION;
-        out.max_readahead = req->max_readahead;
-        out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
-        out.max_background = 32;
-        out.congestion_threshold = 32;
-        out.max_write = 256 * 1024;
-
-        fuse_reply(fuse, hdr->unique, &out, sizeof(out));
-        return;
+        const struct fuse_init_in *req = data;
+        return handle_init(fuse, handler, hdr, req);
     }
+
     default: {
-        struct fuse_out_header h;
-        ERROR("NOTIMPL op=%d uniq=%llx nid=%llx\n",
-                hdr->opcode, hdr->unique, hdr->nodeid);
-
-        oops:
-        h.len = sizeof(h);
-        h.error = -ENOSYS;
-        h.unique = hdr->unique;
-        write(fuse->fd, &h, sizeof(h));
-        break;
+        TRACE("[%d] NOTIMPL op=%d uniq=%llx nid=%llx\n",
+                handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
+        return -ENOSYS;
     }
-    }   
+    }
 }
 
-void handle_fuse_requests(struct fuse *fuse)
+static void handle_fuse_requests(struct fuse_handler* handler)
 {
-    unsigned char req[256 * 1024 + 128];
-    int len;
-    
+    struct fuse* fuse = handler->fuse;
     for (;;) {
-        len = read(fuse->fd, req, sizeof(req));
+        ssize_t len = read(fuse->fd,
+                handler->request_buffer, sizeof(handler->request_buffer));
         if (len < 0) {
-            if (errno == EINTR)
-                continue;
-            ERROR("handle_fuse_requests: errno=%d\n", errno);
-            return;
+            if (errno != EINTR) {
+                ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
+            }
+            continue;
         }
-        handle_fuse_request(fuse, (void*) req, (void*) (req + sizeof(struct fuse_in_header)), len);
+
+        if ((size_t)len < sizeof(struct fuse_in_header)) {
+            ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
+            continue;
+        }
+
+        const struct fuse_in_header *hdr = (void*)handler->request_buffer;
+        if (hdr->len != (size_t)len) {
+            ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
+                    handler->token, (size_t)len, hdr->len);
+            continue;
+        }
+
+        const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
+        size_t data_len = len - sizeof(struct fuse_in_header);
+        __u64 unique = hdr->unique;
+        int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
+
+        /* We do not access the request again after this point because the underlying
+         * buffer storage may have been reused while processing the request. */
+
+        if (res != NO_STATUS) {
+            if (res) {
+                TRACE("[%d] ERROR %d\n", handler->token, res);
+            }
+            fuse_status(fuse, unique, res);
+        }
     }
 }
 
+static void* start_handler(void* data)
+{
+    struct fuse_handler* handler = data;
+    handle_fuse_requests(handler);
+    return NULL;
+}
+
+static int ignite_fuse(struct fuse* fuse, int num_threads)
+{
+    struct fuse_handler* handlers;
+    int i;
+
+    handlers = malloc(num_threads * sizeof(struct fuse_handler));
+    if (!handlers) {
+        ERROR("cannot allocate storage for threads");
+        return -ENOMEM;
+    }
+
+    for (i = 0; i < num_threads; i++) {
+        handlers[i].fuse = fuse;
+        handlers[i].token = i;
+    }
+
+    for (i = 1; i < num_threads; i++) {
+        pthread_t thread;
+        int res = pthread_create(&thread, NULL, start_handler, &handlers[i]);
+        if (res) {
+            ERROR("failed to start thread #%d, error=%d", i, res);
+            goto quit;
+        }
+    }
+    handle_fuse_requests(&handlers[0]);
+    ERROR("terminated prematurely");
+
+    /* don't bother killing all of the other threads or freeing anything,
+     * should never get here anyhow */
+quit:
+    exit(1);
+}
+
 static int usage()
 {
-    ERROR("usage: sdcard [-l -f] <path> <uid> <gid>\n\n\t-l force file names to lower case when creating new files\n\t-f fix up file system before starting (repairs bad file name case and group ownership)\n");
-    return -1;
+    ERROR("usage: sdcard [-t<threads>] <path> <uid> <gid>\n"
+            "    -t<threads>: specify number of threads to use, default -t%d\n"
+            "\n", DEFAULT_NUM_THREADS);
+    return 1;
+}
+
+static int run(const char* path, uid_t uid, gid_t gid, int num_threads)
+{
+    int fd;
+    char opts[256];
+    int res;
+    struct fuse fuse;
+
+    /* cleanup from previous instance, if necessary */
+    umount2(MOUNT_POINT, 2);
+
+    fd = open("/dev/fuse", O_RDWR);
+    if (fd < 0){
+        ERROR("cannot open fuse device (error %d)\n", errno);
+        return -1;
+    }
+
+    snprintf(opts, sizeof(opts),
+            "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
+            fd, uid, gid);
+
+    res = mount("/dev/fuse", MOUNT_POINT, "fuse", MS_NOSUID | MS_NODEV, opts);
+    if (res < 0) {
+        ERROR("cannot mount fuse filesystem (error %d)\n", errno);
+        goto error;
+    }
+
+    res = setgid(gid);
+    if (res < 0) {
+        ERROR("cannot setgid (error %d)\n", errno);
+        goto error;
+    }
+
+    res = setuid(uid);
+    if (res < 0) {
+        ERROR("cannot setuid (error %d)\n", errno);
+        goto error;
+    }
+
+    fuse_init(&fuse, fd, path);
+
+    umask(0);
+    res = ignite_fuse(&fuse, num_threads);
+
+    /* we do not attempt to umount the file system here because we are no longer
+     * running as the root user */
+
+error:
+    close(fd);
+    return res;
 }
 
 int main(int argc, char **argv)
 {
-    struct fuse fuse;
-    char opts[256];
-    int fd;
     int res;
     const char *path = NULL;
+    uid_t uid = 0;
+    gid_t gid = 0;
+    int num_threads = DEFAULT_NUM_THREADS;
     int i;
 
     for (i = 1; i < argc; i++) {
         char* arg = argv[i];
-        if (!path)
+        if (!strncmp(arg, "-t", 2))
+            num_threads = strtoul(arg + 2, 0, 10);
+        else if (!path)
             path = arg;
-        else if (uid == -1)
+        else if (!uid)
             uid = strtoul(arg, 0, 10);
-        else if (gid == -1)
+        else if (!gid)
             gid = strtoul(arg, 0, 10);
         else {
             ERROR("too many arguments\n");
@@ -985,42 +1330,15 @@
         ERROR("no path specified\n");
         return usage();
     }
-    if (uid <= 0 || gid <= 0) {
+    if (!uid || !gid) {
         ERROR("uid and gid must be nonzero\n");
         return usage();
     }
-
-        /* cleanup from previous instance, if necessary */
-    umount2(MOUNT_POINT, 2);
-
-    fd = open("/dev/fuse", O_RDWR);
-    if (fd < 0){
-        ERROR("cannot open fuse device (%d)\n", errno);
-        return -1;
+    if (num_threads < 1) {
+        ERROR("number of threads must be at least 1\n");
+        return usage();
     }
 
-    sprintf(opts, "fd=%i,rootmode=40000,default_permissions,allow_other,"
-            "user_id=%d,group_id=%d", fd, uid, gid);
-    
-    res = mount("/dev/fuse", MOUNT_POINT, "fuse", MS_NOSUID | MS_NODEV, opts);
-    if (res < 0) {
-        ERROR("cannot mount fuse filesystem (%d)\n", errno);
-        return -1;
-    }
-
-    if (setgid(gid) < 0) {
-        ERROR("cannot setgid!\n");
-        return -1;
-    }
-    if (setuid(uid) < 0) {
-        ERROR("cannot setuid!\n");
-        return -1;
-    }
-
-    fuse_init(&fuse, fd, path);
-
-    umask(0);
-    handle_fuse_requests(&fuse);
-    
-    return 0;
+    res = run(path, uid, gid, num_threads);
+    return res < 0 ? 1 : 0;
 }
diff --git a/toolbox/dd.c b/toolbox/dd.c
index c6af3ea..53a6206 100644
--- a/toolbox/dd.c
+++ b/toolbox/dd.c
@@ -64,7 +64,7 @@
 
 #include "dd.h"
 
-#define NO_CONV
+//#define NO_CONV
 
 //#include "extern.h"
 void block(void);
@@ -91,12 +91,9 @@
 extern u_int		files_cnt;
 extern int		progress;
 extern const u_char	*ctab;
-extern const u_char	a2e_32V[], a2e_POSIX[];
-extern const u_char	e2a_32V[], e2a_POSIX[];
-extern const u_char	a2ibm_32V[], a2ibm_POSIX[];
-extern u_char		casetab[];
 
 
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
 #define MAX(a, b) ((a) > (b) ? (a) : (b))
 
 static void dd_close(void);
@@ -243,42 +240,6 @@
 	if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK))
 		(void)ftruncate(out.fd, (off_t)out.offset * out.dbsz);
 
-	/*
-	 * If converting case at the same time as another conversion, build a
-	 * table that does both at once.  If just converting case, use the
-	 * built-in tables.
-	 */
-	if (ddflags & (C_LCASE|C_UCASE)) {
-#ifdef	NO_CONV
-		/* Should not get here, but just in case... */
-		fprintf(stderr, "case conv and -DNO_CONV\n");
-		exit(1);
-		/* NOTREACHED */
-#else	/* NO_CONV */
-		u_int cnt;
-
-		if (ddflags & C_ASCII || ddflags & C_EBCDIC) {
-			if (ddflags & C_LCASE) {
-				for (cnt = 0; cnt < 0377; ++cnt)
-					casetab[cnt] = tolower(ctab[cnt]);
-			} else {
-				for (cnt = 0; cnt < 0377; ++cnt)
-					casetab[cnt] = toupper(ctab[cnt]);
-			}
-		} else {
-			if (ddflags & C_LCASE) {
-				for (cnt = 0; cnt < 0377; ++cnt)
-					casetab[cnt] = tolower(cnt);
-			} else {
-				for (cnt = 0; cnt < 0377; ++cnt)
-					casetab[cnt] = toupper(cnt);
-			}
-		}
-
-		ctab = casetab;
-#endif	/* NO_CONV */
-	}
-
 	(void)gettimeofday(&st.start, NULL);	/* Statistics timestamp. */
 }
 
@@ -796,6 +757,9 @@
 void
 def_close(void)
 {
+	if (ddflags & C_FDATASYNC) {
+		fdatasync(out.fd);
+	}
 
 	/* Just update the count, everything is already in the buffer. */
 	if (in.dbcnt)
@@ -1301,21 +1265,14 @@
 	u_int set, noset;
 	const u_char *ctab;
 } clist[] = {
-	{ "ascii",	C_ASCII,	C_EBCDIC,	e2a_POSIX },
 	{ "block",	C_BLOCK,	C_UNBLOCK,	NULL },
-	{ "ebcdic",	C_EBCDIC,	C_ASCII,	a2e_POSIX },
-	{ "ibm",	C_EBCDIC,	C_ASCII,	a2ibm_POSIX },
-	{ "lcase",	C_LCASE,	C_UCASE,	NULL },
+	{ "fdatasync",	C_FDATASYNC,	0,		NULL },
 	{ "noerror",	C_NOERROR,	0,		NULL },
 	{ "notrunc",	C_NOTRUNC,	0,		NULL },
-	{ "oldascii",	C_ASCII,	C_EBCDIC,	e2a_32V },
-	{ "oldebcdic",	C_EBCDIC,	C_ASCII,	a2e_32V },
-	{ "oldibm",	C_EBCDIC,	C_ASCII,	a2ibm_32V },
 	{ "osync",	C_OSYNC,	C_BS,		NULL },
 	{ "sparse",	C_SPARSE,	0,		NULL },
 	{ "swab",	C_SWAB,		0,		NULL },
 	{ "sync",	C_SYNC,		0,		NULL },
-	{ "ucase",	C_UCASE,	C_LCASE,	NULL },
 	{ "unblock",	C_UNBLOCK,	C_BLOCK,	NULL },
 	/* If you add items to this table, be sure to add the
 	 * conversions to the C_BS check in the jcl routine above.
diff --git a/toolbox/dd.h b/toolbox/dd.h
index cca1024..89f2833 100644
--- a/toolbox/dd.h
+++ b/toolbox/dd.h
@@ -91,3 +91,4 @@
 #define	C_UNBLOCK	0x80000
 #define	C_OSYNC		0x100000
 #define	C_SPARSE	0x200000
+#define	C_FDATASYNC	0x400000
diff --git a/toolbox/powerd.c b/toolbox/powerd.c
deleted file mode 100644
index 1f29a8b..0000000
--- a/toolbox/powerd.c
+++ /dev/null
@@ -1,441 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <fcntl.h>
-#include <string.h>
-#include <errno.h>
-#include <time.h>
-#include <sys/select.h>
-#include <sys/inotify.h>
-
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
-
-//#include <linux/input.h> // this does not compile
-
-// from <linux/input.h>
-
-struct input_event {
-	struct timeval time;
-	__u16 type;
-	__u16 code;
-	__s32 value;
-};
-
-#define EVIOCGVERSION		_IOR('E', 0x01, int)			/* get driver version */
-#define EVIOCGID		_IOR('E', 0x02, struct input_id)	/* get device ID */
-#define EVIOCGKEYCODE		_IOR('E', 0x04, int[2])			/* get keycode */
-#define EVIOCSKEYCODE		_IOW('E', 0x04, int[2])			/* set keycode */
-
-#define EVIOCGNAME(len)		_IOC(_IOC_READ, 'E', 0x06, len)		/* get device name */
-#define EVIOCGPHYS(len)		_IOC(_IOC_READ, 'E', 0x07, len)		/* get physical location */
-#define EVIOCGUNIQ(len)		_IOC(_IOC_READ, 'E', 0x08, len)		/* get unique identifier */
-
-#define EVIOCGKEY(len)		_IOC(_IOC_READ, 'E', 0x18, len)		/* get global keystate */
-#define EVIOCGLED(len)		_IOC(_IOC_READ, 'E', 0x19, len)		/* get all LEDs */
-#define EVIOCGSND(len)		_IOC(_IOC_READ, 'E', 0x1a, len)		/* get all sounds status */
-#define EVIOCGSW(len)		_IOC(_IOC_READ, 'E', 0x1b, len)		/* get all switch states */
-
-#define EVIOCGBIT(ev,len)	_IOC(_IOC_READ, 'E', 0x20 + ev, len)	/* get event bits */
-#define EVIOCGABS(abs)		_IOR('E', 0x40 + abs, struct input_absinfo)		/* get abs value/limits */
-#define EVIOCSABS(abs)		_IOW('E', 0xc0 + abs, struct input_absinfo)		/* set abs value/limits */
-
-#define EVIOCSFF		_IOC(_IOC_WRITE, 'E', 0x80, sizeof(struct ff_effect))	/* send a force effect to a force feedback device */
-#define EVIOCRMFF		_IOW('E', 0x81, int)			/* Erase a force effect */
-#define EVIOCGEFFECTS		_IOR('E', 0x84, int)			/* Report number of effects playable at the same time */
-
-#define EVIOCGRAB		_IOW('E', 0x90, int)			/* Grab/Release device */
-
-/*
- * Event types
- */
-
-#define EV_SYN			0x00
-#define EV_KEY			0x01
-#define EV_REL			0x02
-#define EV_ABS			0x03
-#define EV_MSC			0x04
-#define EV_SW			0x05
-#define EV_LED			0x11
-#define EV_SND			0x12
-#define EV_REP			0x14
-#define EV_FF			0x15
-#define EV_PWR			0x16
-#define EV_FF_STATUS		0x17
-#define EV_MAX			0x1f
-
-#define KEY_POWER		116
-#define KEY_SLEEP		142
-#define SW_0		0x00
-
-// end <linux/input.h>
-
-struct notify_entry {
-    int id;
-    int (*handler)(struct notify_entry *entry, struct inotify_event *event);
-    const char *filename;
-};
-
-int charging_state_notify_handler(struct notify_entry *entry, struct inotify_event *event)
-{
-    static int state = -1;
-    int last_state;
-    char buf[40];
-    int read_len;
-    int fd;
-
-    last_state = state;
-    fd = open(entry->filename, O_RDONLY);
-    read_len = read(fd, buf, sizeof(buf));
-    if(read_len > 0) {
-        //printf("charging_state_notify_handler: \"%s\"\n", buf);
-        state = !(strncmp(buf, "Unknown", 7) == 0 
-                  || strncmp(buf, "Discharging", 11) == 0);
-    }
-    close(fd);
-    //printf("charging_state_notify_handler: %d -> %d\n", last_state, state);
-    return state > last_state;
-}
-
-struct notify_entry watched_files[] = {
-    {
-        .filename = "/sys/android_power/charging_state",
-        .handler = charging_state_notify_handler
-    }
-};
-
-int call_notify_handler(struct inotify_event *event)
-{
-    unsigned int start, i;
-    start = event->wd - watched_files[0].id;
-    if(start >= ARRAY_SIZE(watched_files))
-        start = 0;
-    //printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
-    for(i = start; i < ARRAY_SIZE(watched_files); i++) {
-        if(event->wd == watched_files[i].id) {
-            if(watched_files[i].handler) {
-                return watched_files[i].handler(&watched_files[i], event);
-            }
-            return 1;
-        }
-    }
-    for(i = 0; i < start; i++) {
-        if(event->wd == watched_files[i].id) {
-            if(watched_files[i].handler) {
-                return watched_files[i].handler(&watched_files[i], event);
-            }
-            return 1;
-        }
-    }
-    return 0;
-}
-
-int handle_inotify_event(int nfd)
-{
-    int res;
-    int wake_up = 0;
-    struct inotify_event *event;
-    char event_buf[512];
-    int event_pos = 0;
-
-    res = read(nfd, event_buf, sizeof(event_buf));
-    if(res < (int)sizeof(*event)) {
-        if(errno == EINTR)
-            return 0;
-        fprintf(stderr, "could not get event, %s\n", strerror(errno));
-        return 0;
-    }
-    printf("got %d bytes of event information\n", res);
-    while(res >= (int)sizeof(*event)) {
-        int event_size;
-        event = (struct inotify_event *)(event_buf + event_pos);
-        wake_up |= call_notify_handler(event);
-        event_size = sizeof(*event) + event->len;
-        res -= event_size;
-        event_pos += event_size;
-    }
-    return wake_up;
-}
-
-int powerd_main(int argc, char *argv[])
-{
-    int c;
-    unsigned int i;
-    int res;
-    struct timeval tv;
-    int eventfd;
-    int notifyfd;
-    int powerfd;
-    int powerfd_is_sleep;
-    int user_activity_fd;
-    int acquire_partial_wake_lock_fd;
-    int acquire_full_wake_lock_fd;
-    int release_wake_lock_fd;
-    char *eventdev = "/dev/input/event0";
-    const char *android_sleepdev = "/sys/android_power/request_sleep";
-    const char *android_autooff_dev = "/sys/android_power/auto_off_timeout";
-    const char *android_user_activity_dev = "/sys/android_power/last_user_activity";
-    const char *android_acquire_partial_wake_lock_dev = "/sys/android_power/acquire_partial_wake_lock";
-    const char *android_acquire_full_wake_lock_dev = "/sys/android_power/acquire_full_wake_lock";
-    const char *android_release_wake_lock_dev = "/sys/android_power/release_wake_lock";
-    const char *powerdev = "/sys/power/state";
-    const char suspendstring[] = "standby";
-    const char wakelockstring[] = "powerd";
-    fd_set rfds;
-    struct input_event event;
-    struct input_event light_event;
-    struct input_event light_event2;
-    int gotkey = 1;
-    time_t idle_time = 5;
-    const char *idle_time_string = "5";
-    time_t lcd_light_time = 0;
-    time_t key_light_time = 0;
-    int verbose = 1;
-    int event_sleep = 0;
-    int got_power_key_down = 0;
-    struct timeval power_key_down_time = { 0, 0 };
-
-    light_event.type = EV_LED;
-    light_event.code = 4; // bright lcd backlight
-    light_event.value = 0; // light off -- sleep after timeout
-
-    light_event2.type = EV_LED;
-    light_event2.code = 8; // keyboard backlight
-    light_event2.value = 0; // light off -- sleep after timeout
-
-    do {
-        c = getopt(argc, argv, "e:ni:vql:k:");
-        if (c == EOF)
-            break;
-        switch (c) {
-        case 'e':
-            eventdev = optarg;
-            break;
-        case 'n':
-            gotkey = 0;
-            break;
-        case 'i':
-            idle_time = atoi(optarg);
-            idle_time_string = optarg;
-            break;
-        case 'v':
-            verbose = 2;
-            break;
-        case 'q':
-            verbose = 0;
-            break;
-        case 'l':
-            lcd_light_time = atoi(optarg);
-            break;
-        case 'k':
-            key_light_time = atoi(optarg);
-            break;
-        case '?':
-            fprintf(stderr, "%s: invalid option -%c\n",
-                argv[0], optopt);
-            exit(1);
-        }
-    } while (1);
-    if(optind  != argc) {
-        fprintf(stderr,"%s [-e eventdev]\n", argv[0]);
-        return 1;
-    }
-
-    eventfd = open(eventdev, O_RDWR | O_NONBLOCK);
-    if(eventfd < 0) {
-        fprintf(stderr, "could not open %s, %s\n", eventdev, strerror(errno));
-        return 1;
-    }
-    if(key_light_time >= lcd_light_time) {
-        lcd_light_time = key_light_time + 1;
-        fprintf(stderr,"lcd bright backlight time must be longer than keyboard backlight time.\n"
-            "Setting lcd bright backlight time to %ld seconds\n", lcd_light_time);
-    }
-
-    user_activity_fd = open(android_user_activity_dev, O_RDWR);
-    if(user_activity_fd >= 0) {
-        int auto_off_fd = open(android_autooff_dev, O_RDWR);
-        write(auto_off_fd, idle_time_string, strlen(idle_time_string));
-        close(auto_off_fd);
-    }
-
-    powerfd = open(android_sleepdev, O_RDWR);
-    if(powerfd >= 0) {
-        powerfd_is_sleep = 1;
-        if(verbose > 0)
-            printf("Using android sleep dev: %s\n", android_sleepdev);
-    }
-    else {
-        powerfd_is_sleep = 0;
-        powerfd = open(powerdev, O_RDWR);
-        if(powerfd >= 0) {
-            if(verbose > 0)
-                printf("Using linux power dev: %s\n", powerdev);
-        }
-    }
-    if(powerfd < 0) {
-        fprintf(stderr, "could not open %s, %s\n", powerdev, strerror(errno));
-        return 1;
-    }
-
-    notifyfd = inotify_init();
-    if(notifyfd < 0) {
-        fprintf(stderr, "inotify_init failed, %s\n", strerror(errno));
-        return 1;
-    }
-    fcntl(notifyfd, F_SETFL, O_NONBLOCK | fcntl(notifyfd, F_GETFL));
-    for(i = 0; i < ARRAY_SIZE(watched_files); i++) {
-        watched_files[i].id = inotify_add_watch(notifyfd, watched_files[i].filename, IN_MODIFY);
-        printf("Watching %s, id %d\n", watched_files[i].filename, watched_files[i].id);
-    }
-
-    acquire_partial_wake_lock_fd = open(android_acquire_partial_wake_lock_dev, O_RDWR);
-    acquire_full_wake_lock_fd = open(android_acquire_full_wake_lock_dev, O_RDWR);
-    release_wake_lock_fd = open(android_release_wake_lock_dev, O_RDWR);
-
-    if(user_activity_fd >= 0) {
-        idle_time = 60*60*24; // driver handles real timeout
-    }
-    if(gotkey) {
-        tv.tv_sec = idle_time;
-        tv.tv_usec = 0;
-    }
-    else {
-        tv.tv_sec = 0;
-        tv.tv_usec = 500000;
-    }
-    
-    while(1) {
-        FD_ZERO(&rfds);
-        //FD_SET(0, &rfds);
-        FD_SET(eventfd, &rfds);
-        FD_SET(notifyfd, &rfds);
-        res = select(((notifyfd > eventfd) ? notifyfd : eventfd) + 1, &rfds, NULL, NULL, &tv);
-        if(res < 0) {
-            fprintf(stderr, "select failed, %s\n", strerror(errno));
-            return 1;
-        }
-        if(res == 0) {
-            if(light_event2.value == 1)
-                goto light2_off;
-            if(light_event.value == 1)
-                goto light_off;
-            if(user_activity_fd < 0) {
-                if(gotkey && verbose > 0)
-                    printf("Idle - sleep\n");
-                if(!gotkey && verbose > 1)
-                    printf("Reenter sleep\n");
-                goto sleep;
-            }
-            else {
-                tv.tv_sec = 60*60*24;
-                tv.tv_usec = 0;
-            }
-        }
-        if(res > 0) {
-            //if(FD_ISSET(0, &rfds)) {
-            //  printf("goto data on stdin quit\n");
-            //  return 0;
-            //}
-            if(FD_ISSET(notifyfd, &rfds)) {
-                write(acquire_partial_wake_lock_fd, wakelockstring, sizeof(wakelockstring) - 1);
-                if(handle_inotify_event(notifyfd) > 0) {
-                    write(acquire_full_wake_lock_fd, wakelockstring, sizeof(wakelockstring) - 1);
-                }
-                write(release_wake_lock_fd, wakelockstring, sizeof(wakelockstring) - 1);
-            }
-            if(FD_ISSET(eventfd, &rfds)) {
-                write(acquire_partial_wake_lock_fd, wakelockstring, sizeof(wakelockstring) - 1);
-                res = read(eventfd, &event, sizeof(event));
-                if(res < (int)sizeof(event)) {
-                    fprintf(stderr, "could not get event\n");
-                    write(release_wake_lock_fd, wakelockstring, sizeof(wakelockstring) - 1);
-                    return 1;
-                }
-                if(event.type == EV_PWR && event.code == KEY_SLEEP) {
-                    event_sleep = event.value;
-                }
-                if(event.type == EV_KEY || (event.type == EV_SW && event.code == SW_0 && event.value == 1)) {
-                    gotkey = 1;
-                    if(user_activity_fd >= 0) {
-                        char buf[32];
-                        int len;
-                        len = sprintf(buf, "%ld%06lu000", event.time.tv_sec, event.time.tv_usec);
-                        write(user_activity_fd, buf, len);
-                    }
-                    if(lcd_light_time | key_light_time) {
-                        tv.tv_sec = key_light_time;
-                        light_event.value = 1;
-                        write(eventfd, &light_event, sizeof(light_event));
-                        light_event2.value = 1;
-                        write(eventfd, &light_event2, sizeof(light_event2));
-                    }
-                    else {
-                        tv.tv_sec = idle_time;
-                    }
-                    tv.tv_usec = 0;
-                    if(verbose > 1)
-                        printf("got %s %s %d%s\n", event.type == EV_KEY ? "key" : "switch", event.value ? "down" : "up", event.code, event_sleep ? " from sleep" : "");
-                    if(event.code == KEY_POWER) {
-                        if(event.value == 0) {
-                            int tmp_got_power_key_down = got_power_key_down;
-                            got_power_key_down = 0;
-                            if(tmp_got_power_key_down) {
-                                // power key released
-                                if(verbose > 0)
-                                    printf("Power key released - sleep\n");
-                                write(release_wake_lock_fd, wakelockstring, sizeof(wakelockstring) - 1);
-                                goto sleep;
-                            }
-                        }
-                        else if(event_sleep == 0) {
-                            got_power_key_down = 1;
-                            power_key_down_time = event.time;
-                        }
-                    }
-                }
-                if(event.type == EV_SW && event.code == SW_0 && event.value == 0) {
-                    if(verbose > 0)
-                        printf("Flip closed - sleep\n");
-                    power_key_down_time = event.time;
-                    write(release_wake_lock_fd, wakelockstring, sizeof(wakelockstring) - 1);
-                    goto sleep;
-                }
-                write(release_wake_lock_fd, wakelockstring, sizeof(wakelockstring) - 1);
-            }
-        }
-        if(0) {
-light_off:
-            light_event.value = 0;
-            write(eventfd, &light_event, sizeof(light_event));
-            tv.tv_sec = idle_time - lcd_light_time;
-        }
-        if(0) {
-light2_off:
-            light_event2.value = 0;
-            write(eventfd, &light_event2, sizeof(light_event2));
-            tv.tv_sec = lcd_light_time - key_light_time;
-        }
-        if(0) {
-sleep:
-            if(light_event.value == 1) {
-                light_event.value = 0;
-                write(eventfd, &light_event, sizeof(light_event));
-                light_event2.value = 0;
-                write(eventfd, &light_event2, sizeof(light_event2));
-                tv.tv_sec = idle_time - lcd_light_time;
-            }
-            if(powerfd_is_sleep) {
-                char buf[32];
-                int len;
-                len = sprintf(buf, "%ld%06lu000", power_key_down_time.tv_sec, power_key_down_time.tv_usec);
-                write(powerfd, buf, len);
-            }
-            else
-                write(powerfd, suspendstring, sizeof(suspendstring) - 1);
-            gotkey = 0;
-            tv.tv_sec = 0;
-            tv.tv_usec = 500000;
-        }
-    }
-
-    return 0;
-}
diff --git a/toolbox/rm.c b/toolbox/rm.c
index bd66311..3a24bec 100644
--- a/toolbox/rm.c
+++ b/toolbox/rm.c
@@ -7,14 +7,17 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 
+#define OPT_RECURSIVE 1
+#define OPT_FORCE     2
+
 static int usage()
 {
-    fprintf(stderr,"rm [-rR] <target>\n");
+    fprintf(stderr,"Usage: rm [-rR] [-f] <target>\n");
     return -1;
 }
 
 /* return -1 on failure, with errno set to the first error */
-static int unlink_recursive(const char* name)
+static int unlink_recursive(const char* name, int flags)
 {
     struct stat st;
     DIR *dir;
@@ -23,7 +26,7 @@
 
     /* is it a file or directory? */
     if (lstat(name, &st) < 0)
-        return -1;
+        return ((flags & OPT_FORCE) && errno == ENOENT) ? 0 : -1;
 
     /* a file, so unlink it */
     if (!S_ISDIR(st.st_mode))
@@ -41,7 +44,7 @@
         if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
             continue;
         sprintf(dn, "%s/%s", name, de->d_name);
-        if (unlink_recursive(dn) < 0) {
+        if (unlink_recursive(dn, flags) < 0) {
             fail = 1;
             break;
         }
@@ -66,21 +69,45 @@
 int rm_main(int argc, char *argv[])
 {
     int ret;
-    int i = 1;
-    int recursive = 0;
+    int i, c;
+    int flags = 0;
 
     if (argc < 2)
         return usage();
 
-    /* check if recursive */
-    if (argc >=2 && (!strcmp(argv[1], "-r") || !strcmp(argv[1], "-R"))) {
-        recursive = 1;
-        i = 2;
+    /* check flags */
+    do {
+        c = getopt(argc, argv, "frR");
+        if (c == EOF)
+            break;
+        switch (c) {
+        case 'f':
+            flags |= OPT_FORCE;
+            break;
+        case 'r':
+        case 'R':
+            flags |= OPT_RECURSIVE;
+            break;
+        }
+    } while (1);
+
+    if (optind < 1 || optind >= argc) {
+        usage();
+        return -1;
     }
-    
+
     /* loop over the file/directory args */
-    for (; i < argc; i++) {
-        int ret = recursive ? unlink_recursive(argv[i]) : unlink(argv[i]);
+    for (i = optind; i < argc; i++) {
+
+        if (flags & OPT_RECURSIVE) {
+            ret = unlink_recursive(argv[i], flags);
+        } else {
+            ret = unlink(argv[i]);
+            if (errno == ENOENT && (flags & OPT_FORCE)) {
+                return 0;
+            }
+        }
+
         if (ret < 0) {
             fprintf(stderr, "rm failed for %s, %s\n", argv[i], strerror(errno));
             return -1;