merge in jb-mr1-release history after reset to jb-mr1-dev
diff --git a/adb/adb.c b/adb/adb.c
index 200fc3c..a13a7c9 100644
--- a/adb/adb.c
+++ b/adb/adb.c
@@ -35,6 +35,7 @@
 #include <private/android_filesystem_config.h>
 #include <linux/capability.h>
 #include <linux/prctl.h>
+#include <sys/mount.h>
 #else
 #include "usb_vendors.h"
 #endif
@@ -989,6 +990,26 @@
 }
 #endif /* !ADB_HOST */
 
+#if !ADB_HOST
+/* Give ourselves access to external storage, which is otherwise protected. */
+static void mount_external_storage(void) {
+    // Create private mount namespace for our process
+    if (unshare(CLONE_NEWNS) == -1) {
+        fatal_errno("Failed to unshare()");
+    }
+
+    // Mark rootfs as being a slave in our process so that changes
+    // from parent namespace flow into our process.
+    if (mount("rootfs", "/", NULL, (MS_SLAVE | MS_REC), NULL) == -1) {
+        fatal_errno("Failed to mount() rootfs as MS_SLAVE");
+    }
+
+    if (mount(EXTERNAL_STORAGE_SYSTEM, EXTERNAL_STORAGE_APP, "none", MS_BIND, NULL) == -1) {
+        fatal_errno("Failed to mount() from %s", EXTERNAL_STORAGE_SYSTEM);
+    }
+}
+#endif /* !ADB_HOST */
+
 int adb_main(int is_daemon, int server_port)
 {
 #if !ADB_HOST
@@ -1008,7 +1029,6 @@
 
     init_transport_registration();
 
-
 #if ADB_HOST
     HOST = 1;
     usb_vendors_init();
@@ -1022,6 +1042,8 @@
     }
 #else
 
+    mount_external_storage();
+
     /* don't listen on a port (default 5037) if running in secure mode */
     /* don't run as root if we are running in secure mode */
     if (should_drop_privileges()) {
diff --git a/fastboot/Android.mk b/fastboot/Android.mk
index e3261a7..905f759 100644
--- a/fastboot/Android.mk
+++ b/fastboot/Android.mk
@@ -48,7 +48,13 @@
   LOCAL_C_INCLUDES += development/host/windows/usb/api
 endif
 
-LOCAL_STATIC_LIBRARIES := $(EXTRA_STATIC_LIBS) libzipfile libunz libext4_utils libsparse libz
+LOCAL_STATIC_LIBRARIES := \
+    $(EXTRA_STATIC_LIBS) \
+    libzipfile \
+    libunz \
+    libext4_utils_host \
+    libsparse_host \
+    libz
 
 ifneq ($(HOST_OS),windows)
 ifeq ($(HAVE_SELINUX), true)
@@ -57,8 +63,11 @@
 endif # HOST_OS != windows
 
 include $(BUILD_HOST_EXECUTABLE)
+
+
 $(call dist-for-goals,dist_files,$(LOCAL_BUILT_MODULE))
 
+
 ifeq ($(HOST_OS),linux)
 include $(CLEAR_VARS)
 LOCAL_SRC_FILES := usbtest.c usb_linux.c
diff --git a/gpttool/Android.mk b/gpttool/Android.mk
index a9fffe9..b8f9844 100644
--- a/gpttool/Android.mk
+++ b/gpttool/Android.mk
@@ -7,7 +7,6 @@
 LOCAL_STATIC_LIBRARIES := libz
 
 LOCAL_MODULE := gpttool
-LOCAL_MODULE_TAGS := eng
 
 include $(BUILD_HOST_EXECUTABLE)
 
diff --git a/include/cutils/multiuser.h b/include/cutils/multiuser.h
index 22bb032..0bf403c 100644
--- a/include/cutils/multiuser.h
+++ b/include/cutils/multiuser.h
@@ -30,9 +30,9 @@
 typedef uid_t userid_t;
 typedef uid_t appid_t;
 
-extern userid_t getUserId(uid_t uid);
-extern appid_t getAppId(uid_t uid);
-extern uid_t getUid(userid_t userId, appid_t appId);
+extern userid_t multiuser_getUserId(uid_t uid);
+extern appid_t multiuser_getAppId(uid_t uid);
+extern uid_t multiuser_getUid(userid_t userId, appid_t appId);
 
 #ifdef __cplusplus
 }
