blob: 552fe2f188dedc3ce3cbf8d5544197ff08644177 [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()));
Jeff Sharkey66270a22015-06-24 11:49:24 -070073
74 if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070075 fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
Sudheer Shankadd4bb172019-01-16 23:35:49 -080076 fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT) ||
77 fs_prepare_dir(mFuseFull.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -070078 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080079 return -errno;
80 }
81
Sudheer Shankadd4bb172019-01-16 23:35:49 -080082 dev_t before = GetDevice(mFuseFull);
Jeff Sharkey36801cc2015-03-13 16:09:20 -070083
Jeff Sharkeydeb24052015-03-02 21:01:40 -080084 if (!(mFusePid = fork())) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070085 // clang-format off
Jeff Sharkey36801cc2015-03-13 16:09:20 -070086 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -080087 "-u", "1023", // AID_MEDIA_RW
88 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey66270a22015-06-24 11:49:24 -070089 "-m",
90 "-w",
Rom Lemarchand958c2162017-09-15 18:48:01 +000091 "-G",
Jeff Sharkeyd7e51762018-01-08 11:48:07 -070092 "-i",
Sudheer Shanka8cad97b2019-03-05 10:31:27 -080093 "-o",
Jeff Sharkeydeb24052015-03-02 21:01:40 -080094 mRawPath.c_str(),
Jeff Sharkey81f55c62015-07-07 14:37:03 -070095 label.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -070096 NULL)) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070097 // clang-format on
Jeff Sharkey36801cc2015-03-13 16:09:20 -070098 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080099 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700100
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700101 LOG(ERROR) << "FUSE exiting";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800102 _exit(1);
103 }
104
105 if (mFusePid == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700106 PLOG(ERROR) << getId() << " failed to fork";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800107 return -errno;
108 }
109
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600110 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800111 while (before == GetDevice(mFuseFull)) {
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700112 LOG(DEBUG) << "Waiting for FUSE to spin up...";
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700113 usleep(50000); // 50ms
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600114
115 nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
116 if (nanoseconds_to_milliseconds(now - start) > 5000) {
117 LOG(WARNING) << "Timed out while waiting for FUSE to spin up";
118 return -ETIMEDOUT;
119 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700120 }
Daniel Rosenberg1d79d102017-07-11 17:59:55 -0700121 /* sdcardfs will have exited already. FUSE will still be running */
Daniel Rosenberg8b9a5b32018-03-12 15:47:23 -0700122 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
123 mFusePid = 0;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700124
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800125 return OK;
126}
127
128status_t EmulatedVolume::doUnmount() {
Narayan Kamathea243a32016-01-21 12:26:05 +0000129 // Unmount the storage before we kill the FUSE process. If we kill
130 // the FUSE process first, most file system operations will return
131 // ENOTCONN until the unmount completes. This is an exotic and unusual
132 // error code and might cause broken behaviour in applications.
133 KillProcessesUsingPath(getPath());
134 ForceUnmount(mFuseDefault);
135 ForceUnmount(mFuseRead);
136 ForceUnmount(mFuseWrite);
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800137 ForceUnmount(mFuseFull);
Narayan Kamathea243a32016-01-21 12:26:05 +0000138
Jeff Sharkey66270a22015-06-24 11:49:24 -0700139 rmdir(mFuseDefault.c_str());
140 rmdir(mFuseRead.c_str());
141 rmdir(mFuseWrite.c_str());
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800142 rmdir(mFuseFull.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700143
144 mFuseDefault.clear();
145 mFuseRead.clear();
146 mFuseWrite.clear();
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800147 mFuseFull.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800148
149 return OK;
150}
151
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800152} // namespace vold
153} // namespace android