blob: f652c6babc3b2b71b424e877964fc750f69486bd [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);
53
54 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
55 ResponseCode::VolumeFsTypeChanged,
56 StringPrintf("%s %s", getId().c_str(), mFsType.c_str()).c_str(), false);
57 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
58 ResponseCode::VolumeFsUuidChanged,
59 StringPrintf("%s %s", getId().c_str(), mFsUuid.c_str()).c_str(), false);
60 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
61 ResponseCode::VolumeFsLabelChanged,
62 StringPrintf("%s %s", getId().c_str(), mFsLabel.c_str()).c_str(), false);
63
64 return res;
65}
66
67status_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 cryptfs_revert_ext_volume(getId().c_str());
74
75 // TODO: figure out better SELinux labels for private volumes
76
77 unsigned char* key = (unsigned char*) mKeyRaw.data();
78 char crypto_blkdev[MAXPATHLEN];
79 int res = cryptfs_setup_ext_volume(getId().c_str(), mRawDevPath.c_str(),
80 key, mKeyRaw.size(), crypto_blkdev);
81 mDmDevPath = crypto_blkdev;
82 if (res != 0) {
83 PLOG(ERROR) << getId() << " failed to setup cryptfs";
84 return -EIO;
85 }
86
87 return OK;
88}
89
90status_t PrivateVolume::doDestroy() {
91 if (cryptfs_revert_ext_volume(getId().c_str())) {
92 LOG(ERROR) << getId() << " failed to revert cryptfs";
93 }
94 return DestroyDeviceNode(mRawDevPath);
95}
96
97status_t PrivateVolume::doMount() {
98 if (readMetadata()) {
99 LOG(ERROR) << getId() << " failed to read metadata";
100 return -EIO;
101 }
102
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700103 mPath = StringPrintf("/mnt/expand/%s", mFsUuid.c_str());
104 setPath(mPath);
105
106 if (PrepareDir(mPath, 0700, AID_ROOT, AID_ROOT)) {
107 PLOG(ERROR) << getId() << " failed to create mount point " << mPath;
108 return -EIO;
109 }
110
Jeff Sharkey9c484982015-03-31 10:35:33 -0700111 if (Ext4::check(mDmDevPath.c_str(), mPath.c_str())) {
112 PLOG(ERROR) << getId() << " failed filesystem check";
113 return -EIO;
114 }
115
Jeff Sharkey9c484982015-03-31 10:35:33 -0700116 if (Ext4::doMount(mDmDevPath.c_str(), mPath.c_str(), false, false, true)) {
117 PLOG(ERROR) << getId() << " failed to mount";
118 return -EIO;
119 }
120
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700121 // Verify that common directories are ready to roll
122 if (PrepareDir(mPath + "/app", 0771, AID_SYSTEM, AID_SYSTEM) ||
123 PrepareDir(mPath + "/user", 0711, AID_SYSTEM, AID_SYSTEM) ||
124 PrepareDir(mPath + "/media", 0770, AID_MEDIA_RW, AID_MEDIA_RW) ||
125 PrepareDir(mPath + "/local", 0751, AID_ROOT, AID_ROOT) ||
126 PrepareDir(mPath + "/local/tmp", 0771, AID_SHELL, AID_SHELL)) {
127 PLOG(ERROR) << getId() << " failed to prepare";
128 return -EIO;
129 }
130
Jeff Sharkey9c484982015-03-31 10:35:33 -0700131 return OK;
132}
133
134status_t PrivateVolume::doUnmount() {
135 ForceUnmount(mPath);
136
137 if (TEMP_FAILURE_RETRY(rmdir(mPath.c_str()))) {
138 PLOG(ERROR) << getId() << " failed to rmdir mount point " << mPath;
139 }
140
141 return OK;
142}
143
144status_t PrivateVolume::doFormat() {
145 // TODO: change mountpoint once we have selinux labels
146 if (Ext4::format(mDmDevPath.c_str(), 0, "/data")) {
147 PLOG(ERROR) << getId() << " failed to format";
148 return -EIO;
149 }
150
151 return OK;
152}
153
154} // namespace vold
155} // namespace android