blob: f9339829b8632137e90626d445a40a04b405fdc0 [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"
19
Elliott Hughes7e128fb2015-12-04 15:50:53 -080020#include <android-base/stringprintf.h>
21#include <android-base/logging.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080022#include <cutils/fs.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080023#include <private/android_filesystem_config.h>
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -060024#include <utils/Timers.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080025
26#include <fcntl.h>
27#include <stdlib.h>
28#include <sys/mount.h>
29#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070030#include <sys/sysmacros.h>
Paul Crowley8915d622018-09-18 15:14:18 -070031#include <sys/types.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080032#include <sys/wait.h>
33
Dan Albertae9e8902015-03-16 10:35:17 -070034using android::base::StringPrintf;
35
Jeff Sharkeydeb24052015-03-02 21:01:40 -080036namespace android {
37namespace vold {
38
39static const char* kFusePath = "/system/bin/sdcard";
40
Paul Crowley8915d622018-09-18 15:14:18 -070041EmulatedVolume::EmulatedVolume(const std::string& rawPath)
42 : VolumeBase(Type::kEmulated), mFusePid(0) {
Jeff Sharkey3161fb32015-04-12 16:03:33 -070043 setId("emulated");
Jeff Sharkeydeb24052015-03-02 21:01:40 -080044 mRawPath = rawPath;
Jeff Sharkey66270a22015-06-24 11:49:24 -070045 mLabel = "emulated";
Jeff Sharkey3161fb32015-04-12 16:03:33 -070046}
47
Paul Crowley8915d622018-09-18 15:14:18 -070048EmulatedVolume::EmulatedVolume(const std::string& rawPath, dev_t device, const std::string& fsUuid)
49 : VolumeBase(Type::kEmulated), mFusePid(0) {
Jeff Sharkey3161fb32015-04-12 16:03:33 -070050 setId(StringPrintf("emulated:%u,%u", major(device), minor(device)));
Jeff Sharkey3161fb32015-04-12 16:03:33 -070051 mRawPath = rawPath;
Jeff Sharkey66270a22015-06-24 11:49:24 -070052 mLabel = fsUuid;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080053}
54
Paul Crowley8915d622018-09-18 15:14:18 -070055EmulatedVolume::~EmulatedVolume() {}
Jeff Sharkeydeb24052015-03-02 21:01:40 -080056
57status_t EmulatedVolume::doMount() {
Jeff Sharkey81f55c62015-07-07 14:37:03 -070058 // We could have migrated storage to an adopted private volume, so always
59 // call primary storage "emulated" to avoid media rescans.
60 std::string label = mLabel;
61 if (getMountFlags() & MountFlags::kPrimary) {
62 label = "emulated";
63 }
64
Jeff Sharkey1bd078f2015-08-06 11:40:00 -070065 mFuseDefault = StringPrintf("/mnt/runtime/default/%s", label.c_str());
66 mFuseRead = StringPrintf("/mnt/runtime/read/%s", label.c_str());
67 mFuseWrite = StringPrintf("/mnt/runtime/write/%s", label.c_str());
Sudheer Shankadd4bb172019-01-16 23:35:49 -080068 mFuseFull = StringPrintf("/mnt/runtime/full/%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()));
Jeff Sharkey66270a22015-06-24 11:49:24 -070072
73 if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
Paul Crowley8915d622018-09-18 15:14:18 -070074 fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
Sudheer Shankadd4bb172019-01-16 23:35:49 -080075 fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT) ||
76 fs_prepare_dir(mFuseFull.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
Sudheer Shankadd4bb172019-01-16 23:35:49 -080081 dev_t before = GetDevice(mFuseFull);
Jeff Sharkey36801cc2015-03-13 16:09:20 -070082
Jeff Sharkeydeb24052015-03-02 21:01:40 -080083 if (!(mFusePid = fork())) {
Paul Crowley8915d622018-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 Crowley8915d622018-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);
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800109 while (before == GetDevice(mFuseFull)) {
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700110 LOG(DEBUG) << "Waiting for FUSE to spin up...";
Paul Crowley8915d622018-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);
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800135 ForceUnmount(mFuseFull);
Narayan Kamathea243a32016-01-21 12:26:05 +0000136
Jeff Sharkey66270a22015-06-24 11:49:24 -0700137 rmdir(mFuseDefault.c_str());
138 rmdir(mFuseRead.c_str());
139 rmdir(mFuseWrite.c_str());
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800140 rmdir(mFuseFull.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700141
142 mFuseDefault.clear();
143 mFuseRead.clear();
144 mFuseWrite.clear();
Sudheer Shankadd4bb172019-01-16 23:35:49 -0800145 mFuseFull.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800146
147 return OK;
148}
149
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800150} // namespace vold
151} // namespace android