blob: 20015afa7686605fbec6de75463adc5169029404 [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"
Alfred Piccioni564f6c62022-09-21 17:32:04 +020023#include "fs/Ntfs.h"
Jeff Sharkey37ba1252018-01-19 10:55:18 +090024#include "fs/Vfat.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080025
Elliott Hughes7e128fb2015-12-04 15:50:53 -080026#include <android-base/logging.h>
Sudheer Shanka40ab6742018-09-18 13:07:45 -070027#include <android-base/properties.h>
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070028#include <android-base/stringprintf.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080029#include <cutils/fs.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080030#include <private/android_filesystem_config.h>
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -060031#include <utils/Timers.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080032
33#include <fcntl.h>
34#include <stdlib.h>
35#include <sys/mount.h>
36#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070037#include <sys/sysmacros.h>
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070038#include <sys/types.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080039#include <sys/wait.h>
40
Sudheer Shanka40ab6742018-09-18 13:07:45 -070041using android::base::GetBoolProperty;
Dan Albertae9e8902015-03-16 10:35:17 -070042using android::base::StringPrintf;
43
Jeff Sharkeydeb24052015-03-02 21:01:40 -080044namespace android {
45namespace vold {
46
Martijn Coenenadcc8452019-12-09 14:18:01 +010047static const char* kSdcardFsPath = "/system/bin/sdcard";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080048
Jeff Sharkey36801cc2015-03-13 16:09:20 -070049static const char* kAsecPath = "/mnt/secure/asec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080050
Martijn Coenenadcc8452019-12-09 14:18:01 +010051PublicVolume::PublicVolume(dev_t device) : VolumeBase(Type::kPublic), mDevice(device) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070052 setId(StringPrintf("public:%u,%u", major(device), minor(device)));
53 mDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
Martijn Coenenfd7362d2019-12-11 14:57:59 +010054 mFuseMounted = false;
Daniel Rosenbergf36bddd2020-05-11 22:58:42 -070055 mUseSdcardFs = IsSdcardfsUsed();
Jeff Sharkeydeb24052015-03-02 21:01:40 -080056}
57
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070058PublicVolume::~PublicVolume() {}
Jeff Sharkeydeb24052015-03-02 21:01:40 -080059
60status_t PublicVolume::readMetadata() {
Jeff Sharkey3472e522017-10-06 18:02:53 -060061 status_t res = ReadMetadataUntrusted(mDevPath, &mFsType, &mFsUuid, &mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060062
Jeff Sharkey814e9d32017-09-13 11:49:44 -060063 auto listener = getListener();
64 if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060065
Jeff Sharkey36801cc2015-03-13 16:09:20 -070066 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080067}
68
69status_t PublicVolume::initAsecStage() {
70 std::string legacyPath(mRawPath + "/android_secure");
71 std::string securePath(mRawPath + "/.android_secure");
72
73 // Recover legacy secure path
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070074 if (!access(legacyPath.c_str(), R_OK | X_OK) && access(securePath.c_str(), R_OK | X_OK)) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -080075 if (rename(legacyPath.c_str(), securePath.c_str())) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070076 PLOG(WARNING) << getId() << " failed to rename legacy ASEC dir";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080077 }
78 }
79
Jeff Sharkey36801cc2015-03-13 16:09:20 -070080 if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) {
81 if (errno != EEXIST) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070082 PLOG(WARNING) << getId() << " creating ASEC stage failed";
Jeff Sharkey36801cc2015-03-13 16:09:20 -070083 return -errno;
84 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -080085 }
86
Jeff Sharkey36801cc2015-03-13 16:09:20 -070087 BindMount(securePath, kAsecPath);
88
Jeff Sharkeydeb24052015-03-02 21:01:40 -080089 return OK;
90}
91
Jeff Sharkey9c484982015-03-31 10:35:33 -070092status_t PublicVolume::doCreate() {
93 return CreateDeviceNode(mDevPath, mDevice);
94}
95
96status_t PublicVolume::doDestroy() {
97 return DestroyDeviceNode(mDevPath);
98}
99
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800100status_t PublicVolume::doMount() {
Youkichi Hosoi4461c5a2021-11-13 16:27:02 +0900101 bool isVisible = isVisibleForWrite();
Jeff Sharkey9c484982015-03-31 10:35:33 -0700102 readMetadata();
103
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900104 if (mFsType == "vfat" && vfat::IsSupported()) {
105 if (vfat::Check(mDevPath)) {
106 LOG(ERROR) << getId() << " failed filesystem check";
107 return -EIO;
108 }
109 } else if (mFsType == "exfat" && exfat::IsSupported()) {
110 if (exfat::Check(mDevPath)) {
111 LOG(ERROR) << getId() << " failed filesystem check";
112 return -EIO;
113 }
Alfred Piccioni564f6c62022-09-21 17:32:04 +0200114 } else if (mFsType == "ntfs" && ntfs::IsSupported()) {
115 if (ntfs::Check(mDevPath)) {
116 LOG(ERROR) << getId() << " failed filesystem check";
117 return -EIO;
118 }
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900119 } else {
Makoto Onukic82c9ce2015-06-24 13:30:45 -0700120 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
121 return -EIO;
122 }
123
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700124 // Use UUID as stable name, if available
125 std::string stableName = getId();
126 if (!mFsUuid.empty()) {
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700127 stableName = mFsUuid;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700128 }
129
130 mRawPath = StringPrintf("/mnt/media_rw/%s", stableName.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700131
Martijn Coenenadcc8452019-12-09 14:18:01 +0100132 mSdcardFsDefault = StringPrintf("/mnt/runtime/default/%s", stableName.c_str());
133 mSdcardFsRead = StringPrintf("/mnt/runtime/read/%s", stableName.c_str());
134 mSdcardFsWrite = StringPrintf("/mnt/runtime/write/%s", stableName.c_str());
135 mSdcardFsFull = StringPrintf("/mnt/runtime/full/%s", stableName.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700136
137 setInternalPath(mRawPath);
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100138 if (isVisible) {
Jeff Sharkey8474ee32015-07-30 16:54:23 -0700139 setPath(StringPrintf("/storage/%s", stableName.c_str()));
140 } else {
141 setPath(mRawPath);
142 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700143
Qin Chaoe0074f12015-12-15 15:20:41 +0800144 if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700145 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800146 return -errno;
147 }
148
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900149 if (mFsType == "vfat") {
Zimc9a2be42020-01-24 22:03:02 +0000150 if (vfat::Mount(mDevPath, mRawPath, false, false, false, AID_ROOT,
151 (isVisible ? AID_MEDIA_RW : AID_EXTERNAL_STORAGE), 0007, true)) {
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900152 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
153 return -EIO;
154 }
155 } else if (mFsType == "exfat") {
Zimc9a2be42020-01-24 22:03:02 +0000156 if (exfat::Mount(mDevPath, mRawPath, AID_ROOT,
157 (isVisible ? AID_MEDIA_RW : AID_EXTERNAL_STORAGE), 0007)) {
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900158 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
159 return -EIO;
160 }
Alfred Piccioni564f6c62022-09-21 17:32:04 +0200161 } else if (mFsType == "ntfs") {
162 if (ntfs::Mount(mDevPath, mRawPath, false, false, false, AID_ROOT,
163 (isVisible ? AID_MEDIA_RW : AID_EXTERNAL_STORAGE), 0007)) {
164 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
165 return -EIO;
166 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800167 }
168
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700169 if (getMountFlags() & MountFlags::kPrimary) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700170 initAsecStage();
171 }
172
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100173 if (!isVisible) {
Martijn Coenenadcc8452019-12-09 14:18:01 +0100174 // Not visible to apps, so no need to spin up sdcardfs or FUSE
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700175 return OK;
176 }
177
Martijn Coenen86f21a22020-01-06 09:48:14 +0100178 if (mUseSdcardFs) {
179 if (fs_prepare_dir(mSdcardFsDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
180 fs_prepare_dir(mSdcardFsRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
181 fs_prepare_dir(mSdcardFsWrite.c_str(), 0700, AID_ROOT, AID_ROOT) ||
182 fs_prepare_dir(mSdcardFsFull.c_str(), 0700, AID_ROOT, AID_ROOT)) {
183 PLOG(ERROR) << getId() << " failed to create sdcardfs mount points";
184 return -errno;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800185 }
186
Martijn Coenen86f21a22020-01-06 09:48:14 +0100187 dev_t before = GetDevice(mSdcardFsFull);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800188
Martijn Coenen86f21a22020-01-06 09:48:14 +0100189 int sdcardFsPid;
190 if (!(sdcardFsPid = fork())) {
191 if (getMountFlags() & MountFlags::kPrimary) {
192 // clang-format off
193 if (execl(kSdcardFsPath, kSdcardFsPath,
194 "-u", "1023", // AID_MEDIA_RW
195 "-g", "1023", // AID_MEDIA_RW
196 "-U", std::to_string(getMountUserId()).c_str(),
197 "-w",
198 mRawPath.c_str(),
199 stableName.c_str(),
200 NULL)) {
201 // clang-format on
202 PLOG(ERROR) << "Failed to exec";
203 }
204 } else {
205 // clang-format off
206 if (execl(kSdcardFsPath, kSdcardFsPath,
207 "-u", "1023", // AID_MEDIA_RW
208 "-g", "1023", // AID_MEDIA_RW
209 "-U", std::to_string(getMountUserId()).c_str(),
210 mRawPath.c_str(),
211 stableName.c_str(),
212 NULL)) {
213 // clang-format on
214 PLOG(ERROR) << "Failed to exec";
215 }
216 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800217
Martijn Coenen86f21a22020-01-06 09:48:14 +0100218 LOG(ERROR) << "sdcardfs exiting";
219 _exit(1);
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600220 }
Martijn Coenen86f21a22020-01-06 09:48:14 +0100221
222 if (sdcardFsPid == -1) {
223 PLOG(ERROR) << getId() << " failed to fork";
224 return -errno;
225 }
226
227 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
228 while (before == GetDevice(mSdcardFsFull)) {
229 LOG(DEBUG) << "Waiting for sdcardfs to spin up...";
230 usleep(50000); // 50ms
231
232 nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
233 if (nanoseconds_to_milliseconds(now - start) > 5000) {
234 LOG(WARNING) << "Timed out while waiting for sdcardfs to spin up";
235 return -ETIMEDOUT;
236 }
237 }
238 /* sdcardfs will have exited already. The filesystem will still be running */
239 TEMP_FAILURE_RETRY(waitpid(sdcardFsPid, nullptr, 0));
Jeff Sharkey66270a22015-06-24 11:49:24 -0700240 }
241
Zim415d99d2020-06-29 19:50:47 +0100242 // We need to mount FUSE *after* sdcardfs, since the FUSE daemon may depend
243 // on sdcardfs being up.
244 LOG(INFO) << "Mounting public fuse volume";
245 android::base::unique_fd fd;
246 int user_id = getMountUserId();
247 int result = MountUserFuse(user_id, getInternalPath(), stableName, &fd);
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100248
Zim415d99d2020-06-29 19:50:47 +0100249 if (result != 0) {
250 LOG(ERROR) << "Failed to mount public fuse volume";
251 doUnmount();
252 return -result;
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100253 }
254
Zim415d99d2020-06-29 19:50:47 +0100255 mFuseMounted = true;
256 auto callback = getMountCallback();
257 if (callback) {
258 bool is_ready = false;
259 callback->onVolumeChecking(std::move(fd), getPath(), getInternalPath(), &is_ready);
260 if (!is_ready) {
261 LOG(ERROR) << "Failed to complete public volume mount";
262 doUnmount();
263 return -EIO;
264 }
265 }
266
267 ConfigureReadAheadForFuse(GetFuseMountPathForUser(user_id, stableName), 256u);
268
269 // See comment in model/EmulatedVolume.cpp
270 ConfigureMaxDirtyRatioForFuse(GetFuseMountPathForUser(user_id, stableName), 40u);
271
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800272 return OK;
273}
274
275status_t PublicVolume::doUnmount() {
John Cormie25cc7e32016-04-18 14:23:29 -0700276 // Unmount the storage before we kill the FUSE process. If we kill
277 // the FUSE process first, most file system operations will return
278 // ENOTCONN until the unmount completes. This is an exotic and unusual
279 // error code and might cause broken behaviour in applications.
Jeff Sharkey8aff8542016-03-30 20:37:28 -0600280 KillProcessesUsingPath(getPath());
281
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100282 if (mFuseMounted) {
Zim3623a212019-07-19 16:46:53 +0100283 // Use UUID as stable name, if available
284 std::string stableName = getId();
285 if (!mFsUuid.empty()) {
286 stableName = mFsUuid;
287 }
288
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100289 if (UnmountUserFuse(getMountUserId(), getInternalPath(), stableName) != OK) {
290 PLOG(INFO) << "UnmountUserFuse failed on public fuse volume";
291 return -errno;
292 }
293
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100294 mFuseMounted = false;
Zim3623a212019-07-19 16:46:53 +0100295 }
296
Jeff Sharkey48d81d32015-04-12 21:50:32 -0700297 ForceUnmount(kAsecPath);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700298
Martijn Coenen86f21a22020-01-06 09:48:14 +0100299 if (mUseSdcardFs) {
300 ForceUnmount(mSdcardFsDefault);
301 ForceUnmount(mSdcardFsRead);
302 ForceUnmount(mSdcardFsWrite);
303 ForceUnmount(mSdcardFsFull);
304
305 rmdir(mSdcardFsDefault.c_str());
306 rmdir(mSdcardFsRead.c_str());
307 rmdir(mSdcardFsWrite.c_str());
308 rmdir(mSdcardFsFull.c_str());
309
310 mSdcardFsDefault.clear();
311 mSdcardFsRead.clear();
312 mSdcardFsWrite.clear();
313 mSdcardFsFull.clear();
314 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800315 ForceUnmount(mRawPath);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700316 rmdir(mRawPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700317 mRawPath.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800318
319 return OK;
320}
321
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700322status_t PublicVolume::doFormat(const std::string& fsType) {
Alfred Piccioni564f6c62022-09-21 17:32:04 +0200323 bool isVfatSup = vfat::IsSupported();
324 bool isExfatSup = exfat::IsSupported();
325 bool isNtfsSup = ntfs::IsSupported();
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200326 status_t res = OK;
327
Alfred Piccioni564f6c62022-09-21 17:32:04 +0200328 enum { NONE, VFAT, EXFAT, NTFS } fsPick = NONE;
329
330 // Resolve auto requests
331 if (fsType == "auto" && isVfatSup && isExfatSup) {
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200332 uint64_t size = 0;
333
334 res = GetBlockDevSize(mDevPath, &size);
335 if (res != OK) {
336 LOG(ERROR) << "Couldn't get device size " << mDevPath;
337 return res;
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700338 }
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200339
340 // If both vfat & exfat are supported use exfat for SDXC (>~32GiB) cards
341 if (size > 32896LL * 1024 * 1024) {
Alfred Piccioni564f6c62022-09-21 17:32:04 +0200342 fsPick = EXFAT;
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200343 } else {
Alfred Piccioni564f6c62022-09-21 17:32:04 +0200344 fsPick = VFAT;
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700345 }
Alfred Piccioni564f6c62022-09-21 17:32:04 +0200346 } else if (fsType == "auto" && isExfatSup) {
347 fsPick = EXFAT;
348 } else if (fsType == "auto" && isVfatSup) {
349 fsPick = VFAT;
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200350 }
351
Alfred Piccioni564f6c62022-09-21 17:32:04 +0200352 // Resolve explicit requests
353 if (fsType == "vfat" && isVfatSup) {
354 fsPick = VFAT;
355 } else if (fsType == "exfat" && isExfatSup) {
356 fsPick = EXFAT;
357 } else if (fsType == "ntfs" && isNtfsSup) {
358 fsPick = NTFS;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800359 }
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700360
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200361 if (WipeBlockDevice(mDevPath) != OK) {
362 LOG(WARNING) << getId() << " failed to wipe";
363 }
364
Alfred Piccioni564f6c62022-09-21 17:32:04 +0200365 if (fsPick == VFAT) {
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200366 res = vfat::Format(mDevPath, 0);
Alfred Piccioni564f6c62022-09-21 17:32:04 +0200367 } else if (fsPick == EXFAT) {
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200368 res = exfat::Format(mDevPath);
Alfred Piccioni564f6c62022-09-21 17:32:04 +0200369 } else if (fsPick == NTFS) {
370 res = ntfs::Format(mDevPath, 0);
371 } else {
372 LOG(ERROR) << "Unsupported filesystem " << fsType;
373 return -EINVAL;
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200374 }
375
376 if (res != OK) {
377 LOG(ERROR) << getId() << " failed to format";
378 res = -errno;
379 }
380
381 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800382}
383
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800384} // namespace vold
385} // namespace android