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" |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 21 | #include "VolumeManager.h" |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 22 | |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 23 | #include <android-base/logging.h> |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 24 | #include <android-base/properties.h> |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 25 | #include <android-base/scopeguard.h> |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 26 | #include <android-base/stringprintf.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 27 | #include <cutils/fs.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 28 | #include <private/android_filesystem_config.h> |
Jeff Sharkey | 7bdf4d5 | 2017-09-18 14:47:10 -0600 | [diff] [blame] | 29 | #include <utils/Timers.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 30 | |
| 31 | #include <fcntl.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <sys/mount.h> |
| 34 | #include <sys/stat.h> |
Elliott Hughes | 0e08e84 | 2017-05-18 09:08:24 -0700 | [diff] [blame] | 35 | #include <sys/sysmacros.h> |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 36 | #include <sys/types.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 37 | #include <sys/wait.h> |
| 38 | |
Dan Albert | ae9e890 | 2015-03-16 10:35:17 -0700 | [diff] [blame] | 39 | using android::base::StringPrintf; |
| 40 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 41 | namespace android { |
| 42 | namespace vold { |
| 43 | |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 44 | static const char* kSdcardFsPath = "/system/bin/sdcard"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 45 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 46 | EmulatedVolume::EmulatedVolume(const std::string& rawPath, int userId) |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 47 | : VolumeBase(Type::kEmulated) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 48 | setId(StringPrintf("emulated;%u", userId)); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 49 | mRawPath = rawPath; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 50 | mLabel = "emulated"; |
Martijn Coenen | fd7362d | 2019-12-11 14:57:59 +0100 | [diff] [blame] | 51 | mFuseMounted = false; |
Martijn Coenen | 86f21a2 | 2020-01-06 09:48:14 +0100 | [diff] [blame] | 52 | mUseSdcardFs = IsFilesystemSupported("sdcardfs"); |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 53 | mAppDataIsolationEnabled = base::GetBoolProperty(kVoldAppDataIsolationEnabled, false); |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 56 | EmulatedVolume::EmulatedVolume(const std::string& rawPath, dev_t device, const std::string& fsUuid, |
| 57 | int userId) |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 58 | : VolumeBase(Type::kEmulated) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 59 | setId(StringPrintf("emulated:%u,%u;%u", major(device), minor(device), userId)); |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 60 | mRawPath = rawPath; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 61 | mLabel = fsUuid; |
Greg Kaiser | 5298ccc | 2019-12-12 05:41:46 -0800 | [diff] [blame] | 62 | mFuseMounted = false; |
Martijn Coenen | 86f21a2 | 2020-01-06 09:48:14 +0100 | [diff] [blame] | 63 | mUseSdcardFs = IsFilesystemSupported("sdcardfs"); |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 64 | mAppDataIsolationEnabled = base::GetBoolProperty(kVoldAppDataIsolationEnabled, false); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 67 | EmulatedVolume::~EmulatedVolume() {} |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 68 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 69 | std::string EmulatedVolume::getLabel() { |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 70 | // We could have migrated storage to an adopted private volume, so always |
| 71 | // call primary storage "emulated" to avoid media rescans. |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 72 | if (getMountFlags() & MountFlags::kPrimary) { |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 73 | return "emulated"; |
| 74 | } else { |
| 75 | return mLabel; |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 76 | } |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 79 | // Creates a bind mount from source to target |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 80 | static status_t doFuseBindMount(const std::string& source, const std::string& target, |
| 81 | std::list<std::string>& pathsToUnmount) { |
Martijn Coenen | 3a2dbfe | 2020-01-11 19:38:37 +0100 | [diff] [blame] | 82 | LOG(INFO) << "Bind mounting " << source << " on " << target; |
| 83 | auto status = BindMount(source, target); |
| 84 | if (status != OK) { |
| 85 | return status; |
| 86 | } |
| 87 | LOG(INFO) << "Bind mounted " << source << " on " << target; |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 88 | pathsToUnmount.push_front(target); |
Martijn Coenen | 3a2dbfe | 2020-01-11 19:38:37 +0100 | [diff] [blame] | 89 | return OK; |
| 90 | } |
| 91 | |
Martijn Coenen | 86f21a2 | 2020-01-06 09:48:14 +0100 | [diff] [blame] | 92 | status_t EmulatedVolume::mountFuseBindMounts() { |
| 93 | std::string androidSource; |
| 94 | std::string label = getLabel(); |
| 95 | int userId = getMountUserId(); |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 96 | std::list<std::string> pathsToUnmount; |
| 97 | |
| 98 | auto unmounter = [&]() { |
| 99 | LOG(INFO) << "mountFuseBindMounts() unmount scope_guard running"; |
| 100 | for (const auto& path : pathsToUnmount) { |
| 101 | LOG(INFO) << "Unmounting " << path; |
| 102 | auto status = UnmountTree(path); |
| 103 | if (status != OK) { |
| 104 | LOG(INFO) << "Failed to unmount " << path; |
| 105 | } else { |
| 106 | LOG(INFO) << "Unmounted " << path; |
| 107 | } |
| 108 | } |
| 109 | }; |
| 110 | auto unmount_guard = android::base::make_scope_guard(unmounter); |
Martijn Coenen | 86f21a2 | 2020-01-06 09:48:14 +0100 | [diff] [blame] | 111 | |
| 112 | if (mUseSdcardFs) { |
| 113 | androidSource = StringPrintf("/mnt/runtime/default/%s/%d/Android", label.c_str(), userId); |
| 114 | } else { |
| 115 | androidSource = StringPrintf("/%s/%d/Android", mRawPath.c_str(), userId); |
| 116 | } |
Martijn Coenen | 5700261 | 2019-11-28 11:56:13 +0100 | [diff] [blame] | 117 | |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 118 | status_t status = OK; |
| 119 | // When app data isolation is enabled, obb/ will be mounted per app, otherwise we should |
| 120 | // bind mount the whole Android/ to speed up reading. |
| 121 | if (!mAppDataIsolationEnabled) { |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 122 | std::string androidDataSource = StringPrintf("%s/data", androidSource.c_str()); |
| 123 | std::string androidDataTarget( |
| 124 | StringPrintf("/mnt/user/%d/%s/%d/Android/data", userId, label.c_str(), userId)); |
| 125 | status = doFuseBindMount(androidDataSource, androidDataTarget, pathsToUnmount); |
| 126 | if (status != OK) { |
| 127 | return status; |
| 128 | } |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 129 | |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 130 | std::string androidObbSource = StringPrintf("%s/obb", androidSource.c_str()); |
| 131 | std::string androidObbTarget( |
| 132 | StringPrintf("/mnt/user/%d/%s/%d/Android/obb", userId, label.c_str(), userId)); |
| 133 | status = doFuseBindMount(androidObbSource, androidObbTarget, pathsToUnmount); |
| 134 | if (status != OK) { |
| 135 | return status; |
| 136 | } |
Martijn Coenen | 5700261 | 2019-11-28 11:56:13 +0100 | [diff] [blame] | 137 | } |
Zim | b6488f3 | 2020-03-17 15:15:42 +0000 | [diff] [blame] | 138 | |
Martijn Coenen | 3a2dbfe | 2020-01-11 19:38:37 +0100 | [diff] [blame] | 139 | // Installers get the same view as all other apps, with the sole exception that the |
| 140 | // OBB dirs (Android/obb) are writable to them. On sdcardfs devices, this requires |
| 141 | // a special bind mount, since app-private and OBB dirs share the same GID, but we |
| 142 | // only want to give access to the latter. |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 143 | if (mUseSdcardFs) { |
| 144 | std::string installerSource( |
| 145 | StringPrintf("/mnt/runtime/write/%s/%d/Android/obb", label.c_str(), userId)); |
| 146 | std::string installerTarget( |
| 147 | StringPrintf("/mnt/installer/%d/%s/%d/Android/obb", userId, label.c_str(), userId)); |
Martijn Coenen | 3a2dbfe | 2020-01-11 19:38:37 +0100 | [diff] [blame] | 148 | |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 149 | status = doFuseBindMount(installerSource, installerTarget, pathsToUnmount); |
| 150 | if (status != OK) { |
| 151 | return status; |
| 152 | } |
Martijn Coenen | 3a2dbfe | 2020-01-11 19:38:37 +0100 | [diff] [blame] | 153 | } |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 154 | unmount_guard.Disable(); |
Martijn Coenen | 5700261 | 2019-11-28 11:56:13 +0100 | [diff] [blame] | 155 | return OK; |
| 156 | } |
| 157 | |
Martijn Coenen | 86f21a2 | 2020-01-06 09:48:14 +0100 | [diff] [blame] | 158 | status_t EmulatedVolume::unmountFuseBindMounts() { |
| 159 | std::string label = getLabel(); |
| 160 | int userId = getMountUserId(); |
| 161 | |
Martijn Coenen | 3a2dbfe | 2020-01-11 19:38:37 +0100 | [diff] [blame] | 162 | if (mUseSdcardFs) { |
| 163 | std::string installerTarget( |
| 164 | StringPrintf("/mnt/installer/%d/%s/%d/Android/obb", userId, label.c_str(), userId)); |
| 165 | LOG(INFO) << "Unmounting " << installerTarget; |
| 166 | auto status = UnmountTree(installerTarget); |
| 167 | if (status != OK) { |
| 168 | LOG(ERROR) << "Failed to unmount " << installerTarget; |
| 169 | // Intentional continue to try to unmount the other bind mount |
| 170 | } |
| 171 | } |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 172 | // When app data isolation is enabled, kill all apps that obb/ is mounted, otherwise we should |
| 173 | // umount the whole Android/ dir. |
| 174 | if (mAppDataIsolationEnabled) { |
| 175 | std::string appObbDir(StringPrintf("%s/%d/Android/obb", getPath().c_str(), userId)); |
| 176 | KillProcessesWithMountPrefix(appObbDir); |
| 177 | } else { |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 178 | std::string androidDataTarget( |
| 179 | StringPrintf("/mnt/user/%d/%s/%d/Android/data", userId, label.c_str(), userId)); |
Martijn Coenen | 3a2dbfe | 2020-01-11 19:38:37 +0100 | [diff] [blame] | 180 | |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 181 | LOG(INFO) << "Unmounting " << androidDataTarget; |
| 182 | auto status = UnmountTree(androidDataTarget); |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 183 | if (status != OK) { |
| 184 | return status; |
| 185 | } |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 186 | LOG(INFO) << "Unmounted " << androidDataTarget; |
| 187 | |
| 188 | std::string androidObbTarget( |
| 189 | StringPrintf("/mnt/user/%d/%s/%d/Android/obb", userId, label.c_str(), userId)); |
| 190 | |
| 191 | LOG(INFO) << "Unmounting " << androidObbTarget; |
| 192 | status = UnmountTree(androidObbTarget); |
| 193 | if (status != OK) { |
| 194 | return status; |
| 195 | } |
| 196 | LOG(INFO) << "Unmounted " << androidObbTarget; |
Martijn Coenen | 5700261 | 2019-11-28 11:56:13 +0100 | [diff] [blame] | 197 | } |
Martijn Coenen | 5700261 | 2019-11-28 11:56:13 +0100 | [diff] [blame] | 198 | return OK; |
| 199 | } |
| 200 | |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 201 | status_t EmulatedVolume::unmountSdcardFs() { |
| 202 | if (!mUseSdcardFs || getMountUserId() != 0) { |
| 203 | // For sdcardfs, only unmount for user 0, since user 0 will always be running |
| 204 | // and the paths don't change for different users. |
| 205 | return OK; |
| 206 | } |
| 207 | |
| 208 | ForceUnmount(mSdcardFsDefault); |
| 209 | ForceUnmount(mSdcardFsRead); |
| 210 | ForceUnmount(mSdcardFsWrite); |
| 211 | ForceUnmount(mSdcardFsFull); |
| 212 | |
| 213 | rmdir(mSdcardFsDefault.c_str()); |
| 214 | rmdir(mSdcardFsRead.c_str()); |
| 215 | rmdir(mSdcardFsWrite.c_str()); |
| 216 | rmdir(mSdcardFsFull.c_str()); |
| 217 | |
| 218 | mSdcardFsDefault.clear(); |
| 219 | mSdcardFsRead.clear(); |
| 220 | mSdcardFsWrite.clear(); |
| 221 | mSdcardFsFull.clear(); |
| 222 | |
| 223 | return OK; |
| 224 | } |
| 225 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 226 | status_t EmulatedVolume::doMount() { |
| 227 | std::string label = getLabel(); |
| 228 | bool isVisible = getMountFlags() & MountFlags::kVisible; |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 229 | |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 230 | mSdcardFsDefault = StringPrintf("/mnt/runtime/default/%s", label.c_str()); |
| 231 | mSdcardFsRead = StringPrintf("/mnt/runtime/read/%s", label.c_str()); |
| 232 | mSdcardFsWrite = StringPrintf("/mnt/runtime/write/%s", label.c_str()); |
| 233 | mSdcardFsFull = StringPrintf("/mnt/runtime/full/%s", label.c_str()); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 234 | |
| 235 | setInternalPath(mRawPath); |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 236 | setPath(StringPrintf("/storage/%s", label.c_str())); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 237 | |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 238 | if (fs_prepare_dir(mSdcardFsDefault.c_str(), 0700, AID_ROOT, AID_ROOT) || |
| 239 | fs_prepare_dir(mSdcardFsRead.c_str(), 0700, AID_ROOT, AID_ROOT) || |
| 240 | fs_prepare_dir(mSdcardFsWrite.c_str(), 0700, AID_ROOT, AID_ROOT) || |
| 241 | fs_prepare_dir(mSdcardFsFull.c_str(), 0700, AID_ROOT, AID_ROOT)) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 242 | PLOG(ERROR) << getId() << " failed to create mount points"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 243 | return -errno; |
| 244 | } |
| 245 | |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 246 | dev_t before = GetDevice(mSdcardFsFull); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 247 | |
Abhijeet Kaur | 01fa0e0 | 2019-12-13 10:26:32 +0000 | [diff] [blame] | 248 | bool isFuse = base::GetBoolProperty(kPropFuse, false); |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 249 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 250 | // Mount sdcardfs regardless of FUSE, since we need it to bind-mount on top of the |
| 251 | // FUSE volume for various reasons. |
Martijn Coenen | 86f21a2 | 2020-01-06 09:48:14 +0100 | [diff] [blame] | 252 | if (mUseSdcardFs && getMountUserId() == 0) { |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 253 | LOG(INFO) << "Executing sdcardfs"; |
| 254 | int sdcardFsPid; |
| 255 | if (!(sdcardFsPid = fork())) { |
| 256 | // clang-format off |
| 257 | if (execl(kSdcardFsPath, kSdcardFsPath, |
| 258 | "-u", "1023", // AID_MEDIA_RW |
| 259 | "-g", "1023", // AID_MEDIA_RW |
| 260 | "-m", |
| 261 | "-w", |
| 262 | "-G", |
| 263 | "-i", |
| 264 | "-o", |
| 265 | mRawPath.c_str(), |
| 266 | label.c_str(), |
| 267 | NULL)) { |
| 268 | // clang-format on |
| 269 | PLOG(ERROR) << "Failed to exec"; |
| 270 | } |
| 271 | |
| 272 | LOG(ERROR) << "sdcardfs exiting"; |
| 273 | _exit(1); |
| 274 | } |
| 275 | |
| 276 | if (sdcardFsPid == -1) { |
| 277 | PLOG(ERROR) << getId() << " failed to fork"; |
| 278 | return -errno; |
| 279 | } |
| 280 | |
| 281 | nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME); |
| 282 | while (before == GetDevice(mSdcardFsFull)) { |
| 283 | LOG(DEBUG) << "Waiting for sdcardfs to spin up..."; |
| 284 | usleep(50000); // 50ms |
| 285 | |
| 286 | nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME); |
| 287 | if (nanoseconds_to_milliseconds(now - start) > 5000) { |
| 288 | LOG(WARNING) << "Timed out while waiting for sdcardfs to spin up"; |
| 289 | return -ETIMEDOUT; |
| 290 | } |
| 291 | } |
| 292 | /* sdcardfs will have exited already. The filesystem will still be running */ |
| 293 | TEMP_FAILURE_RETRY(waitpid(sdcardFsPid, nullptr, 0)); |
| 294 | sdcardFsPid = 0; |
| 295 | } |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 296 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 297 | if (isFuse && isVisible) { |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 298 | // Make sure we unmount sdcardfs if we bail out with an error below |
| 299 | auto sdcardfs_unmounter = [&]() { |
| 300 | LOG(INFO) << "sdcardfs_unmounter scope_guard running"; |
| 301 | unmountSdcardFs(); |
| 302 | }; |
| 303 | auto sdcardfs_guard = android::base::make_scope_guard(sdcardfs_unmounter); |
| 304 | |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 305 | LOG(INFO) << "Mounting emulated fuse volume"; |
Nandana Dutt | a914cc7 | 2019-08-29 15:22:42 +0100 | [diff] [blame] | 306 | android::base::unique_fd fd; |
Zim | 981222f | 2019-09-09 10:24:44 +0100 | [diff] [blame] | 307 | int user_id = getMountUserId(); |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 308 | auto volumeRoot = getRootPath(); |
Zim | 981222f | 2019-09-09 10:24:44 +0100 | [diff] [blame] | 309 | |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 310 | // Make sure Android/ dirs exist for bind mounting |
| 311 | status_t res = PrepareAndroidDirs(volumeRoot); |
| 312 | if (res != OK) { |
| 313 | LOG(ERROR) << "Failed to prepare Android/ directories"; |
| 314 | return res; |
| 315 | } |
| 316 | |
| 317 | res = MountUserFuse(user_id, getInternalPath(), label, &fd); |
| 318 | if (res != 0) { |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 319 | PLOG(ERROR) << "Failed to mount emulated fuse volume"; |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 320 | return res; |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 321 | } |
Zim | 5048b4b | 2019-11-19 09:16:03 +0000 | [diff] [blame] | 322 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 323 | mFuseMounted = true; |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 324 | auto fuse_unmounter = [&]() { |
| 325 | LOG(INFO) << "fuse_unmounter scope_guard running"; |
| 326 | fd.reset(); |
| 327 | if (UnmountUserFuse(user_id, getInternalPath(), label) != OK) { |
| 328 | PLOG(INFO) << "UnmountUserFuse failed on emulated fuse volume"; |
| 329 | } |
| 330 | mFuseMounted = false; |
| 331 | }; |
| 332 | auto fuse_guard = android::base::make_scope_guard(fuse_unmounter); |
| 333 | |
Zim | 5048b4b | 2019-11-19 09:16:03 +0000 | [diff] [blame] | 334 | auto callback = getMountCallback(); |
| 335 | if (callback) { |
| 336 | bool is_ready = false; |
| 337 | callback->onVolumeChecking(std::move(fd), getPath(), getInternalPath(), &is_ready); |
| 338 | if (!is_ready) { |
| 339 | return -EIO; |
| 340 | } |
| 341 | } |
Martijn Coenen | 5700261 | 2019-11-28 11:56:13 +0100 | [diff] [blame] | 342 | |
| 343 | // Only do the bind-mounts when we know for sure the FUSE daemon can resolve the path. |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 344 | res = mountFuseBindMounts(); |
Zim | df073f5 | 2020-01-15 15:00:07 +0000 | [diff] [blame] | 345 | if (res != OK) { |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 346 | return res; |
Zim | df073f5 | 2020-01-15 15:00:07 +0000 | [diff] [blame] | 347 | } |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 348 | |
| 349 | // All mounts where successful, disable scope guards |
| 350 | sdcardfs_guard.Disable(); |
| 351 | fuse_guard.Disable(); |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 352 | } |
| 353 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 354 | return OK; |
| 355 | } |
| 356 | |
| 357 | status_t EmulatedVolume::doUnmount() { |
Martijn Coenen | 8f1e7f2 | 2019-11-29 15:38:55 +0100 | [diff] [blame] | 358 | int userId = getMountUserId(); |
| 359 | |
| 360 | // Kill all processes using the filesystem before we unmount it. If we |
| 361 | // unmount the filesystem first, most file system operations will return |
Narayan Kamath | ea243a3 | 2016-01-21 12:26:05 +0000 | [diff] [blame] | 362 | // ENOTCONN until the unmount completes. This is an exotic and unusual |
| 363 | // error code and might cause broken behaviour in applications. |
Martijn Coenen | 8f1e7f2 | 2019-11-29 15:38:55 +0100 | [diff] [blame] | 364 | if (mFuseMounted) { |
| 365 | // For FUSE specifically, we have an emulated volume per user, so only kill |
| 366 | // processes using files from this particular user. |
| 367 | std::string user_path(StringPrintf("%s/%d", getPath().c_str(), getMountUserId())); |
| 368 | LOG(INFO) << "Killing all processes referencing " << user_path; |
| 369 | KillProcessesUsingPath(user_path); |
| 370 | } else { |
| 371 | KillProcessesUsingPath(getPath()); |
| 372 | } |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 373 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 374 | if (mFuseMounted) { |
| 375 | std::string label = getLabel(); |
Ricky Wai | 07e64a4 | 2020-02-11 14:31:24 +0000 | [diff] [blame] | 376 | |
Martijn Coenen | 5700261 | 2019-11-28 11:56:13 +0100 | [diff] [blame] | 377 | // Ignoring unmount return status because we do want to try to unmount |
| 378 | // the rest cleanly. |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 379 | unmountFuseBindMounts(); |
| 380 | |
Martijn Coenen | 5700261 | 2019-11-28 11:56:13 +0100 | [diff] [blame] | 381 | if (UnmountUserFuse(userId, getInternalPath(), label) != OK) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 382 | PLOG(INFO) << "UnmountUserFuse failed on emulated fuse volume"; |
| 383 | return -errno; |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 384 | } |
| 385 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 386 | mFuseMounted = false; |
| 387 | } |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 388 | |
Martijn Coenen | 449a7d8 | 2020-03-16 14:37:33 +0100 | [diff] [blame] | 389 | return unmountSdcardFs(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 390 | } |
| 391 | |
Martijn Coenen | 62a4b27 | 2020-01-31 15:23:09 +0100 | [diff] [blame] | 392 | std::string EmulatedVolume::getRootPath() const { |
| 393 | int user_id = getMountUserId(); |
| 394 | std::string volumeRoot = StringPrintf("%s/%d", getInternalPath().c_str(), user_id); |
| 395 | |
| 396 | return volumeRoot; |
| 397 | } |
| 398 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 399 | } // namespace vold |
| 400 | } // namespace android |