blob: 0a6b351562f9d15648bf3250bda3285f8012e4e4 [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"
18#include "Utils.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070019#include "VolumeManager.h"
Jeff Sharkey37ba1252018-01-19 10:55:18 +090020#include "fs/Exfat.h"
21#include "fs/Vfat.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080022
Elliott Hughes7e128fb2015-12-04 15:50:53 -080023#include <android-base/logging.h>
Sudheer Shanka40ab6742018-09-18 13:07:45 -070024#include <android-base/properties.h>
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070025#include <android-base/stringprintf.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080026#include <cutils/fs.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080027#include <private/android_filesystem_config.h>
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -060028#include <utils/Timers.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080029
30#include <fcntl.h>
31#include <stdlib.h>
32#include <sys/mount.h>
33#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070034#include <sys/sysmacros.h>
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070035#include <sys/types.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080036#include <sys/wait.h>
37
Sudheer Shanka40ab6742018-09-18 13:07:45 -070038using android::base::GetBoolProperty;
Dan Albertae9e8902015-03-16 10:35:17 -070039using android::base::StringPrintf;
40
Jeff Sharkeydeb24052015-03-02 21:01:40 -080041namespace android {
42namespace vold {
43
Jeff Sharkeydeb24052015-03-02 21:01:40 -080044static const char* kFusePath = "/system/bin/sdcard";
45
Jeff Sharkey36801cc2015-03-13 16:09:20 -070046static const char* kAsecPath = "/mnt/secure/asec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080047
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070048PublicVolume::PublicVolume(dev_t device) : VolumeBase(Type::kPublic), mDevice(device), mFusePid(0) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070049 setId(StringPrintf("public:%u,%u", major(device), minor(device)));
50 mDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -080051}
52
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070053PublicVolume::~PublicVolume() {}
Jeff Sharkeydeb24052015-03-02 21:01:40 -080054
55status_t PublicVolume::readMetadata() {
Jeff Sharkey3472e522017-10-06 18:02:53 -060056 status_t res = ReadMetadataUntrusted(mDevPath, &mFsType, &mFsUuid, &mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060057
Jeff Sharkey814e9d32017-09-13 11:49:44 -060058 auto listener = getListener();
59 if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060060
Jeff Sharkey36801cc2015-03-13 16:09:20 -070061 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080062}
63
64status_t PublicVolume::initAsecStage() {
65 std::string legacyPath(mRawPath + "/android_secure");
66 std::string securePath(mRawPath + "/.android_secure");
67
68 // Recover legacy secure path
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070069 if (!access(legacyPath.c_str(), R_OK | X_OK) && access(securePath.c_str(), R_OK | X_OK)) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -080070 if (rename(legacyPath.c_str(), securePath.c_str())) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070071 PLOG(WARNING) << getId() << " failed to rename legacy ASEC dir";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080072 }
73 }
74
Jeff Sharkey36801cc2015-03-13 16:09:20 -070075 if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) {
76 if (errno != EEXIST) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070077 PLOG(WARNING) << getId() << " creating ASEC stage failed";
Jeff Sharkey36801cc2015-03-13 16:09:20 -070078 return -errno;
79 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -080080 }
81
Jeff Sharkey36801cc2015-03-13 16:09:20 -070082 BindMount(securePath, kAsecPath);
83
Jeff Sharkeydeb24052015-03-02 21:01:40 -080084 return OK;
85}
86
Jeff Sharkey9c484982015-03-31 10:35:33 -070087status_t PublicVolume::doCreate() {
88 return CreateDeviceNode(mDevPath, mDevice);
89}
90
91status_t PublicVolume::doDestroy() {
92 return DestroyDeviceNode(mDevPath);
93}
94
Jeff Sharkeydeb24052015-03-02 21:01:40 -080095status_t PublicVolume::doMount() {
Jeff Sharkey9c484982015-03-31 10:35:33 -070096 readMetadata();
97
Jeff Sharkey37ba1252018-01-19 10:55:18 +090098 if (mFsType == "vfat" && vfat::IsSupported()) {
99 if (vfat::Check(mDevPath)) {
100 LOG(ERROR) << getId() << " failed filesystem check";
101 return -EIO;
102 }
103 } else if (mFsType == "exfat" && exfat::IsSupported()) {
104 if (exfat::Check(mDevPath)) {
105 LOG(ERROR) << getId() << " failed filesystem check";
106 return -EIO;
107 }
108 } else {
Makoto Onukic82c9ce2015-06-24 13:30:45 -0700109 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
110 return -EIO;
111 }
112
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700113 // Use UUID as stable name, if available
114 std::string stableName = getId();
115 if (!mFsUuid.empty()) {
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700116 stableName = mFsUuid;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700117 }
118
119 mRawPath = StringPrintf("/mnt/media_rw/%s", stableName.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700120
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700121 mFuseDefault = StringPrintf("/mnt/runtime/default/%s", stableName.c_str());
122 mFuseRead = StringPrintf("/mnt/runtime/read/%s", stableName.c_str());
123 mFuseWrite = StringPrintf("/mnt/runtime/write/%s", stableName.c_str());
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800124 mFuseFull = StringPrintf("/mnt/runtime/full/%s", stableName.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700125
126 setInternalPath(mRawPath);
Jeff Sharkey8474ee32015-07-30 16:54:23 -0700127 if (getMountFlags() & MountFlags::kVisible) {
128 setPath(StringPrintf("/storage/%s", stableName.c_str()));
129 } else {
130 setPath(mRawPath);
131 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700132
Qin Chaoe0074f12015-12-15 15:20:41 +0800133 if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700134 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800135 return -errno;
136 }
137
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900138 if (mFsType == "vfat") {
139 if (vfat::Mount(mDevPath, mRawPath, false, false, false, AID_MEDIA_RW, AID_MEDIA_RW, 0007,
140 true)) {
141 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
142 return -EIO;
143 }
144 } else if (mFsType == "exfat") {
145 if (exfat::Mount(mDevPath, mRawPath, AID_MEDIA_RW, AID_MEDIA_RW, 0007)) {
146 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
147 return -EIO;
148 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800149 }
150
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700151 if (getMountFlags() & MountFlags::kPrimary) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700152 initAsecStage();
153 }
154
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700155 if (!(getMountFlags() & MountFlags::kVisible)) {
156 // Not visible to apps, so no need to spin up FUSE
157 return OK;
158 }
159
Qin Chaoe0074f12015-12-15 15:20:41 +0800160 if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700161 fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800162 fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT) ||
163 fs_prepare_dir(mFuseFull.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Qin Chaoe0074f12015-12-15 15:20:41 +0800164 PLOG(ERROR) << getId() << " failed to create FUSE mount points";
165 return -errno;
166 }
167
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800168 dev_t before = GetDevice(mFuseFull);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700169
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800170 if (!(mFusePid = fork())) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700171 if (getMountFlags() & MountFlags::kPrimary) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700172 // clang-format off
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700173 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800174 "-u", "1023", // AID_MEDIA_RW
175 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey20642ae2015-07-28 10:57:29 -0700176 "-U", std::to_string(getMountUserId()).c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700177 "-w",
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800178 mRawPath.c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700179 stableName.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700180 NULL)) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700181 // clang-format on
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700182 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800183 }
184 } else {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700185 // clang-format off
Sudheer Shanka55049012019-01-09 12:15:15 -0800186 if (execl(kFusePath, kFusePath,
187 "-u", "1023", // AID_MEDIA_RW
188 "-g", "1023", // AID_MEDIA_RW
189 "-U", std::to_string(getMountUserId()).c_str(),
190 mRawPath.c_str(),
191 stableName.c_str(),
192 NULL)) {
193 // clang-format on
194 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800195 }
196 }
197
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700198 LOG(ERROR) << "FUSE exiting";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800199 _exit(1);
200 }
201
202 if (mFusePid == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700203 PLOG(ERROR) << getId() << " failed to fork";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800204 return -errno;
205 }
206
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600207 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800208 while (before == GetDevice(mFuseFull)) {
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700209 LOG(DEBUG) << "Waiting for FUSE to spin up...";
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700210 usleep(50000); // 50ms
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600211
212 nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
213 if (nanoseconds_to_milliseconds(now - start) > 5000) {
214 LOG(WARNING) << "Timed out while waiting for FUSE to spin up";
215 return -ETIMEDOUT;
216 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700217 }
Daniel Rosenberg1d79d102017-07-11 17:59:55 -0700218 /* sdcardfs will have exited already. FUSE will still be running */
Daniel Rosenberg8b9a5b32018-03-12 15:47:23 -0700219 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
220 mFusePid = 0;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700221
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800222 return OK;
223}
224
225status_t PublicVolume::doUnmount() {
John Cormie25cc7e32016-04-18 14:23:29 -0700226 // Unmount the storage before we kill the FUSE process. If we kill
227 // the FUSE process first, most file system operations will return
228 // ENOTCONN until the unmount completes. This is an exotic and unusual
229 // error code and might cause broken behaviour in applications.
Jeff Sharkey8aff8542016-03-30 20:37:28 -0600230 KillProcessesUsingPath(getPath());
231
Jeff Sharkey48d81d32015-04-12 21:50:32 -0700232 ForceUnmount(kAsecPath);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700233
234 ForceUnmount(mFuseDefault);
235 ForceUnmount(mFuseRead);
236 ForceUnmount(mFuseWrite);
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800237 ForceUnmount(mFuseFull);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800238 ForceUnmount(mRawPath);
239
Jeff Sharkey66270a22015-06-24 11:49:24 -0700240 rmdir(mFuseDefault.c_str());
241 rmdir(mFuseRead.c_str());
242 rmdir(mFuseWrite.c_str());
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800243 rmdir(mFuseFull.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700244 rmdir(mRawPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700245
Jeff Sharkey66270a22015-06-24 11:49:24 -0700246 mFuseDefault.clear();
247 mFuseRead.clear();
248 mFuseWrite.clear();
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800249 mFuseFull.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700250 mRawPath.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800251
252 return OK;
253}
254
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700255status_t PublicVolume::doFormat(const std::string& fsType) {
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200256 bool useVfat = vfat::IsSupported();
257 bool useExfat = exfat::IsSupported();
258 status_t res = OK;
259
260 // Resolve the target filesystem type
261 if (fsType == "auto" && useVfat && useExfat) {
262 uint64_t size = 0;
263
264 res = GetBlockDevSize(mDevPath, &size);
265 if (res != OK) {
266 LOG(ERROR) << "Couldn't get device size " << mDevPath;
267 return res;
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700268 }
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200269
270 // If both vfat & exfat are supported use exfat for SDXC (>~32GiB) cards
271 if (size > 32896LL * 1024 * 1024) {
272 useVfat = false;
273 } else {
274 useExfat = false;
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700275 }
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200276 } else if (fsType == "vfat") {
277 useExfat = false;
278 } else if (fsType == "exfat") {
279 useVfat = false;
280 }
281
282 if (!useVfat && !useExfat) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700283 LOG(ERROR) << "Unsupported filesystem " << fsType;
284 return -EINVAL;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800285 }
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700286
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200287 if (WipeBlockDevice(mDevPath) != OK) {
288 LOG(WARNING) << getId() << " failed to wipe";
289 }
290
291 if (useVfat) {
292 res = vfat::Format(mDevPath, 0);
293 } else if (useExfat) {
294 res = exfat::Format(mDevPath);
295 }
296
297 if (res != OK) {
298 LOG(ERROR) << getId() << " failed to format";
299 res = -errno;
300 }
301
302 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800303}
304
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800305} // namespace vold
306} // namespace android