blob: b42bd4903e776da938aa4f9da23f0fbb12f36cae [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";
Martijn Coenenfd7362d2019-12-11 14:57:59 +010050 mFuseMounted = false;
Jeff Sharkey3161fb32015-04-12 16:03:33 -070051}
52
Zima438b242019-09-25 14:37:38 +010053EmulatedVolume::EmulatedVolume(const std::string& rawPath, dev_t device, const std::string& fsUuid,
54 int userId)
Martijn Coenenadcc8452019-12-09 14:18:01 +010055 : VolumeBase(Type::kEmulated) {
Zima438b242019-09-25 14:37:38 +010056 setId(StringPrintf("emulated:%u,%u;%u", major(device), minor(device), userId));
Jeff Sharkey3161fb32015-04-12 16:03:33 -070057 mRawPath = rawPath;
Jeff Sharkey66270a22015-06-24 11:49:24 -070058 mLabel = fsUuid;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080059}
60
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070061EmulatedVolume::~EmulatedVolume() {}
Jeff Sharkeydeb24052015-03-02 21:01:40 -080062
Martijn Coenen6f5802e2019-11-28 11:53:53 +010063std::string EmulatedVolume::getLabel() {
Jeff Sharkey81f55c62015-07-07 14:37:03 -070064 // We could have migrated storage to an adopted private volume, so always
65 // call primary storage "emulated" to avoid media rescans.
Jeff Sharkey81f55c62015-07-07 14:37:03 -070066 if (getMountFlags() & MountFlags::kPrimary) {
Martijn Coenen6f5802e2019-11-28 11:53:53 +010067 return "emulated";
68 } else {
69 return mLabel;
Jeff Sharkey81f55c62015-07-07 14:37:03 -070070 }
Martijn Coenen6f5802e2019-11-28 11:53:53 +010071}
72
Martijn Coenen57002612019-11-28 11:56:13 +010073static status_t mountFuseBindMounts(int userId, const std::string& label) {
74 // TODO(b/134706060) we don't actually want to mount the "write" view by
75 // default, since it gives write access to all OBB dirs.
76 std::string androidSource(
77 StringPrintf("/mnt/runtime/write/%s/%d/Android", label.c_str(), userId));
78 std::string androidTarget(
79 StringPrintf("/mnt/user/%d/%s/%d/Android", userId, label.c_str(), userId));
80
81 if (access(androidSource.c_str(), F_OK) != 0) {
82 // Android path may not exist yet if users has just been created; create it on
83 // the lower fs.
84 if (fs_prepare_dir(androidSource.c_str(), 0771, AID_ROOT, AID_ROOT) != 0) {
85 PLOG(ERROR) << "Failed to create " << androidSource;
86 return -errno;
87 }
88 }
89 LOG(INFO) << "Bind mounting " << androidSource << " on " << androidTarget;
90 auto status = BindMount(androidSource, androidTarget);
91 if (status != OK) {
92 return status;
93 }
94 LOG(INFO) << "Bind mounted " << androidSource << " on " << androidTarget;
95
96 return OK;
97}
98
99static status_t unmountFuseBindMounts(int userId, const std::string& label) {
100 std::string androidTarget(
101 StringPrintf("/mnt/user/%d/%s/%d/Android", userId, label.c_str(), userId));
102
103 LOG(INFO) << "Unmounting " << androidTarget;
104 auto status = UnmountTree(androidTarget);
105 if (status != OK) {
106 return status;
107 }
108 LOG(INFO) << "Unmounted " << androidTarget;
109
110 return OK;
111}
112
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100113status_t EmulatedVolume::doMount() {
114 std::string label = getLabel();
115 bool isVisible = getMountFlags() & MountFlags::kVisible;
Jeff Sharkey81f55c62015-07-07 14:37:03 -0700116
Martijn Coenenadcc8452019-12-09 14:18:01 +0100117 mSdcardFsDefault = StringPrintf("/mnt/runtime/default/%s", label.c_str());
118 mSdcardFsRead = StringPrintf("/mnt/runtime/read/%s", label.c_str());
119 mSdcardFsWrite = StringPrintf("/mnt/runtime/write/%s", label.c_str());
120 mSdcardFsFull = StringPrintf("/mnt/runtime/full/%s", label.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700121
122 setInternalPath(mRawPath);
Jeff Sharkey81f55c62015-07-07 14:37:03 -0700123 setPath(StringPrintf("/storage/%s", label.c_str()));
Jeff Sharkey66270a22015-06-24 11:49:24 -0700124
Martijn Coenenadcc8452019-12-09 14:18:01 +0100125 if (fs_prepare_dir(mSdcardFsDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
126 fs_prepare_dir(mSdcardFsRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
127 fs_prepare_dir(mSdcardFsWrite.c_str(), 0700, AID_ROOT, AID_ROOT) ||
128 fs_prepare_dir(mSdcardFsFull.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700129 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800130 return -errno;
131 }
132
Martijn Coenenadcc8452019-12-09 14:18:01 +0100133 dev_t before = GetDevice(mSdcardFsFull);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700134
shafik5cf32b52019-09-25 13:56:01 +0100135 bool isFuse = base::GetBoolProperty(kPropFuseSnapshot, false);
Zim3623a212019-07-19 16:46:53 +0100136
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100137 // Mount sdcardfs regardless of FUSE, since we need it to bind-mount on top of the
138 // FUSE volume for various reasons.
139 if (getMountUserId() == 0) {
140 LOG(INFO) << "Executing sdcardfs";
141 int sdcardFsPid;
142 if (!(sdcardFsPid = fork())) {
143 // clang-format off
144 if (execl(kSdcardFsPath, kSdcardFsPath,
145 "-u", "1023", // AID_MEDIA_RW
146 "-g", "1023", // AID_MEDIA_RW
147 "-m",
148 "-w",
149 "-G",
150 "-i",
151 "-o",
152 mRawPath.c_str(),
153 label.c_str(),
154 NULL)) {
155 // clang-format on
156 PLOG(ERROR) << "Failed to exec";
157 }
158
159 LOG(ERROR) << "sdcardfs exiting";
160 _exit(1);
161 }
162
163 if (sdcardFsPid == -1) {
164 PLOG(ERROR) << getId() << " failed to fork";
165 return -errno;
166 }
167
168 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
169 while (before == GetDevice(mSdcardFsFull)) {
170 LOG(DEBUG) << "Waiting for sdcardfs to spin up...";
171 usleep(50000); // 50ms
172
173 nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
174 if (nanoseconds_to_milliseconds(now - start) > 5000) {
175 LOG(WARNING) << "Timed out while waiting for sdcardfs to spin up";
176 return -ETIMEDOUT;
177 }
178 }
179 /* sdcardfs will have exited already. The filesystem will still be running */
180 TEMP_FAILURE_RETRY(waitpid(sdcardFsPid, nullptr, 0));
181 sdcardFsPid = 0;
182 }
183 if (isFuse && isVisible) {
Zim3623a212019-07-19 16:46:53 +0100184 LOG(INFO) << "Mounting emulated fuse volume";
Nandana Dutta914cc72019-08-29 15:22:42 +0100185 android::base::unique_fd fd;
Zim981222f2019-09-09 10:24:44 +0100186 int user_id = getMountUserId();
Zima438b242019-09-25 14:37:38 +0100187 int result = MountUserFuse(user_id, getInternalPath(), label, &fd);
Zim981222f2019-09-09 10:24:44 +0100188
Zim3623a212019-07-19 16:46:53 +0100189 if (result != 0) {
190 PLOG(ERROR) << "Failed to mount emulated fuse volume";
191 return -result;
192 }
Zim5048b4b2019-11-19 09:16:03 +0000193
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100194 mFuseMounted = true;
Zim5048b4b2019-11-19 09:16:03 +0000195 auto callback = getMountCallback();
196 if (callback) {
197 bool is_ready = false;
198 callback->onVolumeChecking(std::move(fd), getPath(), getInternalPath(), &is_ready);
199 if (!is_ready) {
200 return -EIO;
201 }
202 }
Martijn Coenen57002612019-11-28 11:56:13 +0100203
204 // Only do the bind-mounts when we know for sure the FUSE daemon can resolve the path.
205 return mountFuseBindMounts(user_id, label);
Zim3623a212019-07-19 16:46:53 +0100206 }
207
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800208 return OK;
209}
210
211status_t EmulatedVolume::doUnmount() {
Martijn Coenen8f1e7f22019-11-29 15:38:55 +0100212 int userId = getMountUserId();
213
214 // Kill all processes using the filesystem before we unmount it. If we
215 // unmount the filesystem first, most file system operations will return
Narayan Kamathea243a32016-01-21 12:26:05 +0000216 // ENOTCONN until the unmount completes. This is an exotic and unusual
217 // error code and might cause broken behaviour in applications.
Martijn Coenen8f1e7f22019-11-29 15:38:55 +0100218 if (mFuseMounted) {
219 // For FUSE specifically, we have an emulated volume per user, so only kill
220 // processes using files from this particular user.
221 std::string user_path(StringPrintf("%s/%d", getPath().c_str(), getMountUserId()));
222 LOG(INFO) << "Killing all processes referencing " << user_path;
223 KillProcessesUsingPath(user_path);
224 } else {
225 KillProcessesUsingPath(getPath());
226 }
Zim3623a212019-07-19 16:46:53 +0100227
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100228 if (mFuseMounted) {
229 std::string label = getLabel();
Martijn Coenen57002612019-11-28 11:56:13 +0100230 // Ignoring unmount return status because we do want to try to unmount
231 // the rest cleanly.
Zima438b242019-09-25 14:37:38 +0100232
Martijn Coenen57002612019-11-28 11:56:13 +0100233 unmountFuseBindMounts(userId, label);
234 if (UnmountUserFuse(userId, getInternalPath(), label) != OK) {
Zima438b242019-09-25 14:37:38 +0100235 PLOG(INFO) << "UnmountUserFuse failed on emulated fuse volume";
236 return -errno;
Zim3623a212019-07-19 16:46:53 +0100237 }
238
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100239 mFuseMounted = false;
240 }
241 if (getMountUserId() != 0) {
Zim2d45d9b2019-11-14 16:19:05 +0000242 // For sdcardfs, only unmount for user 0, since user 0 will always be running
243 // and the paths don't change for different users.
244 return OK;
Zim3623a212019-07-19 16:46:53 +0100245 }
246
Martijn Coenenadcc8452019-12-09 14:18:01 +0100247 ForceUnmount(mSdcardFsDefault);
248 ForceUnmount(mSdcardFsRead);
249 ForceUnmount(mSdcardFsWrite);
250 ForceUnmount(mSdcardFsFull);
Narayan Kamathea243a32016-01-21 12:26:05 +0000251
Martijn Coenenadcc8452019-12-09 14:18:01 +0100252 rmdir(mSdcardFsDefault.c_str());
253 rmdir(mSdcardFsRead.c_str());
254 rmdir(mSdcardFsWrite.c_str());
255 rmdir(mSdcardFsFull.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700256
Martijn Coenenadcc8452019-12-09 14:18:01 +0100257 mSdcardFsDefault.clear();
258 mSdcardFsRead.clear();
259 mSdcardFsWrite.clear();
260 mSdcardFsFull.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800261
262 return OK;
263}
264
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800265} // namespace vold
266} // namespace android