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 | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 17 | #include "EmulatedVolume.h" |
| 18 | #include "Utils.h" |
Sudheer Shanka | 40ab674 | 2018-09-18 13:07:45 -0700 | [diff] [blame] | 19 | #include "VolumeManager.h" |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 20 | |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 21 | #include <android-base/logging.h> |
Sudheer Shanka | 53947a3 | 2018-08-01 10:24:13 -0700 | [diff] [blame] | 22 | #include <android-base/stringprintf.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 23 | #include <cutils/fs.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 24 | #include <private/android_filesystem_config.h> |
Jeff Sharkey | 7bdf4d5 | 2017-09-18 14:47:10 -0600 | [diff] [blame] | 25 | #include <utils/Timers.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 26 | |
| 27 | #include <fcntl.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <sys/mount.h> |
| 30 | #include <sys/stat.h> |
Elliott Hughes | 0e08e84 | 2017-05-18 09:08:24 -0700 | [diff] [blame] | 31 | #include <sys/sysmacros.h> |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 32 | #include <sys/types.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 33 | #include <sys/wait.h> |
| 34 | |
Dan Albert | ae9e890 | 2015-03-16 10:35:17 -0700 | [diff] [blame] | 35 | using android::base::StringPrintf; |
| 36 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 37 | namespace android { |
| 38 | namespace vold { |
| 39 | |
| 40 | static const char* kFusePath = "/system/bin/sdcard"; |
| 41 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 42 | EmulatedVolume::EmulatedVolume(const std::string& rawPath) |
| 43 | : VolumeBase(Type::kEmulated), mFusePid(0) { |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 44 | setId("emulated"); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 45 | mRawPath = rawPath; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 46 | mLabel = "emulated"; |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 49 | EmulatedVolume::EmulatedVolume(const std::string& rawPath, dev_t device, const std::string& fsUuid) |
| 50 | : VolumeBase(Type::kEmulated), mFusePid(0) { |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 51 | setId(StringPrintf("emulated:%u,%u", major(device), minor(device))); |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 52 | mRawPath = rawPath; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 53 | mLabel = fsUuid; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 56 | EmulatedVolume::~EmulatedVolume() {} |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 57 | |
| 58 | status_t EmulatedVolume::doMount() { |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 59 | // We could have migrated storage to an adopted private volume, so always |
| 60 | // call primary storage "emulated" to avoid media rescans. |
| 61 | std::string label = mLabel; |
| 62 | if (getMountFlags() & MountFlags::kPrimary) { |
| 63 | label = "emulated"; |
| 64 | } |
| 65 | |
Jeff Sharkey | 1bd078f | 2015-08-06 11:40:00 -0700 | [diff] [blame] | 66 | mFuseDefault = StringPrintf("/mnt/runtime/default/%s", label.c_str()); |
| 67 | mFuseRead = StringPrintf("/mnt/runtime/read/%s", label.c_str()); |
| 68 | mFuseWrite = StringPrintf("/mnt/runtime/write/%s", label.c_str()); |
Sudheer Shanka | dd4bb17 | 2019-01-16 23:35:49 -0800 | [diff] [blame] | 69 | mFuseFull = StringPrintf("/mnt/runtime/full/%s", label.c_str()); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 70 | |
| 71 | setInternalPath(mRawPath); |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 72 | setPath(StringPrintf("/storage/%s", label.c_str())); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 73 | |
| 74 | if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) || |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 75 | fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) || |
Sudheer Shanka | dd4bb17 | 2019-01-16 23:35:49 -0800 | [diff] [blame] | 76 | fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT) || |
| 77 | fs_prepare_dir(mFuseFull.c_str(), 0700, AID_ROOT, AID_ROOT)) { |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 78 | PLOG(ERROR) << getId() << " failed to create mount points"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 79 | return -errno; |
| 80 | } |
| 81 | |
Sudheer Shanka | dd4bb17 | 2019-01-16 23:35:49 -0800 | [diff] [blame] | 82 | dev_t before = GetDevice(mFuseFull); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 83 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 84 | if (!(mFusePid = fork())) { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 85 | // clang-format off |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 86 | if (execl(kFusePath, kFusePath, |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 87 | "-u", "1023", // AID_MEDIA_RW |
| 88 | "-g", "1023", // AID_MEDIA_RW |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 89 | "-m", |
| 90 | "-w", |
Rom Lemarchand | 958c216 | 2017-09-15 18:48:01 +0000 | [diff] [blame] | 91 | "-G", |
Jeff Sharkey | d7e5176 | 2018-01-08 11:48:07 -0700 | [diff] [blame] | 92 | "-i", |
Sudheer Shanka | 8cad97b | 2019-03-05 10:31:27 -0800 | [diff] [blame] | 93 | "-o", |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 94 | mRawPath.c_str(), |
Jeff Sharkey | 81f55c6 | 2015-07-07 14:37:03 -0700 | [diff] [blame] | 95 | label.c_str(), |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 96 | NULL)) { |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 97 | // clang-format on |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 98 | PLOG(ERROR) << "Failed to exec"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 99 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 100 | |
Jeff Sharkey | c7b5b57 | 2015-06-30 15:54:17 -0700 | [diff] [blame] | 101 | LOG(ERROR) << "FUSE exiting"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 102 | _exit(1); |
| 103 | } |
| 104 | |
| 105 | if (mFusePid == -1) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 106 | PLOG(ERROR) << getId() << " failed to fork"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 107 | return -errno; |
| 108 | } |
| 109 | |
Jeff Sharkey | 7bdf4d5 | 2017-09-18 14:47:10 -0600 | [diff] [blame] | 110 | nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME); |
Sudheer Shanka | dd4bb17 | 2019-01-16 23:35:49 -0800 | [diff] [blame] | 111 | while (before == GetDevice(mFuseFull)) { |
Sudheer Shanka | 4b6ca4e | 2018-09-21 10:54:54 -0700 | [diff] [blame] | 112 | LOG(DEBUG) << "Waiting for FUSE to spin up..."; |
Paul Crowley | edf7a4e | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 113 | usleep(50000); // 50ms |
Jeff Sharkey | 7bdf4d5 | 2017-09-18 14:47:10 -0600 | [diff] [blame] | 114 | |
| 115 | nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME); |
| 116 | if (nanoseconds_to_milliseconds(now - start) > 5000) { |
| 117 | LOG(WARNING) << "Timed out while waiting for FUSE to spin up"; |
| 118 | return -ETIMEDOUT; |
| 119 | } |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 120 | } |
Daniel Rosenberg | 1d79d10 | 2017-07-11 17:59:55 -0700 | [diff] [blame] | 121 | /* sdcardfs will have exited already. FUSE will still be running */ |
Daniel Rosenberg | 8b9a5b3 | 2018-03-12 15:47:23 -0700 | [diff] [blame] | 122 | TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0)); |
| 123 | mFusePid = 0; |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 124 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 125 | return OK; |
| 126 | } |
| 127 | |
| 128 | status_t EmulatedVolume::doUnmount() { |
Narayan Kamath | ea243a3 | 2016-01-21 12:26:05 +0000 | [diff] [blame] | 129 | // Unmount the storage before we kill the FUSE process. If we kill |
| 130 | // the FUSE process first, most file system operations will return |
| 131 | // ENOTCONN until the unmount completes. This is an exotic and unusual |
| 132 | // error code and might cause broken behaviour in applications. |
| 133 | KillProcessesUsingPath(getPath()); |
| 134 | ForceUnmount(mFuseDefault); |
| 135 | ForceUnmount(mFuseRead); |
| 136 | ForceUnmount(mFuseWrite); |
Sudheer Shanka | dd4bb17 | 2019-01-16 23:35:49 -0800 | [diff] [blame] | 137 | ForceUnmount(mFuseFull); |
Narayan Kamath | ea243a3 | 2016-01-21 12:26:05 +0000 | [diff] [blame] | 138 | |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 139 | rmdir(mFuseDefault.c_str()); |
| 140 | rmdir(mFuseRead.c_str()); |
| 141 | rmdir(mFuseWrite.c_str()); |
Sudheer Shanka | dd4bb17 | 2019-01-16 23:35:49 -0800 | [diff] [blame] | 142 | rmdir(mFuseFull.c_str()); |
Jeff Sharkey | 66270a2 | 2015-06-24 11:49:24 -0700 | [diff] [blame] | 143 | |
| 144 | mFuseDefault.clear(); |
| 145 | mFuseRead.clear(); |
| 146 | mFuseWrite.clear(); |
Sudheer Shanka | dd4bb17 | 2019-01-16 23:35:49 -0800 | [diff] [blame] | 147 | mFuseFull.clear(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 148 | |
| 149 | return OK; |
| 150 | } |
| 151 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 152 | } // namespace vold |
| 153 | } // namespace android |