OSDN Git Service

Fix building errors on Android 8.1
[android-x86/system-vold.git] / PrivateVolume.cpp
1 /*
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 "fs/Ext4.h"
18 #include "fs/F2fs.h"
19 #include "PrivateVolume.h"
20 #include "EmulatedVolume.h"
21 #include "Utils.h"
22 #include "VolumeManager.h"
23 #include "ResponseCode.h"
24 #include "cryptfs.h"
25
26 #include <android-base/stringprintf.h>
27 #include <android-base/logging.h>
28 #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>
36 #include <sys/sysmacros.h>
37 #include <sys/wait.h>
38 #include <sys/param.h>
39
40 using android::base::StringPrintf;
41
42 namespace android {
43 namespace vold {
44
45 PrivateVolume::PrivateVolume(dev_t device, const std::string& keyRaw) :
46         VolumeBase(Type::kPrivate), mRawDevice(device), mKeyRaw(keyRaw) {
47     setId(StringPrintf("private:%u,%u", major(device), minor(device)));
48     mRawDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
49 }
50
51 PrivateVolume::~PrivateVolume() {
52 }
53
54 status_t PrivateVolume::readMetadata() {
55     status_t res = ReadMetadata(mDmDevPath, mFsType, mFsUuid, mFsLabel);
56     notifyEvent(ResponseCode::VolumeFsTypeChanged, mFsType);
57     notifyEvent(ResponseCode::VolumeFsUuidChanged, mFsUuid);
58     notifyEvent(ResponseCode::VolumeFsLabelChanged, mFsLabel);
59     return res;
60 }
61
62 status_t PrivateVolume::doCreate() {
63     if (CreateDeviceNode(mRawDevPath, mRawDevice)) {
64         return -EIO;
65     }
66
67     // Recover from stale vold by tearing down any old mappings
68     cryptfs_revert_ext_volume(getId().c_str());
69
70     // TODO: figure out better SELinux labels for private volumes
71
72     unsigned char* key = (unsigned char*) mKeyRaw.data();
73     char crypto_blkdev[MAXPATHLEN];
74     int res = cryptfs_setup_ext_volume(getId().c_str(), mRawDevPath.c_str(),
75             key, mKeyRaw.size(), crypto_blkdev);
76     mDmDevPath = crypto_blkdev;
77     if (res != 0) {
78         PLOG(ERROR) << getId() << " failed to setup cryptfs";
79         return -EIO;
80     }
81
82     return OK;
83 }
84
85 status_t PrivateVolume::doDestroy() {
86     if (cryptfs_revert_ext_volume(getId().c_str())) {
87         LOG(ERROR) << getId() << " failed to revert cryptfs";
88     }
89     return DestroyDeviceNode(mRawDevPath);
90 }
91
92 status_t PrivateVolume::doMount() {
93     if (readMetadata()) {
94         LOG(ERROR) << getId() << " failed to read metadata";
95         return -EIO;
96     }
97
98     mPath = StringPrintf("/mnt/expand/%s", mFsUuid.c_str());
99     setPath(mPath);
100
101     if (PrepareDir(mPath, 0700, AID_ROOT, AID_ROOT)) {
102         PLOG(ERROR) << getId() << " failed to create mount point " << mPath;
103         return -EIO;
104     }
105
106     if (mFsType == "ext4") {
107         int res = ext4::Check(mDmDevPath, mPath, true);
108         if (res == 0 || res == 1) {
109             LOG(DEBUG) << getId() << " passed filesystem check";
110         } else {
111             PLOG(ERROR) << getId() << " failed filesystem check";
112             return -EIO;
113         }
114
115         if (ext4::Mount(mDmDevPath, mPath, false, false, true)) {
116             PLOG(ERROR) << getId() << " failed to mount";
117             return -EIO;
118         }
119
120     } else if (mFsType == "f2fs") {
121         int res = f2fs::Check(mDmDevPath, true);
122         if (res == 0) {
123             LOG(DEBUG) << getId() << " passed filesystem check";
124         } else {
125             PLOG(ERROR) << getId() << " failed filesystem check";
126             return -EIO;
127         }
128
129         if (f2fs::Mount(mDmDevPath, mPath, "")) {
130             PLOG(ERROR) << getId() << " failed to mount";
131             return -EIO;
132         }
133
134     } else {
135         LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
136         return -EIO;
137     }
138
139     RestoreconRecursive(mPath);
140
141     // Verify that common directories are ready to roll
142     if (PrepareDir(mPath + "/app", 0771, AID_SYSTEM, AID_SYSTEM) ||
143             PrepareDir(mPath + "/user", 0711, AID_SYSTEM, AID_SYSTEM) ||
144             PrepareDir(mPath + "/user_de", 0711, AID_SYSTEM, AID_SYSTEM) ||
145             PrepareDir(mPath + "/media", 0770, AID_MEDIA_RW, AID_MEDIA_RW) ||
146             PrepareDir(mPath + "/media/0", 0770, AID_MEDIA_RW, AID_MEDIA_RW) ||
147             PrepareDir(mPath + "/local", 0751, AID_ROOT, AID_ROOT) ||
148             PrepareDir(mPath + "/local/tmp", 0771, AID_SHELL, AID_SHELL)) {
149         PLOG(ERROR) << getId() << " failed to prepare";
150         return -EIO;
151     }
152
153     // Create a new emulated volume stacked above us, it will automatically
154     // be destroyed during unmount
155     std::string mediaPath(mPath + "/media");
156     auto vol = std::shared_ptr<VolumeBase>(
157             new EmulatedVolume(mediaPath, mRawDevice, mFsUuid));
158     addVolume(vol);
159     vol->create();
160
161     return OK;
162 }
163
164 status_t PrivateVolume::doUnmount() {
165     ForceUnmount(mPath);
166
167     if (TEMP_FAILURE_RETRY(rmdir(mPath.c_str()))) {
168         PLOG(ERROR) << getId() << " failed to rmdir mount point " << mPath;
169     }
170
171     return OK;
172 }
173
174 status_t PrivateVolume::doFormat(const std::string& fsType) {
175     std::string resolvedFsType = fsType;
176     if (fsType == "auto") {
177         // For now, assume that all MMC devices are flash-based SD cards, and
178         // give everyone else ext4 because sysfs rotational isn't reliable.
179         if ((major(mRawDevice) == Disk::kMajorBlockMmc) && f2fs::IsSupported()) {
180             resolvedFsType = "f2fs";
181         } else {
182             resolvedFsType = "ext4";
183         }
184         LOG(DEBUG) << "Resolved auto to " << resolvedFsType;
185     }
186
187     if (resolvedFsType == "ext4") {
188         // TODO: change reported mountpoint once we have better selinux support
189         if (ext4::Format(mDmDevPath, 0, "/data")) {
190             PLOG(ERROR) << getId() << " failed to format";
191             return -EIO;
192         }
193     } else if (resolvedFsType == "f2fs") {
194         if (f2fs::Format(mDmDevPath)) {
195             PLOG(ERROR) << getId() << " failed to format";
196             return -EIO;
197         }
198     } else {
199         LOG(ERROR) << getId() << " unsupported filesystem " << fsType;
200         return -EINVAL;
201     }
202
203     return OK;
204 }
205
206 }  // namespace vold
207 }  // namespace android