blob: 1653faefc0b57391063c1b2be0b68fd4a8841289 [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>
28#include <private/android_filesystem_config.h>
29
30#include <fcntl.h>
31#include <stdlib.h>
32#include <sys/mount.h>
Jeff Sharkey9c484982015-03-31 10:35:33 -070033#include <sys/param.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070034#include <sys/stat.h>
35#include <sys/sysmacros.h>
36#include <sys/types.h>
37#include <sys/wait.h>
Jeff Sharkey9c484982015-03-31 10:35:33 -070038
39using android::base::StringPrintf;
40
41namespace android {
42namespace vold {
43
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070044static const unsigned int kMajorBlockMmc = 179;
45
Paul Crowley3d98f5d2020-02-07 11:49:09 -080046PrivateVolume::PrivateVolume(dev_t device, const KeyBuffer& keyRaw)
Paul Crowley14c8c072018-09-18 13:30:21 -070047 : VolumeBase(Type::kPrivate), mRawDevice(device), mKeyRaw(keyRaw) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070048 setId(StringPrintf("private:%u,%u", major(device), minor(device)));
49 mRawDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
Jeff Sharkey9c484982015-03-31 10:35:33 -070050}
51
Paul Crowley14c8c072018-09-18 13:30:21 -070052PrivateVolume::~PrivateVolume() {}
Jeff Sharkey9c484982015-03-31 10:35:33 -070053
54status_t PrivateVolume::readMetadata() {
Jeff Sharkey3472e522017-10-06 18:02:53 -060055 status_t res = ReadMetadata(mDmDevPath, &mFsType, &mFsUuid, &mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060056
Jeff Sharkey814e9d32017-09-13 11:49:44 -060057 auto listener = getListener();
58 if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060059
Jeff Sharkey9c484982015-03-31 10:35:33 -070060 return res;
61}
62
63status_t PrivateVolume::doCreate() {
64 if (CreateDeviceNode(mRawDevPath, mRawDevice)) {
65 return -EIO;
66 }
67
68 // Recover from stale vold by tearing down any old mappings
69 cryptfs_revert_ext_volume(getId().c_str());
70
71 // TODO: figure out better SELinux labels for private volumes
72
Paul Crowley3d98f5d2020-02-07 11:49:09 -080073 int res = cryptfs_setup_ext_volume(getId().c_str(), mRawDevPath.c_str(), mKeyRaw, &mDmDevPath);
Jeff Sharkey9c484982015-03-31 10:35:33 -070074 if (res != 0) {
75 PLOG(ERROR) << getId() << " failed to setup cryptfs";
76 return -EIO;
77 }
78
79 return OK;
80}
81
82status_t PrivateVolume::doDestroy() {
83 if (cryptfs_revert_ext_volume(getId().c_str())) {
84 LOG(ERROR) << getId() << " failed to revert cryptfs";
85 }
86 return DestroyDeviceNode(mRawDevPath);
87}
88
89status_t PrivateVolume::doMount() {
90 if (readMetadata()) {
91 LOG(ERROR) << getId() << " failed to read metadata";
92 return -EIO;
93 }
94
Jeff Sharkeyf0121c52015-04-06 14:08:45 -070095 mPath = StringPrintf("/mnt/expand/%s", mFsUuid.c_str());
96 setPath(mPath);
97
98 if (PrepareDir(mPath, 0700, AID_ROOT, AID_ROOT)) {
99 PLOG(ERROR) << getId() << " failed to create mount point " << mPath;
100 return -EIO;
101 }
102
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700103 if (mFsType == "ext4") {
104 int res = ext4::Check(mDmDevPath, mPath);
105 if (res == 0 || res == 1) {
106 LOG(DEBUG) << getId() << " passed filesystem check";
107 } else {
108 PLOG(ERROR) << getId() << " failed filesystem check";
109 return -EIO;
110 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700111
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700112 if (ext4::Mount(mDmDevPath, mPath, false, false, true)) {
113 PLOG(ERROR) << getId() << " failed to mount";
114 return -EIO;
115 }
116
117 } else if (mFsType == "f2fs") {
118 int res = f2fs::Check(mDmDevPath);
119 if (res == 0) {
120 LOG(DEBUG) << getId() << " passed filesystem check";
121 } else {
122 PLOG(ERROR) << getId() << " failed filesystem check";
123 return -EIO;
124 }
125
126 if (f2fs::Mount(mDmDevPath, mPath)) {
127 PLOG(ERROR) << getId() << " failed to mount";
128 return -EIO;
129 }
130
131 } else {
132 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700133 return -EIO;
134 }
135
Jeff Sharkeyd24aeda2016-07-15 16:20:22 -0600136 RestoreconRecursive(mPath);
Jeff Sharkey34824122015-06-09 10:59:17 -0700137
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700138 // Verify that common directories are ready to roll
139 if (PrepareDir(mPath + "/app", 0771, AID_SYSTEM, AID_SYSTEM) ||
Paul Crowley14c8c072018-09-18 13:30:21 -0700140 PrepareDir(mPath + "/user", 0711, AID_SYSTEM, AID_SYSTEM) ||
141 PrepareDir(mPath + "/user_de", 0711, AID_SYSTEM, AID_SYSTEM) ||
142 PrepareDir(mPath + "/media", 0770, AID_MEDIA_RW, AID_MEDIA_RW) ||
143 PrepareDir(mPath + "/media/0", 0770, AID_MEDIA_RW, AID_MEDIA_RW) ||
144 PrepareDir(mPath + "/local", 0751, AID_ROOT, AID_ROOT) ||
145 PrepareDir(mPath + "/local/tmp", 0771, AID_SHELL, AID_SHELL)) {
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700146 PLOG(ERROR) << getId() << " failed to prepare";
147 return -EIO;
148 }
149
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700150 // Create a new emulated volume stacked above us, it will automatically
151 // be destroyed during unmount
152 std::string mediaPath(mPath + "/media");
Paul Crowley14c8c072018-09-18 13:30:21 -0700153 auto vol = std::shared_ptr<VolumeBase>(new EmulatedVolume(mediaPath, mRawDevice, mFsUuid));
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700154 addVolume(vol);
155 vol->create();
156
Jeff Sharkey9c484982015-03-31 10:35:33 -0700157 return OK;
158}
159
160status_t PrivateVolume::doUnmount() {
161 ForceUnmount(mPath);
162
163 if (TEMP_FAILURE_RETRY(rmdir(mPath.c_str()))) {
164 PLOG(ERROR) << getId() << " failed to rmdir mount point " << mPath;
165 }
166
167 return OK;
168}
169
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700170status_t PrivateVolume::doFormat(const std::string& fsType) {
171 std::string resolvedFsType = fsType;
172 if (fsType == "auto") {
173 // For now, assume that all MMC devices are flash-based SD cards, and
174 // give everyone else ext4 because sysfs rotational isn't reliable.
175 if ((major(mRawDevice) == kMajorBlockMmc) && f2fs::IsSupported()) {
176 resolvedFsType = "f2fs";
177 } else {
178 resolvedFsType = "ext4";
179 }
180 LOG(DEBUG) << "Resolved auto to " << resolvedFsType;
181 }
182
183 if (resolvedFsType == "ext4") {
184 // TODO: change reported mountpoint once we have better selinux support
185 if (ext4::Format(mDmDevPath, 0, "/data")) {
186 PLOG(ERROR) << getId() << " failed to format";
187 return -EIO;
188 }
189 } else if (resolvedFsType == "f2fs") {
190 if (f2fs::Format(mDmDevPath)) {
191 PLOG(ERROR) << getId() << " failed to format";
192 return -EIO;
193 }
194 } else {
195 LOG(ERROR) << getId() << " unsupported filesystem " << fsType;
196 return -EINVAL;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700197 }
198
199 return OK;
200}
201
202} // namespace vold
203} // namespace android