blob: 2b3b7b6404a9ebff47248b9d5d1b3ad631f6d6d3 [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"
Jeff Sharkey3161fb32015-04-12 16:03:33 -070019#include "EmulatedVolume.h"
Jeff Sharkey9c484982015-03-31 10:35:33 -070020#include "Utils.h"
21#include "VolumeManager.h"
22#include "ResponseCode.h"
23#include "cryptfs.h"
24
25#include <base/stringprintf.h>
26#include <base/logging.h>
27#include <cutils/fs.h>
28#include <private/android_filesystem_config.h>
29
30#include <fcntl.h>
31#include <stdlib.h>
32#include <sys/mount.h>
33#include <sys/stat.h>
34#include <sys/types.h>
35#include <sys/wait.h>
36#include <sys/param.h>
37
38using android::base::StringPrintf;
39
40namespace android {
41namespace vold {
42
43PrivateVolume::PrivateVolume(dev_t device, const std::string& keyRaw) :
44 VolumeBase(Type::kPrivate), mRawDevice(device), mKeyRaw(keyRaw) {
45 setId(StringPrintf("private:%u,%u", major(device), minor(device)));
46 mRawDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
Jeff Sharkey9c484982015-03-31 10:35:33 -070047}
48
49PrivateVolume::~PrivateVolume() {
50}
51
52status_t PrivateVolume::readMetadata() {
53 status_t res = ReadMetadata(mDmDevPath, mFsType, mFsUuid, mFsLabel);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070054 notifyEvent(ResponseCode::VolumeFsTypeChanged, mFsType);
55 notifyEvent(ResponseCode::VolumeFsUuidChanged, mFsUuid);
56 notifyEvent(ResponseCode::VolumeFsLabelChanged, mFsLabel);
Jeff Sharkey9c484982015-03-31 10:35:33 -070057 return res;
58}
59
60status_t PrivateVolume::doCreate() {
61 if (CreateDeviceNode(mRawDevPath, mRawDevice)) {
62 return -EIO;
63 }
64
65 // Recover from stale vold by tearing down any old mappings
66 cryptfs_revert_ext_volume(getId().c_str());
67
68 // TODO: figure out better SELinux labels for private volumes
69
70 unsigned char* key = (unsigned char*) mKeyRaw.data();
71 char crypto_blkdev[MAXPATHLEN];
72 int res = cryptfs_setup_ext_volume(getId().c_str(), mRawDevPath.c_str(),
73 key, mKeyRaw.size(), crypto_blkdev);
74 mDmDevPath = crypto_blkdev;
75 if (res != 0) {
76 PLOG(ERROR) << getId() << " failed to setup cryptfs";
77 return -EIO;
78 }
79
80 return OK;
81}
82
83status_t PrivateVolume::doDestroy() {
84 if (cryptfs_revert_ext_volume(getId().c_str())) {
85 LOG(ERROR) << getId() << " failed to revert cryptfs";
86 }
87 return DestroyDeviceNode(mRawDevPath);
88}
89
90status_t PrivateVolume::doMount() {
91 if (readMetadata()) {
92 LOG(ERROR) << getId() << " failed to read metadata";
93 return -EIO;
94 }
95
Jeff Sharkeyf0121c52015-04-06 14:08:45 -070096 mPath = StringPrintf("/mnt/expand/%s", mFsUuid.c_str());
97 setPath(mPath);
98
99 if (PrepareDir(mPath, 0700, AID_ROOT, AID_ROOT)) {
100 PLOG(ERROR) << getId() << " failed to create mount point " << mPath;
101 return -EIO;
102 }
103
Jeff Sharkey9c484982015-03-31 10:35:33 -0700104 if (Ext4::check(mDmDevPath.c_str(), mPath.c_str())) {
105 PLOG(ERROR) << getId() << " failed filesystem check";
106 return -EIO;
107 }
108
Jeff Sharkey9c484982015-03-31 10:35:33 -0700109 if (Ext4::doMount(mDmDevPath.c_str(), mPath.c_str(), false, false, true)) {
110 PLOG(ERROR) << getId() << " failed to mount";
111 return -EIO;
112 }
113
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700114 // Verify that common directories are ready to roll
115 if (PrepareDir(mPath + "/app", 0771, AID_SYSTEM, AID_SYSTEM) ||
116 PrepareDir(mPath + "/user", 0711, AID_SYSTEM, AID_SYSTEM) ||
117 PrepareDir(mPath + "/media", 0770, AID_MEDIA_RW, AID_MEDIA_RW) ||
118 PrepareDir(mPath + "/local", 0751, AID_ROOT, AID_ROOT) ||
119 PrepareDir(mPath + "/local/tmp", 0771, AID_SHELL, AID_SHELL)) {
120 PLOG(ERROR) << getId() << " failed to prepare";
121 return -EIO;
122 }
123
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700124 // Create a new emulated volume stacked above us, it will automatically
125 // be destroyed during unmount
126 std::string mediaPath(mPath + "/media");
127 auto vol = std::shared_ptr<VolumeBase>(
128 new EmulatedVolume(mediaPath, mRawDevice, mFsUuid));
129 addVolume(vol);
130 vol->create();
131
Jeff Sharkey9c484982015-03-31 10:35:33 -0700132 return OK;
133}
134
135status_t PrivateVolume::doUnmount() {
136 ForceUnmount(mPath);
137
138 if (TEMP_FAILURE_RETRY(rmdir(mPath.c_str()))) {
139 PLOG(ERROR) << getId() << " failed to rmdir mount point " << mPath;
140 }
141
142 return OK;
143}
144
145status_t PrivateVolume::doFormat() {
146 // TODO: change mountpoint once we have selinux labels
147 if (Ext4::format(mDmDevPath.c_str(), 0, "/data")) {
148 PLOG(ERROR) << getId() << " failed to format";
149 return -EIO;
150 }
151
152 return OK;
153}
154
155} // namespace vold
156} // namespace android