blob: 0f06c1fbbe8e8213d2286f5bfad0d6b2d5ef11ce [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;
Lyon Wang73670aa2024-11-29 10:20:23 +080048static const unsigned int kMajorBlockHdd = 8;
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070049static const unsigned int kMajorBlockMmc = 179;
50
Paul Crowley3d98f5d2020-02-07 11:49:09 -080051PrivateVolume::PrivateVolume(dev_t device, const KeyBuffer& keyRaw)
Paul Crowley14c8c072018-09-18 13:30:21 -070052 : VolumeBase(Type::kPrivate), mRawDevice(device), mKeyRaw(keyRaw) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070053 setId(StringPrintf("private:%u,%u", major(device), minor(device)));
54 mRawDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
Jeff Sharkey9c484982015-03-31 10:35:33 -070055}
56
Paul Crowley14c8c072018-09-18 13:30:21 -070057PrivateVolume::~PrivateVolume() {}
Jeff Sharkey9c484982015-03-31 10:35:33 -070058
59status_t PrivateVolume::readMetadata() {
Jeff Sharkey3472e522017-10-06 18:02:53 -060060 status_t res = ReadMetadata(mDmDevPath, &mFsType, &mFsUuid, &mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060061
Jeff Sharkey814e9d32017-09-13 11:49:44 -060062 auto listener = getListener();
63 if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060064
Jeff Sharkey9c484982015-03-31 10:35:33 -070065 return res;
66}
67
68status_t PrivateVolume::doCreate() {
69 if (CreateDeviceNode(mRawDevPath, mRawDevice)) {
70 return -EIO;
71 }
72
73 // Recover from stale vold by tearing down any old mappings
Paul Crowley659b63f2020-02-07 12:15:56 -080074 auto& dm = dm::DeviceMapper::Instance();
Ricky Wai9eb43672020-02-15 01:15:42 +000075 // TODO(b/149396179) there appears to be a race somewhere in the system where trying
76 // to delete the device fails with EBUSY; for now, work around this by retrying.
77 bool ret;
78 int tries = 10;
79 while (tries-- > 0) {
80 ret = dm.DeleteDeviceIfExists(getId());
81 if (ret || errno != EBUSY) {
82 break;
83 }
Paul Crowley659b63f2020-02-07 12:15:56 -080084 PLOG(ERROR) << "Cannot remove dm device " << getId();
Ricky Wai9eb43672020-02-15 01:15:42 +000085 std::this_thread::sleep_for(std::chrono::milliseconds(100));
86 }
87 if (!ret) {
Paul Crowley659b63f2020-02-07 12:15:56 -080088 return -EIO;
89 }
Jeff Sharkey9c484982015-03-31 10:35:33 -070090
91 // TODO: figure out better SELinux labels for private volumes
92
Paul Crowley886e5722020-02-07 12:51:56 -080093 if (!setup_ext_volume(getId(), mRawDevPath, mKeyRaw, &mDmDevPath)) {
94 LOG(ERROR) << getId() << " failed to setup metadata encryption";
Jeff Sharkey9c484982015-03-31 10:35:33 -070095 return -EIO;
96 }
97
98 return OK;
99}
100
101status_t PrivateVolume::doDestroy() {
Paul Crowley659b63f2020-02-07 12:15:56 -0800102 auto& dm = dm::DeviceMapper::Instance();
Ricky Wai9eb43672020-02-15 01:15:42 +0000103 // TODO(b/149396179) there appears to be a race somewhere in the system where trying
104 // to delete the device fails with EBUSY; for now, work around this by retrying.
105 bool ret;
106 int tries = 10;
107 while (tries-- > 0) {
108 ret = dm.DeleteDevice(getId());
109 if (ret || errno != EBUSY) {
110 break;
111 }
Paul Crowley659b63f2020-02-07 12:15:56 -0800112 PLOG(ERROR) << "Cannot remove dm device " << getId();
Ricky Wai9eb43672020-02-15 01:15:42 +0000113 std::this_thread::sleep_for(std::chrono::milliseconds(100));
114 }
115 if (!ret) {
Paul Crowley659b63f2020-02-07 12:15:56 -0800116 return -EIO;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700117 }
118 return DestroyDeviceNode(mRawDevPath);
119}
120
121status_t PrivateVolume::doMount() {
122 if (readMetadata()) {
123 LOG(ERROR) << getId() << " failed to read metadata";
124 return -EIO;
125 }
126
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700127 mPath = StringPrintf("/mnt/expand/%s", mFsUuid.c_str());
128 setPath(mPath);
129
130 if (PrepareDir(mPath, 0700, AID_ROOT, AID_ROOT)) {
131 PLOG(ERROR) << getId() << " failed to create mount point " << mPath;
132 return -EIO;
133 }
134
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700135 if (mFsType == "ext4") {
136 int res = ext4::Check(mDmDevPath, mPath);
137 if (res == 0 || res == 1) {
138 LOG(DEBUG) << getId() << " passed filesystem check";
139 } else {
140 PLOG(ERROR) << getId() << " failed filesystem check";
141 return -EIO;
142 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700143
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700144 if (ext4::Mount(mDmDevPath, mPath, false, false, true)) {
145 PLOG(ERROR) << getId() << " failed to mount";
146 return -EIO;
147 }
148
149 } else if (mFsType == "f2fs") {
150 int res = f2fs::Check(mDmDevPath);
151 if (res == 0) {
152 LOG(DEBUG) << getId() << " passed filesystem check";
153 } else {
154 PLOG(ERROR) << getId() << " failed filesystem check";
155 return -EIO;
156 }
157
158 if (f2fs::Mount(mDmDevPath, mPath)) {
159 PLOG(ERROR) << getId() << " failed to mount";
160 return -EIO;
161 }
162
163 } else {
164 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700165 return -EIO;
166 }
167
Jeff Sharkeyd24aeda2016-07-15 16:20:22 -0600168 RestoreconRecursive(mPath);
Jeff Sharkey34824122015-06-09 10:59:17 -0700169
Daniel Rosenberg3bb86642020-08-12 18:31:43 -0700170 int attrs = 0;
171 if (!IsSdcardfsUsed()) attrs = FS_CASEFOLD_FL;
172
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700173 // Verify that common directories are ready to roll
174 if (PrepareDir(mPath + "/app", 0771, AID_SYSTEM, AID_SYSTEM) ||
Eric Biggers714b99d2023-06-08 21:33:49 +0000175 PrepareDir(mPath + "/user", 0511, AID_SYSTEM, AID_SYSTEM) ||
176 PrepareDir(mPath + "/user_de", 0511, AID_SYSTEM, AID_SYSTEM) ||
177 PrepareDir(mPath + "/misc_ce", 0511, AID_SYSTEM, AID_SYSTEM) ||
178 PrepareDir(mPath + "/misc_de", 0511, AID_SYSTEM, AID_SYSTEM) ||
179 PrepareDir(mPath + "/media", 0550, AID_MEDIA_RW, AID_MEDIA_RW, attrs) ||
Paul Crowley14c8c072018-09-18 13:30:21 -0700180 PrepareDir(mPath + "/media/0", 0770, AID_MEDIA_RW, AID_MEDIA_RW) ||
181 PrepareDir(mPath + "/local", 0751, AID_ROOT, AID_ROOT) ||
182 PrepareDir(mPath + "/local/tmp", 0771, AID_SHELL, AID_SHELL)) {
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700183 PLOG(ERROR) << getId() << " failed to prepare";
184 return -EIO;
185 }
186
Martijn Coenen5ec86582020-05-04 14:57:35 +0200187 return OK;
188}
189
190void PrivateVolume::doPostMount() {
Zima438b242019-09-25 14:37:38 +0100191 auto vol_manager = VolumeManager::Instance();
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700192 std::string mediaPath(mPath + "/media");
Zima438b242019-09-25 14:37:38 +0100193
194 // Create a new emulated volume stacked above us for all added users, they will automatically
195 // be destroyed during unmount
196 for (userid_t user : vol_manager->getStartedUsers()) {
197 auto vol = std::shared_ptr<VolumeBase>(
198 new EmulatedVolume(mediaPath, mRawDevice, mFsUuid, user));
199 vol->setMountUserId(user);
200 addVolume(vol);
201 vol->create();
202 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700203}
204
205status_t PrivateVolume::doUnmount() {
206 ForceUnmount(mPath);
207
208 if (TEMP_FAILURE_RETRY(rmdir(mPath.c_str()))) {
209 PLOG(ERROR) << getId() << " failed to rmdir mount point " << mPath;
210 }
211
212 return OK;
213}
214
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700215status_t PrivateVolume::doFormat(const std::string& fsType) {
216 std::string resolvedFsType = fsType;
217 if (fsType == "auto") {
218 // For now, assume that all MMC devices are flash-based SD cards, and
219 // give everyone else ext4 because sysfs rotational isn't reliable.
Alistair Delvac6717312020-05-19 15:49:26 -0700220 // Additionally, prefer f2fs for loop-based devices
221 if ((major(mRawDevice) == kMajorBlockMmc ||
Lyon Wang73670aa2024-11-29 10:20:23 +0800222 major(mRawDevice) == kMajorBlockHdd ||
Alistair Delvac6717312020-05-19 15:49:26 -0700223 major(mRawDevice) == kMajorBlockLoop ||
224 IsVirtioBlkDevice(major(mRawDevice))) && f2fs::IsSupported()) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700225 resolvedFsType = "f2fs";
226 } else {
227 resolvedFsType = "ext4";
228 }
229 LOG(DEBUG) << "Resolved auto to " << resolvedFsType;
230 }
231
232 if (resolvedFsType == "ext4") {
233 // TODO: change reported mountpoint once we have better selinux support
234 if (ext4::Format(mDmDevPath, 0, "/data")) {
235 PLOG(ERROR) << getId() << " failed to format";
236 return -EIO;
237 }
238 } else if (resolvedFsType == "f2fs") {
Daeho Jeong0c841552024-10-21 14:05:31 -0700239 if (f2fs::Format(mDmDevPath, false, {}, {})) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700240 PLOG(ERROR) << getId() << " failed to format";
241 return -EIO;
242 }
243 } else {
244 LOG(ERROR) << getId() << " unsupported filesystem " << fsType;
245 return -EINVAL;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700246 }
247
248 return OK;
249}
250
251} // namespace vold
252} // namespace android