blob: f0adb6cbb2f0cdafb46e7762538e04bde23cb867 [file] [log] [blame]
Jeff Sharkey9c484982015-03-31 10:35:33 -07001/*
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 Sharkeyd0640f62015-05-21 22:35:42 -070017#include "fs/Ext4.h"
18#include "fs/F2fs.h"
Jeff Sharkey9c484982015-03-31 10:35:33 -070019#include "PrivateVolume.h"
Jeff Sharkey3161fb32015-04-12 16:03:33 -070020#include "EmulatedVolume.h"
Jeff Sharkey9c484982015-03-31 10:35:33 -070021#include "Utils.h"
22#include "VolumeManager.h"
Jeff Sharkey9c484982015-03-31 10:35:33 -070023#include "cryptfs.h"
24
Elliott Hughes7e128fb2015-12-04 15:50:53 -080025#include <android-base/stringprintf.h>
26#include <android-base/logging.h>
Jeff Sharkey9c484982015-03-31 10:35:33 -070027#include <cutils/fs.h>
28#include <private/android_filesystem_config.h>
29
30#include <fcntl.h>
31#include <stdlib.h>
32#include <sys/mount.h>
33#include <sys/stat.h>
34#include <sys/types.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070035#include <sys/sysmacros.h>
Jeff Sharkey9c484982015-03-31 10:35:33 -070036#include <sys/wait.h>
37#include <sys/param.h>
38
39using android::base::StringPrintf;
40
LuK13374d530c62018-03-06 11:31:14 +010041using namespace std::chrono_literals;
42
Jeff Sharkey9c484982015-03-31 10:35:33 -070043namespace android {
44namespace vold {
45
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070046static const unsigned int kMajorBlockMmc = 179;
47
Jeff Sharkey9c484982015-03-31 10:35:33 -070048PrivateVolume::PrivateVolume(dev_t device, const std::string& keyRaw) :
49 VolumeBase(Type::kPrivate), mRawDevice(device), mKeyRaw(keyRaw) {
50 setId(StringPrintf("private:%u,%u", major(device), minor(device)));
51 mRawDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
Jeff Sharkey9c484982015-03-31 10:35:33 -070052}
53
54PrivateVolume::~PrivateVolume() {
55}
56
57status_t PrivateVolume::readMetadata() {
Jeff Sharkey3472e522017-10-06 18:02:53 -060058 status_t res = ReadMetadata(mDmDevPath, &mFsType, &mFsUuid, &mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060059
Jeff Sharkey814e9d32017-09-13 11:49:44 -060060 auto listener = getListener();
61 if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060062
Jeff Sharkey9c484982015-03-31 10:35:33 -070063 return res;
64}
65
66status_t PrivateVolume::doCreate() {
67 if (CreateDeviceNode(mRawDevPath, mRawDevice)) {
68 return -EIO;
69 }
Greg Kaiser57f9af62018-02-16 13:13:58 -080070 if (mKeyRaw.size() != cryptfs_get_keysize()) {
71 PLOG(ERROR) << getId() << " Raw keysize " << mKeyRaw.size() <<
72 " does not match crypt keysize " << cryptfs_get_keysize();
73 return -EIO;
74 }
Jeff Sharkey9c484982015-03-31 10:35:33 -070075
76 // Recover from stale vold by tearing down any old mappings
77 cryptfs_revert_ext_volume(getId().c_str());
78
79 // TODO: figure out better SELinux labels for private volumes
80
81 unsigned char* key = (unsigned char*) mKeyRaw.data();
82 char crypto_blkdev[MAXPATHLEN];
83 int res = cryptfs_setup_ext_volume(getId().c_str(), mRawDevPath.c_str(),
Greg Kaiser57f9af62018-02-16 13:13:58 -080084 key, crypto_blkdev);
Jeff Sharkey9c484982015-03-31 10:35:33 -070085 mDmDevPath = crypto_blkdev;
86 if (res != 0) {
87 PLOG(ERROR) << getId() << " failed to setup cryptfs";
88 return -EIO;
89 }
90
91 return OK;
92}
93
94status_t PrivateVolume::doDestroy() {
95 if (cryptfs_revert_ext_volume(getId().c_str())) {
96 LOG(ERROR) << getId() << " failed to revert cryptfs";
97 }
98 return DestroyDeviceNode(mRawDevPath);
99}
100
101status_t PrivateVolume::doMount() {
LuK13373efffb42018-03-14 14:16:08 +0100102 if (!WaitForFile(mDmDevPath, 15s)) {
103 PLOG(ERROR) << "Timed out waiting for " << getId();
104 return -EIO;
105 }
106
Jeff Sharkey9c484982015-03-31 10:35:33 -0700107 if (readMetadata()) {
108 LOG(ERROR) << getId() << " failed to read metadata";
109 return -EIO;
110 }
111
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700112 mPath = StringPrintf("/mnt/expand/%s", mFsUuid.c_str());
113 setPath(mPath);
114
115 if (PrepareDir(mPath, 0700, AID_ROOT, AID_ROOT)) {
116 PLOG(ERROR) << getId() << " failed to create mount point " << mPath;
117 return -EIO;
118 }
119
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700120 if (mFsType == "ext4") {
Michael Bestasa66df7c2015-12-06 23:53:55 +0200121 int res = ext4::Check(mDmDevPath, mPath, true);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700122 if (res == 0 || res == 1) {
123 LOG(DEBUG) << getId() << " passed filesystem check";
124 } else {
125 PLOG(ERROR) << getId() << " failed filesystem check";
126 return -EIO;
127 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700128
Sam Mortimerbb0533a2015-11-27 15:27:03 -0800129 if (ext4::Mount(mDmDevPath, mPath, false, false, true, "", true)) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700130 PLOG(ERROR) << getId() << " failed to mount";
131 return -EIO;
132 }
133
134 } else if (mFsType == "f2fs") {
Michael Bestasa66df7c2015-12-06 23:53:55 +0200135 int res = f2fs::Check(mDmDevPath, true);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700136 if (res == 0) {
137 LOG(DEBUG) << getId() << " passed filesystem check";
138 } else {
139 PLOG(ERROR) << getId() << " failed filesystem check";
140 return -EIO;
141 }
142
Sam Mortimerbb0533a2015-11-27 15:27:03 -0800143 if (f2fs::Mount(mDmDevPath, mPath, "", true)) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700144 PLOG(ERROR) << getId() << " failed to mount";
145 return -EIO;
146 }
147
148 } else {
149 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700150 return -EIO;
151 }
152
Jeff Sharkeyd24aeda2016-07-15 16:20:22 -0600153 RestoreconRecursive(mPath);
Jeff Sharkey34824122015-06-09 10:59:17 -0700154
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700155 // Verify that common directories are ready to roll
156 if (PrepareDir(mPath + "/app", 0771, AID_SYSTEM, AID_SYSTEM) ||
157 PrepareDir(mPath + "/user", 0711, AID_SYSTEM, AID_SYSTEM) ||
Jeff Sharkeyd322c2a2015-11-17 12:16:49 -0700158 PrepareDir(mPath + "/user_de", 0711, AID_SYSTEM, AID_SYSTEM) ||
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700159 PrepareDir(mPath + "/media", 0770, AID_MEDIA_RW, AID_MEDIA_RW) ||
Jeff Sharkey81f55c62015-07-07 14:37:03 -0700160 PrepareDir(mPath + "/media/0", 0770, AID_MEDIA_RW, AID_MEDIA_RW) ||
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700161 PrepareDir(mPath + "/local", 0751, AID_ROOT, AID_ROOT) ||
162 PrepareDir(mPath + "/local/tmp", 0771, AID_SHELL, AID_SHELL)) {
163 PLOG(ERROR) << getId() << " failed to prepare";
164 return -EIO;
165 }
166
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700167 // Create a new emulated volume stacked above us, it will automatically
168 // be destroyed during unmount
169 std::string mediaPath(mPath + "/media");
170 auto vol = std::shared_ptr<VolumeBase>(
171 new EmulatedVolume(mediaPath, mRawDevice, mFsUuid));
172 addVolume(vol);
173 vol->create();
174
Jeff Sharkey9c484982015-03-31 10:35:33 -0700175 return OK;
176}
177
178status_t PrivateVolume::doUnmount() {
179 ForceUnmount(mPath);
180
181 if (TEMP_FAILURE_RETRY(rmdir(mPath.c_str()))) {
182 PLOG(ERROR) << getId() << " failed to rmdir mount point " << mPath;
183 }
184
185 return OK;
186}
187
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700188status_t PrivateVolume::doFormat(const std::string& fsType) {
189 std::string resolvedFsType = fsType;
190 if (fsType == "auto") {
191 // For now, assume that all MMC devices are flash-based SD cards, and
192 // give everyone else ext4 because sysfs rotational isn't reliable.
193 if ((major(mRawDevice) == kMajorBlockMmc) && f2fs::IsSupported()) {
194 resolvedFsType = "f2fs";
195 } else {
196 resolvedFsType = "ext4";
197 }
198 LOG(DEBUG) << "Resolved auto to " << resolvedFsType;
199 }
200
LuK13374d530c62018-03-06 11:31:14 +0100201 if (!WaitForFile(mDmDevPath, 15s)) {
202 PLOG(ERROR) << "Timed out waiting for " << getId();
203 return -EIO;
204 }
205
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700206 if (resolvedFsType == "ext4") {
207 // TODO: change reported mountpoint once we have better selinux support
208 if (ext4::Format(mDmDevPath, 0, "/data")) {
209 PLOG(ERROR) << getId() << " failed to format";
210 return -EIO;
211 }
212 } else if (resolvedFsType == "f2fs") {
213 if (f2fs::Format(mDmDevPath)) {
214 PLOG(ERROR) << getId() << " failed to format";
215 return -EIO;
216 }
217 } else {
218 LOG(ERROR) << getId() << " unsupported filesystem " << fsType;
219 return -EINVAL;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700220 }
221
222 return OK;
223}
224
225} // namespace vold
226} // namespace android