blob: 21746b250ddb2ce438204e553166236f5ff5079e [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 Sharkeyd0640f62015-05-21 22:35:42 -070017#include "fs/Ext4.h"
18#include "fs/F2fs.h"
Jeff Sharkey9c484982015-03-31 10:35:33 -070019#include "PrivateVolume.h"
Jeff Sharkey3161fb32015-04-12 16:03:33 -070020#include "EmulatedVolume.h"
Jeff Sharkey9c484982015-03-31 10:35:33 -070021#include "Utils.h"
22#include "VolumeManager.h"
23#include "ResponseCode.h"
24#include "cryptfs.h"
25
Elliott Hughes7e128fb2015-12-04 15:50:53 -080026#include <android-base/stringprintf.h>
27#include <android-base/logging.h>
Jeff Sharkey9c484982015-03-31 10:35:33 -070028#include <cutils/fs.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/stat.h>
35#include <sys/types.h>
36#include <sys/wait.h>
37#include <sys/param.h>
38
39using android::base::StringPrintf;
40
41namespace android {
42namespace vold {
43
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070044static const unsigned int kMajorBlockMmc = 179;
45
Jeff Sharkey9c484982015-03-31 10:35:33 -070046PrivateVolume::PrivateVolume(dev_t device, const std::string& keyRaw) :
47 VolumeBase(Type::kPrivate), mRawDevice(device), mKeyRaw(keyRaw) {
48 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
52PrivateVolume::~PrivateVolume() {
53}
54
55status_t PrivateVolume::readMetadata() {
56 status_t res = ReadMetadata(mDmDevPath, mFsType, mFsUuid, mFsLabel);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070057 notifyEvent(ResponseCode::VolumeFsTypeChanged, mFsType);
58 notifyEvent(ResponseCode::VolumeFsUuidChanged, mFsUuid);
59 notifyEvent(ResponseCode::VolumeFsLabelChanged, mFsLabel);
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
73 unsigned char* key = (unsigned char*) mKeyRaw.data();
74 char crypto_blkdev[MAXPATHLEN];
75 int res = cryptfs_setup_ext_volume(getId().c_str(), mRawDevPath.c_str(),
76 key, mKeyRaw.size(), crypto_blkdev);
77 mDmDevPath = crypto_blkdev;
78 if (res != 0) {
79 PLOG(ERROR) << getId() << " failed to setup cryptfs";
80 return -EIO;
81 }
82
83 return OK;
84}
85
86status_t PrivateVolume::doDestroy() {
87 if (cryptfs_revert_ext_volume(getId().c_str())) {
88 LOG(ERROR) << getId() << " failed to revert cryptfs";
89 }
90 return DestroyDeviceNode(mRawDevPath);
91}
92
93status_t PrivateVolume::doMount() {
94 if (readMetadata()) {
95 LOG(ERROR) << getId() << " failed to read metadata";
96 return -EIO;
97 }
98
Jeff Sharkeyf0121c52015-04-06 14:08:45 -070099 mPath = StringPrintf("/mnt/expand/%s", mFsUuid.c_str());
100 setPath(mPath);
101
102 if (PrepareDir(mPath, 0700, AID_ROOT, AID_ROOT)) {
103 PLOG(ERROR) << getId() << " failed to create mount point " << mPath;
104 return -EIO;
105 }
106
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700107 if (mFsType == "ext4") {
108 int res = ext4::Check(mDmDevPath, mPath);
109 if (res == 0 || res == 1) {
110 LOG(DEBUG) << getId() << " passed filesystem check";
111 } else {
112 PLOG(ERROR) << getId() << " failed filesystem check";
113 return -EIO;
114 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700115
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700116 if (ext4::Mount(mDmDevPath, mPath, false, false, true)) {
117 PLOG(ERROR) << getId() << " failed to mount";
118 return -EIO;
119 }
120
121 } else if (mFsType == "f2fs") {
122 int res = f2fs::Check(mDmDevPath);
123 if (res == 0) {
124 LOG(DEBUG) << getId() << " passed filesystem check";
125 } else {
126 PLOG(ERROR) << getId() << " failed filesystem check";
127 return -EIO;
128 }
129
130 if (f2fs::Mount(mDmDevPath, mPath)) {
131 PLOG(ERROR) << getId() << " failed to mount";
132 return -EIO;
133 }
134
135 } else {
136 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700137 return -EIO;
138 }
139
Jeff Sharkey34824122015-06-09 10:59:17 -0700140 LOG(VERBOSE) << "Starting restorecon of " << mPath;
141
142 // TODO: find a cleaner way of waiting for restorecon to finish
143 property_set("selinux.restorecon_recursive", "");
144 property_set("selinux.restorecon_recursive", mPath.c_str());
145
146 char value[PROPERTY_VALUE_MAX];
147 while (true) {
148 property_get("selinux.restorecon_recursive", value, "");
149 if (strcmp(mPath.c_str(), value) == 0) {
150 break;
151 }
152 sleep(1);
153 LOG(VERBOSE) << "Waiting for restorecon...";
154 }
155
156 LOG(VERBOSE) << "Finished restorecon of " << mPath;
157
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700158 // Verify that common directories are ready to roll
159 if (PrepareDir(mPath + "/app", 0771, AID_SYSTEM, AID_SYSTEM) ||
160 PrepareDir(mPath + "/user", 0711, AID_SYSTEM, AID_SYSTEM) ||
Jeff Sharkeyd322c2a2015-11-17 12:16:49 -0700161 PrepareDir(mPath + "/user_de", 0711, AID_SYSTEM, AID_SYSTEM) ||
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700162 PrepareDir(mPath + "/media", 0770, AID_MEDIA_RW, AID_MEDIA_RW) ||
Jeff Sharkey81f55c62015-07-07 14:37:03 -0700163 PrepareDir(mPath + "/media/0", 0770, AID_MEDIA_RW, AID_MEDIA_RW) ||
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700164 PrepareDir(mPath + "/local", 0751, AID_ROOT, AID_ROOT) ||
165 PrepareDir(mPath + "/local/tmp", 0771, AID_SHELL, AID_SHELL)) {
166 PLOG(ERROR) << getId() << " failed to prepare";
167 return -EIO;
168 }
169
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700170 // Create a new emulated volume stacked above us, it will automatically
171 // be destroyed during unmount
172 std::string mediaPath(mPath + "/media");
173 auto vol = std::shared_ptr<VolumeBase>(
174 new EmulatedVolume(mediaPath, mRawDevice, mFsUuid));
175 addVolume(vol);
176 vol->create();
177
Jeff Sharkey9c484982015-03-31 10:35:33 -0700178 return OK;
179}
180
181status_t PrivateVolume::doUnmount() {
182 ForceUnmount(mPath);
183
184 if (TEMP_FAILURE_RETRY(rmdir(mPath.c_str()))) {
185 PLOG(ERROR) << getId() << " failed to rmdir mount point " << mPath;
186 }
187
188 return OK;
189}
190
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700191status_t PrivateVolume::doFormat(const std::string& fsType) {
192 std::string resolvedFsType = fsType;
193 if (fsType == "auto") {
194 // For now, assume that all MMC devices are flash-based SD cards, and
195 // give everyone else ext4 because sysfs rotational isn't reliable.
196 if ((major(mRawDevice) == kMajorBlockMmc) && f2fs::IsSupported()) {
197 resolvedFsType = "f2fs";
198 } else {
199 resolvedFsType = "ext4";
200 }
201 LOG(DEBUG) << "Resolved auto to " << resolvedFsType;
202 }
203
204 if (resolvedFsType == "ext4") {
205 // TODO: change reported mountpoint once we have better selinux support
206 if (ext4::Format(mDmDevPath, 0, "/data")) {
207 PLOG(ERROR) << getId() << " failed to format";
208 return -EIO;
209 }
210 } else if (resolvedFsType == "f2fs") {
211 if (f2fs::Format(mDmDevPath)) {
212 PLOG(ERROR) << getId() << " failed to format";
213 return -EIO;
214 }
215 } else {
216 LOG(ERROR) << getId() << " unsupported filesystem " << fsType;
217 return -EINVAL;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700218 }
219
220 return OK;
221}
222
223} // namespace vold
224} // namespace android