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() { |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 100 | bool isVisible = getMountFlags() & MountFlags::kVisible; |
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 | |
Abhijeet Kaur | 01fa0e0 | 2019-12-13 10:26:32 +0000 | [diff] [blame] | 230 | bool isFuse = base::GetBoolProperty(kPropFuse, false); |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 231 | if (isFuse) { |
| 232 | // We need to mount FUSE *after* sdcardfs, since the FUSE daemon may depend |
| 233 | // on sdcardfs being up. |
| 234 | LOG(INFO) << "Mounting public fuse volume"; |
| 235 | android::base::unique_fd fd; |
| 236 | int user_id = getMountUserId(); |
| 237 | int result = MountUserFuse(user_id, getInternalPath(), stableName, &fd); |
| 238 | |
| 239 | if (result != 0) { |
| 240 | LOG(ERROR) << "Failed to mount public fuse volume"; |
| 241 | return -result; |
| 242 | } |
| 243 | |
| 244 | mFuseMounted = true; |
| 245 | auto callback = getMountCallback(); |
| 246 | if (callback) { |
| 247 | bool is_ready = false; |
| 248 | callback->onVolumeChecking(std::move(fd), getPath(), getInternalPath(), &is_ready); |
| 249 | if (!is_ready) { |
| 250 | return -EIO; |
| 251 | } |
| 252 | } |
Nikita Ioffe | dcee5c1 | 2020-06-12 12:59:45 +0100 | [diff] [blame^] | 253 | |
| 254 | ConfigureReadAheadForFuse(GetFuseMountPathForUser(user_id, stableName), 256u); |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 255 | } |
| 256 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 257 | return OK; |
| 258 | } |
| 259 | |
| 260 | status_t PublicVolume::doUnmount() { |
John Cormie | 25cc7e3 | 2016-04-18 14:23:29 -0700 | [diff] [blame] | 261 | // Unmount the storage before we kill the FUSE process. If we kill |
| 262 | // the FUSE process first, most file system operations will return |
| 263 | // ENOTCONN until the unmount completes. This is an exotic and unusual |
| 264 | // error code and might cause broken behaviour in applications. |
Jeff Sharkey | 8aff854 | 2016-03-30 20:37:28 -0600 | [diff] [blame] | 265 | KillProcessesUsingPath(getPath()); |
| 266 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 267 | if (mFuseMounted) { |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 268 | // Use UUID as stable name, if available |
| 269 | std::string stableName = getId(); |
| 270 | if (!mFsUuid.empty()) { |
| 271 | stableName = mFsUuid; |
| 272 | } |
| 273 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 274 | if (UnmountUserFuse(getMountUserId(), getInternalPath(), stableName) != OK) { |
| 275 | PLOG(INFO) << "UnmountUserFuse failed on public fuse volume"; |
| 276 | return -errno; |
| 277 | } |
| 278 | |
Martijn Coenen | 6f5802e | 2019-11-28 11:53:53 +0100 | [diff] [blame] | 279 | mFuseMounted = false; |
Zim | 3623a21 | 2019-07-19 16:46:53 +0100 | [diff] [blame] | 280 | } |
| 281 | |
Jeff Sharkey | 48d81d3 | 2015-04-12 21:50:32 -0700 | [diff] [blame] | 282 | ForceUnmount(kAsecPath); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 283 | |
Martijn Coenen | 86f21a2 | 2020-01-06 09:48:14 +0100 | [diff] [blame] | 284 | if (mUseSdcardFs) { |
| 285 | ForceUnmount(mSdcardFsDefault); |
| 286 | ForceUnmount(mSdcardFsRead); |
| 287 | ForceUnmount(mSdcardFsWrite); |
| 288 | ForceUnmount(mSdcardFsFull); |
| 289 | |
| 290 | rmdir(mSdcardFsDefault.c_str()); |
| 291 | rmdir(mSdcardFsRead.c_str()); |
| 292 | rmdir(mSdcardFsWrite.c_str()); |
| 293 | rmdir(mSdcardFsFull.c_str()); |
| 294 | |
| 295 | mSdcardFsDefault.clear(); |
| 296 | mSdcardFsRead.clear(); |
| 297 | mSdcardFsWrite.clear(); |
| 298 | mSdcardFsFull.clear(); |
| 299 | } |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 300 | ForceUnmount(mRawPath); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 301 | rmdir(mRawPath.c_str()); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 302 | mRawPath.clear(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 303 | |
| 304 | return OK; |
| 305 | } |
| 306 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 307 | status_t PublicVolume::doFormat(const std::string& fsType) { |
Oleksiy Avramchenko | 4cff06d | 2018-05-23 18:59:48 +0200 | [diff] [blame] | 308 | bool useVfat = vfat::IsSupported(); |
| 309 | bool useExfat = exfat::IsSupported(); |
| 310 | status_t res = OK; |
| 311 | |
| 312 | // Resolve the target filesystem type |
| 313 | if (fsType == "auto" && useVfat && useExfat) { |
| 314 | uint64_t size = 0; |
| 315 | |
| 316 | res = GetBlockDevSize(mDevPath, &size); |
| 317 | if (res != OK) { |
| 318 | LOG(ERROR) << "Couldn't get device size " << mDevPath; |
| 319 | return res; |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 320 | } |
Oleksiy Avramchenko | 4cff06d | 2018-05-23 18:59:48 +0200 | [diff] [blame] | 321 | |
| 322 | // If both vfat & exfat are supported use exfat for SDXC (>~32GiB) cards |
| 323 | if (size > 32896LL * 1024 * 1024) { |
| 324 | useVfat = false; |
| 325 | } else { |
| 326 | useExfat = false; |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 327 | } |
Oleksiy Avramchenko | 4cff06d | 2018-05-23 18:59:48 +0200 | [diff] [blame] | 328 | } else if (fsType == "vfat") { |
| 329 | useExfat = false; |
| 330 | } else if (fsType == "exfat") { |
| 331 | useVfat = false; |
| 332 | } |
| 333 | |
| 334 | if (!useVfat && !useExfat) { |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 335 | LOG(ERROR) << "Unsupported filesystem " << fsType; |
| 336 | return -EINVAL; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 337 | } |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 338 | |
Oleksiy Avramchenko | 4cff06d | 2018-05-23 18:59:48 +0200 | [diff] [blame] | 339 | if (WipeBlockDevice(mDevPath) != OK) { |
| 340 | LOG(WARNING) << getId() << " failed to wipe"; |
| 341 | } |
| 342 | |
| 343 | if (useVfat) { |
| 344 | res = vfat::Format(mDevPath, 0); |
| 345 | } else if (useExfat) { |
| 346 | res = exfat::Format(mDevPath); |
| 347 | } |
| 348 | |
| 349 | if (res != OK) { |
| 350 | LOG(ERROR) << getId() << " failed to format"; |
| 351 | res = -errno; |
| 352 | } |
| 353 | |
| 354 | return res; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 355 | } |
| 356 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 357 | } // namespace vold |
| 358 | } // namespace android |