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> |
Ricky Wai | 9eb4367 | 2020-02-15 01:15:42 +0000 | [diff] [blame] | 39 | #include <thread> |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 40 | |
Shivaprasad Hongal | 8ee3fdb | 2018-10-24 17:49:39 -0700 | [diff] [blame] | 41 | #define RETRY_MOUNT_ATTEMPTS 10 |
| 42 | #define RETRY_MOUNT_DELAY_SECONDS 1 |
| 43 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 44 | using android::base::StringPrintf; |
Alistair Delva | c671731 | 2020-05-19 15:49:26 -0700 | [diff] [blame] | 45 | using android::vold::IsVirtioBlkDevice; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 46 | |
| 47 | namespace android { |
| 48 | namespace vold { |
| 49 | |
Martijn Coenen | 6c695ef | 2020-03-11 15:33:22 +0100 | [diff] [blame] | 50 | static const unsigned int kMajorBlockLoop = 7; |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 51 | static const unsigned int kMajorBlockMmc = 179; |
| 52 | |
Paul Crowley | 3d98f5d | 2020-02-07 11:49:09 -0800 | [diff] [blame] | 53 | PrivateVolume::PrivateVolume(dev_t device, const KeyBuffer& keyRaw) |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 54 | : VolumeBase(Type::kPrivate), mRawDevice(device), mKeyRaw(keyRaw) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 55 | setId(StringPrintf("private:%u,%u", major(device), minor(device))); |
| 56 | mRawDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str()); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 59 | PrivateVolume::~PrivateVolume() {} |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 60 | |
| 61 | status_t PrivateVolume::readMetadata() { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 62 | status_t res = ReadMetadata(mDmDevPath, &mFsType, &mFsUuid, &mFsLabel); |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 63 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 64 | auto listener = getListener(); |
| 65 | if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel); |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 66 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 67 | return res; |
| 68 | } |
| 69 | |
| 70 | status_t PrivateVolume::doCreate() { |
| 71 | if (CreateDeviceNode(mRawDevPath, mRawDevice)) { |
| 72 | return -EIO; |
| 73 | } |
| 74 | |
| 75 | // Recover from stale vold by tearing down any old mappings |
Paul Crowley | 659b63f | 2020-02-07 12:15:56 -0800 | [diff] [blame] | 76 | auto& dm = dm::DeviceMapper::Instance(); |
Ricky Wai | 9eb4367 | 2020-02-15 01:15:42 +0000 | [diff] [blame] | 77 | // TODO(b/149396179) there appears to be a race somewhere in the system where trying |
| 78 | // to delete the device fails with EBUSY; for now, work around this by retrying. |
| 79 | bool ret; |
| 80 | int tries = 10; |
| 81 | while (tries-- > 0) { |
| 82 | ret = dm.DeleteDeviceIfExists(getId()); |
| 83 | if (ret || errno != EBUSY) { |
| 84 | break; |
| 85 | } |
Paul Crowley | 659b63f | 2020-02-07 12:15:56 -0800 | [diff] [blame] | 86 | PLOG(ERROR) << "Cannot remove dm device " << getId(); |
Ricky Wai | 9eb4367 | 2020-02-15 01:15:42 +0000 | [diff] [blame] | 87 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 88 | } |
| 89 | if (!ret) { |
Paul Crowley | 659b63f | 2020-02-07 12:15:56 -0800 | [diff] [blame] | 90 | return -EIO; |
| 91 | } |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 92 | |
| 93 | // TODO: figure out better SELinux labels for private volumes |
| 94 | |
Paul Crowley | 886e572 | 2020-02-07 12:51:56 -0800 | [diff] [blame] | 95 | if (!setup_ext_volume(getId(), mRawDevPath, mKeyRaw, &mDmDevPath)) { |
| 96 | LOG(ERROR) << getId() << " failed to setup metadata encryption"; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 97 | return -EIO; |
| 98 | } |
| 99 | |
Shivaprasad Hongal | 8ee3fdb | 2018-10-24 17:49:39 -0700 | [diff] [blame] | 100 | int fd = 0; |
| 101 | int retries = RETRY_MOUNT_ATTEMPTS; |
| 102 | while ((fd = open(mDmDevPath.c_str(), O_WRONLY|O_CLOEXEC)) < 0) { |
| 103 | if (retries > 0) { |
| 104 | retries--; |
| 105 | PLOG(ERROR) << "Error opening crypto_blkdev " << mDmDevPath |
| 106 | << " for private volume. err=" << errno |
| 107 | << "(" << strerror(errno) << "), retrying for the " |
| 108 | << RETRY_MOUNT_ATTEMPTS - retries << " time"; |
| 109 | sleep(RETRY_MOUNT_DELAY_SECONDS); |
| 110 | } else { |
| 111 | PLOG(ERROR) << "Error opening crypto_blkdev " << mDmDevPath |
| 112 | << " for private volume. err=" << errno |
| 113 | << "(" << strerror(errno) << "), retried " |
| 114 | << RETRY_MOUNT_ATTEMPTS << " times"; |
| 115 | close(fd); |
| 116 | return -EIO; |
| 117 | } |
| 118 | } |
| 119 | close(fd); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 120 | return OK; |
| 121 | } |
| 122 | |
| 123 | status_t PrivateVolume::doDestroy() { |
Paul Crowley | 659b63f | 2020-02-07 12:15:56 -0800 | [diff] [blame] | 124 | auto& dm = dm::DeviceMapper::Instance(); |
Ricky Wai | 9eb4367 | 2020-02-15 01:15:42 +0000 | [diff] [blame] | 125 | // TODO(b/149396179) there appears to be a race somewhere in the system where trying |
| 126 | // to delete the device fails with EBUSY; for now, work around this by retrying. |
| 127 | bool ret; |
| 128 | int tries = 10; |
| 129 | while (tries-- > 0) { |
| 130 | ret = dm.DeleteDevice(getId()); |
| 131 | if (ret || errno != EBUSY) { |
| 132 | break; |
| 133 | } |
Paul Crowley | 659b63f | 2020-02-07 12:15:56 -0800 | [diff] [blame] | 134 | PLOG(ERROR) << "Cannot remove dm device " << getId(); |
Ricky Wai | 9eb4367 | 2020-02-15 01:15:42 +0000 | [diff] [blame] | 135 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 136 | } |
| 137 | if (!ret) { |
Paul Crowley | 659b63f | 2020-02-07 12:15:56 -0800 | [diff] [blame] | 138 | return -EIO; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 139 | } |
| 140 | return DestroyDeviceNode(mRawDevPath); |
| 141 | } |
| 142 | |
| 143 | status_t PrivateVolume::doMount() { |
| 144 | if (readMetadata()) { |
| 145 | LOG(ERROR) << getId() << " failed to read metadata"; |
| 146 | return -EIO; |
| 147 | } |
| 148 | |
Jeff Sharkey | f0121c5 | 2015-04-06 14:08:45 -0700 | [diff] [blame] | 149 | mPath = StringPrintf("/mnt/expand/%s", mFsUuid.c_str()); |
| 150 | setPath(mPath); |
| 151 | |
| 152 | if (PrepareDir(mPath, 0700, AID_ROOT, AID_ROOT)) { |
| 153 | PLOG(ERROR) << getId() << " failed to create mount point " << mPath; |
| 154 | return -EIO; |
| 155 | } |
| 156 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 157 | if (mFsType == "ext4") { |
Alexander Martinz | 7226888 | 2023-09-28 14:43:16 +0200 | [diff] [blame] | 158 | int res = ext4::Check(mDmDevPath, mPath); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 159 | if (res == 0 || res == 1) { |
| 160 | LOG(DEBUG) << getId() << " passed filesystem check"; |
| 161 | } else { |
| 162 | PLOG(ERROR) << getId() << " failed filesystem check"; |
| 163 | return -EIO; |
| 164 | } |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 165 | |
Alexander Martinz | 7226888 | 2023-09-28 14:43:16 +0200 | [diff] [blame] | 166 | if (ext4::Mount(mDmDevPath, mPath, false, false, true)) { |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 167 | PLOG(ERROR) << getId() << " failed to mount"; |
| 168 | return -EIO; |
| 169 | } |
| 170 | |
| 171 | } else if (mFsType == "f2fs") { |
Alexander Martinz | 7226888 | 2023-09-28 14:43:16 +0200 | [diff] [blame] | 172 | int res = f2fs::Check(mDmDevPath); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 173 | if (res == 0) { |
| 174 | LOG(DEBUG) << getId() << " passed filesystem check"; |
| 175 | } else { |
| 176 | PLOG(ERROR) << getId() << " failed filesystem check"; |
| 177 | return -EIO; |
| 178 | } |
| 179 | |
Alexander Martinz | 7226888 | 2023-09-28 14:43:16 +0200 | [diff] [blame] | 180 | if (f2fs::Mount(mDmDevPath, mPath)) { |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 181 | PLOG(ERROR) << getId() << " failed to mount"; |
| 182 | return -EIO; |
| 183 | } |
| 184 | |
| 185 | } else { |
| 186 | LOG(ERROR) << getId() << " unsupported filesystem " << mFsType; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 187 | return -EIO; |
| 188 | } |
| 189 | |
Jeff Sharkey | d24aeda | 2016-07-15 16:20:22 -0600 | [diff] [blame] | 190 | RestoreconRecursive(mPath); |
Jeff Sharkey | 3482412 | 2015-06-09 10:59:17 -0700 | [diff] [blame] | 191 | |
Daniel Rosenberg | 3bb8664 | 2020-08-12 18:31:43 -0700 | [diff] [blame] | 192 | int attrs = 0; |
| 193 | if (!IsSdcardfsUsed()) attrs = FS_CASEFOLD_FL; |
| 194 | |
Jeff Sharkey | f0121c5 | 2015-04-06 14:08:45 -0700 | [diff] [blame] | 195 | // Verify that common directories are ready to roll |
| 196 | if (PrepareDir(mPath + "/app", 0771, AID_SYSTEM, AID_SYSTEM) || |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 197 | PrepareDir(mPath + "/user", 0711, AID_SYSTEM, AID_SYSTEM) || |
| 198 | PrepareDir(mPath + "/user_de", 0711, AID_SYSTEM, AID_SYSTEM) || |
Mohammad Samiul Islam | b459591 | 2022-03-07 20:27:06 +0000 | [diff] [blame] | 199 | PrepareDir(mPath + "/misc_ce", 0711, AID_SYSTEM, AID_SYSTEM) || |
| 200 | PrepareDir(mPath + "/misc_de", 0711, AID_SYSTEM, AID_SYSTEM) || |
Daniel Rosenberg | 3bb8664 | 2020-08-12 18:31:43 -0700 | [diff] [blame] | 201 | PrepareDir(mPath + "/media", 0770, AID_MEDIA_RW, AID_MEDIA_RW, attrs) || |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 202 | PrepareDir(mPath + "/media/0", 0770, AID_MEDIA_RW, AID_MEDIA_RW) || |
| 203 | PrepareDir(mPath + "/local", 0751, AID_ROOT, AID_ROOT) || |
| 204 | PrepareDir(mPath + "/local/tmp", 0771, AID_SHELL, AID_SHELL)) { |
Jeff Sharkey | f0121c5 | 2015-04-06 14:08:45 -0700 | [diff] [blame] | 205 | PLOG(ERROR) << getId() << " failed to prepare"; |
| 206 | return -EIO; |
| 207 | } |
| 208 | |
Martijn Coenen | 5ec8658 | 2020-05-04 14:57:35 +0200 | [diff] [blame] | 209 | return OK; |
| 210 | } |
| 211 | |
| 212 | void PrivateVolume::doPostMount() { |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 213 | auto vol_manager = VolumeManager::Instance(); |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 214 | std::string mediaPath(mPath + "/media"); |
Zim | a438b24 | 2019-09-25 14:37:38 +0100 | [diff] [blame] | 215 | |
| 216 | // Create a new emulated volume stacked above us for all added users, they will automatically |
| 217 | // be destroyed during unmount |
| 218 | for (userid_t user : vol_manager->getStartedUsers()) { |
| 219 | auto vol = std::shared_ptr<VolumeBase>( |
| 220 | new EmulatedVolume(mediaPath, mRawDevice, mFsUuid, user)); |
| 221 | vol->setMountUserId(user); |
| 222 | addVolume(vol); |
| 223 | vol->create(); |
| 224 | } |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | status_t PrivateVolume::doUnmount() { |
| 228 | ForceUnmount(mPath); |
| 229 | |
| 230 | if (TEMP_FAILURE_RETRY(rmdir(mPath.c_str()))) { |
| 231 | PLOG(ERROR) << getId() << " failed to rmdir mount point " << mPath; |
| 232 | } |
| 233 | |
| 234 | return OK; |
| 235 | } |
| 236 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 237 | status_t PrivateVolume::doFormat(const std::string& fsType) { |
| 238 | std::string resolvedFsType = fsType; |
| 239 | if (fsType == "auto") { |
| 240 | // For now, assume that all MMC devices are flash-based SD cards, and |
| 241 | // give everyone else ext4 because sysfs rotational isn't reliable. |
Alistair Delva | c671731 | 2020-05-19 15:49:26 -0700 | [diff] [blame] | 242 | // Additionally, prefer f2fs for loop-based devices |
| 243 | if ((major(mRawDevice) == kMajorBlockMmc || |
| 244 | major(mRawDevice) == kMajorBlockLoop || |
| 245 | IsVirtioBlkDevice(major(mRawDevice))) && f2fs::IsSupported()) { |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 246 | resolvedFsType = "f2fs"; |
| 247 | } else { |
| 248 | resolvedFsType = "ext4"; |
| 249 | } |
| 250 | LOG(DEBUG) << "Resolved auto to " << resolvedFsType; |
| 251 | } |
| 252 | |
| 253 | if (resolvedFsType == "ext4") { |
| 254 | // TODO: change reported mountpoint once we have better selinux support |
| 255 | if (ext4::Format(mDmDevPath, 0, "/data")) { |
| 256 | PLOG(ERROR) << getId() << " failed to format"; |
| 257 | return -EIO; |
| 258 | } |
| 259 | } else if (resolvedFsType == "f2fs") { |
| 260 | if (f2fs::Format(mDmDevPath)) { |
| 261 | PLOG(ERROR) << getId() << " failed to format"; |
| 262 | return -EIO; |
| 263 | } |
| 264 | } else { |
| 265 | LOG(ERROR) << getId() << " unsupported filesystem " << fsType; |
| 266 | return -EINVAL; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | return OK; |
| 270 | } |
| 271 | |
| 272 | } // namespace vold |
| 273 | } // namespace android |