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