blob: dcb823debe32cf1696ec7550e3e4ffc63e546af8 [file] [log] [blame]
Tom Marshallbead2612019-01-04 14:37:31 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 * Copyright (C) 2019 The LineageOS Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include "EmulatedVolume.h"
19#include <volume_manager/VolumeManager.h>
20#include "ResponseCode.h"
21#include "Utils.h"
22
23#include <android-base/logging.h>
24#include <android-base/stringprintf.h>
25#include <cutils/fs.h>
26#include <private/android_filesystem_config.h>
27
28#include <fcntl.h>
29#include <stdlib.h>
30#include <sys/mount.h>
31#include <sys/stat.h>
32#include <sys/sysmacros.h>
33#include <sys/types.h>
34#include <sys/wait.h>
Tom Marshallb00c91f2019-07-24 21:12:07 +020035#include <unistd.h>
Tom Marshallbead2612019-01-04 14:37:31 -080036
37using android::base::StringPrintf;
38
39namespace android {
40namespace volmgr {
41
42static const std::string kStagingPath = "/mnt/staging/emulated";
Tom Marshallb00c91f2019-07-24 21:12:07 +020043static const std::string kFbeKeyVersion = kStagingPath + "/unencrypted/key/version";
Tom Marshallbead2612019-01-04 14:37:31 -080044
45EmulatedVolume::EmulatedVolume(FstabEntry* rec, const std::string& subdir)
46 : VolumeBase(Type::kEmulated),
47 mSubdir(subdir),
48 mDevPath(rec->blk_device),
49 mFsType(rec->fs_type),
50 mFlags(rec->flags),
51 mFsOptions(rec->fs_options) {
52 setId("emulated");
53 setPartLabel("internal storage");
54 setPath("/storage/emulated");
55}
56
57EmulatedVolume::~EmulatedVolume() {}
58
59status_t EmulatedVolume::doMount() {
60 if (fs_prepare_dir(kStagingPath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
61 PLOG(ERROR) << getId() << " failed to create mount points";
62 return -errno;
63 }
64 if (fs_prepare_dir(getPath().c_str(), 0700, AID_ROOT, AID_ROOT)) {
65 PLOG(ERROR) << getId() << " failed to create mount points";
66 return -errno;
67 }
68
69 std::string bindPath = kStagingPath + "/" + mSubdir;
70
71 if (::mount(mDevPath.c_str(), kStagingPath.c_str(), mFsType.c_str(), mFlags,
72 mFsOptions.c_str()) != 0) {
73 PLOG(ERROR) << getId() << " failed to mount " << mDevPath << " on " << kStagingPath;
74 return -EIO;
75 }
76 if (BindMount(bindPath, getPath()) != OK) {
77 PLOG(ERROR) << getId() << " failed to bind mount " << bindPath << " on " << getPath();
78 ForceUnmount(kStagingPath);
79 return -EIO;
80 }
81
82 return OK;
83}
84
85status_t EmulatedVolume::doUnmount(bool detach /* = false */) {
86 ForceUnmount(getPath(), detach);
87 ForceUnmount(kStagingPath, detach);
88
89 rmdir(getPath().c_str());
90 rmdir(kStagingPath.c_str());
91
92 return OK;
93}
94
Tom Marshallb00c91f2019-07-24 21:12:07 +020095bool EmulatedVolume::detectMountable() {
96 bool mountable = false;
97 if (doMount() == OK) {
98 // Check if FBE encrypted
99 mountable = access(kFbeKeyVersion.c_str(), F_OK) != 0;
100 doUnmount();
101 }
102 return mountable;
103}
104
Tom Marshallbead2612019-01-04 14:37:31 -0800105} // namespace volmgr
106} // namespace android