blob: 4076e73ed4ff042846302d2ae68f536b486320c0 [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/stringprintf.h>
24#include <android-base/logging.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>
33#include <sys/types.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070034#include <sys/sysmacros.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
46PublicVolume::PublicVolume(dev_t device) :
Jeff Sharkey36801cc2015-03-13 16:09:20 -070047 VolumeBase(Type::kPublic), mDevice(device), mFusePid(0) {
48 setId(StringPrintf("public:%u,%u", major(device), minor(device)));
49 mDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -080050}
51
52PublicVolume::~PublicVolume() {
Jeff Sharkeydeb24052015-03-02 21:01:40 -080053}
54
55status_t PublicVolume::readMetadata() {
Jeff Sharkey3472e522017-10-06 18:02:53 -060056 status_t res = ReadMetadataUntrusted(mDevPath, &mFsType, &mFsUuid, &mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060057
Jeff Sharkey814e9d32017-09-13 11:49:44 -060058 auto listener = getListener();
59 if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060060
Jeff Sharkey36801cc2015-03-13 16:09:20 -070061 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080062}
63
64status_t PublicVolume::initAsecStage() {
65 std::string legacyPath(mRawPath + "/android_secure");
66 std::string securePath(mRawPath + "/.android_secure");
67
68 // Recover legacy secure path
69 if (!access(legacyPath.c_str(), R_OK | X_OK)
70 && access(securePath.c_str(), R_OK | X_OK)) {
71 if (rename(legacyPath.c_str(), securePath.c_str())) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070072 PLOG(WARNING) << getId() << " failed to rename legacy ASEC dir";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080073 }
74 }
75
Jeff Sharkey36801cc2015-03-13 16:09:20 -070076 if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) {
77 if (errno != EEXIST) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070078 PLOG(WARNING) << getId() << " creating ASEC stage failed";
Jeff Sharkey36801cc2015-03-13 16:09:20 -070079 return -errno;
80 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -080081 }
82
Jeff Sharkey36801cc2015-03-13 16:09:20 -070083 BindMount(securePath, kAsecPath);
84
Jeff Sharkeydeb24052015-03-02 21:01:40 -080085 return OK;
86}
87
Jeff Sharkey9c484982015-03-31 10:35:33 -070088status_t PublicVolume::doCreate() {
89 return CreateDeviceNode(mDevPath, mDevice);
90}
91
92status_t PublicVolume::doDestroy() {
93 return DestroyDeviceNode(mDevPath);
94}
95
Jeff Sharkeydeb24052015-03-02 21:01:40 -080096status_t PublicVolume::doMount() {
Jeff Sharkey9c484982015-03-31 10:35:33 -070097 readMetadata();
98
Jeff Sharkey37ba1252018-01-19 10:55:18 +090099 if (mFsType == "vfat" && vfat::IsSupported()) {
100 if (vfat::Check(mDevPath)) {
101 LOG(ERROR) << getId() << " failed filesystem check";
102 return -EIO;
103 }
104 } else if (mFsType == "exfat" && exfat::IsSupported()) {
105 if (exfat::Check(mDevPath)) {
106 LOG(ERROR) << getId() << " failed filesystem check";
107 return -EIO;
108 }
109 } else {
Makoto Onukic82c9ce2015-06-24 13:30:45 -0700110 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
111 return -EIO;
112 }
113
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700114 // Use UUID as stable name, if available
115 std::string stableName = getId();
116 if (!mFsUuid.empty()) {
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700117 stableName = mFsUuid;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700118 }
119
120 mRawPath = StringPrintf("/mnt/media_rw/%s", stableName.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700121
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700122 mFuseDefault = StringPrintf("/mnt/runtime/default/%s", stableName.c_str());
123 mFuseRead = StringPrintf("/mnt/runtime/read/%s", stableName.c_str());
124 mFuseWrite = StringPrintf("/mnt/runtime/write/%s", stableName.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700125
126 setInternalPath(mRawPath);
Jeff Sharkey8474ee32015-07-30 16:54:23 -0700127 if (getMountFlags() & MountFlags::kVisible) {
128 setPath(StringPrintf("/storage/%s", stableName.c_str()));
129 } else {
130 setPath(mRawPath);
131 }
Sudheer Shanka53947a32018-08-01 10:24:13 -0700132 setLabel(stableName);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700133
Qin Chaoe0074f12015-12-15 15:20:41 +0800134 if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700135 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800136 return -errno;
137 }
138
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900139 if (mFsType == "vfat") {
140 if (vfat::Mount(mDevPath, mRawPath, false, false, false, AID_MEDIA_RW, AID_MEDIA_RW, 0007,
141 true)) {
142 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
143 return -EIO;
144 }
145 } else if (mFsType == "exfat") {
146 if (exfat::Mount(mDevPath, mRawPath, AID_MEDIA_RW, AID_MEDIA_RW, 0007)) {
147 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
148 return -EIO;
149 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800150 }
151
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700152 if (getMountFlags() & MountFlags::kPrimary) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700153 initAsecStage();
154 }
155
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700156 if (!(getMountFlags() & MountFlags::kVisible)) {
157 // Not visible to apps, so no need to spin up FUSE
158 return OK;
159 }
160
Qin Chaoe0074f12015-12-15 15:20:41 +0800161 if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
162 fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
163 fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT)) {
164 PLOG(ERROR) << getId() << " failed to create FUSE mount points";
165 return -errno;
166 }
167
Jeff Sharkey66270a22015-06-24 11:49:24 -0700168 dev_t before = GetDevice(mFuseWrite);
169
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800170 if (!(mFusePid = fork())) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700171 if (getMountFlags() & MountFlags::kPrimary) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700172 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800173 "-u", "1023", // AID_MEDIA_RW
174 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey20642ae2015-07-28 10:57:29 -0700175 "-U", std::to_string(getMountUserId()).c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700176 "-w",
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800177 mRawPath.c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700178 stableName.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700179 NULL)) {
180 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800181 }
182 } else {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700183 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800184 "-u", "1023", // AID_MEDIA_RW
185 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey20642ae2015-07-28 10:57:29 -0700186 "-U", std::to_string(getMountUserId()).c_str(),
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800187 mRawPath.c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700188 stableName.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700189 NULL)) {
190 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)) {
205 LOG(VERBOSE) << "Waiting for FUSE to spin up...";
206 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