blob: 98c897f840fd18d4c9bda7d597ba4b52b2b7f6dd [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 Sharkeyd0640f62015-05-21 22:35:42 -070017#include "fs/Vfat.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080018#include "PublicVolume.h"
19#include "Utils.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070020#include "VolumeManager.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080021
Elliott Hughes7e128fb2015-12-04 15:50:53 -080022#include <android-base/stringprintf.h>
23#include <android-base/logging.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080024#include <cutils/fs.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080025#include <private/android_filesystem_config.h>
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -060026#include <utils/Timers.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080027
28#include <fcntl.h>
29#include <stdlib.h>
30#include <sys/mount.h>
31#include <sys/stat.h>
32#include <sys/types.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070033#include <sys/sysmacros.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080034#include <sys/wait.h>
35
Dan Albertae9e8902015-03-16 10:35:17 -070036using android::base::StringPrintf;
37
Jeff Sharkeydeb24052015-03-02 21:01:40 -080038namespace android {
39namespace vold {
40
Jeff Sharkeydeb24052015-03-02 21:01:40 -080041static const char* kFusePath = "/system/bin/sdcard";
42
Jeff Sharkey36801cc2015-03-13 16:09:20 -070043static const char* kAsecPath = "/mnt/secure/asec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080044
45PublicVolume::PublicVolume(dev_t device) :
Jeff Sharkey36801cc2015-03-13 16:09:20 -070046 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 Sharkeydeb24052015-03-02 21:01:40 -080049}
50
51PublicVolume::~PublicVolume() {
Jeff Sharkeydeb24052015-03-02 21:01:40 -080052}
53
54status_t PublicVolume::readMetadata() {
Jeff Sharkey3472e522017-10-06 18:02:53 -060055 status_t res = ReadMetadataUntrusted(mDevPath, &mFsType, &mFsUuid, &mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060056
Jeff Sharkey814e9d32017-09-13 11:49:44 -060057 auto listener = getListener();
58 if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060059
Jeff Sharkey36801cc2015-03-13 16:09:20 -070060 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080061}
62
63status_t PublicVolume::initAsecStage() {
64 std::string legacyPath(mRawPath + "/android_secure");
65 std::string securePath(mRawPath + "/.android_secure");
66
67 // Recover legacy secure path
68 if (!access(legacyPath.c_str(), R_OK | X_OK)
69 && access(securePath.c_str(), R_OK | X_OK)) {
70 if (rename(legacyPath.c_str(), securePath.c_str())) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070071 PLOG(WARNING) << getId() << " failed to rename legacy ASEC dir";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080072 }
73 }
74
Jeff Sharkey36801cc2015-03-13 16:09:20 -070075 if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) {
76 if (errno != EEXIST) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070077 PLOG(WARNING) << getId() << " creating ASEC stage failed";
Jeff Sharkey36801cc2015-03-13 16:09:20 -070078 return -errno;
79 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -080080 }
81
Jeff Sharkey36801cc2015-03-13 16:09:20 -070082 BindMount(securePath, kAsecPath);
83
Jeff Sharkeydeb24052015-03-02 21:01:40 -080084 return OK;
85}
86
Jeff Sharkey9c484982015-03-31 10:35:33 -070087status_t PublicVolume::doCreate() {
88 return CreateDeviceNode(mDevPath, mDevice);
89}
90
91status_t PublicVolume::doDestroy() {
92 return DestroyDeviceNode(mDevPath);
93}
94
Jeff Sharkeydeb24052015-03-02 21:01:40 -080095status_t PublicVolume::doMount() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070096 // TODO: expand to support mounting other filesystems
Jeff Sharkey9c484982015-03-31 10:35:33 -070097 readMetadata();
98
Makoto Onukic82c9ce2015-06-24 13:30:45 -070099 if (mFsType != "vfat") {
100 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
101 return -EIO;
102 }
103
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700104 if (vfat::Check(mDevPath)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700105 LOG(ERROR) << getId() << " failed filesystem check";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800106 return -EIO;
107 }
108
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700109 // Use UUID as stable name, if available
110 std::string stableName = getId();
111 if (!mFsUuid.empty()) {
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700112 stableName = mFsUuid;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700113 }
114
115 mRawPath = StringPrintf("/mnt/media_rw/%s", stableName.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700116
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700117 mFuseDefault = StringPrintf("/mnt/runtime/default/%s", stableName.c_str());
118 mFuseRead = StringPrintf("/mnt/runtime/read/%s", stableName.c_str());
119 mFuseWrite = StringPrintf("/mnt/runtime/write/%s", stableName.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700120
121 setInternalPath(mRawPath);
Jeff Sharkey8474ee32015-07-30 16:54:23 -0700122 if (getMountFlags() & MountFlags::kVisible) {
123 setPath(StringPrintf("/storage/%s", stableName.c_str()));
124 } else {
125 setPath(mRawPath);
126 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700127
Qin Chaoe0074f12015-12-15 15:20:41 +0800128 if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700129 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800130 return -errno;
131 }
132
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700133 if (vfat::Mount(mDevPath, mRawPath, false, false, false,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800134 AID_MEDIA_RW, AID_MEDIA_RW, 0007, true)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700135 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800136 return -EIO;
137 }
138
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700139 if (getMountFlags() & MountFlags::kPrimary) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700140 initAsecStage();
141 }
142
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700143 if (!(getMountFlags() & MountFlags::kVisible)) {
144 // Not visible to apps, so no need to spin up FUSE
145 return OK;
146 }
147
Qin Chaoe0074f12015-12-15 15:20:41 +0800148 if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
149 fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
150 fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT)) {
151 PLOG(ERROR) << getId() << " failed to create FUSE mount points";
152 return -errno;
153 }
154
Jeff Sharkey66270a22015-06-24 11:49:24 -0700155 dev_t before = GetDevice(mFuseWrite);
156
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800157 if (!(mFusePid = fork())) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700158 if (getMountFlags() & MountFlags::kPrimary) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700159 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800160 "-u", "1023", // AID_MEDIA_RW
161 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey20642ae2015-07-28 10:57:29 -0700162 "-U", std::to_string(getMountUserId()).c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700163 "-w",
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800164 mRawPath.c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700165 stableName.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700166 NULL)) {
167 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800168 }
169 } else {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700170 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800171 "-u", "1023", // AID_MEDIA_RW
172 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey20642ae2015-07-28 10:57:29 -0700173 "-U", std::to_string(getMountUserId()).c_str(),
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)) {
177 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800178 }
179 }
180
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700181 LOG(ERROR) << "FUSE exiting";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800182 _exit(1);
183 }
184
185 if (mFusePid == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700186 PLOG(ERROR) << getId() << " failed to fork";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800187 return -errno;
188 }
189
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600190 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700191 while (before == GetDevice(mFuseWrite)) {
192 LOG(VERBOSE) << "Waiting for FUSE to spin up...";
193 usleep(50000); // 50ms
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600194
195 nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
196 if (nanoseconds_to_milliseconds(now - start) > 5000) {
197 LOG(WARNING) << "Timed out while waiting for FUSE to spin up";
198 return -ETIMEDOUT;
199 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700200 }
Daniel Rosenberg1d79d102017-07-11 17:59:55 -0700201 /* sdcardfs will have exited already. FUSE will still be running */
202 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, WNOHANG));
Jeff Sharkey66270a22015-06-24 11:49:24 -0700203
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800204 return OK;
205}
206
207status_t PublicVolume::doUnmount() {
John Cormie25cc7e32016-04-18 14:23:29 -0700208 // Unmount the storage before we kill the FUSE process. If we kill
209 // the FUSE process first, most file system operations will return
210 // ENOTCONN until the unmount completes. This is an exotic and unusual
211 // error code and might cause broken behaviour in applications.
Jeff Sharkey8aff8542016-03-30 20:37:28 -0600212 KillProcessesUsingPath(getPath());
213
Jeff Sharkey48d81d32015-04-12 21:50:32 -0700214 ForceUnmount(kAsecPath);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700215
216 ForceUnmount(mFuseDefault);
217 ForceUnmount(mFuseRead);
218 ForceUnmount(mFuseWrite);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800219 ForceUnmount(mRawPath);
220
John Cormie25cc7e32016-04-18 14:23:29 -0700221 if (mFusePid > 0) {
222 kill(mFusePid, SIGTERM);
223 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
224 mFusePid = 0;
225 }
226
Jeff Sharkey66270a22015-06-24 11:49:24 -0700227 rmdir(mFuseDefault.c_str());
228 rmdir(mFuseRead.c_str());
229 rmdir(mFuseWrite.c_str());
230 rmdir(mRawPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700231
Jeff Sharkey66270a22015-06-24 11:49:24 -0700232 mFuseDefault.clear();
233 mFuseRead.clear();
234 mFuseWrite.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700235 mRawPath.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800236
237 return OK;
238}
239
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700240status_t PublicVolume::doFormat(const std::string& fsType) {
241 if (fsType == "vfat" || fsType == "auto") {
242 if (WipeBlockDevice(mDevPath) != OK) {
243 LOG(WARNING) << getId() << " failed to wipe";
244 }
245 if (vfat::Format(mDevPath, 0)) {
246 LOG(ERROR) << getId() << " failed to format";
247 return -errno;
248 }
249 } else {
250 LOG(ERROR) << "Unsupported filesystem " << fsType;
251 return -EINVAL;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800252 }
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700253
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800254 return OK;
255}
256
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800257} // namespace vold
258} // namespace android