blob: efdb6873b0e00debd1d6c793a623add51e2e9e6c [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 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700132
Qin Chaoe0074f12015-12-15 15:20:41 +0800133 if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700134 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800135 return -errno;
136 }
137
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900138 if (mFsType == "vfat") {
139 if (vfat::Mount(mDevPath, mRawPath, false, false, false, AID_MEDIA_RW, AID_MEDIA_RW, 0007,
140 true)) {
141 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
142 return -EIO;
143 }
144 } else if (mFsType == "exfat") {
145 if (exfat::Mount(mDevPath, mRawPath, AID_MEDIA_RW, AID_MEDIA_RW, 0007)) {
146 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
147 return -EIO;
148 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800149 }
150
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700151 if (getMountFlags() & MountFlags::kPrimary) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700152 initAsecStage();
153 }
154
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700155 if (!(getMountFlags() & MountFlags::kVisible)) {
156 // Not visible to apps, so no need to spin up FUSE
157 return OK;
158 }
159
Qin Chaoe0074f12015-12-15 15:20:41 +0800160 if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
161 fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
162 fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT)) {
163 PLOG(ERROR) << getId() << " failed to create FUSE mount points";
164 return -errno;
165 }
166
Jeff Sharkey66270a22015-06-24 11:49:24 -0700167 dev_t before = GetDevice(mFuseWrite);
168
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800169 if (!(mFusePid = fork())) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700170 if (getMountFlags() & MountFlags::kPrimary) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700171 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800172 "-u", "1023", // AID_MEDIA_RW
173 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey20642ae2015-07-28 10:57:29 -0700174 "-U", std::to_string(getMountUserId()).c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700175 "-w",
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800176 mRawPath.c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700177 stableName.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700178 NULL)) {
179 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800180 }
181 } else {
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)) {
189 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800190 }
191 }
192
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700193 LOG(ERROR) << "FUSE exiting";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800194 _exit(1);
195 }
196
197 if (mFusePid == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700198 PLOG(ERROR) << getId() << " failed to fork";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800199 return -errno;
200 }
201
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600202 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700203 while (before == GetDevice(mFuseWrite)) {
204 LOG(VERBOSE) << "Waiting for FUSE to spin up...";
205 usleep(50000); // 50ms
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600206
207 nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
208 if (nanoseconds_to_milliseconds(now - start) > 5000) {
209 LOG(WARNING) << "Timed out while waiting for FUSE to spin up";
210 return -ETIMEDOUT;
211 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700212 }
Daniel Rosenberg1d79d102017-07-11 17:59:55 -0700213 /* sdcardfs will have exited already. FUSE will still be running */
214 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, WNOHANG));
Jeff Sharkey66270a22015-06-24 11:49:24 -0700215
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800216 return OK;
217}
218
219status_t PublicVolume::doUnmount() {
John Cormie25cc7e32016-04-18 14:23:29 -0700220 // Unmount the storage before we kill the FUSE process. If we kill
221 // the FUSE process first, most file system operations will return
222 // ENOTCONN until the unmount completes. This is an exotic and unusual
223 // error code and might cause broken behaviour in applications.
Jeff Sharkey8aff8542016-03-30 20:37:28 -0600224 KillProcessesUsingPath(getPath());
225
Jeff Sharkey48d81d32015-04-12 21:50:32 -0700226 ForceUnmount(kAsecPath);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700227
228 ForceUnmount(mFuseDefault);
229 ForceUnmount(mFuseRead);
230 ForceUnmount(mFuseWrite);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800231 ForceUnmount(mRawPath);
232
John Cormie25cc7e32016-04-18 14:23:29 -0700233 if (mFusePid > 0) {
234 kill(mFusePid, SIGTERM);
235 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
236 mFusePid = 0;
237 }
238
Jeff Sharkey66270a22015-06-24 11:49:24 -0700239 rmdir(mFuseDefault.c_str());
240 rmdir(mFuseRead.c_str());
241 rmdir(mFuseWrite.c_str());
242 rmdir(mRawPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700243
Jeff Sharkey66270a22015-06-24 11:49:24 -0700244 mFuseDefault.clear();
245 mFuseRead.clear();
246 mFuseWrite.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700247 mRawPath.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800248
249 return OK;
250}
251
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700252status_t PublicVolume::doFormat(const std::string& fsType) {
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900253 if ((fsType == "vfat" || fsType == "auto") && vfat::IsSupported()) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700254 if (WipeBlockDevice(mDevPath) != OK) {
255 LOG(WARNING) << getId() << " failed to wipe";
256 }
257 if (vfat::Format(mDevPath, 0)) {
258 LOG(ERROR) << getId() << " failed to format";
259 return -errno;
260 }
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900261 } else if ((fsType == "exfat" || fsType == "auto") && exfat::IsSupported()) {
262 if (WipeBlockDevice(mDevPath) != OK) {
263 LOG(WARNING) << getId() << " failed to wipe";
264 }
265 if (exfat::Format(mDevPath)) {
266 LOG(ERROR) << getId() << " failed to format";
267 return -errno;
268 }
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700269 } else {
270 LOG(ERROR) << "Unsupported filesystem " << fsType;
271 return -EINVAL;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800272 }
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700273
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800274 return OK;
275}
276
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800277} // namespace vold
278} // namespace android