blob: 5a30fca8ac074de029077be6876b72d8f3294570 [file] [log] [blame]
Jeff Sharkeydeb24052015-03-02 21:01:40 -08001/*
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 Sharkeydeb24052015-03-02 21:01:40 -080017#include "PublicVolume.h"
Zim3623a212019-07-19 16:46:53 +010018
19#include "AppFuseUtil.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080020#include "Utils.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070021#include "VolumeManager.h"
Jeff Sharkey37ba1252018-01-19 10:55:18 +090022#include "fs/Exfat.h"
23#include "fs/Vfat.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080024
Elliott Hughes7e128fb2015-12-04 15:50:53 -080025#include <android-base/logging.h>
Sudheer Shanka40ab6742018-09-18 13:07:45 -070026#include <android-base/properties.h>
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070027#include <android-base/stringprintf.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080028#include <cutils/fs.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080029#include <private/android_filesystem_config.h>
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -060030#include <utils/Timers.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080031
32#include <fcntl.h>
33#include <stdlib.h>
34#include <sys/mount.h>
35#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070036#include <sys/sysmacros.h>
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070037#include <sys/types.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080038#include <sys/wait.h>
Omar Eissafedaddb2025-03-12 16:09:16 +000039#include <android_vold_flags.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080040
Sudheer Shanka40ab6742018-09-18 13:07:45 -070041using android::base::GetBoolProperty;
Dan Albertae9e8902015-03-16 10:35:17 -070042using android::base::StringPrintf;
Omar Eissafedaddb2025-03-12 16:09:16 +000043namespace flags = android::vold::flags;
Dan Albertae9e8902015-03-16 10:35:17 -070044
Jeff Sharkeydeb24052015-03-02 21:01:40 -080045namespace android {
46namespace vold {
47
Martijn Coenenadcc8452019-12-09 14:18:01 +010048static const char* kSdcardFsPath = "/system/bin/sdcard";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080049
Jeff Sharkey36801cc2015-03-13 16:09:20 -070050static const char* kAsecPath = "/mnt/secure/asec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080051
Martijn Coenenadcc8452019-12-09 14:18:01 +010052PublicVolume::PublicVolume(dev_t device) : VolumeBase(Type::kPublic), mDevice(device) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070053 setId(StringPrintf("public:%u,%u", major(device), minor(device)));
54 mDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
Martijn Coenenfd7362d2019-12-11 14:57:59 +010055 mFuseMounted = false;
Daniel Rosenbergf36bddd2020-05-11 22:58:42 -070056 mUseSdcardFs = IsSdcardfsUsed();
Jeff Sharkeydeb24052015-03-02 21:01:40 -080057}
58
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070059PublicVolume::~PublicVolume() {}
Jeff Sharkeydeb24052015-03-02 21:01:40 -080060
61status_t PublicVolume::readMetadata() {
Jeff Sharkey3472e522017-10-06 18:02:53 -060062 status_t res = ReadMetadataUntrusted(mDevPath, &mFsType, &mFsUuid, &mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060063
Jeff Sharkey814e9d32017-09-13 11:49:44 -060064 auto listener = getListener();
65 if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060066
Jeff Sharkey36801cc2015-03-13 16:09:20 -070067 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080068}
69
70status_t PublicVolume::initAsecStage() {
71 std::string legacyPath(mRawPath + "/android_secure");
72 std::string securePath(mRawPath + "/.android_secure");
73
74 // Recover legacy secure path
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070075 if (!access(legacyPath.c_str(), R_OK | X_OK) && access(securePath.c_str(), R_OK | X_OK)) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -080076 if (rename(legacyPath.c_str(), securePath.c_str())) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070077 PLOG(WARNING) << getId() << " failed to rename legacy ASEC dir";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080078 }
79 }
80
Jeff Sharkey36801cc2015-03-13 16:09:20 -070081 if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) {
82 if (errno != EEXIST) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070083 PLOG(WARNING) << getId() << " creating ASEC stage failed";
Jeff Sharkey36801cc2015-03-13 16:09:20 -070084 return -errno;
85 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -080086 }
87
Jeff Sharkey36801cc2015-03-13 16:09:20 -070088 BindMount(securePath, kAsecPath);
89
Jeff Sharkeydeb24052015-03-02 21:01:40 -080090 return OK;
91}
92
Omar Eissad9c4b232025-03-13 11:52:43 +000093std::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 Sharkey9c484982015-03-31 10:35:33 -0700102status_t PublicVolume::doCreate() {
103 return CreateDeviceNode(mDevPath, mDevice);
104}
105
106status_t PublicVolume::doDestroy() {
107 return DestroyDeviceNode(mDevPath);
108}
109
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800110status_t PublicVolume::doMount() {
Youkichi Hosoi4461c5a2021-11-13 16:27:02 +0900111 bool isVisible = isVisibleForWrite();
Jeff Sharkey9c484982015-03-31 10:35:33 -0700112 readMetadata();
113
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900114 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 Onukic82c9ce2015-06-24 13:30:45 -0700125 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
126 return -EIO;
127 }
128
Omar Eissad9c4b232025-03-13 11:52:43 +0000129 std::string stableName = getStableName();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700130
131 mRawPath = StringPrintf("/mnt/media_rw/%s", stableName.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700132
Martijn Coenenadcc8452019-12-09 14:18:01 +0100133 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 Sharkey66270a22015-06-24 11:49:24 -0700137
138 setInternalPath(mRawPath);
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100139 if (isVisible) {
Jeff Sharkey8474ee32015-07-30 16:54:23 -0700140 setPath(StringPrintf("/storage/%s", stableName.c_str()));
141 } else {
142 setPath(mRawPath);
143 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700144
Qin Chaoe0074f12015-12-15 15:20:41 +0800145 if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700146 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800147 return -errno;
148 }
149
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900150 if (mFsType == "vfat") {
Zimc9a2be42020-01-24 22:03:02 +0000151 if (vfat::Mount(mDevPath, mRawPath, false, false, false, AID_ROOT,
152 (isVisible ? AID_MEDIA_RW : AID_EXTERNAL_STORAGE), 0007, true)) {
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900153 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
154 return -EIO;
155 }
156 } else if (mFsType == "exfat") {
Zimc9a2be42020-01-24 22:03:02 +0000157 if (exfat::Mount(mDevPath, mRawPath, AID_ROOT,
158 (isVisible ? AID_MEDIA_RW : AID_EXTERNAL_STORAGE), 0007)) {
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900159 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
160 return -EIO;
161 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800162 }
163
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700164 if (getMountFlags() & MountFlags::kPrimary) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700165 initAsecStage();
166 }
167
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100168 if (!isVisible) {
Martijn Coenenadcc8452019-12-09 14:18:01 +0100169 // Not visible to apps, so no need to spin up sdcardfs or FUSE
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700170 return OK;
171 }
172
Martijn Coenen86f21a22020-01-06 09:48:14 +0100173 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 Sharkeydeb24052015-03-02 21:01:40 -0800180 }
181
Martijn Coenen86f21a22020-01-06 09:48:14 +0100182 dev_t before = GetDevice(mSdcardFsFull);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800183
Martijn Coenen86f21a22020-01-06 09:48:14 +0100184 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 Sharkeydeb24052015-03-02 21:01:40 -0800212
Martijn Coenen86f21a22020-01-06 09:48:14 +0100213 LOG(ERROR) << "sdcardfs exiting";
214 _exit(1);
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600215 }
Martijn Coenen86f21a22020-01-06 09:48:14 +0100216
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 Sharkey66270a22015-06-24 11:49:24 -0700235 }
236
Zim415d99d2020-06-29 19:50:47 +0100237 // 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 Coenen6f5802e2019-11-28 11:53:53 +0100243
Zim415d99d2020-06-29 19:50:47 +0100244 if (result != 0) {
245 LOG(ERROR) << "Failed to mount public fuse volume";
246 doUnmount();
247 return -result;
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100248 }
249
Zim415d99d2020-06-29 19:50:47 +0100250 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
himanshuz0ad08622023-07-27 21:15:00 +0000267 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 Gupta4f179692024-05-06 11:40:19 +0100279 // 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 }
himanshuz0ad08622023-07-27 21:15:00 +0000285 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 Sharkeydeb24052015-03-02 21:01:40 -0800291 return OK;
292}
293
himanshuz0ad08622023-07-27 21:15:00 +0000294status_t PublicVolume::bindMountForUser(userid_t user_id) {
295 userid_t mountUserId = getMountUserId();
Omar Eissad9c4b232025-03-13 11:52:43 +0000296 std::string stableName = getStableName();
himanshuz0ad08622023-07-27 21:15:00 +0000297
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 Sharkeydeb24052015-03-02 21:01:40 -0800309status_t PublicVolume::doUnmount() {
John Cormie25cc7e32016-04-18 14:23:29 -0700310 // 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 Eissafedaddb2025-03-12 16:09:16 +0000314
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 Sharkey8aff8542016-03-30 20:37:28 -0600320
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100321 if (mFuseMounted) {
Omar Eissad9c4b232025-03-13 11:52:43 +0000322 std::string stableName = getStableName();
Zim3623a212019-07-19 16:46:53 +0100323
himanshuz0ad08622023-07-27 21:15:00 +0000324 // Unmount bind mounts for running users
325 auto vol_manager = VolumeManager::Instance();
Omar Eissafedaddb2025-03-12 16:09:16 +0000326 userid_t user_id = getMountUserId();
327 std::vector<std::string> bind_mount_paths;
328 for (userid_t started_user : vol_manager->getStartedUsers()) {
himanshuz0ad08622023-07-27 21:15:00 +0000329 if (started_user == user_id) {
330 // No need to remove bind mount for the user that owns the mount
331 continue;
332 }
Omar Eissafedaddb2025-03-12 16:09:16 +0000333 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
himanshuz0ad08622023-07-27 21:15:00 +0000339 auto mountPath = GetFuseMountPathForUser(started_user, stableName);
Omar Eissafedaddb2025-03-12 16:09:16 +0000340 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 }
himanshuz0ad08622023-07-27 21:15:00 +0000348 }
349
Omar Eissafedaddb2025-03-12 16:09:16 +0000350 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 Coenen6f5802e2019-11-28 11:53:53 +0100362 }
363
Omar Eissafedaddb2025-03-12 16:09:16 +0000364
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100365 mFuseMounted = false;
Zim3623a212019-07-19 16:46:53 +0100366 }
367
Jeff Sharkey48d81d32015-04-12 21:50:32 -0700368 ForceUnmount(kAsecPath);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700369
Martijn Coenen86f21a22020-01-06 09:48:14 +0100370 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.chen4c45ac62021-07-07 20:27:47 +0800386
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 Sharkey36801cc2015-03-13 16:09:20 -0700395 mRawPath.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800396
397 return OK;
398}
399
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700400status_t PublicVolume::doFormat(const std::string& fsType) {
Alfred Piccioni564f6c62022-09-21 17:32:04 +0200401 bool isVfatSup = vfat::IsSupported();
402 bool isExfatSup = exfat::IsSupported();
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200403 status_t res = OK;
404
Alfred Piccionifc4934f2023-02-02 09:46:30 +0000405 enum { NONE, VFAT, EXFAT } fsPick = NONE;
Alfred Piccioni564f6c62022-09-21 17:32:04 +0200406
407 // Resolve auto requests
408 if (fsType == "auto" && isVfatSup && isExfatSup) {
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200409 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 Sharkeyd0640f62015-05-21 22:35:42 -0700415 }
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200416
417 // If both vfat & exfat are supported use exfat for SDXC (>~32GiB) cards
418 if (size > 32896LL * 1024 * 1024) {
Alfred Piccioni564f6c62022-09-21 17:32:04 +0200419 fsPick = EXFAT;
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200420 } else {
Alfred Piccioni564f6c62022-09-21 17:32:04 +0200421 fsPick = VFAT;
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700422 }
Alfred Piccioni564f6c62022-09-21 17:32:04 +0200423 } else if (fsType == "auto" && isExfatSup) {
424 fsPick = EXFAT;
425 } else if (fsType == "auto" && isVfatSup) {
426 fsPick = VFAT;
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200427 }
428
Alfred Piccioni564f6c62022-09-21 17:32:04 +0200429 // Resolve explicit requests
430 if (fsType == "vfat" && isVfatSup) {
431 fsPick = VFAT;
432 } else if (fsType == "exfat" && isExfatSup) {
433 fsPick = EXFAT;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800434 }
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700435
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200436 if (WipeBlockDevice(mDevPath) != OK) {
437 LOG(WARNING) << getId() << " failed to wipe";
438 }
439
Alfred Piccioni564f6c62022-09-21 17:32:04 +0200440 if (fsPick == VFAT) {
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200441 res = vfat::Format(mDevPath, 0);
Alfred Piccioni564f6c62022-09-21 17:32:04 +0200442 } else if (fsPick == EXFAT) {
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200443 res = exfat::Format(mDevPath);
Alfred Piccioni564f6c62022-09-21 17:32:04 +0200444 } else {
445 LOG(ERROR) << "Unsupported filesystem " << fsType;
446 return -EINVAL;
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200447 }
448
449 if (res != OK) {
450 LOG(ERROR) << getId() << " failed to format";
451 res = -errno;
452 }
453
454 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800455}
456
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800457} // namespace vold
458} // namespace android