Merge "Don't abort the encryption process if an internal volume is present but unmounted."
diff --git a/Volume.cpp b/Volume.cpp
index 40a3669..685cb35 100644
--- a/Volume.cpp
+++ b/Volume.cpp
@@ -589,7 +589,7 @@
     if (getState() != Volume::State_Mounted) {
         SLOGE("Volume %s unmount request when not mounted", getLabel());
         errno = EINVAL;
-        return -1;
+        return UNMOUNT_NOT_MOUNTED_ERR;
     }
 
     setState(Volume::State_Unmounting);
diff --git a/VolumeManager.cpp b/VolumeManager.cpp
index 071e027..c21eab3 100644
--- a/VolumeManager.cpp
+++ b/VolumeManager.cpp
@@ -1192,7 +1192,7 @@
         SLOGW("Attempt to unmount volume which isn't mounted (%d)\n",
              v->getState());
         errno = EBUSY;
-        return -1;
+        return UNMOUNT_NOT_MOUNTED_ERR;
     }
 
     cleanupAsec(v, force);
diff --git a/VolumeManager.h b/VolumeManager.h
index 48bb59a..706271d 100644
--- a/VolumeManager.h
+++ b/VolumeManager.h
@@ -137,6 +137,7 @@
 
 extern "C" {
 #endif /* __cplusplus */
+#define UNMOUNT_NOT_MOUNTED_ERR -2
     int vold_unmountVol(const char *label);
     int vold_getNumDirectVolumes(void);
     int vold_getDirectVolumeList(struct volume_info *v);
diff --git a/cryptfs.c b/cryptfs.c
index 8b5f523..0d3c681 100644
--- a/cryptfs.c
+++ b/cryptfs.c
@@ -1062,7 +1062,7 @@
     unsigned long mnt_flags, nr_sec;
     unsigned char master_key[KEY_LEN_BYTES], decrypted_master_key[KEY_LEN_BYTES];
     unsigned char salt[SALT_LEN];
-    int rc=-1, fd, i;
+    int rc=-1, fd, i, ret;
     struct crypt_mnt_ftr crypt_ftr, sd_crypt_ftr;;
     char tmpfs_options[PROPERTY_VALUE_MAX];
     char encrypted_state[PROPERTY_VALUE_MAX];
@@ -1141,7 +1141,10 @@
             }
             close(fd);
 
-            if (vold_unmountVol(vol_list[i].label)) {
+            ret=vold_unmountVol(vol_list[i].label);
+            if ((ret < 0) && (ret != UNMOUNT_NOT_MOUNTED_ERR)) {
+                /* -2 is returned when the device exists but is not currently mounted.
+                 * ignore the error and continue. */
                 SLOGE("Failed to unmount volume %s\n", vol_list[i].label);
                 goto error_unencrypted;
             }