blob: 1391685ae8ca603caabebb53f6d88a9d121577b7 [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;
Martijn Coenen86f21a22020-01-06 09:48:14 +010051 mUseSdcardFs = IsFilesystemSupported("sdcardfs");
Ricky Wai07e64a42020-02-11 14:31:24 +000052 mAppDataIsolationEnabled = base::GetBoolProperty(kVoldAppDataIsolationEnabled, false);
Jeff Sharkey3161fb32015-04-12 16:03:33 -070053}
54
Zima438b242019-09-25 14:37:38 +010055EmulatedVolume::EmulatedVolume(const std::string& rawPath, dev_t device, const std::string& fsUuid,
56 int userId)
Martijn Coenenadcc8452019-12-09 14:18:01 +010057 : VolumeBase(Type::kEmulated) {
Zima438b242019-09-25 14:37:38 +010058 setId(StringPrintf("emulated:%u,%u;%u", major(device), minor(device), userId));
Jeff Sharkey3161fb32015-04-12 16:03:33 -070059 mRawPath = rawPath;
Jeff Sharkey66270a22015-06-24 11:49:24 -070060 mLabel = fsUuid;
Greg Kaiser5298ccc2019-12-12 05:41:46 -080061 mFuseMounted = false;
Martijn Coenen86f21a22020-01-06 09:48:14 +010062 mUseSdcardFs = IsFilesystemSupported("sdcardfs");
Ricky Wai07e64a42020-02-11 14:31:24 +000063 mAppDataIsolationEnabled = base::GetBoolProperty(kVoldAppDataIsolationEnabled, false);
Jeff Sharkeydeb24052015-03-02 21:01:40 -080064}
65
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070066EmulatedVolume::~EmulatedVolume() {}
Jeff Sharkeydeb24052015-03-02 21:01:40 -080067
Martijn Coenen6f5802e2019-11-28 11:53:53 +010068std::string EmulatedVolume::getLabel() {
Jeff Sharkey81f55c62015-07-07 14:37:03 -070069 // We could have migrated storage to an adopted private volume, so always
70 // call primary storage "emulated" to avoid media rescans.
Jeff Sharkey81f55c62015-07-07 14:37:03 -070071 if (getMountFlags() & MountFlags::kPrimary) {
Martijn Coenen6f5802e2019-11-28 11:53:53 +010072 return "emulated";
73 } else {
74 return mLabel;
Jeff Sharkey81f55c62015-07-07 14:37:03 -070075 }
Martijn Coenen6f5802e2019-11-28 11:53:53 +010076}
77
Martijn Coenen62a4b272020-01-31 15:23:09 +010078// Creates a bind mount from source to target
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +010079static status_t doFuseBindMount(const std::string& source, const std::string& target) {
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +010080 LOG(INFO) << "Bind mounting " << source << " on " << target;
81 auto status = BindMount(source, target);
82 if (status != OK) {
83 return status;
84 }
85 LOG(INFO) << "Bind mounted " << source << " on " << target;
86 return OK;
87}
88
Martijn Coenen86f21a22020-01-06 09:48:14 +010089status_t EmulatedVolume::mountFuseBindMounts() {
90 std::string androidSource;
91 std::string label = getLabel();
92 int userId = getMountUserId();
93
94 if (mUseSdcardFs) {
95 androidSource = StringPrintf("/mnt/runtime/default/%s/%d/Android", label.c_str(), userId);
96 } else {
97 androidSource = StringPrintf("/%s/%d/Android", mRawPath.c_str(), userId);
98 }
Martijn Coenen57002612019-11-28 11:56:13 +010099
Ricky Wai07e64a42020-02-11 14:31:24 +0000100 status_t status = OK;
101 // When app data isolation is enabled, obb/ will be mounted per app, otherwise we should
102 // bind mount the whole Android/ to speed up reading.
103 if (!mAppDataIsolationEnabled) {
104 std::string androidTarget(
105 StringPrintf("/mnt/user/%d/%s/%d/Android", userId, label.c_str(), userId));
106 status = doFuseBindMount(androidSource, androidTarget);
107 }
108
Martijn Coenen57002612019-11-28 11:56:13 +0100109 if (status != OK) {
110 return status;
111 }
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +0100112 // Installers get the same view as all other apps, with the sole exception that the
113 // OBB dirs (Android/obb) are writable to them. On sdcardfs devices, this requires
114 // a special bind mount, since app-private and OBB dirs share the same GID, but we
115 // only want to give access to the latter.
116 if (!mUseSdcardFs) {
117 return OK;
118 }
119 std::string installerSource(
120 StringPrintf("/mnt/runtime/write/%s/%d/Android/obb", label.c_str(), userId));
121 std::string installerTarget(
122 StringPrintf("/mnt/installer/%d/%s/%d/Android/obb", userId, label.c_str(), userId));
123
124 status = doFuseBindMount(installerSource, installerTarget);
125 if (status != OK) {
126 return status;
127 }
Ricky Wai07e64a42020-02-11 14:31:24 +0000128
129 if (mAppDataIsolationEnabled) {
Ricky Waic1e33a32020-02-20 16:10:01 +0000130 // Starting from now, fuse is running, and zygote will bind app obb & data directory
Ricky Wai07e64a42020-02-11 14:31:24 +0000131 if (!VolumeManager::Instance()->addFuseMountedUser(userId)) {
132 return UNKNOWN_ERROR;
133 }
134
135 // As all new processes created by zygote will bind app obb data directory, we just need
136 // to have a snapshot of all existing processes and see if any existing process needs to
137 // remount obb data directory.
Ricky Waic1e33a32020-02-20 16:10:01 +0000138 VolumeManager::Instance()->remountAppStorageDirs(userId);
Ricky Wai07e64a42020-02-11 14:31:24 +0000139 }
140
Martijn Coenen57002612019-11-28 11:56:13 +0100141 return OK;
142}
143
Martijn Coenen86f21a22020-01-06 09:48:14 +0100144status_t EmulatedVolume::unmountFuseBindMounts() {
145 std::string label = getLabel();
146 int userId = getMountUserId();
147
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +0100148 if (mUseSdcardFs) {
149 std::string installerTarget(
150 StringPrintf("/mnt/installer/%d/%s/%d/Android/obb", userId, label.c_str(), userId));
151 LOG(INFO) << "Unmounting " << installerTarget;
152 auto status = UnmountTree(installerTarget);
153 if (status != OK) {
154 LOG(ERROR) << "Failed to unmount " << installerTarget;
155 // Intentional continue to try to unmount the other bind mount
156 }
157 }
Ricky Wai07e64a42020-02-11 14:31:24 +0000158 // When app data isolation is enabled, kill all apps that obb/ is mounted, otherwise we should
159 // umount the whole Android/ dir.
160 if (mAppDataIsolationEnabled) {
161 std::string appObbDir(StringPrintf("%s/%d/Android/obb", getPath().c_str(), userId));
162 KillProcessesWithMountPrefix(appObbDir);
163 } else {
164 std::string androidTarget(
165 StringPrintf("/mnt/user/%d/%s/%d/Android", userId, label.c_str(), userId));
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +0100166
Ricky Wai07e64a42020-02-11 14:31:24 +0000167 LOG(INFO) << "Unmounting " << androidTarget;
168 auto status = UnmountTree(androidTarget);
169 if (status != OK) {
170 return status;
171 }
172 LOG(INFO) << "Unmounted " << androidTarget;
Martijn Coenen57002612019-11-28 11:56:13 +0100173 }
Martijn Coenen57002612019-11-28 11:56:13 +0100174
175 return OK;
176}
177
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100178status_t EmulatedVolume::doMount() {
179 std::string label = getLabel();
180 bool isVisible = getMountFlags() & MountFlags::kVisible;
Jeff Sharkey81f55c62015-07-07 14:37:03 -0700181
Martijn Coenenadcc8452019-12-09 14:18:01 +0100182 mSdcardFsDefault = StringPrintf("/mnt/runtime/default/%s", label.c_str());
183 mSdcardFsRead = StringPrintf("/mnt/runtime/read/%s", label.c_str());
184 mSdcardFsWrite = StringPrintf("/mnt/runtime/write/%s", label.c_str());
185 mSdcardFsFull = StringPrintf("/mnt/runtime/full/%s", label.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700186
187 setInternalPath(mRawPath);
Jeff Sharkey81f55c62015-07-07 14:37:03 -0700188 setPath(StringPrintf("/storage/%s", label.c_str()));
Jeff Sharkey66270a22015-06-24 11:49:24 -0700189
Martijn Coenenadcc8452019-12-09 14:18:01 +0100190 if (fs_prepare_dir(mSdcardFsDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
191 fs_prepare_dir(mSdcardFsRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
192 fs_prepare_dir(mSdcardFsWrite.c_str(), 0700, AID_ROOT, AID_ROOT) ||
193 fs_prepare_dir(mSdcardFsFull.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700194 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800195 return -errno;
196 }
197
Martijn Coenenadcc8452019-12-09 14:18:01 +0100198 dev_t before = GetDevice(mSdcardFsFull);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700199
Abhijeet Kaur01fa0e02019-12-13 10:26:32 +0000200 bool isFuse = base::GetBoolProperty(kPropFuse, false);
Zim3623a212019-07-19 16:46:53 +0100201
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100202 // Mount sdcardfs regardless of FUSE, since we need it to bind-mount on top of the
203 // FUSE volume for various reasons.
Martijn Coenen86f21a22020-01-06 09:48:14 +0100204 if (mUseSdcardFs && getMountUserId() == 0) {
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100205 LOG(INFO) << "Executing sdcardfs";
206 int sdcardFsPid;
207 if (!(sdcardFsPid = fork())) {
208 // clang-format off
209 if (execl(kSdcardFsPath, kSdcardFsPath,
210 "-u", "1023", // AID_MEDIA_RW
211 "-g", "1023", // AID_MEDIA_RW
212 "-m",
213 "-w",
214 "-G",
215 "-i",
216 "-o",
217 mRawPath.c_str(),
218 label.c_str(),
219 NULL)) {
220 // clang-format on
221 PLOG(ERROR) << "Failed to exec";
222 }
223
224 LOG(ERROR) << "sdcardfs exiting";
225 _exit(1);
226 }
227
228 if (sdcardFsPid == -1) {
229 PLOG(ERROR) << getId() << " failed to fork";
230 return -errno;
231 }
232
233 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
234 while (before == GetDevice(mSdcardFsFull)) {
235 LOG(DEBUG) << "Waiting for sdcardfs to spin up...";
236 usleep(50000); // 50ms
237
238 nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
239 if (nanoseconds_to_milliseconds(now - start) > 5000) {
240 LOG(WARNING) << "Timed out while waiting for sdcardfs to spin up";
241 return -ETIMEDOUT;
242 }
243 }
244 /* sdcardfs will have exited already. The filesystem will still be running */
245 TEMP_FAILURE_RETRY(waitpid(sdcardFsPid, nullptr, 0));
246 sdcardFsPid = 0;
247 }
248 if (isFuse && isVisible) {
Zim3623a212019-07-19 16:46:53 +0100249 LOG(INFO) << "Mounting emulated fuse volume";
Nandana Dutta914cc72019-08-29 15:22:42 +0100250 android::base::unique_fd fd;
Zim981222f2019-09-09 10:24:44 +0100251 int user_id = getMountUserId();
Martijn Coenen62a4b272020-01-31 15:23:09 +0100252 auto volumeRoot = getRootPath();
Zim981222f2019-09-09 10:24:44 +0100253
Martijn Coenen62a4b272020-01-31 15:23:09 +0100254 // Make sure Android/ dirs exist for bind mounting
255 status_t res = PrepareAndroidDirs(volumeRoot);
256 if (res != OK) {
257 LOG(ERROR) << "Failed to prepare Android/ directories";
258 return res;
259 }
260
261 res = MountUserFuse(user_id, getInternalPath(), label, &fd);
262 if (res != 0) {
Zim3623a212019-07-19 16:46:53 +0100263 PLOG(ERROR) << "Failed to mount emulated fuse volume";
Martijn Coenen62a4b272020-01-31 15:23:09 +0100264 return res;
Zim3623a212019-07-19 16:46:53 +0100265 }
Zim5048b4b2019-11-19 09:16:03 +0000266
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100267 mFuseMounted = true;
Zim5048b4b2019-11-19 09:16:03 +0000268 auto callback = getMountCallback();
269 if (callback) {
270 bool is_ready = false;
271 callback->onVolumeChecking(std::move(fd), getPath(), getInternalPath(), &is_ready);
272 if (!is_ready) {
Zimdf073f52020-01-15 15:00:07 +0000273 fd.reset();
274 doUnmount();
Zim5048b4b2019-11-19 09:16:03 +0000275 return -EIO;
276 }
277 }
Martijn Coenen57002612019-11-28 11:56:13 +0100278
279 // Only do the bind-mounts when we know for sure the FUSE daemon can resolve the path.
Martijn Coenen62a4b272020-01-31 15:23:09 +0100280 res = mountFuseBindMounts();
Zimdf073f52020-01-15 15:00:07 +0000281 if (res != OK) {
282 fd.reset();
283 doUnmount();
284 }
285 return res;
Zim3623a212019-07-19 16:46:53 +0100286 }
287
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800288 return OK;
289}
290
291status_t EmulatedVolume::doUnmount() {
Martijn Coenen8f1e7f22019-11-29 15:38:55 +0100292 int userId = getMountUserId();
293
294 // Kill all processes using the filesystem before we unmount it. If we
295 // unmount the filesystem first, most file system operations will return
Narayan Kamathea243a32016-01-21 12:26:05 +0000296 // ENOTCONN until the unmount completes. This is an exotic and unusual
297 // error code and might cause broken behaviour in applications.
Martijn Coenen8f1e7f22019-11-29 15:38:55 +0100298 if (mFuseMounted) {
299 // For FUSE specifically, we have an emulated volume per user, so only kill
300 // processes using files from this particular user.
301 std::string user_path(StringPrintf("%s/%d", getPath().c_str(), getMountUserId()));
302 LOG(INFO) << "Killing all processes referencing " << user_path;
303 KillProcessesUsingPath(user_path);
304 } else {
305 KillProcessesUsingPath(getPath());
306 }
Zim3623a212019-07-19 16:46:53 +0100307
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100308 if (mFuseMounted) {
309 std::string label = getLabel();
Ricky Wai07e64a42020-02-11 14:31:24 +0000310
311 // Update fuse mounted record
312 if (mAppDataIsolationEnabled &&
313 !VolumeManager::Instance()->removeFuseMountedUser(userId)) {
314 return UNKNOWN_ERROR;
315 }
316
Martijn Coenen57002612019-11-28 11:56:13 +0100317 // Ignoring unmount return status because we do want to try to unmount
318 // the rest cleanly.
Martijn Coenen86f21a22020-01-06 09:48:14 +0100319 unmountFuseBindMounts();
Martijn Coenen57002612019-11-28 11:56:13 +0100320 if (UnmountUserFuse(userId, getInternalPath(), label) != OK) {
Zima438b242019-09-25 14:37:38 +0100321 PLOG(INFO) << "UnmountUserFuse failed on emulated fuse volume";
322 return -errno;
Zim3623a212019-07-19 16:46:53 +0100323 }
324
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100325 mFuseMounted = false;
326 }
Martijn Coenen86f21a22020-01-06 09:48:14 +0100327 if (getMountUserId() != 0 || !mUseSdcardFs) {
Zim2d45d9b2019-11-14 16:19:05 +0000328 // For sdcardfs, only unmount for user 0, since user 0 will always be running
329 // and the paths don't change for different users.
330 return OK;
Zim3623a212019-07-19 16:46:53 +0100331 }
332
Martijn Coenenadcc8452019-12-09 14:18:01 +0100333 ForceUnmount(mSdcardFsDefault);
334 ForceUnmount(mSdcardFsRead);
335 ForceUnmount(mSdcardFsWrite);
336 ForceUnmount(mSdcardFsFull);
Narayan Kamathea243a32016-01-21 12:26:05 +0000337
Martijn Coenenadcc8452019-12-09 14:18:01 +0100338 rmdir(mSdcardFsDefault.c_str());
339 rmdir(mSdcardFsRead.c_str());
340 rmdir(mSdcardFsWrite.c_str());
341 rmdir(mSdcardFsFull.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700342
Martijn Coenenadcc8452019-12-09 14:18:01 +0100343 mSdcardFsDefault.clear();
344 mSdcardFsRead.clear();
345 mSdcardFsWrite.clear();
346 mSdcardFsFull.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800347
348 return OK;
349}
350
Martijn Coenen62a4b272020-01-31 15:23:09 +0100351std::string EmulatedVolume::getRootPath() const {
352 int user_id = getMountUserId();
353 std::string volumeRoot = StringPrintf("%s/%d", getInternalPath().c_str(), user_id);
354
355 return volumeRoot;
356}
357
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800358} // namespace vold
359} // namespace android