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"; |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 52 | EmulatedVolume::EmulatedVolume(const std::string& rawPath, dev_t device, const std::string& fsUuid, |
| 53 | int userId) |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 54 | : VolumeBase(Type::kEmulated) { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 55 | setId(StringPrintf("emulated:%u,%u;%u", major(device), minor(device), userId)); |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 56 | mRawPath = rawPath; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 57 | mLabel = fsUuid; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 60 | EmulatedVolume::~EmulatedVolume() {} |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 61 | |
| 62 | status_t EmulatedVolume::doMount() { |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 63 | // We could have migrated storage to an adopted private volume, so always |
| 64 | // call primary storage "emulated" to avoid media rescans. |
| 65 | std::string label = mLabel; |
| 66 | if (getMountFlags() & MountFlags::kPrimary) { |
| 67 | label = "emulated"; |
| 68 | } |
| 69 | |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 70 | mSdcardFsDefault = StringPrintf("/mnt/runtime/default/%s", label.c_str()); |
| 71 | mSdcardFsRead = StringPrintf("/mnt/runtime/read/%s", label.c_str()); |
| 72 | mSdcardFsWrite = StringPrintf("/mnt/runtime/write/%s", label.c_str()); |
| 73 | mSdcardFsFull = StringPrintf("/mnt/runtime/full/%s", label.c_str()); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 74 | |
| 75 | setInternalPath(mRawPath); |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 76 | setPath(StringPrintf("/storage/%s", label.c_str())); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 77 | |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 78 | if (fs_prepare_dir(mSdcardFsDefault.c_str(), 0700, AID_ROOT, AID_ROOT) || |
| 79 | fs_prepare_dir(mSdcardFsRead.c_str(), 0700, AID_ROOT, AID_ROOT) || |
| 80 | fs_prepare_dir(mSdcardFsWrite.c_str(), 0700, AID_ROOT, AID_ROOT) || |
| 81 | fs_prepare_dir(mSdcardFsFull.c_str(), 0700, AID_ROOT, AID_ROOT)) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 82 | PLOG(ERROR) << getId() << " failed to create mount points"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 83 | return -errno; |
| 84 | } |
| 85 | |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 86 | dev_t before = GetDevice(mSdcardFsFull); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 87 | |
shafik | 5cf32b5 | 2019-09-25 13:56:01 +0100 | [diff] [blame] | 88 | bool isFuse = base::GetBoolProperty(kPropFuseSnapshot, false); |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 89 | |
| 90 | if (isFuse) { |
| 91 | LOG(INFO) << "Mounting emulated fuse volume"; |
Nandana Dutt | a914cc7 | 2019-08-29 15:22:42 +0100 | [diff] [blame] | 92 | android::base::unique_fd fd; |
Zim | 981222f | 2019-09-09 10:24:44 +0100 | [diff] [blame] | 93 | int user_id = getMountUserId(); |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 94 | int result = MountUserFuse(user_id, getInternalPath(), label, &fd); |
Zim | 981222f | 2019-09-09 10:24:44 +0100 | [diff] [blame] | 95 | |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 96 | if (result != 0) { |
| 97 | PLOG(ERROR) << "Failed to mount emulated fuse volume"; |
| 98 | return -result; |
| 99 | } |
Zim | 5048b4b | 2019-11-19 09:16:03 +0000 | [diff] [blame] | 100 | |
| 101 | auto callback = getMountCallback(); |
| 102 | if (callback) { |
| 103 | bool is_ready = false; |
| 104 | callback->onVolumeChecking(std::move(fd), getPath(), getInternalPath(), &is_ready); |
| 105 | if (!is_ready) { |
| 106 | return -EIO; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return OK; |
Zim | 2d45d9b | 2019-11-14 16:19:05 +0000 | [diff] [blame] | 111 | } else if (getMountUserId() != 0) { |
| 112 | // For sdcardfs, only mount for user 0, since user 0 will always be running |
| 113 | // and the paths don't change for different users. Trying to double mount |
| 114 | // will cause sepolicy to scream since sdcardfs prevents 'mounton' |
| 115 | return OK; |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 116 | } |
| 117 | |
Martijn Coenen | 10b122b | 2019-12-04 15:48:42 +0100 | [diff] [blame] | 118 | LOG(INFO) << "Executing sdcardfs"; |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 119 | int sdcardFsPid; |
| 120 | if (!(sdcardFsPid = fork())) { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 121 | // clang-format off |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 122 | if (execl(kSdcardFsPath, kSdcardFsPath, |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 123 | "-u", "1023", // AID_MEDIA_RW |
| 124 | "-g", "1023", // AID_MEDIA_RW |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 125 | "-m", |
| 126 | "-w", |
Rom Lemarchand | 958c216 | 2017-09-15 18:48:01 +0000 | [diff] [blame] | 127 | "-G", |
Jeff Sharkey | d7e5176 | 2018-01-08 11:48:07 -0700 | [diff] [blame] | 128 | "-i", |
Sudheer Shanka | 8cad97b | 2019-03-05 10:31:27 -0800 | [diff] [blame] | 129 | "-o", |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 130 | mRawPath.c_str(), |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 131 | label.c_str(), |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 132 | NULL)) { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 133 | // clang-format on |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 134 | PLOG(ERROR) << "Failed to exec"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 135 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 136 | |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 137 | LOG(ERROR) << "sdcardfs exiting"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 138 | _exit(1); |
| 139 | } |
| 140 | |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 141 | if (sdcardFsPid == -1) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 142 | PLOG(ERROR) << getId() << " failed to fork"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 143 | return -errno; |
| 144 | } |
| 145 | |
Jeff Sharkey | 7bdf4d5 | 2017-09-18 14:47:10 -0600 | [diff] [blame] | 146 | nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME); |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 147 | while (before == GetDevice(mSdcardFsFull)) { |
| 148 | LOG(DEBUG) << "Waiting for sdcardfs to spin up..."; |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 149 | usleep(50000); // 50ms |
Jeff Sharkey | 7bdf4d5 | 2017-09-18 14:47:10 -0600 | [diff] [blame] | 150 | |
| 151 | nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME); |
| 152 | if (nanoseconds_to_milliseconds(now - start) > 5000) { |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 153 | LOG(WARNING) << "Timed out while waiting for sdcardfs to spin up"; |
Jeff Sharkey | 7bdf4d5 | 2017-09-18 14:47:10 -0600 | [diff] [blame] | 154 | return -ETIMEDOUT; |
| 155 | } |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 156 | } |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 157 | /* sdcardfs will have exited already. The filesystem will still be running */ |
| 158 | TEMP_FAILURE_RETRY(waitpid(sdcardFsPid, nullptr, 0)); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 159 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 160 | return OK; |
| 161 | } |
| 162 | |
| 163 | status_t EmulatedVolume::doUnmount() { |
Narayan Kamath | ea243a3 | 2016-01-21 12:26:05 +0000 | [diff] [blame] | 164 | // Unmount the storage before we kill the FUSE process. If we kill |
| 165 | // the FUSE process first, most file system operations will return |
| 166 | // ENOTCONN until the unmount completes. This is an exotic and unusual |
| 167 | // error code and might cause broken behaviour in applications. |
| 168 | KillProcessesUsingPath(getPath()); |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 169 | |
shafik | 5cf32b5 | 2019-09-25 13:56:01 +0100 | [diff] [blame] | 170 | bool isFuse = base::GetBoolProperty(kPropFuseSnapshot, false); |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 171 | if (isFuse) { |
| 172 | // We could have migrated storage to an adopted private volume, so always |
| 173 | // call primary storage "emulated" to avoid media rescans. |
| 174 | std::string label = mLabel; |
| 175 | if (getMountFlags() & MountFlags::kPrimary) { |
| 176 | label = "emulated"; |
| 177 | } |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 178 | |
| 179 | std::string fuse_path(StringPrintf("/mnt/user/%d/%s", getMountUserId(), label.c_str())); |
| 180 | std::string pass_through_path( |
| 181 | StringPrintf("/mnt/pass_through/%d/%s", getMountUserId(), label.c_str())); |
| 182 | if (UnmountUserFuse(pass_through_path, fuse_path) != OK) { |
| 183 | PLOG(INFO) << "UnmountUserFuse failed on emulated fuse volume"; |
| 184 | return -errno; |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 185 | } |
| 186 | |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 187 | rmdir(fuse_path.c_str()); |
| 188 | rmdir(pass_through_path.c_str()); |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 189 | return OK; |
Zim | 2d45d9b | 2019-11-14 16:19:05 +0000 | [diff] [blame] | 190 | } else if (getMountUserId() != 0) { |
| 191 | // For sdcardfs, only unmount for user 0, since user 0 will always be running |
| 192 | // and the paths don't change for different users. |
| 193 | return OK; |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 196 | ForceUnmount(mSdcardFsDefault); |
| 197 | ForceUnmount(mSdcardFsRead); |
| 198 | ForceUnmount(mSdcardFsWrite); |
| 199 | ForceUnmount(mSdcardFsFull); |
Narayan Kamath | ea243a3 | 2016-01-21 12:26:05 +0000 | [diff] [blame] | 200 | |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 201 | rmdir(mSdcardFsDefault.c_str()); |
| 202 | rmdir(mSdcardFsRead.c_str()); |
| 203 | rmdir(mSdcardFsWrite.c_str()); |
| 204 | rmdir(mSdcardFsFull.c_str()); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 205 | |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 206 | mSdcardFsDefault.clear(); |
| 207 | mSdcardFsRead.clear(); |
| 208 | mSdcardFsWrite.clear(); |
| 209 | mSdcardFsFull.clear(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 210 | |
| 211 | return OK; |
| 212 | } |
| 213 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 214 | } // namespace vold |
| 215 | } // namespace android |