blob: 403e85cc70563e150ca1263b34b65f07fe4b6c48 [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>
39
Sudheer Shanka40ab6742018-09-18 13:07:45 -070040using android::base::GetBoolProperty;
Dan Albertae9e8902015-03-16 10:35:17 -070041using android::base::StringPrintf;
42
Jeff Sharkeydeb24052015-03-02 21:01:40 -080043namespace android {
44namespace vold {
45
Jeff Sharkeydeb24052015-03-02 21:01:40 -080046static const char* kFusePath = "/system/bin/sdcard";
47
Jeff Sharkey36801cc2015-03-13 16:09:20 -070048static const char* kAsecPath = "/mnt/secure/asec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080049
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 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700134
Qin Chaoe0074f12015-12-15 15:20:41 +0800135 if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700136 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800137 return -errno;
138 }
139
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900140 if (mFsType == "vfat") {
141 if (vfat::Mount(mDevPath, mRawPath, false, false, false, AID_MEDIA_RW, AID_MEDIA_RW, 0007,
142 true)) {
143 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
144 return -EIO;
145 }
146 } else if (mFsType == "exfat") {
147 if (exfat::Mount(mDevPath, mRawPath, AID_MEDIA_RW, AID_MEDIA_RW, 0007)) {
148 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
149 return -EIO;
150 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800151 }
152
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700153 if (getMountFlags() & MountFlags::kPrimary) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700154 initAsecStage();
155 }
156
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700157 if (!(getMountFlags() & MountFlags::kVisible)) {
158 // Not visible to apps, so no need to spin up FUSE
159 return OK;
160 }
161
Qin Chaoe0074f12015-12-15 15:20:41 +0800162 if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700163 fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800164 fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT) ||
165 fs_prepare_dir(mFuseFull.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Qin Chaoe0074f12015-12-15 15:20:41 +0800166 PLOG(ERROR) << getId() << " failed to create FUSE mount points";
167 return -errno;
168 }
169
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800170 dev_t before = GetDevice(mFuseFull);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700171
shafik5cf32b52019-09-25 13:56:01 +0100172 bool isFuse = base::GetBoolProperty(kPropFuseSnapshot, false);
Zim3623a212019-07-19 16:46:53 +0100173 if (isFuse) {
174 LOG(INFO) << "Mounting public fuse volume";
Nandana Dutta914cc72019-08-29 15:22:42 +0100175 android::base::unique_fd fd;
Zim981222f2019-09-09 10:24:44 +0100176 int user_id = getMountUserId();
Zima438b242019-09-25 14:37:38 +0100177 int result = MountUserFuse(user_id, getInternalPath(), stableName, &fd);
Zim981222f2019-09-09 10:24:44 +0100178
Zim3623a212019-07-19 16:46:53 +0100179 if (result != 0) {
180 LOG(ERROR) << "Failed to mount public fuse volume";
181 return -result;
182 }
Nandana Dutta914cc72019-08-29 15:22:42 +0100183 setFuseFd(std::move(fd));
Zima438b242019-09-25 14:37:38 +0100184 return OK;
Zim3623a212019-07-19 16:46:53 +0100185 }
186
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800187 if (!(mFusePid = fork())) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700188 if (getMountFlags() & MountFlags::kPrimary) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700189 // clang-format off
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700190 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800191 "-u", "1023", // AID_MEDIA_RW
192 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey20642ae2015-07-28 10:57:29 -0700193 "-U", std::to_string(getMountUserId()).c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700194 "-w",
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800195 mRawPath.c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700196 stableName.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700197 NULL)) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700198 // clang-format on
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700199 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800200 }
201 } else {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700202 // clang-format off
Sudheer Shanka55049012019-01-09 12:15:15 -0800203 if (execl(kFusePath, kFusePath,
204 "-u", "1023", // AID_MEDIA_RW
205 "-g", "1023", // AID_MEDIA_RW
206 "-U", std::to_string(getMountUserId()).c_str(),
207 mRawPath.c_str(),
208 stableName.c_str(),
209 NULL)) {
210 // clang-format on
211 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800212 }
213 }
214
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700215 LOG(ERROR) << "FUSE exiting";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800216 _exit(1);
217 }
218
219 if (mFusePid == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700220 PLOG(ERROR) << getId() << " failed to fork";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800221 return -errno;
222 }
223
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600224 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800225 while (before == GetDevice(mFuseFull)) {
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700226 LOG(DEBUG) << "Waiting for FUSE to spin up...";
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700227 usleep(50000); // 50ms
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600228
229 nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
230 if (nanoseconds_to_milliseconds(now - start) > 5000) {
231 LOG(WARNING) << "Timed out while waiting for FUSE to spin up";
232 return -ETIMEDOUT;
233 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700234 }
Daniel Rosenberg1d79d102017-07-11 17:59:55 -0700235 /* sdcardfs will have exited already. FUSE will still be running */
Daniel Rosenberg8b9a5b32018-03-12 15:47:23 -0700236 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
237 mFusePid = 0;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700238
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800239 return OK;
240}
241
242status_t PublicVolume::doUnmount() {
John Cormie25cc7e32016-04-18 14:23:29 -0700243 // Unmount the storage before we kill the FUSE process. If we kill
244 // the FUSE process first, most file system operations will return
245 // ENOTCONN until the unmount completes. This is an exotic and unusual
246 // error code and might cause broken behaviour in applications.
Jeff Sharkey8aff8542016-03-30 20:37:28 -0600247 KillProcessesUsingPath(getPath());
248
shafik5cf32b52019-09-25 13:56:01 +0100249 bool isFuse = base::GetBoolProperty(kPropFuseSnapshot, false);
Zim3623a212019-07-19 16:46:53 +0100250 if (isFuse) {
251 // Use UUID as stable name, if available
252 std::string stableName = getId();
253 if (!mFsUuid.empty()) {
254 stableName = mFsUuid;
255 }
256
Zima438b242019-09-25 14:37:38 +0100257 std::string fuse_path(
258 StringPrintf("/mnt/user/%d/%s", getMountUserId(), stableName.c_str()));
259 std::string pass_through_path(
260 StringPrintf("/mnt/pass_through/%d/%s", getMountUserId(), stableName.c_str()));
261 if (UnmountUserFuse(pass_through_path, fuse_path) != OK) {
262 PLOG(INFO) << "UnmountUserFuse failed on public fuse volume";
263 return -errno;
Zim3623a212019-07-19 16:46:53 +0100264 }
Zima438b242019-09-25 14:37:38 +0100265 ForceUnmount(kAsecPath);
266 ForceUnmount(mRawPath);
Zim3623a212019-07-19 16:46:53 +0100267
Zima438b242019-09-25 14:37:38 +0100268 rmdir(fuse_path.c_str());
269 rmdir(pass_through_path.c_str());
270 rmdir(mRawPath.c_str());
271 mRawPath.clear();
272
Nandana Dutta914cc72019-08-29 15:22:42 +0100273 setFuseFd(android::base::unique_fd());
Zima438b242019-09-25 14:37:38 +0100274
Zim3623a212019-07-19 16:46:53 +0100275 return OK;
276 }
277
Jeff Sharkey48d81d32015-04-12 21:50:32 -0700278 ForceUnmount(kAsecPath);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700279
280 ForceUnmount(mFuseDefault);
281 ForceUnmount(mFuseRead);
282 ForceUnmount(mFuseWrite);
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800283 ForceUnmount(mFuseFull);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800284 ForceUnmount(mRawPath);
285
Jeff Sharkey66270a22015-06-24 11:49:24 -0700286 rmdir(mFuseDefault.c_str());
287 rmdir(mFuseRead.c_str());
288 rmdir(mFuseWrite.c_str());
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800289 rmdir(mFuseFull.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700290 rmdir(mRawPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700291
Jeff Sharkey66270a22015-06-24 11:49:24 -0700292 mFuseDefault.clear();
293 mFuseRead.clear();
294 mFuseWrite.clear();
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800295 mFuseFull.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700296 mRawPath.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800297
298 return OK;
299}
300
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700301status_t PublicVolume::doFormat(const std::string& fsType) {
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200302 bool useVfat = vfat::IsSupported();
303 bool useExfat = exfat::IsSupported();
304 status_t res = OK;
305
306 // Resolve the target filesystem type
307 if (fsType == "auto" && useVfat && useExfat) {
308 uint64_t size = 0;
309
310 res = GetBlockDevSize(mDevPath, &size);
311 if (res != OK) {
312 LOG(ERROR) << "Couldn't get device size " << mDevPath;
313 return res;
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700314 }
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200315
316 // If both vfat & exfat are supported use exfat for SDXC (>~32GiB) cards
317 if (size > 32896LL * 1024 * 1024) {
318 useVfat = false;
319 } else {
320 useExfat = false;
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700321 }
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200322 } else if (fsType == "vfat") {
323 useExfat = false;
324 } else if (fsType == "exfat") {
325 useVfat = false;
326 }
327
328 if (!useVfat && !useExfat) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700329 LOG(ERROR) << "Unsupported filesystem " << fsType;
330 return -EINVAL;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800331 }
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700332
Oleksiy Avramchenko4cff06d2018-05-23 18:59:48 +0200333 if (WipeBlockDevice(mDevPath) != OK) {
334 LOG(WARNING) << getId() << " failed to wipe";
335 }
336
337 if (useVfat) {
338 res = vfat::Format(mDevPath, 0);
339 } else if (useExfat) {
340 res = exfat::Format(mDevPath);
341 }
342
343 if (res != OK) {
344 LOG(ERROR) << getId() << " failed to format";
345 res = -errno;
346 }
347
348 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800349}
350
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800351} // namespace vold
352} // namespace android