blob: 02d5c377302726e7ce78e8ccb8c84351fb4b8003 [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 }
Martijn Coenen57002612019-11-28 11:56:13 +0100128 return OK;
129}
130
Martijn Coenen86f21a22020-01-06 09:48:14 +0100131status_t EmulatedVolume::unmountFuseBindMounts() {
132 std::string label = getLabel();
133 int userId = getMountUserId();
134
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +0100135 if (mUseSdcardFs) {
136 std::string installerTarget(
137 StringPrintf("/mnt/installer/%d/%s/%d/Android/obb", userId, label.c_str(), userId));
138 LOG(INFO) << "Unmounting " << installerTarget;
139 auto status = UnmountTree(installerTarget);
140 if (status != OK) {
141 LOG(ERROR) << "Failed to unmount " << installerTarget;
142 // Intentional continue to try to unmount the other bind mount
143 }
144 }
Ricky Wai07e64a42020-02-11 14:31:24 +0000145 // When app data isolation is enabled, kill all apps that obb/ is mounted, otherwise we should
146 // umount the whole Android/ dir.
147 if (mAppDataIsolationEnabled) {
148 std::string appObbDir(StringPrintf("%s/%d/Android/obb", getPath().c_str(), userId));
149 KillProcessesWithMountPrefix(appObbDir);
150 } else {
151 std::string androidTarget(
152 StringPrintf("/mnt/user/%d/%s/%d/Android", userId, label.c_str(), userId));
Martijn Coenen3a2dbfe2020-01-11 19:38:37 +0100153
Ricky Wai07e64a42020-02-11 14:31:24 +0000154 LOG(INFO) << "Unmounting " << androidTarget;
155 auto status = UnmountTree(androidTarget);
156 if (status != OK) {
157 return status;
158 }
159 LOG(INFO) << "Unmounted " << androidTarget;
Martijn Coenen57002612019-11-28 11:56:13 +0100160 }
Martijn Coenen57002612019-11-28 11:56:13 +0100161
162 return OK;
163}
164
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100165status_t EmulatedVolume::doMount() {
166 std::string label = getLabel();
167 bool isVisible = getMountFlags() & MountFlags::kVisible;
Jeff Sharkey81f55c62015-07-07 14:37:03 -0700168
Martijn Coenenadcc8452019-12-09 14:18:01 +0100169 mSdcardFsDefault = StringPrintf("/mnt/runtime/default/%s", label.c_str());
170 mSdcardFsRead = StringPrintf("/mnt/runtime/read/%s", label.c_str());
171 mSdcardFsWrite = StringPrintf("/mnt/runtime/write/%s", label.c_str());
172 mSdcardFsFull = StringPrintf("/mnt/runtime/full/%s", label.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700173
174 setInternalPath(mRawPath);
Jeff Sharkey81f55c62015-07-07 14:37:03 -0700175 setPath(StringPrintf("/storage/%s", label.c_str()));
Jeff Sharkey66270a22015-06-24 11:49:24 -0700176
Martijn Coenenadcc8452019-12-09 14:18:01 +0100177 if (fs_prepare_dir(mSdcardFsDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
178 fs_prepare_dir(mSdcardFsRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
179 fs_prepare_dir(mSdcardFsWrite.c_str(), 0700, AID_ROOT, AID_ROOT) ||
180 fs_prepare_dir(mSdcardFsFull.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700181 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800182 return -errno;
183 }
184
Martijn Coenenadcc8452019-12-09 14:18:01 +0100185 dev_t before = GetDevice(mSdcardFsFull);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700186
Abhijeet Kaur01fa0e02019-12-13 10:26:32 +0000187 bool isFuse = base::GetBoolProperty(kPropFuse, false);
Zim3623a212019-07-19 16:46:53 +0100188
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100189 // Mount sdcardfs regardless of FUSE, since we need it to bind-mount on top of the
190 // FUSE volume for various reasons.
Martijn Coenen86f21a22020-01-06 09:48:14 +0100191 if (mUseSdcardFs && getMountUserId() == 0) {
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100192 LOG(INFO) << "Executing sdcardfs";
193 int sdcardFsPid;
194 if (!(sdcardFsPid = fork())) {
195 // clang-format off
196 if (execl(kSdcardFsPath, kSdcardFsPath,
197 "-u", "1023", // AID_MEDIA_RW
198 "-g", "1023", // AID_MEDIA_RW
199 "-m",
200 "-w",
201 "-G",
202 "-i",
203 "-o",
204 mRawPath.c_str(),
205 label.c_str(),
206 NULL)) {
207 // clang-format on
208 PLOG(ERROR) << "Failed to exec";
209 }
210
211 LOG(ERROR) << "sdcardfs exiting";
212 _exit(1);
213 }
214
215 if (sdcardFsPid == -1) {
216 PLOG(ERROR) << getId() << " failed to fork";
217 return -errno;
218 }
219
220 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
221 while (before == GetDevice(mSdcardFsFull)) {
222 LOG(DEBUG) << "Waiting for sdcardfs to spin up...";
223 usleep(50000); // 50ms
224
225 nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
226 if (nanoseconds_to_milliseconds(now - start) > 5000) {
227 LOG(WARNING) << "Timed out while waiting for sdcardfs to spin up";
228 return -ETIMEDOUT;
229 }
230 }
231 /* sdcardfs will have exited already. The filesystem will still be running */
232 TEMP_FAILURE_RETRY(waitpid(sdcardFsPid, nullptr, 0));
233 sdcardFsPid = 0;
234 }
235 if (isFuse && isVisible) {
Zim3623a212019-07-19 16:46:53 +0100236 LOG(INFO) << "Mounting emulated fuse volume";
Nandana Dutta914cc72019-08-29 15:22:42 +0100237 android::base::unique_fd fd;
Zim981222f2019-09-09 10:24:44 +0100238 int user_id = getMountUserId();
Martijn Coenen62a4b272020-01-31 15:23:09 +0100239 auto volumeRoot = getRootPath();
Zim981222f2019-09-09 10:24:44 +0100240
Martijn Coenen62a4b272020-01-31 15:23:09 +0100241 // Make sure Android/ dirs exist for bind mounting
242 status_t res = PrepareAndroidDirs(volumeRoot);
243 if (res != OK) {
244 LOG(ERROR) << "Failed to prepare Android/ directories";
245 return res;
246 }
247
248 res = MountUserFuse(user_id, getInternalPath(), label, &fd);
249 if (res != 0) {
Zim3623a212019-07-19 16:46:53 +0100250 PLOG(ERROR) << "Failed to mount emulated fuse volume";
Martijn Coenen62a4b272020-01-31 15:23:09 +0100251 return res;
Zim3623a212019-07-19 16:46:53 +0100252 }
Zim5048b4b2019-11-19 09:16:03 +0000253
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100254 mFuseMounted = true;
Zim5048b4b2019-11-19 09:16:03 +0000255 auto callback = getMountCallback();
256 if (callback) {
257 bool is_ready = false;
258 callback->onVolumeChecking(std::move(fd), getPath(), getInternalPath(), &is_ready);
259 if (!is_ready) {
Zimdf073f52020-01-15 15:00:07 +0000260 fd.reset();
261 doUnmount();
Zim5048b4b2019-11-19 09:16:03 +0000262 return -EIO;
263 }
264 }
Martijn Coenen57002612019-11-28 11:56:13 +0100265
266 // Only do the bind-mounts when we know for sure the FUSE daemon can resolve the path.
Martijn Coenen62a4b272020-01-31 15:23:09 +0100267 res = mountFuseBindMounts();
Zimdf073f52020-01-15 15:00:07 +0000268 if (res != OK) {
269 fd.reset();
270 doUnmount();
271 }
272 return res;
Zim3623a212019-07-19 16:46:53 +0100273 }
274
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800275 return OK;
276}
277
278status_t EmulatedVolume::doUnmount() {
Martijn Coenen8f1e7f22019-11-29 15:38:55 +0100279 int userId = getMountUserId();
280
281 // Kill all processes using the filesystem before we unmount it. If we
282 // unmount the filesystem first, most file system operations will return
Narayan Kamathea243a32016-01-21 12:26:05 +0000283 // ENOTCONN until the unmount completes. This is an exotic and unusual
284 // error code and might cause broken behaviour in applications.
Martijn Coenen8f1e7f22019-11-29 15:38:55 +0100285 if (mFuseMounted) {
286 // For FUSE specifically, we have an emulated volume per user, so only kill
287 // processes using files from this particular user.
288 std::string user_path(StringPrintf("%s/%d", getPath().c_str(), getMountUserId()));
289 LOG(INFO) << "Killing all processes referencing " << user_path;
290 KillProcessesUsingPath(user_path);
291 } else {
292 KillProcessesUsingPath(getPath());
293 }
Zim3623a212019-07-19 16:46:53 +0100294
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100295 if (mFuseMounted) {
296 std::string label = getLabel();
Ricky Wai07e64a42020-02-11 14:31:24 +0000297
Martijn Coenen57002612019-11-28 11:56:13 +0100298 // Ignoring unmount return status because we do want to try to unmount
299 // the rest cleanly.
Martijn Coenen86f21a22020-01-06 09:48:14 +0100300 unmountFuseBindMounts();
Martijn Coenen57002612019-11-28 11:56:13 +0100301 if (UnmountUserFuse(userId, getInternalPath(), label) != OK) {
Zima438b242019-09-25 14:37:38 +0100302 PLOG(INFO) << "UnmountUserFuse failed on emulated fuse volume";
303 return -errno;
Zim3623a212019-07-19 16:46:53 +0100304 }
305
Martijn Coenen6f5802e2019-11-28 11:53:53 +0100306 mFuseMounted = false;
307 }
Martijn Coenen86f21a22020-01-06 09:48:14 +0100308 if (getMountUserId() != 0 || !mUseSdcardFs) {
Zim2d45d9b2019-11-14 16:19:05 +0000309 // For sdcardfs, only unmount for user 0, since user 0 will always be running
310 // and the paths don't change for different users.
311 return OK;
Zim3623a212019-07-19 16:46:53 +0100312 }
313
Martijn Coenenadcc8452019-12-09 14:18:01 +0100314 ForceUnmount(mSdcardFsDefault);
315 ForceUnmount(mSdcardFsRead);
316 ForceUnmount(mSdcardFsWrite);
317 ForceUnmount(mSdcardFsFull);
Narayan Kamathea243a32016-01-21 12:26:05 +0000318
Martijn Coenenadcc8452019-12-09 14:18:01 +0100319 rmdir(mSdcardFsDefault.c_str());
320 rmdir(mSdcardFsRead.c_str());
321 rmdir(mSdcardFsWrite.c_str());
322 rmdir(mSdcardFsFull.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700323
Martijn Coenenadcc8452019-12-09 14:18:01 +0100324 mSdcardFsDefault.clear();
325 mSdcardFsRead.clear();
326 mSdcardFsWrite.clear();
327 mSdcardFsFull.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800328
329 return OK;
330}
331
Martijn Coenen62a4b272020-01-31 15:23:09 +0100332std::string EmulatedVolume::getRootPath() const {
333 int user_id = getMountUserId();
334 std::string volumeRoot = StringPrintf("%s/%d", getInternalPath().c_str(), user_id);
335
336 return volumeRoot;
337}
338
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800339} // namespace vold
340} // namespace android