blob: 6ca54801c3105664e2548ceb31aab996d09f6fd5 [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
17#include "Ext4.h"
18#include "PrivateVolume.h"
19#include "Utils.h"
20#include "VolumeManager.h"
21#include "ResponseCode.h"
22#include "cryptfs.h"
23
24#include <base/stringprintf.h>
25#include <base/logging.h>
26#include <cutils/fs.h>
27#include <private/android_filesystem_config.h>
28
29#include <fcntl.h>
30#include <stdlib.h>
31#include <sys/mount.h>
32#include <sys/stat.h>
33#include <sys/types.h>
34#include <sys/wait.h>
35#include <sys/param.h>
36
37using android::base::StringPrintf;
38
39namespace android {
40namespace vold {
41
42PrivateVolume::PrivateVolume(dev_t device, const std::string& keyRaw) :
43 VolumeBase(Type::kPrivate), mRawDevice(device), mKeyRaw(keyRaw) {
44 setId(StringPrintf("private:%u,%u", major(device), minor(device)));
45 mRawDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
Jeff Sharkey9c484982015-03-31 10:35:33 -070046}
47
48PrivateVolume::~PrivateVolume() {
49}
50
51status_t PrivateVolume::readMetadata() {
52 status_t res = ReadMetadata(mDmDevPath, mFsType, mFsUuid, mFsLabel);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070053 notifyEvent(ResponseCode::VolumeFsTypeChanged, mFsType);
54 notifyEvent(ResponseCode::VolumeFsUuidChanged, mFsUuid);
55 notifyEvent(ResponseCode::VolumeFsLabelChanged, mFsLabel);
Jeff Sharkey9c484982015-03-31 10:35:33 -070056 return res;
57}
58
59status_t PrivateVolume::doCreate() {
60 if (CreateDeviceNode(mRawDevPath, mRawDevice)) {
61 return -EIO;
62 }
63
64 // Recover from stale vold by tearing down any old mappings
65 cryptfs_revert_ext_volume(getId().c_str());
66
67 // TODO: figure out better SELinux labels for private volumes
68
69 unsigned char* key = (unsigned char*) mKeyRaw.data();
70 char crypto_blkdev[MAXPATHLEN];
71 int res = cryptfs_setup_ext_volume(getId().c_str(), mRawDevPath.c_str(),
72 key, mKeyRaw.size(), crypto_blkdev);
73 mDmDevPath = crypto_blkdev;
74 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 Sharkey9c484982015-03-31 10:35:33 -0700103 if (Ext4::check(mDmDevPath.c_str(), mPath.c_str())) {
104 PLOG(ERROR) << getId() << " failed filesystem check";
105 return -EIO;
106 }
107
Jeff Sharkey9c484982015-03-31 10:35:33 -0700108 if (Ext4::doMount(mDmDevPath.c_str(), mPath.c_str(), false, false, true)) {
109 PLOG(ERROR) << getId() << " failed to mount";
110 return -EIO;
111 }
112
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700113 // Verify that common directories are ready to roll
114 if (PrepareDir(mPath + "/app", 0771, AID_SYSTEM, AID_SYSTEM) ||
115 PrepareDir(mPath + "/user", 0711, AID_SYSTEM, AID_SYSTEM) ||
116 PrepareDir(mPath + "/media", 0770, AID_MEDIA_RW, AID_MEDIA_RW) ||
117 PrepareDir(mPath + "/local", 0751, AID_ROOT, AID_ROOT) ||
118 PrepareDir(mPath + "/local/tmp", 0771, AID_SHELL, AID_SHELL)) {
119 PLOG(ERROR) << getId() << " failed to prepare";
120 return -EIO;
121 }
122
Jeff Sharkey9c484982015-03-31 10:35:33 -0700123 return OK;
124}
125
126status_t PrivateVolume::doUnmount() {
127 ForceUnmount(mPath);
128
129 if (TEMP_FAILURE_RETRY(rmdir(mPath.c_str()))) {
130 PLOG(ERROR) << getId() << " failed to rmdir mount point " << mPath;
131 }
132
133 return OK;
134}
135
136status_t PrivateVolume::doFormat() {
137 // TODO: change mountpoint once we have selinux labels
138 if (Ext4::format(mDmDevPath.c_str(), 0, "/data")) {
139 PLOG(ERROR) << getId() << " failed to format";
140 return -EIO;
141 }
142
143 return OK;
144}
145
146} // namespace vold
147} // namespace android