Merge commit '635193ab36302e5f65e99c7df2f256cfd37068c7' into am-b7f1a67b-2024-4274-aa7d-e88e8cceb407

* commit '635193ab36302e5f65e99c7df2f256cfd37068c7':
  Require quotes when searching for blkid keys.

Bug: 80436257
Test: manual
Change-Id: I4cbae2d594edefa10e5e2c5236d17fe3d37818cd
diff --git a/Android.mk b/Android.mk
index cabf9b3..06d98eb 100644
--- a/Android.mk
+++ b/Android.mk
@@ -17,7 +17,7 @@
 	CheckBattery.cpp \
 	Ext4Crypt.cpp \
 	VoldUtil.c \
-	cryptfs.c \
+	cryptfs.cpp \
 	Disk.cpp \
 	VolumeBase.cpp \
 	PublicVolume.cpp \
@@ -33,13 +33,10 @@
 	secontext.cpp \
 
 common_c_includes := \
-	system/extras/ext4_utils \
 	system/extras/f2fs_utils \
 	external/scrypt/lib/crypto \
 	frameworks/native/include \
 	system/security/keystore \
-	hardware/libhardware/include/hardware \
-	system/security/softkeymaster/include/keymaster
 
 common_shared_libraries := \
 	libsysutils \
@@ -51,13 +48,16 @@
 	liblogwrap \
 	libext4_utils \
 	libf2fs_sparseblock \
+	libcrypto_utils \
 	libcrypto \
 	libselinux \
 	libutils \
 	libhardware \
-	libsoftkeymaster \
 	libbase \
-	libkeymaster_messages \
+	libhwbinder \
+	libhidlbase \
+	android.hardware.keymaster@3.0 \
+	libkeystore_binder
 
 common_static_libraries := \
 	libbootloader_message \
@@ -66,12 +66,22 @@
 	libfec_rs \
 	libsquashfs_utils \
 	libscrypt_static \
-	libmincrypt \
 	libbatteryservice \
+	libavb \
 
 vold_conlyflags := -std=c11
 vold_cflags := -Werror -Wall -Wno-missing-field-initializers -Wno-unused-variable -Wno-unused-parameter
 
+required_modules :=
+ifeq ($(TARGET_USERIMAGES_USE_EXT4), true)
+  ifeq ($(TARGET_USES_MKE2FS), true)
+    vold_cflags += -DTARGET_USES_MKE2FS
+    required_modules += mke2fs
+  else
+    required_modules += make_ext4fs
+  endif
+endif
+
 include $(CLEAR_VARS)
 
 LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
@@ -84,6 +94,7 @@
 LOCAL_MODULE_TAGS := eng tests
 LOCAL_CFLAGS := $(vold_cflags)
 LOCAL_CONLYFLAGS := $(vold_conlyflags)
+LOCAL_REQUIRED_MODULES := $(required_modules)
 
 include $(BUILD_STATIC_LIBRARY)
 
@@ -102,14 +113,9 @@
 LOCAL_CFLAGS := $(vold_cflags)
 LOCAL_CONLYFLAGS := $(vold_conlyflags)
 
-ifeq ($(TARGET_HW_DISK_ENCRYPTION),true)
-LOCAL_C_INCLUDES += $(TARGET_CRYPTFS_HW_PATH)
-common_shared_libraries += libcryptfs_hw
-LOCAL_CFLAGS += -DCONFIG_HW_DISK_ENCRYPTION
-endif
-
 LOCAL_SHARED_LIBRARIES := $(common_shared_libraries)
 LOCAL_STATIC_LIBRARIES := $(common_static_libraries)
+LOCAL_REQUIRED_MODULES := $(required_modules)
 
 include $(BUILD_EXECUTABLE)
 
@@ -137,3 +143,5 @@
 LOCAL_CONLYFLAGS := $(vold_conlyflags)
 
 include $(BUILD_EXECUTABLE)
+
+include $(LOCAL_PATH)/tests/Android.mk
diff --git a/BenchmarkGen.h b/BenchmarkGen.h
index 8a4f68b..cd95aae 100644
--- a/BenchmarkGen.h
+++ b/BenchmarkGen.h
@@ -211,7 +211,7 @@
 TEMP_FAILURE_RETRY(pread(t3433f17, buf, 30, 32521955));
 TEMP_FAILURE_RETRY(pread(t3433f17, buf, 45, 32521985));
 TEMP_FAILURE_RETRY(pread(t3433f17, buf, 6350, 32518144)); // mmap2
-int  t3450f18 = TEMP_FAILURE_RETRY(open("file11", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+int  t3450f18 = TEMP_FAILURE_RETRY(open("file11", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 int  t3450f22 = TEMP_FAILURE_RETRY(open("file12", O_RDONLY|O_LARGEFILE));
 TEMP_FAILURE_RETRY(read(t3450f22, buf, 1));
 close(t3450f22);
@@ -219,7 +219,7 @@
 close(t3450f22);
 t3450f22 = TEMP_FAILURE_RETRY(open("file14", O_RDONLY|O_LARGEFILE|O_DIRECTORY|O_CLOEXEC));
 close(t3450f22);
-t3450f22 = TEMP_FAILURE_RETRY(open("file15", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3450f22 = TEMP_FAILURE_RETRY(open("file15", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3450f22, buf, 1));
 TEMP_FAILURE_RETRY(fsync(t3450f22));
 close(t3450f22);
@@ -390,7 +390,7 @@
 TEMP_FAILURE_RETRY(pread(t3455f18, buf, 22960, 0)); // mmap2
 TEMP_FAILURE_RETRY(pread(t3455f18, buf, 4108, 20480)); // mmap2
 close(t3455f18);
-t3455f18 = TEMP_FAILURE_RETRY(open("file11", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3455f18 = TEMP_FAILURE_RETRY(open("file11", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 30, 3513185));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 46, 3513215));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 3262, 3510272)); // mmap2
@@ -404,7 +404,7 @@
 close(t3455f19);
 t3455f19 = TEMP_FAILURE_RETRY(open("file14", O_RDONLY|O_LARGEFILE|O_DIRECTORY|O_CLOEXEC));
 close(t3455f19);
-t3455f19 = TEMP_FAILURE_RETRY(open("file15", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3455f19 = TEMP_FAILURE_RETRY(open("file15", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3455f19, buf, 1));
 TEMP_FAILURE_RETRY(fsync(t3455f19));
 close(t3455f19);
@@ -415,7 +415,7 @@
 TEMP_FAILURE_RETRY(pread(t3455f18, buf, 13748, 0)); // mmap2
 TEMP_FAILURE_RETRY(pread(t3455f18, buf, 4196, 12288)); // mmap2
 close(t3455f18);
-int  t3483f20 = TEMP_FAILURE_RETRY(open("file17", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+int  t3483f20 = TEMP_FAILURE_RETRY(open("file17", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0600));
 TEMP_FAILURE_RETRY(pread(t3483f20, buf, 100, 0));
 int  t3483f21 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3483f21, buf, 1, 0));
@@ -467,7 +467,7 @@
 TEMP_FAILURE_RETRY(pread(t3483f20, buf, 4096, 49152));
 TEMP_FAILURE_RETRY(pread(t3483f20, buf, 4096, 139264));
 TEMP_FAILURE_RETRY(pread(t3483f20, buf, 4096, 172032));
-int  t3483f25 = TEMP_FAILURE_RETRY(open("file19", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+int  t3483f25 = TEMP_FAILURE_RETRY(open("file19", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0600));
 TEMP_FAILURE_RETRY(pread(t3483f25, buf, 100, 0));
 int  t3483f26 = TEMP_FAILURE_RETRY(open("file20", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3483f26, buf, 1, 0));
@@ -550,7 +550,7 @@
 int  t3499f30 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f30, buf, 1, 0));
 close(t3499f30);
-int  t3499f31 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+int  t3499f31 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f31, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f31, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f31, buf, 4096, 516));
@@ -574,7 +574,7 @@
 t3499f30 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f30, buf, 1, 0));
 close(t3499f30);
-t3499f30 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f30 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f30, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f30, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f30, buf, 4096, 516));
@@ -786,7 +786,7 @@
 t3499f31 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f31, buf, 1, 0));
 close(t3499f31);
-t3499f31 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f31 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f31, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f31, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f31, buf, 4096, 516));
@@ -813,7 +813,7 @@
 t3499f31 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f31, buf, 1, 0));
 close(t3499f31);
-t3499f31 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f31 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f31, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f31, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f31, buf, 4096, 516));
@@ -837,7 +837,7 @@
 TEMP_FAILURE_RETRY(pwrite(t3499f31, buf, 28, 0));
 TEMP_FAILURE_RETRY(fdatasync(t3499f31));
 close(t3499f31);
-int  t3492f31 = TEMP_FAILURE_RETRY(open("file30", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+int  t3492f31 = TEMP_FAILURE_RETRY(open("file30", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0600));
 TEMP_FAILURE_RETRY(pread(t3492f31, buf, 100, 0));
 int  t3492f32 = TEMP_FAILURE_RETRY(open("file31", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3492f32, buf, 1, 0));
@@ -883,7 +883,7 @@
 t3499f30 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f30, buf, 1, 0));
 close(t3499f30);
-t3499f30 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f30 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f30, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f30, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f30, buf, 4096, 516));
@@ -910,7 +910,7 @@
 t3499f30 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f30, buf, 1, 0));
 close(t3499f30);
-t3499f30 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f30 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f30, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f30, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f30, buf, 4096, 516));
@@ -937,7 +937,7 @@
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 30, 36389306));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 23, 36389336));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 1664, 36388864)); // mmap2
-t3499f30 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f30 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f30, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f30, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f30, buf, 4096, 516));
@@ -1049,7 +1049,7 @@
 int  t3499f41 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f41, buf, 1, 0));
 close(t3499f41);
-t3499f41 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f41 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f41, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f41, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f41, buf, 4096, 516));
@@ -1076,7 +1076,7 @@
 t3499f41 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f41, buf, 1, 0));
 close(t3499f41);
-t3499f41 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f41 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f41, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f41, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f41, buf, 4096, 516));
@@ -1111,7 +1111,7 @@
 close(t3492f40);
 TEMP_FAILURE_RETRY(pread(t3492f31, buf, 16, 24));
 TEMP_FAILURE_RETRY(pread(t3492f31, buf, 4096, 36864));
-t3499f40 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f40 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 4096, 516));
@@ -1138,7 +1138,7 @@
 t3499f40 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f40, buf, 1, 0));
 close(t3499f40);
-t3499f40 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f40 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 4096, 516));
@@ -1165,7 +1165,7 @@
 t3499f40 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f40, buf, 1, 0));
 close(t3499f40);
-t3499f40 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f40 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 4096, 516));
@@ -1210,7 +1210,7 @@
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 30, 35636928));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 60, 35636958));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 2062, 35635200)); // mmap2
-t3499f40 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f40 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 4096, 516));
@@ -1237,7 +1237,7 @@
 t3499f40 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f40, buf, 1, 0));
 close(t3499f40);
-t3499f40 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f40 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f40, buf, 4096, 516));
@@ -1279,7 +1279,7 @@
 int  t3499f33 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f33, buf, 1, 0));
 close(t3499f33);
-t3499f33 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f33 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f33, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f33, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f33, buf, 4096, 516));
@@ -1306,7 +1306,7 @@
 t3499f33 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f33, buf, 1, 0));
 close(t3499f33);
-t3499f33 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f33 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f33, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f33, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f33, buf, 4096, 516));
@@ -1474,7 +1474,7 @@
 int  t3499f50 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f50, buf, 1, 0));
 close(t3499f50);
-t3499f50 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f50 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f50, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f50, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f50, buf, 4096, 516));
@@ -1501,7 +1501,7 @@
 t3499f50 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f50, buf, 1, 0));
 close(t3499f50);
-t3499f50 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f50 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f50, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f50, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f50, buf, 4096, 516));
@@ -1796,7 +1796,7 @@
 int  t3499f55 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f55, buf, 1, 0));
 close(t3499f55);
-t3499f55 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f55 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f55, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f55, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f55, buf, 4096, 516));
@@ -1834,8 +1834,8 @@
 TEMP_FAILURE_RETRY(read(t3532f55, buf, 16384));
 TEMP_FAILURE_RETRY(read(t3532f55, buf, 16384));
 close(t3532f55);
-t3499f55 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
-int  t3505f56 = TEMP_FAILURE_RETRY(open("file59", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3499f55 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
+int  t3505f56 = TEMP_FAILURE_RETRY(open("file59", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(pwrite(t3499f55, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f55, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f55, buf, 4096, 516));
@@ -1863,7 +1863,7 @@
 int  t3499f58 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f58, buf, 1, 0));
 close(t3499f58);
-t3499f58 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f58 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f58, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f58, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f58, buf, 4096, 516));
@@ -1896,7 +1896,7 @@
 t3499f55 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f55, buf, 1, 0));
 close(t3499f55);
-t3499f55 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f55 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f55, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f55, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f55, buf, 4096, 516));
@@ -1923,7 +1923,7 @@
 t3499f55 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f55, buf, 1, 0));
 close(t3499f55);
-t3499f55 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f55 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f55, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f55, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f55, buf, 4096, 516));
@@ -2012,7 +2012,7 @@
 t3505f56 = TEMP_FAILURE_RETRY(open("file31", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3505f56, buf, 1, 0));
 close(t3505f56);
-t3505f56 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3505f56 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3505f56, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3505f56, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3505f56, buf, 4096, 516));
@@ -2035,7 +2035,7 @@
 TEMP_FAILURE_RETRY(pwrite(t3505f56, buf, 4, 21032));
 TEMP_FAILURE_RETRY(pwrite(t3505f56, buf, 4096, 21036));
 TEMP_FAILURE_RETRY(pwrite(t3505f56, buf, 4, 25132));
-t3499f62 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f62 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f62, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f62, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f62, buf, 4096, 516));
@@ -2133,7 +2133,7 @@
 int  t3499f66 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f66, buf, 1, 0));
 close(t3499f66);
-t3499f66 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f66 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f66, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f66, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f66, buf, 4096, 516));
@@ -2196,7 +2196,7 @@
 TEMP_FAILURE_RETRY(read(t3532f61, buf, 16384));
 close(t3532f61);
 t3533f61 = TEMP_FAILURE_RETRY(open("file68", O_RDONLY|O_LARGEFILE));
-t3499f62 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f62 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f62, buf, 512, 0));
 TEMP_FAILURE_RETRY(read(t3533f61, buf, 16384));
 TEMP_FAILURE_RETRY(pwrite(t3499f62, buf, 4, 512));
@@ -2235,7 +2235,7 @@
 int  t3499f80 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f80, buf, 1, 0));
 close(t3499f80);
-t3499f80 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f80 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f80, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f80, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f80, buf, 4096, 516));
@@ -2259,7 +2259,7 @@
 t3499f66 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f66, buf, 1, 0));
 close(t3499f66);
-t3499f66 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f66 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f66, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f66, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f66, buf, 4096, 516));
@@ -2403,12 +2403,12 @@
 close(t3519f67);
 t3519f67 = TEMP_FAILURE_RETRY(open("file127", O_RDONLY|O_LARGEFILE|O_DIRECTORY|O_CLOEXEC));
 close(t3519f67);
