blob: 8f46191f7d9e9fb02f9788d81487fc86faec2ae6 [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());
Jeff Sharkey66270a22015-06-24 11:49:24 -070069
70 setInternalPath(mRawPath);
Jeff Sharkey81f55c62015-07-07 14:37:03 -070071 setPath(StringPrintf("/storage/%s", label.c_str()));
Sudheer Shanka53947a32018-08-01 10:24:13 -070072 setLabel(label);
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) ||
76 fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -070077 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080078 return -errno;
79 }
80
Jeff Sharkey66270a22015-06-24 11:49:24 -070081 dev_t before = GetDevice(mFuseWrite);
Jeff Sharkey36801cc2015-03-13 16:09:20 -070082
Jeff Sharkeydeb24052015-03-02 21:01:40 -080083 if (!(mFusePid = fork())) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070084 // clang-format off
Jeff Sharkey36801cc2015-03-13 16:09:20 -070085 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -080086 "-u", "1023", // AID_MEDIA_RW
87 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey66270a22015-06-24 11:49:24 -070088 "-m",
89 "-w",
Rom Lemarchand958c2162017-09-15 18:48:01 +000090 "-G",
Jeff Sharkeyd7e51762018-01-08 11:48:07 -070091 "-i",
Jeff Sharkeydeb24052015-03-02 21:01:40 -080092 mRawPath.c_str(),
Jeff Sharkey81f55c62015-07-07 14:37:03 -070093 label.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -070094 NULL)) {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070095 // clang-format on
Jeff Sharkey36801cc2015-03-13 16:09:20 -070096 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080097 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -070098
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -070099 LOG(ERROR) << "FUSE exiting";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800100 _exit(1);
101 }
102
103 if (mFusePid == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700104 PLOG(ERROR) << getId() << " failed to fork";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800105 return -errno;
106 }
107
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600108 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700109 while (before == GetDevice(mFuseWrite)) {
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700110 LOG(DEBUG) << "Waiting for FUSE to spin up...";
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700111 usleep(50000); // 50ms
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600112
113 nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
114 if (nanoseconds_to_milliseconds(now - start) > 5000) {
115 LOG(WARNING) << "Timed out while waiting for FUSE to spin up";
116 return -ETIMEDOUT;
117 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700118 }
Daniel Rosenberg1d79d102017-07-11 17:59:55 -0700119 /* sdcardfs will have exited already. FUSE will still be running */
Daniel Rosenberg8b9a5b32018-03-12 15:47:23 -0700120 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
121 mFusePid = 0;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700122
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800123 return OK;
124}
125
126status_t EmulatedVolume::doUnmount() {
Narayan Kamathea243a32016-01-21 12:26:05 +0000127 // Unmount the storage before we kill the FUSE process. If we kill
128 // the FUSE process first, most file system operations will return
129 // ENOTCONN until the unmount completes. This is an exotic and unusual
130 // error code and might cause broken behaviour in applications.
131 KillProcessesUsingPath(getPath());
132 ForceUnmount(mFuseDefault);
133 ForceUnmount(mFuseRead);
134 ForceUnmount(mFuseWrite);
135
Jeff Sharkey66270a22015-06-24 11:49:24 -0700136 rmdir(mFuseDefault.c_str());
137 rmdir(mFuseRead.c_str());
138 rmdir(mFuseWrite.c_str());
139
140 mFuseDefault.clear();
141 mFuseRead.clear();
142 mFuseWrite.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800143
144 return OK;
145}
146
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800147} // namespace vold
148} // namespace android