blob: 8580da99fa461dbb07530fde326d1c98758ad2e2 [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>
27
28#include <fcntl.h>
29#include <stdlib.h>
30#include <sys/mount.h>
31#include <sys/stat.h>
32#include <sys/types.h>
33#include <sys/wait.h>
34
Dan Albertae9e8902015-03-16 10:35:17 -070035using android::base::StringPrintf;
36
Jeff Sharkeydeb24052015-03-02 21:01:40 -080037namespace android {
38namespace vold {
39
Jeff Sharkeydeb24052015-03-02 21:01:40 -080040static const char* kFusePath = "/system/bin/sdcard";
41
Jeff Sharkey36801cc2015-03-13 16:09:20 -070042static const char* kAsecPath = "/mnt/secure/asec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080043
44PublicVolume::PublicVolume(dev_t device) :
Jeff Sharkey36801cc2015-03-13 16:09:20 -070045 VolumeBase(Type::kPublic), mDevice(device), mFusePid(0) {
46 setId(StringPrintf("public:%u,%u", major(device), minor(device)));
47 mDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -080048}
49
50PublicVolume::~PublicVolume() {
Jeff Sharkeydeb24052015-03-02 21:01:40 -080051}
52
53status_t PublicVolume::readMetadata() {
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070054 status_t res = ReadMetadataUntrusted(mDevPath, mFsType, mFsUuid, mFsLabel);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070055 notifyEvent(ResponseCode::VolumeFsTypeChanged, mFsType);
56 notifyEvent(ResponseCode::VolumeFsUuidChanged, mFsUuid);
57 notifyEvent(ResponseCode::VolumeFsLabelChanged, mFsLabel);
Jeff Sharkey36801cc2015-03-13 16:09:20 -070058 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080059}
60
61status_t PublicVolume::initAsecStage() {
62 std::string legacyPath(mRawPath + "/android_secure");
63 std::string securePath(mRawPath + "/.android_secure");
64
65 // Recover legacy secure path
66 if (!access(legacyPath.c_str(), R_OK | X_OK)
67 && access(securePath.c_str(), R_OK | X_OK)) {
68 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 Sharkey36801cc2015-03-13 16:09:20 -070094 // TODO: expand to support mounting other filesystems
Jeff Sharkey9c484982015-03-31 10:35:33 -070095 readMetadata();
96
Makoto Onukic82c9ce2015-06-24 13:30:45 -070097 if (mFsType != "vfat") {
98 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
99 return -EIO;
100 }
101
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700102 if (vfat::Check(mDevPath)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700103 LOG(ERROR) << getId() << " failed filesystem check";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800104 return -EIO;
105 }
106
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700107 // Use UUID as stable name, if available
108 std::string stableName = getId();
109 if (!mFsUuid.empty()) {
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700110 stableName = mFsUuid;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700111 }
112
113 mRawPath = StringPrintf("/mnt/media_rw/%s", stableName.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700114
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700115 mFuseDefault = StringPrintf("/mnt/runtime/default/%s", stableName.c_str());
116 mFuseRead = StringPrintf("/mnt/runtime/read/%s", stableName.c_str());
117 mFuseWrite = StringPrintf("/mnt/runtime/write/%s", stableName.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700118
119 setInternalPath(mRawPath);
Jeff Sharkey8474ee32015-07-30 16:54:23 -0700120 if (getMountFlags() & MountFlags::kVisible) {
121 setPath(StringPrintf("/storage/%s", stableName.c_str()));
122 } else {
123 setPath(mRawPath);
124 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700125
Qin Chaoe0074f12015-12-15 15:20:41 +0800126 if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700127 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800128 return -errno;
129 }
130
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700131 if (vfat::Mount(mDevPath, mRawPath, false, false, false,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800132 AID_MEDIA_RW, AID_MEDIA_RW, 0007, true)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700133 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800134 return -EIO;
135 }
136
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700137 if (getMountFlags() & MountFlags::kPrimary) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700138 initAsecStage();
139 }
140
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700141 if (!(getMountFlags() & MountFlags::kVisible)) {
142 // Not visible to apps, so no need to spin up FUSE
143 return OK;
144 }
145
Qin Chaoe0074f12015-12-15 15:20:41 +0800146 if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
147 fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
148 fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT)) {
149 PLOG(ERROR) << getId() << " failed to create FUSE mount points";
150 return -errno;
151 }
152
Jeff Sharkey66270a22015-06-24 11:49:24 -0700153 dev_t before = GetDevice(mFuseWrite);
154
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800155 if (!(mFusePid = fork())) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700156 if (getMountFlags() & MountFlags::kPrimary) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700157 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800158 "-u", "1023", // AID_MEDIA_RW
159 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey20642ae2015-07-28 10:57:29 -0700160 "-U", std::to_string(getMountUserId()).c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700161 "-w",
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800162 mRawPath.c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700163 stableName.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700164 NULL)) {
165 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800166 }
167 } else {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700168 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800169 "-u", "1023", // AID_MEDIA_RW
170 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey20642ae2015-07-28 10:57:29 -0700171 "-U", std::to_string(getMountUserId()).c_str(),
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800172 mRawPath.c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700173 stableName.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700174 NULL)) {
175 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800176 }
177 }
178
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700179 LOG(ERROR) << "FUSE exiting";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800180 _exit(1);
181 }
182
183 if (mFusePid == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700184 PLOG(ERROR) << getId() << " failed to fork";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800185 return -errno;
186 }
187
Jeff Sharkey66270a22015-06-24 11:49:24 -0700188 while (before == GetDevice(mFuseWrite)) {
189 LOG(VERBOSE) << "Waiting for FUSE to spin up...";
190 usleep(50000); // 50ms
191 }
192
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800193 return OK;
194}
195
196status_t PublicVolume::doUnmount() {
197 if (mFusePid > 0) {
198 kill(mFusePid, SIGTERM);
199 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
200 mFusePid = 0;
201 }
202
Jeff Sharkey48d81d32015-04-12 21:50:32 -0700203 ForceUnmount(kAsecPath);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700204
205 ForceUnmount(mFuseDefault);
206 ForceUnmount(mFuseRead);
207 ForceUnmount(mFuseWrite);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800208 ForceUnmount(mRawPath);
209
Jeff Sharkey66270a22015-06-24 11:49:24 -0700210 rmdir(mFuseDefault.c_str());
211 rmdir(mFuseRead.c_str());
212 rmdir(mFuseWrite.c_str());
213 rmdir(mRawPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700214
Jeff Sharkey66270a22015-06-24 11:49:24 -0700215 mFuseDefault.clear();
216 mFuseRead.clear();
217 mFuseWrite.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700218 mRawPath.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800219
220 return OK;
221}
222
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700223status_t PublicVolume::doFormat(const std::string& fsType) {
224 if (fsType == "vfat" || fsType == "auto") {
225 if (WipeBlockDevice(mDevPath) != OK) {
226 LOG(WARNING) << getId() << " failed to wipe";
227 }
228 if (vfat::Format(mDevPath, 0)) {
229 LOG(ERROR) << getId() << " failed to format";
230 return -errno;
231 }
232 } else {
233 LOG(ERROR) << "Unsupported filesystem " << fsType;
234 return -EINVAL;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800235 }
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700236
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800237 return OK;
238}
239
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800240} // namespace vold
241} // namespace android