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 "PublicVolume.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" |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 21 | #include "VolumeManager.h" |
Jeff Sharkey | 37ba125 | 2018-01-19 10:55:18 +0900 | [diff] [blame] | 22 | #include "fs/Exfat.h" |
| 23 | #include "fs/Vfat.h" |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 24 | |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 25 | #include <android-base/logging.h> |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 26 | #include <android-base/properties.h> |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -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 | |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 41 | using android::base::GetBoolProperty; |
Dan Albert | ae9e890 | 2015-03-16 10:35:17 -0700 | [diff] [blame] | 42 | using android::base::StringPrintf; |
Omar Eissa | fedaddb | 2025-03-12 16:09:16 +0000 | [diff] [blame] | 43 | namespace flags = android::vold::flags; |
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 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 50 | static const char* kAsecPath = "/mnt/secure/asec"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 51 | |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 52 | PublicVolume::PublicVolume(dev_t device) : VolumeBase(Type::kPublic), mDevice(device) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 53 | setId(StringPrintf("public:%u,%u", major(device), minor(device))); |
| 54 | mDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str()); |
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(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 59 | PublicVolume::~PublicVolume() {} |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 60 | |
| 61 | status_t PublicVolume::readMetadata() { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 62 | status_t res = ReadMetadataUntrusted(mDevPath, &mFsType, &mFsUuid, &mFsLabel); |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 63 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 64 | auto listener = getListener(); |
| 65 | if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel); |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 66 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 67 | return res; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | status_t PublicVolume::initAsecStage() { |
| 71 | std::string legacyPath(mRawPath + "/android_secure"); |
| 72 | std::string securePath(mRawPath + "/.android_secure"); |
| 73 | |
| 74 | // Recover legacy secure path |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 75 | if (!access(legacyPath.c_str(), R_OK | X_OK) && access(securePath.c_str(), R_OK | X_OK)) { |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 76 | if (rename(legacyPath.c_str(), securePath.c_str())) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 77 | PLOG(WARNING) << getId() << " failed to rename legacy ASEC dir"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 81 | if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) { |
| 82 | if (errno != EEXIST) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 83 | PLOG(WARNING) << getId() << " creating ASEC stage failed"; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 84 | return -errno; |
| 85 | } |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 88 | BindMount(securePath, kAsecPath); |
| 89 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 90 | return OK; |
| 91 | } |
| 92 | |
Omar Eissa | d9c4b23 | 2025-03-13 11:52:43 +0000 | [diff] [blame] | 93 | std::string PublicVolume::getStableName() { |
| 94 | // Use UUID as stable name, if available |
| 95 | std::string stableName = getId(); |
| 96 | if (!mFsUuid.empty()) { |
| 97 | stableName = mFsUuid; |
| 98 | } |
| 99 | return stableName; |
| 100 | } |
| 101 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 102 | status_t PublicVolume::doCreate() { |
| 103 | return CreateDeviceNode(mDevPath, mDevice); |
| 104 | } |
| 105 | |
| 106 | status_t PublicVolume::doDestroy() { |
| 107 | return DestroyDeviceNode(mDevPath); |
| 108 | } |
| 109 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 110 | status_t PublicVolume::doMount() { |
Youkichi Hosoi | 4461c5a | 2021-11-13 16:27:02 +0900 | [diff] [blame] | 111 | bool isVisible = isVisibleForWrite(); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 112 | readMetadata(); |
| 113 | |
Jeff Sharkey | 37ba125 | 2018-01-19 10:55:18 +0900 | [diff] [blame] | 114 | if (mFsType == "vfat" && vfat::IsSupported()) { |
| 115 | if (vfat::Check(mDevPath)) { |
| 116 | LOG(ERROR) << getId() << " failed filesystem check"; |
| 117 | return -EIO; |
| 118 | } |
| 119 | } else if (mFsType == "exfat" && exfat::IsSupported()) { |
| 120 | if (exfat::Check(mDevPath)) { |
| 121 | LOG(ERROR) << getId() << " failed filesystem check"; |
| 122 | return -EIO; |
| 123 | } |
| 124 | } else { |
Makoto Onuki | c82c9ce | 2015-06-24 13:30:45 -0700 | [diff] [blame] | 125 | LOG(ERROR) << getId() << " unsupported filesystem " << mFsType; |
| 126 | return -EIO; |
| 127 | } |
| 128 | |
Omar Eissa | d9c4b23 | 2025-03-13 11:52:43 +0000 | [diff] [blame] | 129 | std::string stableName = getStableName(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 130 | |
| 131 | mRawPath = StringPrintf("/mnt/media_rw/%s", stableName.c_str()); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 132 | |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 133 | mSdcardFsDefault = StringPrintf("/mnt/runtime/default/%s", stableName.c_str()); |
| 134 | mSdcardFsRead = StringPrintf("/mnt/runtime/read/%s", stableName.c_str()); |
| 135 | mSdcardFsWrite = StringPrintf("/mnt/runtime/write/%s", stableName.c_str()); |
| 136 | mSdcardFsFull = StringPrintf("/mnt/runtime/full/%s", stableName.c_str()); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 137 | |
| 138 | setInternalPath(mRawPath); |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 139 | if (isVisible) { |
Jeff Sharkey | 8474ee3 | 2015-07-30 16:54:23 -0700 | [diff] [blame] | 140 | setPath(StringPrintf("/storage/%s", stableName.c_str())); |
| 141 | } else { |
| 142 | setPath(mRawPath); |
| 143 | } |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 144 | |
Qin Chao | e0074f1 | 2015-12-15 15:20:41 +0800 | [diff] [blame] | 145 | if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT)) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 146 | PLOG(ERROR) << getId() << " failed to create mount points"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 147 | return -errno; |
| 148 | } |
| 149 | |
Jeff Sharkey | 37ba125 | 2018-01-19 10:55:18 +0900 | [diff] [blame] | 150 | if (mFsType == "vfat") { |
Zim | c9a2be4 | 2020-01-24 22:03:02 +0000 | [diff] [blame] | 151 | if (vfat::Mount(mDevPath, mRawPath, false, false, false, AID_ROOT, |
| 152 | (isVisible ? AID_MEDIA_RW : AID_EXTERNAL_STORAGE), 0007, true)) { |
Jeff Sharkey | 37ba125 | 2018-01-19 10:55:18 +0900 | [diff] [blame] | 153 | PLOG(ERROR) << getId() << " failed to mount " << mDevPath; |
| 154 | return -EIO; |
| 155 | } |
| 156 | } else if (mFsType == "exfat") { |
Zim | c9a2be4 | 2020-01-24 22:03:02 +0000 | [diff] [blame] | 157 | if (exfat::Mount(mDevPath, mRawPath, AID_ROOT, |
| 158 | (isVisible ? AID_MEDIA_RW : AID_EXTERNAL_STORAGE), 0007)) { |
Jeff Sharkey | 37ba125 | 2018-01-19 10:55:18 +0900 | [diff] [blame] | 159 | PLOG(ERROR) << getId() << " failed to mount " << mDevPath; |
| 160 | return -EIO; |
| 161 | } |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 164 | if (getMountFlags() & MountFlags::kPrimary) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 165 | initAsecStage(); |
| 166 | } |
| 167 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 168 | if (!isVisible) { |
Martijn Coenen | adcc845 | 2019-12-09 14:18:01 +0100 | [diff] [blame] | 169 | // Not visible to apps, so no need to spin up sdcardfs or FUSE |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 170 | return OK; |
| 171 | } |
| 172 | |
Martijn Coenen | 86f21a2 | 2020-01-06 09:48:14 +0100 | [diff] [blame] | 173 | if (mUseSdcardFs) { |
| 174 | if (fs_prepare_dir(mSdcardFsDefault.c_str(), 0700, AID_ROOT, AID_ROOT) || |
| 175 | fs_prepare_dir(mSdcardFsRead.c_str(), 0700, AID_ROOT, AID_ROOT) || |
| 176 | fs_prepare_dir(mSdcardFsWrite.c_str(), 0700, AID_ROOT, AID_ROOT) || |
| 177 | fs_prepare_dir(mSdcardFsFull.c_str(), 0700, AID_ROOT, AID_ROOT)) { |
| 178 | PLOG(ERROR) << getId() << " failed to create sdcardfs mount points"; |
| 179 | return -errno; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Martijn Coenen | 86f21a2 | 2020-01-06 09:48:14 +0100 | [diff] [blame] | 182 | dev_t before = GetDevice(mSdcardFsFull); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 183 | |
Martijn Coenen | 86f21a2 | 2020-01-06 09:48:14 +0100 | [diff] [blame] | 184 | int sdcardFsPid; |
| 185 | if (!(sdcardFsPid = fork())) { |
| 186 | if (getMountFlags() & MountFlags::kPrimary) { |
| 187 | // clang-format off |
| 188 | if (execl(kSdcardFsPath, kSdcardFsPath, |
| 189 | "-u", "1023", // AID_MEDIA_RW |
| 190 | "-g", "1023", // AID_MEDIA_RW |
| 191 | "-U", std::to_string(getMountUserId()).c_str(), |
| 192 | "-w", |
| 193 | mRawPath.c_str(), |
| 194 | stableName.c_str(), |
| 195 | NULL)) { |
| 196 | // clang-format on |
| 197 | PLOG(ERROR) << "Failed to exec"; |
| 198 | } |
| 199 | } else { |
| 200 | // clang-format off |
| 201 | if (execl(kSdcardFsPath, kSdcardFsPath, |
| 202 | "-u", "1023", // AID_MEDIA_RW |
| 203 | "-g", "1023", // AID_MEDIA_RW |
| 204 | "-U", std::to_string(getMountUserId()).c_str(), |
| 205 | mRawPath.c_str(), |
| 206 | stableName.c_str(), |
| 207 | NULL)) { |
| 208 | // clang-format on |
| 209 | PLOG(ERROR) << "Failed to exec"; |
| 210 | } |
| 211 | } |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 212 | |
Martijn Coenen | 86f21a2 | 2020-01-06 09:48:14 +0100 | [diff] [blame] | 213 | LOG(ERROR) << "sdcardfs exiting"; |
| 214 | _exit(1); |
Jeff Sharkey | 7bdf4d5 | 2017-09-18 14:47:10 -0600 | [diff] [blame] | 215 | } |
Martijn Coenen | 86f21a2 | 2020-01-06 09:48:14 +0100 | [diff] [blame] | 216 | |
| 217 | if (sdcardFsPid == -1) { |
| 218 | PLOG(ERROR) << getId() << " failed to fork"; |
| 219 | return -errno; |
| 220 | } |
| 221 | |
| 222 | nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME); |
| 223 | while (before == GetDevice(mSdcardFsFull)) { |
| 224 | LOG(DEBUG) << "Waiting for sdcardfs to spin up..."; |
| 225 | usleep(50000); // 50ms |
| 226 | |
| 227 | nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME); |
| 228 | if (nanoseconds_to_milliseconds(now - start) > 5000) { |
| 229 | LOG(WARNING) << "Timed out while waiting for sdcardfs to spin up"; |
| 230 | return -ETIMEDOUT; |
| 231 | } |
| 232 | } |
| 233 | /* sdcardfs will have exited already. The filesystem will still be running */ |
| 234 | TEMP_FAILURE_RETRY(waitpid(sdcardFsPid, nullptr, 0)); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Zim | 415d99d | 2020-06-29 19:50:47 +0100 | [diff] [blame] | 237 | // We need to mount FUSE *after* sdcardfs, since the FUSE daemon may depend |
| 238 | // on sdcardfs being up. |
| 239 | LOG(INFO) << "Mounting public fuse volume"; |
| 240 | android::base::unique_fd fd; |
| 241 | int user_id = getMountUserId(); |
| 242 | int result = MountUserFuse(user_id, getInternalPath(), stableName, &fd); |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 243 | |
Zim | 415d99d | 2020-06-29 19:50:47 +0100 | [diff] [blame] | 244 | if (result != 0) { |
| 245 | LOG(ERROR) << "Failed to mount public fuse volume"; |
| 246 | doUnmount(); |
| 247 | return -result; |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 248 | } |
| 249 | |
Zim | 415d99d | 2020-06-29 19:50:47 +0100 | [diff] [blame] | 250 | mFuseMounted = true; |
| 251 | auto callback = getMountCallback(); |
| 252 | if (callback) { |
| 253 | bool is_ready = false; |
| 254 | callback->onVolumeChecking(std::move(fd), getPath(), getInternalPath(), &is_ready); |
| 255 | if (!is_ready) { |
| 256 | LOG(ERROR) << "Failed to complete public volume mount"; |
| 257 | doUnmount(); |
| 258 | return -EIO; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | ConfigureReadAheadForFuse(GetFuseMountPathForUser(user_id, stableName), 256u); |
| 263 | |
| 264 | // See comment in model/EmulatedVolume.cpp |
| 265 | ConfigureMaxDirtyRatioForFuse(GetFuseMountPathForUser(user_id, stableName), 40u); |
| 266 | |
himanshuz | 0ad0862 | 2023-07-27 21:15:00 +0000 | [diff] [blame] | 267 | auto vol_manager = VolumeManager::Instance(); |
| 268 | // Create bind mounts for all running users |
| 269 | for (userid_t started_user : vol_manager->getStartedUsers()) { |
| 270 | userid_t mountUserId = getMountUserId(); |
| 271 | if (started_user == mountUserId) { |
| 272 | // No need to bind mount for the user that owns the mount |
| 273 | continue; |
| 274 | } |
| 275 | if (mountUserId != VolumeManager::Instance()->getSharedStorageUser(started_user)) { |
| 276 | // No need to bind if the user does not share storage with the mount owner |
| 277 | continue; |
| 278 | } |
Himanshu Gupta | 4f17969 | 2024-05-06 11:40:19 +0100 | [diff] [blame] | 279 | // Create mount directory for the user as there is a chance that no other Volume is mounted |
| 280 | // for the user (ex: if the user is just started), so /mnt/user/user_id does not exist yet. |
| 281 | auto mountDirStatus = PrepareMountDirForUser(started_user); |
| 282 | if (mountDirStatus != OK) { |
| 283 | LOG(ERROR) << "Failed to create Mount Directory for user " << started_user; |
| 284 | } |
himanshuz | 0ad0862 | 2023-07-27 21:15:00 +0000 | [diff] [blame] | 285 | auto bindMountStatus = bindMountForUser(started_user); |
| 286 | if (bindMountStatus != OK) { |
| 287 | LOG(ERROR) << "Bind Mounting Public Volume: " << stableName |
| 288 | << " for user: " << started_user << "Failed. Error: " << bindMountStatus; |
| 289 | } |
| 290 | } |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 291 | return OK; |
| 292 | } |
| 293 | |
himanshuz | 0ad0862 | 2023-07-27 21:15:00 +0000 | [diff] [blame] | 294 | status_t PublicVolume::bindMountForUser(userid_t user_id) { |
| 295 | userid_t mountUserId = getMountUserId(); |
Omar Eissa | d9c4b23 | 2025-03-13 11:52:43 +0000 | [diff] [blame] | 296 | std::string stableName = getStableName(); |
himanshuz | 0ad0862 | 2023-07-27 21:15:00 +0000 | [diff] [blame] | 297 | |
| 298 | LOG(INFO) << "Bind Mounting Public Volume for user: " << user_id |
| 299 | << ".Mount owner: " << mountUserId; |
| 300 | auto sourcePath = GetFuseMountPathForUser(mountUserId, stableName); |
| 301 | auto destPath = GetFuseMountPathForUser(user_id, stableName); |
| 302 | PrepareDir(destPath, 0770, AID_ROOT, AID_MEDIA_RW); |
| 303 | auto mountRes = BindMount(sourcePath, destPath); |
| 304 | LOG(INFO) << "Mount status: " << mountRes; |
| 305 | |
| 306 | return mountRes; |
| 307 | } |
| 308 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 309 | status_t PublicVolume::doUnmount() { |
John Cormie | 25cc7e3 | 2016-04-18 14:23:29 -0700 | [diff] [blame] | 310 | // Unmount the storage before we kill the FUSE process. If we kill |
| 311 | // the FUSE process first, most file system operations will return |
| 312 | // ENOTCONN until the unmount completes. This is an exotic and unusual |
| 313 | // error code and might cause broken behaviour in applications. |
Omar Eissa | fedaddb | 2025-03-12 16:09:16 +0000 | [diff] [blame] | 314 | |
| 315 | // We don't kill processes here if enhance_fuse_unmount as we make sure that we kill processes |
| 316 | // only if unmounting failed |
| 317 | if (!mFuseMounted || !flags::enhance_fuse_unmount()) { |
| 318 | KillProcessesUsingPath(getPath()); |
| 319 | } |
Jeff Sharkey | 8aff854 | 2016-03-30 20:37:28 -0600 | [diff] [blame] | 320 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 321 | if (mFuseMounted) { |
Omar Eissa | d9c4b23 | 2025-03-13 11:52:43 +0000 | [diff] [blame] | 322 | std::string stableName = getStableName(); |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 323 | |
himanshuz | 0ad0862 | 2023-07-27 21:15:00 +0000 | [diff] [blame] | 324 | // Unmount bind mounts for running users |
| 325 | auto vol_manager = VolumeManager::Instance(); |
Omar Eissa | fedaddb | 2025-03-12 16:09:16 +0000 | [diff] [blame] | 326 | userid_t user_id = getMountUserId(); |
| 327 | std::vector<std::string> bind_mount_paths; |
| 328 | for (userid_t started_user : vol_manager->getStartedUsers()) { |
himanshuz | 0ad0862 | 2023-07-27 21:15:00 +0000 | [diff] [blame] | 329 | if (started_user == user_id) { |
| 330 | // No need to remove bind mount for the user that owns the mount |
| 331 | continue; |
| 332 | } |
Omar Eissa | fedaddb | 2025-03-12 16:09:16 +0000 | [diff] [blame] | 333 | if (user_id != VolumeManager::Instance()->getSharedStorageUser(started_user)) { |
| 334 | // No need to remove bind mount |
| 335 | // if the user does not share storage with the mount owner |
| 336 | continue; |
| 337 | } |
| 338 | |
himanshuz | 0ad0862 | 2023-07-27 21:15:00 +0000 | [diff] [blame] | 339 | auto mountPath = GetFuseMountPathForUser(started_user, stableName); |
Omar Eissa | fedaddb | 2025-03-12 16:09:16 +0000 | [diff] [blame] | 340 | if (flags::enhance_fuse_unmount()) { |
| 341 | // Add it to list so that we unmount it as part of UnmountUserFuseEnhanced |
| 342 | bind_mount_paths.push_back(mountPath); |
| 343 | } else { |
| 344 | LOG(INFO) << "Removing Public Volume Bind Mount for: " << started_user; |
| 345 | ForceUnmount(mountPath); |
| 346 | rmdir(mountPath.c_str()); |
| 347 | } |
himanshuz | 0ad0862 | 2023-07-27 21:15:00 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Omar Eissa | fedaddb | 2025-03-12 16:09:16 +0000 | [diff] [blame] | 350 | if (flags::enhance_fuse_unmount()) { |
| 351 | status_t result = UnmountUserFuseEnhanced(getMountUserId(), getInternalPath(), |
| 352 | stableName, getPath(), bind_mount_paths); |
| 353 | if (result != OK) { |
| 354 | PLOG(INFO) << "UnmountUserFuseEnhanced failed on public fuse volume"; |
| 355 | return result; |
| 356 | } |
| 357 | } else { |
| 358 | if (UnmountUserFuse(getMountUserId(), getInternalPath(), stableName) != OK) { |
| 359 | PLOG(INFO) << "UnmountUserFuse failed on public fuse volume"; |
| 360 | return -errno; |
| 361 | } |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 362 | } |
| 363 | |
Omar Eissa | fedaddb | 2025-03-12 16:09:16 +0000 | [diff] [blame] | 364 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 365 | mFuseMounted = false; |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 366 | } |
| 367 | |
Jeff Sharkey | 48d81d3 | 2015-04-12 21:50:32 -0700 | [diff] [blame] | 368 | ForceUnmount(kAsecPath); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 369 | |
Martijn Coenen | 86f21a2 | 2020-01-06 09:48:14 +0100 | [diff] [blame] | 370 | if (mUseSdcardFs) { |
| 371 | ForceUnmount(mSdcardFsDefault); |
| 372 | ForceUnmount(mSdcardFsRead); |
| 373 | ForceUnmount(mSdcardFsWrite); |
| 374 | ForceUnmount(mSdcardFsFull); |
| 375 | |
| 376 | rmdir(mSdcardFsDefault.c_str()); |
| 377 | rmdir(mSdcardFsRead.c_str()); |
| 378 | rmdir(mSdcardFsWrite.c_str()); |
| 379 | rmdir(mSdcardFsFull.c_str()); |
| 380 | |
| 381 | mSdcardFsDefault.clear(); |
| 382 | mSdcardFsRead.clear(); |
| 383 | mSdcardFsWrite.clear(); |
| 384 | mSdcardFsFull.clear(); |
| 385 | } |
silver.chen | 4c45ac6 | 2021-07-07 20:27:47 +0800 | [diff] [blame] | 386 | |
| 387 | if (ForceUnmount(mRawPath) != 0){ |
| 388 | umount2(mRawPath.c_str(),MNT_DETACH); |
| 389 | PLOG(INFO) << "use umount lazy if force unmount fail"; |
| 390 | } |
| 391 | if(rmdir(mRawPath.c_str()) != 0) { |
| 392 | PLOG(INFO) << "rmdir mRawPath=" << mRawPath << " fail"; |
| 393 | KillProcessesUsingPath(getPath()); |
| 394 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 395 | mRawPath.clear(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 396 | |
| 397 | return OK; |
| 398 | } |
| 399 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 400 | status_t PublicVolume::doFormat(const std::string& fsType) { |
Alfred Piccioni | 564f6c6 | 2022-09-21 17:32:04 +0200 | [diff] [blame] | 401 | bool isVfatSup = vfat::IsSupported(); |
| 402 | bool isExfatSup = exfat::IsSupported(); |
Oleksiy Avramchenko | 4cff06d | 2018-05-23 18:59:48 +0200 | [diff] [blame] | 403 | status_t res = OK; |
| 404 | |
Alfred Piccioni | fc4934f | 2023-02-02 09:46:30 +0000 | [diff] [blame] | 405 | enum { NONE, VFAT, EXFAT } fsPick = NONE; |
Alfred Piccioni | 564f6c6 | 2022-09-21 17:32:04 +0200 | [diff] [blame] | 406 | |
| 407 | // Resolve auto requests |
| 408 | if (fsType == "auto" && isVfatSup && isExfatSup) { |
Oleksiy Avramchenko | 4cff06d | 2018-05-23 18:59:48 +0200 | [diff] [blame] | 409 | uint64_t size = 0; |
| 410 | |
| 411 | res = GetBlockDevSize(mDevPath, &size); |
| 412 | if (res != OK) { |
| 413 | LOG(ERROR) << "Couldn't get device size " << mDevPath; |
| 414 | return res; |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 415 | } |
Oleksiy Avramchenko | 4cff06d | 2018-05-23 18:59:48 +0200 | [diff] [blame] | 416 | |
| 417 | // If both vfat & exfat are supported use exfat for SDXC (>~32GiB) cards |
| 418 | if (size > 32896LL * 1024 * 1024) { |
Alfred Piccioni | 564f6c6 | 2022-09-21 17:32:04 +0200 | [diff] [blame] | 419 | fsPick = EXFAT; |
Oleksiy Avramchenko | 4cff06d | 2018-05-23 18:59:48 +0200 | [diff] [blame] | 420 | } else { |
Alfred Piccioni | 564f6c6 | 2022-09-21 17:32:04 +0200 | [diff] [blame] | 421 | fsPick = VFAT; |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 422 | } |
Alfred Piccioni | 564f6c6 | 2022-09-21 17:32:04 +0200 | [diff] [blame] | 423 | } else if (fsType == "auto" && isExfatSup) { |
| 424 | fsPick = EXFAT; |
| 425 | } else if (fsType == "auto" && isVfatSup) { |
| 426 | fsPick = VFAT; |
Oleksiy Avramchenko | 4cff06d | 2018-05-23 18:59:48 +0200 | [diff] [blame] | 427 | } |
| 428 | |
Alfred Piccioni | 564f6c6 | 2022-09-21 17:32:04 +0200 | [diff] [blame] | 429 | // Resolve explicit requests |
| 430 | if (fsType == "vfat" && isVfatSup) { |
| 431 | fsPick = VFAT; |
| 432 | } else if (fsType == "exfat" && isExfatSup) { |
| 433 | fsPick = EXFAT; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 434 | } |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 435 | |
Oleksiy Avramchenko | 4cff06d | 2018-05-23 18:59:48 +0200 | [diff] [blame] | 436 | if (WipeBlockDevice(mDevPath) != OK) { |
| 437 | LOG(WARNING) << getId() << " failed to wipe"; |
| 438 | } |
| 439 | |
Alfred Piccioni | 564f6c6 | 2022-09-21 17:32:04 +0200 | [diff] [blame] | 440 | if (fsPick == VFAT) { |
Oleksiy Avramchenko | 4cff06d | 2018-05-23 18:59:48 +0200 | [diff] [blame] | 441 | res = vfat::Format(mDevPath, 0); |
Alfred Piccioni | 564f6c6 | 2022-09-21 17:32:04 +0200 | [diff] [blame] | 442 | } else if (fsPick == EXFAT) { |
Oleksiy Avramchenko | 4cff06d | 2018-05-23 18:59:48 +0200 | [diff] [blame] | 443 | res = exfat::Format(mDevPath); |
Alfred Piccioni | 564f6c6 | 2022-09-21 17:32:04 +0200 | [diff] [blame] | 444 | } else { |
| 445 | LOG(ERROR) << "Unsupported filesystem " << fsType; |
| 446 | return -EINVAL; |
Oleksiy Avramchenko | 4cff06d | 2018-05-23 18:59:48 +0200 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | if (res != OK) { |
| 450 | LOG(ERROR) << getId() << " failed to format"; |
| 451 | res = -errno; |
| 452 | } |
| 453 | |
| 454 | return res; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 455 | } |
| 456 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 457 | } // namespace vold |
| 458 | } // namespace android |