blob: 3334cbb2623921157725dff7cabc8a49b709a3a2 [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 Hongal8ee3fdb2018-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;
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070051static const unsigned int kMajorBlockMmc = 179;
52
Paul Crowley3d98f5d2020-02-07 11:49:09 -080053PrivateVolume::PrivateVolume(dev_t device, const KeyBuffer& keyRaw)
Paul Crowley14c8c072018-09-18 13:30:21 -070054 : VolumeBase(Type::kPrivate), mRawDevice(device), mKeyRaw(keyRaw) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070055 setId(StringPrintf("private:%u,%u", major(device), minor(device)));
56 mRawDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
Jeff Sharkey9c484982015-03-31 10:35:33 -070057}
58
Paul Crowley14c8c072018-09-18 13:30:21 -070059PrivateVolume::~PrivateVolume() {}
Jeff Sharkey9c484982015-03-31 10:35:33 -070060
61status_t PrivateVolume::readMetadata() {
Jeff Sharkey3472e522017-10-06 18:02:53 -060062 status_t res = ReadMetadata(mDmDevPath, &mFsType, &mFsUuid, &mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060063
Jeff Sharkey814e9d32017-09-13 11:49:44 -060064 auto listener = getListener();
65 if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060066
Jeff Sharkey9c484982015-03-31 10:35:33 -070067 return res;
68}
69
70status_t PrivateVolume::doCreate() {
71 if (CreateDeviceNode(mRawDevPath, mRawDevice)) {
72 return -EIO;
73 }
74
75 // Recover from stale vold by tearing down any old mappings
Paul Crowley659b63f2020-02-07 12:15:56 -080076 auto& dm = dm::DeviceMapper::Instance();
Ricky Wai9eb43672020-02-15 01:15:42 +000077 // TODO(b/149396179) there appears to be a race somewhere in the system where trying
78 // to delete the device fails with EBUSY; for now, work around this by retrying.
79 bool ret;
80 int tries = 10;
81 while (tries-- > 0) {
82 ret = dm.DeleteDeviceIfExists(getId());
83 if (ret || errno != EBUSY) {
84 break;
85 }
Paul Crowley659b63f2020-02-07 12:15:56 -080086 PLOG(ERROR) << "Cannot remove dm device " << getId();
Ricky Wai9eb43672020-02-15 01:15:42 +000087 std::this_thread::sleep_for(std::chrono::milliseconds(100));
88 }
89 if (!ret) {
Paul Crowley659b63f2020-02-07 12:15:56 -080090 return -EIO;
91 }
Jeff Sharkey9c484982015-03-31 10:35:33 -070092
93 // TODO: figure out better SELinux labels for private volumes
94
Paul Crowley886e5722020-02-07 12:51:56 -080095 if (!setup_ext_volume(getId(), mRawDevPath, mKeyRaw, &mDmDevPath)) {
96 LOG(ERROR) << getId() << " failed to setup metadata encryption";
Jeff Sharkey9c484982015-03-31 10:35:33 -070097 return -EIO;
98 }
99
Shivaprasad Hongal8ee3fdb2018-10-24 17:49:39 -0700100 int fd = 0;
101 int retries = RETRY_MOUNT_ATTEMPTS;
102 while ((fd = open(mDmDevPath.c_str(), O_WRONLY|O_CLOEXEC)) < 0) {
103 if (retries > 0) {
104 retries--;
105 PLOG(ERROR) << "Error opening crypto_blkdev " << mDmDevPath
106 << " for private volume. err=" << errno
107 << "(" << strerror(errno) << "), retrying for the "
108 << RETRY_MOUNT_ATTEMPTS - retries << " time";
109 sleep(RETRY_MOUNT_DELAY_SECONDS);
110 } else {
111 PLOG(ERROR) << "Error opening crypto_blkdev " << mDmDevPath
112 << " for private volume. err=" << errno
113 << "(" << strerror(errno) << "), retried "
114 << RETRY_MOUNT_ATTEMPTS << " times";
115 close(fd);
116 return -EIO;
117 }
118 }
119 close(fd);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700120 return OK;
121}
122
123status_t PrivateVolume::doDestroy() {
Paul Crowley659b63f2020-02-07 12:15:56 -0800124 auto& dm = dm::DeviceMapper::Instance();
Ricky Wai9eb43672020-02-15 01:15:42 +0000125 // TODO(b/149396179) there appears to be a race somewhere in the system where trying
126 // to delete the device fails with EBUSY; for now, work around this by retrying.
127 bool ret;
128 int tries = 10;
129 while (tries-- > 0) {
130 ret = dm.DeleteDevice(getId());
131 if (ret || errno != EBUSY) {
132 break;
133 }
Paul Crowley659b63f2020-02-07 12:15:56 -0800134 PLOG(ERROR) << "Cannot remove dm device " << getId();
Ricky Wai9eb43672020-02-15 01:15:42 +0000135 std::this_thread::sleep_for(std::chrono::milliseconds(100));
136 }
137 if (!ret) {
Paul Crowley659b63f2020-02-07 12:15:56 -0800138 return -EIO;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700139 }
140 return DestroyDeviceNode(mRawDevPath);
141}
142
143status_t PrivateVolume::doMount() {
144 if (readMetadata()) {
145 LOG(ERROR) << getId() << " failed to read metadata";
146 return -EIO;
147 }
148
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700149 mPath = StringPrintf("/mnt/expand/%s", mFsUuid.c_str());
150 setPath(mPath);
151
152 if (PrepareDir(mPath, 0700, AID_ROOT, AID_ROOT)) {
153 PLOG(ERROR) << getId() << " failed to create mount point " << mPath;
154 return -EIO;
155 }
156
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700157 if (mFsType == "ext4") {
Alexander Martinz72268882023-09-28 14:43:16 +0200158 int res = ext4::Check(mDmDevPath, mPath);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700159 if (res == 0 || res == 1) {
160 LOG(DEBUG) << getId() << " passed filesystem check";
161 } else {
162 PLOG(ERROR) << getId() << " failed filesystem check";
163 return -EIO;
164 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700165
Alexander Martinz72268882023-09-28 14:43:16 +0200166 if (ext4::Mount(mDmDevPath, mPath, false, false, true)) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700167 PLOG(ERROR) << getId() << " failed to mount";
168 return -EIO;
169 }
170
171 } else if (mFsType == "f2fs") {
Alexander Martinz72268882023-09-28 14:43:16 +0200172 int res = f2fs::Check(mDmDevPath);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700173 if (res == 0) {
174 LOG(DEBUG) << getId() << " passed filesystem check";
175 } else {
176 PLOG(ERROR) << getId() << " failed filesystem check";
177 return -EIO;
178 }
179
Alexander Martinz72268882023-09-28 14:43:16 +0200180 if (f2fs::Mount(mDmDevPath, mPath)) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700181 PLOG(ERROR) << getId() << " failed to mount";
182 return -EIO;
183 }
184
185 } else {
186 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700187 return -EIO;
188 }
189
Jeff Sharkeyd24aeda2016-07-15 16:20:22 -0600190 RestoreconRecursive(mPath);
Jeff Sharkey34824122015-06-09 10:59:17 -0700191
Daniel Rosenberg3bb86642020-08-12 18:31:43 -0700192 int attrs = 0;
193 if (!IsSdcardfsUsed()) attrs = FS_CASEFOLD_FL;
194
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700195 // Verify that common directories are ready to roll
196 if (PrepareDir(mPath + "/app", 0771, AID_SYSTEM, AID_SYSTEM) ||
Paul Crowley14c8c072018-09-18 13:30:21 -0700197 PrepareDir(mPath + "/user", 0711, AID_SYSTEM, AID_SYSTEM) ||
198 PrepareDir(mPath + "/user_de", 0711, AID_SYSTEM, AID_SYSTEM) ||
Mohammad Samiul Islamb4595912022-03-07 20:27:06 +0000199 PrepareDir(mPath + "/misc_ce", 0711, AID_SYSTEM, AID_SYSTEM) ||
200 PrepareDir(mPath + "/misc_de", 0711, AID_SYSTEM, AID_SYSTEM) ||
Daniel Rosenberg3bb86642020-08-12 18:31:43 -0700201 PrepareDir(mPath + "/media", 0770, AID_MEDIA_RW, AID_MEDIA_RW, attrs) ||
Paul Crowley14c8c072018-09-18 13:30:21 -0700202 PrepareDir(mPath + "/media/0", 0770, AID_MEDIA_RW, AID_MEDIA_RW) ||
203 PrepareDir(mPath + "/local", 0751, AID_ROOT, AID_ROOT) ||
204 PrepareDir(mPath + "/local/tmp", 0771, AID_SHELL, AID_SHELL)) {
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700205 PLOG(ERROR) << getId() << " failed to prepare";
206 return -EIO;
207 }
208
Martijn Coenen5ec86582020-05-04 14:57:35 +0200209 return OK;
210}
211
212void PrivateVolume::doPostMount() {
Zima438b242019-09-25 14:37:38 +0100213 auto vol_manager = VolumeManager::Instance();
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700214 std::string mediaPath(mPath + "/media");
Zima438b242019-09-25 14:37:38 +0100215
216 // Create a new emulated volume stacked above us for all added users, they will automatically
217 // be destroyed during unmount
218 for (userid_t user : vol_manager->getStartedUsers()) {
219 auto vol = std::shared_ptr<VolumeBase>(
220 new EmulatedVolume(mediaPath, mRawDevice, mFsUuid, user));
221 vol->setMountUserId(user);
222 addVolume(vol);
223 vol->create();
224 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700225}
226
227status_t PrivateVolume::doUnmount() {
228 ForceUnmount(mPath);
229
230 if (TEMP_FAILURE_RETRY(rmdir(mPath.c_str()))) {
231 PLOG(ERROR) << getId() << " failed to rmdir mount point " << mPath;
232 }
233
234 return OK;
235}
236
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700237status_t PrivateVolume::doFormat(const std::string& fsType) {
238 std::string resolvedFsType = fsType;
239 if (fsType == "auto") {
240 // For now, assume that all MMC devices are flash-based SD cards, and
241 // give everyone else ext4 because sysfs rotational isn't reliable.
Alistair Delvac6717312020-05-19 15:49:26 -0700242 // Additionally, prefer f2fs for loop-based devices
243 if ((major(mRawDevice) == kMajorBlockMmc ||
244 major(mRawDevice) == kMajorBlockLoop ||
245 IsVirtioBlkDevice(major(mRawDevice))) && f2fs::IsSupported()) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700246 resolvedFsType = "f2fs";
247 } else {
248 resolvedFsType = "ext4";
249 }
250 LOG(DEBUG) << "Resolved auto to " << resolvedFsType;
251 }
252
253 if (resolvedFsType == "ext4") {
254 // TODO: change reported mountpoint once we have better selinux support
255 if (ext4::Format(mDmDevPath, 0, "/data")) {
256 PLOG(ERROR) << getId() << " failed to format";
257 return -EIO;
258 }
259 } else if (resolvedFsType == "f2fs") {
260 if (f2fs::Format(mDmDevPath)) {
261 PLOG(ERROR) << getId() << " failed to format";
262 return -EIO;
263 }
264 } else {
265 LOG(ERROR) << getId() << " unsupported filesystem " << fsType;
266 return -EINVAL;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700267 }
268
269 return OK;
270}
271
272} // namespace vold
273} // namespace android