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