blob: 569203c8f1196df136d1efdbed4f4a3930bca791 [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>
26
27#include <fcntl.h>
28#include <stdlib.h>
29#include <sys/mount.h>
30#include <sys/stat.h>
31#include <sys/types.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070032#include <sys/sysmacros.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080033#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 Sharkeycbe69fc2017-09-15 16:50:28 -060055
Jeff Sharkey814e9d32017-09-13 11:49:44 -060056 auto listener = getListener();
57 if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060058
Jeff Sharkey36801cc2015-03-13 16:09:20 -070059 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080060}
61
62status_t PublicVolume::initAsecStage() {
63 std::string legacyPath(mRawPath + "/android_secure");
64 std::string securePath(mRawPath + "/.android_secure");
65
66 // Recover legacy secure path
67 if (!access(legacyPath.c_str(), R_OK | X_OK)
68 && access(securePath.c_str(), R_OK | X_OK)) {
69 if (rename(legacyPath.c_str(), securePath.c_str())) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070070 PLOG(WARNING) << getId() << " failed to rename legacy ASEC dir";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080071 }
72 }
73
Jeff Sharkey36801cc2015-03-13 16:09:20 -070074 if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) {
75 if (errno != EEXIST) {
Jeff Sharkey9c484982015-03-31 10:35:33 -070076 PLOG(WARNING) << getId() << " creating ASEC stage failed";
Jeff Sharkey36801cc2015-03-13 16:09:20 -070077 return -errno;
78 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -080079 }
80
Jeff Sharkey36801cc2015-03-13 16:09:20 -070081 BindMount(securePath, kAsecPath);
82
Jeff Sharkeydeb24052015-03-02 21:01:40 -080083 return OK;
84}
85
Jeff Sharkey9c484982015-03-31 10:35:33 -070086status_t PublicVolume::doCreate() {
87 return CreateDeviceNode(mDevPath, mDevice);
88}
89
90status_t PublicVolume::doDestroy() {
91 return DestroyDeviceNode(mDevPath);
92}
93
Jeff Sharkeydeb24052015-03-02 21:01:40 -080094status_t PublicVolume::doMount() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070095 // TODO: expand to support mounting other filesystems
Jeff Sharkey9c484982015-03-31 10:35:33 -070096 readMetadata();
97
Makoto Onukic82c9ce2015-06-24 13:30:45 -070098 if (mFsType != "vfat") {
99 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
100 return -EIO;
101 }
102
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700103 if (vfat::Check(mDevPath)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700104 LOG(ERROR) << getId() << " failed filesystem check";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800105 return -EIO;
106 }
107
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700108 // Use UUID as stable name, if available
109 std::string stableName = getId();
110 if (!mFsUuid.empty()) {
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700111 stableName = mFsUuid;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700112 }
113
114 mRawPath = StringPrintf("/mnt/media_rw/%s", stableName.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700115
Jeff Sharkey1bd078f2015-08-06 11:40:00 -0700116 mFuseDefault = StringPrintf("/mnt/runtime/default/%s", stableName.c_str());
117 mFuseRead = StringPrintf("/mnt/runtime/read/%s", stableName.c_str());
118 mFuseWrite = StringPrintf("/mnt/runtime/write/%s", stableName.c_str());
Jeff Sharkey66270a22015-06-24 11:49:24 -0700119
120 setInternalPath(mRawPath);
Jeff Sharkey8474ee32015-07-30 16:54:23 -0700121 if (getMountFlags() & MountFlags::kVisible) {
122 setPath(StringPrintf("/storage/%s", stableName.c_str()));
123 } else {
124 setPath(mRawPath);
125 }
Jeff Sharkey66270a22015-06-24 11:49:24 -0700126
Qin Chaoe0074f12015-12-15 15:20:41 +0800127 if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
Jeff Sharkey66270a22015-06-24 11:49:24 -0700128 PLOG(ERROR) << getId() << " failed to create mount points";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800129 return -errno;
130 }
131
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700132 if (vfat::Mount(mDevPath, mRawPath, false, false, false,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800133 AID_MEDIA_RW, AID_MEDIA_RW, 0007, true)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700134 PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800135 return -EIO;
136 }
137
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700138 if (getMountFlags() & MountFlags::kPrimary) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700139 initAsecStage();
140 }
141
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700142 if (!(getMountFlags() & MountFlags::kVisible)) {
143 // Not visible to apps, so no need to spin up FUSE
144 return OK;
145 }
146
Qin Chaoe0074f12015-12-15 15:20:41 +0800147 if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
148 fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
149 fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT)) {
150 PLOG(ERROR) << getId() << " failed to create FUSE mount points";
151 return -errno;
152 }
153
Jeff Sharkey66270a22015-06-24 11:49:24 -0700154 dev_t before = GetDevice(mFuseWrite);
155
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800156 if (!(mFusePid = fork())) {
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700157 if (getMountFlags() & MountFlags::kPrimary) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700158 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800159 "-u", "1023", // AID_MEDIA_RW
160 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey20642ae2015-07-28 10:57:29 -0700161 "-U", std::to_string(getMountUserId()).c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700162 "-w",
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800163 mRawPath.c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700164 stableName.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700165 NULL)) {
166 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800167 }
168 } else {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700169 if (execl(kFusePath, kFusePath,
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800170 "-u", "1023", // AID_MEDIA_RW
171 "-g", "1023", // AID_MEDIA_RW
Jeff Sharkey20642ae2015-07-28 10:57:29 -0700172 "-U", std::to_string(getMountUserId()).c_str(),
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800173 mRawPath.c_str(),
Jeff Sharkey66270a22015-06-24 11:49:24 -0700174 stableName.c_str(),
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700175 NULL)) {
176 PLOG(ERROR) << "Failed to exec";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800177 }
178 }
179
Jeff Sharkeyc7b5b572015-06-30 15:54:17 -0700180 LOG(ERROR) << "FUSE exiting";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800181 _exit(1);
182 }
183
184 if (mFusePid == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700185 PLOG(ERROR) << getId() << " failed to fork";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800186 return -errno;
187 }
188
Jeff Sharkey66270a22015-06-24 11:49:24 -0700189 while (before == GetDevice(mFuseWrite)) {
190 LOG(VERBOSE) << "Waiting for FUSE to spin up...";
191 usleep(50000); // 50ms
192 }
Daniel Rosenberg1d79d102017-07-11 17:59:55 -0700193 /* sdcardfs will have exited already. FUSE will still be running */
194 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, WNOHANG));
Jeff Sharkey66270a22015-06-24 11:49:24 -0700195
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800196 return OK;
197}
198
199status_t PublicVolume::doUnmount() {
John Cormie25cc7e32016-04-18 14:23:29 -0700200 // Unmount the storage before we kill the FUSE process. If we kill
201 // the FUSE process first, most file system operations will return
202 // ENOTCONN until the unmount completes. This is an exotic and unusual
203 // error code and might cause broken behaviour in applications.
Jeff Sharkey8aff8542016-03-30 20:37:28 -0600204 KillProcessesUsingPath(getPath());
205
Jeff Sharkey48d81d32015-04-12 21:50:32 -0700206 ForceUnmount(kAsecPath);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700207
208 ForceUnmount(mFuseDefault);
209 ForceUnmount(mFuseRead);
210 ForceUnmount(mFuseWrite);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800211 ForceUnmount(mRawPath);
212
John Cormie25cc7e32016-04-18 14:23:29 -0700213 if (mFusePid > 0) {
214 kill(mFusePid, SIGTERM);
215 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
216 mFusePid = 0;
217 }
218
Jeff Sharkey66270a22015-06-24 11:49:24 -0700219 rmdir(mFuseDefault.c_str());
220 rmdir(mFuseRead.c_str());
221 rmdir(mFuseWrite.c_str());
222 rmdir(mRawPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700223
Jeff Sharkey66270a22015-06-24 11:49:24 -0700224 mFuseDefault.clear();
225 mFuseRead.clear();
226 mFuseWrite.clear();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700227 mRawPath.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800228
229 return OK;
230}
231
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700232status_t PublicVolume::doFormat(const std::string& fsType) {
233 if (fsType == "vfat" || fsType == "auto") {
234 if (WipeBlockDevice(mDevPath) != OK) {
235 LOG(WARNING) << getId() << " failed to wipe";
236 }
237 if (vfat::Format(mDevPath, 0)) {
238 LOG(ERROR) << getId() << " failed to format";
239 return -errno;
240 }
241 } else {
242 LOG(ERROR) << "Unsupported filesystem " << fsType;
243 return -EINVAL;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800244 }
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700245
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800246 return OK;
247}
248
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800249} // namespace vold
250} // namespace android