1 /*
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
17 #include "PrivateVolume.h"
18 #include "EmulatedVolume.h"
19 #include "Utils.h"
20 #include "VolumeEncryption.h"
21 #include "VolumeManager.h"
22 #include "fs/Ext4.h"
23 #include "fs/F2fs.h"
24
25 #include <android-base/logging.h>
26 #include <android-base/stringprintf.h>
27 #include <cutils/fs.h>
28 #include <libdm/dm.h>
29 #include <private/android_filesystem_config.h>
30
31 #include <fcntl.h>
32 #include <stdlib.h>
33 #include <sys/mount.h>
34 #include <sys/param.h>
35 #include <sys/stat.h>
36 #include <sys/sysmacros.h>
37 #include <sys/types.h>
38 #include <sys/wait.h>
39 #include <thread>
40
41 using android::base::StringPrintf;
42 using android::vold::IsVirtioBlkDevice;
43
44 namespace android {
45 namespace vold {
46
47 static const unsigned int kMajorBlockLoop = 7;
48 static const unsigned int kMajorBlockMmc = 179;
49
PrivateVolume(dev_t device,const KeyBuffer & keyRaw)50 PrivateVolume::PrivateVolume(dev_t device, const KeyBuffer& keyRaw)
51 : VolumeBase(Type::kPrivate), mRawDevice(device), mKeyRaw(keyRaw) {
52 setId(StringPrintf("private:%u,%u", major(device), minor(device)));
53 mRawDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
54 }
55
~PrivateVolume()56 PrivateVolume::~PrivateVolume() {}
57
readMetadata()58 status_t PrivateVolume::readMetadata() {
59 status_t res = ReadMetadata(mDmDevPath, &mFsType, &mFsUuid, &mFsLabel);
60
61 auto listener = getListener();
62 if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
63
64 return res;
65 }
66
doCreate()67 status_t PrivateVolume::doCreate() {
68 if (CreateDeviceNode(mRawDevPath, mRawDevice)) {
69 return -EIO;
70 }
71
72 // Recover from stale vold by tearing down any old mappings
73 auto& dm = dm::DeviceMapper::Instance();
74 // 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 }
83 PLOG(ERROR) << "Cannot remove dm device " << getId();
84 std::this_thread::sleep_for(std::chrono::milliseconds(100));
85 }
86 if (!ret) {
87 return -EIO;
88 }
89
90 // TODO: figure out better SELinux labels for private volumes
91
92 if (!setup_ext_volume(getId(), mRawDevPath, mKeyRaw, &mDmDevPath)) {
93 LOG(ERROR) << getId() << " failed to setup metadata encryption";
94 return -EIO;
95 }
96
97 return OK;
98 }
99
doDestroy()100 status_t PrivateVolume::doDestroy() {
101 auto& dm = dm::DeviceMapper::Instance();
102 // 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 }
111 PLOG(ERROR) << "Cannot remove dm device " << getId();
112 std::this_thread::sleep_for(std::chrono::milliseconds(100));
113 }
114 if (!ret) {
115 return -EIO;
116 }
117 return DestroyDeviceNode(mRawDevPath);
118 }
119
doMount()120 status_t PrivateVolume::doMount() {
121 if (readMetadata()) {
122 LOG(ERROR) << getId() << " failed to read metadata";
123 return -EIO;
124 }
125
126 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
134 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 }
142
143 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;
164 return -EIO;
165 }
166
167 RestoreconRecursive(mPath);
168
169 int attrs = 0;
170 if (!IsSdcardfsUsed()) attrs = FS_CASEFOLD_FL;
171
172 // Verify that common directories are ready to roll
173 if (PrepareDir(mPath + "/app", 0771, AID_SYSTEM, AID_SYSTEM) ||
174 PrepareDir(mPath + "/user", 0711, AID_SYSTEM, AID_SYSTEM) ||
175 PrepareDir(mPath + "/user_de", 0711, AID_SYSTEM, AID_SYSTEM) ||
176 PrepareDir(mPath + "/media", 0770, AID_MEDIA_RW, AID_MEDIA_RW, attrs) ||
177 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)) {
180 PLOG(ERROR) << getId() << " failed to prepare";
181 return -EIO;
182 }
183
184 return OK;
185 }
186
doPostMount()187 void PrivateVolume::doPostMount() {
188 auto vol_manager = VolumeManager::Instance();
189 std::string mediaPath(mPath + "/media");
190
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 }
200 }
201
doUnmount()202 status_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
doFormat(const std::string & fsType)212 status_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.
217 // Additionally, prefer f2fs for loop-based devices
218 if ((major(mRawDevice) == kMajorBlockMmc ||
219 major(mRawDevice) == kMajorBlockLoop ||
220 IsVirtioBlkDevice(major(mRawDevice))) && f2fs::IsSupported()) {
221 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;
242 }
243
244 return OK;
245 }
246
247 } // namespace vold
248 } // namespace android
249