Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [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/Vfat.h" |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 18 | #include "PublicVolume.h" |
| 19 | #include "Utils.h" |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 20 | #include "VolumeManager.h" |
| 21 | #include "ResponseCode.h" |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 22 | |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 23 | #include <android-base/stringprintf.h> |
| 24 | #include <android-base/logging.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 25 | #include <cutils/fs.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 26 | #include <private/android_filesystem_config.h> |
| 27 | |
| 28 | #include <fcntl.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <sys/mount.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <sys/types.h> |
Elliott Hughes | 0e08e84 | 2017-05-18 09:08:24 -0700 | [diff] [blame] | 33 | #include <sys/sysmacros.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 34 | #include <sys/wait.h> |
| 35 | |
Dan Albert | ae9e890 | 2015-03-16 10:35:17 -0700 | [diff] [blame] | 36 | using android::base::StringPrintf; |
| 37 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 38 | namespace android { |
| 39 | namespace vold { |
| 40 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 41 | static const char* kFusePath = "/system/bin/sdcard"; |
| 42 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 43 | static const char* kAsecPath = "/mnt/secure/asec"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 44 | |
| 45 | PublicVolume::PublicVolume(dev_t device) : |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 46 | VolumeBase(Type::kPublic), mDevice(device), mFusePid(0) { |
| 47 | setId(StringPrintf("public:%u,%u", major(device), minor(device))); |
| 48 | mDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str()); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | PublicVolume::~PublicVolume() { |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | status_t PublicVolume::readMetadata() { |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 55 | status_t res = ReadMetadataUntrusted(mDevPath, mFsType, mFsUuid, mFsLabel); |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 56 | #if ENABLE_BINDER |
| 57 | auto listener = getListener(); |
| 58 | if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel); |
| 59 | #else |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 60 | notifyEvent(ResponseCode::VolumeFsTypeChanged, mFsType); |
| 61 | notifyEvent(ResponseCode::VolumeFsUuidChanged, mFsUuid); |
| 62 | notifyEvent(ResponseCode::VolumeFsLabelChanged, mFsLabel); |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 63 | #endif |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 64 | return res; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | status_t PublicVolume::initAsecStage() { |
| 68 | std::string legacyPath(mRawPath + "/android_secure"); |
| 69 | std::string securePath(mRawPath + "/.android_secure"); |
| 70 | |
| 71 | // Recover legacy secure path |
| 72 | if (!access(legacyPath.c_str(), R_OK | X_OK) |
| 73 | && access(securePath.c_str(), R_OK | X_OK)) { |
| 74 | if (rename(legacyPath.c_str(), securePath.c_str())) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 75 | PLOG(WARNING) << getId() << " failed to rename legacy ASEC dir"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 79 | if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) { |
| 80 | if (errno != EEXIST) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 81 | PLOG(WARNING) << getId() << " creating ASEC stage failed"; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 82 | return -errno; |
| 83 | } |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 86 | BindMount(securePath, kAsecPath); |
| 87 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 88 | return OK; |
| 89 | } |
| 90 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 91 | status_t PublicVolume::doCreate() { |
| 92 | return CreateDeviceNode(mDevPath, mDevice); |
| 93 | } |
| 94 | |
| 95 | status_t PublicVolume::doDestroy() { |
| 96 | return DestroyDeviceNode(mDevPath); |
| 97 | } |
| 98 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 99 | status_t PublicVolume::doMount() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 100 | // TODO: expand to support mounting other filesystems |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 101 | readMetadata(); |
| 102 | |
Makoto Onuki | c82c9ce | 2015-06-24 13:30:45 -0700 | [diff] [blame] | 103 | if (mFsType != "vfat") { |
| 104 | LOG(ERROR) << getId() << " unsupported filesystem " << mFsType; |
| 105 | return -EIO; |
| 106 | } |
| 107 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 108 | if (vfat::Check(mDevPath)) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 109 | LOG(ERROR) << getId() << " failed filesystem check"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 110 | return -EIO; |
| 111 | } |
| 112 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 113 | // Use UUID as stable name, if available |
| 114 | std::string stableName = getId(); |
| 115 | if (!mFsUuid.empty()) { |
Jeff Sharkey | f0121c5 | 2015-04-06 14:08:45 -0700 | [diff] [blame] | 116 | stableName = mFsUuid; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | mRawPath = StringPrintf("/mnt/media_rw/%s", stableName.c_str()); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 120 | |
Jeff Sharkey | 1bd078f | 2015-08-06 11:40:00 -0700 | [diff] [blame] | 121 | mFuseDefault = StringPrintf("/mnt/runtime/default/%s", stableName.c_str()); |
| 122 | mFuseRead = StringPrintf("/mnt/runtime/read/%s", stableName.c_str()); |
| 123 | mFuseWrite = StringPrintf("/mnt/runtime/write/%s", stableName.c_str()); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 124 | |
| 125 | setInternalPath(mRawPath); |
Jeff Sharkey | 8474ee3 | 2015-07-30 16:54:23 -0700 | [diff] [blame] | 126 | if (getMountFlags() & MountFlags::kVisible) { |
| 127 | setPath(StringPrintf("/storage/%s", stableName.c_str())); |
| 128 | } else { |
| 129 | setPath(mRawPath); |
| 130 | } |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 131 | |
Qin Chao | e0074f1 | 2015-12-15 15:20:41 +0800 | [diff] [blame] | 132 | if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT)) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 133 | PLOG(ERROR) << getId() << " failed to create mount points"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 134 | return -errno; |
| 135 | } |
| 136 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 137 | if (vfat::Mount(mDevPath, mRawPath, false, false, false, |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 138 | AID_MEDIA_RW, AID_MEDIA_RW, 0007, true)) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 139 | PLOG(ERROR) << getId() << " failed to mount " << mDevPath; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 140 | return -EIO; |
| 141 | } |
| 142 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 143 | if (getMountFlags() & MountFlags::kPrimary) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 144 | initAsecStage(); |
| 145 | } |
| 146 | |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 147 | if (!(getMountFlags() & MountFlags::kVisible)) { |
| 148 | // Not visible to apps, so no need to spin up FUSE |
| 149 | return OK; |
| 150 | } |
| 151 | |
Qin Chao | e0074f1 | 2015-12-15 15:20:41 +0800 | [diff] [blame] | 152 | if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) || |
| 153 | fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) || |
| 154 | fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT)) { |
| 155 | PLOG(ERROR) << getId() << " failed to create FUSE mount points"; |
| 156 | return -errno; |
| 157 | } |
| 158 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 159 | dev_t before = GetDevice(mFuseWrite); |
| 160 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 161 | if (!(mFusePid = fork())) { |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 162 | if (getMountFlags() & MountFlags::kPrimary) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 163 | if (execl(kFusePath, kFusePath, |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 164 | "-u", "1023", // AID_MEDIA_RW |
| 165 | "-g", "1023", // AID_MEDIA_RW |
Jeff Sharkey | 20642ae | 2015-07-28 10:57:29 -0700 | [diff] [blame] | 166 | "-U", std::to_string(getMountUserId()).c_str(), |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 167 | "-w", |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 168 | mRawPath.c_str(), |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 169 | stableName.c_str(), |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 170 | NULL)) { |
| 171 | PLOG(ERROR) << "Failed to exec"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 172 | } |
| 173 | } else { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 174 | if (execl(kFusePath, kFusePath, |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 175 | "-u", "1023", // AID_MEDIA_RW |
| 176 | "-g", "1023", // AID_MEDIA_RW |
Jeff Sharkey | 20642ae | 2015-07-28 10:57:29 -0700 | [diff] [blame] | 177 | "-U", std::to_string(getMountUserId()).c_str(), |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 178 | mRawPath.c_str(), |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 179 | stableName.c_str(), |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 180 | NULL)) { |
| 181 | PLOG(ERROR) << "Failed to exec"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 185 | LOG(ERROR) << "FUSE exiting"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 186 | _exit(1); |
| 187 | } |
| 188 | |
| 189 | if (mFusePid == -1) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 190 | PLOG(ERROR) << getId() << " failed to fork"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 191 | return -errno; |
| 192 | } |
| 193 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 194 | while (before == GetDevice(mFuseWrite)) { |
| 195 | LOG(VERBOSE) << "Waiting for FUSE to spin up..."; |
| 196 | usleep(50000); // 50ms |
| 197 | } |
Daniel Rosenberg | 1d79d10 | 2017-07-11 17:59:55 -0700 | [diff] [blame] | 198 | /* sdcardfs will have exited already. FUSE will still be running */ |
| 199 | TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, WNOHANG)); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 200 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 201 | return OK; |
| 202 | } |
| 203 | |
| 204 | status_t PublicVolume::doUnmount() { |
John Cormie | 25cc7e3 | 2016-04-18 14:23:29 -0700 | [diff] [blame] | 205 | // Unmount the storage before we kill the FUSE process. If we kill |
| 206 | // the FUSE process first, most file system operations will return |
| 207 | // ENOTCONN until the unmount completes. This is an exotic and unusual |
| 208 | // error code and might cause broken behaviour in applications. |
Jeff Sharkey | 8aff854 | 2016-03-30 20:37:28 -0600 | [diff] [blame] | 209 | KillProcessesUsingPath(getPath()); |
| 210 | |
Jeff Sharkey | 48d81d3 | 2015-04-12 21:50:32 -0700 | [diff] [blame] | 211 | ForceUnmount(kAsecPath); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 212 | |
| 213 | ForceUnmount(mFuseDefault); |
| 214 | ForceUnmount(mFuseRead); |
| 215 | ForceUnmount(mFuseWrite); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 216 | ForceUnmount(mRawPath); |
| 217 | |
John Cormie | 25cc7e3 | 2016-04-18 14:23:29 -0700 | [diff] [blame] | 218 | if (mFusePid > 0) { |
| 219 | kill(mFusePid, SIGTERM); |
| 220 | TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0)); |
| 221 | mFusePid = 0; |
| 222 | } |
| 223 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 224 | rmdir(mFuseDefault.c_str()); |
| 225 | rmdir(mFuseRead.c_str()); |
| 226 | rmdir(mFuseWrite.c_str()); |
| 227 | rmdir(mRawPath.c_str()); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 228 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 229 | mFuseDefault.clear(); |
| 230 | mFuseRead.clear(); |
| 231 | mFuseWrite.clear(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 232 | mRawPath.clear(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 233 | |
| 234 | return OK; |
| 235 | } |
| 236 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 237 | status_t PublicVolume::doFormat(const std::string& fsType) { |
| 238 | if (fsType == "vfat" || fsType == "auto") { |
| 239 | if (WipeBlockDevice(mDevPath) != OK) { |
| 240 | LOG(WARNING) << getId() << " failed to wipe"; |
| 241 | } |
| 242 | if (vfat::Format(mDevPath, 0)) { |
| 243 | LOG(ERROR) << getId() << " failed to format"; |
| 244 | return -errno; |
| 245 | } |
| 246 | } else { |
| 247 | LOG(ERROR) << "Unsupported filesystem " << fsType; |
| 248 | return -EINVAL; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 249 | } |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 250 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 251 | return OK; |
| 252 | } |
| 253 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 254 | } // namespace vold |
| 255 | } // namespace android |