blob: 56111049639ff1f2b024c1cf1df9c30cbb3403e1 [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"
Paul Crowley886e5722020-02-07 12:51:56 -080020#include "VolumeEncryption.h"
Jeff Sharkey9c484982015-03-31 10:35:33 -070021#include "VolumeManager.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
Shivaprasad Hongala36b1b62018-10-24 17:49:39 -070041#define RETRY_MOUNT_ATTEMPTS 10
42#define RETRY_MOUNT_DELAY_SECONDS 1
43
Jeff Sharkey9c484982015-03-31 10:35:33 -070044using android::base::StringPrintf;
Alistair Delvac6717312020-05-19 15:49:26 -070045using android::vold::IsVirtioBlkDevice;
Jeff Sharkey9c484982015-03-31 10:35:33 -070046
47namespace android {
48namespace vold {
49
Martijn Coenen6c695ef2020-03-11 15:33:22 +010050static const unsigned int kMajorBlockLoop = 7;
Lyon Wang73670aa2024-11-29 10:20:23 +080051static const unsigned int kMajorBlockHdd = 8;
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070052static const unsigned int kMajorBlockMmc = 179;
53
Paul Crowley3d98f5d2020-02-07 11:49:09 -080054PrivateVolume::PrivateVolume(dev_t device, const KeyBuffer& keyRaw)
Paul Crowley14c8c072018-09-18 13:30:21 -070055 : VolumeBase(Type::kPrivate), mRawDevice(device), mKeyRaw(keyRaw) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070056 setId(StringPrintf("private:%u,%u", major(device), minor(device)));
57 mRawDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
Jeff Sharkey9c484982015-03-31 10:35:33 -070058}
59
Paul Crowley14c8c072018-09-18 13:30:21 -070060PrivateVolume::~PrivateVolume() {}
Jeff Sharkey9c484982015-03-31 10:35:33 -070061
62status_t PrivateVolume::readMetadata() {
Jeff Sharkey3472e522017-10-06 18:02:53 -060063 status_t res = ReadMetadata(mDmDevPath, &mFsType, &mFsUuid, &mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060064
Jeff Sharkey814e9d32017-09-13 11:49:44 -060065 auto listener = getListener();
66 if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060067
Jeff Sharkey9c484982015-03-31 10:35:33 -070068 return res;
69}
70
71status_t PrivateVolume::doCreate() {
72 if (CreateDeviceNode(mRawDevPath, mRawDevice)) {
73 return -EIO;
74 }
75
76 // Recover from stale vold by tearing down any old mappings
Paul Crowley659b63f2020-02-07 12:15:56 -080077 auto& dm = dm::DeviceMapper::Instance();
Ricky Wai9eb43672020-02-15 01:15:42 +000078 // TODO(b/149396179) there appears to be a race somewhere in the system where trying
79 // to delete the device fails with EBUSY; for now, work around this by retrying.
80 bool ret;
81 int tries = 10;
82 while (tries-- > 0) {
83 ret = dm.DeleteDeviceIfExists(getId());
84 if (ret || errno != EBUSY) {
85 break;
86 }
Paul Crowley659b63f2020-02-07 12:15:56 -080087 PLOG(ERROR) << "Cannot remove dm device " << getId();
Ricky Wai9eb43672020-02-15 01:15:42 +000088 std::this_thread::sleep_for(std::chrono::milliseconds(100));
89 }
90 if (!ret) {
Paul Crowley659b63f2020-02-07 12:15:56 -080091 return -EIO;
92 }
Jeff Sharkey9c484982015-03-31 10:35:33 -070093
94 // TODO: figure out better SELinux labels for private volumes
95
Paul Crowley886e5722020-02-07 12:51:56 -080096 if (!setup_ext_volume(getId(), mRawDevPath, mKeyRaw, &mDmDevPath)) {
97 LOG(ERROR) << getId() << " failed to setup metadata encryption";
Jeff Sharkey9c484982015-03-31 10:35:33 -070098 return -EIO;
99 }
100
Shivaprasad Hongala36b1b62018-10-24 17:49:39 -0700101 int fd = 0;
102 int retries = RETRY_MOUNT_ATTEMPTS;
103 while ((fd = open(mDmDevPath.c_str(), O_WRONLY|O_CLOEXEC)) < 0) {
104 if (retries > 0) {
105 retries--;
106 PLOG(ERROR) << "Error opening crypto_blkdev " << mDmDevPath
107 << " for private volume. err=" << errno
108 << "(" << strerror(errno) << "), retrying for the "
109 << RETRY_MOUNT_ATTEMPTS - retries << " time";
110 sleep(RETRY_MOUNT_DELAY_SECONDS);
111 } else {
112 PLOG(ERROR) << "Error opening crypto_blkdev " << mDmDevPath
113 << " for private volume. err=" << errno
114 << "(" << strerror(errno) << "), retried "
115 << RETRY_MOUNT_ATTEMPTS << " times";
116 close(fd);
117 return -EIO;
118 }
119 }
120 close(fd);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700121 return OK;
122}
123
124status_t PrivateVolume::doDestroy() {
Paul Crowley659b63f2020-02-07 12:15:56 -0800125 auto& dm = dm::DeviceMapper::Instance();
Ricky Wai9eb43672020-02-15 01:15:42 +0000126 // TODO(b/149396179) there appears to be a race somewhere in the system where trying
127 // to delete the device fails with EBUSY; for now, work around this by retrying.
128 bool ret;
129 int tries = 10;
130 while (tries-- > 0) {
131 ret = dm.DeleteDevice(getId());
132 if (ret || errno != EBUSY) {
133 break;
134 }
Paul Crowley659b63f2020-02-07 12:15:56 -0800135 PLOG(ERROR) << "Cannot remove dm device " << getId();
Ricky Wai9eb43672020-02-15 01:15:42 +0000136 std::this_thread::sleep_for(std::chrono::milliseconds(100));
137 }
138 if (!ret) {
Paul Crowley659b63f2020-02-07 12:15:56 -0800139 return -EIO;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700140 }
141 return DestroyDeviceNode(mRawDevPath);
142}
143
144status_t PrivateVolume::doMount() {
145 if (readMetadata()) {
146 LOG(ERROR) << getId() << " failed to read metadata";
147 return -EIO;
148 }
149
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700150 mPath = StringPrintf("/mnt/expand/%s", mFsUuid.c_str());
151 setPath(mPath);
152
153 if (PrepareDir(mPath, 0700, AID_ROOT, AID_ROOT)) {
154 PLOG(ERROR) << getId() << " failed to create mount point " << mPath;
155 return -EIO;
156 }
157
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700158 if (mFsType == "ext4") {
Michael Bestas4c686912015-12-06 23:53:55 +0200159 int res = ext4::Check(mDmDevPath, mPath, true);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700160 if (res == 0 || res == 1) {
161 LOG(DEBUG) << getId() << " passed filesystem check";
162 } else {
163 PLOG(ERROR) << getId() << " failed filesystem check";
164 return -EIO;
165 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700166
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700167 if (ext4::Mount(mDmDevPath, mPath, false, false, true)) {
168 PLOG(ERROR) << getId() << " failed to mount";
169 return -EIO;
170 }
171
172 } else if (mFsType == "f2fs") {
Michael Bestas4c686912015-12-06 23:53:55 +0200173 int res = f2fs::Check(mDmDevPath, true);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700174 if (res == 0) {
175 LOG(DEBUG) << getId() << " passed filesystem check";
176 } else {
177 PLOG(ERROR) << getId() << " failed filesystem check";
178 return -EIO;
179 }
180
181 if (f2fs::Mount(mDmDevPath, mPath)) {
182 PLOG(ERROR) << getId() << " failed to mount";
183 return -EIO;
184 }
185
186 } else {
187 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700188 return -EIO;
189 }
190
Jeff Sharkeyd24aeda2016-07-15 16:20:22 -0600191 RestoreconRecursive(mPath);
Jeff Sharkey34824122015-06-09 10:59:17 -0700192
Daniel Rosenberg3bb86642020-08-12 18:31:43 -0700193 int attrs = 0;
194 if (!IsSdcardfsUsed()) attrs = FS_CASEFOLD_FL;
195
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700196 // Verify that common directories are ready to roll
197 if (PrepareDir(mPath + "/app", 0771, AID_SYSTEM, AID_SYSTEM) ||
Eric Biggers714b99d2023-06-08 21:33:49 +0000198 PrepareDir(mPath + "/user", 0511, AID_SYSTEM, AID_SYSTEM) ||
199 PrepareDir(mPath + "/user_de", 0511, AID_SYSTEM, AID_SYSTEM) ||
200 PrepareDir(mPath + "/misc_ce", 0511, AID_SYSTEM, AID_SYSTEM) ||
201 PrepareDir(mPath + "/misc_de", 0511, AID_SYSTEM, AID_SYSTEM) ||
202 PrepareDir(mPath + "/media", 0550, AID_MEDIA_RW, AID_MEDIA_RW, attrs) ||
Paul Crowley14c8c072018-09-18 13:30:21 -0700203 PrepareDir(mPath + "/media/0", 0770, AID_MEDIA_RW, AID_MEDIA_RW) ||
204 PrepareDir(mPath + "/local", 0751, AID_ROOT, AID_ROOT) ||
205 PrepareDir(mPath + "/local/tmp", 0771, AID_SHELL, AID_SHELL)) {
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700206 PLOG(ERROR) << getId() << " failed to prepare";
207 return -EIO;
208 }
209
Martijn Coenen5ec86582020-05-04 14:57:35 +0200210 return OK;
211}
212
213void PrivateVolume::doPostMount() {
Zima438b242019-09-25 14:37:38 +0100214 auto vol_manager = VolumeManager::Instance();
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700215 std::string mediaPath(mPath + "/media");
Zima438b242019-09-25 14:37:38 +0100216
217 // Create a new emulated volume stacked above us for all added users, they will automatically
218 // be destroyed during unmount
219 for (userid_t user : vol_manager->getStartedUsers()) {
220 auto vol = std::shared_ptr<VolumeBase>(
221 new EmulatedVolume(mediaPath, mRawDevice, mFsUuid, user));
222 vol->setMountUserId(user);
223 addVolume(vol);
224 vol->create();
225 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700226}
227
228status_t PrivateVolume::doUnmount() {
229 ForceUnmount(mPath);
230
231 if (TEMP_FAILURE_RETRY(rmdir(mPath.c_str()))) {
232 PLOG(ERROR) << getId() << " failed to rmdir mount point " << mPath;
233 }
234
235 return OK;
236}
237
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700238status_t PrivateVolume::doFormat(const std::string& fsType) {
239 std::string resolvedFsType = fsType;
240 if (fsType == "auto") {
241 // For now, assume that all MMC devices are flash-based SD cards, and
242 // give everyone else ext4 because sysfs rotational isn't reliable.
Alistair Delvac6717312020-05-19 15:49:26 -0700243 // Additionally, prefer f2fs for loop-based devices
244 if ((major(mRawDevice) == kMajorBlockMmc ||
Lyon Wang73670aa2024-11-29 10:20:23 +0800245 major(mRawDevice) == kMajorBlockHdd ||
Alistair Delvac6717312020-05-19 15:49:26 -0700246 major(mRawDevice) == kMajorBlockLoop ||
247 IsVirtioBlkDevice(major(mRawDevice))) && f2fs::IsSupported()) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700248 resolvedFsType = "f2fs";
249 } else {
250 resolvedFsType = "ext4";
251 }
252 LOG(DEBUG) << "Resolved auto to " << resolvedFsType;
253 }
254
255 if (resolvedFsType == "ext4") {
256 // TODO: change reported mountpoint once we have better selinux support
257 if (ext4::Format(mDmDevPath, 0, "/data")) {
258 PLOG(ERROR) << getId() << " failed to format";
259 return -EIO;
260 }
261 } else if (resolvedFsType == "f2fs") {
Daeho Jeong0c841552024-10-21 14:05:31 -0700262 if (f2fs::Format(mDmDevPath, false, {}, {})) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700263 PLOG(ERROR) << getId() << " failed to format";
264 return -EIO;
265 }
266 } else {
267 LOG(ERROR) << getId() << " unsupported filesystem " << fsType;
268 return -EINVAL;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700269 }
270
271 return OK;
272}
273
274} // namespace vold
275} // namespace android