blob: cf18d73a8713bfd37096164bd26ae49e6cb53190 [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
Sudheer Shanka40ab6742018-09-18 13:07:45 -070048static const char* kIsolatedStorage = "persist.sys.isolated_storage";
49
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070050PublicVolume::PublicVolume(dev_t device) : VolumeBase(Type::kPublic), mDevice(device), mFusePid(0) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070051 setId(StringPrintf("public:%u,%u", major(device), minor(device)));
52 mDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -080053}
54
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070055PublicVolume::~PublicVolume() {}
Jeff Sharkeydeb24052015-03-02 21:01:40 -080056
57status_t PublicVolume::readMetadata() {
Jeff Sharkey3472e522017-10-06 18:02:53 -060058 status_t res = ReadMetadataUntrusted(mDevPath, &mFsType, &mFsUuid, &mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060059
Jeff Sharkey814e9d32017-09-13 11:49:44 -060060 auto listener = getListener();
61 if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060062
Jeff Sharkey36801cc2015-03-13 16:09:20 -070063 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080064}
65
66status_t PublicVolume::initAsecStage() {
67 std::string legacyPath(mRawPath + "/android_secure");
68 std::string securePath(mRawPath + "/.android_secure");
69
70 // Recover legacy secure path
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070071 if (!access(legacyPath.c_str(), R_OK | X_OK) && access(securePath.c_str(), R_OK | X_OK)) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -080072 if (rename(legacyPath.c_str(), securePath.c_str())) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070073 PLOG(WARNING) << getId() << " failed to rename legacy ASEC dir";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080074 }
75 }
76
Jeff Sharkey36801cc2015-03-13 16:09:20 -070077 if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) {
78 if (errno != EEXIST) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070079 PLOG(WARNING) << getId() << " creating ASEC stage failed";
Jeff Sharkey36801cc2015-03-13 16:09:20 -070080 return -errno;
81 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -080082 }
83
Jeff Sharkey36801cc2015-03-13 16:09:20 -070084 BindMount(securePath, kAsecPath);
85
Jeff Sharkeydeb24052015-03-02 21:01:40 -080086 return OK;
87}
88
Jeff Sharkey9c484982015-03-31 10:35:33 -070089status_t PublicVolume::doCreate() {
90 return CreateDeviceNode(mDevPath, mDevice);
91}
92
93status_t PublicVolume::doDestroy() {
94 return DestroyDeviceNode(mDevPath);
95}
96
Jeff Sharkeydeb24052015-03-02 21:01:40 -080097status_t PublicVolume::doMount() {
Jeff Sharkey9c484982015-03-31 10:35:33 -070098 readMetadata();
99
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900100 if (mFsType == "vfat" && vfat::IsSupported()) {
101 if (vfat::Check(mDevPath)) {
102 LOG(ERROR) << getId() << " failed filesystem check";
103 return -EIO;
104 }
105 } else if (mFsType == "exfat" && exfat::IsSupported()) {
106 if (exfat::Check(mDevPath)) {
107 LOG(ERROR) << getId() << " failed filesystem check";
108 return -EIO;
109 }
110 } else {
Makoto Onukic82c9ce2015-06-24 13:30:45 -0700111 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
112 return -EIO;
113 }
114
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700115 // Use UUID as stable name, if available
116 std::string stableName = getId();
117 if (!mFsUuid.empty()) {
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700118 stableName = mFsUuid;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700119 }
120
121 mRawPath = StringPrintf("/mnt/media_rw/%s", stableName.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700122
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700123 mFuseDefault = StringPrintf("/mnt/runtime/default/%s", stableName.c_str());
124 mFuseRead = StringPrintf("/mnt/runtime/read/%s", stableName.c_str());
125 mFuseWrite = StringPrintf("/mnt/runtime/write/%s", stableName.c_str());
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800126 mFuseFull = StringPrintf("/mnt/runtime/full/%s", stableName.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700127
128 setInternalPath(mRawPath);
Jeff Sharkey8474ee32015-07-30 16:54:23 -0700129 if (getMountFlags() & MountFlags::kVisible) {
130 setPath(StringPrintf("/storage/%s", stableName.c_str()));
131 } else {
132 setPath(mRawPath);
133 }
Sudheer Shanka53947a32018-08-01 10:24:13 -0700134 setLabel(stableName);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700135
Qin Chaoe0074f12015-12-15 15:20:41 +0800136 if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700137 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800138 return -errno;
139 }
140
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900141 if (mFsType == "vfat") {
142 if (vfat::Mount(mDevPath, mRawPath, false, false, false, AID_MEDIA_RW, AID_MEDIA_RW, 0007,
143 true)) {
144 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
145 return -EIO;
146 }
147 } else if (mFsType == "exfat") {
148 if (exfat::Mount(mDevPath, mRawPath, AID_MEDIA_RW, AID_MEDIA_RW, 0007)) {
149 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
150 return -EIO;
151 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800152 }
153
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700154 if (getMountFlags() & MountFlags::kPrimary) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700155 initAsecStage();
156 }
157
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700158 if (!(getMountFlags() & MountFlags::kVisible)) {
159 // Not visible to apps, so no need to spin up FUSE
160 return OK;
161 }
162
Qin Chaoe0074f12015-12-15 15:20:41 +0800163 if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700164 fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800165 fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT) ||
166 fs_prepare_dir(mFuseFull.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Qin Chaoe0074f12015-12-15 15:20:41 +0800167 PLOG(ERROR) << getId() << " failed to create FUSE mount points";
168 return -errno;
169 }
170
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800171 dev_t before = GetDevice(mFuseFull);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700172
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800173 if (!(mFusePid = fork())) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700174 if (getMountFlags() & MountFlags::kPrimary) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700175 // clang-format off
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700176 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800177 "-u", "1023", // AID_MEDIA_RW
178 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey20642ae2015-07-28 10:57:29 -0700179 "-U", std::to_string(getMountUserId()).c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700180 "-w",
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800181 mRawPath.c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700182 stableName.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700183 NULL)) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700184 // clang-format on
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700185 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800186 }
187 } else {
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700188 // In Pre-Q, apps have full read access to secondary storage devices but only
189 // write access for their package specific directories. In Q, they only have access
190 // to their own sandboxes and they can write anywhere inside the sandbox. Instead of
191 // updating sdcardfs to allow packages writing into their own sandboxes, we could
192 // just allow them to write anywhere by passing "-w".
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700193 // clang-format off
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700194 if (GetBoolProperty(kIsolatedStorage, false)) {
195 if (execl(kFusePath, kFusePath,
196 "-u", "1023", // AID_MEDIA_RW
197 "-g", "1023", // AID_MEDIA_RW
198 "-U", std::to_string(getMountUserId()).c_str(),
199 "-w",
200 mRawPath.c_str(),
201 stableName.c_str(),
202 NULL)) {
203 // clang-format on
204 PLOG(ERROR) << "Failed to exec";
205 }
206 } else {
207 // clang-format off
208 if (execl(kFusePath, kFusePath,
209 "-u", "1023", // AID_MEDIA_RW
210 "-g", "1023", // AID_MEDIA_RW
211 "-U", std::to_string(getMountUserId()).c_str(),
212 mRawPath.c_str(),
213 stableName.c_str(),
214 NULL)) {
215 // clang-format on
216 PLOG(ERROR) << "Failed to exec";
217 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800218 }
219 }
220
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700221 LOG(ERROR) << "FUSE exiting";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800222 _exit(1);
223 }
224
225 if (mFusePid == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700226 PLOG(ERROR) << getId() << " failed to fork";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800227 return -errno;
228 }
229
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600230 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800231 while (before == GetDevice(mFuseFull)) {
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700232 LOG(DEBUG) << "Waiting for FUSE to spin up...";
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700233 usleep(50000); // 50ms
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600234
235 nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
236 if (nanoseconds_to_milliseconds(now - start) > 5000) {
237 LOG(WARNING) << "Timed out while waiting for FUSE to spin up";
238 return -ETIMEDOUT;
239 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700240 }
Daniel Rosenberg1d79d102017-07-11 17:59:55 -0700241 /* sdcardfs will have exited already. FUSE will still be running */
Daniel Rosenberg8b9a5b32018-03-12 15:47:23 -0700242 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
243 mFusePid = 0;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700244
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800245 return OK;
246}
247
248status_t PublicVolume::doUnmount() {
John Cormie25cc7e32016-04-18 14:23:29 -0700249 // Unmount the storage before we kill the FUSE process. If we kill
250 // the FUSE process first, most file system operations will return
251 // ENOTCONN until the unmount completes. This is an exotic and unusual
252 // error code and might cause broken behaviour in applications.
Jeff Sharkey8aff8542016-03-30 20:37:28 -0600253 KillProcessesUsingPath(getPath());
254
Jeff Sharkey48d81d32015-04-12 21:50:32 -0700255 ForceUnmount(kAsecPath);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700256
257 ForceUnmount(mFuseDefault);
258 ForceUnmount(mFuseRead);
259 ForceUnmount(mFuseWrite);
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800260 ForceUnmount(mFuseFull);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800261 ForceUnmount(mRawPath);
262
Jeff Sharkey66270a22015-06-24 11:49:24 -0700263 rmdir(mFuseDefault.c_str());
264 rmdir(mFuseRead.c_str());
265 rmdir(mFuseWrite.c_str());
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800266 rmdir(mFuseFull.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700267 rmdir(mRawPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700268
Jeff Sharkey66270a22015-06-24 11:49:24 -0700269 mFuseDefault.clear();
270 mFuseRead.clear();
271 mFuseWrite.clear();
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800272 mFuseFull.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700273 mRawPath.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800274
275 return OK;
276}
277
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700278status_t PublicVolume::doFormat(const std::string& fsType) {
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200279 bool useVfat = vfat::IsSupported();
280 bool useExfat = exfat::IsSupported();
281 status_t res = OK;
282
283 // Resolve the target filesystem type
284 if (fsType == "auto" && useVfat && useExfat) {
285 uint64_t size = 0;
286
287 res = GetBlockDevSize(mDevPath, &size);
288 if (res != OK) {
289 LOG(ERROR) << "Couldn't get device size " << mDevPath;
290 return res;
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700291 }
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200292
293 // If both vfat & exfat are supported use exfat for SDXC (>~32GiB) cards
294 if (size > 32896LL * 1024 * 1024) {
295 useVfat = false;
296 } else {
297 useExfat = false;
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700298 }
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200299 } else if (fsType == "vfat") {
300 useExfat = false;
301 } else if (fsType == "exfat") {
302 useVfat = false;
303 }
304
305 if (!useVfat && !useExfat) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700306 LOG(ERROR) << "Unsupported filesystem " << fsType;
307 return -EINVAL;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800308 }
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700309
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200310 if (WipeBlockDevice(mDevPath) != OK) {
311 LOG(WARNING) << getId() << " failed to wipe";
312 }
313
314 if (useVfat) {
315 res = vfat::Format(mDevPath, 0);
316 } else if (useExfat) {
317 res = exfat::Format(mDevPath);
318 }
319
320 if (res != OK) {
321 LOG(ERROR) << getId() << " failed to format";
322 res = -errno;
323 }
324
325 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800326}
327
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800328} // namespace vold
329} // namespace android