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 | |
| 17 | #include "Ext4.h" |
| 18 | #include "PrivateVolume.h" |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 19 | #include "EmulatedVolume.h" |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 20 | #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 | |
| 38 | using android::base::StringPrintf; |
| 39 | |
| 40 | namespace android { |
| 41 | namespace vold { |
| 42 | |
| 43 | PrivateVolume::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 Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | PrivateVolume::~PrivateVolume() { |
| 50 | } |
| 51 | |
| 52 | status_t PrivateVolume::readMetadata() { |
| 53 | status_t res = ReadMetadata(mDmDevPath, mFsType, mFsUuid, mFsLabel); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 54 | notifyEvent(ResponseCode::VolumeFsTypeChanged, mFsType); |
| 55 | notifyEvent(ResponseCode::VolumeFsUuidChanged, mFsUuid); |
| 56 | notifyEvent(ResponseCode::VolumeFsLabelChanged, mFsLabel); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 57 | return res; |
| 58 | } |
| 59 | |
| 60 | status_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 | |
| 83 | status_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 | |
| 90 | status_t PrivateVolume::doMount() { |
| 91 | if (readMetadata()) { |
| 92 | LOG(ERROR) << getId() << " failed to read metadata"; |
| 93 | return -EIO; |
| 94 | } |
| 95 | |
Jeff Sharkey | f0121c5 | 2015-04-06 14:08:45 -0700 | [diff] [blame] | 96 | 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 Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 104 | int res = Ext4::check(mDmDevPath.c_str(), mPath.c_str()); |
| 105 | if (res == 0 || res == 1) { |
| 106 | LOG(DEBUG) << getId() << " passed filesystem check"; |
| 107 | } else { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 108 | PLOG(ERROR) << getId() << " failed filesystem check"; |
| 109 | return -EIO; |
| 110 | } |
| 111 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 112 | if (Ext4::doMount(mDmDevPath.c_str(), mPath.c_str(), false, false, true)) { |
| 113 | PLOG(ERROR) << getId() << " failed to mount"; |
| 114 | return -EIO; |
| 115 | } |
| 116 | |
Jeff Sharkey | f0121c5 | 2015-04-06 14:08:45 -0700 | [diff] [blame] | 117 | // Verify that common directories are ready to roll |
| 118 | if (PrepareDir(mPath + "/app", 0771, AID_SYSTEM, AID_SYSTEM) || |
| 119 | PrepareDir(mPath + "/user", 0711, AID_SYSTEM, AID_SYSTEM) || |
| 120 | PrepareDir(mPath + "/media", 0770, AID_MEDIA_RW, AID_MEDIA_RW) || |
| 121 | PrepareDir(mPath + "/local", 0751, AID_ROOT, AID_ROOT) || |
| 122 | PrepareDir(mPath + "/local/tmp", 0771, AID_SHELL, AID_SHELL)) { |
| 123 | PLOG(ERROR) << getId() << " failed to prepare"; |
| 124 | return -EIO; |
| 125 | } |
| 126 | |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 127 | // Create a new emulated volume stacked above us, it will automatically |
| 128 | // be destroyed during unmount |
| 129 | std::string mediaPath(mPath + "/media"); |
| 130 | auto vol = std::shared_ptr<VolumeBase>( |
| 131 | new EmulatedVolume(mediaPath, mRawDevice, mFsUuid)); |
| 132 | addVolume(vol); |
| 133 | vol->create(); |
| 134 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 135 | return OK; |
| 136 | } |
| 137 | |
| 138 | status_t PrivateVolume::doUnmount() { |
| 139 | ForceUnmount(mPath); |
| 140 | |
| 141 | if (TEMP_FAILURE_RETRY(rmdir(mPath.c_str()))) { |
| 142 | PLOG(ERROR) << getId() << " failed to rmdir mount point " << mPath; |
| 143 | } |
| 144 | |
| 145 | return OK; |
| 146 | } |
| 147 | |
| 148 | status_t PrivateVolume::doFormat() { |
| 149 | // TODO: change mountpoint once we have selinux labels |
| 150 | if (Ext4::format(mDmDevPath.c_str(), 0, "/data")) { |
| 151 | PLOG(ERROR) << getId() << " failed to format"; |
| 152 | return -EIO; |
| 153 | } |
| 154 | |
| 155 | return OK; |
| 156 | } |
| 157 | |
| 158 | } // namespace vold |
| 159 | } // namespace android |