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