blob: 9a96082095d2f035c38aee21be62945cb3c82229 [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>
Elliott Hughes0e08e842017-05-18 09:08:24 -070036#include <sys/sysmacros.h>
Jeff Sharkey9c484982015-03-31 10:35:33 -070037#include <sys/wait.h>
38#include <sys/param.h>
39
40using android::base::StringPrintf;
41
42namespace android {
43namespace vold {
44
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070045static const unsigned int kMajorBlockMmc = 179;
46
Jeff Sharkey9c484982015-03-31 10:35:33 -070047PrivateVolume::PrivateVolume(dev_t device, const std::string& keyRaw) :
48 VolumeBase(Type::kPrivate), mRawDevice(device), mKeyRaw(keyRaw) {
49 setId(StringPrintf("private:%u,%u", major(device), minor(device)));
50 mRawDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
Jeff Sharkey9c484982015-03-31 10:35:33 -070051}
52
53PrivateVolume::~PrivateVolume() {
54}
55
56status_t PrivateVolume::readMetadata() {
57 status_t res = ReadMetadata(mDmDevPath, mFsType, mFsUuid, mFsLabel);
Jeff Sharkey814e9d32017-09-13 11:49:44 -060058#if ENABLE_BINDER
59 auto listener = getListener();
60 if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
61#else
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070062 notifyEvent(ResponseCode::VolumeFsTypeChanged, mFsType);
63 notifyEvent(ResponseCode::VolumeFsUuidChanged, mFsUuid);
64 notifyEvent(ResponseCode::VolumeFsLabelChanged, mFsLabel);
Jeff Sharkey814e9d32017-09-13 11:49:44 -060065#endif
Jeff Sharkey9c484982015-03-31 10:35:33 -070066 return res;
67}
68
69status_t PrivateVolume::doCreate() {
70 if (CreateDeviceNode(mRawDevPath, mRawDevice)) {
71 return -EIO;
72 }
73
74 // Recover from stale vold by tearing down any old mappings
75 cryptfs_revert_ext_volume(getId().c_str());
76
77 // TODO: figure out better SELinux labels for private volumes
78
79 unsigned char* key = (unsigned char*) mKeyRaw.data();
80 char crypto_blkdev[MAXPATHLEN];
81 int res = cryptfs_setup_ext_volume(getId().c_str(), mRawDevPath.c_str(),
82 key, mKeyRaw.size(), crypto_blkdev);
83 mDmDevPath = crypto_blkdev;
84 if (res != 0) {
85 PLOG(ERROR) << getId() << " failed to setup cryptfs";
86 return -EIO;
87 }
88
89 return OK;
90}
91
92status_t PrivateVolume::doDestroy() {
93 if (cryptfs_revert_ext_volume(getId().c_str())) {
94 LOG(ERROR) << getId() << " failed to revert cryptfs";
95 }
96 return DestroyDeviceNode(mRawDevPath);
97}
98
99status_t PrivateVolume::doMount() {
100 if (readMetadata()) {
101 LOG(ERROR) << getId() << " failed to read metadata";
102 return -EIO;
103 }
104
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700105 mPath = StringPrintf("/mnt/expand/%s", mFsUuid.c_str());
106 setPath(mPath);
107
108 if (PrepareDir(mPath, 0700, AID_ROOT, AID_ROOT)) {
109 PLOG(ERROR) << getId() << " failed to create mount point " << mPath;
110 return -EIO;
111 }
112
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700113 if (mFsType == "ext4") {
114 int res = ext4::Check(mDmDevPath, mPath);
115 if (res == 0 || res == 1) {
116 LOG(DEBUG) << getId() << " passed filesystem check";
117 } else {
118 PLOG(ERROR) << getId() << " failed filesystem check";
119 return -EIO;
120 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700121
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700122 if (ext4::Mount(mDmDevPath, mPath, false, false, true)) {
123 PLOG(ERROR) << getId() << " failed to mount";
124 return -EIO;
125 }
126
127 } else if (mFsType == "f2fs") {
128 int res = f2fs::Check(mDmDevPath);
129 if (res == 0) {
130 LOG(DEBUG) << getId() << " passed filesystem check";
131 } else {
132 PLOG(ERROR) << getId() << " failed filesystem check";
133 return -EIO;
134 }
135
136 if (f2fs::Mount(mDmDevPath, mPath)) {
137 PLOG(ERROR) << getId() << " failed to mount";
138 return -EIO;
139 }
140
141 } else {
142 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700143 return -EIO;
144 }
145
Jeff Sharkeyd24aeda2016-07-15 16:20:22 -0600146 RestoreconRecursive(mPath);
Jeff Sharkey34824122015-06-09 10:59:17 -0700147
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700148 // Verify that common directories are ready to roll
149 if (PrepareDir(mPath + "/app", 0771, AID_SYSTEM, AID_SYSTEM) ||
150 PrepareDir(mPath + "/user", 0711, AID_SYSTEM, AID_SYSTEM) ||
Jeff Sharkeyd322c2a2015-11-17 12:16:49 -0700151 PrepareDir(mPath + "/user_de", 0711, AID_SYSTEM, AID_SYSTEM) ||
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700152 PrepareDir(mPath + "/media", 0770, AID_MEDIA_RW, AID_MEDIA_RW) ||
Jeff Sharkey81f55c62015-07-07 14:37:03 -0700153 PrepareDir(mPath + "/media/0", 0770, AID_MEDIA_RW, AID_MEDIA_RW) ||
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700154 PrepareDir(mPath + "/local", 0751, AID_ROOT, AID_ROOT) ||
155 PrepareDir(mPath + "/local/tmp", 0771, AID_SHELL, AID_SHELL)) {
156 PLOG(ERROR) << getId() << " failed to prepare";
157 return -EIO;
158 }
159
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700160 // Create a new emulated volume stacked above us, it will automatically
161 // be destroyed during unmount
162 std::string mediaPath(mPath + "/media");
163 auto vol = std::shared_ptr<VolumeBase>(
164 new EmulatedVolume(mediaPath, mRawDevice, mFsUuid));
165 addVolume(vol);
166 vol->create();
167
Jeff Sharkey9c484982015-03-31 10:35:33 -0700168 return OK;
169}
170
171status_t PrivateVolume::doUnmount() {
172 ForceUnmount(mPath);
173
174 if (TEMP_FAILURE_RETRY(rmdir(mPath.c_str()))) {
175 PLOG(ERROR) << getId() << " failed to rmdir mount point " << mPath;
176 }
177
178 return OK;
179}
180
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700181status_t PrivateVolume::doFormat(const std::string& fsType) {
182 std::string resolvedFsType = fsType;
183 if (fsType == "auto") {
184 // For now, assume that all MMC devices are flash-based SD cards, and
185 // give everyone else ext4 because sysfs rotational isn't reliable.
186 if ((major(mRawDevice) == kMajorBlockMmc) && f2fs::IsSupported()) {
187 resolvedFsType = "f2fs";
188 } else {
189 resolvedFsType = "ext4";
190 }
191 LOG(DEBUG) << "Resolved auto to " << resolvedFsType;
192 }
193
194 if (resolvedFsType == "ext4") {
195 // TODO: change reported mountpoint once we have better selinux support
196 if (ext4::Format(mDmDevPath, 0, "/data")) {
197 PLOG(ERROR) << getId() << " failed to format";
198 return -EIO;
199 }
200 } else if (resolvedFsType == "f2fs") {
201 if (f2fs::Format(mDmDevPath)) {
202 PLOG(ERROR) << getId() << " failed to format";
203 return -EIO;
204 }
205 } else {
206 LOG(ERROR) << getId() << " unsupported filesystem " << fsType;
207 return -EINVAL;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700208 }
209
210 return OK;
211}
212
213} // namespace vold
214} // namespace android