blob: 73bf6d172f1bd81e53fba1cd1a58e0defff3201f [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 "EmulatedVolume.h"
18#include "Utils.h"
Sudheer Shanka40ab6742018-09-18 13:07:45 -070019#include "VolumeManager.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080020
Elliott Hughes7e128fb2015-12-04 15:50:53 -080021#include <android-base/logging.h>
Sudheer Shanka53947a32018-08-01 10:24:13 -070022#include <android-base/stringprintf.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080023#include <cutils/fs.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080024#include <private/android_filesystem_config.h>
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -060025#include <utils/Timers.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080026
27#include <fcntl.h>
28#include <stdlib.h>
29#include <sys/mount.h>
30#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070031#include <sys/sysmacros.h>
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070032#include <sys/types.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080033#include <sys/wait.h>
34
Dan Albertae9e8902015-03-16 10:35:17 -070035using android::base::StringPrintf;
36
Jeff Sharkeydeb24052015-03-02 21:01:40 -080037namespace android {
38namespace vold {
39
40static const char* kFusePath = "/system/bin/sdcard";
41
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070042EmulatedVolume::EmulatedVolume(const std::string& rawPath)
43 : VolumeBase(Type::kEmulated), mFusePid(0) {
Jeff Sharkey3161fb32015-04-12 16:03:33 -070044 setId("emulated");
Jeff Sharkeydeb24052015-03-02 21:01:40 -080045 mRawPath = rawPath;
Jeff Sharkey66270a22015-06-24 11:49:24 -070046 mLabel = "emulated";
Jeff Sharkey3161fb32015-04-12 16:03:33 -070047}
48
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070049EmulatedVolume::EmulatedVolume(const std::string& rawPath, dev_t device, const std::string& fsUuid)
50 : VolumeBase(Type::kEmulated), mFusePid(0) {
Jeff Sharkey3161fb32015-04-12 16:03:33 -070051 setId(StringPrintf("emulated:%u,%u", major(device), minor(device)));
Jeff Sharkey3161fb32015-04-12 16:03:33 -070052 mRawPath = rawPath;
Jeff Sharkey66270a22015-06-24 11:49:24 -070053 mLabel = fsUuid;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080054}
55
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070056EmulatedVolume::~EmulatedVolume() {}
Jeff Sharkeydeb24052015-03-02 21:01:40 -080057
58status_t EmulatedVolume::doMount() {
Jeff Sharkey81f55c62015-07-07 14:37:03 -070059 // We could have migrated storage to an adopted private volume, so always
60 // call primary storage "emulated" to avoid media rescans.
61 std::string label = mLabel;
62 if (getMountFlags() & MountFlags::kPrimary) {
63 label = "emulated";
64 }
65
Jeff Sharkey1bd078f2015-08-06 11:40:00 -070066 mFuseDefault = StringPrintf("/mnt/runtime/default/%s", label.c_str());
67 mFuseRead = StringPrintf("/mnt/runtime/read/%s", label.c_str());
68 mFuseWrite = StringPrintf("/mnt/runtime/write/%s", label.c_str());
Sudheer Shankadd4bb172019-01-16 23:35:49 -080069 mFuseFull = StringPrintf("/mnt/runtime/full/%s", label.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -070070
71 setInternalPath(mRawPath);
Jeff Sharkey81f55c62015-07-07 14:37:03 -070072 setPath(StringPrintf("/storage/%s", label.c_str()));
Sudheer Shanka53947a32018-08-01 10:24:13 -070073 setLabel(label);
Jeff Sharkey66270a22015-06-24 11:49:24 -070074
75 if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070076 fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
Sudheer Shankadd4bb172019-01-16 23:35:49 -080077 fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT) ||
78 fs_prepare_dir(mFuseFull.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -070079 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080080 return -errno;
81 }
82
Sudheer Shankadd4bb172019-01-16 23:35:49 -080083 dev_t before = GetDevice(mFuseFull);
Jeff Sharkey36801cc2015-03-13 16:09:20 -070084
Jeff Sharkeydeb24052015-03-02 21:01:40 -080085 if (!(mFusePid = fork())) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070086 // clang-format off
Jeff Sharkey36801cc2015-03-13 16:09:20 -070087 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -080088 "-u", "1023", // AID_MEDIA_RW
89 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey66270a22015-06-24 11:49:24 -070090 "-m",
91 "-w",
Rom Lemarchand958c2162017-09-15 18:48:01 +000092 "-G",
Jeff Sharkeyd7e51762018-01-08 11:48:07 -070093 "-i",
Sudheer Shanka8cad97b2019-03-05 10:31:27 -080094 "-o",
Jeff Sharkeydeb24052015-03-02 21:01:40 -080095 mRawPath.c_str(),
Jeff Sharkey81f55c62015-07-07 14:37:03 -070096 label.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -070097 NULL)) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070098 // clang-format on
Jeff Sharkey36801cc2015-03-13 16:09:20 -070099 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800100 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700101
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700102 LOG(ERROR) << "FUSE exiting";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800103 _exit(1);
104 }
105
106 if (mFusePid == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700107 PLOG(ERROR) << getId() << " failed to fork";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800108 return -errno;
109 }
110
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600111 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800112 while (before == GetDevice(mFuseFull)) {
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700113 LOG(DEBUG) << "Waiting for FUSE to spin up...";
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700114 usleep(50000); // 50ms
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600115
116 nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
117 if (nanoseconds_to_milliseconds(now - start) > 5000) {
118 LOG(WARNING) << "Timed out while waiting for FUSE to spin up";
119 return -ETIMEDOUT;
120 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700121 }
Daniel Rosenberg1d79d102017-07-11 17:59:55 -0700122 /* sdcardfs will have exited already. FUSE will still be running */
Daniel Rosenberg8b9a5b32018-03-12 15:47:23 -0700123 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
124 mFusePid = 0;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700125
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800126 return OK;
127}
128
129status_t EmulatedVolume::doUnmount() {
Narayan Kamathea243a32016-01-21 12:26:05 +0000130 // Unmount the storage before we kill the FUSE process. If we kill
131 // the FUSE process first, most file system operations will return
132 // ENOTCONN until the unmount completes. This is an exotic and unusual
133 // error code and might cause broken behaviour in applications.
134 KillProcessesUsingPath(getPath());
135 ForceUnmount(mFuseDefault);
136 ForceUnmount(mFuseRead);
137 ForceUnmount(mFuseWrite);
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800138 ForceUnmount(mFuseFull);
Narayan Kamathea243a32016-01-21 12:26:05 +0000139
Jeff Sharkey66270a22015-06-24 11:49:24 -0700140 rmdir(mFuseDefault.c_str());
141 rmdir(mFuseRead.c_str());
142 rmdir(mFuseWrite.c_str());
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800143 rmdir(mFuseFull.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700144
145 mFuseDefault.clear();
146 mFuseRead.clear();
147 mFuseWrite.clear();
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800148 mFuseFull.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800149
150 return OK;
151}
152
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800153} // namespace vold
154} // namespace android