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> |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 25 | #include <android-base/stringprintf.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 26 | #include <cutils/fs.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 27 | #include <private/android_filesystem_config.h> |
Jeff Sharkey | 7bdf4d5 | 2017-09-18 14:47:10 -0600 | [diff] [blame] | 28 | #include <utils/Timers.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 29 | |
| 30 | #include <fcntl.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <sys/mount.h> |
| 33 | #include <sys/stat.h> |
Elliott Hughes | 0e08e84 | 2017-05-18 09:08:24 -0700 | [diff] [blame] | 34 | #include <sys/sysmacros.h> |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 35 | #include <sys/types.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 36 | #include <sys/wait.h> |
| 37 | |
Dan Albert | ae9e890 | 2015-03-16 10:35:17 -0700 | [diff] [blame] | 38 | using android::base::StringPrintf; |
| 39 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 40 | namespace android { |
| 41 | namespace vold { |
| 42 | |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 43 | static const char* kSdcardFsPath = "/system/bin/sdcard"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 44 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 45 | EmulatedVolume::EmulatedVolume(const std::string& rawPath, int userId) |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 46 | : VolumeBase(Type::kEmulated) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 47 | setId(StringPrintf("emulated;%u", userId)); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 48 | mRawPath = rawPath; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 49 | mLabel = "emulated"; |
Martijn Coenen | fd7362d | 2019-12-11 14:57:59 +0100 | [diff] [blame] | 50 | mFuseMounted = false; |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 53 | EmulatedVolume::EmulatedVolume(const std::string& rawPath, dev_t device, const std::string& fsUuid, |
| 54 | int userId) |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 55 | : VolumeBase(Type::kEmulated) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 56 | setId(StringPrintf("emulated:%u,%u;%u", major(device), minor(device), userId)); |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 57 | mRawPath = rawPath; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 58 | mLabel = fsUuid; |
Greg Kaiser | 5298ccc | 2019-12-12 05:41:46 -0800 | [diff] [blame] | 59 | mFuseMounted = false; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 62 | EmulatedVolume::~EmulatedVolume() {} |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 63 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 64 | std::string EmulatedVolume::getLabel() { |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 65 | // We could have migrated storage to an adopted private volume, so always |
| 66 | // call primary storage "emulated" to avoid media rescans. |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 67 | if (getMountFlags() & MountFlags::kPrimary) { |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 68 | return "emulated"; |
| 69 | } else { |
| 70 | return mLabel; |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 71 | } |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Martijn Coenen | 5700261 | 2019-11-28 11:56:13 +0100 | [diff] [blame] | 74 | static status_t mountFuseBindMounts(int userId, const std::string& label) { |
| 75 | // TODO(b/134706060) we don't actually want to mount the "write" view by |
| 76 | // default, since it gives write access to all OBB dirs. |
| 77 | std::string androidSource( |
Martijn Coenen | adecd0a | 2019-12-13 16:05:03 +0100 | [diff] [blame] | 78 | StringPrintf("/mnt/runtime/default/%s/%d/Android", label.c_str(), userId)); |
Martijn Coenen | 5700261 | 2019-11-28 11:56:13 +0100 | [diff] [blame] | 79 | std::string androidTarget( |
| 80 | StringPrintf("/mnt/user/%d/%s/%d/Android", userId, label.c_str(), userId)); |
| 81 | |
| 82 | if (access(androidSource.c_str(), F_OK) != 0) { |
| 83 | // Android path may not exist yet if users has just been created; create it on |
| 84 | // the lower fs. |
| 85 | if (fs_prepare_dir(androidSource.c_str(), 0771, AID_ROOT, AID_ROOT) != 0) { |
| 86 | PLOG(ERROR) << "Failed to create " << androidSource; |
| 87 | return -errno; |
| 88 | } |
| 89 | } |
| 90 | LOG(INFO) << "Bind mounting " << androidSource << " on " << androidTarget; |
| 91 | auto status = BindMount(androidSource, androidTarget); |
| 92 | if (status != OK) { |
| 93 | return status; |
| 94 | } |
| 95 | LOG(INFO) << "Bind mounted " << androidSource << " on " << androidTarget; |
| 96 | |
| 97 | return OK; |
| 98 | } |
| 99 | |
| 100 | static status_t unmountFuseBindMounts(int userId, const std::string& label) { |
| 101 | std::string androidTarget( |
| 102 | StringPrintf("/mnt/user/%d/%s/%d/Android", userId, label.c_str(), userId)); |
| 103 | |
| 104 | LOG(INFO) << "Unmounting " << androidTarget; |
| 105 | auto status = UnmountTree(androidTarget); |
| 106 | if (status != OK) { |
| 107 | return status; |
| 108 | } |
| 109 | LOG(INFO) << "Unmounted " << androidTarget; |
| 110 | |
| 111 | return OK; |
| 112 | } |
| 113 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 114 | status_t EmulatedVolume::doMount() { |
| 115 | std::string label = getLabel(); |
| 116 | bool isVisible = getMountFlags() & MountFlags::kVisible; |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 117 | |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 118 | mSdcardFsDefault = StringPrintf("/mnt/runtime/default/%s", label.c_str()); |
| 119 | mSdcardFsRead = StringPrintf("/mnt/runtime/read/%s", label.c_str()); |
| 120 | mSdcardFsWrite = StringPrintf("/mnt/runtime/write/%s", label.c_str()); |
| 121 | mSdcardFsFull = StringPrintf("/mnt/runtime/full/%s", label.c_str()); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 122 | |
| 123 | setInternalPath(mRawPath); |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 124 | setPath(StringPrintf("/storage/%s", label.c_str())); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 125 | |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 126 | if (fs_prepare_dir(mSdcardFsDefault.c_str(), 0700, AID_ROOT, AID_ROOT) || |
| 127 | fs_prepare_dir(mSdcardFsRead.c_str(), 0700, AID_ROOT, AID_ROOT) || |
| 128 | fs_prepare_dir(mSdcardFsWrite.c_str(), 0700, AID_ROOT, AID_ROOT) || |
| 129 | fs_prepare_dir(mSdcardFsFull.c_str(), 0700, AID_ROOT, AID_ROOT)) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 130 | PLOG(ERROR) << getId() << " failed to create mount points"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 131 | return -errno; |
| 132 | } |
| 133 | |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 134 | dev_t before = GetDevice(mSdcardFsFull); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 135 | |
Abhijeet Kaur | 01fa0e0 | 2019-12-13 10:26:32 +0000 | [diff] [blame] | 136 | bool isFuse = base::GetBoolProperty(kPropFuse, false); |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 137 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 138 | // Mount sdcardfs regardless of FUSE, since we need it to bind-mount on top of the |
| 139 | // FUSE volume for various reasons. |
| 140 | if (getMountUserId() == 0) { |
| 141 | LOG(INFO) << "Executing sdcardfs"; |
| 142 | int sdcardFsPid; |
| 143 | if (!(sdcardFsPid = fork())) { |
| 144 | // clang-format off |
| 145 | if (execl(kSdcardFsPath, kSdcardFsPath, |
| 146 | "-u", "1023", // AID_MEDIA_RW |
| 147 | "-g", "1023", // AID_MEDIA_RW |
| 148 | "-m", |
| 149 | "-w", |
| 150 | "-G", |
| 151 | "-i", |
| 152 | "-o", |
| 153 | mRawPath.c_str(), |
| 154 | label.c_str(), |
| 155 | NULL)) { |
| 156 | // clang-format on |
| 157 | PLOG(ERROR) << "Failed to exec"; |
| 158 | } |
| 159 | |
| 160 | LOG(ERROR) << "sdcardfs exiting"; |
| 161 | _exit(1); |
| 162 | } |
| 163 | |
| 164 | if (sdcardFsPid == -1) { |
| 165 | PLOG(ERROR) << getId() << " failed to fork"; |
| 166 | return -errno; |
| 167 | } |
| 168 | |
| 169 | nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME); |
| 170 | while (before == GetDevice(mSdcardFsFull)) { |
| 171 | LOG(DEBUG) << "Waiting for sdcardfs to spin up..."; |
| 172 | usleep(50000); // 50ms |
| 173 | |
| 174 | nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME); |
| 175 | if (nanoseconds_to_milliseconds(now - start) > 5000) { |
| 176 | LOG(WARNING) << "Timed out while waiting for sdcardfs to spin up"; |
| 177 | return -ETIMEDOUT; |
| 178 | } |
| 179 | } |
| 180 | /* sdcardfs will have exited already. The filesystem will still be running */ |
| 181 | TEMP_FAILURE_RETRY(waitpid(sdcardFsPid, nullptr, 0)); |
| 182 | sdcardFsPid = 0; |
| 183 | } |
| 184 | if (isFuse && isVisible) { |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 185 | LOG(INFO) << "Mounting emulated fuse volume"; |
Nandana Dutt | a914cc7 | 2019-08-29 15:22:42 +0100 | [diff] [blame] | 186 | android::base::unique_fd fd; |
Zim | 981222f | 2019-09-09 10:24:44 +0100 | [diff] [blame] | 187 | int user_id = getMountUserId(); |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 188 | int result = MountUserFuse(user_id, getInternalPath(), label, &fd); |
Zim | 981222f | 2019-09-09 10:24:44 +0100 | [diff] [blame] | 189 | |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 190 | if (result != 0) { |
| 191 | PLOG(ERROR) << "Failed to mount emulated fuse volume"; |
| 192 | return -result; |
| 193 | } |
Zim | 5048b4b | 2019-11-19 09:16:03 +0000 | [diff] [blame] | 194 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 195 | mFuseMounted = true; |
Zim | 5048b4b | 2019-11-19 09:16:03 +0000 | [diff] [blame] | 196 | auto callback = getMountCallback(); |
| 197 | if (callback) { |
| 198 | bool is_ready = false; |
| 199 | callback->onVolumeChecking(std::move(fd), getPath(), getInternalPath(), &is_ready); |
| 200 | if (!is_ready) { |
| 201 | return -EIO; |
| 202 | } |
| 203 | } |
Martijn Coenen | 5700261 | 2019-11-28 11:56:13 +0100 | [diff] [blame] | 204 | |
| 205 | // Only do the bind-mounts when we know for sure the FUSE daemon can resolve the path. |
| 206 | return mountFuseBindMounts(user_id, label); |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 207 | } |
| 208 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 209 | return OK; |
| 210 | } |
| 211 | |
| 212 | status_t EmulatedVolume::doUnmount() { |
Martijn Coenen | 8f1e7f2 | 2019-11-29 15:38:55 +0100 | [diff] [blame] | 213 | int userId = getMountUserId(); |
| 214 | |
| 215 | // Kill all processes using the filesystem before we unmount it. If we |
| 216 | // unmount the filesystem first, most file system operations will return |
Narayan Kamath | ea243a3 | 2016-01-21 12:26:05 +0000 | [diff] [blame] | 217 | // ENOTCONN until the unmount completes. This is an exotic and unusual |
| 218 | // error code and might cause broken behaviour in applications. |
Martijn Coenen | 8f1e7f2 | 2019-11-29 15:38:55 +0100 | [diff] [blame] | 219 | if (mFuseMounted) { |
| 220 | // For FUSE specifically, we have an emulated volume per user, so only kill |
| 221 | // processes using files from this particular user. |
| 222 | std::string user_path(StringPrintf("%s/%d", getPath().c_str(), getMountUserId())); |
| 223 | LOG(INFO) << "Killing all processes referencing " << user_path; |
| 224 | KillProcessesUsingPath(user_path); |
| 225 | } else { |
| 226 | KillProcessesUsingPath(getPath()); |
| 227 | } |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 228 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 229 | if (mFuseMounted) { |
| 230 | std::string label = getLabel(); |
Martijn Coenen | 5700261 | 2019-11-28 11:56:13 +0100 | [diff] [blame] | 231 | // Ignoring unmount return status because we do want to try to unmount |
| 232 | // the rest cleanly. |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 233 | |
Martijn Coenen | 5700261 | 2019-11-28 11:56:13 +0100 | [diff] [blame] | 234 | unmountFuseBindMounts(userId, label); |
| 235 | if (UnmountUserFuse(userId, getInternalPath(), label) != OK) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 236 | PLOG(INFO) << "UnmountUserFuse failed on emulated fuse volume"; |
| 237 | return -errno; |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 238 | } |
| 239 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 240 | mFuseMounted = false; |
| 241 | } |
| 242 | if (getMountUserId() != 0) { |
Zim | 2d45d9b | 2019-11-14 16:19:05 +0000 | [diff] [blame] | 243 | // For sdcardfs, only unmount for user 0, since user 0 will always be running |
| 244 | // and the paths don't change for different users. |
| 245 | return OK; |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 248 | ForceUnmount(mSdcardFsDefault); |
| 249 | ForceUnmount(mSdcardFsRead); |
| 250 | ForceUnmount(mSdcardFsWrite); |
| 251 | ForceUnmount(mSdcardFsFull); |
Narayan Kamath | ea243a3 | 2016-01-21 12:26:05 +0000 | [diff] [blame] | 252 | |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 253 | rmdir(mSdcardFsDefault.c_str()); |
| 254 | rmdir(mSdcardFsRead.c_str()); |
| 255 | rmdir(mSdcardFsWrite.c_str()); |
| 256 | rmdir(mSdcardFsFull.c_str()); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 257 | |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 258 | mSdcardFsDefault.clear(); |
| 259 | mSdcardFsRead.clear(); |
| 260 | mSdcardFsWrite.clear(); |
| 261 | mSdcardFsFull.clear(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 262 | |
| 263 | return OK; |
| 264 | } |
| 265 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 266 | } // namespace vold |
| 267 | } // namespace android |