blob: 42eea644740eb317f054725229e2ed5d28f3caa8 [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());
46 mPath = StringPrintf("/mnt/secure/%s", getId().c_str());
47}
48
49PrivateVolume::~PrivateVolume() {
50}
51
52status_t PrivateVolume::readMetadata() {
53 status_t res = ReadMetadata(mDmDevPath, mFsType, mFsUuid, mFsLabel);
54
55 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
56 ResponseCode::VolumeFsTypeChanged,
57 StringPrintf("%s %s", getId().c_str(), mFsType.c_str()).c_str(), false);
58 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
59 ResponseCode::VolumeFsUuidChanged,
60 StringPrintf("%s %s", getId().c_str(), mFsUuid.c_str()).c_str(), false);
61 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
62 ResponseCode::VolumeFsLabelChanged,
63 StringPrintf("%s %s", getId().c_str(), mFsLabel.c_str()).c_str(), false);
64
65 return res;
66}
67
68status_t PrivateVolume::doCreate() {
69 if (CreateDeviceNode(mRawDevPath, mRawDevice)) {
70 return -EIO;
71 }
72
73 // Recover from stale vold by tearing down any old mappings
74 cryptfs_revert_ext_volume(getId().c_str());
75
76 // TODO: figure out better SELinux labels for private volumes
77
78 unsigned char* key = (unsigned char*) mKeyRaw.data();
79 char crypto_blkdev[MAXPATHLEN];
80 int res = cryptfs_setup_ext_volume(getId().c_str(), mRawDevPath.c_str(),
81 key, mKeyRaw.size(), crypto_blkdev);
82 mDmDevPath = crypto_blkdev;
83 if (res != 0) {
84 PLOG(ERROR) << getId() << " failed to setup cryptfs";
85 return -EIO;
86 }
87
88 return OK;
89}
90
91status_t PrivateVolume::doDestroy() {
92 if (cryptfs_revert_ext_volume(getId().c_str())) {
93 LOG(ERROR) << getId() << " failed to revert cryptfs";
94 }
95 return DestroyDeviceNode(mRawDevPath);
96}
97
98status_t PrivateVolume::doMount() {
99 if (readMetadata()) {
100 LOG(ERROR) << getId() << " failed to read metadata";
101 return -EIO;
102 }
103
104 if (Ext4::check(mDmDevPath.c_str(), mPath.c_str())) {
105 PLOG(ERROR) << getId() << " failed filesystem check";
106 return -EIO;
107 }
108
109 setPath(mPath);
110
111 if (fs_prepare_dir(mPath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
112 PLOG(ERROR) << getId() << " failed to create mount point " << mPath;
113 return -errno;
114 }
115
116 if (Ext4::doMount(mDmDevPath.c_str(), mPath.c_str(), false, false, true)) {
117 PLOG(ERROR) << getId() << " failed to mount";
118 return -EIO;
119 }
120
121 return OK;
122}
123
124status_t PrivateVolume::doUnmount() {
125 ForceUnmount(mPath);
126
127 if (TEMP_FAILURE_RETRY(rmdir(mPath.c_str()))) {
128 PLOG(ERROR) << getId() << " failed to rmdir mount point " << mPath;
129 }
130
131 return OK;
132}
133
134status_t PrivateVolume::doFormat() {
135 // TODO: change mountpoint once we have selinux labels
136 if (Ext4::format(mDmDevPath.c_str(), 0, "/data")) {
137 PLOG(ERROR) << getId() << " failed to format";
138 return -EIO;
139 }
140
141 return OK;
142}
143
144} // namespace vold
145} // namespace android