blob: 99abdd5321c7d76d98ec6e8fec2cccf18c548982 [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"
Zim3623a212019-07-19 16:46:53 +010018
19#include "AppFuseUtil.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080020#include "Utils.h"
Sudheer Shanka40ab6742018-09-18 13:07:45 -070021#include "VolumeManager.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080022
Elliott Hughes7e128fb2015-12-04 15:50:53 -080023#include <android-base/logging.h>
Zim3623a212019-07-19 16:46:53 +010024#include <android-base/properties.h>
Sudheer Shanka53947a32018-08-01 10:24:13 -070025#include <android-base/stringprintf.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080026#include <cutils/fs.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080027#include <private/android_filesystem_config.h>
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -060028#include <utils/Timers.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080029
30#include <fcntl.h>
31#include <stdlib.h>
32#include <sys/mount.h>
33#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070034#include <sys/sysmacros.h>
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070035#include <sys/types.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080036#include <sys/wait.h>
37
Dan Albertae9e8902015-03-16 10:35:17 -070038using android::base::StringPrintf;
39
Jeff Sharkeydeb24052015-03-02 21:01:40 -080040namespace android {
41namespace vold {
42
Martijn Coenenadcc8452019-12-09 14:18:01 +010043static const char* kSdcardFsPath = "/system/bin/sdcard";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080044
Zima438b242019-09-25 14:37:38 +010045EmulatedVolume::EmulatedVolume(const std::string& rawPath, int userId)
Martijn Coenenadcc8452019-12-09 14:18:01 +010046 : VolumeBase(Type::kEmulated) {
Zima438b242019-09-25 14:37:38 +010047 setId(StringPrintf("emulated;%u", userId));
Jeff Sharkeydeb24052015-03-02 21:01:40 -080048 mRawPath = rawPath;
Jeff Sharkey66270a22015-06-24 11:49:24 -070049 mLabel = "emulated";
Jeff Sharkey3161fb32015-04-12 16:03:33 -070050}
51
Zima438b242019-09-25 14:37:38 +010052EmulatedVolume::EmulatedVolume(const std::string& rawPath, dev_t device, const std::string& fsUuid,
53 int userId)
Martijn Coenenadcc8452019-12-09 14:18:01 +010054 : VolumeBase(Type::kEmulated) {
Zima438b242019-09-25 14:37:38 +010055 setId(StringPrintf("emulated:%u,%u;%u", major(device), minor(device), userId));
Jeff Sharkey3161fb32015-04-12 16:03:33 -070056 mRawPath = rawPath;
Jeff Sharkey66270a22015-06-24 11:49:24 -070057 mLabel = fsUuid;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080058}
59
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070060EmulatedVolume::~EmulatedVolume() {}
Jeff Sharkeydeb24052015-03-02 21:01:40 -080061
Martijn Coenen6f5802e2019-11-28 11:53:53 +010062std::string EmulatedVolume::getLabel() {
Jeff Sharkey81f55c62015-07-07 14:37:03 -070063 // We could have migrated storage to an adopted private volume, so always
64 // call primary storage "emulated" to avoid media rescans.
Jeff Sharkey81f55c62015-07-07 14:37:03 -070065 if (getMountFlags() & MountFlags::kPrimary) {
Martijn Coenen6f5802e2019-11-28 11:53:53 +010066 return "emulated";
67 } else {
68 return mLabel;
Jeff Sharkey81f55c62015-07-07 14:37:03 -070069 }
Martijn Coenen6f5802e2019-11-28 11:53:53 +010070}
71
72status_t EmulatedVolume::doMount() {
73 std::string label = getLabel();
74 bool isVisible = getMountFlags() & MountFlags::kVisible;
Jeff Sharkey81f55c62015-07-07 14:37:03 -070075
Martijn Coenenadcc8452019-12-09 14:18:01 +010076 mSdcardFsDefault = StringPrintf("/mnt/runtime/default/%s", label.c_str());
77 mSdcardFsRead = StringPrintf("/mnt/runtime/read/%s", label.c_str());
78 mSdcardFsWrite = StringPrintf("/mnt/runtime/write/%s", label.c_str());
79 mSdcardFsFull = StringPrintf("/mnt/runtime/full/%s", label.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -070080
81 setInternalPath(mRawPath);
Jeff Sharkey81f55c62015-07-07 14:37:03 -070082 setPath(StringPrintf("/storage/%s", label.c_str()));
Jeff Sharkey66270a22015-06-24 11:49:24 -070083
Martijn Coenenadcc8452019-12-09 14:18:01 +010084 if (fs_prepare_dir(mSdcardFsDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
85 fs_prepare_dir(mSdcardFsRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
86 fs_prepare_dir(mSdcardFsWrite.c_str(), 0700, AID_ROOT, AID_ROOT) ||
87 fs_prepare_dir(mSdcardFsFull.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -070088 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080089 return -errno;
90 }
91
Martijn Coenenadcc8452019-12-09 14:18:01 +010092 dev_t before = GetDevice(mSdcardFsFull);
Jeff Sharkey36801cc2015-03-13 16:09:20 -070093
shafik5cf32b52019-09-25 13:56:01 +010094 bool isFuse = base::GetBoolProperty(kPropFuseSnapshot, false);
Zim3623a212019-07-19 16:46:53 +010095
Martijn Coenen6f5802e2019-11-28 11:53:53 +010096 // Mount sdcardfs regardless of FUSE, since we need it to bind-mount on top of the
97 // FUSE volume for various reasons.
98 if (getMountUserId() == 0) {
99 LOG(INFO) << "Executing sdcardfs";
100 int sdcardFsPid;
101 if (!(sdcardFsPid = fork())) {
102 // clang-format off
103 if (execl(kSdcardFsPath, kSdcardFsPath,
104 "-u", "1023", // AID_MEDIA_RW
105 "-g", "1023", // AID_MEDIA_RW
106 "-m",
107 "-w",
108 "-G",
109 "-i",
110 "-o",
111 mRawPath.c_str(),
112 label.c_str(),
113 NULL)) {
114 // clang-format on
115 PLOG(ERROR) << "Failed to exec";
116 }
117
118 LOG(ERROR) << "sdcardfs exiting";
119 _exit(1);
120 }
121
122 if (sdcardFsPid == -1) {
123 PLOG(ERROR) << getId() << " failed to fork";
124 return -errno;
125 }
126
127 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
128 while (before == GetDevice(mSdcardFsFull)) {
129 LOG(DEBUG) << "Waiting for sdcardfs to spin up...";
130 usleep(50000); // 50ms
131
132 nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
133 if (nanoseconds_to_milliseconds(now - start) > 5000) {
134 LOG(WARNING) << "Timed out while waiting for sdcardfs to spin up";
135 return -ETIMEDOUT;
136 }
137 }
138 /* sdcardfs will have exited already. The filesystem will still be running */
139 TEMP_FAILURE_RETRY(waitpid(sdcardFsPid, nullptr, 0));
140 sdcardFsPid = 0;
141 }
142 if (isFuse && isVisible) {
Zim3623a212019-07-19 16:46:53 +0100143 LOG(INFO) << "Mounting emulated fuse volume";
Nandana Dutta914cc72019-08-29 15:22:42 +0100144 android::base::unique_fd fd;
Zim981222f2019-09-09 10:24:44 +0100145 int user_id = getMountUserId();
Zima438b242019-09-25 14:37:38 +0100146 int result = MountUserFuse(user_id, getInternalPath(), label, &fd);
Zim981222f2019-09-09 10:24:44 +0100147
Zim3623a212019-07-19 16:46:53 +0100148 if (result != 0) {
149 PLOG(ERROR) << "Failed to mount emulated fuse volume";
150 return -result;
151 }
Zim5048b4b2019-11-19 09:16:03 +0000152
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100153 mFuseMounted = true;
Zim5048b4b2019-11-19 09:16:03 +0000154 auto callback = getMountCallback();
155 if (callback) {
156 bool is_ready = false;
157 callback->onVolumeChecking(std::move(fd), getPath(), getInternalPath(), &is_ready);
158 if (!is_ready) {
159 return -EIO;
160 }
161 }
Zim3623a212019-07-19 16:46:53 +0100162 }
163
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800164 return OK;
165}
166
167status_t EmulatedVolume::doUnmount() {
Narayan Kamathea243a32016-01-21 12:26:05 +0000168 // Unmount the storage before we kill the FUSE process. If we kill
169 // the FUSE process first, most file system operations will return
170 // ENOTCONN until the unmount completes. This is an exotic and unusual
171 // error code and might cause broken behaviour in applications.
172 KillProcessesUsingPath(getPath());
Zim3623a212019-07-19 16:46:53 +0100173
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100174 if (mFuseMounted) {
175 std::string label = getLabel();
Zima438b242019-09-25 14:37:38 +0100176
177 std::string fuse_path(StringPrintf("/mnt/user/%d/%s", getMountUserId(), label.c_str()));
178 std::string pass_through_path(
179 StringPrintf("/mnt/pass_through/%d/%s", getMountUserId(), label.c_str()));
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100180 if (UnmountUserFuse(getMountUserId(), getInternalPath(), label) != OK) {
Zima438b242019-09-25 14:37:38 +0100181 PLOG(INFO) << "UnmountUserFuse failed on emulated fuse volume";
182 return -errno;
Zim3623a212019-07-19 16:46:53 +0100183 }
184
Zima438b242019-09-25 14:37:38 +0100185 rmdir(fuse_path.c_str());
186 rmdir(pass_through_path.c_str());
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100187
188 mFuseMounted = false;
189 }
190 if (getMountUserId() != 0) {
Zim2d45d9b2019-11-14 16:19:05 +0000191 // For sdcardfs, only unmount for user 0, since user 0 will always be running
192 // and the paths don't change for different users.
193 return OK;
Zim3623a212019-07-19 16:46:53 +0100194 }
195
Martijn Coenenadcc8452019-12-09 14:18:01 +0100196 ForceUnmount(mSdcardFsDefault);
197 ForceUnmount(mSdcardFsRead);
198 ForceUnmount(mSdcardFsWrite);
199 ForceUnmount(mSdcardFsFull);
Narayan Kamathea243a32016-01-21 12:26:05 +0000200
Martijn Coenenadcc8452019-12-09 14:18:01 +0100201 rmdir(mSdcardFsDefault.c_str());
202 rmdir(mSdcardFsRead.c_str());
203 rmdir(mSdcardFsWrite.c_str());
204 rmdir(mSdcardFsFull.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700205
Martijn Coenenadcc8452019-12-09 14:18:01 +0100206 mSdcardFsDefault.clear();
207 mSdcardFsRead.clear();
208 mSdcardFsWrite.clear();
209 mSdcardFsFull.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800210
211 return OK;
212}
213
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800214} // namespace vold
215} // namespace android