Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 17 | #include "EmulatedVolume.h" |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 18 | |
| 19 | #include "AppFuseUtil.h" |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 20 | #include "Utils.h" |
Martijn Coenen | 73e3010 | 2022-07-12 08:11:02 +0000 | [diff] [blame] | 21 | #include "VolumeBase.h" |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 22 | #include "VolumeManager.h" |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 23 | |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 24 | #include <android-base/logging.h> |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 25 | #include <android-base/properties.h> |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 26 | #include <android-base/scopeguard.h> |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 27 | #include <android-base/stringprintf.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 28 | #include <cutils/fs.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 29 | #include <private/android_filesystem_config.h> |
Jeff Sharkey | 7bdf4d5 | 2017-09-18 14:47:10 -0600 | [diff] [blame] | 30 | #include <utils/Timers.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 31 | |
| 32 | #include <fcntl.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <sys/mount.h> |
| 35 | #include <sys/stat.h> |
Elliott Hughes | 0e08e84 | 2017-05-18 09:08:24 -0700 | [diff] [blame] | 36 | #include <sys/sysmacros.h> |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 37 | #include <sys/types.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 38 | #include <sys/wait.h> |
Omar Eissa | fedaddb | 2025-03-12 16:09:16 +0000 | [diff] [blame] | 39 | #include <android_vold_flags.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 40 | |
Dan Albert | ae9e890 | 2015-03-16 10:35:17 -0700 | [diff] [blame] | 41 | using android::base::StringPrintf; |
Omar Eissa | fedaddb | 2025-03-12 16:09:16 +0000 | [diff] [blame] | 42 | namespace flags = android::vold::flags; |
| 43 | |
Dan Albert | ae9e890 | 2015-03-16 10:35:17 -0700 | [diff] [blame] | 44 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 45 | namespace android { |
| 46 | namespace vold { |
| 47 | |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 48 | static const char* kSdcardFsPath = "/system/bin/sdcard"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 49 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 50 | EmulatedVolume::EmulatedVolume(const std::string& rawPath, int userId) |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 51 | : VolumeBase(Type::kEmulated) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 52 | setId(StringPrintf("emulated;%u", userId)); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 53 | mRawPath = rawPath; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 54 | mLabel = "emulated"; |
Martijn Coenen | fd7362d | 2019-12-11 14:57:59 +0100 | [diff] [blame] | 55 | mFuseMounted = false; |
Daniel Rosenberg | f36bddd | 2020-05-11 22:58:42 -0700 | [diff] [blame] | 56 | mUseSdcardFs = IsSdcardfsUsed(); |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 57 | mAppDataIsolationEnabled = base::GetBoolProperty(kVoldAppDataIsolationEnabled, false); |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 60 | EmulatedVolume::EmulatedVolume(const std::string& rawPath, dev_t device, const std::string& fsUuid, |
| 61 | int userId) |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 62 | : VolumeBase(Type::kEmulated) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 63 | setId(StringPrintf("emulated:%u,%u;%u", major(device), minor(device), userId)); |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 64 | mRawPath = rawPath; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 65 | mLabel = fsUuid; |
Greg Kaiser | 5298ccc | 2019-12-12 05:41:46 -0800 | [diff] [blame] | 66 | mFuseMounted = false; |
Daniel Rosenberg | f36bddd | 2020-05-11 22:58:42 -0700 | [diff] [blame] | 67 | mUseSdcardFs = IsSdcardfsUsed(); |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 68 | mAppDataIsolationEnabled = base::GetBoolProperty(kVoldAppDataIsolationEnabled, false); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 71 | EmulatedVolume::~EmulatedVolume() {} |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 72 | |
Martijn Coenen | 73e3010 | 2022-07-12 08:11:02 +0000 | [diff] [blame] | 73 | std::string EmulatedVolume::getLabel() const { |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 74 | // We could have migrated storage to an adopted private volume, so always |
| 75 | // call primary storage "emulated" to avoid media rescans. |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 76 | if (getMountFlags() & MountFlags::kPrimary) { |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 77 | return "emulated"; |
| 78 | } else { |
| 79 | return mLabel; |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 80 | } |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 83 | // Creates a bind mount from source to target |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 84 | static status_t doFuseBindMount(const std::string& source, const std::string& target, |
| 85 | std::list<std::string>& pathsToUnmount) { |
Martijn Coenen | 3a2dbfe | 2020-01-11 19:38:37 +0100 | [diff] [blame] | 86 | LOG(INFO) << "Bind mounting " << source << " on " << target; |
| 87 | auto status = BindMount(source, target); |
| 88 | if (status != OK) { |
| 89 | return status; |
| 90 | } |
| 91 | LOG(INFO) << "Bind mounted " << source << " on " << target; |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 92 | pathsToUnmount.push_front(target); |
Martijn Coenen | 3a2dbfe | 2020-01-11 19:38:37 +0100 | [diff] [blame] | 93 | return OK; |
| 94 | } |
| 95 | |
Martijn Coenen | 73e3010 | 2022-07-12 08:11:02 +0000 | [diff] [blame] | 96 | // Bind mounts the volume 'volume' onto this volume. |
| 97 | status_t EmulatedVolume::bindMountVolume(const EmulatedVolume& volume, |
| 98 | std::list<std::string>& pathsToUnmount) { |
| 99 | int myUserId = getMountUserId(); |
| 100 | int volumeUserId = volume.getMountUserId(); |
| 101 | std::string label = volume.getLabel(); |
| 102 | |
| 103 | // eg /mnt/user/10/emulated/10 |
| 104 | std::string srcUserPath = GetFuseMountPathForUser(volumeUserId, label); |
| 105 | std::string srcPath = StringPrintf("%s/%d", srcUserPath.c_str(), volumeUserId); |
| 106 | // eg /mnt/user/0/emulated/10 |
| 107 | std::string dstUserPath = GetFuseMountPathForUser(myUserId, label); |
| 108 | std::string dstPath = StringPrintf("%s/%d", dstUserPath.c_str(), volumeUserId); |
| 109 | |
| 110 | auto status = doFuseBindMount(srcPath, dstPath, pathsToUnmount); |
| 111 | if (status == OK) { |
| 112 | // Store the mount path, so we can unmount it when this volume goes away |
| 113 | mSharedStorageMountPath = dstPath; |
| 114 | } |
| 115 | |
| 116 | return status; |
| 117 | } |
| 118 | |
Omar Eissa | ddfc061 | 2025-03-10 08:15:10 -0800 | [diff] [blame] | 119 | std::shared_ptr<android::vold::VolumeBase> getSharedStorageVolume(int userId) { |
| 120 | userid_t sharedStorageUserId = VolumeManager::Instance()->getSharedStorageUser(userId); |
| 121 | if (sharedStorageUserId != USER_UNKNOWN) { |
| 122 | auto filter_fn = [&](const VolumeBase &vol) { |
| 123 | if (vol.getState() != VolumeBase::State::kMounted) { |
| 124 | // The volume must be mounted |
| 125 | return false; |
| 126 | } |
| 127 | if (vol.getType() != VolumeBase::Type::kEmulated) { |
| 128 | return false; |
| 129 | } |
| 130 | if (vol.getMountUserId() != sharedStorageUserId) { |
| 131 | return false; |
| 132 | } |
| 133 | if ((vol.getMountFlags() & EmulatedVolume::MountFlags::kPrimary) == 0) { |
| 134 | // We only care about the primary emulated volume, so not a private |
| 135 | // volume with an emulated volume stacked on top. |
| 136 | return false; |
| 137 | } |
| 138 | return true; |
| 139 | }; |
| 140 | return VolumeManager::Instance()->findVolumeWithFilter(filter_fn); |
| 141 | } |
| 142 | return nullptr; |
| 143 | } |
| 144 | |
Martijn Coenen | 86f21a2 | 2020-01-06 09:48:14 +0100 | [diff] [blame] | 145 | status_t EmulatedVolume::mountFuseBindMounts() { |
| 146 | std::string androidSource; |
| 147 | std::string label = getLabel(); |
| 148 | int userId = getMountUserId(); |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 149 | std::list<std::string> pathsToUnmount; |
| 150 | |
| 151 | auto unmounter = [&]() { |
| 152 | LOG(INFO) << "mountFuseBindMounts() unmount scope_guard running"; |
| 153 | for (const auto& path : pathsToUnmount) { |
| 154 | LOG(INFO) << "Unmounting " << path; |
| 155 | auto status = UnmountTree(path); |
| 156 | if (status != OK) { |
| 157 | LOG(INFO) << "Failed to unmount " << path; |
| 158 | } else { |
| 159 | LOG(INFO) << "Unmounted " << path; |
| 160 | } |
| 161 | } |
| 162 | }; |
| 163 | auto unmount_guard = android::base::make_scope_guard(unmounter); |
Martijn Coenen | 86f21a2 | 2020-01-06 09:48:14 +0100 | [diff] [blame] | 164 | |
| 165 | if (mUseSdcardFs) { |
| 166 | androidSource = StringPrintf("/mnt/runtime/default/%s/%d/Android", label.c_str(), userId); |
| 167 | } else { |
| 168 | androidSource = StringPrintf("/%s/%d/Android", mRawPath.c_str(), userId); |
| 169 | } |
Martijn Coenen | 5700261 | 2019-11-28 11:56:13 +0100 | [diff] [blame] | 170 | |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 171 | status_t status = OK; |
Ricky Wai | 259a49a | 2021-03-19 15:35:49 +0000 | [diff] [blame] | 172 | // Zygote will unmount these dirs if app data isolation is enabled, so apps |
| 173 | // cannot access these dirs directly. |
| 174 | std::string androidDataSource = StringPrintf("%s/data", androidSource.c_str()); |
| 175 | std::string androidDataTarget( |
| 176 | StringPrintf("/mnt/user/%d/%s/%d/Android/data", userId, label.c_str(), userId)); |
| 177 | status = doFuseBindMount(androidDataSource, androidDataTarget, pathsToUnmount); |
| 178 | if (status != OK) { |
| 179 | return status; |
| 180 | } |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 181 | |
Ricky Wai | 259a49a | 2021-03-19 15:35:49 +0000 | [diff] [blame] | 182 | std::string androidObbSource = StringPrintf("%s/obb", androidSource.c_str()); |
| 183 | std::string androidObbTarget( |
| 184 | StringPrintf("/mnt/user/%d/%s/%d/Android/obb", userId, label.c_str(), userId)); |
| 185 | status = doFuseBindMount(androidObbSource, androidObbTarget, pathsToUnmount); |
| 186 | if (status != OK) { |
| 187 | return status; |
Martijn Coenen | 5700261 | 2019-11-28 11:56:13 +0100 | [diff] [blame] | 188 | } |
Zim | b6488f3 | 2020-03-17 15:15:42 +0000 | [diff] [blame] | 189 | |
Martijn Coenen | 3a2dbfe | 2020-01-11 19:38:37 +0100 | [diff] [blame] | 190 | // Installers get the same view as all other apps, with the sole exception that the |
| 191 | // OBB dirs (Android/obb) are writable to them. On sdcardfs devices, this requires |
| 192 | // a special bind mount, since app-private and OBB dirs share the same GID, but we |
| 193 | // only want to give access to the latter. |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 194 | if (mUseSdcardFs) { |
Ricky Wai | ef63921 | 2020-04-07 13:43:20 +0100 | [diff] [blame] | 195 | std::string obbSource(StringPrintf("/mnt/runtime/write/%s/%d/Android/obb", |
| 196 | label.c_str(), userId)); |
| 197 | std::string obbInstallerTarget(StringPrintf("/mnt/installer/%d/%s/%d/Android/obb", |
| 198 | userId, label.c_str(), userId)); |
Martijn Coenen | 3a2dbfe | 2020-01-11 19:38:37 +0100 | [diff] [blame] | 199 | |
Ricky Wai | ef63921 | 2020-04-07 13:43:20 +0100 | [diff] [blame] | 200 | status = doFuseBindMount(obbSource, obbInstallerTarget, pathsToUnmount); |
| 201 | if (status != OK) { |
| 202 | return status; |
| 203 | } |
Ricky Wai | ef63921 | 2020-04-07 13:43:20 +0100 | [diff] [blame] | 204 | } |
| 205 | |
Martijn Coenen | 73e3010 | 2022-07-12 08:11:02 +0000 | [diff] [blame] | 206 | // For users that share their volume with another user (eg a clone |
| 207 | // profile), the current mount setup can cause page cache inconsistency |
| 208 | // issues. Let's say this is user 10, and the user it shares storage with |
| 209 | // is user 0. |
| 210 | // Then: |
| 211 | // * The FUSE daemon for user 0 serves /mnt/user/0 |
| 212 | // * The FUSE daemon for user 10 serves /mnt/user/10 |
| 213 | // The emulated volume for user 10 would be located at two paths: |
| 214 | // /mnt/user/0/emulated/10 |
| 215 | // /mnt/user/10/emulated/10 |
| 216 | // Since these paths refer to the same files but are served by different FUSE |
| 217 | // daemons, this can result in page cache inconsistency issues. To prevent this, |
| 218 | // bind mount the relevant paths for the involved users: |
| 219 | // 1. /mnt/user/10/emulated/10 =B=> /mnt/user/0/emulated/10 |
| 220 | // 2. /mnt/user/0/emulated/0 =B=> /mnt/user/10/emulated/0 |
| 221 | // |
| 222 | // This will ensure that any access to the volume for a specific user always |
| 223 | // goes through a single FUSE daemon. |
Omar Eissa | ddfc061 | 2025-03-10 08:15:10 -0800 | [diff] [blame] | 224 | auto vol = getSharedStorageVolume(userId); |
| 225 | if (vol != nullptr) { |
| 226 | auto sharedVol = static_cast<EmulatedVolume*>(vol.get()); |
| 227 | // Bind mount this volume in the other user's primary volume |
| 228 | status = sharedVol->bindMountVolume(*this, pathsToUnmount); |
| 229 | if (status != OK) { |
| 230 | return status; |
| 231 | } |
| 232 | // And vice-versa |
| 233 | status = bindMountVolume(*sharedVol, pathsToUnmount); |
| 234 | if (status != OK) { |
| 235 | return status; |
Martijn Coenen | 73e3010 | 2022-07-12 08:11:02 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
Omar Eissa | ddfc061 | 2025-03-10 08:15:10 -0800 | [diff] [blame] | 238 | |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 239 | unmount_guard.Disable(); |
Martijn Coenen | 5700261 | 2019-11-28 11:56:13 +0100 | [diff] [blame] | 240 | return OK; |
| 241 | } |
| 242 | |
Omar Eissa | ddfc061 | 2025-03-10 08:15:10 -0800 | [diff] [blame] | 243 | status_t EmulatedVolume::unbindSharedStorageMountPath() { |
Martijn Coenen | 73e3010 | 2022-07-12 08:11:02 +0000 | [diff] [blame] | 244 | if (!mSharedStorageMountPath.empty()) { |
| 245 | LOG(INFO) << "Unmounting " << mSharedStorageMountPath; |
| 246 | auto status = UnmountTree(mSharedStorageMountPath); |
| 247 | if (status != OK) { |
| 248 | LOG(ERROR) << "Failed to unmount " << mSharedStorageMountPath; |
| 249 | } |
| 250 | mSharedStorageMountPath = ""; |
Omar Eissa | ddfc061 | 2025-03-10 08:15:10 -0800 | [diff] [blame] | 251 | return status; |
Martijn Coenen | 73e3010 | 2022-07-12 08:11:02 +0000 | [diff] [blame] | 252 | } |
Omar Eissa | ddfc061 | 2025-03-10 08:15:10 -0800 | [diff] [blame] | 253 | return OK; |
| 254 | } |
| 255 | |
| 256 | |
| 257 | status_t EmulatedVolume::unmountFuseBindMounts() { |
| 258 | std::string label = getLabel(); |
| 259 | int userId = getMountUserId(); |
| 260 | |
| 261 | if (!mSharedStorageMountPath.empty()) { |
| 262 | unbindSharedStorageMountPath(); |
| 263 | auto vol = getSharedStorageVolume(userId); |
| 264 | if (vol != nullptr) { |
| 265 | auto sharedVol = static_cast<EmulatedVolume*>(vol.get()); |
| 266 | sharedVol->unbindSharedStorageMountPath(); |
| 267 | } |
| 268 | } |
| 269 | |
Ricky Wai | ef63921 | 2020-04-07 13:43:20 +0100 | [diff] [blame] | 270 | if (mUseSdcardFs || mAppDataIsolationEnabled) { |
Martijn Coenen | 3a2dbfe | 2020-01-11 19:38:37 +0100 | [diff] [blame] | 271 | std::string installerTarget( |
| 272 | StringPrintf("/mnt/installer/%d/%s/%d/Android/obb", userId, label.c_str(), userId)); |
| 273 | LOG(INFO) << "Unmounting " << installerTarget; |
| 274 | auto status = UnmountTree(installerTarget); |
| 275 | if (status != OK) { |
| 276 | LOG(ERROR) << "Failed to unmount " << installerTarget; |
| 277 | // Intentional continue to try to unmount the other bind mount |
| 278 | } |
| 279 | } |
Ricky Wai | ef63921 | 2020-04-07 13:43:20 +0100 | [diff] [blame] | 280 | if (mAppDataIsolationEnabled) { |
| 281 | std::string obbTarget( StringPrintf("/mnt/androidwritable/%d/%s/%d/Android/obb", |
| 282 | userId, label.c_str(), userId)); |
| 283 | LOG(INFO) << "Unmounting " << obbTarget; |
| 284 | auto status = UnmountTree(obbTarget); |
| 285 | if (status != OK) { |
| 286 | LOG(ERROR) << "Failed to unmount " << obbTarget; |
| 287 | // Intentional continue to try to unmount the other bind mount |
| 288 | } |
| 289 | std::string dataTarget(StringPrintf("/mnt/androidwritable/%d/%s/%d/Android/data", |
| 290 | userId, label.c_str(), userId)); |
| 291 | LOG(INFO) << "Unmounting " << dataTarget; |
| 292 | status = UnmountTree(dataTarget); |
| 293 | if (status != OK) { |
| 294 | LOG(ERROR) << "Failed to unmount " << dataTarget; |
| 295 | // Intentional continue to try to unmount the other bind mount |
| 296 | } |
| 297 | } |
| 298 | |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 299 | // When app data isolation is enabled, kill all apps that obb/ is mounted, otherwise we should |
| 300 | // umount the whole Android/ dir. |
| 301 | if (mAppDataIsolationEnabled) { |
| 302 | std::string appObbDir(StringPrintf("%s/%d/Android/obb", getPath().c_str(), userId)); |
Ricky Wai | 2335637 | 2021-04-30 09:53:07 +0100 | [diff] [blame] | 303 | // Here we assume obb/data dirs is mounted as tmpfs, then it must be caused by |
| 304 | // app data isolation. |
| 305 | KillProcessesWithTmpfsMountPrefix(appObbDir); |
Martijn Coenen | 5700261 | 2019-11-28 11:56:13 +0100 | [diff] [blame] | 306 | } |
Ricky Wai | 5f2a9fe | 2021-05-05 14:43:45 +0000 | [diff] [blame] | 307 | |
| 308 | // Always unmount data and obb dirs as they are mounted to lowerfs for speeding up access. |
| 309 | std::string androidDataTarget( |
| 310 | StringPrintf("/mnt/user/%d/%s/%d/Android/data", userId, label.c_str(), userId)); |
| 311 | |
| 312 | LOG(INFO) << "Unmounting " << androidDataTarget; |
| 313 | auto status = UnmountTree(androidDataTarget); |
| 314 | if (status != OK) { |
| 315 | return status; |
| 316 | } |
| 317 | LOG(INFO) << "Unmounted " << androidDataTarget; |
| 318 | |
| 319 | std::string androidObbTarget( |
| 320 | StringPrintf("/mnt/user/%d/%s/%d/Android/obb", userId, label.c_str(), userId)); |
| 321 | |
| 322 | LOG(INFO) << "Unmounting " << androidObbTarget; |
| 323 | status = UnmountTree(androidObbTarget); |
| 324 | if (status != OK) { |
| 325 | return status; |
| 326 | } |
| 327 | LOG(INFO) << "Unmounted " << androidObbTarget; |
Martijn Coenen | 5700261 | 2019-11-28 11:56:13 +0100 | [diff] [blame] | 328 | return OK; |
| 329 | } |
| 330 | |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 331 | status_t EmulatedVolume::unmountSdcardFs() { |
| 332 | if (!mUseSdcardFs || getMountUserId() != 0) { |
| 333 | // For sdcardfs, only unmount for user 0, since user 0 will always be running |
| 334 | // and the paths don't change for different users. |
| 335 | return OK; |
| 336 | } |
| 337 | |
| 338 | ForceUnmount(mSdcardFsDefault); |
| 339 | ForceUnmount(mSdcardFsRead); |
| 340 | ForceUnmount(mSdcardFsWrite); |
| 341 | ForceUnmount(mSdcardFsFull); |
| 342 | |
| 343 | rmdir(mSdcardFsDefault.c_str()); |
| 344 | rmdir(mSdcardFsRead.c_str()); |
| 345 | rmdir(mSdcardFsWrite.c_str()); |
| 346 | rmdir(mSdcardFsFull.c_str()); |
| 347 | |
| 348 | mSdcardFsDefault.clear(); |
| 349 | mSdcardFsRead.clear(); |
| 350 | mSdcardFsWrite.clear(); |
| 351 | mSdcardFsFull.clear(); |
| 352 | |
| 353 | return OK; |
| 354 | } |
| 355 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 356 | status_t EmulatedVolume::doMount() { |
| 357 | std::string label = getLabel(); |
Youkichi Hosoi | 2991cbe | 2021-11-11 11:23:01 +0900 | [diff] [blame] | 358 | bool isVisible = isVisibleForWrite(); |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 359 | |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 360 | mSdcardFsDefault = StringPrintf("/mnt/runtime/default/%s", label.c_str()); |
| 361 | mSdcardFsRead = StringPrintf("/mnt/runtime/read/%s", label.c_str()); |
| 362 | mSdcardFsWrite = StringPrintf("/mnt/runtime/write/%s", label.c_str()); |
| 363 | mSdcardFsFull = StringPrintf("/mnt/runtime/full/%s", label.c_str()); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 364 | |
| 365 | setInternalPath(mRawPath); |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 366 | setPath(StringPrintf("/storage/%s", label.c_str())); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 367 | |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 368 | if (fs_prepare_dir(mSdcardFsDefault.c_str(), 0700, AID_ROOT, AID_ROOT) || |
| 369 | fs_prepare_dir(mSdcardFsRead.c_str(), 0700, AID_ROOT, AID_ROOT) || |
| 370 | fs_prepare_dir(mSdcardFsWrite.c_str(), 0700, AID_ROOT, AID_ROOT) || |
| 371 | fs_prepare_dir(mSdcardFsFull.c_str(), 0700, AID_ROOT, AID_ROOT)) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 372 | PLOG(ERROR) << getId() << " failed to create mount points"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 373 | return -errno; |
| 374 | } |
| 375 | |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 376 | dev_t before = GetDevice(mSdcardFsFull); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 377 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 378 | // Mount sdcardfs regardless of FUSE, since we need it to bind-mount on top of the |
| 379 | // FUSE volume for various reasons. |
Martijn Coenen | 86f21a2 | 2020-01-06 09:48:14 +0100 | [diff] [blame] | 380 | if (mUseSdcardFs && getMountUserId() == 0) { |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 381 | LOG(INFO) << "Executing sdcardfs"; |
| 382 | int sdcardFsPid; |
| 383 | if (!(sdcardFsPid = fork())) { |
| 384 | // clang-format off |
| 385 | if (execl(kSdcardFsPath, kSdcardFsPath, |
| 386 | "-u", "1023", // AID_MEDIA_RW |
| 387 | "-g", "1023", // AID_MEDIA_RW |
| 388 | "-m", |
| 389 | "-w", |
| 390 | "-G", |
| 391 | "-i", |
| 392 | "-o", |
| 393 | mRawPath.c_str(), |
| 394 | label.c_str(), |
| 395 | NULL)) { |
| 396 | // clang-format on |
| 397 | PLOG(ERROR) << "Failed to exec"; |
| 398 | } |
| 399 | |
| 400 | LOG(ERROR) << "sdcardfs exiting"; |
| 401 | _exit(1); |
| 402 | } |
| 403 | |
| 404 | if (sdcardFsPid == -1) { |
| 405 | PLOG(ERROR) << getId() << " failed to fork"; |
| 406 | return -errno; |
| 407 | } |
| 408 | |
| 409 | nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME); |
| 410 | while (before == GetDevice(mSdcardFsFull)) { |
| 411 | LOG(DEBUG) << "Waiting for sdcardfs to spin up..."; |
| 412 | usleep(50000); // 50ms |
| 413 | |
| 414 | nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME); |
| 415 | if (nanoseconds_to_milliseconds(now - start) > 5000) { |
| 416 | LOG(WARNING) << "Timed out while waiting for sdcardfs to spin up"; |
| 417 | return -ETIMEDOUT; |
| 418 | } |
| 419 | } |
| 420 | /* sdcardfs will have exited already. The filesystem will still be running */ |
| 421 | TEMP_FAILURE_RETRY(waitpid(sdcardFsPid, nullptr, 0)); |
| 422 | sdcardFsPid = 0; |
| 423 | } |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 424 | |
Ricky Wai | e78c78c | 2021-01-14 15:51:54 +0000 | [diff] [blame] | 425 | if (isVisible) { |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 426 | // Make sure we unmount sdcardfs if we bail out with an error below |
| 427 | auto sdcardfs_unmounter = [&]() { |
| 428 | LOG(INFO) << "sdcardfs_unmounter scope_guard running"; |
| 429 | unmountSdcardFs(); |
| 430 | }; |
| 431 | auto sdcardfs_guard = android::base::make_scope_guard(sdcardfs_unmounter); |
| 432 | |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 433 | LOG(INFO) << "Mounting emulated fuse volume"; |
Nandana Dutt | a914cc7 | 2019-08-29 15:22:42 +0100 | [diff] [blame] | 434 | android::base::unique_fd fd; |
Zim | 981222f | 2019-09-09 10:24:44 +0100 | [diff] [blame] | 435 | int user_id = getMountUserId(); |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 436 | auto volumeRoot = getRootPath(); |
Zim | 981222f | 2019-09-09 10:24:44 +0100 | [diff] [blame] | 437 | |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 438 | // Make sure Android/ dirs exist for bind mounting |
| 439 | status_t res = PrepareAndroidDirs(volumeRoot); |
| 440 | if (res != OK) { |
| 441 | LOG(ERROR) << "Failed to prepare Android/ directories"; |
| 442 | return res; |
| 443 | } |
| 444 | |
| 445 | res = MountUserFuse(user_id, getInternalPath(), label, &fd); |
| 446 | if (res != 0) { |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 447 | PLOG(ERROR) << "Failed to mount emulated fuse volume"; |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 448 | return res; |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 449 | } |
Zim | 5048b4b | 2019-11-19 09:16:03 +0000 | [diff] [blame] | 450 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 451 | mFuseMounted = true; |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 452 | auto fuse_unmounter = [&]() { |
| 453 | LOG(INFO) << "fuse_unmounter scope_guard running"; |
| 454 | fd.reset(); |
Omar Eissa | fedaddb | 2025-03-12 16:09:16 +0000 | [diff] [blame] | 455 | if (flags::enhance_fuse_unmount()) { |
| 456 | std::string user_path(StringPrintf("%s/%d", getPath().c_str(), getMountUserId())); |
| 457 | if (UnmountUserFuseEnhanced(user_id, getInternalPath(), label, user_path) != OK) { |
| 458 | PLOG(INFO) << "UnmountUserFuseEnhanced failed on emulated fuse volume"; |
| 459 | } |
| 460 | } else { |
| 461 | if (UnmountUserFuse(user_id, getInternalPath(), label) != OK) { |
| 462 | PLOG(INFO) << "UnmountUserFuse failed on emulated fuse volume"; |
| 463 | } |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 464 | } |
Omar Eissa | fedaddb | 2025-03-12 16:09:16 +0000 | [diff] [blame] | 465 | |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 466 | mFuseMounted = false; |
| 467 | }; |
| 468 | auto fuse_guard = android::base::make_scope_guard(fuse_unmounter); |
| 469 | |
Zim | 5048b4b | 2019-11-19 09:16:03 +0000 | [diff] [blame] | 470 | auto callback = getMountCallback(); |
| 471 | if (callback) { |
| 472 | bool is_ready = false; |
| 473 | callback->onVolumeChecking(std::move(fd), getPath(), getInternalPath(), &is_ready); |
| 474 | if (!is_ready) { |
| 475 | return -EIO; |
| 476 | } |
| 477 | } |
Martijn Coenen | 5700261 | 2019-11-28 11:56:13 +0100 | [diff] [blame] | 478 | |
Paul Lawrence | a688c4f | 2022-12-14 14:35:05 -0800 | [diff] [blame] | 479 | if (!IsFuseBpfEnabled()) { |
Alessio Balsini | dd1e91f | 2021-10-13 12:03:18 +0100 | [diff] [blame] | 480 | // Only do the bind-mounts when we know for sure the FUSE daemon can resolve the path. |
| 481 | res = mountFuseBindMounts(); |
| 482 | if (res != OK) { |
| 483 | return res; |
| 484 | } |
Zim | df073f5 | 2020-01-15 15:00:07 +0000 | [diff] [blame] | 485 | } |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 486 | |
Nikita Ioffe | dcee5c1 | 2020-06-12 12:59:45 +0100 | [diff] [blame] | 487 | ConfigureReadAheadForFuse(GetFuseMountPathForUser(user_id, label), 256u); |
| 488 | |
Martijn Coenen | a485006 | 2020-06-29 11:53:34 +0200 | [diff] [blame] | 489 | // By default, FUSE has a max_dirty ratio of 1%. This means that out of |
| 490 | // all dirty pages in the system, only 1% is allowed to belong to any |
| 491 | // FUSE filesystem. The reason this is in place is that FUSE |
| 492 | // filesystems shouldn't be trusted by default; a FUSE filesystem could |
| 493 | // take up say 100% of dirty pages, and subsequently refuse to write |
| 494 | // them back to storage. The kernel will then apply rate-limiting, and |
| 495 | // block other tasks from writing. For this particular FUSE filesystem |
| 496 | // however, we trust the implementation, because it is a part of the |
| 497 | // Android platform. So use the default ratio of 100%. |
| 498 | // |
| 499 | // The reason we're setting this is that there's a suspicion that the |
| 500 | // kernel starts rate-limiting the FUSE filesystem under extreme |
| 501 | // memory pressure scenarios. While the kernel will only rate limit if |
| 502 | // the writeback can't keep up with the write rate, under extreme |
| 503 | // memory pressure the write rate may dip as well, in which case FUSE |
| 504 | // writes to a 1% max_ratio filesystem are throttled to an extreme amount. |
| 505 | // |
| 506 | // To prevent this, just give FUSE 40% max_ratio, meaning it can take |
| 507 | // up to 40% of all dirty pages in the system. |
| 508 | ConfigureMaxDirtyRatioForFuse(GetFuseMountPathForUser(user_id, label), 40u); |
| 509 | |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 510 | // All mounts where successful, disable scope guards |
| 511 | sdcardfs_guard.Disable(); |
| 512 | fuse_guard.Disable(); |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 513 | } |
| 514 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 515 | return OK; |
| 516 | } |
| 517 | |
| 518 | status_t EmulatedVolume::doUnmount() { |
Martijn Coenen | 8f1e7f2 | 2019-11-29 15:38:55 +0100 | [diff] [blame] | 519 | int userId = getMountUserId(); |
| 520 | |
Martijn Coenen | 8f1e7f2 | 2019-11-29 15:38:55 +0100 | [diff] [blame] | 521 | if (mFuseMounted) { |
Martijn Coenen | 8f1e7f2 | 2019-11-29 15:38:55 +0100 | [diff] [blame] | 522 | std::string user_path(StringPrintf("%s/%d", getPath().c_str(), getMountUserId())); |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 523 | |
Omar Eissa | fedaddb | 2025-03-12 16:09:16 +0000 | [diff] [blame] | 524 | // We don't kill processes before trying to unmount in case enhance_fuse_unmount enabled |
| 525 | // As we make sure to kill processes if needed if unmounting failed |
| 526 | if (!flags::enhance_fuse_unmount()) { |
| 527 | // Kill all processes using the filesystem before we unmount it. If we |
| 528 | // unmount the filesystem first, most file system operations will return |
| 529 | // ENOTCONN until the unmount completes. This is an exotic and unusual |
| 530 | // error code and might cause broken behaviour in applications. |
| 531 | // For FUSE specifically, we have an emulated volume per user, so only kill |
| 532 | // processes using files from this particular user. |
| 533 | LOG(INFO) << "Killing all processes referencing " << user_path; |
| 534 | KillProcessesUsingPath(user_path); |
| 535 | } |
| 536 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 537 | std::string label = getLabel(); |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 538 | |
Paul Lawrence | a688c4f | 2022-12-14 14:35:05 -0800 | [diff] [blame] | 539 | if (!IsFuseBpfEnabled()) { |
Alessio Balsini | dd1e91f | 2021-10-13 12:03:18 +0100 | [diff] [blame] | 540 | // Ignoring unmount return status because we do want to try to |
| 541 | // unmount the rest cleanly. |
| 542 | unmountFuseBindMounts(); |
| 543 | } |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 544 | |
Omar Eissa | fedaddb | 2025-03-12 16:09:16 +0000 | [diff] [blame] | 545 | if (flags::enhance_fuse_unmount()) { |
| 546 | status_t result = UnmountUserFuseEnhanced(userId, getInternalPath(), label, user_path); |
| 547 | if (result != OK) { |
| 548 | PLOG(INFO) << "UnmountUserFuseEnhanced failed on emulated fuse volume"; |
| 549 | return result; |
| 550 | } |
| 551 | } else { |
| 552 | if (UnmountUserFuse(userId, getInternalPath(), label) != OK) { |
| 553 | PLOG(INFO) << "UnmountUserFuse failed on emulated fuse volume"; |
| 554 | return -errno; |
| 555 | } |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 556 | } |
| 557 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 558 | mFuseMounted = false; |
Omar Eissa | fedaddb | 2025-03-12 16:09:16 +0000 | [diff] [blame] | 559 | } else { |
| 560 | // This branch is needed to help with unmounting private volumes that aren't set to primary |
| 561 | // and don't have fuse mounted but have stacked emulated volumes |
| 562 | KillProcessesUsingPath(getPath()); |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 563 | } |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 564 | |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 565 | return unmountSdcardFs(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 566 | } |
| 567 | |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 568 | std::string EmulatedVolume::getRootPath() const { |
| 569 | int user_id = getMountUserId(); |
| 570 | std::string volumeRoot = StringPrintf("%s/%d", getInternalPath().c_str(), user_id); |
| 571 | |
| 572 | return volumeRoot; |
| 573 | } |
| 574 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 575 | } // namespace vold |
| 576 | } // namespace android |