-t3519f70 = TEMP_FAILURE_RETRY(open("file128", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+t3519f70 = TEMP_FAILURE_RETRY(open("file128", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3519f70);
-int  t3526f70 = TEMP_FAILURE_RETRY(open("file129", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
-int  t3519f75 = TEMP_FAILURE_RETRY(open("file128", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+int  t3526f70 = TEMP_FAILURE_RETRY(open("file129", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
+int  t3519f75 = TEMP_FAILURE_RETRY(open("file128", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 close(t3526f70);
-t3526f70 = TEMP_FAILURE_RETRY(open("file129", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3526f70 = TEMP_FAILURE_RETRY(open("file129", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3519f75, buf, 2991));
 close(t3519f75);
 TEMP_FAILURE_RETRY(write(t3526f70, buf, 3974));
@@ -2419,7 +2419,7 @@
 TEMP_FAILURE_RETRY(pread(t3499f72, buf, 1, 0));
 close(t3499f72);
 TEMP_FAILURE_RETRY(read(t3519f67, buf, 16384));
-t3499f72 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f72 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f72, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f72, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f72, buf, 4096, 516));
@@ -2441,26 +2441,26 @@
 TEMP_FAILURE_RETRY(pwrite(t3499f72, buf, 28, 0));
 TEMP_FAILURE_RETRY(fdatasync(t3499f72));
 close(t3499f72);
-t3526f70 = TEMP_FAILURE_RETRY(open("file131", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+t3526f70 = TEMP_FAILURE_RETRY(open("file131", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3526f70);
-t3526f70 = TEMP_FAILURE_RETRY(open("file131", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3526f70 = TEMP_FAILURE_RETRY(open("file131", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3526f70, buf, 4622));
 close(t3526f70);
-int  t3526f72 = TEMP_FAILURE_RETRY(open("file132", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3526f72 = TEMP_FAILURE_RETRY(open("file132", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3526f72);
-t3526f72 = TEMP_FAILURE_RETRY(open("file132", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3526f72 = TEMP_FAILURE_RETRY(open("file132", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3526f72, buf, 16384));
 TEMP_FAILURE_RETRY(write(t3526f72, buf, 6849));
 close(t3526f72);
-t3526f70 = TEMP_FAILURE_RETRY(open("file133", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+t3526f70 = TEMP_FAILURE_RETRY(open("file133", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3526f70);
-int  t3526f75 = TEMP_FAILURE_RETRY(open("file133", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+int  t3526f75 = TEMP_FAILURE_RETRY(open("file133", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3526f75, buf, 13332));
 close(t3526f75);
 int  t3495f70 = TEMP_FAILURE_RETRY(open("file31", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3495f70, buf, 1, 0));
 close(t3495f70);
-t3495f70 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3495f70 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3495f70, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3495f70, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3495f70, buf, 4096, 516));
@@ -2481,9 +2481,9 @@
 TEMP_FAILURE_RETRY(pwrite(t3495f70, buf, 28, 0));
 TEMP_FAILURE_RETRY(fdatasync(t3495f70));
 close(t3495f70);
-int  t3526f93 = TEMP_FAILURE_RETRY(open("file134", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3526f93 = TEMP_FAILURE_RETRY(open("file134", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3526f93);
-int  t3526f88 = TEMP_FAILURE_RETRY(open("file134", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+int  t3526f88 = TEMP_FAILURE_RETRY(open("file134", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3526f88, buf, 15056));
 close(t3526f88);
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 30, 34433108));
@@ -2498,7 +2498,7 @@
 t3495f75 = TEMP_FAILURE_RETRY(open("file31", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3495f75, buf, 1, 0));
 close(t3495f75);
-t3495f75 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3495f75 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3495f75, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3495f75, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3495f75, buf, 4096, 516));
@@ -2522,7 +2522,7 @@
 int  t3499f74 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f74, buf, 1, 0));
 close(t3499f74);
-t3499f74 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f74 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f74, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f74, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f74, buf, 4096, 516));
@@ -2543,15 +2543,15 @@
 TEMP_FAILURE_RETRY(pwrite(t3499f74, buf, 28, 0));
 TEMP_FAILURE_RETRY(fdatasync(t3499f74));
 close(t3499f74);
-int  t3526f64 = TEMP_FAILURE_RETRY(open("file135", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3526f64 = TEMP_FAILURE_RETRY(open("file135", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3526f64);
-t3526f64 = TEMP_FAILURE_RETRY(open("file135", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3526f64 = TEMP_FAILURE_RETRY(open("file135", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3526f64, buf, 16384));
 TEMP_FAILURE_RETRY(write(t3526f64, buf, 4873));
 close(t3526f64);
-int  t3526f90 = TEMP_FAILURE_RETRY(open("file136", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3526f90 = TEMP_FAILURE_RETRY(open("file136", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3526f90);
-t3526f90 = TEMP_FAILURE_RETRY(open("file136", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3526f90 = TEMP_FAILURE_RETRY(open("file136", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3526f90, buf, 4199));
 close(t3526f90);
 int  t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
@@ -2560,7 +2560,7 @@
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 30, 35511105));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 28, 35511135));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 3217, 35508224)); // mmap2
-t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 30, 35511441));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 40, 35511471));
 TEMP_FAILURE_RETRY(pwrite(t3499f90, buf, 512, 0));
@@ -2596,7 +2596,7 @@
 t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f90, buf, 1, 0));
 close(t3499f90);
-t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f90, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f90, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f90, buf, 4096, 516));
@@ -2623,7 +2623,7 @@
 t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f90, buf, 1, 0));
 close(t3499f90);
-t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f90, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f90, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f90, buf, 4096, 516));
@@ -2650,11 +2650,11 @@
 int  t3499f84 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f84, buf, 1, 0));
 close(t3499f84);
-t3499f72 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f72 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f72, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f72, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f72, buf, 4096, 516));
-int  t3495f84 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+int  t3495f84 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f72, buf, 4, 4612));
 TEMP_FAILURE_RETRY(pwrite(t3499f72, buf, 4, 4616));
 TEMP_FAILURE_RETRY(pwrite(t3499f72, buf, 4096, 4620));
@@ -2701,7 +2701,7 @@
 int  t3495f66 = TEMP_FAILURE_RETRY(open("file31", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3495f66, buf, 1, 0));
 close(t3495f66);
-t3495f66 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3495f66 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3495f66, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3495f66, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3495f66, buf, 4096, 516));
@@ -2725,7 +2725,7 @@
 t3499f66 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f66, buf, 1, 0));
 close(t3499f66);
-t3499f66 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f66 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f66, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f66, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f66, buf, 4096, 516));
@@ -2753,7 +2753,7 @@
 int  t3505f66 = TEMP_FAILURE_RETRY(open("file31", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3505f66, buf, 1, 0));
 close(t3505f66);
-t3505f66 = TEMP_FAILURE_RETRY(open("file138", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3505f66 = TEMP_FAILURE_RETRY(open("file138", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 t3526f70 = TEMP_FAILURE_RETRY(open("file139", O_RDONLY|O_LARGEFILE));
 TEMP_FAILURE_RETRY(read(t3526f70, buf, 16384));
 TEMP_FAILURE_RETRY(read(t3526f70, buf, 16384));
@@ -2764,7 +2764,7 @@
 t3495f70 = TEMP_FAILURE_RETRY(open("file31", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3495f70, buf, 1, 0));
 close(t3495f70);
-t3495f70 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3495f70 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3495f70, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3495f70, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3495f70, buf, 4096, 516));
@@ -2786,19 +2786,19 @@
 TEMP_FAILURE_RETRY(fdatasync(t3495f70));
 close(t3495f70);
 TEMP_FAILURE_RETRY(write(t3505f66, buf, 10592));
-int  t3533f70 = TEMP_FAILURE_RETRY(open("file140", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3533f70 = TEMP_FAILURE_RETRY(open("file140", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3533f70);
-t3533f70 = TEMP_FAILURE_RETRY(open("file140", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3533f70 = TEMP_FAILURE_RETRY(open("file140", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3533f70, buf, 4042));
 close(t3533f70);
 TEMP_FAILURE_RETRY(write(t3505f66, buf, 9664));
 TEMP_FAILURE_RETRY(write(t3505f66, buf, 23656));
-t3533f70 = TEMP_FAILURE_RETRY(open("file141", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+t3533f70 = TEMP_FAILURE_RETRY(open("file141", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3533f70);
-t3526f70 = TEMP_FAILURE_RETRY(open("file142", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
-int  t3533f72 = TEMP_FAILURE_RETRY(open("file141", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3526f70 = TEMP_FAILURE_RETRY(open("file142", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
+int  t3533f72 = TEMP_FAILURE_RETRY(open("file141", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 close(t3526f70);
-t3526f70 = TEMP_FAILURE_RETRY(open("file142", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3526f70 = TEMP_FAILURE_RETRY(open("file142", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3526f70, buf, 5057));
 TEMP_FAILURE_RETRY(write(t3533f72, buf, 4595));
 close(t3526f70);
@@ -2808,7 +2808,7 @@
 t3499f70 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f70, buf, 1, 0));
 close(t3499f70);
-t3499f70 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f70 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f70, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f70, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f70, buf, 4096, 516));
@@ -2836,7 +2836,7 @@
 t3505f66 = TEMP_FAILURE_RETRY(open("file31", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3505f66, buf, 1, 0));
 close(t3505f66);
-t3505f66 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3505f66 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3505f66, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3505f66, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3505f66, buf, 4096, 516));
@@ -2872,7 +2872,7 @@
 t3505f66 = TEMP_FAILURE_RETRY(open("file31", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3505f66, buf, 1, 0));
 close(t3505f66);
-t3505f66 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3505f66 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3505f66, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3505f66, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3505f66, buf, 4096, 516));
@@ -2923,9 +2923,9 @@
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 30, 34261624));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 49, 34261654));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 2993, 34258944)); // mmap2
-int  t3533f91 = TEMP_FAILURE_RETRY(open("file143", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3533f91 = TEMP_FAILURE_RETRY(open("file143", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3533f91);
-t3533f91 = TEMP_FAILURE_RETRY(open("file143", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3533f91 = TEMP_FAILURE_RETRY(open("file143", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 30, 31210525));
 TEMP_FAILURE_RETRY(pread(t3455f17, buf, 44, 31210555));
 TEMP_FAILURE_RETRY(write(t3533f91, buf, 16384));
@@ -2935,7 +2935,7 @@
 int  t3499f91 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f91, buf, 1, 0));
 close(t3499f91);
-t3499f91 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f91 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f91, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f91, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f91, buf, 4096, 516));
@@ -2959,7 +2959,7 @@
 int  t3505f84 = TEMP_FAILURE_RETRY(open("file31", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3505f84, buf, 1, 0));
 close(t3505f84);
-t3505f84 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3505f84 = TEMP_FAILURE_RETRY(open("file31", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3505f84, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3505f84, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3505f84, buf, 4096, 516));
@@ -2980,41 +2980,41 @@
 TEMP_FAILURE_RETRY(pwrite(t3505f84, buf, 28, 0));
 TEMP_FAILURE_RETRY(fdatasync(t3505f84));
 close(t3505f84);
-int  t3533f102 = TEMP_FAILURE_RETRY(open("file144", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3533f102 = TEMP_FAILURE_RETRY(open("file144", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3533f102);
-t3533f72 = TEMP_FAILURE_RETRY(open("file144", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3533f72 = TEMP_FAILURE_RETRY(open("file144", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3533f72, buf, 5550));
 close(t3533f72);
-int  t3526f84 = TEMP_FAILURE_RETRY(open("file145", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3526f84 = TEMP_FAILURE_RETRY(open("file145", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3526f84);
-int  t3526f101 = TEMP_FAILURE_RETRY(open("file145", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+int  t3526f101 = TEMP_FAILURE_RETRY(open("file145", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3526f101, buf, 3612));
 close(t3526f101);
-t3526f90 = TEMP_FAILURE_RETRY(open("file146", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+t3526f90 = TEMP_FAILURE_RETRY(open("file146", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3526f90);
-t3526f90 = TEMP_FAILURE_RETRY(open("file146", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3526f90 = TEMP_FAILURE_RETRY(open("file146", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3526f90, buf, 5414));
-int  t3533f96 = TEMP_FAILURE_RETRY(open("file147", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3533f96 = TEMP_FAILURE_RETRY(open("file147", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3533f96);
-t3533f96 = TEMP_FAILURE_RETRY(open("file147", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3533f96 = TEMP_FAILURE_RETRY(open("file147", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 close(t3526f90);
 TEMP_FAILURE_RETRY(write(t3533f96, buf, 3834));
 close(t3533f96);
-int  t3519f90 = TEMP_FAILURE_RETRY(open("file148", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3519f90 = TEMP_FAILURE_RETRY(open("file148", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3519f90);
-t3519f90 = TEMP_FAILURE_RETRY(open("file148", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3519f90 = TEMP_FAILURE_RETRY(open("file148", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3519f90, buf, 3461));
-int  t3526f96 = TEMP_FAILURE_RETRY(open("file149", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3526f96 = TEMP_FAILURE_RETRY(open("file149", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3526f96);
 close(t3519f90);
-t3526f90 = TEMP_FAILURE_RETRY(open("file149", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3526f90 = TEMP_FAILURE_RETRY(open("file149", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(write(t3526f90, buf, 16384));
 TEMP_FAILURE_RETRY(write(t3526f90, buf, 12766));
 close(t3526f90);
-int  t3533f90 = TEMP_FAILURE_RETRY(open("file150", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3533f90 = TEMP_FAILURE_RETRY(open("file150", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3533f90);
-t3533f90 = TEMP_FAILURE_RETRY(open("file150", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
-int  t3505f96 = TEMP_FAILURE_RETRY(open("file151", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3533f90 = TEMP_FAILURE_RETRY(open("file150", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
+int  t3505f96 = TEMP_FAILURE_RETRY(open("file151", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0600));
 TEMP_FAILURE_RETRY(pread(t3505f96, buf, 100, 0));
 TEMP_FAILURE_RETRY(write(t3533f90, buf, 10056));
 close(t3533f90);
@@ -3036,11 +3036,11 @@
 close(t3505f90);
 TEMP_FAILURE_RETRY(pread(t3505f96, buf, 16, 24));
 t3505f90 = TEMP_FAILURE_RETRY(open("file152", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
-int  t3533f99 = TEMP_FAILURE_RETRY(open("file153", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3533f99 = TEMP_FAILURE_RETRY(open("file153", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600));
 close(t3533f99);
 TEMP_FAILURE_RETRY(pread(t3505f90, buf, 1, 0));
 close(t3505f90);
-t3533f90 = TEMP_FAILURE_RETRY(open("file153", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
+t3533f90 = TEMP_FAILURE_RETRY(open("file153", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
 TEMP_FAILURE_RETRY(pread(t3505f96, buf, 16, 24));
 TEMP_FAILURE_RETRY(write(t3533f90, buf, 13271));
 close(t3533f90);
@@ -3066,7 +3066,7 @@
 t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f90, buf, 1, 0));
 close(t3499f90);
-t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f90, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f90, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f90, buf, 4096, 516));
@@ -3090,7 +3090,7 @@
 t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f90, buf, 1, 0));
 close(t3499f90);
-t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f90 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f90, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f90, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f90, buf, 4096, 516));
@@ -3120,7 +3120,7 @@
 t3499f92 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f92, buf, 1, 0));
 close(t3499f92);
-t3499f92 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f92 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f92, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f92, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f92, buf, 4096, 516));
@@ -3147,7 +3147,7 @@
 t3499f92 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f92, buf, 1, 0));
 close(t3499f92);
-t3499f92 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f92 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f92, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f92, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f92, buf, 4096, 516));
@@ -3177,7 +3177,7 @@
 int  t3499f100 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f100, buf, 1, 0));
 close(t3499f100);
-t3499f100 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f100 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f100, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f100, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f100, buf, 4096, 516));
@@ -3201,7 +3201,7 @@
 t3499f100 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f100, buf, 1, 0));
 close(t3499f100);
-t3499f100 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f100 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f100, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f100, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f100, buf, 4096, 516));
@@ -3228,7 +3228,7 @@
 t3499f100 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f100, buf, 1, 0));
 close(t3499f100);
-t3499f100 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f100 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f100, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f100, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f100, buf, 4096, 516));
@@ -3252,7 +3252,7 @@
 t3499f100 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f100, buf, 1, 0));
 close(t3499f100);
-t3499f100 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f100 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f100, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f100, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f100, buf, 4096, 516));
@@ -3282,7 +3282,7 @@
 t3499f84 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f84, buf, 1, 0));
 close(t3499f84);
-int  t3499f27 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+int  t3499f27 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f27, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f27, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f27, buf, 4096, 516));
@@ -3309,7 +3309,7 @@
 t3499f27 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f27, buf, 1, 0));
 close(t3499f27);
-t3499f92 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f92 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f92, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f92, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f92, buf, 4096, 516));
@@ -3336,7 +3336,7 @@
 t3499f92 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f92, buf, 1, 0));
 close(t3499f92);
-t3499f92 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f92 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f92, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f92, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f92, buf, 4096, 516));
@@ -3360,7 +3360,7 @@
 TEMP_FAILURE_RETRY(pwrite(t3499f92, buf, 28, 0));
 TEMP_FAILURE_RETRY(fdatasync(t3499f92));
 close(t3499f92);
-int  t3545f92 = TEMP_FAILURE_RETRY(open("file154", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+int  t3545f92 = TEMP_FAILURE_RETRY(open("file154", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0600));
 TEMP_FAILURE_RETRY(pread(t3545f92, buf, 100, 0));
 int  t3545f97 = TEMP_FAILURE_RETRY(open("file155", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3545f97, buf, 1, 0));
@@ -3402,7 +3402,7 @@
 TEMP_FAILURE_RETRY(pread(t3545f97, buf, 1, 0));
 close(t3545f97);
 TEMP_FAILURE_RETRY(pread(t3545f92, buf, 16, 24));
-t3545f97 = TEMP_FAILURE_RETRY(open("file155", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3545f97 = TEMP_FAILURE_RETRY(open("file155", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3545f97, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3545f97, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3545f97, buf, 4096, 516));
@@ -3435,7 +3435,7 @@
 TEMP_FAILURE_RETRY(pread(t3545f29, buf, 1, 0));
 close(t3545f29);
 TEMP_FAILURE_RETRY(pread(t3545f92, buf, 16, 24));
-t3545f97 = TEMP_FAILURE_RETRY(open("file155", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3545f97 = TEMP_FAILURE_RETRY(open("file155", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3545f97, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3545f97, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3545f97, buf, 4096, 516));
@@ -3460,7 +3460,7 @@
 TEMP_FAILURE_RETRY(pwrite(t3545f97, buf, 28, 0));
 TEMP_FAILURE_RETRY(fdatasync(t3545f97));
 close(t3545f97);
-int  t3575f29 = TEMP_FAILURE_RETRY(open("file16", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE));
+int  t3575f29 = TEMP_FAILURE_RETRY(open("file16", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0));
 TEMP_FAILURE_RETRY(write(t3575f29, buf, 17344));
 close(t3575f29);
 t3545f97 = TEMP_FAILURE_RETRY(open("file155", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
@@ -3471,7 +3471,7 @@
 TEMP_FAILURE_RETRY(pread(t3545f29, buf, 1, 0));
 close(t3545f29);
 TEMP_FAILURE_RETRY(pread(t3545f92, buf, 16, 24));
-t3545f29 = TEMP_FAILURE_RETRY(open("file155", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3545f29 = TEMP_FAILURE_RETRY(open("file155", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3545f29, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3545f29, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3545f29, buf, 4096, 516));
@@ -3499,7 +3499,7 @@
 int  t3499f26 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f26, buf, 1, 0));
 close(t3499f26);
-t3499f26 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f26 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f26, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f26, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f26, buf, 4096, 516));
@@ -3523,7 +3523,7 @@
 t3499f26 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f26, buf, 1, 0));
 close(t3499f26);
-int  t3499f28 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+int  t3499f28 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f28, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f28, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f28, buf, 4096, 516));
@@ -3569,7 +3569,7 @@
 TEMP_FAILURE_RETRY(pread(t3545f29, buf, 1, 0));
 close(t3545f29);
 TEMP_FAILURE_RETRY(pread(t3545f92, buf, 16, 24));
-t3545f29 = TEMP_FAILURE_RETRY(open("file155", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3545f29 = TEMP_FAILURE_RETRY(open("file155", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3545f29, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3545f29, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3545f29, buf, 4096, 516));
@@ -3598,7 +3598,7 @@
 TEMP_FAILURE_RETRY(pread(t3545f84, buf, 1, 0));
 close(t3545f84);
 TEMP_FAILURE_RETRY(pread(t3545f92, buf, 16, 24));
-int  t3584f84 = TEMP_FAILURE_RETRY(open("file157", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+int  t3584f84 = TEMP_FAILURE_RETRY(open("file157", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0600));
 TEMP_FAILURE_RETRY(pread(t3584f84, buf, 100, 0));
 int  t3584f90 = TEMP_FAILURE_RETRY(open("file158", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3584f90, buf, 1, 0));
@@ -3608,7 +3608,7 @@
 TEMP_FAILURE_RETRY(pread(t3545f90, buf, 1, 0));
 close(t3545f90);
 TEMP_FAILURE_RETRY(pread(t3545f92, buf, 16, 24));
-t3545f90 = TEMP_FAILURE_RETRY(open("file155", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3545f90 = TEMP_FAILURE_RETRY(open("file155", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3545f90, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3545f90, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3545f90, buf, 4096, 516));
@@ -3657,7 +3657,7 @@
 TEMP_FAILURE_RETRY(pread(t3584f84, buf, 4096, 16384));
 TEMP_FAILURE_RETRY(pread(t3584f84, buf, 4096, 12288));
 TEMP_FAILURE_RETRY(pread(t3545f92, buf, 4096, 32768));
-t3545f99 = TEMP_FAILURE_RETRY(open("file155", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3545f99 = TEMP_FAILURE_RETRY(open("file155", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3545f99, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3545f99, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3545f99, buf, 4096, 516));
@@ -3785,8 +3785,8 @@
 t3499f28 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f28, buf, 1, 0));
 close(t3499f28);
-int  t3496f28 = TEMP_FAILURE_RETRY(open("file169", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE));
-int  t3499f103 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+int  t3496f28 = TEMP_FAILURE_RETRY(open("file169", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0600));
+int  t3499f103 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f103, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f103, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f103, buf, 4096, 516));
@@ -3814,7 +3814,7 @@
 TEMP_FAILURE_RETRY(pread(t3499f106, buf, 1, 0));
 close(t3499f106);
 close(t3496f28);
-t3499f28 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f28 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f28, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f28, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f28, buf, 4096, 516));
@@ -3835,7 +3835,7 @@
 TEMP_FAILURE_RETRY(pwrite(t3499f28, buf, 28, 0));
 TEMP_FAILURE_RETRY(fdatasync(t3499f28));
 close(t3499f28);
-int  t3541f103 = TEMP_FAILURE_RETRY(open("file170", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+int  t3541f103 = TEMP_FAILURE_RETRY(open("file170", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0600));
 TEMP_FAILURE_RETRY(pread(t3541f103, buf, 100, 0));
 int  t3541f105 = TEMP_FAILURE_RETRY(open("file171", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3541f105, buf, 1, 0));
@@ -3872,7 +3872,7 @@
 int  t3499f107 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f107, buf, 1, 0));
 close(t3499f107);
-t3499f107 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f107 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f107, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f107, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f107, buf, 4096, 516));
@@ -3896,7 +3896,7 @@
 t3499f107 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f107, buf, 1, 0));
 close(t3499f107);
-t3499f105 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+t3499f105 = TEMP_FAILURE_RETRY(open("file18", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0660));
 TEMP_FAILURE_RETRY(pwrite(t3499f105, buf, 512, 0));
 TEMP_FAILURE_RETRY(pwrite(t3499f105, buf, 4, 512));
 TEMP_FAILURE_RETRY(pwrite(t3499f105, buf, 4096, 516));
@@ -3920,7 +3920,7 @@
 int  t3505f106 = TEMP_FAILURE_RETRY(open("file171", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3505f106, buf, 1, 0));
 close(t3505f106);
-int  t3540f107 = TEMP_FAILURE_RETRY(open("file173", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+int  t3540f107 = TEMP_FAILURE_RETRY(open("file173", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0600));
 TEMP_FAILURE_RETRY(pread(t3540f107, buf, 100, 0));
 int  t3540f108 = TEMP_FAILURE_RETRY(open("file174", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3540f108, buf, 1, 0));
@@ -3968,7 +3968,7 @@
 t3499f108 = TEMP_FAILURE_RETRY(open("file18", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3499f108, buf, 1, 0));
 close(t3499f108);
-int  t3597f108 = TEMP_FAILURE_RETRY(open("file177", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC));
+int  t3597f108 = TEMP_FAILURE_RETRY(open("file177", O_RDWR|O_CREAT|O_LARGEFILE|O_CLOEXEC, 0600));
 TEMP_FAILURE_RETRY(pread(t3597f108, buf, 100, 0));
 int  t3597f109 = TEMP_FAILURE_RETRY(open("file178", O_RDONLY|O_LARGEFILE|O_CLOEXEC));
 TEMP_FAILURE_RETRY(pread(t3597f109, buf, 1, 0));
@@ -4035,7 +4035,7 @@
         LOG(ERROR) << "Failed to read random data";
         return -EIO;
     }
-    if ((out = TEMP_FAILURE_RETRY(open(name, O_WRONLY|O_CREAT|O_TRUNC))) < 0) {
+    if ((out = TEMP_FAILURE_RETRY(open(name, O_WRONLY|O_CREAT|O_TRUNC, 0644))) < 0) {
         PLOG(ERROR) << "Failed to open " << name;
         return -errno;
     }
@@ -4436,3 +4436,4 @@
 
 }  // namespace vold
 }  // namespace android
+
diff --git a/CommandListener.cpp b/CommandListener.cpp
index b548a91..c2b8310 100644
--- a/CommandListener.cpp
+++ b/CommandListener.cpp
@@ -36,6 +36,7 @@
 
 #include <android-base/logging.h>
 #include <android-base/stringprintf.h>
+#include <android-base/unique_fd.h>
 #include <cutils/fs.h>
 
 #include <sysutils/SocketClient.h>
@@ -54,6 +55,8 @@
 #define DUMP_ARGS 0
 #define DEBUG_APPFUSE 0
 
+using android::base::unique_fd;
+
 CommandListener::CommandListener() :
                  FrameworkListener("vold", true) {
     registerCmd(new DumpCmd());
@@ -120,7 +123,7 @@
         cli->sendMsg(ResponseCode::CommandOkay, "Devmapper dump failed", true);
     }
     cli->sendMsg(0, "Dumping mounted filesystems", false);
-    FILE *fp = fopen("/proc/mounts", "r");
+    FILE *fp = fopen("/proc/mounts", "re");
     if (fp) {
         char line[1024];
         while (fgets(line, sizeof(line), fp)) {
@@ -680,16 +683,16 @@
                    << " in namespace " << uid;
     }
 
-    const android::vold::ScopedDir dir(opendir("/proc"));
-    if (dir.get() == nullptr) {
+    unique_fd dir(open("/proc", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
+    if (dir.get() == -1) {
         PLOG(ERROR) << "Failed to open /proc";
         return -errno;
     }
 
     // Obtains process file descriptor.
     const std::string pid_str = android::base::StringPrintf("%d", pid);
-    const android::vold::ScopedFd pid_fd(
-            openat(dirfd(dir.get()), pid_str.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC));
+    const unique_fd pid_fd(
+            openat(dir.get(), pid_str.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC));
     if (pid_fd.get() == -1) {
         PLOG(ERROR) << "Failed to open /proc/" << pid;
         return -errno;
@@ -703,8 +706,9 @@
             PLOG(ERROR) << "Failed to stat /proc/" << pid;
             return -errno;
         }
-        if (sb.st_uid != uid) {
-            LOG(ERROR) << "Mismatch UID expected=" << uid << ", actual=" << sb.st_uid;
+        if (sb.st_uid != AID_SYSTEM) {
+            LOG(ERROR) << "Only system can mount appfuse. UID expected=" << AID_SYSTEM
+                    << ", actual=" << sb.st_uid;
             return -EPERM;
         }
     }
@@ -714,7 +718,7 @@
         char rootName[PATH_MAX];
         char pidName[PATH_MAX];
         const int root_result =
-                android::vold::SaneReadLinkAt(dirfd(dir.get()), "1/ns/mnt", rootName, PATH_MAX);
+                android::vold::SaneReadLinkAt(dir.get(), "1/ns/mnt", rootName, PATH_MAX);
         const int pid_result =
                 android::vold::SaneReadLinkAt(pid_fd.get(), "ns/mnt", pidName, PATH_MAX);
         if (root_result == -1) {
@@ -732,7 +736,7 @@
     }
 
     // We purposefully leave the namespace open across the fork
-    android::vold::ScopedFd ns_fd(openat(pid_fd.get(), "ns/mnt", O_RDONLY));
+    unique_fd ns_fd(openat(pid_fd.get(), "ns/mnt", O_RDONLY)); // not O_CLOEXEC
     if (ns_fd.get() < 0) {
         PLOG(ERROR) << "Failed to open namespace for /proc/" << pid << "/ns/mnt";
         return -errno;
@@ -748,7 +752,17 @@
         if (command == "mount") {
             _exit(mountInNamespace(uid, device_fd, path));
         } else if (command == "unmount") {
-            android::vold::ForceUnmount(path);
+            // If it's just after all FD opened on mount point are closed, umount2 can fail with
+            // EBUSY. To avoid the case, specify MNT_DETACH.
+            if (umount2(path.c_str(), UMOUNT_NOFOLLOW | MNT_DETACH) != 0 &&
+                    errno != EINVAL && errno != ENOENT) {
+                PLOG(ERROR) << "Failed to unmount directory.";
+                _exit(-errno);
+            }
+            if (rmdir(path.c_str()) != 0) {
+                PLOG(ERROR) << "Failed to remove the mount directory.";
+                _exit(-errno);
+            }
             _exit(android::OK);
         } else {
             LOG(ERROR) << "Unknown appfuse command " << command;
@@ -800,7 +814,7 @@
         }
 
         // Open device FD.
-        android::vold::ScopedFd device_fd(open("/dev/fuse", O_RDWR));
+        unique_fd device_fd(open("/dev/fuse", O_RDWR)); // not O_CLOEXEC
         if (device_fd.get() == -1) {
             PLOG(ERROR) << "Failed to open /dev/fuse";
             return sendGenericOkFail(cli, -errno);
diff --git a/CryptCommandListener.cpp b/CryptCommandListener.cpp
index 02c2701..094a474 100644
--- a/CryptCommandListener.cpp
+++ b/CryptCommandListener.cpp
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include <assert.h>
 #include <stdlib.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
@@ -413,6 +414,11 @@
         return sendGenericOkFailOnBool(cli,
                 e4crypt_destroy_user_storage(parseNull(argv[2]), atoi(argv[3]), atoi(argv[4])));
 
+    } else if (subcommand == "secdiscard") {
+        if (!check_argc(cli, subcommand, argc, 3, "<path>")) return 0;
+        return sendGenericOkFailOnBool(cli,
+                e4crypt_secdiscard(parseNull(argv[2])));
+
     } else {
         dumpArgs(argc, argv, -1);
         cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown cryptfs subcommand", false);
diff --git a/Disk.cpp b/Disk.cpp
index 1e1a63e..8e58be0 100644
--- a/Disk.cpp
+++ b/Disk.cpp
@@ -47,8 +47,10 @@
 static const char* kSgdiskPath = "/system/bin/sgdisk";
 static const char* kSgdiskToken = " \t\n";
 
+static const char* kSysfsLoopMaxMinors = "/sys/module/loop/parameters/max_part";
 static const char* kSysfsMmcMaxMinors = "/sys/module/mmcblk/parameters/perdev_minors";
 
+static const unsigned int kMajorBlockLoop = 7;
 static const unsigned int kMajorBlockScsiA = 8;
 static const unsigned int kMajorBlockScsiB = 65;
 static const unsigned int kMajorBlockScsiC = 66;
@@ -136,7 +138,7 @@
 }
 
 void Disk::listVolumes(VolumeBase::Type type, std::list<std::string>& list) {
-    for (auto vol : mVolumes) {
+    for (const auto& vol : mVolumes) {
         if (vol->getType() == type) {
             list.push_back(vol->getId());
         }
@@ -209,7 +211,7 @@
 }
 
 void Disk::destroyAllVolumes() {
-    for (auto vol : mVolumes) {
+    for (const auto& vol : mVolumes) {
         vol->destroy();
     }
     mVolumes.clear();
@@ -229,6 +231,10 @@
 
     unsigned int majorId = major(mDevice);
     switch (majorId) {
+    case kMajorBlockLoop: {
+        mLabel = "Virtual";
+        break;
+    }
     case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
     case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
     case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
@@ -305,7 +311,7 @@
 
     Table table = Table::kUnknown;
     bool foundParts = false;
-    for (auto line : output) {
+    for (const auto& line : output) {
         char* cline = (char*) line.c_str();
         char* token = strtok(cline, kSgdiskToken);
         if (token == nullptr) continue;
@@ -370,7 +376,7 @@
 }
 
 status_t Disk::unmountAll() {
-    for (auto vol : mVolumes) {
+    for (const auto& vol : mVolumes) {
         vol->unmount();
     }
     return OK;
@@ -534,6 +540,14 @@
     // Figure out maximum partition devices supported
     unsigned int majorId = major(mDevice);
     switch (majorId) {
+    case kMajorBlockLoop: {
+        std::string tmp;
+        if (!ReadFileToString(kSysfsLoopMaxMinors, &tmp)) {
+            LOG(ERROR) << "Failed to read max minors";
+            return -errno;
+        }
+        return atoi(tmp.c_str());
+    }
     case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
     case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
     case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
diff --git a/EmulatedVolume.h b/EmulatedVolume.h
index 09686c1..9b0c049 100644
--- a/EmulatedVolume.h
+++ b/EmulatedVolume.h
@@ -37,7 +37,7 @@
  */
 class EmulatedVolume : public VolumeBase {
 public:
-    EmulatedVolume(const std::string& rawPath);
+    explicit EmulatedVolume(const std::string& rawPath);
     EmulatedVolume(const std::string& rawPath, dev_t device, const std::string& fsUuid);
     virtual ~EmulatedVolume();
 
diff --git a/Ext4Crypt.cpp b/Ext4Crypt.cpp
index cb41295..b41e09c 100644
--- a/Ext4Crypt.cpp
+++ b/Ext4Crypt.cpp
@@ -40,13 +40,13 @@
 #include <private/android_filesystem_config.h>
 
 #include "cryptfs.h"
-#include "ext4_crypt.h"
-#include "key_control.h"
 
 #define EMULATED_USES_SELINUX 0
 #define MANAGE_MISC_DIRS 0
 
 #include <cutils/fs.h>
+#include <ext4_utils/ext4_crypt.h>
+#include <ext4_utils/key_control.h>
 
 #include <android-base/file.h>
 #include <android-base/logging.h>
@@ -60,6 +60,7 @@
 static constexpr int FLAG_STORAGE_CE = 1 << 1;
 
 namespace {
+
 const std::string device_key_dir = std::string() + DATA_MNT_POINT + e4crypt_unencrypted_folder;
 const std::string device_key_path = device_key_dir + "/key";
 const std::string device_key_temp = device_key_dir + "/temp";
@@ -75,8 +76,7 @@
 // Map user ids to key references
 std::map<userid_t, std::string> s_de_key_raw_refs;
 std::map<userid_t, std::string> s_ce_key_raw_refs;
-// TODO abolish this map. Keys should not be long-lived in user memory, only kernel memory.
-// See b/26948053
+// TODO abolish this map, per b/26948053
 std::map<userid_t, std::string> s_ce_keys;
 
 // ext4enc:TODO get this const from somewhere good
@@ -137,7 +137,7 @@
 static std::string keyname(const std::string& raw_ref) {
     std::ostringstream o;
     o << "ext4:";
-    for (auto i : raw_ref) {
+    for (unsigned char i : raw_ref) {
         o << std::hex << std::setw(2) << std::setfill('0') << (int)i;
     }
     return o.str();
@@ -374,9 +374,14 @@
 }
 
 static bool ensure_policy(const std::string& raw_ref, const std::string& path) {
+    const char *contents_mode;
+    const char *filenames_mode;
+
+    cryptfs_get_file_encryption_modes(&contents_mode, &filenames_mode);
+
     if (e4crypt_policy_ensure(path.c_str(),
                               raw_ref.data(), raw_ref.size(),
-                              cryptfs_get_file_encryption_mode()) != 0) {
+                              contents_mode, filenames_mode) != 0) {
         LOG(ERROR) << "Failed to set policy on: " << path;
         return false;
     }
@@ -435,9 +440,13 @@
         return true;
     }
 
+    const char *contents_mode;
+    const char *filenames_mode;
+    cryptfs_get_file_encryption_modes(&contents_mode, &filenames_mode);
+    std::string modestring = std::string(contents_mode) + ":" + filenames_mode;
+
     std::string mode_filename = std::string("/data") + e4crypt_key_mode;
-    std::string mode = cryptfs_get_file_encryption_mode();
-    if (!android::base::WriteStringToFile(mode, mode_filename)) {
+    if (!android::base::WriteStringToFile(modestring, mode_filename)) {
         PLOG(ERROR) << "Cannot save type";
         return false;
     }
@@ -517,15 +526,45 @@
     return true;
 }
 
+static bool evict_key(const std::string &raw_ref) {
+    auto ref = keyname(raw_ref);
+    key_serial_t device_keyring;
+    if (!e4crypt_keyring(&device_keyring)) return false;
+    auto key_serial = keyctl_search(device_keyring, "logon", ref.c_str(), 0);
+
+    // Unlink the key from the keyring.  Prefer unlinking to revoking or
+    // invalidating, since unlinking is actually no less secure currently, and
+    // it avoids bugs in certain kernel versions where the keyring key is
+    // referenced from places it shouldn't be.
+    if (keyctl_unlink(key_serial, device_keyring) != 0) {
+        PLOG(ERROR) << "Failed to unlink key with serial " << key_serial << " ref " << ref;
+        return false;
+    }
+    LOG(DEBUG) << "Unlinked key with serial " << key_serial << " ref " << ref;
+    return true;
+}
+
+static bool evict_ce_key(userid_t user_id) {
+    s_ce_keys.erase(user_id);
+    bool success = true;
+    std::string raw_ref;
+    // If we haven't loaded the CE key, no need to evict it.
+    if (lookup_key_ref(s_ce_key_raw_refs, user_id, &raw_ref)) {
+        success &= evict_key(raw_ref);
+    }
+    s_ce_key_raw_refs.erase(user_id);
+    return success;
+}
+
 bool e4crypt_destroy_user_key(userid_t user_id) {
     LOG(DEBUG) << "e4crypt_destroy_user_key(" << user_id << ")";
     if (!e4crypt_is_native()) {
         return true;
     }
     bool success = true;
-    s_ce_keys.erase(user_id);
     std::string raw_ref;
-    s_ce_key_raw_refs.erase(user_id);
+    success &= evict_ce_key(user_id);
+    success &= lookup_key_ref(s_de_key_raw_refs, user_id, &raw_ref) && evict_key(raw_ref);
     s_de_key_raw_refs.erase(user_id);
     auto it = s_ephemeral_users.find(user_id);
     if (it != s_ephemeral_users.end()) {
@@ -660,8 +699,9 @@
 
 // TODO: rename to 'evict' for consistency
 bool e4crypt_lock_user_key(userid_t user_id) {
+    LOG(DEBUG) << "e4crypt_lock_user_key " << user_id;
     if (e4crypt_is_native()) {
-        // TODO: remove from kernel keyring
+        return evict_ce_key(user_id);
     } else if (e4crypt_is_emulated()) {
         // When in emulation mode, we just use chmod
         if (!emulated_lock(android::vold::BuildDataSystemCePath(user_id)) ||
@@ -686,7 +726,6 @@
         auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
         auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
         auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
-        auto foreign_de_path = android::vold::BuildDataProfilesForeignDexDePath(user_id);
 
         // DE_n key
         auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
@@ -699,7 +738,6 @@
                 multiuser_get_uid(user_id, AID_EVERYBODY))) return false;
 #endif
         if (!prepare_dir(profiles_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return false;
-        if (!prepare_dir(foreign_de_path, 0773, AID_SYSTEM, AID_SYSTEM)) return false;
 
         if (!prepare_dir(system_de_path, 0770, AID_SYSTEM, AID_SYSTEM)) return false;
         if (!prepare_dir(misc_de_path, 01771, AID_SYSTEM, AID_MISC)) return false;
@@ -757,7 +795,6 @@
         auto system_legacy_path = android::vold::BuildDataSystemLegacyPath(user_id);
         auto misc_legacy_path = android::vold::BuildDataMiscLegacyPath(user_id);
         auto profiles_de_path = android::vold::BuildDataProfilesDePath(user_id);
-        auto foreign_de_path = android::vold::BuildDataProfilesForeignDexDePath(user_id);
 
         // DE_n key
         auto system_de_path = android::vold::BuildDataSystemDePath(user_id);
@@ -770,7 +807,6 @@
             res &= destroy_dir(misc_legacy_path);
 #endif
             res &= destroy_dir(profiles_de_path);
-            res &= destroy_dir(foreign_de_path);
             res &= destroy_dir(system_de_path);
             res &= destroy_dir(misc_de_path);
         }
@@ -794,3 +830,7 @@
 
     return res;
 }
+
+bool e4crypt_secdiscard(const char* path) {
+    return android::vold::runSecdiscardSingle(std::string(path));
+}
diff --git a/Ext4Crypt.h b/Ext4Crypt.h
index 2dcc197..e90167b 100644
--- a/Ext4Crypt.h
+++ b/Ext4Crypt.h
@@ -38,4 +38,5 @@
 bool e4crypt_prepare_user_storage(const char* volume_uuid, userid_t user_id, int serial, int flags);
 bool e4crypt_destroy_user_storage(const char* volume_uuid, userid_t user_id, int flags);
 
+bool e4crypt_secdiscard(const char* path);
 __END_DECLS
diff --git a/KeyStorage.cpp b/KeyStorage.cpp
index 986f403..a36ac6a 100644
--- a/KeyStorage.cpp
+++ b/KeyStorage.cpp
@@ -23,11 +23,14 @@
 #include <vector>
 
 #include <errno.h>
+#include <stdio.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <unistd.h>
 
+#include <openssl/err.h>
+#include <openssl/evp.h>
 #include <openssl/sha.h>
 
 #include <android-base/file.h>
@@ -37,7 +40,8 @@
 
 #include <hardware/hw_auth_token.h>
 
-#include <keymaster/authorization_set.h>
+#include <keystore/authorization_set.h>
+#include <keystore/keystore_hidl_support.h>
 
 extern "C" {
 
@@ -46,6 +50,7 @@
 
 namespace android {
 namespace vold {
+using namespace keystore;
 
 const KeyAuthentication kEmptyAuthentication{"", ""};
 
@@ -64,8 +69,11 @@
 static const char* kStretch_none = "none";
 static const char* kStretch_nopassword = "nopassword";
 static const std::string kStretchPrefix_scrypt = "scrypt ";
+static const char* kHashPrefix_secdiscardable = "Android secdiscardable SHA512";
+static const char* kHashPrefix_keygen = "Android key wrapping key generation SHA512";
 static const char* kFn_encrypted_key = "encrypted_key";
 static const char* kFn_keymaster_key_blob = "keymaster_key_blob";
+static const char* kFn_keymaster_key_blob_upgraded = "keymaster_key_blob_upgraded";
 static const char* kFn_salt = "salt";
 static const char* kFn_secdiscardable = "secdiscardable";
 static const char* kFn_stretching = "stretching";
@@ -80,17 +88,17 @@
     return true;
 }
 
-static std::string hashSecdiscardable(const std::string& secdiscardable) {
+static std::string hashWithPrefix(char const* prefix, const std::string& tohash) {
     SHA512_CTX c;
 
     SHA512_Init(&c);
     // Personalise the hashing by introducing a fixed prefix.
     // Hashing applications should use personalization except when there is a
     // specific reason not to; see section 4.11 of https://www.schneier.com/skein1.3.pdf
-    std::string secdiscardableHashingPrefix = "Android secdiscardable SHA512";
-    secdiscardableHashingPrefix.resize(SHA512_CBLOCK);
-    SHA512_Update(&c, secdiscardableHashingPrefix.data(), secdiscardableHashingPrefix.size());
-    SHA512_Update(&c, secdiscardable.data(), secdiscardable.size());
+    std::string hashingPrefix = prefix;
+    hashingPrefix.resize(SHA512_CBLOCK);
+    SHA512_Update(&c, hashingPrefix.data(), hashingPrefix.size());
+    SHA512_Update(&c, tohash.data(), tohash.size());
     std::string res(SHA512_DIGEST_LENGTH, '\0');
     SHA512_Final(reinterpret_cast<uint8_t*>(&res[0]), &c);
     return res;
@@ -98,15 +106,15 @@
 
 static bool generateKeymasterKey(Keymaster& keymaster, const KeyAuthentication& auth,
                                  const std::string& appId, std::string* key) {
-    auto paramBuilder = keymaster::AuthorizationSetBuilder()
+    auto paramBuilder = AuthorizationSetBuilder()
                             .AesEncryptionKey(AES_KEY_BYTES * 8)
-                            .Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_GCM)
-                            .Authorization(keymaster::TAG_MIN_MAC_LENGTH, GCM_MAC_BYTES * 8)
-                            .Authorization(keymaster::TAG_PADDING, KM_PAD_NONE);
-    addStringParam(&paramBuilder, keymaster::TAG_APPLICATION_ID, appId);
+                            .Authorization(TAG_BLOCK_MODE, BlockMode::GCM)
+                            .Authorization(TAG_MIN_MAC_LENGTH, GCM_MAC_BYTES * 8)
+                            .Authorization(TAG_PADDING, PaddingMode::NONE)
+                            .Authorization(TAG_APPLICATION_ID, blob2hidlVec(appId));
     if (auth.token.empty()) {
         LOG(DEBUG) << "Creating key that doesn't need auth token";
-        paramBuilder.Authorization(keymaster::TAG_NO_AUTH_REQUIRED);
+        paramBuilder.Authorization(TAG_NO_AUTH_REQUIRED);
     } else {
         LOG(DEBUG) << "Auth token required for key";
         if (auth.token.size() != sizeof(hw_auth_token_t)) {
@@ -115,65 +123,27 @@
             return false;
         }
         const hw_auth_token_t* at = reinterpret_cast<const hw_auth_token_t*>(auth.token.data());
-        paramBuilder.Authorization(keymaster::TAG_USER_SECURE_ID, at->user_id);
-        paramBuilder.Authorization(keymaster::TAG_USER_AUTH_TYPE, HW_AUTH_PASSWORD);
-        paramBuilder.Authorization(keymaster::TAG_AUTH_TIMEOUT, AUTH_TIMEOUT);
+        paramBuilder.Authorization(TAG_USER_SECURE_ID, at->user_id);
+        paramBuilder.Authorization(TAG_USER_AUTH_TYPE, HardwareAuthenticatorType::PASSWORD);
+        paramBuilder.Authorization(TAG_AUTH_TIMEOUT, AUTH_TIMEOUT);
     }
-    return keymaster.generateKey(paramBuilder.build(), key);
+    return keymaster.generateKey(paramBuilder, key);
 }
 
-static keymaster::AuthorizationSetBuilder beginParams(const KeyAuthentication& auth,
-                                                      const std::string& appId) {
-    auto paramBuilder = keymaster::AuthorizationSetBuilder()
-                            .Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_GCM)
-                            .Authorization(keymaster::TAG_MAC_LENGTH, GCM_MAC_BYTES * 8)
-                            .Authorization(keymaster::TAG_PADDING, KM_PAD_NONE);
-    addStringParam(&paramBuilder, keymaster::TAG_APPLICATION_ID, appId);
+static AuthorizationSet beginParams(const KeyAuthentication& auth,
+                                               const std::string& appId) {
+    auto paramBuilder = AuthorizationSetBuilder()
+                            .Authorization(TAG_BLOCK_MODE, BlockMode::GCM)
+                            .Authorization(TAG_MAC_LENGTH, GCM_MAC_BYTES * 8)
+                            .Authorization(TAG_PADDING, PaddingMode::NONE)
+                            .Authorization(TAG_APPLICATION_ID, blob2hidlVec(appId));
     if (!auth.token.empty()) {
         LOG(DEBUG) << "Supplying auth token to Keymaster";
-        addStringParam(&paramBuilder, keymaster::TAG_AUTH_TOKEN, auth.token);
+        paramBuilder.Authorization(TAG_AUTH_TOKEN, blob2hidlVec(auth.token));
     }
     return paramBuilder;
 }
 
-static bool encryptWithKeymasterKey(Keymaster& keymaster, const std::string& key,
-                                    const KeyAuthentication& auth, const std::string& appId,
-                                    const std::string& message, std::string* ciphertext) {
-    auto params = beginParams(auth, appId).build();
-    keymaster::AuthorizationSet outParams;
-    auto opHandle = keymaster.begin(KM_PURPOSE_ENCRYPT, key, params, &outParams);
-    if (!opHandle) return false;
-    keymaster_blob_t nonceBlob;
-    if (!outParams.GetTagValue(keymaster::TAG_NONCE, &nonceBlob)) {
-        LOG(ERROR) << "GCM encryption but no nonce generated";
-        return false;
-    }
-    // nonceBlob here is just a pointer into existing data, must not be freed
-    std::string nonce(reinterpret_cast<const char*>(nonceBlob.data), nonceBlob.data_length);
-    if (!checkSize("nonce", nonce.size(), GCM_NONCE_BYTES)) return false;
-    std::string body;
-    if (!opHandle.updateCompletely(message, &body)) return false;
-
-    std::string mac;
-    if (!opHandle.finishWithOutput(&mac)) return false;
-    if (!checkSize("mac", mac.size(), GCM_MAC_BYTES)) return false;
-    *ciphertext = nonce + body + mac;
-    return true;
-}
-
-static bool decryptWithKeymasterKey(Keymaster& keymaster, const std::string& key,
-                                    const KeyAuthentication& auth, const std::string& appId,
-                                    const std::string& ciphertext, std::string* message) {
-    auto nonce = ciphertext.substr(0, GCM_NONCE_BYTES);
-    auto bodyAndMac = ciphertext.substr(GCM_NONCE_BYTES);
-    auto params = addStringParam(beginParams(auth, appId), keymaster::TAG_NONCE, nonce).build();
-    auto opHandle = keymaster.begin(KM_PURPOSE_DECRYPT, key, params);
-    if (!opHandle) return false;
-    if (!opHandle.updateCompletely(bodyAndMac, message)) return false;
-    if (!opHandle.finish()) return false;
-    return true;
-}
-
 static bool readFileToString(const std::string& filename, std::string* result) {
     if (!android::base::ReadFileToString(filename, result)) {
         PLOG(ERROR) << "Failed to read from " << filename;
@@ -190,11 +160,89 @@
     return true;
 }
 
-static std::string getStretching() {
-    char paramstr[PROPERTY_VALUE_MAX];
+static KeymasterOperation begin(Keymaster& keymaster, const std::string& dir,
+                                KeyPurpose purpose,
+                                const AuthorizationSet &keyParams,
+                                const AuthorizationSet &opParams,
+                                AuthorizationSet* outParams) {
+    auto kmKeyPath = dir + "/" + kFn_keymaster_key_blob;
+    std::string kmKey;
+    if (!readFileToString(kmKeyPath, &kmKey)) return KeymasterOperation();
+    AuthorizationSet inParams(keyParams);
+    inParams.append(opParams.begin(), opParams.end());
+    for (;;) {
+        auto opHandle = keymaster.begin(purpose, kmKey, inParams, outParams);
+        if (opHandle) {
+            return opHandle;
+        }
+        if (opHandle.errorCode() != ErrorCode::KEY_REQUIRES_UPGRADE) return opHandle;
+        LOG(DEBUG) << "Upgrading key: " << dir;
+        std::string newKey;
+        if (!keymaster.upgradeKey(kmKey, keyParams, &newKey)) return KeymasterOperation();
+        auto newKeyPath = dir + "/" + kFn_keymaster_key_blob_upgraded;
+        if (!writeStringToFile(newKey, newKeyPath)) return KeymasterOperation();
+        if (rename(newKeyPath.c_str(), kmKeyPath.c_str()) != 0) {
+            PLOG(ERROR) << "Unable to move upgraded key to location: " << kmKeyPath;
+            return KeymasterOperation();
+        }
+        if (!keymaster.deleteKey(kmKey)) {
+            LOG(ERROR) << "Key deletion failed during upgrade, continuing anyway: " << dir;
+        }
+        kmKey = newKey;
+        LOG(INFO) << "Key upgraded: " << dir;
+    }
+}
 
-    property_get(SCRYPT_PROP, paramstr, SCRYPT_DEFAULTS);
-    return std::string() + kStretchPrefix_scrypt + paramstr;
+static bool encryptWithKeymasterKey(Keymaster& keymaster, const std::string& dir,
+                                    const AuthorizationSet &keyParams,
+                                    const std::string& message, std::string* ciphertext) {
+    AuthorizationSet opParams;
+    AuthorizationSet outParams;
+    auto opHandle = begin(keymaster, dir, KeyPurpose::ENCRYPT, keyParams, opParams, &outParams);
+    if (!opHandle) return false;
+    auto nonceBlob = outParams.GetTagValue(TAG_NONCE);
+    if (!nonceBlob.isOk()) {
+        LOG(ERROR) << "GCM encryption but no nonce generated";
+        return false;
+    }
+    // nonceBlob here is just a pointer into existing data, must not be freed
+    std::string nonce(reinterpret_cast<const char*>(&nonceBlob.value()[0]), nonceBlob.value().size());
+    if (!checkSize("nonce", nonce.size(), GCM_NONCE_BYTES)) return false;
+    std::string body;
+    if (!opHandle.updateCompletely(message, &body)) return false;
+
+    std::string mac;
+    if (!opHandle.finish(&mac)) return false;
+    if (!checkSize("mac", mac.size(), GCM_MAC_BYTES)) return false;
+    *ciphertext = nonce + body + mac;
+    return true;
+}
+
+static bool decryptWithKeymasterKey(Keymaster& keymaster, const std::string& dir,
+                                    const AuthorizationSet &keyParams,
+                                    const std::string& ciphertext, std::string* message) {
+    auto nonce = ciphertext.substr(0, GCM_NONCE_BYTES);
+    auto bodyAndMac = ciphertext.substr(GCM_NONCE_BYTES);
+    auto opParams = AuthorizationSetBuilder()
+            .Authorization(TAG_NONCE, blob2hidlVec(nonce));
+    auto opHandle = begin(keymaster, dir, KeyPurpose::DECRYPT, keyParams, opParams, nullptr);
+    if (!opHandle) return false;
+    if (!opHandle.updateCompletely(bodyAndMac, message)) return false;
+    if (!opHandle.finish(nullptr)) return false;
+    return true;
+}
+
+static std::string getStretching(const KeyAuthentication& auth) {
+    if (!auth.usesKeymaster()) {
+        return kStretch_none;
+    } else if (auth.secret.empty()) {
+        return kStretch_nopassword;
+    } else {
+        char paramstr[PROPERTY_VALUE_MAX];
+
+        property_get(SCRYPT_PROP, paramstr, SCRYPT_DEFAULTS);
+        return std::string() + kStretchPrefix_scrypt + paramstr;
+    }
 }
 
 static bool stretchingNeedsSalt(const std::string& stretching) {
@@ -239,7 +287,116 @@
                           std::string* appId) {
     std::string stretched;
     if (!stretchSecret(stretching, auth.secret, salt, &stretched)) return false;
-    *appId = hashSecdiscardable(secdiscardable) + stretched;
+    *appId = hashWithPrefix(kHashPrefix_secdiscardable, secdiscardable) + stretched;
+    return true;
+}
+
+static bool readRandomBytesOrLog(size_t count, std::string* out) {
+    auto status = ReadRandomBytes(count, *out);
+    if (status != OK) {
+        LOG(ERROR) << "Random read failed with status: " << status;
+        return false;
+    }
+    return true;
+}
+
+static void logOpensslError() {
+    LOG(ERROR) << "Openssl error: " << ERR_get_error();
+}
+
+static bool encryptWithoutKeymaster(const std::string& preKey,
+                                    const std::string& plaintext, std::string* ciphertext) {
+    auto key = hashWithPrefix(kHashPrefix_keygen, preKey);
+    key.resize(AES_KEY_BYTES);
+    if (!readRandomBytesOrLog(GCM_NONCE_BYTES, ciphertext)) return false;
+    auto ctx = std::unique_ptr<EVP_CIPHER_CTX, decltype(&::EVP_CIPHER_CTX_free)>(
+        EVP_CIPHER_CTX_new(), EVP_CIPHER_CTX_free);
+    if (!ctx) {
+        logOpensslError();
+        return false;
+    }
+    if (1 != EVP_EncryptInit_ex(ctx.get(), EVP_aes_256_gcm(), NULL,
+            reinterpret_cast<const uint8_t*>(key.data()),
+            reinterpret_cast<const uint8_t*>(ciphertext->data()))) {
+        logOpensslError();
+        return false;
+    }
+    ciphertext->resize(GCM_NONCE_BYTES + plaintext.size() + GCM_MAC_BYTES);
+    int outlen;
+    if (1 != EVP_EncryptUpdate(ctx.get(),
+        reinterpret_cast<uint8_t*>(&(*ciphertext)[0] + GCM_NONCE_BYTES), &outlen,
+        reinterpret_cast<const uint8_t*>(plaintext.data()), plaintext.size())) {
+        logOpensslError();
+        return false;
+    }
+    if (outlen != static_cast<int>(plaintext.size())) {
+        LOG(ERROR) << "GCM ciphertext length should be " << plaintext.size() << " was " << outlen;
+        return false;
+    }
+    if (1 != EVP_EncryptFinal_ex(ctx.get(),
+        reinterpret_cast<uint8_t*>(&(*ciphertext)[0] + GCM_NONCE_BYTES + plaintext.size()), &outlen)) {
+        logOpensslError();
+        return false;
+    }
+    if (outlen != 0) {
+        LOG(ERROR) << "GCM EncryptFinal should be 0, was " << outlen;
+        return false;
+    }
+    if (1 != EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_GET_TAG, GCM_MAC_BYTES,
+        reinterpret_cast<uint8_t*>(&(*ciphertext)[0] + GCM_NONCE_BYTES + plaintext.size()))) {
+        logOpensslError();
+        return false;
+    }
+    return true;
+}
+
+static bool decryptWithoutKeymaster(const std::string& preKey,
+                                    const std::string& ciphertext, std::string* plaintext) {
+    if (ciphertext.size() < GCM_NONCE_BYTES + GCM_MAC_BYTES) {
+        LOG(ERROR) << "GCM ciphertext too small: " << ciphertext.size();
+        return false;
+    }
+    auto key = hashWithPrefix(kHashPrefix_keygen, preKey);
+    key.resize(AES_KEY_BYTES);
+    auto ctx = std::unique_ptr<EVP_CIPHER_CTX, decltype(&::EVP_CIPHER_CTX_free)>(
+        EVP_CIPHER_CTX_new(), EVP_CIPHER_CTX_free);
+    if (!ctx) {
+        logOpensslError();
+        return false;
+    }
+    if (1 != EVP_DecryptInit_ex(ctx.get(), EVP_aes_256_gcm(), NULL,
+            reinterpret_cast<const uint8_t*>(key.data()),
+            reinterpret_cast<const uint8_t*>(ciphertext.data()))) {
+        logOpensslError();
+        return false;
+    }
+    plaintext->resize(ciphertext.size() - GCM_NONCE_BYTES - GCM_MAC_BYTES);
+    int outlen;
+    if (1 != EVP_DecryptUpdate(ctx.get(),
+        reinterpret_cast<uint8_t*>(&(*plaintext)[0]), &outlen,
+        reinterpret_cast<const uint8_t*>(ciphertext.data() + GCM_NONCE_BYTES), plaintext->size())) {
+        logOpensslError();
+        return false;
+    }
+    if (outlen != static_cast<int>(plaintext->size())) {
+        LOG(ERROR) << "GCM plaintext length should be " << plaintext->size() << " was " << outlen;
+        return false;
+    }
+    if (1 != EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_SET_TAG, GCM_MAC_BYTES,
+        const_cast<void *>(
+            reinterpret_cast<const void*>(ciphertext.data() + GCM_NONCE_BYTES + plaintext->size())))) {
+        logOpensslError();
+        return false;
+    }
+    if (1 != EVP_DecryptFinal_ex(ctx.get(),
+        reinterpret_cast<uint8_t*>(&(*plaintext)[0] + plaintext->size()), &outlen)) {
+        logOpensslError();
+        return false;
+    }
+    if (outlen != 0) {
+        LOG(ERROR) << "GCM EncryptFinal should be 0, was " << outlen;
+        return false;
+    }
     return true;
 }
 
@@ -250,13 +407,9 @@
     }
     if (!writeStringToFile(kCurrentVersion, dir + "/" + kFn_version)) return false;
     std::string secdiscardable;
-    if (ReadRandomBytes(SECDISCARDABLE_BYTES, secdiscardable) != OK) {
-        // TODO status_t plays badly with PLOG, fix it.
-        LOG(ERROR) << "Random read failed";
-        return false;
-    }
+    if (!readRandomBytesOrLog(SECDISCARDABLE_BYTES, &secdiscardable)) return false;
     if (!writeStringToFile(secdiscardable, dir + "/" + kFn_secdiscardable)) return false;
-    std::string stretching = auth.secret.empty() ? kStretch_nopassword : getStretching();
+    std::string stretching = getStretching(auth);
     if (!writeStringToFile(stretching, dir + "/" + kFn_stretching)) return false;
     std::string salt;
     if (stretchingNeedsSalt(stretching)) {
@@ -268,13 +421,18 @@
     }
     std::string appId;
     if (!generateAppId(auth, stretching, salt, secdiscardable, &appId)) return false;
-    Keymaster keymaster;
-    if (!keymaster) return false;
-    std::string kmKey;
-    if (!generateKeymasterKey(keymaster, auth, appId, &kmKey)) return false;
-    if (!writeStringToFile(kmKey, dir + "/" + kFn_keymaster_key_blob)) return false;
     std::string encryptedKey;
-    if (!encryptWithKeymasterKey(keymaster, kmKey, auth, appId, key, &encryptedKey)) return false;
+    if (auth.usesKeymaster()) {
+        Keymaster keymaster;
+        if (!keymaster) return false;
+        std::string kmKey;
+        if (!generateKeymasterKey(keymaster, auth, appId, &kmKey)) return false;
+        if (!writeStringToFile(kmKey, dir + "/" + kFn_keymaster_key_blob)) return false;
+        auto keyParams = beginParams(auth, appId);
+        if (!encryptWithKeymasterKey(keymaster, dir, keyParams, key, &encryptedKey)) return false;
+    } else {
+        if (!encryptWithoutKeymaster(appId, key, &encryptedKey)) return false;
+    }
     if (!writeStringToFile(encryptedKey, dir + "/" + kFn_encrypted_key)) return false;
     return true;
 }
@@ -296,13 +454,17 @@
     }
     std::string appId;
     if (!generateAppId(auth, stretching, salt, secdiscardable, &appId)) return false;
-    std::string kmKey;
-    if (!readFileToString(dir + "/" + kFn_keymaster_key_blob, &kmKey)) return false;
     std::string encryptedMessage;
     if (!readFileToString(dir + "/" + kFn_encrypted_key, &encryptedMessage)) return false;
-    Keymaster keymaster;
-    if (!keymaster) return false;
-    return decryptWithKeymasterKey(keymaster, kmKey, auth, appId, encryptedMessage, key);
+    if (auth.usesKeymaster()) {
+        Keymaster keymaster;
+        if (!keymaster) return false;
+        auto keyParams = beginParams(auth, appId);
+        if (!decryptWithKeymasterKey(keymaster, dir, keyParams, encryptedMessage, key)) return false;
+    } else {
+        if (!decryptWithoutKeymaster(appId, encryptedMessage, key)) return false;
+    }
+    return true;
 }
 
 static bool deleteKey(const std::string& dir) {
@@ -327,6 +489,16 @@
     return true;
 }
 
+bool runSecdiscardSingle(const std::string& file) {
+    if (ForkExecvp(
+            std::vector<std::string>{kSecdiscardPath, "--",
+                file}) != 0) {
+        LOG(ERROR) << "secdiscard failed";
+        return false;
+    }
+    return true;
+}
+
 static bool recursiveDeleteKey(const std::string& dir) {
     if (ForkExecvp(std::vector<std::string>{kRmPath, "-rf", dir}) != 0) {
         LOG(ERROR) << "recursive delete failed";
diff --git a/KeyStorage.h b/KeyStorage.h
index 10ed789..bce6a99 100644
--- a/KeyStorage.h
+++ b/KeyStorage.h
@@ -24,11 +24,15 @@
 
 // Represents the information needed to decrypt a disk encryption key.
 // If "token" is nonempty, it is passed in as a required Gatekeeper auth token.
-// If "secret" is nonempty, it is appended to the application-specific
+// If "token" and "secret" are nonempty, "secret" is appended to the application-specific
 // binary needed to unlock.
+// If only "secret" is nonempty, it is used to decrypt in a non-Keymaster process.
 class KeyAuthentication {
   public:
     KeyAuthentication(std::string t, std::string s) : token{t}, secret{s} {};
+
+    bool usesKeymaster() const { return !token.empty() || secret.empty(); };
+
     const std::string token;
     const std::string secret;
 };
@@ -47,6 +51,7 @@
 // Securely destroy the key stored in the named directory and delete the directory.
 bool destroyKey(const std::string& dir);
 
+bool runSecdiscardSingle(const std::string& file);
 }  // namespace vold
 }  // namespace android
 
diff --git a/Keymaster.cpp b/Keymaster.cpp
index d271b6a..ffa3a7a 100644
--- a/Keymaster.cpp
+++ b/Keymaster.cpp
@@ -17,121 +17,50 @@
 #include "Keymaster.h"
 
 #include <android-base/logging.h>
-#include <hardware/hardware.h>
-#include <hardware/keymaster1.h>
-#include <hardware/keymaster2.h>
+#include <keystore/keymaster_tags.h>
+#include <keystore/authorization_set.h>
+#include <keystore/keystore_hidl_support.h>
+
+using namespace ::keystore;
+using android::hardware::hidl_string;
 
 namespace android {
 namespace vold {
 
-class IKeymasterDevice {
-  public:
-    IKeymasterDevice() {}
-    virtual ~IKeymasterDevice() {}
-    virtual keymaster_error_t generate_key(const keymaster_key_param_set_t* params,
-                                           keymaster_key_blob_t* key_blob) const = 0;
-    virtual keymaster_error_t delete_key(const keymaster_key_blob_t* key) const = 0;
-    virtual keymaster_error_t begin(keymaster_purpose_t purpose, const keymaster_key_blob_t* key,
-                                    const keymaster_key_param_set_t* in_params,
-                                    keymaster_key_param_set_t* out_params,
-                                    keymaster_operation_handle_t* operation_handle) const = 0;
-    virtual keymaster_error_t update(keymaster_operation_handle_t operation_handle,
-                                     const keymaster_key_param_set_t* in_params,
-                                     const keymaster_blob_t* input, size_t* input_consumed,
-                                     keymaster_key_param_set_t* out_params,
-                                     keymaster_blob_t* output) const = 0;
-    virtual keymaster_error_t finish(keymaster_operation_handle_t operation_handle,
-                                     const keymaster_key_param_set_t* in_params,
-                                     const keymaster_blob_t* signature,
-                                     keymaster_key_param_set_t* out_params,
-                                     keymaster_blob_t* output) const = 0;
-    virtual keymaster_error_t abort(keymaster_operation_handle_t operation_handle) const = 0;
-
-  protected:
-    DISALLOW_COPY_AND_ASSIGN(IKeymasterDevice);
-};
-
-template <typename T> class KeymasterDevice : public IKeymasterDevice {
-  public:
-    KeymasterDevice(T* d) : mDevice{d} {}
-    keymaster_error_t generate_key(const keymaster_key_param_set_t* params,
-                                   keymaster_key_blob_t* key_blob) const override final {
-        return mDevice->generate_key(mDevice, params, key_blob, nullptr);
-    }
-    keymaster_error_t delete_key(const keymaster_key_blob_t* key) const override final {
-        if (mDevice->delete_key == nullptr) return KM_ERROR_OK;
-        return mDevice->delete_key(mDevice, key);
-    }
-    keymaster_error_t begin(keymaster_purpose_t purpose, const keymaster_key_blob_t* key,
-                            const keymaster_key_param_set_t* in_params,
-                            keymaster_key_param_set_t* out_params,
-                            keymaster_operation_handle_t* operation_handle) const override final {
-        return mDevice->begin(mDevice, purpose, key, in_params, out_params, operation_handle);
-    }
-    keymaster_error_t update(keymaster_operation_handle_t operation_handle,
-                             const keymaster_key_param_set_t* in_params,
-                             const keymaster_blob_t* input, size_t* input_consumed,
-                             keymaster_key_param_set_t* out_params,
-                             keymaster_blob_t* output) const override final {
-        return mDevice->update(mDevice, operation_handle, in_params, input, input_consumed,
-                               out_params, output);
-    }
-    keymaster_error_t abort(keymaster_operation_handle_t operation_handle) const override final {
-        return mDevice->abort(mDevice, operation_handle);
-    }
-
-  protected:
-    T* const mDevice;
-};
-
-class Keymaster1Device : public KeymasterDevice<keymaster1_device_t> {
-  public:
-    Keymaster1Device(keymaster1_device_t* d) : KeymasterDevice<keymaster1_device_t>{d} {}
-    ~Keymaster1Device() override final { keymaster1_close(mDevice); }
-    keymaster_error_t finish(keymaster_operation_handle_t operation_handle,
-                             const keymaster_key_param_set_t* in_params,
-                             const keymaster_blob_t* signature,
-                             keymaster_key_param_set_t* out_params,
-                             keymaster_blob_t* output) const override final {
-        return mDevice->finish(mDevice, operation_handle, in_params, signature, out_params, output);
-    }
-};
-
-class Keymaster2Device : public KeymasterDevice<keymaster2_device_t> {
-  public:
-    Keymaster2Device(keymaster2_device_t* d) : KeymasterDevice<keymaster2_device_t>{d} {}
-    ~Keymaster2Device() override final { keymaster2_close(mDevice); }
-    keymaster_error_t finish(keymaster_operation_handle_t operation_handle,
-                             const keymaster_key_param_set_t* in_params,
-                             const keymaster_blob_t* signature,
-                             keymaster_key_param_set_t* out_params,
-                             keymaster_blob_t* output) const override final {
-        return mDevice->finish(mDevice, operation_handle, in_params, nullptr, signature, out_params,
-                               output);
-    }
-};
-
 KeymasterOperation::~KeymasterOperation() {
-    if (mDevice) mDevice->abort(mOpHandle);
+    if (mDevice.get()) mDevice->abort(mOpHandle);
 }
 
 bool KeymasterOperation::updateCompletely(const std::string& input, std::string* output) {
-    output->clear();
+    if (output)
+        output->clear();
     auto it = input.begin();
+    uint32_t inputConsumed;
+
+    ErrorCode km_error;
+    auto hidlCB = [&] (ErrorCode ret, uint32_t _inputConsumed,
+            const hidl_vec<KeyParameter>& /*ignored*/, const hidl_vec<uint8_t>& _output) {
+        km_error = ret;
+        if (km_error != ErrorCode::OK) return;
+        inputConsumed = _inputConsumed;
+        if (output)
+            output->append(reinterpret_cast<const char*>(&_output[0]), _output.size());
+    };
+
     while (it != input.end()) {
         size_t toRead = static_cast<size_t>(input.end() - it);
-        keymaster_blob_t inputBlob{reinterpret_cast<const uint8_t*>(&*it), toRead};
-        keymaster_blob_t outputBlob;
-        size_t inputConsumed;
-        auto error =
-            mDevice->update(mOpHandle, nullptr, &inputBlob, &inputConsumed, nullptr, &outputBlob);
-        if (error != KM_ERROR_OK) {
-            LOG(ERROR) << "update failed, code " << error;
+        auto inputBlob = blob2hidlVec(reinterpret_cast<const uint8_t*>(&*it), toRead);
+        auto error = mDevice->update(mOpHandle, hidl_vec<KeyParameter>(), inputBlob, hidlCB);
+        if (!error.isOk()) {
+            LOG(ERROR) << "update failed: " << error.description();
             mDevice = nullptr;
             return false;
         }
-        output->append(reinterpret_cast<const char*>(outputBlob.data), outputBlob.data_length);
-        free(const_cast<uint8_t*>(outputBlob.data));
+        if (km_error != ErrorCode::OK) {
+            LOG(ERROR) << "update failed, code " << int32_t(km_error);
+            mDevice = nullptr;
+            return false;
+        }
         if (inputConsumed > toRead) {
             LOG(ERROR) << "update reported too much input consumed";
             mDevice = nullptr;
@@ -142,109 +71,248 @@
     return true;
 }
 
-bool KeymasterOperation::finish() {
-    auto error = mDevice->finish(mOpHandle, nullptr, nullptr, nullptr, nullptr);
+bool KeymasterOperation::finish(std::string* output) {
+    ErrorCode km_error;
+    auto hidlCb = [&] (ErrorCode ret, const hidl_vec<KeyParameter>& /*ignored*/,
+            const hidl_vec<uint8_t>& _output) {
+        km_error = ret;
+        if (km_error != ErrorCode::OK) return;
+        if (output)
+            output->assign(reinterpret_cast<const char*>(&_output[0]), _output.size());
+    };
+    auto error = mDevice->finish(mOpHandle, hidl_vec<KeyParameter>(), hidl_vec<uint8_t>(),
+            hidl_vec<uint8_t>(), hidlCb);
     mDevice = nullptr;
-    if (error != KM_ERROR_OK) {
-        LOG(ERROR) << "finish failed, code " << error;
+    if (!error.isOk()) {
+        LOG(ERROR) << "finish failed: " << error.description();
         return false;
     }
-    return true;
-}
-
-bool KeymasterOperation::finishWithOutput(std::string* output) {
-    keymaster_blob_t outputBlob;
-    auto error = mDevice->finish(mOpHandle, nullptr, nullptr, nullptr, &outputBlob);
-    mDevice = nullptr;
-    if (error != KM_ERROR_OK) {
-        LOG(ERROR) << "finish failed, code " << error;
+    if (km_error != ErrorCode::OK) {
+        LOG(ERROR) << "finish failed, code " << int32_t(km_error);
         return false;
     }
-    output->assign(reinterpret_cast<const char*>(outputBlob.data), outputBlob.data_length);
-    free(const_cast<uint8_t*>(outputBlob.data));
     return true;
 }
 
 Keymaster::Keymaster() {
-    mDevice = nullptr;
-    const hw_module_t* module;
-    int ret = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &module);
-    if (ret != 0) {
-        LOG(ERROR) << "hw_get_module_by_class returned " << ret;
-        return;
-    }
-    if (module->module_api_version == KEYMASTER_MODULE_API_VERSION_1_0) {
-        keymaster1_device_t* device;
-        ret = keymaster1_open(module, &device);
-        if (ret != 0) {
-            LOG(ERROR) << "keymaster1_open returned " << ret;
-            return;
-        }
-        mDevice = std::make_shared<Keymaster1Device>(device);
-    } else if (module->module_api_version == KEYMASTER_MODULE_API_VERSION_2_0) {
-        keymaster2_device_t* device;
-        ret = keymaster2_open(module, &device);
-        if (ret != 0) {
-            LOG(ERROR) << "keymaster2_open returned " << ret;
-            return;
-        }
-        mDevice = std::make_shared<Keymaster2Device>(device);
-    } else {
-        LOG(ERROR) << "module_api_version is " << module->module_api_version;
-        return;
-    }
+    mDevice = ::android::hardware::keymaster::V3_0::IKeymasterDevice::getService();
 }
 
-bool Keymaster::generateKey(const keymaster::AuthorizationSet& inParams, std::string* key) {
-    keymaster_key_blob_t keyBlob;
-    auto error = mDevice->generate_key(&inParams, &keyBlob);
-    if (error != KM_ERROR_OK) {
-        LOG(ERROR) << "generate_key failed, code " << error;
+bool Keymaster::generateKey(const AuthorizationSet& inParams, std::string* key) {
+    ErrorCode km_error;
+    auto hidlCb = [&] (ErrorCode ret, const hidl_vec<uint8_t>& keyBlob,
+            const KeyCharacteristics& /*ignored*/) {
+        km_error = ret;
+        if (km_error != ErrorCode::OK) return;
+        if (key)
+            key->assign(reinterpret_cast<const char*>(&keyBlob[0]), keyBlob.size());
+    };
+
+    auto error = mDevice->generateKey(inParams.hidl_data(), hidlCb);
+    if (!error.isOk()) {
+        LOG(ERROR) << "generate_key failed: " << error.description();
         return false;
     }
-    key->assign(reinterpret_cast<const char*>(keyBlob.key_material), keyBlob.key_material_size);
-    free(const_cast<uint8_t*>(keyBlob.key_material));
+    if (km_error != ErrorCode::OK) {
+        LOG(ERROR) << "generate_key failed, code " << int32_t(km_error);
+        return false;
+    }
     return true;
 }
 
 bool Keymaster::deleteKey(const std::string& key) {
-    keymaster_key_blob_t keyBlob{reinterpret_cast<const uint8_t*>(key.data()), key.size()};
-    auto error = mDevice->delete_key(&keyBlob);
-    if (error != KM_ERROR_OK) {
-        LOG(ERROR) << "delete_key failed, code " << error;
+    auto keyBlob = blob2hidlVec(key);
+    auto error = mDevice->deleteKey(keyBlob);
+    if (!error.isOk()) {
+        LOG(ERROR) << "delete_key failed: " << error.description();
+        return false;
+    }
+    if (ErrorCode(error) != ErrorCode::OK) {
+        LOG(ERROR) << "delete_key failed, code " << uint32_t(ErrorCode(error));
         return false;
     }
     return true;
 }
 
-KeymasterOperation Keymaster::begin(keymaster_purpose_t purpose, const std::string& key,
-                                    const keymaster::AuthorizationSet& inParams,
-                                    keymaster::AuthorizationSet* outParams) {
-    keymaster_key_blob_t keyBlob{reinterpret_cast<const uint8_t*>(key.data()), key.size()};
-    keymaster_operation_handle_t mOpHandle;
-    keymaster_key_param_set_t outParams_set;
-    auto error = mDevice->begin(purpose, &keyBlob, &inParams, &outParams_set, &mOpHandle);
-    if (error != KM_ERROR_OK) {
-        LOG(ERROR) << "begin failed, code " << error;
-        return KeymasterOperation(nullptr, mOpHandle);
+bool Keymaster::upgradeKey(const std::string& oldKey, const AuthorizationSet& inParams,
+                           std::string* newKey) {
+    auto oldKeyBlob = blob2hidlVec(oldKey);
+    ErrorCode km_error;
+    auto hidlCb = [&] (ErrorCode ret, const hidl_vec<uint8_t>& upgradedKeyBlob) {
+        km_error = ret;
+        if (km_error != ErrorCode::OK) return;
+        if (newKey)
+            newKey->assign(reinterpret_cast<const char*>(&upgradedKeyBlob[0]),
+                    upgradedKeyBlob.size());
+    };
+    auto error = mDevice->upgradeKey(oldKeyBlob, inParams.hidl_data(), hidlCb);
+    if (!error.isOk()) {
+        LOG(ERROR) << "upgrade_key failed: " << error.description();
+        return false;
     }
-    outParams->Clear();
-    outParams->push_back(outParams_set);
-    keymaster_free_param_set(&outParams_set);
-    return KeymasterOperation(mDevice, mOpHandle);
+    if (km_error != ErrorCode::OK) {
+        LOG(ERROR) << "upgrade_key failed, code " << int32_t(km_error);
+        return false;
+    }
+    return true;
 }
 
-KeymasterOperation Keymaster::begin(keymaster_purpose_t purpose, const std::string& key,
-                                    const keymaster::AuthorizationSet& inParams) {
-    keymaster_key_blob_t keyBlob{reinterpret_cast<const uint8_t*>(key.data()), key.size()};
-    keymaster_operation_handle_t mOpHandle;
-    auto error = mDevice->begin(purpose, &keyBlob, &inParams, nullptr, &mOpHandle);
-    if (error != KM_ERROR_OK) {
-        LOG(ERROR) << "begin failed, code " << error;
-        return KeymasterOperation(nullptr, mOpHandle);
+KeymasterOperation Keymaster::begin(KeyPurpose purpose, const std::string& key,
+                                    const AuthorizationSet& inParams,
+                                    AuthorizationSet* outParams) {
+    auto keyBlob = blob2hidlVec(key);
+    uint64_t mOpHandle;
+    ErrorCode km_error;
+
+    auto hidlCb = [&] (ErrorCode ret, const hidl_vec<KeyParameter>& _outParams,
+            uint64_t operationHandle) {
+        km_error = ret;
+        if (km_error != ErrorCode::OK) return;
+        if (outParams)
+            *outParams = _outParams;
+        mOpHandle = operationHandle;
+    };
+
+    auto error = mDevice->begin(purpose, keyBlob, inParams.hidl_data(), hidlCb);
+    if (!error.isOk()) {
+        LOG(ERROR) << "begin failed: " << error.description();
+        return KeymasterOperation(ErrorCode::UNKNOWN_ERROR);
+    }
+    if (km_error != ErrorCode::OK) {
+        LOG(ERROR) << "begin failed, code " << int32_t(km_error);
+        return KeymasterOperation(km_error);
     }
     return KeymasterOperation(mDevice, mOpHandle);
 }
+bool Keymaster::isSecure() {
+    bool _isSecure = false;
+    auto rc = mDevice->getHardwareFeatures(
+            [&] (bool isSecure, bool, bool, bool, bool, const hidl_string&, const hidl_string&) {
+                _isSecure = isSecure; });
+    return rc.isOk() && _isSecure;
+}
 
 }  // namespace vold
 }  // namespace android
+
+using namespace ::android::vold;
+
+int keymaster_compatibility_cryptfs_scrypt() {
+    Keymaster dev;
+    if (!dev) {
+        LOG(ERROR) << "Failed to initiate keymaster session";
+        return -1;
+    }
+    return dev.isSecure();
+}
+
+int keymaster_create_key_for_cryptfs_scrypt(uint32_t rsa_key_size,
+                                            uint64_t rsa_exponent,
+                                            uint32_t ratelimit,
+                                            uint8_t* key_buffer,
+                                            uint32_t key_buffer_size,
+                                            uint32_t* key_out_size)
+{
+    Keymaster dev;
+    std::string key;
+    if (!dev) {
+        LOG(ERROR) << "Failed to initiate keymaster session";
+        return -1;
+    }
+    if (!key_buffer || !key_out_size) {
+        LOG(ERROR) << __FILE__ << ":" << __LINE__ << ":Invalid argument";
+        return -1;
+    }
+    if (key_out_size) {
+        *key_out_size = 0;
+    }
+
+    auto paramBuilder = AuthorizationSetBuilder()
+                            .Authorization(TAG_ALGORITHM, Algorithm::RSA)
+                            .Authorization(TAG_KEY_SIZE, rsa_key_size)
+                            .Authorization(TAG_RSA_PUBLIC_EXPONENT, rsa_exponent)
+                            .Authorization(TAG_PURPOSE, KeyPurpose::SIGN)
+                            .Authorization(TAG_PADDING, PaddingMode::NONE)
+                            .Authorization(TAG_DIGEST, Digest::NONE)
+                            .Authorization(TAG_BLOB_USAGE_REQUIREMENTS,
+                                    KeyBlobUsageRequirements::STANDALONE)
+                            .Authorization(TAG_NO_AUTH_REQUIRED)
+                            .Authorization(TAG_MIN_SECONDS_BETWEEN_OPS, ratelimit);
+
+    if (!dev.generateKey(paramBuilder, &key)) {
+        return -1;
+    }
+
+    if (key_out_size) {
+        *key_out_size = key.size();
+    }
+
+    if (key_buffer_size < key.size()) {
+        return -1;
+    }
+
+    std::copy(key.data(), key.data() + key.size(), key_buffer);
+    return 0;
+}
+
+int keymaster_sign_object_for_cryptfs_scrypt(const uint8_t* key_blob,
+                                             size_t key_blob_size,
+                                             uint32_t ratelimit,
+                                             const uint8_t* object,
+                                             const size_t object_size,
+                                             uint8_t** signature_buffer,
+                                             size_t* signature_buffer_size)
+{
+    Keymaster dev;
+    if (!dev) {
+        LOG(ERROR) << "Failed to initiate keymaster session";
+        return -1;
+    }
+    if (!key_blob || !object || !signature_buffer || !signature_buffer_size) {
+        LOG(ERROR) << __FILE__ << ":" << __LINE__ << ":Invalid argument";
+        return -1;
+    }
+
+    AuthorizationSet outParams;
+    std::string key(reinterpret_cast<const char*>(key_blob), key_blob_size);
+    std::string input(reinterpret_cast<const char*>(object), object_size);
+    std::string output;
+    KeymasterOperation op;
+
+    auto paramBuilder = AuthorizationSetBuilder()
+                            .Authorization(TAG_PADDING, PaddingMode::NONE)
+                            .Authorization(TAG_DIGEST, Digest::NONE);
+
+    while (true) {
+        op = dev.begin(KeyPurpose::SIGN, key, paramBuilder, &outParams);
+        if (op.errorCode() == ErrorCode::KEY_RATE_LIMIT_EXCEEDED) {
+            sleep(ratelimit);
+            continue;
+        } else break;
+    }
+
+    if (op.errorCode() != ErrorCode::OK) {
+        LOG(ERROR) << "Error starting keymaster signature transaction: " << int32_t(op.errorCode());
+        return -1;
+    }
+
+    if (!op.updateCompletely(input, &output)) {
+        LOG(ERROR) << "Error sending data to keymaster signature transaction: "
+                   << uint32_t(op.errorCode());
+        return -1;
+    }
+
+    if (!op.finish(&output)) {
+        LOG(ERROR) << "Error finalizing keymaster signature transaction: " << int32_t(op.errorCode());
+        return -1;
+    }
+
+    *signature_buffer = reinterpret_cast<uint8_t*>(malloc(output.size()));
+    if (*signature_buffer == nullptr) {
+        LOG(ERROR) << "Error allocation buffer for keymaster signature";
+        return -1;
+    }
+    *signature_buffer_size = output.size();
+    std::copy(output.data(), output.data() + output.size(), *signature_buffer);
+    return 0;
+}
diff --git a/Keymaster.h b/Keymaster.h
index 412110c..e47b403 100644
--- a/Keymaster.h
+++ b/Keymaster.h
@@ -17,26 +17,27 @@
 #ifndef ANDROID_VOLD_KEYMASTER_H
 #define ANDROID_VOLD_KEYMASTER_H
 
+#ifdef __cplusplus
+
 #include <memory>
 #include <string>
 #include <utility>
 
-#include <keymaster/authorization_set.h>
+#include <android/hardware/keymaster/3.0/IKeymasterDevice.h>
+#include <keystore/authorization_set.h>
 
 namespace android {
 namespace vold {
+using ::android::hardware::keymaster::V3_0::IKeymasterDevice;
+using ::keystore::ErrorCode;
+using ::keystore::KeyPurpose;
+using ::keystore::AuthorizationSet;
 
-using namespace keymaster;
-
-// C++ wrappers to the Keymaster C interface.
+// C++ wrappers to the Keymaster hidl interface.
 // This is tailored to the needs of KeyStorage, but could be extended to be
 // a more general interface.
 
-// Class that wraps a keymaster1_device_t or keymaster2_device_t and provides methods
-// they have in common. Also closes the device on destruction.
-class IKeymasterDevice;
-
-// Wrapper for a keymaster_operation_handle_t representing an
+// Wrapper for a Keymaster operation handle representing an
 // ongoing Keymaster operation.  Aborts the operation
 // in the destructor if it is unfinished. Methods log failures
 // to LOG(ERROR).
@@ -45,25 +46,42 @@
     ~KeymasterOperation();
     // Is this instance valid? This is false if creation fails, and becomes
     // false on finish or if an update fails.
-    explicit operator bool() { return mDevice != nullptr; }
+    explicit operator bool() { return mError == ErrorCode::OK; }
+    ErrorCode errorCode() { return mError; }
     // Call "update" repeatedly until all of the input is consumed, and
     // concatenate the output. Return true on success.
     bool updateCompletely(const std::string& input, std::string* output);
-    // Finish; pass nullptr for the "output" param.
-    bool finish();
-    // Finish and write the output to this string.
-    bool finishWithOutput(std::string* output);
+    // Finish and write the output to this string, unless pointer is null.
+    bool finish(std::string* output);
     // Move constructor
     KeymasterOperation(KeymasterOperation&& rhs) {
-        mOpHandle = std::move(rhs.mOpHandle);
         mDevice = std::move(rhs.mDevice);
+        mOpHandle = std::move(rhs.mOpHandle);
+        mError = std::move(rhs.mError);
+    }
+    // Construct an object in an error state for error returns
+    KeymasterOperation()
+        : mDevice{nullptr}, mOpHandle{0},
+          mError {ErrorCode::UNKNOWN_ERROR} {}
+    // Move Assignment
+    KeymasterOperation& operator= (KeymasterOperation&& rhs) {
+        mDevice = std::move(rhs.mDevice);
+        mOpHandle = std::move(rhs.mOpHandle);
+        mError = std::move(rhs.mError);
+        rhs.mError = ErrorCode::UNKNOWN_ERROR;
+        rhs.mOpHandle = 0;
+        return *this;
     }
 
   private:
-    KeymasterOperation(std::shared_ptr<IKeymasterDevice> d, keymaster_operation_handle_t h)
-        : mDevice{d}, mOpHandle{h} {}
-    std::shared_ptr<IKeymasterDevice> mDevice;
-    keymaster_operation_handle_t mOpHandle;
+    KeymasterOperation(const sp<IKeymasterDevice>& d, uint64_t h)
+        : mDevice{d}, mOpHandle{h}, mError {ErrorCode::OK} {}
+    KeymasterOperation(ErrorCode error)
+        : mDevice{nullptr}, mOpHandle{0},
+          mError {error} {}
+    sp<IKeymasterDevice> mDevice;
+    uint64_t mOpHandle;
+    ErrorCode mError;
     DISALLOW_COPY_AND_ASSIGN(KeymasterOperation);
     friend class Keymaster;
 };
@@ -74,37 +92,56 @@
   public:
     Keymaster();
     // false if we failed to open the keymaster device.
-    explicit operator bool() { return mDevice != nullptr; }
+    explicit operator bool() { return mDevice.get() != nullptr; }
     // Generate a key in the keymaster from the given params.
     bool generateKey(const AuthorizationSet& inParams, std::string* key);
     // If the keymaster supports it, permanently delete a key.
     bool deleteKey(const std::string& key);
-    // Begin a new cryptographic operation, collecting output parameters.
-    KeymasterOperation begin(keymaster_purpose_t purpose, const std::string& key,
+    // Replace stored key blob in response to KM_ERROR_KEY_REQUIRES_UPGRADE.
+    bool upgradeKey(const std::string& oldKey, const AuthorizationSet& inParams,
+                    std::string* newKey);
+    // Begin a new cryptographic operation, collecting output parameters if pointer is non-null
+    KeymasterOperation begin(KeyPurpose purpose, const std::string& key,
                              const AuthorizationSet& inParams, AuthorizationSet* outParams);
-    // Begin a new cryptographic operation; don't collect output parameters.
-    KeymasterOperation begin(keymaster_purpose_t purpose, const std::string& key,
-                             const AuthorizationSet& inParams);
+    bool isSecure();
 
   private:
-    std::shared_ptr<IKeymasterDevice> mDevice;
+    sp<hardware::keymaster::V3_0::IKeymasterDevice> mDevice;
     DISALLOW_COPY_AND_ASSIGN(Keymaster);
 };
 
-template <keymaster_tag_t Tag>
-inline AuthorizationSetBuilder& addStringParam(AuthorizationSetBuilder&& params,
-                                               TypedTag<KM_BYTES, Tag> tag,
-                                               const std::string& val) {
-    return params.Authorization(tag, val.data(), val.size());
-}
-
-template <keymaster_tag_t Tag>
-inline void addStringParam(AuthorizationSetBuilder* params, TypedTag<KM_BYTES, Tag> tag,
-                           const std::string& val) {
-    params->Authorization(tag, val.data(), val.size());
-}
-
 }  // namespace vold
 }  // namespace android
 
+#endif // __cplusplus
+
+
+/*
+ * The following functions provide C bindings to keymaster services
+ * needed by cryptfs scrypt. The compatibility check checks whether
+ * the keymaster implementation is considered secure, i.e., TEE backed.
+ * The create_key function generates an RSA key for signing.
+ * The sign_object function signes an object with the given keymaster
+ * key.
+ */
+__BEGIN_DECLS
+
+int keymaster_compatibility_cryptfs_scrypt();
+int keymaster_create_key_for_cryptfs_scrypt(uint32_t rsa_key_size,
+                                            uint64_t rsa_exponent,
+                                            uint32_t ratelimit,
+                                            uint8_t* key_buffer,
+                                            uint32_t key_buffer_size,
+                                            uint32_t* key_out_size);
+
+int keymaster_sign_object_for_cryptfs_scrypt(const uint8_t* key_blob,
+                                             size_t key_blob_size,
+                                             uint32_t ratelimit,
+                                             const uint8_t* object,
+                                             const size_t object_size,
+                                             uint8_t** signature_buffer,
+                                             size_t* signature_buffer_size);
+
+__END_DECLS
+
 #endif
diff --git a/Loop.cpp b/Loop.cpp
index a5863b3..6ec5e6d 100644
--- a/Loop.cpp
+++ b/Loop.cpp
@@ -32,12 +32,19 @@
 
 #include <cutils/log.h>
 
+#include <android-base/logging.h>
+#include <android-base/stringprintf.h>
+#include <android-base/unique_fd.h>
+
 #include <sysutils/SocketClient.h>
 #include "Loop.h"
 #include "Asec.h"
 #include "VoldUtil.h"
 #include "sehandle.h"
 
+using android::base::StringPrintf;
+using android::base::unique_fd;
+
 int Loop::dumpState(SocketClient *c) {
     int i;
     int fd;
@@ -47,7 +54,7 @@
         struct loop_info64 li;
         int rc;
 
-        sprintf(filename, "/dev/block/loop%d", i);
+        snprintf(filename, sizeof(filename), "/dev/block/loop%d", i);
 
         if ((fd = open(filename, O_RDWR | O_CLOEXEC)) < 0) {
             if (errno != ENOENT) {
@@ -91,7 +98,7 @@
         struct loop_info64 li;
         int rc;
 
-        sprintf(filename, "/dev/block/loop%d", i);
+        snprintf(filename, sizeof(filename), "/dev/block/loop%d", i);
 
         if ((fd = open(filename, O_RDWR | O_CLOEXEC)) < 0) {
             if (errno != ENOENT) {
@@ -137,7 +144,7 @@
         int rc;
         char *secontext = NULL;
 
-        sprintf(filename, "/dev/block/loop%d", i);
+        snprintf(filename, sizeof(filename), "/dev/block/loop%d", i);
 
         /*
          * The kernel starts us off with 8 loop nodes, but more
@@ -229,6 +236,40 @@
     return 0;
 }
 
+int Loop::create(const std::string& target, std::string& out_device) {
+    unique_fd ctl_fd(open("/dev/loop-control", O_RDWR | O_CLOEXEC));
+    if (ctl_fd.get() == -1) {
+        PLOG(ERROR) << "Failed to open loop-control";
+        return -errno;
+    }
+
+    int num = ioctl(ctl_fd.get(), LOOP_CTL_GET_FREE);
+    if (num == -1) {
+        PLOG(ERROR) << "Failed LOOP_CTL_GET_FREE";
+        return -errno;
+    }
+
+    out_device = StringPrintf("/dev/block/loop%d", num);
+
+    unique_fd target_fd(open(target.c_str(), O_RDWR | O_CLOEXEC));
+    if (target_fd.get() == -1) {
+        PLOG(ERROR) << "Failed to open " << target;
+        return -errno;
+    }
+    unique_fd device_fd(open(out_device.c_str(), O_RDWR | O_CLOEXEC));
+    if (device_fd.get() == -1) {
+        PLOG(ERROR) << "Failed to open " << out_device;
+        return -errno;
+    }
+
+    if (ioctl(device_fd.get(), LOOP_SET_FD, target_fd.get()) == -1) {
+        PLOG(ERROR) << "Failed to LOOP_SET_FD";
+        return -errno;
+    }
+
+    return 0;
+}
+
 int Loop::destroyByDevice(const char *loopDevice) {
     int device_fd;
 
@@ -254,19 +295,18 @@
 }
 
 int Loop::createImageFile(const char *file, unsigned long numSectors) {
-    int fd;
-
-    if ((fd = creat(file, 0600)) < 0) {
-        SLOGE("Error creating imagefile (%s)", strerror(errno));
-        return -1;
+    unique_fd fd(open(file, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, 0600));
+    if (fd.get() == -1) {
+        PLOG(ERROR) << "Failed to create image " << file;
+        return -errno;
     }
-
-    if (ftruncate(fd, numSectors * 512) < 0) {
-        SLOGE("Error truncating imagefile (%s)", strerror(errno));
-        close(fd);
-        return -1;
+    if (fallocate(fd.get(), 0, 0, numSectors * 512) == -1) {
+        PLOG(WARNING) << "Failed to fallocate; falling back to ftruncate";
+        if (ftruncate(fd, numSectors * 512) == -1) {
+            PLOG(ERROR) << "Failed to ftruncate";
+            return -errno;
+        }
     }
-    close(fd);
     return 0;
 }
 
diff --git a/Loop.h b/Loop.h
index 72130b0..5d8f427 100644
--- a/Loop.h
+++ b/Loop.h
@@ -17,6 +17,7 @@
 #ifndef _LOOP_H
 #define _LOOP_H
 
+#include <string>
 #include <unistd.h>
 #include <linux/loop.h>
 
@@ -29,6 +30,7 @@
     static int lookupActive(const char *id, char *buffer, size_t len);
     static int lookupInfo(const char *loopDevice, struct asec_superblock *sb, unsigned long *nr_sec);
     static int create(const char *id, const char *loopFile, char *loopDeviceBuffer, size_t len);
+    static int create(const std::string& file, std::string& out_device);
     static int destroyByDevice(const char *loopDevice);
     static int destroyByFile(const char *loopFile);
     static int createImageFile(const char *file, unsigned long numSectors);
diff --git a/MoveTask.cpp b/MoveTask.cpp
index a0522d2..ea64a1c 100644
--- a/MoveTask.cpp
+++ b/MoveTask.cpp
@@ -27,7 +27,9 @@
 #include <dirent.h>
 #include <sys/wait.h>
 
-#define CONSTRAIN(amount, low, high) (amount < low ? low : (amount > high ? high : amount))
+#define CONSTRAIN(amount, low, high) ((amount) < (low) ? (low) : ((amount) > (high) ? (high) : (amount)))
+
+#define EXEC_BLOCKING 0
 
 using android::base::StringPrintf;
 
@@ -93,6 +95,9 @@
         return OK;
     }
 
+#if EXEC_BLOCKING
+    return ForkExecvp(cmd);
+#else
     pid_t pid = ForkExecvpAsync(cmd);
     if (pid == -1) return -1;
 
@@ -113,6 +118,7 @@
                 ((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress));
     }
     return -1;
+#endif
 }
 
 static status_t execCp(const std::string& fromPath, const std::string& toPath,
@@ -122,6 +128,12 @@
     uint64_t expectedBytes = GetTreeBytes(fromPath);
     uint64_t startFreeBytes = GetFreeBytes(toPath);
 
+    if (expectedBytes > startFreeBytes) {
+        LOG(ERROR) << "Data size " << expectedBytes << " is too large to fit in free space "
+                << startFreeBytes;
+        return -1;
+    }
+
     std::vector<std::string> cmd;
     cmd.push_back(kCpPath);
     cmd.push_back("-p"); /* preserve timestamps, ownership, and permissions */
@@ -134,6 +146,9 @@
     }
     cmd.push_back(toPath.c_str());
 
+#if EXEC_BLOCKING
+    return ForkExecvp(cmd);
+#else
     pid_t pid = ForkExecvpAsync(cmd);
     if (pid == -1) return -1;
 
@@ -154,6 +169,7 @@
                 ((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress));
     }
     return -1;
+#endif
 }
 
 static void bringOffline(const std::shared_ptr<VolumeBase>& vol) {
diff --git a/NetlinkHandler.h b/NetlinkHandler.h
index 00a31c8..56eb23c 100644
--- a/NetlinkHandler.h
+++ b/NetlinkHandler.h
@@ -22,7 +22,7 @@
 class NetlinkHandler: public NetlinkListener {
 
 public:
-    NetlinkHandler(int listenerSocket);
+    explicit NetlinkHandler(int listenerSocket);
     virtual ~NetlinkHandler();
 
     int start(void);
diff --git a/Process.cpp b/Process.cpp
index 962a460..fd757d5 100644
--- a/Process.cpp
+++ b/Process.cpp
@@ -85,7 +85,7 @@
 
     // compute path to process's directory of open files
     char    path[PATH_MAX];
-    sprintf(path, "/proc/%d/fd", pid);
+    snprintf(path, sizeof(path), "/proc/%d/fd", pid);
     DIR *dir = opendir(path);
     if (!dir)
         return 0;
@@ -129,8 +129,8 @@
     FILE *file;
     char buffer[PATH_MAX + 100];
 
-    sprintf(buffer, "/proc/%d/maps", pid);
-    file = fopen(buffer, "r");
+    snprintf(buffer, sizeof(buffer), "/proc/%d/maps", pid);
+    file = fopen(buffer, "re");
     if (!file)
         return 0;
     
@@ -155,7 +155,7 @@
     char    path[PATH_MAX];
     char    link[PATH_MAX];
 
-    sprintf(path, "/proc/%d/%s", pid, name);
+    snprintf(path, sizeof(path), "/proc/%d/%s", pid, name);
     if (readSymLink(path, link, sizeof(link)) && pathMatchesMountPoint(link, mountPoint)) 
         return 1;
     return 0;
diff --git a/ScryptParameters.cpp b/ScryptParameters.cpp
index 669809b..c0e2030 100644
--- a/ScryptParameters.cpp
+++ b/ScryptParameters.cpp
@@ -20,7 +20,7 @@
 #include <string.h>
 
 bool parse_scrypt_parameters(const char* paramstr, int *Nf, int *rf, int *pf) {
-    int params[3];
+    int params[3] = {};
     char *token;
     char *saveptr;
     int i;
diff --git a/TrimTask.cpp b/TrimTask.cpp
index d7bfda7..08e6499 100644
--- a/TrimTask.cpp
+++ b/TrimTask.cpp
@@ -53,7 +53,7 @@
     VolumeManager* vm = VolumeManager::Instance();
     std::list<std::string> privateIds;
     vm->listVolumes(VolumeBase::Type::kPrivate, privateIds);
-    for (auto id : privateIds) {
+    for (const auto& id : privateIds) {
         auto vol = vm->findVolume(id);
         if (vol != nullptr && vol->getState() == VolumeBase::State::kMounted) {
             mPaths.push_back(vol->getPath());
@@ -65,10 +65,10 @@
 }
 
 void TrimTask::addFromFstab() {
-    struct fstab *fstab;
+    std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
+                                                               fs_mgr_free_fstab);
     struct fstab_rec *prev_rec = NULL;
 
-    fstab = fs_mgr_read_fstab(android::vold::DefaultFstabPath().c_str());
     for (int i = 0; i < fstab->num_entries; i++) {
         /* Skip raw partitions */
         if (!strcmp(fstab->recs[i].fs_type, "emmc") ||
@@ -96,7 +96,6 @@
         mPaths.push_back(fstab->recs[i].mount_point);
         prev_rec = &fstab->recs[i];
     }
-    fs_mgr_free_fstab(fstab);
 }
 
 void TrimTask::start() {
@@ -114,7 +113,7 @@
 void TrimTask::run() {
     acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
 
-    for (auto path : mPaths) {
+    for (const auto& path : mPaths) {
         LOG(DEBUG) << "Starting trim of " << path;
 
         int fd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
diff --git a/TrimTask.h b/TrimTask.h
index 57be802..2ade7b5 100644
--- a/TrimTask.h
+++ b/TrimTask.h
@@ -27,7 +27,7 @@
 
 class TrimTask {
 public:
-    TrimTask(int flags);
+    explicit TrimTask(int flags);
     virtual ~TrimTask();
 
     enum Flags {
diff --git a/Utils.cpp b/Utils.cpp
index 43014bd..f9ce065 100644
--- a/Utils.cpp
+++ b/Utils.cpp
@@ -208,10 +208,10 @@
     }
 
     char value[128];
-    for (auto line : output) {
+    for (const auto& line : output) {
         // Extract values from blkid output, if defined
         const char* cline = line.c_str();
-        char* start = strstr(cline, "TYPE=\"");
+        const char* start = strstr(cline, "TYPE=\"");
         if (start != nullptr && sscanf(start + 5, "\"%127[^\"]\"", value) == 1) {
             fsType = value;
         }
@@ -432,7 +432,7 @@
 uint64_t GetFreeBytes(const std::string& path) {
     struct statvfs sb;
     if (statvfs(path.c_str(), &sb) == 0) {
-        return (uint64_t)sb.f_bfree * sb.f_bsize;
+        return (uint64_t) sb.f_bavail * sb.f_frsize;
     } else {
         return -1;
     }
@@ -483,7 +483,7 @@
                     continue;
             }
 
-            subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
+            subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
             if (subfd >= 0) {
                 size += calculate_dir_size(subfd);
             }
@@ -494,7 +494,7 @@
 }
 
 uint64_t GetTreeBytes(const std::string& path) {
-    int dirfd = open(path.c_str(), O_DIRECTORY, O_RDONLY);
+    int dirfd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC);
     if (dirfd < 0) {
         PLOG(WARNING) << "Failed to open " << path;
         return -1;
@@ -589,11 +589,6 @@
     return StringPrintf("%s/misc/profiles/cur/%u", BuildDataPath(nullptr).c_str(), userId);
 }
 
-std::string BuildDataProfilesForeignDexDePath(userid_t userId) {
-    std::string profiles_path = BuildDataProfilesDePath(userId);
-    return StringPrintf("%s/foreign-dex", profiles_path.c_str());
-}
-
 std::string BuildDataPath(const char* volumeUuid) {
     // TODO: unify with installd path generation logic
     if (volumeUuid == nullptr) {
@@ -640,12 +635,6 @@
     }
 }
 
-std::string DefaultFstabPath() {
-    char hardware[PROPERTY_VALUE_MAX];
-    property_get("ro.hardware", hardware, "");
-    return StringPrintf("/fstab.%s", hardware);
-}
-
 status_t RestoreconRecursive(const std::string& path) {
     LOG(VERBOSE) << "Starting restorecon of " << path;
 
@@ -679,20 +668,6 @@
     }
 }
 
-ScopedFd::ScopedFd(int fd) : fd_(fd) {}
-
-ScopedFd::~ScopedFd() {
-    close(fd_);
-}
-
-ScopedDir::ScopedDir(DIR* dir) : dir_(dir) {}
-
-ScopedDir::~ScopedDir() {
-    if (dir_ != nullptr) {
-        closedir(dir_);
-    }
-}
-
 bool IsRunningInEmulator() {
     return property_get_bool("ro.kernel.qemu", 0);
 }
diff --git a/Utils.h b/Utils.h
index 4bfd8e9..813ffac 100644
--- a/Utils.h
+++ b/Utils.h
@@ -103,7 +103,6 @@
 std::string BuildDataMiscCePath(userid_t userid);
 std::string BuildDataMiscDePath(userid_t userid);
 std::string BuildDataProfilesDePath(userid_t userid);
-std::string BuildDataProfilesForeignDexDePath(userid_t userid);
 
 std::string BuildDataPath(const char* volumeUuid);
 std::string BuildDataMediaCePath(const char* volumeUuid, userid_t userid);
@@ -112,32 +111,10 @@
 
 dev_t GetDevice(const std::string& path);
 
-std::string DefaultFstabPath();
-
 status_t RestoreconRecursive(const std::string& path);
 
 status_t SaneReadLinkAt(int dirfd, const char* path, char* buf, size_t bufsiz);
 
-class ScopedFd {
-    const int fd_;
-public:
-    ScopedFd(int fd);
-    ~ScopedFd();
-    int get() const { return fd_; }
-
-    DISALLOW_COPY_AND_ASSIGN(ScopedFd);
-};
-
-class ScopedDir {
-    DIR* const dir_;
-public:
-    ScopedDir(DIR* dir);
-    ~ScopedDir();
-    DIR* get() const { return dir_; }
-
-    DISALLOW_COPY_AND_ASSIGN(ScopedDir);
-};
-
 /* Checks if Android is running in QEMU */
 bool IsRunningInEmulator();
 
diff --git a/VoldCommand.h b/VoldCommand.h
index 5ddc666..e435159 100644
--- a/VoldCommand.h
+++ b/VoldCommand.h
@@ -21,7 +21,7 @@
 
 class VoldCommand : public FrameworkCommand {
 public:
-    VoldCommand(const char *cmd);
+    explicit VoldCommand(const char *cmd);
     virtual ~VoldCommand() {}
 };
 
diff --git a/VolumeBase.cpp b/VolumeBase.cpp
index ea4d372..3f27d87 100644
--- a/VolumeBase.cpp
+++ b/VolumeBase.cpp
@@ -30,8 +30,6 @@
 
 using android::base::StringPrintf;
 
-#define DEBUG 1
-
 namespace android {
 namespace vold {
 
@@ -219,7 +217,7 @@
     }
 
     setState(State::kEjecting);
-    for (auto vol : mVolumes) {
+    for (const auto& vol : mVolumes) {
         if (vol->destroy()) {
             LOG(WARNING) << getId() << " failed to destroy " << vol->getId()
                     << " stacked above";
diff --git a/VolumeManager.cpp b/VolumeManager.cpp
index 5cc60a1..3b4c054 100644
--- a/VolumeManager.cpp
+++ b/VolumeManager.cpp
@@ -63,8 +63,8 @@
 
 #define MASS_STORAGE_FILE_PATH  "/sys/class/android_usb/android0/f_mass_storage/lun/file"
 
-#define ROUND_UP_POWER_OF_2(number, po2) (((!!(number & ((1U << po2) - 1))) << po2)\
-                                         + (number & (~((1U << po2) - 1))))
+#define ROUND_UP_POWER_OF_2(number, po2) (((!!((number) & ((1U << (po2)) - 1))) << (po2))\
+                                         + ((number) & (~((1U << (po2)) - 1))))
 
 using android::base::StringPrintf;
 
@@ -88,7 +88,13 @@
  */
 const char *VolumeManager::LOOPDIR           = "/mnt/obb";
 
-static const char* kUserMountPath = "/mnt/user";
+static const char* kPathUserMount = "/mnt/user";
+static const char* kPathVirtualDisk = "/data/misc/vold/virtual_disk";
+
+static const char* kPropVirtualDisk = "persist.sys.virtual_disk";
+
+/* 512MiB is large enough for testing purposes */
+static const unsigned int kSizeVirtualDisk = 536870912;
 
 static const unsigned int kMajorBlockMmc = 179;
 static const unsigned int kMajorBlockExperimentalMin = 240;
@@ -249,6 +255,55 @@
     return buffer;
 }
 
+int VolumeManager::updateVirtualDisk() {
+    if (property_get_bool(kPropVirtualDisk, false)) {
+        if (access(kPathVirtualDisk, F_OK) != 0) {
+            Loop::createImageFile(kPathVirtualDisk, kSizeVirtualDisk / 512);
+        }
+
+        if (mVirtualDisk == nullptr) {
+            if (Loop::create(kPathVirtualDisk, mVirtualDiskPath) != 0) {
+                LOG(ERROR) << "Failed to create virtual disk";
+                return -1;
+            }
+
+            struct stat buf;
+            if (stat(mVirtualDiskPath.c_str(), &buf) < 0) {
+                PLOG(ERROR) << "Failed to stat " << mVirtualDiskPath;
+                return -1;
+            }
+
+            auto disk = new android::vold::Disk("virtual", buf.st_rdev, "virtual",
+                    android::vold::Disk::Flags::kAdoptable | android::vold::Disk::Flags::kSd);
+            disk->create();
+            mVirtualDisk = std::shared_ptr<android::vold::Disk>(disk);
+            mDisks.push_back(mVirtualDisk);
+        }
+    } else {
+        if (mVirtualDisk != nullptr) {
+            dev_t device = mVirtualDisk->getDevice();
+
+            auto i = mDisks.begin();
+            while (i != mDisks.end()) {
+                if ((*i)->getDevice() == device) {
+                    (*i)->destroy();
+                    i = mDisks.erase(i);
+                } else {
+                    ++i;
+                }
+            }
+
+            Loop::destroyByDevice(mVirtualDiskPath.c_str());
+            mVirtualDisk = nullptr;
+        }
+
+        if (access(kPathVirtualDisk, F_OK) == 0) {
+            unlink(kPathVirtualDisk);
+        }
+    }
+    return 0;
+}
+
 int VolumeManager::setDebug(bool enable) {
     mDebug = enable;
     return 0;
@@ -266,6 +321,9 @@
             new android::vold::EmulatedVolume("/data/media"));
     mInternalEmulated->create();
 
+    // Consider creating a virtual disk
+    updateVirtualDisk();
+
     return 0;
 }
 
@@ -296,7 +354,7 @@
 
     switch (evt->getAction()) {
     case NetlinkEvent::Action::kAdd: {
-        for (auto source : mDiskSources) {
+        for (const auto& source : mDiskSources) {
             if (source->matches(eventPath)) {
                 // For now, assume that MMC and virtio-blk (the latter is
                 // emulator-specific; see Disk.cpp for details) devices are SD,
@@ -322,7 +380,7 @@
     }
     case NetlinkEvent::Action::kChange: {
         LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
-        for (auto disk : mDisks) {
+        for (const auto& disk : mDisks) {
             if (disk->getDevice() == device) {
                 disk->readMetadata();
                 disk->readPartitions();
@@ -350,6 +408,7 @@
 }
 
 void VolumeManager::addDiskSource(const std::shared_ptr<DiskSource>& diskSource) {
+    std::lock_guard<std::mutex> lock(mLock);
     mDiskSources.push_back(diskSource);
 }
 
@@ -366,7 +425,7 @@
     if (mInternalEmulated->getId() == id) {
         return mInternalEmulated;
     }
-    for (auto disk : mDisks) {
+    for (const auto& disk : mDisks) {
         auto vol = disk->findVolume(id);
         if (vol != nullptr) {
             return vol;
@@ -378,7 +437,7 @@
 void VolumeManager::listVolumes(android::vold::VolumeBase::Type type,
         std::list<std::string>& list) {
     list.clear();
-    for (auto disk : mDisks) {
+    for (const auto& disk : mDisks) {
         disk->listVolumes(type, list);
     }
 }
@@ -454,7 +513,7 @@
     // Note that sometimes the system will spin up processes from Zygote
     // before actually starting the user, so we're okay if Zygote
     // already created this directory.
-    std::string path(StringPrintf("%s/%d", kUserMountPath, userId));
+    std::string path(StringPrintf("%s/%d", kPathUserMount, userId));
     fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
 
     mStartedUsers.insert(userId);
@@ -497,7 +556,7 @@
     }
     endmntent(fp);
 
-    for (auto path : toUnmount) {
+    for (const auto& path : toUnmount) {
         if (umount2(path.c_str(), MNT_DETACH)) {
             ALOGW("Failed to unmount %s: %s", path.c_str(), strerror(errno));
         }
@@ -558,7 +617,7 @@
         }
 
         // We purposefully leave the namespace open across the fork
-        nsFd = openat(pidFd, "ns/mnt", O_RDONLY);
+        nsFd = openat(pidFd, "ns/mnt", O_RDONLY); // not O_CLOEXEC
         if (nsFd < 0) {
             PLOG(WARNING) << "Failed to open namespace for " << de->d_name;
             goto next;
@@ -584,11 +643,17 @@
                 _exit(0);
             }
             if (TEMP_FAILURE_RETRY(mount(storageSource.c_str(), "/storage",
-                    NULL, MS_BIND | MS_REC | MS_SLAVE, NULL)) == -1) {
+                    NULL, MS_BIND | MS_REC, NULL)) == -1) {
                 PLOG(ERROR) << "Failed to mount " << storageSource << " for "
                         << de->d_name;
                 _exit(1);
             }
+            if (TEMP_FAILURE_RETRY(mount(NULL, "/storage", NULL,
+                    MS_REC | MS_SLAVE, NULL)) == -1) {
+                PLOG(ERROR) << "Failed to set MS_SLAVE to /storage for "
+                        << de->d_name;
+                _exit(1);
+            }
 
             // Mount user-specific symlink helper into place
             userid_t user_id = multiuser_get_user_id(uid);
@@ -623,18 +688,24 @@
     // newly connected framework hears all events.
     mInternalEmulated->destroy();
     mInternalEmulated->create();
-    for (auto disk : mDisks) {
+    for (const auto& disk : mDisks) {
         disk->destroy();
         disk->create();
     }
+    updateVirtualDisk();
     mAddedUsers.clear();
     mStartedUsers.clear();
     return 0;
 }
 
+// Can be called twice (sequentially) during shutdown. should be safe for that.
 int VolumeManager::shutdown() {
+    if (mInternalEmulated == nullptr) {
+        return 0; // already shutdown
+    }
     mInternalEmulated->destroy();
-    for (auto disk : mDisks) {
+    mInternalEmulated = nullptr;
+    for (const auto& disk : mDisks) {
         disk->destroy();
     }
     mDisks.clear();
@@ -648,7 +719,7 @@
     if (mInternalEmulated != nullptr) {
         mInternalEmulated->unmount();
     }
-    for (auto disk : mDisks) {
+    for (const auto& disk : mDisks) {
         disk->unmountAll();
     }
 
@@ -672,7 +743,7 @@
     }
     endmntent(fp);
 
-    for (auto path : toUnmount) {
+    for (const auto& path : toUnmount) {
         SLOGW("Tearing down stale mount %s", path.c_str());
         android::vold::ForceUnmount(path);
     }
@@ -1001,24 +1072,6 @@
 
     oldNumSec = info.st_size / 512;
 
-    unsigned long numImgSectors;
-    if (sb.c_opts & ASEC_SB_C_OPTS_EXT4)
-        numImgSectors = adjustSectorNumExt4(numSectors);
-    else
-        numImgSectors = adjustSectorNumFAT(numSectors);
-    /*
-     *  add one block for the superblock
-     */
-    SLOGD("Resizing from %lu sectors to %lu sectors", oldNumSec, numImgSectors + 1);
-    if (oldNumSec == numImgSectors + 1) {
-        SLOGW("Size unchanged; ignoring resize request");
-        return 0;
-    } else if (oldNumSec > numImgSectors + 1) {
-        SLOGE("Only growing is currently supported.");
-        close(fd);
-        return -1;
-    }
-
     /*
      * Try to read superblock.
      */
@@ -1044,10 +1097,26 @@
         return -1;
     }
 
+    unsigned long numImgSectors;
     if (!(sb.c_opts & ASEC_SB_C_OPTS_EXT4)) {
         SLOGE("Only ext4 partitions are supported for resize");
         errno = EINVAL;
         return -1;
+    } else {
+        numImgSectors = adjustSectorNumExt4(numSectors);
+    }
+
+    /*
+     *  add one block for the superblock
+     */
+    SLOGD("Resizing from %lu sectors to %lu sectors", oldNumSec, numImgSectors + 1);
+    if (oldNumSec == numImgSectors + 1) {
+        SLOGW("Size unchanged; ignoring resize request");
+        return 0;
+    } else if (oldNumSec > numImgSectors + 1) {
+        SLOGE("Only growing is currently supported.");
+        close(fd);
+        return -1;
     }
 
     if (Loop::resizeImageFile(asecFileName, numImgSectors + 1)) {
diff --git a/VolumeManager.h b/VolumeManager.h
index 39fc8f9..796a91d 100644
--- a/VolumeManager.h
+++ b/VolumeManager.h
@@ -176,6 +176,7 @@
     int unmountLoopImage(const char *containerId, const char *loopId,
             const char *fileName, const char *mountPoint, bool force);
 
+    int updateVirtualDisk();
     int setDebug(bool enable);
 
     void setBroadcaster(SocketListener *sl) { mBroadcaster = sl; }
@@ -211,13 +212,15 @@
     std::unordered_map<userid_t, int> mAddedUsers;
     std::unordered_set<userid_t> mStartedUsers;
 
+    std::string mVirtualDiskPath;
+    std::shared_ptr<android::vold::Disk> mVirtualDisk;
     std::shared_ptr<android::vold::VolumeBase> mInternalEmulated;
     std::shared_ptr<android::vold::VolumeBase> mPrimary;
 };
 
 extern "C" {
 #endif /* __cplusplus */
-#define UNMOUNT_NOT_MOUNTED_ERR -2
+#define UNMOUNT_NOT_MOUNTED_ERR (-2)
     int vold_unmountAll(void);
 #ifdef __cplusplus
 }
diff --git a/bench/benchgen.py b/bench/benchgen.py
index 9a2bcd7..c852169 100644
--- a/bench/benchgen.py
+++ b/bench/benchgen.py
@@ -183,7 +183,12 @@
                 if handle not in defined:
                     print >>bench, "int ",
                     defined.add(handle)
-                print >>bench, '%s = TEMP_FAILURE_RETRY(open("file%s", %s));' % (handle, f.ident, e.args[2])
+                create_mode = ''
+                if 'O_CREAT' in e.args[2]:
+                    assert len(e.args) > 3, 'File creation lacks a mode?'
+                    create_mode = ', ' + e.args[3]
+                print >>bench, '%s = TEMP_FAILURE_RETRY(open("file%s", %s%s));' \
+                        % (handle, f.ident, e.args[2], create_mode)
 
         elif e.call == "close":
             fd, f, handle = extract_file(e, e.args[0])
@@ -273,7 +278,7 @@
         LOG(ERROR) << "Failed to read random data";
         return -EIO;
     }
-    if ((out = TEMP_FAILURE_RETRY(open(name, O_WRONLY|O_CREAT|O_TRUNC))) < 0) {
+    if ((out = TEMP_FAILURE_RETRY(open(name, O_WRONLY|O_CREAT|O_TRUNC, 0644))) < 0) {
         PLOG(ERROR) << "Failed to open " << name;
         return -errno;
     }
diff --git a/cryptfs.c b/cryptfs.cpp
similarity index 89%
rename from cryptfs.c
rename to cryptfs.cpp
index 2a52336..f2f0f18 100644
--- a/cryptfs.c
+++ b/cryptfs.cpp
@@ -38,7 +38,8 @@
 #include <openssl/evp.h>
 #include <openssl/sha.h>
 #include <errno.h>
-#include <ext4.h>
+#include <ext4_utils/ext4.h>
+#include <ext4_utils/ext4_utils.h>
 #include <linux/kdev_t.h>
 #include <fs_mgr.h>
 #include <time.h>
@@ -55,25 +56,19 @@
 #include "ScryptParameters.h"
 #include "VolumeManager.h"
 #include "VoldUtil.h"
-#include "crypto_scrypt.h"
 #include "Ext4Crypt.h"
-#include "ext4_utils.h"
 #include "f2fs_sparseblock.h"
 #include "CheckBattery.h"
 #include "Process.h"
-
+#include "Keymaster.h"
+#include "android-base/properties.h"
 #include <bootloader_message/bootloader_message.h>
-#include <hardware/keymaster0.h>
-#include <hardware/keymaster1.h>
+extern "C" {
+#include <crypto_scrypt.h>
+}
 
 #define UNUSED __attribute__((unused))
 
-#define UNUSED __attribute__((unused))
-
-#ifdef CONFIG_HW_DISK_ENCRYPTION
-#include "cryptfs_hw.h"
-#endif
-
 #define DM_CRYPT_BUF_SIZE 4096
 
 #define HASH_COUNT 2000
@@ -101,188 +96,37 @@
 #define RETRY_MOUNT_ATTEMPTS 10
 #define RETRY_MOUNT_DELAY_SECONDS 1
 
-char *me = "cryptfs";
-
 static unsigned char saved_master_key[KEY_LEN_BYTES];
 static char *saved_mount_point;
 static int  master_key_saved = 0;
 static struct crypt_persist_data *persist_data = NULL;
 
-static int keymaster_init(keymaster0_device_t **keymaster0_dev,
-                          keymaster1_device_t **keymaster1_dev)
-{
-    int rc;
-
-    const hw_module_t* mod;
-    rc = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &mod);
-    if (rc) {
-        ALOGE("could not find any keystore module");
-        goto err;
-    }
-
-    SLOGI("keymaster module name is %s", mod->name);
-    SLOGI("keymaster version is %d", mod->module_api_version);
-
-    *keymaster0_dev = NULL;
-    *keymaster1_dev = NULL;
-    if (mod->module_api_version == KEYMASTER_MODULE_API_VERSION_1_0) {
-        SLOGI("Found keymaster1 module, using keymaster1 API.");
-        rc = keymaster1_open(mod, keymaster1_dev);
-    } else {
-        SLOGI("Found keymaster0 module, using keymaster0 API.");
-        rc = keymaster0_open(mod, keymaster0_dev);
-    }
-
-    if (rc) {
-        ALOGE("could not open keymaster device in %s (%s)",
-              KEYSTORE_HARDWARE_MODULE_ID, strerror(-rc));
-        goto err;
-    }
-
-    return 0;
-
-err:
-    *keymaster0_dev = NULL;
-    *keymaster1_dev = NULL;
-    return rc;
-}
-
 /* Should we use keymaster? */
 static int keymaster_check_compatibility()
 {
-    keymaster0_device_t *keymaster0_dev = 0;
-    keymaster1_device_t *keymaster1_dev = 0;
-    int rc = 0;
-
-    if (keymaster_init(&keymaster0_dev, &keymaster1_dev)) {
-        SLOGE("Failed to init keymaster");
-        rc = -1;
-        goto out;
-    }
-
-    if (keymaster1_dev) {
-        rc = 1;
-        goto out;
-    }
-
-    if (!keymaster0_dev || !keymaster0_dev->common.module) {
-        rc = -1;
-        goto out;
-    }
-
-    // TODO(swillden): Check to see if there's any reason to require v0.3.  I think v0.1 and v0.2
-    // should work.
-    if (keymaster0_dev->common.module->module_api_version
-            < KEYMASTER_MODULE_API_VERSION_0_3) {
-        rc = 0;
-        goto out;
-    }
-
-    if (!(keymaster0_dev->flags & KEYMASTER_SOFTWARE_ONLY) &&
-        (keymaster0_dev->flags & KEYMASTER_BLOBS_ARE_STANDALONE)) {
-        rc = 1;
-    }
-
-out:
-    if (keymaster1_dev) {
-        keymaster1_close(keymaster1_dev);
-    }
-    if (keymaster0_dev) {
-        keymaster0_close(keymaster0_dev);
-    }
-    return rc;
+    return keymaster_compatibility_cryptfs_scrypt();
 }
 
 /* Create a new keymaster key and store it in this footer */
 static int keymaster_create_key(struct crypt_mnt_ftr *ftr)
 {
-    uint8_t* key = 0;
-    keymaster0_device_t *keymaster0_dev = 0;
-    keymaster1_device_t *keymaster1_dev = 0;
-
     if (ftr->keymaster_blob_size) {
         SLOGI("Already have key");
         return 0;
     }
 
-    if (keymaster_init(&keymaster0_dev, &keymaster1_dev)) {
-        SLOGE("Failed to init keymaster");
+    int rc = keymaster_create_key_for_cryptfs_scrypt(RSA_KEY_SIZE, RSA_EXPONENT,
+            KEYMASTER_CRYPTFS_RATE_LIMIT, ftr->keymaster_blob, KEYMASTER_BLOB_SIZE,
+            &ftr->keymaster_blob_size);
+    if (rc) {
+        if (ftr->keymaster_blob_size > KEYMASTER_BLOB_SIZE) {
+            SLOGE("Keymaster key blob to large)");
+            ftr->keymaster_blob_size = 0;
+        }
+        SLOGE("Failed to generate keypair");
         return -1;
     }
-
-    int rc = 0;
-    size_t key_size = 0;
-    if (keymaster1_dev) {
-        keymaster_key_param_t params[] = {
-            /* Algorithm & size specifications.  Stick with RSA for now.  Switch to AES later. */
-            keymaster_param_enum(KM_TAG_ALGORITHM, KM_ALGORITHM_RSA),
-            keymaster_param_int(KM_TAG_KEY_SIZE, RSA_KEY_SIZE),
-            keymaster_param_long(KM_TAG_RSA_PUBLIC_EXPONENT, RSA_EXPONENT),
-
-	    /* The only allowed purpose for this key is signing. */
-	    keymaster_param_enum(KM_TAG_PURPOSE, KM_PURPOSE_SIGN),
-
-            /* Padding & digest specifications. */
-            keymaster_param_enum(KM_TAG_PADDING, KM_PAD_NONE),
-            keymaster_param_enum(KM_TAG_DIGEST, KM_DIGEST_NONE),
-
-            /* Require that the key be usable in standalone mode.  File system isn't available. */
-            keymaster_param_enum(KM_TAG_BLOB_USAGE_REQUIREMENTS, KM_BLOB_STANDALONE),
-
-            /* No auth requirements, because cryptfs is not yet integrated with gatekeeper. */
-            keymaster_param_bool(KM_TAG_NO_AUTH_REQUIRED),
-
-            /* Rate-limit key usage attempts, to rate-limit brute force */
-            keymaster_param_int(KM_TAG_MIN_SECONDS_BETWEEN_OPS, KEYMASTER_CRYPTFS_RATE_LIMIT),
-        };
-        keymaster_key_param_set_t param_set = { params, sizeof(params)/sizeof(*params) };
-        keymaster_key_blob_t key_blob;
-        keymaster_error_t error = keymaster1_dev->generate_key(keymaster1_dev, &param_set,
-                                                               &key_blob,
-                                                               NULL /* characteristics */);
-        if (error != KM_ERROR_OK) {
-            SLOGE("Failed to generate keymaster1 key, error %d", error);
-            rc = -1;
-            goto out;
-        }
-
-        key = (uint8_t*)key_blob.key_material;
-        key_size = key_blob.key_material_size;
-    }
-    else if (keymaster0_dev) {
-        keymaster_rsa_keygen_params_t params;
-        memset(&params, '\0', sizeof(params));
-        params.public_exponent = RSA_EXPONENT;
-        params.modulus_size = RSA_KEY_SIZE;
-
-        if (keymaster0_dev->generate_keypair(keymaster0_dev, TYPE_RSA, &params,
-                                             &key, &key_size)) {
-            SLOGE("Failed to generate keypair");
-            rc = -1;
-            goto out;
-        }
-    } else {
-        SLOGE("Cryptfs bug: keymaster_init succeeded but didn't initialize a device");
-        rc = -1;
-        goto out;
-    }
-
-    if (key_size > KEYMASTER_BLOB_SIZE) {
-        SLOGE("Keymaster key too large for crypto footer");
-        rc = -1;
-        goto out;
-    }
-
-    memcpy(ftr->keymaster_blob, key, key_size);
-    ftr->keymaster_blob_size = key_size;
-
-out:
-    if (keymaster0_dev)
-        keymaster0_close(keymaster0_dev);
-    if (keymaster1_dev)
-        keymaster1_close(keymaster1_dev);
-    free(key);
-    return rc;
+    return 0;
 }
 
 /* This signs the given object using the keymaster key. */
@@ -292,15 +136,6 @@
                                  unsigned char **signature,
                                  size_t *signature_size)
 {
-    int rc = 0;
-    keymaster0_device_t *keymaster0_dev = 0;
-    keymaster1_device_t *keymaster1_dev = 0;
-    if (keymaster_init(&keymaster0_dev, &keymaster1_dev)) {
-        SLOGE("Failed to init keymaster");
-        rc = -1;
-        goto out;
-    }
-
     unsigned char to_sign[RSA_KEY_SIZE_BYTES];
     size_t to_sign_size = sizeof(to_sign);
     memset(to_sign, 0, RSA_KEY_SIZE_BYTES);
@@ -327,96 +162,15 @@
             // object size.  However, it's still broken (but not unusably
             // so) because we really should be using a proper deterministic
             // RSA padding function, such as PKCS1.
-            memcpy(to_sign + 1, object, min(RSA_KEY_SIZE_BYTES - 1, object_size));
+            memcpy(to_sign + 1, object, std::min((size_t)RSA_KEY_SIZE_BYTES - 1, object_size));
             SLOGI("Signing safely-padded object");
             break;
         default:
             SLOGE("Unknown KDF type %d", ftr->kdf_type);
-            rc = -1;
-            goto out;
+            return -1;
     }
-
-    if (keymaster0_dev) {
-        keymaster_rsa_sign_params_t params;
-        params.digest_type = DIGEST_NONE;
-        params.padding_type = PADDING_NONE;
-
-        rc = keymaster0_dev->sign_data(keymaster0_dev,
-                                      &params,
-                                      ftr->keymaster_blob,
-                                      ftr->keymaster_blob_size,
-                                      to_sign,
-                                      to_sign_size,
-                                      signature,
-                                      signature_size);
-        goto out;
-    } else if (keymaster1_dev) {
-        keymaster_key_blob_t key = { ftr->keymaster_blob, ftr->keymaster_blob_size };
-        keymaster_key_param_t params[] = {
-            keymaster_param_enum(KM_TAG_PADDING, KM_PAD_NONE),
-            keymaster_param_enum(KM_TAG_DIGEST, KM_DIGEST_NONE),
-        };
-        keymaster_key_param_set_t param_set = { params, sizeof(params)/sizeof(*params) };
-        keymaster_operation_handle_t op_handle;
-        keymaster_error_t error = keymaster1_dev->begin(keymaster1_dev, KM_PURPOSE_SIGN, &key,
-                                                        &param_set, NULL /* out_params */,
-                                                        &op_handle);
-        if (error == KM_ERROR_KEY_RATE_LIMIT_EXCEEDED) {
-            // Key usage has been rate-limited.  Wait a bit and try again.
-            sleep(KEYMASTER_CRYPTFS_RATE_LIMIT);
-            error = keymaster1_dev->begin(keymaster1_dev, KM_PURPOSE_SIGN, &key,
-                                          &param_set, NULL /* out_params */,
-                                          &op_handle);
-        }
-        if (error != KM_ERROR_OK) {
-            SLOGE("Error starting keymaster signature transaction: %d", error);
-            rc = -1;
-            goto out;
-        }
-
-        keymaster_blob_t input = { to_sign, to_sign_size };
-        size_t input_consumed;
-        error = keymaster1_dev->update(keymaster1_dev, op_handle, NULL /* in_params */,
-                                       &input, &input_consumed, NULL /* out_params */,
-                                       NULL /* output */);
-        if (error != KM_ERROR_OK) {
-            SLOGE("Error sending data to keymaster signature transaction: %d", error);
-            rc = -1;
-            goto out;
-        }
-        if (input_consumed != to_sign_size) {
-            // This should never happen.  If it does, it's a bug in the keymaster implementation.
-            SLOGE("Keymaster update() did not consume all data.");
-            keymaster1_dev->abort(keymaster1_dev, op_handle);
-            rc = -1;
-            goto out;
-        }
-
-        keymaster_blob_t tmp_sig;
-        error = keymaster1_dev->finish(keymaster1_dev, op_handle, NULL /* in_params */,
-                                       NULL /* verify signature */, NULL /* out_params */,
-                                       &tmp_sig);
-        if (error != KM_ERROR_OK) {
-            SLOGE("Error finishing keymaster signature transaction: %d", error);
-            rc = -1;
-            goto out;
-        }
-
-        *signature = (uint8_t*)tmp_sig.data;
-        *signature_size = tmp_sig.data_length;
-    } else {
-        SLOGE("Cryptfs bug: keymaster_init succeded but didn't initialize a device.");
-        rc = -1;
-        goto out;
-    }
-
-    out:
-        if (keymaster1_dev)
-            keymaster1_close(keymaster1_dev);
-        if (keymaster0_dev)
-            keymaster0_close(keymaster0_dev);
-
-        return rc;
+    return keymaster_sign_object_for_cryptfs_scrypt(ftr->keymaster_blob, ftr->keymaster_blob_size,
+            KEYMASTER_CRYPTFS_RATE_LIMIT, to_sign, to_sign_size, signature, signature_size);
 }
 
 /* Store password when userdata is successfully decrypted and mounted.
@@ -693,7 +447,7 @@
 
         SLOGW("upgrading crypto footer to 1.1");
 
-        pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
+        pdata = (crypt_persist_data *)malloc(CRYPT_PERSIST_DATA_SIZE);
         if (pdata == NULL) {
             SLOGE("Cannot allocate persisent data\n");
             return;
@@ -861,7 +615,7 @@
     /* If not encrypted, just allocate an empty table and initialize it */
     property_get("ro.crypto.state", encrypted_state, "");
     if (strcmp(encrypted_state, "encrypted") ) {
-        pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
+        pdata = (crypt_persist_data*)malloc(CRYPT_PERSIST_DATA_SIZE);
         if (pdata) {
             init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
             persist_data = pdata;
@@ -895,7 +649,7 @@
         return -1;
     }
 
-    pdata = malloc(crypt_ftr.persist_data_size);
+    pdata = (crypt_persist_data*)malloc(crypt_ftr.persist_data_size);
     if (pdata == NULL) {
         SLOGE("Cannot allocate memory for persistent data");
         goto err;
@@ -974,7 +728,7 @@
         return -1;
     }
 
-    pdata = malloc(crypt_ftr.persist_data_size);
+    pdata = (crypt_persist_data*)malloc(crypt_ftr.persist_data_size);
     if (pdata == NULL) {
         SLOGE("Cannot allocate persistant data");
         goto err;
@@ -1063,11 +817,12 @@
 static int load_crypto_mapping_table(struct crypt_mnt_ftr *crypt_ftr,
         const unsigned char *master_key, const char *real_blk_name,
         const char *name, int fd, const char *extra_params) {
-  _Alignas(struct dm_ioctl) char buffer[DM_CRYPT_BUF_SIZE];
+  alignas(struct dm_ioctl) char buffer[DM_CRYPT_BUF_SIZE];
   struct dm_ioctl *io;
   struct dm_target_spec *tgt;
   char *crypt_params;
   char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
+  size_t buff_offset;
   int i;
 
   io = (struct dm_ioctl *) buffer;
@@ -1080,21 +835,15 @@
   tgt->status = 0;
   tgt->sector_start = 0;
   tgt->length = crypt_ftr->fs_size;
-#ifdef CONFIG_HW_DISK_ENCRYPTION
-  if (!strcmp((char *)crypt_ftr->crypto_type_name, "aes-xts")) {
-    strlcpy(tgt->target_type, "req-crypt", DM_MAX_TYPE_NAME);
-  }
-  else {
-    strlcpy(tgt->target_type, "crypt", DM_MAX_TYPE_NAME);
-  }
-#else
   strlcpy(tgt->target_type, "crypt", DM_MAX_TYPE_NAME);
-#endif
 
   crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
   convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
-  sprintf(crypt_params, "%s %s 0 %s 0 %s", crypt_ftr->crypto_type_name,
-          master_key_ascii, real_blk_name, extra_params);
+
+  buff_offset = crypt_params - buffer;
+  snprintf(crypt_params, sizeof(buffer) - buff_offset, "%s %s 0 %s 0 %s",
+           crypt_ftr->crypto_type_name, master_key_ascii, real_blk_name,
+           extra_params);
   crypt_params += strlen(crypt_params) + 1;
   crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
   tgt->next = crypt_params - buffer;
@@ -1134,11 +883,7 @@
      */
     v = (struct dm_target_versions *) &buffer[sizeof(struct dm_ioctl)];
     while (v->next) {
-#ifdef CONFIG_HW_DISK_ENCRYPTION
-        if (! strcmp(v->name, "crypt") || ! strcmp(v->name, "req-crypt")) {
-#else
         if (! strcmp(v->name, "crypt")) {
-#endif
             /* We found the crypt driver, return the version, and get out */
             version[0] = v->version[0];
             version[1] = v->version[1];
@@ -1161,7 +906,7 @@
   int err;
   int retval = -1;
   int version[3];
-  char *extra_params;
+  const char *extra_params;
   int load_count;
 
   if ((fd = open("/dev/device-mapper", O_RDWR|O_CLOEXEC)) < 0 ) {
@@ -1223,7 +968,7 @@
   return retval;
 }
 
-static int delete_crypto_blk_dev(char *name)
+static int delete_crypto_blk_dev(const char *name)
 {
   int fd;
   char buffer[DM_CRYPT_BUF_SIZE];
@@ -1405,6 +1150,8 @@
       SLOGE("encrypt_master_key: crypto_scrypt failed");
     }
 
+    EVP_CIPHER_CTX_cleanup(&e_ctx);
+
     return 0;
 }
 
@@ -1448,12 +1195,14 @@
   /* Copy intermediate key if needed by params */
   if (intermediate_key && intermediate_key_size) {
     *intermediate_key = (unsigned char*) malloc(KEY_LEN_BYTES);
-    if (intermediate_key) {
+    if (*intermediate_key) {
       memcpy(*intermediate_key, ikey, KEY_LEN_BYTES);
       *intermediate_key_size = KEY_LEN_BYTES;
     }
   }
 
+  EVP_CIPHER_CTX_cleanup(&d_ctx);
+
   return 0;
 }
 
@@ -1491,7 +1240,7 @@
     return ret;
 }
 
-static int create_encrypted_random_key(char *passwd, unsigned char *master_key, unsigned char *salt,
+static int create_encrypted_random_key(const char *passwd, unsigned char *master_key, unsigned char *salt,
         struct crypt_mnt_ftr *crypt_ftr) {
     int fd;
     unsigned char key_buf[KEY_LEN_BYTES];
@@ -1552,7 +1301,6 @@
     return rc;
 }
 
-#define DATA_PREP_TIMEOUT 1000
 static int prep_data_fs(void)
 {
     int i;
@@ -1566,17 +1314,9 @@
     SLOGD("Just triggered post_fs_data\n");
 
     /* Wait a max of 50 seconds, hopefully it takes much less */
-    for (i=0; i<DATA_PREP_TIMEOUT; i++) {
-        char p[PROPERTY_VALUE_MAX];
-
-        property_get("vold.post_fs_data_done", p, "0");
-        if (*p == '1') {
-            break;
-        } else {
-            usleep(50000);
-        }
-    }
-    if (i == DATA_PREP_TIMEOUT) {
+    if (!android::base::WaitForProperty("vold.post_fs_data_done",
+                                        "1",
+                                        std::chrono::seconds(50))) {
         /* Ugh, we failed to prep /data in time.  Bail. */
         SLOGE("post_fs_data timed out!\n");
         return -1;
@@ -1733,11 +1473,11 @@
             return -1;
         }
 
-        property_set("vold.decrypt", "trigger_load_persist_props");
         /* Create necessary paths on /data */
         if (prep_data_fs()) {
             return -1;
         }
+        property_set("vold.decrypt", "trigger_load_persist_props");
 
         /* startup service classes main and late_start */
         property_set("vold.decrypt", "trigger_restart_framework");
@@ -1766,7 +1506,7 @@
     return cryptfs_restart_internal(1);
 }
 
-static int do_crypto_complete(char *mount_point)
+static int do_crypto_complete(const char *mount_point)
 {
   struct crypt_mnt_ftr crypt_ftr;
   char encrypted_state[PROPERTY_VALUE_MAX];
@@ -1823,7 +1563,7 @@
 }
 
 static int test_mount_encrypted_fs(struct crypt_mnt_ftr* crypt_ftr,
-                                   char *passwd, char *mount_point, char *label)
+                                   const char *passwd, const char *mount_point, const char *label)
 {
   /* Allocate enough space for a 256 bit key, but we may use less */
   unsigned char decrypted_master_key[32];
@@ -1836,6 +1576,9 @@
   int upgrade = 0;
   unsigned char* intermediate_key = 0;
   size_t intermediate_key_size = 0;
+  int N = 1 << crypt_ftr->N_factor;
+  int r = 1 << crypt_ftr->r_factor;
+  int p = 1 << crypt_ftr->p_factor;
 
   SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr->fs_size);
   orig_failed_decrypt_count = crypt_ftr->failed_decrypt_count;
@@ -1851,14 +1594,6 @@
 
   fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
 
-#ifdef CONFIG_HW_DISK_ENCRYPTION
-  if (!strcmp((char *)crypt_ftr->crypto_type_name, "aes-xts")) {
-    if(!set_hw_device_encryption_key(passwd, (char*) crypt_ftr->crypto_type_name)) {
-      SLOGE("Hardware encryption key does not match");
-    }
-  }
-#endif
-
   // Create crypto block device - all (non fatal) code paths
   // need it
   if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key,
@@ -1871,9 +1606,6 @@
   /* Work out if the problem is the password or the data */
   unsigned char scrypted_intermediate_key[sizeof(crypt_ftr->
                                                  scrypted_intermediate_key)];
-  int N = 1 << crypt_ftr->N_factor;
-  int r = 1 << crypt_ftr->r_factor;
-  int p = 1 << crypt_ftr->p_factor;
 
   rc = crypto_scrypt(intermediate_key, intermediate_key_size,
                      crypt_ftr->salt, sizeof(crypt_ftr->salt),
@@ -1889,7 +1621,8 @@
   } else {
     /* Try mounting the file system anyway, just in case the problem's with
      * the footer, not the key. */
-    sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point);
+    snprintf(tmp_mount_point, sizeof(tmp_mount_point), "%s/tmp_mnt",
+             mount_point);
     mkdir(tmp_mount_point, 0755);
     if (fs_mgr_do_mount(fstab, DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) {
       SLOGE("Error temp mounting decrypted block device\n");
@@ -2029,7 +1762,7 @@
     return 0;
 }
 
-int cryptfs_check_passwd(char *passwd)
+int cryptfs_check_passwd(const char *passwd)
 {
     SLOGI("cryptfs_check_passwd");
     if (e4crypt_is_native()) {
@@ -2185,6 +1918,19 @@
     int rc = -1;
 
     if (type == EXT4_FS) {
+#ifdef TARGET_USES_MKE2FS
+        args[0] = "/system/bin/mke2fs";
+        args[1] = "-M";
+        args[2] = "/data";
+        args[3] = "-b";
+        args[4] = "4096";
+        args[5] = "-t";
+        args[6] = "ext4";
+        args[7] = crypto_blkdev;
+        snprintf(size_str, sizeof(size_str), "%" PRId64, size / (4096 / 512));
+        args[8] = size_str;
+        num_args = 9;
+#else
         args[0] = "/system/bin/make_ext4fs";
         args[1] = "-a";
         args[2] = "/data";
@@ -2193,6 +1939,7 @@
         args[4] = size_str;
         args[5] = crypto_blkdev;
         num_args = 6;
+#endif
         SLOGI("Making empty filesystem with command %s %s %s %s %s %s\n",
               args[0], args[1], args[2], args[3], args[4], args[5]);
     } else if (type == F2FS_FS) {
@@ -2238,11 +1985,7 @@
 /* aligned 32K writes tends to make flash happy.
  * SD card association recommends it.
  */
-#ifndef CONFIG_HW_DISK_ENCRYPTION
 #define BLOCKS_AT_A_TIME 8
-#else
-#define BLOCKS_AT_A_TIME 1024
-#endif
 
 struct encryptGroupsData
 {
@@ -2373,13 +2116,13 @@
     off64_t ret;
     int rc = -1;
 
-    data->buffer = malloc(info.block_size * BLOCKS_AT_A_TIME);
+    data->buffer = (char *)malloc(info.block_size * BLOCKS_AT_A_TIME);
     if (!data->buffer) {
         SLOGE("Failed to allocate crypto buffer");
         goto errout;
     }
 
-    block_bitmap = malloc(info.block_size);
+    block_bitmap = (u8 *)malloc(info.block_size);
     if (!block_bitmap) {
         SLOGE("failed to allocate block bitmap");
         goto errout;
@@ -2389,8 +2132,8 @@
         SLOGI("Encrypting group %d", i);
 
         u32 first_block = aux_info.first_data_block + i * info.blocks_per_group;
-        u32 block_count = min(info.blocks_per_group,
-                             aux_info.len_blocks - first_block);
+        u32 block_count = std::min(info.blocks_per_group,
+                                   (u32)(aux_info.len_blocks - first_block));
 
         off64_t offset = (u64)info.block_size
                          * aux_info.bg_desc[i].bg_block_bitmap;
@@ -2462,6 +2205,8 @@
     u32 i;
     struct encryptGroupsData data;
     int rc; // Can't initialize without causing warning -Wclobbered
+    struct timespec time_started = {0};
+    int retries = RETRY_MOUNT_ATTEMPTS;
 
     if (previously_encrypted_upto > *size_already_done) {
         SLOGD("Not fast encrypting since resuming part way through");
@@ -2480,7 +2225,6 @@
     }
 
     // Wait until the block device appears.  Re-use the mount retry values since it is reasonable.
-    int retries = RETRY_MOUNT_ATTEMPTS;
     while ((data.cryptofd = open(crypto_blkdev, O_WRONLY|O_CLOEXEC)) < 0) {
         if (--retries) {
             SLOGE("Error opening crypto_blkdev %s for ext4 inplace encrypt. err=%d(%s), retrying\n",
@@ -2520,7 +2264,6 @@
     data.one_pct = data.tot_used_blocks / 100;
     data.cur_pct = 0;
 
-    struct timespec time_started = {0};
     if (clock_gettime(CLOCK_MONOTONIC, &time_started)) {
         SLOGW("Error getting time at start");
         // Note - continue anyway - we'll run with 0
@@ -2638,7 +2381,7 @@
     data.time_started = time(NULL);
     data.remaining_time = -1;
 
-    data.buffer = malloc(f2fs_info->block_size);
+    data.buffer = (char *)malloc(f2fs_info->block_size);
     if (!data.buffer) {
         SLOGE("Failed to allocate crypto buffer");
         goto errout;
@@ -2934,7 +2677,7 @@
     return rc;
 }
 
-int cryptfs_enable_internal(char *howarg, int crypt_type, char *passwd,
+int cryptfs_enable_internal(char *howarg, int crypt_type, const char *passwd,
                             int no_ui)
 {
     int how = 0;
@@ -2949,6 +2692,8 @@
     int num_vols;
     off64_t previously_encrypted_upto = 0;
     bool rebootEncryption = false;
+    bool onlyCreateHeader = false;
+    int fd = -1;
 
     if (!strcmp(howarg, "wipe")) {
       how = CRYPTO_ENABLE_WIPE;
@@ -2999,7 +2744,7 @@
     fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
 
     /* Get the size of the real block device */
-    int fd = open(real_blkdev, O_RDONLY|O_CLOEXEC);
+    fd = open(real_blkdev, O_RDONLY|O_CLOEXEC);
     if (fd == -1) {
         SLOGE("Cannot open block device %s\n", real_blkdev);
         goto error_unencrypted;
@@ -3048,7 +2793,6 @@
     /* no_ui means we are being called from init, not settings.
        Now we always reboot from settings, so !no_ui means reboot
      */
-    bool onlyCreateHeader = false;
     if (!no_ui) {
         /* Try fallback, which is to reboot and try there */
         onlyCreateHeader = true;
@@ -3109,23 +2853,7 @@
             crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
         }
         crypt_ftr.crypt_type = crypt_type;
-#ifndef CONFIG_HW_DISK_ENCRYPTION
         strlcpy((char *)crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256", MAX_CRYPTO_TYPE_NAME_LEN);
-#else
-        strlcpy((char *)crypt_ftr.crypto_type_name, "aes-xts", MAX_CRYPTO_TYPE_NAME_LEN);
-
-        rc = clear_hw_device_encryption_key();
-        if (!rc) {
-          SLOGE("Error clearing device encryption hardware key. rc = %d", rc);
-        }
-
-        rc = set_hw_device_encryption_key(passwd,
-                                          (char*) crypt_ftr.crypto_type_name);
-        if (!rc) {
-          SLOGE("Error initializing device encryption hardware key. rc = %d", rc);
-          goto error_shutting_down;
-        }
-#endif
 
         /* Make an encrypted master key */
         if (create_encrypted_random_key(onlyCreateHeader ? DEFAULT_PASSWORD : passwd,
@@ -3150,7 +2878,7 @@
          * If none, create a valid empty table and save that.
          */
         if (!persist_data) {
-           pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
+            pdata = (crypt_persist_data *)malloc(CRYPT_PERSIST_DATA_SIZE);
            if (pdata) {
                init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
                persist_data = pdata;
@@ -3263,8 +2991,12 @@
         if (!strcmp(value, "1")) {
             /* wipe data if encryption failed */
             SLOGE("encryption failed - rebooting into recovery to wipe data\n");
-            if (!write_bootloader_message("--wipe_data\n--reason=cryptfs_enable_internal\n")) {
-                SLOGE("could not write bootloader message\n");
+            std::string err;
+            const std::vector<std::string> options = {
+                "--wipe_data\n--reason=cryptfs_enable_internal\n"
+            };
+            if (!write_bootloader_message(options, &err)) {
+                SLOGE("could not write bootloader message: %s", err.c_str());
             }
             cryptfs_reboot(recovery);
         } else {
@@ -3359,21 +3091,6 @@
     /* save the key */
     put_crypt_ftr_and_key(&crypt_ftr);
 
-#ifdef CONFIG_HW_DISK_ENCRYPTION
-    if (!strcmp((char *)crypt_ftr.crypto_type_name, "aes-xts")) {
-        if (crypt_type == CRYPT_TYPE_DEFAULT) {
-            int rc = update_hw_device_encryption_key(DEFAULT_PASSWORD, (char*) crypt_ftr.crypto_type_name);
-            SLOGD("Update hardware encryption key to default for crypt_type: %d. rc = %d", crypt_type, rc);
-            if (!rc)
-                return -1;
-        } else {
-            int rc = update_hw_device_encryption_key(newpw, (char*) crypt_ftr.crypto_type_name);
-            SLOGD("Update hardware encryption key for crypt_type: %d. rc = %d", crypt_type, rc);
-            if (!rc)
-                return -1;
-        }
-    }
-#endif
     return 0;
 }
 
@@ -3856,8 +3573,9 @@
                               ftr);
 }
 
-const char* cryptfs_get_file_encryption_mode()
+void cryptfs_get_file_encryption_modes(const char **contents_mode_ret,
+                                       const char **filenames_mode_ret)
 {
     struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
-    return fs_mgr_get_file_encryption_mode(rec);
+    fs_mgr_get_file_encryption_modes(rec, contents_mode_ret, filenames_mode_ret);
 }
diff --git a/cryptfs.h b/cryptfs.h
index 11d9bb7..352a576 100644
--- a/cryptfs.h
+++ b/cryptfs.h
@@ -191,32 +191,32 @@
 /* Return values for cryptfs_crypto_complete */
 #define CRYPTO_COMPLETE_NOT_ENCRYPTED  1
 #define CRYPTO_COMPLETE_ENCRYPTED      0
-#define CRYPTO_COMPLETE_BAD_METADATA  -1
-#define CRYPTO_COMPLETE_PARTIAL       -2
-#define CRYPTO_COMPLETE_INCONSISTENT  -3
-#define CRYPTO_COMPLETE_CORRUPT       -4
+#define CRYPTO_COMPLETE_BAD_METADATA  (-1)
+#define CRYPTO_COMPLETE_PARTIAL       (-2)
+#define CRYPTO_COMPLETE_INCONSISTENT  (-3)
+#define CRYPTO_COMPLETE_CORRUPT       (-4)
 
 /* Return values for cryptfs_enable_inplace*() */
 #define ENABLE_INPLACE_OK 0
-#define ENABLE_INPLACE_ERR_OTHER -1
-#define ENABLE_INPLACE_ERR_DEV -2  /* crypto_blkdev issue */
+#define ENABLE_INPLACE_ERR_OTHER (-1)
+#define ENABLE_INPLACE_ERR_DEV (-2)  /* crypto_blkdev issue */
 
 /* Return values for cryptfs_getfield */
 #define CRYPTO_GETFIELD_OK                   0
-#define CRYPTO_GETFIELD_ERROR_NO_FIELD      -1
-#define CRYPTO_GETFIELD_ERROR_OTHER         -2
-#define CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL -3
+#define CRYPTO_GETFIELD_ERROR_NO_FIELD      (-1)
+#define CRYPTO_GETFIELD_ERROR_OTHER         (-2)
+#define CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL (-3)
 
 /* Return values for cryptfs_setfield */
 #define CRYPTO_SETFIELD_OK                    0
-#define CRYPTO_SETFIELD_ERROR_OTHER          -1
-#define CRYPTO_SETFIELD_ERROR_FIELD_TOO_LONG -2
-#define CRYPTO_SETFIELD_ERROR_VALUE_TOO_LONG -3
+#define CRYPTO_SETFIELD_ERROR_OTHER          (-1)
+#define CRYPTO_SETFIELD_ERROR_FIELD_TOO_LONG (-2)
+#define CRYPTO_SETFIELD_ERROR_VALUE_TOO_LONG (-3)
 
 /* Return values for persist_del_key */
 #define PERSIST_DEL_KEY_OK                 0
-#define PERSIST_DEL_KEY_ERROR_OTHER       -1
-#define PERSIST_DEL_KEY_ERROR_NO_FIELD    -2
+#define PERSIST_DEL_KEY_ERROR_OTHER       (-1)
+#define PERSIST_DEL_KEY_ERROR_NO_FIELD    (-2)
 
 #ifdef __cplusplus
 extern "C" {
@@ -228,7 +228,7 @@
                           unsigned char *ikey, void *params);
 
   int cryptfs_crypto_complete(void);
-  int cryptfs_check_passwd(char *pw);
+  int cryptfs_check_passwd(const char *pw);
   int cryptfs_verify_passwd(char *newpw);
   int cryptfs_restart(void);
   int cryptfs_enable(char *flag, int type, char *passwd, int no_ui);
@@ -252,7 +252,8 @@
                              unsigned char* master_key);
   int cryptfs_set_password(struct crypt_mnt_ftr* ftr, const char* password,
                            const unsigned char* master_key);
-  const char* cryptfs_get_file_encryption_mode();
+  void cryptfs_get_file_encryption_modes(const char **contents_mode_ret,
+                                         const char **filenames_mode_ret);
 
 #ifdef __cplusplus
 }
diff --git a/fs/Ext4.cpp b/fs/Ext4.cpp
index 0bd5b0c..0670bb5 100644
--- a/fs/Ext4.cpp
+++ b/fs/Ext4.cpp
@@ -55,7 +55,11 @@
 namespace ext4 {
 
 static const char* kResizefsPath = "/system/bin/resize2fs";
+#ifdef TARGET_USES_MKE2FS
+static const char* kMkfsPath = "/system/bin/mke2fs";
+#else
 static const char* kMkfsPath = "/system/bin/make_ext4fs";
+#endif
 static const char* kFsckPath = "/system/bin/e2fsck";
 
 bool IsSupported() {
@@ -165,6 +169,25 @@
         const std::string& target) {
     std::vector<std::string> cmd;
     cmd.push_back(kMkfsPath);
+
+#ifdef TARGET_USES_MKE2FS
+    cmd.push_back("-b");
+    cmd.push_back("4096");
+
+    cmd.push_back("-t");
+    cmd.push_back("ext4");
+
+    cmd.push_back("-M");
+    cmd.push_back(target);
+
+    cmd.push_back("-O");
+    cmd.push_back("^has_journal");
+
+    cmd.push_back(source);
+
+    if (numSectors)
+        cmd.push_back(StringPrintf("%lu", numSectors * (4096 / 512)));
+#else
     cmd.push_back("-J");
 
     cmd.push_back("-a");
@@ -178,6 +201,7 @@
     // Always generate a real UUID
     cmd.push_back("-u");
     cmd.push_back(source);
+#endif
 
     return ForkExecvp(cmd);
 }
diff --git a/fs/Vfat.cpp b/fs/Vfat.cpp
index 38681c9..1803c4b 100644
--- a/fs/Vfat.cpp
+++ b/fs/Vfat.cpp
@@ -139,7 +139,7 @@
     flags |= (ro ? MS_RDONLY : 0);
     flags |= (remount ? MS_REMOUNT : 0);
 
-    sprintf(mountData,
+    snprintf(mountData, sizeof(mountData),
             "utf8,uid=%d,gid=%d,fmask=%o,dmask=%o,shortname=mixed",
             ownerUid, ownerGid, permMask, permMask);
 
diff --git a/main.cpp b/main.cpp
index 68477ac..4657377 100644
--- a/main.cpp
+++ b/main.cpp
@@ -117,9 +117,6 @@
         exit(1);
     }
 
-    coldboot("/sys/block");
-//    coldboot("/sys/class/switch");
-
     /*
      * Now that we're up, we can respond to commands
      */
@@ -137,9 +134,13 @@
     // a deadlock between vold and init (see b/34278978 for details)
     property_set("vold.has_adoptable", has_adoptable ? "1" : "0");
 
+    // Do coldboot here so it won't block booting,
+    // also the cold boot is needed in case we have flash drive
+    // connected before Vold launched
+    coldboot("/sys/block");
     // Eventually we'll become the monitoring thread
     while(1) {
-        sleep(1000);
+        pause();
     }
 
     LOG(ERROR) << "Vold exiting";
@@ -191,7 +192,7 @@
         if (de->d_type != DT_DIR && lvl > 0)
             continue;
 
-        fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
+        fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
         if(fd < 0)
             continue;
 
@@ -214,10 +215,9 @@
 }
 
 static int process_config(VolumeManager *vm, bool* has_adoptable) {
-    std::string path(android::vold::DefaultFstabPath());
-    fstab = fs_mgr_read_fstab(path.c_str());
+    fstab = fs_mgr_read_fstab_default();
     if (!fstab) {
-        PLOG(ERROR) << "Failed to open default fstab " << path;
+        PLOG(ERROR) << "Failed to open default fstab";
         return -1;
     }
 
diff --git a/tests/Android.mk b/tests/Android.mk
index f974e7f..416e621 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -7,11 +7,7 @@
 LOCAL_C_INCLUDES := \
     system/core/fs_mgr/include
 
-LOCAL_SHARED_LIBRARIES := \
-    liblog \
-    libcrypto \
-
-LOCAL_STATIC_LIBRARIES := libvold
+LOCAL_STATIC_LIBRARIES := libselinux libvold liblog libcrypto
 LOCAL_SRC_FILES := VolumeManager_test.cpp
 LOCAL_MODULE := vold_tests
 LOCAL_MODULE_TAGS := eng tests
diff --git a/vdc.cpp b/vdc.cpp
index 4eb26cd..7ab5fcc 100644
--- a/vdc.cpp
+++ b/vdc.cpp
@@ -30,6 +30,7 @@
 #include <sys/types.h>
 #include <sys/un.h>
 
+#include <android-base/logging.h>
 #include <android-base/stringprintf.h>
 
 #include <cutils/sockets.h>
@@ -48,6 +49,13 @@
 
     progname = argv[0];
 
+    if (getppid() == 1) {
+        // If init is calling us then it's during boot and we should log to kmsg
+        android::base::InitLogging(argv, &android::base::KernelLogger);
+    } else {
+        android::base::InitLogging(argv, &android::base::StderrLogger);
+    }
+
     wait_for_socket = argc > 1 && strcmp(argv[1], "--wait") == 0;
     if (wait_for_socket) {
         argv++;
@@ -68,7 +76,7 @@
                                  ANDROID_SOCKET_NAMESPACE_RESERVED,
                                  SOCK_STREAM)) < 0) {
         if (!wait_for_socket) {
-            fprintf(stdout, "Error connecting to %s: %s\n", sockname, strerror(errno));
+            PLOG(ERROR) << "Error connecting to " << sockname;
             exit(4);
         } else {
             usleep(10000);
@@ -101,7 +109,7 @@
     }
 
     if (TEMP_FAILURE_RETRY(write(sock, cmd.c_str(), cmd.length() + 1)) < 0) {
-        fprintf(stderr, "Failed to write command: %s\n", strerror(errno));
+        PLOG(ERROR) << "Failed to write command";
         return errno;
     }
 
@@ -113,7 +121,7 @@
     int timeout = kCommandTimeoutMs;
 
     if (stop_after_seq == 0) {
-        fprintf(stderr, "Connected to vold\n");
+        LOG(INFO) << "Connected to vold";
         timeout = -1;
     }
 
@@ -121,25 +129,25 @@
         struct pollfd poll_sock = { sock, POLLIN, 0 };
         int rc = TEMP_FAILURE_RETRY(poll(&poll_sock, 1, timeout));
         if (rc == 0) {
-            fprintf(stderr, "Timeout waiting for %d\n", stop_after_seq);
+            LOG(ERROR) << "Timeout waiting for " << stop_after_seq;
             return ETIMEDOUT;
         } else if (rc < 0) {
-            fprintf(stderr, "Failed during poll: %s\n", strerror(errno));
+            PLOG(ERROR) << "Failed during poll";
             return errno;
         }
 
         if (!(poll_sock.revents & POLLIN)) {
-            fprintf(stderr, "No data; trying again\n");
+            LOG(INFO) << "No data; trying again";
             continue;
         }
 
         memset(buffer, 0, sizeof(buffer));
         rc = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer)));
         if (rc == 0) {
-            fprintf(stderr, "Lost connection, did vold crash?\n");
+            LOG(ERROR) << "Lost connection, did vold crash?";
             return ECONNRESET;
         } else if (rc < 0) {
-            fprintf(stderr, "Error reading data: %s\n", strerror(errno));
+            PLOG(ERROR) << "Error reading data";
             return errno;
         }
 
@@ -169,6 +177,5 @@
 }
 
 static void usage(char *progname) {
-    fprintf(stderr,
-            "Usage: %s [--wait] <monitor>|<cmd> [arg1] [arg2...]\n", progname);
+    LOG(INFO) << "Usage: " << progname << " [--wait] <monitor>|<cmd> [arg1] [arg2...]";
 }