blob: 75aa938f0bb7839fc89c02e9421aa7ef8ec0f4c0 [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 Sharkey9c484982015-03-31 10:35:33 -070017#include "PrivateVolume.h"
Jeff Sharkey3161fb32015-04-12 16:03:33 -070018#include "EmulatedVolume.h"
Jeff Sharkey9c484982015-03-31 10:35:33 -070019#include "Utils.h"
20#include "VolumeManager.h"
Jeff Sharkey9c484982015-03-31 10:35:33 -070021#include "cryptfs.h"
Paul Crowley14c8c072018-09-18 13:30:21 -070022#include "fs/Ext4.h"
23#include "fs/F2fs.h"
Jeff Sharkey9c484982015-03-31 10:35:33 -070024
Elliott Hughes7e128fb2015-12-04 15:50:53 -080025#include <android-base/logging.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070026#include <android-base/stringprintf.h>
Jeff Sharkey9c484982015-03-31 10:35:33 -070027#include <cutils/fs.h>
Paul Crowley659b63f2020-02-07 12:15:56 -080028#include <libdm/dm.h>
Jeff Sharkey9c484982015-03-31 10:35:33 -070029#include <private/android_filesystem_config.h>
30
31#include <fcntl.h>
32#include <stdlib.h>
33#include <sys/mount.h>
Jeff Sharkey9c484982015-03-31 10:35:33 -070034#include <sys/param.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070035#include <sys/stat.h>
36#include <sys/sysmacros.h>
37#include <sys/types.h>
38#include <sys/wait.h>
Ricky Wai9eb43672020-02-15 01:15:42 +000039#include <thread>
Jeff Sharkey9c484982015-03-31 10:35:33 -070040
41using android::base::StringPrintf;
42
43namespace android {
44namespace vold {
45
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070046static const unsigned int kMajorBlockMmc = 179;
47
Paul Crowley3d98f5d2020-02-07 11:49:09 -080048PrivateVolume::PrivateVolume(dev_t device, const KeyBuffer& keyRaw)
Paul Crowley14c8c072018-09-18 13:30:21 -070049 : VolumeBase(Type::kPrivate), mRawDevice(device), mKeyRaw(keyRaw) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070050 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
Paul Crowley14c8c072018-09-18 13:30:21 -070054PrivateVolume::~PrivateVolume() {}
Jeff Sharkey9c484982015-03-31 10:35:33 -070055
56status_t PrivateVolume::readMetadata() {
Jeff Sharkey3472e522017-10-06 18:02:53 -060057 status_t res = ReadMetadata(mDmDevPath, &mFsType, &mFsUuid, &mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060058
Jeff Sharkey814e9d32017-09-13 11:49:44 -060059 auto listener = getListener();
60 if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060061
Jeff Sharkey9c484982015-03-31 10:35:33 -070062 return res;
63}
64
65status_t PrivateVolume::doCreate() {
66 if (CreateDeviceNode(mRawDevPath, mRawDevice)) {
67 return -EIO;
68 }
69
70 // Recover from stale vold by tearing down any old mappings
Paul Crowley659b63f2020-02-07 12:15:56 -080071 auto& dm = dm::DeviceMapper::Instance();
Ricky Wai9eb43672020-02-15 01:15:42 +000072 // TODO(b/149396179) there appears to be a race somewhere in the system where trying
73 // to delete the device fails with EBUSY; for now, work around this by retrying.
74 bool ret;
75 int tries = 10;
76 while (tries-- > 0) {
77 ret = dm.DeleteDeviceIfExists(getId());
78 if (ret || errno != EBUSY) {
79 break;
80 }
Paul Crowley659b63f2020-02-07 12:15:56 -080081 PLOG(ERROR) << "Cannot remove dm device " << getId();
Ricky Wai9eb43672020-02-15 01:15:42 +000082 std::this_thread::sleep_for(std::chrono::milliseconds(100));
83 }
84 if (!ret) {
Paul Crowley659b63f2020-02-07 12:15:56 -080085 return -EIO;
86 }
Jeff Sharkey9c484982015-03-31 10:35:33 -070087
88 // TODO: figure out better SELinux labels for private volumes
89
Paul Crowley3d98f5d2020-02-07 11:49:09 -080090 int res = cryptfs_setup_ext_volume(getId().c_str(), mRawDevPath.c_str(), mKeyRaw, &mDmDevPath);
Jeff Sharkey9c484982015-03-31 10:35:33 -070091 if (res != 0) {
92 PLOG(ERROR) << getId() << " failed to setup cryptfs";
93 return -EIO;
94 }
95
96 return OK;
97}
98
99status_t PrivateVolume::doDestroy() {
Paul Crowley659b63f2020-02-07 12:15:56 -0800100 auto& dm = dm::DeviceMapper::Instance();
Ricky Wai9eb43672020-02-15 01:15:42 +0000101 // TODO(b/149396179) there appears to be a race somewhere in the system where trying
102 // to delete the device fails with EBUSY; for now, work around this by retrying.
103 bool ret;
104 int tries = 10;
105 while (tries-- > 0) {
106 ret = dm.DeleteDevice(getId());
107 if (ret || errno != EBUSY) {
108 break;
109 }
Paul Crowley659b63f2020-02-07 12:15:56 -0800110 PLOG(ERROR) << "Cannot remove dm device " << getId();
Ricky Wai9eb43672020-02-15 01:15:42 +0000111 std::this_thread::sleep_for(std::chrono::milliseconds(100));
112 }
113 if (!ret) {
Paul Crowley659b63f2020-02-07 12:15:56 -0800114 return -EIO;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700115 }
116 return DestroyDeviceNode(mRawDevPath);
117}
118
119status_t PrivateVolume::doMount() {
120 if (readMetadata()) {
121 LOG(ERROR) << getId() << " failed to read metadata";
122 return -EIO;
123 }
124
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700125 mPath = StringPrintf("/mnt/expand/%s", mFsUuid.c_str());
126 setPath(mPath);
127
128 if (PrepareDir(mPath, 0700, AID_ROOT, AID_ROOT)) {
129 PLOG(ERROR) << getId() << " failed to create mount point " << mPath;
130 return -EIO;
131 }
132
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700133 if (mFsType == "ext4") {
134 int res = ext4::Check(mDmDevPath, mPath);
135 if (res == 0 || res == 1) {
136 LOG(DEBUG) << getId() << " passed filesystem check";
137 } else {
138 PLOG(ERROR) << getId() << " failed filesystem check";
139 return -EIO;
140 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700141
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700142 if (ext4::Mount(mDmDevPath, mPath, false, false, true)) {
143 PLOG(ERROR) << getId() << " failed to mount";
144 return -EIO;
145 }
146
147 } else if (mFsType == "f2fs") {
148 int res = f2fs::Check(mDmDevPath);
149 if (res == 0) {
150 LOG(DEBUG) << getId() << " passed filesystem check";
151 } else {
152 PLOG(ERROR) << getId() << " failed filesystem check";
153 return -EIO;
154 }
155
156 if (f2fs::Mount(mDmDevPath, mPath)) {
157 PLOG(ERROR) << getId() << " failed to mount";
158 return -EIO;
159 }
160
161 } else {
162 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700163 return -EIO;
164 }
165
Jeff Sharkeyd24aeda2016-07-15 16:20:22 -0600166 RestoreconRecursive(mPath);
Jeff Sharkey34824122015-06-09 10:59:17 -0700167
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700168 // Verify that common directories are ready to roll
169 if (PrepareDir(mPath + "/app", 0771, AID_SYSTEM, AID_SYSTEM) ||
Paul Crowley14c8c072018-09-18 13:30:21 -0700170 PrepareDir(mPath + "/user", 0711, AID_SYSTEM, AID_SYSTEM) ||
171 PrepareDir(mPath + "/user_de", 0711, AID_SYSTEM, AID_SYSTEM) ||
172 PrepareDir(mPath + "/media", 0770, AID_MEDIA_RW, AID_MEDIA_RW) ||
173 PrepareDir(mPath + "/media/0", 0770, AID_MEDIA_RW, AID_MEDIA_RW) ||
174 PrepareDir(mPath + "/local", 0751, AID_ROOT, AID_ROOT) ||
175 PrepareDir(mPath + "/local/tmp", 0771, AID_SHELL, AID_SHELL)) {
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700176 PLOG(ERROR) << getId() << " failed to prepare";
177 return -EIO;
178 }
179
Zima438b242019-09-25 14:37:38 +0100180 auto vol_manager = VolumeManager::Instance();
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700181 std::string mediaPath(mPath + "/media");
Zima438b242019-09-25 14:37:38 +0100182
183 // Create a new emulated volume stacked above us for all added users, they will automatically
184 // be destroyed during unmount
185 for (userid_t user : vol_manager->getStartedUsers()) {
186 auto vol = std::shared_ptr<VolumeBase>(
187 new EmulatedVolume(mediaPath, mRawDevice, mFsUuid, user));
188 vol->setMountUserId(user);
189 addVolume(vol);
190 vol->create();
191 }
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700192
Jeff Sharkey9c484982015-03-31 10:35:33 -0700193 return OK;
194}
195
196status_t PrivateVolume::doUnmount() {
197 ForceUnmount(mPath);
198
199 if (TEMP_FAILURE_RETRY(rmdir(mPath.c_str()))) {
200 PLOG(ERROR) << getId() << " failed to rmdir mount point " << mPath;
201 }
202
203 return OK;
204}
205
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700206status_t PrivateVolume::doFormat(const std::string& fsType) {
207 std::string resolvedFsType = fsType;
208 if (fsType == "auto") {
209 // For now, assume that all MMC devices are flash-based SD cards, and
210 // give everyone else ext4 because sysfs rotational isn't reliable.
211 if ((major(mRawDevice) == kMajorBlockMmc) && f2fs::IsSupported()) {
212 resolvedFsType = "f2fs";
213 } else {
214 resolvedFsType = "ext4";
215 }
216 LOG(DEBUG) << "Resolved auto to " << resolvedFsType;
217 }
218
219 if (resolvedFsType == "ext4") {
220 // TODO: change reported mountpoint once we have better selinux support
221 if (ext4::Format(mDmDevPath, 0, "/data")) {
222 PLOG(ERROR) << getId() << " failed to format";
223 return -EIO;
224 }
225 } else if (resolvedFsType == "f2fs") {
226 if (f2fs::Format(mDmDevPath)) {
227 PLOG(ERROR) << getId() << " failed to format";
228 return -EIO;
229 }
230 } else {
231 LOG(ERROR) << getId() << " unsupported filesystem " << fsType;
232 return -EINVAL;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700233 }
234
235 return OK;
236}
237
238} // namespace vold
239} // namespace android