diff --git a/include/private/android_filesystem_config.h b/include/private/android_filesystem_config.h
index 6521cbe..012c2ae 100644
--- a/include/private/android_filesystem_config.h
+++ b/include/private/android_filesystem_config.h
@@ -229,6 +229,9 @@
     { 00644, AID_ROOT,      AID_ROOT,       0 },
 };
 
+#define EXTERNAL_STORAGE_SYSTEM "/mnt/secure/sdcard0"
+#define EXTERNAL_STORAGE_APP "/storage/sdcard0"
+
 static inline void fs_config(const char *path, int dir,
                              unsigned *uid, unsigned *gid, unsigned *mode)
 {
diff --git a/libcutils/Android.mk b/libcutils/Android.mk
index 8684e8f..78bae8a 100644
--- a/libcutils/Android.mk
+++ b/libcutils/Android.mk
@@ -50,7 +50,7 @@
 	threads.c \
 	sched_policy.c \
 	iosched_policy.c \
-	str_parms.c
+	str_parms.c \
 
 commonHostSources := \
         ashmem-host.c
@@ -78,6 +78,7 @@
         mspace.c \
         selector.c \
         tztime.c \
+        multiuser.c \
         zygote.c
 
     commonHostSources += \
@@ -124,8 +125,7 @@
         mq.c \
         partition_utils.c \
         qtaguid.c \
-        uevent.c \
-        multiuser.c
+        uevent.c
 
 ifeq ($(TARGET_ARCH),arm)
 LOCAL_SRC_FILES += arch-arm/memset32.S
diff --git a/libcutils/multiuser.c b/libcutils/multiuser.c
index be9304d..3c86cee 100644
--- a/libcutils/multiuser.c
+++ b/libcutils/multiuser.c
@@ -14,19 +14,16 @@
  * limitations under the License.
  */
 
-#include <stdlib.h>
-#include <unistd.h>
-#include <fcntl.h>
 #include <cutils/multiuser.h>
 
-userid_t getUserId(uid_t uid) {
+userid_t multiuser_getUserId(uid_t uid) {
     return uid / MULTIUSER_APP_PER_USER_RANGE;
 }
 
-appid_t getAppId(uid_t uid) {
+appid_t multiuser_getAppId(uid_t uid) {
     return uid % MULTIUSER_APP_PER_USER_RANGE;
 }
 
-uid_t getUid(userid_t userId, appid_t appId) {
+uid_t multiuser_getUid(userid_t userId, appid_t appId) {
     return userId * MULTIUSER_APP_PER_USER_RANGE + (appId % MULTIUSER_APP_PER_USER_RANGE);
 }
diff --git a/libsparse/Android.mk b/libsparse/Android.mk
index 69b52c3..9025cc0 100644
--- a/libsparse/Android.mk
+++ b/libsparse/Android.mk
@@ -10,92 +10,90 @@
         sparse_err.c \
         sparse_read.c
 
-include $(CLEAR_VARS)
 
+include $(CLEAR_VARS)
 LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
 LOCAL_SRC_FILES := $(libsparse_src_files)
-LOCAL_MODULE := libsparse
-LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE := libsparse_host
 LOCAL_STATIC_LIBRARIES := libz
 LOCAL_C_INCLUDES += $(LOCAL_PATH)/include external/zlib
-
 include $(BUILD_HOST_STATIC_LIBRARY)
 
-include $(CLEAR_VARS)
 
+include $(CLEAR_VARS)
 LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
 LOCAL_SRC_FILES := $(libsparse_src_files)
 LOCAL_MODULE := libsparse
-LOCAL_MODULE_TAGS := optional
 LOCAL_C_INCLUDES += $(LOCAL_PATH)/include external/zlib
-LOCAL_SHARED_LIBRARIES := libz
-
+LOCAL_SHARED_LIBRARIES := \
+    libz
 include $(BUILD_SHARED_LIBRARY)
 
-include $(CLEAR_VARS)
 
+include $(CLEAR_VARS)
 LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
 LOCAL_SRC_FILES := $(libsparse_src_files)
-LOCAL_MODULE := libsparse
-LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE := libsparse_static
 LOCAL_C_INCLUDES += $(LOCAL_PATH)/include external/zlib
 LOCAL_STATIC_LIBRARIES := libz
-
 include $(BUILD_STATIC_LIBRARY)
 
-include $(CLEAR_VARS)
 
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := simg2img.c \
+	sparse_crc32.c
+LOCAL_MODULE := simg2img_host
+# Need a unique module name, but exe should still be called simg2img
+LOCAL_MODULE_STEM := simg2img
+LOCAL_STATIC_LIBRARIES := \
+    libsparse_host \
+    libz
+include $(BUILD_HOST_EXECUTABLE)
+
+
+include $(CLEAR_VARS)
 LOCAL_SRC_FILES := simg2img.c \
 	sparse_crc32.c
 LOCAL_MODULE := simg2img
-LOCAL_MODULE_TAGS := debug
-LOCAL_STATIC_LIBRARIES := libsparse libz
-
-include $(BUILD_HOST_EXECUTABLE)
-
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES := simg2img.c \
-	sparse_crc32.c
-LOCAL_MODULE := simg2img
-LOCAL_MODULE_TAGS := optional
-LOCAL_STATIC_LIBRARIES := libsparse libz
-
+LOCAL_STATIC_LIBRARIES := \
+    libsparse_static \
+    libz
 include $(BUILD_EXECUTABLE)
 
+
 include $(CLEAR_VARS)
-
 LOCAL_SRC_FILES := img2simg.c
-LOCAL_MODULE := img2simg
-LOCAL_MODULE_TAGS := debug
-LOCAL_STATIC_LIBRARIES := libsparse libz
-
+LOCAL_MODULE := img2simg_host
+# Need a unique module name, but exe should still be called simg2img
+LOCAL_MODULE_STEM := img2simg
+LOCAL_STATIC_LIBRARIES := \
+    libsparse_host \
+    libz
 include $(BUILD_HOST_EXECUTABLE)
 
-include $(CLEAR_VARS)
 
+include $(CLEAR_VARS)
 LOCAL_SRC_FILES := img2simg.c
 LOCAL_MODULE := img2simg
-LOCAL_MODULE_TAGS := optional
-LOCAL_STATIC_LIBRARIES := libsparse libz
-
+LOCAL_STATIC_LIBRARIES := \
+    libsparse_static \
+    libz
 include $(BUILD_EXECUTABLE)
 
-include $(CLEAR_VARS)
 
+include $(CLEAR_VARS)
 LOCAL_SRC_FILES := simg2simg.c
 LOCAL_MODULE := simg2simg
-LOCAL_MODULE_TAGS := debug
-LOCAL_STATIC_LIBRARIES := libsparse libz
-
+LOCAL_STATIC_LIBRARIES := \
+    libsparse_host \
+    libz
 include $(BUILD_HOST_EXECUTABLE)
 
-include $(CLEAR_VARS)
 
+include $(CLEAR_VARS)
 LOCAL_MODULE := simg_dump.py
-LOCAL_MODULE_TAGS := debug
 LOCAL_SRC_FILES := simg_dump.py
 LOCAL_MODULE_CLASS := EXECUTABLES
 LOCAL_IS_HOST_MODULE := true
-
 include $(BUILD_PREBUILT)
+
diff --git a/rootdir/init.rc b/rootdir/init.rc
index 4c20ec1..5a31a8e 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -128,6 +128,8 @@
 on post-fs
     # once everything is setup, no need to modify /
     mount rootfs rootfs / ro remount
+    # mount shared so changes propagate into child namespaces
+    mount rootfs rootfs / shared rec
 
     # We chown/chmod /cache again so because mount is run as root + defaults
     chown system cache /cache
diff --git a/rootdir/ueventd.rc b/rootdir/ueventd.rc
index c1fca00..3889298 100644
--- a/rootdir/ueventd.rc
+++ b/rootdir/ueventd.rc
@@ -27,6 +27,7 @@
 /dev/android_adb          0660   adb        adb
 /dev/android_adb_enable   0660   adb        adb
 /dev/ttyMSM0              0600   bluetooth  bluetooth
+/dev/uhid                 0660   system     bluetooth
 /dev/uinput               0660   system     bluetooth
 /dev/alarm                0664   system     radio
 /dev/tty0                 0660   root       system