blob: 1875b7b23135df18dfb944bef520a0f98f0a037c [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
41using android::base::StringPrintf;
Alistair Delvac6717312020-05-19 15:49:26 -070042using android::vold::IsVirtioBlkDevice;
Jeff Sharkey9c484982015-03-31 10:35:33 -070043
44namespace android {
45namespace vold {
46
Martijn Coenen6c695ef2020-03-11 15:33:22 +010047static const unsigned int kMajorBlockLoop = 7;
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070048static const unsigned int kMajorBlockMmc = 179;
49
Paul Crowley3d98f5d2020-02-07 11:49:09 -080050PrivateVolume::PrivateVolume(dev_t device, const KeyBuffer& keyRaw)
Paul Crowley14c8c072018-09-18 13:30:21 -070051 : VolumeBase(Type::kPrivate), mRawDevice(device), mKeyRaw(keyRaw) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070052 setId(StringPrintf("private:%u,%u", major(device), minor(device)));
53 mRawDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
Jeff Sharkey9c484982015-03-31 10:35:33 -070054}
55
Paul Crowley14c8c072018-09-18 13:30:21 -070056PrivateVolume::~PrivateVolume() {}
Jeff Sharkey9c484982015-03-31 10:35:33 -070057
58status_t PrivateVolume::readMetadata() {
Jeff Sharkey3472e522017-10-06 18:02:53 -060059 status_t res = ReadMetadata(mDmDevPath, &mFsType, &mFsUuid, &mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060060
Jeff Sharkey814e9d32017-09-13 11:49:44 -060061 auto listener = getListener();
62 if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060063
Jeff Sharkey9c484982015-03-31 10:35:33 -070064 return res;
65}
66
67status_t PrivateVolume::doCreate() {
68 if (CreateDeviceNode(mRawDevPath, mRawDevice)) {
69 return -EIO;
70 }
71
72 // Recover from stale vold by tearing down any old mappings
Paul Crowley659b63f2020-02-07 12:15:56 -080073 auto& dm = dm::DeviceMapper::Instance();
Ricky Wai9eb43672020-02-15 01:15:42 +000074 // TODO(b/149396179) there appears to be a race somewhere in the system where trying
75 // to delete the device fails with EBUSY; for now, work around this by retrying.
76 bool ret;
77 int tries = 10;
78 while (tries-- > 0) {
79 ret = dm.DeleteDeviceIfExists(getId());
80 if (ret || errno != EBUSY) {
81 break;
82 }
Paul Crowley659b63f2020-02-07 12:15:56 -080083 PLOG(ERROR) << "Cannot remove dm device " << getId();
Ricky Wai9eb43672020-02-15 01:15:42 +000084 std::this_thread::sleep_for(std::chrono::milliseconds(100));
85 }
86 if (!ret) {
Paul Crowley659b63f2020-02-07 12:15:56 -080087 return -EIO;
88 }
Jeff Sharkey9c484982015-03-31 10:35:33 -070089
90 // TODO: figure out better SELinux labels for private volumes
91
Paul Crowley886e5722020-02-07 12:51:56 -080092 if (!setup_ext_volume(getId(), mRawDevPath, mKeyRaw, &mDmDevPath)) {
93 LOG(ERROR) << getId() << " failed to setup metadata encryption";
Jeff Sharkey9c484982015-03-31 10:35:33 -070094 return -EIO;
95 }
96
97 return OK;
98}
99
100status_t PrivateVolume::doDestroy() {
Paul Crowley659b63f2020-02-07 12:15:56 -0800101 auto& dm = dm::DeviceMapper::Instance();
Ricky Wai9eb43672020-02-15 01:15:42 +0000102 // TODO(b/149396179) there appears to be a race somewhere in the system where trying
103 // to delete the device fails with EBUSY; for now, work around this by retrying.
104 bool ret;
105 int tries = 10;
106 while (tries-- > 0) {
107 ret = dm.DeleteDevice(getId());
108 if (ret || errno != EBUSY) {
109 break;
110 }
Paul Crowley659b63f2020-02-07 12:15:56 -0800111 PLOG(ERROR) << "Cannot remove dm device " << getId();
Ricky Wai9eb43672020-02-15 01:15:42 +0000112 std::this_thread::sleep_for(std::chrono::milliseconds(100));
113 }
114 if (!ret) {
Paul Crowley659b63f2020-02-07 12:15:56 -0800115 return -EIO;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700116 }
117 return DestroyDeviceNode(mRawDevPath);
118}
119
120status_t PrivateVolume::doMount() {
121 if (readMetadata()) {
122 LOG(ERROR) << getId() << " failed to read metadata";
123 return -EIO;
124 }
125
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700126 mPath = StringPrintf("/mnt/expand/%s", mFsUuid.c_str());
127 setPath(mPath);
128
129 if (PrepareDir(mPath, 0700, AID_ROOT, AID_ROOT)) {
130 PLOG(ERROR) << getId() << " failed to create mount point " << mPath;
131 return -EIO;
132 }
133
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700134 if (mFsType == "ext4") {
135 int res = ext4::Check(mDmDevPath, mPath);
136 if (res == 0 || res == 1) {
137 LOG(DEBUG) << getId() << " passed filesystem check";
138 } else {
139 PLOG(ERROR) << getId() << " failed filesystem check";
140 return -EIO;
141 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700142
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700143 if (ext4::Mount(mDmDevPath, mPath, false, false, true)) {
144 PLOG(ERROR) << getId() << " failed to mount";
145 return -EIO;
146 }
147
148 } else if (mFsType == "f2fs") {
149 int res = f2fs::Check(mDmDevPath);
150 if (res == 0) {
151 LOG(DEBUG) << getId() << " passed filesystem check";
152 } else {
153 PLOG(ERROR) << getId() << " failed filesystem check";
154 return -EIO;
155 }
156
157 if (f2fs::Mount(mDmDevPath, mPath)) {
158 PLOG(ERROR) << getId() << " failed to mount";
159 return -EIO;
160 }
161
162 } else {
163 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700164 return -EIO;
165 }
166
Jeff Sharkeyd24aeda2016-07-15 16:20:22 -0600167 RestoreconRecursive(mPath);
Jeff Sharkey34824122015-06-09 10:59:17 -0700168
Daniel Rosenberg3bb86642020-08-12 18:31:43 -0700169 int attrs = 0;
170 if (!IsSdcardfsUsed()) attrs = FS_CASEFOLD_FL;
171
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700172 // Verify that common directories are ready to roll
173 if (PrepareDir(mPath + "/app", 0771, AID_SYSTEM, AID_SYSTEM) ||
Paul Crowley14c8c072018-09-18 13:30:21 -0700174 PrepareDir(mPath + "/user", 0711, AID_SYSTEM, AID_SYSTEM) ||
175 PrepareDir(mPath + "/user_de", 0711, AID_SYSTEM, AID_SYSTEM) ||
Daniel Rosenberg3bb86642020-08-12 18:31:43 -0700176 PrepareDir(mPath + "/media", 0770, AID_MEDIA_RW, AID_MEDIA_RW, attrs) ||
Paul Crowley14c8c072018-09-18 13:30:21 -0700177 PrepareDir(mPath + "/media/0", 0770, AID_MEDIA_RW, AID_MEDIA_RW) ||
178 PrepareDir(mPath + "/local", 0751, AID_ROOT, AID_ROOT) ||
179 PrepareDir(mPath + "/local/tmp", 0771, AID_SHELL, AID_SHELL)) {
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700180 PLOG(ERROR) << getId() << " failed to prepare";
181 return -EIO;
182 }
183
Martijn Coenen5ec86582020-05-04 14:57:35 +0200184 return OK;
185}
186
187void PrivateVolume::doPostMount() {
Zima438b242019-09-25 14:37:38 +0100188 auto vol_manager = VolumeManager::Instance();
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700189 std::string mediaPath(mPath + "/media");
Zima438b242019-09-25 14:37:38 +0100190
191 // Create a new emulated volume stacked above us for all added users, they will automatically
192 // be destroyed during unmount
193 for (userid_t user : vol_manager->getStartedUsers()) {
194 auto vol = std::shared_ptr<VolumeBase>(
195 new EmulatedVolume(mediaPath, mRawDevice, mFsUuid, user));
196 vol->setMountUserId(user);
197 addVolume(vol);
198 vol->create();
199 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700200}
201
202status_t PrivateVolume::doUnmount() {
203 ForceUnmount(mPath);
204
205 if (TEMP_FAILURE_RETRY(rmdir(mPath.c_str()))) {
206 PLOG(ERROR) << getId() << " failed to rmdir mount point " << mPath;
207 }
208
209 return OK;
210}
211
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700212status_t PrivateVolume::doFormat(const std::string& fsType) {
213 std::string resolvedFsType = fsType;
214 if (fsType == "auto") {
215 // For now, assume that all MMC devices are flash-based SD cards, and
216 // give everyone else ext4 because sysfs rotational isn't reliable.
Alistair Delvac6717312020-05-19 15:49:26 -0700217 // Additionally, prefer f2fs for loop-based devices
218 if ((major(mRawDevice) == kMajorBlockMmc ||
219 major(mRawDevice) == kMajorBlockLoop ||
220 IsVirtioBlkDevice(major(mRawDevice))) && f2fs::IsSupported()) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700221 resolvedFsType = "f2fs";
222 } else {
223 resolvedFsType = "ext4";
224 }
225 LOG(DEBUG) << "Resolved auto to " << resolvedFsType;
226 }
227
228 if (resolvedFsType == "ext4") {
229 // TODO: change reported mountpoint once we have better selinux support
230 if (ext4::Format(mDmDevPath, 0, "/data")) {
231 PLOG(ERROR) << getId() << " failed to format";
232 return -EIO;
233 }
234 } else if (resolvedFsType == "f2fs") {
235 if (f2fs::Format(mDmDevPath)) {
236 PLOG(ERROR) << getId() << " failed to format";
237 return -EIO;
238 }
239 } else {
240 LOG(ERROR) << getId() << " unsupported filesystem " << fsType;
241 return -EINVAL;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700242 }
243
244 return OK;
245}
246
247} // namespace vold
248} // namespace android