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 + "/misc_ce", 0711, AID_SYSTEM, AID_SYSTEM) ||
177 PrepareDir(mPath + "/misc_de", 0711, AID_SYSTEM, AID_SYSTEM) ||
178 PrepareDir(mPath + "/media", 0770, AID_MEDIA_RW, AID_MEDIA_RW, attrs) ||
179 PrepareDir(mPath + "/media/0", 0770, AID_MEDIA_RW, AID_MEDIA_RW) ||
180 PrepareDir(mPath + "/local", 0751, AID_ROOT, AID_ROOT) ||
181 PrepareDir(mPath + "/local/tmp", 0771, AID_SHELL, AID_SHELL)) {
182 PLOG(ERROR) << getId() << " failed to prepare";
183 return -EIO;
184 }
185
186 return OK;
187 }
188
doPostMount()189 void PrivateVolume::doPostMount() {
190 auto vol_manager = VolumeManager::Instance();
191 std::string mediaPath(mPath + "/media");
192
193 // Create a new emulated volume stacked above us for all added users, they will automatically
194 // be destroyed during unmount
195 for (userid_t user : vol_manager->getStartedUsers()) {
196 auto vol = std::shared_ptr<VolumeBase>(
197 new EmulatedVolume(mediaPath, mRawDevice, mFsUuid, user));
198 vol->setMountUserId(user);
199 addVolume(vol);
200 vol->create();
201 }
202 }
203
doUnmount()204 status_t PrivateVolume::doUnmount() {
205 ForceUnmount(mPath);
206
207 if (TEMP_FAILURE_RETRY(rmdir(mPath.c_str()))) {
208 PLOG(ERROR) << getId() << " failed to rmdir mount point " << mPath;
209 }
210
211 return OK;
212 }
213
doFormat(const std::string & fsType)214 status_t PrivateVolume::doFormat(const std::string& fsType) {
215 std::string resolvedFsType = fsType;
216 if (fsType == "auto") {
217 // For now, assume that all MMC devices are flash-based SD cards, and
218 // give everyone else ext4 because sysfs rotational isn't reliable.
219 // Additionally, prefer f2fs for loop-based devices
220 if ((major(mRawDevice) == kMajorBlockMmc ||
221 major(mRawDevice) == kMajorBlockLoop ||
222 IsVirtioBlkDevice(major(mRawDevice))) && f2fs::IsSupported()) {
223 resolvedFsType = "f2fs";
224 } else {
225 resolvedFsType = "ext4";
226 }
227 LOG(DEBUG) << "Resolved auto to " << resolvedFsType;
228 }
229
230 if (resolvedFsType == "ext4") {
231 // TODO: change reported mountpoint once we have better selinux support
232 if (ext4::Format(mDmDevPath, 0, "/data")) {
233 PLOG(ERROR) << getId() << " failed to format";
234 return -EIO;
235 }
236 } else if (resolvedFsType == "f2fs") {
237 if (f2fs::Format(mDmDevPath)) {
238 PLOG(ERROR) << getId() << " failed to format";
239 return -EIO;
240 }
241 } else {
242 LOG(ERROR) << getId() << " unsupported filesystem " << fsType;
243 return -EINVAL;
244 }
245
246 return OK;
247 }
248
249 } // namespace vold
250 } // namespace android
251