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" |
| 18 | #include "Utils.h" |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 19 | #include "VolumeManager.h" |
Jeff Sharkey | 37ba125 | 2018-01-19 10:55:18 +0900 | [diff] [blame] | 20 | #include "fs/Exfat.h" |
| 21 | #include "fs/Vfat.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> |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 24 | #include <android-base/stringprintf.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 25 | #include <cutils/fs.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 26 | #include <private/android_filesystem_config.h> |
Jeff Sharkey | 7bdf4d5 | 2017-09-18 14:47:10 -0600 | [diff] [blame] | 27 | #include <utils/Timers.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 28 | |
| 29 | #include <fcntl.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <sys/mount.h> |
| 32 | #include <sys/stat.h> |
Elliott Hughes | 0e08e84 | 2017-05-18 09:08:24 -0700 | [diff] [blame] | 33 | #include <sys/sysmacros.h> |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 34 | #include <sys/types.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 35 | #include <sys/wait.h> |
| 36 | |
Dan Albert | ae9e890 | 2015-03-16 10:35:17 -0700 | [diff] [blame] | 37 | using android::base::StringPrintf; |
| 38 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 39 | namespace android { |
| 40 | namespace vold { |
| 41 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 42 | static const char* kFusePath = "/system/bin/sdcard"; |
| 43 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 44 | static const char* kAsecPath = "/mnt/secure/asec"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 45 | |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 46 | PublicVolume::PublicVolume(dev_t device) : VolumeBase(Type::kPublic), mDevice(device), mFusePid(0) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 47 | setId(StringPrintf("public:%u,%u", major(device), minor(device))); |
| 48 | mDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str()); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 51 | PublicVolume::~PublicVolume() {} |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 52 | |
| 53 | status_t PublicVolume::readMetadata() { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 54 | status_t res = ReadMetadataUntrusted(mDevPath, &mFsType, &mFsUuid, &mFsLabel); |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 55 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 56 | auto listener = getListener(); |
| 57 | if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel); |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 58 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 59 | return res; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | status_t PublicVolume::initAsecStage() { |
| 63 | std::string legacyPath(mRawPath + "/android_secure"); |
| 64 | std::string securePath(mRawPath + "/.android_secure"); |
| 65 | |
| 66 | // Recover legacy secure path |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 67 | 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] | 68 | if (rename(legacyPath.c_str(), securePath.c_str())) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 69 | PLOG(WARNING) << getId() << " failed to rename legacy ASEC dir"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 73 | if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) { |
| 74 | if (errno != EEXIST) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 75 | PLOG(WARNING) << getId() << " creating ASEC stage failed"; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 76 | return -errno; |
| 77 | } |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 80 | BindMount(securePath, kAsecPath); |
| 81 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 82 | return OK; |
| 83 | } |
| 84 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 85 | status_t PublicVolume::doCreate() { |
| 86 | return CreateDeviceNode(mDevPath, mDevice); |
| 87 | } |
| 88 | |
| 89 | status_t PublicVolume::doDestroy() { |
| 90 | return DestroyDeviceNode(mDevPath); |
| 91 | } |
| 92 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 93 | status_t PublicVolume::doMount() { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 94 | readMetadata(); |
| 95 | |
Jeff Sharkey | 37ba125 | 2018-01-19 10:55:18 +0900 | [diff] [blame] | 96 | if (mFsType == "vfat" && vfat::IsSupported()) { |
| 97 | if (vfat::Check(mDevPath)) { |
| 98 | LOG(ERROR) << getId() << " failed filesystem check"; |
| 99 | return -EIO; |
| 100 | } |
| 101 | } else if (mFsType == "exfat" && exfat::IsSupported()) { |
| 102 | if (exfat::Check(mDevPath)) { |
| 103 | LOG(ERROR) << getId() << " failed filesystem check"; |
| 104 | return -EIO; |
| 105 | } |
| 106 | } else { |
Makoto Onuki | c82c9ce | 2015-06-24 13:30:45 -0700 | [diff] [blame] | 107 | LOG(ERROR) << getId() << " unsupported filesystem " << mFsType; |
| 108 | return -EIO; |
| 109 | } |
| 110 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 111 | // Use UUID as stable name, if available |
| 112 | std::string stableName = getId(); |
| 113 | if (!mFsUuid.empty()) { |
Jeff Sharkey | f0121c5 | 2015-04-06 14:08:45 -0700 | [diff] [blame] | 114 | stableName = mFsUuid; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | mRawPath = StringPrintf("/mnt/media_rw/%s", stableName.c_str()); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 118 | |
Jeff Sharkey | 1bd078f | 2015-08-06 11:40:00 -0700 | [diff] [blame] | 119 | mFuseDefault = StringPrintf("/mnt/runtime/default/%s", stableName.c_str()); |
| 120 | mFuseRead = StringPrintf("/mnt/runtime/read/%s", stableName.c_str()); |
| 121 | mFuseWrite = StringPrintf("/mnt/runtime/write/%s", stableName.c_str()); |
Sudheer Shanka | dd4bb17 | 2019-01-16 23:35:49 -0800 | [diff] [blame] | 122 | mFuseFull = StringPrintf("/mnt/runtime/full/%s", stableName.c_str()); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 123 | |
| 124 | setInternalPath(mRawPath); |
Jeff Sharkey | 8474ee3 | 2015-07-30 16:54:23 -0700 | [diff] [blame] | 125 | if (getMountFlags() & MountFlags::kVisible) { |
| 126 | setPath(StringPrintf("/storage/%s", stableName.c_str())); |
| 127 | } else { |
| 128 | setPath(mRawPath); |
| 129 | } |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 130 | |
Qin Chao | e0074f1 | 2015-12-15 15:20:41 +0800 | [diff] [blame] | 131 | if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT)) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 132 | PLOG(ERROR) << getId() << " failed to create mount points"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 133 | return -errno; |
| 134 | } |
| 135 | |
Jeff Sharkey | 37ba125 | 2018-01-19 10:55:18 +0900 | [diff] [blame] | 136 | if (mFsType == "vfat") { |
| 137 | if (vfat::Mount(mDevPath, mRawPath, false, false, false, AID_MEDIA_RW, AID_MEDIA_RW, 0007, |
| 138 | true)) { |
| 139 | PLOG(ERROR) << getId() << " failed to mount " << mDevPath; |
| 140 | return -EIO; |
| 141 | } |
| 142 | } else if (mFsType == "exfat") { |
| 143 | if (exfat::Mount(mDevPath, mRawPath, AID_MEDIA_RW, AID_MEDIA_RW, 0007)) { |
| 144 | PLOG(ERROR) << getId() << " failed to mount " << mDevPath; |
| 145 | return -EIO; |
| 146 | } |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 149 | if (getMountFlags() & MountFlags::kPrimary) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 150 | initAsecStage(); |
| 151 | } |
| 152 | |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 153 | if (!(getMountFlags() & MountFlags::kVisible)) { |
| 154 | // Not visible to apps, so no need to spin up FUSE |
| 155 | return OK; |
| 156 | } |
| 157 | |
Qin Chao | e0074f1 | 2015-12-15 15:20:41 +0800 | [diff] [blame] | 158 | if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) || |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 159 | fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) || |
Sudheer Shanka | dd4bb17 | 2019-01-16 23:35:49 -0800 | [diff] [blame] | 160 | fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT) || |
| 161 | fs_prepare_dir(mFuseFull.c_str(), 0700, AID_ROOT, AID_ROOT)) { |
Qin Chao | e0074f1 | 2015-12-15 15:20:41 +0800 | [diff] [blame] | 162 | PLOG(ERROR) << getId() << " failed to create FUSE mount points"; |
| 163 | return -errno; |
| 164 | } |
| 165 | |
Sudheer Shanka | dd4bb17 | 2019-01-16 23:35:49 -0800 | [diff] [blame] | 166 | dev_t before = GetDevice(mFuseFull); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 167 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 168 | if (!(mFusePid = fork())) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 169 | if (getMountFlags() & MountFlags::kPrimary) { |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 170 | // clang-format off |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 171 | if (execl(kFusePath, kFusePath, |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 172 | "-u", "1023", // AID_MEDIA_RW |
| 173 | "-g", "1023", // AID_MEDIA_RW |
Jeff Sharkey | 20642ae | 2015-07-28 10:57:29 -0700 | [diff] [blame] | 174 | "-U", std::to_string(getMountUserId()).c_str(), |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 175 | "-w", |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 176 | mRawPath.c_str(), |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 177 | stableName.c_str(), |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 178 | NULL)) { |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 179 | // clang-format on |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 180 | PLOG(ERROR) << "Failed to exec"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 181 | } |
| 182 | } else { |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 183 | // clang-format off |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 184 | if (execl(kFusePath, kFusePath, |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 185 | "-u", "1023", // AID_MEDIA_RW |
| 186 | "-g", "1023", // AID_MEDIA_RW |
Jeff Sharkey | 20642ae | 2015-07-28 10:57:29 -0700 | [diff] [blame] | 187 | "-U", std::to_string(getMountUserId()).c_str(), |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 188 | mRawPath.c_str(), |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 189 | stableName.c_str(), |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 190 | NULL)) { |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 191 | // clang-format on |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 192 | PLOG(ERROR) << "Failed to exec"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 196 | LOG(ERROR) << "FUSE exiting"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 197 | _exit(1); |
| 198 | } |
| 199 | |
| 200 | if (mFusePid == -1) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 201 | PLOG(ERROR) << getId() << " failed to fork"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 202 | return -errno; |
| 203 | } |
| 204 | |
Jeff Sharkey | 7bdf4d5 | 2017-09-18 14:47:10 -0600 | [diff] [blame] | 205 | nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME); |
Sudheer Shanka | dd4bb17 | 2019-01-16 23:35:49 -0800 | [diff] [blame] | 206 | while (before == GetDevice(mFuseFull)) { |
Sudheer Shanka | 4b6ca4e | 2018-09-21 10:54:54 -0700 | [diff] [blame] | 207 | LOG(DEBUG) << "Waiting for FUSE to spin up..."; |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 208 | usleep(50000); // 50ms |
Jeff Sharkey | 7bdf4d5 | 2017-09-18 14:47:10 -0600 | [diff] [blame] | 209 | |
| 210 | nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME); |
| 211 | if (nanoseconds_to_milliseconds(now - start) > 5000) { |
| 212 | LOG(WARNING) << "Timed out while waiting for FUSE to spin up"; |
| 213 | return -ETIMEDOUT; |
| 214 | } |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 215 | } |
Daniel Rosenberg | 1d79d10 | 2017-07-11 17:59:55 -0700 | [diff] [blame] | 216 | /* sdcardfs will have exited already. FUSE will still be running */ |
Daniel Rosenberg | 8b9a5b3 | 2018-03-12 15:47:23 -0700 | [diff] [blame] | 217 | TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0)); |
| 218 | mFusePid = 0; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 219 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 220 | return OK; |
| 221 | } |
| 222 | |
| 223 | status_t PublicVolume::doUnmount() { |
John Cormie | 25cc7e3 | 2016-04-18 14:23:29 -0700 | [diff] [blame] | 224 | // Unmount the storage before we kill the FUSE process. If we kill |
| 225 | // the FUSE process first, most file system operations will return |
| 226 | // ENOTCONN until the unmount completes. This is an exotic and unusual |
| 227 | // error code and might cause broken behaviour in applications. |
Jeff Sharkey | 8aff854 | 2016-03-30 20:37:28 -0600 | [diff] [blame] | 228 | KillProcessesUsingPath(getPath()); |
| 229 | |
Jeff Sharkey | 48d81d3 | 2015-04-12 21:50:32 -0700 | [diff] [blame] | 230 | ForceUnmount(kAsecPath); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 231 | |
| 232 | ForceUnmount(mFuseDefault); |
| 233 | ForceUnmount(mFuseRead); |
| 234 | ForceUnmount(mFuseWrite); |
Sudheer Shanka | dd4bb17 | 2019-01-16 23:35:49 -0800 | [diff] [blame] | 235 | ForceUnmount(mFuseFull); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 236 | ForceUnmount(mRawPath); |
| 237 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 238 | rmdir(mFuseDefault.c_str()); |
| 239 | rmdir(mFuseRead.c_str()); |
| 240 | rmdir(mFuseWrite.c_str()); |
Sudheer Shanka | dd4bb17 | 2019-01-16 23:35:49 -0800 | [diff] [blame] | 241 | rmdir(mFuseFull.c_str()); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 242 | rmdir(mRawPath.c_str()); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 243 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 244 | mFuseDefault.clear(); |
| 245 | mFuseRead.clear(); |
| 246 | mFuseWrite.clear(); |
Sudheer Shanka | dd4bb17 | 2019-01-16 23:35:49 -0800 | [diff] [blame] | 247 | mFuseFull.clear(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 248 | mRawPath.clear(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 249 | |
| 250 | return OK; |
| 251 | } |
| 252 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 253 | status_t PublicVolume::doFormat(const std::string& fsType) { |
Oleksiy Avramchenko | 4cff06d | 2018-05-23 18:59:48 +0200 | [diff] [blame] | 254 | bool useVfat = vfat::IsSupported(); |
| 255 | bool useExfat = exfat::IsSupported(); |
| 256 | status_t res = OK; |
| 257 | |
| 258 | // Resolve the target filesystem type |
| 259 | if (fsType == "auto" && useVfat && useExfat) { |
| 260 | uint64_t size = 0; |
| 261 | |
| 262 | res = GetBlockDevSize(mDevPath, &size); |
| 263 | if (res != OK) { |
| 264 | LOG(ERROR) << "Couldn't get device size " << mDevPath; |
| 265 | return res; |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 266 | } |
Oleksiy Avramchenko | 4cff06d | 2018-05-23 18:59:48 +0200 | [diff] [blame] | 267 | |
| 268 | // If both vfat & exfat are supported use exfat for SDXC (>~32GiB) cards |
| 269 | if (size > 32896LL * 1024 * 1024) { |
| 270 | useVfat = false; |
| 271 | } else { |
| 272 | useExfat = false; |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 273 | } |
Oleksiy Avramchenko | 4cff06d | 2018-05-23 18:59:48 +0200 | [diff] [blame] | 274 | } else if (fsType == "vfat") { |
| 275 | useExfat = false; |
| 276 | } else if (fsType == "exfat") { |
| 277 | useVfat = false; |
| 278 | } |
| 279 | |
| 280 | if (!useVfat && !useExfat) { |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 281 | LOG(ERROR) << "Unsupported filesystem " << fsType; |
| 282 | return -EINVAL; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 283 | } |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 284 | |
Oleksiy Avramchenko | 4cff06d | 2018-05-23 18:59:48 +0200 | [diff] [blame] | 285 | if (WipeBlockDevice(mDevPath) != OK) { |
| 286 | LOG(WARNING) << getId() << " failed to wipe"; |
| 287 | } |
| 288 | |
| 289 | if (useVfat) { |
| 290 | res = vfat::Format(mDevPath, 0); |
| 291 | } else if (useExfat) { |
| 292 | res = exfat::Format(mDevPath); |
| 293 | } |
| 294 | |
| 295 | if (res != OK) { |
| 296 | LOG(ERROR) << getId() << " failed to format"; |
| 297 | res = -errno; |
| 298 | } |
| 299 | |
| 300 | return res; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 301 | } |
| 302 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 303 | } // namespace vold |
| 304 | } // namespace android |