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