blob: b212c0ed361943c0608f13ab6914e5d79885cafc [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;
Zimb6488f32020-03-17 15:15:42 +000051 mAndroidMounted = false;
Martijn Coenen86f21a22020-01-06 09:48:14 +010052 mUseSdcardFs = IsFilesystemSupported("sdcardfs");
Ricky Wai07e64a42020-02-11 14:31:24 +000053 mAppDataIsolationEnabled = base::GetBoolProperty(kVoldAppDataIsolationEnabled, false);
Jeff Sharkey3161fb32015-04-12 16:03:33 -070054}
55
Zima438b242019-09-25 14:37:38 +010056EmulatedVolume::EmulatedVolume(const std::string& rawPath, dev_t device, const std::string& fsUuid,
57 int userId)
Martijn Coenenadcc8452019-12-09 14:18:01 +010058 : VolumeBase(Type::kEmulated) {
Zima438b242019-09-25 14:37:38 +010059 setId(StringPrintf("emulated:%u,%u;%u", major(device), minor(device), userId));
Jeff Sharkey3161fb32015-04-12 16:03:33 -070060 mRawPath = rawPath;
Jeff Sharkey66270a22015-06-24 11:49:24 -070061 mLabel = fsUuid;
Greg Kaiser5298ccc2019-12-12 05:41:46 -080062 mFuseMounted = false;
Zimb6488f32020-03-17 15:15:42 +000063 mAndroidMounted = false;
Martijn Coenen86f21a22020-01-06 09:48:14 +010064 mUseSdcardFs = IsFilesystemSupported("sdcardfs");
Ricky Wai07e64a42020-02-11 14:31:24 +000065 mAppDataIsolationEnabled = base::GetBoolProperty(kVoldAppDataIsolationEnabled, false);
Jeff Sharkeydeb24052015-03-02 21:01:40 -080066}
67
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070068EmulatedVolume::~EmulatedVolume() {}
Jeff Sharkeydeb24052015-03-02 21:01:40 -080069
Martijn Coenen6f5802e2019-11-28 11:53:53 +010070std::string EmulatedVolume::getLabel() {
Jeff Sharkey81f55c62015-07-07 14:37:03 -070071 // We could have migrated storage to an adopted private volume, so always
72 // call primary storage "emulated" to avoid media rescans.
Jeff Sharkey81f55c62015-07-07 14:37:03 -070073 if (getMountFlags() & MountFlags::kPrimary) {
Martijn Coenen6f5802e2019-11-28 11:53:53 +010074 return "emulated";
75 } else {
76 return mLabel;
Jeff Sharkey81f55c62015-07-07 14:37:03 -070077 }
Martijn Coenen6f5802e2019-11-28 11:53:53 +010078}
79
Martijn Coenen62a4b272020-01-31 15:23:09 +010080// Creates a bind mount from source to target
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +010081static status_t doFuseBindMount(const std::string& source, const std::string& target) {
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +010082 LOG(INFO) << "Bind mounting " << source << " on " << target;
83 auto status = BindMount(source, target);
84 if (status != OK) {
85 return status;
86 }
87 LOG(INFO) << "Bind mounted " << source << " on " << target;
88 return OK;
89}
90
Martijn Coenen86f21a22020-01-06 09:48:14 +010091status_t EmulatedVolume::mountFuseBindMounts() {
Zimb6488f32020-03-17 15:15:42 +000092 CHECK(!mAndroidMounted);
93
Martijn Coenen86f21a22020-01-06 09:48:14 +010094 std::string androidSource;
95 std::string label = getLabel();
96 int userId = getMountUserId();
97
98 if (mUseSdcardFs) {
99 androidSource = StringPrintf("/mnt/runtime/default/%s/%d/Android", label.c_str(), userId);
100 } else {
101 androidSource = StringPrintf("/%s/%d/Android", mRawPath.c_str(), userId);
102 }
Martijn Coenen57002612019-11-28 11:56:13 +0100103
Ricky Wai07e64a42020-02-11 14:31:24 +0000104 status_t status = OK;
105 // When app data isolation is enabled, obb/ will be mounted per app, otherwise we should
106 // bind mount the whole Android/ to speed up reading.
107 if (!mAppDataIsolationEnabled) {
108 std::string androidTarget(
109 StringPrintf("/mnt/user/%d/%s/%d/Android", userId, label.c_str(), userId));
110 status = doFuseBindMount(androidSource, androidTarget);
111 }
112
Martijn Coenen57002612019-11-28 11:56:13 +0100113 if (status != OK) {
114 return status;
115 }
Zimb6488f32020-03-17 15:15:42 +0000116 mAndroidMounted = true;
117
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +0100118 // Installers get the same view as all other apps, with the sole exception that the
119 // OBB dirs (Android/obb) are writable to them. On sdcardfs devices, this requires
120 // a special bind mount, since app-private and OBB dirs share the same GID, but we
121 // only want to give access to the latter.
122 if (!mUseSdcardFs) {
123 return OK;
124 }
125 std::string installerSource(
126 StringPrintf("/mnt/runtime/write/%s/%d/Android/obb", label.c_str(), userId));
127 std::string installerTarget(
128 StringPrintf("/mnt/installer/%d/%s/%d/Android/obb", userId, label.c_str(), userId));
129
130 status = doFuseBindMount(installerSource, installerTarget);
131 if (status != OK) {
132 return status;
133 }
Martijn Coenen57002612019-11-28 11:56:13 +0100134 return OK;
135}
136
Martijn Coenen86f21a22020-01-06 09:48:14 +0100137status_t EmulatedVolume::unmountFuseBindMounts() {
Zimb6488f32020-03-17 15:15:42 +0000138 CHECK(mAndroidMounted);
139
Martijn Coenen86f21a22020-01-06 09:48:14 +0100140 std::string label = getLabel();
141 int userId = getMountUserId();
142
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +0100143 if (mUseSdcardFs) {
144 std::string installerTarget(
145 StringPrintf("/mnt/installer/%d/%s/%d/Android/obb", userId, label.c_str(), userId));
146 LOG(INFO) << "Unmounting " << installerTarget;
147 auto status = UnmountTree(installerTarget);
148 if (status != OK) {
149 LOG(ERROR) << "Failed to unmount " << installerTarget;
150 // Intentional continue to try to unmount the other bind mount
151 }
152 }
Ricky Wai07e64a42020-02-11 14:31:24 +0000153 // When app data isolation is enabled, kill all apps that obb/ is mounted, otherwise we should
154 // umount the whole Android/ dir.
155 if (mAppDataIsolationEnabled) {
156 std::string appObbDir(StringPrintf("%s/%d/Android/obb", getPath().c_str(), userId));
157 KillProcessesWithMountPrefix(appObbDir);
158 } else {
159 std::string androidTarget(
160 StringPrintf("/mnt/user/%d/%s/%d/Android", userId, label.c_str(), userId));
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +0100161
Ricky Wai07e64a42020-02-11 14:31:24 +0000162 LOG(INFO) << "Unmounting " << androidTarget;
163 auto status = UnmountTree(androidTarget);
164 if (status != OK) {
165 return status;
166 }
167 LOG(INFO) << "Unmounted " << androidTarget;
Martijn Coenen57002612019-11-28 11:56:13 +0100168 }
Martijn Coenen57002612019-11-28 11:56:13 +0100169 return OK;
170}
171
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100172status_t EmulatedVolume::doMount() {
173 std::string label = getLabel();
174 bool isVisible = getMountFlags() & MountFlags::kVisible;
Jeff Sharkey81f55c62015-07-07 14:37:03 -0700175
Martijn Coenenadcc8452019-12-09 14:18:01 +0100176 mSdcardFsDefault = StringPrintf("/mnt/runtime/default/%s", label.c_str());
177 mSdcardFsRead = StringPrintf("/mnt/runtime/read/%s", label.c_str());
178 mSdcardFsWrite = StringPrintf("/mnt/runtime/write/%s", label.c_str());
179 mSdcardFsFull = StringPrintf("/mnt/runtime/full/%s", label.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700180
181 setInternalPath(mRawPath);
Jeff Sharkey81f55c62015-07-07 14:37:03 -0700182 setPath(StringPrintf("/storage/%s", label.c_str()));
Jeff Sharkey66270a22015-06-24 11:49:24 -0700183
Martijn Coenenadcc8452019-12-09 14:18:01 +0100184 if (fs_prepare_dir(mSdcardFsDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
185 fs_prepare_dir(mSdcardFsRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
186 fs_prepare_dir(mSdcardFsWrite.c_str(), 0700, AID_ROOT, AID_ROOT) ||
187 fs_prepare_dir(mSdcardFsFull.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700188 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800189 return -errno;
190 }
191
Martijn Coenenadcc8452019-12-09 14:18:01 +0100192 dev_t before = GetDevice(mSdcardFsFull);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700193
Abhijeet Kaur01fa0e02019-12-13 10:26:32 +0000194 bool isFuse = base::GetBoolProperty(kPropFuse, false);
Zim3623a212019-07-19 16:46:53 +0100195
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100196 // Mount sdcardfs regardless of FUSE, since we need it to bind-mount on top of the
197 // FUSE volume for various reasons.
Martijn Coenen86f21a22020-01-06 09:48:14 +0100198 if (mUseSdcardFs && getMountUserId() == 0) {
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100199 LOG(INFO) << "Executing sdcardfs";
200 int sdcardFsPid;
201 if (!(sdcardFsPid = fork())) {
202 // clang-format off
203 if (execl(kSdcardFsPath, kSdcardFsPath,
204 "-u", "1023", // AID_MEDIA_RW
205 "-g", "1023", // AID_MEDIA_RW
206 "-m",
207 "-w",
208 "-G",
209 "-i",
210 "-o",
211 mRawPath.c_str(),
212 label.c_str(),
213 NULL)) {
214 // clang-format on
215 PLOG(ERROR) << "Failed to exec";
216 }
217
218 LOG(ERROR) << "sdcardfs exiting";
219 _exit(1);
220 }
221
222 if (sdcardFsPid == -1) {
223 PLOG(ERROR) << getId() << " failed to fork";
224 return -errno;
225 }
226
227 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
228 while (before == GetDevice(mSdcardFsFull)) {
229 LOG(DEBUG) << "Waiting for sdcardfs to spin up...";
230 usleep(50000); // 50ms
231
232 nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
233 if (nanoseconds_to_milliseconds(now - start) > 5000) {
234 LOG(WARNING) << "Timed out while waiting for sdcardfs to spin up";
235 return -ETIMEDOUT;
236 }
237 }
238 /* sdcardfs will have exited already. The filesystem will still be running */
239 TEMP_FAILURE_RETRY(waitpid(sdcardFsPid, nullptr, 0));
240 sdcardFsPid = 0;
241 }
242 if (isFuse && isVisible) {
Zim3623a212019-07-19 16:46:53 +0100243 LOG(INFO) << "Mounting emulated fuse volume";
Nandana Dutta914cc72019-08-29 15:22:42 +0100244 android::base::unique_fd fd;
Zim981222f2019-09-09 10:24:44 +0100245 int user_id = getMountUserId();
Martijn Coenen62a4b272020-01-31 15:23:09 +0100246 auto volumeRoot = getRootPath();
Zim981222f2019-09-09 10:24:44 +0100247
Martijn Coenen62a4b272020-01-31 15:23:09 +0100248 // Make sure Android/ dirs exist for bind mounting
249 status_t res = PrepareAndroidDirs(volumeRoot);
250 if (res != OK) {
251 LOG(ERROR) << "Failed to prepare Android/ directories";
252 return res;
253 }
254
255 res = MountUserFuse(user_id, getInternalPath(), label, &fd);
256 if (res != 0) {
Zim3623a212019-07-19 16:46:53 +0100257 PLOG(ERROR) << "Failed to mount emulated fuse volume";
Martijn Coenen62a4b272020-01-31 15:23:09 +0100258 return res;
Zim3623a212019-07-19 16:46:53 +0100259 }
Zim5048b4b2019-11-19 09:16:03 +0000260
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100261 mFuseMounted = true;
Zim5048b4b2019-11-19 09:16:03 +0000262 auto callback = getMountCallback();
263 if (callback) {
264 bool is_ready = false;
265 callback->onVolumeChecking(std::move(fd), getPath(), getInternalPath(), &is_ready);
266 if (!is_ready) {
Zimdf073f52020-01-15 15:00:07 +0000267 fd.reset();
268 doUnmount();
Zim5048b4b2019-11-19 09:16:03 +0000269 return -EIO;
270 }
271 }
Martijn Coenen57002612019-11-28 11:56:13 +0100272
273 // Only do the bind-mounts when we know for sure the FUSE daemon can resolve the path.
Martijn Coenen62a4b272020-01-31 15:23:09 +0100274 res = mountFuseBindMounts();
Zimdf073f52020-01-15 15:00:07 +0000275 if (res != OK) {
276 fd.reset();
277 doUnmount();
278 }
279 return res;
Zim3623a212019-07-19 16:46:53 +0100280 }
281
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800282 return OK;
283}
284
285status_t EmulatedVolume::doUnmount() {
Martijn Coenen8f1e7f22019-11-29 15:38:55 +0100286 int userId = getMountUserId();
287
288 // Kill all processes using the filesystem before we unmount it. If we
289 // unmount the filesystem first, most file system operations will return
Narayan Kamathea243a32016-01-21 12:26:05 +0000290 // ENOTCONN until the unmount completes. This is an exotic and unusual
291 // error code and might cause broken behaviour in applications.
Martijn Coenen8f1e7f22019-11-29 15:38:55 +0100292 if (mFuseMounted) {
293 // For FUSE specifically, we have an emulated volume per user, so only kill
294 // processes using files from this particular user.
295 std::string user_path(StringPrintf("%s/%d", getPath().c_str(), getMountUserId()));
296 LOG(INFO) << "Killing all processes referencing " << user_path;
297 KillProcessesUsingPath(user_path);
298 } else {
299 KillProcessesUsingPath(getPath());
300 }
Zim3623a212019-07-19 16:46:53 +0100301
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100302 if (mFuseMounted) {
303 std::string label = getLabel();
Ricky Wai07e64a42020-02-11 14:31:24 +0000304
Martijn Coenen57002612019-11-28 11:56:13 +0100305 // Ignoring unmount return status because we do want to try to unmount
306 // the rest cleanly.
Zimb6488f32020-03-17 15:15:42 +0000307 if (mAndroidMounted) {
308 unmountFuseBindMounts();
309 mAndroidMounted = false;
310 }
Martijn Coenen57002612019-11-28 11:56:13 +0100311 if (UnmountUserFuse(userId, getInternalPath(), label) != OK) {
Zima438b242019-09-25 14:37:38 +0100312 PLOG(INFO) << "UnmountUserFuse failed on emulated fuse volume";
313 return -errno;
Zim3623a212019-07-19 16:46:53 +0100314 }
315
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100316 mFuseMounted = false;
317 }
Martijn Coenen86f21a22020-01-06 09:48:14 +0100318 if (getMountUserId() != 0 || !mUseSdcardFs) {
Zim2d45d9b2019-11-14 16:19:05 +0000319 // For sdcardfs, only unmount for user 0, since user 0 will always be running
320 // and the paths don't change for different users.
321 return OK;
Zim3623a212019-07-19 16:46:53 +0100322 }
323
Martijn Coenenadcc8452019-12-09 14:18:01 +0100324 ForceUnmount(mSdcardFsDefault);
325 ForceUnmount(mSdcardFsRead);
326 ForceUnmount(mSdcardFsWrite);
327 ForceUnmount(mSdcardFsFull);
Narayan Kamathea243a32016-01-21 12:26:05 +0000328
Martijn Coenenadcc8452019-12-09 14:18:01 +0100329 rmdir(mSdcardFsDefault.c_str());
330 rmdir(mSdcardFsRead.c_str());
331 rmdir(mSdcardFsWrite.c_str());
332 rmdir(mSdcardFsFull.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700333
Martijn Coenenadcc8452019-12-09 14:18:01 +0100334 mSdcardFsDefault.clear();
335 mSdcardFsRead.clear();
336 mSdcardFsWrite.clear();
337 mSdcardFsFull.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800338
339 return OK;
340}
341
Martijn Coenen62a4b272020-01-31 15:23:09 +0100342std::string EmulatedVolume::getRootPath() const {
343 int user_id = getMountUserId();
344 std::string volumeRoot = StringPrintf("%s/%d", getInternalPath().c_str(), user_id);
345
346 return volumeRoot;
347}
348
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800349} // namespace vold
350} // namespace android