1 /*
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
17 #include "PublicVolume.h"
18 #include "Utils.h"
19 #include "VolumeManager.h"
20 #include "fs/Exfat.h"
21 #include "fs/Vfat.h"
22
23 #include <android-base/logging.h>
24 #include <android-base/properties.h>
25 #include <android-base/stringprintf.h>
26 #include <cutils/fs.h>
27 #include <private/android_filesystem_config.h>
28 #include <utils/Timers.h>
29
30 #include <fcntl.h>
31 #include <stdlib.h>
32 #include <sys/mount.h>
33 #include <sys/stat.h>
34 #include <sys/sysmacros.h>
35 #include <sys/types.h>
36 #include <sys/wait.h>
37
38 using android::base::GetBoolProperty;
39 using android::base::StringPrintf;
40
41 namespace android {
42 namespace vold {
43
44 static const char* kFusePath = "/system/bin/sdcard";
45
46 static const char* kAsecPath = "/mnt/secure/asec";
47
PublicVolume(dev_t device)48 PublicVolume::PublicVolume(dev_t device) : VolumeBase(Type::kPublic), mDevice(device), mFusePid(0) {
49 setId(StringPrintf("public:%u,%u", major(device), minor(device)));
50 mDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
51 }
52
~PublicVolume()53 PublicVolume::~PublicVolume() {}
54
readMetadata()55 status_t PublicVolume::readMetadata() {
56 status_t res = ReadMetadataUntrusted(mDevPath, &mFsType, &mFsUuid, &mFsLabel);
57
58 auto listener = getListener();
59 if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
60
61 return res;
62 }
63
initAsecStage()64 status_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) && access(securePath.c_str(), R_OK | X_OK)) {
70 if (rename(legacyPath.c_str(), securePath.c_str())) {
71 PLOG(WARNING) << getId() << " failed to rename legacy ASEC dir";
72 }
73 }
74
75 if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) {
76 if (errno != EEXIST) {
77 PLOG(WARNING) << getId() << " creating ASEC stage failed";
78 return -errno;
79 }
80 }
81
82 BindMount(securePath, kAsecPath);
83
84 return OK;
85 }
86
doCreate()87 status_t PublicVolume::doCreate() {
88 return CreateDeviceNode(mDevPath, mDevice);
89 }
90
doDestroy()91 status_t PublicVolume::doDestroy() {
92 return DestroyDeviceNode(mDevPath);
93 }
94
doMount()95 status_t PublicVolume::doMount() {
96 readMetadata();
97
98 if (mFsType == "vfat" && vfat::IsSupported()) {
99 if (vfat::Check(mDevPath)) {
100 LOG(ERROR) << getId() << " failed filesystem check";
101 return -EIO;
102 }
103 } else if (mFsType == "exfat" && exfat::IsSupported()) {
104 if (exfat::Check(mDevPath)) {
105 LOG(ERROR) << getId() << " failed filesystem check";
106 return -EIO;
107 }
108 } else {
109 LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
110 return -EIO;
111 }
112
113 // Use UUID as stable name, if available
114 std::string stableName = getId();
115 if (!mFsUuid.empty()) {
116 stableName = mFsUuid;
117 }
118
119 mRawPath = StringPrintf("/mnt/media_rw/%s", stableName.c_str());
120
121 mFuseDefault = StringPrintf("/mnt/runtime/default/%s", stableName.c_str());
122 mFuseRead = StringPrintf("/mnt/runtime/read/%s", stableName.c_str());
123 mFuseWrite = StringPrintf("/mnt/runtime/write/%s", stableName.c_str());
124 mFuseFull = StringPrintf("/mnt/runtime/full/%s", stableName.c_str());
125
126 setInternalPath(mRawPath);
127 if (getMountFlags() & MountFlags::kVisible) {
128 setPath(StringPrintf("/storage/%s", stableName.c_str()));
129 } else {
130 setPath(mRawPath);
131 }
132
133 if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
134 PLOG(ERROR) << getId() << " failed to create mount points";
135 return -errno;
136 }
137
138 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 }
149 }
150
151 if (getMountFlags() & MountFlags::kPrimary) {
152 initAsecStage();
153 }
154
155 if (!(getMountFlags() & MountFlags::kVisible)) {
156 // Not visible to apps, so no need to spin up FUSE
157 return OK;
158 }
159
160 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 fs_prepare_dir(mFuseFull.c_str(), 0700, AID_ROOT, AID_ROOT)) {
164 PLOG(ERROR) << getId() << " failed to create FUSE mount points";
165 return -errno;
166 }
167
168 dev_t before = GetDevice(mFuseFull);
169
170 if (!(mFusePid = fork())) {
171 if (getMountFlags() & MountFlags::kPrimary) {
172 // clang-format off
173 if (execl(kFusePath, kFusePath,
174 "-u", "1023", // AID_MEDIA_RW
175 "-g", "1023", // AID_MEDIA_RW
176 "-U", std::to_string(getMountUserId()).c_str(),
177 "-w",
178 mRawPath.c_str(),
179 stableName.c_str(),
180 NULL)) {
181 // clang-format on
182 PLOG(ERROR) << "Failed to exec";
183 }
184 } else {
185 // clang-format off
186 if (execl(kFusePath, kFusePath,
187 "-u", "1023", // AID_MEDIA_RW
188 "-g", "1023", // AID_MEDIA_RW
189 "-U", std::to_string(getMountUserId()).c_str(),
190 mRawPath.c_str(),
191 stableName.c_str(),
192 NULL)) {
193 // clang-format on
194 PLOG(ERROR) << "Failed to exec";
195 }
196 }
197
198 LOG(ERROR) << "FUSE exiting";
199 _exit(1);
200 }
201
202 if (mFusePid == -1) {
203 PLOG(ERROR) << getId() << " failed to fork";
204 return -errno;
205 }
206
207 nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
208 while (before == GetDevice(mFuseFull)) {
209 LOG(DEBUG) << "Waiting for FUSE to spin up...";
210 usleep(50000); // 50ms
211
212 nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
213 if (nanoseconds_to_milliseconds(now - start) > 5000) {
214 LOG(WARNING) << "Timed out while waiting for FUSE to spin up";
215 return -ETIMEDOUT;
216 }
217 }
218 /* sdcardfs will have exited already. FUSE will still be running */
219 TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
220 mFusePid = 0;
221
222 return OK;
223 }
224
doUnmount()225 status_t PublicVolume::doUnmount() {
226 // Unmount the storage before we kill the FUSE process. If we kill
227 // the FUSE process first, most file system operations will return
228 // ENOTCONN until the unmount completes. This is an exotic and unusual
229 // error code and might cause broken behaviour in applications.
230 KillProcessesUsingPath(getPath());
231
232 ForceUnmount(kAsecPath);
233
234 ForceUnmount(mFuseDefault);
235 ForceUnmount(mFuseRead);
236 ForceUnmount(mFuseWrite);
237 ForceUnmount(mFuseFull);
238 ForceUnmount(mRawPath);
239
240 rmdir(mFuseDefault.c_str());
241 rmdir(mFuseRead.c_str());
242 rmdir(mFuseWrite.c_str());
243 rmdir(mFuseFull.c_str());
244 rmdir(mRawPath.c_str());
245
246 mFuseDefault.clear();
247 mFuseRead.clear();
248 mFuseWrite.clear();
249 mFuseFull.clear();
250 mRawPath.clear();
251
252 return OK;
253 }
254
doFormat(const std::string & fsType)255 status_t PublicVolume::doFormat(const std::string& fsType) {
256 bool useVfat = vfat::IsSupported();
257 bool useExfat = exfat::IsSupported();
258 status_t res = OK;
259
260 // Resolve the target filesystem type
261 if (fsType == "auto" && useVfat && useExfat) {
262 uint64_t size = 0;
263
264 res = GetBlockDevSize(mDevPath, &size);
265 if (res != OK) {
266 LOG(ERROR) << "Couldn't get device size " << mDevPath;
267 return res;
268 }
269
270 // If both vfat & exfat are supported use exfat for SDXC (>~32GiB) cards
271 if (size > 32896LL * 1024 * 1024) {
272 useVfat = false;
273 } else {
274 useExfat = false;
275 }
276 } else if (fsType == "vfat") {
277 useExfat = false;
278 } else if (fsType == "exfat") {
279 useVfat = false;
280 }
281
282 if (!useVfat && !useExfat) {
283 LOG(ERROR) << "Unsupported filesystem " << fsType;
284 return -EINVAL;
285 }
286
287 if (WipeBlockDevice(mDevPath) != OK) {
288 LOG(WARNING) << getId() << " failed to wipe";
289 }
290
291 if (useVfat) {
292 res = vfat::Format(mDevPath, 0);
293 } else if (useExfat) {
294 res = exfat::Format(mDevPath);
295 }
296
297 if (res != OK) {
298 LOG(ERROR) << getId() << " failed to format";
299 res = -errno;
300 }
301
302 return res;
303 }
304
305 } // namespace vold
306 } // namespace android
307