blob: dc7c3c14f60b5f64f8b6ed9c9bc885b35ece6f61 [file] [log] [blame]
Jeff Sharkeydeb24052015-03-02 21:01:40 -08001/*
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 Sharkeydeb24052015-03-02 21:01:40 -080017#include "PublicVolume.h"
18#include "Utils.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070019#include "VolumeManager.h"
Jeff Sharkey37ba1252018-01-19 10:55:18 +090020#include "fs/Exfat.h"
21#include "fs/Vfat.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080022
Elliott Hughes7e128fb2015-12-04 15:50:53 -080023#include <android-base/logging.h>
Paul Crowley8915d622018-09-18 15:14:18 -070024#include <android-base/stringprintf.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080025#include <cutils/fs.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080026#include <private/android_filesystem_config.h>
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -060027#include <utils/Timers.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080028
29#include <fcntl.h>
30#include <stdlib.h>
31#include <sys/mount.h>
32#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070033#include <sys/sysmacros.h>
Paul Crowley8915d622018-09-18 15:14:18 -070034#include <sys/types.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080035#include <sys/wait.h>
36
Dan Albertae9e8902015-03-16 10:35:17 -070037using android::base::StringPrintf;
38
Jeff Sharkeydeb24052015-03-02 21:01:40 -080039namespace android {
40namespace vold {
41
Jeff Sharkeydeb24052015-03-02 21:01:40 -080042static const char* kFusePath = "/system/bin/sdcard";
43
Jeff Sharkey36801cc2015-03-13 16:09:20 -070044static const char* kAsecPath = "/mnt/secure/asec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080045
Paul Crowley8915d622018-09-18 15:14:18 -070046PublicVolume::PublicVolume(dev_t device) : VolumeBase(Type::kPublic), mDevice(device), mFusePid(0) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070047 setId(StringPrintf("public:%u,%u", major(device), minor(device)));
48 mDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -080049}
50
Paul Crowley8915d622018-09-18 15:14:18 -070051PublicVolume::~PublicVolume() {}
Jeff Sharkeydeb24052015-03-02 21:01:40 -080052
53status_t PublicVolume::readMetadata() {
Jeff Sharkey3472e522017-10-06 18:02:53 -060054 status_t res = ReadMetadataUntrusted(mDevPath, &mFsType, &mFsUuid, &mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060055
Jeff Sharkey814e9d32017-09-13 11:49:44 -060056 auto listener = getListener();
57 if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060058
Jeff Sharkey36801cc2015-03-13 16:09:20 -070059 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080060}
61
62status_t PublicVolume::initAsecStage() {
63 std::string legacyPath(mRawPath + "/android_secure");
64 std::string securePath(mRawPath + "/.android_secure");
65
66 // Recover legacy secure path
Paul Crowley8915d622018-09-18 15:14:18 -070067 if (!access(legacyPath.c_str(), R_OK | X_OK) && access(securePath.c_str(), R_OK | X_OK)) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -080068 if (rename(legacyPath.c_str(), securePath.c_str())) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070069 PLOG(WARNING) << getId() << " failed to rename legacy ASEC dir";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080070 }
71 }
72
Jeff Sharkey36801cc2015-03-13 16:09:20 -070073 if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) {
74 if (errno != EEXIST) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070075 PLOG(WARNING) << getId() << " creating ASEC stage failed";
Jeff Sharkey36801cc2015-03-13 16:09:20 -070076 return -errno;
77 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -080078 }
79
Jeff Sharkey36801cc2015-03-13 16:09:20 -070080 BindMount(securePath, kAsecPath);
81
Jeff Sharkeydeb24052015-03-02 21:01:40 -080082 return OK;
83}
84
Jeff Sharkey9c484982015-03-31 10:35:33 -070085status_t PublicVolume::doCreate() {
86 return CreateDeviceNode(mDevPath, mDevice);
87}
88
89status_t PublicVolume::doDestroy() {
90 return DestroyDeviceNode(mDevPath);
91}
92
Jeff Sharkeydeb24052015-03-02 21:01:40 -080093status_t PublicVolume::doMount() {
Jeff Sharkey9c484982015-03-31 10:35:33 -070094 readMetadata();
95
Jeff Sharkey37ba1252018-01-19 10:55:18 +090096 if (mFsType == "vfat" && vfat::IsSupported()) {
97 if (vfat::Check(mDevPath)) {
98 LOG(ERROR) << getId() << " failed filesystem check";
99 return -EIO;
100 }
101 } else if (mFsType == "exfat" && exfat::IsSupported()) {
102 if (exfat::Check(mDevPath)) {
103 LOG(ERROR) << getId() << " failed filesystem check";
104 return -EIO;
105 }
106 } else {
Makoto Onukic82c9ce2015-06-24 13:30:45 -0700107 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
108 return -EIO;
109 }
110
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700111 // Use UUID as stable name, if available
112 std::string stableName = getId();
113 if (!mFsUuid.empty()) {
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700114 stableName = mFsUuid;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700115 }
116
117 mRawPath = StringPrintf("/mnt/media_rw/%s", stableName.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700118
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700119 mFuseDefault = StringPrintf("/mnt/runtime/default/%s", stableName.c_str());
120 mFuseRead = StringPrintf("/mnt/runtime/read/%s", stableName.c_str());
121 mFuseWrite = StringPrintf("/mnt/runtime/write/%s", stableName.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700122
123 setInternalPath(mRawPath);
Jeff Sharkey8474ee32015-07-30 16:54:23 -0700124 if (getMountFlags() & MountFlags::kVisible) {
125 setPath(StringPrintf("/storage/%s", stableName.c_str()));
126 } else {
127 setPath(mRawPath);
128 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700129
Qin Chaoe0074f12015-12-15 15:20:41 +0800130 if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700131 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800132 return -errno;
133 }
134
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900135 if (mFsType == "vfat") {
136 if (vfat::Mount(mDevPath, mRawPath, false, false, false, AID_MEDIA_RW, AID_MEDIA_RW, 0007,
137 true)) {
138 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
139 return -EIO;
140 }
141 } else if (mFsType == "exfat") {
142 if (exfat::Mount(mDevPath, mRawPath, AID_MEDIA_RW, AID_MEDIA_RW, 0007)) {
143 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
144 return -EIO;
145 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800146 }
147
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700148 if (getMountFlags() & MountFlags::kPrimary) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700149 initAsecStage();
150 }
151
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700152 if (!(getMountFlags() & MountFlags::kVisible)) {
153 // Not visible to apps, so no need to spin up FUSE
154 return OK;
155 }
156
Qin Chaoe0074f12015-12-15 15:20:41 +0800157 if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
Paul Crowley8915d622018-09-18 15:14:18 -0700158 fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
159 fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Qin Chaoe0074f12015-12-15 15:20:41 +0800160 PLOG(ERROR) << getId() << " failed to create FUSE mount points";
161 return -errno;
162 }
163
Jeff Sharkey66270a22015-06-24 11:49:24 -0700164 dev_t before = GetDevice(mFuseWrite);
165
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800166 if (!(mFusePid = fork())) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700167 if (getMountFlags() & MountFlags::kPrimary) {
Paul Crowley8915d622018-09-18 15:14:18 -0700168 // clang-format off
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700169 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800170 "-u", "1023", // AID_MEDIA_RW
171 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey20642ae2015-07-28 10:57:29 -0700172 "-U", std::to_string(getMountUserId()).c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700173 "-w",
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800174 mRawPath.c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700175 stableName.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700176 NULL)) {
Paul Crowley8915d622018-09-18 15:14:18 -0700177 // clang-format on
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700178 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800179 }
180 } else {
Paul Crowley8915d622018-09-18 15:14:18 -0700181 // clang-format off
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700182 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800183 "-u", "1023", // AID_MEDIA_RW
184 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey20642ae2015-07-28 10:57:29 -0700185 "-U", std::to_string(getMountUserId()).c_str(),
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800186 mRawPath.c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700187 stableName.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700188 NULL)) {
Paul Crowley8915d622018-09-18 15:14:18 -0700189 // clang-format on
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700190 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800191 }
192 }
193
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700194 LOG(ERROR) << "FUSE exiting";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800195 _exit(1);
196 }
197
198 if (mFusePid == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700199 PLOG(ERROR) << getId() << " failed to fork";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800200 return -errno;
201 }
202
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600203 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700204 while (before == GetDevice(mFuseWrite)) {
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700205 LOG(DEBUG) << "Waiting for FUSE to spin up...";
Paul Crowley8915d622018-09-18 15:14:18 -0700206 usleep(50000); // 50ms
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600207
208 nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
209 if (nanoseconds_to_milliseconds(now - start) > 5000) {
210 LOG(WARNING) << "Timed out while waiting for FUSE to spin up";
211 return -ETIMEDOUT;
212 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700213 }
Daniel Rosenberg1d79d102017-07-11 17:59:55 -0700214 /* sdcardfs will have exited already. FUSE will still be running */
Daniel Rosenberg8b9a5b32018-03-12 15:47:23 -0700215 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
216 mFusePid = 0;
Jeff Sharkey66270a22015-06-24 11:49:24 -0700217
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800218 return OK;
219}
220
221status_t PublicVolume::doUnmount() {
John Cormie25cc7e32016-04-18 14:23:29 -0700222 // Unmount the storage before we kill the FUSE process. If we kill
223 // the FUSE process first, most file system operations will return
224 // ENOTCONN until the unmount completes. This is an exotic and unusual
225 // error code and might cause broken behaviour in applications.
Jeff Sharkey8aff8542016-03-30 20:37:28 -0600226 KillProcessesUsingPath(getPath());
227
Jeff Sharkey48d81d32015-04-12 21:50:32 -0700228 ForceUnmount(kAsecPath);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700229
230 ForceUnmount(mFuseDefault);
231 ForceUnmount(mFuseRead);
232 ForceUnmount(mFuseWrite);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800233 ForceUnmount(mRawPath);
234
Jeff Sharkey66270a22015-06-24 11:49:24 -0700235 rmdir(mFuseDefault.c_str());
236 rmdir(mFuseRead.c_str());
237 rmdir(mFuseWrite.c_str());
238 rmdir(mRawPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700239
Jeff Sharkey66270a22015-06-24 11:49:24 -0700240 mFuseDefault.clear();
241 mFuseRead.clear();
242 mFuseWrite.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700243 mRawPath.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800244
245 return OK;
246}
247
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700248status_t PublicVolume::doFormat(const std::string& fsType) {
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900249 if ((fsType == "vfat" || fsType == "auto") && vfat::IsSupported()) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700250 if (WipeBlockDevice(mDevPath) != OK) {
251 LOG(WARNING) << getId() << " failed to wipe";
252 }
253 if (vfat::Format(mDevPath, 0)) {
254 LOG(ERROR) << getId() << " failed to format";
255 return -errno;
256 }
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900257 } else if ((fsType == "exfat" || fsType == "auto") && exfat::IsSupported()) {
258 if (WipeBlockDevice(mDevPath) != OK) {
259 LOG(WARNING) << getId() << " failed to wipe";
260 }
261 if (exfat::Format(mDevPath)) {
262 LOG(ERROR) << getId() << " failed to format";
263 return -errno;
264 }
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700265 } else {
266 LOG(ERROR) << "Unsupported filesystem " << fsType;
267 return -EINVAL;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800268 }
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700269
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800270 return OK;
271}
272
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800273} // namespace vold
274} // namespace android