blob: 6ad2ae18591375bc7c3fc6655aaae0a05af2c63f [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"
21#include "ResponseCode.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 Sharkey95c87cc2015-04-01 11:54:32 -070056 status_t res = ReadMetadataUntrusted(mDevPath, mFsType, mFsUuid, mFsLabel);
Jeff Sharkey814e9d32017-09-13 11:49:44 -060057#if ENABLE_BINDER
58 auto listener = getListener();
59 if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
60#else
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070061 notifyEvent(ResponseCode::VolumeFsTypeChanged, mFsType);
62 notifyEvent(ResponseCode::VolumeFsUuidChanged, mFsUuid);
63 notifyEvent(ResponseCode::VolumeFsLabelChanged, mFsLabel);
Jeff Sharkey814e9d32017-09-13 11:49:44 -060064#endif
Jeff Sharkey36801cc2015-03-13 16:09:20 -070065 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080066}
67
68status_t PublicVolume::initAsecStage() {
69 std::string legacyPath(mRawPath + "/android_secure");
70 std::string securePath(mRawPath + "/.android_secure");
71
72 // Recover legacy secure path
73 if (!access(legacyPath.c_str(), R_OK | X_OK)
74 && access(securePath.c_str(), R_OK | X_OK)) {
75 if (rename(legacyPath.c_str(), securePath.c_str())) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070076 PLOG(WARNING) << getId() << " failed to rename legacy ASEC dir";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080077 }
78 }
79
Jeff Sharkey36801cc2015-03-13 16:09:20 -070080 if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) {
81 if (errno != EEXIST) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070082 PLOG(WARNING) << getId() << " creating ASEC stage failed";
Jeff Sharkey36801cc2015-03-13 16:09:20 -070083 return -errno;
84 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -080085 }
86
Jeff Sharkey36801cc2015-03-13 16:09:20 -070087 BindMount(securePath, kAsecPath);
88
Jeff Sharkeydeb24052015-03-02 21:01:40 -080089 return OK;
90}
91
Jeff Sharkey9c484982015-03-31 10:35:33 -070092status_t PublicVolume::doCreate() {
93 return CreateDeviceNode(mDevPath, mDevice);
94}
95
96status_t PublicVolume::doDestroy() {
97 return DestroyDeviceNode(mDevPath);
98}
99
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800100status_t PublicVolume::doMount() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700101 // TODO: expand to support mounting other filesystems
Jeff Sharkey9c484982015-03-31 10:35:33 -0700102 readMetadata();
103
Makoto Onukic82c9ce2015-06-24 13:30:45 -0700104 if (mFsType != "vfat") {
105 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
106 return -EIO;
107 }
108
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700109 if (vfat::Check(mDevPath)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700110 LOG(ERROR) << getId() << " failed filesystem check";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800111 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 Sharkeyd0640f62015-05-21 22:35:42 -0700138 if (vfat::Mount(mDevPath, mRawPath, false, false, false,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800139 AID_MEDIA_RW, AID_MEDIA_RW, 0007, true)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700140 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800141 return -EIO;
142 }
143
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700144 if (getMountFlags() & MountFlags::kPrimary) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700145 initAsecStage();
146 }
147
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700148 if (!(getMountFlags() & MountFlags::kVisible)) {
149 // Not visible to apps, so no need to spin up FUSE
150 return OK;
151 }
152
Qin Chaoe0074f12015-12-15 15:20:41 +0800153 if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
154 fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
155 fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT)) {
156 PLOG(ERROR) << getId() << " failed to create FUSE mount points";
157 return -errno;
158 }
159
Jeff Sharkey66270a22015-06-24 11:49:24 -0700160 dev_t before = GetDevice(mFuseWrite);
161
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800162 if (!(mFusePid = fork())) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700163 if (getMountFlags() & MountFlags::kPrimary) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700164 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800165 "-u", "1023", // AID_MEDIA_RW
166 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey20642ae2015-07-28 10:57:29 -0700167 "-U", std::to_string(getMountUserId()).c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700168 "-w",
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800169 mRawPath.c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700170 stableName.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700171 NULL)) {
172 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800173 }
174 } else {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700175 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800176 "-u", "1023", // AID_MEDIA_RW
177 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey20642ae2015-07-28 10:57:29 -0700178 "-U", std::to_string(getMountUserId()).c_str(),
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800179 mRawPath.c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700180 stableName.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700181 NULL)) {
182 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800183 }
184 }
185
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700186 LOG(ERROR) << "FUSE exiting";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800187 _exit(1);
188 }
189
190 if (mFusePid == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700191 PLOG(ERROR) << getId() << " failed to fork";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800192 return -errno;
193 }
194
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600195 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700196 while (before == GetDevice(mFuseWrite)) {
197 LOG(VERBOSE) << "Waiting for FUSE to spin up...";
198 usleep(50000); // 50ms
Jeff Sharkey7bdf4d52017-09-18 14:47:10 -0600199
200 nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
201 if (nanoseconds_to_milliseconds(now - start) > 5000) {
202 LOG(WARNING) << "Timed out while waiting for FUSE to spin up";
203 return -ETIMEDOUT;
204 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700205 }
Daniel Rosenberg1d79d102017-07-11 17:59:55 -0700206 /* sdcardfs will have exited already. FUSE will still be running */
207 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, WNOHANG));
Jeff Sharkey66270a22015-06-24 11:49:24 -0700208
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800209 return OK;
210}
211
212status_t PublicVolume::doUnmount() {
John Cormie25cc7e32016-04-18 14:23:29 -0700213 // Unmount the storage before we kill the FUSE process. If we kill
214 // the FUSE process first, most file system operations will return
215 // ENOTCONN until the unmount completes. This is an exotic and unusual
216 // error code and might cause broken behaviour in applications.
Jeff Sharkey8aff8542016-03-30 20:37:28 -0600217 KillProcessesUsingPath(getPath());
218
Jeff Sharkey48d81d32015-04-12 21:50:32 -0700219 ForceUnmount(kAsecPath);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700220
221 ForceUnmount(mFuseDefault);
222 ForceUnmount(mFuseRead);
223 ForceUnmount(mFuseWrite);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800224 ForceUnmount(mRawPath);
225
John Cormie25cc7e32016-04-18 14:23:29 -0700226 if (mFusePid > 0) {
227 kill(mFusePid, SIGTERM);
228 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
229 mFusePid = 0;
230 }
231
Jeff Sharkey66270a22015-06-24 11:49:24 -0700232 rmdir(mFuseDefault.c_str());
233 rmdir(mFuseRead.c_str());
234 rmdir(mFuseWrite.c_str());
235 rmdir(mRawPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700236
Jeff Sharkey66270a22015-06-24 11:49:24 -0700237 mFuseDefault.clear();
238 mFuseRead.clear();
239 mFuseWrite.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700240 mRawPath.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800241
242 return OK;
243}
244
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700245status_t PublicVolume::doFormat(const std::string& fsType) {
246 if (fsType == "vfat" || fsType == "auto") {
247 if (WipeBlockDevice(mDevPath) != OK) {
248 LOG(WARNING) << getId() << " failed to wipe";
249 }
250 if (vfat::Format(mDevPath, 0)) {
251 LOG(ERROR) << getId() << " failed to format";
252 return -errno;
253 }
254 } else {
255 LOG(ERROR) << "Unsupported filesystem " << fsType;
256 return -EINVAL;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800257 }
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700258
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800259 return OK;
260}
261
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800262} // namespace vold
263} // namespace android