vold ext4/f2fs: do not use dirsync if we're mounting adopted storage

Change-Id: I6f8ca75385c2d4080b1430c0b6545b61e6632e2c

Fix mounting ext4 adopted storage

Change-Id: I36cb858b96968a949632017874addb278679b3a4
diff --git a/fs/Ext4.cpp b/fs/Ext4.cpp
index aef20a6..cce418e 100644
--- a/fs/Ext4.cpp
+++ b/fs/Ext4.cpp
@@ -122,7 +122,7 @@
 }
 
 status_t Mount(const std::string& source, const std::string& target, bool ro, bool remount,
-               bool executable, const std::string& opts /* = "" */, bool portable) {
+               bool executable, const std::string& opts /* = "" */, bool trusted, bool portable) {
     int rc;
     unsigned long flags;
 
@@ -138,7 +138,12 @@
     const char* c_target = target.c_str();
     const char* c_data = data.c_str();
 
-    flags = MS_NOATIME | MS_NODEV | MS_NOSUID | MS_DIRSYNC;
+    flags = MS_NOATIME | MS_NODEV | MS_NOSUID;
+
+    // Only use MS_DIRSYNC if we're not mounting adopted storage
+    if (!trusted) {
+        flags |= MS_DIRSYNC;
+    }
 
     flags |= (executable ? 0 : MS_NOEXEC);
     flags |= (ro ? MS_RDONLY : 0);
diff --git a/fs/Ext4.h b/fs/Ext4.h
index c1c14e8..ad2c966 100644
--- a/fs/Ext4.h
+++ b/fs/Ext4.h
@@ -29,7 +29,8 @@
 
 status_t Check(const std::string& source, const std::string& target, bool trusted);
 status_t Mount(const std::string& source, const std::string& target, bool ro, bool remount,
-               bool executable, const std::string& opts = "", bool portable = false);
+               bool executable, const std::string& opts = "", bool trusted = false,
+               bool portable = false);
 status_t Format(const std::string& source, unsigned long numSectors, const std::string& target);
 status_t Resize(const std::string& source, unsigned long numSectors);
 
diff --git a/fs/F2fs.cpp b/fs/F2fs.cpp
index dc828db..cf78123 100644
--- a/fs/F2fs.cpp
+++ b/fs/F2fs.cpp
@@ -53,8 +53,8 @@
     return ForkExecvp(cmd, nullptr, trusted ? sFsckContext : sFsckUntrustedContext);
 }
 
-status_t Mount(const std::string& source, const std::string& target, const std::string& opts /* = "" */, bool portable) {
-
+status_t Mount(const std::string& source, const std::string& target,
+        const std::string& opts /* = "" */, bool trusted, bool portable) {
     std::string data(opts);
 
     if (portable) {
@@ -67,7 +67,13 @@
     const char* c_source = source.c_str();
     const char* c_target = target.c_str();
     const char* c_data = data.c_str();
-    unsigned long flags = MS_NOATIME | MS_NODEV | MS_NOSUID | MS_DIRSYNC;
+
+    unsigned long flags = MS_NOATIME | MS_NODEV | MS_NOSUID;
+
+    // Only use MS_DIRSYNC if we're not mounting adopted storage
+    if (!trusted) {
+        flags |= MS_DIRSYNC;
+    }
 
     int res = mount(c_source, c_target, "f2fs", flags, c_data);
     if (portable && res == 0) {
diff --git a/fs/F2fs.h b/fs/F2fs.h
index 1ad8263..ecfc0c7 100644
--- a/fs/F2fs.h
+++ b/fs/F2fs.h
@@ -28,7 +28,9 @@
 bool IsSupported();
 
 status_t Check(const std::string& source, bool trusted);
-status_t Mount(const std::string& source, const std::string& target, const std::string& opts = "", bool portable = false);
+status_t Mount(const std::string& source, const std::string& target,
+        const std::string& opts = "", bool trusted = false,
+        bool portable = false);
 status_t Format(const std::string& source);
 
 }  // namespace f2fs
diff --git a/model/PrivateVolume.cpp b/model/PrivateVolume.cpp
index 35613b2..c794a8c 100644
--- a/model/PrivateVolume.cpp
+++ b/model/PrivateVolume.cpp
@@ -163,7 +163,7 @@
             return -EIO;
         }
 
-        if (ext4::Mount(mDmDevPath, mPath, false, false, true)) {
+        if (ext4::Mount(mDmDevPath, mPath, false, false, true, "", true)) {
             PLOG(ERROR) << getId() << " failed to mount";
             return -EIO;
         }
@@ -177,7 +177,7 @@
             return -EIO;
         }
 
-        if (f2fs::Mount(mDmDevPath, mPath, "")) {
+        if (f2fs::Mount(mDmDevPath, mPath, "", true)) {
             PLOG(ERROR) << getId() << " failed to mount";
             return -EIO;
         }
diff --git a/model/PublicVolume.cpp b/model/PublicVolume.cpp
index db5b9ba..d2bc5e3 100644
--- a/model/PublicVolume.cpp
+++ b/model/PublicVolume.cpp
@@ -159,9 +159,10 @@
         ret = exfat::Mount(mDevPath, mRawPath, AID_ROOT,
                  (isVisible ? AID_MEDIA_RW : AID_EXTERNAL_STORAGE), 0007);
     } else if (mFsType == "ext4") {
-        ret = ext4::Mount(mDevPath, mRawPath, false, false, true, mMntOpts, true);
+        ret = ext4::Mount(mDevPath, mRawPath, false, false, true, mMntOpts,
+                false, true);
     } else if (mFsType == "f2fs") {
-        ret = f2fs::Mount(mDevPath, mRawPath, mMntOpts, true);
+        ret = f2fs::Mount(mDevPath, mRawPath, mMntOpts, false, true);
     } else if (mFsType == "ntfs") {
         ret = ntfs::Mount(mDevPath, mRawPath, AID_ROOT,
                  (isVisible ? AID_MEDIA_RW : AID_EXTERNAL_STORAGE), 0007);